pando_device_login.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. #include "pando_device_login.h"
  2. #include "pando_storage_interface.h"
  3. #include "gateway_defs.h"
  4. #include "pando_sys.h"
  5. #include "pando_types.h"
  6. #include "converter.h"
  7. #include "jsonparse.h"
  8. #include "jsontree.h"
  9. #include "pando_json.h"
  10. #include "pando_net_http.h"
  11. #include "debug.h"
  12. #include "pando_gateway.h"
  13. #define MAX_BUF_LEN 256
  14. #define KEY_BUF_LEN 64
  15. #define MSG_BUF_LEN 32
  16. #define ACCESS_TOKEN_LEN 8
  17. static gateway_callback device_login_callback = NULL;
  18. static char * request = NULL;
  19. extern uint8_t pando_device_token[ACCESS_TOKEN_LEN];
  20. static void FUNCTION_ATTRIBUTE
  21. http_callback_login(char * response)
  22. {
  23. if(request != NULL)
  24. {
  25. pd_free(request);
  26. request = NULL;
  27. }
  28. if(response == NULL)
  29. {
  30. device_login_callback(PANDO_LOGIN_FAIL);
  31. return;
  32. }
  33. pd_printf("response=%s\n(end)\n", response);
  34. uint16_t response_len = pd_strlen(response) + 1;
  35. char* login_response = (char*)pd_malloc(response_len);
  36. pd_memset(login_response, 0, response_len);
  37. pd_memcpy(login_response, response, response_len);
  38. struct jsonparse_state json_state;
  39. jsonparse_setup(&json_state, login_response, pd_strlen(login_response));
  40. int code;
  41. char message[MSG_BUF_LEN];
  42. char access_token[ACCESS_TOKEN_LEN*2 + 16];
  43. char access_addr[KEY_BUF_LEN];
  44. access_token[ACCESS_TOKEN_LEN*2] = '\0';
  45. int type;
  46. while ((type = jsonparse_next(&json_state)) != 0)
  47. {
  48. if (type == JSON_TYPE_PAIR_NAME)
  49. {
  50. if(jsonparse_strcmp_value(&json_state, "code") == 0)
  51. {
  52. jsonparse_next(&json_state);
  53. jsonparse_next(&json_state);
  54. code = jsonparse_get_value_as_int(&json_state);
  55. }
  56. else if(jsonparse_strcmp_value(&json_state, "message") == 0)
  57. {
  58. jsonparse_next(&json_state);
  59. jsonparse_next(&json_state);
  60. jsonparse_copy_value(&json_state, message, MSG_BUF_LEN);
  61. }
  62. else if(jsonparse_strcmp_value(&json_state, "data") == 0)
  63. {
  64. while((type = jsonparse_next(&json_state)) != 0 && json_state.depth > 1)
  65. {
  66. if(type == JSON_TYPE_PAIR_NAME)
  67. {
  68. if(jsonparse_strcmp_value(&json_state, "access_token") == 0)
  69. {
  70. jsonparse_next(&json_state);
  71. jsonparse_next(&json_state);
  72. jsonparse_copy_value(&json_state, access_token, ACCESS_TOKEN_LEN*2 + 16);
  73. }
  74. else if(jsonparse_strcmp_value(&json_state, "access_addr") == 0)
  75. {
  76. jsonparse_next(&json_state);
  77. jsonparse_next(&json_state);
  78. jsonparse_copy_value(&json_state, access_addr, KEY_BUF_LEN);
  79. }
  80. }
  81. }
  82. }
  83. }
  84. }
  85. if(login_response != NULL)
  86. {
  87. pd_free(login_response);
  88. }
  89. if(code != 0)
  90. {
  91. pd_printf("device login failed: %s\n", message);
  92. if(device_login_callback != NULL)
  93. {
  94. device_login_callback(PANDO_LOGIN_FAIL);
  95. }
  96. return;
  97. }
  98. hex2bin(pando_device_token, access_token);
  99. pd_printf("device login success, access_addr : %s\n", access_addr);
  100. pando_data_set(DATANAME_ACCESS_ADDR, access_addr);
  101. pando_data_set(DATANAME_ACCESS_TOKEN, access_token);
  102. if(device_login_callback != NULL)
  103. {
  104. device_login_callback(PANDO_LOGIN_OK);
  105. }
  106. }
  107. /******************************************************************************
  108. * FunctionName : pando_device_login
  109. * Description : try login device using pando cloud device register api.
  110. * Parameters : the specify login callback function
  111. * Returns :
  112. *******************************************************************************/
  113. void FUNCTION_ATTRIBUTE
  114. pando_device_login(gateway_callback callback)
  115. {
  116. pd_printf("begin login device...\n");
  117. if(callback != NULL)
  118. {
  119. device_login_callback = callback;
  120. }
  121. char * str_device_id = NULL;
  122. char * str_device_secret = NULL;
  123. str_device_id = pando_data_get(DATANAME_DEVICE_ID);
  124. str_device_secret = pando_data_get(DATANAME_DEVICE_SECRET);
  125. if(str_device_id == NULL || str_device_secret == NULL)
  126. {
  127. // has not registered
  128. pd_printf("login failed ! device has not been registerd...\n");
  129. device_login_callback(PANDO_NOT_REGISTERED);
  130. return;
  131. }
  132. int device_id = atol(str_device_id);
  133. // try login via HTTP
  134. struct jsontree_int json_device_id = JSONTREE_INT(device_id);
  135. struct jsontree_string json_device_secret = JSONTREE_STRING(str_device_secret);
  136. struct jsontree_string json_protocol = JSONTREE_STRING("mqtt");
  137. JSONTREE_OBJECT_EXT(device_info,
  138. JSONTREE_PAIR("device_id", &json_device_id),
  139. JSONTREE_PAIR("device_secret", &json_device_secret),
  140. JSONTREE_PAIR("protocol", &json_protocol));
  141. request = (char *)pd_malloc(MAX_BUF_LEN);
  142. int ret = pando_json_print((struct jsontree_value*)(&device_info), request, MAX_BUF_LEN);
  143. pd_printf("device login request:::\n%s\n(end)\n", request);
  144. char post_url[128] = "";
  145. pd_sprintf(post_url, "%s/v1/devices/authentication", PANDO_API_URL);
  146. net_http_post(post_url, request, http_callback_login);
  147. if(request != NULL)
  148. {
  149. pd_free(request);
  150. request = NULL;
  151. }
  152. }