coap_server_request.c
Go to the documentation of this file.
1 /**
2  * @file coap_server_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.0
29  **/
30 
31 //Switch to the appropriate trace level
32 #define TRACE_LEVEL COAP_TRACE_LEVEL
33 
34 //Dependencies
35 #include "coap/coap_server.h"
37 #include "debug.h"
38 
39 //Check TCP/IP stack configuration
40 #if (COAP_SERVER_SUPPORT == ENABLED)
41 
42 
43 /**
44  * @brief Get request method
45  * @param[in] context Pointer to the CoAP server context
46  * @param[out] code Method code (GET, POST, PUT or DELETE)
47  * @return Error code
48  **/
49 
51 {
52  //Check parameters
53  if(context == NULL || code == NULL)
55 
56  //Get request method
57  return coapGetCode(&context->request, code);
58 }
59 
60 
61 /**
62  * @brief Get Uri-Path option
63  * @param[in] context Pointer to the CoAP server context
64  * @param[out] path Pointer to the buffer where to copy the path component
65  * @param[in] maxLen Maximum number of characters the buffer can hold
66  * @return Error code
67  **/
68 
70  size_t maxLen)
71 {
72  //Check parameters
73  if(context == NULL || path == NULL)
75 
76  //Reconstruct the path component from Uri-Path options
77  return coapJoinRepeatableOption(&context->request, COAP_OPT_URI_PATH,
78  path, maxLen, '/');
79 }
80 
81 
82 /**
83  * @brief Get Uri-Query option
84  * @param[in] context Pointer to the CoAP server context
85  * @param[out] queryString Pointer to the buffer where to copy the query string
86  * @param[in] maxLen Maximum number of characters the buffer can hold
87  * @return Error code
88  **/
89 
91  size_t maxLen)
92 {
93  //Check parameters
94  if(context == NULL || queryString == NULL)
96 
97  //Reconstruct the query string from Uri-Query options
98  return coapJoinRepeatableOption(&context->request, COAP_OPT_URI_QUERY,
99  queryString, maxLen, '&');
100 }
101 
102 
103 /**
104  * @brief Read an opaque option from the CoAP request
105  * @param[in] context Pointer to the CoAP server context
106  * @param[in] optionNum Option number to search for
107  * @param[in] optionIndex Occurrence index (for repeatable options only)
108  * @param[out] optionValue Pointer to the first byte of the option value
109  * @param[out] optionLen Length of the option, in bytes
110  * @return Error code
111  **/
112 
114  uint_t optionIndex, const uint8_t **optionValue, size_t *optionLen)
115 {
116  //Check parameters
117  if(context == NULL || optionValue == NULL || optionLen == NULL)
119 
120  //Search the CoAP message for the specified option number
121  return coapGetOption(&context->request, optionNum, optionIndex, optionValue,
122  optionLen);
123 }
124 
125 
126 /**
127  * @brief Read a string option from the CoAP request
128  * @param[in] context Pointer to the CoAP server context
129  * @param[in] optionNum Option number to search for
130  * @param[in] optionIndex Occurrence index (for repeatable options only)
131  * @param[out] optionValue Pointer to the first byte of the option value
132  * @param[out] optionLen Length of the option, in characters
133  * @return Error code
134  **/
135 
137  uint_t optionIndex, const char_t **optionValue, size_t *optionLen)
138 {
139  //Check parameters
140  if(context == NULL || optionValue == NULL || optionLen == NULL)
142 
143  //Search the CoAP message for the specified option number
144  return coapGetOption(&context->request, optionNum, optionIndex,
145  (const uint8_t **) optionValue, optionLen);
146 }
147 
148 
149 /**
150  * @brief Read an uint option from the CoAP request
151  * @param[in] context Pointer to the CoAP server context
152  * @param[in] optionNum Option number to search for
153  * @param[in] optionIndex Occurrence index (for repeatable options only)
154  * @param[out] optionValue Option value (unsigned integer)
155  * @return Error code
156  **/
157 
158 error_t coapServerGetUintOption(CoapServerContext *context, uint16_t optionNum,
159  uint_t optionIndex, uint32_t *optionValue)
160 {
161  //Check parameters
162  if(context == NULL || optionValue == NULL)
164 
165  //Search the CoAP message for the specified option number
166  return coapGetUintOption(&context->request, optionNum, optionIndex,
167  optionValue);
168 }
169 
170 
171 /**
172  * @brief Get request payload
173  * @param[in] context Pointer to the CoAP server context
174  * @param[out] payload Pointer to the first byte of the payload
175  * @param[out] payloadLen Length of the payload, in bytes
176  * @return Error code
177  **/
178 
180  size_t *payloadLen)
181 {
182  //Check parameters
183  if(context == NULL || payload == NULL || payloadLen == NULL)
185 
186  //Get response payload
187  return coapGetPayload(&context->request, payload, payloadLen);
188 }
189 
190 
191 /**
192  * @brief Read request payload data
193  * @param[in] context Pointer to the CoAP server context
194  * @param[out] data Buffer into which received data will be placed
195  * @param[in] size Maximum number of bytes that can be received
196  * @param[out] length Number of bytes that have been received
197  * @return Error code
198  **/
199 
201  size_t *length)
202 {
203  //Check parameters
204  if(context == NULL || data == NULL)
206 
207  //Read payload data
208  return coapReadPayload(&context->request, data, size, length);
209 }
210 
211 
212 /**
213  * @brief Set response method
214  * @param[in] context Pointer to the CoAP server context
215  * @param[in] code Response code
216  * @return Error code
217  **/
218 
220 {
221  //Make sure the CoAP message is valid
222  if(context == NULL)
224 
225  //Set response code
226  return coapSetCode(&context->response, code);
227 }
228 
229 
230 /**
231  * @brief Set Location-Path option
232  * @param[in] context Pointer to the CoAP server context
233  * @param[in] path NULL-terminated string that contains the path component
234  * @return Error code
235  **/
236 
238  const char_t *path)
239 {
240  //Check parameters
241  if(context == NULL || path == NULL)
243 
244  //Encode the path component into multiple Location-Path options
245  return coapSplitRepeatableOption(&context->response, COAP_OPT_LOCATION_PATH,
246  path, '/');
247 }
248 
249 
250 /**
251  * @brief Set Location-Query option
252  * @param[in] context Pointer to the CoAP server context
253  * @param[in] queryString NULL-terminated string that contains the query string
254  * @return Error code
255  **/
256 
258  const char_t *queryString)
259 {
260  //Check parameters
261  if(context == NULL || queryString == NULL)
263 
264  //Encode the query string into multiple Location-Query options
265  return coapSplitRepeatableOption(&context->response, COAP_OPT_LOCATION_QUERY,
266  queryString, '&');
267 }
268 
269 
270 /**
271  * @brief Add an opaque option to the CoAP response
272  * @param[in] context Pointer to the CoAP server context
273  * @param[in] optionNum Option number
274  * @param[in] optionIndex Occurrence index (for repeatable options only)
275  * @param[in] optionValue Pointer to the first byte of the option value
276  * @param[in] optionLen Length of the option, in bytes
277  * @return Error code
278  **/
279 
281  uint_t optionIndex, const uint8_t *optionValue, size_t optionLen)
282 {
283  //Make sure the CoAP message is valid
284  if(context == NULL)
286 
287  //Inconsistent option value?
288  if(optionValue == NULL && optionLen != 0)
290 
291  //Add the specified option to the CoAP message
292  return coapSetOption(&context->response, optionNum, optionIndex,
293  optionValue, optionLen);
294 }
295 
296 
297 /**
298  * @brief Add a string option to the CoAP response
299  * @param[in] context Pointer to the CoAP server context
300  * @param[in] optionNum Option number
301  * @param[in] optionIndex Occurrence index (for repeatable options only)
302  * @param[in] optionValue NULL-terminated string that contains the option value
303  * @return Error code
304  **/
305 
307  uint_t optionIndex, const char_t *optionValue)
308 {
309  size_t n;
310 
311  //Check parameters
312  if(context == NULL || optionValue == NULL)
314 
315  //Retrieve the length of the string
316  n = osStrlen(optionValue);
317 
318  //Add the specified option to the CoAP message
319  return coapSetOption(&context->response, optionNum, optionIndex,
320  (const uint8_t *) optionValue, n);
321 }
322 
323 
324 /**
325  * @brief Add a uint option to the CoAP response
326  * @param[in] context Pointer to the CoAP server context
327  * @param[in] optionNum Option number
328  * @param[in] optionIndex Occurrence index (for repeatable options only)
329  * @param[in] optionValue Option value (unsigned integer)
330  * @return Error code
331  **/
332 
333 error_t coapServerSetUintOption(CoapServerContext *context, uint16_t optionNum,
334  uint_t optionIndex, uint32_t optionValue)
335 {
336  //Make sure the CoAP message is valid
337  if(context == NULL)
339 
340  //Add the specified option to the CoAP message
341  return coapSetUintOption(&context->response, optionNum, optionIndex,
342  optionValue);
343 }
344 
345 
346 /**
347  * @brief Remove an option from the CoAP response
348  * @param[in] context Pointer to the CoAP server context
349  * @param[in] optionNum Option number
350  * @param[in] optionIndex Occurrence index (for repeatable options only)
351  * @return Error code
352  **/
353 
354 error_t coapServerDeleteOption(CoapServerContext *context, uint16_t optionNum,
355  uint_t optionIndex)
356 {
357  //Make sure the CoAP message is valid
358  if(context == NULL)
360 
361  //Remove the specified option from the CoAP message
362  return coapDeleteOption(&context->response, optionNum, optionIndex);
363 }
364 
365 
366 /**
367  * @brief Set response payload
368  * @param[in] context Pointer to the CoAP server context
369  * @param[out] payload Pointer to request payload
370  * @param[out] payloadLen Length of the payload, in bytes
371  * @return Error code
372  **/
373 
375  size_t payloadLen)
376 {
377  //Make sure the CoAP message is valid
378  if(context == NULL)
380 
381  //Check parameters
382  if(payload == NULL && payloadLen != 0)
384 
385  //Set message payload
386  return coapSetPayload(&context->response, payload, payloadLen);
387 }
388 
389 
390 /**
391  * @brief Write payload data
392  * @param[in] context Pointer to the CoAP server context
393  * @param[in] data Pointer to a buffer containing the data to be written
394  * @param[in] length Number of bytes to written
395  * @return Error code
396  **/
397 
399  size_t length)
400 {
401  //Check parameters
402  if(context == NULL || data == NULL)
404 
405  //Write payload data
406  return coapWritePayload(&context->response, data, length);
407 }
408 
409 #endif
uint8_t code
Definition: coap_common.h:179
CoapCode
CoAP method and response codes.
Definition: coap_common.h:113
error_t coapReadPayload(CoapMessage *message, void *data, size_t size, size_t *length)
Read payload data.
Definition: coap_message.c:468
error_t coapGetCode(const CoapMessage *message, CoapCode *code)
Get method or response code.
Definition: coap_message.c:226
error_t coapWritePayload(CoapMessage *message, const void *data, size_t length)
Write payload data.
Definition: coap_message.c:386
error_t coapSetCode(CoapMessage *message, CoapCode code)
Set method or response code.
Definition: coap_message.c:201
error_t coapSetPayload(CoapMessage *message, const void *payload, size_t payloadLen)
Set CoAP message payload.
Definition: coap_message.c:252
error_t coapGetPayload(const CoapMessage *message, const uint8_t **payload, size_t *payloadLen)
Get CoAP message payload.
Definition: coap_message.c:324
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
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 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
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 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 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_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
@ COAP_OPT_LOCATION_PATH
Definition: coap_option.h:98
@ COAP_OPT_LOCATION_QUERY
Definition: coap_option.h:104
@ COAP_OPT_URI_QUERY
Definition: coap_option.h:102
@ COAP_OPT_URI_PATH
Definition: coap_option.h:99
CoAP server.
#define CoapServerContext
Definition: coap_server.h:121
error_t coapServerReadPayload(CoapServerContext *context, void *data, size_t size, size_t *length)
Read request payload data.
error_t coapServerGetOpaqueOption(CoapServerContext *context, uint16_t optionNum, uint_t optionIndex, const uint8_t **optionValue, size_t *optionLen)
Read an opaque option from the CoAP request.
error_t coapServerGetUriQuery(CoapServerContext *context, char_t *queryString, size_t maxLen)
Get Uri-Query option.
error_t coapServerGetUintOption(CoapServerContext *context, uint16_t optionNum, uint_t optionIndex, uint32_t *optionValue)
Read an uint option from the CoAP request.
error_t coapServerSetLocationQuery(CoapServerContext *context, const char_t *queryString)
Set Location-Query option.
error_t coapServerSetPayload(CoapServerContext *context, const void *payload, size_t payloadLen)
Set response payload.
error_t coapServerSetResponseCode(CoapServerContext *context, CoapCode code)
Set response method.
error_t coapServerGetUriPath(CoapServerContext *context, char_t *path, size_t maxLen)
Get Uri-Path option.
error_t coapServerGetMethodCode(CoapServerContext *context, CoapCode *code)
Get request method.
error_t coapServerSetLocationPath(CoapServerContext *context, const char_t *path)
Set Location-Path option.
error_t coapServerGetStringOption(CoapServerContext *context, uint16_t optionNum, uint_t optionIndex, const char_t **optionValue, size_t *optionLen)
Read a string option from the CoAP request.
error_t coapServerSetUintOption(CoapServerContext *context, uint16_t optionNum, uint_t optionIndex, uint32_t optionValue)
Add a uint option to the CoAP response.
error_t coapServerGetPayload(CoapServerContext *context, const uint8_t **payload, size_t *payloadLen)
Get request payload.
error_t coapServerSetStringOption(CoapServerContext *context, uint16_t optionNum, uint_t optionIndex, const char_t *optionValue)
Add a string option to the CoAP response.
error_t coapServerDeleteOption(CoapServerContext *context, uint16_t optionNum, uint_t optionIndex)
Remove an option from the CoAP response.
error_t coapServerSetOpaqueOption(CoapServerContext *context, uint16_t optionNum, uint_t optionIndex, const uint8_t *optionValue, size_t optionLen)
Add an opaque option to the CoAP response.
error_t coapServerWritePayload(CoapServerContext *context, const void *data, size_t length)
Write payload data.
CoAP request handling.
unsigned int uint_t
Definition: compiler_port.h:50
char char_t
Definition: compiler_port.h:48
Debugging facilities.
uint8_t n
error_t
Error codes.
Definition: error.h:43
@ ERROR_INVALID_PARAMETER
Invalid parameter.
Definition: error.h:47
uint8_t data[]
Definition: ethernet.h:222
uint8_t payload[]
Definition: ipv6.h:277
uint16_t payloadLen
Definition: ipv6.h:272
#define osStrlen(s)
Definition: os_port.h:165
uint8_t length
Definition: tcp.h:368