coap

package module
v0.0.0-...-4698924 Latest Latest
Warning

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

Go to latest
Published: Jun 27, 2017 License: MIT Imports: 11 Imported by: 4

README

Constrained Application Protocol Client and Server for go

You can read more about CoAP in RFC 7252. I also did some preliminary work on SUBSCRIBE support from an early draft.

Differences from original dustin/go-coap

  1. Added minor helper function to populate URI options.
  2. Added configurable receive timeout in backgwards compatible way. Changed ResponseTimeout variable to DefaultResponseTimeout to emphasise its role change and to be able to track down its usage within package.
  3. Example programs package references have been changed from dustin/go-coap to Kulak/go-coap.
  4. Message can be cloned.

Motivation for all this change: need to support existing application functionality.

Documentation

Overview

Package coap provides a CoAP client and server.

Index

Constants

View Source
const (
	// DefaultResponseTimeout is the amount of time to wait for a
	// response.
	DefaultResponseTimeout = time.Second * 2
	// ResponseRandomFactor is a multiplier for response backoff.
	ResponseRandomFactor = 1.5
	// MaxRetransmit is the maximum number of times a message will
	// be retransmitted.
	MaxRetransmit = 4
)

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

func ListenAndServe

func ListenAndServe(n, addr string, rh Handler) error

ListenAndServe binds to the given address and serve requests forever.

func Serve

func Serve(listener *net.UDPConn, rh Handler) error

Serve processes incoming UDP packets on the given listener, and processes these requests forever (or until the listener is closed).

func Transmit

func Transmit(l *net.UDPConn, a *net.UDPAddr, m Message) error

Transmit a message.

Types

type COAPCode

type COAPCode uint8

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

const (
	GET    COAPCode = 1
	POST   COAPCode = 2
	PUT    COAPCode = 3
	DELETE COAPCode = 4
)

Request Codes

const (
	Created               COAPCode = 65
	Deleted               COAPCode = 66
	Valid                 COAPCode = 67
	Changed               COAPCode = 68
	Content               COAPCode = 69
	BadRequest            COAPCode = 128
	Unauthorized          COAPCode = 129
	BadOption             COAPCode = 130
	Forbidden             COAPCode = 131
	NotFound              COAPCode = 132
	MethodNotAllowed      COAPCode = 133
	NotAcceptable         COAPCode = 134
	PreconditionFailed    COAPCode = 140
	RequestEntityTooLarge COAPCode = 141
	UnsupportedMediaType  COAPCode = 143
	InternalServerError   COAPCode = 160
	NotImplemented        COAPCode = 161
	BadGateway            COAPCode = 162
	ServiceUnavailable    COAPCode = 163
	GatewayTimeout        COAPCode = 164
	ProxyingNotSupported  COAPCode = 165
)

Response Codes

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 Conn

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

Conn is a CoAP client connection.

func Dial

func Dial(n, addr string) (*Conn, error)

Dial connects a CoAP client.

func DialWithTimeout

func DialWithTimeout(n, addr string, responseTimeout time.Duration) (*Conn, error)

DialWithTimeout provides control of response timeout.

func (*Conn) Receive

func (c *Conn) Receive() (*Message, error)

Receive a message.

func (*Conn) Send

func (c *Conn) Send(req Message) (*Message, error)

Send a message. Get a response if there is one.

type Handler

type Handler interface {
	// Handle the message and optionally return a response message.
	ServeCOAP(l *net.UDPConn, a *net.UDPAddr, m *Message) *Message
}

Handler is a type that handles CoAP messages.

func FuncHandler

func FuncHandler(f func(l *net.UDPConn, a *net.UDPAddr, m *Message) *Message) Handler

FuncHandler builds a handler from a function.

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 Receive

func Receive(l *net.UDPConn, buf []byte, responseTimeout time.Duration) (Message, error)

Receive a message.

func (*Message) AddOption

func (m *Message) AddOption(opID OptionID, val interface{})

AddOption adds an option.

func (Message) Clone

func (m Message) Clone() Message

Clone creates a copy of Message structure. It is necessary, because opts are not exposed and replication of options based on the given API is not a simple task.

func (Message) IsConfirmable

func (m Message) IsConfirmable() bool

IsConfirmable returns true if this message is confirmable.

func (*Message) MarshalBinary

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

MarshalBinary produces the binary form of this 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) 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) Query

func (m Message) Query() []string

Query gets the URI-Query option set on this message if any.

func (Message) QueryParamValue

func (m Message) QueryParamValue(name string, separator string) (string, error)

func (Message) QueryString

func (m Message) QueryString() string

QueryString assembles query options by joining with ampresand. Question mark is not pre-pended.

func (*Message) RemoveOption

func (m *Message) RemoveOption(opID OptionID)

RemoveOption removes all references to an option

func (*Message) SetOption

func (m *Message) SetOption(opID OptionID, val interface{})

SetOption sets an option, discarding any previous value

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

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

SetPath updates or adds a URIPath attribute on this message.

func (*Message) SetQueryString

func (m *Message) SetQueryString(s string)

SetQueryString sets a query by a & separated string. If query string starts with '?' character, '?' is ignored.

func (*Message) UnmarshalBinary

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

UnmarshalBinary parses the given binary slice as a Message.

type OptionID

type OptionID uint8

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.

type ServeMux

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

ServeMux provides mappings from a common endpoint to handlers by request path.

func NewServeMux

func NewServeMux() *ServeMux

NewServeMux creates a new ServeMux.

func (*ServeMux) Handle

func (mux *ServeMux) Handle(pattern string, handler Handler)

Handle configures a handler for the given path.

func (*ServeMux) HandleFunc

func (mux *ServeMux) HandleFunc(pattern string,
	f func(l *net.UDPConn, a *net.UDPAddr, m *Message) *Message)

HandleFunc configures a handler for the given path.

func (*ServeMux) ServeCOAP

func (mux *ServeMux) ServeCOAP(l *net.UDPConn, a *net.UDPAddr, m *Message) *Message

ServeCOAP handles a single COAP message. The message arrives from the given listener having originated from the given UDPAddr.

type TcpMessage

type TcpMessage struct {
	Message
}

TcpMessage is a CoAP Message that can encode itself for TCP transport.

func Decode

func Decode(r io.Reader) (*TcpMessage, error)

Decode reads a single message from its input.

func (*TcpMessage) MarshalBinary

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

func (*TcpMessage) UnmarshalBinary

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

Directories

Path Synopsis
example

Jump to

Keyboard shortcuts

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