parseDeviceMessage.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  1. #include "parseDeviceMessage.h"
  2. #include "cJson.h"
  3. #include "systick.h"
  4. #include "usart.h"
  5. #include "string.h"
  6. #include <math.h>
  7. #include "mqttRecv.h"
  8. #include "log.h"
  9. #include "ec800m.h"
  10. #include "main.h"
  11. #define MQTT_SUB_CMD_WRITE 0
  12. #define MQTT_SUB_CMD_READ 1
  13. ring_buffer mqttRecv;
  14. uint8_t protocol;
  15. void processHTTPjson(cJSON *json);
  16. char *processStringData(cJSON *data_obj, const char *key);
  17. int processIntData(cJSON *data_obj, const char *key);
  18. static void extract_data_from_buffer(const char *buffer, uint32_t *len_ptr, uint16_t *checkCode_ptr);
  19. //static void extract_data_from_buffer232(const char *buffer, uint32_t *len_ptr, uint16_t *checkCode_ptr);
  20. static uint16_t checksum(const char *str, uint16_t len);
  21. // json解析字符串
  22. char *processStringData(cJSON *data_obj, const char *key)
  23. {
  24. cJSON *json_data = cJSON_GetObjectItemCaseSensitive(data_obj, key);
  25. if (cJSON_IsString(json_data) && (json_data->valuestring != NULL))
  26. {
  27. return json_data->valuestring;
  28. }
  29. else
  30. {
  31. return NULL;
  32. }
  33. }
  34. // json解析int
  35. int processIntData(cJSON *data_obj, const char *key)
  36. {
  37. cJSON *json_data = cJSON_GetObjectItemCaseSensitive(data_obj, key);
  38. // if (cJSON_IsNumber(json_data) && (json_data->valueint != NULL)) 解决寄存器地址为0问题
  39. if (cJSON_IsNumber(json_data))
  40. {
  41. return json_data->valueint;
  42. }
  43. else
  44. {
  45. // gateway.data_valid_flag = 0; // 如果任意数据为NULL则此次数据为无效
  46. return -1;
  47. }
  48. }
  49. /*
  50. * 函数名:void WaitForUpData(void)
  51. * 输入参数:无
  52. * 输出参数:无
  53. * 返回值:无
  54. * 函数作用:等待更新本地flash信息
  55. */
  56. bool WaitForUpData(char *dmaBuffer)
  57. {
  58. if (UART0_RX_STAT > 0)
  59. {
  60. UART0_RX_STAT = 0;
  61. uint32_t len;
  62. uint16_t checkCode;
  63. char *temp;
  64. extract_data_from_buffer(dmaBuffer, &len, &checkCode);
  65. uint16_t jsonCheck = checksum(dmaBuffer, len);
  66. if (checkCode == jsonCheck)
  67. {
  68. addGatewayParams(dmaBuffer);
  69. return true;
  70. }
  71. else
  72. {
  73. return false;
  74. }
  75. }
  76. }
  77. /*
  78. * 函数名:static void extract_data_from_buffer(const char* buffer, uint32_t *len_ptr, uint16_t *checkCode_ptr)
  79. * 输入参数:buffer字符串
  80. * 输出参数:json有效字符串长度len_ptr,checkCode_ptr校验码指针
  81. * 返回值:无
  82. * 函数作用:eg. QFDWL: 621,3e23 从json信息最后端取出这段json的有效长度和校验码
  83. */
  84. static void extract_data_from_buffer(const char *buffer, uint32_t *len_ptr, uint16_t *checkCode_ptr)
  85. {
  86. char *start = strstr(buffer, "+QFDWL:");
  87. if (start != NULL)
  88. {
  89. start += 8; // 跳过"+QFDWL:"
  90. uint32_t len = 0;
  91. sscanf(start, "%u,", &len); // 读取长度
  92. start = strchr(start, ',') + 1; // 跳过逗号
  93. uint16_t checkCode = 0;
  94. sscanf(start, "%hx", &checkCode); // 读取16进制数据
  95. // 将提取的数据存入形参
  96. *len_ptr = len;
  97. *checkCode_ptr = checkCode;
  98. }
  99. }
  100. /*
  101. * 函数名:void dlt645_read(CONFIG_PARAMS *gateway)
  102. * 输入参数:设备地址*address 数据标识 readCode
  103. * 输出参数:无
  104. * 返回值:无
  105. * 函数作用:读取645协议内部数据
  106. 备注:dlt645协议为网上下载,返回值为数据长度
  107. 现在要加读日期信息YYMMDDhhmm
  108. 对源代码改动不多的方法是通过返回的数据长度去做判断
  109. 当数据长度为2、3、4时此时 read_buf数据包内仅含采集到的浮点数数据、 把float值4个字节直接拷贝出即可
  110. 当数据长度为5时 read_buf数据包为日期数据 其中5个字节分别为YYMMDDhhmm直接拷贝出来
  111. 当数据长度大于5时 read_buf内既包含浮点数又包含日期时间 其中前4个字节为浮点数数据后五个字节为日期信息
  112. */
  113. #if 0
  114. void dlt645_read(CONFIG_PARAMS *gateway)
  115. {
  116. uint8_t read_buf[10];
  117. for (int i = 0; i < gateway->device_read_data_num; i++)
  118. {
  119. gateway->device_read_data[i].rxLen = 0;
  120. memset(read_buf, 0, 10);
  121. memset(gateway->device_read_data[i].data, 0, 10);
  122. dlt645_set_addr(&dlt645, gateway->device_read_data[i].deviceID645);
  123. int8_t rs;
  124. if(gateway->dataType645==DLT645_2007){
  125. rs = dlt645_read_data(&dlt645, gateway->device_read_data[i].dataType645, read_buf, DLT645_2007);
  126. }
  127. else {
  128. rs = dlt645_read_data(&dlt645, gateway->device_read_data[i].dataType645, read_buf, DLT645_1997);
  129. }
  130. if (rs != -1)
  131. {
  132. if (rs <= 4)
  133. {
  134. memcpy(gateway->device_read_data[i].data, read_buf, 4);
  135. gateway->device_read_data[i].rxLen = rs;
  136. }
  137. else if (rs == 5)
  138. {
  139. memcpy(gateway->device_read_data[i].data, read_buf, 5);
  140. gateway->device_read_data[i].rxLen = rs;
  141. }
  142. else if (rs > 5)
  143. {
  144. memcpy(gateway->device_read_data[i].data, read_buf, 9);
  145. gateway->device_read_data[i].rxLen = rs;
  146. }
  147. }
  148. else
  149. {
  150. gateway->device_read_data[i].rxLen = 0;
  151. }
  152. task_fwdgt_reload();
  153. }
  154. }
  155. #endif
  156. /*
  157. * 函数名: void Read_Data(GATEWAY_PARAMS *gateway)
  158. * 输入参数:gateway网关存储的配置参数
  159. * 输出参数:网关结构体指针
  160. * 返回值:无
  161. * 函数作用:根据存储的设备信息发送modbus信息返回值存储到gateway->device_read_data[i].data中
  162. * modbusRead读数据只考虑读16位寄存器和32位寄存器这两种情况,但对其返回值进行处理后返回值一律为float型4个字节存储在内部
  163. dlt645根据其返回长度判断其具体是否挈带了日期数据
  164. */
  165. void Read_Data()
  166. {
  167. GATEWAY_PARAMS *gateway;
  168. gateway=get_gateway_config_params();
  169. DEVICE_PARAMS *currentDevice=gateway->device_params;
  170. READ_MODBUS_COMMAND *read_modbus_command=NULL;
  171. READ_DLT645_COMMAND *read_dlt645_command=NULL;
  172. switch(currentDevice->protocol)
  173. {
  174. case DLT645_07:
  175. case DLT645_97:
  176. protocol=1;
  177. read_dlt645_command=currentDevice->params->node_read_dlt645_command;
  178. break;
  179. case MODBUS:
  180. protocol=2;
  181. read_modbus_command=currentDevice->params->node_read_modbus_command;
  182. mmodbus_set16bitOrder(currentDevice->MDBbigLittleFormat); //设置大小端
  183. break;
  184. }
  185. while(1)//进入轮询
  186. {
  187. __START__WHILE:
  188. delay_1ms(10);
  189. switch(currentDevice->protocol)
  190. {
  191. case DLT645_07:
  192. case DLT645_97:
  193. {
  194. uint8_t read_buf[10];
  195. memset(read_buf, 0, 10);
  196. dlt645_set_addr(&dlt645,read_dlt645_command->deviceID645);
  197. int8_t rs;
  198. if(currentDevice->protocol==DLT645_97)
  199. {
  200. rs = dlt645_read_data(&dlt645, read_dlt645_command->Identification, read_buf, DLT645_1997);
  201. }
  202. else if(currentDevice->protocol==DLT645_07)
  203. {
  204. rs = dlt645_read_data(&dlt645, read_dlt645_command->Identification, read_buf, DLT645_2007);
  205. }
  206. if (rs != -1)
  207. {
  208. if (rs <= 4)
  209. {
  210. memcpy(read_dlt645_command->data, read_buf, 4);
  211. read_dlt645_command->rxLen = 4;
  212. }
  213. else if (rs == 5)
  214. {
  215. memcpy(read_dlt645_command->data, read_buf, 5);
  216. read_dlt645_command->rxLen = 5;
  217. }
  218. else if (rs > 5)
  219. {
  220. memcpy(read_dlt645_command->data, read_buf, 9);
  221. read_dlt645_command->rxLen = 9;
  222. }
  223. }
  224. else
  225. {
  226. read_dlt645_command->rxLen = 0;
  227. }
  228. }
  229. break;
  230. case MODBUS:
  231. {
  232. uint16_t data[read_modbus_command->registerByteNum/2];
  233. switch(read_modbus_command->functionCode)
  234. {
  235. case 0x03://读寄存器
  236. {
  237. bool success=mmodbus_readHoldingRegisters16i(read_modbus_command->slaveAddress,
  238. read_modbus_command->registerAddress,
  239. read_modbus_command->registerByteNum/2,
  240. data);
  241. if(success)
  242. {
  243. uint32_t value;
  244. if(read_modbus_command->registerByteNum==4)
  245. {
  246. value = (uint32_t)data[0] << 16 | data[1];
  247. read_modbus_command->rxLen=4;
  248. }
  249. else if(read_modbus_command->registerByteNum==2)
  250. {
  251. value = data[0];
  252. read_modbus_command->rxLen=4;
  253. }
  254. if(read_modbus_command->decimalPoint!=0)
  255. {
  256. float convertedValue =(float)value/pow(10,read_modbus_command->decimalPoint);
  257. memcpy(read_modbus_command->value,&convertedValue,4);
  258. }
  259. else
  260. {
  261. memcpy(read_modbus_command->value,&value,4);
  262. }
  263. }
  264. else
  265. {
  266. read_modbus_command->rxLen=0;
  267. }
  268. }
  269. break;
  270. case 0x01://读线圈
  271. {
  272. uint8_t data[2];
  273. bool success = mmodbus_readCoils(read_modbus_command->slaveAddress,
  274. read_modbus_command->registerAddress,
  275. read_modbus_command->registerByteNum/2,
  276. data);
  277. if(success)
  278. {
  279. read_modbus_command->rxLen=4;
  280. uint32_t value = (uint32_t)data[0] << 8 | data[1];
  281. memcpy(read_modbus_command->value,&value,4);
  282. }
  283. else
  284. {
  285. read_modbus_command->rxLen=0;
  286. }
  287. }
  288. break;
  289. default:
  290. break;
  291. }
  292. }
  293. break;
  294. }
  295. switch(currentDevice->protocol)
  296. {
  297. case DLT645_07:
  298. case DLT645_97:
  299. read_dlt645_command=read_dlt645_command->nextParams;
  300. break;
  301. case MODBUS:
  302. read_modbus_command=read_modbus_command->nextParams;
  303. break;
  304. }
  305. if((read_dlt645_command==NULL&&protocol==1)||(read_modbus_command==NULL&&protocol==2))
  306. {
  307. currentDevice=currentDevice->nextDevice;
  308. switch(currentDevice->protocol)
  309. {
  310. case DLT645_97:
  311. case DLT645_07:
  312. protocol=1;
  313. break;
  314. case MODBUS:
  315. protocol=2;
  316. break;
  317. }
  318. if(currentDevice==NULL)
  319. {
  320. return;//一直到没有设备时跳出循环
  321. }
  322. else
  323. {
  324. read_dlt645_command=currentDevice->params->node_read_dlt645_command;
  325. read_modbus_command=currentDevice->params->node_read_modbus_command;
  326. }
  327. }
  328. task_fwdgt_reload();//喂狗
  329. goto __START__WHILE;
  330. }
  331. }
  332. /*
  333. * 函数名:void parseMQTTData(CONFIG_PARAMS *gateway)
  334. * 输入参数:device存储的各类产品信息
  335. * 输出参数:无
  336. * 返回值:无
  337. * 函数作用:从环形缓冲区中一个个把json信息读出,与存储的gateway内的子设备信息匹配后将对应指令匹配执行并返回相应的值
  338. * 查询逻辑现在有两套数组read数组和write数组,当接收中断产生时先判断是进行读还是写、之后在查询此网关支持的协议MODBUS还是
  339. * 645,后根据协议去不同数组查询不同的本地存储信息、组装发送给子设备,然后根据查询的值进行采集上传数据
  340. */
  341. void parseMQTTData(GATEWAY_PARAMS *gateway)
  342. {
  343. while (UART0_RX_MQTT_SUB_STAT == 1)
  344. {
  345. UART0_RX_MQTT_SUB_STAT = 0;
  346. char payload_out[200];
  347. memset(payload_out,0,200);
  348. char mqtt_publish[200];
  349. char json[128];
  350. while (MQTT_BUFFER_READ(json))
  351. {
  352. LogPrint(LOG_INFO, __func__, __LINE__, "Received JSON: %s", json);
  353. cJSON *mqtt_sub_json = cJSON_Parse(json);
  354. if (mqtt_sub_json != NULL)
  355. {
  356. uint8_t action1[1];
  357. uint8_t action;
  358. strcpy(action1, processStringData(mqtt_sub_json, "action"));
  359. // 解决字符串为
  360. if (action1[0] == '0')
  361. {
  362. action = 0;
  363. }
  364. else if (action1[0] == '1')
  365. {
  366. action = 1;
  367. }
  368. char identifier[20];
  369. strcpy(identifier, processStringData(mqtt_sub_json, "identifier"));
  370. char deviceId[20];
  371. strcpy(deviceId, processStringData(mqtt_sub_json, "deviceId"));
  372. uint16_t value = processIntData(mqtt_sub_json, "parameter");
  373. char messageId[10];
  374. strcpy(messageId, processStringData(mqtt_sub_json, "messageId"));
  375. DEVICE_PARAMS *currentDevice=gateway->device_params;
  376. WRITE_MODBUS_COMMAND *write_modbus_command=NULL;
  377. READ_MODBUS_COMMAND *read_modbus_command=NULL;
  378. READ_DLT645_COMMAND *read_dlt645_command=NULL;
  379. // 判断其指令过来是读还是写
  380. if (action == MQTT_SUB_CMD_WRITE)//写指令目前只支持modbus
  381. {
  382. while(1)
  383. {
  384. if(strcmp((char *)&currentDevice->deviceID,deviceId)!=0)
  385. {
  386. currentDevice=currentDevice->nextDevice;
  387. if(currentDevice==NULL)
  388. {
  389. break; //如果找不到所属设备则跳出
  390. }
  391. }
  392. else
  393. {
  394. protocol=2;//决定当前使用modbus回调函数还是dlt645
  395. if(write_modbus_command==NULL)
  396. {
  397. write_modbus_command=currentDevice->params->node_write_modbus_command;
  398. }
  399. if(strcmp((char*)&write_modbus_command->keyword,identifier)!=0)//寻找设备层内所属属性名称
  400. {
  401. write_modbus_command=write_modbus_command->nextParams;
  402. if(write_modbus_command==NULL)
  403. {
  404. break;
  405. }
  406. }
  407. else //找到所属于的属性了
  408. {
  409. mmodbus_set16bitOrder(currentDevice->MDBbigLittleFormat);
  410. if(write_modbus_command->functionCode==0x05)
  411. {
  412. bool success = mmodbus_writeCoil(write_modbus_command->slaveAddress,
  413. write_modbus_command->registerAddress,
  414. (uint8_t)value);
  415. // 成功
  416. if (success)
  417. {
  418. sprintf((char *)payload_out,
  419. "{\"action\":\"%s\",\"identifier\":\"%s\",\"deviceId\":\"%s\",\"messageId\":\"%s\",\"state\":%d}",
  420. action1,
  421. write_modbus_command->keyword,
  422. currentDevice->deviceID, messageId, success);
  423. break;
  424. }
  425. else
  426. {
  427. sprintf((char *)payload_out,
  428. "{\"action\":\"%s\",\"identifier\":\"%s\",\"deviceId\":\"%s\",\"messageId\":\"%s\",\"state\":%d}",
  429. action1,
  430. write_modbus_command->keyword,
  431. currentDevice->deviceID, messageId, success);
  432. break;
  433. }
  434. }
  435. else if(write_modbus_command->functionCode==0x06)
  436. {
  437. bool success = mmodbus_writeHoldingRegister16i(write_modbus_command->slaveAddress,
  438. write_modbus_command->registerAddress,
  439. (uint16_t)value);
  440. if (success)
  441. {
  442. sprintf((char *)payload_out,
  443. "{\"action\":\"%s\",\"identifier\":\"%s\",\"deviceId\":\"%s\",\"messageId\":\"%s\",\"state\":%d}",
  444. action1,
  445. write_modbus_command->keyword,
  446. currentDevice->deviceID, messageId, success);
  447. break;
  448. }
  449. else
  450. {
  451. sprintf((char *)payload_out,
  452. "{\"action\":\"%s\",\"identifier\":\"%s\",\"deviceId\":\"%s\",\"messageId\":\"%s\",\"state\":%d}",
  453. action1,
  454. write_modbus_command->keyword,
  455. currentDevice->deviceID, messageId, success);
  456. break;
  457. }
  458. }
  459. }
  460. }
  461. }
  462. }
  463. // 指令是来读某属性值的
  464. else if (action == MQTT_SUB_CMD_READ)
  465. {
  466. while(1)
  467. {
  468. if(strcmp((char *)&currentDevice->deviceID,deviceId)!=0)
  469. {
  470. currentDevice=currentDevice->nextDevice;
  471. if(currentDevice==NULL)
  472. {
  473. break; //如果找不到所属设备则跳出
  474. }
  475. }
  476. else //找到了设备
  477. {
  478. if(currentDevice->protocol==MODBUS)
  479. {
  480. protocol=2;//决定当前使用modbus=2回调函数还是dlt645=1
  481. if(read_modbus_command==NULL)read_modbus_command=currentDevice->params->node_read_modbus_command;
  482. if(strcmp((char *)&read_modbus_command->keyword,identifier)!=0)
  483. {
  484. read_modbus_command=read_modbus_command->nextParams;
  485. if(read_modbus_command==NULL)break;
  486. }
  487. else//找到了属性
  488. {
  489. mmodbus_set16bitOrder(currentDevice->MDBbigLittleFormat);
  490. if(read_modbus_command->functionCode==0x01)
  491. {
  492. uint8_t data[2];
  493. bool success = mmodbus_readCoils(read_modbus_command->slaveAddress,
  494. read_modbus_command->registerAddress,
  495. read_modbus_command->registerByteNum/2,
  496. data);
  497. uint8_t value1;
  498. if (success)
  499. {
  500. uint16_t value1 = (uint16_t)data[0] << 8 | data[1];
  501. if (value1 == 0xFF00)
  502. {
  503. value1 = 1;
  504. }
  505. else if (value1 == 0x0000)
  506. {
  507. value1 = 0;
  508. }
  509. // 读线圈成功的json
  510. sprintf((char *)payload_out,
  511. "{\"action\":\"%s\",\"identifier\":\"%s\",\"deviceId\":\"%s\",\"messageId\":\"%s\",\"state\":%d,\"parameter\":%d}",
  512. action1,
  513. read_modbus_command->keyword,
  514. currentDevice->deviceID, messageId, success, value);
  515. break;
  516. }
  517. // 读线圈失败的json
  518. else
  519. {
  520. sprintf((char *)payload_out,
  521. "{\"action\":\"%s\",\"identifier\":\"%s\",\"deviceId\":\"%s\",\"messageId\":\"%s\",\"state\":%d}",
  522. action1,
  523. read_modbus_command->keyword,
  524. currentDevice->deviceID, messageId, success);
  525. break;
  526. }
  527. }
  528. }
  529. }
  530. else//dlt645读取指令
  531. {
  532. protocol=1;//决定当前使用modbus=2回调函数还是dlt645=1
  533. float value;
  534. uint8_t minute, hour, day, month, year;
  535. uint8_t read_buf[10];
  536. memset(read_buf, 0, 10);
  537. if(read_dlt645_command==NULL)read_dlt645_command=currentDevice->params->node_read_dlt645_command;
  538. dlt645_set_addr(&dlt645, read_dlt645_command->deviceID645);
  539. int8_t rs ;
  540. if(currentDevice->protocol==DLT645_07)
  541. {
  542. rs = dlt645_read_data(&dlt645, read_dlt645_command->Identification, read_buf, DLT645_2007);
  543. }else if(currentDevice->protocol==DLT645_97)
  544. {
  545. rs = dlt645_read_data(&dlt645, read_dlt645_command->Identification, read_buf, DLT645_1997);
  546. }
  547. if (rs != -1)
  548. {
  549. if (rs <= 4)
  550. {
  551. memcpy(&value, read_buf, 4);
  552. sprintf((char *)payload_out,
  553. "{\"action\":\"%s\",\"identifier\":\"%s\",\"deviceId\":\"%s\",\"messageId\":\"%s\",\"state\":%d,\"value\":%.2f}",
  554. action1,
  555. read_dlt645_command->keyword,
  556. currentDevice->deviceID, messageId, 1, value);
  557. break;
  558. }
  559. else if (rs == 5)
  560. {
  561. year = read_buf[4];
  562. month = read_buf[3];
  563. day = read_buf[2];
  564. hour = read_buf[1];
  565. minute = read_buf[0];
  566. sprintf((char *)payload_out,
  567. "{\"action\":\"%s\",\"identifier\":\"%s\",\"deviceId\":\"%s\",\"messageId\":\"%s\",\"state\":%d,\"value\":\"%02X%02X%02X%02X%02X\"}",
  568. action1,
  569. read_dlt645_command->keyword,
  570. currentDevice->deviceID, messageId, 1, year, month, day, hour, minute);
  571. break;
  572. }
  573. else if (rs > 5)
  574. {
  575. memcpy(&value, read_buf, 4);
  576. year = read_buf[8];
  577. month = read_buf[7];
  578. day = read_buf[6];
  579. hour = read_buf[5];
  580. minute = read_buf[4];
  581. sprintf((char *)payload_out,
  582. "{\"action\":\"%s\",\"identifier\":\"%s\",\"deviceId\":\"%s\",\"messageId\":\"%s\",\"state\":%d,\"value\":\"%02X%02X%02X%02X%02X%.2f\"}",
  583. action1,
  584. read_dlt645_command->keyword,
  585. currentDevice->deviceID, messageId, 1, year, month, day, hour, minute, value);
  586. break;
  587. }
  588. }
  589. else
  590. {
  591. // dlt645读取失败的json组成
  592. sprintf((char *)payload_out,
  593. "{\"action\":\"%s\",\"identifier\":\"%s\",\"deviceId\":\"%s\",\"messageId\":\"%s\",\"state\":%d}",
  594. action1,
  595. read_dlt645_command->keyword,
  596. currentDevice->deviceID, messageId, 0);
  597. break;
  598. }
  599. }
  600. }
  601. }
  602. }
  603. // 上传对应的响应信息
  604. sprintf((char *)mqtt_publish, "AT+QMTPUBEX=0,0,0,0,\"%s\",%d\r\n", gateway->messageTopic, strlen(payload_out));
  605. EC800MSendCmd(mqtt_publish, strlen(mqtt_publish));
  606. WaitResponse("<", 1000);
  607. EC800MSendCmd(payload_out, strlen(payload_out));
  608. WaitResponse(RSP_OK, 1000);
  609. }
  610. cJSON_Delete(mqtt_sub_json);
  611. }
  612. }
  613. }
  614. // 模块下载download校验值
  615. static uint16_t checksum(const char *str, uint16_t len)
  616. {
  617. uint16_t sum = 0;
  618. uint8_t odd = 0;
  619. // 如果字符串长度为奇数,则将最后一个字符设置为高8位,低8位设置为0
  620. if (len % 2 == 1)
  621. {
  622. odd = 1;
  623. len--;
  624. }
  625. // 将每两个字符作为一个16位的数值进行异或操作
  626. for (uint16_t i = 0; i < len; i += 2)
  627. {
  628. sum ^= ((uint16_t)str[i] << 8) | (uint16_t)str[i + 1];
  629. }
  630. // 如果字符串长度为奇数,则还需要将最后一个字符与0xFF00异或
  631. if (odd)
  632. {
  633. sum ^= (uint16_t)str[len] << 8;
  634. }
  635. // 返回校验和
  636. return sum;
  637. }
  638. //比较本次数据和上次数据是否相同如果相同则不进行数据的更新
  639. int compareWithLastData(char *old_data,const char* new_data,uint8_t dataLen) {
  640. if (memcmp(new_data, old_data,dataLen) == 0) {
  641. return 0;
  642. } else {
  643. memcpy(old_data,&new_data,dataLen);;
  644. return 1;
  645. }
  646. }