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