Updated CoAP to use the logging module

This commit is contained in:
Niclas Finne 2017-12-01 00:41:58 +01:00
parent 21d464952b
commit 4c56184a9b
21 changed files with 805 additions and 515 deletions

View File

@ -42,14 +42,10 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#define DEBUG 0 /* Log configuration */
#if DEBUG #include "coap-log.h"
#define PRINTF(...) printf(__VA_ARGS__) #define LOG_MODULE "coap-hex"
#define PRINTEP(ep) coap_endpoint_print(ep) #define LOG_LEVEL LOG_LEVEL_COAP
#else
#define PRINTF(...)
#define PRINTEP(ep)
#endif
#ifdef WITH_DTLS #ifdef WITH_DTLS
#include "tinydtls.h" #include "tinydtls.h"
@ -104,12 +100,32 @@ coap_endpoint_cmp(const coap_endpoint_t *e1, const coap_endpoint_t *e2)
} }
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
void void
coap_endpoint_log(const coap_endpoint_t *ep)
{
LOG_OUTPUT("%u", ep->addr);
}
/*---------------------------------------------------------------------------*/
void
coap_endpoint_print(const coap_endpoint_t *ep) coap_endpoint_print(const coap_endpoint_t *ep)
{ {
printf("%u", ep->addr); printf("%u", ep->addr);
} }
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
int 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) 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 */ /* 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); buf[len] = (uint8_t)(((v1 << 4) | v2) & 0xff);
} }
PRINTF("RECV from "); LOG_INFO("RECV from ");
PRINTEP(&last_source); LOG_INFO_COAP_EP(&last_source);
PRINTF(" %u bytes\n", len); LOG_INFO_(" %u bytes\n", len);
coap_buf_len = len; coap_buf_len = len;
if(DEBUG) { if(LOG_DBG_ENABLED) {
int i; int i;
uint8_t *data; uint8_t *data;
data = coap_databuf(); data = coap_databuf();
printf("Received:"); LOG_DBG("Received: ");
for(i = 0; i < len; i++) { for(i = 0; i < len; i++) {
printf("%02x", data[i]); LOG_DBG_("%02x", data[i]);
} }
printf("\n"); LOG_DBG_("\n");
} }
#ifdef WITH_DTLS #ifdef WITH_DTLS
@ -208,13 +224,13 @@ coap_transport_init(void)
{ {
select_set_stdin_callback(stdin_callback); select_set_stdin_callback(stdin_callback);
printf("CoAP listening on standard in\n"); LOG_INFO("CoAP listening on standard in\n");
#ifdef WITH_DTLS #ifdef WITH_DTLS
/* create new contet with app-data - no real app-data... */ /* create new contet with app-data - no real app-data... */
dtls_context = dtls_new_context(&last_source); dtls_context = dtls_new_context(&last_source);
if(!dtls_context) { if(!dtls_context) {
PRINTF("DTLS: cannot create context\n"); LOG_ERR("DTLS: cannot create context\n");
exit(-1); exit(-1);
} }
@ -224,7 +240,7 @@ coap_transport_init(void)
memcpy(psk_id, PSK_DEFAULT_IDENTITY, psk_id_length); memcpy(psk_id, PSK_DEFAULT_IDENTITY, psk_id_length);
memcpy(psk_key, PSK_DEFAULT_KEY, psk_key_length); memcpy(psk_key, PSK_DEFAULT_KEY, psk_key_length);
#endif /* DTLS_PSK */ #endif /* DTLS_PSK */
PRINTF("Setting DTLS handler\n"); LOG_DBG("Setting DTLS handler\n");
dtls_set_handler(dtls_context, &cb); dtls_set_handler(dtls_context, &cb);
#endif /* WITH_DTLS */ #endif /* WITH_DTLS */
@ -234,7 +250,7 @@ int
coap_sendto(const coap_endpoint_t *ep, const uint8_t *data, uint16_t len) coap_sendto(const coap_endpoint_t *ep, const uint8_t *data, uint16_t len)
{ {
if(!coap_endpoint_is_connected(ep)) { if(!coap_endpoint_is_connected(ep)) {
PRINTF("CoAP endpoint not connected\n"); LOG_WARN("CoAP endpoint not connected\n");
return -1; return -1;
} }
@ -261,9 +277,9 @@ coap_endpoint_connect(coap_endpoint_t *ep)
return 1; return 1;
} }
#ifdef WITH_DTLS #ifdef WITH_DTLS
PRINTF("DTLS EP:"); LOG_DBG("DTLS EP:");
PRINTEP(ep); LOG_DBG_COAP_EP(ep);
PRINTF(" len:%d\n", ep->size); LOG_DBG_(" len:%d\n", ep->size);
/* setup all address info here... should be done to connect */ /* 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); peer = dtls_get_peer(dtls_context, ep);
if(peer != NULL) { if(peer != NULL) {
/* only if handshake is done! */ /* only if handshake is done! */
PRINTF("peer state for "); LOG_DBG("peer state for ");
PRINTEP(ep); LOG_DBG_COAP_EP(ep);
PRINTF(" is %d %d\n", peer->state, dtls_peer_is_connected(peer)); LOG_DBG_(" is %d %d\n", peer->state, dtls_peer_is_connected(peer));
return dtls_peer_is_connected(peer); return dtls_peer_is_connected(peer);
} else { } else {
PRINTF("Did not find peer "); LOG_DBG("Did not find peer ");
PRINTEP(ep); LOG_DBG_COAP_EP(ep);
PRINTF("\n"); LOG_DBG_("\n");
} }
#endif /* WITH_DTLS */ #endif /* WITH_DTLS */
return 0; return 0;
@ -324,11 +340,13 @@ input_from_peer(struct dtls_context_t *ctx,
dtls_peer_t *peer; dtls_peer_t *peer;
printf("received data:"); printf("received data:");
for (i = 0; i < len; i++) for(i = 0; i < len; i++) {
printf("%c", data[i]); printf("%c", data[i]);
}
printf("\nHex:"); printf("\nHex:");
for (i = 0; i < len; i++) for(i = 0; i < len; i++) {
printf("%02x", data[i]); printf("%02x", data[i]);
}
printf("\n"); printf("\n");
/* Send this into coap-input */ /* Send this into coap-input */
@ -377,7 +395,7 @@ get_psk_info(struct dtls_context_t *ctx,
const unsigned char *id, size_t id_len, const unsigned char *id, size_t id_len,
unsigned char *result, size_t result_length) unsigned char *result, size_t result_length)
{ {
PRINTF("---===>>> Getting the Key or ID <<<===---\n"); LOG_DBG("---===>>> Getting the Key or ID <<<===---\n");
switch(type) { switch(type) {
case DTLS_PSK_IDENTITY: case DTLS_PSK_IDENTITY:
if(id_len) { if(id_len) {

View File

@ -50,15 +50,10 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#define DEBUG 1 /* Log configuration */
#define DEBUG_VERBOSE ((DEBUG) && 0) #include "coap-log.h"
#if DEBUG #define LOG_MODULE "coap-ipv4"
#define PRINTF(...) printf(__VA_ARGS__) #define LOG_LEVEL LOG_LEVEL_COAP
#define PRINTEP(ep) coap_endpoint_print(ep)
#else /* DEBUG */
#define PRINTF(...)
#define PRINTEP(ep)
#endif /* DEBUG */
#ifdef WITH_DTLS #ifdef WITH_DTLS
#include "tinydtls.h" #include "tinydtls.h"
@ -109,14 +104,14 @@ coap_endpoint_is_connected(const coap_endpoint_t *ep)
peer = dtls_get_peer(dtls_context, ep); peer = dtls_get_peer(dtls_context, ep);
if(peer != NULL) { if(peer != NULL) {
/* only if handshake is done! */ /* only if handshake is done! */
PRINTF("DTLS peer state for "); LOG_DBG("DTLS peer state for ");
PRINTEP(ep); LOG_DBG_COAP_EP(ep);
PRINTF(" is %d %d\n", peer->state, dtls_peer_is_connected(peer)); LOG_DBG_(" is %d %d\n", peer->state, dtls_peer_is_connected(peer));
return dtls_peer_is_connected(peer); return dtls_peer_is_connected(peer);
} else { } else {
PRINTF("DTLS did not find peer "); LOG_DBG("DTLS did not find peer ");
PRINTEP(ep); LOG_DBG_COAP_EP(ep);
PRINTF("\n"); LOG_DBG_("\n");
} }
#endif /* WITH_DTLS */ #endif /* WITH_DTLS */
return 0; return 0;
@ -133,9 +128,9 @@ coap_endpoint_connect(coap_endpoint_t *ep)
} }
#ifdef WITH_DTLS #ifdef WITH_DTLS
PRINTF("DTLS connect to "); LOG_DBG("DTLS connect to ");
PRINTEP(ep); LOG_DBG_COAP_EP(ep);
PRINTF(" len:%d\n", ep->size); LOG_DBG_(" len:%d\n", ep->size);
/* setup all address info here... should be done to connect */ /* setup all address info here... should be done to connect */
if(dtls_context) { if(dtls_context) {
@ -179,6 +174,19 @@ coap_endpoint_cmp(const coap_endpoint_t *e1, const coap_endpoint_t *e2)
} }
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
void 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) coap_endpoint_print(const coap_endpoint_t *ep)
{ {
const char *address; const char *address;
@ -192,6 +200,28 @@ coap_endpoint_print(const coap_endpoint_t *ep)
} }
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
int 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) coap_endpoint_parse(const char *text, size_t size, coap_endpoint_t *ep)
{ {
/* text = format coap://host:port/... we assume */ /* 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 secure;
int offset = 0; int offset = 0;
int i; 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) { if(strncmp("coap://", text, 7) == 0) {
secure = 0; secure = 0;
offset = 7; offset = 7;
} else if(strncmp("coaps://", text, 8) == 0) { } else if(strncmp("coaps://", text, 8) == 0) {
secure = 1; secure = 1;
offset = 8; offset = 8;
PRINTF("secure "); LOG_DBG_("secure ");
} else { } else {
secure = 0; secure = 0;
} }
@ -227,7 +257,7 @@ coap_endpoint_parse(const char *text, size_t size, coap_endpoint_t *ep)
port = atoi(&text[i + 1]); 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_family = AF_INET;
ep->addr.sin_port = htons(port); 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; ep->secure = secure;
if(inet_aton(host, &ep->addr.sin_addr) == 0) { if(inet_aton(host, &ep->addr.sin_addr) == 0) {
/* Failed to parse the address */ /* 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 0;
} }
return 1; return 1;
@ -272,9 +302,9 @@ read_packet_to_coapbuf(int fd, int is_secure)
last_source.secure = is_secure; last_source.secure = is_secure;
PRINTF("CoAP-IPv4: RECV from "); LOG_INFO("RECV from ");
PRINTEP(&last_source); LOG_INFO_COAP_EP(&last_source);
PRINTF(" %u bytes\n", len); LOG_INFO_(" %u bytes\n", len);
coap_buf_len = len; coap_buf_len = len;
#if 0 #if 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(coap_ipv4_fd >= 0 && FD_ISSET(coap_ipv4_fd, rset)) {
if(read_packet_to_coapbuf(coap_ipv4_fd, 0)) { if(read_packet_to_coapbuf(coap_ipv4_fd, 0)) {
#if DEBUG_VERBOSE #if LOG_DBG_ENABLED
int i; int i;
uint8_t *data; uint8_t *data;
data = coap_databuf(); data = coap_databuf();
PRINTF("Received:"); LOG_DBG("Received: ");
for(i = 0; i < coap_buf_len; i++) { for(i = 0; i < coap_buf_len; i++) {
PRINTF("%02x", data[i]); LOG_DBG_("%02x", data[i]);
} }
PRINTF("\n"); LOG_DBG_("\n");
#endif /* DEBUG_VERBOSE */ #endif /* LOG_DBG_ENABLED */
coap_receive(coap_src_endpoint(), coap_databuf(), coap_datalen()); coap_receive(coap_src_endpoint(), coap_databuf(), coap_datalen());
} }
} }
@ -377,7 +407,7 @@ coap_transport_init(void)
exit(1); 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); select_set_callback(coap_ipv4_fd, &udp_callback);
#ifdef WITH_DTLS #ifdef WITH_DTLS
@ -402,7 +432,7 @@ coap_transport_init(void)
exit(1); 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); select_set_callback(dtls_ipv4_fd, &dtls_callback);
/* create new contet with app-data */ /* 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; int ret;
if(!coap_endpoint_is_connected(ep)) { if(!coap_endpoint_is_connected(ep)) {
PRINTF("CoAP-IPv4: endpoint "); LOG_WARN("endpoint ");
PRINTEP(ep); LOG_WARN_COAP_EP(ep);
PRINTF(" not connected - dropping packet\n"); LOG_WARN_(" not connected - dropping packet\n");
return -1; 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(coap_endpoint_is_secure(ep)) {
if(dtls_context) { if(dtls_context) {
ret = dtls_write(dtls_context, (session_t *)ep, (uint8_t *)data, len); ret = dtls_write(dtls_context, (session_t *)ep, (uint8_t *)data, len);
PRINTF("CoAP-IPv4: SENT DTLS to "); LOG_DBG("SENT DTLS to ");
PRINTEP(ep); LOG_DBG_COAP_EP(ep);
if(ret < 0) { if(ret < 0) {
PRINTF(" - error %d\n", ret); LOG_DBG_(" - error %d\n", ret);
} else { } else {
PRINTF(" %d/%u bytes\n", ret, len); LOG_DBG_(" %d/%u bytes\n", ret, len);
} }
return ret; return ret;
} }
PRINTF("CoAP-IPv4: no DTLS context\n"); LOG_WARN("no DTLS context\n");
return -1; return -1;
} }
#endif /* WITH_DTLS */ #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) { if(coap_ipv4_fd >= 0) {
ret = sendto(coap_ipv4_fd, data, len, 0, (struct sockaddr *)&ep->addr, ret = sendto(coap_ipv4_fd, data, len, 0, (struct sockaddr *)&ep->addr,
ep->size); ep->size);
PRINTF("CoAP-IPv4: SENT to "); LOG_INFO("SENT to ");
PRINTEP(ep); LOG_INFO_COAP_EP(ep);
if(ret < 0) { if(ret < 0) {
PRINTF(" - error %d: %s\n", ret, strerror(errno)); LOG_INFO_(" - error %d: %s\n", ret, strerror(errno));
} else { } 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; int i;
PRINTF("Sent:"); LOG_DBG("Sent: ");
for(i = 0; i < len; i++) { for(i = 0; i < len; i++) {
PRINTF("%02x", data[i]); LOG_DBG_("%02x", data[i]);
} }
PRINTF("\n"); LOG_DBG_("\n");
} }
} }
return ret; return ret;
} }
PRINTF("CoAP-IPv4: failed to send - no socket\n"); LOG_DBG("failed to send - no socket\n");
return -1; return -1;
} }
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
@ -481,19 +511,20 @@ static int
input_from_peer(struct dtls_context_t *ctx, input_from_peer(struct dtls_context_t *ctx,
session_t *session, uint8_t *data, size_t len) session_t *session, uint8_t *data, size_t len)
{ {
#if DEBUG_VERBOSE #if LOG_DBG_ENABLED
size_t i; size_t i;
PRINTF("DTLS received data:"); LOG_DBG("DTLS received data: ");
for(i = 0; i < len; i++) { 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++) { for(i = 0; i < len; i++) {
PRINTF("%02x", data[i]); LOG_DBG_("%02x", data[i]);
} }
PRINTF("\n"); LOG_DBG_("\n");
#endif /* DEBUG_VERBOSE */ #endif /* LOG_DBG_ENABLED */
/* Send this into coap-input */ /* Send this into coap-input */
memmove(coap_databuf(), data, len); 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) session_t *session, uint8_t *data, size_t len)
{ {
int fd = *(int *)dtls_get_app_data(ctx); int fd = *(int *)dtls_get_app_data(ctx);
#if DEBUG_VERBOSE LOG_DBG("DTLS output_to_peer len:%d %d (s-size: %d)\n", (int)len, fd,
PRINTF("DTLS output_to_peer len:%d %d (s-size: %d)\n", (int)len, fd,
session->size); session->size);
#endif /* DEBUG_VERBOSE */
return sendto(fd, data, len, MSG_DONTWAIT, return sendto(fd, data, len, MSG_DONTWAIT,
(struct sockaddr *)&session->addr, session->size); (struct sockaddr *)&session->addr, session->size);
} }
@ -543,12 +572,12 @@ get_psk_info(struct dtls_context_t *ctx,
keystore = dtls_keystore; keystore = dtls_keystore;
if(keystore == NULL) { if(keystore == NULL) {
PRINTF("--- No key store available ---\n"); LOG_DBG("--- No key store available ---\n");
return 0; return 0;
} }
memset(&ks, 0, sizeof(ks)); memset(&ks, 0, sizeof(ks));
PRINTF("---===>>> Getting the Key or ID <<<===---\n"); LOG_DBG("---===>>> Getting the Key or ID <<<===---\n");
switch(type) { switch(type) {
case DTLS_PSK_IDENTITY: case DTLS_PSK_IDENTITY:
if(id && id_len) { 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); keystore->coap_get_psk_info((coap_endpoint_t *)session, &ks);
} }
if(ks.identity == NULL || ks.identity_len == 0) { if(ks.identity == NULL || ks.identity_len == 0) {
PRINTF("no psk_identity found\n"); LOG_DBG("no psk_identity found\n");
return 0; return 0;
} }
if(result_length < ks.identity_len) { 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); return dtls_alert_fatal_create(DTLS_ALERT_INTERNAL_ERROR);
} }
memcpy(result, ks.identity, ks.identity_len); 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; return ks.identity_len;
case DTLS_PSK_KEY: 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); keystore->coap_get_psk_info((coap_endpoint_t *)session, &ks);
} }
if(ks.key == NULL || ks.key_len == 0) { 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); return dtls_alert_fatal_create(DTLS_ALERT_ILLEGAL_PARAMETER);
} }
if(result_length < ks.key_len) { 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); return dtls_alert_fatal_create(DTLS_ALERT_INTERNAL_ERROR);
} }
memcpy(result, ks.key, ks.key_len); 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; return ks.key_len;
default: default:

