gateway_message.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. #include "string.h"
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include "gateway_message.h"
  5. GATEWAY_PARAMS gateway_config_params = {0};
  6. GATEWAY_PARAMS *get_gateway_config_params()
  7. {
  8. return &gateway_config_params;
  9. }
  10. void parseStringField(const char *data, const char *field, char *value);
  11. int parseIntField(const char *data, const char *field);
  12. int extract_substring(const char *input_string, const char *start_token, const char *end_token, char *result);
  13. void addSensorListParams(char *paramString, DEVICE_PARAMS *device);
  14. void addCommandListParams(char *paramString, DEVICE_PARAMS *device);
  15. /**
  16. * @brief 解析输入字符串网关结构体信息,将数据保存
  17. * @param char *gatewayString 输入字符串的数据
  18. * @retval 无
  19. */
  20. void addGatewayParams(char *gatewayString)
  21. {
  22. //gateway_config_params.device_params = NULL;
  23. gateway_config_params.port = parseIntField(gatewayString, "\"port\":");
  24. gateway_config_params.stopBit = parseIntField(gatewayString, "\"stopBit\":");
  25. gateway_config_params.dataBits = parseIntField(gatewayString, "\"dataBit\":");
  26. gateway_config_params.baudrate = parseIntField(gatewayString, "\"baudrate\":");
  27. gateway_config_params.checkBit = parseIntField(gatewayString, "\"checkBit\":");
  28. gateway_config_params.inboundTime= parseIntField(gatewayString, "\"inboundTime\":");
  29. parseStringField(gatewayString, "\"host\":\"", (char *)&gateway_config_params.host);
  30. parseStringField(gatewayString, "\"deviceId\":\"", (char *)&gateway_config_params.deviceId);
  31. parseStringField(gatewayString, "\"commandTopic\":\"", (char *)&gateway_config_params.commandTopic);
  32. parseStringField(gatewayString, "\"messageTopic\":\"", (char *)&gateway_config_params.messageTopic);
  33. parseStringField(gatewayString, "\"username\":\"", (char *)&gateway_config_params.username);
  34. parseStringField(gatewayString, "\"password\":\"", (char *)&gateway_config_params.passwd);
  35. char *deviceString = strstr(gatewayString,"deviceList"); //移位置到节点数据开始处
  36. while (1)
  37. {
  38. if (deviceString)
  39. {
  40. addDevice(deviceString); // 往此地址下挂载设备信息
  41. deviceString=deviceString;//重新获取设备字符串的位置
  42. deviceString = strstr(deviceString, "}");
  43. deviceString[0]='A';
  44. deviceString++;
  45. if(deviceString[0]==']')
  46. {
  47. break;
  48. }
  49. }
  50. else
  51. {
  52. return;
  53. }
  54. }
  55. }
  56. /**
  57. * @brief 解析输入字段的字符串,解析出属于该节点下的设备信息
  58. * @param uint8_t *string输入的字符串数据,NODE_PARAMS *node节点信息
  59. * @retval 无
  60. */
  61. void addDevice(char *deviceString)
  62. {
  63. char *paramString = (char *)deviceString; // 属性指针
  64. DEVICE_PARAMS *newDevicePage = (DEVICE_PARAMS *)mymalloc(SRAMEX, sizeof(DEVICE_PARAMS)); // 创建新设备页
  65. newDevicePage->nextDevice = NULL;
  66. newDevicePage->protocol = parseIntField(deviceString, "\"protocol\":");
  67. newDevicePage->MDBdataType = parseIntField(deviceString, "\"dataType\":");
  68. newDevicePage->MDBbigLittleFormat = parseIntField(deviceString, "\"bigLittleFormat\":");
  69. parseStringField(deviceString, "\"deviceId\":\"", (char *)&newDevicePage->deviceID);
  70. newDevicePage->params = (PARAMS_PROTOCOL_COMMAND *)mymalloc(SRAMEX,sizeof(PARAMS_PROTOCOL_COMMAND));
  71. memset(newDevicePage->params, 0, sizeof(PARAMS_PROTOCOL_COMMAND));
  72. paramString = strstr(paramString, "\"sensorData\":["); // 找到该节点的轮询上发属性
  73. while (1) // 此处数据是以数组形式存储所以解析方法和上面不一样
  74. {
  75. addSensorListParams(paramString, newDevicePage); // 解析一个属性并挂载该属性于该属性下
  76. paramString=paramString;
  77. paramString = strstr(paramString, "}"); // 移动到下一条属性
  78. paramString[0]='A';
  79. paramString++;
  80. if (paramString[0] == ']')
  81. {
  82. paramString = (char *)deviceString; // 复原指针位置
  83. break; // 找到了结束符,跳出循环
  84. }
  85. }
  86. if(newDevicePage->protocol==MODBUS) //如果为modbus的话解析写指令
  87. {
  88. paramString=deviceString; //移动到最开始的地方
  89. newDevicePage->MDBbigLittleFormat = parseIntField(paramString, "\"bigLittleFormat\":");
  90. newDevicePage->MDBdataType= parseIntField(paramString, "\"dataType\":");
  91. paramString = strstr(paramString, "\"commandData\":["); // 找到其轮询的写命令
  92. if(paramString!=NULL)
  93. {
  94. while(1)
  95. {
  96. addCommandListParams(paramString, newDevicePage);
  97. paramString = strstr(paramString, "}");
  98. paramString[0]='A';
  99. paramString++;
  100. if (paramString[0] == ']')
  101. {
  102. paramString = (char *)deviceString;// 复原指针位置
  103. break; // 找到了结束符,跳出循环
  104. }
  105. }
  106. }
  107. }
  108. // 解析下发的mqtt存储信息下发控制指令
  109. // 添加设备页到链表末尾
  110. if (gateway_config_params.device_params == NULL)
  111. {
  112. gateway_config_params.device_params = newDevicePage;
  113. }
  114. else
  115. {
  116. DEVICE_PARAMS *current = gateway_config_params.device_params;
  117. while (current->nextDevice != NULL)
  118. {
  119. current = current->nextDevice;
  120. }
  121. current->nextDevice = newDevicePage;
  122. }
  123. //解析modbus command指令将其加载到设备链表中
  124. }
  125. /**
  126. * @brief 解析输入字符串的paramString数据,将数据保存到至该设备结构体下,此处解析sensorList
  127. * @param uint8_t *paramString输入的字符串数据,DEVICE_PARAMS *device节点信息
  128. * @retval 无
  129. */
  130. void addSensorListParams(char *paramString, DEVICE_PARAMS *device)
  131. {
  132. switch (device->protocol)
  133. {
  134. case DLT645_97:
  135. case DLT645_07:
  136. {
  137. GATEWAY_READ_DLT645_COMMAND *read_dlt645_command = mymalloc(SRAMEX,sizeof(GATEWAY_READ_DLT645_COMMAND));
  138. read_dlt645_command->Identification = parseIntField(paramString, "\"identifier645\":");
  139. parseStringField(paramString, "\"identifier\":\"", (char *)&read_dlt645_command->keyword);
  140. char *string = mymalloc(SRAMEX,13);
  141. parseStringField(paramString, "\"deviceID645\":\"", string);
  142. for (int j = 0; j < 6; j++)
  143. {
  144. uint8_t byte;
  145. sscanf((const char *)&string[j * 2], "%2hhx", &byte);
  146. read_dlt645_command->deviceID645[j]=byte;
  147. }
  148. myfree(SRAMEX, string);
  149. string = NULL;
  150. if (device->params->gateway_read_dlt645_command == NULL)
  151. {
  152. device->params->gateway_read_dlt645_command = read_dlt645_command;
  153. }
  154. else
  155. {
  156. GATEWAY_READ_DLT645_COMMAND *current = device->params->gateway_read_dlt645_command;
  157. while (current->nextParams != NULL)
  158. {
  159. current = current->nextParams;
  160. }
  161. current->nextParams = read_dlt645_command;
  162. }
  163. }
  164. break;
  165. case MODBUS:
  166. {
  167. GATEWAY_READ_MODBUS_COMMAND *read_modbus_command = mymalloc(SRAMEX,sizeof(GATEWAY_READ_MODBUS_COMMAND));
  168. memset(read_modbus_command, 0, sizeof(GATEWAY_READ_MODBUS_COMMAND));
  169. parseStringField(paramString,"\"identifier\":\"",(char *)&read_modbus_command->keyword);
  170. read_modbus_command->decimalPoint = parseIntField(paramString, "\"precise\":");
  171. read_modbus_command->functionCode = parseIntField(paramString, "\"rFunctionCode\":");
  172. read_modbus_command->slaveAddress = parseIntField(paramString, "\"slaveAddress\":");
  173. read_modbus_command->registerAddress = parseIntField(paramString, "\"registerAddress\":");
  174. read_modbus_command->registerByteNum = parseIntField(paramString, "\"registerByteNum\":");
  175. if (device->params->gateway_read_modbus_command== NULL)
  176. {
  177. device->params->gateway_read_modbus_command = read_modbus_command;
  178. }
  179. else
  180. {
  181. GATEWAY_READ_MODBUS_COMMAND *current = device->params->gateway_read_modbus_command;
  182. while (current->nextParams != NULL)
  183. {
  184. current = current->nextParams;
  185. }
  186. current->nextParams = read_modbus_command;
  187. }
  188. }
  189. break;
  190. default:
  191. break;
  192. }
  193. }
  194. /**
  195. * @brief 解析输入字符串的paramString数据,将数据保存到至该设备结构体下,此处解析commandList
  196. * @param uint8_t *paramString输入的字符串数据,DEVICE_PARAMS *device节点信息
  197. * @retval 无
  198. */
  199. void addCommandListParams(char *paramString, DEVICE_PARAMS *device)
  200. {
  201. GATEWAY_WRITE_MODBUS_COMMAND *write_modbus_command=mymalloc(SRAMEX,sizeof(GATEWAY_WRITE_MODBUS_COMMAND));
  202. memset(write_modbus_command, 0, sizeof(GATEWAY_WRITE_MODBUS_COMMAND));
  203. parseStringField(paramString,"\"identifier\":\"",(char *)&write_modbus_command->keyword);
  204. write_modbus_command->functionCode=parseIntField(paramString, "\"wFunctionCode\":");
  205. write_modbus_command->slaveAddress=parseIntField(paramString, "\"slaveAddress\":");
  206. write_modbus_command->registerAddress = parseIntField(paramString, "\"registerAddress\":");
  207. write_modbus_command->registerByteNum = parseIntField(paramString, "\"registerByteNum\":");
  208. if(device->params->gateway_write_modbus_command == NULL)
  209. {
  210. device->params->gateway_write_modbus_command=write_modbus_command;
  211. }
  212. else
  213. {
  214. GATEWAY_WRITE_MODBUS_COMMAND *current=device->params->gateway_write_modbus_command;
  215. while (current->nextParams != NULL)
  216. {
  217. current = current->nextParams;
  218. }
  219. current->nextParams = write_modbus_command;
  220. }
  221. }
  222. // 提取int数据
  223. int parseIntField(const char *data, const char *field)
  224. {
  225. char *ptr = strstr(data, field) + strlen(field);
  226. int value;
  227. value = strtol(ptr, &ptr, 10);
  228. return value;
  229. }
  230. // 提取string字符串
  231. void parseStringField(const char *data, const char *field, char *value)
  232. {
  233. char *ptr = strstr(data, field) + strlen(field);
  234. sscanf(ptr, "%[^\"],", value);
  235. }
  236. // 不采用json解析硬解json数据
  237. void processStringJson(uint8_t *data)
  238. {
  239. GATEWAY_PARAMS *gateway;
  240. gateway = get_gateway_config_params();
  241. uint8_t *ptr = (uint8_t *)data;
  242. parseStringField((char *)&ptr, "\"messageTopic\":\"", (char *)&gateway->messageTopic);
  243. parseStringField((char *)&ptr, "\"commandTopic\":\"", (char *)&gateway->commandTopic);
  244. gateway->port = parseIntField((char *)&ptr, "\"port\":\"");
  245. parseStringField((char *)&ptr, "\"host\":\"", (char *)&gateway->host);
  246. parseStringField((char *)&ptr, "\"deviceId\":\"", (char *)&gateway->deviceId);
  247. // 解析最外层数据完成,申请空间存储节点信息
  248. int nodeNum = 1;
  249. char *node_index = mymalloc(SRAMEX,10);
  250. sprintf(node_index, "node_%d", nodeNum);
  251. strstr(ptr, node_index); // 将指针指向第一个节点
  252. while (*ptr == NULL)
  253. {
  254. }
  255. myfree(SRAMEX, node_index);
  256. }
  257. /**
  258. * @brief 从输入的input_string中寻找开始到结束位置的字符串数据,并将数据截取出来,传给result;
  259. * @param input_string输入字符串,start_token字符串起始位置,end_token字符串结束位置,result截取出的字符串
  260. * @retval 0:没有被截取到的字符串。1:有被截取到的字符串。
  261. */
  262. int extract_substring(const char *input_string, const char *start_token, const char *end_token, char *result)
  263. {
  264. const char *start_ptr = strstr(input_string, start_token);
  265. if (start_ptr == NULL)
  266. {
  267. return 0; // 未找到起始标记
  268. }
  269. start_ptr += strlen(start_token); // 移动指针到起始标记之后
  270. const char *end_ptr = strstr(start_ptr, end_token);
  271. if (end_ptr == NULL)
  272. {
  273. // 如果未找到结束标记,将从起始标记开始的字符串一直复制到 \0 结束
  274. strcpy(result, start_ptr);
  275. }
  276. else
  277. {
  278. // 找到结束标记,计算截取的长度并复制
  279. size_t length = end_ptr - start_ptr;
  280. strncpy(result, start_ptr, length);
  281. result[length] = '\0'; // 添加字符串结束符
  282. }
  283. return 1;
  284. }