Tuoreniot.c 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #include "stm32f10x.h"
  2. #include <stdio.h>
  3. #include <stdarg.h>
  4. #include <string.h>
  5. #include <stdlib.h>
  6. #include "Tuoreniot.h"
  7. #include "mbedtls_util.h"
  8. #include "Log_Module.h"
  9. #include "At_Module.h"
  10. #include "Common_Util.h"
  11. #include "ec800m.h"
  12. #include "cJSON.h"
  13. unsigned char key[17]="tuorenzhinenghua";
  14. /**
  15. *
  16. *注册参数信息打包
  17. */
  18. void packRegistParams(uint8_t * pack_data, uint16_t * pack_data_length, struct Regist_Params_Struct registParams)
  19. {
  20. sprintf((char *)(pack_data + 2), "{\"platform\":\"%s\",\"connectionType\":\"%s\",\"deviceId\":\"%s\"}",
  21. registParams.platform, registParams.connType, registParams.deviceId); // 拼接AT指令
  22. Log_Printf_Debug("%s\r\n", (char *)(pack_data + 2));
  23. uint16_t datalen = utils_aes128_ECB_base64_enc_with_length((char *)key, (pack_data + 2));
  24. pack_data[0] = datalen>>8;//头2个字节赋值数据长度
  25. pack_data[1] = datalen;
  26. * pack_data_length = datalen + 2;
  27. }
  28. /**
  29. *
  30. *注册数据解密
  31. */
  32. uint8_t analysisRegistData(uint8_t * regist_data, uint16_t regist_data_length, struct Regist_Params_Struct * regist_data_struct)
  33. {
  34. Log_Printf_Debug("regist_data: \r\n");
  35. Log_SendHex(regist_data, regist_data_length);
  36. Log_Printf_Debug("\r\n");
  37. uint8_t result;
  38. utils_aes128_ECB_base64_dec((char *)key,regist_data,regist_data_length);
  39. Log_Printf_Debug("解密数据(%d):%s\r\n", regist_data_length, (char *)regist_data);
  40. cJSON *json = cJSON_Parse((char *)regist_data);
  41. if(!json)
  42. {
  43. Log_Printf_Debug("json parse error,%s\r\n",cJSON_GetErrorPtr());
  44. result = 0;
  45. }
  46. else if(cJSON_GetObjectItem(json, "code")->valueint == 200)
  47. {
  48. strcpy(regist_data_struct->deviceSecret, cJSON_GetObjectItem(json, "deviceSecret")->valuestring);
  49. strcpy(regist_data_struct->productKey, cJSON_GetObjectItem(json, "productKey")->valuestring);
  50. strcpy(regist_data_struct->deviceName, cJSON_GetObjectItem(json, "deviceName")->valuestring);
  51. result = 1;
  52. }
  53. else
  54. {
  55. result = 0;
  56. }
  57. cJSON_Delete(json);
  58. return result;
  59. }
  60. void printf_regist_param_struct(struct Regist_Params_Struct regist_params_struct)
  61. {
  62. Log_Printf("------print regist params start------\r\n");
  63. Log_Printf("connType=%s\r\n", regist_params_struct.connType);
  64. Log_Printf("deviceId=%s\r\n", regist_params_struct.deviceId);
  65. Log_Printf("deviceName=%s\r\n", regist_params_struct.deviceName);
  66. Log_Printf("platform=%s\r\n", regist_params_struct.platform);
  67. Log_Printf("deviceSecret=%s\r\n", regist_params_struct.deviceSecret);
  68. Log_Printf("Host=%s\r\n", regist_params_struct.host);
  69. Log_Printf("port=%d\r\n", regist_params_struct.port);
  70. Log_Printf("productKey=%s\r\n", regist_params_struct.productKey);
  71. Log_Printf("userId=%d\r\n", regist_params_struct.userId);
  72. Log_Printf("------print regist params end------\r\n");
  73. }