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