coapmsg

package
v0.0.0-...-ead4ddb Latest Latest
Warning

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

Go to latest
Published: Oct 18, 2021 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

We use this options outside of the package. It's more similar to the http Header API

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidTokenLen   = errors.New("invalid token length")
	ErrOptionTooLong     = errors.New("option is too long")
	ErrOptionGapTooLarge = errors.New("option gap too large")
)

Message encoding errors.

Functions

This section is empty.

Types

type COAPCode

type COAPCode uint8

COAPCode is the type used for both request and response codes.

const (
	GET    COAPCode = 1 // 0.01
	POST   COAPCode = 2 // 0.02
	PUT    COAPCode = 3 // 0.03
	DELETE COAPCode = 4 // 0.04
)

Request Codes

const (
	Empty                 COAPCode = 0   // 0.00
	Created               COAPCode = 65  // 2.01
	Deleted               COAPCode = 66  // 2.02
	Valid                 COAPCode = 67  // 2.03
	Changed               COAPCode = 68  // 2.04
	Content               COAPCode = 69  // 2.05
	BadRequest            COAPCode = 128 // 4.00
	Unauthorized          COAPCode = 129 // 4.01
	BadOption             COAPCode = 130 // 4.02
	Forbidden             COAPCode = 131 // 4.03
	NotFound              COAPCode = 132 // 4.04
	MethodNotAllowed      COAPCode = 133 // 4.05
	NotAcceptable         COAPCode = 134 // 4.06
	PreconditionFailed    COAPCode = 140 // 4.12
	RequestEntityTooLarge COAPCode = 141 // 4.13
	UnsupportedMediaType  COAPCode = 143 // 4.15
	InternalServerError   COAPCode = 160 // 5.00
	NotImplemented        COAPCode = 161 // 5.01
	BadGateway            COAPCode = 162 // 5.02
	ServiceUnavailable    COAPCode = 163 // 5.03
	GatewayTimeout        COAPCode = 164 // 5.04
	ProxyingNotSupported  COAPCode = 165 // 5.05
)

Response Codes

func BuildCode

func BuildCode(class, detail uint8) COAPCode

func (COAPCode) Class

func (c COAPCode) Class() uint8

First 3 bits of the code [0, 7]

func (COAPCode) Detail

func (c COAPCode) Detail() uint8

Last 5 bits of the code [0, 31]

func (COAPCode) IsError

func (c COAPCode) IsError() bool

func (COAPCode) IsSuccess

func (c COAPCode) IsSuccess() bool

func (COAPCode) Number

func (c COAPCode) Number() uint8

func (COAPCode) String

func (c COAPCode) String() string

type COAPType

type COAPType uint8

COAPType represents the message type.

const (
	// Confirmable messages require acknowledgements.
	Confirmable COAPType = 0
	// NonConfirmable messages do not require acknowledgements.
	NonConfirmable COAPType = 1
	// Acknowledgement is a message indicating a response to confirmable message.
	Acknowledgement COAPType = 2
	// Reset indicates a permanent negative acknowledgement.
	Reset COAPType = 3
)

func (COAPType) String

func (t COAPType) String() string

type CoapOptions

type CoapOptions map[OptionId]Option

A CoapOptions represents a option mapping keys to sets of values.

func (CoapOptions) Add

func (h CoapOptions) Add(key OptionId, value interface{}) error

Add adds the key, value pair to the header. It appends to any existing values associated with key.

func (CoapOptions) Clear

func (h CoapOptions) Clear()

Clear deletes all options.

func (CoapOptions) Del

func (h CoapOptions) Del(key OptionId)

Del deletes the values associated with key.

func (CoapOptions) Get

func (h CoapOptions) Get(key OptionId) Option

Get gets the first value associated with the given key. If there are no values associated with the key, Get returns NilOptionValue. Get is a convenience method. For more complex queries, access the map directly.

func (CoapOptions) Set

func (h CoapOptions) Set(key OptionId, value interface{}) error

Set sets the header entries associated with key to the single element value. It replaces any existing values associated with key.

func (CoapOptions) String

func (h CoapOptions) String() string

type MediaType

type MediaType byte

MediaType specifies the content type of a message.

const (
	TextPlain     MediaType = 0  // text/plain;charset=utf-8
	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
)

Content types.

type Message

type Message struct {
	Type      COAPType
	Code      COAPCode
	MessageID uint16

	Token, Payload []byte
	// contains filtered or unexported fields
}

Message is a CoAP message.

func NewAck

func NewAck(messageId uint16) Message

func NewMessage

func NewMessage() Message

func NewPing

func NewPing(messageId uint16) Message

NewPing creates a an Empty Confirmable message (CoAP ping)

func NewRst

func NewRst(messageId uint16) Message

