tcp_server.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. #include "lwip/opt.h"
  2. #include "lwip/sys.h"
  3. #include "tcp_server.h"
  4. #include <string.h>
  5. #include <stdio.h>
  6. #include "gd32f30x.h"
  7. #include "main.h"
  8. #include "lwip/tcp.h"
  9. #include "lwip/memp.h"
  10. #include "lwip/api.h"
  11. #include "sockets.h"
  12. #include "log.h"
  13. #include "netconf.h"
  14. #include "gateway_message.h"
  15. #include "gd32_flash.h"
  16. #include "stdlib.h"
  17. #include "protocol.h"
  18. #include "delay.h"
  19. #include "sys_mqtt.h"
  20. #include "updata.h"
  21. uint8_t closeFlag = 0; // DHCP关闭标志位
  22. uint8_t tcp_config = 0; // 存储config标志位
  23. uint8_t ProtocolsModeFlag = 1;// 开启内置协议模式
  24. uint8_t TransparentModeFlag = 0; // 表示用串口透传 将数据原封不动的发给客户端
  25. ip_config load_ip_config = {0};
  26. ip_config *get_ip_config()
  27. {
  28. return &load_ip_config;
  29. }
  30. // 解析设备当前的硬件信息(结构体内的数据)
  31. void get_device_params(char* device_params)
  32. {
  33. GATEWAY_PARAMS *get;
  34. get= get_gateway_config_params();
  35. DEVICE_PARAMS *current_device=get->device_params;
  36. sprintf(device_params, "{\"read_config\":\"success\",\"baudrate\":%d,\"checkBit\":%d,\"commandTopic\":%s,\"dataBit\":%d,\
  37. \"deviceId\": \"%s\",\"host\":\"%s\",\"inboundTime\":%d,\"messageTopic\":\"%s\",\"port\":%d,\"stopBit\":%d,\"deviceList\":[",get->baudrate,
  38. get->checkBit,get->commandTopic,get->dataBits,get->deviceId,get->host,get->inboundTime,get->messageTopic,get->port,get->stopBit);
  39. while(current_device != NULL)
  40. {
  41. sprintf(device_params + strlen(device_params),"{\"protocol\":%d,\"bigLittleFormat\":%d,\"deviceId\":\"%s\",\"sensorData\":[",
  42. current_device->protocol,current_device->MDBbigLittleFormat,current_device->deviceID);
  43. GATEWAY_READ_DLT645_COMMAND *read_dlt645_command = current_device->params->gateway_read_dlt645_command;
  44. GATEWAY_READ_MODBUS_COMMAND *read_modbus_command = current_device->params->gateway_read_modbus_command;
  45. GATEWAY_WRITE_MODBUS_COMMAND *write_modbus_command = current_device->params->gateway_write_modbus_command;
  46. // dlt645 read
  47. while(read_dlt645_command != NULL)
  48. {
  49. sprintf(device_params + strlen(device_params),"{\"identifier645\":%d,\"identifier\":\"%s\",\"deviceID645\":\"%s\"},",
  50. read_dlt645_command->Identification,read_dlt645_command->keyword,read_dlt645_command->deviceID645);
  51. read_dlt645_command = read_dlt645_command->nextParams;
  52. }
  53. // modbus read
  54. while(read_modbus_command != NULL)
  55. {
  56. sprintf(device_params + strlen(device_params),"{\"rFunctionCode\":%d,\"registerAddress\":%d,\"slaveAddress\":%d,\"registerByteNum\":%d\"identifier\":\"%s\",\"precise\":%d},",
  57. read_modbus_command->functionCode, read_modbus_command->registerAddress,read_modbus_command->slaveAddress,
  58. read_modbus_command->registerByteNum,read_modbus_command->keyword,read_modbus_command->decimalPoint);
  59. read_modbus_command = read_modbus_command->nextParams;
  60. }
  61. // modbus write
  62. sprintf(device_params + strlen(device_params)-1,"], \"commandData\":[");//sensorData:[
  63. while(write_modbus_command != NULL)
  64. {
  65. sprintf(device_params + strlen(device_params),"{\"registerAddress\":%d,\"slaveAddress\":%d,\"wFunctionCode\":%d,\"registerByteNum\":%d},",
  66. write_modbus_command->registerAddress,write_modbus_command->slaveAddress,write_modbus_command->functionCode,write_modbus_command->registerByteNum);
  67. write_modbus_command = write_modbus_command->nextParams;
  68. }
  69. sprintf(device_params + strlen(device_params)-1,"]}");// commandData:[
  70. current_device = current_device->nextDevice;
  71. }
  72. sprintf(device_params + strlen(device_params),"]}");
  73. }
  74. // 储存上位机发送的config数据,并返回上位机操作结果
  75. void save_config(int client_socket,char* dataBuf)
  76. {
  77. save_config_params(dataBuf);// 储存到flash 不用判断失败
  78. char* retMsg = pvPortMalloc(32);
  79. memset(retMsg, 0, strlen(retMsg));
  80. retMsg = "{\"write_config\":\"success\"}";
  81. send(client_socket, retMsg, strlen(retMsg), 0);
  82. vPortFree(retMsg);
  83. tcp_config = 1;
  84. }
  85. // 储存上位机发送的config_add数据,并返回上位机操作结果
  86. void add_config(int client_socket, char* dataBuf)
  87. {
  88. GATEWAY_PARAMS *get;
  89. get= get_gateway_config_params();
  90. DEVICE_PARAMS *device=get->device_params;
  91. char* retMsg = pvPortMalloc(32);
  92. memset(retMsg, 0, strlen(retMsg));
  93. while(device != NULL)// 一直轮询到当前为NULL
  94. {
  95. device = device->nextDevice;
  96. }
  97. addDevice(dataBuf);
  98. // 再检查更新后的deviceId是否为NULL
  99. if(device == NULL)// error
  100. {
  101. retMsg = "{\"write_config\":\"error\"}";
  102. send(client_socket, retMsg, strlen(retMsg), 0);
  103. }
  104. else// success
  105. {
  106. retMsg = "{\"write_config\":\"success\"}";
  107. send(client_socket, retMsg, strlen(retMsg), 0);
  108. }
  109. vPortFree(retMsg);
  110. }
  111. // 设备嗅探
  112. void find_device(int client_socket)
  113. {
  114. char deviceId[50];// 发送设备名
  115. GATEWAY_PARAMS *get;
  116. get= get_gateway_config_params();
  117. if(get->device_params == NULL)
  118. {
  119. sprintf(deviceId, "{\"find_device\":\"%s\"}", gatewayId);
  120. send(client_socket, deviceId, strlen(deviceId), 0);
  121. }
  122. else
  123. {
  124. sprintf(deviceId, "{\"find_device\":\"%s\"}", get->deviceId);
  125. send(client_socket, deviceId, strlen(deviceId), 0);
  126. }
  127. memset(deviceId, 0, 50);
  128. }
  129. // 发送设备当前的config数据
  130. void send_config(int client_socket)
  131. {
  132. GATEWAY_PARAMS *get;
  133. get= get_gateway_config_params();
  134. char* device_params = pvPortMalloc(3 * 1024);
  135. memset(device_params,0,3 * 1024);
  136. if(get->device_params == NULL)
  137. {
  138. sprintf(device_params, "{\"read_config\":\"error\"}");
  139. send(client_socket, device_params, strlen(device_params), 0);
  140. }
  141. else
  142. {
  143. get_device_params(device_params);
  144. send(client_socket, device_params, strlen(device_params), 0);
  145. }
  146. vPortFree(device_params);
  147. }
  148. // 解析上位机发送的ip_config数据
  149. void analysis_ip_config(int client_socket, int sockfd,struct sockaddr_in tcpServerSock, char* buf)
  150. {
  151. // 打开DHCP
  152. if (strstr(buf, "\"dhcpMode\":\"open\"") != NULL)
  153. {
  154. DHCP_open();
  155. char* retMsg = pvPortMalloc(32);
  156. retMsg = "{\"ip_config\":\"success\"}";
  157. send(client_socket,retMsg,strlen(retMsg),0);
  158. vPortFree(retMsg);
  159. }
  160. // 关闭DHCP
  161. else if (strstr(buf, "\"dhcpMode\":\"close\"") != NULL)
  162. {
  163. uint8_t prot[6];
  164. // 设置静态IP地址,并关闭DHCP
  165. set_ipaddr(buf);
  166. // 解析buf内的数据,保存到load_ip_config结构体
  167. parseStringField(buf, "\"ipv4\":\"", (char *)&load_ip_config.host);// eg:192.168.1.100
  168. parseStringField(buf, "\"subnetMask\":\"", (char *)&load_ip_config.subnetMask);// eg:255.255.255.0
  169. parseStringField(buf, "\"defaultGateway\":\"", (char *)&load_ip_config.defaultGateway);// eg:192.168.1.1
  170. parseStringField(buf, "\"udpLogPort\":\"", (char *)&prot);
  171. load_ip_config.udpLogPort = atoi((char *)&prot);
  172. // 关闭服务端和客户端
  173. lwip_close(sockfd);
  174. lwip_close(client_socket);
  175. // DHCP关闭标志位置 1
  176. closeFlag = 1;
  177. }
  178. }
  179. // 切换工作模式
  180. void work_mode(char* buf)
  181. {
  182. /* 用标志位开关data_task任务中,发送数据的方式 */
  183. // 内置协议模式
  184. if(strstr(buf,"protocolsMode") != NULL)
  185. {
  186. ProtocolsModeFlag = 1;// 开启内置协议模式
  187. TransparentModeFlag = 0; // 关闭透明传输模式
  188. LogPrint(LOG_INFO,__FILE__,__FUNCTION__,__LINE__,"ProtocolsMode");
  189. }
  190. // 透明传输模式
  191. if(strstr(buf,"TransparentMode") != NULL)
  192. {
  193. ProtocolsModeFlag = 0;// 关闭内置协议模式
  194. TransparentModeFlag = 1; // 开启透明传输模式
  195. LogPrint(LOG_INFO,__FILE__,__FUNCTION__,__LINE__,"TransparentModeFlag");
  196. }
  197. }
  198. #define RECV_BUF_SIZE 3 * 1024
  199. void tcp_server_task(void *pvParameters)
  200. {
  201. int ret,sockfd;
  202. int recv_size;
  203. struct sockaddr_in tcpServerSock;
  204. struct sockaddr_in client_sock;
  205. // 命令集
  206. char* recv_cmd[] = {"\"cmd\":\"write_config\"","\"cmd\":\"write_config_add\"",
  207. "\"cmd\":\"read_config\"","\"cmd\":\"find_device\"",
  208. "\"cmd\":\"ip_config\"","\"cmd\":\"toggle_work_mode\"",
  209. "\"cmd\":\"software_update\"","\"cmd\":\"reboot\"",
  210. "\"cmd\":\"error_cmd\""};// cmd:error_cmd 仅为了判断错误的命令
  211. while(dhcp_done!=1)
  212. {
  213. vTaskDelay(100);
  214. }
  215. tcpServerSock.sin_family = AF_INET;
  216. inet_aton("192.168.0.100",&(tcpServerSock.sin_addr));
  217. // tcpServerSock.sin_addr.s_addr = htonl(IPADDR_ANY);
  218. tcpServerSock.sin_port = htons(8080);
  219. tcp_server_begin:
  220. sockfd = socket(AF_INET, SOCK_STREAM, 0);
  221. if (sockfd < 0)
  222. {
  223. goto tcp_server_begin;
  224. }
  225. ret = bind(sockfd, (struct sockaddr *)&tcpServerSock, sizeof(tcpServerSock));
  226. if (ret < 0)
  227. {
  228. lwip_close(sockfd);
  229. sockfd = -1;
  230. goto tcp_server_begin;
  231. }
  232. ret = listen(sockfd, 10);
  233. if (ret < 0)
  234. {
  235. lwip_close(sockfd);
  236. sockfd = -1;
  237. goto tcp_server_begin;
  238. }
  239. socklen_t len = sizeof(client_sock);
  240. int client_socket = accept(sockfd, (struct sockaddr*)&client_sock,&len);
  241. if (client_socket<0)
  242. {
  243. printf("error");
  244. }
  245. LogPrint(LOG_INFO,__FILE__,__FUNCTION__,__LINE__,"PC connect success");
  246. while (1)
  247. {
  248. vTaskDelay(100);
  249. char* dataBuf = pvPortMalloc(RECV_BUF_SIZE);// 存储config数据 最大20K,暂定3K
  250. if(dataBuf == NULL)
  251. LogPrint(LOG_ERROR,__FILE__,__FUNCTION__,__LINE__,"recv buf malloc fail");
  252. memset(dataBuf,0,strlen(dataBuf));
  253. // 获取上位机发送的数据
  254. recv_size=recv(client_socket,dataBuf,RECV_BUF_SIZE,0);
  255. // 插入tcp_config标志位,后续不通过http获取默认配置数据
  256. sprintf(dataBuf + strlen(dataBuf),"tcp_config");
  257. // 接收到消息
  258. // 解析上位机发送的CMD命令
  259. if(recv_size>0)
  260. {
  261. int j = 0;
  262. for(int i = 0; i < sizeof(recv_cmd)/sizeof(recv_cmd[0]); i++)
  263. {
  264. if(strstr(dataBuf,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. save_config(client_socket, dataBuf);
  274. vPortFree(dataBuf);
  275. LogPrint(LOG_INFO, __FILE__, __FUNCTION__, __LINE__, "write config");
  276. break;
  277. case WRITE_CONFIG_ADD:
  278. add_config(client_socket, dataBuf);
  279. vPortFree(dataBuf);
  280. LogPrint(LOG_INFO, __FILE__, __FUNCTION__, __LINE__, "write config add");
  281. break;
  282. case READ_CONFIG:
  283. send_config(client_socket);
  284. vPortFree(dataBuf);
  285. LogPrint(LOG_INFO, __FILE__, __FUNCTION__, __LINE__, "read config");
  286. break;
  287. case FIND_DEVICE:
  288. find_device(client_socket);
  289. vPortFree(dataBuf);
  290. LogPrint(LOG_INFO, __FILE__, __FUNCTION__, __LINE__, "find device");
  291. break;
  292. case IP_CONFIG:
  293. LogPrint(LOG_INFO, __FILE__, __FUNCTION__, __LINE__, "ipconfig");
  294. analysis_ip_config(client_socket, sockfd, tcpServerSock, dataBuf);
  295. if (closeFlag == 1) {
  296. // 更新新的地址
  297. inet_aton((char*)&load_ip_config.host, &(tcpServerSock.sin_addr));
  298. closeFlag = 0;
  299. vPortFree(dataBuf);
  300. goto tcp_server_begin;
  301. }
  302. vPortFree(dataBuf);
  303. break;
  304. case TOGGLE_MODE:
  305. work_mode(dataBuf);
  306. vPortFree(dataBuf);
  307. LogPrint(LOG_INFO, __FILE__, __FUNCTION__, __LINE__, "toggle work mode");
  308. break;
  309. case UPDATE:
  310. LogPrint(LOG_INFO, __FILE__, __FUNCTION__, __LINE__, "update");
  311. vPortFree(dataBuf);
  312. updata_task_creat(client_socket);
  313. break;
  314. case REBOOT:
  315. send(client_socket,"{\"reboot\":\"success\"}", 20, 0);
  316. LogPrint(LOG_INFO, __FILE__, __FUNCTION__, __LINE__, "reboot");
  317. __set_PRIMASK(1);
  318. NVIC_SystemReset();
  319. break;
  320. default:
  321. send(client_socket,"{\"cmd:error\"}", 15, 0);
  322. LogPrint(LOG_INFO, __FILE__, __FUNCTION__, __LINE__, "Command error");
  323. }
  324. } else {
  325. switch (j) {
  326. case FIND_DEVICE:
  327. find_device(client_socket);
  328. vPortFree(dataBuf);
  329. LogPrint(LOG_INFO, __FILE__, __FUNCTION__, __LINE__, "find device");
  330. break;
  331. case IP_CONFIG:
  332. LogPrint(LOG_INFO, __FILE__, __FUNCTION__, __LINE__, "ip config");
  333. analysis_ip_config(client_socket, sockfd, tcpServerSock, dataBuf);
  334. if (closeFlag == 1) {
  335. // 更新新的地址
  336. inet_aton((char*)&load_ip_config.host, &(tcpServerSock.sin_addr));
  337. closeFlag = 0;
  338. vPortFree(dataBuf);
  339. goto tcp_server_begin;
  340. }
  341. vPortFree(dataBuf);
  342. break;
  343. case TOGGLE_MODE:
  344. work_mode(dataBuf);
  345. vPortFree(dataBuf);
  346. LogPrint(LOG_INFO, __FILE__, __FUNCTION__, __LINE__, "toggle work mode");
  347. break;
  348. case UPDATE:
  349. LogPrint(LOG_INFO, __FILE__, __FUNCTION__, __LINE__, "update");
  350. vPortFree(dataBuf);
  351. updata_task_creat(client_socket);
  352. break;
  353. case REBOOT:
  354. send(client_socket,"{\"reboot\":\"success\"}", 20, 0);
  355. LogPrint(LOG_INFO, __FILE__, __FUNCTION__, __LINE__, "reboot");
  356. __set_PRIMASK(1);
  357. NVIC_SystemReset();
  358. break;
  359. default:
  360. send(client_socket,"{\"cmd:error\"}", 15, 0);
  361. LogPrint(LOG_INFO, __FILE__, __FUNCTION__, __LINE__, "Command error");
  362. }
  363. }
  364. }
  365. // 没接收到消息
  366. else if(recv_size==0)
  367. {
  368. lwip_close(client_socket);
  369. LOG_PRINT(LOG_ERROR,"PC disconnect,wait reconnect");
  370. client_socket= accept(sockfd, (struct sockaddr*)&client_sock,&len);
  371. }
  372. vPortFree(dataBuf);
  373. }
  374. }
  375. /*!
  376. \brief initialize the tcp_client application
  377. \param[in] none
  378. \param[out] none
  379. \retval none
  380. */
  381. void tcp_server_init(void)
  382. {
  383. xTaskCreate(tcp_server_task, "TCP_SERVER", DEFAULT_THREAD_STACKSIZE, NULL, 1, NULL);
  384. }