/
coap.go
191 lines (180 loc) · 4.33 KB
/
coap.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
package dots_common
import (
"math/rand"
"github.com/amyllz/my-go-dots/libcoap"
log "github.com/sirupsen/logrus"
)
/*
* CoAP message types
*/
type Type uint8
const (
Confirmable Type = 0
NonConfirmable Type = 1
Acknowledgement Type = 2
Reset Type = 3
)
/*
* CoAPType is a function to obtain given CoAP types.
*/
func (t Type) CoAPType() libcoap.Type {
switch t {
case Confirmable:
return libcoap.TypeCon
case NonConfirmable:
return libcoap.TypeNon
case Acknowledgement:
return libcoap.TypeAck
case Reset:
return libcoap.TypeRst
default:
panic("unexpected Type")
}
}
const letterBytes = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
func RandStringBytes(n int) []byte {
b := make([]byte, n)
for i := range b {
b[i] = letterBytes[rand.Intn(len(letterBytes))]
}
return b
}
/*
*CoAP message codes.
*/
type Code uint8
const (
Created Code = 65
Deleted Code = 66
Valid Code = 67
Changed Code = 68
Content Code = 69
Limit2xxCode Code = 100
BadRequest Code = 128
Unauthorized Code = 129
BadOption Code = 130
Forbidden Code = 131
NotFound Code = 132
MethodNotAllowed Code = 133
NotAcceptable Code = 134
Conflict Code = 137
PreconditionFailed Code = 140
RequestEntityTooLarge Code = 141
UnsupportedMediaType Code = 143
UnprocessableEntity Code = 150
InternalServerError Code = 160
NotImplemented Code = 161
BadGateway Code = 162
ServiceUnavailable Code = 163
GatewayTimeout Code = 164
ProxyingNotSupported Code = 165
)
/*
convert CoAP types to strings.
*/
func (c Code) String() string {
switch c {
case Created:
return "Created"
case Deleted:
return "Deleted"
case Valid:
return "Valid"
case Changed:
return "Changed"
case Content:
return "Content"
case BadRequest:
return "BadRequest"
case Unauthorized:
return "Unauthorized"
case BadOption:
return "BadOption"
case Forbidden:
return "Forbidden"
case NotFound:
return "NotFound"
case MethodNotAllowed:
return "MethodNotAllowed"
case NotAcceptable:
return "NotAcceptable"
case Conflict:
return "Conflict"
case PreconditionFailed:
return "PreconditionFailed"
case RequestEntityTooLarge:
return "RequestEntityTooLarge"
case UnsupportedMediaType:
return "UnsupportedMediaType"
case UnprocessableEntity:
return "UnprocessableEntity"
case InternalServerError:
return "InternalServerError"
case NotImplemented:
return "NotImplemented"
case BadGateway:
return "BadGateway"
case ServiceUnavailable:
return "ServiceUnavailable"
case GatewayTimeout:
return "GatewayTimeout"
case ProxyingNotSupported:
return "ProxyingNotSupported"
default:
return "Unexpected Error"
}
}
/*
* CoAPCode is a function to obtain given CoAP codes.
*/
func (c Code) CoAPCode() libcoap.Code {
switch c {
case Created:
return libcoap.ResponseCreated
case Deleted:
return libcoap.ResponseDeleted
case Valid:
return libcoap.ResponseValid
case Changed:
return libcoap.ResponseChanged
case Content:
return libcoap.ResponseContent
case BadRequest:
return libcoap.ResponseBadRequest
case Unauthorized:
return libcoap.ResponseUnauthorized
case BadOption:
return libcoap.ResponseBadOption
case Forbidden:
return libcoap.ResponseForbidden
case NotFound:
return libcoap.ResponseNotFound
case MethodNotAllowed:
return libcoap.ResponseMethodNotAllowed
case NotAcceptable:
return libcoap.ResponseNotAcceptable
case Conflict:
return libcoap.ResponseConflict
case PreconditionFailed:
return libcoap.ResponsePreconditionFailed
case RequestEntityTooLarge:
return libcoap.RequestEntityTooLarge
case UnsupportedMediaType:
return libcoap.ResponseUnsupportedMediaType
case InternalServerError:
return libcoap.ResponseInternalServerError
case NotImplemented:
return libcoap.ResponseNotImplemented
case BadGateway:
return libcoap.ResponseBadGateway
case ServiceUnavailable:
return libcoap.ResponseServiceUnavailable
case GatewayTimeout:
return libcoap.ResponseGatewayTimeout
case ProxyingNotSupported:
return libcoap.ResponseProxyingNotSupported
default:
log.WithFields(log.Fields{"code": int(c)}).Error("invalid coap code")
return libcoap.ResponseInternalServerError
}
}