-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathconstants.go
291 lines (258 loc) · 8.98 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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
package coalago
import (
"errors"
"time"
)
var (
timeWait = time.Second
maxSendAttempts = 6
sumTimeAttempts = timeWait*time.Duration(maxSendAttempts) + 100
)
const (
SESSIONS_POOL_EXPIRATION = time.Second * 60 * 2
MAX_PAYLOAD_SIZE = 1024
DEFAULT_WINDOW_SIZE = 300
MIN_WiNDOW_SIZE = 50
MAX_WINDOW_SIZE = 1500
MTU = 1500
)
const PayloadMarker = 0xff
const (
COAP_SCHEME = 0
COAPS_SCHEME = 1
)
type CoapType uint8
const (
CON CoapType = 0
NON CoapType = 1
ACK CoapType = 2
RST CoapType = 3
)
type CoapMethod uint8
const (
CoapMethodGet CoapMethod = 1
CoapMethodPut CoapMethod = 2
CoapMethodPost CoapMethod = 3
CoapMethodDelete CoapMethod = 4
)
type CoapCode uint8
const (
//methods
GET CoapCode = 1
POST CoapCode = 2
PUT CoapCode = 3
DELETE CoapCode = 4
// Response
CoapCodeEmpty CoapCode = 0
CoapCodeCreated CoapCode = 65
CoapCodeDeleted CoapCode = 66
CoapCodeValid CoapCode = 67
CoapCodeChanged CoapCode = 68
CoapCodeContent CoapCode = 69
CoapCodeContinue CoapCode = 95 // (2.31 Continue)
// Errors
CoapCodeBadRequest CoapCode = 128
CoapCodeUnauthorized CoapCode = 129
CoapCodeBadOption CoapCode = 130
CoapCodeForbidden CoapCode = 131
CoapCodeNotFound CoapCode = 132
CoapCodeMethodNotAllowed CoapCode = 133
CoapCodeNotAcceptable CoapCode = 134
CoapCodeRequestEntityIncomplete CoapCode = 136 // (4.08)
CoapCodeConflict CoapCode = 137
CoapCodePreconditionFailed CoapCode = 140
CoapCodeRequestEntityTooLarge CoapCode = 141
CoapCodeUnsupportedContentFormat CoapCode = 143
CoapCodeInternalServerError CoapCode = 160
CoapCodeNotImplemented CoapCode = 161
CoapCodeBadGateway CoapCode = 162
CoapCodeServiceUnavailable CoapCode = 163
CoapCodeGatewayTimeout CoapCode = 164
CoapCodeProxyingNotSupported CoapCode = 165
)
func (code CoapCode) String() string {
switch code {
case GET:
return "GET"
case POST:
return "POST"
case PUT:
return "PUT"
case DELETE:
return "DELETE"
case CoapCodeEmpty:
return "0 Empty"
case CoapCodeCreated:
return "201 Created"
case CoapCodeDeleted:
return "202 Deleted"
case CoapCodeValid:
return "203 Valid"
case CoapCodeChanged:
return "204 Changed"
case CoapCodeContent:
return "205 Content"
case CoapCodeContinue:
return "231 Continue"
case CoapCodeBadRequest:
return "400 Bad Request"
case CoapCodeUnauthorized:
return "401 Unauthorized"
case CoapCodeBadOption:
return "402 Bad Option"
case CoapCodeForbidden:
return "403 Forbidden"
case CoapCodeNotFound:
return "404 Not Found"
case CoapCodeMethodNotAllowed:
return "405 Method Not Allowed"
case CoapCodeNotAcceptable:
return "406 Not Acceptable"
case CoapCodePreconditionFailed:
return "412 Precondition Failed"
case CoapCodeRequestEntityTooLarge:
return "413 Request Entity Too Large"
case CoapCodeUnsupportedContentFormat:
return "415 Unsupported Content Format"
case CoapCodeInternalServerError:
return "500 Internal Server Error"
case CoapCodeNotImplemented:
return "501 Not Implemented"
case CoapCodeBadGateway:
return "502 Bad Gateway"
case CoapCodeServiceUnavailable:
return "503 Service Unavailable"
case CoapCodeGatewayTimeout:
return "504 Gateway Timeout"
case CoapCodeProxyingNotSupported:
return "505 Proxying Not Supported"
default:
return "Unknown"
}
}
func (c CoapCode) Group() string {
if c > 64 && c < 96 {
return "2.xx"
} else if c > 127 && c < 144 {
return "4.xx"
} else if c > 159 && c < 166 {
return "5.xx"
}
return "others"
}
func (c *CoapCode) IsRegisteredMethod() bool {
return (*c > 0 && *c <= 4)
}
func (c *CoapCode) IsCommonError() bool {
return (*c >= 128 && *c < 160)
}
func (c *CoapCode) IsInternalError() bool {
return (*c >= 160 && *c <= 165)
}
type MediaType int
const (
MediaTypeTextPlain MediaType = 0
MediaTypeTextXML MediaType = 1
MediaTypeTextCsv MediaType = 2
MediaTypeTextHTML MediaType = 3
MediaTypeImageGif MediaType = 21
MediaTypeImageJpeg MediaType = 22
MediaTypeImagePng MediaType = 23
MediaTypeImageTiff MediaType = 24
MediaTypeAudioRaw MediaType = 25
MediaTypeVideoRaw MediaType = 26
MediaTypeApplicationLinkFormat MediaType = 40
MediaTypeApplicationXML MediaType = 41
MediaTypeApplicationOctetStream MediaType = 42
MediaTypeApplicationRdfXML MediaType = 43
MediaTypeApplicationSoapXML MediaType = 44
MediaTypeApplicationAtomXML MediaType = 45
MediaTypeApplicationXmppXML MediaType = 46
MediaTypeApplicationExi MediaType = 47
MediaTypeApplicationFastInfoSet MediaType = 48
MediaTypeApplicationSoapFastInfoSet MediaType = 49
MediaTypeApplicationJSON MediaType = 50
MediaTypeApplicationXObitBinary MediaType = 51
MediaTypeTextPlainVndOmaLwm2m MediaType = 1541
MediaTypeTlvVndOmaLwm2m MediaType = 1542
MediaTypeJSONVndOmaLwm2m MediaType = 1543
MediaTypeOpaqueVndOmaLwm2m MediaType = 1544
)
type CoapHandshakeType uint8
const (
CoapHandshakeTypeClientHello = 1
CoapHandshakeTypePeerHello = 2
CoapHandshakeTypeClientSignature = 3
CoapHandshakeTypePeerSignature = 4
)
type OptionCode int
const (
OptionIfMatch OptionCode = 1
OptionURIHost OptionCode = 3
OptionEtag OptionCode = 4
OptionIfNoneMatch OptionCode = 5
OptionObserve OptionCode = 6
OptionURIPort OptionCode = 7
OptionLocationPath OptionCode = 8
OptionURIPath OptionCode = 11
OptionContentFormat OptionCode = 12
OptionMaxAge OptionCode = 14
OptionURIQuery OptionCode = 15
OptionAccept OptionCode = 17
OptionLocationQuery OptionCode = 20
OptionBlock2 OptionCode = 23
OptionBlock1 OptionCode = 27
OptionSize2 OptionCode = 28
OptionProxyURI OptionCode = 35
OptionProxyScheme OptionCode = 39
OptionSize1 OptionCode = 60
/// URI scheme options specifies scheme to be used for message transmission
/// See `CoAPMessage.GetScheme()`. Scheme is stored using it's raw value
OptionURIScheme OptionCode = 2111
/// Handshake option is used by Coala library to detect handshake CoAP messages
OptionHandshakeType OptionCode = 3999
/// Session Not Found option indicates to sender that peer has no active coaps:// session.
/// Upon receiving the message with this option sender must restart the session
OptionSessionNotFound OptionCode = 4001
/// Session expired option indicates that peer's coaps:// session expired
/// Upon receiving the message with this option sender must restart the session
OptionSessionExpired OptionCode = 4003
OptionSelectiveRepeatWindowSize OptionCode = 3001
OptionProxySecurityID OptionCode = 3004
// OptionWindowtOffset OptionCode = 3012
OptionСoapsUri OptionCode = 4005
)
// Fragments/parts of a CoAP Message packet
const (
DataHeader = 0
DataCode = 1
DataMsgIDStart = 2
DataMsgIDEnd = 4
DataTokenStart = 4
)
var (
ErrPacketLengthLessThan4 = errors.New("Packet length less than 4 bytes")
ErrInvalidCoapVersion = errors.New("Invalid CoAP version. Should be 1.")
ErrOptionLengthUsesValue15 = errors.New(("Message format error. Option length has reserved value of 15"))
ErrOptionDeltaUsesValue15 = errors.New(("Message format error. Option delta has reserved value of 15"))
ErrUnknownMessageType = errors.New("Unknown message type")
ErrInvalidTokenLength = errors.New("Invalid Token Length ( > 8)")
ErrUnknownCriticalOption = errors.New("Unknown critical option encountered")
ErrUnsupportedMethod = errors.New("Unsupported Method")
ErrNoMatchingRoute = errors.New("No matching route found")
ErrUnsupportedContentFormat = errors.New("Unsupported Content-Format")
ErrNoMatchingMethod = errors.New("No matching method")
ErrNilMessage = errors.New("Message is nil")
ErrRepeatedMessage = errors.New("Repeated message")
ErrNilConn = errors.New("Connection object is nil")
ErrNilAddr = errors.New("Address cannot be nil")
ErrOptionLenghtOutOfRangePackets = errors.New("Option lenght out of range packet")
ErrUndefinedScheme = errors.New("undefined scheme")
ErrMaxAttempts = errors.New("max attempts")
ErrorSessionNotFound = errors.New("session not found")
ErrorClientSessionNotFound = errors.New("client session not found")
ErrorSessionExpired = errors.New("session expired")
ErrorClientSessionExpired = errors.New("client session expired")
ErrorHandshake = errors.New("error handshake")
ERR_KEYS_NOT_MATCH = "Expected and current public keys do not match"
)