protocol.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. #include "protocol.h"
  2. #include "stdint.h"
  3. #include "stdlib.h"
  4. #include "string.h"
  5. #include "node_message.h"
  6. #include "sys_mqtt.h"
  7. /*****************************protocol主要为主网关与子节点通讯的协议使用**********************************/
  8. /*****************************其主要作用主网关给子节点轮询发送数据的编码**********************************/
  9. /*****************************子节点解析该主网关发送的数据 **********************************/
  10. /*****************************子节点回应对应的响应 **********************************/
  11. /*****************************子节点回应对应的响应的解析 **********************************/
  12. static volatile int deviceNum = 0; // 记录轮询读取设备的结束位置
  13. static volatile int paramsNum = 0; // 记录轮询读取属性的结束位置
  14. static volatile int startDeviceNum = 0; // 记录轮询读取设备的开始位置
  15. static volatile int startParamsNum = 0; // 记录轮询读取设备属性的开始位置
  16. void insertHexData(uint8_t *String, const uint8_t *hexData, uint8_t insertIndex, uint8_t length);
  17. enum
  18. {
  19. protocol_dlt645_97 = 0x08,
  20. protocol_modbus_read,
  21. protocol_dlt645_07
  22. } PROTOCOL_LENGTH; // 每个协议的数据包长度用于最开始区分不同协议
  23. // 枚举采集数据的存储个数
  24. enum
  25. {
  26. typeInt = 1,
  27. typeFloat,
  28. typeDouble
  29. } DATAType;
  30. // 枚举MODBUS协议具体功能
  31. enum
  32. {
  33. READ_COIL = 0x01,
  34. WRITE_COIL = 0x05,
  35. WRITE_COILS = 0x0F,
  36. READ_REGISTER = 0x03,
  37. WRITE_REFISTER = 0x06,
  38. WRITE_REGISTERS = 0x10
  39. } MODBUS_COMMAND;
  40. // 大小端
  41. typedef enum
  42. {
  43. MModBus_16bitOrder_AB = 1, // 大端
  44. MModBus_16bitOrder_BA,
  45. } MModBus_16bitOrder_t;
  46. /*
  47. *********************************************************************************************************
  48. * 函 数 名: void masterSendNodeString(uint8_t nodeIndex)Index下标从0开始
  49. * 功能说明: 负责取出网关第nodeIndex下的数据组成下发格式的数据,注:此处没有对node是否为NULL进行判断,请确保有该节点的信息
  50. * 形 参:nodeIndex第几个节点数据、string所总成的传递的字符串、size组成的字符串大小
  51. * 返 回 值: 0:该节点下已经没有属性需要在去读出了,1:该节点下还拥有其他属性等待读出。
  52. 注:其一次性只能传输20条属性的值若超出其范围还有数据则要多次组成发送
  53. 逻辑:每次调用该函数都会总成一条string,属性足够的时候会组成一条具有20个属性的指令,并记录结束的设备链表位置,属性链表位置,下次再调用从结束位置再开始,到最后可能就组成不了20个属性的string了,此时将开始的设备num,属性都归位
  54. 且也要记录每次开始的设备位置和属性位置,方便接收应答信号的解析
  55. *********************************************************************************************************
  56. */
  57. int masterSendNodeString(uint8_t nodeIndex, uint8_t *string, uint16_t *size)
  58. {
  59. startDeviceNum = deviceNum; // 起始位置等于上次结束的位置
  60. startParamsNum = paramsNum; // 起始位置等于上次结束的位置
  61. int nowDeviceNum = deviceNum;
  62. int nowparamsNum = paramsNum;
  63. GATEWAY_PARAMS *gateway;
  64. gateway = get_gateway_config_params();
  65. // 找到当前的设备结束位置
  66. DEVICE_PARAMS *currentDevice = gateway->device_params;
  67. while (nowDeviceNum)
  68. {
  69. currentDevice = currentDevice->nextDevice;
  70. nowDeviceNum--;
  71. }
  72. // 移动位置到结束位置属性的下一位
  73. GATEWAY_READ_MODBUS_COMMAND *currentModbusParams = currentDevice->params->gateway_read_modbus_command;
  74. GATEWAY_READ_DLT645_COMMAND *currentDlt645Params = currentDevice->params->gateway_read_dlt645_command;
  75. switch (currentDevice->protocol)
  76. {
  77. case MODBUS:
  78. while (nowparamsNum)
  79. {
  80. currentModbusParams = currentModbusParams->nextParams;
  81. nowparamsNum--;
  82. }
  83. break;
  84. case DLT645_07:
  85. case DLT645_97:
  86. while (nowparamsNum)
  87. {
  88. currentDlt645Params = currentDlt645Params->nextParams;
  89. nowparamsNum--;
  90. }
  91. break;
  92. default:
  93. break;
  94. }
  95. // 最多进行20次循环组成下发数据
  96. int i = 0;
  97. int len;
  98. len = strlen((char*)string);
  99. do
  100. {
  101. switch (currentDevice->protocol)
  102. {
  103. case MODBUS_READ:
  104. {
  105. uint8_t protocolHexData = protocol_modbus_read;
  106. insertHexData(string, &protocolHexData, len, 1); // 插入modbus长度
  107. len++;
  108. insertHexData(string, &currentModbusParams->functionCode, len, 1); // 插入读取功能码
  109. len++;
  110. insertHexData(string, &currentDevice->MDBdataType, len, 1); // 插入读取的数据格式
  111. len++;
  112. insertHexData(string, &currentModbusParams->slaveAddress, len, 1); // 插入modbus从站地址
  113. len++;
  114. insertHexData(string, (uint8_t *)&currentModbusParams->registerAddress, len, 2); // 插入modbus寄存器地址
  115. len += 2;
  116. insertHexData(string, &currentModbusParams->decimalPoint, len, 1); // 插入小数点位数
  117. len++;
  118. insertHexData(string, &currentDevice->MDBbigLittleFormat, len, 1); // 插入大小端
  119. len++;
  120. // 属性读取完毕,移位到下一个属性
  121. i++;
  122. currentModbusParams = currentModbusParams->nextParams;
  123. if (currentModbusParams == NULL)
  124. {
  125. currentDevice = currentDevice->nextDevice;
  126. if (currentModbusParams == NULL)
  127. {
  128. deviceNum = 0; // 将结束位置全部归0
  129. paramsNum = 0;
  130. *size = len;
  131. return 1;
  132. }
  133. currentModbusParams = currentDevice->params->gateway_read_modbus_command;
  134. nowDeviceNum++;
  135. nowparamsNum = 0;
  136. }
  137. break;
  138. }
  139. case DLT645_07:
  140. {
  141. uint8_t protocolHexData = protocol_dlt645_07;
  142. insertHexData(string, &protocolHexData, len, 1); // 插入dlt645_07数据长度
  143. len++;
  144. insertHexData(string, (uint8_t *)&currentDlt645Params->deviceID645, len, 6); // 插入dlt645地址域
  145. len += 6;
  146. insertHexData(string, (uint8_t *)&currentDlt645Params->Identification, len, 4);
  147. len += 4;
  148. // 属性读取完毕,移位到下一个属性
  149. i++;
  150. currentDlt645Params = currentDlt645Params->nextParams;
  151. if (currentDlt645Params == NULL)
  152. {
  153. currentDevice = currentDevice->nextDevice;
  154. if (currentDevice == NULL)
  155. {
  156. deviceNum = 0; // 将结束位置全部归0
  157. paramsNum = 0;
  158. *size = len;
  159. return 1;
  160. }
  161. currentDlt645Params = currentDevice->params->gateway_read_dlt645_command;
  162. nowDeviceNum++;
  163. nowparamsNum = 0;
  164. }
  165. break;
  166. }
  167. case DLT645_97:
  168. {
  169. uint8_t protocolHexData = protocol_dlt645_97;
  170. insertHexData(string, &protocolHexData, len, 1); // 插入dlt645_97数据长度
  171. len++;
  172. insertHexData(string, (uint8_t *)&currentDlt645Params->deviceID645, len, 6); // 插入dlt645_97地址域
  173. len += 6;
  174. insertHexData(string, (uint8_t *)&currentDlt645Params->Identification, len, 4); // 插入dlt645_97数据标识
  175. len += 4;
  176. // 属性读取完毕,移位到下一个属性
  177. i++;
  178. currentDlt645Params = currentDlt645Params->nextParams;
  179. if (currentDlt645Params == NULL)
  180. {
  181. currentDevice = currentDevice->nextDevice;
  182. currentDlt645Params = currentDevice->params->gateway_read_dlt645_command;
  183. if (currentDlt645Params == NULL)
  184. {
  185. deviceNum = 0; // 将结束位置全部归0
  186. paramsNum = 0;
  187. *size = len;
  188. return 1;
  189. }
  190. currentDlt645Params = currentDevice->params->gateway_read_dlt645_command; // 还有属性则更新位置
  191. nowDeviceNum++;
  192. nowparamsNum = 0;
  193. }
  194. break;
  195. }
  196. default:
  197. goto end_while;
  198. }
  199. nowparamsNum++;
  200. } while (i != 20);
  201. end_while:
  202. // 更新结束的位置分为两种情况,一种还在当前设备的属性中轮询此时nowDeviceNum没有改变过,另外一种情况设备内的属性
  203. if (nowDeviceNum == 0)
  204. {
  205. deviceNum = startDeviceNum;
  206. paramsNum = startParamsNum + nowparamsNum;
  207. }
  208. else
  209. {
  210. deviceNum = startDeviceNum + nowDeviceNum;
  211. paramsNum = nowparamsNum - 1;
  212. }
  213. *size = len;
  214. return 0;
  215. }
  216. /*
  217. *********************************************************************************************************
  218. * 函 数 名: void insertHexData(uint8_t *originalString,const uint8_t *hexData,int position)
  219. * 形 参:uint8_t *String需要插入的字符串,hexData插入的hex数据,insertIndex插入的下标位置,length插入的数据长度
  220. * 返 回 值: 无
  221. 注:插入的数据以被插入的字符串都可能包含0x00不要使用strlen
  222. *********************************************************************************************************
  223. */
  224. void insertHexData(uint8_t *String, const uint8_t *hexData, uint8_t insertIndex, uint8_t length)
  225. {
  226. memcpy(String + insertIndex, hexData, length);
  227. }
  228. /*
  229. *********************************************************************************************************
  230. * 函 数 名: uint8_t SlaveProtocolAnalysis(uint8_t *buff,uint16_t len)
  231. * 形 参:uint8_t *buff等待解析的字符串数据,
  232. * 返 回 值: 0:不是该节点的消息,数据没有进行任何处理。1:为该节点信息,接收到要进行的相应的切换工作
  233. 解析这段数据时要先判断是否为该节点的消息,stuct B *p=malloc(sizeof(struct B);
  234. *********************************************************************************************************
  235. */
  236. uint16_t LocalAddress=0x1F6E;
  237. uint8_t SlaveProtocolAnalysis(uint8_t *buff, uint16_t len)
  238. {
  239. NODE_DEVICE_PARAMS *node_receive_params;
  240. free_all_node_params();
  241. node_receive_params = get_node_receive_params();
  242. uint16_t slaveAdress = buff[0] << 8 | buff[1];
  243. int protocol_location = 2; // 起始的协议所处位置
  244. uint8_t i = 0;
  245. while (1)
  246. {
  247. if (slaveAdress == LocalAddress) // 判断此消息是否为该节点的消息,如果不是则跳出
  248. {
  249. switch (buff[protocol_location]) // 读取接收到的数据属于什么协议
  250. {
  251. case protocol_modbus_read:
  252. protocol_location++;
  253. node_receive_params->params[i].protcol = MODBUS_READ;
  254. node_receive_params->params[i].dlt645_params = NULL;
  255. node_receive_params->params[i].modbus_read = malloc(sizeof(NODE_MODBUS_READ));
  256. node_receive_params->params[i].modbus_write = NULL;
  257. node_receive_params->params[i].modbus_read->functionCode = buff[protocol_location + 1]; // 读出functionCode
  258. node_receive_params->params[i].modbus_read->dataType = buff[protocol_location + 2]; // 读出数据格式
  259. node_receive_params->params[i].modbus_read->slaveAddress = buff[protocol_location + 3]; // 读出从站地址
  260. node_receive_params->params[i].modbus_read->registerAddress = buff[protocol_location + 5] << 8 || buff[protocol_location + 4]; // 读出寄存器地址
  261. node_receive_params->params[i].modbus_read->registerLength = buff[protocol_location + 7] << 8 || buff[protocol_location + 6]; // 读出要读的寄存器长度
  262. node_receive_params->params[i].modbus_read->precision = buff[protocol_location + 8]; // 读出小数点精度
  263. node_receive_params->params[i].modbus_read->bigLittleFormat = buff[protocol_location + 9]; // 读出数据大小端格式
  264. protocol_location += protocol_modbus_read;
  265. break;
  266. case protocol_dlt645_07:
  267. protocol_location++;
  268. node_receive_params->params[i].protcol = DLT645_07;
  269. node_receive_params->params[i].dlt645_params = malloc(sizeof(NODE_DLT645_PARAMS));
  270. node_receive_params->params[i].modbus_read = NULL;
  271. node_receive_params->params[i].modbus_write = NULL;
  272. memcpy(node_receive_params->params[i].dlt645_params->deviceType645, buff + protocol_location, 6);
  273. memcpy((uint8_t *)&node_receive_params->params[i].dlt645_params->dataType645, buff + protocol_location + 6, 4);
  274. protocol_location += protocol_dlt645_07;
  275. break;
  276. case protocol_dlt645_97:
  277. protocol_location++;
  278. node_receive_params->params[i].protcol = DLT645_97;
  279. node_receive_params->params[i].dlt645_params = malloc(sizeof(NODE_DLT645_PARAMS));
  280. node_receive_params->params[i].modbus_read = NULL;
  281. node_receive_params->params[i].modbus_write = NULL;
  282. memcpy(node_receive_params->params[i].dlt645_params->deviceType645, buff + protocol_location, 6);
  283. memcpy((uint8_t *)&node_receive_params->params[i].dlt645_params->dataType645, buff + protocol_location + 6, 2);
  284. protocol_location += protocol_dlt645_97;
  285. break;
  286. default: // 解析没满20个属性
  287. return 1;
  288. }
  289. }
  290. else
  291. {
  292. node_receive_params->Index = i; // 记录本次有多少数据传输过来了
  293. return 0;
  294. }
  295. i++;
  296. if (protocol_location > len) // 判断数据解析是否越界了,越界则跳转出
  297. {
  298. return 1;
  299. }
  300. }
  301. }
  302. /*
  303. *********************************************************************************************************
  304. * 函 数 名: nodeSendReadValue(uint8_t *string)
  305. * 形 参:组成node回传数据,传输数据为
  306. * 返 回 值: 无
  307. *********************************************************************************************************
  308. */
  309. void nodeSendReaddValue(uint8_t *string,uint16_t *size)
  310. {
  311. NODE_DEVICE_PARAMS *node_receive_params;
  312. node_receive_params = get_node_receive_params();
  313. int i = 0;
  314. int len=0;
  315. //先加载node的地址node地址暂时本地写死后续加载
  316. string[len]= LocalAddress>>8;
  317. string[len+1]=LocalAddress;
  318. len+=2;
  319. while (i != 20)
  320. {
  321. switch (node_receive_params->params[i].protcol)
  322. {
  323. case DLT645_07:
  324. case DLT645_97:
  325. if (node_receive_params->params[i].dlt645_params->rxLen <= 4)
  326. {
  327. string[len] = 0x04;
  328. len++;
  329. memcpy(&string[len], node_receive_params->params[i].dlt645_params->value, 4);
  330. len += 4;
  331. }
  332. else if (node_receive_params->params[i].dlt645_params->rxLen == 5)
  333. {
  334. string[len] = 0x05;
  335. len++;
  336. memcpy(&string[len], node_receive_params->params[i].dlt645_params->value, 5);
  337. len += 5;
  338. }
  339. else if (node_receive_params->params[i].dlt645_params->rxLen == 9)
  340. {
  341. string[len] = 0x09;
  342. len++;
  343. memcpy(&string[len], node_receive_params->params[i].dlt645_params->value, 9);
  344. len += 9;
  345. }
  346. else
  347. {
  348. string[len] = 0x00;
  349. len++;
  350. }
  351. break;
  352. case MODBUS:
  353. if (node_receive_params->params[i].modbus_read->rxLen == 4)
  354. {
  355. string[len] = 0x04;
  356. len++;
  357. memcpy(&string[len], node_receive_params->params[i].dlt645_params->value, 4);
  358. len += 4;
  359. }
  360. else
  361. {
  362. string[len] = 0x00;
  363. len++;
  364. }
  365. break;
  366. default:
  367. *size=len;
  368. return;
  369. }
  370. i++;
  371. }
  372. *size=len;
  373. }
  374. /*
  375. *********************************************************************************************************
  376. * 函 数 名: GatewayProtocolAnalysis(uint8_t *string)
  377. * 形 参:将节点应答信息依次解析为json数据
  378. * 返 回 值: 0未读到json数据,1读到了json数据
  379. *********************************************************************************************************
  380. */
  381. int GatewayProtocolAnalysis(uint8_t *string, uint16_t len)
  382. {
  383. // 读取上次循环最终停止位置
  384. int nowDeviceNum = startDeviceNum;
  385. int nowparamsNum = startParamsNum;
  386. GATEWAY_PARAMS *gateway;
  387. gateway = get_gateway_config_params();
  388. // 找到该节点进行轮询
  389. DEVICE_PARAMS *currentDevice = gateway->device_params;
  390. while (nowDeviceNum)
  391. {
  392. currentDevice = currentDevice->nextDevice;
  393. nowDeviceNum--;
  394. }
  395. // 移动位置到开始属性
  396. GATEWAY_READ_MODBUS_COMMAND *currentModbusParams = currentDevice->params->gateway_read_modbus_command;
  397. GATEWAY_READ_DLT645_COMMAND *currentDlt645Params = currentDevice->params->gateway_read_dlt645_command;
  398. switch (currentDevice->protocol)
  399. {
  400. case MODBUS:
  401. while (nowparamsNum)
  402. {
  403. currentModbusParams = currentModbusParams->nextParams;
  404. nowparamsNum--;
  405. }
  406. break;
  407. case DLT645_07:
  408. case DLT645_97:
  409. while (nowparamsNum)
  410. {
  411. currentDlt645Params = currentDlt645Params->nextParams;
  412. nowparamsNum--;
  413. }
  414. break;
  415. default:
  416. break;
  417. }
  418. memset(pubJsonString,0,jsonMaxSize);
  419. sprintf(pubJsonString, "{\"data\":[");
  420. int index = 2; // 子节点地址
  421. while (index <len)
  422. {
  423. switch (currentDevice->protocol)
  424. {
  425. case MODBUS:
  426. // modbus协议
  427. switch (*(string + index))
  428. {
  429. case 0x00:
  430. // 没有数据
  431. index++;
  432. break;
  433. case 0x04:
  434. if (currentDevice->MDBdataType == 0x01 && currentModbusParams->decimalPoint == 0x00) // modbus底层存储的数据类型 1INT 2float
  435. {
  436. uint32_t data;
  437. data = string[index + 1] + (string[index + 2] >> 8) + (string[index + 3] >> 16) + (string[index + 4] >> 24);
  438. sprintf(pubJsonString + strlen(pubJsonString), "{\"deviceId\":\"%s\",\"%s\":%d},", currentDevice->deviceID, currentModbusParams->keyword, data);
  439. }
  440. else // 如果是int且有小数点,或者底层就是float按照float来计算
  441. {
  442. float value;
  443. memcpy(&value,string+index+1,4);
  444. sprintf(pubJsonString + strlen(pubJsonString), "{\"deviceId\":\"%s\",\"%s\":%.2f},", currentDevice->deviceID,currentModbusParams->keyword,value);
  445. }
  446. index += 4;
  447. break;
  448. default:
  449. break;
  450. }
  451. currentModbusParams = currentModbusParams->nextParams;
  452. if (currentModbusParams == NULL)
  453. {
  454. currentDevice = currentDevice->nextDevice;
  455. if(currentDevice->protocol==MODBUS)
  456. {
  457. currentModbusParams=currentModbusParams->nextParams;
  458. }
  459. else
  460. {
  461. currentDlt645Params=currentDlt645Params->nextParams;
  462. }
  463. }
  464. break;
  465. case DLT645_07:
  466. case DLT645_97:
  467. switch (*(string + index))
  468. {
  469. case 0x00:
  470. index++;
  471. break;
  472. case 0x04:
  473. {
  474. index++;
  475. float value;
  476. memcpy(&value,string+index,sizeof(float));
  477. sprintf(pubJsonString + strlen(pubJsonString), "{\"deviceId\":\"%s\",\"%s\": %.2f},", currentDevice->deviceID, currentDlt645Params->keyword,value);
  478. index += 4;
  479. }
  480. break;
  481. case 0x05:
  482. {
  483. index++;
  484. //%02依次为年月日时分
  485. sprintf(pubJsonString + strlen(pubJsonString), "{\"deviceId\":\"%s\",\"%s\": \"%02X%02X%02X%02X%02X\"},",
  486. currentDevice->deviceID, currentDlt645Params->keyword, string[index + 4], string[index + 3], string[index + 2], string[index + 1], string[index]);
  487. index += 5;
  488. }
  489. break;
  490. case 9:
  491. {
  492. index++;
  493. float value;
  494. memcpy(&value, &string[index], 4);
  495. sprintf(pubJsonString + strlen(pubJsonString), "{\"deviceId\":\"%s\",\"%s\":\"%02X%02X%02X%02X%02X%.2f\"},",
  496. currentDevice->deviceID, currentDlt645Params->keyword, string[index + 8], string[index + 7], string[index + 6], string[index + 5], string[index + 4], value);
  497. index+=9;
  498. }
  499. break;
  500. default:
  501. break;
  502. }
  503. //移动到下个属性
  504. switch (currentDevice->protocol)
  505. {
  506. case DLT645_97:
  507. case DLT645_07:
  508. currentDlt645Params = currentDlt645Params->nextParams;
  509. if (currentDlt645Params == NULL)
  510. {
  511. currentDevice = currentDevice->nextDevice;
  512. currentDlt645Params=currentDevice->params->gateway_read_dlt645_command;
  513. }
  514. break;
  515. case MODBUS:
  516. currentModbusParams=currentModbusParams->nextParams;
  517. if(currentModbusParams==NULL)
  518. {
  519. currentDevice=currentDevice->nextDevice;
  520. currentDlt645Params=currentDevice->params->gateway_read_dlt645_command;
  521. }
  522. break;
  523. }
  524. break;
  525. default:
  526. break;
  527. }
  528. }
  529. if(strlen(pubJsonString)<10)
  530. {
  531. return 0;
  532. }
  533. sprintf(pubJsonString+strlen(pubJsonString)-1,"]}");
  534. //组装完成json数据开始向mqtt邮箱发送完成标识
  535. int msg = MBOX_USER_PUBLISHQOS2;
  536. if(mqtt_connectFlag==1) OSMboxPost(mqtt_sendMseeageMbox, &msg);
  537. return 1;
  538. }