func ParseMessage

func ParseMessage(data []byte) (Message, error)

func (*Message) IsConfirmable

func (m *Message) IsConfirmable() bool

IsConfirmable returns true if this message is confirmable.

func (*Message) IsNonConfirmable

func (m *Message) IsNonConfirmable() bool

IsConfirmable returns true if this message is confirmable.

func (*Message) MarshalBinary

func (m *Message) MarshalBinary() ([]byte, error)

Fulfill the encoding.BinaryMarshaler interface

func (*Message) MustMarshalBinary

func (m *Message) MustMarshalBinary() []byte

MarshalBinary produces the binary form of this Message.

func (*Message) Options

func (m *Message) Options() CoapOptions

func (*Message) Path

func (m *Message) Path() []string

Path gets the Path set on this message if any.

func (*Message) PathString

func (m *Message) PathString() string

PathString gets a path as a / separated string.

func (*Message) SetOptions

func (m *Message) SetOptions(o CoapOptions)

func (*Message) SetPath

func (m *Message) SetPath(s []string)

SetPath updates or adds a URIPath attribute on this message.

func (*Message) SetPathString

func (m *Message) SetPathString(s string)

SetPathString sets a path by a / separated string.

func (*Message) String

func (m *Message) String() string

func (*Message) UnmarshalBinary

func (m *Message) UnmarshalBinary(data []byte) error

UnmarshalBinary parses the given binary slice as a Message.

type Option

type Option struct {
	Id OptionId
	// contains filtered or unexported fields
}

func (Option) AsBytes

func (o Option) AsBytes() []byte

In case of multiple option values it returns the first

func (Option) AsString

func (o Option) AsString() string

In case of multiple option values it returns the first

func (Option) AsUInt16

func (o Option) AsUInt16() uint16

In case of multiple option values it returns the first

func (Option) AsUInt32

func (o Option) AsUInt32() uint32

In case of multiple option values it returns the first

func (Option) AsUInt64

func (o Option) AsUInt64() uint64

In case of multiple option values it returns the first

func (Option) AsUInt8

func (o Option) AsUInt8() uint8

In case of multiple option values it returns the first

func (Option) IsNotSet

func (o Option) IsNotSet() bool

func (Option) IsSet

func (o Option) IsSet() bool

func (Option) Len

func (o Option) Len() int

func (Option) String

func (o Option) String() string

Pretty print option

type OptionDef

type OptionDef struct {
	Number       OptionId
	MinLength    int
	MaxLength    int
	DefaultValue []byte // Or interface{} or OptionValue?
	Repeatable   bool
	Format       ValueFormat
}

Currently only used in tests to find options

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
	ProxyURI      OptionId = 35
	ProxyScheme   OptionId = 39
	Size1         OptionId = 60
)

Option IDs.

func (OptionId) Critical

func (o OptionId) Critical() bool

func (OptionId) NoCacheKey

func (o OptionId) NoCacheKey() bool

NoCacheKey only has a meaning for options that are Safe-to-Forward

func (OptionId) String

func (i OptionId) String() string

func (OptionId) UnSafe

func (o OptionId) UnSafe() bool

"Unsafe to forward" proxies will not forward unsafe options

type OptionValue

type OptionValue struct {
	// contains filtered or unexported fields
}
var NilOptionValue OptionValue = OptionValue{/* contains filtered or unexported fields */}

func (OptionValue) AsBytes

func (v OptionValue) AsBytes() []byte

func (OptionValue) AsString

func (v OptionValue) AsString() string

func (OptionValue) AsUInt16

func (v OptionValue) AsUInt16() uint16

For signed values just convert the result

func (OptionValue) AsUInt32

func (v OptionValue) AsUInt32() uint32

For signed values just convert the result

func (OptionValue) AsUInt64

func (v OptionValue) AsUInt64() uint64

For signed values just convert the result

func (OptionValue) AsUInt8

func (v OptionValue) AsUInt8() uint8

For signed values just convert the result

func (OptionValue) Len

func (v OptionValue) Len() int

type ValueFormat

type ValueFormat uint8

Option value format (RFC7252 section 3.2) Defines the option format inside the packet

const (
	ValueUnknown ValueFormat = iota
	ValueEmpty               // A zero-length sequence of bytes.
	ValueOpaque              // An opaque sequence of bytes.
	// A non-negative integer that is represented in network byte
	// order using the number of bytes given by the Option Length
	// field.
	ValueUint
	// A Unicode string that is encoded using UTF-8 [RFC3629] in
	// Net-Unicode form [RFC5198].
	ValueString
)

func (ValueFormat) PrettyPrint

func (f ValueFormat) PrettyPrint(val OptionValue) string

Jump to

Keyboard shortcuts

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