TcpComm.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /********************************************************************************
  2. * File Name: TcpComm.h
  3. * Function Describe: Header file for TcpComm.c
  4. * Explain:
  5. * Writer:
  6. * Date:
  7. *******************************************************************************/
  8. #ifndef _TCP_COMM_H
  9. #define _TCP_COMM_H
  10. /*****************************************************************************/
  11. #include <rtl.h>
  12. #include "MsgQueue.h"
  13. #include "Modem.h"
  14. //-------------------------------------------------------------------------------
  15. #define TCP_MAGIC (0x98)//头
  16. #define TCP_VER (0x0001)//默认版本
  17. //type
  18. #define TCP_TYPE_REQ (0x00)//请求消息
  19. #define TCP_TYPE_ACK (0x01)//响应消息
  20. //cmd
  21. #define TCP_CMD_LOGIN (0x0001)//DTU登录
  22. #define TCP_CMD_UPLOAD (0x0002)//终端信息上传
  23. #define TCP_CMD_HEART (0x0003)//终端心跳发送
  24. #define TCP_CMD_DOWNLOAD (0x0004)//服务器下发
  25. //data
  26. #define TCP_DATA_NUM_MAX (500)//最长data长度--根据实际情况修改
  27. //----------------------------------------------------------------------------------------------------------
  28. // 格式
  29. // |帧头 | 帧控制 | 帧内容 | 帧尾 |
  30. // |magic 1字|len 4字| ver 2字| type 1字| cmd 2字| id 4字| msgNum 4字| utc 4字|data[x] X字|crc16 2字|
  31. // 帧头: 固定码
  32. // 帧长度: 整包长度(含CRC) 24 + data[]长度
  33. // 帧内容: JSON格式的ASCII码
  34. //----------------------------------------------------------------------------------------------------------
  35. //TCP 帧控制域(数据区不在内)
  36. typedef struct __tcpMsgCtrl_t
  37. {
  38. unsigned int len;//整个包长度
  39. unsigned short ver;//版本
  40. unsigned char type;//request REQUEST(TCP_TYPE_REQ) or RESPONSE(TCP_TYPE_ACK)
  41. unsigned short cmd;//指令码
  42. unsigned int id;//会话ID(用0x0000登录后由服务器生成返回,登录后每次都需要带上)
  43. unsigned int msgNum;//消息流水号,由发起端产生,响应端原值返回
  44. unsigned int utc;//UTC时间戳
  45. }__attribute__((packed)) tcpMsgCtrl_t;
  46. //TCP frame 完整帧
  47. typedef struct __tcpMsg_t
  48. {
  49. unsigned char magic;
  50. tcpMsgCtrl_t ctrl;
  51. char data[TCP_DATA_NUM_MAX];//报文内容(JSON格式ASCII码),整个LEN长度的包,最后两字节为CRC16(帧尾)
  52. }__attribute__((packed)) tcpMsg_t;
  53. //-------------------------------------------------------------------------------
  54. //TCP ACK 报文数据
  55. typedef struct __tcpMsgDatAck_t
  56. {
  57. unsigned char resultCode;//结果码(0成功,否则为相应的错误代码)
  58. //char reason[40];//错误原因
  59. }__attribute__((packed)) tcpMsgDatAck_t;
  60. //数据域为(JSON格式)ascii码,以下为提取内容的格式
  61. //数据域内容
  62. //{
  63. //"imsi":"460030785666759",
  64. //"iccid":"8986031640020231458P",
  65. //"authType": 1,
  66. //"dtuVender":"xxxx",
  67. //"dtuId":xxxx,
  68. //"productKey": "xxxx",
  69. //"sn":"08e7a3ae-f8f3-4414-a312",
  70. //"md5": "9E107D9D372BB6826BD81D3542A419D6"
  71. //}
  72. //说明:
  73. //Imsi:卡imsi号,
  74. //Iccid:卡iccid号,
  75. //authType:接入方式。1:设备直连;2:DTU登陆;(待扩展)
  76. //dtuVender:DTU厂商编码,
  77. //dtuId:dtu编码,
  78. //productKey:产品(平台定义,固定10字节,数字和小写字母混合)
  79. //sn:设备编码
  80. //备注:
  81. //当authType是1(设备登陆)时, dtuVender, dtuId, productKey, sn都有效,dtuVender和dtuId选传,productKey,sn必传。
  82. //当authType是2(DTU登陆)时,dtuVender, dtuId字段必传,productKey和sn字段无效,可以不传。
  83. //LOGIN 报文数据
  84. typedef struct __tcpMsgDatLogin_t
  85. {
  86. char imsi[18];//16字节有效
  87. char iccid[32];//20字节有效
  88. char authType;//接入方式。1:设备直连;2:DTU登陆;(待扩展)
  89. char dtuVender[16];//8字节设备商,这里为DTU供应商
  90. char dtuId[16];//dtu编码
  91. char productKey[16];//产品(平台定义,固定10字节,数字和小写字母混合)
  92. char sn[32];//设备编码
  93. char md5[34];//32位
  94. }__attribute__((packed)) tcpMsgDatLogin_t;
  95. //-------------------------------------------------------------------------------
  96. //上传服务器的数据
  97. //UPLOAD 报文数据
  98. //{
  99. // "sn": "123456789",
  100. // "attrs":
  101. // {
  102. // "Country":1,
  103. // "Province":1,
  104. // "District":1,
  105. // "Shipowner":1,
  106. // "ShipNumber":"XX01",
  107. // "Address":28,
  108. // "Type":1,
  109. // "Status":1,
  110. // "AntiTheft":[0,0,0,0,0],
  111. // "Reserve":[0,0,0,0]
  112. // }
  113. //}
  114. //数据说明:
  115. //sn:设备编号
  116. //Country:国家: 中国 1
  117. //Province:省份: 广东:1 (广东:1、广西:2、福建:3、海南:4、山东:5、浙江:6、辽宁:7、江苏:8、上海:9、天津:10、河北:11)
  118. //District:地区 (具体待定,DTU只透传)
  119. //Shipowner:船东(渔政船:1、渔业公司:2、个人船东:3)
  120. //ShipNumber:渔船号码。13位数
  121. //Address:地址码
  122. //Type:类型码: 感温:1;感烟:2;手报:3;输入模块:4;输出模块:5;声光报警器:6;地址接口模块:7
  123. //Status:状态码: 正常:1报警:2故障:3启动:4
  124. //AntiTheft:防盗预留
  125. //Reserve:预留
  126. //-------------------------------------------------------------------------------
  127. typedef struct __tcpMsgDatUpload_t
  128. {
  129. char sn[32];//SN:16位
  130. unsigned char country;//
  131. unsigned char province;//
  132. unsigned char district;//
  133. unsigned char shipowner;//
  134. unsigned char shipNumber[20];//
  135. unsigned char address;//
  136. unsigned char type;//
  137. unsigned char status;//
  138. unsigned char antiTheft[5];//
  139. unsigned char reserve[4];//
  140. //以下新增
  141. unsigned char GpsSwitch;
  142. unsigned char WirelessSwitch;
  143. unsigned char WiredSwitch;
  144. unsigned char AntiTheftSwitch;
  145. unsigned short UploadInterval;
  146. unsigned char PosF;//定位标志 0--未定位 1--已定位
  147. unsigned short aspect;//方向 0~360度
  148. float Speed; //速度 节
  149. double Longitude;//经度 度
  150. double Latitude;//纬度 度
  151. }__attribute__((packed)) tcpMsgDatUpload_t;
  152. //-------------------------------------------------------------------------------
  153. //服务器下发的数据
  154. //DOWNLOAD 报文数据
  155. //{
  156. // "sn": "sn1234567891",
  157. // "attrs":
  158. // {
  159. // 无
  160. // }
  161. //}
  162. //-------------------------------------------------------------------------------
  163. typedef struct __tcpMsgDatDownload_t
  164. {
  165. char sn[32];//SN:16位
  166. }__attribute__((packed)) tcpMsgDatDownload_t;
  167. //-------------------------------------------------------------------------------
  168. //心跳(90s内周期上传UPLOAD 报文时可以不用心跳)
  169. // 报文数据
  170. //{
  171. // "sn": "sn1234567891"
  172. //}
  173. //说明: DTU和设备一一对应时可以不用传SN,数据域为空.如净水器应用
  174. //-------------------------------------------------------------------------------
  175. typedef struct __tcpMsgDatHeart_t
  176. {
  177. char sn[32];//SN:16位
  178. }__attribute__((packed)) tcpMsgDatHeart_t;
  179. //----------------------------------------------------------------------
  180. //帧控制缓存
  181. extern tcpMsgCtrl_t tcpSendMsgCtrl, tcpRecvMsgCtrl;
  182. extern unsigned int g_uiTcpMsgNum;//发送消息流水号
  183. extern unsigned int g_uiSysUtc;//系统UTC时间戳
  184. //报文数据内容缓存
  185. //登录
  186. extern tcpMsgDatLogin_t tcpMsgDatLogin;
  187. extern tcpMsgDatAck_t tcpMsgDatLoginAck;
  188. //上传
  189. extern tcpMsgDatUpload_t tcpMsgDatUpload;
  190. extern tcpMsgDatAck_t tcpMsgDatUploadAck;
  191. //服务器下发
  192. extern tcpMsgDatDownload_t tcpMsgDatDownload;
  193. extern tcpMsgDatAck_t tcpMsgDatDownloadAck;
  194. //心跳
  195. extern tcpMsgDatHeart_t tcpMsgDatHeart;
  196. extern tcpMsgDatAck_t tcpMsgDatHeartAck;
  197. //----------------------------------------------------------------------
  198. unsigned int BigLittle32Conv(unsigned int srcData);
  199. unsigned short BigLittle16Conv(unsigned short srcData);
  200. unsigned short TcpPackLogin(unsigned char *pBuf, tcpMsgCtrl_t tcpCtrl, tcpMsgDatLogin_t tcpLogin);
  201. unsigned short TcpPackUpload(unsigned char *pBuf, tcpMsgCtrl_t tcpCtrl, tcpMsgDatUpload_t tcpUpload);
  202. unsigned short TcpPackHeart(unsigned char *pBuf, tcpMsgCtrl_t tcpCtrl, tcpMsgDatHeart_t tcpHeart);
  203. unsigned short TcpPackAck(unsigned char *pBuf, tcpMsgCtrl_t tcpCtrl, tcpMsgDatAck_t tcpAck);
  204. unsigned char TcpUnPackAndCheck(tcpMsgCtrl_t *pTcpCtrl, char **pJson, tcpMsg_t *pTcpMsgRecv);
  205. unsigned char TcpJsonToAck(tcpMsgDatAck_t *pTcpAck, char *pJson);
  206. unsigned char TcpJsonToDownload(tcpMsgDatDownload_t *pTcpDownload, char *pJson);/*****************************************************************************/
  207. #endif
  208. /*****************************************************************************/