coap

package module
v1.8.10 Latest Latest
Warning

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

Go to latest
Published: May 5, 2026 License: MPL-2.0 Imports: 17 Imported by: 0

README

coap

GoDoc License ReportCard

https://github.com/qwerty-iot/coap

License

Mozilla Public License Version 2.0

Documentation

Index

Constants

View Source
const (
	LogLevelError string = "error"
	LogLevelWarn  string = "warn"
	LogLevelInfo  string = "info"
	LogLevelDebug string = "debug"
)
View Source
const (
	SniffWrite = "write"
	SniffRead  = "read"
)

Variables

View Source
var (
	ErrTimeout               = errors.New("coap: timeout")
	ErrBadRequest            = errors.New("coap: bad request")
	ErrNotFound              = errors.New("coap: not found")
	ErrUnauthorized          = errors.New("coap: not authorized")
	ErrMethodNotAllowed      = errors.New("coap: method not allowed")
	ErrEncodingNotAcceptable = errors.New("coap: encoding not acceptable")
	ErrInternalServerError   = errors.New("coap: internal server error")
	ErrInvalidTokenLen       = errors.New("coap: invalid token length")
	ErrOptionTooLong         = errors.New("coap: option is too long")
	ErrOptionGapTooLarge     = errors.New("coap: option gap too large")
)

Functions

func NstartClear added in v1.6.8

func NstartClear(addr string)

func NstartEntryCount added in v1.6.15

func NstartEntryCount() int

func ObserveRegister

func ObserveRegister(token string, path string, callback ObserveCallback, arg interface{})

func ObserveTokens

func ObserveTokens(callback func(string))

func RspCodeToError

func RspCodeToError(code COAPCode) error

func SetLogFunc

func SetLogFunc(lf LogFunc)

func SetLogLevel

func SetLogLevel(level string)

func SetSniffPacketsCallback added in v1.6.6

func SetSniffPacketsCallback(callback SniffPacketsCallback)

Types

type BlockMetadata added in v1.1.0

type BlockMetadata struct {
	Size int
	More bool
	Num  int
}

func (*BlockMetadata) Encode added in v1.1.0

func (bm *BlockMetadata) Encode() []byte

type COAPCode

type COAPCode uint8

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

const (
	CodeEmpty COAPCode = 0

	CodeGet    COAPCode = 1
	CodePost   COAPCode = 2
	CodePut    COAPCode = 3
	CodeDelete COAPCode = 4
	CodeFetch  COAPCode = 5
	CodePatch  COAPCode = 6
	CodeIPatch COAPCode = 7
)

Request Codes

const (
	RspCodeCreated                 COAPCode = 65
	RspCodeDeleted                 COAPCode = 66
	RspCodeValid                   COAPCode = 67
	RspCodeChanged                 COAPCode = 68
	RspCodeContent                 COAPCode = 69
	RspCodeContinue                COAPCode = 95
	RspCodeBadRequest              COAPCode = 128
	RspCodeUnauthorized            COAPCode = 129
	RspCodeBadOption               COAPCode = 130
	RspCodeForbidden               COAPCode = 131
	RspCodeNotFound                COAPCode = 132
	RspCodeMethodNotAllowed        COAPCode = 133
	RspCodeNotAcceptable           COAPCode = 134
	RspCodeRequestEntityIncomplete COAPCode = 136
	RspCodePreconditionFailed      COAPCode = 140
	RspCodeRequestEntityTooLarge   COAPCode = 141
	RspCodeUnsupportedMediaType    COAPCode = 143
	RspCodeInternalServerError     COAPCode = 160
	RspCodeNotImplemented          COAPCode = 161
	RspCodeBadGateway              COAPCode = 162
	RspCodeServiceUnavailable      COAPCode = 163
	RspCodeGatewayTimeout          COAPCode = 164
	RspCodeProxyingNotSupported    COAPCode = 165
)

Response Codes

func ToCOAPCode added in v1.4.0

func ToCOAPCode(val string) COAPCode

func (COAPCode) NumberString added in v1.4.0

func (c COAPCode) NumberString() string

func (COAPCode) String

func (c COAPCode) String() string

type COAPType

type COAPType uint8

COAPType represents the message type.

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

func (COAPType) String

func (t COAPType) String() string

type Config

type Config struct {
	DeduplicateExpiration   time.Duration
	DeduplicateInterval     time.Duration
	ObserveNotFoundCallback ObserveNotFoundCallback
	BlockDefaultSize        int
	BlockInactivityTimeout  time.Duration
	MaxMessageDefaultSize   int
	NStart                  int
	Name                    string
	Ref                     any
	ProxyCallbacks          map[string]ProxyFunction
}

func NewConfig added in v1.5.2

func NewConfig() *Config

func (*Config) AddProxyReceiver added in v1.7.1

