Documentation ¶
Index ¶
- Constants
- Variables
- func CRC16Bytes(data []byte) uint16
- func CRC32Bytes(data []byte) uint32
- func CRC32String(str string) uint32
- func DecodeUint32(buf []byte) (uint32, int, error)
- func EncodeUint32(buf []byte, value uint32) (int, error)
- func FlagIsNoAck(flags uint32) bool
- func GetEncoder(encoderTypeX string) (encoderType int32, encoderID int32)
- func GetEncoderType(encoderType int32, encoderID int32) string
- func GetMID() int32
- func GetPathBufferSize(path string) (int, error)
- func RSUM8(data []byte) byte
- func RandMID() int32
- func ValidateEID(eid int32) bool
- func ValidateETP(etp int32) bool
- func ValidateMID(mid int32) bool
- func ValidateType(typ Type) bool
- func ValidateVer(typ Ver) bool
- func VerifyOptLen(optionDefs map[OptionID]OptionDef, optionID OptionID, valueLen int) bool
- type Code
- type MediaType
- type Message
- func (m *Message) AddOption(opID OptionID, val interface{})
- func (m *Message) Analyse() string
- func (m Message) IsConfirmable() bool
- func (m Message) Option(o OptionID) interface{}
- func (m Message) Options(o OptionID) []interface{}
- func (m Message) Path() []string
- func (m Message) PathString() string
- func (m *Message) RemoveOption(opID OptionID)
- func (m *Message) SetOption(opID OptionID, val interface{})
- func (m *Message) SetPath(s []string)
- func (m *Message) SetPathString(s string)
- func (m *Message) String() string
- type Option
- func (o Option) Marshal(buf []byte, previousID OptionID) (int, error)
- func (o Option) MarshalValue(buf []byte) (int, error)
- func (o Option) String() string
- func (o Option) ToBytes() []byte
- func (o *Option) Unmarshal(optionDefs map[OptionID]OptionDef, optionID OptionID, data []byte) (int, error)
- func (o *Option) UnmarshalValue(optionDefs map[OptionID]OptionDef, buf []byte) (int, error)
- type OptionDef
- type OptionID
- type Options
- func (options Options) Accept() (MediaType, error)
- func (options Options) Add(opt Option) Options
- func (options Options) AddBytes(buf []byte, id OptionID, data []byte) (Options, int, error)
- func (options Options) AddString(buf []byte, id OptionID, str string) (Options, int, error)
- func (options Options) AddUint32(buf []byte, id OptionID, value uint32) (Options, int, error)
- func (options Options) Clone() (Options, error)
- func (options Options) ContentFormat() (MediaType, error)
- func (options Options) Find(id OptionID) (int, int, error)
- func (options Options) GetBytes(id OptionID) ([]byte, error)
- func (options Options) GetBytess(id OptionID, r [][]byte) (int, error)
- func (options Options) GetString(id OptionID) (string, error)
- func (options Options) GetStrings(id OptionID, r []string) (int, error)
- func (options Options) GetUint32(id OptionID) (uint32, error)
- func (options Options) GetUint32s(id OptionID, r []uint32) (int, error)
- func (options Options) HasOption(id OptionID) bool
- func (o Options) Len() int
- func (o Options) Less(i, j int) bool
- func (options Options) LocationPath() (string, error)
- func (options Options) Marshal(buf []byte) (int, error)
- func (o Options) Minus(oid OptionID) Options
- func (options Options) Observe() (uint32, error)
- func (options Options) Path() (string, error)
- func (options Options) Queries() ([]string, error)
- func (options Options) Remove(id OptionID) Options
- func (options Options) ResetOptionsTo(buf []byte, in Options) (Options, int, error)
- func (options Options) Set(opt Option) Options
- func (options Options) SetAccept(buf []byte, contentFormat MediaType) (Options, int, error)
- func (options Options) SetBytes(buf []byte, id OptionID, data []byte) (Options, int, error)
- func (options Options) SetContentFormat(buf []byte, contentFormat MediaType) (Options, int, error)
- func (options Options) SetLocationPath(buf []byte, path string) (Options, int, error)
- func (options Options) SetObserve(buf []byte, observe uint32) (Options, int, error)
- func (options Options) SetPath(buf []byte, path string) (Options, int, error)
- func (options Options) SetString(buf []byte, id OptionID, str string) (Options, int, error)
- func (options Options) SetUint32(buf []byte, id OptionID, value uint32) (Options, int, error)
- func (options Options) String(sep string) string
- func (o Options) Swap(i, j int)
- func (options Options) URL() string
- func (options *Options) Unmarshal(data []byte, optionDefs map[OptionID]OptionDef) (int, error)
- type Rand
- type Token
- type Type
- type ValueFormat
- type Ver
Constants ¶
const ( // Indicates an Empty message Empty Code = 0 // Request Codes GET Code = 1 POST Code = 2 PUT Code = 3 DELETE Code = 4 // Response Codes Created Code = 65 Deleted Code = 66 Valid Code = 67 Changed Code = 68 Content Code = 69 Continue Code = 95 BadRequest Code = 128 BadOption Code = 130 Forbidden Code = 131 NotFound Code = 132 MethodNotAllowed Code = 133 NotAcceptable Code = 134 RequestEntityIncomplete Code = 136 PreconditionFailed Code = 140 RequestEntityTooLarge Code = 141 UnsupportedMediaType Code = 143 TooManyRequests Code = 157 InternalServerError Code = 160 NotImplemented Code = 161 BadGateway Code = 162 GatewayTimeout Code = 164 ProxyingNotSupported Code = 165 // 6.00-6.31 Reserved GiterlabErrnoOk = 192 // 正常响应 [PV1/PV2] GiterlabErrnoParamConfigure = 193 // 有新的配置参数 [PV2] GiterlabErrnoFirmwareUpdate = 194 // 有新的固件可以更新 [PV2] GiterlabErrnoUserCommand = 195 // 有用户命令需要执行 [PV2] GiterlabErrnoEnterFlightMode = 220 // 进入飞行模式[PV2] // 7.00-7.31 Reserved GiterlabErrnoIllegalKey = 224 // KEY错误,设备激活码错误 [PV1/PV2] GiterlabErrnoDataError = 225 // 数据错误 [PV1/PV2] GiterlabErrnoDeviceNotExist = 226 // 设备不存在或设备传感器类型匹配错误 [PV1/PV2] GiterlabErrnoTimeExpired = 227 // 时间过期 [PV1/PV2] GiterlabErrnoNotSupportProtocolVersion = 228 // 不支持的协议版本 [PV1/PV2] GiterlabErrnoProtocolParsingErrors = 229 // 议解析错误 [PV1/PV2] GiterlabErrnoRequestTimeout = 230 // [*]请求超时 [PV1/PV2] GiterlabErrnoOptProtocolParsingErrors = 231 // 可选附加头解析错误 [PV1/PV2] GiterlabErrnoNotSupportAnalyticalMethods = 232 // 不支持的可选附加头解析方法 [PV1/PV2] GiterlabErrnoNotSupportPacketType = 233 // 不支持的包类型 [PV1/PV2] GiterlabErrnoDataDecodingError = 234 // 数据解码错误 [PV1/PV2] GiterlabErrnoPackageLengthError = 235 // 数据包长度字段错误 [PV1/PV2] GiterlabErrnoDuoxieyunServerRequestBusy = 236 // [*]多协云服务器请求失败 [PV1过时了] GiterlabErrnoSluanServerRequestBusy = 237 // [*]石峦服务器请求失败 [PV2过时了] GiterlabErrnoCacheServiceErrors = 238 // [*]缓存服务出错 [PV1/PV2] GiterlabErrnoTableStoreServiceErrors = 239 // [*]表格存储服务出错 [PV1/PV2] GiterlabErrnoDatabaseServiceErrors = 240 // [*]数据库存储出错 [PV1/PV2] GiterlabErrnoNotSupportEncodingType = 241 // 不支持的编码类型 [PV1/PV2] GiterlabErrnoDeviceRepeatRegistered = 242 // 设备重复注册 [PV2] GiterlabErrnoDeviceSimCardUsed = 243 // 设备手机卡重复使用 [PV2] GiterlabErrnoDeviceSimCardIllegal = 244 // 设备手机卡未登记,非法的SIM卡 [PV2] GiterlabErrnoDeviceUpdateForcedFailed = 245 // 强制更新设备信息失败 [PV2] )
const ( // EncoderTypeNoneUserDefine none/userdefine EncoderTypeNoneUserDefine = "none/userdefine" // EncoderTypeTextBase64 text/base64 EncoderTypeTextBase64 = "text/base64" // EncoderTypeTextPlain text/plain EncoderTypeTextPlain = "text/plain" // EncoderTypeTextHex text/hex EncoderTypeTextHex = "text/hex" // EncoderTypeApplicationOctetStream application/octet-stream EncoderTypeApplicationOctetStream = "application/octet-stream" // EncoderTypeApplicationProtobuf application/protobuf EncoderTypeApplicationProtobuf = "application/protobuf" // EncoderTypeApplicationJson application/json EncoderTypeApplicationJson = "application/json" )
const ( ExtendOptionByteCode = 13 ExtendOptionByteAddend = 13 ExtendOptionWordCode = 14 ExtendOptionWordAddend = 269 ExtendOptionError = 15 )
const (
FlagNoAck = 0x00000001
)
const MaxTokenSize = 8
MaxTokenSize maximum of token size that can be used in message
Variables ¶
var ( ErrTooSmall = errors.New("too small bytes buffer") ErrShortRead = errors.New("invalid short read") ErrInvalidOptionHeaderExt = errors.New("invalid option header ext") ErrInvalidTokenLen = errors.New("invalid token length") ErrInvalidValueLength = errors.New("invalid value length") ErrInvalidEncoding = errors.New("invalid encoding") ErrOptionTruncated = errors.New("option truncated") ErrOptionUnexpectedExtendMarker = errors.New("option unexpected extend marker") ErrOptionsTooSmall = errors.New("too small options buffer") ErrOptionTooLong = errors.New("option is too long") ErrOptionGapTooLarge = errors.New("option gap too large") ErrOptionNotFound = errors.New("option not found") ErrOptionDuplicate = errors.New("duplicated option") ErrMessageNil = errors.New("message is nil") ErrMessageTruncated = errors.New("message is truncated") ErrMessageInvalidVersion = errors.New("message has invalid version") ErrMessageInvalidRSUM8 = errors.New("message has invalid rsum8") ErrInvalidRCRC16 = errors.New("message has invalid crc16") )
var CoapOptionDefs = map[OptionID]OptionDef{ IfMatch: {ValueFormat: ValueOpaque, MinLen: 0, MaxLen: 8}, URIHost: {ValueFormat: ValueString, MinLen: 1, MaxLen: 255}, ETag: {ValueFormat: ValueOpaque, MinLen: 1, MaxLen: 8}, IfNoneMatch: {ValueFormat: ValueEmpty, MinLen: 0, MaxLen: 0}, Observe: {ValueFormat: ValueUint, MinLen: 0, MaxLen: 3}, URIPort: {ValueFormat: ValueUint, MinLen: 0, MaxLen: 2}, LocationPath: {ValueFormat: ValueString, MinLen: 0, MaxLen: 255}, URIPath: {ValueFormat: ValueString, MinLen: 0, MaxLen: 255}, ContentFormat: {ValueFormat: ValueUint, MinLen: 0, MaxLen: 2}, MaxAge: {ValueFormat: ValueUint, MinLen: 0, MaxLen: 4}, URIQuery: {ValueFormat: ValueString, MinLen: 0, MaxLen: 255}, Accept: {ValueFormat: ValueUint, MinLen: 0, MaxLen: 2}, LocationQuery: {ValueFormat: ValueString, MinLen: 0, MaxLen: 255}, Block2: {ValueFormat: ValueUint, MinLen: 0, MaxLen: 3}, Block1: {ValueFormat: ValueUint, MinLen: 0, MaxLen: 3}, Size2: {ValueFormat: ValueUint, MinLen: 0, MaxLen: 4}, ProxyURI: {ValueFormat: ValueString, MinLen: 1, MaxLen: 1034}, ProxyScheme: {ValueFormat: ValueString, MinLen: 1, MaxLen: 255}, Size1: {ValueFormat: ValueUint, MinLen: 0, MaxLen: 4}, GiterLabID: {ValueFormat: ValueString, MinLen: 0, MaxLen: 255}, GiterLabKey: {ValueFormat: ValueString, MinLen: 0, MaxLen: 255}, AccessID: {ValueFormat: ValueString, MinLen: 0, MaxLen: 255}, AccessKey: {ValueFormat: ValueString, MinLen: 0, MaxLen: 255}, CheckCRC32: {ValueFormat: ValueUint, MinLen: 0, MaxLen: 4}, EncoderType: {ValueFormat: ValueUint, MinLen: 0, MaxLen: 4}, EncoderID: {ValueFormat: ValueUint, MinLen: 0, MaxLen: 4}, Flags: {ValueFormat: ValueUint, MinLen: 0, MaxLen: 4}, PackageNumber: {ValueFormat: ValueUint, MinLen: 0, MaxLen: 4}, }
Functions ¶
func GetEncoder ¶
GetEncoder 根据编码器,获取对应的编码类型
func GetEncoderType ¶
GetEncoderType 获取协议 Payload 编码类型, coap协议默认是 application/protobuf
func GetPathBufferSize ¶
GetPathBufferSize gets the size of the buffer required to store path in URI-Path options.
If the path cannot be stored an error is returned.
func ValidateEID ¶ added in v1.0.1
ValidateEID validates a message eid for Payload. (0 <= eid <= 15)
func ValidateETP ¶ added in v1.0.1
ValidateETP validates a message etp for Payload. (0 <= etp <= 15)
func ValidateMID ¶
ValidateMID validates a message id for UDP. (0 <= mid <= 65535)
func ValidateType ¶
ValidateType validates the type for UDP. (0 <= typ <= 255)
func ValidateVer ¶ added in v1.0.3
ValidateVer validates the ver for UDP. (0 <= typ <= 3)
Types ¶
type MediaType ¶
type MediaType uint16
MediaType specifies the content type of a message.
const ( TextPlain MediaType = 0 // text/plain; charset=utf-8 AppCoseEncrypt0 MediaType = 16 // application/cose; cose-type="cose-encrypt0" (RFC 8152) AppCoseMac0 MediaType = 17 // application/cose; cose-type="cose-mac0" (RFC 8152) AppCoseSign1 MediaType = 18 // application/cose; cose-type="cose-sign1" (RFC 8152) AppLinkFormat MediaType = 40 // application/link-format AppXML MediaType = 41 // application/xml AppOctets MediaType = 42 // application/octet-stream AppExi MediaType = 47 // application/exi AppJSON MediaType = 50 // application/json AppJSONPatch MediaType = 51 // application/json-patch+json (RFC6902) AppJSONMergePatch MediaType = 52 // application/merge-patch+json (RFC7396) AppCBOR MediaType = 60 // application/cbor (RFC 7049) AppCWT MediaType = 61 // application/cwt AppCoseEncrypt MediaType = 96 // application/cose; cose-type="cose-encrypt" (RFC 8152) AppCoseMac MediaType = 97 // application/cose; cose-type="cose-mac" (RFC 8152) AppCoseSign MediaType = 98 // application/cose; cose-type="cose-sign" (RFC 8152) AppCoseKey MediaType = 101 // application/cose-key (RFC 8152) AppCoseKeySet MediaType = 102 // application/cose-key-set (RFC 8152) AppSenmlJSON MediaType = 110 // application/senml+json AppSenmlCbor MediaType = 112 // application/senml+cbor AppCoapGroup MediaType = 256 // coap-group+json (RFC 7390) AppSenmlEtchJSON MediaType = 320 // application/senml-etch+json AppSenmlEtchCbor MediaType = 322 // application/senml-etch+cbor AppOcfCbor MediaType = 10000 // application/vnd.ocf+cbor AppLwm2mTLV MediaType = 11542 // application/vnd.oma.lwm2m+tlv AppLwm2mJSON MediaType = 11543 // application/vnd.oma.lwm2m+json AppLwm2mCbor MediaType = 11544 // application/vnd.oma.lwm2m+cbor )
Content types.
func ToMediaType ¶
type Message ¶
type Message struct { Ver Ver Token Token Opts Options Code Code Payload []byte // For DTLS and UDP messages MessageID int32 // uint16 is valid, all other values are invalid, -1 is used for unset Type Type // uint8 is valid, all other values are invalid, -1 is used for unset // Additional fields EncoderID int32 // 4 bits is valid, all other values are invalid, -1 is used for unset EncoderType int32 // 4 bits is valid, all other values are invalid, -1 is used for unset // Checksum Crc16 uint16 Rsum8 uint8 }
Message is a Secoap message.
func (Message) IsConfirmable ¶
IsConfirmable returns true if this message is confirmable.
func (Message) PathString ¶
PathString gets a path as a / separated string.
func (*Message) RemoveOption ¶
RemoveOption removes all references to an option
func (*Message) SetPathString ¶
SetPathString sets a path by a / separated string.
type Option ¶
type Option struct { ID OptionID Value interface{} }
func (Option) Marshal ¶
Marshal 将 Option 按照 Option Format 序列化到 buf 中, previousID 为前一个 Option 的 ID, 用于计算 Option Delta
type OptionDef ¶
type OptionDef struct { MinLen int MaxLen int ValueFormat ValueFormat }
type OptionID ¶
type OptionID uint32
OptionID identifies an option in a message.
const ( IfMatch OptionID = 1 URIHost OptionID = 3 ETag OptionID = 4 IfNoneMatch OptionID = 5 Observe OptionID = 6 URIPort OptionID = 7 LocationPath OptionID = 8 URIPath OptionID = 11 ContentFormat OptionID = 12 MaxAge OptionID = 14 URIQuery OptionID = 15 Accept OptionID = 17 LocationQuery OptionID = 20 Block2 OptionID = 23 Block1 OptionID = 27 Size2 OptionID = 28 ProxyURI OptionID = 35 ProxyScheme OptionID = 39 Size1 OptionID = 60 NoResponse OptionID = 258 // The IANA policy for future additions to this sub-registry is split // into three tiers as follows. The range of 0..255 is reserved for // options defined by the IETF (IETF Review or IESG Approval). The // range of 256..2047 is reserved for commonly used options with public // specifications (Specification Required). The range of 2048..64999 is // for all other options including private or vendor-specific ones, // which undergo a Designated Expert review to help ensure that the // option semantics are defined correctly. The option numbers between // 65000 and 65535 inclusive are reserved for experiments. They are not // meant for vendor-specific use of any kind and MUST NOT be used in // operational deployments. GiterLabID OptionID = 65000 GiterLabKey OptionID = 65001 AccessID OptionID = 65002 AccessKey OptionID = 65003 CheckCRC32 OptionID = 65004 EncoderType OptionID = 65005 EncoderID OptionID = 65006 Flags OptionID = 65007 PackageNumber OptionID = 65100 )
Option IDs.
func ToOptionID ¶
type Options ¶
type Options []Option
func (Options) AddBytes ¶
AddBytes appends bytes of a option to options.
Returns modified options, number of used buf bytes and error if occurs.
func (Options) AddString ¶
AddString appends string option to options.
Returns modified options, number of used buf bytes and error if occurs.
func (Options) AddUint32 ¶
AddUint32 appends uint32 option to options.
Returns modified options, number of used buf bytes and error if occurs.
func (Options) ContentFormat ¶
ContentFormat gets the content format of body.
func (Options) Find ¶
Find returns range of type options. First number is index and second number is index of next option type.
func (Options) GetStrings ¶
GetStrings gets string array of all options with the given id.
func (Options) GetUint32s ¶
GetUint32s gets all options with same id.
func (Options) LocationPath ¶
LocationPath joins Location-Path options by '/' to the buf.
Returns number of used buf bytes or error when occurs.
func (Options) Path ¶
Path joins URIPath options by '/' to the buf.
Returns number of used buf bytes or error when occurs.
func (Options) ResetOptionsTo ¶
ResetOptionsTo resets options to in options.
Returns modified options, number of used buf bytes and error if occurs.
func (Options) SetBytes ¶
SetBytes replaces/stores bytes of a option to options.
Returns modified options, number of used buf bytes and error if occurs.
func (Options) SetContentFormat ¶
SetContentFormat sets ContentFormat option.
func (Options) SetLocationPath ¶
SetLocationPath splits path by '/' to LocationPath options and copies it to buffer.
Returns modified options, number of used buf bytes and error if occurs.
@note the url encoded into LocationPath is expected to be absolute (https://www.rfc-editor.org/rfc/rfc7252.txt)
func (Options) SetObserve ¶
SetObserve sets Observe option.
func (Options) SetPath ¶
SetPath splits path by '/' to URIPath options and copies it to buffer.
Returns modified options, number of used buf bytes and error if occurs.
@note the url encoded into URIHost, URIPort, URIPath is expected to be absolute (https://www.rfc-editor.org/rfc/rfc7252.txt)
func (Options) SetString ¶
SetString replaces/stores string option to options.
Returns modified options, number of used buf bytes and error if occurs.
func (Options) SetUint32 ¶
SetUint32 replaces/stores uint32 option to options.
Returns modified options, number of used buf bytes and error if occurs.
type Type ¶
type Type int16
Type represents the message type. It's only part of Secoap UDP messages. Reliable transports like TCP do not have a type.
const ( // Used for unset Unset Type = -1 // Confirmable messages require acknowledgements. Confirmable Type = 0 // NonConfirmable messages do not require acknowledgements. NonConfirmable Type = 1 // Acknowledgement is a message indicating a response to confirmable message. Acknowledgement Type = 2 // Reset indicates a permanent negative acknowledgement. Reset Type = 3 )
type ValueFormat ¶
type ValueFormat uint8
Option value format (RFC7252 section 3.2)
const ( ValueUnknown ValueFormat = iota ValueEmpty ValueOpaque ValueUint ValueString )
type Ver ¶ added in v1.0.3
type Ver int8
Ver represents the message ver. It's only part of Secoap UDP messages.
func GetVersion ¶ added in v1.0.5
GetVersion gets the version from the payload.