remote.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. #include "remote.h"
  2. #include "module_wrapper.h"
  3. #include "TypeDefine.h"
  4. #include "SystemAlarm.h"
  5. #include "string.h"
  6. #include "sys.h"
  7. #include "DrawLCDGUI.h"
  8. #include "ProgramData.h"
  9. #include "cJSON.h"
  10. #include "AES.h"
  11. #include <stdlib.h>
  12. #define REMOTE_CH 1
  13. uint8_t remote_flag = 0;
  14. extern uint8_t mcu_id[8]; //mcu ID号缓冲区
  15. extern uint16_t Version_data; //版本号
  16. extern unsigned char key[17];
  17. extern unsigned char expansionkey[15 * 16];
  18. static int remoteModuleStartCou = 0; //注册是模块启动次数
  19. static char remoteData[200] = {0};
  20. static void remoteModuleKeepLine(void)
  21. {
  22. if(module_start_status == MODULE_OK && module_init_status == MODULE_OK)
  23. {
  24. if(module_channel[REMOTE_CH].connect_status == TCP_CLOSED && tcp_connect_status == TCP_OPEN)
  25. {
  26. at_module_channel_open(REMOTE_SERVER,REMOTE_PORT,TCP,REMOTE_CH);
  27. }
  28. }
  29. }
  30. static void packRemotetDataAndSend(void)
  31. {
  32. int i=0;
  33. int datalen = 0;
  34. const char *method = "getRemoteConf";
  35. memset(remoteData,0, sizeof(remoteData));
  36. char* dataPtr = remoteData+2;
  37. memcpy(dataPtr,method,strlen(method));
  38. memcpy(dataPtr+strlen(dataPtr),"&",1);
  39. byteToHexStr(mcu_id, dataPtr+strlen(dataPtr), sizeof(mcu_id));
  40. datalen = ECBAesEncrypt((unsigned char*)dataPtr, expansionkey, 10); //2、AES 加密
  41. remoteData[0] = datalen>>8;
  42. remoteData[1] = datalen;
  43. at_module_send_ch(remoteData,datalen+2,REMOTE_CH,KEPP_RRC);
  44. }
  45. static void packRemotetDataJSONAndSend(void)
  46. {
  47. int i=0;
  48. int datalen = 0;
  49. uint8_t mcuid_str[20]={0};
  50. const char *method = "getRemoteConf";
  51. char* dataPtr = remoteData+2;
  52. byteToHexStr(mcu_id, (char*)mcuid_str, sizeof(mcu_id));
  53. cJSON *jsonData = cJSON_CreateObject();
  54. cJSON *jsonParam = cJSON_CreateObject();
  55. cJSON_AddStringToObject(jsonData,"method","getRemoteConf");
  56. cJSON_AddStringToObject(jsonParam,"devive",(char*)mcuid_str);
  57. cJSON_AddItemToObject(jsonData,"params",jsonParam);
  58. char *ptrs = cJSON_PrintUnformatted(jsonData);
  59. memcpy(dataPtr,ptrs,strlen(ptrs));
  60. UsartPrintf(USART_DEBUG, "craete json:%s \r\n",ptrs);
  61. datalen = ECBAesEncrypt((unsigned char*)dataPtr, expansionkey, 10);
  62. remoteData[0] = datalen>>8;
  63. remoteData[1] = datalen;
  64. at_module_send_ch(remoteData,datalen+2,REMOTE_CH,KEPP_RRC);
  65. end:
  66. cJSON_Delete(jsonData);
  67. free(ptrs);
  68. }
  69. //处理Coap注册信息,0: 成功, 1: 失败
  70. static int handleRemoteInfo(void)
  71. {
  72. int flag = 1;
  73. uint64_t hosnum = 0;
  74. //char str_mcu_id[20]={0};
  75. cJSON *json = NULL,
  76. *json_code = NULL,
  77. *json_type = NULL,
  78. *json_hosnum=NULL,
  79. *json_total=NULL,
  80. *json_limit = NULL,
  81. *json_add = NULL,
  82. *json_lock = NULL,
  83. *json_flow = NULL,
  84. *json_first = NULL;
  85. json = cJSON_Parse(module_channel[REMOTE_CH].valid_data.buf);
  86. if(!json)
  87. {
  88. UsartPrintf(USART_DEBUG, "json parse error,%s\r\n",cJSON_GetErrorPtr());
  89. goto end;
  90. }
  91. //UsartPrintf(USART_DEBUG, "json parse data,%s\r\n",module_channel[REMOTE_CH].valid_data.buf);
  92. json_code = cJSON_GetObjectItem(json,"code");
  93. json_type = cJSON_GetObjectItem(json,"type");
  94. json_hosnum = cJSON_GetObjectItem(json,"hosnum");
  95. json_total = cJSON_GetObjectItem(json,"total");
  96. json_limit = cJSON_GetObjectItem(json,"limit");
  97. json_add = cJSON_GetObjectItem(json,"add");
  98. json_flow = cJSON_GetObjectItem(json,"flow");
  99. json_first = cJSON_GetObjectItem(json,"first");
  100. UsartPrintf(USART_DEBUG, "json_code:%d\r\n",json_code->valueint);
  101. if(json_type && json_code && (json_code->valueint == 0))
  102. {
  103. if(json_type->valueint == 0x15)
  104. {
  105. if(json_hosnum)
  106. {
  107. UsartPrintf(USART_DEBUG, "json_hosnum:%d\r\n",json_hosnum->valuestring);
  108. hosnum = my_atoui64(json_hosnum->valuestring);
  109. verifyInfo.hospitalNO = hosnum;
  110. }
  111. if(json_total)
  112. {
  113. UsartPrintf(USART_DEBUG, "json_total:%d\r\n",json_total->valueint);
  114. setParamInfo.totalDose = json_total->valueint;
  115. }
  116. if(json_limit)
  117. {
  118. UsartPrintf(USART_DEBUG, "json_limit:%d\r\n",json_limit->valueint);
  119. setParamInfo.limitDose1=json_limit->valueint;
  120. }
  121. if(json_add)
  122. {
  123. UsartPrintf(USART_DEBUG, "json_add:%d\r\n",json_add->valueint);
  124. setParamInfo.superaddition1=json_add->valueint;
  125. }
  126. if(json_flow)
  127. {
  128. UsartPrintf(USART_DEBUG, "json_flow:%d\r\n",json_flow->valueint);
  129. setParamInfo.continueDose1 = json_flow->valueint;
  130. }
  131. if(json_first)
  132. {
  133. UsartPrintf(USART_DEBUG, "json_first:%d\r\n",json_first->valueint);
  134. setParamInfo.firstDose = json_first->valueint;
  135. }
  136. }
  137. }
  138. end:
  139. cJSON_Delete(json);
  140. return flag;
  141. }
  142. void remoteHandle()
  143. {
  144. static uint8_t send_flag = 1;
  145. static uint8_t remote_flag = 0; //远程配置结果, 0:未配置,1:已配置
  146. if(remote_flag) return;
  147. remoteModuleKeepLine();
  148. if(module_channel[REMOTE_CH].connect_status == TCP_OPEN && send_flag)
  149. {
  150. send_flag = 0;
  151. ScheduleKey(key, expansionkey, 4, 10); //1、密钥扩展生成
  152. //packRemotetDataAndSend();
  153. packRemotetDataJSONAndSend();
  154. }
  155. at_module_handle_event();
  156. if(module_channel[REMOTE_CH].valid_data.len>0)
  157. {
  158. if(module_channel[REMOTE_CH].valid_data.len%AES_BLOCK_SIZE == 0)
  159. {
  160. remote_flag = 1;
  161. ECBAesDecrypt((unsigned char*)module_channel[REMOTE_CH].valid_data.buf,module_channel[REMOTE_CH].valid_data.len, expansionkey, 10);//AES 解密
  162. UsartPrintf(USART_DEBUG, "AesDecrypt valid_data,%s\r\n",module_channel[REMOTE_CH].valid_data.buf);
  163. handleRemoteInfo();
  164. channel_data_clear(REMOTE_CH);
  165. }
  166. else
  167. {
  168. UsartPrintf(USART_DEBUG, "AesDecrypt valid_data length error\r\n");
  169. }
  170. at_module_channel_close(REMOTE_CH);
  171. }
  172. }