9 #include "coap_config.h" 11 #if defined(HAVE_STRNLEN) && defined(__GNUC__) && !defined(_GNU_SOURCE) 15 #if defined(HAVE_ASSERT_H) && !defined(assert) 24 #ifdef HAVE_ARPA_INET_H 25 #include <arpa/inet.h> 38 # define fprintf(fd, ...) LWIP_PLATFORM_DIAG((__VA_ARGS__)) 44 # define DEBUG DEBUG_PRINT 46 #include "net/ip/uip-debug.h" 56 return PACKAGE_STRING;
71 "EMRG",
"ALRT",
"CRIT",
"ERR",
"WARN",
"NOTE",
"INFO",
"DEBG" 80 tmp = localtime(&now);
81 return strftime(s, len,
"%b %d %H:%M:%S", tmp);
89 return snprintf(s, len,
"%u.%03u",
114 while(*s++ && n < maxlen)
122 unsigned char *result,
unsigned int buflen,
int encode_always ) {
123 const unsigned char hex[] =
"0123456789ABCDEF";
124 unsigned int cnt = 0;
132 if (!encode_always && isprint(*data)) {
133 if (cnt+1 < buflen) {
140 if (cnt+4 < buflen) {
143 *result++ = hex[(*data & 0xf0) >> 4];
144 *result++ = hex[*data & 0x0f];
158 #define min(a,b) ((a) < (b) ? (a) : (b)) 163 #ifdef HAVE_ARPA_INET_H 164 const void *addrptr = NULL;
166 unsigned char *p = buf;
168 switch (addr->
addr.
sa.sa_family) {
170 addrptr = &addr->
addr.
sin.sin_addr;
171 port = ntohs(addr->
addr.
sin.sin_port);
179 addrptr = &addr->
addr.
sin6.sin6_addr;
180 port = ntohs(addr->
addr.
sin6.sin6_port);
184 memcpy(buf,
"(unknown address type)",
min(22, len));
188 if (inet_ntop(addr->
addr.
sa.sa_family, addrptr, (
char *)p, len) == 0) {
189 perror(
"coap_print_addr");
195 if (addr->
addr.
sa.sa_family == AF_INET6) {
202 p += snprintf((
char *)p, buf + len - p + 1,
":%d", port);
204 return buf + len - p;
207 unsigned char *p = buf;
209 # if NETSTACK_CONF_WITH_IPV6 210 const unsigned char hex[] =
"0123456789ABCDEF";
217 for (i=0; i < 16; i += 2) {
221 *p++ = hex[(addr->
addr.u8[i] & 0xf0) >> 4];
222 *p++ = hex[(addr->
addr.u8[i] & 0x0f)];
223 *p++ = hex[(addr->
addr.u8[i+1] & 0xf0) >> 4];
224 *p++ = hex[(addr->
addr.u8[i+1] & 0x0f)];
228 # warning "IPv4 network addresses will not be included in debug output" 233 if (buf + len - p < 6)
237 p += snprintf((
char *)p, buf + len - p + 1,
":%d", uip_htons(addr->port));
245 # warning "inet_ntop() not available, network addresses will not be included in debug output" 252 # define fprintf(fd, ...) PRINTF(__VA_ARGS__) 256 # define vfprintf(fd, ...) vprintf(__VA_ARGS__) 258 # define vfprintf(fd, ...) PRINTF(__VA_ARGS__) 265 static char *types[] = {
"CON",
"NON",
"ACK",
"RST",
"???" };
267 return types[
min(t,
sizeof(types)/
sizeof(
char *) - 1)];
273 static char *methods[] = {
"0.00",
"GET",
"POST",
"PUT",
"DELETE",
"PATCH" };
276 if (c <
sizeof(methods)/
sizeof(
char *)) {
279 snprintf(buf,
sizeof(buf),
"%u.%02u", c >> 5, c & 0x1f);
287 struct option_desc_t {
292 static struct option_desc_t options[] = {
318 for (i = 0; i <
sizeof(options)/
sizeof(
struct option_desc_t); i++) {
319 if (option_type == options[i].type) {
320 return options[i].name;
325 snprintf(buf,
sizeof(buf),
"%u", option_type);
331 unsigned char *result,
unsigned int buflen) {
337 static struct desc_t formats[] = {
350 for (i = 0; i <
sizeof(formats)/
sizeof(
struct desc_t); i++) {
351 if (format_type == formats[i].type) {
352 return snprintf((
char *)result, buflen,
"%s", formats[i].name);
357 return snprintf((
char *)result, buflen,
"%d", format_type);
367 return !(content_format == -1 ||
378 int encode = 0, have_options = 0, i;
381 int content_format = -1;
405 switch (opt_iter.
type) {
417 buf_len = snprintf((
char *)buf,
sizeof(buf),
"%u/%c/%u",
429 buf_len = snprintf((
char *)buf,
sizeof(buf),
"%u",
449 buf,
sizeof(buf), encode);
496 fprintf(log_fd,
"%s ", timebuf);
499 fprintf(log_fd,
"%s ",
loglevels[level]);
501 va_start(ap, format);
502 vfprintf(log_fd, format, ap);
#define COAP_OPTION_IF_MATCH
unsigned int coap_opt_block_num(const coap_opt_t *block_opt)
Returns the value of field num in the given block option block_opt.
#define COAP_OPTION_PROXY_URI
#define COAP_OPTION_CONTENT_FORMAT
static char * loglevels[]
void coap_show_pdu(const coap_pdu_t *pdu)
#define COAP_OPTION_NORESPONSE
multi-purpose address abstraction
static const char * msg_type_string(uint8_t t)
Returns a textual description of the message type t.
#define COAP_OPT_ALL
Pre-defined filter that includes all options.
#define COAP_OPTION_OBSERVE
#define COAP_MEDIATYPE_APPLICATION_JSON
coap_time_t coap_ticks_to_rt(coap_tick_t t)
Helper function that converts coap ticks to wallclock time.
#define COAP_OPTION_BLOCK1
int coap_get_data(coap_pdu_t *pdu, size_t *len, unsigned char **data)
Retrieves the length and data pointer of specified PDU.
#define COAP_OPTION_MAXAGE
coap_opt_t * coap_option_next(coap_opt_iterator_t *oi)
Updates the iterator oi to point to the next option.
static const char * msg_option_string(uint16_t option_type)
Returns a textual description of the option name.
coap_hdr_t * hdr
Address of the first byte of the CoAP message.
unsigned long coap_tick_t
This data type represents internal timer ticks with COAP_TICKS_PER_SECOND resolution.
#define COAP_OPTION_LOCATION_QUERY
#define COAP_OPTION_PROXY_SCHEME
#define COAP_MEDIATYPE_APPLICATION_EXI
size_t coap_print_addr(const struct coap_address_t *addr, unsigned char *buf, size_t len)
#define COAP_TICKS_PER_SECOND
Use ms resolution on POSIX systems.
Header structure for CoAP PDUs.
coap_opt_iterator_t * coap_option_iterator_init(coap_pdu_t *pdu, coap_opt_iterator_t *oi, const coap_opt_filter_t filter)
Initializes the given option iterator oi to point to the beginning of the pdu's option list...
coap_log_t
Pre-defined log levels akin to what is used in syslog.
const char * coap_package_version(void)
Returns a zero-terminated string with the library version.
#define COAP_OPTION_URI_PORT
void coap_ticks(coap_tick_t *t)
Sets t to the internal time with COAP_TICKS_PER_SECOND resolution.
#define COAP_OPTION_IF_NONE_MATCH
Iterator to run through PDU options.
#define COAP_MEDIATYPE_APPLICATION_LINK_FORMAT
static const char * msg_code_string(uint8_t c)
Returns a textual description of the method or response code.
#define COAP_MAX_PDU_SIZE
#define COAP_OPTION_SIZE1
static size_t strnlen(const char *s, size_t maxlen)
A length-safe strlen() fake.
#define COAP_MEDIATYPE_APPLICATION_XML
unsigned short type
decoded option type
void coap_log_impl(coap_log_t level, const char *format,...)
Writes the given text to COAP_ERR_FD (for level <= LOG_CRIT) or COAP_DEBUG_FD (for level >= LOG_WARNI...
#define COAP_OPTION_BLOCK2
#define COAP_OPTION_LOCATION_PATH
const char * coap_package_name(void)
Returns a zero-terminated string with the name of this library.
#define COAP_OPT_LENGTH(opt)
unsigned int token_length
#define COAP_OPT_BLOCK_SZX(opt)
Returns the value of the SZX-field of a Block option opt.
static unsigned int print_readable(const unsigned char *data, unsigned int len, unsigned char *result, unsigned int buflen, int encode_always)
#define COAP_OPTION_URI_PATH
#define COAP_MEDIATYPE_TEXT_PLAIN
static size_t print_timestamp(char *s, size_t len, coap_tick_t t)
void coap_set_log_level(coap_log_t level)
Sets the log level to the specified value.
#define COAP_OPTION_URI_QUERY
unsigned char coap_opt_t
Use byte-oriented access methods here because sliding a complex struct coap_opt_t over the data buffe...
union coap_address_t::@0 addr
#define COAP_OPTION_ACCEPT
#define COAP_MEDIATYPE_APPLICATION_CBOR
static int is_binary(int content_format)
Returns 1 if the given content_format is either unknown or known to carry binary data.
#define COAP_OPT_BLOCK_MORE(opt)
Returns the value of the More-bit of a Block option opt.
coap_log_t coap_get_log_level(void)
Returns the current log level.
#define COAP_MEDIATYPE_APPLICATION_OCTET_STREAM
unsigned int coap_decode_var_bytes(unsigned char *buf, unsigned int len)
Decodes multiple-length byte sequences.
#define COAP_OPTION_URI_HOST
static unsigned int print_content_format(unsigned int format_type, unsigned char *result, unsigned int buflen)
#define COAP_OPT_VALUE(opt)