Documentation ¶
Overview ¶
Package coap provides a CoAP client and server.
Index ¶
- Constants
- Variables
- func Debug(enable bool)
- func FlagIsNoAck(flags uint32) bool
- func HealthMonitor(enable bool)
- func ListenAndServe(n, addr string, rh Handler) error
- func Serve(listener *net.UDPConn, rh Handler) error
- func SetUserDebug(f TraceFunc)
- func TraceError(format string, v ...interface{})
- func TraceInfo(format string, v ...interface{})
- func Transmit(l *net.UDPConn, a *net.UDPAddr, m Message) error
- type CCode
- type CType
- type Conn
- type Handler
- type MediaType
- type Message
- func (m *Message) AddOption(opID OptionID, val interface{})
- func (m Message) IsConfirmable() bool
- func (m *Message) MarshalBinary() ([]byte, error)
- func (m Message) Option(o OptionID) interface{}
- func (m Message) Options(o OptionID) []interface{}
- func (m Message) Path() []string
- func (m Message) PathString() string
- func (m *Message) RemoveOption(opID OptionID)
- func (m *Message) SetOption(opID OptionID, val interface{})
- func (m *Message) SetPath(s []string)
- func (m *Message) SetPathString(s string)
- func (m *Message) UnmarshalBinary(data []byte) error
- type OptionID
- type ServeMux
- type TCPMessage
- type TraceFunc
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 ( LevelEmergency = iota LevelAlert LevelCritical LevelError LevelWarning LevelNotice LevelInformational LevelDebug )
const ( Created CCode = 65 Deleted CCode = 66 Valid CCode = 67 Changed CCode = 68 Content CCode = 69 BadRequest CCode = 128 BadOption CCode = 130 Forbidden CCode = 131 NotFound CCode = 132 MethodNotAllowed CCode = 133 NotAcceptable CCode = 134 PreconditionFailed CCode = 140 RequestEntityTooLarge CCode = 141 UnsupportedMediaType CCode = 143 InternalServerError CCode = 160 NotImplemented CCode = 161 BadGateway CCode = 162 GatewayTimeout CCode = 164 ProxyingNotSupported CCode = 165 // All Code values are assigned by sub-registries according to the // following ranges: // 0.00 Indicates an Empty message (see Section 4.1). // 0.01-0.31 Indicates a request. Values in this range are assigned by // the "CoAP Method Codes" sub-registry (see Section 12.1.1). // 1.00-1.31 Reserved // 2.00-5.31 Indicates a response. Values in this range are assigned by // the "CoAP Response Codes" sub-registry (see // Section 12.1.2). // 6.00-7.31 Reserved // 6.00-6.31 GiterlabErrnoOk = 192 // 正常响应 [PV1/PV2] GiterlabErrnoParamConfigure = 193 // 有新的配置参数 [PV2] GiterlabErrnoFirmwareUpdate = 194 // 有新的固件可以更新 [PV2] GiterlabErrnoUserCommand = 195 // 有用户命令需要执行 [PV2] GiterlabErrnoEnterFlightMode = 220 // 进入飞行模式[PV2] // 7.00-7.31 GiterlabErrnoIllegalKey = 224 // KEY错误,设备激活码错误 [PV1/PV2] GiterlabErrnoDataError = 225 // 数据错误 [PV1/PV2] GiterlabErrnoDeviceNotExist = 226 // 设备不存在或设备传感器类型匹配错误 [PV1/PV2] GiterlabErrnoTimeExpired = 227 // 时间过期 [PV1/PV2] GiterlabErrnoNotSupportProtocolVersion = 228 // 不支持的协议版本 [PV1/PV2] GiterlabErrnoProtocolParsingErrors = 229 // 议解析错误 [PV1/PV2] GiterlabErrnoRequestTimeout = 230 // [*]请求超时 [PV1/PV2] GiterlabErrnoOptProtocolParsingErrors = 231 // 可选附加头解析错误 [PV1/PV2] GiterlabErrnoNotSupportAnalyticalMethods = 232 // 不支持的可选附加头解析方法 [PV1/PV2] GiterlabErrnoNotSupportPacketType = 233 // 不支持的包类型 [PV1/PV2] GiterlabErrnoDataDecodingError = 234 // 数据解码错误 [PV1/PV2] GiterlabErrnoPackageLengthError = 235 // 数据包长度字段错误 [PV1/PV2] GiterlabErrnoDuoxieyunServerRequestBusy = 236 // [*]多协云服务器请求失败 [PV1过时了] GiterlabErrnoSluanServerRequestBusy = 237 // [*]石峦服务器请求失败 [PV2过时了] GiterlabErrnoCacheServiceErrors = 238 // [*]缓存服务出错 [PV1/PV2] GiterlabErrnoTableStoreServiceErrors = 239 // [*]表格存储服务出错 [PV1/PV2] GiterlabErrnoDatabaseServiceErrors = 240 // [*]数据库存储出错 [PV1/PV2] GiterlabErrnoNotSupportEncodingType = 241 // 不支持的编码类型 [PV1/PV2] GiterlabErrnoDeviceRepeatRegistered = 242 // 设备重复注册 [PV2] GiterlabErrnoDeviceSimCardUsed = 243 // 设备手机卡重复使用 [PV2] GiterlabErrnoDeviceSimCardIllegal = 244 // 设备手机卡未登记,非法的SIM卡 [PV2] GiterlabErrnoDeviceUpdateForcedFailed = 245 // 强制更新设备信息失败 [PV2] )
Response Codes
const (
FlagNoAck = 0x00000001
)
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 CType ¶
type CType uint8
CType represents the message type.
const ( // Confirmable messages require acknowledgements. Confirmable CType = 0 // NonConfirmable messages do not require acknowledgements. NonConfirmable CType = 1 // Acknowledgement is a message indicating a response to confirmable message. Acknowledgement CType = 2 // Reset indicates a permanent negative acknowledgement. Reset CType = 3 )
type Conn ¶
type Conn struct {
// contains filtered or unexported fields
}
Conn is a CoAP client connection.
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 struct { Type CType Code CCode MessageID uint16 Token, Payload []byte // contains filtered or unexported fields }
Message is a CoAP message.
func ParseMessage ¶
ParseMessage extracts the Message from the given input.
func (Message) IsConfirmable ¶
IsConfirmable returns true if this message is confirmable.
func (*Message) MarshalBinary ¶
MarshalBinary produces the binary form of this Message.
func (Message) PathString ¶
PathString gets a path as a / separated string.
func (*Message) RemoveOption ¶
RemoveOption removes all references to an option
func (*Message) SetPathString ¶
SetPathString sets a path by a / separated string.
func (*Message) UnmarshalBinary ¶
UnmarshalBinary parses the given binary slice as a Message.
type OptionID ¶
type OptionID uint32
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 // The IANA policy for future additions to this sub-registry is split // into three tiers as follows. The range of 0..255 is reserved for // options defined by the IETF (IETF Review or IESG Approval). The // range of 256..2047 is reserved for commonly used options with public // specifications (Specification Required). The range of 2048..64999 is // for all other options including private or vendor-specific ones, // which undergo a Designated Expert review to help ensure that the // option semantics are defined correctly. The option numbers between // 65000 and 65535 inclusive are reserved for experiments. They are not // meant for vendor-specific use of any kind and MUST NOT be used in // operational deployments. GiterLabID OptionID = 65000 GiterLabKey OptionID = 65001 AccessID OptionID = 65002 AccessKey OptionID = 65003 CheckCRC32 OptionID = 65004 EncoderType OptionID = 65005 EncoderID OptionID = 65006 Flags OptionID = 65007 PackageNumber OptionID = 65100 )
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 {
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)
MarshalBinary marshal to binary
func (*TCPMessage) UnmarshalBinary ¶
func (m *TCPMessage) UnmarshalBinary(data []byte) error
UnmarshalBinary unmarshal to binary