Documentation ¶
Overview ¶
Package coap provides a CoAP client and server.
Index ¶
- Constants
- Variables
- func ListenAndServe(n, addr string, rh Handler) error
- func Serve(listener *net.UDPConn, rh Handler) error
- func Transmit(l *net.UDPConn, a *net.UDPAddr, m Message) error
- type COAPCode
- type COAPType
- type Conn
- type DgramMessage
- type Handler
- type MediaType
- type Message
- type MessageBase
- func (m *MessageBase) AddOption(opID OptionID, val interface{})
- func (m *MessageBase) AllOptions() options
- func (m *MessageBase) Code() COAPCode
- func (m *MessageBase) IsConfirmable() bool
- func (m *MessageBase) MessageID() uint16
- func (m *MessageBase) Option(o OptionID) interface{}
- func (m *MessageBase) Options(o OptionID) []interface{}
- func (m *MessageBase) Path() []string
- func (m *MessageBase) PathString() string
- func (m *MessageBase) Payload() []byte
- func (m *MessageBase) RemoveOption(opID OptionID)
- func (m *MessageBase) SetObserve(b int)
- func (m *MessageBase) SetOption(opID OptionID, val interface{})
- func (m *MessageBase) SetPath(s []string)
- func (m *MessageBase) SetPathString(s string)
- func (m *MessageBase) SetPayload(p []byte)
- func (m *MessageBase) SetURIQuery(s string)
- func (m *MessageBase) Token() []byte
- func (m *MessageBase) Type() COAPType
- type MessageParams
- type OptionID
- type ServeMux
- type TcpMessage
Constants ¶
const ( // ResponseTimeout is the amount of time to wait for a // response. ResponseTimeout = 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 )
const ( TCP_MESSAGE_LEN13_BASE = 13 TCP_MESSAGE_LEN14_BASE = 269 TCP_MESSAGE_LEN15_BASE = 65805 TCP_MESSAGE_MAX_LEN = 0x7fff0000 // Large number that works in 32-bit builds. )
Variables ¶
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 ¶
ListenAndServe binds to the given address and serve requests forever.
Types ¶
type COAPCode ¶
type COAPCode uint8
COAPCode is the type used for both request and response codes.
const ( Created COAPCode = 65 Deleted COAPCode = 66 Valid COAPCode = 67 Changed COAPCode = 68 Content COAPCode = 69 BadRequest COAPCode = 128 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 GatewayTimeout COAPCode = 164 ProxyingNotSupported COAPCode = 165 )
Response Codes
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 )
type Conn ¶
type Conn struct {
// contains filtered or unexported fields
}
Conn is a CoAP client connection.
type DgramMessage ¶
type DgramMessage struct {
MessageBase
}
DgramMessage implements Message interface.
func NewDgramMessage ¶
func NewDgramMessage(p MessageParams) *DgramMessage
func ParseDgramMessage ¶
func ParseDgramMessage(data []byte) (*DgramMessage, error)
ParseDgramMessage extracts the Message from the given input.
func (*DgramMessage) MarshalBinary ¶
func (m *DgramMessage) MarshalBinary() ([]byte, error)
MarshalBinary produces the binary form of this DgramMessage.
func (*DgramMessage) UnmarshalBinary ¶
func (m *DgramMessage) UnmarshalBinary(data []byte) error
UnmarshalBinary parses the given binary slice as a DgramMessage.
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.
type MediaType ¶
type MediaType uint16
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 interface { Type() COAPType Code() COAPCode MessageID() uint16 Token() []byte Payload() []byte AllOptions() options IsConfirmable() bool Options(o OptionID) []interface{} Option(o OptionID) interface{} Path() []string PathString() string SetPathString(s string) SetPath(s []string) SetURIQuery(s string) SetObserve(b int) SetPayload(p []byte) RemoveOption(opID OptionID) AddOption(opID OptionID, val interface{}) SetOption(opID OptionID, val interface{}) MarshalBinary() ([]byte, error) UnmarshalBinary(data []byte) error // contains filtered or unexported methods }
type MessageBase ¶
type MessageBase struct {
// contains filtered or unexported fields
}
MessageBase is a CoAP message.
func (*MessageBase) AddOption ¶
func (m *MessageBase) AddOption(opID OptionID, val interface{})
AddOption adds an option.
func (*MessageBase) AllOptions ¶
func (m *MessageBase) AllOptions() options
func (*MessageBase) Code ¶
func (m *MessageBase) Code() COAPCode
func (*MessageBase) IsConfirmable ¶
func (m *MessageBase) IsConfirmable() bool
IsConfirmable returns true if this message is confirmable.
func (*MessageBase) MessageID ¶
func (m *MessageBase) MessageID() uint16
func (*MessageBase) Option ¶
func (m *MessageBase) Option(o OptionID) interface{}
Option gets the first value for the given option ID.
func (*MessageBase) Options ¶
func (m *MessageBase) Options(o OptionID) []interface{}
Options gets all the values for the given option.
func (*MessageBase) Path ¶
func (m *MessageBase) Path() []string
Path gets the Path set on this message if any.
func (*MessageBase) PathString ¶
func (m *MessageBase) PathString() string
PathString gets a path as a / separated string.
func (*MessageBase) Payload ¶
func (m *MessageBase) Payload() []byte
func (*MessageBase) RemoveOption ¶
func (m *MessageBase) RemoveOption(opID OptionID)
RemoveOption removes all references to an option
func (*MessageBase) SetObserve ¶
func (m *MessageBase) SetObserve(b int)
Set Observer attribute to the message
func (*MessageBase) SetOption ¶
func (m *MessageBase) SetOption(opID OptionID, val interface{})
SetOption sets an option, discarding any previous value
func (*MessageBase) SetPath ¶
func (m *MessageBase) SetPath(s []string)
SetPath updates or adds a URIPath attribute on this message.
func (*MessageBase) SetPathString ¶
func (m *MessageBase) SetPathString(s string)
SetPathString sets a path by a / separated string.
func (*MessageBase) SetURIQuery ¶
func (m *MessageBase) SetURIQuery(s string)
Set URIQuery attibute to the message
func (*MessageBase) Token ¶
func (m *MessageBase) Token() []byte
func (*MessageBase) Type ¶
func (m *MessageBase) Type() COAPType
type MessageParams ¶
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.
type TcpMessage ¶
type TcpMessage struct {
MessageBase
}
TcpMessage is a CoAP MessageBase 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 NewTcpMessage ¶
func NewTcpMessage(p MessageParams) *TcpMessage
func PullTcp ¶
func PullTcp(data []byte) (*TcpMessage, []byte, error)
PullTcp extracts a complete TCP CoAP message from the front of a byte queue.
Return values:
*TcpMessage: On success, points to the extracted message; nil if a complete message could not be extracted. []byte: The unread portion of of the supplied byte buffer. If a message was not extracted, this is the unchanged buffer that was passed in. error: Non-nil if the buffer contains an invalid CoAP message.
Note: It is not an error if the supplied buffer does not contain a complete message. In such a case, nil *TclMessage and error values are returned along with the original buffer.
func (*TcpMessage) MarshalBinary ¶
func (m *TcpMessage) MarshalBinary() ([]byte, error)
func (*TcpMessage) UnmarshalBinary ¶
func (m *TcpMessage) UnmarshalBinary(data []byte) error