usart_server.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. #include "usart.h"
  2. #include "gateway_message.h"
  3. #include "string.h"
  4. #include "tcp_server.h"
  5. #include "timer.h"
  6. #include "updata.h"
  7. /* 上位机功能 */
  8. //储存上位机发送的config数据,并返回上位机操作结果
  9. void save_config_232(uint8_t* buf)
  10. {
  11. GATEWAY_PARAMS *get;
  12. get= get_gateway_config_params();
  13. char* saveData = mymalloc(SRAMEX,20 * 1024);//(RECV_BUF_SIZE);// 存储config数据 最大20K,暂定3K
  14. if(saveData == NULL)
  15. LogPrint(LOG_ERROR,__FILE__,__FUNCTION__,__LINE__,"recv buf malloc fail");
  16. memset(saveData,0,strlen(saveData));
  17. // 插入tcp_config标志位,后续不通过http获取默认配置数据
  18. sprintf(saveData, "tcp_config");
  19. sprintf(saveData + strlen(saveData),"%s",buf);
  20. // 删除当前文件,写入新文件
  21. DeleteDirFile("device.txt");
  22. write_file("device.txt",saveData,strlen(saveData));// 储存到flash 不用判断失败
  23. // 释放结构体
  24. myfree(SRAMEX, get->device_params->params->gateway_read_dlt645_command);
  25. get->device_params->params->gateway_read_dlt645_command = NULL;
  26. myfree(SRAMEX, get->device_params->params->gateway_read_modbus_command);
  27. get->device_params->params->gateway_read_modbus_command = NULL;
  28. myfree(SRAMEX, get->device_params->params->gateway_write_modbus_command);
  29. get->device_params->params->gateway_write_modbus_command = NULL;
  30. myfree(SRAMEX, get->device_params->params);
  31. get->device_params->params = NULL;
  32. myfree(SRAMEX, get->device_params);
  33. get->device_params = NULL;
  34. get = NULL;
  35. // 重新解析config数据
  36. addGatewayParams(saveData);
  37. myfree(SRAMEX, saveData);
  38. char* retMsg = mymalloc(SRAMEX,32);
  39. memset(retMsg, 0, strlen(retMsg));
  40. retMsg = "{\"write_config\":\"success\"}";
  41. USART_232_Send((uint8_t*)retMsg, strlen(retMsg));
  42. myfree(SRAMEX ,retMsg);
  43. }
  44. void read_device_params(char* device_params)
  45. {
  46. GATEWAY_PARAMS *get;
  47. get= get_gateway_config_params();
  48. DEVICE_PARAMS *current_device=get->device_params;
  49. sprintf(device_params, "{\"read_config\":\"success\",\"baudrate\":%d,\"checkBit\":%d,\"commandTopic\":\"%s\",\"dataBit\":%d,\
  50. \"deviceId\":\"%s\",\"host\":\"%s\",\"inboundTime\":%d,\"messageTopic\":\"%s\",\"port\":%d,\"stopBit\":%d,\"deviceList\":[",get->baudrate,
  51. get->checkBit,get->commandTopic,get->dataBits,get->deviceId,get->host,get->inboundTime,get->messageTopic,get->port,get->stopBit);
  52. while(current_device != NULL)
  53. {
  54. sprintf(device_params + strlen(device_params),"{\"protocol\":%d,\"bigLittleFormat\":%d,\"deviceId\":\"%s\",\"sensorData\":[",
  55. current_device->protocol,current_device->MDBbigLittleFormat,current_device->deviceID);
  56. GATEWAY_READ_DLT645_COMMAND *read_dlt645_command = current_device->params->gateway_read_dlt645_command;
  57. GATEWAY_READ_MODBUS_COMMAND *read_modbus_command = current_device->params->gateway_read_modbus_command;
  58. GATEWAY_WRITE_MODBUS_COMMAND *write_modbus_command = current_device->params->gateway_write_modbus_command;
  59. // dlt645 read
  60. while(read_dlt645_command != NULL)
  61. {
  62. sprintf(device_params + strlen(device_params),"{\"identifier645\":%d,\"identifier\":\"%s\",\"deviceID645\":\"%s\"},",
  63. read_dlt645_command->Identification,read_dlt645_command->keyword,read_dlt645_command->deviceID645);
  64. read_dlt645_command = read_dlt645_command->nextParams;
  65. }
  66. // modbus read
  67. while(read_modbus_command != NULL)
  68. {
  69. sprintf(device_params + strlen(device_params),"{\"rFunctionCode\":%d,\"registerAddress\":%d,\"slaveAddress\":%d,\"registerByteNum\":%d,\"identifier\":\"%s\",\"precise\":%d},",
  70. read_modbus_command->functionCode, read_modbus_command->registerAddress,read_modbus_command->slaveAddress,
  71. read_modbus_command->registerByteNum,read_modbus_command->keyword,read_modbus_command->decimalPoint);
  72. read_modbus_command = read_modbus_command->nextParams;
  73. }
  74. // modbus write
  75. sprintf(device_params + strlen(device_params)-1,"], \"commandData\":[");//sensorData:[
  76. while(write_modbus_command != NULL)
  77. {
  78. sprintf(device_params + strlen(device_params),"{\"registerAddress\":%d,\"slaveAddress\":%d,\"wFunctionCode\":%d,\"registerByteNum\":%d},",
  79. write_modbus_command->registerAddress,write_modbus_command->slaveAddress,write_modbus_command->functionCode,write_modbus_command->registerByteNum);
  80. write_modbus_command = write_modbus_command->nextParams;
  81. }
  82. sprintf(device_params + strlen(device_params)-1,"]},");// commandData:[
  83. current_device = current_device->nextDevice;
  84. }
  85. sprintf(device_params + strlen(device_params) -1 ,"]}");
  86. }
  87. void read_config_232(void)
  88. {
  89. GATEWAY_PARAMS *get;
  90. get= get_gateway_config_params();
  91. char* device_params = mymalloc(SRAMEX, 3 * 1024);
  92. memset(device_params,0,3 * 1024);
  93. if(get->device_params == NULL)
  94. {
  95. sprintf(device_params, "{\"read_config\":\"error\"}");
  96. USART_232_Send((uint8_t*)device_params, strlen(device_params));
  97. }
  98. else
  99. {
  100. read_device_params(device_params);
  101. USART_232_Send((uint8_t*)device_params, strlen(device_params));
  102. }
  103. myfree(SRAMEX, device_params);
  104. }
  105. // 设备嗅探
  106. void find_device_232(void)
  107. {
  108. char deviceId[50];// 发送设备名
  109. GATEWAY_PARAMS *get;
  110. get= get_gateway_config_params();
  111. if(get->device_params == NULL)
  112. {
  113. // sprintf(deviceId, "{\"find_device\":\"%s\"}", gatewayId);
  114. sprintf(deviceId, "{\"find_device\":\"error\"}");
  115. USART_232_Send((uint8_t*)deviceId, strlen(deviceId));
  116. }
  117. else
  118. {
  119. sprintf(deviceId, "{\"find_device\":\"%s\"}", get->deviceId);
  120. USART_232_Send((uint8_t*)deviceId, strlen(deviceId));
  121. }
  122. memset(deviceId, 0, 50);
  123. }
  124. // 储存上位机发送的config_add数据,并返回上位机操作结果
  125. void add_config_232(uint8_t* dataBuf)
  126. {
  127. GATEWAY_PARAMS *get;
  128. get= get_gateway_config_params();
  129. DEVICE_PARAMS *device=get->device_params;
  130. char* retMsg = mymalloc(SRAMEX,32);
  131. memset(retMsg, 0, strlen(retMsg));
  132. while(device != NULL)// 一直轮询到当前为NULL
  133. {
  134. device = device->nextDevice;
  135. }
  136. addDevice((char*)dataBuf);
  137. // 再检查更新后的deviceId是否为NULL
  138. if(device == NULL)// error
  139. {
  140. retMsg = "{\"write_config\":\"error\"}";
  141. USART_232_Send((uint8_t*)retMsg, strlen(retMsg));
  142. }
  143. else// success
  144. {
  145. retMsg = "{\"write_config\":\"success\"}";
  146. USART_232_Send((uint8_t*)retMsg, strlen(retMsg));
  147. }
  148. myfree(SRAMEX, retMsg);
  149. }
  150. // 切换工作模式
  151. void work_mode_232(uint8_t* buf)
  152. {
  153. /* 用标志位开关data_task任务中,发送数据的方式 */
  154. // 内置协议模式
  155. if(strstr((char*)buf,"protocolsMode") != NULL)
  156. {
  157. ProtocolsModeFlag = 1;// 开启内置协议模式
  158. TransparentModeFlag = 0; // 关闭透明传输模式
  159. LogPrint(LOG_INFO,__FILE__,__FUNCTION__,__LINE__,"ProtocolsMode");
  160. }
  161. // 透明传输模式
  162. if(strstr((char*)buf,"TransparentMode") != NULL)
  163. {
  164. ProtocolsModeFlag = 0;// 关闭内置协议模式
  165. TransparentModeFlag = 1; // 开启透明传输模式
  166. LogPrint(LOG_INFO,__FILE__,__FUNCTION__,__LINE__,"TransparentModeFlag");
  167. }
  168. }
  169. // 更新OTA数据
  170. void updata_232(void)
  171. {
  172. int i;
  173. uint16_t crc;
  174. uint8_t databuf[1024];// 保存包数据
  175. uint16_t packId = 0x01;
  176. uint8_t stx = 0x02,eot = 0x04,ack = 0x06,nak = 0x15,C = 0x43;
  177. // 向上位机发送就绪信号
  178. char* retMsg = mymalloc(SRAMEX,32);
  179. memset(retMsg, 0, strlen(retMsg));
  180. retMsg = "{\"software_update\":\"ready\"}";
  181. USART_232_Send((uint8_t*)retMsg, strlen(retMsg));
  182. myfree(SRAMEX, retMsg);
  183. memset(rx_232_Buffer, 0, strlen((char*)rx_232_Buffer));
  184. delay_ms(300);
  185. // 发送第一个C
  186. USART_232_Send(&C, 1);
  187. delay_ms(300);
  188. __start:
  189. i = 0;
  190. //等待接收到消息,最长等待3S
  191. while(recv_232_done == 0 && i < 3000)
  192. {
  193. delay_ms(1);
  194. i++;
  195. }
  196. if(recv_232_done == 0) USART_232_Send(&C, 1);//如果还没接收到消息,再发送一个C
  197. do{
  198. // 接收到结束信号,反馈ack
  199. if(rx_232_Buffer[0] == eot)
  200. {
  201. USART_232_Send(&ack, 1);
  202. delay_ms(300);
  203. break;
  204. }
  205. // 检测头标志
  206. if(rx_232_Buffer[0] != stx)
  207. {
  208. USART_232_Send(&nak, 1);
  209. delay_ms(300);
  210. goto __start;
  211. }
  212. // 检测包序列
  213. if(rx_232_Buffer[1] != packId)
  214. {
  215. USART_232_Send(&nak, 1);
  216. delay_ms(300);
  217. goto __start;
  218. }
  219. // 检测包号反码
  220. if(rx_232_Buffer[2] !=(~packId & 0xff))
  221. {
  222. USART_232_Send(&nak, 1);
  223. delay_ms(300);
  224. goto __start;
  225. }
  226. // 提取数据包
  227. for(int i = 0; i < 1024; i++)
  228. {
  229. databuf[i] = rx_232_Buffer[3 + i];
  230. }
  231. crc = Checkcrc16(databuf, 1024);
  232. // 检测数据包的校验码
  233. if (((crc & 0x00FF) != rx_232_Buffer[1028]) || (((crc & 0xFF00) >> 8) != rx_232_Buffer[1027]))
  234. {
  235. USART_232_Send(&nak, 1);
  236. delay_ms(300);
  237. goto __start;
  238. }
  239. // 对数据包进行操作
  240. /* */
  241. memset(databuf, 0, sizeof(databuf));
  242. // 准备接收下一包数据
  243. USART_232_Send(&ack, 1);
  244. delay_ms(300);
  245. packId += 1;
  246. }while(recv_232_done);
  247. // 所有数据包接受完成,准备重启
  248. __set_PRIMASK(1);
  249. NVIC_SystemReset();
  250. }
  251. void USART_232_task(void const * argument)
  252. {
  253. int j;
  254. char* recv_cmd[] = {"\"cmd\":\"write_config\"","\"cmd\":\"write_config_add\"",
  255. "\"cmd\":\"read_config\"","\"cmd\":\"find_device\"",
  256. "\"cmd\":\"ip_config\"","\"cmd\":\"toggle_work_mode\"",
  257. "\"cmd\":\"software_update\"","\"cmd\":\"reboot\"",
  258. "\"cmd\":\"cmd error\"" };
  259. while(1)
  260. {
  261. j = 0;
  262. for(int i = 0; i < sizeof(recv_cmd)/sizeof(recv_cmd[0]); i++)
  263. {
  264. if(strstr((char*)rx_232_Buffer,recv_cmd[i]) != NULL)
  265. {
  266. i = sizeof(recv_cmd)/sizeof(recv_cmd[0]);
  267. }
  268. j++;
  269. }
  270. if (ProtocolsModeFlag) {
  271. switch (j) {
  272. case WRITE_CONFIG:
  273. while(!recv_232_done) vTaskDelay(100);// 等待串口接收完成
  274. save_config_232(rx_232_Buffer);
  275. LogPrint(LOG_INFO, __FILE__, __FUNCTION__, __LINE__, "write config");
  276. // __set_PRIMASK(1);
  277. // NVIC_SystemReset();
  278. break;
  279. case WRITE_CONFIG_ADD:
  280. add_config_232(rx_232_Buffer);
  281. LogPrint(LOG_INFO, __FILE__, __FUNCTION__, __LINE__, "write config add");
  282. break;
  283. case READ_CONFIG:
  284. read_config_232();
  285. LogPrint(LOG_INFO, __FILE__, __FUNCTION__, __LINE__, "read config");
  286. break;
  287. case FIND_DEVICE:
  288. find_device_232();
  289. LogPrint(LOG_INFO, __FILE__, __FUNCTION__, __LINE__, "find device");
  290. break;
  291. case TOGGLE_MODE:
  292. work_mode_232(rx_232_Buffer);
  293. LogPrint(LOG_INFO, __FILE__, __FUNCTION__, __LINE__, "toggle work mode");
  294. break;
  295. case UPDATE:
  296. LogPrint(LOG_INFO, __FILE__, __FUNCTION__, __LINE__, "update");
  297. updata_232();
  298. break;
  299. case REBOOT:
  300. LogPrint(LOG_INFO, __FILE__, __FUNCTION__, __LINE__, "reboot");
  301. __set_PRIMASK(1);
  302. NVIC_SystemReset();
  303. break;
  304. case CMD_ERROR:
  305. goto __exit;
  306. }
  307. } else {
  308. switch (j) {
  309. case FIND_DEVICE:
  310. find_device_232();
  311. LogPrint(LOG_INFO, __FILE__, __FUNCTION__, __LINE__, "find device");
  312. break;
  313. case TOGGLE_MODE:
  314. work_mode_232(rx_232_Buffer);
  315. LogPrint(LOG_INFO, __FILE__, __FUNCTION__, __LINE__, "toggle work mode");
  316. break;
  317. case UPDATE:
  318. updata_232();
  319. LogPrint(LOG_INFO, __FILE__, __FUNCTION__, __LINE__, "update");
  320. break;
  321. case REBOOT:
  322. LogPrint(LOG_INFO, __FILE__, __FUNCTION__, __LINE__, "reboot");
  323. __set_PRIMASK(1);
  324. NVIC_SystemReset();
  325. break;
  326. case CMD_ERROR:
  327. goto __exit;
  328. }
  329. }
  330. memset(rx_232_Buffer, 0, strlen((char*)rx_232_Buffer));
  331. __exit:
  332. vTaskDelay(500);
  333. }
  334. }
  335. void USART_232_task_creat()
  336. {
  337. osThreadDef(USART_task, USART_232_task, osPriorityNormal, 0, configMINIMAL_STACK_SIZE*4);
  338. osThreadCreate(osThread(USART_task), NULL);
  339. }