diff --git a/examples/lwm2m/standalone/coap-hex/coap-hex.c b/examples/lwm2m/standalone/coap-hex/coap-hex.c index 7fba02ccf..d663d7e24 100644 --- a/examples/lwm2m/standalone/coap-hex/coap-hex.c +++ b/examples/lwm2m/standalone/coap-hex/coap-hex.c @@ -42,14 +42,10 @@ #include #include -#define DEBUG 0 -#if DEBUG -#define PRINTF(...) printf(__VA_ARGS__) -#define PRINTEP(ep) coap_endpoint_print(ep) -#else -#define PRINTF(...) -#define PRINTEP(ep) -#endif +/* Log configuration */ +#include "coap-log.h" +#define LOG_MODULE "coap-hex" +#define LOG_LEVEL LOG_LEVEL_COAP #ifdef WITH_DTLS #include "tinydtls.h" @@ -104,12 +100,32 @@ coap_endpoint_cmp(const coap_endpoint_t *e1, const coap_endpoint_t *e2) } /*---------------------------------------------------------------------------*/ void +coap_endpoint_log(const coap_endpoint_t *ep) +{ + LOG_OUTPUT("%u", ep->addr); +} +/*---------------------------------------------------------------------------*/ +void coap_endpoint_print(const coap_endpoint_t *ep) { printf("%u", ep->addr); } /*---------------------------------------------------------------------------*/ int +coap_endpoint_snprint(char *buf, size_t size, const coap_endpoint_t *ep) +{ + int n; + if(size == 0) { + return 0; + } + n = snprintf(buf, size - 1, "%u", ep->addr); + if(n >= size - 1) { + buf[size - 1] = '\0'; + } + return n; +} +/*---------------------------------------------------------------------------*/ +int coap_endpoint_parse(const char *text, size_t size, coap_endpoint_t *ep) { /* Hex based CoAP has no addresses, just writes data to standard out */ @@ -178,20 +194,20 @@ stdin_callback(const char *line) buf[len] = (uint8_t)(((v1 << 4) | v2) & 0xff); } - PRINTF("RECV from "); - PRINTEP(&last_source); - PRINTF(" %u bytes\n", len); + LOG_INFO("RECV from "); + LOG_INFO_COAP_EP(&last_source); + LOG_INFO_(" %u bytes\n", len); coap_buf_len = len; - if(DEBUG) { + if(LOG_DBG_ENABLED) { int i; uint8_t *data; data = coap_databuf(); - printf("Received:"); + LOG_DBG("Received: "); for(i = 0; i < len; i++) { - printf("%02x", data[i]); + LOG_DBG_("%02x", data[i]); } - printf("\n"); + LOG_DBG_("\n"); } #ifdef WITH_DTLS @@ -208,13 +224,13 @@ coap_transport_init(void) { select_set_stdin_callback(stdin_callback); - printf("CoAP listening on standard in\n"); + LOG_INFO("CoAP listening on standard in\n"); #ifdef WITH_DTLS /* create new contet with app-data - no real app-data... */ dtls_context = dtls_new_context(&last_source); - if (!dtls_context) { - PRINTF("DTLS: cannot create context\n"); + if(!dtls_context) { + LOG_ERR("DTLS: cannot create context\n"); exit(-1); } @@ -224,7 +240,7 @@ coap_transport_init(void) memcpy(psk_id, PSK_DEFAULT_IDENTITY, psk_id_length); memcpy(psk_key, PSK_DEFAULT_KEY, psk_key_length); #endif /* DTLS_PSK */ - PRINTF("Setting DTLS handler\n"); + LOG_DBG("Setting DTLS handler\n"); dtls_set_handler(dtls_context, &cb); #endif /* WITH_DTLS */ @@ -234,7 +250,7 @@ int coap_sendto(const coap_endpoint_t *ep, const uint8_t *data, uint16_t len) { if(!coap_endpoint_is_connected(ep)) { - PRINTF("CoAP endpoint not connected\n"); + LOG_WARN("CoAP endpoint not connected\n"); return -1; } @@ -261,9 +277,9 @@ coap_endpoint_connect(coap_endpoint_t *ep) return 1; } #ifdef WITH_DTLS - PRINTF("DTLS EP:"); - PRINTEP(ep); - PRINTF(" len:%d\n", ep->size); + LOG_DBG("DTLS EP:"); + LOG_DBG_COAP_EP(ep); + LOG_DBG_(" len:%d\n", ep->size); /* setup all address info here... should be done to connect */ @@ -295,14 +311,14 @@ coap_endpoint_is_connected(const coap_endpoint_t *ep) peer = dtls_get_peer(dtls_context, ep); if(peer != NULL) { /* only if handshake is done! */ - PRINTF("peer state for "); - PRINTEP(ep); - PRINTF(" is %d %d\n", peer->state, dtls_peer_is_connected(peer)); + LOG_DBG("peer state for "); + LOG_DBG_COAP_EP(ep); + LOG_DBG_(" is %d %d\n", peer->state, dtls_peer_is_connected(peer)); return dtls_peer_is_connected(peer); } else { - PRINTF("Did not find peer "); - PRINTEP(ep); - PRINTF("\n"); + LOG_DBG("Did not find peer "); + LOG_DBG_COAP_EP(ep); + LOG_DBG_("\n"); } #endif /* WITH_DTLS */ return 0; @@ -324,11 +340,13 @@ input_from_peer(struct dtls_context_t *ctx, dtls_peer_t *peer; printf("received data:"); - for (i = 0; i < len; i++) + for(i = 0; i < len; i++) { printf("%c", data[i]); + } printf("\nHex:"); - for (i = 0; i < len; i++) + for(i = 0; i < len; i++) { printf("%02x", data[i]); + } printf("\n"); /* Send this into coap-input */ @@ -377,14 +395,14 @@ get_psk_info(struct dtls_context_t *ctx, const unsigned char *id, size_t id_len, unsigned char *result, size_t result_length) { - PRINTF("---===>>> Getting the Key or ID <<<===---\n"); - switch (type) { + LOG_DBG("---===>>> Getting the Key or ID <<<===---\n"); + switch(type) { case DTLS_PSK_IDENTITY: - if (id_len) { + if(id_len) { dtls_debug("got psk_identity_hint: '%.*s'\n", id_len, id); } - if (result_length < psk_id_length) { + if(result_length < psk_id_length) { dtls_warn("cannot set psk_identity -- buffer too small\n"); return dtls_alert_fatal_create(DTLS_ALERT_INTERNAL_ERROR); } @@ -392,10 +410,10 @@ get_psk_info(struct dtls_context_t *ctx, memcpy(result, psk_id, psk_id_length); return psk_id_length; case DTLS_PSK_KEY: - if (id_len != psk_id_length || memcmp(psk_id, id, id_len) != 0) { + if(id_len != psk_id_length || memcmp(psk_id, id, id_len) != 0) { dtls_warn("PSK for unknown id requested, exiting\n"); return dtls_alert_fatal_create(DTLS_ALERT_ILLEGAL_PARAMETER); - } else if (result_length < psk_key_length) { + } else if(result_length < psk_key_length) { dtls_warn("cannot set psk -- buffer too small\n"); return dtls_alert_fatal_create(DTLS_ALERT_INTERNAL_ERROR); } diff --git a/examples/lwm2m/standalone/coap-ipv4/coap-ipv4.c b/examples/lwm2m/standalone/coap-ipv4/coap-ipv4.c index 2b493b9a6..b8b5c464c 100644 --- a/examples/lwm2m/standalone/coap-ipv4/coap-ipv4.c +++ b/examples/lwm2m/standalone/coap-ipv4/coap-ipv4.c @@ -50,15 +50,10 @@ #include #include -#define DEBUG 1 -#define DEBUG_VERBOSE ((DEBUG) && 0) -#if DEBUG -#define PRINTF(...) printf(__VA_ARGS__) -#define PRINTEP(ep) coap_endpoint_print(ep) -#else /* DEBUG */ -#define PRINTF(...) -#define PRINTEP(ep) -#endif /* DEBUG */ +/* Log configuration */ +#include "coap-log.h" +#define LOG_MODULE "coap-ipv4" +#define LOG_LEVEL LOG_LEVEL_COAP #ifdef WITH_DTLS #include "tinydtls.h" @@ -109,14 +104,14 @@ coap_endpoint_is_connected(const coap_endpoint_t *ep) peer = dtls_get_peer(dtls_context, ep); if(peer != NULL) { /* only if handshake is done! */ - PRINTF("DTLS peer state for "); - PRINTEP(ep); - PRINTF(" is %d %d\n", peer->state, dtls_peer_is_connected(peer)); + LOG_DBG("DTLS peer state for "); + LOG_DBG_COAP_EP(ep); + LOG_DBG_(" is %d %d\n", peer->state, dtls_peer_is_connected(peer)); return dtls_peer_is_connected(peer); } else { - PRINTF("DTLS did not find peer "); - PRINTEP(ep); - PRINTF("\n"); + LOG_DBG("DTLS did not find peer "); + LOG_DBG_COAP_EP(ep); + LOG_DBG_("\n"); } #endif /* WITH_DTLS */ return 0; @@ -133,9 +128,9 @@ coap_endpoint_connect(coap_endpoint_t *ep) } #ifdef WITH_DTLS - PRINTF("DTLS connect to "); - PRINTEP(ep); - PRINTF(" len:%d\n", ep->size); + LOG_DBG("DTLS connect to "); + LOG_DBG_COAP_EP(ep); + LOG_DBG_(" len:%d\n", ep->size); /* setup all address info here... should be done to connect */ if(dtls_context) { @@ -179,6 +174,19 @@ coap_endpoint_cmp(const coap_endpoint_t *e1, const coap_endpoint_t *e2) } /*---------------------------------------------------------------------------*/ void +coap_endpoint_log(const coap_endpoint_t *ep) +{ + const char *address; + address = inet_ntoa(ep->addr.sin_addr); + if(address != NULL) { + LOG_OUTPUT("coap%s://%s:%u", ep->secure ? "s" : "", + address, ntohs(ep->addr.sin_port)); + } else { + LOG_OUTPUT("<#N/A>"); + } +} +/*---------------------------------------------------------------------------*/ +void coap_endpoint_print(const coap_endpoint_t *ep) { const char *address; @@ -192,6 +200,28 @@ coap_endpoint_print(const coap_endpoint_t *ep) } /*---------------------------------------------------------------------------*/ int +coap_endpoint_snprint(char *buf, size_t size, const coap_endpoint_t *ep) +{ + const char *address; + int n; + if(size == 0) { + return 0; + } + address = inet_ntoa(ep->addr.sin_addr); + if(address != NULL) { + n = snprintf(buf, size - 1, + "coap%s://%s:%u", ep->secure ? "s" : "", + address, ntohs(ep->addr.sin_port)); + } else { + n = snprintf(buf, size - 1, "<#N/A>"); + } + if(n >= size - 1) { + buf[size - 1] = '\0'; + } + return n; +} +/*---------------------------------------------------------------------------*/ +int coap_endpoint_parse(const char *text, size_t size, coap_endpoint_t *ep) { /* text = format coap://host:port/... we assume */ @@ -203,14 +233,14 @@ coap_endpoint_parse(const char *text, size_t size, coap_endpoint_t *ep) int secure; int offset = 0; int i; - PRINTF("CoAP-IPv4: parsing endpoint %.*s => ", (int)size, text); + LOG_DBG("parsing endpoint %.*s => ", (int)size, text); if(strncmp("coap://", text, 7) == 0) { secure = 0; offset = 7; } else if(strncmp("coaps://", text, 8) == 0) { secure = 1; offset = 8; - PRINTF("secure "); + LOG_DBG_("secure "); } else { secure = 0; } @@ -227,7 +257,7 @@ coap_endpoint_parse(const char *text, size_t size, coap_endpoint_t *ep) port = atoi(&text[i + 1]); } - PRINTF("endpoint at %s:%u\n", host, port); + LOG_DBG_("endpoint at %s:%u\n", host, port); ep->addr.sin_family = AF_INET; ep->addr.sin_port = htons(port); @@ -235,7 +265,7 @@ coap_endpoint_parse(const char *text, size_t size, coap_endpoint_t *ep) ep->secure = secure; if(inet_aton(host, &ep->addr.sin_addr) == 0) { /* Failed to parse the address */ - PRINTF("CoAP-IPv4: Failed to parse endpoint host '%s'\n", host); + LOG_WARN("Failed to parse endpoint host '%s'\n", host); return 0; } return 1; @@ -272,13 +302,13 @@ read_packet_to_coapbuf(int fd, int is_secure) last_source.secure = is_secure; - PRINTF("CoAP-IPv4: RECV from "); - PRINTEP(&last_source); - PRINTF(" %u bytes\n", len); + LOG_INFO("RECV from "); + LOG_INFO_COAP_EP(&last_source); + LOG_INFO_(" %u bytes\n", len); coap_buf_len = len; #if 0 - if((rand() & 0xffff) < 0x1000) { + if((rand() & 0xffff) < 0x1000) { printf("*********---- PACKET LOSS ----********\n"); return 0; } @@ -303,16 +333,16 @@ coap_ipv4_handle_fd(fd_set *rset, fd_set *wset) if(coap_ipv4_fd >= 0 && FD_ISSET(coap_ipv4_fd, rset)) { if(read_packet_to_coapbuf(coap_ipv4_fd, 0)) { -#if DEBUG_VERBOSE +#if LOG_DBG_ENABLED int i; uint8_t *data; data = coap_databuf(); - PRINTF("Received:"); + LOG_DBG("Received: "); for(i = 0; i < coap_buf_len; i++) { - PRINTF("%02x", data[i]); + LOG_DBG_("%02x", data[i]); } - PRINTF("\n"); -#endif /* DEBUG_VERBOSE */ + LOG_DBG_("\n"); +#endif /* LOG_DBG_ENABLED */ coap_receive(coap_src_endpoint(), coap_databuf(), coap_datalen()); } } @@ -377,7 +407,7 @@ coap_transport_init(void) exit(1); } - printf("CoAP server listening on port %u\n", COAP_SERVER_PORT); + LOG_INFO("CoAP server listening on port %u\n", COAP_SERVER_PORT); select_set_callback(coap_ipv4_fd, &udp_callback); #ifdef WITH_DTLS @@ -402,7 +432,7 @@ coap_transport_init(void) exit(1); } - printf("CoAP DTLS server listening on port %u\n", COAP_DEFAULT_SECURE_PORT); + LOG_INFO("CoAP DTLS server listening on port %u\n", COAP_DEFAULT_SECURE_PORT); select_set_callback(dtls_ipv4_fd, &dtls_callback); /* create new contet with app-data */ @@ -422,9 +452,9 @@ coap_sendto(const coap_endpoint_t *ep, const uint8_t *data, uint16_t len) int ret; if(!coap_endpoint_is_connected(ep)) { - PRINTF("CoAP-IPv4: endpoint "); - PRINTEP(ep); - PRINTF(" not connected - dropping packet\n"); + LOG_WARN("endpoint "); + LOG_WARN_COAP_EP(ep); + LOG_WARN_(" not connected - dropping packet\n"); return -1; } @@ -432,16 +462,16 @@ coap_sendto(const coap_endpoint_t *ep, const uint8_t *data, uint16_t len) if(coap_endpoint_is_secure(ep)) { if(dtls_context) { ret = dtls_write(dtls_context, (session_t *)ep, (uint8_t *)data, len); - PRINTF("CoAP-IPv4: SENT DTLS to "); - PRINTEP(ep); + LOG_DBG("SENT DTLS to "); + LOG_DBG_COAP_EP(ep); if(ret < 0) { - PRINTF(" - error %d\n", ret); + LOG_DBG_(" - error %d\n", ret); } else { - PRINTF(" %d/%u bytes\n", ret, len); + LOG_DBG_(" %d/%u bytes\n", ret, len); } return ret; } - PRINTF("CoAP-IPv4: no DTLS context\n"); + LOG_WARN("no DTLS context\n"); return -1; } #endif /* WITH_DTLS */ @@ -449,26 +479,26 @@ coap_sendto(const coap_endpoint_t *ep, const uint8_t *data, uint16_t len) if(coap_ipv4_fd >= 0) { ret = sendto(coap_ipv4_fd, data, len, 0, (struct sockaddr *)&ep->addr, ep->size); - PRINTF("CoAP-IPv4: SENT to "); - PRINTEP(ep); + LOG_INFO("SENT to "); + LOG_INFO_COAP_EP(ep); if(ret < 0) { - PRINTF(" - error %d: %s\n", ret, strerror(errno)); + LOG_INFO_(" - error %d: %s\n", ret, strerror(errno)); } else { - PRINTF(" %d/%u bytes\n", ret, len); + LOG_INFO_(" %d/%u bytes\n", ret, len); - if(DEBUG_VERBOSE) { + if(LOG_DBG_ENABLED) { int i; - PRINTF("Sent:"); + LOG_DBG("Sent: "); for(i = 0; i < len; i++) { - PRINTF("%02x", data[i]); + LOG_DBG_("%02x", data[i]); } - PRINTF("\n"); + LOG_DBG_("\n"); } } return ret; } - PRINTF("CoAP-IPv4: failed to send - no socket\n"); + LOG_DBG("failed to send - no socket\n"); return -1; } /*---------------------------------------------------------------------------*/ @@ -481,19 +511,20 @@ static int input_from_peer(struct dtls_context_t *ctx, session_t *session, uint8_t *data, size_t len) { -#if DEBUG_VERBOSE +#if LOG_DBG_ENABLED size_t i; - PRINTF("DTLS received data:"); + LOG_DBG("DTLS received data: "); for(i = 0; i < len; i++) { - PRINTF("%c", data[i]); + LOG_DBG_("%c", data[i]); } - PRINTF("\nHex:"); + LOG_DBG_("\n"); + LOG_DBG("Hex: "); for(i = 0; i < len; i++) { - PRINTF("%02x", data[i]); + LOG_DBG_("%02x", data[i]); } - PRINTF("\n"); -#endif /* DEBUG_VERBOSE */ + LOG_DBG_("\n"); +#endif /* LOG_DBG_ENABLED */ /* Send this into coap-input */ memmove(coap_databuf(), data, len); @@ -513,10 +544,8 @@ output_to_peer(struct dtls_context_t *ctx, session_t *session, uint8_t *data, size_t len) { int fd = *(int *)dtls_get_app_data(ctx); -#if DEBUG_VERBOSE - PRINTF("DTLS output_to_peer len:%d %d (s-size: %d)\n", (int)len, fd, - session->size); -#endif /* DEBUG_VERBOSE */ + LOG_DBG("DTLS output_to_peer len:%d %d (s-size: %d)\n", (int)len, fd, + session->size); return sendto(fd, data, len, MSG_DONTWAIT, (struct sockaddr *)&session->addr, session->size); } @@ -543,12 +572,12 @@ get_psk_info(struct dtls_context_t *ctx, keystore = dtls_keystore; if(keystore == NULL) { - PRINTF("--- No key store available ---\n"); + LOG_DBG("--- No key store available ---\n"); return 0; } memset(&ks, 0, sizeof(ks)); - PRINTF("---===>>> Getting the Key or ID <<<===---\n"); + LOG_DBG("---===>>> Getting the Key or ID <<<===---\n"); switch(type) { case DTLS_PSK_IDENTITY: if(id && id_len) { @@ -562,16 +591,16 @@ get_psk_info(struct dtls_context_t *ctx, keystore->coap_get_psk_info((coap_endpoint_t *)session, &ks); } if(ks.identity == NULL || ks.identity_len == 0) { - PRINTF("no psk_identity found\n"); + LOG_DBG("no psk_identity found\n"); return 0; } if(result_length < ks.identity_len) { - PRINTF("cannot return psk_identity -- buffer too small\n"); + LOG_DBG("cannot return psk_identity -- buffer too small\n"); return dtls_alert_fatal_create(DTLS_ALERT_INTERNAL_ERROR); } memcpy(result, ks.identity, ks.identity_len); - PRINTF("psk_identity with %u bytes found\n", ks.identity_len); + LOG_DBG("psk_identity with %u bytes found\n", ks.identity_len); return ks.identity_len; case DTLS_PSK_KEY: @@ -582,16 +611,16 @@ get_psk_info(struct dtls_context_t *ctx, keystore->coap_get_psk_info((coap_endpoint_t *)session, &ks); } if(ks.key == NULL || ks.key_len == 0) { - PRINTF("PSK for unknown id requested, exiting\n"); + LOG_DBG("PSK for unknown id requested, exiting\n"); return dtls_alert_fatal_create(DTLS_ALERT_ILLEGAL_PARAMETER); } if(result_length < ks.key_len) { - PRINTF("cannot return psk -- buffer too small\n"); + LOG_DBG("cannot return psk -- buffer too small\n"); return dtls_alert_fatal_create(DTLS_ALERT_INTERNAL_ERROR); } memcpy(result, ks.key, ks.key_len); - PRINTF("psk with %u bytes found\n", ks.key_len); + LOG_DBG("psk with %u bytes found\n", ks.key_len); return ks.key_len; default: diff --git a/examples/lwm2m/standalone/coap-log-conf.h b/examples/lwm2m/standalone/coap-log-conf.h new file mode 100644 index 000000000..a6c58c218 --- /dev/null +++ b/examples/lwm2m/standalone/coap-log-conf.h @@ -0,0 +1,111 @@ +/* + * Copyright (c) 2017, RISE SICS. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/** + * \file + * Standalone CoAP log configuration + * \author + * Niclas Finne + * Joakim Eriksson + */ + +#ifndef COAP_LOG_CONF_H_ +#define COAP_LOG_CONF_H_ + +#include "contiki.h" +#include + +/* The different log levels available */ +#define LOG_LEVEL_NONE 0 /* No log */ +#define LOG_LEVEL_ERR 1 /* Errors */ +#define LOG_LEVEL_WARN 2 /* Warnings */ +#define LOG_LEVEL_INFO 3 /* Basic info */ +#define LOG_LEVEL_DBG 4 /* Detailed debug */ + +#ifndef LOG_LEVEL_COAP +#define LOG_LEVEL_COAP LOG_LEVEL_DBG +#endif /* LOG_LEVEL_COAP */ + +#ifndef LOG_WITH_LOC +#define LOG_WITH_LOC 0 +#endif /* LOG_WITH_LOC */ + +#ifndef LOG_WITH_MODULE_PREFIX +#define LOG_WITH_MODULE_PREFIX 1 +#endif /* LOG_WITH_MODULE_PREFIX */ + +/* Custom output function -- default is printf */ +#ifdef LOG_CONF_OUTPUT +#define LOG_OUTPUT(...) LOG_CONF_OUTPUT(__VA_ARGS__) +#else /* LOG_CONF_OUTPUT */ +#define LOG_OUTPUT(...) printf(__VA_ARGS__) +#endif /* LOG_CONF_OUTPUT */ + +/* Custom line prefix output function -- default is LOG_OUTPUT */ +#ifdef LOG_CONF_OUTPUT_PREFIX +#define LOG_OUTPUT_PREFIX(level, levelstr, module) LOG_CONF_OUTPUT_PREFIX(level, levelstr, module) +#else /* LOG_CONF_OUTPUT_PREFIX */ +#define LOG_OUTPUT_PREFIX(level, levelstr, module) LOG_OUTPUT("[%-4s: %-10s] ", levelstr, module) +#endif /* LOG_CONF_OUTPUT_PREFIX */ + +/* Main log function */ + +#define LOG(newline, level, levelstr, ...) do { \ + if(level <= (LOG_LEVEL)) { \ + if(newline) { \ + if(LOG_WITH_MODULE_PREFIX) { \ + LOG_OUTPUT_PREFIX(level, levelstr, LOG_MODULE); \ + } \ + if(LOG_WITH_LOC) { \ + LOG_OUTPUT("[%s: %d] ", __FILE__, __LINE__); \ + } \ + } \ + LOG_OUTPUT(__VA_ARGS__); \ + } \ + } while (0) + +/* More compact versions of LOG macros */ +#define LOG_ERR(...) LOG(1, LOG_LEVEL_ERR, "ERR", __VA_ARGS__) +#define LOG_WARN(...) LOG(1, LOG_LEVEL_WARN, "WARN", __VA_ARGS__) +#define LOG_INFO(...) LOG(1, LOG_LEVEL_INFO, "INFO", __VA_ARGS__) +#define LOG_DBG(...) LOG(1, LOG_LEVEL_DBG, "DBG", __VA_ARGS__) + +#define LOG_ERR_(...) LOG(0, LOG_LEVEL_ERR, "ERR", __VA_ARGS__) +#define LOG_WARN_(...) LOG(0, LOG_LEVEL_WARN, "WARN", __VA_ARGS__) +#define LOG_INFO_(...) LOG(0, LOG_LEVEL_INFO, "INFO", __VA_ARGS__) +#define LOG_DBG_(...) LOG(0, LOG_LEVEL_DBG, "DBG", __VA_ARGS__) + +/* For testing log level */ +#define LOG_ERR_ENABLED ((LOG_LEVEL) >= (LOG_LEVEL_ERR)) +#define LOG_WARN_ENABLED ((LOG_LEVEL) >= (LOG_LEVEL_WARN)) +#define LOG_INFO_ENABLED ((LOG_LEVEL) >= (LOG_LEVEL_INFO)) +#define LOG_DBG_ENABLED ((LOG_LEVEL) >= (LOG_LEVEL_DBG)) + +#endif /* COAP_LOG_CONF_H_ */ diff --git a/examples/lwm2m/standalone/contiki.h b/examples/lwm2m/standalone/contiki.h index 43a85eaf2..e626cc816 100644 --- a/examples/lwm2m/standalone/contiki.h +++ b/examples/lwm2m/standalone/contiki.h @@ -42,6 +42,8 @@ #include "posix-main.h" +#define COAP_LOG_CONF_PATH "coap-log-conf.h" + #define COAP_TIMER_CONF_DRIVER coap_timer_native_driver #define LWM2M_ENGINE_CLIENT_ENDPOINT_NAME "lwm2m-ex" diff --git a/os/net/app-layer/coap/coap-block1.c b/os/net/app-layer/coap/coap-block1.c index 4ecd8a1c5..d39e3d730 100644 --- a/os/net/app-layer/coap/coap-block1.c +++ b/os/net/app-layer/coap/coap-block1.c @@ -36,18 +36,16 @@ * Lars Schmertmann */ -#include #include +#include #include "coap.h" #include "coap-block1.h" -#define DEBUG 0 -#if DEBUG -#define PRINTF(...) printf(__VA_ARGS__) -#else -#define PRINTF(...) -#endif +/* Log configuration */ +#include "coap-log.h" +#define LOG_MODULE "coap-block1" +#define LOG_LEVEL LOG_LEVEL_COAP /*----------------------------------------------------------------------------*/ @@ -97,11 +95,12 @@ coap_block1_handler(coap_message_t *request, coap_message_t *response, } if(coap_is_option(request, COAP_OPTION_BLOCK1)) { - PRINTF("Blockwise: block 1 request: Num: %u, More: %u, Size: %u, Offset: %u\n", - request->block1_num, - request->block1_more, - request->block1_size, - request->block1_offset); + LOG_DBG("Blockwise: block 1 request: Num: %"PRIu32 + ", More: %u, Size: %u, Offset: %"PRIu32"\n", + request->block1_num, + request->block1_more, + request->block1_size, + request->block1_offset); coap_set_header_block1(response, request->block1_num, request->block1_more, request->block1_size); if(request->block1_more) { diff --git a/os/net/app-layer/coap/coap-blocking-api.c b/os/net/app-layer/coap/coap-blocking-api.c index bd0877a67..c8e8a1c6c 100644 --- a/os/net/app-layer/coap/coap-blocking-api.c +++ b/os/net/app-layer/coap/coap-blocking-api.c @@ -42,14 +42,12 @@ #include #include #include +#include -#define DEBUG 0 -#if DEBUG -#include -#define PRINTF(...) printf(__VA_ARGS__) -#else -#define PRINTF(...) -#endif +/* Log configuration */ +#include "coap-log.h" +#define LOG_MODULE "coap-blocking-api" +#define LOG_LEVEL LOG_LEVEL_COAP /*---------------------------------------------------------------------------*/ /*- Client Part -------------------------------------------------------------*/ @@ -99,29 +97,30 @@ PT_THREAD(coap_blocking_request message); coap_send_transaction(state->transaction); - PRINTF("Requested #%lu (MID %u)\n", state->block_num, request->mid); + LOG_DBG("Requested #%"PRIu32" (MID %u)\n", state->block_num, request->mid); PT_YIELD_UNTIL(&state->pt, ev == PROCESS_EVENT_POLL); if(!state->response) { - PRINTF("Server not responding\n"); + LOG_WARN("Server not responding\n"); PT_EXIT(&state->pt); } coap_get_header_block2(state->response, &res_block, &more, NULL, NULL); - PRINTF("Received #%lu%s (%u bytes)\n", res_block, more ? "+" : "", - state->response->payload_len); + LOG_DBG("Received #%"PRIu32"%s (%u bytes)\n", res_block, more ? "+" : "", + state->response->payload_len); if(res_block == state->block_num) { request_callback(state->response); ++(state->block_num); } else { - PRINTF("WRONG BLOCK %lu/%lu\n", res_block, state->block_num); + LOG_WARN("WRONG BLOCK %"PRIu32"/%"PRIu32"\n", + res_block, state->block_num); ++block_error; } } else { - PRINTF("Could not allocate transaction buffer"); + LOG_WARN("Could not allocate transaction buffer"); PT_EXIT(&state->pt); } } while(more && block_error < COAP_MAX_ATTEMPTS); diff --git a/os/net/app-layer/coap/coap-callback-api.c b/os/net/app-layer/coap/coap-callback-api.c index 1f20e0a86..a83806727 100644 --- a/os/net/app-layer/coap/coap-callback-api.c +++ b/os/net/app-layer/coap/coap-callback-api.c @@ -45,13 +45,10 @@ #include #include -#define DEBUG 0 -#if DEBUG -#include -#define PRINTF(...) printf(__VA_ARGS__) -#else -#define PRINTF(...) -#endif +/* Log configuration */ +#include "coap-log.h" +#define LOG_MODULE "coap-callback-api" +#define LOG_LEVEL LOG_LEVEL_COAP /* These should go into the state struct so that we can have multiple requests */ @@ -81,8 +78,8 @@ progress_request(coap_request_state_t *state) { coap_serialize_message(request, state->transaction->message); coap_send_transaction(state->transaction); - PRINTF("Requested #%lu (MID %u)\n", (unsigned long) state->block_num, - request->mid); + LOG_DBG("Requested #%lu (MID %u)\n", (unsigned long)state->block_num, + request->mid); } } @@ -96,10 +93,10 @@ coap_request_callback(void *callback_data, coap_message_t *response) state->response = response; - PRINTF("COAP: request callback\n"); + LOG_DBG("request callback\n"); if(!state->response) { - PRINTF("Server not responding giving up...\n"); + LOG_WARN("Server not responding giving up...\n"); state->callback(state); return; } @@ -108,10 +105,10 @@ coap_request_callback(void *callback_data, coap_message_t *response) coap_get_header_block2(state->response, &res_block, &more, NULL, NULL); coap_get_header_block1(state->response, &res_block1, NULL, NULL, NULL); - PRINTF("Received #%lu%s B1:%lu (%u bytes)\n", - (unsigned long) res_block, (unsigned) more ? "+" : "", - (unsigned long) res_block1, - state->response->payload_len); + LOG_DBG("Received #%lu%s B1:%lu (%u bytes)\n", + (unsigned long)res_block, (unsigned)more ? "+" : "", + (unsigned long)res_block1, + state->response->payload_len); if(res_block == state->block_num) { /* Call the callback function as we have more data */ @@ -119,8 +116,8 @@ coap_request_callback(void *callback_data, coap_message_t *response) /* this is only for counting BLOCK2 blocks.*/ ++(state->block_num); } else { - PRINTF("WRONG BLOCK %lu/%lu\n", (unsigned long) res_block, - (unsigned long) state->block_num); + LOG_WARN("WRONG BLOCK %lu/%lu\n", (unsigned long)res_block, + (unsigned long)state->block_num); ++block_error; } diff --git a/os/net/app-layer/coap/coap-endpoint.h b/os/net/app-layer/coap/coap-endpoint.h index 208eb49a7..705b10fa2 100644 --- a/os/net/app-layer/coap/coap-endpoint.h +++ b/os/net/app-layer/coap/coap-endpoint.h @@ -56,7 +56,10 @@ void coap_endpoint_copy(coap_endpoint_t *destination, int coap_endpoint_cmp(const coap_endpoint_t *e1, const coap_endpoint_t *e2); +void coap_endpoint_log(const coap_endpoint_t *ep); void coap_endpoint_print(const coap_endpoint_t *ep); +int coap_endpoint_snprint(char *str, size_t size, + const coap_endpoint_t *ep); int coap_endpoint_parse(const char *text, size_t size, coap_endpoint_t *ep); diff --git a/os/net/app-layer/coap/coap-engine.c b/os/net/app-layer/coap/coap-engine.c index 43d9a2c8d..25d48671e 100644 --- a/os/net/app-layer/coap/coap-engine.c +++ b/os/net/app-layer/coap/coap-engine.c @@ -44,17 +44,10 @@ #include #include -#define DEBUG 0 -#if DEBUG -#include -#define PRINTF(...) printf(__VA_ARGS__) -#define PRINTS(l,s,f) do { int i; \ - for(i = 0; i < l; i++) printf(f, s[i]); \ - } while(0) -#else -#define PRINTF(...) -#define PRINTS(l,s,f) -#endif +/* Log configuration */ +#include "coap-log.h" +#define LOG_MODULE "coap-engine" +#define LOG_LEVEL LOG_LEVEL_COAP static void process_callback(coap_timer_t *t); @@ -160,13 +153,14 @@ coap_receive(const coap_endpoint_t *src, /*TODO duplicates suppression, if required by application */ - PRINTF(" Parsed: v %u, t %u, tkl %u, c %u, mid %u\n", message->version, - message->type, message->token_len, message->code, message->mid); - PRINTF(" URL:"); - PRINTS(message->uri_path_len, message->uri_path, "%c"); - PRINTF("\n Payload: "); - PRINTS(message->payload_len, message->payload, "%c"); - PRINTF("\n"); + LOG_DBG(" Parsed: v %u, t %u, tkl %u, c %u, mid %u\n", message->version, + message->type, message->token_len, message->code, message->mid); + LOG_DBG(" URL:"); + LOG_DBG_COAP_STRING(message->uri_path, message->uri_path_len); + LOG_DBG_("\n"); + LOG_DBG(" Payload: "); + LOG_DBG_COAP_STRING((const char *)message->payload, message->payload_len); + LOG_DBG_("\n"); /* handle requests */ if(message->code >= COAP_GET && message->code <= COAP_DELETE) { @@ -195,8 +189,8 @@ coap_receive(const coap_endpoint_t *src, } if(coap_get_header_block2 (message, &block_num, NULL, &block_size, &block_offset)) { - PRINTF("Blockwise: block request %"PRIu32" (%u/%u) @ %"PRIu32" bytes\n", - block_num, block_size, COAP_MAX_BLOCK_SIZE, block_offset); + LOG_DBG("Blockwise: block request %"PRIu32" (%u/%u) @ %"PRIu32" bytes\n", + block_num, block_size, COAP_MAX_BLOCK_SIZE, block_offset); block_size = MIN(block_size, COAP_MAX_BLOCK_SIZE); new_offset = block_offset; } @@ -215,7 +209,7 @@ coap_receive(const coap_endpoint_t *src, if(coap_is_option(message, COAP_OPTION_BLOCK1) && response->code < BAD_REQUEST_4_00 && !coap_is_option(response, COAP_OPTION_BLOCK1)) { - PRINTF("Block1 NOT IMPLEMENTED\n"); + LOG_DBG("Block1 NOT IMPLEMENTED\n"); coap_status_code = NOT_IMPLEMENTED_5_01; coap_error_message = "NoBlock1Support"; @@ -225,12 +219,10 @@ coap_receive(const coap_endpoint_t *src, /* unchanged new_offset indicates that resource is unaware of blockwise transfer */ if(new_offset == block_offset) { - PRINTF - ("Blockwise: unaware resource with payload length %u/%u\n", - response->payload_len, block_size); + LOG_DBG("Blockwise: unaware resource with payload length %u/%u\n", + response->payload_len, block_size); if(block_offset >= response->payload_len) { - PRINTF - ("handle_incoming_data(): block_offset >= response->payload_len\n"); + LOG_DBG("handle_incoming_data(): block_offset >= response->payload_len\n"); response->code = BAD_OPTION_4_02; coap_set_payload(response, "BlockOutOfScope", 15); /* a const char str[] and sizeof(str) produces larger code size */ @@ -247,8 +239,8 @@ coap_receive(const coap_endpoint_t *src, /* resource provides chunk-wise data */ } else { - PRINTF("Blockwise: blockwise resource, new offset %"PRId32"\n", - new_offset); + LOG_DBG("Blockwise: blockwise resource, new offset %"PRId32"\n", + new_offset); coap_set_header_block2(response, block_num, new_offset != -1 || response->payload_len > @@ -262,9 +254,8 @@ coap_receive(const coap_endpoint_t *src, /* Resource requested Block2 transfer */ } else if(new_offset != 0) { - PRINTF - ("Blockwise: no block option for blockwise resource, using block size %u\n", - COAP_MAX_BLOCK_SIZE); + LOG_DBG("Blockwise: no block option for blockwise resource, using block size %u\n", + COAP_MAX_BLOCK_SIZE); coap_set_header_block2(response, 0, new_offset != -1, COAP_MAX_BLOCK_SIZE); @@ -293,13 +284,13 @@ coap_receive(const coap_endpoint_t *src, } else { if(message->type == COAP_TYPE_CON && message->code == 0) { - PRINTF("Received Ping\n"); + LOG_INFO("Received Ping\n"); coap_status_code = PING_RESPONSE; } else if(message->type == COAP_TYPE_ACK) { /* transactions are closed through lookup below */ - PRINTF("Received ACK\n"); + LOG_DBG("Received ACK\n"); } else if(message->type == COAP_TYPE_RST) { - PRINTF("Received RST\n"); + LOG_INFO("Received RST\n"); /* cancel possible subscriptions */ coap_remove_observer_by_mid(src, message->mid); } @@ -323,7 +314,7 @@ coap_receive(const coap_endpoint_t *src, /* if observe notification */ if((message->type == COAP_TYPE_CON || message->type == COAP_TYPE_NON) && coap_is_option(message, COAP_OPTION_OBSERVE)) { - PRINTF("Observe [%u]\n", message->observe); + LOG_DBG("Observe [%"PRId32"]\n", message->observe); coap_handle_notification(src, message); } #endif /* COAP_OBSERVE_CLIENT */ @@ -336,12 +327,12 @@ coap_receive(const coap_endpoint_t *src, coap_send_transaction(transaction); } } else if(coap_status_code == MANUAL_RESPONSE) { - PRINTF("Clearing transaction for manual response"); + LOG_DBG("Clearing transaction for manual response"); coap_clear_transaction(transaction); } else { coap_message_type_t reply_type = COAP_TYPE_ACK; - PRINTF("ERROR %u: %s\n", coap_status_code, coap_error_message); + LOG_WARN("ERROR %u: %s\n", coap_status_code, coap_error_message); coap_clear_transaction(transaction); if(coap_status_code == PING_RESPONSE) { @@ -368,12 +359,12 @@ coap_engine_init(void) { /* avoid initializing twice */ if(is_initialized) { - PRINTF("CoAP engine process already running - double initialization?\n"); + LOG_DBG("already running - double initialization?\n"); return; } is_initialized = 1; - PRINTF("Starting CoAP engine...\n"); + LOG_INFO("Starting CoAP engine...\n"); list_init(coap_handlers); list_init(coap_resource_services); @@ -401,14 +392,13 @@ coap_activate_resource(coap_resource_t *resource, const char *path) resource->url = path; list_add(coap_resource_services, resource); - PRINTF("Activating: %s\n", resource->url); + LOG_INFO("Activating: %s\n", resource->url); /* Only add periodic resources with a periodic_handler and a period > 0. */ if(resource->flags & IS_PERIODIC && resource->periodic && resource->periodic->periodic_handler && resource->periodic->period) { - PRINTF("Periodic resource: %p (%s)\n", resource->periodic, - resource->periodic->resource->url); + LOG_DBG("Periodic resource: %p (%s)\n", resource->periodic, path); list_add(coap_resource_periodic_services, resource->periodic); periodic = resource->periodic; coap_timer_set_callback(&periodic->periodic_timer, process_callback); @@ -459,8 +449,8 @@ invoke_coap_resource_service(coap_message_t *request, coap_message_t *response, coap_resource_flags_t method = coap_get_method_type(request); found = 1; - PRINTF("/%s, method %u, resource->flags %u\n", resource->url, - (uint16_t)method, resource->flags); + LOG_INFO("/%s, method %u, resource->flags %u\n", resource->url, + (uint16_t)method, resource->flags); if((method & METHOD_GET) && resource->get_handler != NULL) { /* call handler function */ @@ -502,8 +492,8 @@ process_callback(coap_timer_t *t) resource = coap_timer_get_user_data(t); if(resource != NULL && (resource->flags & IS_PERIODIC) && resource->periodic != NULL && resource->periodic->period) { - PRINTF("Periodic: timer expired for /%s (period: %lu)\n", - resource->url, resource->periodic->period); + LOG_DBG("Periodic: timer expired for /%s (period: %"PRIu32")\n", + resource->url, resource->periodic->period); if(!is_initialized) { /* CoAP has not yet been initialized. */ diff --git a/os/net/app-layer/coap/coap-log.c b/os/net/app-layer/coap/coap-log.c new file mode 100644 index 000000000..083c9aaf4 --- /dev/null +++ b/os/net/app-layer/coap/coap-log.c @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2017, RISE SICS. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/** + * \file + * Log support for CoAP + * \author + * Niclas Finne + * Joakim Eriksson + */ + +#include "coap-log.h" +/*---------------------------------------------------------------------------*/ +void +coap_log_string(const char *text, size_t len) +{ + int i; + if(text == NULL) { + LOG_OUTPUT("(NULL STR)"); + return; + } + + for(i = 0; i < len && *text != '\0'; i++, text++) { + LOG_OUTPUT("%c", *text); + } +} +/*---------------------------------------------------------------------------*/ diff --git a/os/net/app-layer/coap/coap-log.h b/os/net/app-layer/coap/coap-log.h new file mode 100644 index 000000000..79457bbe4 --- /dev/null +++ b/os/net/app-layer/coap/coap-log.h @@ -0,0 +1,78 @@ +/* + * Copyright (c) 2017, RISE SICS. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/** + * \file + * Log support for CoAP + * \author + * Niclas Finne + * Joakim Eriksson + */ + +#ifndef COAP_LOG_H_ +#define COAP_LOG_H_ + +#include "contiki.h" + +#ifdef COAP_LOG_CONF_PATH +#include COAP_LOG_CONF_PATH +#else /* COAP_LOG_CONF_PATH */ +#include "sys/log.h" +#endif /* COAP_LOG_CONF_PATH */ + +#include "coap-endpoint.h" + +/* CoAP endpoint */ +#define LOG_COAP_EP(level, endpoint) do { \ + if(level <= (LOG_LEVEL)) { \ + coap_endpoint_log(endpoint); \ + } \ + } while (0) + +#define LOG_ERR_COAP_EP(endpoint) LOG_COAP_EP(LOG_LEVEL_ERR, endpoint) +#define LOG_WARN_COAP_EP(endpoint) LOG_COAP_EP(LOG_LEVEL_WARN, endpoint) +#define LOG_INFO_COAP_EP(endpoint) LOG_COAP_EP(LOG_LEVEL_INFO, endpoint) +#define LOG_DBG_COAP_EP(endpoint) LOG_COAP_EP(LOG_LEVEL_DBG, endpoint) + +/* CoAP strings */ +#define LOG_COAP_STRING(level, text, len) do { \ + if(level <= (LOG_LEVEL)) { \ + coap_log_string(text, len); \ + } \ + } while (0) + +#define LOG_ERR_COAP_STRING(text, len) LOG_COAP_STRING(LOG_LEVEL_ERR, text, len) +#define LOG_WARN_COAP_STRING(text, len) LOG_COAP_STRING(LOG_LEVEL_WARN, text, len) +#define LOG_INFO_COAP_STRING(text, len) LOG_COAP_STRING(LOG_LEVEL_INFO, text, len) +#define LOG_DBG_COAP_STRING(text, len) LOG_COAP_STRING(LOG_LEVEL_DBG, text, len) + +void coap_log_string(const char *text, size_t len); + +#endif /* COAP_LOG_H_ */ diff --git a/os/net/app-layer/coap/coap-observe-client.c b/os/net/app-layer/coap/coap-observe-client.c index 93ad47f12..f264663eb 100644 --- a/os/net/app-layer/coap/coap-observe-client.c +++ b/os/net/app-layer/coap/coap-observe-client.c @@ -48,14 +48,10 @@ /* Compile this code only if client-side support for CoAP Observe is required */ #if COAP_OBSERVE_CLIENT -#define DEBUG 0 -#if DEBUG -#define PRINTF(...) printf(__VA_ARGS__) -#define PRINTEP(ep) coap_endpoint_print(ep) -#else -#define PRINTF(...) -#define PRINTEP(ep) -#endif +/* Log configuration */ +#include "coap-log.h" +#define LOG_MODULE "coap-observe-client" +#define LOG_LEVEL LOG_LEVEL_COAP MEMB(obs_subjects_memb, coap_observee_t, COAP_MAX_OBSERVEES); LIST(obs_subjects_list); @@ -97,8 +93,8 @@ coap_obs_add_observee(const coap_endpoint_t *endpoint, /* o->last_mid = 0; */ o->notification_callback = notification_callback; o->data = data; - PRINTF("Adding obs_subject for /%s [0x%02X%02X]\n", o->url, o->token[0], - o->token[1]); + LOG_DBG("Adding obs_subject for /%s [0x%02X%02X]\n", o->url, o->token[0], + o->token[1]); list_add(obs_subjects_list, o); } @@ -108,8 +104,8 @@ coap_obs_add_observee(const coap_endpoint_t *endpoint, void coap_obs_remove_observee(coap_observee_t *o) { - PRINTF("Removing obs_subject for /%s [0x%02X%02X]\n", o->url, o->token[0], - o->token[1]); + LOG_DBG("Removing obs_subject for /%s [0x%02X%02X]\n", o->url, o->token[0], + o->token[1]); memb_free(&obs_subjects_memb, o); list_remove(obs_subjects_list, o); } @@ -121,7 +117,7 @@ coap_get_obs_subject_by_token(const uint8_t *token, size_t token_len) for(obs = (coap_observee_t *)list_head(obs_subjects_list); obs; obs = obs->next) { - PRINTF("Looking for token 0x%02X%02X\n", token[0], token[1]); + LOG_DBG("Looking for token 0x%02X%02X\n", token[0], token[1]); if(obs->token_len == token_len && memcmp(obs->token, token, token_len) == 0) { return obs; @@ -140,7 +136,7 @@ coap_obs_remove_observee_by_token(const coap_endpoint_t *endpoint, for(obs = (coap_observee_t *)list_head(obs_subjects_list); obs; obs = obs->next) { - PRINTF("Remove check Token 0x%02X%02X\n", token[0], token[1]); + LOG_DBG("Remove check Token 0x%02X%02X\n", token[0], token[1]); if(coap_endpoint_cmp(&obs->endpoint, endpoint) && obs->token_len == token_len && memcmp(obs->token, token, token_len) == 0) { @@ -160,7 +156,7 @@ coap_obs_remove_observee_by_url(const coap_endpoint_t *endpoint, for(obs = (coap_observee_t *)list_head(obs_subjects_list); obs; obs = obs->next) { - PRINTF("Remove check URL %s\n", url); + LOG_DBG("Remove check URL %s\n", url); if(coap_endpoint_cmp(&obs->endpoint, endpoint) && (obs->url == url || memcmp(obs->url, url, strlen(obs->url)) == 0)) { coap_obs_remove_observee(obs); @@ -186,16 +182,16 @@ static coap_notification_flag_t classify_notification(coap_message_t *response, int first) { if(!response) { - PRINTF("no response\n"); + LOG_DBG("no response\n"); return NO_REPLY_FROM_SERVER; } - PRINTF("server replied\n"); + LOG_DBG("server replied\n"); if(!IS_RESPONSE_CODE_2_XX(response)) { - PRINTF("error response code\n"); + LOG_DBG("error response code\n"); return ERROR_RESPONSE_CODE; } if(!coap_is_option(response, COAP_OPTION_OBSERVE)) { - PRINTF("server does not support observe\n"); + LOG_DBG("server does not support observe\n"); return OBSERVE_NOT_SUPPORTED; } if(first) { @@ -214,19 +210,19 @@ coap_handle_notification(const coap_endpoint_t *endpoint, coap_notification_flag_t flag; uint32_t observe; - PRINTF("coap_handle_notification()\n"); + LOG_DBG("coap_handle_notification()\n"); token_len = get_token(notification, &token); - PRINTF("Getting token\n"); + LOG_DBG("Getting token\n"); if(0 == token_len) { - PRINTF("Error while handling coap observe notification: " - "no token in message\n"); + LOG_DBG("Error while handling coap observe notification: " + "no token in message\n"); return; } - PRINTF("Getting observee info\n"); + LOG_DBG("Getting observee info\n"); obs = coap_get_obs_subject_by_token(token, token_len); if(NULL == obs) { - PRINTF("Error while handling coap observe notification: " - "no matching token found\n"); + LOG_DBG("Error while handling coap observe notification: " + "no matching token found\n"); simple_reply(COAP_TYPE_RST, endpoint, notification); return; } @@ -240,7 +236,7 @@ coap_handle_notification(const coap_endpoint_t *endpoint, if(flag == NOTIFICATION_OK) { coap_get_header_observe(notification, &observe); if(observe == obs->last_observe) { - PRINTF("Discarding duplicate\n"); + LOG_DBG("Discarding duplicate\n"); return; } obs->last_observe = observe; @@ -256,7 +252,7 @@ handle_obs_registration_response(void *data, coap_message_t *response) notification_callback_t notification_callback; coap_notification_flag_t flag; - PRINTF("handle_obs_registration_response(): "); + LOG_DBG("handle_obs_registration_response()\n"); obs = (coap_observee_t *)data; notification_callback = obs->notification_callback; flag = classify_notification(response, 1); @@ -306,11 +302,11 @@ coap_obs_request_registration(const coap_endpoint_t *endpoint, char *uri, t->message_len = coap_serialize_message(request, t->message); coap_send_transaction(t); } else { - PRINTF("Could not allocate obs_subject resource buffer"); + LOG_DBG("Could not allocate obs_subject resource buffer\n"); coap_clear_transaction(t); } } else { - PRINTF("Could not allocate transaction buffer"); + LOG_DBG("Could not allocate transaction buffer\n"); } return obs; } diff --git a/os/net/app-layer/coap/coap-observe.c b/os/net/app-layer/coap/coap-observe.c index dea8414ef..ae3fe843f 100644 --- a/os/net/app-layer/coap/coap-observe.c +++ b/os/net/app-layer/coap/coap-observe.c @@ -43,15 +43,10 @@ #include "lib/memb.h" #include "lib/list.h" -#define DEBUG 0 -#if DEBUG -#include -#define PRINTF(...) printf(__VA_ARGS__) -#define PRINTEP(ep) coap_endpoint_print(ep) -#else -#define PRINTF(...) -#define PRINTEP(ep) -#endif +/* Log configuration */ +#include "coap-log.h" +#define LOG_MODULE "coap-observe" +#define LOG_LEVEL LOG_LEVEL_COAP /*---------------------------------------------------------------------------*/ MEMB(observers_memb, coap_observer_t, COAP_MAX_OBSERVERS); @@ -80,9 +75,9 @@ add_observer(const coap_endpoint_t *endpoint, const uint8_t *token, memcpy(o->token, token, token_len); o->last_mid = 0; - PRINTF("Adding observer (%u/%u) for /%s [0x%02X%02X]\n", - list_length(observers_list) + 1, COAP_MAX_OBSERVERS, - o->url, o->token[0], o->token[1]); + LOG_INFO("Adding observer (%u/%u) for /%s [0x%02X%02X]\n", + list_length(observers_list) + 1, COAP_MAX_OBSERVERS, + o->url, o->token[0], o->token[1]); list_add(observers_list, o); } @@ -94,8 +89,8 @@ add_observer(const coap_endpoint_t *endpoint, const uint8_t *token, void coap_remove_observer(coap_observer_t *o) { - PRINTF("Removing observer for /%s [0x%02X%02X]\n", o->url, o->token[0], - o->token[1]); + LOG_INFO("Removing observer for /%s [0x%02X%02X]\n", o->url, o->token[0], + o->token[1]); memb_free(&observers_memb, o); list_remove(observers_list, o); @@ -107,9 +102,9 @@ coap_remove_observer_by_client(const coap_endpoint_t *endpoint) int removed = 0; coap_observer_t *obs = NULL; - PRINTF("Remove check client "); - PRINTEP(endpoint); - PRINTF("\n"); + LOG_DBG("Remove check client "); + LOG_DBG_COAP_EP(endpoint); + LOG_DBG_("\n"); for(obs = (coap_observer_t *)list_head(observers_list); obs; obs = obs->next) { if(coap_endpoint_cmp(&obs->endpoint, endpoint)) { @@ -129,7 +124,7 @@ coap_remove_observer_by_token(const coap_endpoint_t *endpoint, for(obs = (coap_observer_t *)list_head(observers_list); obs; obs = obs->next) { - PRINTF("Remove check Token 0x%02X%02X\n", token[0], token[1]); + LOG_DBG("Remove check Token 0x%02X%02X\n", token[0], token[1]); if(coap_endpoint_cmp(&obs->endpoint, endpoint) && obs->token_len == token_len && memcmp(obs->token, token, token_len) == 0) { @@ -149,7 +144,7 @@ coap_remove_observer_by_uri(const coap_endpoint_t *endpoint, for(obs = (coap_observer_t *)list_head(observers_list); obs; obs = obs->next) { - PRINTF("Remove check URL %p\n", uri); + LOG_DBG("Remove check URL %p\n", uri); if((endpoint == NULL || (coap_endpoint_cmp(&obs->endpoint, endpoint))) && (obs->url == uri || memcmp(obs->url, uri, strlen(obs->url)) == 0)) { @@ -168,7 +163,7 @@ coap_remove_observer_by_mid(const coap_endpoint_t *endpoint, uint16_t mid) for(obs = (coap_observer_t *)list_head(observers_list); obs; obs = obs->next) { - PRINTF("Remove check MID %u\n", mid); + LOG_DBG("Remove check MID %u\n", mid); if(coap_endpoint_cmp(&obs->endpoint, endpoint) && obs->last_mid == mid) { coap_remove_observer(obs); @@ -214,7 +209,7 @@ coap_notify_observers_sub(coap_resource_t *resource, const char *subpath) /* Ensure url is null terminated because strncpy does not guarantee this */ url[COAP_OBSERVER_URL_LEN - 1] = '\0'; /* url now contains the notify URL that needs to match the observer */ - PRINTF("Observe: Notification from %s\n", url); + LOG_INFO("Notification from %s\n", url); coap_init_message(notification, COAP_TYPE_NON, CONTENT_2_05, 0); /* create a "fake" request for the URI */ @@ -245,13 +240,13 @@ coap_notify_observers_sub(coap_resource_t *resource, const char *subpath) if((transaction = coap_new_transaction(coap_get_mid(), &obs->endpoint))) { if(obs->obs_counter % COAP_OBSERVE_REFRESH_INTERVAL == 0) { - PRINTF(" Force Confirmable for\n"); + LOG_DBG(" Force Confirmable for\n"); notification->type = COAP_TYPE_CON; } - PRINTF(" Observer "); - PRINTEP(&obs->endpoint); - PRINTF("\n"); + LOG_DBG(" Observer "); + LOG_DBG_COAP_EP(&obs->endpoint); + LOG_DBG_("\n"); /* update last MID for RST matching */ obs->last_mid = transaction->mid; @@ -263,7 +258,7 @@ coap_notify_observers_sub(coap_resource_t *resource, const char *subpath) if(coap_call_handlers(request, notification, transaction->message + COAP_MAX_HEADER_SIZE, COAP_MAX_CHUNK_SIZE, NULL) > 0) { - PRINTF("Notification on new handlers\n"); + LOG_DBG("Notification on new handlers\n"); } else { if(resource != NULL) { resource->get_handler(request, notification, @@ -298,7 +293,7 @@ coap_observe_handler(coap_resource_t *resource, coap_message_t *coap_req, const coap_endpoint_t *src_ep; coap_observer_t *obs; - PRINTF("CoAP observer handler rsc: %d\n", resource != NULL); + LOG_DBG("CoAP observer handler rsc: %d\n", resource != NULL); if(coap_req->code == COAP_GET && coap_res->code < 128) { /* GET request and response without error code */ if(coap_is_option(coap_req, COAP_OPTION_OBSERVE)) { diff --git a/os/net/app-layer/coap/coap-res-well-known-core.c b/os/net/app-layer/coap/coap-res-well-known-core.c index 248271434..752cfa90e 100644 --- a/os/net/app-layer/coap/coap-res-well-known-core.c +++ b/os/net/app-layer/coap/coap-res-well-known-core.c @@ -40,13 +40,10 @@ #include #include -#define DEBUG 0 -#if DEBUG -#include -#define PRINTF(...) printf(__VA_ARGS__) -#else -#define PRINTF(...) -#endif +/* Log configuration */ +#include "coap-log.h" +#define LOG_MODULE "coap-res-well-known-core" +#define LOG_LEVEL LOG_LEVEL_COAP #define ADD_CHAR_IF_POSSIBLE(char) \ if(strpos >= *offset && bufpos < preferred_size) { \ @@ -64,7 +61,7 @@ + (*offset - (int32_t)strpos > 0 ? \ *offset - (int32_t)strpos : 0)); \ if(bufpos op preferred_size) { \ - PRINTF("res: BREAK at %s (%p)\n", string, resource); \ + LOG_DBG("BREAK at %s (%p)\n", string, resource); \ break; \ } \ } \ @@ -99,7 +96,9 @@ well_known_core_get_handler(coap_message_t *request, coap_message_t *response, ++value; len -= strlen(filter) + 1; - PRINTF("Filter %s = %.*s\n", filter, len, value); + LOG_DBG("Filter %s = ", filter); + LOG_DBG_COAP_STRING(value, len); + LOG_DBG_("\n"); if(strcmp(filter, "href") == 0 && value[0] == '/') { ++value; @@ -109,7 +108,7 @@ well_known_core_get_handler(coap_message_t *request, coap_message_t *response, lastchar = value[len - 1]; value[len - 1] = '\0'; } -#endif +#endif /* COAP_LINK_FORMAT_FILTERING */ for(resource = coap_get_first_resource(); resource; resource = coap_get_next_resource(resource)) { @@ -133,7 +132,7 @@ well_known_core_get_handler(coap_message_t *request, coap_message_t *response, end = strchr(attrib, '"'); } - PRINTF("Filter: res has attrib %s (%s)\n", attrib, value); + LOG_DBG("Filter: res has attrib %s (%s)\n", attrib, value); found = attrib; while((found = strstr(found, value)) != NULL) { if(found > end) { @@ -148,17 +147,17 @@ well_known_core_get_handler(coap_message_t *request, coap_message_t *response, if(found == NULL) { continue; } - PRINTF("Filter: res has prefix %s\n", found); + LOG_DBG("Filter: res has prefix %s\n", found); if(lastchar != '*' && (found[len] != '"' && found[len] != ' ' && found[len] != '\0')) { continue; } - PRINTF("Filter: res has match\n"); + LOG_DBG("Filter: res has match\n"); } #endif - PRINTF("res: /%s (%p)\npos: s%zu, o%ld, b%zu\n", resource->url, resource, - strpos, (long)*offset, bufpos); + LOG_DBG("/%s (%p)\npos: s%zu, o%ld, b%zu\n", resource->url, resource, + strpos, (long)*offset, bufpos); if(strpos > 0) { ADD_CHAR_IF_POSSIBLE(','); @@ -175,28 +174,30 @@ well_known_core_get_handler(coap_message_t *request, coap_message_t *response, /* buffer full, but resource not completed yet; or: do not break if resource exactly fills buffer. */ if(bufpos > preferred_size && strpos - bufpos > *offset) { - PRINTF("res: BREAK at %s (%p)\n", resource->url, resource); + LOG_DBG("BREAK at %s (%p)\n", resource->url, resource); break; } } if(bufpos > 0) { - PRINTF("BUF %zu: %.*s\n", bufpos, (int)bufpos, (char *)buffer); + LOG_DBG("BUF %zu: ", bufpos); + LOG_DBG_COAP_STRING((char *)buffer, bufpos); + LOG_DBG_("\n"); coap_set_payload(response, buffer, bufpos); coap_set_header_content_format(response, APPLICATION_LINK_FORMAT); } else if(strpos > 0) { - PRINTF("well_known_core_handler(): bufpos<=0\n"); + LOG_DBG("well_known_core_handler(): bufpos<=0\n"); coap_set_status_code(response, BAD_OPTION_4_02); coap_set_payload(response, "BlockOutOfScope", 15); } if(resource == NULL) { - PRINTF("res: DONE\n"); + LOG_DBG("DONE\n"); *offset = -1; } else { - PRINTF("res: MORE at %s (%p)\n", resource->url, resource); + LOG_DBG("MORE at %s (%p)\n", resource->url, resource); *offset += preferred_size; } } diff --git a/os/net/app-layer/coap/coap-separate.c b/os/net/app-layer/coap/coap-separate.c index ea2f11261..15f20eefa 100644 --- a/os/net/app-layer/coap/coap-separate.c +++ b/os/net/app-layer/coap/coap-separate.c @@ -42,13 +42,10 @@ #include "sys/cc.h" #include -#define DEBUG 0 -#if DEBUG -#include -#define PRINTF(...) printf(__VA_ARGS__) -#else -#define PRINTF(...) -#endif +/* Log configuration */ +#include "coap-log.h" +#define LOG_MODULE "coap-separate" +#define LOG_LEVEL LOG_LEVEL_COAP /*---------------------------------------------------------------------------*/ /*- Separate Response API ---------------------------------------------------*/ @@ -85,8 +82,9 @@ coap_separate_accept(coap_message_t *coap_req, coap_separate_t *separate_store) { coap_transaction_t *const t = coap_get_transaction_by_mid(coap_req->mid); - PRINTF("Separate ACCEPT: /%.*s MID %u\n", (int)coap_req->uri_path_len, - coap_req->uri_path, coap_req->mid); + LOG_DBG("Separate ACCEPT: /"); + LOG_DBG_COAP_STRING(coap_req->uri_path, coap_req->uri_path_len); + LOG_DBG_(" MID %u\n", coap_req->mid); if(t) { /* send separate ACK for CON */ if(coap_req->type == COAP_TYPE_CON) { @@ -95,7 +93,7 @@ coap_separate_accept(coap_message_t *coap_req, coap_separate_t *separate_store) ep = coap_get_src_endpoint(coap_req); if(ep == NULL) { - PRINTF("ERROR: no endpoint in request\n"); + LOG_ERR("ERROR: no endpoint in request\n"); } else { /* ACK with empty code (0) */ coap_init_message(ack, COAP_TYPE_ACK, 0, coap_req->mid); @@ -125,7 +123,7 @@ coap_separate_accept(coap_message_t *coap_req, coap_separate_t *separate_store) /* signal the engine to skip automatic response and clear transaction by engine */ coap_status_code = MANUAL_RESPONSE; } else { - PRINTF("ERROR: Response transaction for separate request not found!\n"); + LOG_ERR("ERROR: Response transaction for separate request not found!\n"); coap_status_code = INTERNAL_SERVER_ERROR_5_00; } } diff --git a/os/net/app-layer/coap/coap-timer-default.c b/os/net/app-layer/coap/coap-timer-default.c index 707d6367d..f0323dfee 100644 --- a/os/net/app-layer/coap/coap-timer-default.c +++ b/os/net/app-layer/coap/coap-timer-default.c @@ -40,13 +40,10 @@ #include "sys/etimer.h" #include "sys/process.h" -#define DEBUG 0 -#if DEBUG -#include -#define PRINTF(...) printf(__VA_ARGS__) -#else -#define PRINTF(...) -#endif +/* Log configuration */ +#include "coap-log.h" +#define LOG_MODULE "coap-timer-default" +#define LOG_LEVEL LOG_LEVEL_NONE PROCESS(coap_timer_process, "coap timer process"); @@ -58,7 +55,7 @@ update_timer(void) { uint64_t remaining; remaining = coap_timer_time_to_next_expiration(); - PRINTF("coap-timer-default: remaining %lu msec\n", (unsigned long)remaining); + LOG_DBG("remaining %lu msec\n", (unsigned long)remaining); if(remaining == 0) { /* Run as soon as possible */ process_poll(&coap_timer_process); diff --git a/os/net/app-layer/coap/coap-timer.c b/os/net/app-layer/coap/coap-timer.c index 6e2b15616..30583d33b 100644 --- a/os/net/app-layer/coap/coap-timer.c +++ b/os/net/app-layer/coap/coap-timer.c @@ -38,13 +38,10 @@ #include "coap-timer.h" #include "lib/list.h" -#define DEBUG 0 -#if DEBUG -#include -#define PRINTF(...) printf(__VA_ARGS__) -#else -#define PRINTF(...) -#endif +/* Log configuration */ +#include "coap-log.h" +#define LOG_MODULE "coap-timer" +#define LOG_LEVEL LOG_LEVEL_NONE #ifndef NULL #define NULL 0 @@ -63,8 +60,8 @@ add_timer(coap_timer_t *timer) coap_timer_init(); } - PRINTF("coap-timer: adding timer %p at %lu\n", timer, - (unsigned long)timer->expiration_time); + LOG_DBG("adding timer %p at %lu\n", timer, + (unsigned long)timer->expiration_time); p = list_head(timer_list); @@ -92,7 +89,7 @@ add_timer(coap_timer_t *timer) void coap_timer_stop(coap_timer_t *timer) { - PRINTF("coap-timer: stopping timer %p\n", timer); + LOG_DBG("stopping timer %p\n", timer); /* Mark timer as expired right now */ timer->expiration_time = coap_timer_uptime(); @@ -150,8 +147,7 @@ coap_timer_run(void) } if(next->expiration_time <= now) { - PRINTF("coap-timer: timer %p expired at %lu\n", next, - (unsigned long)now); + LOG_DBG("timer %p expired at %lu\n", next, (unsigned long)now); /* This timer should expire now */ list_remove(timer_list, next); diff --git a/os/net/app-layer/coap/coap-transactions.c b/os/net/app-layer/coap/coap-transactions.c index 73efa0650..b10d5a31a 100644 --- a/os/net/app-layer/coap/coap-transactions.c +++ b/os/net/app-layer/coap/coap-transactions.c @@ -43,15 +43,10 @@ #include "lib/list.h" #include -#define DEBUG 0 -#if DEBUG -#include -#define PRINTF(...) printf(__VA_ARGS__) -#define PRINTEP(ep) coap_endpoint_print(ep) -#else -#define PRINTF(...) -#define PRINTEP(ep) -#endif +/* Log configuration */ +#include "coap-log.h" +#define LOG_MODULE "coap-transactions" +#define LOG_LEVEL LOG_LEVEL_COAP /*---------------------------------------------------------------------------*/ MEMB(transactions_memb, coap_transaction_t, COAP_MAX_OPEN_TRANSACTIONS); @@ -63,11 +58,11 @@ coap_retransmit_transaction(coap_timer_t *nt) { coap_transaction_t *t = coap_timer_get_user_data(nt); if(t == NULL) { - PRINTF("No retransmission data in coap_timer!\n"); + LOG_DBG("No retransmission data in coap_timer!\n"); return; } ++(t->retrans_counter); - PRINTF("Retransmitting %u (%u)\n", t->mid, t->retrans_counter); + LOG_DBG("Retransmitting %u (%u)\n", t->mid, t->retrans_counter); coap_send_transaction(t); } /*---------------------------------------------------------------------------*/ @@ -96,7 +91,7 @@ coap_new_transaction(uint16_t mid, const coap_endpoint_t *endpoint) void coap_send_transaction(coap_transaction_t *t) { - PRINTF("Sending transaction %u\n", t->mid); + LOG_DBG("Sending transaction %u\n", t->mid); coap_sendto(&t->endpoint, t->message, t->message_len); @@ -104,7 +99,7 @@ coap_send_transaction(coap_transaction_t *t) ((COAP_HEADER_TYPE_MASK & t->message[0]) >> COAP_HEADER_TYPE_POSITION)) { if(t->retrans_counter < COAP_MAX_RETRANSMIT) { /* not timed out yet */ - PRINTF("Keeping transaction %u\n", t->mid); + LOG_DBG("Keeping transaction %u\n", t->mid); if(t->retrans_counter == 0) { coap_timer_set_callback(&t->retrans_timer, coap_retransmit_transaction); @@ -112,19 +107,19 @@ coap_send_transaction(coap_transaction_t *t) t->retrans_interval = COAP_RESPONSE_TIMEOUT_TICKS + (rand() % COAP_RESPONSE_TIMEOUT_BACKOFF_MASK); - PRINTF("Initial interval %lu msec\n", - (unsigned long)t->retrans_interval); + LOG_DBG("Initial interval %lu msec\n", + (unsigned long)t->retrans_interval); } else { t->retrans_interval <<= 1; /* double */ - PRINTF("Doubled (%u) interval %d s\n", t->retrans_counter, - t->retrans_interval / 1000); + LOG_DBG("Doubled (%u) interval %lu s\n", t->retrans_counter, + (unsigned long)(t->retrans_interval / 1000)); } /* interval updated above */ coap_timer_set(&t->retrans_timer, t->retrans_interval); } else { /* timed out */ - PRINTF("Timeout\n"); + LOG_DBG("Timeout\n"); coap_resource_response_handler_t callback = t->callback; void *callback_data = t->callback_data; @@ -146,7 +141,7 @@ void coap_clear_transaction(coap_transaction_t *t) { if(t) { - PRINTF("Freeing transaction %u: %p\n", t->mid, t); + LOG_DBG("Freeing transaction %u: %p\n", t->mid, t); coap_timer_stop(&t->retrans_timer); list_remove(transactions_list, t); @@ -161,7 +156,7 @@ coap_get_transaction_by_mid(uint16_t mid) for(t = (coap_transaction_t *)list_head(transactions_list); t; t = t->next) { if(t->mid == mid) { - PRINTF("Found transaction for MID %u: %p\n", t->mid, t); + LOG_DBG("Found transaction for MID %u: %p\n", t->mid, t); return t; } } diff --git a/os/net/app-layer/coap/coap-uip.c b/os/net/app-layer/coap/coap-uip.c index 5ea982d93..2468b6f47 100644 --- a/os/net/app-layer/coap/coap-uip.c +++ b/os/net/app-layer/coap/coap-uip.c @@ -36,7 +36,6 @@ */ #include "contiki.h" -#include "sys/cc.h" #include "net/ipv6/uip-udp-packet.h" #include "net/ipv6/uiplib.h" #include "coap.h" @@ -48,21 +47,14 @@ #include "coap-keystore.h" #include "coap-keystore-simple.h" - #if UIP_CONF_IPV6_RPL #include "rpl.h" #endif /* UIP_CONF_IPV6_RPL */ -#define DEBUG DEBUG_FULL -#include "net/ipv6/uip-debug.h" - -#define DEBUG_VERBOSE ((DEBUG) && 0) - -#if DEBUG -#define PRINTEP(X) coap_endpoint_print(X) -#else -#define PRINTEP(X) -#endif +/* Log configuration */ +#include "coap-log.h" +#define LOG_MODULE "coap-uip" +#define LOG_LEVEL LOG_LEVEL_COAP #ifdef WITH_DTLS #include "tinydtls.h" @@ -70,7 +62,6 @@ #include "dtls_debug.h" #endif /* WITH_DTLS */ - /* sanity check for configured values */ #if COAP_MAX_PACKET_SIZE > (UIP_BUFSIZE - UIP_IPH_LEN - UIP_UDPH_LEN) #error "UIP_CONF_BUFFER_SIZE too small for COAP_MAX_CHUNK_SIZE" @@ -99,20 +90,67 @@ PROCESS(coap_engine, "CoAP Engine"); static struct uip_udp_conn *udp_conn = NULL; +/*---------------------------------------------------------------------------*/ +void +coap_endpoint_log(const coap_endpoint_t *ep) +{ + if(ep == NULL) { + LOG_OUTPUT("(NULL EP)"); + return; + } + if(ep->secure) { + LOG_OUTPUT("coaps://["); + } else { + LOG_OUTPUT("coap://["); + } + log_6addr(&ep->ipaddr); + LOG_OUTPUT("]:%u", uip_ntohs(ep->port)); +} /*---------------------------------------------------------------------------*/ void coap_endpoint_print(const coap_endpoint_t *ep) { - if(ep->secure) { - printf("coaps:"); - } else { - printf("coap:"); + if(ep == NULL) { + printf("(NULL EP)"); + return; } - printf("//["); - uip_debug_ipaddr_print(&ep->ipaddr); + if(ep->secure) { + printf("coaps://["); + } else { + printf("coap://["); + } + uiplib_ipaddr_print(&ep->ipaddr); printf("]:%u", uip_ntohs(ep->port)); } /*---------------------------------------------------------------------------*/ +int +coap_endpoint_snprint(char *buf, size_t size, const coap_endpoint_t *ep) +{ + int n; + if(buf == NULL || size == 0) { + return 0; + } + if(ep == NULL) { + n = snprintf(buf, size - 1, "(NULL EP)"); + } else { + if(ep->secure) { + n = snprintf(buf, size - 1, "coaps://["); + } else { + n = snprintf(buf, size - 1, "coap://["); + } + if(n < size - 1) { + n += uiplib_ipaddr_snprint(&buf[n], size - n - 1, &ep->ipaddr); + } + if(n < size - 1) { + n += snprintf(&buf[n], size -n - 1, "]:%u", uip_ntohs(ep->port)); + } + } + if(n >= size - 1) { + buf[size - 1] = '\0'; + } + return n; +} +/*---------------------------------------------------------------------------*/ void coap_endpoint_copy(coap_endpoint_t *destination, const coap_endpoint_t *from) @@ -178,7 +216,7 @@ coap_endpoint_parse(const char *text, size_t size, coap_endpoint_t *ep) * Secure CoAP should use a different port but for now * the same port is used. */ - PRINTF("Using secure port (coaps)\n"); + LOG_DBG("Using secure port (coaps)\n"); ep->port = SERVER_LISTEN_SECURE_PORT; ep->secure = 1; } else { @@ -232,14 +270,15 @@ coap_endpoint_is_connected(const coap_endpoint_t *ep) peer = dtls_get_peer(dtls_context, ep); if(peer != NULL) { /* only if handshake is done! */ - PRINTF("DTLS peer state for "); - PRINTEP(ep); - PRINTF(" is %d %d\n", peer->state, dtls_peer_is_connected(peer)); + LOG_DBG("DTLS peer state for "); + LOG_DBG_COAP_EP(ep); + LOG_DBG_(" is %d %d\n", peer->state, dtls_peer_is_connected(peer)); return dtls_peer_is_connected(peer); } else { - PRINTF("DTLS did not find peer "); - PRINTEP(ep); - PRINTF("\n"); + LOG_DBG("DTLS did not find peer "); + LOG_DBG_COAP_EP(ep); + LOG_DBG_("\n"); + return 0; } } #endif /* WITH_DTLS */ @@ -252,16 +291,16 @@ int coap_endpoint_connect(coap_endpoint_t *ep) { if(ep->secure == 0) { - PRINTF("CoAP connect to "); - PRINTEP(ep); - PRINTF("\n"); + LOG_DBG("connect to "); + LOG_DBG_COAP_EP(ep); + LOG_DBG_("\n"); return 1; } #ifdef WITH_DTLS - PRINTF("DTLS connect to "); - PRINTEP(ep); - PRINTF("\n"); + LOG_DBG("DTLS connect to "); + LOG_DBG_COAP_EP(ep); + LOG_DBG_("\n"); /* setup all address info here... should be done to connect */ if(dtls_context) { @@ -314,10 +353,10 @@ coap_transport_init(void) static void process_secure_data(void) { - PRINTF("receiving secure UDP datagram from ["); - PRINT6ADDR(&UIP_IP_BUF->srcipaddr); - PRINTF("]:%u\n Length: %u\n", uip_ntohs(UIP_UDP_BUF->srcport), - uip_datalen()); + LOG_INFO("receiving secure UDP datagram from ["); + LOG_INFO_6ADDR(&UIP_IP_BUF->srcipaddr); + LOG_INFO_("]:%u\n Length: %u\n", uip_ntohs(UIP_UDP_BUF->srcport), + uip_datalen()); if(dtls_context) { dtls_handle_message(dtls_context, (coap_endpoint_t *)get_src_endpoint(1), @@ -329,10 +368,10 @@ process_secure_data(void) static void process_data(void) { - PRINTF("receiving UDP datagram from ["); - PRINT6ADDR(&UIP_IP_BUF->srcipaddr); - PRINTF("]:%u\n Length: %u\n", uip_ntohs(UIP_UDP_BUF->srcport), - uip_datalen()); + LOG_INFO("receiving UDP datagram from ["); + LOG_INFO_6ADDR(&UIP_IP_BUF->srcipaddr); + LOG_INFO_("]:%u\n Length: %u\n", uip_ntohs(UIP_UDP_BUF->srcport), + uip_datalen()); coap_receive(get_src_endpoint(0), uip_appdata, uip_datalen()); } @@ -341,14 +380,14 @@ int coap_sendto(const coap_endpoint_t *ep, const uint8_t *data, uint16_t length) { if(ep == NULL) { - PRINTF("coap-uip: failed to send - no endpoint\n"); + LOG_WARN("failed to send - no endpoint\n"); return -1; } if(!coap_endpoint_is_connected(ep)) { - PRINTF("coap-uip: endpoint "); - PRINTEP(ep); - PRINTF(" not connected - dropping packet\n"); + LOG_WARN("endpoint "); + LOG_WARN_COAP_EP(ep); + LOG_WARN_(" not connected - dropping packet\n"); return -1; } @@ -358,24 +397,25 @@ coap_sendto(const coap_endpoint_t *ep, const uint8_t *data, uint16_t length) int ret; ret = dtls_write(dtls_context, (session_t *)ep, (uint8_t *)data, length); - PRINTF("coap-uip: sent DTLS to "); - PRINTEP(ep); + LOG_INFO("sent DTLS to "); + LOG_INFO_COAP_EP(ep); if(ret < 0) { - PRINTF(" - error %d\n", ret); + LOG_INFO_(" - error %d\n", ret); } else { - PRINTF(" %d/%u bytes\n", ret, length); + LOG_INFO_(" %d/%u bytes\n", ret, length); } return ret; + } else { + LOG_WARN("no DTLS context\n"); + return -1; } - PRINTF("coap-uip: no DTLS context\n"); - return -1; } #endif /* WITH_DTLS */ uip_udp_packet_sendto(udp_conn, data, length, &ep->ipaddr, ep->port); - PRINTF("coap-uip: sent to "); - PRINTEP(ep); - PRINTF(" %u bytes\n", length); + LOG_INFO("sent to "); + LOG_INFO_COAP_EP(ep); + LOG_INFO_(" %u bytes\n", length); return length; } /*---------------------------------------------------------------------------*/ @@ -386,18 +426,18 @@ PROCESS_THREAD(coap_engine, ev, data) /* new connection with remote host */ udp_conn = udp_new(NULL, 0, NULL); udp_bind(udp_conn, SERVER_LISTEN_PORT); - PRINTF("Listening on port %u\n", uip_ntohs(udp_conn->lport)); + LOG_INFO("Listening on port %u\n", uip_ntohs(udp_conn->lport)); #ifdef WITH_DTLS /* create new context with app-data */ dtls_conn = udp_new(NULL, 0, NULL); if(dtls_conn != NULL) { udp_bind(dtls_conn, SERVER_LISTEN_SECURE_PORT); - PRINTF("DTLS listening on port %u\n", uip_ntohs(dtls_conn->lport)); + LOG_INFO("DTLS listening on port %u\n", uip_ntohs(dtls_conn->lport)); dtls_context = dtls_new_context(dtls_conn); } if(!dtls_context) { - PRINTF("DTLS: cannot create context\n"); + LOG_WARN("DTLS: cannot create context\n"); } else { dtls_set_handler(dtls_context, &cb); } @@ -432,19 +472,20 @@ static int input_from_peer(struct dtls_context_t *ctx, session_t *session, uint8_t *data, size_t len) { -#if DEBUG_VERBOSE size_t i; - PRINTF("received DTLS data:"); - for(i = 0; i < len; i++) { - PRINTF("%c", data[i]); + if(LOG_DBG_ENABLED) { + LOG_DBG("received DTLS data:"); + for(i = 0; i < len; i++) { + LOG_DBG_("%c", data[i]); + } + LOG_DBG_("\n"); + LOG_DBG("Hex:"); + for(i = 0; i < len; i++) { + LOG_DBG_("%02x", data[i]); + } + LOG_DBG_("\n"); } - PRINTF("\nHex:"); - for(i = 0; i < len; i++) { - PRINTF("%02x", data[i]); - } - PRINTF("\n"); -#endif /* DEBUG_VERBOSE */ /* Ensure that the endpoint is tagged as secure */ session->secure = 1; @@ -460,9 +501,9 @@ output_to_peer(struct dtls_context_t *ctx, session_t *session, uint8_t *data, size_t len) { struct uip_udp_conn *udp_connection = dtls_get_app_data(ctx); - PRINTF("coap-uip: output_to DTLS peer ["); - PRINT6ADDR(&session->ipaddr); - PRINTF("]:%u %d bytes\n", session->port, (int)len); + LOG_DBG("output_to DTLS peer ["); + LOG_DBG_6ADDR(&session->ipaddr); + LOG_DBG_("]:%u %d bytes\n", session->port, (int)len); uip_udp_packet_sendto(udp_connection, data, len, &session->ipaddr, session->port); return len; @@ -488,18 +529,20 @@ get_psk_info(struct dtls_context_t *ctx, coap_keystore_psk_entry_t ks; if(dtls_keystore == NULL) { - PRINTF("coap-uip: --- No key store available ---\n"); + LOG_DBG("--- No key store available ---\n"); return 0; } memset(&ks, 0, sizeof(ks)); - PRINTF("coap-uip: ---===>>> Getting the Key or ID <<<===---\n"); + LOG_DBG("---===>>> Getting the Key or ID <<<===---\n"); switch(type) { case DTLS_PSK_IDENTITY: if(id && id_len) { ks.identity_hint = id; ks.identity_hint_len = id_len; - PRINTF("coap-uip: got psk_identity_hint: '%.*s'\n", id_len, id); + LOG_DBG("got psk_identity_hint: '"); + LOG_DBG_COAP_STRING(id, id_len); + LOG_DBG_("'\n"); } if(dtls_keystore->coap_get_psk_info) { @@ -507,16 +550,16 @@ get_psk_info(struct dtls_context_t *ctx, dtls_keystore->coap_get_psk_info((coap_endpoint_t *)session, &ks); } if(ks.identity == NULL || ks.identity_len == 0) { - PRINTF("coap-uip: no psk_identity found\n"); + LOG_DBG("no psk_identity found\n"); return 0; } if(result_length < ks.identity_len) { - PRINTF("coap-uip: cannot return psk_identity -- buffer too small\n"); + LOG_DBG("cannot return psk_identity -- buffer too small\n"); return dtls_alert_fatal_create(DTLS_ALERT_INTERNAL_ERROR); } memcpy(result, ks.identity, ks.identity_len); - PRINTF("coap-uip: psk_identity with %u bytes found\n", ks.identity_len); + LOG_DBG("psk_identity with %u bytes found\n", ks.identity_len); return ks.identity_len; case DTLS_PSK_KEY: @@ -527,20 +570,20 @@ get_psk_info(struct dtls_context_t *ctx, dtls_keystore->coap_get_psk_info((coap_endpoint_t *)session, &ks); } if(ks.key == NULL || ks.key_len == 0) { - PRINTF("coap-uip: PSK for unknown id requested, exiting\n"); + LOG_DBG("PSK for unknown id requested, exiting\n"); return dtls_alert_fatal_create(DTLS_ALERT_ILLEGAL_PARAMETER); } if(result_length < ks.key_len) { - PRINTF("coap-uip: cannot return psk -- buffer too small\n"); + LOG_DBG("cannot return psk -- buffer too small\n"); return dtls_alert_fatal_create(DTLS_ALERT_INTERNAL_ERROR); } memcpy(result, ks.key, ks.key_len); - PRINTF("coap-uip: psk with %u bytes found\n", ks.key_len); + LOG_DBG("psk with %u bytes found\n", ks.key_len); return ks.key_len; default: - PRINTF("coap-uip: unsupported key store request type: %d\n", type); + LOG_WARN("unsupported key store request type: %d\n", type); } return dtls_alert_fatal_create(DTLS_ALERT_INTERNAL_ERROR); diff --git a/os/net/app-layer/coap/coap.c b/os/net/app-layer/coap/coap.c index ad6f68b3e..9e46eea5a 100644 --- a/os/net/app-layer/coap/coap.c +++ b/os/net/app-layer/coap/coap.c @@ -52,19 +52,10 @@ #include "coap.h" #include "coap-transactions.h" -#define DEBUG 0 -#if DEBUG -#include -#define PRINTF(...) printf(__VA_ARGS__) -#define PRINTS(l,s,f) do { int i; \ - for(i = 0; i < l; i++) printf(f, s[i]); \ - } while(0) -#define PRINTPRE(p,l,s) do { PRINTF(p);PRINTS(l,s,"%c"); } while(0); -#else -#define PRINTF(...) -#define PRINTS(l,s,f) -#define PRINTPRE(p,l,s) -#endif +/* Log configuration */ +#include "coap-log.h" +#define LOG_MODULE "coap" +#define LOG_LEVEL LOG_LEVEL_COAP /*---------------------------------------------------------------------------*/ /*- Variables ---------------------------------------------------------------*/ @@ -135,7 +126,7 @@ coap_set_option_header(unsigned int delta, size_t length, uint8_t *buffer) buffer[++written] = (length - 13); } - PRINTF("WRITTEN %zu B opt header\n", 1 + written); + LOG_DBG("WRITTEN %zu B opt header\n", 1 + written); return ++written; } @@ -158,8 +149,8 @@ coap_serialize_int_option(unsigned int number, unsigned int current_number, if(0xFFFFFFFF & value) { ++i; } - PRINTF("OPTION %u (delta %u, len %zu)\n", number, number - current_number, - i); + LOG_DBG("OPTION %u (delta %u, len %zu)\n", number, number - current_number, + i); i = coap_set_option_header(number - current_number, i, buffer); @@ -185,9 +176,9 @@ coap_serialize_array_option(unsigned int number, unsigned int current_number, { size_t i = 0; - PRINTF("ARRAY type %u, len %zu, full [", number, length); - PRINTS(length, array, "%c"); - PRINTF("]\n"); + LOG_DBG("ARRAY type %u, len %zu, full [", number, length); + LOG_DBG_COAP_STRING((const char *)array, length); + LOG_DBG_("]\n"); if(split_char != '\0') { int j; @@ -196,7 +187,7 @@ coap_serialize_array_option(unsigned int number, unsigned int current_number, size_t temp_length; for(j = 0; j <= length + 1; ++j) { - PRINTF("STEP %u/%zu (%c)\n", j, length, array[j]); + LOG_DBG("STEP %u/%zu (%c)\n", j, length, array[j]); if(array[j] == split_char || j == length) { part_end = array + j; temp_length = part_end - part_start; @@ -206,10 +197,10 @@ coap_serialize_array_option(unsigned int number, unsigned int current_number, memcpy(&buffer[i], part_start, temp_length); i += temp_length; - PRINTF("OPTION type %u, delta %u, len %zu, part [", number, - number - current_number, i); - PRINTS((int)temp_length, part_start, "%c"); - PRINTF("]\n"); + LOG_DBG("OPTION type %u, delta %u, len %zu, part [", number, + number - current_number, i); + LOG_DBG_COAP_STRING((const char *)part_start, temp_length); + LOG_DBG_("]\n"); ++j; /* skip the splitter */ current_number = number; part_start = array + j; @@ -220,8 +211,8 @@ coap_serialize_array_option(unsigned int number, unsigned int current_number, memcpy(&buffer[i], array, length); i += length; - PRINTF("OPTION type %u, delta %u, len %zu\n", number, - number - current_number, length); + LOG_DBG("OPTION type %u, delta %u, len %zu\n", number, + number - current_number, length); } return i; @@ -320,7 +311,7 @@ coap_serialize_message(coap_message_t *coap_pkt, uint8_t *buffer) coap_pkt->buffer = buffer; coap_pkt->version = 1; - PRINTF("-Serializing MID %u to %p, ", coap_pkt->mid, coap_pkt->buffer); + LOG_DBG("-Serializing MID %u to %p, ", coap_pkt->mid, coap_pkt->buffer); /* set header fields */ coap_pkt->buffer[0] = 0x00; @@ -336,25 +327,25 @@ coap_serialize_message(coap_message_t *coap_pkt, uint8_t *buffer) /* empty message, dont need to do more stuff */ if(!coap_pkt->code) { - PRINTF("-Done serializing empty message at %p-\n", coap_pkt->buffer); + LOG_DBG_("-Done serializing empty message at %p-\n", coap_pkt->buffer); return 4; } /* set Token */ - PRINTF("Token (len %u)", coap_pkt->token_len); + LOG_DBG_("Token (len %u)", coap_pkt->token_len); option = coap_pkt->buffer + COAP_HEADER_LEN; for(current_number = 0; current_number < coap_pkt->token_len; ++current_number) { - PRINTF(" %02X", coap_pkt->token[current_number]); + LOG_DBG_(" %02X", coap_pkt->token[current_number]); *option = coap_pkt->token[current_number]; ++option; } - PRINTF("-\n"); + LOG_DBG_("-\n"); /* Serialize options */ current_number = 0; - PRINTF("-Serializing options at %p-\n", option); + LOG_DBG("-Serializing options at %p-\n", option); /* The options must be serialized in the order of their number */ COAP_SERIALIZE_BYTE_OPTION(COAP_OPTION_IF_MATCH, if_match, "If-Match"); @@ -372,7 +363,7 @@ coap_serialize_message(coap_message_t *coap_pkt, uint8_t *buffer) "Location-Path"); COAP_SERIALIZE_STRING_OPTION(COAP_OPTION_URI_PATH, uri_path, '/', "Uri-Path"); - PRINTF("Serialize content format: %d\n", coap_pkt->content_format); + LOG_DBG("Serialize content format: %d\n", coap_pkt->content_format); COAP_SERIALIZE_INT_OPTION(COAP_OPTION_CONTENT_FORMAT, content_format, "Content-Format"); COAP_SERIALIZE_INT_OPTION(COAP_OPTION_MAX_AGE, max_age, "Max-Age"); @@ -390,7 +381,7 @@ coap_serialize_message(coap_message_t *coap_pkt, uint8_t *buffer) "Proxy-Scheme"); COAP_SERIALIZE_INT_OPTION(COAP_OPTION_SIZE1, size1, "Size1"); - PRINTF("-Done serializing at %p----\n", option); + LOG_DBG("-Done serializing at %p----\n", option); /* Pack payload */ if((option - coap_pkt->buffer) <= COAP_MAX_HEADER_SIZE) { @@ -407,19 +398,15 @@ coap_serialize_message(coap_message_t *coap_pkt, uint8_t *buffer) return 0; } - PRINTF("-Done %u B (header len %u, payload len %u)-\n", - (unsigned int)(coap_pkt->payload_len + option - buffer), - (unsigned int)(option - buffer), - (unsigned int)coap_pkt->payload_len); + LOG_DBG("-Done %u B (header len %u, payload len %u)-\n", + (unsigned int)(coap_pkt->payload_len + option - buffer), + (unsigned int)(option - buffer), + (unsigned int)coap_pkt->payload_len); - PRINTF("Dump [0x%02X %02X %02X %02X %02X %02X %02X %02X]\n", - coap_pkt->buffer[0], - coap_pkt->buffer[1], - coap_pkt->buffer[2], - coap_pkt->buffer[3], - coap_pkt->buffer[4], - coap_pkt->buffer[5], coap_pkt->buffer[6], coap_pkt->buffer[7] - ); + LOG_DBG("Dump [0x%02X %02X %02X %02X %02X %02X %02X %02X]\n", + coap_pkt->buffer[0], coap_pkt->buffer[1], coap_pkt->buffer[2], + coap_pkt->buffer[3], coap_pkt->buffer[4], coap_pkt->buffer[5], + coap_pkt->buffer[6], coap_pkt->buffer[7]); return (option - buffer) + coap_pkt->payload_len; /* message length */ } @@ -456,11 +443,11 @@ coap_parse_message(coap_message_t *coap_pkt, uint8_t *data, uint16_t data_len) uint8_t *current_option = data + COAP_HEADER_LEN; memcpy(coap_pkt->token, current_option, coap_pkt->token_len); - PRINTF("Token (len %u) [0x%02X%02X%02X%02X%02X%02X%02X%02X]\n", - coap_pkt->token_len, coap_pkt->token[0], coap_pkt->token[1], - coap_pkt->token[2], coap_pkt->token[3], coap_pkt->token[4], - coap_pkt->token[5], coap_pkt->token[6], coap_pkt->token[7] - ); /*FIXME always prints 8 bytes */ + LOG_DBG("Token (len %u) [0x%02X%02X%02X%02X%02X%02X%02X%02X]\n", + coap_pkt->token_len, coap_pkt->token[0], coap_pkt->token[1], + coap_pkt->token[2], coap_pkt->token[3], coap_pkt->token[4], + coap_pkt->token[5], coap_pkt->token[6], coap_pkt->token[7] + ); /* FIXME always prints 8 bytes */ /* parse options */ memset(coap_pkt->options, 0, sizeof(coap_pkt->options)); @@ -514,8 +501,8 @@ coap_parse_message(coap_message_t *coap_pkt, uint8_t *data, uint16_t data_len) if(current_option + option_length > data + data_len) { /* Malformed CoAP - out of bounds */ - PRINTF("BAD REQUEST: options outside data message: %u > %u\n", - (unsigned)(current_option + option_length - data), data_len); + LOG_WARN("BAD REQUEST: options outside data message: %u > %u\n", + (unsigned)(current_option + option_length - data), data_len); return BAD_REQUEST_4_00; } @@ -523,12 +510,12 @@ coap_parse_message(coap_message_t *coap_pkt, uint8_t *data, uint16_t data_len) if(option_number > COAP_OPTION_SIZE1) { /* Malformed CoAP - out of bounds */ - PRINTF("BAD REQUEST: option number too large: %u\n", option_number); + LOG_WARN("BAD REQUEST: option number too large: %u\n", option_number); return BAD_REQUEST_4_00; } - PRINTF("OPTION %u (delta %u, len %zu): ", option_number, option_delta, - option_length); + LOG_DBG("OPTION %u (delta %u, len %zu): ", option_number, option_delta, + option_length); coap_set_option(coap_pkt, option_number); @@ -536,51 +523,51 @@ coap_parse_message(coap_message_t *coap_pkt, uint8_t *data, uint16_t data_len) case COAP_OPTION_CONTENT_FORMAT: coap_pkt->content_format = coap_parse_int_option(current_option, option_length); - PRINTF("Content-Format [%u]\n", coap_pkt->content_format); + LOG_DBG_("Content-Format [%u]\n", coap_pkt->content_format); break; case COAP_OPTION_MAX_AGE: coap_pkt->max_age = coap_parse_int_option(current_option, option_length); - PRINTF("Max-Age [%lu]\n", (unsigned long)coap_pkt->max_age); + LOG_DBG_("Max-Age [%lu]\n", (unsigned long)coap_pkt->max_age); break; case COAP_OPTION_ETAG: coap_pkt->etag_len = MIN(COAP_ETAG_LEN, option_length); memcpy(coap_pkt->etag, current_option, coap_pkt->etag_len); - PRINTF("ETag %u [0x%02X%02X%02X%02X%02X%02X%02X%02X]\n", - coap_pkt->etag_len, coap_pkt->etag[0], coap_pkt->etag[1], - coap_pkt->etag[2], coap_pkt->etag[3], coap_pkt->etag[4], - coap_pkt->etag[5], coap_pkt->etag[6], coap_pkt->etag[7] - ); /*FIXME always prints 8 bytes */ + LOG_DBG_("ETag %u [0x%02X%02X%02X%02X%02X%02X%02X%02X]\n", + coap_pkt->etag_len, coap_pkt->etag[0], coap_pkt->etag[1], + coap_pkt->etag[2], coap_pkt->etag[3], coap_pkt->etag[4], + coap_pkt->etag[5], coap_pkt->etag[6], coap_pkt->etag[7] + ); /*FIXME always prints 8 bytes */ break; case COAP_OPTION_ACCEPT: coap_pkt->accept = coap_parse_int_option(current_option, option_length); - PRINTF("Accept [%u]\n", coap_pkt->accept); + LOG_DBG_("Accept [%u]\n", coap_pkt->accept); break; case COAP_OPTION_IF_MATCH: /* TODO support multiple ETags */ coap_pkt->if_match_len = MIN(COAP_ETAG_LEN, option_length); memcpy(coap_pkt->if_match, current_option, coap_pkt->if_match_len); - PRINTF("If-Match %u [0x%02X%02X%02X%02X%02X%02X%02X%02X]\n", - coap_pkt->if_match_len, coap_pkt->if_match[0], - coap_pkt->if_match[1], coap_pkt->if_match[2], - coap_pkt->if_match[3], coap_pkt->if_match[4], - coap_pkt->if_match[5], coap_pkt->if_match[6], - coap_pkt->if_match[7] - ); /* FIXME always prints 8 bytes */ + LOG_DBG_("If-Match %u [0x%02X%02X%02X%02X%02X%02X%02X%02X]\n", + coap_pkt->if_match_len, coap_pkt->if_match[0], + coap_pkt->if_match[1], coap_pkt->if_match[2], + coap_pkt->if_match[3], coap_pkt->if_match[4], + coap_pkt->if_match[5], coap_pkt->if_match[6], + coap_pkt->if_match[7] + ); /* FIXME always prints 8 bytes */ break; case COAP_OPTION_IF_NONE_MATCH: coap_pkt->if_none_match = 1; - PRINTF("If-None-Match\n"); + LOG_DBG_("If-None-Match\n"); break; case COAP_OPTION_PROXY_URI: #if COAP_PROXY_OPTION_PROCESSING coap_pkt->proxy_uri = (char *)current_option; coap_pkt->proxy_uri_len = option_length; -#endif - PRINTPRE("Proxy-Uri NOT IMPLEMENTED [",(int)coap_pkt->proxy_uri_len, - coap_pkt->proxy_uri); - PRINTF("]\n"); +#endif /* COAP_PROXY_OPTION_PROCESSING */ + LOG_DBG_("Proxy-Uri NOT IMPLEMENTED ["); + LOG_DBG_COAP_STRING(coap_pkt->proxy_uri, coap_pkt->proxy_uri_len); + LOG_DBG_("]\n"); coap_error_message = "This is a constrained server (Contiki)"; return PROXYING_NOT_SUPPORTED_5_05; @@ -590,9 +577,9 @@ coap_parse_message(coap_message_t *coap_pkt, uint8_t *data, uint16_t data_len) coap_pkt->proxy_scheme = (char *)current_option; coap_pkt->proxy_scheme_len = option_length; #endif - PRINTPRE("Proxy-Scheme NOT IMPLEMENTED [", - (int)coap_pkt->proxy_scheme_len, coap_pkt->proxy_scheme); - PRINTF("]\n"); + LOG_DBG_("Proxy-Scheme NOT IMPLEMENTED ["); + LOG_DBG_COAP_STRING(coap_pkt->proxy_scheme, coap_pkt->proxy_scheme_len); + LOG_DBG_("]\n"); coap_error_message = "This is a constrained server (Contiki)"; return PROXYING_NOT_SUPPORTED_5_05; break; @@ -600,30 +587,32 @@ coap_parse_message(coap_message_t *coap_pkt, uint8_t *data, uint16_t data_len) case COAP_OPTION_URI_HOST: coap_pkt->uri_host = (char *)current_option; coap_pkt->uri_host_len = option_length; - PRINTPRE("Uri-Host [", (int)coap_pkt->uri_host_len, - coap_pkt->uri_host); - PRINTF("]\n"); + LOG_DBG_("Uri-Host ["); + LOG_DBG_COAP_STRING(coap_pkt->uri_host, coap_pkt->uri_host_len); + LOG_DBG_("]\n"); break; case COAP_OPTION_URI_PORT: coap_pkt->uri_port = coap_parse_int_option(current_option, option_length); - PRINTF("Uri-Port [%u]\n", coap_pkt->uri_port); + LOG_DBG_("Uri-Port [%u]\n", coap_pkt->uri_port); break; case COAP_OPTION_URI_PATH: /* coap_merge_multi_option() operates in-place on the IPBUF, but final message field should be const string -> cast to string */ coap_merge_multi_option((char **)&(coap_pkt->uri_path), &(coap_pkt->uri_path_len), current_option, option_length, '/'); - PRINTPRE("Uri-Path [",(int)coap_pkt->uri_path_len, coap_pkt->uri_path); - PRINTF("]\n"); + LOG_DBG_("Uri-Path ["); + LOG_DBG_COAP_STRING(coap_pkt->uri_path, coap_pkt->uri_path_len); + LOG_DBG_("]\n"); break; case COAP_OPTION_URI_QUERY: /* coap_merge_multi_option() operates in-place on the IPBUF, but final message field should be const string -> cast to string */ coap_merge_multi_option((char **)&(coap_pkt->uri_query), &(coap_pkt->uri_query_len), current_option, option_length, '&'); - PRINTPRE("Uri-Query[",(int)coap_pkt->uri_query_len,coap_pkt->uri_query); - PRINTF("]\n"); + LOG_DBG_("Uri-Query["); + LOG_DBG_COAP_STRING(coap_pkt->uri_query, coap_pkt->uri_query_len); + LOG_DBG_("]\n"); break; case COAP_OPTION_LOCATION_PATH: @@ -632,24 +621,24 @@ coap_parse_message(coap_message_t *coap_pkt, uint8_t *data, uint16_t data_len) &(coap_pkt->location_path_len), current_option, option_length, '/'); - PRINTPRE("Location-Path [",(int)coap_pkt->location_path_len, - coap_pkt->location_path); - PRINTF("]\n"); + LOG_DBG_("Location-Path ["); + LOG_DBG_COAP_STRING(coap_pkt->location_path, coap_pkt->location_path_len); + LOG_DBG_("]\n"); break; case COAP_OPTION_LOCATION_QUERY: /* coap_merge_multi_option() operates in-place on the IPBUF, but final message field should be const string -> cast to string */ coap_merge_multi_option((char **)&(coap_pkt->location_query), &(coap_pkt->location_query_len), current_option, option_length, '&'); - PRINTPRE("Location-Query [",(int)coap_pkt->location_query_len, - coap_pkt->location_query); - PRINTF("]\n"); + LOG_DBG_("Location-Query ["); + LOG_DBG_COAP_STRING(coap_pkt->location_query, coap_pkt->location_query_len); + LOG_DBG_("]\n"); break; case COAP_OPTION_OBSERVE: coap_pkt->observe = coap_parse_int_option(current_option, option_length); - PRINTF("Observe [%lu]\n", (unsigned long)coap_pkt->observe); + LOG_DBG_("Observe [%lu]\n", (unsigned long)coap_pkt->observe); break; case COAP_OPTION_BLOCK2: coap_pkt->block2_num = coap_parse_int_option(current_option, @@ -659,9 +648,9 @@ coap_parse_message(coap_message_t *coap_pkt, uint8_t *data, uint16_t data_len) coap_pkt->block2_offset = (coap_pkt->block2_num & ~0x0000000F) << (coap_pkt->block2_num & 0x07); coap_pkt->block2_num >>= 4; - PRINTF("Block2 [%lu%s (%u B/blk)]\n", - (unsigned long)coap_pkt->block2_num, - coap_pkt->block2_more ? "+" : "", coap_pkt->block2_size); + LOG_DBG_("Block2 [%lu%s (%u B/blk)]\n", + (unsigned long)coap_pkt->block2_num, + coap_pkt->block2_more ? "+" : "", coap_pkt->block2_size); break; case COAP_OPTION_BLOCK1: coap_pkt->block1_num = coap_parse_int_option(current_option, @@ -671,20 +660,20 @@ coap_parse_message(coap_message_t *coap_pkt, uint8_t *data, uint16_t data_len) coap_pkt->block1_offset = (coap_pkt->block1_num & ~0x0000000F) << (coap_pkt->block1_num & 0x07); coap_pkt->block1_num >>= 4; - PRINTF("Block1 [%lu%s (%u B/blk)]\n", - (unsigned long)coap_pkt->block1_num, - coap_pkt->block1_more ? "+" : "", coap_pkt->block1_size); + LOG_DBG_("Block1 [%lu%s (%u B/blk)]\n", + (unsigned long)coap_pkt->block1_num, + coap_pkt->block1_more ? "+" : "", coap_pkt->block1_size); break; case COAP_OPTION_SIZE2: coap_pkt->size2 = coap_parse_int_option(current_option, option_length); - PRINTF("Size2 [%lu]\n", (unsigned long)coap_pkt->size2); + LOG_DBG_("Size2 [%lu]\n", (unsigned long)coap_pkt->size2); break; case COAP_OPTION_SIZE1: coap_pkt->size1 = coap_parse_int_option(current_option, option_length); - PRINTF("Size1 [%lu]\n", (unsigned long)coap_pkt->size1); + LOG_DBG_("Size1 [%lu]\n", (unsigned long)coap_pkt->size1); break; default: - PRINTF("unknown (%u)\n", option_number); + LOG_DBG_("unknown (%u)\n", option_number); /* check if critical (odd) */ if(option_number & 1) { coap_error_message = "Unsupported critical option"; @@ -694,7 +683,7 @@ coap_parse_message(coap_message_t *coap_pkt, uint8_t *data, uint16_t data_len) current_option += option_length; } /* for */ - PRINTF("-Done parsing-------\n"); + LOG_DBG("-Done parsing-------\n"); return NO_ERROR; } diff --git a/os/net/app-layer/coap/coap.h b/os/net/app-layer/coap/coap.h index 4cc97a869..9a93c7525 100644 --- a/os/net/app-layer/coap/coap.h +++ b/os/net/app-layer/coap/coap.h @@ -146,13 +146,13 @@ coap_is_option(const coap_message_t *message, unsigned int opt) /* option format serialization */ #define COAP_SERIALIZE_INT_OPTION(number, field, text) \ if(coap_is_option(coap_pkt, number)) { \ - PRINTF(text " [%u]\n", (unsigned int)coap_pkt->field); \ + LOG_DBG(text " [%u]\n", (unsigned int)coap_pkt->field); \ option += coap_serialize_int_option(number, current_number, option, coap_pkt->field); \ current_number = number; \ } #define COAP_SERIALIZE_BYTE_OPTION(number, field, text) \ if(coap_is_option(coap_pkt, number)) { \ - PRINTF(text " %u [0x%02X%02X%02X%02X%02X%02X%02X%02X]\n", (unsigned int)coap_pkt->field##_len, \ + LOG_DBG(text " %u [0x%02X%02X%02X%02X%02X%02X%02X%02X]\n", (unsigned int)coap_pkt->field##_len, \ coap_pkt->field[0], \ coap_pkt->field[1], \ coap_pkt->field[2], \ @@ -165,21 +165,21 @@ coap_is_option(const coap_message_t *message, unsigned int opt) option += coap_serialize_array_option(number, current_number, option, coap_pkt->field, coap_pkt->field##_len, '\0'); \ current_number = number; \ } -#define COAP_SERIALIZE_STRING_OPTION(number, field, splitter, text) \ - if(coap_is_option(coap_pkt, number)) { \ - PRINTPRE(text " [", (int)coap_pkt->field##_len, coap_pkt->field); \ - PRINTF("]\n"); \ +#define COAP_SERIALIZE_STRING_OPTION(number, field, splitter, text) \ + if(coap_is_option(coap_pkt, number)) { \ + LOG_DBG(text " ["); \ + LOG_DBG_COAP_STRING(coap_pkt->field, coap_pkt->field##_len); \ + LOG_DBG_("]\n"); \ option += coap_serialize_array_option(number, current_number, option, (uint8_t *)coap_pkt->field, coap_pkt->field##_len, splitter); \ current_number = number; \ } #define COAP_SERIALIZE_BLOCK_OPTION(number, field, text) \ - if(coap_is_option(coap_pkt, number)) \ - { \ - PRINTF(text " [%lu%s (%u B/blk)]\n", (unsigned long)coap_pkt->field##_num, coap_pkt->field##_more ? "+" : "", coap_pkt->field##_size); \ + if(coap_is_option(coap_pkt, number)) { \ + LOG_DBG(text " [%lu%s (%u B/blk)]\n", (unsigned long)coap_pkt->field##_num, coap_pkt->field##_more ? "+" : "", coap_pkt->field##_size); \ uint32_t block = coap_pkt->field##_num << 4; \ if(coap_pkt->field##_more) { block |= 0x8; } \ block |= 0xF & coap_log_2(coap_pkt->field##_size / 16); \ - PRINTF(text " encoded: 0x%lX\n", (unsigned long)block); \ + LOG_DBG(text " encoded: 0x%lX\n", (unsigned long)block); \ option += coap_serialize_int_option(number, current_number, option, block); \ current_number = number; \ }