coap_server.c
Go to the documentation of this file.
1 /**
2  * @file coap_server.c
3  * @brief CoAP server
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2026 Oryx Embedded SARL. All rights reserved.
10  *
11  * This file is part of CycloneTCP Open.
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26  *
27  * @author Oryx Embedded SARL (www.oryx-embedded.com)
28  * @version 2.6.4
29  **/
30 
31 //Switch to the appropriate trace level
32 #define TRACE_LEVEL COAP_TRACE_LEVEL
33 
34 //Dependencies
35 #include <stdlib.h>
36 #include "coap/coap_server.h"
39 #include "coap/coap_server_misc.h"
40 #include "coap/coap_debug.h"
41 #include "debug.h"
42 
43 //Check TCP/IP stack configuration
44 #if (COAP_SERVER_SUPPORT == ENABLED)
45 
46 
47 /**
48  * @brief Initialize settings with default values
49  * @param[out] settings Structure that contains CoAP server settings
50  **/
51 
53 {
54  //Default task parameters
55  settings->task = OS_TASK_DEFAULT_PARAMS;
58 
59  //TCP/IP stack context
60  settings->netContext = NULL;
61  //The CoAP server is not bound to any interface
62  settings->interface = NULL;
63 
64  //CoAP port number
65  settings->port = COAP_PORT;
66 
67 #if (COAP_SERVER_DTLS_SUPPORT == ENABLED)
68  //DTLS sessions
69  settings->numSessions = 0;
70  settings->sessions = NULL;
71 #endif
72 
73 #if (COAP_SERVER_OBSERVE_SUPPORT == ENABLED)
74  //Observable resources
75  settings->numResources = 0;
76  settings->resources = NULL;
77 
78  //Observers
79  settings->numObservers = 0;
80  settings->observers = NULL;
81 #endif
82 
83  //UDP initialization callback
84  settings->udpInitCallback = NULL;
85 
86 #if (COAP_SERVER_DTLS_SUPPORT == ENABLED)
87  //DTLS initialization callback function
88  settings->dtlsInitCallback = NULL;
89 #endif
90 
91  //CoAP request callback function
92  settings->requestCallback = NULL;
93 
94 #if (COAP_SERVER_OBSERVE_SUPPORT == ENABLED)
95  //Observe callback
96  settings->observeCallback = NULL;
97 #endif
98 }
99 
100 
101 /**
102  * @brief CoAP server initialization
103  * @param[in] context Pointer to the CoAP server context
104  * @param[in] settings CoAP server specific settings
105  * @return Error code
106  **/
107 
109  const CoapServerSettings *settings)
110 {
111  error_t error;
112 #if (COAP_SERVER_DTLS_SUPPORT == ENABLED || COAP_SERVER_OBSERVE_SUPPORT == ENABLED)
113  uint_t i;
114 #endif
115 
116  //Debug message
117  TRACE_INFO("Initializing CoAP server...\r\n");
118 
119  //Ensure the parameters are valid
120  if(context == NULL || settings == NULL)
122 
123 #if (COAP_SERVER_DTLS_SUPPORT == ENABLED)
124  if(settings->sessions == NULL && settings->numSessions != 0)
126 #endif
127 
128 #if (COAP_SERVER_OBSERVE_SUPPORT == ENABLED)
129  if(settings->resources == NULL && settings->numResources != 0)
131 
132  if(settings->observers == NULL && settings->numObservers != 0)
134 #endif
135 
136  //Initialize status code
137  error = NO_ERROR;
138 
139  //Clear the CoAP server context
140  osMemset(context, 0, sizeof(CoapServerContext));
141 
142  //Initialize task parameters
143  context->taskParams = settings->task;
144  context->taskId = OS_INVALID_TASK_ID;
145 
146  //Attach TCP/IP stack context
147  if(settings->netContext != NULL)
148  {
149  context->netContext = settings->netContext;
150  }
151  else if(settings->interface != NULL)
152  {
153  context->netContext = settings->interface->netContext;
154  }
155  else
156  {
157  context->netContext = netGetDefaultContext();
158  }
159 
160  //Save user settings
161  context->interface = settings->interface;
162  context->port = settings->port;
163  context->udpInitCallback = settings->udpInitCallback;
164  context->requestCallback = settings->requestCallback;
165 
166 #if (COAP_SERVER_DTLS_SUPPORT == ENABLED)
167  //DTLS sessions
168  context->numSessions = settings->numSessions;
169  context->sessions = settings->sessions;
170 
171  //Initialize entries
172  for(i = 0; i < context->numSessions; i++)
173  {
174  osMemset(&context->sessions[i], 0, sizeof(CoapDtlsSession));
175  }
176 
177  //DTLS initialization callback
178  context->dtlsInitCallback = settings->dtlsInitCallback;
179 #endif
180 
181 #if (COAP_SERVER_OBSERVE_SUPPORT == ENABLED)
182  //Observable resources
183  context->numResources = settings->numResources;
184  context->resources = settings->resources;
185 
186  //Initialize entries
187  for(i = 0; i < context->numResources; i++)
188  {
189  osMemset(&context->resources[i], 0, sizeof(CoapResource));
190  }
191 
192  //Observers
193  context->numObservers = settings->numObservers;
194  context->observers = settings->observers;
195 
196  //Initialize entries
197  for(i = 0; i < context->numObservers; i++)
198  {
199  osMemset(&context->observers[i], 0, sizeof(CoapObserver));
200  }
201 
202  //Observe callback
203  context->observeCallback = settings->observeCallback;
204 
205  //It is strongly recommended that the initial value of the message ID be
206  //randomized (refer to RFC 7252, section 4.4)
207  context->mid = (uint16_t) netGetRand(context->netContext);
208 #endif
209 
210  //Start of exception handling block
211  do
212  {
213  //Create a mutex to prevent simultaneous access to the context
214  if(!osCreateMutex(&context->mutex))
215  {
216  //Failed to create mutex
217  error = ERROR_OUT_OF_RESOURCES;
218  break;
219  }
220 
221  //Create an event object to poll the state of the UDP socket
222  if(!osCreateEvent(&context->event))
223  {
224  //Failed to create event
225  error = ERROR_OUT_OF_RESOURCES;
226  break;
227  }
228 
229 #if (COAP_SERVER_DTLS_SUPPORT == ENABLED && TLS_TICKET_SUPPORT == ENABLED)
230  //Initialize ticket encryption context
231  error = tlsInitTicketContext(&context->dtlsTicketContext);
232  //Any error to report?
233  if(error)
234  break;
235 #endif
236 
237  //End of exception handling block
238  } while(0);
239 
240  //Check status code
241  if(error)
242  {
243  //Clean up side effects
244  coapServerDeinit(context);
245  }
246 
247  //Return status code
248  return error;
249 }
250 
251 
252 /**
253  * @brief Set cookie secret
254  *
255  * This function specifies the cookie secret used while generating and
256  * verifying a cookie during the DTLS handshake
257  *
258  * @param[in] context Pointer to the CoAP server context
259  * @param[in] cookieSecret Pointer to the secret key
260  * @param[in] cookieSecretLen Length of the secret key, in bytes
261  * @return Error code
262  **/
263 
265  const uint8_t *cookieSecret, size_t cookieSecretLen)
266 {
267 #if (COAP_SERVER_DTLS_SUPPORT == ENABLED)
268  //Ensure the parameters are valid
269  if(context == NULL || cookieSecret == NULL)
271  if(cookieSecretLen > COAP_SERVER_MAX_COOKIE_SECRET_SIZE)
273 
274  //Save the secret key
275  osMemcpy(context->cookieSecret, cookieSecret, cookieSecretLen);
276  //Save the length of the secret key
277  context->cookieSecretLen = cookieSecretLen;
278 
279  //Successful processing
280  return NO_ERROR;
281 #else
282  //Not implemented
283  return ERROR_NOT_IMPLEMENTED;
284 #endif
285 }
286 
287 
288 /**
289  * @brief Start CoAP server
290  * @param[in] context Pointer to the CoAP server context
291  * @return Error code
292  **/
293 
295 {
296  error_t error;
297 
298  //Make sure the CoAP server context is valid
299  if(context == NULL)
301 
302  //Debug message
303  TRACE_INFO("Starting CoAP server...\r\n");
304 
305  //Make sure the CoAP server is not already running
306  if(context->running)
307  return ERROR_ALREADY_RUNNING;
308 
309  //Start of exception handling block
310  do
311  {
312  //Open a UDP socket
313  context->socket = socketOpenEx(context->netContext, SOCKET_TYPE_DGRAM,
315  //Failed to open socket?
316  if(context->socket == NULL)
317  {
318  //Report an error
319  error = ERROR_OPEN_FAILED;
320  break;
321  }
322 
323  //Force the socket to operate in non-blocking mode
324  error = socketSetTimeout(context->socket, 0);
325  //Any error to report?
326  if(error)
327  break;
328 
329  //Associate the socket with the relevant interface
330  error = socketBindToInterface(context->socket, context->interface);
331  //Unable to bind the socket to the desired interface?
332  if(error)
333  break;
334 
335  //The CoAP server listens for datagrams on port 5683
336  error = socketBind(context->socket, &IP_ADDR_ANY, context->port);
337  //Unable to bind the socket to the desired port?
338  if(error)
339  break;
340 
341  //Any registered callback?
342  if(context->udpInitCallback != NULL)
343  {
344  //Invoke user callback function
345  error = context->udpInitCallback(context, context->socket);
346  //Any error to report?
347  if(error)
348  break;
349  }
350 
351  //Start the CoAP server
352  context->stop = FALSE;
353  context->running = TRUE;
354 
355  //Create a task
356  context->taskId = osCreateTask("CoAP Server", (OsTaskCode) coapServerTask,
357  context, &context->taskParams);
358 
359  //Failed to create task?
360  if(context->taskId == OS_INVALID_TASK_ID)
361  {
362  //Report an error
363  error = ERROR_OUT_OF_RESOURCES;
364  break;
365  }
366 
367  //End of exception handling block
368  } while(0);
369 
370  //Any error to report?
371  if(error)
372  {
373  //Clean up side effects
374  context->running = FALSE;
375 
376  //Close the UDP socket
377  socketClose(context->socket);
378  context->socket = NULL;
379  }
380 
381  //Return status code
382  return error;
383 }
384 
385 
386 /**
387  * @brief Stop CoAP server
388  * @param[in] context Pointer to the CoAP server context
389  * @return Error code
390  **/
391 
393 {
394 #if (COAP_SERVER_DTLS_SUPPORT == ENABLED)
395  uint_t i;
396 #endif
397 
398  //Make sure the CoAP server context is valid
399  if(context == NULL)
401 
402  //Debug message
403  TRACE_INFO("Stopping CoAP server...\r\n");
404 
405  //Check whether the CoAP server is running
406  if(context->running)
407  {
408 #if (NET_RTOS_SUPPORT == ENABLED)
409  //Stop the CoAP server
410  context->stop = TRUE;
411  //Send a signal to the task to abort any blocking operation
412  osSetEvent(&context->event);
413 
414  //Wait for the task to terminate
415  while(context->running)
416  {
417  osDelayTask(1);
418  }
419 #endif
420 
421 #if (COAP_SERVER_DTLS_SUPPORT == ENABLED)
422  //Loop through DTLS sessions
423  for(i = 0; i < context->numSessions; i++)
424  {
425  //Release DTLS session
426  coapServerDeleteSession(&context->sessions[i]);
427  }
428 #endif
429 
430  //Close the UDP socket
431  socketClose(context->socket);
432  context->socket = NULL;
433  }
434 
435  //Successful processing
436  return NO_ERROR;
437 }
438 
439 
440 /**
441  * @brief Create a new observable resource
442  * @param[in] context Pointer to the CoAP server context
443  * @param[in] uri NULL-terminated string containing the path to the resource
444  * @return Error code
445  **/
446 
448 {
449 #if (COAP_SERVER_OBSERVE_SUPPORT == ENABLED)
450  error_t error;
451  uint_t i;
452  CoapResource *resource;
453 
454  //Check parameters
455  if(context == NULL || uri == NULL)
457 
458  //Check the length of the URI
460  return ERROR_INVALID_LENGTH;
461 
462  //Initialize status code
463  error = NO_ERROR;
464 
465  //Acquire exclusive access to the CoAP server context
466  osAcquireMutex(&context->mutex);
467 
468  //Search the list of resources for a matching URI
469  resource = coapServerFindResource(context, uri);
470 
471  //If the resource does not exist, then create a new entry
472  if(resource == NULL)
473  {
474  //Loop through the list of resources
475  for(i = 0; i < context->numResources; i++)
476  {
477  //Check whether the entry is available
478  if(context->resources[i].uri[0] == '\0')
479  {
480  resource = &context->resources[i];
481  break;
482  }
483  }
484  }
485 
486  //Valid entry
487  if(resource != NULL)
488  {
489  //Save the full request URI
490  osStrcpy(resource->uri, uri);
491  //Initialize resource state
492  resource->dataLen = 0;
493 
494  //The sequence number may start at any value (refer to RFC 7641,
495  //section 4.4)
496  resource->seqNum = netGetRand(context->netContext) & 0x00FFFFFF;
497  }
498  else
499  {
500  //A new entry cannot be created
501  error = ERROR_OUT_OF_RESOURCES;
502  }
503 
504  //Release exclusive access to the CoAP server context
505  osReleaseMutex(&context->mutex);
506 
507  //Return status code
508  return error;
509 #else
510  //Not implemented
511  return ERROR_NOT_IMPLEMENTED;
512 #endif
513 }
514 
515 
516 /**
517  * @brief Update the state of an observable resource
518  * @param[in] context Pointer to the CoAP server context
519  * @param[in] uri NULL-terminated string containing the path to the resource
520  * @param[in] data New resource state
521  * @param[in] length Length of the resource state
522  * @return Error code
523  **/
524 
526  const void *data, size_t length)
527 {
528 #if (COAP_SERVER_OBSERVE_SUPPORT == ENABLED)
529  error_t error;
530  uint_t i;
531  CoapResource *resource;
532  CoapObserver *observer;
533 
534  //Check parameters
535  if(context == NULL || uri == NULL)
537 
538  //Check the length of the resource state
540  return ERROR_INVALID_LENGTH;
541 
542  //Initialize status code
543  error = NO_ERROR;
544 
545  //Acquire exclusive access to the CoAP server context
546  osAcquireMutex(&context->mutex);
547 
548  //Search the list of resources for a matching URI
549  resource = coapServerFindResource(context, uri);
550 
551  //Matching resource found?
552  if(resource != NULL)
553  {
554  //Save the new resource state
555  osMemcpy(resource->data, data, length);
556  //Update the length of the resource state
557  resource->dataLen = length;
558 
559  //An implementation can store a 24-bit unsigned integer variable per
560  //resource and increment this variable each time the resource undergoes
561  //a change of state (refer to RFC 7641, section 4.4)
562  resource->seqNum = (resource->seqNum + 1) & 0x00FFFFFF;
563 
564  //Loop through the list of registered observers
565  for(i = 0; i < context->numObservers; i++)
566  {
567  //Point to the current entry
568  observer = &context->observers[i];
569 
570  //Matching entry?
571  if(observer->resource == resource)
572  {
573  //The resource state has changed
574  observer->changed = TRUE;
575  }
576  }
577 
578  //Whenever the state of a resource changes, the server notifies each
579  //client in the list of observers of the resource
580  osSetEvent(&context->event);
581  }
582  else
583  {
584  //The target resource does not exist
585  error = ERROR_NOT_FOUND;
586  }
587 
588  //Release exclusive access to the CoAP server context
589  osReleaseMutex(&context->mutex);
590 
591  //Return status code
592  return error;
593 #else
594  //Not implemented
595  return ERROR_NOT_IMPLEMENTED;
596 #endif
597 }
598 
599 
600 /**
601  * @brief Delete an observable resource
602  * @param[in] context Pointer to the CoAP server context
603  * @param[in] uri NULL-terminated string containing the path to the resource
604  * @return Error code
605  **/
606 
608 {
609 #if (COAP_SERVER_OBSERVE_SUPPORT == ENABLED)
610  error_t error;
611  uint_t i;
612  CoapResource *resource;
613  CoapObserver *observer;
614 
615  //Check parameters
616  if(context == NULL || uri == NULL)
618 
619  //Initialize status code
620  error = NO_ERROR;
621 
622  //Acquire exclusive access to the CoAP server context
623  osAcquireMutex(&context->mutex);
624 
625  //Search the list of resources for a matching URI
626  resource = coapServerFindResource(context, uri);
627 
628  //Matching resource found?
629  if(resource != NULL)
630  {
631  //Loop through the list of registered observers
632  for(i = 0; i < context->numObservers; i++)
633  {
634  //Point to the current entry
635  observer = &context->observers[i];
636 
637  //Matching entry?
638  if(observer->resource == resource)
639  {
640  //Remove the client's entry from the list of observers of the
641  //resource
642  observer->resource = NULL;
643 
644  //The resource state has changed
645  observer->changed = TRUE;
646  }
647  }
648 
649  //The resource is deleted
650  osMemset(resource, 0, sizeof(CoapResource));
651  }
652  else
653  {
654  //The target resource does not exist
655  error = ERROR_NOT_FOUND;
656  }
657 
658  //Release exclusive access to the CoAP server context
659  osReleaseMutex(&context->mutex);
660 
661  //Return status code
662  return error;
663 #else
664  //Not implemented
665  return ERROR_NOT_IMPLEMENTED;
666 #endif
667 }
668 
669 
670 /**
671  * @brief CoAP server task
672  * @param[in] context Pointer to the CoAP server context
673  **/
674 
676 {
677  error_t error;
678  SocketMsg msg;
679  SocketEventDesc eventDesc;
680 
681 #if (NET_RTOS_SUPPORT == ENABLED)
682  //Task prologue
683  osEnterTask();
684 
685  //Process events
686  while(1)
687  {
688 #endif
689  //Specify the events the application is interested in
690  eventDesc.socket = context->socket;
691  eventDesc.eventMask = SOCKET_EVENT_RX_READY;
692  eventDesc.eventFlags = 0;
693 
694  //Wait for an event
695  socketPoll(&eventDesc, 1, &context->event, COAP_SERVER_TICK_INTERVAL);
696 
697  //Stop request?
698  if(context->stop)
699  {
700  //Stop CoAP server operation
701  context->running = FALSE;
702  //Task epilogue
703  osExitTask();
704  //Kill ourselves
706  }
707 
708  //Any datagram received?
709  if(eventDesc.eventFlags != 0)
710  {
711  //Point to the receive buffer
712  msg = SOCKET_DEFAULT_MSG;
713  msg.data = context->buffer;
715 
716  //Receive incoming datagram
717  error = socketReceiveMsg(context->socket, &msg, 0);
718 
719  //Check status code
720  if(!error)
721  {
722  //An endpoint must be prepared to receive multicast messages but may
723  //ignore them if multicast service discovery is not desired (refer
724  //to RFC 7252, section 8)
725  if(!ipIsMulticastAddr(&msg.destIpAddr))
726  {
727  //Retrieve the length of the datagram
728  context->bufferLen = msg.length;
729 
730  //Get the source and destination IP addresses
731  context->localInterface = msg.interface;
732  context->localIpAddr = msg.destIpAddr;
733  context->remoteIpAddr = msg.srcIpAddr;
734  context->remotePort = msg.srcPort;
735 
736  //Acquire exclusive access to the CoAP server context
737  osAcquireMutex(&context->mutex);
738 
739 #if (COAP_SERVER_DTLS_SUPPORT == ENABLED)
740  //DTLS-secured communication?
741  if(context->dtlsInitCallback != NULL)
742  {
743  //Demultiplexing of incoming datagrams into separate DTLS sessions
744  error = coapServerDemultiplexSession(context);
745  }
746  else
747 #endif
748  {
749  //Process the received CoAP message
750  error = coapServerProcessMessage(context, context->buffer,
751  context->bufferLen);
752  }
753 
754  //Release exclusive access to the CoAP server context
755  osReleaseMutex(&context->mutex);
756  }
757  }
758  }
759 
760  //Acquire exclusive access to the CoAP server context
761  osAcquireMutex(&context->mutex);
762  //Handle periodic operations
763  coapServerTick(context);
764  //Release exclusive access to the CoAP server context
765  osReleaseMutex(&context->mutex);
766 #if (NET_RTOS_SUPPORT == ENABLED)
767  }
768 #endif
769 }
770 
771 
772 /**
773  * @brief Release CoAP server context
774  * @param[in] context Pointer to the CoAP server context
775  **/
776 
778 {
779  //Make sure the CoAP server context is valid
780  if(context != NULL)
781  {
782  //Release previously allocated resources
783  osDeleteMutex(&context->mutex);
784  osDeleteEvent(&context->event);
785 
786 #if (COAP_SERVER_DTLS_SUPPORT == ENABLED && TLS_TICKET_SUPPORT == ENABLED)
787  //Release ticket encryption context
788  tlsFreeTicketContext(&context->dtlsTicketContext);
789 #endif
790 
791  //Clear CoAP server context
792  osMemset(context, 0, sizeof(CoapServerContext));
793  }
794 }
795 
796 #endif
NetInterface * interface
Underlying network interface.
Definition: coap_server.h:241
OsTaskId osCreateTask(const char_t *name, OsTaskCode taskCode, void *arg, const OsTaskParameters *params)
Create a task.
@ SOCKET_IP_PROTO_UDP
Definition: socket.h:108
error_t socketBind(Socket *socket, const IpAddr *localIpAddr, uint16_t localPort)
Associate a local address with a socket.
Definition: socket.c:1344
error_t tlsInitTicketContext(TlsTicketContext *ticketContext)
Initialize ticket encryption context.
Definition: tls_ticket.c:49
@ ERROR_NOT_FOUND
Definition: error.h:148
bool_t osCreateMutex(OsMutex *mutex)
Create a mutex object.
#define osExitTask()
@ ERROR_NOT_IMPLEMENTED
Definition: error.h:66
#define TRUE
Definition: os_port.h:50
uint8_t data[]
Definition: ethernet.h:224
Message and ancillary data.
Definition: socket.h:241
#define OS_INVALID_TASK_ID
void socketClose(Socket *socket)
Close an existing socket.
Definition: socket.c:2094
error_t coapServerUpdateResource(CoapServerContext *context, const char_t *uri, const void *data, size_t length)
Update the state of an observable resource.
Definition: coap_server.c:525
void coapServerTask(CoapServerContext *context)
CoAP server task.
Definition: coap_server.c:675
uint_t numSessions
Maximum number of DTLS sessions.
Definition: coap_server.h:244
@ ERROR_OUT_OF_RESOURCES
Definition: error.h:64
CoapServerUdpInitCallback udpInitCallback
UDP initialization callback.
Definition: coap_server.h:253
@ SOCKET_TYPE_DGRAM
Definition: socket.h:93
#define CoapObserver
Definition: coap_server.h:176
void * data
Pointer to the payload.
Definition: socket.h:242
Helper functions for CoAP server.
#define osStrlen(s)
Definition: os_port.h:171
error_t coapServerProcessMessage(CoapServerContext *context, const uint8_t *data, size_t length)
Process CoAP message.
#define OS_SELF_TASK_ID
Structure describing socket events.
Definition: socket.h:433
#define CoapResource
Definition: coap_server.h:172
#define COAP_SERVER_TICK_INTERVAL
Definition: coap_server.h:70
@ ERROR_OPEN_FAILED
Definition: error.h:75
uint16_t port
CoAP port number.
Definition: coap_server.h:242
error_t coapServerDeleteResource(CoapServerContext *context, const char_t *uri)
Delete an observable resource.
Definition: coap_server.c:607
OsTaskParameters task
Task parameters.
Definition: coap_server.h:239
const IpAddr IP_ADDR_ANY
Definition: ip.c:53
void osDeleteTask(OsTaskId taskId)
Delete a task.
NetInterface * interface
Underlying network interface.
Definition: socket.h:248
#define FALSE
Definition: os_port.h:46
const SocketMsg SOCKET_DEFAULT_MSG
Definition: socket.c:52
void coapServerTick(CoapServerContext *context)
Handle periodic operations.
size_t length
Actual length of the payload, in bytes.
Definition: socket.h:244
uint_t numObservers
Maximum number of observers.
Definition: coap_server.h:250
@ ERROR_INVALID_PARAMETER
Invalid parameter.
Definition: error.h:47
#define osMemcpy(dest, src, length)
Definition: os_port.h:147
uint32_t netGetRand(NetContext *context)
Generate a random 32-bit value.
Definition: net.c:441
CoapServerObserveCallback observeCallback
Observe callback.
Definition: coap_server.h:259
error_t
Error codes.
Definition: error.h:43
bool_t ipIsMulticastAddr(const IpAddr *ipAddr)
Determine whether an IP address is a multicast address.
Definition: ip.c:251
void(* OsTaskCode)(void *arg)
Task routine.
#define COAP_SERVER_PRIORITY
Definition: coap_server.h:147
CoapObserver * observers
Observers.
Definition: coap_server.h:251
void coapServerDeleteSession(CoapDtlsSession *session)
Delete DTLS session.
void osDeleteEvent(OsEvent *event)
Delete an event object.
#define COAP_PORT
Definition: coap_common.h:38
IpAddr srcIpAddr
Source IP address.
Definition: socket.h:249
@ ERROR_INVALID_LENGTH
Definition: error.h:111
error_t socketReceiveMsg(Socket *socket, SocketMsg *message, uint_t flags)
Receive a message from a connectionless socket.
Definition: socket.c:1926
NetContext * netGetDefaultContext(void)
Get default TCP/IP stack context.
Definition: net.c:527
const OsTaskParameters OS_TASK_DEFAULT_PARAMS
#define TRACE_INFO(...)
Definition: debug.h:105
uint8_t length
Definition: tcp.h:375
error_t coapServerInit(CoapServerContext *context, const CoapServerSettings *settings)
CoAP server initialization.
Definition: coap_server.c:108
#define osEnterTask()
uint_t eventFlags
Returned events.
Definition: socket.h:436
#define CoapDtlsSession
Definition: coap_server.h:168
NetContext * netContext
TCP/IP stack context.
Definition: coap_server.h:240
CoapResource * coapServerFindResource(CoapServerContext *context, const char_t *uri)
Search the list of resources for a given URI.
error_t socketPoll(SocketEventDesc *eventDesc, uint_t size, OsEvent *extEvent, systime_t timeout)
Wait for one of a set of sockets to become ready to perform I/O.
Definition: socket.c:2182
#define socketBindToInterface
Definition: net_legacy.h:193
CoAP server.
IpAddr destIpAddr
Destination IP address.
Definition: socket.h:251
char char_t
Definition: compiler_port.h:55
void osDeleteMutex(OsMutex *mutex)
Delete a mutex object.
error_t coapServerDemultiplexSession(CoapServerContext *context)
DTLS session demultiplexing.
@ SOCKET_EVENT_RX_READY
Definition: socket.h:179
Data logging functions for debugging purpose (CoAP)
#define COAP_SERVER_MAX_URI_LEN
Definition: coap_server.h:140
void coapServerDeinit(CoapServerContext *context)
Release CoAP server context.
Definition: coap_server.c:777
uint_t numResources
Maximum number of observable resources.
Definition: coap_server.h:248
void osAcquireMutex(OsMutex *mutex)
Acquire ownership of the specified mutex object.
void coapServerGetDefaultSettings(CoapServerSettings *settings)
Initialize settings with default values.
Definition: coap_server.c:52
void osReleaseMutex(OsMutex *mutex)
Release ownership of the specified mutex object.
Socket * socketOpenEx(NetContext *context, uint_t type, uint_t protocol)
Create a socket.
Definition: socket.c:146
#define CoapServerContext
Definition: coap_server.h:164
CoapServerRequestCallback requestCallback
CoAP request callback.
Definition: coap_server.h:257
Transport protocol abstraction layer.
error_t coapServerSetCookieSecret(CoapServerContext *context, const uint8_t *cookieSecret, size_t cookieSecretLen)
Set cookie secret.
Definition: coap_server.c:264
bool_t osCreateEvent(OsEvent *event)
Create an event object.
size_t size
Size of the payload, in bytes.
Definition: socket.h:243
error_t coapServerCreateResource(CoapServerContext *context, const char_t *uri)
Create a new observable resource.
Definition: coap_server.c:447
void osDelayTask(systime_t delay)
Delay routine.
void osSetEvent(OsEvent *event)
Set the specified event object to the signaled state.
CoapServerDtlsInitCallback dtlsInitCallback
DTLS initialization callback.
Definition: coap_server.h:255
void tlsFreeTicketContext(TlsTicketContext *ticketContext)
Properly dispose ticket encryption context.
Definition: tls_ticket.c:448
CoapResource * resources
Observable resources.
Definition: coap_server.h:249
#define COAP_SERVER_STACK_SIZE
Definition: coap_server.h:63
#define COAP_SERVER_MAX_COOKIE_SECRET_SIZE
Definition: coap_server.h:133
Socket * socket
Handle to a socket to monitor.
Definition: socket.h:434
unsigned int uint_t
Definition: compiler_port.h:57
#define osMemset(p, value, length)
Definition: os_port.h:141
error_t coapServerStop(CoapServerContext *context)
Stop CoAP server.
Definition: coap_server.c:392
CoapDtlsSession * sessions
DTLS sessions.
Definition: coap_server.h:245
CoAP server settings.
Definition: coap_server.h:238
#define osStrcpy(s1, s2)
Definition: os_port.h:213
uint16_t srcPort
Source port.
Definition: socket.h:250
error_t socketSetTimeout(Socket *socket, systime_t timeout)
Set timeout value for blocking operations.
Definition: socket.c:169
#define COAP_SERVER_BUFFER_SIZE
Definition: coap_server.h:119
error_t coapServerStart(CoapServerContext *context)
Start CoAP server.
Definition: coap_server.c:294
uint_t eventMask
Requested events.
Definition: socket.h:435
@ ERROR_ALREADY_RUNNING
Definition: error.h:294
#define COAP_SERVER_MAX_OBS_RESOURCE_SIZE
Definition: coap_server.h:126
@ NO_ERROR
Success.
Definition: error.h:44
Debugging facilities.