remote.c 5.3 KB

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