message

package
Version: v2.0.0-...-691db5a Latest Latest
Warning

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

Go to latest
Published: Jun 8, 2021 License: Apache-2.0 Imports: 11 Imported by: 3

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 shor 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 calculate ETag from payload via CRC64

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 {
	// Context context of request.
	Context context.Context
	Token   Token
	Code    codes.Code
	Options Options
	// 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 {
	ID    OptionID
	Value []byte
}

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 {
	ValueFormat ValueFormat
	MinLen      int
	MaxLen      int
}

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 get's accept option.

func (Options) Add

func (options Options) Add(opt Option) Options

Add append's option to options.

func (Options) AddBytes

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

AddBytes append's byte's option to options.

Return's 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 append's string option to options.

Return's 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 append's uint32 option to options.

Return's 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 get's content format of body.

func (Options) Find

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

Find return's 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 get's first option with id of type byte's.

func (Options) GetBytess

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

GetBytess get's all options with same id.

func (Options) GetString

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

GetString get's first option with id of type string.

func (Options) GetStrings

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

GetStrings get's all options with same id.

func (Options) GetUint32

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

GetUint32 get's first option with id of type uint32.

func (Options) GetUint32s

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

GetUint32s get's all options with same id.

func (Options) HasOption

func (options Options) HasOption(id OptionID) bool

HasOption return's true is option exist in options.

func (Options) Marshal

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

Marshal marshal's options to buf.

Return's number of used buf byte's.

func (Options) Observe

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

Observe get's observe option.

func (Options) Path

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

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

Return's number of used buf bytes or error when occurs.

func (Options) Queries

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

Queries get's URIQuery parameters.

func (Options) Remove

func (options Options) Remove(ID OptionID) Options

Remove remove's all options with ID.

func (Options) ResetOptionsTo

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

ResetOptionsTo reset's options to in options.

Return's modified options, number of used buf bytes and error if occurs.

func (Options) Set

func (options Options) Set(opt Option) Options

Set replace's/store's option at options.

Return's modified options.

func (Options) SetAccept

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

SetAccept set's accept option.

func (Options) SetBytes

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

SetBytes replace's/store's byte's option to options.

Return's 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 set's ContentFormat option.

func (Options) SetObserve

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

SetObserve set's ContentFormat option.

func (Options) SetPath

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

SetPath splits path by '/' to URIPath options and copy it to buffer.

Return's modified options, number of used buf bytes and error if occurs.

func (Options) SetString

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

SetString replace's/store's string option to options.

Return's 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 replace's/store's uint32 option to options.

Return's 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 unmarshal's data bytes to options and returns number of consumned byte's.

type Token

type Token []byte

func GetToken

func GetToken() (Token, error)

GetToken generates a random token by a given length

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