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-2019 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 1.9.6
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  * @return Pointer to the CoAP request
348  **/
349 
351 {
352  //Valid CoAP request?
353  if(request != NULL)
354  {
355  //Acquire exclusive access to the CoAP client context
356  osAcquireMutex(&request->context->mutex);
357  //The request is no more in use
358  request->state = COAP_REQ_STATE_UNUSED;
359  //Release exclusive access to the CoAP client context
360  osReleaseMutex(&request->context->mutex);
361  }
362 }
363 
364 
365 /**
366  * @brief Get request message
367  * @param[in] request CoAP request handle
368  * @return Pointer to the request message
369  **/
370 
372 {
373  CoapClientContext *context;
375 
376  //Point to the CoAP client context
377  context = request->context;
378 
379  //Acquire exclusive access to the CoAP client context
380  osAcquireMutex(&context->mutex);
381 
382  //Check request state
383  if(request->state == COAP_REQ_STATE_INIT ||
384  request->state == COAP_REQ_STATE_OBSERVE ||
385  request->state == COAP_REQ_STATE_DONE ||
386  request->state == COAP_REQ_STATE_RESET ||
387  request->state == COAP_REQ_STATE_TIMEOUT ||
388  request->state == COAP_REQ_STATE_CANCELED)
389  {
390  //Re-initialize request state
392  //Point to the request message
393  message = &request->message;
394  }
395  else
396  {
397  //The request cannot be accessed while the message exchange is on-going
398  message = NULL;
399  }
400 
401  //Release exclusive access to the CoAP client context
402  osReleaseMutex(&context->mutex);
403 
404  //Return a pointer to the request message
405  return message;
406 }
407 
408 
409 /**
410  * @brief Get response message
411  * @param[in] request CoAP request handle
412  * @return Pointer to the response message
413  **/
414 
416 {
417  CoapClientContext *context;
419 
420  //Point to the CoAP client context
421  context = request->context;
422 
423  //Acquire exclusive access to the CoAP client context
424  osAcquireMutex(&context->mutex);
425 
426  //Check request state
427  if(request->state == COAP_REQ_STATE_INIT ||
428  request->state == COAP_REQ_STATE_OBSERVE ||
429  request->state == COAP_REQ_STATE_DONE)
430  {
431  //Point to the response message
432  message = &context->response;
433  }
434  else
435  {
436  //The response cannot be accessed while the message exchange is on-going
437  message = NULL;
438  }
439 
440  //Release exclusive access to the CoAP client context
441  osReleaseMutex(&context->mutex);
442 
443  //Return a pointer to the response message
444  return message;
445 }
446 
447 
448 /**
449  * @brief Set message type
450  * @param[in] message Pointer to the CoAP message
451  * @param[in] type Message type (Confirmable or Non-confirmable)
452  * @return Error code
453  **/
454 
456 {
457  //Make sure the CoAP message is valid
458  if(message == NULL)
460 
461  //Set message type
462  return coapSetType(message, type);
463 }
464 
465 
466 /**
467  * @brief Get message type
468  * @param[in] message Pointer to the CoAP message
469  * @param[out] type Message type
470  * @return Error code
471  **/
472 
474 {
475  //Check parameters
476  if(message == NULL || type == NULL)
478 
479  //Get message type
480  return coapGetType(message, type);
481 }
482 
483 
484 /**
485  * @brief Set request method
486  * @param[in] message Pointer to the CoAP message
487  * @param[in] code Method code (GET, POST, PUT or DELETE)
488  * @return Error code
489  **/
490 
492 {
493  //Make sure the CoAP message is valid
494  if(message == NULL)
496 
497  //Set request method
498  return coapSetCode(message, code);
499 }
500 
501 
502 /**
503  * @brief Get request method
504  * @param[in] message Pointer to the CoAP message
505  * @param[out] code Method code
506  * @return Error code
507  **/
508 
510 {
511  //Check parameters
512  if(message == NULL || code == NULL)
514 
515  //Get request method
516  return coapGetCode(message, code);
517 }
518 
519 
520 /**
521  * @brief Get response code
522  * @param[in] message Pointer to the CoAP message
523  * @param[out] code Response code
524  * @return Error code
525  **/
526 
528 {
529  //Check parameters
530  if(message == NULL || code == NULL)
532 
533  //Get response code
534  return coapGetCode(message, code);
535 }
536 
537 
538 /**
539  * @brief Set Uri-Path option
540  * @param[in] message Pointer to the CoAP message
541  * @param[in] path NULL-terminated string that contains the path component
542  * @return Error code
543  **/
544 
546 {
547  //Check parameters
548  if(message == NULL || path == NULL)
550 
551  //Encode the path component into multiple Uri-Path options
553 }
554 
555 
556 /**
557  * @brief Get Uri-Path option
558  * @param[in] message Pointer to the CoAP message
559  * @param[out] path Pointer to the buffer where to copy the path component
560  * @param[in] maxLen Maximum number of characters the buffer can hold
561  * @return Error code
562  **/
563 
565  size_t maxLen)
566 {
567  //Check parameters
568  if(message == NULL || path == NULL)
570 
571  //Reconstruct the path component from Uri-Path options
573  maxLen, '/');
574 }
575 
576 
577 /**
578  * @brief Set Uri-Query option
579  * @param[in] message Pointer to the CoAP message
580  * @param[in] queryString NULL-terminated string that contains the query string
581  * @return Error code
582  **/
583 
585 {
586  //Check parameters
587  if(message == NULL || queryString == NULL)
589 
590  //Encode the query string into multiple Uri-Query options
592  queryString, '&');
593 }
594 
595 
596 /**
597  * @brief Get Uri-Query option
598  * @param[in] message Pointer to the CoAP message
599  * @param[out] queryString Pointer to the buffer where to copy the query string
600  * @param[in] maxLen Maximum number of characters the buffer can hold
601  * @return Error code
602  **/
603 
605  size_t maxLen)
606 {
607  //Check parameters
608  if(message == NULL || queryString == NULL)
610 
611  //Reconstruct the query string from Uri-Query options
613  maxLen, '&');
614 }
615 
616 
617 /**
618  * @brief Get Location-Path option
619  * @param[in] message Pointer to the CoAP message
620  * @param[out] path Pointer to the buffer where to copy the path component
621  * @param[in] maxLen Maximum number of characters the buffer can hold
622  * @return Error code
623  **/
624 
626  size_t maxLen)
627 {
628  //Check parameters
629  if(message == NULL || path == NULL)
631 
632  //Reconstruct the path component from Location-Path options
634  maxLen, '/');
635 }
636 
637 
638 /**
639  * @brief Get Location-Query option
640  * @param[in] message Pointer to the CoAP message
641  * @param[out] queryString Pointer to the buffer where to copy the query string
642  * @param[in] maxLen Maximum number of characters the buffer can hold
643  * @return Error code
644  **/
645 
647  char_t *queryString, size_t maxLen)
648 {
649  //Check parameters
650  if(message == NULL || queryString == NULL)
652 
653  //Reconstruct the query string from Location-Query options
655  queryString, maxLen, '&');
656 }
657 
658 
659 /**
660  * @brief Add an opaque option to the CoAP message
661  * @param[in] message Pointer to the CoAP message
662  * @param[in] optionNum Option number
663  * @param[in] optionIndex Occurrence index (for repeatable options only)
664  * @param[in] optionValue Pointer to the first byte of the option value
665  * @param[in] optionLen Length of the option, in bytes
666  * @return Error code
667  **/
668 
670  uint_t optionIndex, const uint8_t *optionValue, size_t optionLen)
671 {
672  //Make sure the CoAP message is valid
673  if(message == NULL)
675 
676  //Inconsistent option value?
677  if(optionValue == NULL && optionLen != 0)
679 
680  //Add the specified option to the CoAP message
681  return coapSetOption(message, optionNum, optionIndex, optionValue,
682  optionLen);
683 }
684 
685 
686 /**
687  * @brief Add a string option to the CoAP message
688  * @param[in] message Pointer to the CoAP message
689  * @param[in] optionNum Option number
690  * @param[in] optionIndex Occurrence index (for repeatable options only)
691  * @param[in] optionValue NULL-terminated string that contains the option value
692  * @return Error code
693  **/
694 
696  uint_t optionIndex, const char_t *optionValue)
697 {
698  size_t n;
699 
700  //Check parameters
701  if(message == NULL || optionValue == NULL)
703 
704  //Retrieve the length of the string
705  n = strlen(optionValue);
706 
707  //Add the specified option to the CoAP message
708  return coapSetOption(message, optionNum, optionIndex,
709  (const uint8_t *) optionValue, n);
710 }
711 
712 
713 /**
714  * @brief Add a uint option to the CoAP message
715  * @param[in] message Pointer to the CoAP message
716  * @param[in] optionNum Option number
717  * @param[in] optionIndex Occurrence index (for repeatable options only)
718  * @param[in] optionValue Option value (unsigned integer)
719  * @return Error code
720  **/
721 
723  uint_t optionIndex, uint32_t optionValue)
724 {
725  //Make sure the CoAP message is valid
726  if(message == NULL)
728 
729  //Add the specified option to the CoAP message
730  return coapSetUintOption(message, optionNum, optionIndex, optionValue);
731 }
732 
733 
734 /**
735  * @brief Read an opaque option from the CoAP message
736  * @param[in] message Pointer to the CoAP message
737  * @param[in] optionNum Option number to search for
738  * @param[in] optionIndex Occurrence index (for repeatable options only)
739  * @param[out] optionValue Pointer to the first byte of the option value
740  * @param[out] optionLen Length of the option, in bytes
741  * @return Error code
742  **/
743 
745  uint_t optionIndex, const uint8_t **optionValue, size_t *optionLen)
746 {
747  //Check parameters
748  if(message == NULL || optionValue == NULL || optionLen == NULL)
750 
751  //Search the CoAP message for the specified option number
752  return coapGetOption(message, optionNum, optionIndex, optionValue,
753  optionLen);
754 }
755 
756 
757 /**
758  * @brief Read a string option from the CoAP message
759  * @param[in] message Pointer to the CoAP message
760  * @param[in] optionNum Option number to search for
761  * @param[in] optionIndex Occurrence index (for repeatable options only)
762  * @param[out] optionValue Pointer to the first byte of the option value
763  * @param[out] optionLen Length of the option, in characters
764  * @return Error code
765  **/
766 
768  uint_t optionIndex, const char_t **optionValue, size_t *optionLen)
769 {
770  //Check parameters
771  if(message == NULL || optionValue == NULL || optionLen == NULL)
773 
774  //Search the CoAP message for the specified option number
775  return coapGetOption(message, optionNum, optionIndex,
776  (const uint8_t **) optionValue, optionLen);
777 }
778 
779 
780 /**
781  * @brief Read an uint option from the CoAP message
782  * @param[in] message Pointer to the CoAP message
783  * @param[in] optionNum Option number to search for
784  * @param[in] optionIndex Occurrence index (for repeatable options only)
785  * @param[out] optionValue Option value (unsigned integer)
786  * @return Error code
787  **/
788 
790  uint_t optionIndex, uint32_t *optionValue)
791 {
792  //Check parameters
793  if(message == NULL || optionValue == NULL)
795 
796  //Search the CoAP message for the specified option number
797  return coapGetUintOption(message, optionNum, optionIndex, optionValue);
798 }
799 
800 
801 /**
802  * @brief Remove an option from the CoAP message
803  * @param[in] message Pointer to the CoAP message
804  * @param[in] optionNum Option number
805  * @param[in] optionIndex Occurrence index (for repeatable options only)
806  * @return Error code
807  **/
808 
810  uint_t optionIndex)
811 {
812  //Make sure the CoAP message is valid
813  if(message == NULL)
815 
816  //Remove the specified option from the CoAP message
817  return coapDeleteOption(message, optionNum, optionIndex);
818 }
819 
820 
821 /**
822  * @brief Set message payload
823  * @param[in] message Pointer to the CoAP message
824  * @param[out] payload Pointer to request payload
825  * @param[out] payloadLen Length of the payload, in bytes
826  * @return Error code
827  **/
828 
830  size_t payloadLen)
831 {
832  //Make sure the CoAP message is valid
833  if(message == NULL)
835 
836  //Check parameters
837  if(payload == NULL && payloadLen != 0)
839 
840  //Set message payload
841  return coapSetPayload(message, payload, payloadLen);
842 }
843 
844 
845 /**
846  * @brief Get message payload
847  * @param[in] message Pointer to the CoAP message
848  * @param[out] payload Pointer to the first byte of the payload
849  * @param[out] payloadLen Length of the payload, in bytes
850  * @return Error code
851  **/
852 
853 error_t coapClientGetPayload(const CoapMessage *message, const uint8_t **payload,
854  size_t *payloadLen)
855 {
856  //Check parameters
857  if(message == NULL || payload == NULL || payloadLen == NULL)
859 
860  //Get response payload
861  return coapGetPayload(message, payload, payloadLen);
862 }
863 
864 
865 /**
866  * @brief Write payload data
867  * @param[in] message Pointer to the CoAP message
868  * @param[in] data Pointer to a buffer containing the data to be written
869  * @param[in] length Number of bytes to written
870  * @return Error code
871  **/
872 
874  size_t length)
875 {
876  //Check parameters
877  if(message == NULL || data == NULL)
879 
880  //Write payload data
882 }
883 
884 
885 /**
886  * @brief Read payload data
887  * @param[in] message Pointer to the CoAP message
888  * @param[out] data Buffer into which received data will be placed
889  * @param[in] size Maximum number of bytes that can be received
890  * @param[out] length Number of bytes that have been received
891  * @return Error code
892  **/
893 
895  size_t *length)
896 {
897  //Check parameters
898  if(message == NULL || data == NULL)
900 
901  //Read payload data
902  return coapReadPayload(message, data, size, length);
903 }
904 
905 #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.
uint8_t length
Definition: dtls_misc.h:149
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:181
CoapMessage * coapClientGetRequestMessage(CoapClientRequest *request)
Get request message.
@ ERROR_WOULD_BLOCK
Definition: error.h:95
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
error_t coapDeleteOption(CoapMessage *message, uint16_t optionNum, uint_t optionIndex)
Remove an option from the specified CoAP message.
Definition: coap_option.c:688
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
CoapClientRequest * coapClientCreateRequest(CoapClientContext *context)
Initialize a new CoAP request.
@ COAP_REQ_STATE_RESET
@ COAP_REQ_STATE_DONE
#define CoapClientContext
Definition: coap_client.h:139
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:143
CoapMessage * coapClientGetResponseMessage(CoapClientRequest *request)
Get response message.
@ ERROR_WRONG_STATE
Definition: error.h:207
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:74
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
char_t type
error_t
Error codes.
Definition: error.h:42
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:55
__start_packed struct @106 CoapMessageHeader
CoAP message format.
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:135
@ COAP_TYPE_CON
Confirmable message.
Definition: coap_common.h:94
@ 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.
Helper functions for CoAP client.
void coapClientGenerateMessageId(CoapClientContext *context, CoapMessageHeader *header)
Generate a new message identifier.
@ ERROR_TIMEOUT
Definition: error.h:94
char char_t
Definition: compiler_port.h:43
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:120
CoapMessageType
CoAP message types.
Definition: coap_common.h:92
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.
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.
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
uint8_t message[]
Definition: chap.h:152
@ 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:167
uint8_t payloadLen
Definition: ipv6.h:343
uint8_t value[]
Definition: dtls_misc.h:150
unsigned int uint_t
Definition: compiler_port.h:45
TCP/IP stack core.
uint8_t data[]
Definition: dtls_misc.h:176
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:117
uint32_t systime_t
Definition: compiler_port.h:46
@ NO_ERROR
Success.
Definition: error.h:44
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