diff --git a/examples/libcoap-client/Makefile.ci b/examples/libcoap-client/Makefile.ci index 0e7e4cfccfd3..9e746742194b 100644 --- a/examples/libcoap-client/Makefile.ci +++ b/examples/libcoap-client/Makefile.ci @@ -1,5 +1,15 @@ BOARD_INSUFFICIENT_MEMORY := \ airfy-beacon \ + arduino-duemilanove \ + arduino-leonardo \ + arduino-mega2560 \ + arduino-nano \ + arduino-uno \ + atmega256rfr2-xpro \ + atmega328p \ + atmega328p-xplained-mini \ + atmega8 \ + atxmega-a3bu-xplained \ b-l072z-lrwan1 \ blackpill-stm32f103c8 \ blackpill-stm32f103cb \ @@ -9,6 +19,7 @@ BOARD_INSUFFICIENT_MEMORY := \ calliope-mini \ cc2650-launchpad \ cc2650stk \ + derfmega128 \ hifive1 \ hifive1b \ i-nucleo-lrwan1 \ @@ -16,6 +27,9 @@ BOARD_INSUFFICIENT_MEMORY := \ lsn50 \ maple-mini \ microbit \ + microduino-corerf \ + msb-430 \ + msb-430h \ nrf51dk \ nrf51dongle \ nrf6310 \ @@ -32,10 +46,13 @@ BOARD_INSUFFICIENT_MEMORY := \ nucleo-l031k6 \ nucleo-l053r8 \ nucleo-l073rz \ + olimex-msp430-h1611 \ + olimex-msp430-h2618 \ opencm904 \ samd10-xmini \ saml10-xpro \ saml11-xpro \ + samr21-xpro \ slstk3400a \ spark-core \ stk3200 \ @@ -45,5 +62,9 @@ BOARD_INSUFFICIENT_MEMORY := \ stm32g0316-disco \ stm32l0538-disco \ stm32mp157c-dk2 \ + telosb \ + waspmote-pro \ yunjia-nrf51822 \ + z1 \ + zigduino \ # diff --git a/examples/libcoap-client/client-coap.c b/examples/libcoap-client/client-coap.c index 1187b682b779..440728888cd7 100644 --- a/examples/libcoap-client/client-coap.c +++ b/examples/libcoap-client/client-coap.c @@ -13,7 +13,6 @@ #include #include #include -#include #include "client-coap.h" #include "macros/utils.h" #include "net/utils.h" @@ -48,206 +47,213 @@ static coap_response_t message_handler(coap_session_t *session, const coap_pdu_t *sent, const coap_pdu_t *received, - const coap_mid_t id) { - const uint8_t *data; - size_t len; - size_t offset; - size_t total; - - (void)session; - (void)sent; - (void)id; - if (coap_get_data_large(received, &len, &data, &offset, &total)) { - printf("%*.*s", (int)len, (int)len, (const char *)data); - if (len + offset == total) { - printf("\n"); - quit = 1; - } - } - return COAP_RESPONSE_OK; + const coap_mid_t id) +{ + const uint8_t *data; + size_t len; + size_t offset; + size_t total; + + (void)session; + (void)sent; + (void)id; + if (coap_get_data_large(received, &len, &data, &offset, &total)) { + printf("%*.*s", (int)len, (int)len, (const char *)data); + if (len + offset == total) { + printf("\n"); + quit = 1; + } + } + return COAP_RESPONSE_OK; } static void nack_handler(coap_session_t *session COAP_UNUSED, const coap_pdu_t *sent COAP_UNUSED, const coap_nack_reason_t reason, - const coap_mid_t id COAP_UNUSED) { - - switch (reason) { - case COAP_NACK_TOO_MANY_RETRIES: - case COAP_NACK_NOT_DELIVERABLE: - case COAP_NACK_RST: - case COAP_NACK_TLS_FAILED: - case COAP_NACK_TLS_LAYER_FAILED: - case COAP_NACK_WS_LAYER_FAILED: - case COAP_NACK_WS_FAILED: - coap_log_err("cannot send CoAP pdu\n"); - quit = 1; - break; - case COAP_NACK_ICMP_ISSUE: - case COAP_NACK_BAD_RESPONSE: - default: - break; - } - return; + const coap_mid_t id COAP_UNUSED) +{ + + switch (reason) { + case COAP_NACK_TOO_MANY_RETRIES: + case COAP_NACK_NOT_DELIVERABLE: + case COAP_NACK_RST: + case COAP_NACK_TLS_FAILED: + case COAP_NACK_TLS_LAYER_FAILED: + case COAP_NACK_WS_LAYER_FAILED: + case COAP_NACK_WS_FAILED: + coap_log_err("cannot send CoAP pdu\n"); + quit = 1; + break; + case COAP_NACK_ICMP_ISSUE: + case COAP_NACK_BAD_RESPONSE: + default: + break; + } + return; } static int resolve_address(const char *host, const char *service, coap_address_t *dst, - int scheme_hint_bits) { - uint16_t port = service ? atoi(service) : 0; - int ret = 0; - coap_str_const_t str_host; - coap_addr_info_t *addr_info; - - str_host.s = (const uint8_t *)host; - str_host.length = strlen(host); - addr_info = coap_resolve_address_info(&str_host, port, port, port, port, - AF_UNSPEC, scheme_hint_bits, - COAP_RESOLVE_TYPE_REMOTE); - if (addr_info) { - ret = 1; - *dst = addr_info->addr; - } - - coap_free_address_info(addr_info); - return ret; + int scheme_hint_bits) +{ + uint16_t port = service ? atoi(service) : 0; + int ret = 0; + coap_str_const_t str_host; + coap_addr_info_t *addr_info; + + str_host.s = (const uint8_t *)host; + str_host.length = strlen(host); + addr_info = coap_resolve_address_info(&str_host, port, port, port, port, + AF_UNSPEC, scheme_hint_bits, + COAP_RESOLVE_TYPE_REMOTE); + if (addr_info) { + ret = 1; + *dst = addr_info->addr; + } + + coap_free_address_info(addr_info); + return ret; } void -client_coap_init(int argc, char **argv) { - coap_session_t *session = NULL; - coap_pdu_t *pdu; - coap_address_t dst; - coap_mid_t mid; - int len; - coap_uri_t uri; - char portbuf[8]; +client_coap_init(int argc, char **argv) +{ + coap_session_t *session = NULL; + coap_pdu_t *pdu; + coap_address_t dst; + coap_mid_t mid; + int len; + coap_uri_t uri; + char portbuf[8]; + #define BUFSIZE 100 - unsigned char buf[BUFSIZE]; - int res; - const char *coap_uri = COAP_CLIENT_URI; - - if (argc > 1) { - coap_uri = argv[1]; - } - - /* Initialize libcoap library */ - coap_startup(); - - coap_set_log_level(COAP_MAX_LOGGING_LEVEL); - - /* Parse the URI */ - len = coap_split_uri((const unsigned char *)coap_uri, strlen(coap_uri), &uri); - if (len != 0) { - coap_log_warn("Failed to parse uri %s\n", coap_uri); - goto fail; - } - - snprintf(portbuf, sizeof(portbuf), "%d", uri.port); - snprintf((char *)buf, sizeof(buf), "%*.*s", (int)uri.host.length, - (int)uri.host.length, (const char *)uri.host.s); - /* resolve destination address where packet should be sent */ - len = resolve_address((const char *)buf, portbuf, &dst, 1 << uri.scheme); - if (len <= 0) { - coap_log_warn("Failed to resolve address %*.*s\n", (int)uri.host.length, - (int)uri.host.length, (const char *)uri.host.s); - goto fail; - } - - main_coap_context = coap_new_context(NULL); - if (!main_coap_context) { - coap_log_warn("Failed to initialize context\n"); - goto fail; - } - - coap_context_set_block_mode(main_coap_context, COAP_BLOCK_USE_LIBCOAP); - - if (uri.scheme == COAP_URI_SCHEME_COAP) { - session = coap_new_client_session(main_coap_context, NULL, &dst, - COAP_PROTO_UDP); - } else if (uri.scheme == COAP_URI_SCHEME_COAP_TCP) { - session = coap_new_client_session(main_coap_context, NULL, &dst, - COAP_PROTO_TCP); -#if defined (COAP_USE_PSK) && defined(COAP_USE_PSK_ID) - } else { - static coap_dtls_cpsk_t dtls_psk; - static char client_sni[256]; + unsigned char buf[BUFSIZE]; + int res; + const char *coap_uri = COAP_CLIENT_URI; - memset(client_sni, 0, sizeof(client_sni)); - memset(&dtls_psk, 0, sizeof(dtls_psk)); - dtls_psk.version = COAP_DTLS_CPSK_SETUP_VERSION; - if (uri.host.length) { - memcpy(client_sni, uri.host.s, - MIN(uri.host.length, sizeof(client_sni) - 1)); + if (argc > 1) { + coap_uri = argv[1]; } - else { - memcpy(client_sni, "localhost", 9); + + /* Initialize libcoap library */ + coap_startup(); + + coap_set_log_level(COAP_MAX_LOGGING_LEVEL); + + /* Parse the URI */ + len = coap_split_uri((const unsigned char *)coap_uri, strlen(coap_uri), &uri); + if (len != 0) { + coap_log_warn("Failed to parse uri %s\n", coap_uri); + goto fail; + } + + snprintf(portbuf, sizeof(portbuf), "%d", uri.port); + snprintf((char *)buf, sizeof(buf), "%*.*s", (int)uri.host.length, + (int)uri.host.length, (const char *)uri.host.s); + /* resolve destination address where packet should be sent */ + len = resolve_address((const char *)buf, portbuf, &dst, 1 << uri.scheme); + if (len <= 0) { + coap_log_warn("Failed to resolve address %*.*s\n", (int)uri.host.length, + (int)uri.host.length, (const char *)uri.host.s); + goto fail; } - dtls_psk.client_sni = client_sni; - dtls_psk.psk_info.identity.s = (const uint8_t *)COAP_USE_PSK_ID; - dtls_psk.psk_info.identity.length = strlen(COAP_USE_PSK_ID); - dtls_psk.psk_info.key.s = (const uint8_t *)COAP_USE_PSK; - dtls_psk.psk_info.key.length = strlen(COAP_USE_PSK); - session = coap_new_client_session_psk2(main_coap_context, NULL, &dst, - COAP_PROTO_DTLS, &dtls_psk); + main_coap_context = coap_new_context(NULL); + if (!main_coap_context) { + coap_log_warn("Failed to initialize context\n"); + goto fail; + } + + coap_context_set_block_mode(main_coap_context, COAP_BLOCK_USE_LIBCOAP); + + if (uri.scheme == COAP_URI_SCHEME_COAP) { + session = coap_new_client_session(main_coap_context, NULL, &dst, + COAP_PROTO_UDP); + } + else if (uri.scheme == COAP_URI_SCHEME_COAP_TCP) { + session = coap_new_client_session(main_coap_context, NULL, &dst, + COAP_PROTO_TCP); +#if defined (COAP_USE_PSK) && defined(COAP_USE_PSK_ID) + } + else { + static coap_dtls_cpsk_t dtls_psk; + static char client_sni[256]; + + memset(client_sni, 0, sizeof(client_sni)); + memset(&dtls_psk, 0, sizeof(dtls_psk)); + dtls_psk.version = COAP_DTLS_CPSK_SETUP_VERSION; + if (uri.host.length) { + memcpy(client_sni, uri.host.s, + MIN(uri.host.length, sizeof(client_sni) - 1)); + } + else { + memcpy(client_sni, "localhost", 9); + } + dtls_psk.client_sni = client_sni; + dtls_psk.psk_info.identity.s = (const uint8_t *)COAP_USE_PSK_ID; + dtls_psk.psk_info.identity.length = strlen(COAP_USE_PSK_ID); + dtls_psk.psk_info.key.s = (const uint8_t *)COAP_USE_PSK; + dtls_psk.psk_info.key.length = strlen(COAP_USE_PSK); + + session = coap_new_client_session_psk2(main_coap_context, NULL, &dst, + COAP_PROTO_DTLS, &dtls_psk); #else /* ! COAP_USE_PSK && ! COAP_USE_PSK_ID */ - coap_log_err("CONFIG_LIBCOAP_USE_PSK and CONFIG_LIBCOAP_USE_PSK_ID not defined\n"); - goto fail; + coap_log_err("CONFIG_LIBCOAP_USE_PSK and CONFIG_LIBCOAP_USE_PSK_ID not defined\n"); + goto fail; #endif /* ! COAP_USE_PSK && ! COAP_USE_PSK_ID */ - } - - if (!session) { - coap_log_warn("Failed to create session\n"); - goto fail; - } - - coap_register_response_handler(main_coap_context, message_handler); - coap_register_nack_handler(main_coap_context, nack_handler); - - /* construct CoAP message */ - pdu = coap_pdu_init(COAP_MESSAGE_CON, - COAP_REQUEST_CODE_GET, - coap_new_message_id(session), - coap_session_max_pdu_size(session)); - if (!pdu) { - coap_log_warn("Failed to create PDU\n"); - goto fail; - } - - len = coap_uri_into_options(&uri, &dst, &optlist, 1, buf, sizeof(buf)); - if (len) { - coap_log_warn("Failed to create options\n"); - goto fail; - } - - /* Add option list (which will be sorted) to the PDU */ - if (optlist) { - res = coap_add_optlist_pdu(pdu, &optlist); - if (res != 1) { - coap_log_warn("Failed to add options to PDU\n"); - goto fail; - } - } - - /* and send the PDU */ - mid = coap_send(session, pdu); - if (mid == COAP_INVALID_MID) { - coap_log_warn("Failed to send PDU\n"); - goto fail; - } - while (!quit) { - coap_io_process(main_coap_context, 1000); - } + } + + if (!session) { + coap_log_warn("Failed to create session\n"); + goto fail; + } + + coap_register_response_handler(main_coap_context, message_handler); + coap_register_nack_handler(main_coap_context, nack_handler); + + /* construct CoAP message */ + pdu = coap_pdu_init(COAP_MESSAGE_CON, + COAP_REQUEST_CODE_GET, + coap_new_message_id(session), + coap_session_max_pdu_size(session)); + if (!pdu) { + coap_log_warn("Failed to create PDU\n"); + goto fail; + } + + len = coap_uri_into_options(&uri, &dst, &optlist, 1, buf, sizeof(buf)); + if (len) { + coap_log_warn("Failed to create options\n"); + goto fail; + } + + /* Add option list (which will be sorted) to the PDU */ + if (optlist) { + res = coap_add_optlist_pdu(pdu, &optlist); + if (res != 1) { + coap_log_warn("Failed to add options to PDU\n"); + goto fail; + } + } + + /* and send the PDU */ + mid = coap_send(session, pdu); + if (mid == COAP_INVALID_MID) { + coap_log_warn("Failed to send PDU\n"); + goto fail; + } + while (!quit) { + coap_io_process(main_coap_context, 1000); + } fail: - /* Clean up library usage so client can be run again */ - quit = 0; - coap_delete_optlist(optlist); - optlist = NULL; - coap_session_release(session); - session = NULL; - coap_free_context(main_coap_context); - main_coap_context = NULL; - coap_cleanup(); + /* Clean up library usage so client can be run again */ + quit = 0; + coap_delete_optlist(optlist); + optlist = NULL; + coap_session_release(session); + session = NULL; + coap_free_context(main_coap_context); + main_coap_context = NULL; + coap_cleanup(); } diff --git a/examples/libcoap-client/main.c b/examples/libcoap-client/main.c index d0a06158ee0b..23dacae51fb0 100644 --- a/examples/libcoap-client/main.c +++ b/examples/libcoap-client/main.c @@ -32,22 +32,24 @@ static msg_t _main_msg_queue[MAIN_QUEUE_SIZE]; extern int client_coap_init(int argc, char **argv); static const shell_command_t shell_commands[] = { - { "coapc", "Start a libcoap client", client_coap_init }, - { NULL, NULL, NULL } + { "coapc", "Start a libcoap client", client_coap_init }, + { NULL, NULL, NULL } }; int -main(void) { - /* we need a message queue for the thread running the shell in order to - * receive potentially fast incoming networking packets */ - msg_init_queue(_main_msg_queue, MAIN_QUEUE_SIZE); - puts("RIOT libcoap client testing implementation"); - - /* start shell */ - puts("All up, running the shell now"); - char line_buf[SHELL_DEFAULT_BUFSIZE]; - shell_run(shell_commands, line_buf, SHELL_DEFAULT_BUFSIZE); - - /* should be never reached */ - return 0; +main(void) +{ + /* we need a message queue for the thread running the shell in order to + * receive potentially fast incoming networking packets */ + msg_init_queue(_main_msg_queue, MAIN_QUEUE_SIZE); + puts("RIOT libcoap client testing implementation"); + + /* start shell */ + puts("All up, running the shell now"); + char line_buf[SHELL_DEFAULT_BUFSIZE]; + + shell_run(shell_commands, line_buf, SHELL_DEFAULT_BUFSIZE); + + /* should be never reached */ + return 0; } diff --git a/examples/libcoap-server/Makefile.ci b/examples/libcoap-server/Makefile.ci index 0e7e4cfccfd3..9e746742194b 100644 --- a/examples/libcoap-server/Makefile.ci +++ b/examples/libcoap-server/Makefile.ci @@ -1,5 +1,15 @@ BOARD_INSUFFICIENT_MEMORY := \ airfy-beacon \ + arduino-duemilanove \ + arduino-leonardo \ + arduino-mega2560 \ + arduino-nano \ + arduino-uno \ + atmega256rfr2-xpro \ + atmega328p \ + atmega328p-xplained-mini \ + atmega8 \ + atxmega-a3bu-xplained \ b-l072z-lrwan1 \ blackpill-stm32f103c8 \ blackpill-stm32f103cb \ @@ -9,6 +19,7 @@ BOARD_INSUFFICIENT_MEMORY := \ calliope-mini \ cc2650-launchpad \ cc2650stk \ + derfmega128 \ hifive1 \ hifive1b \ i-nucleo-lrwan1 \ @@ -16,6 +27,9 @@ BOARD_INSUFFICIENT_MEMORY := \ lsn50 \ maple-mini \ microbit \ + microduino-corerf \ + msb-430 \ + msb-430h \ nrf51dk \ nrf51dongle \ nrf6310 \ @@ -32,10 +46,13 @@ BOARD_INSUFFICIENT_MEMORY := \ nucleo-l031k6 \ nucleo-l053r8 \ nucleo-l073rz \ + olimex-msp430-h1611 \ + olimex-msp430-h2618 \ opencm904 \ samd10-xmini \ saml10-xpro \ saml11-xpro \ + samr21-xpro \ slstk3400a \ spark-core \ stk3200 \ @@ -45,5 +62,9 @@ BOARD_INSUFFICIENT_MEMORY := \ stm32g0316-disco \ stm32l0538-disco \ stm32mp157c-dk2 \ + telosb \ + waspmote-pro \ yunjia-nrf51822 \ + z1 \ + zigduino \ # diff --git a/examples/libcoap-server/main.c b/examples/libcoap-server/main.c index 78506c57dd09..a9db1ffc60e3 100644 --- a/examples/libcoap-server/main.c +++ b/examples/libcoap-server/main.c @@ -32,22 +32,24 @@ static msg_t _main_msg_queue[MAIN_QUEUE_SIZE]; extern int server_coap_init(int argc, char **argv); static const shell_command_t shell_commands[] = { - { "coaps", "Start a libcoap server", server_coap_init }, - { NULL, NULL, NULL } + { "coaps", "Start a libcoap server", server_coap_init }, + { NULL, NULL, NULL } }; int -main(void) { - /* we need a message queue for the thread running the shell in order to - * receive potentially fast incoming networking packets */ - msg_init_queue(_main_msg_queue, MAIN_QUEUE_SIZE); - puts("RIOT libcoap server testing implementation"); - - /* start shell */ - puts("All up, running the shell now"); - char line_buf[SHELL_DEFAULT_BUFSIZE]; - shell_run(shell_commands, line_buf, SHELL_DEFAULT_BUFSIZE); - - /* should be never reached */ - return 0; +main(void) +{ + /* we need a message queue for the thread running the shell in order to + * receive potentially fast incoming networking packets */ + msg_init_queue(_main_msg_queue, MAIN_QUEUE_SIZE); + puts("RIOT libcoap server testing implementation"); + + /* start shell */ + puts("All up, running the shell now"); + char line_buf[SHELL_DEFAULT_BUFSIZE]; + + shell_run(shell_commands, line_buf, SHELL_DEFAULT_BUFSIZE); + + /* should be never reached */ + return 0; } diff --git a/examples/libcoap-server/server-coap.c b/examples/libcoap-server/server-coap.c index 4ec12efe6829..e692c2217fa9 100644 --- a/examples/libcoap-server/server-coap.c +++ b/examples/libcoap-server/server-coap.c @@ -33,259 +33,270 @@ static coap_resource_t *time_resource = NULL; /* just for testing */ static 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; - - (void)resource; - (void)session; - (void)request; - /* FIXME: return time, e.g. in human-readable by default and ticks - * when query ?ticks is given. */ - - /* if my_clock_base was deleted, we pretend to have no such resource */ - coap_pdu_set_code(response, my_clock_base ? COAP_RESPONSE_CODE_CONTENT : - COAP_RESPONSE_CODE_NOT_FOUND); - if (my_clock_base) { - coap_add_option(response, COAP_OPTION_CONTENT_FORMAT, - coap_encode_var_safe(buf, sizeof(buf), - COAP_MEDIATYPE_TEXT_PLAIN), - buf); - } - - coap_add_option(response, COAP_OPTION_MAXAGE, - coap_encode_var_safe(buf, sizeof(buf), 0x01), buf); - - if (my_clock_base) { - - /* calculate current time */ - coap_ticks(&t); - now = my_clock_base + (t / COAP_TICKS_PER_SECOND); - - if (query != NULL - && coap_string_equal(query, coap_make_str_const("ticks"))) { - /* output ticks */ - len = snprintf((char *)buf, sizeof(buf), "%u", (unsigned int)now); - coap_add_data(response, len, buf); + coap_pdu_t *response) +{ + unsigned char buf[40]; + size_t len; + coap_tick_t now; + coap_tick_t t; + + (void)resource; + (void)session; + (void)request; + /* FIXME: return time, e.g. in human-readable by default and ticks + * when query ?ticks is given. */ + + /* if my_clock_base was deleted, we pretend to have no such resource */ + coap_pdu_set_code(response, my_clock_base ? COAP_RESPONSE_CODE_CONTENT : + COAP_RESPONSE_CODE_NOT_FOUND); + if (my_clock_base) { + coap_add_option(response, COAP_OPTION_CONTENT_FORMAT, + coap_encode_var_safe(buf, sizeof(buf), + COAP_MEDIATYPE_TEXT_PLAIN), + buf); + } + + coap_add_option(response, COAP_OPTION_MAXAGE, + coap_encode_var_safe(buf, sizeof(buf), 0x01), buf); + + if (my_clock_base) { + + /* calculate current time */ + coap_ticks(&t); + now = my_clock_base + (t / COAP_TICKS_PER_SECOND); + + if (query != NULL + && coap_string_equal(query, coap_make_str_const("ticks"))) { + /* output ticks */ + len = snprintf((char *)buf, sizeof(buf), "%u", (unsigned int)now); + coap_add_data(response, len, buf); + } } - } } static void -init_coap_resources(coap_context_t *ctx) { - coap_resource_t *r; +init_coap_resources(coap_context_t *ctx) +{ + coap_resource_t *r; + #if 0 - r = coap_resource_init(NULL, 0, 0); - coap_register_handler(r, COAP_REQUEST_GET, hnd_get_index); + r = coap_resource_init(NULL, 0, 0); + coap_register_handler(r, COAP_REQUEST_GET, hnd_get_index); - coap_add_attr(r, coap_make_str_const("ct"), coap_make_str_const("0"), 0); - coap_add_attr(r, coap_make_str_const("title"), coap_make_str_const("\"General Info\""), 0); - coap_add_resource(ctx, r); + coap_add_attr(r, coap_make_str_const("ct"), coap_make_str_const("0"), 0); + coap_add_attr(r, coap_make_str_const("title"), coap_make_str_const("\"General Info\""), 0); + coap_add_resource(ctx, r); #endif - /* store clock base to use in /time */ - my_clock_base = clock_offset; + /* store clock base to use in /time */ + my_clock_base = clock_offset; - r = coap_resource_init(coap_make_str_const("time"), 0); - if (!r) { - goto error; - } + 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); + coap_resource_set_get_observable(r, 1); + time_resource = r; + coap_register_handler(r, COAP_REQUEST_GET, hnd_get_time); #if 0 - coap_register_handler(r, COAP_REQUEST_PUT, hnd_put_time); - coap_register_handler(r, COAP_REQUEST_DELETE, hnd_delete_time); + coap_register_handler(r, COAP_REQUEST_PUT, hnd_put_time); + coap_register_handler(r, COAP_REQUEST_DELETE, hnd_delete_time); #endif - coap_add_attr(r, coap_make_str_const("ct"), coap_make_str_const("0"), 0); - /* coap_add_attr(r, coap_make_str_const("title"), - coap_make_str_const("\"Internal Clock\""), 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_attr(r, coap_make_str_const("ct"), coap_make_str_const("0"), 0); + /* coap_add_attr(r, coap_make_str_const("title"), + coap_make_str_const("\"Internal Clock\""), 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); + coap_add_resource(ctx, r); #if 0 - if (coap_async_is_supported()) { - r = coap_resource_init(coap_make_str_const("async"), 0); - coap_register_handler(r, COAP_REQUEST_GET, hnd_get_async); + if (coap_async_is_supported()) { + r = coap_resource_init(coap_make_str_const("async"), 0); + coap_register_handler(r, COAP_REQUEST_GET, hnd_get_async); - coap_add_attr(r, coap_make_str_const("ct"), coap_make_str_const("0"), 0); - coap_add_resource(ctx, r); - } + coap_add_attr(r, coap_make_str_const("ct"), coap_make_str_const("0"), 0); + coap_add_resource(ctx, r); + } #endif - return; + return; error: - coap_log_crit("cannot create resource\n"); + coap_log_crit("cannot create resource\n"); } static int -init_coap_context_endpoints(const char *use_psk) { - coap_address_t listenaddress; - gnrc_netif_t *netif = gnrc_netif_iter(NULL); - ipv6_addr_t addr; - char addr_str[INET6_ADDRSTRLEN + 8]; - int scheme_hint_bits = 1 << COAP_URI_SCHEME_COAP; - coap_addr_info_t *info = NULL; - coap_addr_info_t *info_list = NULL; - coap_str_const_t local; - int have_ep = 0; - - /* Get the first address on the interface */ - if (gnrc_netif_ipv6_addrs_get(netif, &addr, sizeof(addr)) < 0) { - puts("Unable to get first address of the interface"); - return 0; - } - - coap_address_init(&listenaddress); - listenaddress.addr.sin6.sin6_family = AF_INET6; - memcpy(&listenaddress.addr.sin6.sin6_addr, &addr, - sizeof(listenaddress.addr.sin6.sin6_addr)); - coap_print_ip_addr(&listenaddress, addr_str, sizeof(addr_str)); - coap_log_info("Server IP [%s]\n", addr_str); - - main_coap_context = coap_new_context(NULL); - if (!main_coap_context) { - return 0; - } - - if (use_psk && coap_dtls_is_supported()) { - coap_dtls_spsk_t setup_data; - - /* Need PSK set up before setting up endpoints */ - memset(&setup_data, 0, sizeof(setup_data)); - setup_data.version = COAP_DTLS_SPSK_SETUP_VERSION; - setup_data.psk_info.key.s = (const uint8_t *)use_psk; - setup_data.psk_info.key.length = strlen(use_psk); - coap_context_set_psk2(main_coap_context, &setup_data); - scheme_hint_bits |= 1 << COAP_URI_SCHEME_COAPS; - } - - local.s = (uint8_t *)addr_str; - local.length = strlen(addr_str); - info_list = coap_resolve_address_info(&local, COAP_DEFAULT_PORT, - COAPS_DEFAULT_PORT, - 0, 0, - 0, - scheme_hint_bits, - COAP_RESOLVE_TYPE_REMOTE); - for (info = info_list; info != NULL; info = info->next) { - coap_endpoint_t *ep; - - ep = coap_new_endpoint(main_coap_context, &info->addr, info->proto); - if (!ep) { - coap_log_warn("cannot create endpoint for proto %u\n", - info->proto); - } else { - have_ep = 1; +init_coap_context_endpoints(const char *use_psk) +{ + coap_address_t listenaddress; + gnrc_netif_t *netif = gnrc_netif_iter(NULL); + ipv6_addr_t addr; + char addr_str[INET6_ADDRSTRLEN + 8]; + int scheme_hint_bits = 1 << COAP_URI_SCHEME_COAP; + coap_addr_info_t *info = NULL; + coap_addr_info_t *info_list = NULL; + coap_str_const_t local; + int have_ep = 0; + + /* Get the first address on the interface */ + if (gnrc_netif_ipv6_addrs_get(netif, &addr, sizeof(addr)) < 0) { + puts("Unable to get first address of the interface"); + return 0; } - } - coap_free_address_info(info_list); - if (!have_ep) { - return 0; - } - return 1; + coap_address_init(&listenaddress); + listenaddress.addr.sin6.sin6_family = AF_INET6; + memcpy(&listenaddress.addr.sin6.sin6_addr, &addr, + sizeof(listenaddress.addr.sin6.sin6_addr)); + coap_print_ip_addr(&listenaddress, addr_str, sizeof(addr_str)); + coap_log_info("Server IP [%s]\n", addr_str); + + main_coap_context = coap_new_context(NULL); + if (!main_coap_context) { + return 0; + } + + if (use_psk && coap_dtls_is_supported()) { + coap_dtls_spsk_t setup_data; + + /* Need PSK set up before setting up endpoints */ + memset(&setup_data, 0, sizeof(setup_data)); + setup_data.version = COAP_DTLS_SPSK_SETUP_VERSION; + setup_data.psk_info.key.s = (const uint8_t *)use_psk; + setup_data.psk_info.key.length = strlen(use_psk); + coap_context_set_psk2(main_coap_context, &setup_data); + scheme_hint_bits |= 1 << COAP_URI_SCHEME_COAPS; + } + + local.s = (uint8_t *)addr_str; + local.length = strlen(addr_str); + info_list = coap_resolve_address_info(&local, COAP_DEFAULT_PORT, + COAPS_DEFAULT_PORT, + 0, 0, + 0, + scheme_hint_bits, + COAP_RESOLVE_TYPE_REMOTE); + for (info = info_list; info != NULL; info = info->next) { + coap_endpoint_t *ep; + + ep = coap_new_endpoint(main_coap_context, &info->addr, info->proto); + if (!ep) { + coap_log_warn("cannot create endpoint for proto %u\n", + info->proto); + } + else { + have_ep = 1; + } + } + coap_free_address_info(info_list); + if (!have_ep) { + return 0; + } + + return 1; } void * -server_coap_run(void *arg) { - (void)arg; +server_coap_run(void *arg) +{ + (void)arg; - /* Initialize libcoap library */ - coap_startup(); + /* Initialize libcoap library */ + coap_startup(); - coap_set_log_level(COAP_MAX_LOGGING_LEVEL); + coap_set_log_level(COAP_MAX_LOGGING_LEVEL); - if (!init_coap_context_endpoints(COAP_USE_PSK)) { - goto fail; - } + if (!init_coap_context_endpoints(COAP_USE_PSK)) { + goto fail; + } - /* Limit the number of idle sessions to save RAM */ - coap_context_set_max_idle_sessions(main_coap_context, 2); - clock_offset = 1; /* Need a non-zero value */ - init_coap_resources(main_coap_context); + /* Limit the number of idle sessions to save RAM */ + coap_context_set_max_idle_sessions(main_coap_context, 2); + clock_offset = 1; /* Need a non-zero value */ + init_coap_resources(main_coap_context); - coap_log_info("libcoap server ready\n"); - /* Keep on processing ... */ - while (quit == 0) { - coap_io_process(main_coap_context, 1000); - } + coap_log_info("libcoap server ready\n"); + /* Keep on processing ... */ + while (quit == 0) { + coap_io_process(main_coap_context, 1000); + } fail: - /* Clean up library usage so client can be run again */ - coap_free_context(main_coap_context); - main_coap_context = NULL; - coap_cleanup(); - running = 0; - quit = 0; - coap_log_info("libcoap server stopped\n"); - return NULL; + /* Clean up library usage so client can be run again */ + coap_free_context(main_coap_context); + main_coap_context = NULL; + coap_cleanup(); + running = 0; + quit = 0; + coap_log_info("libcoap server stopped\n"); + return NULL; } static char server_stack[THREAD_STACKSIZE_MAIN + - THREAD_EXTRA_STACKSIZE_PRINTF]; + THREAD_EXTRA_STACKSIZE_PRINTF]; static void -start_server(void) { - kernel_pid_t server_pid; +start_server(void) +{ + kernel_pid_t server_pid; + + /* Only one instance of the server */ + if (running) { + puts("Error: server already running"); + return; + } - /* Only one instance of the server */ - if (running) { - puts("Error: server already running"); - return; - } - - /* The server is initialized */ - server_pid = thread_create(server_stack, - sizeof(server_stack), - THREAD_PRIORITY_MAIN - 1, - THREAD_CREATE_STACKTEST, - server_coap_run, NULL, "libcoap_server"); - - /* Uncommon but better be sure */ - if (server_pid == EINVAL) { - puts("ERROR: Thread invalid"); - return; - } + /* The server is initialized */ + server_pid = thread_create(server_stack, + sizeof(server_stack), + THREAD_PRIORITY_MAIN - 1, + THREAD_CREATE_STACKTEST, + server_coap_run, NULL, "libcoap_server"); + + /* Uncommon but better be sure */ + if (server_pid == EINVAL) { + puts("ERROR: Thread invalid"); + return; + } - if (server_pid == EOVERFLOW) { - puts("ERROR: Thread overflow!"); - return; - } + if (server_pid == EOVERFLOW) { + puts("ERROR: Thread overflow!"); + return; + } - running = 1; - return; + running = 1; + return; } static void -stop_server(void) { - /* check if server is running at all */ - if (running == 0) { - puts("Error: libcoap server is not running"); - return; - } +stop_server(void) +{ + /* check if server is running at all */ + if (running == 0) { + puts("Error: libcoap server is not running"); + return; + } - quit = 1; + quit = 1; - puts("Stopping server..."); + puts("Stopping server..."); } void -server_coap_init(int argc, char **argv) { - if (argc < 2) { - printf("usage: %s start|stop\n", argv[0]); +server_coap_init(int argc, char **argv) +{ + if (argc < 2) { + printf("usage: %s start|stop\n", argv[0]); + return; + } + if (strcmp(argv[1], "start") == 0) { + start_server(); + } + else if (strcmp(argv[1], "stop") == 0) { + stop_server(); + } + else { + printf("Error: invalid command. Usage: %s start|stop\n", argv[0]); + } return; - } - if (strcmp(argv[1], "start") == 0) { - start_server(); - } else if (strcmp(argv[1], "stop") == 0) { - stop_server(); - } else { - printf("Error: invalid command. Usage: %s start|stop\n", argv[0]); - } - return; } diff --git a/pkg/libcoap/Makefile b/pkg/libcoap/Makefile index 571a96e993c7..a9c7289fea1b 100644 --- a/pkg/libcoap/Makefile +++ b/pkg/libcoap/Makefile @@ -1,6 +1,6 @@ PKG_NAME=libcoap -PKG_URL=https://github.com/obgm/libcoap -PKG_VERSION=269216eb1990f0826680a3fb434a28de8e995bfa +PKG_URL=https://github.com/mrdeep1/libcoap +PKG_VERSION=a1612e8eb24a76a4d3297a23194943e3d1f492c2 PKG_LICENSE=BSD-2-Clause LIBCOAP_BUILD_DIR=$(BINDIR)/pkg/$(PKG_NAME)