gateway_message.c 11 KB

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