Updated LWM2M to use the logging module
This commit is contained in:
parent
49d38e6d06
commit
c9d255b847
@ -53,6 +53,10 @@
|
||||
#define LOG_LEVEL_COAP LOG_LEVEL_DBG
|
||||
#endif /* LOG_LEVEL_COAP */
|
||||
|
||||
#ifndef LOG_LEVEL_LWM2M
|
||||
#define LOG_LEVEL_LWM2M LOG_LEVEL_DBG
|
||||
#endif /* LOG_LEVEL_LWM2M */
|
||||
|
||||
#ifndef LOG_WITH_LOC
|
||||
#define LOG_WITH_LOC 0
|
||||
#endif /* LOG_WITH_LOC */
|
||||
|
@ -46,13 +46,10 @@
|
||||
#include "lwm2m-engine.h"
|
||||
#include <string.h>
|
||||
|
||||
#define DEBUG 0
|
||||
#if DEBUG
|
||||
#include <stdio.h>
|
||||
#define PRINTF(...) printf(__VA_ARGS__)
|
||||
#else
|
||||
#define PRINTF(...)
|
||||
#endif
|
||||
/* Log configuration */
|
||||
#include "coap-log.h"
|
||||
#define LOG_MODULE "lwm2m-device"
|
||||
#define LOG_LEVEL LOG_LEVEL_LWM2M
|
||||
|
||||
static const lwm2m_resource_id_t resources[] =
|
||||
{ RO(LWM2M_DEVICE_MANUFACTURER_ID),
|
||||
@ -161,7 +158,7 @@ lwm2m_callback(lwm2m_object_instance_t *object, lwm2m_context_t *ctx)
|
||||
case LWM2M_DEVICE_TYPE_ID:
|
||||
return write_string(ctx, LWM2M_DEVICE_TYPE);
|
||||
case LWM2M_DEVICE_TIME_ID:
|
||||
PRINTF("Reading time: %u\n", (unsigned int)lwm2m_device_get_time());
|
||||
LOG_DBG("Reading time: %u\n", (unsigned int)lwm2m_device_get_time());
|
||||
lwm2m_object_write_int(ctx, lwm2m_device_get_time());
|
||||
return LWM2M_STATUS_OK;
|
||||
case LWM2M_DEVICE_AVAILABLE_POWER_SOURCES:
|
||||
@ -178,14 +175,14 @@ lwm2m_callback(lwm2m_object_instance_t *object, lwm2m_context_t *ctx)
|
||||
sizeof(power_current)/sizeof(uint16_t));
|
||||
return LWM2M_STATUS_OK;
|
||||
default:
|
||||
PRINTF("lwm2m-device: Not found: %u\n", ctx->resource_id);
|
||||
LOG_WARN("Not found: %u\n", ctx->resource_id);
|
||||
return LWM2M_STATUS_NOT_FOUND;
|
||||
}
|
||||
|
||||
} else if(ctx->operation == LWM2M_OP_EXECUTE) {
|
||||
if(ctx->resource_id == LWM2M_DEVICE_REBOOT_ID) {
|
||||
/* Do THE REBOOT */
|
||||
PRINTF("REBOOT\n");
|
||||
LOG_INFO("REBOOT\n");
|
||||
return LWM2M_STATUS_OK;
|
||||
}
|
||||
|
||||
@ -196,12 +193,12 @@ lwm2m_callback(lwm2m_object_instance_t *object, lwm2m_context_t *ctx)
|
||||
len = lwm2m_object_read_int(ctx, ctx->inbuf->buffer, ctx->inbuf->size,
|
||||
&lw_time);
|
||||
if(len == 0) {
|
||||
PRINTF("FAIL: could not write time\n");
|
||||
LOG_WARN("FAIL: could not write time\n");
|
||||
return LWM2M_STATUS_WRITE_ERROR;
|
||||
} else {
|
||||
lwm2m_device_set_time(lw_time);
|
||||
PRINTF("Write time %lu sec => offset = %ld\n",
|
||||
(unsigned long)lw_time, (long)time_offset);
|
||||
LOG_DBG("Write time %lu sec => offset = %ld\n",
|
||||
(unsigned long)lw_time, (long)time_offset);
|
||||
return LWM2M_STATUS_OK;
|
||||
}
|
||||
}
|
||||
|
@ -61,18 +61,10 @@
|
||||
#include "net/ipv6/uip-ds6.h"
|
||||
#endif /* UIP_CONF_IPV6_RPL */
|
||||
|
||||
#define DEBUG 0
|
||||
#if DEBUG
|
||||
#define PRINTF(...) printf(__VA_ARGS__)
|
||||
#define PRINTS(l,s,f) do { int i; \
|
||||
for(i = 0; i < l; i++) printf(f, s[i]); \
|
||||
} while(0)
|
||||
#define PRINTPRE(p,l,s) do { PRINTF(p);PRINTS(l,s,"%c"); } while(0);
|
||||
#else
|
||||
#define PRINTF(...)
|
||||
#define PRINTS(l,s,f)
|
||||
#define PRINTPRE(p,l,s);
|
||||
#endif
|
||||
/* Log configuration */
|
||||
#include "coap-log.h"
|
||||
#define LOG_MODULE "lwm2m-engine"
|
||||
#define LOG_LEVEL LOG_LEVEL_LWM2M
|
||||
|
||||
#ifndef LWM2M_ENGINE_CLIENT_ENDPOINT_PREFIX
|
||||
#ifdef LWM2M_DEVICE_MODEL_NUMBER
|
||||
@ -240,8 +232,8 @@ double_buffer_flush(lwm2m_buffer_t *ctxbuf, lwm2m_buffer_t *outbuf, int size)
|
||||
size = ctxbuf->len;
|
||||
}
|
||||
if(ctxbuf->len >= size && outbuf->size >= size) {
|
||||
PRINTF("Double buffer - copying out %d bytes remaining: %d\n",
|
||||
size, ctxbuf->len - size);
|
||||
LOG_DBG("Double buffer - copying out %d bytes remaining: %d\n",
|
||||
size, ctxbuf->len - size);
|
||||
memcpy(outbuf->buffer, ctxbuf->buffer, size);
|
||||
memcpy(ctxbuf->buffer, &ctxbuf->buffer[size],
|
||||
ctxbuf->len - size);
|
||||
@ -252,7 +244,6 @@ double_buffer_flush(lwm2m_buffer_t *ctxbuf, lwm2m_buffer_t *outbuf, int size)
|
||||
return 0;
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
#if DEBUG
|
||||
static inline const char *
|
||||
get_method_as_string(coap_resource_flags_t method)
|
||||
{
|
||||
@ -268,9 +259,7 @@ get_method_as_string(coap_resource_flags_t method)
|
||||
return "UNKNOWN";
|
||||
}
|
||||
}
|
||||
#endif /* DEBUG */
|
||||
/*--------------------------------------------------------------------------*/
|
||||
#if DEBUG
|
||||
static const char *
|
||||
get_status_as_string(lwm2m_status_t status)
|
||||
{
|
||||
@ -307,7 +296,6 @@ get_status_as_string(lwm2m_status_t status)
|
||||
return buffer;
|
||||
}
|
||||
}
|
||||
#endif /* DEBUG */
|
||||
/*--------------------------------------------------------------------------*/
|
||||
static int
|
||||
parse_path(const char *path, int path_len,
|
||||
@ -319,9 +307,9 @@ parse_path(const char *path, int path_len,
|
||||
char c = 0;
|
||||
|
||||
/* get object id */
|
||||
PRINTF("lwm2m: parse PATH: \"");
|
||||
PRINTS(path_len, path, "%c");
|
||||
PRINTF("\"\n");
|
||||
LOG_DBG("parse PATH: \"");
|
||||
LOG_DBG_COAP_STRING(path, path_len);
|
||||
LOG_DBG_("\"\n");
|
||||
|
||||
ret = 0;
|
||||
pos = 0;
|
||||
@ -374,7 +362,8 @@ void lwm2m_engine_set_opaque_callback(lwm2m_context_t *ctx, lwm2m_write_opaque_c
|
||||
{
|
||||
/* Here we should set the callback for the opaque that we are currently generating... */
|
||||
/* And we should in the future associate the callback with the CoAP message info - MID */
|
||||
PRINTF("Setting opaque handler - offset: %d,%d\n", ctx->offset, ctx->outbuf->len);
|
||||
LOG_DBG("Setting opaque handler - offset: %d,%d\n",
|
||||
ctx->offset, ctx->outbuf->len);
|
||||
|
||||
current_opaque_offset = 0;
|
||||
current_opaque_callback = cb;
|
||||
@ -393,14 +382,14 @@ lwm2m_engine_set_rd_data(lwm2m_buffer_t *outbuf, int block)
|
||||
if(lwm2m_buf_lock[0] != 0 && (lwm2m_buf_lock_timeout > coap_timer_uptime()) &&
|
||||
((lwm2m_buf_lock[1] != 0xffff) ||
|
||||
(lwm2m_buf_lock[2] != 0xffff))) {
|
||||
PRINTF("Set-RD: already exporting resource: %d/%d/%d\n",
|
||||
lwm2m_buf_lock[1], lwm2m_buf_lock[2], lwm2m_buf_lock[3]);
|
||||
LOG_DBG("Set-RD: already exporting resource: %d/%d/%d\n",
|
||||
lwm2m_buf_lock[1], lwm2m_buf_lock[2], lwm2m_buf_lock[3]);
|
||||
/* fail - what should we return here? */
|
||||
return 0;
|
||||
}
|
||||
|
||||
if(block == 0) {
|
||||
PRINTF("Starting RD genereation\n");
|
||||
LOG_DBG("Starting RD generation\n");
|
||||
/* start with simple object instances */
|
||||
instance = list_head(object_list);
|
||||
object = NULL;
|
||||
@ -427,22 +416,22 @@ lwm2m_engine_set_rd_data(lwm2m_buffer_t *outbuf, int block)
|
||||
|
||||
lwm2m_buf_lock_timeout = coap_timer_uptime() + 1000;
|
||||
|
||||
PRINTF("Generating RD list:");
|
||||
LOG_DBG("Generating RD list:");
|
||||
while(instance != NULL || object != NULL) {
|
||||
int pos = lwm2m_buf.len;
|
||||
if(instance != NULL) {
|
||||
len = snprintf((char *) &lwm2m_buf.buffer[pos],
|
||||
lwm2m_buf.size - pos, (pos > 0 || block > 0) ? ",</%d/%d>" : "</%d/%d>",
|
||||
instance->object_id, instance->instance_id);
|
||||
PRINTF((pos > 0 || block > 0) ? ",</%d/%d>" : "</%d/%d>",
|
||||
instance->object_id, instance->instance_id);
|
||||
LOG_DBG_((pos > 0 || block > 0) ? ",</%d/%d>" : "</%d/%d>",
|
||||
instance->object_id, instance->instance_id);
|
||||
} else if(object->impl != NULL) {
|
||||
len = snprintf((char *) &lwm2m_buf.buffer[pos],
|
||||
lwm2m_buf.size - pos,
|
||||
(pos > 0 || block > 0) ? ",</%d>" : "</%d>",
|
||||
object->impl->object_id);
|
||||
PRINTF((pos > 0 || block > 0) ? ",</%d>" : "</%d>",
|
||||
object->impl->object_id);
|
||||
LOG_DBG_((pos > 0 || block > 0) ? ",</%d>" : "</%d>",
|
||||
object->impl->object_id);
|
||||
} else {
|
||||
len = 0;
|
||||
}
|
||||
@ -476,7 +465,8 @@ lwm2m_engine_set_rd_data(lwm2m_buffer_t *outbuf, int block)
|
||||
}
|
||||
|
||||
if(lwm2m_buf.len >= maxsize) {
|
||||
PRINTF("**** CoAP MAX BLOCK Reached!!! **** SEND\n");
|
||||
LOG_DBG_("\n");
|
||||
LOG_DBG("**** CoAP MAX BLOCK Reached!!! **** SEND\n");
|
||||
/* If the produced data is larger than a CoAP block we need to send
|
||||
this now */
|
||||
double_buffer_flush(&lwm2m_buf, outbuf, maxsize);
|
||||
@ -484,7 +474,7 @@ lwm2m_engine_set_rd_data(lwm2m_buffer_t *outbuf, int block)
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
PRINTF("\n");
|
||||
LOG_DBG_("\n");
|
||||
double_buffer_flush(&lwm2m_buf, outbuf, maxsize);
|
||||
/* unlock the buffer */
|
||||
lwm2m_buf_lock[0] = 0;
|
||||
@ -580,7 +570,7 @@ lwm2m_engine_select_writer(lwm2m_context_t *context, unsigned int accept)
|
||||
context->writer = &lwm2m_json_writer;
|
||||
break;
|
||||
default:
|
||||
PRINTF("Unknown Accept type %u, using LWM2M plain text\n", accept);
|
||||
LOG_WARN("Unknown Accept type %u, using LWM2M plain text\n", accept);
|
||||
context->writer = &lwm2m_plain_text_writer;
|
||||
/* Set the response type to plain text */
|
||||
accept = LWM2M_TEXT_PLAIN;
|
||||
@ -613,8 +603,8 @@ lwm2m_engine_select_reader(lwm2m_context_t *context, unsigned int content_format
|
||||
context->reader = &lwm2m_plain_text_reader;
|
||||
break;
|
||||
default:
|
||||
PRINTF("Unknown content type %u, using LWM2M plain text\n",
|
||||
content_format);
|
||||
LOG_WARN("Unknown content type %u, using LWM2M plain text\n",
|
||||
content_format);
|
||||
context->reader = &lwm2m_plain_text_reader;
|
||||
break;
|
||||
}
|
||||
@ -660,13 +650,14 @@ perform_multi_resource_read_op(lwm2m_object_t *object,
|
||||
((lwm2m_buf_lock[1] != ctx->object_id) ||
|
||||
(lwm2m_buf_lock[2] != ctx->object_instance_id) ||
|
||||
(lwm2m_buf_lock[3] != ctx->resource_id))) {
|
||||
PRINTF("Multi-read: already exporting resource: %d/%d/%d\n",
|
||||
lwm2m_buf_lock[1], lwm2m_buf_lock[2], lwm2m_buf_lock[3]);
|
||||
LOG_DBG("Multi-read: already exporting resource: %d/%d/%d\n",
|
||||
lwm2m_buf_lock[1], lwm2m_buf_lock[2], lwm2m_buf_lock[3]);
|
||||
return LWM2M_STATUS_SERVICE_UNAVAILABLE;
|
||||
}
|
||||
|
||||
PRINTF("MultiRead: %d/%d/%d lv:%d offset:%d\n",
|
||||
ctx->object_id, ctx->object_instance_id, ctx->resource_id, ctx->level, ctx->offset);
|
||||
LOG_DBG("MultiRead: %d/%d/%d lv:%d offset:%d\n",
|
||||
ctx->object_id, ctx->object_instance_id, ctx->resource_id,
|
||||
ctx->level, ctx->offset);
|
||||
|
||||
/* Make use of the double buffer */
|
||||
ctx->outbuf = &lwm2m_buf;
|
||||
@ -705,7 +696,10 @@ perform_multi_resource_read_op(lwm2m_object_t *object,
|
||||
if(instance->resource_ids != NULL && instance->resource_count > 0) {
|
||||
/* show all the available resources (or read all) */
|
||||
while(last_rsc_pos < instance->resource_count) {
|
||||
PRINTF("READ: %x %x %x lv:%d\n", instance->resource_ids[last_rsc_pos], RSC_ID(instance->resource_ids[last_rsc_pos]), ctx->resource_id, ctx->level);
|
||||
LOG_DBG("READ: %x %x %x lv:%d\n",
|
||||
instance->resource_ids[last_rsc_pos],
|
||||
RSC_ID(instance->resource_ids[last_rsc_pos]),
|
||||
ctx->resource_id, ctx->level);
|
||||
|
||||
/* Check if this is a object read or if it is the correct resource */
|
||||
if(ctx->level < 3 || ctx->resource_id == RSC_ID(instance->resource_ids[last_rsc_pos])) {
|
||||
@ -731,7 +725,7 @@ perform_multi_resource_read_op(lwm2m_object_t *object,
|
||||
if(len < 0 || ctx->outbuf->len >= size) {
|
||||
double_buffer_flush(ctx->outbuf, outbuf, size);
|
||||
|
||||
PRINTF("Copied lwm2m buf - remaining: %d\n", lwm2m_buf.len);
|
||||
LOG_DBG("Copied lwm2m buf - remaining: %d\n", lwm2m_buf.len);
|
||||
/* switch buffer */
|
||||
ctx->outbuf = outbuf;
|
||||
ctx->writer_flags |= WRITER_HAS_MORE;
|
||||
@ -764,20 +758,20 @@ perform_multi_resource_read_op(lwm2m_object_t *object,
|
||||
/* Now we need to initialize the object writing for this new object */
|
||||
len = ctx->writer->init_write(ctx);
|
||||
ctx->outbuf->len += len;
|
||||
PRINTF("INIT WRITE len:%d size:%d\n", len, (int) ctx->outbuf->size);
|
||||
LOG_DBG("INIT WRITE len:%d size:%d\n", len, (int) ctx->outbuf->size);
|
||||
initialized = 1;
|
||||
}
|
||||
|
||||
if(current_opaque_callback == NULL) {
|
||||
PRINTF("Doing the callback to the resource %d\n", ctx->outbuf->len);
|
||||
LOG_DBG("Doing the callback to the resource %d\n", ctx->outbuf->len);
|
||||
/* No special opaque callback to handle - use regular callback */
|
||||
success = instance->callback(instance, ctx);
|
||||
PRINTF("After the callback to the resource %d: %s\n",
|
||||
ctx->outbuf->len, get_status_as_string(success));
|
||||
LOG_DBG("After the callback to the resource %d: %s\n",
|
||||
ctx->outbuf->len, get_status_as_string(success));
|
||||
|
||||
if(success != LWM2M_STATUS_OK) {
|
||||
/* What to do here? */
|
||||
PRINTF("Callback failed: %s\n", get_status_as_string(success));
|
||||
LOG_DBG("Callback failed: %s\n", get_status_as_string(success));
|
||||
if(lv < 3) {
|
||||
if(success == LWM2M_STATUS_NOT_FOUND) {
|
||||
/* ok with a not found during a multi read - what more
|
||||
@ -799,11 +793,11 @@ perform_multi_resource_read_op(lwm2m_object_t *object,
|
||||
we should produce data via that callback until the opaque has fully
|
||||
been handled */
|
||||
ctx->offset = current_opaque_offset;
|
||||
/* PRINTF("Calling the opaque handler %x\n", ctx->writer_flags); */
|
||||
/* LOG_DBG("Calling the opaque handler %x\n", ctx->writer_flags); */
|
||||
success = current_opaque_callback(instance, ctx, num_write);
|
||||
if((ctx->writer_flags & WRITER_HAS_MORE) == 0) {
|
||||
/* This opaque stream is now done! */
|
||||
/* PRINTF("Setting opaque callback to null - it is done!\n"); */
|
||||
/* LOG_DBG("Setting opaque callback to null - it is done!\n"); */
|
||||
current_opaque_callback = NULL;
|
||||
} else if(ctx->outbuf->len < COAP_MAX_BLOCK_SIZE) {
|
||||
lwm2m_buf_lock[0] = 0;
|
||||
@ -811,21 +805,21 @@ perform_multi_resource_read_op(lwm2m_object_t *object,
|
||||
}
|
||||
current_opaque_offset += num_write;
|
||||
ctx->offset = old_offset;
|
||||
/* PRINTF("Setting back offset to: %d\n", ctx->offset); */
|
||||
/* LOG_DBG("Setting back offset to: %d\n", ctx->offset); */
|
||||
}
|
||||
|
||||
/* here we have "read" out something */
|
||||
num_read++;
|
||||
/* We will need to handle no-success and other things */
|
||||
PRINTF("Called %u/%u/%u outlen:%u %s\n",
|
||||
ctx->object_id, ctx->object_instance_id, ctx->resource_id,
|
||||
ctx->outbuf->len, get_status_as_string(success));
|
||||
LOG_DBG("Called %u/%u/%u outlen:%u %s\n",
|
||||
ctx->object_id, ctx->object_instance_id, ctx->resource_id,
|
||||
ctx->outbuf->len, get_status_as_string(success));
|
||||
|
||||
/* we need to handle full buffer, etc here also! */
|
||||
ctx->level = lv;
|
||||
} else {
|
||||
PRINTF("Resource %u not readable\n",
|
||||
RSC_ID(instance->resource_ids[last_rsc_pos]));
|
||||
LOG_DBG("Resource %u not readable\n",
|
||||
RSC_ID(instance->resource_ids[last_rsc_pos]));
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -833,18 +827,18 @@ perform_multi_resource_read_op(lwm2m_object_t *object,
|
||||
/* This resource is now done - (only when the opaque is also done) */
|
||||
last_rsc_pos++;
|
||||
} else {
|
||||
PRINTF("Opaque is set - continue with that.\n");
|
||||
LOG_DBG("Opaque is set - continue with that.\n");
|
||||
}
|
||||
|
||||
if(ctx->outbuf->len >= COAP_MAX_BLOCK_SIZE) {
|
||||
PRINTF("**** CoAP MAX BLOCK Reached!!! **** SEND\n");
|
||||
LOG_DBG("**** CoAP MAX BLOCK Reached!!! **** SEND\n");
|
||||
/* If the produced data is larger than a CoAP block we need to send
|
||||
this now */
|
||||
if(ctx->outbuf->len < 2 * COAP_MAX_BLOCK_SIZE) {
|
||||
/* We assume that size is equal to COAP_MAX_BLOCK_SIZE here */
|
||||
double_buffer_flush(ctx->outbuf, outbuf, size);
|
||||
|
||||
PRINTF("Copied lwm2m buf - remaining: %d\n", lwm2m_buf.len);
|
||||
LOG_DBG("Copied lwm2m buf - remaining: %d\n", lwm2m_buf.len);
|
||||
/* switch buffer */
|
||||
ctx->outbuf = outbuf;
|
||||
ctx->writer_flags |= WRITER_HAS_MORE;
|
||||
@ -852,7 +846,7 @@ perform_multi_resource_read_op(lwm2m_object_t *object,
|
||||
/* OK - everything went well... but we have more. - keep the lock here! */
|
||||
return LWM2M_STATUS_OK;
|
||||
} else {
|
||||
PRINTF("*** ERROR Overflow?\n");
|
||||
LOG_WARN("*** ERROR Overflow?\n");
|
||||
return LWM2M_STATUS_ERROR;
|
||||
}
|
||||
}
|
||||
@ -866,10 +860,10 @@ perform_multi_resource_read_op(lwm2m_object_t *object,
|
||||
last_instance_id = NO_INSTANCE;
|
||||
}
|
||||
if(ctx->operation == LWM2M_OP_READ) {
|
||||
PRINTF("END Writer %d ->", ctx->outbuf->len);
|
||||
LOG_DBG("END Writer %d ->", ctx->outbuf->len);
|
||||
len = ctx->writer->end_write(ctx);
|
||||
ctx->outbuf->len += len;
|
||||
PRINTF("%d\n", ctx->outbuf->len);
|
||||
LOG_DBG("%d\n", ctx->outbuf->len);
|
||||
}
|
||||
|
||||
initialized = 0;
|
||||
@ -896,7 +890,7 @@ perform_multi_resource_read_op(lwm2m_object_t *object,
|
||||
lwm2m_buf_lock[0] = 0;
|
||||
}
|
||||
|
||||
PRINTF("At END: Copied lwm2m buf %d\n", len);
|
||||
LOG_DBG("At END: Copied lwm2m buf %d\n", len);
|
||||
|
||||
return LWM2M_STATUS_OK;
|
||||
}
|
||||
@ -913,7 +907,7 @@ create_instance(lwm2m_context_t *context, lwm2m_object_t *object)
|
||||
/* NOTE: context->object_instance_id needs to be set before calling */
|
||||
instance = object->impl->create_instance(context->object_instance_id, NULL);
|
||||
if(instance != NULL) {
|
||||
PRINTF("Created instance: %u/%u\n", context->object_id, context->object_instance_id);
|
||||
LOG_DBG("Created instance: %u/%u\n", context->object_id, context->object_instance_id);
|
||||
coap_set_status_code(context->response, CREATED_2_01);
|
||||
#if USE_RD_CLIENT
|
||||
lwm2m_rd_client_set_update_rd();
|
||||
@ -934,8 +928,8 @@ get_or_create_instance(lwm2m_context_t *ctx, lwm2m_object_t *object,
|
||||
lwm2m_object_instance_t *instance;
|
||||
|
||||
instance = get_instance_by_context(ctx, NULL);
|
||||
PRINTF("Instance: %u/%u/%u = %p\n", ctx->object_id,
|
||||
ctx->object_instance_id, ctx->resource_id, instance);
|
||||
LOG_DBG("Instance: %u/%u/%u = %p\n", ctx->object_id,
|
||||
ctx->object_instance_id, ctx->resource_id, instance);
|
||||
/* by default we assume that the instance is not created... so we set flag to zero */
|
||||
if(created != NULL) {
|
||||
*created = 0;
|
||||
@ -980,8 +974,8 @@ process_tlv_write(lwm2m_context_t *ctx, lwm2m_object_t *object,
|
||||
ctx->inbuf->size = len;
|
||||
ctx->level = 3;
|
||||
ctx->resource_id = rid;
|
||||
PRINTF(" Doing callback to %u/%u/%u\n", ctx->object_id,
|
||||
ctx->object_instance_id, ctx->resource_id);
|
||||
LOG_DBG(" Doing callback to %u/%u/%u\n", ctx->object_id,
|
||||
ctx->object_instance_id, ctx->resource_id);
|
||||
instance = get_or_create_instance(ctx, object, &created);
|
||||
if(instance != NULL && instance->callback != NULL) {
|
||||
if(created || check_write(instance, rid)) {
|
||||
@ -1019,11 +1013,15 @@ perform_multi_resource_write_op(lwm2m_object_t *object,
|
||||
while(lwm2m_json_next_token(ctx, &json)) {
|
||||
int i;
|
||||
uint8_t created = 0;
|
||||
PRINTF("JSON: '");
|
||||
for(i = 0; i < json.name_len; i++) PRINTF("%c", json.name[i]);
|
||||
PRINTF("':'");
|
||||
for(i = 0; i < json.value_len; i++) PRINTF("%c", json.value[i]);
|
||||
PRINTF("'\n");
|
||||
LOG_DBG("JSON: '");
|
||||
for(i = 0; i < json.name_len; i++) {
|
||||
LOG_DBG_("%c", json.name[i]);
|
||||
}
|
||||
LOG_DBG_("':'");
|
||||
for(i = 0; i < json.value_len; i++) {
|
||||
LOG_DBG_("%c", json.value[i]);
|
||||
}
|
||||
LOG_DBG_("'\n");
|
||||
if(json.name[0] == 'n') {
|
||||
i = parse_path((const char *) json.value, json.value_len, &oid, &iid, &rid);
|
||||
if(i > 0) {
|
||||
@ -1085,10 +1083,10 @@ perform_multi_resource_write_op(lwm2m_object_t *object,
|
||||
future */
|
||||
|
||||
if(coap_get_header_block1(ctx->request, &num, &more, &size, &offset)) {
|
||||
PRINTF("CoAP BLOCK1: %d/%d/%d offset:%d\n", num, more, size, offset);
|
||||
PRINTF("LWM2M CTX->offset= %d\n", ctx->offset);
|
||||
PRINTF("Last TLV ID:%d final:%d\n", last_tlv_id,
|
||||
lwm2m_object_is_final_incoming(ctx));
|
||||
LOG_DBG("CoAP BLOCK1: %d/%d/%d offset:%d\n", num, more, size, offset);
|
||||
LOG_DBG("LWM2M CTX->offset= %d\n", ctx->offset);
|
||||
LOG_DBG("Last TLV ID:%d final:%d\n", last_tlv_id,
|
||||
lwm2m_object_is_final_incoming(ctx));
|
||||
if(offset > 0) {
|
||||
status = process_tlv_write(ctx, object, last_tlv_id,
|
||||
inbuf, size);
|
||||
@ -1098,7 +1096,7 @@ perform_multi_resource_write_op(lwm2m_object_t *object,
|
||||
|
||||
while(tlvpos < insize) {
|
||||
len = lwm2m_tlv_read(&tlv, &inbuf[tlvpos], insize - tlvpos);
|
||||
PRINTF("Got TLV format First is: type:%d id:%d len:%d (p:%d len:%d/%d)\n",
|
||||
LOG_DBG("Got TLV format First is: type:%d id:%d len:%d (p:%d len:%d/%d)\n",
|
||||
tlv.type, tlv.id, (int) tlv.length,
|
||||
(int) tlvpos, (int) len, (int) insize);
|
||||
if(tlv.type == LWM2M_TLV_TYPE_OBJECT_INSTANCE) {
|
||||
@ -1114,9 +1112,9 @@ perform_multi_resource_write_op(lwm2m_object_t *object,
|
||||
}
|
||||
while(pos < tlv.length && (len2 = lwm2m_tlv_read(&tlv2, &tlv.value[pos],
|
||||
tlv.length - pos))) {
|
||||
PRINTF(" TLV type:%d id:%d len:%d (len:%d/%d)\n",
|
||||
tlv2.type, tlv2.id, (int) tlv2.length,
|
||||
(int) len2, (int) insize);
|
||||
LOG_DBG(" TLV type:%d id:%d len:%d (len:%d/%d)\n",
|
||||
tlv2.type, tlv2.id, (int)tlv2.length,
|
||||
(int)len2, (int)insize);
|
||||
if(tlv2.type == LWM2M_TLV_TYPE_RESOURCE) {
|
||||
last_tlv_id = tlv2.id;
|
||||
status = process_tlv_write(ctx, object, tlv2.id,
|
||||
@ -1172,13 +1170,12 @@ lwm2m_engine_add_object(lwm2m_object_instance_t *object)
|
||||
|
||||
if(object == NULL || object->callback == NULL) {
|
||||
/* Insufficient object configuration */
|
||||
PRINTF("lwm2m-engine: failed to register NULL object\n");
|
||||
LOG_DBG("failed to register NULL object\n");
|
||||
return 0;
|
||||
}
|
||||
if(get_object(object->object_id) != NULL) {
|
||||
/* A generic object with this id has already been registered */
|
||||
PRINTF("lwm2m-engine: object with id %u already registered\n",
|
||||
object->object_id);
|
||||
LOG_DBG("object with id %u already registered\n", object->object_id);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -1187,7 +1184,7 @@ lwm2m_engine_add_object(lwm2m_object_instance_t *object)
|
||||
instance = instance->next) {
|
||||
if(object->object_id == instance->object_id) {
|
||||
if(object->instance_id == instance->instance_id) {
|
||||
PRINTF("lwm2m-engine: object with id %u/%u already registered\n",
|
||||
LOG_DBG("object with id %u/%u already registered\n",
|
||||
instance->object_id, instance->instance_id);
|
||||
return 0;
|
||||
}
|
||||
@ -1236,19 +1233,19 @@ lwm2m_engine_add_generic_object(lwm2m_object_t *object)
|
||||
|| object->impl->get_first == NULL
|
||||
|| object->impl->get_next == NULL
|
||||
|| object->impl->get_by_id == NULL) {
|
||||
PRINTF("lwm2m-engine: failed to register NULL object\n");
|
||||
LOG_WARN("failed to register NULL object\n");
|
||||
return 0;
|
||||
}
|
||||
if(get_object(object->impl->object_id) != NULL) {
|
||||
/* A generic object with this id has already been registered */
|
||||
PRINTF("lwm2m-engine: object with id %u already registered\n",
|
||||
object->impl->object_id);
|
||||
LOG_WARN("object with id %u already registered\n",
|
||||
object->impl->object_id);
|
||||
return 0;
|
||||
}
|
||||
if(has_non_generic_object(object->impl->object_id)) {
|
||||
/* An object with this id has already been registered */
|
||||
PRINTF("lwm2m-engine: object with id %u already registered\n",
|
||||
object->impl->object_id);
|
||||
LOG_WARN("object with id %u already registered\n",
|
||||
object->impl->object_id);
|
||||
return 0;
|
||||
}
|
||||
list_add(generic_object_list, object);
|
||||
@ -1346,9 +1343,9 @@ lwm2m_handler_callback(coap_message_t *request, coap_message_t *response,
|
||||
uint16_t bsize;
|
||||
coap_get_header_block1(request, NULL, NULL, &bsize, NULL);
|
||||
|
||||
PRINTF("Block1 size:%d\n", bsize);
|
||||
LOG_DBG("Block1 size:%d\n", bsize);
|
||||
if(bsize > COAP_MAX_BLOCK_SIZE) {
|
||||
PRINTF("Entity too large...\n");
|
||||
LOG_WARN("Entity too large: %u...\n", bsize);
|
||||
coap_set_status_code(response, REQUEST_ENTITY_TOO_LARGE_4_13);
|
||||
coap_set_header_size1(response, COAP_MAX_BLOCK_SIZE);
|
||||
return COAP_HANDLER_STATUS_PROCESSED;
|
||||
@ -1364,7 +1361,7 @@ lwm2m_handler_callback(coap_message_t *request, coap_message_t *response,
|
||||
url_len = coap_get_header_uri_path(request, &url);
|
||||
|
||||
if(url_len == 2 && strncmp("bs", url, 2) == 0) {
|
||||
PRINTF("BOOTSTRAPPED!!!\n");
|
||||
LOG_INFO("BOOTSTRAPPED!!!\n");
|
||||
coap_set_status_code(response, CHANGED_2_04);
|
||||
return COAP_HANDLER_STATUS_PROCESSED;
|
||||
}
|
||||
@ -1376,13 +1373,13 @@ lwm2m_handler_callback(coap_message_t *request, coap_message_t *response,
|
||||
return COAP_HANDLER_STATUS_CONTINUE;
|
||||
}
|
||||
|
||||
PRINTF("%s URL:'", get_method_as_string(coap_get_method_type(request)));
|
||||
PRINTS(url_len, url, "%c");
|
||||
PRINTF("' CTX:%u/%u/%u dp:%u bs:%d\n", context.object_id, context.object_instance_id,
|
||||
LOG_DBG("%s URL:'", get_method_as_string(coap_get_method_type(request)));
|
||||
LOG_DBG_COAP_STRING(url, url_len);
|
||||
LOG_DBG_("' CTX:%u/%u/%u dp:%u bs:%d\n", context.object_id, context.object_instance_id,
|
||||
context.resource_id, depth, buffer_size);
|
||||
/* Get format and accept */
|
||||
if(!coap_get_header_content_format(request, &format)) {
|
||||
PRINTF("lwm2m: No format given. Assume text plain...\n");
|
||||
LOG_DBG("No format given. Assume text plain...\n");
|
||||
format = TEXT_PLAIN;
|
||||
} else if(format == LWM2M_TEXT_PLAIN) {
|
||||
/* CoAP content format text plain - assume LWM2M text plain */
|
||||
@ -1390,11 +1387,10 @@ lwm2m_handler_callback(coap_message_t *request, coap_message_t *response,
|
||||
}
|
||||
if(!coap_get_header_accept(request, &accept)) {
|
||||
if(format == TEXT_PLAIN && depth < 3) {
|
||||
PRINTF("lwm2m: No Accept header, assume JSON\n");
|
||||
LOG_DBG("No Accept header, assume JSON\n");
|
||||
accept = LWM2M_JSON;
|
||||
} else {
|
||||
PRINTF("lwm2m: No Accept header, using same as content-format: %d\n",
|
||||
format);
|
||||
LOG_DBG("No Accept header, using same as content-format: %d\n", format);
|
||||
accept = format;
|
||||
}
|
||||
}
|
||||
@ -1407,7 +1403,7 @@ lwm2m_handler_callback(coap_message_t *request, coap_message_t *response,
|
||||
if(depth < 1) {
|
||||
/* No possible object id found in URL - ignore request unless delete all */
|
||||
if(coap_get_method_type(request) == METHOD_DELETE) {
|
||||
PRINTF("This is a delete all - for bootstrap...\n");
|
||||
LOG_DBG("This is a delete all - for bootstrap...\n");
|
||||
context.operation = LWM2M_OP_DELETE;
|
||||
coap_set_status_code(response, DELETED_2_02);
|
||||
|
||||
@ -1435,8 +1431,8 @@ lwm2m_handler_callback(coap_message_t *request, coap_message_t *response,
|
||||
/* ALLOW generic instance if CREATE / WRITE*/
|
||||
instance = create_instance(&context, object);
|
||||
if(instance == NULL) {
|
||||
PRINTF("lwm2m-engine: failed to create instance %u/%u\n",
|
||||
context.object_id, context.object_instance_id);
|
||||
LOG_WARN("failed to create instance %u/%u\n",
|
||||
context.object_id, context.object_instance_id);
|
||||
}
|
||||
}
|
||||
|
||||
@ -1449,9 +1445,9 @@ lwm2m_handler_callback(coap_message_t *request, coap_message_t *response,
|
||||
return COAP_HANDLER_STATUS_CONTINUE;
|
||||
}
|
||||
|
||||
PRINTF("lwm2m: Context: %u/%u/%u found: %d\n",
|
||||
context.object_id,
|
||||
context.object_instance_id, context.resource_id, depth);
|
||||
LOG_INFO("Context: %u/%u/%u found: %d\n",
|
||||
context.object_id, context.object_instance_id,
|
||||
context.resource_id, depth);
|
||||
|
||||
/*
|
||||
* Select reader and writer based on provided Content type and
|
||||
@ -1492,24 +1488,25 @@ lwm2m_handler_callback(coap_message_t *request, coap_message_t *response,
|
||||
break;
|
||||
}
|
||||
|
||||
#if DEBUG
|
||||
/* for debugging */
|
||||
PRINTPRE("lwm2m: [", url_len, url);
|
||||
PRINTF("] %s Format:%d ID:%d bsize:%u offset:%d\n",
|
||||
get_method_as_string(coap_get_method_type(request)),
|
||||
format, context.object_id, buffer_size,
|
||||
offset != NULL ? ((int)*offset) : 0);
|
||||
if(format == TEXT_PLAIN) {
|
||||
/* a string */
|
||||
const uint8_t *data;
|
||||
int plen = coap_get_payload(request, &data);
|
||||
if(plen > 0) {
|
||||
PRINTF("Data: '");
|
||||
PRINTS(plen, data, "%c");
|
||||
PRINTF("'\n");
|
||||
if(LOG_DBG_ENABLED) {
|
||||
/* for debugging */
|
||||
LOG_DBG("[");
|
||||
LOG_DBG_COAP_STRING(url, url_len);
|
||||
LOG_DBG_("] %s Format:%d ID:%d bsize:%u offset:%d\n",
|
||||
get_method_as_string(coap_get_method_type(request)),
|
||||
format, context.object_id, buffer_size,
|
||||
offset != NULL ? ((int)*offset) : 0);
|
||||
if(format == TEXT_PLAIN) {
|
||||
/* a string */
|
||||
const uint8_t *data;
|
||||
int plen = coap_get_payload(request, &data);
|
||||
if(plen > 0) {
|
||||
LOG_DBG("Data: '");
|
||||
LOG_DBG_COAP_STRING((const char *)data, plen);
|
||||
LOG_DBG_("'\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif /* DEBUG */
|
||||
|
||||
/* PUT/POST - e.g. write will not send in offset here - Maybe in the future? */
|
||||
if((offset != NULL && *offset == 0) &&
|
||||
@ -1552,19 +1549,20 @@ lwm2m_handler_callback(coap_message_t *request, coap_message_t *response,
|
||||
if(success == LWM2M_STATUS_OK) {
|
||||
/* Handle blockwise 1 */
|
||||
if(coap_is_option(request, COAP_OPTION_BLOCK1)) {
|
||||
PRINTF("Setting BLOCK 1 num:%d o2:%d o:%d\n", (int) bnum, (int) boffset,
|
||||
(int) (offset != NULL ? *offset : 0));
|
||||
LOG_DBG("Setting BLOCK 1 num:%d o2:%d o:%d\n", (int) bnum, (int) boffset,
|
||||
(int) (offset != NULL ? *offset : 0));
|
||||
coap_set_header_block1(response, bnum, 0, bsize);
|
||||
}
|
||||
|
||||
if(context.outbuf->len > 0) {
|
||||
PRINTPRE("lwm2m: [", url_len, url);
|
||||
PRINTF("] replying with %u bytes\n", context.outbuf->len);
|
||||
LOG_DBG("[");
|
||||
LOG_DBG_COAP_STRING(url, url_len);
|
||||
LOG_DBG_("] replying with %u bytes\n", context.outbuf->len);
|
||||
coap_set_payload(response, context.outbuf->buffer, context.outbuf->len);
|
||||
coap_set_header_content_format(response, context.content_type);
|
||||
|
||||
if(offset != NULL) {
|
||||
PRINTF("Setting new offset: oo %d, no: %d\n", *offset, context.offset);
|
||||
LOG_DBG("Setting new offset: oo %d, no: %d\n", *offset, context.offset);
|
||||
if(context.writer_flags & WRITER_HAS_MORE) {
|
||||
*offset = context.offset;
|
||||
} else {
|
||||
@ -1573,8 +1571,9 @@ lwm2m_handler_callback(coap_message_t *request, coap_message_t *response,
|
||||
}
|
||||
}
|
||||
} else {
|
||||
PRINTPRE("lwm2m: [", url_len, url);
|
||||
PRINTF("] no data in reply\n");
|
||||
LOG_DBG("[");
|
||||
LOG_DBG_COAP_STRING(url, url_len);
|
||||
LOG_DBG_("] no data in reply\n");
|
||||
}
|
||||
} else {
|
||||
switch(success) {
|
||||
@ -1598,8 +1597,9 @@ lwm2m_handler_callback(coap_message_t *request, coap_message_t *response,
|
||||
coap_set_status_code(response, INTERNAL_SERVER_ERROR_5_00);
|
||||
break;
|
||||
}
|
||||
PRINTPRE("lwm2m: [", url_len, url);
|
||||
PRINTF("] resource failed: %s\n", get_status_as_string(success));
|
||||
LOG_WARN("[");
|
||||
LOG_WARN_COAP_STRING(url, url_len);
|
||||
LOG_WARN("] resource failed: %s\n", get_status_as_string(success));
|
||||
}
|
||||
return COAP_HANDLER_STATUS_PROCESSED;
|
||||
}
|
||||
|
@ -41,13 +41,10 @@
|
||||
#include "coap.h"
|
||||
#include <string.h>
|
||||
|
||||
#define DEBUG 0
|
||||
#if DEBUG
|
||||
#include <stdio.h>
|
||||
#define PRINTF(...) printf(__VA_ARGS__)
|
||||
#else
|
||||
#define PRINTF(...)
|
||||
#endif
|
||||
/* Log configuration */
|
||||
#include "coap-log.h"
|
||||
#define LOG_MODULE "lwm2m-firmware"
|
||||
#define LOG_LEVEL LOG_LEVEL_LWM2M
|
||||
|
||||
#define UPDATE_PACKAGE 0
|
||||
#define UPDATE_PACKAGE_URI 1
|
||||
@ -87,14 +84,13 @@ static lwm2m_status_t
|
||||
lwm2m_callback(lwm2m_object_instance_t *object,
|
||||
lwm2m_context_t *ctx)
|
||||
{
|
||||
#if DEBUG
|
||||
uint32_t num;
|
||||
uint8_t more;
|
||||
uint16_t size;
|
||||
uint32_t offset;
|
||||
#endif
|
||||
|
||||
PRINTF("Got request at: %d/%d/%d lv:%d\n", ctx->object_id, ctx->object_instance_id, ctx->resource_id, ctx->level);
|
||||
LOG_DBG("Got request at: %d/%d/%d lv:%d\n", ctx->object_id,
|
||||
ctx->object_instance_id, ctx->resource_id, ctx->level);
|
||||
|
||||
if(ctx->level == 1 || ctx->level == 2) {
|
||||
/* Should not happen - as it will be taken care of by the lwm2m engine itself. */
|
||||
@ -111,17 +107,19 @@ lwm2m_callback(lwm2m_object_instance_t *object,
|
||||
return LWM2M_STATUS_OK;
|
||||
}
|
||||
} else if(ctx->operation == LWM2M_OP_WRITE) {
|
||||
#if DEBUG
|
||||
if(coap_get_header_block1(ctx->request, &num, &more, &size, &offset)) {
|
||||
PRINTF("CoAP BLOCK1: %d/%d/%d offset:%d\n", num, more, size, offset);
|
||||
PRINTF("LWM2M CTX->offset= %d\n", ctx->offset);
|
||||
|
||||
if(LOG_DBG_ENABLED) {
|
||||
if(coap_get_header_block1(ctx->request, &num, &more, &size, &offset)) {
|
||||
LOG_DBG("CoAP BLOCK1: %d/%d/%d offset:%d\n", num, more, size, offset);
|
||||
LOG_DBG("LWM2M CTX->offset= %d\n", ctx->offset);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
switch(ctx->resource_id) {
|
||||
case UPDATE_PACKAGE:
|
||||
/* The firmware is written */
|
||||
PRINTF("Firmware received: %d %d fin:%d\n", ctx->offset, (int) ctx->inbuf->size,
|
||||
lwm2m_object_is_final_incoming(ctx));
|
||||
LOG_DBG("Firmware received: %d %d fin:%d\n", ctx->offset,
|
||||
(int)ctx->inbuf->size, lwm2m_object_is_final_incoming(ctx));
|
||||
if(lwm2m_object_is_final_incoming(ctx)) {
|
||||
state = STATE_DOWNLOADED;
|
||||
} else {
|
||||
@ -130,15 +128,15 @@ lwm2m_callback(lwm2m_object_instance_t *object,
|
||||
return LWM2M_STATUS_OK;
|
||||
case UPDATE_PACKAGE_URI:
|
||||
/* The firmware URI is written */
|
||||
PRINTF("Firmware URI received: %d %d fin:%d\n", ctx->offset, (int) ctx->inbuf->size,
|
||||
lwm2m_object_is_final_incoming(ctx));
|
||||
if(DEBUG) {
|
||||
LOG_DBG("Firmware URI received: %d %d fin:%d\n", ctx->offset,
|
||||
(int)ctx->inbuf->size, lwm2m_object_is_final_incoming(ctx));
|
||||
if(LOG_DBG_ENABLED) {
|
||||
int i;
|
||||
PRINTF("Data: '");
|
||||
LOG_DBG("Data: '");
|
||||
for(i = 0; i < ctx->inbuf->size; i++) {
|
||||
PRINTF("%c", ctx->inbuf->buffer[i]);
|
||||
LOG_DBG_("%c", ctx->inbuf->buffer[i]);
|
||||
}
|
||||
PRINTF("'\n");
|
||||
LOG_DBG_("'\n");
|
||||
}
|
||||
return LWM2M_STATUS_OK;
|
||||
}
|
||||
|
@ -49,12 +49,10 @@
|
||||
#include <stdint.h>
|
||||
#include <inttypes.h>
|
||||
|
||||
#define DEBUG 0
|
||||
#if DEBUG
|
||||
#define PRINTF(...) printf(__VA_ARGS__)
|
||||
#else
|
||||
#define PRINTF(...)
|
||||
#endif
|
||||
/* Log configuration */
|
||||
#include "coap-log.h"
|
||||
#define LOG_MODULE "lwm2m-json"
|
||||
#define LOG_LEVEL LOG_LEVEL_NONE
|
||||
/*---------------------------------------------------------------------------*/
|
||||
|
||||
/* {"e":[{"n":"111/1","v":123},{"n":"111/2","v":42}]} */
|
||||
@ -70,7 +68,9 @@
|
||||
|
||||
/* Simlified JSON style reader for reading in values from a LWM2M JSON
|
||||
string */
|
||||
int lwm2m_json_next_token(lwm2m_context_t *ctx, struct json_data *json) {
|
||||
int
|
||||
lwm2m_json_next_token(lwm2m_context_t *ctx, struct json_data *json)
|
||||
{
|
||||
int pos = ctx->inbuf->pos;
|
||||
uint8_t type = T_NONE;
|
||||
uint8_t vpos_start = 0;
|
||||
@ -123,7 +123,7 @@ int lwm2m_json_next_token(lwm2m_context_t *ctx, struct json_data *json) {
|
||||
} else {
|
||||
/* Could be in string or at illegal pos */
|
||||
if(type != T_STRING_B) {
|
||||
PRINTF("ERROR - illegal ':'\n");
|
||||
LOG_DBG("ERROR - illegal ':'\n");
|
||||
}
|
||||
}
|
||||
break;
|
||||
@ -178,7 +178,7 @@ static size_t
|
||||
enter_sub(lwm2m_context_t *ctx)
|
||||
{
|
||||
/* set some flags in state */
|
||||
PRINTF("Enter sub-resource rsc=%d\n", ctx->resource_id);
|
||||
LOG_DBG("Enter sub-resource rsc=%d\n", ctx->resource_id);
|
||||
ctx->writer_flags |= WRITER_RESOURCE_INSTANCE;
|
||||
return 0;
|
||||
}
|
||||
@ -187,7 +187,7 @@ static size_t
|
||||
exit_sub(lwm2m_context_t *ctx)
|
||||
{
|
||||
/* clear out state info */
|
||||
PRINTF("Exit sub-resource rsc=%d\n", ctx->resource_id);
|
||||
LOG_DBG("Exit sub-resource rsc=%d\n", ctx->resource_id);
|
||||
ctx->writer_flags &= ~WRITER_RESOURCE_INSTANCE;
|
||||
return 0;
|
||||
}
|
||||
@ -206,7 +206,7 @@ write_boolean(lwm2m_context_t *ctx, uint8_t *outbuf, size_t outlen,
|
||||
if((len < 0) || (len >= outlen)) {
|
||||
return 0;
|
||||
}
|
||||
PRINTF("JSON: Write bool:%s\n", outbuf);
|
||||
LOG_DBG("JSON: Write bool:%s\n", outbuf);
|
||||
|
||||
ctx->writer_flags |= WRITER_OUTPUT_VALUE;
|
||||
return len;
|
||||
@ -226,7 +226,7 @@ write_int(lwm2m_context_t *ctx, uint8_t *outbuf, size_t outlen,
|
||||
if((len < 0) || (len >= outlen)) {
|
||||
return 0;
|
||||
}
|
||||
PRINTF("JSON: Write int:%s\n", outbuf);
|
||||
LOG_DBG("Write int:%s\n", outbuf);
|
||||
|
||||
ctx->writer_flags |= WRITER_OUTPUT_VALUE;
|
||||
return len;
|
||||
@ -272,7 +272,7 @@ write_string(lwm2m_context_t *ctx, uint8_t *outbuf, size_t outlen,
|
||||
size_t i;
|
||||
size_t len = 0;
|
||||
int res;
|
||||
PRINTF("{\"n\":\"%u\",\"sv\":\"", ctx->resource_id);
|
||||
LOG_DBG("{\"n\":\"%u\",\"sv\":\"", ctx->resource_id);
|
||||
if(ctx->writer_flags & WRITER_RESOURCE_INSTANCE) {
|
||||
res = snprintf((char *)outbuf, outlen, "%s{\"n\":\"%u/%u\",\"sv\":\"", sep,
|
||||
ctx->resource_id, ctx->resource_instance_id);
|
||||
@ -288,7 +288,7 @@ write_string(lwm2m_context_t *ctx, uint8_t *outbuf, size_t outlen,
|
||||
/* Escape special characters */
|
||||
/* TODO: Handle UTF-8 strings */
|
||||
if(value[i] < '\x20') {
|
||||
PRINTF("\\x%x", value[i]);
|
||||
LOG_DBG_("\\x%x", value[i]);
|
||||
res = snprintf((char *)&outbuf[len], outlen - len, "\\x%x", value[i]);
|
||||
if((res < 0) || (res >= (outlen - len))) {
|
||||
return 0;
|
||||
@ -296,27 +296,27 @@ write_string(lwm2m_context_t *ctx, uint8_t *outbuf, size_t outlen,
|
||||
len += res;
|
||||
continue;
|
||||
} else if(value[i] == '"' || value[i] == '\\') {
|
||||
PRINTF("\\");
|
||||
LOG_DBG_("\\");
|
||||
outbuf[len] = '\\';
|
||||
++len;
|
||||
if(len >= outlen) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
PRINTF("%c", value[i]);
|
||||
LOG_DBG_("%c", value[i]);
|
||||
outbuf[len] = value[i];
|
||||
++len;
|
||||
if(len >= outlen) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
PRINTF("\"}\n");
|
||||
LOG_DBG_("\"}\n");
|
||||
res = snprintf((char *)&outbuf[len], outlen - len, "\"}");
|
||||
if((res < 0) || (res >= (outlen - len))) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
PRINTF("JSON: Write string:%s\n", outbuf);
|
||||
LOG_DBG("JSON: Write string:%s\n", outbuf);
|
||||
|
||||
len += res;
|
||||
ctx->writer_flags |= WRITER_OUTPUT_VALUE;
|
||||
|
@ -47,12 +47,10 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define DEBUG 0
|
||||
#if DEBUG
|
||||
#define PRINTF(...) printf(__VA_ARGS__)
|
||||
#else
|
||||
#define PRINTF(...)
|
||||
#endif
|
||||
/* Log configuration */
|
||||
#include "coap-log.h"
|
||||
#define LOG_MODULE "lwm2m-plain-text"
|
||||
#define LOG_LEVEL LOG_LEVEL_NONE
|
||||
|
||||
/*---------------------------------------------------------------------------*/
|
||||
static size_t
|
||||
@ -121,16 +119,16 @@ lwm2m_plain_text_read_float32fix(const uint8_t *inbuf, size_t len,
|
||||
if(frac > 1) {
|
||||
*value += ((counter << bits) / frac);
|
||||
}
|
||||
PRINTF("READ FLOATFIX: \"%.*s\" => int(%ld) frac(%ld) f=%ld Value=%ld\n",
|
||||
(int)len, (char *)inbuf,
|
||||
(long)integerpart,
|
||||
(long)counter,
|
||||
(long)frac,
|
||||
(long)*value);
|
||||
LOG_DBG("READ FLOATFIX: \"%.*s\" => int(%ld) frac(%ld) f=%ld Value=%ld\n",
|
||||
(int)len, (char *)inbuf,
|
||||
(long)integerpart,
|
||||
(long)counter,
|
||||
(long)frac,
|
||||
(long)*value);
|
||||
if(neg) {
|
||||
*value = -*value;
|
||||
}
|
||||
|
||||
|
||||
return i;
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
|
@ -62,23 +62,10 @@
|
||||
#include "rpl.h"
|
||||
#endif /* UIP_CONF_IPV6_RPL */
|
||||
|
||||
#define DEBUG 1
|
||||
#if DEBUG
|
||||
#include <stdio.h>
|
||||
#define PRINTF(...) printf(__VA_ARGS__)
|
||||
#define PRINTS(l,s,f) do { int i; \
|
||||
for(i = 0; i < l; i++) printf(f, s[i]); \
|
||||
} while(0)
|
||||
#define PRINT6ADDR(addr) PRINTF("[%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x]", ((uint8_t *)addr)[0], ((uint8_t *)addr)[1], ((uint8_t *)addr)[2], ((uint8_t *)addr)[3], ((uint8_t *)addr)[4], ((uint8_t *)addr)[5], ((uint8_t *)addr)[6], ((uint8_t *)addr)[7], ((uint8_t *)addr)[8], ((uint8_t *)addr)[9], ((uint8_t *)addr)[10], ((uint8_t *)addr)[11], ((uint8_t *)addr)[12], ((uint8_t *)addr)[13], ((uint8_t *)addr)[14], ((uint8_t *)addr)[15])
|
||||
#define PRINTLLADDR(lladdr) PRINTF("[%02x:%02x:%02x:%02x:%02x:%02x]", (lladdr)->addr[0], (lladdr)->addr[1], (lladdr)->addr[2], (lladdr)->addr[3], (lladdr)->addr[4], (lladdr)->addr[5])
|
||||
#define PRINTEP(ep) coap_endpoint_print(ep)
|
||||
#else
|
||||
#define PRINTF(...)
|
||||
#define PRINTS(l,s,f)
|
||||
#define PRINT6ADDR(addr)
|
||||
#define PRINTLLADDR(addr)
|
||||
#define PRINTEP(ep)
|
||||
#endif
|
||||
/* Log configuration */
|
||||
#include "coap-log.h"
|
||||
#define LOG_MODULE "lwm2m-rd-client"
|
||||
#define LOG_LEVEL LOG_LEVEL_LWM2M
|
||||
|
||||
#ifndef LWM2M_DEFAULT_CLIENT_LIFETIME
|
||||
#define LWM2M_DEFAULT_CLIENT_LIFETIME 600 /* sec */
|
||||
@ -150,7 +137,7 @@ set_rd_data(coap_message_t *request)
|
||||
|
||||
if(rd_more) {
|
||||
/* set the first block here */
|
||||
PRINTF("Setting block1 in request\n");
|
||||
LOG_DBG("Setting block1 in request\n");
|
||||
coap_set_header_block1(request, 0, 1, sizeof(rd_data));
|
||||
}
|
||||
return outbuf.len;
|
||||
@ -162,7 +149,7 @@ prepare_update(coap_message_t *request, int triggered) {
|
||||
coap_set_header_uri_path(request, session_info.assigned_ep);
|
||||
|
||||
snprintf(query_data, sizeof(query_data) - 1, "?lt=%d&b=%s", session_info.lifetime, session_info.binding);
|
||||
PRINTF("UPDATE:%s %s\n", session_info.assigned_ep, query_data);
|
||||
LOG_DBG("UPDATE:%s %s\n", session_info.assigned_ep, query_data);
|
||||
coap_set_header_uri_query(request, query_data);
|
||||
|
||||
if((triggered || rd_flags & FLAG_RD_DATA_UPDATE_ON_DIRTY) && (rd_flags & FLAG_RD_DATA_DIRTY)) {
|
||||
@ -212,8 +199,8 @@ static void
|
||||
perform_session_callback(int state)
|
||||
{
|
||||
if(session_info.callback != NULL) {
|
||||
PRINTF("Performing session callback: %d cb:%p\n",
|
||||
state, session_info.callback);
|
||||
LOG_DBG("Performing session callback: %d cb:%p\n",
|
||||
state, session_info.callback);
|
||||
session_info.callback(&session_info, state);
|
||||
}
|
||||
}
|
||||
@ -360,23 +347,23 @@ update_bootstrap_server(void)
|
||||
static void
|
||||
bootstrap_callback(coap_request_state_t *state)
|
||||
{
|
||||
PRINTF("Bootstrap callback Response: %d, ", state->response != NULL);
|
||||
LOG_DBG("Bootstrap callback Response: %d, ", state->response != NULL);
|
||||
if(state->response) {
|
||||
if(CHANGED_2_04 == state->response->code) {
|
||||
PRINTF("Considered done!\n");
|
||||
LOG_DBG_("Considered done!\n");
|
||||
rd_state = BOOTSTRAP_DONE;
|
||||
return;
|
||||
}
|
||||
/* Possible error response codes are 4.00 Bad request & 4.15 Unsupported content format */
|
||||
PRINTF("Failed with code %d. Retrying\n", state->response->code);
|
||||
LOG_DBG_("Failed with code %d. Retrying\n", state->response->code);
|
||||
/* TODO Application callback? */
|
||||
rd_state = INIT;
|
||||
} else if(BOOTSTRAP_SENT == rd_state) { /* this can handle double invocations */
|
||||
/* Failure! */
|
||||
PRINTF("Bootstrap failed! Retry?");
|
||||
LOG_DBG("Bootstrap failed! Retry?");
|
||||
rd_state = DO_BOOTSTRAP;
|
||||
} else {
|
||||
PRINTF("Ignore\n");
|
||||
LOG_DBG("Ignore\n");
|
||||
}
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
@ -385,7 +372,7 @@ produce_more_rd(void)
|
||||
{
|
||||
lwm2m_buffer_t outbuf;
|
||||
|
||||
PRINTF("GOT Continue!\n");
|
||||
LOG_DBG("GOT Continue!\n");
|
||||
|
||||
/* setup the output buffer */
|
||||
outbuf.buffer = rd_data;
|
||||
@ -398,8 +385,8 @@ produce_more_rd(void)
|
||||
rd_more = lwm2m_engine_set_rd_data(&outbuf, rd_block1);
|
||||
coap_set_payload(request, rd_data, outbuf.len);
|
||||
|
||||
PRINTF("Setting block1 in request - block: %d more: %d\n",
|
||||
(int) rd_block1, (int) rd_more);
|
||||
LOG_DBG("Setting block1 in request - block: %d more: %d\n",
|
||||
(int)rd_block1, (int)rd_more);
|
||||
coap_set_header_block1(request, rd_block1, rd_more, sizeof(rd_data));
|
||||
|
||||
coap_send_request(&rd_request_state, &session_info.server_ep, request, rd_callback);
|
||||
@ -417,7 +404,7 @@ block1_rd_callback(coap_timer_t *timer)
|
||||
static void
|
||||
registration_callback(coap_request_state_t *state)
|
||||
{
|
||||
PRINTF("Registration callback. Response: %d, ", state->response != NULL);
|
||||
LOG_DBG("Registration callback. Response: %d, ", state->response != NULL);
|
||||
if(state->response) {
|
||||
/* check state and possibly set registration to done */
|
||||
/* If we get a continue - we need to call the rd generator one more time */
|
||||
@ -426,6 +413,7 @@ registration_callback(coap_request_state_t *state)
|
||||
coap_get_header_block1(state->response, &rd_block1, NULL, NULL, NULL);
|
||||
coap_timer_set_callback(&block1_timer, block1_rd_callback);
|
||||
coap_timer_set(&block1_timer, 1); /* delay 1 ms */
|
||||
LOG_DBG_("Continue\n");
|
||||
} else if(CREATED_2_01 == state->response->code) {
|
||||
if(state->response->location_path_len < LWM2M_RD_CLIENT_ASSIGNED_ENDPOINT_MAX_LEN) {
|
||||
memcpy(session_info.assigned_ep, state->response->location_path,
|
||||
@ -435,27 +423,27 @@ registration_callback(coap_request_state_t *state)
|
||||
rd_state = REGISTRATION_DONE;
|
||||
/* remember the last reg time */
|
||||
last_update = coap_timer_uptime();
|
||||
PRINTF("Done (assigned EP='%s')!\n", session_info.assigned_ep);
|
||||
LOG_DBG_("Done (assigned EP='%s')!\n", session_info.assigned_ep);
|
||||
perform_session_callback(LWM2M_RD_CLIENT_REGISTERED);
|
||||
return;
|
||||
}
|
||||
|
||||
PRINTF("failed to handle assigned EP: '");
|
||||
PRINTS(state->response->location_path_len,
|
||||
state->response->location_path, "%c");
|
||||
PRINTF("'. Re-init network.\n");
|
||||
LOG_DBG_("failed to handle assigned EP: '");
|
||||
LOG_DBG_COAP_STRING(state->response->location_path,
|
||||
state->response->location_path_len);
|
||||
LOG_DBG_("'. Re-init network.\n");
|
||||
} else {
|
||||
/* Possible error response codes are 4.00 Bad request & 4.03 Forbidden */
|
||||
PRINTF("failed with code %d. Re-init network\n", state->response->code);
|
||||
LOG_DBG_("failed with code %d. Re-init network\n", state->response->code);
|
||||
}
|
||||
/* TODO Application callback? */
|
||||
rd_state = INIT;
|
||||
} else if(REGISTRATION_SENT == rd_state) { /* this can handle double invocations */
|
||||
/* Failure! */
|
||||
PRINTF("Registration failed! Retry?\n");
|
||||
LOG_DBG_("Registration failed! Retry?\n");
|
||||
rd_state = DO_REGISTRATION;
|
||||
} else {
|
||||
PRINTF("Ignore\n");
|
||||
LOG_DBG_("Ignore\n");
|
||||
}
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
@ -465,7 +453,7 @@ registration_callback(coap_request_state_t *state)
|
||||
static void
|
||||
update_callback(coap_request_state_t *state)
|
||||
{
|
||||
PRINTF("Update callback. Response: %d, ", state->response != NULL);
|
||||
LOG_DBG("Update callback. Response: %d, ", state->response != NULL);
|
||||
|
||||
if(state->response) {
|
||||
/* If we get a continue - we need to call the rd generator one more time */
|
||||
@ -475,7 +463,7 @@ update_callback(coap_request_state_t *state)
|
||||
coap_timer_set_callback(&block1_timer, block1_rd_callback);
|
||||
coap_timer_set(&block1_timer, 1); /* delay 1 ms */
|
||||
} else if(CHANGED_2_04 == state->response->code) {
|
||||
PRINTF("Done!\n");
|
||||
LOG_DBG_("Done!\n");
|
||||
/* remember the last reg time */
|
||||
last_update = coap_timer_uptime();
|
||||
rd_state = REGISTRATION_DONE;
|
||||
@ -483,33 +471,33 @@ update_callback(coap_request_state_t *state)
|
||||
return;
|
||||
}
|
||||
/* Possible error response codes are 4.00 Bad request & 4.04 Not Found */
|
||||
PRINTF("Failed with code %d. Retrying registration\n", state->response->code);
|
||||
LOG_DBG_("Failed with code %d. Retrying registration\n", state->response->code);
|
||||
rd_state = DO_REGISTRATION;
|
||||
} else if(REGISTRATION_SENT == rd_state) { /* this can handle the current double invocation */
|
||||
/*Failure! */
|
||||
PRINTF("Registration failed! Retry?");
|
||||
/* Failure! */
|
||||
LOG_DBG("Registration failed! Retry?");
|
||||
rd_state = DO_REGISTRATION;
|
||||
} else if(UPDATE_SENT == rd_state) {
|
||||
/* Update failed */
|
||||
PRINTF("Update failed! Retry?");
|
||||
LOG_DBG("Update failed! Retry?");
|
||||
rd_state = DO_REGISTRATION;
|
||||
} else {
|
||||
PRINTF("Ignore\n");
|
||||
LOG_DBG("Ignore\n");
|
||||
}
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
static void
|
||||
deregister_callback(coap_request_state_t *state)
|
||||
{
|
||||
PRINTF("Deregister callback. Response Code: %d\n",
|
||||
state->response != NULL ? state->response->code : 0);
|
||||
LOG_DBG("Deregister callback. Response Code: %d\n",
|
||||
state->response != NULL ? state->response->code : 0);
|
||||
|
||||
if(state->response && (DELETED_2_02 == state->response->code)) {
|
||||
PRINTF("Deregistration success\n");
|
||||
LOG_DBG("Deregistration success\n");
|
||||
rd_state = DEREGISTERED;
|
||||
perform_session_callback(LWM2M_RD_CLIENT_DEREGISTERED);
|
||||
} else {
|
||||
PRINTF("Deregistration failed\n");
|
||||
LOG_DBG("Deregistration failed\n");
|
||||
if(rd_state == DEREGISTER_SENT) {
|
||||
rd_state = DEREGISTER_FAILED;
|
||||
perform_session_callback(LWM2M_RD_CLIENT_DEREGISTER_FAILED);
|
||||
@ -527,19 +515,19 @@ periodic_process(coap_timer_t *timer)
|
||||
coap_timer_reset(&rd_timer, STATE_MACHINE_UPDATE_INTERVAL);
|
||||
now = coap_timer_uptime();
|
||||
|
||||
PRINTF("RD Client - state: %d, ms: %lu\n", rd_state,
|
||||
(unsigned long)coap_timer_uptime());
|
||||
LOG_DBG("RD Client - state: %d, ms: %lu\n", rd_state,
|
||||
(unsigned long)coap_timer_uptime());
|
||||
|
||||
switch(rd_state) {
|
||||
case INIT:
|
||||
PRINTF("RD Client started with endpoint '%s' and client lifetime %d\n", session_info.ep, session_info.lifetime);
|
||||
LOG_DBG("RD Client started with endpoint '%s' and client lifetime %d\n", session_info.ep, session_info.lifetime);
|
||||
rd_state = WAIT_NETWORK;
|
||||
break;
|
||||
case WAIT_NETWORK:
|
||||
if(now > wait_until_network_check) {
|
||||
/* check each 10 seconds before next check */
|
||||
PRINTF("Checking for network... %lu\n",
|
||||
(unsigned long)wait_until_network_check);
|
||||
LOG_DBG("Checking for network... %lu\n",
|
||||
(unsigned long)wait_until_network_check);
|
||||
wait_until_network_check = now + 10000;
|
||||
if(has_network_access()) {
|
||||
/* Either do bootstrap then registration */
|
||||
@ -561,9 +549,9 @@ periodic_process(coap_timer_t *timer)
|
||||
|
||||
snprintf(query_data, sizeof(query_data) - 1, "?ep=%s", session_info.ep);
|
||||
coap_set_header_uri_query(request, query_data);
|
||||
PRINTF("Registering ID with bootstrap server [");
|
||||
PRINTEP(&session_info.bs_server_ep);
|
||||
PRINTF("] as '%s'\n", query_data);
|
||||
LOG_INFO("Registering ID with bootstrap server [");
|
||||
LOG_INFO_COAP_EP(&session_info.bs_server_ep);
|
||||
LOG_INFO_("] as '%s'\n", query_data);
|
||||
|
||||
coap_send_request(&rd_request_state, &session_info.bs_server_ep,
|
||||
request, bootstrap_callback);
|
||||
@ -579,7 +567,7 @@ periodic_process(coap_timer_t *timer)
|
||||
/* check that we should still use bootstrap */
|
||||
if(session_info.use_bootstrap) {
|
||||
lwm2m_security_server_t *security;
|
||||
PRINTF("*** Bootstrap - checking for server info...\n");
|
||||
LOG_DBG("*** Bootstrap - checking for server info...\n");
|
||||
/* get the security object - ignore bootstrap servers */
|
||||
for(security = lwm2m_security_get_first();
|
||||
security != NULL;
|
||||
@ -594,9 +582,10 @@ periodic_process(coap_timer_t *timer)
|
||||
if(security->server_uri_len > 0) {
|
||||
uint8_t secure = 0;
|
||||
|
||||
PRINTF("**** Found security instance using: ");
|
||||
PRINTS(security->server_uri_len, security->server_uri, "%c");
|
||||
PRINTF(" (len %d) \n", security->server_uri_len);
|
||||
LOG_DBG("**** Found security instance using: ");
|
||||
LOG_DBG_COAP_STRING((const char *)security->server_uri,
|
||||
security->server_uri_len);
|
||||
LOG_DBG_(" (len %d) \n", security->server_uri_len);
|
||||
/* TODO Should verify it is a URI */
|
||||
/* Check if secure */
|
||||
secure = strncmp((const char *)security->server_uri,
|
||||
@ -605,22 +594,23 @@ periodic_process(coap_timer_t *timer)
|
||||
if(!coap_endpoint_parse((const char *)security->server_uri,
|
||||
security->server_uri_len,
|
||||
&session_info.server_ep)) {
|
||||
PRINTF("Failed to parse server URI!\n");
|
||||
LOG_DBG("Failed to parse server URI!\n");
|
||||
} else {
|
||||
PRINTF("Server address:");
|
||||
PRINTEP(&session_info.server_ep);
|
||||
PRINTF("\n");
|
||||
LOG_DBG("Server address:");
|
||||
LOG_DBG_COAP_EP(&session_info.server_ep);
|
||||
LOG_DBG_("\n");
|
||||
if(secure) {
|
||||
PRINTF("Secure CoAP requested but not supported - can not bootstrap\n");
|
||||
LOG_DBG("Secure CoAP requested but not supported - can not bootstrap\n");
|
||||
} else {
|
||||
lwm2m_rd_client_register_with_server(&session_info.server_ep);
|
||||
session_info.bootstrapped++;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
PRINTF("** failed to parse URI ");
|
||||
PRINTS(security->server_uri_len, security->server_uri, "%c");
|
||||
PRINTF("\n");
|
||||
LOG_DBG("** failed to parse URI ");
|
||||
LOG_DBG_COAP_STRING((const char *)security->server_uri,
|
||||
security->server_uri_len);
|
||||
LOG_DBG_("\n");
|
||||
}
|
||||
}
|
||||
|
||||
@ -637,7 +627,7 @@ periodic_process(coap_timer_t *timer)
|
||||
if(!coap_endpoint_is_connected(&session_info.server_ep)) {
|
||||
/* Not connected... wait a bit... and retry connection */
|
||||
coap_endpoint_connect(&session_info.server_ep);
|
||||
PRINTF("Wait until connected... \n");
|
||||
LOG_DBG("Wait until connected... \n");
|
||||
return;
|
||||
}
|
||||
if(session_info.use_registration && !session_info.registered &&
|
||||
@ -654,13 +644,13 @@ periodic_process(coap_timer_t *timer)
|
||||
len = set_rd_data(request);
|
||||
rd_callback = registration_callback;
|
||||
|
||||
PRINTF("Registering with [");
|
||||
PRINTEP(&session_info.server_ep);
|
||||
PRINTF("] lwm2m endpoint '%s': '", query_data);
|
||||
LOG_INFO("Registering with [");
|
||||
LOG_INFO_COAP_EP(&session_info.server_ep);
|
||||
LOG_INFO_("] lwm2m endpoint '%s': '", query_data);
|
||||
if(len) {
|
||||
PRINTS(len, rd_data, "%c");
|
||||
LOG_INFO_COAP_STRING((const char *)rd_data, len);
|
||||
}
|
||||
PRINTF("' More:%d\n", rd_more);
|
||||
LOG_INFO_("' More:%d\n", rd_more);
|
||||
|
||||
coap_send_request(&rd_request_state, &session_info.server_ep,
|
||||
request, registration_callback);
|
||||
@ -690,7 +680,7 @@ periodic_process(coap_timer_t *timer)
|
||||
/* just wait until the callback kicks us to the next state... */
|
||||
break;
|
||||
case DEREGISTER:
|
||||
PRINTF("DEREGISTER %s\n", session_info.assigned_ep);
|
||||
LOG_INFO("DEREGISTER %s\n", session_info.assigned_ep);
|
||||
coap_init_message(request, COAP_TYPE_CON, COAP_DELETE, 0);
|
||||
coap_set_header_uri_path(request, session_info.assigned_ep);
|
||||
coap_send_request(&rd_request_state, &session_info.server_ep, request,
|
||||
@ -705,7 +695,7 @@ periodic_process(coap_timer_t *timer)
|
||||
break;
|
||||
|
||||
default:
|
||||
PRINTF("Unhandled state: %d\n", rd_state);
|
||||
LOG_WARN("Unhandled state: %d\n", rd_state);
|
||||
}
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
|
@ -51,21 +51,10 @@
|
||||
#include "coap-keystore.h"
|
||||
#include "lib/list.h"
|
||||
|
||||
#define DEBUG 0
|
||||
#if DEBUG
|
||||
#include <stdio.h>
|
||||
#define PRINTF(...) printf(__VA_ARGS__)
|
||||
#define PRINTS(l,s,f) do { int i; \
|
||||
for(i = 0; i < l; i++) printf(f, s[i]); \
|
||||
} while(0)
|
||||
#define PRINTPRE(p,l,s) do { PRINTF(p);PRINTS(l,s,"%c"); } while(0);
|
||||
#define PRINTEP(ep) coap_endpoint_print(ep)
|
||||
#else
|
||||
#define PRINTF(...)
|
||||
#define PRINTS(l,s,f)
|
||||
#define PRINTPRE(p,l,s);
|
||||
#define PRINTEP(ep)
|
||||
#endif
|
||||
/* Log configuration */
|
||||
#include "coap-log.h"
|
||||
#define LOG_MODULE "lwm2m-security"
|
||||
#define LOG_LEVEL LOG_LEVEL_LWM2M
|
||||
|
||||
#define MAX_COUNT LWM2M_SERVER_MAX_COUNT
|
||||
|
||||
@ -111,7 +100,7 @@ create_instance(uint16_t instance_id, lwm2m_status_t *status)
|
||||
sizeof(resources) / sizeof(lwm2m_resource_id_t);
|
||||
list_add(instances_list, &instances[i].instance);
|
||||
|
||||
PRINTF("SEC: Create new security instance\n");
|
||||
LOG_DBG("Create new security instance\n");
|
||||
return &instances[i].instance;
|
||||
}
|
||||
}
|
||||
@ -187,7 +176,7 @@ lwm2m_callback(lwm2m_object_instance_t *object,
|
||||
/* Handle the writes */
|
||||
switch(ctx->resource_id) {
|
||||
case LWM2M_SECURITY_SERVER_URI_ID:
|
||||
PRINTF("Writing security URI value: len: %d\n", (int)ctx->inbuf->size);
|
||||
LOG_DBG("Writing security URI value: len: %d\n", (int)ctx->inbuf->size);
|
||||
value = lwm2m_object_read_string(ctx, ctx->inbuf->buffer, ctx->inbuf->size, security->server_uri, URI_SIZE);
|
||||
/* This is string... */
|
||||
security->server_uri_len = ctx->last_value_len;
|
||||
@ -195,18 +184,18 @@ lwm2m_callback(lwm2m_object_instance_t *object,
|
||||
case LWM2M_SECURITY_BOOTSTRAP_SERVER_ID:
|
||||
value = lwm2m_object_read_boolean(ctx, ctx->inbuf->buffer, ctx->inbuf->size, &iv);
|
||||
if(value > 0) {
|
||||
PRINTF("Set Bootstrap: %d\n", iv);
|
||||
LOG_DBG("Set Bootstrap: %d\n", iv);
|
||||
security->bootstrap = (uint8_t) iv;
|
||||
} else {
|
||||
PRINTF("Failed to set bootstrap\n");
|
||||
LOG_WARN("Failed to set bootstrap\n");
|
||||
}
|
||||
break;
|
||||
case LWM2M_SECURITY_MODE_ID:
|
||||
{
|
||||
int32_t v2;
|
||||
value = lwm2m_object_read_int(ctx, ctx->inbuf->buffer, ctx->inbuf->size, &v2);
|
||||
PRINTF("Writing security MODE value: %d len: %d\n", v2,
|
||||
(int)ctx->inbuf->size);
|
||||
LOG_DBG("Writing security MODE value: %d len: %d\n", v2,
|
||||
(int)ctx->inbuf->size);
|
||||
security->security_mode = v2;
|
||||
}
|
||||
break;
|
||||
@ -214,17 +203,19 @@ lwm2m_callback(lwm2m_object_instance_t *object,
|
||||
value = lwm2m_object_read_string(ctx, ctx->inbuf->buffer, ctx->inbuf->size, security->public_key, KEY_SIZE);
|
||||
security->public_key_len = ctx->last_value_len;
|
||||
|
||||
PRINTF("Writing client PKI: len: %d '", (int)ctx->last_value_len);
|
||||
PRINTS(ctx->last_value_len, security->public_key, "%c");
|
||||
PRINTF("'\n");
|
||||
LOG_DBG("Writing client PKI: len: %d '", (int)ctx->last_value_len);
|
||||
LOG_DBG_COAP_STRING((const char *)security->public_key,
|
||||
ctx->last_value_len);
|
||||
LOG_DBG_("'\n");
|
||||
break;
|
||||
case LWM2M_SECURITY_KEY_ID:
|
||||
value = lwm2m_object_read_string(ctx, ctx->inbuf->buffer, ctx->inbuf->size, security->secret_key, URI_SIZE);
|
||||
security->secret_key_len = ctx->last_value_len;
|
||||
|
||||
PRINTF("Writing secret key: len: %d '", (int)ctx->last_value_len);
|
||||
PRINTS(ctx->last_value_len, security->secret_key, "%c");
|
||||
PRINTF("'\n");
|
||||
LOG_DBG("Writing secret key: len: %d '", (int)ctx->last_value_len);
|
||||
LOG_DBG_COAP_STRING((const char *)security->secret_key,
|
||||
ctx->last_value_len);
|
||||
LOG_DBG_("'\n");
|
||||
|
||||
break;
|
||||
}
|
||||
@ -264,7 +255,7 @@ lwm2m_security_add_server(uint16_t instance_id,
|
||||
int i;
|
||||
|
||||
if(server_uri_len > URI_SIZE) {
|
||||
PRINTF("lwm2m-sec: too long server URI\n");
|
||||
LOG_WARN("too long server URI: %u\n", server_uri_len);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@ -273,13 +264,13 @@ lwm2m_security_add_server(uint16_t instance_id,
|
||||
server = lwm2m_security_get_next(server)) {
|
||||
if(server->server_id == server_id) {
|
||||
if(server->instance.instance_id != instance_id) {
|
||||
PRINTF("lwm2m-sec: wrong instance id\n");
|
||||
LOG_WARN("wrong instance id\n");
|
||||
return NULL;
|
||||
}
|
||||
/* Correct server id and instance id */
|
||||
break;
|
||||
} else if(server->instance.instance_id == instance_id) {
|
||||
PRINTF("lwm2m-sec: wrong server id\n");
|
||||
LOG_WARN("wrong server id\n");
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
@ -299,7 +290,7 @@ lwm2m_security_add_server(uint16_t instance_id,
|
||||
}
|
||||
}
|
||||
if(server == NULL) {
|
||||
PRINTF("lwm2m-sec: no space for more servers\n");
|
||||
LOG_WARN("no space for more servers\n");
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
@ -321,11 +312,11 @@ lwm2m_security_set_server_psk(lwm2m_security_server_t *server,
|
||||
return 0;
|
||||
}
|
||||
if(identity_len > KEY_SIZE) {
|
||||
PRINTF("lwm2m-sec: too large identity\n");
|
||||
LOG_WARN("too large identity: %u\n", identity_len);
|
||||
return 0;
|
||||
}
|
||||
if(key_len > KEY_SIZE) {
|
||||
PRINTF("lwm2m-sec: too large identity\n");
|
||||
LOG_WARN("too large identity: %u\n", key_len);
|
||||
return 0;
|
||||
}
|
||||
memcpy(server->public_key, identity, identity_len);
|
||||
@ -376,18 +367,18 @@ get_psk_info(const coap_endpoint_t *address_info,
|
||||
}
|
||||
if(!coap_endpoint_parse((char *)e->server_uri, e->server_uri_len, &ep)) {
|
||||
/* Failed to parse URI to endpoint */
|
||||
PRINTF("lwm2m-sec: failed to parse server URI ");
|
||||
PRINTS(e->server_uri_len, e->server_uri, "%c");
|
||||
PRINTF("\n");
|
||||
LOG_DBG("failed to parse server URI ");
|
||||
LOG_DBG_COAP_STRING((char *)e->server_uri, e->server_uri_len);
|
||||
LOG_DBG_("\n");
|
||||
continue;
|
||||
}
|
||||
if(!coap_endpoint_cmp(address_info, &ep)) {
|
||||
/* Wrong server */
|
||||
PRINTF("lwm2m-sec: wrong server ");
|
||||
PRINTEP(address_info);
|
||||
PRINTF(" != ");
|
||||
PRINTEP(&ep);
|
||||
PRINTF("\n");
|
||||
LOG_DBG("wrong server ");
|
||||
LOG_DBG_COAP_EP(address_info);
|
||||
LOG_DBG_(" != ");
|
||||
LOG_DBG_COAP_EP(&ep);
|
||||
LOG_DBG_("\n");
|
||||
continue;
|
||||
}
|
||||
if(info->identity_len > 0 && info->identity != NULL) {
|
||||
@ -395,12 +386,12 @@ get_psk_info(const coap_endpoint_t *address_info,
|
||||
if(info->identity_len != e->public_key_len ||
|
||||
memcmp(info->identity, e->public_key, info->identity_len)) {
|
||||
/* Identity not matching */
|
||||
PRINTF("lwm2m-sec: identity not matching\n");
|
||||
LOG_DBG("identity not matching\n");
|
||||
continue;
|
||||
}
|
||||
}
|
||||
/* Found security information for this server */
|
||||
PRINTF("lwm2m-sec: found security match!\n");
|
||||
LOG_DBG("found security match!\n");
|
||||
break;
|
||||
}
|
||||
|
||||
@ -436,7 +427,7 @@ lwm2m_security_init(void)
|
||||
{
|
||||
int i;
|
||||
|
||||
PRINTF("lwm2m-sec: init\n");
|
||||
LOG_INFO("init\n");
|
||||
|
||||
list_init(instances_list);
|
||||
|
||||
@ -449,12 +440,12 @@ lwm2m_security_init(void)
|
||||
#if COAP_DTLS_KEYSTORE_CONF_WITH_LWM2M
|
||||
/* Security object handler added - register keystore */
|
||||
coap_set_keystore(&key_store);
|
||||
PRINTF("lwm2m-sec: registered keystore\n");
|
||||
LOG_DBG("registered keystore\n");
|
||||
#endif /* COAP_DTLS_KEYSTORE_CONF_WITH_LWM2M */
|
||||
#endif /* WITH_DTLS */
|
||||
|
||||
} else {
|
||||
PRINTF("lwm2m-sec: failed to register\n");
|
||||
LOG_WARN("failed to register\n");
|
||||
}
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
|
@ -49,13 +49,10 @@
|
||||
#include "lwm2m-server.h"
|
||||
#include "lwm2m-rd-client.h"
|
||||
|
||||
#define DEBUG 0
|
||||
#if DEBUG
|
||||
#include <stdio.h>
|
||||
#define PRINTF(...) printf(__VA_ARGS__)
|
||||
#else
|
||||
#define PRINTF(...)
|
||||
#endif
|
||||
/* Log configuration */
|
||||
#include "coap-log.h"
|
||||
#define LOG_MODULE "lwm2m-server"
|
||||
#define LOG_LEVEL LOG_LEVEL_LWM2M
|
||||
|
||||
#define MAX_COUNT LWM2M_SERVER_MAX_COUNT
|
||||
|
||||
@ -205,7 +202,7 @@ lwm2m_callback(lwm2m_object_instance_t *object,
|
||||
server = (lwm2m_server_t *) object;
|
||||
|
||||
if(ctx->operation == LWM2M_OP_WRITE) {
|
||||
PRINTF("Write to: %d\n", ctx->resource_id);
|
||||
LOG_DBG("Write to: %d\n", ctx->resource_id);
|
||||
switch(ctx->resource_id) {
|
||||
case LWM2M_SERVER_LIFETIME_ID:
|
||||
lwm2m_object_read_int(ctx, ctx->inbuf->buffer, ctx->inbuf->size, &value);
|
||||
@ -247,16 +244,14 @@ lwm2m_server_add(uint16_t instance_id, uint16_t server_id, uint32_t lifetime)
|
||||
/* Found a matching server */
|
||||
if(server->instance.instance_id != instance_id) {
|
||||
/* Non-matching instance id */
|
||||
PRINTF("lwm2m-server: non-matching instance id for server %u\n",
|
||||
server_id);
|
||||
LOG_DBG("non-matching instance id for server %u\n", server_id);
|
||||
return NULL;
|
||||
}
|
||||
server->lifetime = lifetime;
|
||||
return server;
|
||||
} else if(server->instance.instance_id == instance_id) {
|
||||
/* Right instance but wrong server id */
|
||||
PRINTF("lwm2m-server: non-matching server id for instance %u\n",
|
||||
instance_id);
|
||||
LOG_DBG("non-matching server id for instance %u\n", instance_id);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
@ -277,7 +272,7 @@ lwm2m_server_add(uint16_t instance_id, uint16_t server_id, uint32_t lifetime)
|
||||
}
|
||||
}
|
||||
|
||||
PRINTF("lwm2m-server: no space for more servers\n");
|
||||
LOG_WARN("no space for more servers\n");
|
||||
|
||||
return NULL;
|
||||
}
|
||||
@ -287,7 +282,7 @@ lwm2m_server_init(void)
|
||||
{
|
||||
int i;
|
||||
|
||||
PRINTF("*** Init lwm2m-server\n");
|
||||
LOG_INFO("init\n");
|
||||
|
||||
list_init(server_list);
|
||||
|
||||
|
@ -45,12 +45,10 @@
|
||||
#include "lwm2m-object.h"
|
||||
#include "lwm2m-tlv.h"
|
||||
|
||||
#define DEBUG 0
|
||||
#if DEBUG
|
||||
#define PRINTF(...) printf(__VA_ARGS__)
|
||||
#else
|
||||
#define PRINTF(...)
|
||||
#endif
|
||||
/* Log configuration */
|
||||
#include "coap-log.h"
|
||||
#define LOG_MODULE "lwm2m-tlv-writer"
|
||||
#define LOG_LEVEL LOG_LEVEL_NONE
|
||||
|
||||
/*---------------------------------------------------------------------------*/
|
||||
static size_t
|
||||
@ -125,7 +123,7 @@ enter_sub(lwm2m_context_t *ctx)
|
||||
/* set some flags in state */
|
||||
lwm2m_tlv_t tlv;
|
||||
int len = 0;
|
||||
PRINTF("Enter sub-resource rsc=%d mark:%d\n", ctx->resource_id, ctx->outbuf->len);
|
||||
LOG_DBG("Enter sub-resource rsc=%d mark:%d\n", ctx->resource_id, ctx->outbuf->len);
|
||||
ctx->writer_flags |= WRITER_RESOURCE_INSTANCE;
|
||||
tlv.type = LWM2M_TLV_TYPE_MULTI_RESOURCE;
|
||||
tlv.length = 8; /* create an 8-bit TLV */
|
||||
@ -152,8 +150,8 @@ exit_sub(lwm2m_context_t *ctx)
|
||||
}
|
||||
len = ctx->outbuf->len - ctx->out_mark_pos_ri;
|
||||
|
||||
PRINTF("Exit sub-resource rsc=%d mark:%d len=%d\n", ctx->resource_id,
|
||||
ctx->out_mark_pos_ri, len);
|
||||
LOG_DBG("Exit sub-resource rsc=%d mark:%d len=%d\n", ctx->resource_id,
|
||||
ctx->out_mark_pos_ri, len);
|
||||
|
||||
/* update the lenght byte... Assume TLV header is pos + 1 bytes. */
|
||||
ctx->outbuf->buffer[pos + ctx->out_mark_pos_ri] = len - (pos + 1);
|
||||
|
@ -46,13 +46,10 @@
|
||||
#include <stdint.h>
|
||||
#include "lwm2m-tlv.h"
|
||||
|
||||
#define DEBUG 0
|
||||
#if DEBUG
|
||||
#include <stdio.h>
|
||||
#define PRINTF(...) printf(__VA_ARGS__)
|
||||
#else
|
||||
#define PRINTF(...)
|
||||
#endif
|
||||
/* Log configuration */
|
||||
#include "coap-log.h"
|
||||
#define LOG_MODULE "lwm2m-tlv"
|
||||
#define LOG_LEVEL LOG_LEVEL_NONE
|
||||
|
||||
/*---------------------------------------------------------------------------*/
|
||||
static inline uint8_t
|
||||
@ -131,7 +128,7 @@ lwm2m_tlv_write(const lwm2m_tlv_t *tlv, uint8_t *buffer, size_t buffersize)
|
||||
pos = 1 + len_type;
|
||||
/* ensure that we do not write too much */
|
||||
if(tlv->value != NULL && buffersize < tlv->length + pos) {
|
||||
PRINTF("LWM2M-TLV: Could not write the TLV - buffer overflow.\n");
|
||||
LOG_WARN("Could not write the TLV - buffer overflow.\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -167,13 +164,13 @@ lwm2m_tlv_write(const lwm2m_tlv_t *tlv, uint8_t *buffer, size_t buffersize)
|
||||
memcpy(&buffer[pos], tlv->value, tlv->length);
|
||||
}
|
||||
|
||||
if(DEBUG) {
|
||||
if(LOG_DBG_ENABLED) {
|
||||
int i;
|
||||
PRINTF("TLV:");
|
||||
LOG_DBG("TLV: ");
|
||||
for(i = 0; i < pos + ((tlv->value != NULL) ? tlv->length : 0); i++) {
|
||||
PRINTF("%02x", buffer[i]);
|
||||
LOG_DBG_("%02x", buffer[i]);
|
||||
}
|
||||
PRINTF("\n");
|
||||
LOG_DBG_("\n");
|
||||
}
|
||||
|
||||
return pos + ((tlv->value != NULL) ? tlv->length : 0);
|
||||
@ -208,7 +205,7 @@ lwm2m_tlv_write_int32(uint8_t type, int16_t id, int32_t value, uint8_t *buffer,
|
||||
int i;
|
||||
int v;
|
||||
int last_bit;
|
||||
PRINTF("Exporting int32 %d %ld ", id, (long)value);
|
||||
LOG_DBG("Exporting int32 %d %ld ", id, (long)value);
|
||||
|
||||
v = value < 0 ? -1 : 0;
|
||||
i = 0;
|
||||
@ -221,7 +218,7 @@ lwm2m_tlv_write_int32(uint8_t type, int16_t id, int32_t value, uint8_t *buffer,
|
||||
} while((value != v || last_bit) && i < 4);
|
||||
|
||||
/* export INT as TLV */
|
||||
PRINTF("len: %d\n", i);
|
||||
LOG_DBG("len: %d\n", i);
|
||||
tlv.type = type;
|
||||
tlv.length = i;
|
||||
tlv.value = &buf[3 - (i - 1)];
|
||||
@ -255,11 +252,11 @@ lwm2m_tlv_write_float32(uint8_t type, int16_t id, int32_t value, int bits,
|
||||
e++;
|
||||
}
|
||||
|
||||
PRINTF("Sign: %d, Fraction: %06lx 0b", value < 0, (long)val);
|
||||
LOG_DBG("Sign: %d, Fraction: %06lx 0b", value < 0, (long)val);
|
||||
for(i = 0; i < 23; i++) {
|
||||
PRINTF("%d", (int)((val >> (22 - i)) & 1));
|
||||
LOG_DBG_("%d", (int)((val >> (22 - i)) & 1));
|
||||
}
|
||||
PRINTF("\nExp:%d\n", e);
|
||||
LOG_DBG_("\nExp:%d\n", e);
|
||||
|
||||
/* convert to the thing we should have */
|
||||
e = e - bits + 127;
|
||||
@ -274,7 +271,7 @@ lwm2m_tlv_write_float32(uint8_t type, int16_t id, int32_t value, int bits,
|
||||
b[2] = (val >> 8) & 0xff;
|
||||
b[3] = val & 0xff;
|
||||
|
||||
PRINTF("B=%02x%02x%02x%02x\n", b[0], b[1], b[2], b[3]);
|
||||
LOG_DBG("B=%02x%02x%02x%02x\n", b[0], b[1], b[2], b[3]);
|
||||
/* construct the TLV */
|
||||
tlv.type = type;
|
||||
tlv.length = 4;
|
||||
@ -295,19 +292,19 @@ lwm2m_tlv_float32_to_fix(const lwm2m_tlv_t *tlv, int32_t *value, int bits)
|
||||
e = ((tlv->value[0] << 1) & 0xff) | (tlv->value[1] >> 7);
|
||||
val = (((long)tlv->value[1] & 0x7f) << 16) | (tlv->value[2] << 8) | tlv->value[3];
|
||||
|
||||
PRINTF("Sign: %d, Fraction: %06lx 0b", val < 0, (long)val);
|
||||
LOG_DBG("Sign: %d, Fraction: %06lx 0b", val < 0, (long)val);
|
||||
for(i = 0; i < 23; i++) {
|
||||
PRINTF("%d", (int)((val >> (22 - i)) & 1));
|
||||
LOG_DBG_("%d", (int)((val >> (22 - i)) & 1));
|
||||
}
|
||||
PRINTF("\nExp:%d => %d\n", e, e - 127);
|
||||
LOG_DBG("\nExp:%d => %d\n", e, e - 127);
|
||||
|
||||
e = e - 127 + bits;
|
||||
|
||||
/* e corresponds to the number of times we need to roll the number */
|
||||
|
||||
PRINTF("Actual e=%d\n", e);
|
||||
LOG_DBG("Actual e=%d\n", e);
|
||||
e = e - 23;
|
||||
PRINTF("E after sub %d\n", e);
|
||||
LOG_DBG("E after sub %d\n", e);
|
||||
val = val | 1L << 23;
|
||||
if(e > 0) {
|
||||
val = val << e;
|
||||
@ -332,9 +329,8 @@ int main(int argc, char *argv[])
|
||||
data[0] = 0x00;
|
||||
data[1] = 0x80,
|
||||
|
||||
PRINTF("TLV:%d\n", lwm2m_tlv_get_int32(&tlv));
|
||||
|
||||
PRINTF("Len: %d\n", lwm2m_tlv_write_int32(0, 1, -0x88987f, data, 24));
|
||||
printf("TLV:%d\n", lwm2m_tlv_get_int32(&tlv));
|
||||
|
||||
printf("Len: %d\n", lwm2m_tlv_write_int32(0, 1, -0x88987f, data, 24));
|
||||
}
|
||||
#endif
|
||||
|
Loading…
Reference in New Issue
Block a user