import "github.com/cloudwalkio/omg-coap"
CoAP Client and Server in Go
[![Build Status](https://drone.io/github.com/cloudwalkio/omg-coap/status.png)](https://drone.io/github.com/cloudwalkio/omg-coap/latest)
- Constants
- Variables
- func Handle
- func HandleFunc
- func ListenAndServe
- func Notify
- func Serve
- func StringToTime
- func TimeToString
- func Transmit
- func TransmitToObserver
- type COAPCode
- type COAPType
- type Conn
- type Handler
- type HandlerFunc
- type MediaType
- type Message
- func Receive
- func ReceiveTimeout
- func (*Message) AddOption
- func (Message) IsConfirmable
- func (Message) IsObserver
- func (Message) Option
- func (Message) Options
- func (Message) Path
- func (Message) PathString
- func (*Message) RemoveOption
- func (*Message) SetOption
- func (*Message) SetPath
- func (*Message) SetPathString
- type OptionID
- type RemoteAddr
- type ServeMux
- type Server
const (
// ResponseTimeout is the amount of time to wait for a
// response.
ResponseTimeout = time.Second * 1
// ResponseRandomFactor is a multiplier for response backoff.
ResponseRandomFactor = 1.5
// MaxRetransmit is the maximum number of times a message will
// be retransmitted.
MaxRetransmit = 0
// Print debug messages
Verbose = false
)
const (
// Confirmable messages require acknowledgements.
Confirmable = COAPType(0)
// NonConfirmable messages do not require acknowledgements.
NonConfirmable = COAPType(1)
// Acknowledgement is a message type indicating a response to
// a confirmable message.
Acknowledgement = COAPType(2)
// Reset indicates a permanent negative acknowledgement.
Reset = COAPType(3)
)
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)
)
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.
var (
ErrInvalidTokenLen = errors.New("invalid token length")
ErrOptionTooLong = errors.New("option is too long")
ErrOptionGapTooLarge = errors.New("option gap too large")
)
Message encoding errors.
var DefaultServeMux = NewServeMux()
var DefaultServer = new(Server)
func Handle
func Handle(pattern string, handler Handler)
func HandleFunc
func HandleFunc(pattern string, handler func(r *RemoteAddr, m *Message) *Message)
func ListenAndServe
func ListenAndServe(addr string, handler Handler) error
Bind to the given address and serve requests forever.
func Notify
func Notify(resource string, m *Message)
Notify observers of resource.
func Serve
func Serve(l *net.UDPConn, handler Handler) error
func StringToTime
func StringToTime(s string) (uint32, error)
StringToTime translates the RRSIG's incep. and expir. times from string values like "20110403154150" to an 32 bit integer. It takes serial arithmetic (RFC 1982) into observe option.
func TimeToString
func TimeToString(t uint32) string
TimeToString translates the RRSIG's incep. and expir. times to the string representation used when printing the record. It takes serial arithmetic (RFC 1982) into observe option.
func Transmit
func Transmit(r *RemoteAddr, m Message) error
Unicast message to remote address
func TransmitToObserver
func TransmitToObserver(resource, id string, m *Message) (done chan bool)
Transmit to an observer
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
// deprecated
SUBSCRIBE COAPCode = 5
)
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.
func (COAPType) String
func (t COAPType) String() string
type Conn
type Conn struct {
}
Conn is a CoAP client connection.
func Dial
func Dial(n, addr string) (*Conn, error)
Dial connects a CoAP client.
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(r *RemoteAddr, m *Message) *Message
}
Handler is a type that handles CoAP messages.
type HandlerFunc
type HandlerFunc func(r *RemoteAddr, m *Message) *Message
func (HandlerFunc) ServeCOAP
func (f HandlerFunc) ServeCOAP(r *RemoteAddr, m *Message) *Message
type MediaType
type MediaType byte
MediaType specifies the content type of a message.
type Message
type Message struct {
Type COAPType
Code COAPCode
MessageID uint16
Token, Payload []byte
}
Message is a CoAP message.
func Receive
func Receive(l *net.UDPConn, buf []byte) (Message, error)
Receive a message.
func ReceiveTimeout
func ReceiveTimeout(l *net.UDPConn, rt time.Duration, buf []byte) (Message, error)
Receive a message with timeout.
func (*Message) AddOption
func (m *Message) AddOption(opId OptionID, val interface{})
AddOption adds an option.
func (Message) IsConfirmable
func (m Message) IsConfirmable() bool
IsConfirmable returns true if this message is confirmable.
func (Message) IsObserver
func (m Message) IsObserver() bool
IsObserver returns true if this message is for observe.
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{}
Get 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) 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 LocationPath attribute on this message.
func (*Message) SetPathString
func (m *Message) SetPathString(s string)
SetPathString sets a path by a / separated string.
type OptionID
type OptionID uint8
Option IDs.
type RemoteAddr
type RemoteAddr struct {
*net.UDPAddr
}
type ServeMux
type ServeMux struct {
}
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, handler func(r *RemoteAddr, m *Message) *Message)
HandleFunc configures a handler for the given path.
func (*ServeMux) ServeCOAP
func (mux *ServeMux) ServeCOAP(r *RemoteAddr, m *Message) *Message
ServeCOAP handles a single COAP message. The message arrives from the given listener having originated from the given UDPAddr.
type Server
type Server struct {
Addr string // UDP address to listen on, ":coap" if empty
Handler Handler // handler to invoke, coap.ServeMux if nil
ReadTimeout time.Duration // maximum duration before timing out read of the request
WriteTimeout time.Duration // maximum duration before timing out write of the response
MaxHeaderBytes int // maximum size of request headers, DefaultMaxHeaderBytes if 0
}
func (*Server) ListenAndServe
func (srv *Server) ListenAndServe() error
func (*Server) Serve
func (srv *Server) Serve(l *net.UDPConn) error