View File

@ -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 <niclas.finne@ri.se>
* Joakim Eriksson <joakim.eriksson@ri.se>
*/
#ifndef COAP_LOG_CONF_H_
#define COAP_LOG_CONF_H_
#include "contiki.h"
#include <stdio.h>
/* 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_ */

View File

@ -42,6 +42,8 @@
#include "posix-main.h" #include "posix-main.h"
#define COAP_LOG_CONF_PATH "coap-log-conf.h"
#define COAP_TIMER_CONF_DRIVER coap_timer_native_driver #define COAP_TIMER_CONF_DRIVER coap_timer_native_driver
#define LWM2M_ENGINE_CLIENT_ENDPOINT_NAME "lwm2m-ex" #define LWM2M_ENGINE_CLIENT_ENDPOINT_NAME "lwm2m-ex"

View File

@ -36,18 +36,16 @@
* Lars Schmertmann <SmallLars@t-online.de> * Lars Schmertmann <SmallLars@t-online.de>
*/ */
#include <stdio.h>
#include <string.h> #include <string.h>
#include <inttypes.h>
#include "coap.h" #include "coap.h"
#include "coap-block1.h" #include "coap-block1.h"
#define DEBUG 0 /* Log configuration */
#if DEBUG #include "coap-log.h"
#define PRINTF(...) printf(__VA_ARGS__) #define LOG_MODULE "coap-block1"
#else #define LOG_LEVEL LOG_LEVEL_COAP
#define PRINTF(...)
#endif
/*----------------------------------------------------------------------------*/ /*----------------------------------------------------------------------------*/
@ -97,7 +95,8 @@ coap_block1_handler(coap_message_t *request, coap_message_t *response,
} }
if(coap_is_option(request, COAP_OPTION_BLOCK1)) { if(coap_is_option(request, COAP_OPTION_BLOCK1)) {
PRINTF("Blockwise: block 1 request: Num: %u, More: %u, Size: %u, Offset: %u\n", LOG_DBG("Blockwise: block 1 request: Num: %"PRIu32
", More: %u, Size: %u, Offset: %"PRIu32"\n",
request->block1_num, request->block1_num,
request->block1_more, request->block1_more,
request->block1_size, request->block1_size,

View File

@ -42,14 +42,12 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <inttypes.h>
#define DEBUG 0 /* Log configuration */
#if DEBUG #include "coap-log.h"
#include <stdio.h> #define LOG_MODULE "coap-blocking-api"
#define PRINTF(...) printf(__VA_ARGS__) #define LOG_LEVEL LOG_LEVEL_COAP
#else
#define PRINTF(...)
#endif
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
/*- Client Part -------------------------------------------------------------*/ /*- Client Part -------------------------------------------------------------*/
@ -99,29 +97,30 @@ PT_THREAD(coap_blocking_request
message); message);
coap_send_transaction(state->transaction); 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); PT_YIELD_UNTIL(&state->pt, ev == PROCESS_EVENT_POLL);
if(!state->response) { if(!state->response) {
PRINTF("Server not responding\n"); LOG_WARN("Server not responding\n");
PT_EXIT(&state->pt); PT_EXIT(&state->pt);
} }
coap_get_header_block2(state->response, &res_block, &more, NULL, NULL); coap_get_header_block2(state->response, &res_block, &more, NULL, NULL);
PRINTF("Received #%lu%s (%u bytes)\n", res_block, more ? "+" : "", LOG_DBG("Received #%"PRIu32"%s (%u bytes)\n", res_block, more ? "+" : "",
state->response->payload_len); state->response->payload_len);
if(res_block == state->block_num) { if(res_block == state->block_num) {
request_callback(state->response); request_callback(state->response);
++(state->block_num); ++(state->block_num);
} else { } 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; ++block_error;
} }
} else { } else {
PRINTF("Could not allocate transaction buffer"); LOG_WARN("Could not allocate transaction buffer");
PT_EXIT(&state->pt); PT_EXIT(&state->pt);
} }
} while(more && block_error < COAP_MAX_ATTEMPTS); } while(more && block_error < COAP_MAX_ATTEMPTS);

View File