func (c *Config) AddProxyReceiver(prefix string, f ProxyFunction)

type DtlsListener added in v1.3.0

type DtlsListener struct {
	// contains filtered or unexported fields
}

func (*DtlsListener) Close added in v1.4.0

func (l *DtlsListener) Close()

func (*DtlsListener) ClosePeer added in v1.4.8

func (l *DtlsListener) ClosePeer(addr string)

func (*DtlsListener) FindPeer added in v1.3.0

func (l *DtlsListener) FindPeer(addr string) *dtls.Peer

type LogFunc

type LogFunc func(ts time.Time, level string, msg *Message, err error, log string)

type MediaType

type MediaType int

MediaType specifies the content type of a message.

const (
	None          MediaType = -1
	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
	AppCBOR       MediaType = 60    // application/cbor
	AppSenmlCBOR  MediaType = 112   // application/senml_cbor
	AppLwm2mTLV   MediaType = 11542 //application/vnd.oma.lwm2m+tlv
	AppLwm2mJSON  MediaType = 11543 //application/vnd.oma.lwm2m+json
)

Content types.

func (MediaType) String added in v1.6.16

func (m MediaType) String() string

type Message

type Message struct {
	Type      COAPType
	Code      COAPCode
	MessageID uint16
	Token     []byte

	Payload []byte

	PathVars map[string]string

	Meta Metadata
	// contains filtered or unexported fields
}

Message is a CoAP message.

func NewMessage

func NewMessage() *Message

func (*Message) Accept added in v1.0.3

func (m *Message) Accept() MediaType

func (*Message) ContentFormat

func (m *Message) ContentFormat() MediaType

func (*Message) GetBlock1 added in v1.3.11

func (m *Message) GetBlock1() *BlockMetadata

func (*Message) GetBlock2 added in v1.3.11

func (m *Message) GetBlock2() *BlockMetadata

func (*Message) IsConfirmable

func (m *Message) IsConfirmable() bool

IsConfirmable returns true if this message is confirmable.

func (*Message) IsRequest added in v1.3.0

func (m *Message) IsRequest() bool

func (*Message) LocationPath

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

Path gets the Path set on this message if any.

func (*Message) LocationPathString

func (m *Message) LocationPathString() string

PathString gets a path as a / separated string.

func (*Message) MakeReply

func (m *Message) MakeReply(code COAPCode, payload []byte) *Message

func (*Message) Option

func (m *Message) Option(o OptionID) interface{}

Option gets the first value for the given option ID.

func (*Message) Options

func (m *Message) Options(o OptionID) []interface{}

Options gets all the values for the given option.

func (*Message) OptionsMap added in v1.4.0

func (m *Message) OptionsMap() map[string]interface{}

func (*Message) PacketSize

func (m *Message) PacketSize() int

func (*Message) ParseQuery

func (m *Message) ParseQuery() map[string]string

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) QueryString added in v1.1.0

func (m *Message) QueryString() string

func (*Message) RemoveOption

func (m *Message) RemoveOption(opID OptionID)

RemoveOption removes all references to an option

func (*Message) RequiresBlockwise added in v1.1.0

func (m *Message) RequiresBlockwise() bool

func (*Message) WithAccept

func (m *Message) WithAccept(mt MediaType) *Message

func (*Message) WithBlock1 added in v1.1.0

func (m *Message) WithBlock1(bm *BlockMetadata) *Message

func (*Message) WithBlock2 added in v1.1.0

func (m *Message) WithBlock2(bm *BlockMetadata) *Message

func (*Message) WithCode

func (m *Message) WithCode(code COAPCode) *Message

func (*Message) WithContentFormat

func (m *Message) WithContentFormat(mt MediaType) *Message

func (*Message) WithLocationPath

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

func (*Message) WithLocationPathString

func (m *Message) WithLocationPathString(path string) *Message

func (*Message) WithOption

func (m *Message) WithOption(opID OptionID, val interface{}, replace bool) *Message

AddOption adds an option.

func (*Message) WithPath

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

WithPath updates or adds a URIPath attribute on this message.

func (*Message) WithPathString

func (m *Message) WithPathString(s string) *Message

WithPathString sets a path by a / separated string.

func (*Message) WithPayload

func (m *Message) WithPayload(payload []byte) *Message

func (*Message) WithQuery added in v1.0.5

func (m *Message) WithQuery(q map[string]string) *Message

func (*Message) WithSize1 added in v1.1.4

func (m *Message) WithSize1(sz int) *Message

func (*Message) WithSize2 added in v1.1.4

func (m *Message) WithSize2(sz int) *Message

func (*Message) WithToken added in v1.3.0

func (m *Message) WithToken(token []byte) *Message

func (*Message) WithType added in v1.3.0

func (m *Message) WithType(ty COAPType) *Message

type Metadata

