message

package
v3.3.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 2, 2024 License: Apache-2.0 Imports: 14 Imported by: 75

Documentation

Index

Constants

View Source
const (
	ExtendOptionByteCode   = 13
	ExtendOptionByteAddend = 13
	ExtendOptionWordCode   = 14
	ExtendOptionWordAddend = 269
	ExtendOptionError      = 15
)
View Source
const MaxTokenSize = 8

MaxTokenSize maximum of token size that can be used in message

Variables

View Source
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")
)
View Source
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},
	NoResponse:    {ValueFormat: ValueUint, MinLen: 0, MaxLen: 1},
}
View Source
var TCPSignalAbortOptionDefs = map[OptionID]OptionDef{
	TCPBadCSMOption: {ValueFormat: ValueUint, MinLen: 0, MaxLen: 2},
}
View Source
var TCPSignalCSMOptionDefs = map[OptionID]OptionDef{
	TCPMaxMessageSize:    {ValueFormat: ValueUint, MinLen: 0, MaxLen: 4},
	TCPBlockWiseTransfer: {ValueFormat: ValueEmpty, MinLen: 0, MaxLen: 0},
}
View Source
var TCPSignalPingPongOptionDefs = map[OptionID]OptionDef{
	TCPCustody: {ValueFormat: ValueEmpty, MinLen: 0, MaxLen: 0},
}
View Source
var TCPSignalReleaseOptionDefs = map[OptionID]OptionDef{
	TCPAlternativeAddress: {ValueFormat: ValueString, MinLen: 1, MaxLen: 255},
	TCPHoldOff:            {ValueFormat: ValueUint, MinLen: 0, MaxLen: 3},
}

Functions

func DecodeUint32

func DecodeUint32(buf []byte) (uint32, int, error)

func EncodeUint32

func EncodeUint32(buf []byte, value uint32) (int, error)

func GetETag

func GetETag(r io.ReadSeeker) ([]byte, error)

GetETag calculates ETag from payload via CRC64

func GetMID

func GetMID() int32

GetMID generates a message id for UDP. (0 <= mid <= 65535)

func GetPathBufferSize

func GetPathBufferSize(path string) (int, error)

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 RandMID

func RandMID() int32

func ValidateMID added in v3.0.2

func ValidateMID(mid int32) bool

ValidateMID validates a message id for UDP. (0 <= mid <= 65535)

func ValidateType added in v3.0.2

func ValidateType(typ Type) bool

ValidateType validates the type for UDP. (0 <= typ <= 255)

func VerifyOptLen

func VerifyOptLen(optID OptionID, valueLen int) bool

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)
	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 ToMediaType

func ToMediaType(v string) (MediaType, error)

func (MediaType) String

func (c MediaType) String() string

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
}

func (*Message) IsPing added in v3.3.0

func (r *Message) IsPing(isTCP bool) bool

IsPing returns true if the message is a ping.

func (*Message) String

func (r *Message) String() string

type Option

type Option struct {
	Value []byte
	ID    OptionID
}

func (Option) Marshal

func (o Option) Marshal(buf []byte, previousID OptionID) (int, error)

func (Option) MarshalValue

func (o Option) MarshalValue(buf []byte) (int, error)

func (*Option) Unmarshal

func (o *Option) Unmarshal(data []byte, optionDefs map[OptionID]OptionDef, optionID OptionID) (int, error)

func (*Option) UnmarshalValue

func (o *Option) UnmarshalValue(buf []byte) (int, error)

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
	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
)

Option IDs.

const (
	TCPMaxMessageSize    OptionID = 2
	TCPBlockWiseTransfer OptionID = 4
)
const (
	TCPAlternativeAddress OptionID = 2
	TCPHoldOff            OptionID = 4
)
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

func ToOptionID(v string) (OptionID, error)

func (OptionID) String

func (o OptionID) String() string

type Options

type Options []Option

Options Container of COAP Options, It must be always sort'ed after modification.

func (Options) Accept

func (options Options) Accept() (MediaType, error)

Accept gets accept option.

func (Options) Add

func (options Options) Add(opt Option) Options

Add appends option to options.

func (Options) AddBytes

func (options Options) AddBytes(buf []byte, id OptionID, data []byte) (Options, int, error)

AddBytes appends bytes of a option to options.

Returns modified options, number of used buf bytes and error if occurs.

func (Options) AddString

func (options Options) AddString(buf []byte, id OptionID, str string) (Options, int, error)

AddString appends string option to options.

Returns modified options, number of used buf bytes and error if occurs.

func (Options) AddUint32

func (options Options) AddUint32(buf []byte, id OptionID, value uint32) (Options, int, error)