@ -45,13 +45,10 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#define DEBUG 0 /* Log configuration */
#if DEBUG #include "coap-log.h"
#include <stdio.h> #define LOG_MODULE "coap-callback-api"
#define PRINTF(...) printf(__VA_ARGS__) #define LOG_LEVEL LOG_LEVEL_COAP
#else
#define PRINTF(...)
#endif
/* These should go into the state struct so that we can have multiple /* These should go into the state struct so that we can have multiple
requests */ requests */
@ -81,7 +78,7 @@ progress_request(coap_request_state_t *state) {
coap_serialize_message(request, state->transaction->message); coap_serialize_message(request, state->transaction->message);
coap_send_transaction(state->transaction); coap_send_transaction(state->transaction);
PRINTF("Requested #%lu (MID %u)\n", (unsigned long) state->block_num, LOG_DBG("Requested #%lu (MID %u)\n", (unsigned long)state->block_num,
request->mid); request->mid);
} }
} }
@ -96,10 +93,10 @@ coap_request_callback(void *callback_data, coap_message_t *response)
state->response = response; state->response = response;
PRINTF("COAP: request callback\n"); LOG_DBG("request callback\n");
if(!state->response) { if(!state->response) {
PRINTF("Server not responding giving up...\n"); LOG_WARN("Server not responding giving up...\n");
state->callback(state); state->callback(state);
return; return;
} }
@ -108,7 +105,7 @@ coap_request_callback(void *callback_data, coap_message_t *response)
coap_get_header_block2(state->response, &res_block, &more, NULL, NULL); coap_get_header_block2(state->response, &res_block, &more, NULL, NULL);
coap_get_header_block1(state->response, &res_block1, NULL, NULL, NULL); coap_get_header_block1(state->response, &res_block1, NULL, NULL, NULL);
PRINTF("Received #%lu%s B1:%lu (%u bytes)\n", LOG_DBG("Received #%lu%s B1:%lu (%u bytes)\n",
(unsigned long)res_block, (unsigned)more ? "+" : "", (unsigned long)res_block, (unsigned)more ? "+" : "",
(unsigned long)res_block1, (unsigned long)res_block1,
state->response->payload_len); state->response->payload_len);
@ -119,7 +116,7 @@ coap_request_callback(void *callback_data, coap_message_t *response)
/* this is only for counting BLOCK2 blocks.*/ /* this is only for counting BLOCK2 blocks.*/
++(state->block_num); ++(state->block_num);
} else { } else {
PRINTF("WRONG BLOCK %lu/%lu\n", (unsigned long) res_block, LOG_WARN("WRONG BLOCK %lu/%lu\n", (unsigned long)res_block,
(unsigned long)state->block_num); (unsigned long)state->block_num);
++block_error; ++block_error;
} }

View File

@ -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); 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); 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); int coap_endpoint_parse(const char *text, size_t size, coap_endpoint_t *ep);

View File

