forked from Oryx-Embedded/CycloneTCP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoap_client.c
460 lines (378 loc) · 11.8 KB
/
coap_client.c
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
/**
* @file coap_client.c
* @brief CoAP client
*
* @section License
*
* SPDX-License-Identifier: GPL-2.0-or-later
*
* Copyright (C) 2010-2019 Oryx Embedded SARL. All rights reserved.
*
* This file is part of CycloneTCP Open.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* @author Oryx Embedded SARL (www.oryx-embedded.com)
* @version 1.9.6
**/
//Switch to the appropriate trace level
#define TRACE_LEVEL COAP_TRACE_LEVEL
//Dependencies
#include <stdlib.h>
#include "core/net.h"
#include "coap/coap_client.h"
#include "coap/coap_client_transport.h"
#include "coap/coap_client_misc.h"
#include "coap/coap_debug.h"
#include "debug.h"
//Check TCP/IP stack configuration
#if (COAP_CLIENT_SUPPORT == ENABLED)
/**
* @brief Initialize CoAP client context
* @param[in] context Pointer to the CoAP client context
* @return Error code
**/
error_t coapClientInit(CoapClientContext *context)
{
error_t error;
//Make sure the CoAP client context is valid
if(context == NULL)
return ERROR_INVALID_PARAMETER;
//Clear CoAP client context
memset(context, 0, sizeof(CoapClientContext));
//Initialize status code
error = NO_ERROR;
//Start of exception handling block
do
{
//Create a mutex to prevent simultaneous access to the context
if(!osCreateMutex(&context->mutex))
{
//Report an error
error = ERROR_OUT_OF_RESOURCES;
break;
}
//Create a event object to receive notifications
if(!osCreateEvent(&context->event))
{
//Report an error
error = ERROR_OUT_OF_RESOURCES;
break;
}
#if (COAP_CLIENT_DTLS_SUPPORT == ENABLED)
//Initialize DTLS session state
error = tlsInitSessionState(&context->dtlsSession);
//Any error to report?
if(error)
break;
#endif
//Initialize CoAP client state
context->state = COAP_CLIENT_STATE_DISCONNECTED;
//Default transport protocol
context->transportProtocol = COAP_TRANSPORT_PROTOCOL_UDP;
//Default timeout
context->timeout = COAP_CLIENT_DEFAULT_TIMEOUT;
//Default token length
context->tokenLen = COAP_CLIENT_DEFAULT_TOKEN_LEN;
//It is strongly recommended that the initial value of the message ID
//be randomized (refer to RFC 7252, section 4.4)
context->mid = (uint16_t) netGetRand();
//End of exception handling block
} while(0);
//Check status code
if(error)
{
//Clean up side effects
coapClientDeinit(context);
}
//Return status code
return error;
}
/**
* @brief Set the transport protocol to be used
* @param[in] context Pointer to the CoAP client context
* @param[in] transportProtocol Transport protocol to be used (UDP or DTLS)
* @return Error code
**/
error_t coapClientSetTransportProtocol(CoapClientContext *context,
CoapTransportProtocol transportProtocol)
{
//Make sure the CoAP client context is valid
if(context == NULL)
return ERROR_INVALID_PARAMETER;
//Acquire exclusive access to the CoAP client context
osAcquireMutex(&context->mutex);
//Save the transport protocol to be used
context->transportProtocol = transportProtocol;
//Release exclusive access to the CoAP client context
osReleaseMutex(&context->mutex);
//Successful processing
return NO_ERROR;
}
#if (COAP_CLIENT_DTLS_SUPPORT == ENABLED)
/**
* @brief Register DTLS initialization callback function
* @param[in] context Pointer to the CoAP client context
* @param[in] callback DTLS initialization callback function
* @return Error code
**/
error_t coapClientRegisterDtlsInitCallback(CoapClientContext *context,
CoapClientDtlsInitCallback callback)
{
//Check parameters
if(context == NULL || callback == NULL)
return ERROR_INVALID_PARAMETER;
//Acquire exclusive access to the CoAP client context
osAcquireMutex(&context->mutex);
//Save callback function
context->dtlsInitCallback = callback;
//Release exclusive access to the CoAP client context
osReleaseMutex(&context->mutex);
//Successful processing
return NO_ERROR;
}
#endif
/**
* @brief Set default request timeout
* @param[in] context Pointer to the CoAP client context
* @param[in] timeout Timeout value, in milliseconds
* @return Error code
**/
error_t coapClientSetTimeout(CoapClientContext *context, systime_t timeout)
{
//Make sure the CoAP client context is valid
if(context == NULL)
return ERROR_INVALID_PARAMETER;
//Acquire exclusive access to the CoAP client context
osAcquireMutex(&context->mutex);
//Save timeout value
context->timeout = timeout;
//Release exclusive access to the CoAP client context
osReleaseMutex(&context->mutex);
//Successful processing
return NO_ERROR;
}
/**
* @brief Set the length of the token
* @param[in] context Pointer to the CoAP client context
* @param[in] length Token length
* @return Error code
**/
error_t coapClientSetTokenLength(CoapClientContext *context, size_t length)
{
//Make sure the CoAP client context is valid
if(context == NULL)
return ERROR_INVALID_PARAMETER;
//Make sure the token length is acceptable
if(length > COAP_MAX_TOKEN_LEN)
return ERROR_INVALID_PARAMETER;
//Acquire exclusive access to the CoAP client context
osAcquireMutex(&context->mutex);
//Save token length
context->tokenLen = length;
//Release exclusive access to the CoAP client context
osReleaseMutex(&context->mutex);
//Successful processing
return NO_ERROR;
}
/**
* @brief Bind the CoAP client to a particular network interface
* @param[in] context Pointer to the CoAP client context
* @param[in] interface Network interface to be used
* @return Error code
**/
error_t coapClientBindToInterface(CoapClientContext *context,
NetInterface *interface)
{
//Make sure the CoAP client context is valid
if(context == NULL)
return ERROR_INVALID_PARAMETER;
//Acquire exclusive access to the CoAP client context
osAcquireMutex(&context->mutex);
//Explicitly associate the CoAP client with the specified interface
context->interface = interface;
//Release exclusive access to the CoAP client context
osReleaseMutex(&context->mutex);
//Successful processing
return NO_ERROR;
}
/**
* @brief Establish connection with the CoAP server
* @param[in] context Pointer to the CoAP client context
* @param[in] serverIpAddr IP address of the CoAP server to connect to
* @param[in] serverPort UDP port number that will be used
* @return Error code
**/
error_t coapClientConnect(CoapClientContext *context,
const IpAddr *serverIpAddr, uint16_t serverPort)
{
error_t error;
systime_t time;
//Check parameters
if(context == NULL || serverIpAddr == NULL)
return ERROR_INVALID_PARAMETER;
//Acquire exclusive access to the CoAP client context
osAcquireMutex(&context->mutex);
//Initialize status code
error = NO_ERROR;
//Establish connection with the CoAP server
while(!error)
{
//Get current time
time = osGetSystemTime();
//Check current state
if(context->state == COAP_CLIENT_STATE_DISCONNECTED)
{
//Open network connection
error = coapClientOpenConnection(context);
//Check status code
if(!error)
{
//Save current time
context->startTime = time;
//Update CoAP client state
context->state = COAP_CLIENT_STATE_CONNECTING;
}
}
else if(context->state == COAP_CLIENT_STATE_CONNECTING)
{
//Establish DTLS connection
error = coapClientEstablishConnection(context, serverIpAddr,
serverPort);
//Check status code
if(error == NO_ERROR)
{
//Update CoAP client state
context->state = COAP_CLIENT_STATE_CONNECTED;
}
else if(error == ERROR_WOULD_BLOCK || error == ERROR_TIMEOUT)
{
//Check whether the timeout has elapsed
if(timeCompare(time, context->startTime + context->timeout) < 0)
{
//Wait for an incoming datagram
coapClientWaitForDatagram(context, COAP_CLIENT_TICK_INTERVAL);
//Continue processing
error = NO_ERROR;
}
else
{
//Report an error
error = ERROR_TIMEOUT;
}
}
else
{
//Just for sanity
}
}
else if(context->state == COAP_CLIENT_STATE_CONNECTED)
{
//The CoAP client is connected
break;
}
else
{
//Invalid state
error = ERROR_WRONG_STATE;
}
}
//Failed to establish connection with the CoAP server?
if(error)
{
//Clean up side effects
coapClientCloseConnection(context);
//Update CoAP client state
context->state = COAP_CLIENT_STATE_DISCONNECTED;
}
//Release exclusive access to the CoAP client context
osReleaseMutex(&context->mutex);
//Return status code
return error;
}
/**
* @brief Process CoAP client events
* @param[in] context Pointer to the CoAP client context
* @param[in] timeout Maximum time to wait before returning
* @return Error code
**/
error_t coapClientTask(CoapClientContext *context, systime_t timeout)
{
error_t error;
//Make sure the CoAP client context is valid
if(context == NULL)
return ERROR_INVALID_PARAMETER;
//Acquire exclusive access to the CoAP client context
osAcquireMutex(&context->mutex);
//Process CoAP client events
error = coapClientProcessEvents(context, timeout);
//Release exclusive access to the CoAP client context
osReleaseMutex(&context->mutex);
//Return status code
return error;
}
/**
* @brief Disconnect from the CoAP server
* @param[in] context Pointer to the CoAP client context
* @return Error code
**/
error_t coapClientDisconnect(CoapClientContext *context)
{
error_t error;
//Make sure the CoAP client context is valid
if(context == NULL)
return ERROR_INVALID_PARAMETER;
//Initialize status code
error = NO_ERROR;
//Acquire exclusive access to the CoAP client context
osAcquireMutex(&context->mutex);
//Check current state
if(context->state == COAP_CLIENT_STATE_CONNECTED)
{
//Terminate DTLS connection
error = coapClientShutdownConnection(context);
}
//Close connection
coapClientCloseConnection(context);
//Update CoAP client state
context->state = COAP_CLIENT_STATE_DISCONNECTED;
//Release exclusive access to the CoAP client context
osReleaseMutex(&context->mutex);
//Return status code
return error;
}
/**
* @brief Release CoAP client context
* @param[in] context Pointer to the CoAP client context
**/
void coapClientDeinit(CoapClientContext *context)
{
//Make sure the CoAP client context is valid
if(context != NULL)
{
//Close connection
coapClientCloseConnection(context);
#if (COAP_CLIENT_DTLS_SUPPORT == ENABLED)
//Release DTLS session state
tlsFreeSessionState(&context->dtlsSession);
#endif
//Release previously allocated resources
osDeleteMutex(&context->mutex);
osDeleteEvent(&context->event);
//Clear CoAP client context
memset(context, 0, sizeof(CoapClientContext));
}
}
#endif