Documentation
¶
Index ¶
- Constants
- Variables
- func DecodeUint32(buf []byte) (uint32, int, error)
- func EncodeUint32(buf []byte, value uint32) (int, error)
- func GetETag(r io.ReadSeeker) ([]byte, error)
- func GetMID() int32
- func GetPathBufferSize(path string) (int, error)
- func RandMID() int32
- func ValidateMID(mid int32) bool
- func ValidateType(typ Type) bool
- func VerifyOptLen(optID OptionID, valueLen int) bool
- type MediaType
- type Message
- type Option
- 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 (options Options) LocationPath() (string, error)
- func (options Options) Marshal(buf []byte) (int, error)
- 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) Unmarshal(data []byte, optionDefs map[OptionID]OptionDef) (int, error)
- type Token
- type Type
- type ValueFormat
Constants ¶
const ( ExtendOptionByteCode = 13 ExtendOptionByteAddend = 13 ExtendOptionWordCode = 14 ExtendOptionWordAddend = 269 ExtendOptionError = 15 )
const MaxTokenSize = 8
MaxTokenSize maximum of token size that can be used in message
Variables ¶
var ( ErrTooSmall = errors.New("too small bytes buffer") ErrInvalidOptionHeaderExt = errors.New("invalid option header ext") ErrInvalidTokenLen = errors.New("invalid token length") ErrInvalidValueLength = errors.New("invalid value length") ErrShortRead = errors.New("invalid short read") ErrOptionTruncated = errors.New("option truncated") ErrOptionUnexpectedExtendMarker = errors.New("option unexpected extend marker") ErrOptionsTooSmall = errors.New("too small options buffer") ErrInvalidEncoding = errors.New("invalid encoding") ErrOptionNotFound = errors.New("option not found") ErrOptionDuplicate = errors.New("duplicated option") ErrPayloadMarkerWithoutPayload = errors.New("payload marker followed by zero-length payload") )
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}, HopLimit: {ValueFormat: ValueUint, MinLen: 1, MaxLen: 1}, Accept: {ValueFormat: ValueUint, MinLen: 0, MaxLen: 2}, QBlock1: {ValueFormat: ValueUint, MinLen: 0, MaxLen: 3}, 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}, QBlock2: {ValueFormat: ValueUint, MinLen: 0, MaxLen: 3}, ProxyURI: {ValueFormat: ValueString, MinLen: 1, MaxLen: 1034}, ProxyScheme: {ValueFormat: ValueString, MinLen: 1, MaxLen: 255}, Size1: {ValueFormat: ValueUint, MinLen: 0, MaxLen: 4}, Echo: {ValueFormat: ValueOpaque, MinLen: 1, MaxLen: 40}, NoResponse: {ValueFormat: ValueUint, MinLen: 0, MaxLen: 1}, RequestTag: {ValueFormat: ValueOpaque, MinLen: 0, MaxLen: 8}, }
var TCPSignalAbortOptionDefs = map[OptionID]OptionDef{ TCPBadCSMOption: {ValueFormat: ValueUint, MinLen: 0, MaxLen: 2}, }
var TCPSignalCSMOptionDefs = map[OptionID]OptionDef{ TCPMaxMessageSize: {ValueFormat: ValueUint, MinLen: 0, MaxLen: 4}, TCPBlockWiseTransfer: {ValueFormat: ValueEmpty, MinLen: 0, MaxLen: 0}, }
var TCPSignalPingPongOptionDefs = map[OptionID]OptionDef{ TCPCustody: {ValueFormat: ValueEmpty, MinLen: 0, MaxLen: 0}, }
var TCPSignalReleaseOptionDefs = map[OptionID]OptionDef{ TCPAlternativeAddress: {ValueFormat: ValueString, MinLen: 1, MaxLen: 255}, TCPHoldOff: {ValueFormat: ValueUint, MinLen: 0, MaxLen: 3}, }
Functions ¶
func GetETag ¶
func GetETag(r io.ReadSeeker) ([]byte, error)
GetETag calculates ETag from payload via CRC64
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 ValidateMID ¶ added in v3.0.2
ValidateMID validates a message id for UDP. (0 <= mid <= 65535)
func ValidateType ¶ added in v3.0.2
ValidateType validates the type for UDP. (0 <= typ <= 255)
func VerifyOptLen ¶
VerifyOptLen checks whether valueLen is within (min, max) length limits for given option.
Types ¶
type MediaType ¶
type MediaType uint16
MediaType specifies the content format of a message.
var ( TextPlain MediaType // 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) AppMissingBlocksCBORSeq MediaType = 272 // application/missing-blocks+cbor-seq (RFC 9177) 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 formats.
func MediaTypeFromNumber ¶ added in v3.3.5
func MediaTypeFromNumber[T constraints.Integer](v T) (MediaType, error)
func ToMediaType ¶
type Message ¶
type Message struct {
Token Token
Options Options
Code codes.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
}
type OptionDef ¶
type OptionDef struct {
MinLen uint32
MaxLen uint32
ValueFormat ValueFormat
}
type OptionID ¶
type OptionID uint16
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 // OSCORE is the critical option defined by RFC 8613. An OSCORE-enabled // transport must process it before dispatching the request to an application handler. OSCORE OptionID = 9 URIPath OptionID = 11 ContentFormat OptionID = 12 MaxAge OptionID = 14 URIQuery OptionID = 15 HopLimit OptionID = 16 Accept OptionID = 17 QBlock1 OptionID = 19 LocationQuery OptionID = 20 Block2 OptionID = 23 Block1 OptionID = 27 Size2 OptionID = 28 QBlock2 OptionID = 31 ProxyURI OptionID = 35 ProxyScheme OptionID = 39 Size1 OptionID = 60 Echo OptionID = 252 NoResponse OptionID = 258 RequestTag OptionID = 292 )
Option IDs.
const (
TCPBadCSMOption OptionID = 2
)
Signal Abort Option IDs
+-----+---+---+---------------------+--------+--------+---------+ | No. | C | R | Name | Format | Length | Default | +-----+---+---+---------------------+--------+--------+---------+ | 2 | | | Bad-CSM-Option | uint | 0-2 | (none) | +-----+---+---+---------------------+--------+--------+---------+ C=Critical, R=Repeatable
const (
TCPCustody OptionID = 2
)
func ToOptionID ¶
type Options ¶
type Options []Option
Options Container of COAP Options, It must be always sort'ed after modification.
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. If the buffer is too small, the original options remain unchanged and the returned byte count is the size required for the input values.
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. On error, the original options remain unchanged.
@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. On error, the original options remain unchanged.
@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.
type Type ¶
type Type int16
Type represents the message type. It's only part of CoAP 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 )