@ -44,17 +44,10 @@
#include <inttypes.h> #include <inttypes.h>
#include <string.h> #include <string.h>
#define DEBUG 0 /* Log configuration */
#if DEBUG #include "coap-log.h"
#include <stdio.h> #define LOG_MODULE "coap-engine"
#define PRINTF(...) printf(__VA_ARGS__) #define LOG_LEVEL LOG_LEVEL_COAP
#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
static void process_callback(coap_timer_t *t); 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 */ /*TODO duplicates suppression, if required by application */
PRINTF(" Parsed: v %u, t %u, tkl %u, c %u, mid %u\n", message->version, 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); message->type, message->token_len, message->code, message->mid);
PRINTF(" URL:"); LOG_DBG(" URL:");
PRINTS(message->uri_path_len, message->uri_path, "%c"); LOG_DBG_COAP_STRING(message->uri_path, message->uri_path_len);
PRINTF("\n Payload: "); LOG_DBG_("\n");
PRINTS(message->payload_len, message->payload, "%c"); LOG_DBG(" Payload: ");
PRINTF("\n"); LOG_DBG_COAP_STRING((const char *)message->payload, message->payload_len);
LOG_DBG_("\n");
/* handle requests */ /* handle requests */
if(message->code >= COAP_GET && message->code <= COAP_DELETE) { if(message->code >= COAP_GET && message->code <= COAP_DELETE) {
@ -195,7 +189,7 @@ coap_receive(const coap_endpoint_t *src,
} }
if(coap_get_header_block2 if(coap_get_header_block2
(message, &block_num, NULL, &block_size, &block_offset)) { (message, &block_num, NULL, &block_size, &block_offset)) {
PRINTF("Blockwise: block request %"PRIu32" (%u/%u) @ %"PRIu32" bytes\n", LOG_DBG("Blockwise: block request %"PRIu32" (%u/%u) @ %"PRIu32" bytes\n",
block_num, block_size, COAP_MAX_BLOCK_SIZE, block_offset); block_num, block_size, COAP_MAX_BLOCK_SIZE, block_offset);
block_size = MIN(block_size, COAP_MAX_BLOCK_SIZE); block_size = MIN(block_size, COAP_MAX_BLOCK_SIZE);
new_offset = block_offset; new_offset = block_offset;
@ -215,7 +209,7 @@ coap_receive(const coap_endpoint_t *src,
if(coap_is_option(message, COAP_OPTION_BLOCK1) if(coap_is_option(message, COAP_OPTION_BLOCK1)
&& response->code < BAD_REQUEST_4_00 && response->code < BAD_REQUEST_4_00
&& !coap_is_option(response, COAP_OPTION_BLOCK1)) { && !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_status_code = NOT_IMPLEMENTED_5_01;
coap_error_message = "NoBlock1Support"; 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 */ /* unchanged new_offset indicates that resource is unaware of blockwise transfer */
if(new_offset == block_offset) { if(new_offset == block_offset) {
PRINTF LOG_DBG("Blockwise: unaware resource with payload length %u/%u\n",
("Blockwise: unaware resource with payload length %u/%u\n",
response->payload_len, block_size); response->payload_len, block_size);
if(block_offset >= response->payload_len) { if(block_offset >= response->payload_len) {
PRINTF LOG_DBG("handle_incoming_data(): block_offset >= response->payload_len\n");
("handle_incoming_data(): block_offset >= response->payload_len\n");
response->code = BAD_OPTION_4_02; response->code = BAD_OPTION_4_02;
coap_set_payload(response, "BlockOutOfScope", 15); /* a const char str[] and sizeof(str) produces larger code size */ coap_set_payload(response, "BlockOutOfScope", 15); /* a const char str[] and sizeof(str) produces larger code size */
@ -247,7 +239,7 @@ coap_receive(const coap_endpoint_t *src,
/* resource provides chunk-wise data */ /* resource provides chunk-wise data */
} else { } else {
PRINTF("Blockwise: blockwise resource, new offset %"PRId32"\n", LOG_DBG("Blockwise: blockwise resource, new offset %"PRId32"\n",
new_offset); new_offset);
coap_set_header_block2(response, block_num, coap_set_header_block2(response, block_num,
new_offset != -1 new_offset != -1
@ -262,8 +254,7 @@ coap_receive(const coap_endpoint_t *src,
/* Resource requested Block2 transfer */ /* Resource requested Block2 transfer */
} else if(new_offset != 0) { } else if(new_offset != 0) {
PRINTF LOG_DBG("Blockwise: no block option for blockwise resource, using block size %u\n",
("Blockwise: no block option for blockwise resource, using block size %u\n",
COAP_MAX_BLOCK_SIZE); COAP_MAX_BLOCK_SIZE);
coap_set_header_block2(response, 0, new_offset != -1, coap_set_header_block2(response, 0, new_offset != -1,
@ -293,13 +284,13 @@ coap_receive(const coap_endpoint_t *src,
} else { } else {
if(message->type == COAP_TYPE_CON && message->code == 0) { if(message->type == COAP_TYPE_CON && message->code == 0) {
PRINTF("Received Ping\n"); LOG_INFO("Received Ping\n");
coap_status_code = PING_RESPONSE; coap_status_code = PING_RESPONSE;
} else if(message->type == COAP_TYPE_ACK) { } else if(message->type == COAP_TYPE_ACK) {
/* transactions are closed through lookup below */ /* transactions are closed through lookup below */
PRINTF("Received ACK\n"); LOG_DBG("Received ACK\n");
} else if(message->type == COAP_TYPE_RST) { } else if(message->type == COAP_TYPE_RST) {
PRINTF("Received RST\n"); LOG_INFO("Received RST\n");
/* cancel possible subscriptions */ /* cancel possible subscriptions */
coap_remove_observer_by_mid(src, message->mid); coap_remove_observer_by_mid(src, message->mid);
} }
@ -323,7 +314,7 @@ coap_receive(const coap_endpoint_t *src,
/* if observe notification */ /* if observe notification */
if((message->type == COAP_TYPE_CON || message->type == COAP_TYPE_NON) if((message->type == COAP_TYPE_CON || message->type == COAP_TYPE_NON)
&& coap_is_option(message, COAP_OPTION_OBSERVE)) { && 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); coap_handle_notification(src, message);
} }
#endif /* COAP_OBSERVE_CLIENT */ #endif /* COAP_OBSERVE_CLIENT */
@ -336,12 +327,12 @@ coap_receive(const coap_endpoint_t *src,
coap_send_transaction(transaction); coap_send_transaction(transaction);
} }
} else if(coap_status_code == MANUAL_RESPONSE) { } else if(coap_status_code == MANUAL_RESPONSE) {
PRINTF("Clearing transaction for manual response"); LOG_DBG("Clearing transaction for manual response");
coap_clear_transaction(transaction); coap_clear_transaction(transaction);
} else { } else {
coap_message_type_t reply_type = COAP_TYPE_ACK; 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); coap_clear_transaction(transaction);
if(coap_status_code == PING_RESPONSE) { if(coap_status_code == PING_RESPONSE) {
@ -368,12 +359,12 @@ coap_engine_init(void)
{ {
/* avoid initializing twice */ /* avoid initializing twice */
if(is_initialized) { if(is_initialized) {
PRINTF("CoAP engine process already running - double initialization?\n"); LOG_DBG("already running - double initialization?\n");
return; return;
} }
is_initialized = 1; is_initialized = 1;
PRINTF("Starting CoAP engine...\n"); LOG_INFO("Starting CoAP engine...\n");
list_init(coap_handlers); list_init(coap_handlers);
list_init(coap_resource_services); list_init(coap_resource_services);
@ -401,14 +392,13 @@ coap_activate_resource(coap_resource_t *resource, const char *path)
resource->url = path; resource->url = path;
list_add(coap_resource_services, resource); 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. */ /* Only add periodic resources with a periodic_handler and a period > 0. */
if(resource->flags & IS_PERIODIC && resource->periodic if(resource->flags & IS_PERIODIC && resource->periodic
&& resource->periodic->periodic_handler && resource->periodic->periodic_handler
&& resource->periodic->period) { && resource->periodic->period) {
PRINTF("Periodic resource: %p (%s)\n", resource->periodic, LOG_DBG("Periodic resource: %p (%s)\n", resource->periodic, path);
resource->periodic->resource->url);
list_add(coap_resource_periodic_services, resource->periodic); list_add(coap_resource_periodic_services, resource->periodic);
periodic = resource->periodic; periodic = resource->periodic;
coap_timer_set_callback(&periodic->periodic_timer, process_callback); coap_timer_set_callback(&periodic->periodic_timer, process_callback);
@ -459,7 +449,7 @@ invoke_coap_resource_service(coap_message_t *request, coap_message_t *response,
coap_resource_flags_t method = coap_get_method_type(request); coap_resource_flags_t method = coap_get_method_type(request);
found = 1; found = 1;
PRINTF("/%s, method %u, resource->flags %u\n", resource->url, LOG_INFO("/%s, method %u, resource->flags %u\n", resource->url,
(uint16_t)method, resource->flags); (uint16_t)method, resource->flags);
if((method & METHOD_GET) && resource->get_handler != NULL) { if((method & METHOD_GET) && resource->get_handler != NULL) {
@ -502,7 +492,7 @@ process_callback(coap_timer_t *t)
resource = coap_timer_get_user_data(t); resource = coap_timer_get_user_data(t);
if(resource != NULL && (resource->flags & IS_PERIODIC) if(resource != NULL && (resource->flags & IS_PERIODIC)
&& resource->periodic != NULL && resource->periodic->period) { && resource->periodic != NULL && resource->periodic->period) {
PRINTF("Periodic: timer expired for /%s (period: %lu)\n", LOG_DBG("Periodic: timer expired for /%s (period: %"PRIu32")\n",
resource->url, resource->periodic->period); resource->url, resource->periodic->period);
if(!is_initialized) { if(!is_initialized) {

View File

@ -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 <niclas.finne@ri.se>
* Joakim Eriksson <joakim.eriksson@ri.se>
*/
#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);
}
}
/*---------------------------------------------------------------------------*/

View File

@ -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 <niclas.finne@ri.se>
* Joakim Eriksson <joakim.eriksson@ri.se>
*/
#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_ */

View File

@ -48,14 +48,10 @@
/* Compile this code only if client-side support for CoAP Observe is required */ /* Compile this code only if client-side support for CoAP Observe is required */
#if COAP_OBSERVE_CLIENT #if COAP_OBSERVE_CLIENT
#define DEBUG 0 /* Log configuration */
#if DEBUG #include "coap-log.h"
#define PRINTF(...) printf(__VA_ARGS__) #define LOG_MODULE "coap-observe-client"
#define PRINTEP(ep) coap_endpoint_print(ep) #define LOG_LEVEL LOG_LEVEL_COAP
#else
#define PRINTF(...)
#define PRINTEP(ep)
#endif
MEMB(obs_subjects_memb, coap_observee_t, COAP_MAX_OBSERVEES); MEMB(obs_subjects_memb, coap_observee_t, COAP_MAX_OBSERVEES);
LIST(obs_subjects_list); LIST(obs_subjects_list);
@ -97,7 +93,7 @@ coap_obs_add_observee(const coap_endpoint_t *endpoint,
/* o->last_mid = 0; */ /* o->last_mid = 0; */
o->notification_callback = notification_callback; o->notification_callback = notification_callback;
o->data = data; o->data = data;
PRINTF("Adding obs_subject for /%s [0x%02X%02X]\n", o->url, o->token[0], LOG_DBG("Adding obs_subject for /%s [0x%02X%02X]\n", o->url, o->token[0],
o->token[1]); o->token[1]);
list_add(obs_subjects_list, o); list_add(obs_subjects_list, o);
} }
@ -108,7 +104,7 @@ coap_obs_add_observee(const coap_endpoint_t *endpoint,
void void
coap_obs_remove_observee(coap_observee_t *o) coap_obs_remove_observee(coap_observee_t *o)
{ {
PRINTF("Removing obs_subject for /%s [0x%02X%02X]\n", o->url, o->token[0], LOG_DBG("Removing obs_subject for /%s [0x%02X%02X]\n", o->url, o->token[0],
o->token[1]); o->token[1]);
memb_free(&obs_subjects_memb, o); memb_free(&obs_subjects_memb, o);
list_remove(obs_subjects_list, 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; for(obs = (coap_observee_t *)list_head(obs_subjects_list); obs;
obs = obs->next) { 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 if(obs->token_len == token_len
&& memcmp(obs->token, token, token_len) == 0) { && memcmp(obs->token, token, token_len) == 0) {
return obs; 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; for(obs = (coap_observee_t *)list_head(obs_subjects_list); obs;
obs = obs->next) { 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) if(coap_endpoint_cmp(&obs->endpoint, endpoint)
&& obs->token_len == token_len && obs->token_len == token_len
&& memcmp(obs->token, token, token_len) == 0) { && 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; for(obs = (coap_observee_t *)list_head(obs_subjects_list); obs;
obs = obs->next) { 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) if(coap_endpoint_cmp(&obs->endpoint, endpoint)
&& (obs->url == url || memcmp(obs->url, url, strlen(obs->url)) == 0)) { && (obs->url == url || memcmp(obs->url, url, strlen(obs->url)) == 0)) {
coap_obs_remove_observee(obs); coap_obs_remove_observee(obs);
@ -186,16 +182,16 @@ static coap_notification_flag_t
classify_notification(coap_message_t *response, int first) classify_notification(coap_message_t *response, int first)
{ {
if(!response) { if(!response) {
PRINTF("no response\n"); LOG_DBG("no response\n");
return NO_REPLY_FROM_SERVER; return NO_REPLY_FROM_SERVER;
} }
PRINTF("server replied\n"); LOG_DBG("server replied\n");
if(!IS_RESPONSE_CODE_2_XX(response)) { if(!IS_RESPONSE_CODE_2_XX(response)) {
PRINTF("error response code\n"); LOG_DBG("error response code\n");
return ERROR_RESPONSE_CODE; return ERROR_RESPONSE_CODE;
} }
if(!coap_is_option(response, COAP_OPTION_OBSERVE)) { 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; return OBSERVE_NOT_SUPPORTED;
} }
if(first) { if(first) {
@ -214,18 +210,18 @@ coap_handle_notification(const coap_endpoint_t *endpoint,
coap_notification_flag_t flag; coap_notification_flag_t flag;
uint32_t observe; uint32_t observe;
PRINTF("coap_handle_notification()\n"); LOG_DBG("coap_handle_notification()\n");
token_len = get_token(notification, &token); token_len = get_token(notification, &token);
PRINTF("Getting token\n"); LOG_DBG("Getting token\n");
if(0 == token_len) { if(0 == token_len) {
PRINTF("Error while handling coap observe notification: " LOG_DBG("Error while handling coap observe notification: "
"no token in message\n"); "no token in message\n");
return; return;
} }
PRINTF("Getting observee info\n"); LOG_DBG("Getting observee info\n");
obs = coap_get_obs_subject_by_token(token, token_len); obs = coap_get_obs_subject_by_token(token, token_len);
if(NULL == obs) { if(NULL == obs) {
PRINTF("Error while handling coap observe notification: " LOG_DBG("Error while handling coap observe notification: "
"no matching token found\n"); "no matching token found\n");
simple_reply(COAP_TYPE_RST, endpoint, notification); simple_reply(COAP_TYPE_RST, endpoint, notification);
return; return;
@ -240,7 +236,7 @@ coap_handle_notification(const coap_endpoint_t *endpoint,
if(flag == NOTIFICATION_OK) { if(flag == NOTIFICATION_OK) {
coap_get_header_observe(notification, &observe); coap_get_header_observe(notification, &observe);
if(observe == obs->last_observe) { if(observe == obs->last_observe) {
PRINTF("Discarding duplicate\n"); LOG_DBG("Discarding duplicate\n");
return; return;
} }
obs->last_observe = observe; obs->last_observe = observe;
@ -256,7 +252,7 @@ handle_obs_registration_response(void *data, coap_message_t *response)
notification_callback_t notification_callback; notification_callback_t notification_callback;
coap_notification_flag_t flag; coap_notification_flag_t flag;
PRINTF("handle_obs_registration_response(): "); LOG_DBG("handle_obs_registration_response()\n");
obs = (coap_observee_t *)data; obs = (coap_observee_t *)data;
notification_callback = obs->notification_callback; notification_callback = obs->notification_callback;
flag = classify_notification(response, 1); 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); t->message_len = coap_serialize_message(request, t->message);
coap_send_transaction(t); coap_send_transaction(t);
} else { } else {
PRINTF("Could not allocate obs_subject resource buffer"); LOG_DBG("Could not allocate obs_subject resource buffer\n");
coap_clear_transaction(t); coap_clear_transaction(t);
} }
} else { } else {
PRINTF("Could not allocate transaction buffer"); LOG_DBG("Could not allocate transaction buffer\n");
} }
return obs; return obs;
} }

View File

@ -43,15 +43,10 @@
#include "lib/memb.h" #include "lib/memb.h"
#include "lib/list.h" #include "lib/list.h"
#define DEBUG 0 /* Log configuration */
#if DEBUG #include "coap-log.h"
#include <stdio.h> #define LOG_MODULE "coap-observe"
#define PRINTF(...) printf(__VA_ARGS__) #define LOG_LEVEL LOG_LEVEL_COAP
#define PRINTEP(ep) coap_endpoint_print(ep)
#else
#define PRINTF(...)
#define PRINTEP(ep)
#endif
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
MEMB(observers_memb, coap_observer_t, COAP_MAX_OBSERVERS); MEMB(observers_memb, coap_observer_t, COAP_MAX_OBSERVERS);
@ -80,7 +75,7 @@ add_observer(const coap_endpoint_t *endpoint, const uint8_t *token,
memcpy(o->token, token, token_len); memcpy(o->token, token, token_len);
o->last_mid = 0; o->last_mid = 0;
PRINTF("Adding observer (%u/%u) for /%s [0x%02X%02X]\n", LOG_INFO("Adding observer (%u/%u) for /%s [0x%02X%02X]\n",
list_length(observers_list) + 1, COAP_MAX_OBSERVERS, list_length(observers_list) + 1, COAP_MAX_OBSERVERS,
o->url, o->token[0], o->token[1]); o->url, o->token[0], o->token[1]);
list_add(observers_list, o); list_add(observers_list, o);
@ -94,7 +89,7 @@ add_observer(const coap_endpoint_t *endpoint, const uint8_t *token,
void void
coap_remove_observer(coap_observer_t *o) coap_remove_observer(coap_observer_t *o)
{ {
PRINTF("Removing observer for /%s [0x%02X%02X]\n", o->url, o->token[0], LOG_INFO("Removing observer for /%s [0x%02X%02X]\n", o->url, o->token[0],
o->token[1]); o->token[1]);
memb_free(&observers_memb, o); memb_free(&observers_memb, o);
@ -107,9 +102,9 @@ coap_remove_observer_by_client(const coap_endpoint_t *endpoint)
int removed = 0; int removed = 0;
coap_observer_t *obs = NULL; coap_observer_t *obs = NULL;
PRINTF("Remove check client "); LOG_DBG("Remove check client ");
PRINTEP(endpoint); LOG_DBG_COAP_EP(endpoint);
PRINTF("\n"); LOG_DBG_("\n");
for(obs = (coap_observer_t *)list_head(observers_list); obs; for(obs = (coap_observer_t *)list_head(observers_list); obs;
obs = obs->next) { obs = obs->next) {
if(coap_endpoint_cmp(&obs->endpoint, endpoint)) { 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; for(obs = (coap_observer_t *)list_head(observers_list); obs;
obs = obs->next) { 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) if(coap_endpoint_cmp(&obs->endpoint, endpoint)
&& obs->token_len == token_len && obs->token_len == token_len
&& memcmp(obs->token, token, token_len) == 0) { && 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; for(obs = (coap_observer_t *)list_head(observers_list); obs;
obs = obs->next) { obs = obs->next) {
PRINTF("Remove check URL %p\n", uri); LOG_DBG("Remove check URL %p\n", uri);
if((endpoint == NULL if((endpoint == NULL
|| (coap_endpoint_cmp(&obs->endpoint, endpoint))) || (coap_endpoint_cmp(&obs->endpoint, endpoint)))
&& (obs->url == uri || memcmp(obs->url, uri, strlen(obs->url)) == 0)) { && (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; for(obs = (coap_observer_t *)list_head(observers_list); obs;
obs = obs->next) { 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) if(coap_endpoint_cmp(&obs->endpoint, endpoint)
&& obs->last_mid == mid) { && obs->last_mid == mid) {
coap_remove_observer(obs); 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 */ /* Ensure url is null terminated because strncpy does not guarantee this */
url[COAP_OBSERVER_URL_LEN - 1] = '\0'; url[COAP_OBSERVER_URL_LEN - 1] = '\0';
/* url now contains the notify URL that needs to match the observer */ /* 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); coap_init_message(notification, COAP_TYPE_NON, CONTENT_2_05, 0);
/* create a "fake" request for the URI */ /* 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((transaction = coap_new_transaction(coap_get_mid(), &obs->endpoint))) {
if(obs->obs_counter % COAP_OBSERVE_REFRESH_INTERVAL == 0) { 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; notification->type = COAP_TYPE_CON;
} }
PRINTF(" Observer "); LOG_DBG(" Observer ");
PRINTEP(&obs->endpoint); LOG_DBG_COAP_EP(&obs->endpoint);
PRINTF("\n"); LOG_DBG_("\n");
/* update last MID for RST matching */ /* update last MID for RST matching */
obs->last_mid = transaction->mid; 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 + if(coap_call_handlers(request, notification, transaction->message +
COAP_MAX_HEADER_SIZE, COAP_MAX_CHUNK_SIZE, COAP_MAX_HEADER_SIZE, COAP_MAX_CHUNK_SIZE,
NULL) > 0) { NULL) > 0) {
PRINTF("Notification on new handlers\n"); LOG_DBG("Notification on new handlers\n");
} else { } else {
if(resource != NULL) { if(resource != NULL) {
resource->get_handler(request, notification, 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; const coap_endpoint_t *src_ep;
coap_observer_t *obs; 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_req->code == COAP_GET && coap_res->code < 128) { /* GET request and response without error code */
if(coap_is_option(coap_req, COAP_OPTION_OBSERVE)) { if(coap_is_option(coap_req, COAP_OPTION_OBSERVE)) {

View File

@ -40,13 +40,10 @@
#include <string.h> #include <string.h>
#include <stdio.h> #include <stdio.h>
#define DEBUG 0 /* Log configuration */
#if DEBUG #include "coap-log.h"
#include <stdio.h> #define LOG_MODULE "coap-res-well-known-core"
#define PRINTF(...) printf(__VA_ARGS__) #define LOG_LEVEL LOG_LEVEL_COAP
#else
#define PRINTF(...)
#endif
#define ADD_CHAR_IF_POSSIBLE(char) \ #define ADD_CHAR_IF_POSSIBLE(char) \
if(strpos >= *offset && bufpos < preferred_size) { \ if(strpos >= *offset && bufpos < preferred_size) { \
@ -64,7 +61,7 @@
+ (*offset - (int32_t)strpos > 0 ? \ + (*offset - (int32_t)strpos > 0 ? \
*offset - (int32_t)strpos : 0)); \ *offset - (int32_t)strpos : 0)); \
if(bufpos op preferred_size) { \ if(bufpos op preferred_size) { \
PRINTF("res: BREAK at %s (%p)\n", string, resource); \ LOG_DBG("BREAK at %s (%p)\n", string, resource); \
break; \ break; \
} \ } \
} \ } \
@ -99,7 +96,9 @@ well_known_core_get_handler(coap_message_t *request, coap_message_t *response,
++value; ++value;
len -= strlen(filter) + 1; 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] == '/') { if(strcmp(filter, "href") == 0 && value[0] == '/') {
++value; ++value;
@ -109,7 +108,7 @@ well_known_core_get_handler(coap_message_t *request, coap_message_t *response,
lastchar = value[len - 1]; lastchar = value[len - 1];
value[len - 1] = '\0'; value[len - 1] = '\0';
} }
#endif #endif /* COAP_LINK_FORMAT_FILTERING */
for(resource = coap_get_first_resource(); resource; for(resource = coap_get_first_resource(); resource;
resource = coap_get_next_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, '"'); 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; found = attrib;
while((found = strstr(found, value)) != NULL) { while((found = strstr(found, value)) != NULL) {
if(found > end) { if(found > end) {
@ -148,16 +147,16 @@ well_known_core_get_handler(coap_message_t *request, coap_message_t *response,
if(found == NULL) { if(found == NULL) {
continue; continue;
} }
PRINTF("Filter: res has prefix %s\n", found); LOG_DBG("Filter: res has prefix %s\n", found);
if(lastchar != '*' if(lastchar != '*'
&& (found[len] != '"' && found[len] != ' ' && found[len] != '\0')) { && (found[len] != '"' && found[len] != ' ' && found[len] != '\0')) {
continue; continue;
} }
PRINTF("Filter: res has match\n"); LOG_DBG("Filter: res has match\n");
} }
#endif #endif
PRINTF("res: /%s (%p)\npos: s%zu, o%ld, b%zu\n", resource->url, resource, LOG_DBG("/%s (%p)\npos: s%zu, o%ld, b%zu\n", resource->url, resource,
strpos, (long)*offset, bufpos); strpos, (long)*offset, bufpos);
if(strpos > 0) { if(strpos > 0) {
@ -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. */ /* buffer full, but resource not completed yet; or: do not break if resource exactly fills buffer. */
if(bufpos > preferred_size && strpos - bufpos > *offset) { 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; break;
} }
} }
if(bufpos > 0) { 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_payload(response, buffer, bufpos);
coap_set_header_content_format(response, APPLICATION_LINK_FORMAT); coap_set_header_content_format(response, APPLICATION_LINK_FORMAT);
} else if(strpos > 0) { } 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_status_code(response, BAD_OPTION_4_02);
coap_set_payload(response, "BlockOutOfScope", 15); coap_set_payload(response, "BlockOutOfScope", 15);
} }
if(resource == NULL) { if(resource == NULL) {
PRINTF("res: DONE\n"); LOG_DBG("DONE\n");
*offset = -1; *offset = -1;
} else { } else {
PRINTF("res: MORE at %s (%p)\n", resource->url, resource); LOG_DBG("MORE at %s (%p)\n", resource->url, resource);
*offset += preferred_size; *offset += preferred_size;
} }
} }

View File

@ -42,13 +42,10 @@
#include "sys/cc.h" #include "sys/cc.h"
#include <string.h> #include <string.h>
#define DEBUG 0 /* Log configuration */
#if DEBUG #include "coap-log.h"
#include <stdio.h> #define LOG_MODULE "coap-separate"
#define PRINTF(...) printf(__VA_ARGS__) #define LOG_LEVEL LOG_LEVEL_COAP
#else
#define PRINTF(...)
#endif
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
/*- Separate Response API ---------------------------------------------------*/ /*- 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); 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, LOG_DBG("Separate ACCEPT: /");
coap_req->uri_path, coap_req->mid); LOG_DBG_COAP_STRING(coap_req->uri_path, coap_req->uri_path_len);
LOG_DBG_(" MID %u\n", coap_req->mid);
if(t) { if(t) {
/* send separate ACK for CON */ /* send separate ACK for CON */
if(coap_req->type == COAP_TYPE_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); ep = coap_get_src_endpoint(coap_req);
if(ep == NULL) { if(ep == NULL) {
PRINTF("ERROR: no endpoint in request\n"); LOG_ERR("ERROR: no endpoint in request\n");
} else { } else {
/* ACK with empty code (0) */ /* ACK with empty code (0) */
coap_init_message(ack, COAP_TYPE_ACK, 0, coap_req->mid); 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 */ /* signal the engine to skip automatic response and clear transaction by engine */
coap_status_code = MANUAL_RESPONSE; coap_status_code = MANUAL_RESPONSE;
} else { } 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; coap_status_code = INTERNAL_SERVER_ERROR_5_00;
} }
} }

View File

@ -40,13 +40,10 @@
#include "sys/etimer.h" #include "sys/etimer.h"
#include "sys/process.h" #include "sys/process.h"
#define DEBUG 0 /* Log configuration */
#if DEBUG #include "coap-log.h"
#include <stdio.h> #define LOG_MODULE "coap-timer-default"
#define PRINTF(...) printf(__VA_ARGS__) #define LOG_LEVEL LOG_LEVEL_NONE
#else
#define PRINTF(...)
#endif
PROCESS(coap_timer_process, "coap timer process"); PROCESS(coap_timer_process, "coap timer process");
@ -58,7 +55,7 @@ update_timer(void)
{ {
uint64_t remaining; uint64_t remaining;
remaining = coap_timer_time_to_next_expiration(); 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) { if(remaining == 0) {
/* Run as soon as possible */ /* Run as soon as possible */
process_poll(&coap_timer_process); process_poll(&coap_timer_process);

View File

@ -38,13 +38,10 @@
#include "coap-timer.h" #include "coap-timer.h"
#include "lib/list.h" #include "lib/list.h"
#define DEBUG 0 /* Log configuration */
#if DEBUG #include "coap-log.h"
#include <stdio.h> #define LOG_MODULE "coap-timer"
#define PRINTF(...) printf(__VA_ARGS__) #define LOG_LEVEL LOG_LEVEL_NONE
#else
#define PRINTF(...)
#endif
#ifndef NULL #ifndef NULL
#define NULL 0 #define NULL 0
@ -63,7 +60,7 @@ add_timer(coap_timer_t *timer)
coap_timer_init(); coap_timer_init();
} }
PRINTF("coap-timer: adding timer %p at %lu\n", timer, LOG_DBG("adding timer %p at %lu\n", timer,
(unsigned long)timer->expiration_time); (unsigned long)timer->expiration_time);
p = list_head(timer_list); p = list_head(timer_list);
@ -92,7 +89,7 @@ add_timer(coap_timer_t *timer)
void void
coap_timer_stop(coap_timer_t *timer) 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 */ /* Mark timer as expired right now */
timer->expiration_time = coap_timer_uptime(); timer->expiration_time = coap_timer_uptime();
@ -150,8 +147,7 @@ coap_timer_run(void)
} }
if(next->expiration_time <= now) { if(next->expiration_time <= now) {
PRINTF("coap-timer: timer %p expired at %lu\n", next, LOG_DBG("timer %p expired at %lu\n", next, (unsigned long)now);
(unsigned long)now);
/* This timer should expire now */ /* This timer should expire now */
list_remove(timer_list, next); list_remove(timer_list, next);

View File

@ -43,15 +43,10 @@
#include "lib/list.h" #include "lib/list.h"
#include <stdlib.h> #include <stdlib.h>
#define DEBUG 0 /* Log configuration */
#if DEBUG #include "coap-log.h"
#include <stdio.h> #define LOG_MODULE "coap-transactions"
#define PRINTF(...) printf(__VA_ARGS__) #define LOG_LEVEL LOG_LEVEL_COAP
#define PRINTEP(ep) coap_endpoint_print(ep)
#else
#define PRINTF(...)
#define PRINTEP(ep)
#endif
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
MEMB(transactions_memb, coap_transaction_t, COAP_MAX_OPEN_TRANSACTIONS); 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); coap_transaction_t *t = coap_timer_get_user_data(nt);
if(t == NULL) { if(t == NULL) {
PRINTF("No retransmission data in coap_timer!\n"); LOG_DBG("No retransmission data in coap_timer!\n");
return; return;
} }
++(t->retrans_counter); ++(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); coap_send_transaction(t);
} }
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
@ -96,7 +91,7 @@ coap_new_transaction(uint16_t mid, const coap_endpoint_t *endpoint)
void void
coap_send_transaction(coap_transaction_t *t) 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); 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)) { ((COAP_HEADER_TYPE_MASK & t->message[0]) >> COAP_HEADER_TYPE_POSITION)) {
if(t->retrans_counter < COAP_MAX_RETRANSMIT) { if(t->retrans_counter < COAP_MAX_RETRANSMIT) {
/* not timed out yet */ /* not timed out yet */
PRINTF("Keeping transaction %u\n", t->mid); LOG_DBG("Keeping transaction %u\n", t->mid);
if(t->retrans_counter == 0) { if(t->retrans_counter == 0) {
coap_timer_set_callback(&t->retrans_timer, coap_retransmit_transaction); coap_timer_set_callback(&t->retrans_timer, coap_retransmit_transaction);
@ -112,19 +107,19 @@ coap_send_transaction(coap_transaction_t *t)
t->retrans_interval = t->retrans_interval =
COAP_RESPONSE_TIMEOUT_TICKS + (rand() % COAP_RESPONSE_TIMEOUT_TICKS + (rand() %
COAP_RESPONSE_TIMEOUT_BACKOFF_MASK); COAP_RESPONSE_TIMEOUT_BACKOFF_MASK);
PRINTF("Initial interval %lu msec\n", LOG_DBG("Initial interval %lu msec\n",
(unsigned long)t->retrans_interval); (unsigned long)t->retrans_interval);
} else { } else {
t->retrans_interval <<= 1; /* double */ t->retrans_interval <<= 1; /* double */
PRINTF("Doubled (%u) interval %d s\n", t->retrans_counter, LOG_DBG("Doubled (%u) interval %lu s\n", t->retrans_counter,
t->retrans_interval / 1000); (unsigned long)(t->retrans_interval / 1000));
} }
/* interval updated above */ /* interval updated above */
coap_timer_set(&t->retrans_timer, t->retrans_interval); coap_timer_set(&t->retrans_timer, t->retrans_interval);
} else { } else {
/* timed out */ /* timed out */
PRINTF("Timeout\n"); LOG_DBG("Timeout\n");
coap_resource_response_handler_t callback = t->callback; coap_resource_response_handler_t callback = t->callback;
void *callback_data = t->callback_data; void *callback_data = t->callback_data;
@ -146,7 +141,7 @@ void
coap_clear_transaction(coap_transaction_t *t) coap_clear_transaction(coap_transaction_t *t)
{ {
if(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); coap_timer_stop(&t->retrans_timer);
list_remove(transactions_list, t); 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) { for(t = (coap_transaction_t *)list_head(transactions_list); t; t = t->next) {
if(t->mid == mid) { 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; return t;
} }
} }

View File

@ -36,7 +36,6 @@
*/ */
#include "contiki.h" #include "contiki.h"
#include "sys/cc.h"
#include "net/ipv6/uip-udp-packet.h" #include "net/ipv6/uip-udp-packet.h"
#include "net/ipv6/uiplib.h" #include "net/ipv6/uiplib.h"
#include "coap.h" #include "coap.h"
@ -48,21 +47,14 @@
#include "coap-keystore.h" #include "coap-keystore.h"
#include "coap-keystore-simple.h" #include "coap-keystore-simple.h"
#if UIP_CONF_IPV6_RPL #if UIP_CONF_IPV6_RPL
#include "rpl.h" #include "rpl.h"
#endif /* UIP_CONF_IPV6_RPL */ #endif /* UIP_CONF_IPV6_RPL */
#define DEBUG DEBUG_FULL /* Log configuration */
#include "net/ipv6/uip-debug.h" #include "coap-log.h"
#define LOG_MODULE "coap-uip"
#define DEBUG_VERBOSE ((DEBUG) && 0) #define LOG_LEVEL LOG_LEVEL_COAP
#if DEBUG
#define PRINTEP(X) coap_endpoint_print(X)
#else
#define PRINTEP(X)
#endif
#ifdef WITH_DTLS #ifdef WITH_DTLS
#include "tinydtls.h" #include "tinydtls.h"
@ -70,7 +62,6 @@
#include "dtls_debug.h" #include "dtls_debug.h"
#endif /* WITH_DTLS */ #endif /* WITH_DTLS */
/* sanity check for configured values */ /* sanity check for configured values */
#if COAP_MAX_PACKET_SIZE > (UIP_BUFSIZE - UIP_IPH_LEN - UIP_UDPH_LEN) #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" #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; 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 void
coap_endpoint_print(const coap_endpoint_t *ep) coap_endpoint_print(const coap_endpoint_t *ep)
{ {
if(ep->secure) { if(ep == NULL) {
printf("coaps:"); printf("(NULL EP)");
} else { return;
printf("coap:");
} }
printf("//["); if(ep->secure) {
uip_debug_ipaddr_print(&ep->ipaddr); printf("coaps://[");
} else {
printf("coap://[");
}
uiplib_ipaddr_print(&ep->ipaddr);
printf("]:%u", uip_ntohs(ep->port)); 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 void
coap_endpoint_copy(coap_endpoint_t *destination, coap_endpoint_copy(coap_endpoint_t *destination,
const coap_endpoint_t *from) 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 * Secure CoAP should use a different port but for now
* the same port is used. * 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->port = SERVER_LISTEN_SECURE_PORT;
ep->secure = 1; ep->secure = 1;
} else { } else {
@ -232,14 +270,15 @@ coap_endpoint_is_connected(const coap_endpoint_t *ep)
peer = dtls_get_peer(dtls_context, ep); peer = dtls_get_peer(dtls_context, ep);
if(peer != NULL) { if(peer != NULL) {
/* only if handshake is done! */ /* only if handshake is done! */
PRINTF("DTLS peer state for "); LOG_DBG("DTLS peer state for ");
PRINTEP(ep); LOG_DBG_COAP_EP(ep);
PRINTF(" is %d %d\n", peer->state, dtls_peer_is_connected(peer)); LOG_DBG_(" is %d %d\n", peer->state, dtls_peer_is_connected(peer));
return dtls_peer_is_connected(peer); return dtls_peer_is_connected(peer);
} else { } else {
PRINTF("DTLS did not find peer "); LOG_DBG("DTLS did not find peer ");
PRINTEP(ep); LOG_DBG_COAP_EP(ep);
PRINTF("\n"); LOG_DBG_("\n");
return 0;
} }
} }
#endif /* WITH_DTLS */ #endif /* WITH_DTLS */
@ -252,16 +291,16 @@ int
coap_endpoint_connect(coap_endpoint_t *ep) coap_endpoint_connect(coap_endpoint_t *ep)
{ {
if(ep->secure == 0) { if(ep->secure == 0) {
PRINTF("CoAP connect to "); LOG_DBG("connect to ");
PRINTEP(ep); LOG_DBG_COAP_EP(ep);
PRINTF("\n"); LOG_DBG_("\n");
return 1; return 1;
} }
#ifdef WITH_DTLS #ifdef WITH_DTLS
PRINTF("DTLS connect to "); LOG_DBG("DTLS connect to ");
PRINTEP(ep); LOG_DBG_COAP_EP(ep);
PRINTF("\n"); LOG_DBG_("\n");
/* setup all address info here... should be done to connect */ /* setup all address info here... should be done to connect */
if(dtls_context) { if(dtls_context) {
@ -314,9 +353,9 @@ coap_transport_init(void)
static void static void
process_secure_data(void) process_secure_data(void)
{ {
PRINTF("receiving secure UDP datagram from ["); LOG_INFO("receiving secure UDP datagram from [");
PRINT6ADDR(&UIP_IP_BUF->srcipaddr); LOG_INFO_6ADDR(&UIP_IP_BUF->srcipaddr);
PRINTF("]:%u\n Length: %u\n", uip_ntohs(UIP_UDP_BUF->srcport), LOG_INFO_("]:%u\n Length: %u\n", uip_ntohs(UIP_UDP_BUF->srcport),
uip_datalen()); uip_datalen());
if(dtls_context) { if(dtls_context) {
@ -329,9 +368,9 @@ process_secure_data(void)
static void static void
process_data(void) process_data(void)
{ {
PRINTF("receiving UDP datagram from ["); LOG_INFO("receiving UDP datagram from [");
PRINT6ADDR(&UIP_IP_BUF->srcipaddr); LOG_INFO_6ADDR(&UIP_IP_BUF->srcipaddr);
PRINTF("]:%u\n Length: %u\n", uip_ntohs(UIP_UDP_BUF->srcport), LOG_INFO_("]:%u\n Length: %u\n", uip_ntohs(UIP_UDP_BUF->srcport),
uip_datalen()); uip_datalen());
coap_receive(get_src_endpoint(0), uip_appdata, 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) coap_sendto(const coap_endpoint_t *ep, const uint8_t *data, uint16_t length)
{ {
if(ep == NULL) { if(ep == NULL) {
PRINTF("coap-uip: failed to send - no endpoint\n"); LOG_WARN("failed to send - no endpoint\n");
return -1; return -1;
} }
if(!coap_endpoint_is_connected(ep)) { if(!coap_endpoint_is_connected(ep)) {
PRINTF("coap-uip: endpoint "); LOG_WARN("endpoint ");
PRINTEP(ep); LOG_WARN_COAP_EP(ep);
PRINTF(" not connected - dropping packet\n"); LOG_WARN_(" not connected - dropping packet\n");
return -1; return -1;
} }
@ -358,24 +397,25 @@ coap_sendto(const coap_endpoint_t *ep, const uint8_t *data, uint16_t length)
int ret; int ret;
ret = dtls_write(dtls_context, (session_t *)ep, (uint8_t *)data, length); ret = dtls_write(dtls_context, (session_t *)ep, (uint8_t *)data, length);
PRINTF("coap-uip: sent DTLS to "); LOG_INFO("sent DTLS to ");
PRINTEP(ep); LOG_INFO_COAP_EP(ep);
if(ret < 0) { if(ret < 0) {
PRINTF(" - error %d\n", ret); LOG_INFO_(" - error %d\n", ret);
} else { } else {
PRINTF(" %d/%u bytes\n", ret, length); LOG_INFO_(" %d/%u bytes\n", ret, length);
} }
return ret; return ret;
} } else {
PRINTF("coap-uip: no DTLS context\n"); LOG_WARN("no DTLS context\n");
return -1; return -1;
} }
}
#endif /* WITH_DTLS */ #endif /* WITH_DTLS */
uip_udp_packet_sendto(udp_conn, data, length, &ep->ipaddr, ep->port); uip_udp_packet_sendto(udp_conn, data, length, &ep->ipaddr, ep->port);
PRINTF("coap-uip: sent to "); LOG_INFO("sent to ");
PRINTEP(ep); LOG_INFO_COAP_EP(ep);
PRINTF(" %u bytes\n", length); LOG_INFO_(" %u bytes\n", length);
return length; return length;
} }
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
@ -386,18 +426,18 @@ PROCESS_THREAD(coap_engine, ev, data)
/* new connection with remote host */ /* new connection with remote host */
udp_conn = udp_new(NULL, 0, NULL); udp_conn = udp_new(NULL, 0, NULL);
udp_bind(udp_conn, SERVER_LISTEN_PORT); 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 #ifdef WITH_DTLS
/* create new context with app-data */ /* create new context with app-data */
dtls_conn = udp_new(NULL, 0, NULL); dtls_conn = udp_new(NULL, 0, NULL);
if(dtls_conn != NULL) { if(dtls_conn != NULL) {
udp_bind(dtls_conn, SERVER_LISTEN_SECURE_PORT); 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); dtls_context = dtls_new_context(dtls_conn);
} }
if(!dtls_context) { if(!dtls_context) {
PRINTF("DTLS: cannot create context\n"); LOG_WARN("DTLS: cannot create context\n");
} else { } else {
dtls_set_handler(dtls_context, &cb); dtls_set_handler(dtls_context, &cb);
} }
@ -432,19 +472,20 @@ static int
input_from_peer(struct dtls_context_t *ctx, input_from_peer(struct dtls_context_t *ctx,
session_t *session, uint8_t *data, size_t len) session_t *session, uint8_t *data, size_t len)
{ {
#if DEBUG_VERBOSE
size_t i; size_t i;
PRINTF("received DTLS data:"); if(LOG_DBG_ENABLED) {
LOG_DBG("received DTLS data:");
for(i = 0; i < len; i++) { 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++) { for(i = 0; i < len; i++) {
PRINTF("%02x", data[i]); LOG_DBG_("%02x", data[i]);
}
LOG_DBG_("\n");
} }
PRINTF("\n");
#endif /* DEBUG_VERBOSE */
/* Ensure that the endpoint is tagged as secure */ /* Ensure that the endpoint is tagged as secure */
session->secure = 1; session->secure = 1;
@ -460,9 +501,9 @@ output_to_peer(struct dtls_context_t *ctx,
session_t *session, uint8_t *data, size_t len) session_t *session, uint8_t *data, size_t len)
{ {
struct uip_udp_conn *udp_connection = dtls_get_app_data(ctx); struct uip_udp_conn *udp_connection = dtls_get_app_data(ctx);
PRINTF("coap-uip: output_to DTLS peer ["); LOG_DBG("output_to DTLS peer [");
PRINT6ADDR(&session->ipaddr); LOG_DBG_6ADDR(&session->ipaddr);
PRINTF("]:%u %d bytes\n", session->port, (int)len); LOG_DBG_("]:%u %d bytes\n", session->port, (int)len);
uip_udp_packet_sendto(udp_connection, data, len, uip_udp_packet_sendto(udp_connection, data, len,
&session->ipaddr, session->port); &session->ipaddr, session->port);
return len; return len;
@ -488,18 +529,20 @@ get_psk_info(struct dtls_context_t *ctx,
coap_keystore_psk_entry_t ks; coap_keystore_psk_entry_t ks;
if(dtls_keystore == NULL) { if(dtls_keystore == NULL) {
PRINTF("coap-uip: --- No key store available ---\n"); LOG_DBG("--- No key store available ---\n");
return 0; return 0;
} }
memset(&ks, 0, sizeof(ks)); memset(&ks, 0, sizeof(ks));
PRINTF("coap-uip: ---===>>> Getting the Key or ID <<<===---\n"); LOG_DBG("---===>>> Getting the Key or ID <<<===---\n");
switch(type) { switch(type) {
case DTLS_PSK_IDENTITY: case DTLS_PSK_IDENTITY:
if(id && id_len) { if(id && id_len) {
ks.identity_hint = id; ks.identity_hint = id;
ks.identity_hint_len = id_len; 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) { 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); dtls_keystore->coap_get_psk_info((coap_endpoint_t *)session, &ks);
} }
if(ks.identity == NULL || ks.identity_len == 0) { 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; return 0;
} }
if(result_length < ks.identity_len) { 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); return dtls_alert_fatal_create(DTLS_ALERT_INTERNAL_ERROR);
} }
memcpy(result, ks.identity, ks.identity_len); 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; return ks.identity_len;
case DTLS_PSK_KEY: 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); dtls_keystore->coap_get_psk_info((coap_endpoint_t *)session, &ks);
} }
if(ks.key == NULL || ks.key_len == 0) { 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); return dtls_alert_fatal_create(DTLS_ALERT_ILLEGAL_PARAMETER);
} }
if(result_length < ks.key_len) { 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); return dtls_alert_fatal_create(DTLS_ALERT_INTERNAL_ERROR);
} }
memcpy(result, ks.key, ks.key_len); 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; return ks.key_len;
default: 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); return dtls_alert_fatal_create(DTLS_ALERT_INTERNAL_ERROR);

View File

@ -52,19 +52,10 @@
#include "coap.h" #include "coap.h"
#include "coap-transactions.h" #include "coap-transactions.h"
#define DEBUG 0 /* Log configuration */
#if DEBUG #include "coap-log.h"
#include <stdio.h> #define LOG_MODULE "coap"
#define PRINTF(...) printf(__VA_ARGS__) #define LOG_LEVEL LOG_LEVEL_COAP
#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
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
/*- Variables ---------------------------------------------------------------*/ /*- Variables ---------------------------------------------------------------*/
@ -135,7 +126,7 @@ coap_set_option_header(unsigned int delta, size_t length, uint8_t *buffer)
buffer[++written] = (length - 13); 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; return ++written;
} }
@ -158,7 +149,7 @@ coap_serialize_int_option(unsigned int number, unsigned int current_number,
if(0xFFFFFFFF & value) { if(0xFFFFFFFF & value) {
++i; ++i;
} }
PRINTF("OPTION %u (delta %u, len %zu)\n", number, number - current_number, LOG_DBG("OPTION %u (delta %u, len %zu)\n", number, number - current_number,
i); i);
i = coap_set_option_header(number - current_number, i, buffer); 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; size_t i = 0;
PRINTF("ARRAY type %u, len %zu, full [", number, length); LOG_DBG("ARRAY type %u, len %zu, full [", number, length);
PRINTS(length, array, "%c"); LOG_DBG_COAP_STRING((const char *)array, length);
PRINTF("]\n"); LOG_DBG_("]\n");
if(split_char != '\0') { if(split_char != '\0') {
int j; int j;
@ -196,7 +187,7 @@ coap_serialize_array_option(unsigned int number, unsigned int current_number,
size_t temp_length; size_t temp_length;
for(j = 0; j <= length + 1; ++j) { 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) { if(array[j] == split_char || j == length) {
part_end = array + j; part_end = array + j;
temp_length = part_end - part_start; 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); memcpy(&buffer[i], part_start, temp_length);
i += temp_length; i += temp_length;
PRINTF("OPTION type %u, delta %u, len %zu, part [", number, LOG_DBG("OPTION type %u, delta %u, len %zu, part [", number,
number - current_number, i); number - current_number, i);
PRINTS((int)temp_length, part_start, "%c"); LOG_DBG_COAP_STRING((const char *)part_start, temp_length);
PRINTF("]\n"); LOG_DBG_("]\n");
++j; /* skip the splitter */ ++j; /* skip the splitter */
current_number = number; current_number = number;
part_start = array + j; part_start = array + j;
@ -220,7 +211,7 @@ coap_serialize_array_option(unsigned int number, unsigned int current_number,
memcpy(&buffer[i], array, length); memcpy(&buffer[i], array, length);
i += length; i += length;
PRINTF("OPTION type %u, delta %u, len %zu\n", number, LOG_DBG("OPTION type %u, delta %u, len %zu\n", number,
number - current_number, length); number - current_number, length);
} }
@ -320,7 +311,7 @@ coap_serialize_message(coap_message_t *coap_pkt, uint8_t *buffer)
coap_pkt->buffer = buffer; coap_pkt->buffer = buffer;
coap_pkt->version = 1; 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 */ /* set header fields */
coap_pkt->buffer[0] = 0x00; 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 */ /* empty message, dont need to do more stuff */
if(!coap_pkt->code) { 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; return 4;
} }
/* set Token */ /* 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; option = coap_pkt->buffer + COAP_HEADER_LEN;
for(current_number = 0; current_number < coap_pkt->token_len; for(current_number = 0; current_number < coap_pkt->token_len;
++current_number) { ++current_number) {
PRINTF(" %02X", coap_pkt->token[current_number]); LOG_DBG_(" %02X", coap_pkt->token[current_number]);
*option = coap_pkt->token[current_number]; *option = coap_pkt->token[current_number];
++option; ++option;
} }
PRINTF("-\n"); LOG_DBG_("-\n");
/* Serialize options */ /* Serialize options */
current_number = 0; 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 */ /* The options must be serialized in the order of their number */
COAP_SERIALIZE_BYTE_OPTION(COAP_OPTION_IF_MATCH, if_match, "If-Match"); 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"); "Location-Path");
COAP_SERIALIZE_STRING_OPTION(COAP_OPTION_URI_PATH, uri_path, '/', COAP_SERIALIZE_STRING_OPTION(COAP_OPTION_URI_PATH, 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, COAP_SERIALIZE_INT_OPTION(COAP_OPTION_CONTENT_FORMAT, content_format,
"Content-Format"); "Content-Format");
COAP_SERIALIZE_INT_OPTION(COAP_OPTION_MAX_AGE, max_age, "Max-Age"); 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"); "Proxy-Scheme");
COAP_SERIALIZE_INT_OPTION(COAP_OPTION_SIZE1, size1, "Size1"); 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 */ /* Pack payload */
if((option - coap_pkt->buffer) <= COAP_MAX_HEADER_SIZE) { 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; return 0;
} }
PRINTF("-Done %u B (header len %u, payload len %u)-\n", LOG_DBG("-Done %u B (header len %u, payload len %u)-\n",
(unsigned int)(coap_pkt->payload_len + option - buffer), (unsigned int)(coap_pkt->payload_len + option - buffer),
(unsigned int)(option - buffer), (unsigned int)(option - buffer),
(unsigned int)coap_pkt->payload_len); (unsigned int)coap_pkt->payload_len);
PRINTF("Dump [0x%02X %02X %02X %02X %02X %02X %02X %02X]\n", LOG_DBG("Dump [0x%02X %02X %02X %02X %02X %02X %02X %02X]\n",
coap_pkt->buffer[0], coap_pkt->buffer[0], coap_pkt->buffer[1], coap_pkt->buffer[2],
coap_pkt->buffer[1], coap_pkt->buffer[3], coap_pkt->buffer[4], coap_pkt->buffer[5],
coap_pkt->buffer[2], coap_pkt->buffer[6], coap_pkt->buffer[7]);
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 */ return (option - buffer) + coap_pkt->payload_len; /* message length */
} }
@ -456,7 +443,7 @@ coap_parse_message(coap_message_t *coap_pkt, uint8_t *data, uint16_t data_len)
uint8_t *current_option = data + COAP_HEADER_LEN; uint8_t *current_option = data + COAP_HEADER_LEN;
memcpy(coap_pkt->token, current_option, coap_pkt->token_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", 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_len, coap_pkt->token[0], coap_pkt->token[1],
coap_pkt->token[2], coap_pkt->token[3], coap_pkt->token[4], coap_pkt->token[2], coap_pkt->token[3], coap_pkt->token[4],
coap_pkt->token[5], coap_pkt->token[6], coap_pkt->token[7] coap_pkt->token[5], coap_pkt->token[6], coap_pkt->token[7]
@ -514,7 +501,7 @@ coap_parse_message(coap_message_t *coap_pkt, uint8_t *data, uint16_t data_len)
if(current_option + option_length > data + data_len) { if(current_option + option_length > data + data_len) {
/* Malformed CoAP - out of bounds */ /* Malformed CoAP - out of bounds */
PRINTF("BAD REQUEST: options outside data message: %u > %u\n", LOG_WARN("BAD REQUEST: options outside data message: %u > %u\n",
(unsigned)(current_option + option_length - data), data_len); (unsigned)(current_option + option_length - data), data_len);
return BAD_REQUEST_4_00; return BAD_REQUEST_4_00;
} }
@ -523,11 +510,11 @@ coap_parse_message(coap_message_t *coap_pkt, uint8_t *data, uint16_t data_len)
if(option_number > COAP_OPTION_SIZE1) { if(option_number > COAP_OPTION_SIZE1) {
/* Malformed CoAP - out of bounds */ /* 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; return BAD_REQUEST_4_00;
} }
PRINTF("OPTION %u (delta %u, len %zu): ", option_number, option_delta, LOG_DBG("OPTION %u (delta %u, len %zu): ", option_number, option_delta,
option_length); option_length);
coap_set_option(coap_pkt, option_number); coap_set_option(coap_pkt, option_number);
@ -536,17 +523,17 @@ coap_parse_message(coap_message_t *coap_pkt, uint8_t *data, uint16_t data_len)
case COAP_OPTION_CONTENT_FORMAT: case COAP_OPTION_CONTENT_FORMAT:
coap_pkt->content_format = coap_parse_int_option(current_option, coap_pkt->content_format = coap_parse_int_option(current_option,
option_length); option_length);
PRINTF("Content-Format [%u]\n", coap_pkt->content_format); LOG_DBG_("Content-Format [%u]\n", coap_pkt->content_format);
break; break;
case COAP_OPTION_MAX_AGE: case COAP_OPTION_MAX_AGE:
coap_pkt->max_age = coap_parse_int_option(current_option, coap_pkt->max_age = coap_parse_int_option(current_option,
option_length); 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; break;
case COAP_OPTION_ETAG: case COAP_OPTION_ETAG:
coap_pkt->etag_len = MIN(COAP_ETAG_LEN, option_length); coap_pkt->etag_len = MIN(COAP_ETAG_LEN, option_length);
memcpy(coap_pkt->etag, current_option, coap_pkt->etag_len); memcpy(coap_pkt->etag, current_option, coap_pkt->etag_len);
PRINTF("ETag %u [0x%02X%02X%02X%02X%02X%02X%02X%02X]\n", 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_len, coap_pkt->etag[0], coap_pkt->etag[1],
coap_pkt->etag[2], coap_pkt->etag[3], coap_pkt->etag[4], coap_pkt->etag[2], coap_pkt->etag[3], coap_pkt->etag[4],
coap_pkt->etag[5], coap_pkt->etag[6], coap_pkt->etag[7] coap_pkt->etag[5], coap_pkt->etag[6], coap_pkt->etag[7]
@ -554,13 +541,13 @@ coap_parse_message(coap_message_t *coap_pkt, uint8_t *data, uint16_t data_len)
break; break;
case COAP_OPTION_ACCEPT: case COAP_OPTION_ACCEPT:
coap_pkt->accept = coap_parse_int_option(current_option, option_length); 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; break;
case COAP_OPTION_IF_MATCH: case COAP_OPTION_IF_MATCH:
/* TODO support multiple ETags */ /* TODO support multiple ETags */
coap_pkt->if_match_len = MIN(COAP_ETAG_LEN, option_length); coap_pkt->if_match_len = MIN(COAP_ETAG_LEN, option_length);
memcpy(coap_pkt->if_match, current_option, coap_pkt->if_match_len); 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", 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_len, coap_pkt->if_match[0],
coap_pkt->if_match[1], coap_pkt->if_match[2], coap_pkt->if_match[1], coap_pkt->if_match[2],
coap_pkt->if_match[3], coap_pkt->if_match[4], coap_pkt->if_match[3], coap_pkt->if_match[4],
@ -570,17 +557,17 @@ coap_parse_message(coap_message_t *coap_pkt, uint8_t *data, uint16_t data_len)
break; break;
case COAP_OPTION_IF_NONE_MATCH: case COAP_OPTION_IF_NONE_MATCH:
coap_pkt->if_none_match = 1; coap_pkt->if_none_match = 1;
PRINTF("If-None-Match\n"); LOG_DBG_("If-None-Match\n");
break; break;
case COAP_OPTION_PROXY_URI: case COAP_OPTION_PROXY_URI:
#if COAP_PROXY_OPTION_PROCESSING #if COAP_PROXY_OPTION_PROCESSING
coap_pkt->proxy_uri = (char *)current_option; coap_pkt->proxy_uri = (char *)current_option;
coap_pkt->proxy_uri_len = option_length; coap_pkt->proxy_uri_len = option_length;
#endif #endif /* COAP_PROXY_OPTION_PROCESSING */
PRINTPRE("Proxy-Uri NOT IMPLEMENTED [",(int)coap_pkt->proxy_uri_len, LOG_DBG_("Proxy-Uri NOT IMPLEMENTED [");
coap_pkt->proxy_uri); LOG_DBG_COAP_STRING(coap_pkt->proxy_uri, coap_pkt->proxy_uri_len);
PRINTF("]\n"); LOG_DBG_("]\n");
coap_error_message = "This is a constrained server (Contiki)"; coap_error_message = "This is a constrained server (Contiki)";
return PROXYING_NOT_SUPPORTED_5_05; 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 = (char *)current_option;
coap_pkt->proxy_scheme_len = option_length; coap_pkt->proxy_scheme_len = option_length;
#endif #endif
PRINTPRE("Proxy-Scheme NOT IMPLEMENTED [", LOG_DBG_("Proxy-Scheme NOT IMPLEMENTED [");
(int)coap_pkt->proxy_scheme_len, coap_pkt->proxy_scheme); LOG_DBG_COAP_STRING(coap_pkt->proxy_scheme, coap_pkt->proxy_scheme_len);
PRINTF("]\n"); LOG_DBG_("]\n");
coap_error_message = "This is a constrained server (Contiki)"; coap_error_message = "This is a constrained server (Contiki)";
return PROXYING_NOT_SUPPORTED_5_05; return PROXYING_NOT_SUPPORTED_5_05;
break; 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: case COAP_OPTION_URI_HOST:
coap_pkt->uri_host = (char *)current_option; coap_pkt->uri_host = (char *)current_option;
coap_pkt->uri_host_len = option_length; coap_pkt->uri_host_len = option_length;
PRINTPRE("Uri-Host [", (int)coap_pkt->uri_host_len, LOG_DBG_("Uri-Host [");
coap_pkt->uri_host); LOG_DBG_COAP_STRING(coap_pkt->uri_host, coap_pkt->uri_host_len);
PRINTF("]\n"); LOG_DBG_("]\n");
break; break;
case COAP_OPTION_URI_PORT: case COAP_OPTION_URI_PORT:
coap_pkt->uri_port = coap_parse_int_option(current_option, coap_pkt->uri_port = coap_parse_int_option(current_option,
option_length); option_length);
PRINTF("Uri-Port [%u]\n", coap_pkt->uri_port); LOG_DBG_("Uri-Port [%u]\n", coap_pkt->uri_port);
break; break;
case COAP_OPTION_URI_PATH: 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() 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_merge_multi_option((char **)&(coap_pkt->uri_path),
&(coap_pkt->uri_path_len), current_option, &(coap_pkt->uri_path_len), current_option,
option_length, '/'); option_length, '/');
PRINTPRE("Uri-Path [",(int)coap_pkt->uri_path_len, coap_pkt->uri_path); LOG_DBG_("Uri-Path [");
PRINTF("]\n"); LOG_DBG_COAP_STRING(coap_pkt->uri_path, coap_pkt->uri_path_len);
LOG_DBG_("]\n");
break; break;
case COAP_OPTION_URI_QUERY: 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() 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_merge_multi_option((char **)&(coap_pkt->uri_query),
&(coap_pkt->uri_query_len), current_option, &(coap_pkt->uri_query_len), current_option,
option_length, '&'); option_length, '&');
PRINTPRE("Uri-Query[",(int)coap_pkt->uri_query_len,coap_pkt->uri_query); LOG_DBG_("Uri-Query[");
PRINTF("]\n"); LOG_DBG_COAP_STRING(coap_pkt->uri_query, coap_pkt->uri_query_len);
LOG_DBG_("]\n");
break; break;
case COAP_OPTION_LOCATION_PATH: 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, &(coap_pkt->location_path_len), current_option,
option_length, '/'); option_length, '/');
PRINTPRE("Location-Path [",(int)coap_pkt->location_path_len, LOG_DBG_("Location-Path [");
coap_pkt->location_path); LOG_DBG_COAP_STRING(coap_pkt->location_path, coap_pkt->location_path_len);
PRINTF("]\n"); LOG_DBG_("]\n");
break; break;
case COAP_OPTION_LOCATION_QUERY: 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() 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_merge_multi_option((char **)&(coap_pkt->location_query),
&(coap_pkt->location_query_len), current_option, &(coap_pkt->location_query_len), current_option,
option_length, '&'); option_length, '&');
PRINTPRE("Location-Query [",(int)coap_pkt->location_query_len, LOG_DBG_("Location-Query [");
coap_pkt->location_query); LOG_DBG_COAP_STRING(coap_pkt->location_query, coap_pkt->location_query_len);
PRINTF("]\n"); LOG_DBG_("]\n");
break; break;
case COAP_OPTION_OBSERVE: case COAP_OPTION_OBSERVE:
coap_pkt->observe = coap_parse_int_option(current_option, coap_pkt->observe = coap_parse_int_option(current_option,
option_length); option_length);
PRINTF("Observe [%lu]\n", (unsigned long)coap_pkt->observe); LOG_DBG_("Observe [%lu]\n", (unsigned long)coap_pkt->observe);
break; break;
case COAP_OPTION_BLOCK2: case COAP_OPTION_BLOCK2:
coap_pkt->block2_num = coap_parse_int_option(current_option, coap_pkt->block2_num = coap_parse_int_option(current_option,
@ -659,7 +648,7 @@ 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_offset = (coap_pkt->block2_num & ~0x0000000F)
<< (coap_pkt->block2_num & 0x07); << (coap_pkt->block2_num & 0x07);
coap_pkt->block2_num >>= 4; coap_pkt->block2_num >>= 4;
PRINTF("Block2 [%lu%s (%u B/blk)]\n", LOG_DBG_("Block2 [%lu%s (%u B/blk)]\n",
(unsigned long)coap_pkt->block2_num, (unsigned long)coap_pkt->block2_num,
coap_pkt->block2_more ? "+" : "", coap_pkt->block2_size); coap_pkt->block2_more ? "+" : "", coap_pkt->block2_size);
break; break;
@ -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_offset = (coap_pkt->block1_num & ~0x0000000F)
<< (coap_pkt->block1_num & 0x07); << (coap_pkt->block1_num & 0x07);
coap_pkt->block1_num >>= 4; coap_pkt->block1_num >>= 4;
PRINTF("Block1 [%lu%s (%u B/blk)]\n", LOG_DBG_("Block1 [%lu%s (%u B/blk)]\n",
(unsigned long)coap_pkt->block1_num, (unsigned long)coap_pkt->block1_num,
coap_pkt->block1_more ? "+" : "", coap_pkt->block1_size); coap_pkt->block1_more ? "+" : "", coap_pkt->block1_size);
break; break;
case COAP_OPTION_SIZE2: case COAP_OPTION_SIZE2:
coap_pkt->size2 = coap_parse_int_option(current_option, option_length); 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; break;
case COAP_OPTION_SIZE1: case COAP_OPTION_SIZE1:
coap_pkt->size1 = coap_parse_int_option(current_option, option_length); 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; break;
default: default:
PRINTF("unknown (%u)\n", option_number); LOG_DBG_("unknown (%u)\n", option_number);
/* check if critical (odd) */ /* check if critical (odd) */
if(option_number & 1) { if(option_number & 1) {
coap_error_message = "Unsupported critical option"; 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; current_option += option_length;
} /* for */ } /* for */
PRINTF("-Done parsing-------\n"); LOG_DBG("-Done parsing-------\n");
return NO_ERROR; return NO_ERROR;
} }

View File

@ -146,13 +146,13 @@ coap_is_option(const coap_message_t *message, unsigned int opt)
/* option format serialization */ /* option format serialization */
#define COAP_SERIALIZE_INT_OPTION(number, field, text) \ #define COAP_SERIALIZE_INT_OPTION(number, field, text) \
if(coap_is_option(coap_pkt, number)) { \ 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); \ option += coap_serialize_int_option(number, current_number, option, coap_pkt->field); \
current_number = number; \ current_number = number; \
} }
#define COAP_SERIALIZE_BYTE_OPTION(number, field, text) \ #define COAP_SERIALIZE_BYTE_OPTION(number, field, text) \
if(coap_is_option(coap_pkt, number)) { \ 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[0], \
coap_pkt->field[1], \ coap_pkt->field[1], \
coap_pkt->field[2], \ coap_pkt->field[2], \
@ -167,19 +167,19 @@ coap_is_option(const coap_message_t *message, unsigned int opt)
} }
#define COAP_SERIALIZE_STRING_OPTION(number, field, splitter, text) \ #define COAP_SERIALIZE_STRING_OPTION(number, field, splitter, text) \
if(coap_is_option(coap_pkt, number)) { \ if(coap_is_option(coap_pkt, number)) { \
PRINTPRE(text " [", (int)coap_pkt->field##_len, coap_pkt->field); \ LOG_DBG(text " ["); \
PRINTF("]\n"); \ 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); \ option += coap_serialize_array_option(number, current_number, option, (uint8_t *)coap_pkt->field, coap_pkt->field##_len, splitter); \
current_number = number; \ current_number = number; \
} }
#define COAP_SERIALIZE_BLOCK_OPTION(number, field, text) \ #define COAP_SERIALIZE_BLOCK_OPTION(number, field, text) \
if(coap_is_option(coap_pkt, number)) \ 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); \
PRINTF(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; \ uint32_t block = coap_pkt->field##_num << 4; \
if(coap_pkt->field##_more) { block |= 0x8; } \ if(coap_pkt->field##_more) { block |= 0x8; } \
block |= 0xF & coap_log_2(coap_pkt->field##_size / 16); \ 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); \ option += coap_serialize_int_option(number, current_number, option, block); \
current_number = number; \ current_number = number; \
} }