coap_client_request.c
Go to the documentation of this file.
1 /**
2  * @file coap_client_request.c
3  * @brief CoAP request handling
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2024 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.4.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 "core/net.h"
37 #include "coap/coap_client.h"
39 #include "coap/coap_client_block.h"
40 #include "coap/coap_client_misc.h"
41 #include "debug.h"
42 
43 //Check TCP/IP stack configuration
44 #if (COAP_CLIENT_SUPPORT == ENABLED)
45 
46 
47 /**
48  * @brief Initialize a new CoAP request
49  * @param[in] context Pointer to the CoAP client context
50  * @return Pointer to the new CoAP request
51  **/
52 
54 {
55  uint_t i;
56  CoapClientRequest *request;
57  CoapMessageHeader *header;
58 
59  //Initialize request handle
60  request = NULL;
61 
62  //Make sure the CoAP client context is valid
63  if(context != NULL)
64  {
65  //Acquire exclusive access to the CoAP client context
66  osAcquireMutex(&context->mutex);
67 
68  //Loop through the CoAP request table
69  for(i = 0; i < COAP_CLIENT_NSTART; i++)
70  {
71  //Unused request found?
72  if(context->request[i].state == COAP_REQ_STATE_UNUSED)
73  {
74  //Point to the current request
75  request = &context->request[i];
76 
77  //Initialize request state
78  request->state = COAP_REQ_STATE_INIT;
79  //Attach CoAP client context
80  request->context = context;
81  //Set default request timeout
82  request->timeout = context->timeout;
83  //Initialize request callback
84  request->callback = NULL;
85 
86 #if (COAP_CLIENT_BLOCK_SUPPORT == ENABLED)
87  //Default TX block size
88  request->txBlockSzx = coapClientGetMaxBlockSize();
89  //Default RX block size
90  request->rxBlockSzx = COAP_BLOCK_SIZE_RESERVED;
91 #endif
92  //Point to the CoAP message header
93  header = (CoapMessageHeader *) request->message.buffer;
94 
95  //Set default parameters
96  header->version = COAP_VERSION_1;
97  header->type = COAP_TYPE_CON;
98  header->tokenLen = (uint8_t) context->tokenLen;
99  header->code = COAP_CODE_GET;
100  header->mid = 0;
101 
102  //A Token is used to match responses to requests independently from
103  //the underlying message
104  coapClientGenerateToken(context, header);
105 
106  //Calculate the length of the CoAP message
107  request->message.length = sizeof(CoapMessageHeader) + header->tokenLen;
108 
109  //We are done
110  break;
111  }
112  }
113 
114  //Release exclusive access to the CoAP client context
115  osReleaseMutex(&context->mutex);
116  }
117 
118  //Return a handle to the freshly created request
119  return request;
120 }
121 
122 
123 /**
124  * @brief Set request timeout
125  * @param[in] request CoAP request handle
126  * @param[in] timeout Timeout value, in milliseconds
127  * @return Error code
128  **/
129 
131  systime_t timeout)
132 {
133  //Make sure the CoAP request handle is valid
134  if(request == NULL)
136 
137  //Acquire exclusive access to the CoAP client context
138  osAcquireMutex(&request->context->mutex);
139  //Save timeout value
140  request->timeout = timeout;
141  //Release exclusive access to the CoAP client context
142  osReleaseMutex(&request->context->mutex);
143 
144  //Successful processing
145  return NO_ERROR;
146 }
147 
148 
149 /**
150  * @brief Send a CoAP request
151  * @param[in] request CoAP request handle
152  * @param[in] callback Callback function to invoke when the request completes
153  * @param[in] param Callback function parameter
154  * @return Error code
155  **/
156 
158  CoapRequestCallback callback, void *param)
159 {
160  error_t error;
161  CoapClientContext *context;
162  CoapMessageHeader *header;
163 
164  //Make sure the CoAP request handle is valid
165  if(request == NULL)
167 
168  //Point to the CoAP client context
169  context = request->context;
170 
171  //Acquire exclusive access to the CoAP client context
172  osAcquireMutex(&context->mutex);
173 
174  //Initialize status code
175  error = NO_ERROR;
176 
177  //Send the CoAP request and wait for the response
178  while(!error)
179  {
180  //Check current state
181  if(request->state == COAP_REQ_STATE_INIT)
182  {
183  //Point to the CoAP message header
184  header = (CoapMessageHeader *) request->message.buffer;
185 
186  //The message ID is a 16-bit unsigned integer that is generated by
187  //the sender of a Confirmable or Non-confirmable message
188  coapClientGenerateMessageId(context, header);
189 
190  //Reset retransmission counter
191  request->retransmitCount = 0;
192 
193  //Save callback function
194  request->callback = callback;
195  request->param = param;
196 
197  //Send CoAP request
199  }
200  else
201  {
202  //Any callback function defined?
203  if(request->callback != NULL)
204  {
205  //The CoAP client operates in asynchronous mode when a callback
206  //function has been defined
207  break;
208  }
209  else
210  {
211  //When the CoAP client operates in synchronous mode, the function
212  //blocks until a response is received or a timeout occurs
213  if(request->state == COAP_REQ_STATE_TRANSMIT ||
214  request->state == COAP_REQ_STATE_RECEIVE ||
215  request->state == COAP_REQ_STATE_SEPARATE)
216  {
217  //Wait for a response to be received
218  error = coapClientProcessEvents(context,
220 
221 #if (NET_RTOS_SUPPORT == DISABLED)
222  //Check status code
223  if(error == NO_ERROR)
224  {
225  //The CoAP client operates in non-blocking mode
226  if(request->state == COAP_REQ_STATE_TRANSMIT ||
227  request->state == COAP_REQ_STATE_RECEIVE ||
228  request->state == COAP_REQ_STATE_SEPARATE)
229  {
230  //Exit immediately
231  error = ERROR_WOULD_BLOCK;
232  }
233  }
234 #endif
235  }
236  else if(request->state == COAP_REQ_STATE_OBSERVE ||
237  request->state == COAP_REQ_STATE_DONE)
238  {
239  //A valid response has been received
240  break;
241  }
242  else if(request->state == COAP_REQ_STATE_RESET)
243  {
244  //A Reset message has been received
245  error = ERROR_FAILURE;
246  }
247  else if(request->state == COAP_REQ_STATE_TIMEOUT)
248  {
249  //Report a timeout error
250  error = ERROR_TIMEOUT;
251  }
252  else
253  {
254  //Invalid state
255  error = ERROR_WRONG_STATE;
256  }
257  }
258  }
259  }
260 
261  //Release exclusive access to the CoAP client context
262  osReleaseMutex(&context->mutex);
263 
264  //Return status code
265  return error;
266 }
267 
268 
269 /**
270  * @brief Cancel an outstanding CoAP request
271  * @param[in] request CoAP request handle
272  * @return Pointer to the CoAP request
273  **/
274 
276 {
277  error_t error;
278 #if (COAP_CLIENT_OBSERVE_SUPPORT == ENABLED)
279  uint32_t value;
280  CoapMessageHeader *header;
281 #endif
282 
283  //Make sure the CoAP request handle is valid
284  if(request == NULL)
286 
287  //Acquire exclusive access to the CoAP client context
288  osAcquireMutex(&request->context->mutex);
289 
290 #if (COAP_CLIENT_OBSERVE_SUPPORT == ENABLED)
291  //Search the CoAP request for an Observe option
292  error = coapGetUintOption(&request->message, COAP_OPT_OBSERVE, 0, &value);
293 
294  //Check whether the option has been found
295  if(!error)
296  {
297  //Point to the CoAP message header
298  header = (CoapMessageHeader *) request->message.buffer;
299 
300  //The message ID is a 16-bit unsigned integer that is generated by
301  //the sender of a Confirmable or Non-confirmable message
302  coapClientGenerateMessageId(request->context, header);
303 
304  //Set the Observe option to the value 1 (deregister)
305  error = coapSetUintOption(&request->message, COAP_OPT_OBSERVE, 0,
307 
308  //Check status code
309  if(!error)
310  {
311  //Reset retransmission counter
312  request->retransmitCount = 0;
313 
314  //A client may explicitly deregister by issuing a GET request that has
315  //the Token field set to the token of the observation to be cancelled
316  //and includes an Observe Option with the value set to 1 (deregister)
318  }
319  else
320  {
321  //Cancel the specified request
323  //Release the resources associated with the CoAP request
324  request->state = COAP_REQ_STATE_UNUSED;
325  }
326  }
327  else
328 #endif
329  {
330  //Cancel the specified request
332  //Release the resources associated with the CoAP request
333  request->state = COAP_REQ_STATE_UNUSED;
334  }
335 
336  //Release exclusive access to the CoAP client context
337  osReleaseMutex(&request->context->mutex);
338 
339  //Return status code
340  return error;
341 }
342 
343 
344 /**
345  * @brief Release the resources associated with a CoAP request
346  * @param[in] request CoAP request handle
347  **/
348 
350 {
351  //Valid CoAP request?
352  if(request != NULL)
353  {
354  //Acquire exclusive access to the CoAP client context
355  osAcquireMutex(&request->context->mutex);
356  //The request is no more in use
357  request->state = COAP_REQ_STATE_UNUSED;
358  //Release exclusive access to the CoAP client context
359  osReleaseMutex(&request->context->mutex);
360  }
361 }
362 
363 
364 /**
365  * @brief Get request message
366  * @param[in] request CoAP request handle
367  * @return Pointer to the request message
368  **/
369 
371 {
372  CoapClientContext *context;
374 
375  //Point to the CoAP client context
376  context = request->context;
377 
378  //Acquire exclusive access to the CoAP client context
379  osAcquireMutex(&context->mutex);
380 
381  //Check request state
382  if(request->state == COAP_REQ_STATE_INIT ||
383  request->state == COAP_REQ_STATE_OBSERVE ||
384  request->state == COAP_REQ_STATE_DONE ||
385  request->state == COAP_REQ_STATE_RESET ||
386  request->state == COAP_REQ_STATE_TIMEOUT ||
387  request->state == COAP_REQ_STATE_CANCELED)
388  {
389  //Re-initialize request state
391  //Point to the request message
392  message = &request->message;
393  }
394  else
395  {
396  //The request cannot be accessed while the message exchange is on-going
397  message = NULL;
398  }
399 
400  //Release exclusive access to the CoAP client context
401  osReleaseMutex(&context->mutex);
402 
403  //Return a pointer to the request message
404  return message;
405 }
406 
407 
408 /**
409  * @brief Get response message
410  * @param[in] request CoAP request handle
411  * @return Pointer to the response message
412  **/
413 
415 {
416  CoapClientContext *context;
418 
419  //Point to the CoAP client context
420  context = request->context;
421 
422  //Acquire exclusive access to the CoAP client context
423  osAcquireMutex(&context->mutex);
424 
425  //Check request state
426  if(request->state == COAP_REQ_STATE_INIT ||
427  request->state == COAP_REQ_STATE_OBSERVE ||
428  request->state == COAP_REQ_STATE_DONE)
429  {
430  //Point to the response message
431  message = &context->response;
432  }
433  else
434  {
435  //The response cannot be accessed while the message exchange is on-going
436  message = NULL;
437  }
438 
439  //Release exclusive access to the CoAP client context
440  osReleaseMutex(&context->mutex);
441 
442  //Return a pointer to the response message
443  return message;
444 }
445 
446 
447 /**
448  * @brief Set message type
449  * @param[in] message Pointer to the CoAP message
450  * @param[in] type Message type (Confirmable or Non-confirmable)
451  * @return Error code
452  **/
453 
455 {
456  //Make sure the CoAP message is valid
457  if(message == NULL)
459 
460  //Set message type
461  return coapSetType(message, type);
462 }
463 
464 
465 /**
466  * @brief Get message type
467  * @param[in] message Pointer to the CoAP message
468  * @param[out] type Message type
469  * @return Error code
470  **/
471 
473 {
474  //Check parameters
475  if(message == NULL || type == NULL)
477 
478  //Get message type
479  return coapGetType(message, type);
480 }
481 
482 
483 /**
484  * @brief Set request method
485  * @param[in] message Pointer to the CoAP message
486  * @param[in] code Method code (GET, POST, PUT or DELETE)
487  * @return Error code
488  **/
489 
491 {
492  //Make sure the CoAP message is valid
493  if(message == NULL)
495 
496  //Set request method
497  return coapSetCode(message, code);
498 }
499 
500 
501 /**
502  * @brief Get request method
503  * @param[in] message Pointer to the CoAP message
504  * @param[out] code Method code
505  * @return Error code
506  **/
507 
509 {
510  //Check parameters
511  if(message == NULL || code == NULL)
513 
514  //Get request method
515  return coapGetCode(message, code);
516 }
517 
518 
519 /**
520  * @brief Get response code
521  * @param[in] message Pointer to the CoAP message
522  * @param[out] code Response code
523  * @return Error code
524  **/
525 
527 {
528  //Check parameters
529  if(message == NULL || code == NULL)
531 
532  //Get response code
533  return coapGetCode(message, code);
534 }
535 
536 
537 /**
538  * @brief Set Uri-Path option
539  * @param[in] message Pointer to the CoAP message
540  * @param[in] path NULL-terminated string that contains the path component
541  * @return Error code
542  **/
543 
545 {
546  //Check parameters
547  if(message == NULL || path == NULL)
549 
550  //Encode the path component into multiple Uri-Path options
552 }
553 
554 
555 /**
556  * @brief Get Uri-Path option
557  * @param[in] message Pointer to the CoAP message
558  * @param[out] path Pointer to the buffer where to copy the path component
559  * @param[in] maxLen Maximum number of characters the buffer can hold
560  * @return Error code
561  **/
562 
564  size_t maxLen)
565 {
566  //Check parameters
567  if(message == NULL || path == NULL)
569 
570  //Reconstruct the path component from Uri-Path options
572  maxLen, '/');
573 }
574 
575 
576 /**
577  * @brief Set Uri-Query option
578  * @param[in] message Pointer to the CoAP message
579  * @param[in] queryString NULL-terminated string that contains the query string
580  * @return Error code
581  **/
582 
584 {
585  //Check parameters
586  if(message == NULL || queryString == NULL)
588 
589  //Encode the query string into multiple Uri-Query options
591  queryString, '&');
592 }
593 
594 
595 /**
596  * @brief Get Uri-Query option
597  * @param[in] message Pointer to the CoAP message
598  * @param[out] queryString Pointer to the buffer where to copy the query string
599  * @param[in] maxLen Maximum number of characters the buffer can hold
600  * @return Error code
601  **/
602 
604  size_t maxLen)
605 {
606  //Check parameters
607  if(message == NULL || queryString == NULL)
609 
610  //Reconstruct the query string from Uri-Query options
612  maxLen, '&');
613 }
614 
615 
616 /**
617  * @brief Get Location-Path option
618  * @param[in] message Pointer to the CoAP message
619  * @param[out] path Pointer to the buffer where to copy the path component
620  * @param[in] maxLen Maximum number of characters the buffer can hold
621  * @return Error code
622  **/
623 
625  size_t maxLen)
626 {
627  //Check parameters
628  if(message == NULL || path == NULL)
630 
631  //Reconstruct the path component from Location-Path options
633  maxLen, '/');
634 }
635 
636 
637 /**
638  * @brief Get Location-Query option
639  * @param[in] message Pointer to the CoAP message
640  * @param[out] queryString Pointer to the buffer where to copy the query string
641  * @param[in] maxLen Maximum number of characters the buffer can hold
642  * @return Error code
643  **/
644 
646  char_t *queryString, size_t maxLen)
647 {
648  //Check parameters
649  if(message == NULL || queryString == NULL)
651 
652  //Reconstruct the query string from Location-Query options
654  queryString, maxLen, '&');
655 }
656 
657 
658 /**
659  * @brief Add an opaque option to the CoAP message
660  * @param[in] message Pointer to the CoAP message
661  * @param[in] optionNum Option number
662  * @param[in] optionIndex Occurrence index (for repeatable options only)
663  * @param[in] optionValue Pointer to the first byte of the option value
664  * @param[in] optionLen Length of the option, in bytes
665  * @return Error code
666  **/
667 
669  uint_t optionIndex, const uint8_t *optionValue, size_t optionLen)
670 {
671  //Make sure the CoAP message is valid
672  if(message == NULL)
674 
675  //Inconsistent option value?
676  if(optionValue == NULL && optionLen != 0)
678 
679  //Add the specified option to the CoAP message
680  return coapSetOption(message, optionNum, optionIndex, optionValue,
681  optionLen);
682 }
683 
684 
685 /**
686  * @brief Add a string option to the CoAP message
687  * @param[in] message Pointer to the CoAP message
688  * @param[in] optionNum Option number
689  * @param[in] optionIndex Occurrence index (for repeatable options only)
690  * @param[in] optionValue NULL-terminated string that contains the option value
691  * @return Error code
692  **/
693 
695  uint_t optionIndex, const char_t *optionValue)
696 {
697  size_t n;
698 
699  //Check parameters
700  if(message == NULL || optionValue == NULL)
702 
703  //Retrieve the length of the string
704  n = osStrlen(optionValue);
705 
706  //Add the specified option to the CoAP message
707  return coapSetOption(message, optionNum, optionIndex,
708  (const uint8_t *) optionValue, n);
709 }
710 
711 
712 /**
713  * @brief Add a uint option to the CoAP message
714  * @param[in] message Pointer to the CoAP message
715  * @param[in] optionNum Option number
716  * @param[in] optionIndex Occurrence index (for repeatable options only)
717  * @param[in] optionValue Option value (unsigned integer)
718  * @return Error code
719  **/
720 
722  uint_t optionIndex, uint32_t optionValue)
723 {
724  //Make sure the CoAP message is valid
725  if(message == NULL)
727 
728  //Add the specified option to the CoAP message
729  return coapSetUintOption(message, optionNum, optionIndex, optionValue);
730 }
731 
732 
733 /**
734  * @brief Read an opaque option from the CoAP message
735  * @param[in] message Pointer to the CoAP message
736  * @param[in] optionNum Option number to search for
737  * @param[in] optionIndex Occurrence index (for repeatable options only)
738  * @param[out] optionValue Pointer to the first byte of the option value
739  * @param[out] optionLen Length of the option, in bytes
740  * @return Error code
741  **/
742 
744  uint_t optionIndex, const uint8_t **optionValue, size_t *optionLen)
745 {
746  //Check parameters
747  if(message == NULL || optionValue == NULL || optionLen == NULL)
749 
750  //Search the CoAP message for the specified option number
751  return coapGetOption(message, optionNum, optionIndex, optionValue,
752  optionLen);
753 }
754 
755 
756 /**
757  * @brief Read a string option from the CoAP message
758  * @param[in] message Pointer to the CoAP message
759  * @param[in] optionNum Option number to search for
760  * @param[in] optionIndex Occurrence index (for repeatable options only)
761  * @param[out] optionValue Pointer to the first byte of the option value
762  * @param[out] optionLen Length of the option, in characters
763  * @return Error code
764  **/
765 
767  uint_t optionIndex, const char_t **optionValue, size_t *optionLen)
768 {
769  //Check parameters
770  if(message == NULL || optionValue == NULL || optionLen == NULL)
772 
773  //Search the CoAP message for the specified option number
774  return coapGetOption(message, optionNum, optionIndex,
775  (const uint8_t **) optionValue, optionLen);
776 }
777 
778 
779 /**
780  * @brief Read an uint option from the CoAP message
781  * @param[in] message Pointer to the CoAP message
782  * @param[in] optionNum Option number to search for
783  * @param[in] optionIndex Occurrence index (for repeatable options only)
784  * @param[out] optionValue Option value (unsigned integer)
785  * @return Error code
786  **/
787 
789  uint_t optionIndex, uint32_t *optionValue)
790 {
791  //Check parameters
792  if(message == NULL || optionValue == NULL)
794 
795  //Search the CoAP message for the specified option number
796  return coapGetUintOption(message, optionNum, optionIndex, optionValue);
797 }
798 
799 
800 /**
801  * @brief Remove an option from the CoAP message
802  * @param[in] message Pointer to the CoAP message
803  * @param[in] optionNum Option number
804  * @param[in] optionIndex Occurrence index (for repeatable options only)
805  * @return Error code
806  **/
807 
809  uint_t optionIndex)
810 {
811  //Make sure the CoAP message is valid
812  if(message == NULL)
814 
815  //Remove the specified option from the CoAP message
816  return coapDeleteOption(message, optionNum, optionIndex);
817 }
818 
819 
820 /**
821  * @brief Set message payload
822  * @param[in] message Pointer to the CoAP message
823  * @param[out] payload Pointer to request payload
824  * @param[out] payloadLen Length of the payload, in bytes
825  * @return Error code
826  **/
827 
829  size_t payloadLen)
830 {
831  //Make sure the CoAP message is valid
832  if(message == NULL)
834 
835  //Check parameters
836  if(payload == NULL && payloadLen != 0)
838 
839  //Set message payload
841 }
842 
843 
844 /**
845  * @brief Get message payload
846  * @param[in] message Pointer to the CoAP message
847  * @param[out] payload Pointer to the first byte of the payload
848  * @param[out] payloadLen Length of the payload, in bytes
849  * @return Error code
850  **/
851 
853  size_t *payloadLen)
854 {
855  //Check parameters
856  if(message == NULL || payload == NULL || payloadLen == NULL)
858 
859  //Get response payload
861 }
862 
863 
864 /**
865  * @brief Write payload data
866  * @param[in] message Pointer to the CoAP message
867  * @param[in] data Pointer to a buffer containing the data to be written
868  * @param[in] length Number of bytes to written
869  * @return Error code
870  **/
871 
873  size_t length)
874 {
875  //Check parameters
876  if(message == NULL || data == NULL)
878 
879  //Write payload data
881 }
882 
883 
884 /**
885  * @brief Read payload data
886  * @param[in] message Pointer to the CoAP message
887  * @param[out] data Buffer into which received data will be placed
888  * @param[in] size Maximum number of bytes that can be received
889  * @param[out] length Number of bytes that have been received
890  * @return Error code
891  **/
892 
894  size_t *length)
895 {
896  //Check parameters
897  if(message == NULL || data == NULL)
899 
900  //Read payload data
901  return coapReadPayload(message, data, size, length);
902 }
903 
904 #endif
error_t coapClientSetPayload(CoapMessage *message, const void *payload, size_t payloadLen)
Set message payload.
error_t coapSetCode(CoapMessage *message, CoapCode code)
Set method or response code.
Definition: coap_message.c:201
error_t coapClientGetLocationPath(const CoapMessage *message, char_t *path, size_t maxLen)
Get Location-Path option.
CoAP request handling.
error_t coapSetUintOption(CoapMessage *message, uint16_t optionNum, uint_t optionIndex, uint32_t optionValue)
Add a uint option to the specified CoAP message.
Definition: coap_option.c:543
error_t coapGetPayload(const CoapMessage *message, const uint8_t **payload, size_t *payloadLen)
Get CoAP message payload.
Definition: coap_message.c:324
error_t coapSetType(CoapMessage *message, CoapMessageType type)
Set message type.
Definition: coap_message.c:151
error_t coapGetUintOption(const CoapMessage *message, uint16_t optionNum, uint_t optionIndex, uint32_t *optionValue)
Get the value of the specified uint option.
Definition: coap_option.c:651
uint8_t code
Definition: coap_common.h:179
CoapMessage * coapClientGetRequestMessage(CoapClientRequest *request)
Get request message.
@ ERROR_WOULD_BLOCK
Definition: error.h:96
error_t coapClientSendRequest(CoapClientRequest *request, CoapRequestCallback callback, void *param)
Send a CoAP request.
@ COAP_OPT_LOCATION_PATH
Definition: coap_option.h:98
@ COAP_REQ_STATE_TRANSMIT
uint8_t message[]
Definition: chap.h:154
error_t coapDeleteOption(CoapMessage *message, uint16_t optionNum, uint_t optionIndex)
Remove an option from the specified CoAP message.
Definition: coap_option.c:688
uint8_t data[]
Definition: ethernet.h:222
error_t coapClientSetUriQuery(CoapMessage *message, const char_t *queryString)
Set Uri-Query option.
error_t coapJoinRepeatableOption(const CoapMessage *message, uint16_t optionNum, char_t *optionValue, size_t maxLen, char_t separator)
Decode a path or query component from multiple repeatable options.
Definition: coap_option.c:877
uint8_t type
Definition: coap_common.h:176
#define osStrlen(s)
Definition: os_port.h:165
CoapClientRequest * coapClientCreateRequest(CoapClientContext *context)
Initialize a new CoAP request.
@ COAP_REQ_STATE_RESET
@ COAP_REQ_STATE_DONE
#define CoapClientContext
Definition: coap_client.h:144
error_t coapClientChangeRequestState(CoapClientRequest *request, CoapRequestState newState)
Update CoAP request state.
#define COAP_CLIENT_NSTART
Definition: coap_client.h:84
error_t coapClientGetUriQuery(const CoapMessage *message, char_t *queryString, size_t maxLen)
Get Uri-Query option.
#define CoapClientRequest
Definition: coap_client.h:148
CoapMessage * coapClientGetResponseMessage(CoapClientRequest *request)
Get response message.
@ ERROR_WRONG_STATE
Definition: error.h:209
error_t coapClientSetOpaqueOption(CoapMessage *message, uint16_t optionNum, uint_t optionIndex, const uint8_t *optionValue, size_t optionLen)
Add an opaque option to the CoAP message.
error_t coapClientSetRequestTimeout(CoapClientRequest *request, systime_t timeout)
Set request timeout.
error_t coapClientGetMethodCode(const CoapMessage *message, CoapCode *code)
Get request method.
#define COAP_CLIENT_TICK_INTERVAL
Definition: coap_client.h:70
error_t coapClientSetUriPath(CoapMessage *message, const char_t *path)
Set Uri-Path option.
@ COAP_VERSION_1
CoAP version 1.
Definition: coap_common.h:69
error_t coapClientGetOpaqueOption(const CoapMessage *message, uint16_t optionNum, uint_t optionIndex, const uint8_t **optionValue, size_t *optionLen)
Read an opaque option from the CoAP message.
@ COAP_REQ_STATE_OBSERVE
@ COAP_OPT_URI_QUERY
Definition: coap_option.h:102
@ ERROR_INVALID_PARAMETER
Invalid parameter.
Definition: error.h:47
error_t coapClientSetMethodCode(CoapMessage *message, CoapCode code)
Set request method.
error_t coapGetType(const CoapMessage *message, CoapMessageType *type)
Get message type.
Definition: coap_message.c:176
error_t
Error codes.
Definition: error.h:43
error_t coapSetOption(CoapMessage *message, uint16_t optionNum, uint_t optionIndex, const uint8_t *optionValue, size_t optionLen)
Add an option to the specified CoAP message.
Definition: coap_option.c:388
@ ERROR_FAILURE
Generic error code.
Definition: error.h:45
error_t coapClientGetStringOption(const CoapMessage *message, uint16_t optionNum, uint_t optionIndex, const char_t **optionValue, size_t *optionLen)
Read a string option from the CoAP message.
error_t coapClientGetPayload(const CoapMessage *message, const uint8_t **payload, size_t *payloadLen)
Get message payload.
CoAP message.
Definition: coap_message.h:56
CoAP client.
error_t coapClientSetType(CoapMessage *message, CoapMessageType type)
Set message type.
error_t coapClientProcessEvents(CoapClientContext *context, systime_t timeout)
Process CoAP client events.
CoapBlockSize coapClientGetMaxBlockSize(void)
Get maximum block size.
error_t coapClientGetUriPath(const CoapMessage *message, char_t *path, size_t maxLen)
Get Uri-Path option.
@ COAP_OPT_OBSERVE
Definition: coap_option.h:96
@ COAP_OBSERVE_DEREGISTER
Definition: coap_option.h:137
uint8_t length
Definition: tcp.h:368
@ COAP_TYPE_CON
Confirmable message.
Definition: coap_common.h:89
@ COAP_OPT_URI_PATH
Definition: coap_option.h:99
error_t coapClientReadPayload(CoapMessage *message, void *data, size_t size, size_t *length)
Read payload data.
error_t coapSplitRepeatableOption(CoapMessage *message, uint16_t optionNum, const char_t *optionValue, char_t separator)
Encode a path or query component into multiple repeatable options.
Definition: coap_option.c:822
error_t coapClientGetLocationQuery(const CoapMessage *message, char_t *queryString, size_t maxLen)
Get Location-Query option.
@ COAP_REQ_STATE_CANCELED
@ COAP_REQ_STATE_UNUSED
error_t coapClientSetStringOption(CoapMessage *message, uint16_t optionNum, uint_t optionIndex, const char_t *optionValue)
Add a string option to the CoAP message.
void coapClientGenerateToken(CoapClientContext *context, CoapMessageHeader *header)
Generate a new token.
uint32_t systime_t
System time.
Helper functions for CoAP client.
void coapClientGenerateMessageId(CoapClientContext *context, CoapMessageHeader *header)
Generate a new message identifier.
@ ERROR_TIMEOUT
Definition: error.h:95
char char_t
Definition: compiler_port.h:48
error_t coapGetCode(const CoapMessage *message, CoapCode *code)
Get method or response code.
Definition: coap_message.c:226
CoAP block-wise transfer.
@ COAP_REQ_STATE_RECEIVE
@ COAP_CODE_GET
Definition: coap_common.h:115
CoapMessageType
CoAP message types.
Definition: coap_common.h:88
void coapClientDeleteRequest(CoapClientRequest *request)
Release the resources associated with a CoAP request.
uint8_t n
@ COAP_OPT_LOCATION_QUERY
Definition: coap_option.h:104
error_t coapClientGetType(const CoapMessage *message, CoapMessageType *type)
Get message type.
uint8_t payload[]
Definition: ipv6.h:286
void osAcquireMutex(OsMutex *mutex)
Acquire ownership of the specified mutex object.
error_t coapWritePayload(CoapMessage *message, const void *data, size_t length)
Write payload data.
Definition: coap_message.c:386
void osReleaseMutex(OsMutex *mutex)
Release ownership of the specified mutex object.
error_t coapClientWritePayload(CoapMessage *message, const void *data, size_t length)
Write payload data.
uint8_t value[]
Definition: tcp.h:369
error_t coapGetOption(const CoapMessage *message, uint16_t optionNum, uint_t optionIndex, const uint8_t **optionValue, size_t *optionLen)
Get the value of the specified option.
Definition: coap_option.c:571
@ COAP_REQ_STATE_SEPARATE
error_t coapClientGetUintOption(const CoapMessage *message, uint16_t optionNum, uint_t optionIndex, uint32_t *optionValue)
Read an uint option from the CoAP message.
error_t coapClientCancelRequest(CoapClientRequest *request)
Cancel an outstanding CoAP request.
error_t(* CoapRequestCallback)(CoapClientContext *context, CoapClientRequest *request, CoapRequestStatus status, void *param)
Request completed callback.
error_t coapClientSetUintOption(CoapMessage *message, uint16_t optionNum, uint_t optionIndex, uint32_t optionValue)
Add a uint option to the CoAP message.
@ COAP_BLOCK_SIZE_RESERVED
Definition: coap_option.h:208
uint16_t payloadLen
Definition: ipv6.h:281
unsigned int uint_t
Definition: compiler_port.h:50
TCP/IP stack core.
error_t coapClientDeleteOption(CoapMessage *message, uint16_t optionNum, uint_t optionIndex)
Remove an option from the CoAP message.
error_t coapReadPayload(CoapMessage *message, void *data, size_t size, size_t *length)
Read payload data.
Definition: coap_message.c:468
CoapCode
CoAP method and response codes.
Definition: coap_common.h:113
@ NO_ERROR
Success.
Definition: error.h:44
CoapMessageHeader
Definition: coap_common.h:182
error_t coapClientGetResponseCode(const CoapMessage *message, CoapCode *code)
Get response code.
Debugging facilities.
error_t coapSetPayload(CoapMessage *message, const void *payload, size_t payloadLen)
Set CoAP message payload.
Definition: coap_message.c:252
@ COAP_REQ_STATE_TIMEOUT
@ COAP_REQ_STATE_INIT