type Metadata struct {
	ListenerName   string
	RemoteAddr     string
	DtlsPeer       *dtls.Peer
	ReceivedAt     time.Time
	BlockSize      int
	MaxMessageSize int
	Server         *Server
}

type Observation

type Observation struct {
	// contains filtered or unexported fields
}

type ObserveCallback

type ObserveCallback func(req *Message, arg interface{}) error

type ObserveNotFoundCallback

type ObserveNotFoundCallback func(req *Message) bool

type OptionID

type OptionID uint8

OptionID identifies an option in a message.

const (
	OptIfMatch       OptionID = 1
	OptURIHost       OptionID = 3
	OptETag          OptionID = 4
	OptIfNoneMatch   OptionID = 5
	OptObserve       OptionID = 6
	OptURIPort       OptionID = 7
	OptLocationPath  OptionID = 8
	OptURIPath       OptionID = 11
	OptContentFormat OptionID = 12
	OptMaxAge        OptionID = 14
	OptURIQuery      OptionID = 15
	OptAccept        OptionID = 17
	OptLocationQuery OptionID = 20
	OptBlock2        OptionID = 23
	OptBlock1        OptionID = 27
	OptSize2         OptionID = 28
	OptProxyURI      OptionID = 35
	OptProxyScheme   OptionID = 39
	OptSize1         OptionID = 60
)

Option IDs.

type ProxyFunction added in v1.7.0

type ProxyFunction func(rawReq []byte, to string) error

type RouteCallback

type RouteCallback func(req *Message) *Message

type SendOptions

type SendOptions struct {
	MaxRetransmit  int           `json:"MaxRetransmit"`
	ActTimeout     time.Duration `json:"ActTimeout"`
	RandomFactor   float64       `json:"RandomFactor"`
	BlockSize      int           `json:"BlockSize"`
	MaxMessageSize int           `json:"MaxMessageSize"`
	NStart         int           `json:"NStart"`
}

func (*SendOptions) NoRetry

func (so *SendOptions) NoRetry() *SendOptions

func (*SendOptions) WithBlockSize added in v1.2.7

func (so *SendOptions) WithBlockSize(bs int) *SendOptions

func (*SendOptions) WithMaxMessageSize added in v1.8.4

func (so *SendOptions) WithMaxMessageSize(ms int) *SendOptions

func (*SendOptions) WithNStart added in v1.2.7

func (so *SendOptions) WithNStart(ns int) *SendOptions

func (*SendOptions) WithRetry

func (so *SendOptions) WithRetry(count int, timeout time.Duration, randomFactor float64) *SendOptions

type Server added in v1.3.0

type Server struct {
	// contains filtered or unexported fields
}

func NewServer added in v1.3.0

func NewServer(conf *Config, udpAddr string, dtlsListener *dtls.Listener) (*Server, error)

func (*Server) AddRoute added in v1.3.0

func (s *Server) AddRoute(path string, callback RouteCallback)

func (*Server) BlockCacheSize added in v1.3.0

func (s *Server) BlockCacheSize() (int64, int64)

func (*Server) Close added in v1.4.0

func (s *Server) Close()

func (*Server) GetConfig added in v1.7.6

func (s *Server) GetConfig() *Config

func (*Server) GetNextMsgId added in v1.6.0

func (s *Server) GetNextMsgId() uint16

func (*Server) GetPorts added in v1.7.6

func (s *Server) GetPorts() (int, int)

func (*Server) GetRef added in v1.4.0

func (s *Server) GetRef() any

func (*Server) LastActivity added in v1.4.5

func (s *Server) LastActivity() time.Time

func (*Server) NewOptions added in v1.3.0

func (s *Server) NewOptions() *SendOptions

func (*Server) Observe added in v1.3.0

func (s *Server) Observe(addr string, code COAPCode, path string, payload []byte, encoding MediaType, callback ObserveCallback, arg interface{}, options *SendOptions) (string, error)

func (*Server) ObserveCancel added in v1.3.0

func (s *Server) ObserveCancel(addr string, path string, token string, options *SendOptions) error

func (*Server) ProxySend added in v1.3.0

func (s *Server) ProxySend(prefix string, rawReq []byte, from string) ([]byte, error)

func (*Server) Send added in v1.3.0

func (s *Server) Send(addr string, msg *Message, options *SendOptions) (*Message, error)

type SniffPacketsCallback added in v1.6.6

type SniffPacketsCallback func(transportType string, op string, from string, to string, data []byte)

type UdpListener added in v1.3.0

type UdpListener struct {
	// contains filtered or unexported fields
}

func (*UdpListener) Close added in v1.4.0

func (l *UdpListener) Close()

func (*UdpListener) Send added in v1.3.0

func (l *UdpListener) Send(addr string, data []byte) error

Jump to

Keyboard shortcuts

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