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-2020 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.8
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  TRACE_DEBUG("%c", option->value[i]);
366 
367  //Add a line feed
368  TRACE_DEBUG("\"\r\n");
369  }
370  else
371  {
372  //Dump option value
373  TRACE_DEBUG_ARRAY(" ", option->value, option->length);
374  }
375  }
376  else
377  {
378  //Dump option number
379  TRACE_DEBUG(" Option %" PRIu16 " (%" PRIu16 " bytes)\r\n",
380  option->number, option->length);
381 
382  //Dump option value
383  TRACE_DEBUG_ARRAY(" ", option->value, option->length);
384  }
385 #endif
386 
387  //Successful processing
388  return NO_ERROR;
389 }
390 
391 
392 /**
393  * @brief Convert a parameter to string representation
394  * @param[in] value Parameter value
395  * @param[in] paramList List of acceptable parameters
396  * @param[in] paramListLen Number of entries in the list
397  * @return NULL-terminated string describing the parameter
398  **/
399 
401  const CoapParamName *paramList, size_t paramListLen)
402 {
403  uint_t i;
404 
405  //Default name for unknown values
406  static const char_t defaultName[] = "Unknown";
407 
408  //Loop through the list of acceptable parameters
409  for(i = 0; i < paramListLen; i++)
410  {
411  if(paramList[i].value == value)
412  return paramList[i].name;
413  }
414 
415  //Unknown value
416  return defaultName;
417 }
418 
419 #endif
const CoapParamName coapCodeList[]
Definition: coap_debug.c:54
uint8_t length
Definition: coap_common.h:190
size_t length
Definition: coap_option.h:216
CoapOptionFormat format
Option format.
Definition: coap_option.h:233
uint8_t p
Definition: ndp.h:298
Non-negative integer.
Definition: coap_option.h:123
#define COAP_PAYLOAD_MARKER
Definition: coap_common.h:46
uint16_t number
Definition: coap_option.h:215
char_t name[]
#define osStrcmp(s1, s2)
Definition: os_port.h:158
error_t coapDumpOption(const CoapOption *option)
Dump CoAP option.
Definition: coap_debug.c:288
const char_t * name
Option name.
Definition: coap_option.h:232
const CoapParamName coapContentFormatList[]
Definition: coap_debug.c:104
#define COAP_GET_BLOCK_NUM(value)
Definition: coap_option.h:69
error_t
Error codes.
Definition: error.h:42
error_t coapDumpMessageHeader(const uint8_t *p, size_t length, size_t *consumed)
Dump CoAP message header.
Definition: coap_debug.c:182
const CoapOptionParameters * coapGetOptionParameters(uint16_t optionNum)
Retrieve parameters for a given option number.
Definition: coap_option.c:950
uint8_t value[]
Definition: tcp.h:332
#define COAP_GET_BLOCK_M(value)
Definition: coap_option.h:71
CoAP client.
const char_t * name
Definition: coap_debug.h:51
CoAP option parameters.
Definition: coap_option.h:225
__start_packed struct @3 CoapMessageHeader
CoAP message format.
Parameter value/name binding.
Definition: coap_debug.h:48
Acknowledgment message.
Definition: coap_common.h:89
const CoapParamName coapTypeList[]
Definition: coap_debug.c:45
Confirmable message.
Definition: coap_common.h:87
Reset message.
Definition: coap_common.h:90
error_t coapDumpOptions(const uint8_t *p, size_t length, size_t *consumed)
Dump the list of CoAP options.
Definition: coap_debug.c:234
const char_t * coapGetParamName(uint_t value, const CoapParamName *paramList, size_t paramListLen)
Convert a parameter to string representation.
Definition: coap_debug.c:400
CoAP server.
#define ntohs(value)
Definition: cpu_endian.h:421
#define TRACE_DEBUG(...)
Definition: debug.h:107
const CoapParamName coapObserveList[]
Definition: coap_debug.c:97
char char_t
Definition: compiler_port.h:43
#define TRACE_DEBUG_ARRAY(p, a, n)
Definition: debug.h:108
uint8_t n
Data logging functions for debugging purpose (CoAP)
const uint8_t * value
Definition: coap_option.h:217
uint8_t message[]
Definition: chap.h:152
CoAP option.
Definition: coap_option.h:212
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
#define PRIuSIZE
Definition: compiler_port.h:78
unsigned int uint_t
Definition: compiler_port.h:45
TCP/IP stack core.
Non-confirmable message.
Definition: coap_common.h:88
#define COAP_GET_BLOCK_SZX(value)
Definition: coap_option.h:73
Success.
Definition: error.h:44
Debugging facilities.
error_t coapDumpMessage(const void *message, size_t length)
Dump CoAP message for debugging purpose.
Definition: coap_debug.c:122
#define arraysize(a)
Definition: os_port.h:70