AddUint32 appends uint32 option to options.

Returns modified options, number of used buf bytes and error if occurs.

func (Options) Clone

func (options Options) Clone() (Options, error)

Clone create duplicates of options.

func (Options) ContentFormat

func (options Options) ContentFormat() (MediaType, error)

ContentFormat gets the content format of body.

func (Options) Find

func (options Options) Find(id OptionID) (int, int, error)

Find returns range of type options. First number is index and second number is index of next option type.

func (Options) GetBytes

func (options Options) GetBytes(id OptionID) ([]byte, error)

GetBytes gets bytes of the first option with given id.

func (Options) GetBytess

func (options Options) GetBytess(id OptionID, r [][]byte) (int, error)

GetBytess gets array of bytes of all options with the same id.

func (Options) GetString

func (options Options) GetString(id OptionID) (string, error)

GetString gets the string value of the first option with the given ID.

func (Options) GetStrings

func (options Options) GetStrings(id OptionID, r []string) (int, error)

GetStrings gets string array of all options with the given id.

func (Options) GetUint32

func (options Options) GetUint32(id OptionID) (uint32, error)

GetUint32 gets the uin32 value of the first option with the given ID.

func (Options) GetUint32s

func (options Options) GetUint32s(id OptionID, r []uint32) (int, error)

GetUint32s gets all options with same id.

func (Options) HasOption

func (options Options) HasOption(id OptionID) bool

HasOption returns true is option exist in options.

func (Options) LocationPath

func (options Options) LocationPath() (string, error)

LocationPath joins Location-Path options by '/' to the buf.

Returns number of used buf bytes or error when occurs.

func (Options) Marshal

func (options Options) Marshal(buf []byte) (int, error)

Marshal marshals options to buf.

Returns the number of used buf bytes.

func (Options) Observe

func (options Options) Observe() (uint32, error)

Observe gets Observe option.

func (Options) Path

func (options Options) Path() (string, error)

Path joins URIPath options by '/' to the buf.

Returns number of used buf bytes or error when occurs.

func (Options) Queries

func (options Options) Queries() ([]string, error)

Queries gets URIQuery parameters.

func (Options) Remove

func (options Options) Remove(id OptionID) Options

Remove removes all options with ID.

func (Options) ResetOptionsTo

func (options Options) ResetOptionsTo(buf []byte, in Options) (Options, int, error)

ResetOptionsTo resets options to in options.

Returns modified options, number of used buf bytes and error if occurs.

func (Options) Set

func (options Options) Set(opt Option) Options

Set replaces/stores option at options.

Returns modified options.

func (Options) SetAccept

func (options Options) SetAccept(buf []byte, contentFormat MediaType) (Options, int, error)

SetAccept sets accept option.

func (Options) SetBytes

func (options Options) SetBytes(buf []byte, id OptionID, data []byte) (Options, int, error)

SetBytes replaces/stores bytes of a option to options.

Returns modified options, number of used buf bytes and error if occurs.

func (Options) SetContentFormat

func (options Options) SetContentFormat(buf []byte, contentFormat MediaType) (Options, int, error)

SetContentFormat sets ContentFormat option.

func (Options) SetLocationPath

func (options Options) SetLocationPath(buf []byte, path string) (Options, int, error)

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

func (options Options) SetObserve(buf []byte, observe uint32) (Options, int, error)

SetObserve sets Observe option.

func (Options) SetPath

func (options Options) SetPath(buf []byte, path string) (Options, int, error)

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

func (options Options) SetString(buf []byte, id OptionID, str string) (Options, int, error)

SetString replaces/stores string option to options.

Returns modified options, number of used buf bytes and error if occurs.

func (Options) SetUint32

func (options Options) SetUint32(buf []byte, id OptionID, value uint32) (Options, int, error)

SetUint32 replaces/stores uint32 option to options.

Returns modified options, number of used buf bytes and error if occurs.

func (*Options) Unmarshal

func (options *Options) Unmarshal(data []byte, optionDefs map[OptionID]OptionDef) (int, error)

Unmarshal unmarshals data bytes to options and returns the number of consumed bytes.

type Token

type Token []byte

func GetToken

func GetToken() (Token, error)

GetToken generates a random token by a given length

func (Token) Hash

func (t Token) Hash() uint64

func (Token) String

func (t Token) String() string

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
)

func (Type) String

func (t Type) String() string

type ValueFormat

type ValueFormat uint8

Option value format (RFC7252 section 3.2)

const (
	ValueUnknown ValueFormat = iota
	ValueEmpty
	ValueOpaque
	ValueUint
	ValueString
)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL