message

package
v2.6.0 Latest Latest
Warning

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

Go to latest
Published: Jun 15, 2022 License: Apache-2.0 Imports: 11 Imported by: 61

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},
}

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 GetPathBufferSize added in v2.6.0

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 VerifyOptLen added in v2.6.0

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 = 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)
	AppCoapGroup      MediaType = 256   //coap-group+json (RFC 7390)
	AppOcfCbor        MediaType = 10000 //application/vnd.ocf+cbor
	AppLwm2mTLV       MediaType = 11542 //application/vnd.oma.lwm2m+tlv
	AppLwm2mJSON      MediaType = 11543 //application/vnd.oma.lwm2m+json
)

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
	// Context context of request.
	Context context.Context
	// Body of message. It is nil for message without body.
	Body io.ReadSeeker
}

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.

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 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 added in v2.3.0

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 added in v2.6.0

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 added in v2.6.0

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 ContentFormat 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 added in v2.5.0

func (t Token) Hash() uint64

func (Token) String

func (t Token) 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