coap_debug.c
Go to the documentation of this file.
1 /**
2  * @file coap_debug.c
3  * @brief Data logging functions for debugging purpose (CoAP)
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 "core/net.h"
36 #include "coap/coap_client.h"
37 #include "coap/coap_server.h"
38 #include "coap/coap_debug.h"
39 #include "debug.h"
40 
41 //Check TCP/IP stack configuration
42 #if (COAP_CLIENT_SUPPORT == ENABLED || COAP_SERVER_SUPPORT == ENABLED)
43 
44 //CoAP message types
46 {
47  {COAP_TYPE_CON, "CON"},
48  {COAP_TYPE_NON, "NON"},
49  {COAP_TYPE_ACK, "ACK"},
50  {COAP_TYPE_RST, "RST"}
51 };
52 
53 //CoAP method and response codes
55 {
56  {COAP_CODE_EMPTY, "Empty"},
57  {COAP_CODE_GET, "GET"},
58  {COAP_CODE_POST, "POST"},
59  {COAP_CODE_PUT, "PUT"},
60  {COAP_CODE_DELETE, "DELETE"},
61  {COAP_CODE_FETCH, "FETCH"},
62  {COAP_CODE_PATCH, "PATCH"},
63  {COAP_CODE_IPATCH, "iPATCH"},
64  {COAP_CODE_CREATED, "Created"},
65  {COAP_CODE_DELETED, "Deleted"},
66  {COAP_CODE_VALID, "Valid"},
67  {COAP_CODE_CHANGED, "Changed"},
68  {COAP_CODE_CONTENT, "Content"},
69  {COAP_CODE_CONTINUE, "Continue"},
70  {COAP_CODE_BAD_REQUEST, "Bad Request"},
71  {COAP_CODE_UNAUTHOZED, "Unauthorized"},
72  {COAP_CODE_BAD_OPTION, "Bad Option"},
73  {COAP_CODE_FORBIDDEN, "Forbidden"},
74  {COAP_CODE_NOT_FOUND, "Not Found"},
75  {COAP_CODE_METHOD_NOT_ALLOWED, "Method Not Allowed"},
76  {COAP_CODE_NOT_ACCEPTABLE, "Not Acceptable"},
77  {COAP_CODE_REQUEST_ENTITY_INCOMPLETE, "Request Entity Incomplete"},
78  {COAP_CODE_CONFLICT, "Conflict"},
79  {COAP_CODE_PRECONDITION_FAILED, "Precondition Failed"},
80  {COAP_CODE_REQUEST_ENTITY_TO_LARGE, "Request Entity Too Large"},
81  {COAP_CODE_UNSUPPORTED_CONTENT_FORMAT, "Unsupported Content-Format"},
82  {COAP_CODE_UNPROCESSABLE_ENTITY, "Unprocessable Entity"},
83  {COAP_CODE_INTERNAL_SERVER, "Internal Server Error"},
84  {COAP_CODE_NOT_IMPLEMENTED, "Not Implemented"},
85  {COAP_CODE_BAD_GATEWAY, "Bad Gateway"},
86  {COAP_CODE_SERVICE_UNAVAILABLE, "Service Unavailable"},
87  {COAP_CODE_GATEWAY_TIMEOUT, "Gateway Timeout"},
88  {COAP_CODE_PROXYING_NOT_SUPPORTED, "Proxying Not Supported"},
89  {COAP_CODE_CSM, "CSM"},
90  {COAP_CODE_PING, "Ping"},
91  {COAP_CODE_PONG, "Pong"},
92  {COAP_CODE_RELEASE, "Release"},
93  {COAP_CODE_ABORT, "Abort"}
94 };
95 
96 //Observe option values
98 {
99  {COAP_OBSERVE_REGISTER, "Register"},
100  {COAP_OBSERVE_DEREGISTER, "Deregister"}
101 };
102 
103 //Content-Format option values
105 {
106  {COAP_CONTENT_FORMAT_TEXT_PLAIN, "text/plain"},
107  {COAP_CONTENT_FORMAT_APP_LINK_FORMAT, "application/link-format"},
108  {COAP_CONTENT_FORMAT_APP_XML, "application/xml"},
109  {COAP_CONTENT_FORMAT_APP_OCTET_STREAM, "application/octet-stream"},
110  {COAP_CONTENT_FORMAT_APP_EXI, "application/exi"},
111  {COAP_CONTENT_FORMAT_APP_JSON, "application/json"}
112 };
113 
114 
115 /**
116  * @brief Dump CoAP message for debugging purpose
117  * @param[in] message Pointer to the CoAP message
118  * @param[in] length Length of the message, in bytes
119  * @return Error code
120  **/
121 
122 error_t coapDumpMessage(const void *message, size_t length)
123 {
124 #if (COAP_TRACE_LEVEL >= TRACE_LEVEL_DEBUG)
125  error_t error;
126  size_t n;
127  const uint8_t *p;
128 
129  //Point to the first byte of the CoAP message
130  p = message;
131 
132  //Dump message header
133  error = coapDumpMessageHeader(p, length, &n);
134  //Any error to report?
135  if(error)
136  return error;
137 
138  //Point to the first option of the message
139  p += n;
140  //Number of bytes left to process
141  length -= n;
142 
143  //Parse the list of options
144  error = coapDumpOptions(p, length, &n);
145  //Any error to report?
146  if(error)
147  return error;
148 
149  //Point to the payload
150  p += n;
151  //Number of bytes left to process
152  length -= n;
153 
154  //The payload is optional
155  if(length > 0)
156  {
157  //The payload is prefixed by a fixed, one-byte payload marker
158  p++;
159  //Retrieve the length of the payload
160  length--;
161 
162  //Dump payload
163  TRACE_DEBUG(" Payload (%" PRIuSIZE " bytes)\r\n", length);
164  TRACE_DEBUG_ARRAY(" ", p, length);
165  //TRACE_DEBUG(" %s\r\n", p);
166  }
167 #endif
168 
169  //Successful processing
170  return NO_ERROR;
171 }
172 
173 
174 /**
175  * @brief Dump CoAP message header
176  * @param[in] p Input stream where to read the CoAP message header
177  * @param[in] length Number of bytes available in the input stream
178  * @param[out] consumed Total number of bytes that have been consumed
179  * @return Error code
180  **/
181 
182 error_t coapDumpMessageHeader(const uint8_t *p, size_t length, size_t *consumed)
183 {
184  CoapMessageHeader *header;
185 
186  //Malformed CoAP message?
187  if(length < sizeof(CoapMessageHeader))
188  return ERROR_INVALID_MESSAGE;
189 
190  //Point to the CoAP message header
191  header = (CoapMessageHeader *) p;
192 
193  //Dump CoAP version number
194  TRACE_DEBUG(" Version = %" PRIu8 "\r\n", header->version);
195 
196  //Dump message type
197  TRACE_DEBUG(" Type = %" PRIu8 " (%s)\r\n", header->type,
199 
200  //Dump token length
201  TRACE_DEBUG(" Token Length = %" PRIu8 "\r\n", header->tokenLen);
202 
203  //Dump method or response code
204  TRACE_DEBUG(" Code = %" PRIu8 ".%02" PRIu8 " (%s)\r\n",
205  header->code / 32, header->code & 31,
207 
208  //Dump message identifier
209  TRACE_DEBUG(" Message ID = 0x%04" PRIX16 "\r\n", ntohs(header->mid));
210 
211  //Malformed CoAP message?
212  if(length < (sizeof(CoapMessageHeader) + header->tokenLen))
213  return ERROR_INVALID_LENGTH;
214 
215  //Dump token
216  TRACE_DEBUG_ARRAY(" Token = ", header->token, header->tokenLen);
217 
218  //Total number of bytes that have been consumed
219  *consumed = sizeof(CoapMessageHeader) + header->tokenLen;
220 
221  //Successful processing
222  return NO_ERROR;
223 }
224 
225 
226 /**
227  * @brief Dump the list of CoAP options
228  * @param[in] p Input stream where to read the CoAP options
229  * @param[in] length Number of bytes available in the input stream
230  * @param[out] consumed Total number of bytes that have been consumed
231  * @return Error code
232  **/
233 
234 error_t coapDumpOptions(const uint8_t *p, size_t length, size_t *consumed)
235 {
236  error_t error;
237  size_t n;
238  CoapOption option;
239 
240  //Initialize status code
241  error = NO_ERROR;
242 
243  //Total number of bytes that have been consumed
244  *consumed = 0;
245 
246  //For the first option in a message, a preceding option instance with
247  //Option Number zero is assumed
248  option.number = 0;
249 
250  //Loop through CoAP options
251  while(length > 0)
252  {
253  //Payload marker found?
254  if(*p == COAP_PAYLOAD_MARKER)
255  break;
256 
257  //Parse current option
258  error = coapParseOption(p, length, option.number, &option, &n);
259  //Any error to report?
260  if(error)
261  break;
262 
263  //Dump current option
264  error = coapDumpOption(&option);
265  //Any error to report?
266  if(error)
267  break;
268 
269  //Total number of bytes that have been consumed
270  *consumed += n;
271 
272  //Jump to the next option
273  p += n;
274  length -= n;
275  }
276 
277  //Return status code
278  return error;
279 }
280 
281 
282 /**
283  * @brief Dump CoAP option
284  * @param[out] option CoAP option content
285  * @return Error code
286  **/
287 
289 {
290 #if (COAP_TRACE_LEVEL >= TRACE_LEVEL_DEBUG)
291  size_t i;
292  uint32_t value;
293  const char_t *name;
294  const CoapOptionParameters *optionParams;
295 
296  //Retrieve option parameters
297  optionParams = coapGetOptionParameters(option->number);
298 
299  //Check whether the specified option is supported
300  if(optionParams != NULL)
301  {
302  //Dump option number
303  TRACE_DEBUG(" Option %" PRIu16 " (%s)\r\n",
304  option->number, optionParams->name);
305 
306  //Check option type
307  if(optionParams->format == COAP_OPT_FORMAT_UINT)
308  {
309  //Initialize integer value
310  value = 0;
311 
312  //Convert the integer from network byte order
313  for(i = 0; i < option->length; i++)
314  {
315  value = (value << 8) + option->value[i];
316  }
317 
318  //Check option number
319  if(option->number == COAP_OPT_OBSERVE)
320  {
321  //Convert the Observe option to string representation
324 
325  //Dump the value of the option
326  if(osStrcmp(name, "Unknown"))
327  {
328  TRACE_DEBUG(" %" PRIu32 " (%s)\r\n", value, name);
329  }
330  else
331  {
332  TRACE_DEBUG(" %" PRIu32 "\r\n", value);
333  }
334  }
335  else if(option->number == COAP_OPT_CONTENT_FORMAT)
336  {
337  //Convert the Content-Format option to string representation
340 
341  //Dump the value of the option
342  TRACE_DEBUG(" %" PRIu32 " (%s)\r\n", value, name);
343  }
344  else if(option->number == COAP_OPT_BLOCK1 ||
345  option->number == COAP_OPT_BLOCK2)
346  {
347  //Dump the value of the Block option
348  TRACE_DEBUG(" %" PRIu32 "/%" PRIu32 "/%" PRIu32 "\r\n",
350  16 << COAP_GET_BLOCK_SZX(value));
351  }
352  else
353  {
354  //Dump the value of the option
355  TRACE_DEBUG(" %" PRIu32 "\r\n", value);
356  }
357  }
358  else if(optionParams->format == COAP_OPT_FORMAT_STRING)
359  {
360  //Append prefix
361  TRACE_DEBUG(" \"");
362 
363  //Dump string value
364  for(i = 0; i < option->length; i++)
365  {
366  TRACE_DEBUG("%c", option->value[i]);
367  }
368 
369  //Add a line feed
370  TRACE_DEBUG("\"\r\n");
371  }
372  else
373  {
374  //Dump option value
375  TRACE_DEBUG_ARRAY(" ", option->value, option->length);
376  }
377  }
378  else
379  {
380  //Dump option number
381  TRACE_DEBUG(" Option %" PRIu16 " (%" PRIu16 " bytes)\r\n",
382  option->number, option->length);
383 
384  //Dump option value
385  TRACE_DEBUG_ARRAY(" ", option->value, option->length);
386  }
387 #endif
388 
389  //Successful processing
390  return NO_ERROR;
391 }
392 
393 
394 /**
395  * @brief Convert a parameter to string representation
396  * @param[in] value Parameter value
397  * @param[in] paramList List of acceptable parameters
398  * @param[in] paramListLen Number of entries in the list
399  * @return NULL-terminated string describing the parameter
400  **/
401 
403  size_t paramListLen)
404 {
405  uint_t i;
406 
407  //Default name for unknown values
408  static const char_t defaultName[] = "Unknown";
409 
410  //Loop through the list of acceptable parameters
411  for(i = 0; i < paramListLen; i++)
412  {
413  if(paramList[i].value == value)
414  return paramList[i].name;
415  }
416 
417  //Unknown value
418  return defaultName;
419 }
420 
421 #endif
uint8_t message[]
Definition: chap.h:154
CoAP client.
CoapMessageHeader
Definition: coap_common.h:182
#define COAP_PAYLOAD_MARKER
Definition: coap_common.h:48
@ COAP_TYPE_RST
Reset message.
Definition: coap_common.h:92
@ COAP_TYPE_NON
Non-confirmable message.
Definition: coap_common.h:90
@ COAP_TYPE_CON
Confirmable message.
Definition: coap_common.h:89
@ COAP_TYPE_ACK
Acknowledgment message.
Definition: coap_common.h:91
@ COAP_CODE_METHOD_NOT_ALLOWED
Definition: coap_common.h:133
@ COAP_CODE_DELETE
Definition: coap_common.h:118
@ COAP_CODE_PATCH
Definition: coap_common.h:120
@ COAP_CODE_UNAUTHOZED
Definition: coap_common.h:129
@ COAP_CODE_FORBIDDEN
Definition: coap_common.h:131
@ COAP_CODE_CSM
Definition: coap_common.h:148
@ COAP_CODE_INTERNAL_SERVER
Definition: coap_common.h:141
@ COAP_CODE_BAD_OPTION
Definition: coap_common.h:130
@ COAP_CODE_CONTENT
Definition: coap_common.h:126
@ COAP_CODE_RELEASE
Definition: coap_common.h:151
@ COAP_CODE_DELETED
Definition: coap_common.h:123
@ COAP_CODE_ABORT
Definition: coap_common.h:152
@ COAP_CODE_CONFLICT
Definition: coap_common.h:136
@ COAP_CODE_GATEWAY_TIMEOUT
Definition: coap_common.h:145
@ COAP_CODE_BAD_REQUEST
Definition: coap_common.h:128
@ COAP_CODE_PUT
Definition: coap_common.h:117
@ COAP_CODE_PING
Definition: coap_common.h:149
@ COAP_CODE_BAD_GATEWAY
Definition: coap_common.h:143
@ COAP_CODE_NOT_FOUND
Definition: coap_common.h:132
@ COAP_CODE_CREATED
Definition: coap_common.h:122
@ COAP_CODE_UNPROCESSABLE_ENTITY
Definition: coap_common.h:140
@ COAP_CODE_UNSUPPORTED_CONTENT_FORMAT
Definition: coap_common.h:139
@ COAP_CODE_PRECONDITION_FAILED
Definition: coap_common.h:137
@ COAP_CODE_VALID
Definition: coap_common.h:124
@ COAP_CODE_PONG
Definition: coap_common.h:150
@ COAP_CODE_POST
Definition: coap_common.h:116
@ COAP_CODE_NOT_ACCEPTABLE
Definition: coap_common.h:134
@ COAP_CODE_PROXYING_NOT_SUPPORTED
Definition: coap_common.h:146
@ COAP_CODE_GET
Definition: coap_common.h:115
@ COAP_CODE_CHANGED
Definition: coap_common.h:125
@ COAP_CODE_FETCH
Definition: coap_common.h:119
@ COAP_CODE_SERVICE_UNAVAILABLE
Definition: coap_common.h:144
@ COAP_CODE_REQUEST_ENTITY_INCOMPLETE
Definition: coap_common.h:135
@ COAP_CODE_CONTINUE
Definition: coap_common.h:127
@ COAP_CODE_NOT_IMPLEMENTED
Definition: coap_common.h:142
@ COAP_CODE_EMPTY
Definition: coap_common.h:114
@ COAP_CODE_REQUEST_ENTITY_TO_LARGE
Definition: coap_common.h:138
@ COAP_CODE_IPATCH
Definition: coap_common.h:121
const CoapParamName coapTypeList[]
Definition: coap_debug.c:45
const CoapParamName coapObserveList[]
Definition: coap_debug.c:97
error_t coapDumpOptions(const uint8_t *p, size_t length, size_t *consumed)
Dump the list of CoAP options.
Definition: coap_debug.c:234
error_t coapDumpMessage(const void *message, size_t length)
Dump CoAP message for debugging purpose.
Definition: coap_debug.c:122
const CoapParamName coapContentFormatList[]
Definition: coap_debug.c:104
error_t coapDumpOption(const CoapOption *option)
Dump CoAP option.
Definition: coap_debug.c:288
const CoapParamName coapCodeList[]
Definition: coap_debug.c:54
const char_t * coapGetParamName(uint_t value, const CoapParamName *paramList, size_t paramListLen)
Convert a parameter to string representation.
Definition: coap_debug.c:402
error_t coapDumpMessageHeader(const uint8_t *p, size_t length, size_t *consumed)
Dump CoAP message header.
Definition: coap_debug.c:182
Data logging functions for debugging purpose (CoAP)
const CoapOptionParameters * coapGetOptionParameters(uint16_t optionNum)
Retrieve parameters for a given option number.
Definition: coap_option.c:950
error_t coapParseOption(const uint8_t *p, size_t length, uint16_t prevOptionNum, CoapOption *option, size_t *consumed)
Parse CoAP option.
Definition: coap_option.c:133
@ COAP_OPT_FORMAT_UINT
Non-negative integer.
Definition: coap_option.h:125
@ COAP_OPT_FORMAT_STRING
UTF-8 string.
Definition: coap_option.h:126
@ COAP_OBSERVE_REGISTER
Definition: coap_option.h:136
@ COAP_OBSERVE_DEREGISTER
Definition: coap_option.h:137
#define COAP_GET_BLOCK_NUM(value)
Definition: coap_option.h:69
#define COAP_GET_BLOCK_SZX(value)
Definition: coap_option.h:73
@ COAP_OPT_BLOCK1
Definition: coap_option.h:106
@ COAP_OPT_BLOCK2
Definition: coap_option.h:105
@ COAP_OPT_OBSERVE
Definition: coap_option.h:96
@ COAP_OPT_CONTENT_FORMAT
Definition: coap_option.h:100
#define COAP_GET_BLOCK_M(value)
Definition: coap_option.h:71
@ COAP_CONTENT_FORMAT_APP_LINK_FORMAT
Definition: coap_option.h:151
@ COAP_CONTENT_FORMAT_APP_EXI
Definition: coap_option.h:154
@ COAP_CONTENT_FORMAT_APP_OCTET_STREAM
Definition: coap_option.h:153
@ COAP_CONTENT_FORMAT_APP_JSON
Definition: coap_option.h:155
@ COAP_CONTENT_FORMAT_APP_XML
Definition: coap_option.h:152
@ COAP_CONTENT_FORMAT_TEXT_PLAIN
Definition: coap_option.h:147
CoAP server.
unsigned int uint_t
Definition: compiler_port.h:50
#define PRIuSIZE
char char_t
Definition: compiler_port.h:48
#define ntohs(value)
Definition: cpu_endian.h:421
Debugging facilities.
#define TRACE_DEBUG_ARRAY(p, a, n)
Definition: debug.h:108
#define TRACE_DEBUG(...)
Definition: debug.h:107
uint8_t n
error_t
Error codes.
Definition: error.h:43
@ ERROR_INVALID_MESSAGE
Definition: error.h:105
@ NO_ERROR
Success.
Definition: error.h:44
@ ERROR_INVALID_LENGTH
Definition: error.h:111
uint8_t p
Definition: ndp.h:300
TCP/IP stack core.
#define osStrcmp(s1, s2)
Definition: os_port.h:171
#define arraysize(a)
Definition: os_port.h:71
char_t name[]
CoAP option.
Definition: coap_option.h:217
const uint8_t * value
Definition: coap_option.h:221
uint16_t number
Definition: coap_option.h:219
size_t length
Definition: coap_option.h:220
CoAP option parameters.
Definition: coap_option.h:230
CoapOptionFormat format
Option format.
Definition: coap_option.h:237
const char_t * name
Option name.
Definition: coap_option.h:236
Parameter value/name binding.
Definition: coap_debug.h:49
const char_t * name
Definition: coap_debug.h:51
uint8_t length
Definition: tcp.h:368
uint8_t value[]
Definition: tcp.h:369