123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220 |
- /********************************************************************************
- * File Name: TcpComm.h
- * Function Describe: Header file for TcpComm.c
- * Explain:
- * Writer:
- * Date:
- *******************************************************************************/
- #ifndef _TCP_COMM_H
- #define _TCP_COMM_H
- /*****************************************************************************/
- #include <rtl.h>
- #include "MsgQueue.h"
- #include "Modem.h"
- //-------------------------------------------------------------------------------
- #define TCP_MAGIC (0x98)//头
- #define TCP_VER (0x0001)//默认版本
- //type
- #define TCP_TYPE_REQ (0x00)//请求消息
- #define TCP_TYPE_ACK (0x01)//响应消息
- //cmd
- #define TCP_CMD_LOGIN (0x0001)//DTU登录
- #define TCP_CMD_UPLOAD (0x0002)//终端信息上传
- #define TCP_CMD_HEART (0x0003)//终端心跳发送
- #define TCP_CMD_DOWNLOAD (0x0004)//服务器下发
- //data
- #define TCP_DATA_NUM_MAX (500)//最长data长度--根据实际情况修改
- //----------------------------------------------------------------------------------------------------------
- // 格式
- // |帧头 | 帧控制 | 帧内容 | 帧尾 |
- // |magic 1字|len 4字| ver 2字| type 1字| cmd 2字| id 4字| msgNum 4字| utc 4字|data[x] X字|crc16 2字|
- // 帧头: 固定码
- // 帧长度: 整包长度(含CRC) 24 + data[]长度
- // 帧内容: JSON格式的ASCII码
- //----------------------------------------------------------------------------------------------------------
- //TCP 帧控制域(数据区不在内)
- typedef struct __tcpMsgCtrl_t
- {
- unsigned int len;//整个包长度
- unsigned short ver;//版本
- unsigned char type;//request REQUEST(TCP_TYPE_REQ) or RESPONSE(TCP_TYPE_ACK)
- unsigned short cmd;//指令码
- unsigned int id;//会话ID(用0x0000登录后由服务器生成返回,登录后每次都需要带上)
- unsigned int msgNum;//消息流水号,由发起端产生,响应端原值返回
- unsigned int utc;//UTC时间戳
- }__attribute__((packed)) tcpMsgCtrl_t;
- //TCP frame 完整帧
- typedef struct __tcpMsg_t
- {
- unsigned char magic;
- tcpMsgCtrl_t ctrl;
- char data[TCP_DATA_NUM_MAX];//报文内容(JSON格式ASCII码),整个LEN长度的包,最后两字节为CRC16(帧尾)
- }__attribute__((packed)) tcpMsg_t;
- //-------------------------------------------------------------------------------
- //TCP ACK 报文数据
- typedef struct __tcpMsgDatAck_t
- {
- unsigned char resultCode;//结果码(0成功,否则为相应的错误代码)
- //char reason[40];//错误原因
- }__attribute__((packed)) tcpMsgDatAck_t;
- //数据域为(JSON格式)ascii码,以下为提取内容的格式
- //数据域内容
- //{
- //"imsi":"460030785666759",
- //"iccid":"8986031640020231458P",
- //"authType": 1,
- //"dtuVender":"xxxx",
- //"dtuId":xxxx,
- //"productKey": "xxxx",
- //"sn":"08e7a3ae-f8f3-4414-a312",
- //"md5": "9E107D9D372BB6826BD81D3542A419D6"
- //}
- //说明:
- //Imsi:卡imsi号,
- //Iccid:卡iccid号,
- //authType:接入方式。1:设备直连;2:DTU登陆;(待扩展)
- //dtuVender:DTU厂商编码,
- //dtuId:dtu编码,
- //productKey:产品(平台定义,固定10字节,数字和小写字母混合)
- //sn:设备编码
- //备注:
- //当authType是1(设备登陆)时, dtuVender, dtuId, productKey, sn都有效,dtuVender和dtuId选传,productKey,sn必传。
- //当authType是2(DTU登陆)时,dtuVender, dtuId字段必传,productKey和sn字段无效,可以不传。
- //LOGIN 报文数据
- typedef struct __tcpMsgDatLogin_t
- {
- char imsi[18];//16字节有效
- char iccid[32];//20字节有效
- char authType;//接入方式。1:设备直连;2:DTU登陆;(待扩展)
- char dtuVender[16];//8字节设备商,这里为DTU供应商
- char dtuId[16];//dtu编码
- char productKey[16];//产品(平台定义,固定10字节,数字和小写字母混合)
- char sn[32];//设备编码
- char md5[34];//32位
- }__attribute__((packed)) tcpMsgDatLogin_t;
- //-------------------------------------------------------------------------------
- //上传服务器的数据
- //UPLOAD 报文数据
- //{
- // "sn": "123456789",
- // "attrs":
- // {
- // "Country":1,
- // "Province":1,
- // "District":1,
- // "Shipowner":1,
- // "ShipNumber":"XX01",
- // "Address":28,
- // "Type":1,
- // "Status":1,
- // "AntiTheft":[0,0,0,0,0],
- // "Reserve":[0,0,0,0]
- // }
- //}
- //数据说明:
- //sn:设备编号
- //Country:国家: 中国 1
- //Province:省份: 广东:1 (广东:1、广西:2、福建:3、海南:4、山东:5、浙江:6、辽宁:7、江苏:8、上海:9、天津:10、河北:11)
- //District:地区 (具体待定,DTU只透传)
- //Shipowner:船东(渔政船:1、渔业公司:2、个人船东:3)
- //ShipNumber:渔船号码。13位数
- //Address:地址码
- //Type:类型码: 感温:1;感烟:2;手报:3;输入模块:4;输出模块:5;声光报警器:6;地址接口模块:7
- //Status:状态码: 正常:1报警:2故障:3启动:4
- //AntiTheft:防盗预留
- //Reserve:预留
- //-------------------------------------------------------------------------------
- typedef struct __tcpMsgDatUpload_t
- {
- char sn[32];//SN:16位
- unsigned char country;//
- unsigned char province;//
- unsigned char district;//
- unsigned char shipowner;//
- unsigned char shipNumber[20];//
- unsigned char address;//
- unsigned char type;//
- unsigned char status;//
- unsigned char antiTheft[5];//
- unsigned char reserve[4];//
- //以下新增
- unsigned char GpsSwitch;
- unsigned char WirelessSwitch;
- unsigned char WiredSwitch;
- unsigned char AntiTheftSwitch;
- unsigned short UploadInterval;
- unsigned char PosF;//定位标志 0--未定位 1--已定位
- unsigned short aspect;//方向 0~360度
- float Speed; //速度 节
- double Longitude;//经度 度
- double Latitude;//纬度 度
- }__attribute__((packed)) tcpMsgDatUpload_t;
- //-------------------------------------------------------------------------------
- //服务器下发的数据
- //DOWNLOAD 报文数据
- //{
- // "sn": "sn1234567891",
- // "attrs":
- // {
- // 无
- // }
- //}
- //-------------------------------------------------------------------------------
- typedef struct __tcpMsgDatDownload_t
- {
- char sn[32];//SN:16位
-
- }__attribute__((packed)) tcpMsgDatDownload_t;
- //-------------------------------------------------------------------------------
- //心跳(90s内周期上传UPLOAD 报文时可以不用心跳)
- // 报文数据
- //{
- // "sn": "sn1234567891"
- //}
- //说明: DTU和设备一一对应时可以不用传SN,数据域为空.如净水器应用
- //-------------------------------------------------------------------------------
- typedef struct __tcpMsgDatHeart_t
- {
- char sn[32];//SN:16位
- }__attribute__((packed)) tcpMsgDatHeart_t;
- //----------------------------------------------------------------------
- //帧控制缓存
- extern tcpMsgCtrl_t tcpSendMsgCtrl, tcpRecvMsgCtrl;
- extern unsigned int g_uiTcpMsgNum;//发送消息流水号
- extern unsigned int g_uiSysUtc;//系统UTC时间戳
- //报文数据内容缓存
- //登录
- extern tcpMsgDatLogin_t tcpMsgDatLogin;
- extern tcpMsgDatAck_t tcpMsgDatLoginAck;
- //上传
- extern tcpMsgDatUpload_t tcpMsgDatUpload;
- extern tcpMsgDatAck_t tcpMsgDatUploadAck;
- //服务器下发
- extern tcpMsgDatDownload_t tcpMsgDatDownload;
- extern tcpMsgDatAck_t tcpMsgDatDownloadAck;
- //心跳
- extern tcpMsgDatHeart_t tcpMsgDatHeart;
- extern tcpMsgDatAck_t tcpMsgDatHeartAck;
- //----------------------------------------------------------------------
- unsigned int BigLittle32Conv(unsigned int srcData);
- unsigned short BigLittle16Conv(unsigned short srcData);
- unsigned short TcpPackLogin(unsigned char *pBuf, tcpMsgCtrl_t tcpCtrl, tcpMsgDatLogin_t tcpLogin);
- unsigned short TcpPackUpload(unsigned char *pBuf, tcpMsgCtrl_t tcpCtrl, tcpMsgDatUpload_t tcpUpload);
- unsigned short TcpPackHeart(unsigned char *pBuf, tcpMsgCtrl_t tcpCtrl, tcpMsgDatHeart_t tcpHeart);
- unsigned short TcpPackAck(unsigned char *pBuf, tcpMsgCtrl_t tcpCtrl, tcpMsgDatAck_t tcpAck);
- unsigned char TcpUnPackAndCheck(tcpMsgCtrl_t *pTcpCtrl, char **pJson, tcpMsg_t *pTcpMsgRecv);
- unsigned char TcpJsonToAck(tcpMsgDatAck_t *pTcpAck, char *pJson);
- unsigned char TcpJsonToDownload(tcpMsgDatDownload_t *pTcpDownload, char *pJson);/*****************************************************************************/
- #endif
- /*****************************************************************************/
|