-
Notifications
You must be signed in to change notification settings - Fork 0
/
constants.go
195 lines (175 loc) · 5.74 KB
/
constants.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
package coap
import (
"fmt"
"strings"
"github.com/qwerty-iot/tox"
)
// COAPType represents the message type.
type COAPType uint8
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
)
var typeNames = [256]string{
TypeConfirmable: "Confirmable",
TypeNonConfirmable: "NonConfirmable",
TypeAcknowledgement: "Acknowledgement",
TypeReset: "Reset",
}
func init() {
for i := range typeNames {
if typeNames[i] == "" {
typeNames[i] = fmt.Sprintf("Unknown (0x%x)", i)
}
}
}
func (t COAPType) String() string {
return typeNames[t]
}
// COAPCode is the type used for both request and response codes.
type COAPCode uint8
// Request 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
)
// Response 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
)
var codeNames = [256]string{
CodeEmpty: "EMPTY",
CodeGet: "GET",
CodePost: "POST",
CodePut: "PUT",
CodeDelete: "DELETE",
CodeFetch: "FETCH",
CodePatch: "PATCH",
CodeIPatch: "iPATCH",
RspCodeCreated: "Created",
RspCodeDeleted: "Deleted",
RspCodeValid: "Valid",
RspCodeChanged: "Changed",
RspCodeContent: "Content",
RspCodeContinue: "Continue",
RspCodeBadRequest: "BadRequest",
RspCodeUnauthorized: "Unauthorized",
RspCodeBadOption: "BadOption",
RspCodeForbidden: "Forbidden",
RspCodeNotFound: "NotFound",
RspCodeMethodNotAllowed: "MethodNotAllowed",
RspCodeNotAcceptable: "NotAcceptable",
RspCodeRequestEntityIncomplete: "RequestEntityIncomplete",
RspCodePreconditionFailed: "PreconditionFailed",
RspCodeRequestEntityTooLarge: "RequestEntityTooLarge",
RspCodeUnsupportedMediaType: "UnsupportedMediaType",
RspCodeInternalServerError: "InternalServerError",
RspCodeNotImplemented: "NotImplemented",
RspCodeBadGateway: "BadGateway",
RspCodeServiceUnavailable: "ServiceUnavailable",
RspCodeGatewayTimeout: "GatewayTimeout",
RspCodeProxyingNotSupported: "ProxyingNotSupported",
}
func init() {
for i := range codeNames {
if codeNames[i] == "" {
codeNames[i] = fmt.Sprintf("Unknown (0x%x)", i)
}
}
}
func ToCOAPCode(val string) COAPCode {
ss := strings.Split(val, ".")
if len(ss) != 2 {
return RspCodeInternalServerError
}
return COAPCode(tox.ToInt(ss[0])<<5 | tox.ToInt(ss[1])&0x1F)
}
func (c COAPCode) String() string {
return codeNames[c]
}
func (c COAPCode) NumberString() string {
lower := c & 0x1F
upper := c >> 5
return fmt.Sprintf("%d.%02d", upper, lower)
}
// MediaType specifies the content type of a message.
type MediaType int
// Content types.
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
)
func (m MediaType) String() string {
switch m {
case None:
return "none"
case TextPlain:
return "text/plain;charset=utf-8"
case AppLinkFormat:
return "application/link-format"
case AppXML:
return "application/xml"
case AppOctets:
return "application/octet-stream"
case AppExi:
return "application/exi"
case AppJSON:
return "application/json"
case AppCBOR:
return "application/cbor"
case AppSenmlCBOR:
return "application/senml_cbor"
case AppLwm2mTLV:
return "application/vnd.oma.lwm2m+tlv"
case AppLwm2mJSON:
return "application/vnd.oma.lwm2m+json"
default:
return fmt.Sprintf("unknown-%d", m)
}
}