/
client.go
226 lines (192 loc) · 5.71 KB
/
client.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
package libcoap
/*
#cgo LDFLAGS: -lcoap-3-openssl -lssl -lcrypto
#include <coap3/coap.h>
#include "callback.h"
*/
import "C"
import "errors"
import "unsafe"
type ResponseHandler func(*Context, *Session, *Pdu, *Pdu)
type NackHandler func(*Session, *Pdu, NackReason)
type NackReason C.coap_nack_reason_t
const (
NackTooManyRetries NackReason = C.COAP_NACK_TOO_MANY_RETRIES
NackNotDeliverable NackReason = C.COAP_NACK_NOT_DELIVERABLE
NackRst NackReason = C.COAP_NACK_RST
NackTlsFailed NackReason = C.COAP_NACK_TLS_FAILED
)
type Proto C.coap_proto_t
const (
ProtoUdp Proto = C.COAP_PROTO_UDP
ProtoTcp Proto = C.COAP_PROTO_TCP
ProtoDtls Proto = C.COAP_PROTO_DTLS
ProtoTls Proto = C.COAP_PROTO_TLS
)
func (ctx *Context) NewClientSession(dst Address, proto Proto) *Session {
ptr := C.coap_new_client_session(ctx.ptr,
nil,
&dst.value,
C.coap_proto_t(proto))
if ptr != nil {
session := &Session{ ptr, nil }
sessions[ptr] = session
return session
} else {
return nil
}
}
func (ctx *Context) NewClientSessionPSK(dst Address, proto Proto, identity string, key []byte) *Session {
cid := C.CString(identity)
defer C.free(unsafe.Pointer(cid))
ptr := C.coap_new_client_session_psk(ctx.ptr,
nil,
&dst.value,
C.coap_proto_t(proto),
cid,
(*C.uint8_t)(&key[0]),
C.uint(len(key)))
if ptr != nil {
session := &Session{ ptr, nil }
sessions[ptr] = session
return session
} else {
return nil
}
}
func (ctx *Context) NewClientSessionDTLS(dst Address, proto Proto) *Session {
ptr := C.coap_new_client_session(ctx.ptr,
nil,
&dst.value,
C.coap_proto_t(proto))
if ptr != nil {
session := &Session{ ptr, nil }
sessions[ptr] = session
return session
}
return nil
}
func (session *Session) NewMessageID() uint16 {
return uint16(C.coap_new_message_id(session.ptr))
}
func (session *Session) Send(pdu *Pdu) (err error) {
cpdu, err := pdu.toC(session)
if err != nil {
return
}
if C.COAP_INVALID_MID == C.coap_send(session.ptr, cpdu) {
err = errors.New("coap_session() -> COAP_INVALID_TID")
return
}
return
}
//export export_response_handler
func export_response_handler(sess *C.coap_session_t,
sent *C.coap_pdu_t,
received *C.coap_pdu_t,
id C.coap_mid_t) (response C.coap_response_t) {
response = C.COAP_RESPONSE_FAIL
ctx := C.coap_session_get_context(sess)
if ctx == nil {
return
}
context, ok := contexts[ctx]
if !ok {
return
}
session, ok := sessions[sess]
if !ok {
return
}
var req *Pdu = nil
var err error
if sent != nil {
req, err = sent.toGo()
if err != nil {
return
}
}
res, err := received.toGo()
if err != nil {
return
}
if context.handler != nil {
context.handler(context, session, req, res)
response = C.COAP_RESPONSE_OK
}
return
}
//export export_method_from_server_handler
func export_method_from_server_handler(rsrc *C.coap_resource_t,
sess *C.coap_session_t,
req *C.coap_pdu_t,
query *C.coap_string_t,
resp *C.coap_pdu_t) {
ctx := C.coap_session_get_context(sess)
if ctx == nil {
return
}
context, ok := contexts[ctx]
if !ok {
return
}
resource, ok := resources[rsrc]
if !ok {
return
}
session, ok := sessions[sess]
if !ok {
return
}
request, err := req.toGo()
if err != nil {
return
}
tok := C.coap_get_token_from_request_pdu(req)
token := tok.toBytes()
queryString := query.toString()
handler, ok := resource.handlers[request.Code]
response := Pdu{}
if ok {
handler(context, resource, session, request, token, queryString, &response)
}
response.fillC(resp, nil)
}
//export export_nack_handler
func export_nack_handler(sess *C.coap_session_t,
sent *C.coap_pdu_t,
reason C.coap_nack_reason_t,
id C.coap_mid_t) {
ctx := C.coap_session_get_context(sess)
if ctx == nil {
return
}
context, ok := contexts[ctx]
if !ok {
return
}
session, ok := sessions[sess]
if !ok {
return
}
req, err := sent.toGo()
if err != nil {
return
}
// If previous message is Ping message or Session Config message
if context.nackHandler != nil && req.Type == C.COAP_MESSAGE_CON {
context.nackHandler(session, req, NackReason(reason))
}
}
func (context *Context) RegisterResponseHandler(handler ResponseHandler) {
context.handler = handler
C.coap_register_response_handler(context.ptr, C.coap_response_handler_t(C.response_handler))
}
func (context *Context) RegisterNackHandler(handler NackHandler) {
context.nackHandler = handler
C.coap_register_nack_handler(context.ptr, C.coap_nack_handler_t(C.nack_handler))
}
func (resource *Resource) RegisterServerHandler(method Code, handler MethodHandler) {
resource.handlers[method] = handler
C.coap_register_handler(resource.ptr, C.coap_request_t(method), C.coap_method_handler_t(C.method_from_server_handler))
}