#include "coap_config.h"
#define DEBUG DEBUG_PRINT
#include "net/ip/uip-debug.h"
#include "net/net-debug.h"
#include <string.h>
#include "coap3/coap.h"
static coap_context_t *coap_context;
static clock_time_t clock_offset;
static clock_time_t my_clock_base = 0;
static coap_resource_t *time_resource = NULL;
PROCESS(coap_server_process, "CoAP server process");
AUTOSTART_PROCESSES(&coap_server_process);
void
init_coap_server(coap_context_t **ctx) {
coap_address_t listen_addr;
uip_ipaddr_t gw_addr;
assert(ctx);
coap_set_log_level(LOG_DEBUG);
coap_address_init(&listen_addr);
listen_addr.port = UIP_HTONS(COAP_DEFAULT_PORT);
uip_ip6addr(&listen_addr.addr, 0xaaaa, 0, 0, 0, 0, 0, 0, NODE_ADDR);
#ifndef CONTIKI_TARGET_MINIMAL_NET
uip_ds6_prefix_add(&listen_addr.addr, 64, 0, 0, 0, 0);
#endif
uip_ds6_addr_add(&listen_addr.addr, 0, ADDR_MANUAL);
uip_ip6addr(&gw_addr, 0xaaaa, 0, 0, 0, 0, 0, 0, 0x0001);
uip_ds6_defrt_add(&gw_addr, 0);
PRINTLLADDR(&uip_lladdr);
printf("\r\n");
PRINT6ADDR(&listen_addr.addr);
printf("\r\n");
*ctx = coap_new_context(&listen_addr);
if (!*ctx) {
coap_log(LOG_CRIT, "cannot create CoAP context\r\n");
}
}
#ifndef min
# define min(a,b) ((a) < (b) ? (a) : (b))
#endif
void
hnd_get_time(coap_resource_t *resource, coap_session_t *session,
const coap_pdu_t *request, const coap_string_t *query,
coap_pdu_t *response) {
unsigned char buf[40];
size_t len;
coap_tick_t now;
coap_tick_t t;
if (my_clock_base) {
coap_ticks(&t);
now = my_clock_base + (t / COAP_TICKS_PER_SECOND);
if (query != NULL
&& coap_string_equal(query, coap_make_str_const("ticks"))) {
len = snprintf((char *)buf, sizeof(buf), "%u", (unsigned int)now);
} else {
struct tm *tmp;
time_t tnow = now;
tmp = gmtime(&tnow);
if (!tmp) {
coap_pdu_set_code(response, COAP_RESPONSE_CODE_NOT_FOUND);
return;
}
else {
len = strftime((char *)buf, sizeof(buf), "%b %d %H:%M:%S", tmp);
}
}
coap_add_data_blocked_response(request, response,
COAP_MEDIATYPE_TEXT_PLAIN, 1,
len,
buf);
}
else {
coap_pdu_set_code(response, COAP_RESPONSE_CODE_NOT_FOUND);
}
}
void
init_coap_resources(coap_context_t *ctx) {
coap_resource_t *r;
#if 0#endif
my_clock_base = clock_offset;
r = coap_resource_init(coap_make_str_const("time"), 0);
if (!r)
goto error;
coap_resource_set_get_observable(r, 1);
time_resource = r;
coap_register_handler(r, COAP_REQUEST_GET, hnd_get_time);
#if 0#endif
coap_add_attr(r, coap_make_str_const("ct"), coap_make_str_const("0"), 0);
coap_add_attr(r, coap_make_str_const("rt"), coap_make_str_const("\"ticks\""), 0);
coap_add_attr(r, coap_make_str_const("if"), coap_make_str_const("\"clock\""), 0);
coap_add_resource(ctx, r);
#if 0#endif
return;
error:
coap_log(LOG_CRIT, "cannot create resource\n");
}
struct etimer dirty_timer;
PROCESS_THREAD(coap_server_process, ev, data)
{
PROCESS_BEGIN();
clock_offset = clock_time();
init_coap_server(&coap_context);
if (!coap_context) {
coap_log(LOG_EMERG, "cannot create context\n");
PROCESS_EXIT();
}
init_coap_resources(coap_context);
if (!coap_context) {
coap_log(LOG_EMERG, "cannot create context\n");
PROCESS_EXIT();
}
etimer_set(&dirty_timer, 30 * CLOCK_SECOND);
while(1) {
PROCESS_YIELD();
if(ev == tcpip_event) {
coap_io_process(coap_context, COAP_IO_WAIT);
} else if (ev == PROCESS_EVENT_TIMER && etimer_expired(&dirty_timer)) {
coap_resource_notify_observers(time_resource, NULL);
etimer_reset(&dirty_timer);
}
}
PROCESS_END();
}