Categorygithub.com/GiterLab/go-secoap
modulepackage
1.1.0
Repository: https://github.com/giterlab/go-secoap.git
Documentation: pkg.go.dev

# README

go-secoap

secoap go语言实现,私有协议,一种coap协议的变种

secoap

secoapsensorh.com 私有定制的一套物联网协议,基于 coap 协议上的改进,可用于设备与服务器之间的通信。

设备与服务器之间的通信(大端模式), 数据内容由 HeaderPayload 组成,具体协议描述如下:

secoap协议框架:

    0                   1                   2                   3
    0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |Ver|  TKL  | T |  EID  |  ETP  |   CRC16                       |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |   Message ID                  |   Code        |   RSUM8       |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |   Token (if any, TKL bytes) ...
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |   Options (if any) ...
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |1 1 1 1 1 1 1 1|    Payload (if any) ...
   | (if Ver != 0) |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

coap协议框架:

    0                   1                   2                   3
    0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |Ver| T |  TKL  |      Code     |          Message ID           |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |   Token (if any, TKL bytes) ...
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |   Options (if any) ...
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |1 1 1 1 1 1 1 1|    Payload (if any) ...
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

不同版本的协议头部格式具体如下:

Ver: 0, 是没有0xFF分割位,CRC16 为小端存储(历史遗留问题),如下所示:

    0                   1                   2                   3
    0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |0 0|R|R|R|R| T |  EID  |  ETP  |    CRC16-L    |    CRC16-H    |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   | Payload (if any) ...
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

Ver: 1, 标准COAP协议,如下所示:

    0                   1                   2                   3
    0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |0 1| T |  TKL  |      Code     |          Message ID           |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |   Token (if any, TKL bytes) ...
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |   Options (if any) ...
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |1 1 1 1 1 1 1 1|    Payload (if any) ...
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

Ver: 2, 有0xFF分割位,CRC16 为大端存储,如下所示:

    0                   1                   2                   3
    0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |1 0|  TKL  | T |  EID  |  ETP  |   CRC16                       |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |   Message ID                  |   Code        |   RSUM8       |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |   Token (if any, TKL bytes) ...
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |   Options (if any) ...
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |1 1 1 1 1 1 1 1|    Payload (if any) ...
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

Header

协议包由定长头(4字节或8字节)组成

  • R: 1 bit,保留位

  • Ver: 2 bits,协议版本号,目前支持 0 和 2 两个版本,其中 1 保留了标准COAP协议的格式

  • TKL: 4 bits,可变长度Token字段(0-8字节)。长度9-15是保留,不能被发送,并且必须处理为消息格式错误。

  • T: 2 bits,包类型

    Confirmable (0) 可确认包,可靠的消息
    Non-confirmable (1) 非可确认包,不可靠的消息
    Acknowledgement (2) 确认包或非可确认包的回复响应
    Reset (3) 重置包,当客户端或服务器无法处理时可重置
    
  • EID: 4 bits,编码ID,用于标识编码类型的不同版本

  • ETP: 4 bits, 编码类型,用于对 Payload 内容的应用编码类型,详见编码类型列表

    ETPEIDContent-Type说明
    00none/userdefine无编码, 不要设置, 字段留空, 为了兼容老设备(未设置编码类型字段),保留用户自定义协议
    10text/base64base64编码
    20text/plain纯文本字符串编码
    30text/hex字符串十六进制编码
    40application/octet-stream二进制编码
    50application/protobufProtobuf编码
    60application/jsonJSON编码
  • CRC16: 校验和,对 Payload 内容进行 CRC16/MODBUS 计算后的结果, 2 bytes, 在 Ver = 0时,采用小端存储,Ver = 1 时,采用大端存储

  • Message ID: 2 bytes,消息ID,用于标识消息的唯一性

  • Code: 1 byte,消息类型,用于标识消息的类型,详见消息类型列表

    Code说明
    1GET
    2POST
    3POST
    3PUT
    4DELETE
    --
    0Empty
    65Created
    66Deleted
    67Valid
    68Changed
    69Content
    95Continue
    128BadRequest
    129Unauthorized
    130BadOption
    131Forbidden
    132NotFound
    133MethodNotAllowed
    134NotAcceptable
    136RequestEntityIncomplete
    140PreconditionFailed
    141RequestEntityTooLarge
    143UnsupportedMediaType
    157TooManyRequests
    160InternalServerError
    161NotImplemented
    162BadGateway
    163ServiceUnavailable
    164GatewayTimeout
    165ProxyingNotSupported
    --
    192GiterlabErrnoOk
    193GiterlabErrnoParamConfigure
    194GiterlabErrnoFirmwareUpdate
    195GiterlabErrnoUserCommand
    220GiterlabErrnoEnterFlightMode
    224GiterlabErrnoIllegalKey
    225GiterlabErrnoDataError
    226GiterlabErrnoDeviceNotExist
    227GiterlabErrnoTimeExpired
    228GiterlabErrnoNotSupportProtocolVersion
    229GiterlabErrnoProtocolParsingErrors
    230GiterlabErrnoRequestTimeout
    231GiterlabErrnoOptProtocolParsingErrors
    232GiterlabErrnoNotSupportAnalyticalMethods
    233GiterlabErrnoNotSupportPacketType
    234GiterlabErrnoDataDecodingError
    235GiterlabErrnoPackageLengthError
    236GiterlabErrnoDuoxieyunServerRequestBusy
    237GiterlabErrnoSluanServerRequestBusy
    238GiterlabErrnoCacheServiceErrors
    239GiterlabErrnoTableStoreServiceErrors
    240GiterlabErrnoDatabaseServiceErrors
    241GiterlabErrnoNotSupportEncodingType
    242GiterlabErrnoDeviceRepeatRegistered
    243GiterlabErrnoDeviceSimCardUsed
    244GiterlabErrnoDeviceSimCardIllegal
    245GiterlabErrnoDeviceUpdateForcedFailed
  • RSUM8: 1 byte,反转校验和,对 HeaderPayload 字段进行校验和计算后的结果,计算公式如下:

        uint8_t rsum8(void *data, uint16_t len) {
            uint8_t sum  = 0;
            uint8_t *pdata = (uint8_t *)data;
    
            while (len--) {
                sum += ~(*pdata);
                pdata++;
            }
            return sum;
        }
    
  • Token: Token字段,长度由 TKL 指定(0 到 8 bytes),Token长度为 0 时,Token字段为空

  • Options: 可选字段,长度不定,用于扩展消息头部信息,Type-Length-Value (TLV) format, 详见 CoAP-Option Format

  • Payload: 消息体,长度不定,用于携带消息内容

Payload

Payload 为设备指定的数据格式,通过指定编码类型进行序列号后的数据。

  • 内容部分: 数据,可扩充对象数据或序列化后的对象数据

# Packages

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Functions

NewSecoap 创建一个Secoap协议实例.

# Constants

Version0 版本0,私有SECOAP协议.
Version1 版本1,标准COAP协议.
Version2 版本2,私有SECOAP协议, 默认使用此协议.

# Structs

Secoap Secoap协议实例.