diff --git a/examples/cc26xx/cc26xx-web-demo/Makefile b/examples/cc26xx/cc26xx-web-demo/Makefile index 622156a4d..1da0a7747 100644 --- a/examples/cc26xx/cc26xx-web-demo/Makefile +++ b/examples/cc26xx/cc26xx-web-demo/Makefile @@ -5,7 +5,7 @@ all: cc26xx-web-demo REST_RESOURCES_DIR = ./resources REST_RESOURCES_FILES += res-leds.c res-toggle-leds.c res-device.c -REST_RESOURCES_FILES += res-sensors.c res-ble-advd.c +REST_RESOURCES_FILES += res-sensors.c res-ble-advd.c res-net.c PROJECTDIRS += $(REST_RESOURCES_DIR) PROJECT_SOURCEFILES += $(REST_RESOURCES_FILES) diff --git a/examples/cc26xx/cc26xx-web-demo/cc26xx-web-demo.c b/examples/cc26xx/cc26xx-web-demo/cc26xx-web-demo.c index 4d8c9ca86..1eaf35648 100644 --- a/examples/cc26xx/cc26xx-web-demo/cc26xx-web-demo.c +++ b/examples/cc26xx/cc26xx-web-demo/cc26xx-web-demo.c @@ -44,6 +44,7 @@ #include "lib/sensors.h" #include "lib/list.h" #include "sys/process.h" +#include "net/ipv6/sicslowpan.h" #include "button-sensor.h" #include "batmon-sensor.h" #include "httpd-simple.h" @@ -77,13 +78,20 @@ struct ctimer bmp_timer, hdc_timer, tmp_timer, opt_timer, mpu_timer; static struct etimer et; static struct ctimer ct; /*---------------------------------------------------------------------------*/ +/* Parent RSSI functionality */ +#if CC26XX_WEB_DEMO_READ_PARENT_RSSI +static struct uip_icmp6_echo_reply_notification echo_reply_notification; +static struct etimer echo_request_timer; +int def_rt_rssi = 0; +#endif +/*---------------------------------------------------------------------------*/ process_event_t cc26xx_web_demo_publish_event; process_event_t cc26xx_web_demo_config_loaded_event; process_event_t cc26xx_web_demo_load_config_defaults; /*---------------------------------------------------------------------------*/ /* Saved settings on flash: store, offset, magic */ #define CONFIG_FLASH_OFFSET 0 -#define CONFIG_MAGIC 0xCC265001 +#define CONFIG_MAGIC 0xCC265002 cc26xx_web_demo_config_t cc26xx_web_demo_config; /*---------------------------------------------------------------------------*/ @@ -374,8 +382,57 @@ sensor_readings_handler(char *key, int key_len, char *val, int val_len) return HTTPD_SIMPLE_POST_HANDLER_UNKNOWN; } /*---------------------------------------------------------------------------*/ +#if CC26XX_WEB_DEMO_READ_PARENT_RSSI +static int +ping_interval_post_handler(char *key, int key_len, char *val, int val_len) +{ + int rv = 0; + + if(key_len != strlen("ping_interval") || + strncasecmp(key, "ping_interval", strlen("ping_interval")) != 0) { + /* Not ours */ + return HTTPD_SIMPLE_POST_HANDLER_UNKNOWN; + } + + rv = atoi(val); + + if(rv < CC26XX_WEB_DEMO_RSSI_MEASURE_INTERVAL_MIN || + rv > CC26XX_WEB_DEMO_RSSI_MEASURE_INTERVAL_MAX) { + return HTTPD_SIMPLE_POST_HANDLER_ERROR; + } + + cc26xx_web_demo_config.def_rt_ping_interval = rv * CLOCK_SECOND; + + return HTTPD_SIMPLE_POST_HANDLER_OK; +} +#endif +/*---------------------------------------------------------------------------*/ HTTPD_SIMPLE_POST_HANDLER(sensor, sensor_readings_handler); HTTPD_SIMPLE_POST_HANDLER(defaults, defaults_post_handler); + +#if CC26XX_WEB_DEMO_READ_PARENT_RSSI +HTTPD_SIMPLE_POST_HANDLER(ping_interval, ping_interval_post_handler); +/*---------------------------------------------------------------------------*/ +static void +echo_reply_handler(uip_ipaddr_t *source, uint8_t ttl, uint8_t *data, + uint16_t datalen) +{ + if(uip_ip6addr_cmp(source, uip_ds6_defrt_choose())) { + def_rt_rssi = sicslowpan_get_last_rssi(); + } +} +/*---------------------------------------------------------------------------*/ +static void +ping_parent(void) +{ + if(uip_ds6_get_global(ADDR_PREFERRED) == NULL) { + return; + } + + uip_icmp6_send(uip_ds6_defrt_choose(), ICMP6_ECHO_REQUEST, 0, + CC26XX_WEB_DEMO_ECHO_REQ_PAYLOAD_LEN); +} +#endif /*---------------------------------------------------------------------------*/ static void get_batmon_reading(void *data) @@ -828,6 +885,8 @@ PROCESS_THREAD(cc26xx_web_demo_process, ev, data) * own defaults and restore saved config from flash... */ cc26xx_web_demo_config.sensors_bitmap = 0xFFFFFFFF; /* all on by default */ + cc26xx_web_demo_config.def_rt_ping_interval = + CC26XX_WEB_DEMO_DEFAULT_RSSI_MEAS_INTERVAL; load_config(); /* @@ -841,6 +900,15 @@ PROCESS_THREAD(cc26xx_web_demo_process, ev, data) httpd_simple_register_post_handler(&sensor_handler); httpd_simple_register_post_handler(&defaults_handler); +#if CC26XX_WEB_DEMO_READ_PARENT_RSSI + httpd_simple_register_post_handler(&ping_interval_handler); + + def_rt_rssi = 0x8000000; + uip_icmp6_echo_reply_callback_add(&echo_reply_notification, + echo_reply_handler); + etimer_set(&echo_request_timer, CC26XX_WEB_DEMO_NET_CONNECT_PERIODIC); +#endif + etimer_set(&et, CC26XX_WEB_DEMO_NET_CONNECT_PERIODIC); /* @@ -848,6 +916,25 @@ PROCESS_THREAD(cc26xx_web_demo_process, ev, data) * (e.g a button press / or reed trigger) */ while(1) { + if(ev == PROCESS_EVENT_TIMER && etimer_expired(&et)) { + if(uip_ds6_get_global(ADDR_PREFERRED) == NULL) { + leds_on(CC26XX_WEB_DEMO_STATUS_LED); + ctimer_set(&ct, NO_NET_LED_DURATION, publish_led_off, NULL); + etimer_set(&et, CC26XX_WEB_DEMO_NET_CONNECT_PERIODIC); + } + } + +#if CC26XX_WEB_DEMO_READ_PARENT_RSSI + if(ev == PROCESS_EVENT_TIMER && etimer_expired(&echo_request_timer)) { + if(uip_ds6_get_global(ADDR_PREFERRED) == NULL) { + etimer_set(&echo_request_timer, CC26XX_WEB_DEMO_NET_CONNECT_PERIODIC); + } else { + ping_parent(); + etimer_set(&echo_request_timer, cc26xx_web_demo_config.def_rt_ping_interval); + } + } +#endif + if(ev == sensors_event && data == CC26XX_WEB_DEMO_SENSOR_READING_TRIGGER) { if((CC26XX_WEB_DEMO_SENSOR_READING_TRIGGER)->value( BUTTON_SENSOR_VALUE_DURATION) > CLOCK_SECOND * 5) { @@ -858,12 +945,6 @@ PROCESS_THREAD(cc26xx_web_demo_process, ev, data) process_post(PROCESS_BROADCAST, cc26xx_web_demo_publish_event, NULL); } - } else if(ev == PROCESS_EVENT_TIMER && etimer_expired(&et)) { - if(uip_ds6_get_global(ADDR_PREFERRED) == NULL) { - leds_on(CC26XX_WEB_DEMO_STATUS_LED); - ctimer_set(&ct, NO_NET_LED_DURATION, publish_led_off, NULL); - etimer_set(&et, CC26XX_WEB_DEMO_NET_CONNECT_PERIODIC); - } } else if(ev == httpd_simple_event_new_config) { save_config(); #if BOARD_SENSORTAG diff --git a/examples/cc26xx/cc26xx-web-demo/cc26xx-web-demo.h b/examples/cc26xx/cc26xx-web-demo/cc26xx-web-demo.h index 18c0289d4..026461f32 100644 --- a/examples/cc26xx/cc26xx-web-demo/cc26xx-web-demo.h +++ b/examples/cc26xx/cc26xx-web-demo/cc26xx-web-demo.h @@ -80,6 +80,16 @@ #define CC26XX_WEB_DEMO_NET_UART 1 #endif /*---------------------------------------------------------------------------*/ +/* Active probing of RSSI from our preferred parent */ +#if (CC26XX_WEB_DEMO_COAP_SERVER || CC26XX_WEB_DEMO_MQTT_CLIENT) +#define CC26XX_WEB_DEMO_READ_PARENT_RSSI 1 +#else +#define CC26XX_WEB_DEMO_READ_PARENT_RSSI 0 +#endif + +#define CC26XX_WEB_DEMO_RSSI_MEASURE_INTERVAL_MAX 86400 /* secs: 1 day */ +#define CC26XX_WEB_DEMO_RSSI_MEASURE_INTERVAL_MIN 5 /* secs */ +/*---------------------------------------------------------------------------*/ /* User configuration */ /* Take a sensor reading on button press */ #define CC26XX_WEB_DEMO_SENSOR_READING_TRIGGER &button_left_sensor @@ -169,6 +179,7 @@ typedef struct cc26xx_web_demo_config_s { uint32_t magic; int len; uint32_t sensors_bitmap; + int def_rt_ping_interval; mqtt_client_config_t mqtt_config; net_uart_config_t net_uart; } cc26xx_web_demo_config_t; diff --git a/examples/cc26xx/cc26xx-web-demo/coap-server.c b/examples/cc26xx/cc26xx-web-demo/coap-server.c index 4dec9cce2..9b4155d00 100644 --- a/examples/cc26xx/cc26xx-web-demo/coap-server.c +++ b/examples/cc26xx/cc26xx-web-demo/coap-server.c @@ -56,6 +56,9 @@ extern resource_t res_device_hw; extern resource_t res_device_uptime; extern resource_t res_device_cfg_reset; +extern resource_t res_parent_rssi; +extern resource_t res_parent_ip; + #if RF_BLE_ENABLED extern resource_t res_ble_advd; #endif @@ -138,6 +141,9 @@ PROCESS_THREAD(coap_server_process, ev, data) rest_activate_resource(&res_device_uptime, "dev/uptime"); rest_activate_resource(&res_device_cfg_reset, "dev/cfg_reset"); + rest_activate_resource(&res_parent_rssi, "net/parent/RSSI"); + rest_activate_resource(&res_parent_ip, "net/parent/IPv6"); + #if RF_BLE_ENABLED rest_activate_resource(&res_ble_advd, "dev/ble_advd"); #endif diff --git a/examples/cc26xx/cc26xx-web-demo/httpd-simple.c b/examples/cc26xx/cc26xx-web-demo/httpd-simple.c index 29c22d22b..9800bbc48 100644 --- a/examples/cc26xx/cc26xx-web-demo/httpd-simple.c +++ b/examples/cc26xx/cc26xx-web-demo/httpd-simple.c @@ -93,8 +93,8 @@ static int state; #define STRINGIFY(x) XSTR(x) #define XSTR(x) #x -#define RSSI_INT_MAX STRINGIFY(MQTT_CLIENT_RSSI_MEASURE_INTERVAL_MAX) -#define RSSI_INT_MIN STRINGIFY(MQTT_CLIENT_RSSI_MEASURE_INTERVAL_MIN) +#define RSSI_INT_MAX STRINGIFY(CC26XX_WEB_DEMO_RSSI_MEASURE_INTERVAL_MAX) +#define RSSI_INT_MIN STRINGIFY(CC26XX_WEB_DEMO_RSSI_MEASURE_INTERVAL_MIN) #define PUB_INT_MAX STRINGIFY(MQTT_CLIENT_PUBLISH_INTERVAL_MAX) #define PUB_INT_MIN STRINGIFY(MQTT_CLIENT_PUBLISH_INTERVAL_MIN) /*---------------------------------------------------------------------------*/ @@ -568,12 +568,54 @@ PT_THREAD(generate_config(struct httpd_state *s)) s->reading->publish ? "" : " Checked", config_div_close)); } + PT_WAIT_THREAD(&s->generate_pt, enqueue_chunk(s, 0, "")); PT_WAIT_THREAD(&s->generate_pt, enqueue_chunk(s, 0, "")); + /* RSSI measurements */ +#if CC26XX_WEB_DEMO_READ_PARENT_RSSI + PT_WAIT_THREAD(&s->generate_pt, + enqueue_chunk(s, 0, "

RSSI Probing

")); + + PT_WAIT_THREAD(&s->generate_pt, + enqueue_chunk(s, 0, + "
generate_pt, + enqueue_chunk(s, 0, "method=\"post\" enctype=\"")); + PT_WAIT_THREAD(&s->generate_pt, + enqueue_chunk(s, 0, "application/x-www-form-urlencoded\" ")); + PT_WAIT_THREAD(&s->generate_pt, + enqueue_chunk(s, 0, "accept-charset=\"UTF-8\">")); + + PT_WAIT_THREAD(&s->generate_pt, + enqueue_chunk(s, 0, "%sPeriod (secs):%s", + config_div_left, config_div_close)); + PT_WAIT_THREAD(&s->generate_pt, + enqueue_chunk(s, 0, "%sgenerate_pt, + enqueue_chunk(s, 0, "value=\"%lu\" ", + (clock_time_t) + (cc26xx_web_demo_config.def_rt_ping_interval + / CLOCK_SECOND))); + PT_WAIT_THREAD(&s->generate_pt, + enqueue_chunk(s, 0, + "min=\"" RSSI_INT_MIN "\" " + "max=\"" RSSI_INT_MAX "\" " + "name=\"ping_interval\">%s", + config_div_close)); + + PT_WAIT_THREAD(&s->generate_pt, + enqueue_chunk(s, 0, + "")); + PT_WAIT_THREAD(&s->generate_pt, + enqueue_chunk(s, 0, "
")); +#endif + /* Actions */ PT_WAIT_THREAD(&s->generate_pt, enqueue_chunk(s, 0, "

Actions

")); PT_WAIT_THREAD(&s->generate_pt, @@ -732,24 +774,6 @@ PT_THREAD(generate_mqtt_config(struct httpd_state *s)) "name=\"broker_port\">%s", config_div_close)); - PT_WAIT_THREAD(&s->generate_pt, - enqueue_chunk(s, 0, "%sRSSI Interval (secs):%s", - config_div_left, config_div_close)); - PT_WAIT_THREAD(&s->generate_pt, - enqueue_chunk(s, 0, "%sgenerate_pt, - enqueue_chunk(s, 0, "value=\"%lu\" ", - (clock_time_t) - (cc26xx_web_demo_config.mqtt_config.def_rt_ping_interval - / CLOCK_SECOND))); - PT_WAIT_THREAD(&s->generate_pt, - enqueue_chunk(s, 0, - "min=\"" RSSI_INT_MIN "\" " - "max=\"" RSSI_INT_MAX "\" " - "name=\"ping_interval\">%s", - config_div_close)); - PT_WAIT_THREAD(&s->generate_pt, enqueue_chunk(s, 0, "")); diff --git a/examples/cc26xx/cc26xx-web-demo/httpd-simple.h b/examples/cc26xx/cc26xx-web-demo/httpd-simple.h index c7631adb1..25b8db3e5 100644 --- a/examples/cc26xx/cc26xx-web-demo/httpd-simple.h +++ b/examples/cc26xx/cc26xx-web-demo/httpd-simple.h @@ -48,7 +48,7 @@ /*---------------------------------------------------------------------------*/ /* Ideally a multiple of TCP_MSS */ #ifdef HTTPD_SIMPLE_CONF_MAIN_BUF_SIZE -#define HTTPD_SIMPLE_MAIN_BUF_SIZE HTTPD_SIMPLE_CONF_BUF_SIZE +#define HTTPD_SIMPLE_MAIN_BUF_SIZE HTTPD_SIMPLE_CONF_MAIN_BUF_SIZE #else #define HTTPD_SIMPLE_MAIN_BUF_SIZE UIP_TCP_MSS #endif diff --git a/examples/cc26xx/cc26xx-web-demo/mqtt-client.c b/examples/cc26xx/cc26xx-web-demo/mqtt-client.c index 4fa0736ef..cf60d6c63 100644 --- a/examples/cc26xx/cc26xx-web-demo/mqtt-client.c +++ b/examples/cc26xx/cc26xx-web-demo/mqtt-client.c @@ -41,7 +41,6 @@ #include "net/rpl/rpl.h" #include "net/ip/uip.h" #include "net/ipv6/uip-icmp6.h" -#include "net/ipv6/sicslowpan.h" #include "sys/etimer.h" #include "sys/ctimer.h" #include "lib/sensors.h" @@ -86,7 +85,7 @@ static const char *broker_ip = "0064:ff9b:0000:0000:0000:0000:b8ac:7cbd"; * Number of times to try reconnecting to the broker. * Can be a limited number (e.g. 3, 10 etc) or can be set to RETRY_FOREVER */ -#define RECONNECT_ATTEMPTS RETRY_FOREVER +#define RECONNECT_ATTEMPTS 5 #define CONNECTION_STABLE_TIME (CLOCK_SECOND * 5) #define NEW_CONFIG_WAIT_INTERVAL (CLOCK_SECOND * 20) static struct timer connection_life; @@ -139,9 +138,7 @@ static uint16_t seq_nr_value = 0; static uip_ip6addr_t def_route; /*---------------------------------------------------------------------------*/ /* Parent RSSI functionality */ -static struct uip_icmp6_echo_reply_notification echo_reply_notification; -static struct etimer echo_request_timer; -int def_rt_rssi = 0; +extern int def_rt_rssi; /*---------------------------------------------------------------------------*/ const static cc26xx_web_demo_sensor_reading_t *reading; /*---------------------------------------------------------------------------*/ @@ -388,29 +385,6 @@ reconnect_post_handler(char *key, int key_len, char *val, int val_len) return HTTPD_SIMPLE_POST_HANDLER_OK; } /*---------------------------------------------------------------------------*/ -static int -ping_interval_post_handler(char *key, int key_len, char *val, int val_len) -{ - int rv = 0; - - if(key_len != strlen("ping_interval") || - strncasecmp(key, "ping_interval", strlen("ping_interval")) != 0) { - /* Not ours */ - return HTTPD_SIMPLE_POST_HANDLER_UNKNOWN; - } - - rv = atoi(val); - - if(rv < MQTT_CLIENT_RSSI_MEASURE_INTERVAL_MIN || - rv > MQTT_CLIENT_RSSI_MEASURE_INTERVAL_MAX) { - return HTTPD_SIMPLE_POST_HANDLER_ERROR; - } - - conf->def_rt_ping_interval = rv * CLOCK_SECOND; - - return HTTPD_SIMPLE_POST_HANDLER_OK; -} -/*---------------------------------------------------------------------------*/ HTTPD_SIMPLE_POST_HANDLER(org_id, org_id_post_handler); HTTPD_SIMPLE_POST_HANDLER(type_id, type_id_post_handler); HTTPD_SIMPLE_POST_HANDLER(event_type_id, event_type_id_post_handler); @@ -420,16 +394,6 @@ HTTPD_SIMPLE_POST_HANDLER(ip_addr, ip_addr_post_handler); HTTPD_SIMPLE_POST_HANDLER(port, port_post_handler); HTTPD_SIMPLE_POST_HANDLER(interval, interval_post_handler); HTTPD_SIMPLE_POST_HANDLER(reconnect, reconnect_post_handler); -HTTPD_SIMPLE_POST_HANDLER(ping_interval, ping_interval_post_handler); -/*---------------------------------------------------------------------------*/ -static void -echo_reply_handler(uip_ipaddr_t *source, uint8_t ttl, uint8_t *data, - uint16_t datalen) -{ - if(uip_ip6addr_cmp(source, uip_ds6_defrt_choose())) { - def_rt_rssi = sicslowpan_get_last_rssi(); - } -} /*---------------------------------------------------------------------------*/ static void pub_handler(const char *topic, uint16_t topic_len, const uint8_t *chunk, @@ -624,7 +588,6 @@ init_config() conf->broker_port = CC26XX_WEB_DEMO_DEFAULT_BROKER_PORT; conf->pub_interval = CC26XX_WEB_DEMO_DEFAULT_PUBLISH_INTERVAL; - conf->def_rt_ping_interval = CC26XX_WEB_DEMO_DEFAULT_RSSI_MEAS_INTERVAL; return 1; } @@ -641,7 +604,6 @@ register_http_post_handlers(void) httpd_simple_register_post_handler(&port_handler); httpd_simple_register_post_handler(&ip_addr_handler); httpd_simple_register_post_handler(&reconnect_handler); - httpd_simple_register_post_handler(&ping_interval_handler); } /*---------------------------------------------------------------------------*/ static void @@ -664,6 +626,7 @@ publish(void) /* Publish MQTT topic in IBM quickstart format */ int len; int remaining = APP_BUFFER_SIZE; + char def_rt_str[64]; seq_nr_value++; @@ -686,7 +649,6 @@ publish(void) buf_ptr += len; /* Put our Default route's string representation in a buffer */ - char def_rt_str[64]; memset(def_rt_str, 0, sizeof(def_rt_str)); cc26xx_web_demo_ipaddr_sprintf(def_rt_str, sizeof(def_rt_str), uip_ds6_defrt_choose()); @@ -743,17 +705,6 @@ connect_to_broker(void) } /*---------------------------------------------------------------------------*/ static void -ping_parent(void) -{ - if(uip_ds6_get_global(ADDR_PREFERRED) == NULL) { - return; - } - - uip_icmp6_send(uip_ds6_defrt_choose(), ICMP6_ECHO_REQUEST, 0, - CC26XX_WEB_DEMO_ECHO_REQ_PAYLOAD_LEN); -} -/*---------------------------------------------------------------------------*/ -static void state_machine(void) { switch(state) { @@ -794,7 +745,6 @@ state_machine(void) if(uip_ds6_get_global(ADDR_PREFERRED) != NULL) { /* Registered and with a public IP. Connect */ DBG("Registered. Connect attempt %u\n", connect_attempt); - ping_parent(); connect_to_broker(); } etimer_set(&publish_periodic_timer, CC26XX_WEB_DEMO_NET_CONNECT_PERIODIC); @@ -923,11 +873,6 @@ PROCESS_THREAD(mqtt_client_process, ev, data) update_config(); - def_rt_rssi = 0x8000000; - uip_icmp6_echo_reply_callback_add(&echo_reply_notification, - echo_reply_handler); - etimer_set(&echo_request_timer, conf->def_rt_ping_interval); - /* Main loop */ while(1) { @@ -956,11 +901,6 @@ PROCESS_THREAD(mqtt_client_process, ev, data) state_machine(); } - if(ev == PROCESS_EVENT_TIMER && data == &echo_request_timer) { - ping_parent(); - etimer_set(&echo_request_timer, conf->def_rt_ping_interval); - } - if(ev == cc26xx_web_demo_load_config_defaults) { init_config(); etimer_set(&publish_periodic_timer, NEW_CONFIG_WAIT_INTERVAL); diff --git a/examples/cc26xx/cc26xx-web-demo/mqtt-client.h b/examples/cc26xx/cc26xx-web-demo/mqtt-client.h index b4b50085f..ab7c08227 100644 --- a/examples/cc26xx/cc26xx-web-demo/mqtt-client.h +++ b/examples/cc26xx/cc26xx-web-demo/mqtt-client.h @@ -45,8 +45,6 @@ #define MQTT_CLIENT_CONFIG_CMD_TYPE_LEN 8 #define MQTT_CLIENT_CONFIG_IP_ADDR_STR_LEN 64 /*---------------------------------------------------------------------------*/ -#define MQTT_CLIENT_RSSI_MEASURE_INTERVAL_MAX 86400 /* secs: 1 day */ -#define MQTT_CLIENT_RSSI_MEASURE_INTERVAL_MIN 5 /* secs */ #define MQTT_CLIENT_PUBLISH_INTERVAL_MAX 86400 /* secs: 1 day */ #define MQTT_CLIENT_PUBLISH_INTERVAL_MIN 5 /* secs */ /*---------------------------------------------------------------------------*/ @@ -63,7 +61,6 @@ typedef struct mqtt_client_config { char broker_ip[MQTT_CLIENT_CONFIG_IP_ADDR_STR_LEN]; char cmd_type[MQTT_CLIENT_CONFIG_CMD_TYPE_LEN]; clock_time_t pub_interval; - int def_rt_ping_interval; uint16_t broker_port; } mqtt_client_config_t; /*---------------------------------------------------------------------------*/ diff --git a/examples/cc26xx/cc26xx-web-demo/resources/res-net.c b/examples/cc26xx/cc26xx-web-demo/resources/res-net.c new file mode 100644 index 000000000..1a5ce7b81 --- /dev/null +++ b/examples/cc26xx/cc26xx-web-demo/resources/res-net.c @@ -0,0 +1,126 @@ +/* + * Copyright (c) 2015, Texas Instruments Incorporated - http://www.ti.com/ + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + */ +/*---------------------------------------------------------------------------*/ +/** + * \addtogroup cc26xx-web-demo + * @{ + * + * \file + * CoAP resource handler for network-related resources + */ +/*---------------------------------------------------------------------------*/ +#include "contiki.h" +#include "rest-engine.h" +#include "er-coap.h" +#include "coap-server.h" +#include "cc26xx-web-demo.h" + +#include "ti-lib.h" + +#include +/*---------------------------------------------------------------------------*/ +extern int def_rt_rssi; +/*---------------------------------------------------------------------------*/ +static void +res_get_handler_parent_rssi(void *request, void *response, uint8_t *buffer, + uint16_t preferred_size, int32_t *offset) +{ + unsigned int accept = -1; + + REST.get_header_accept(request, &accept); + + if(accept == -1 || accept == REST.type.TEXT_PLAIN) { + REST.set_header_content_type(response, REST.type.TEXT_PLAIN); + snprintf((char *)buffer, REST_MAX_CHUNK_SIZE, "%d", def_rt_rssi); + + REST.set_response_payload(response, (uint8_t *)buffer, strlen((char *)buffer)); + } else if(accept == REST.type.APPLICATION_JSON) { + REST.set_header_content_type(response, REST.type.APPLICATION_JSON); + snprintf((char *)buffer, REST_MAX_CHUNK_SIZE, "{\"Parent RSSI\":\"%d\"}", + def_rt_rssi); + + REST.set_response_payload(response, buffer, strlen((char *)buffer)); + } else if(accept == REST.type.APPLICATION_XML) { + REST.set_header_content_type(response, REST.type.APPLICATION_XML); + snprintf((char *)buffer, REST_MAX_CHUNK_SIZE, + "", def_rt_rssi); + + REST.set_response_payload(response, buffer, strlen((char *)buffer)); + } else { + REST.set_response_status(response, REST.status.NOT_ACCEPTABLE); + REST.set_response_payload(response, coap_server_supported_msg, + strlen(coap_server_supported_msg)); + } +} +/*---------------------------------------------------------------------------*/ +static void +res_get_handler_pref_parent(void *request, void *response, uint8_t *buffer, + uint16_t preferred_size, int32_t *offset) +{ + unsigned int accept = -1; + char def_rt_str[64]; + + REST.get_header_accept(request, &accept); + + memset(def_rt_str, 0, sizeof(def_rt_str)); + cc26xx_web_demo_ipaddr_sprintf(def_rt_str, sizeof(def_rt_str), + uip_ds6_defrt_choose()); + + if(accept == -1 || accept == REST.type.TEXT_PLAIN) { + REST.set_header_content_type(response, REST.type.TEXT_PLAIN); + snprintf((char *)buffer, REST_MAX_CHUNK_SIZE, "%s", def_rt_str); + + REST.set_response_payload(response, (uint8_t *)buffer, strlen((char *)buffer)); + } else if(accept == REST.type.APPLICATION_JSON) { + REST.set_header_content_type(response, REST.type.APPLICATION_JSON); + snprintf((char *)buffer, REST_MAX_CHUNK_SIZE, "{\"Parent\":\"%s\"}", + def_rt_str); + + REST.set_response_payload(response, buffer, strlen((char *)buffer)); + } else if(accept == REST.type.APPLICATION_XML) { + REST.set_header_content_type(response, REST.type.APPLICATION_XML); + snprintf((char *)buffer, REST_MAX_CHUNK_SIZE, + "", def_rt_str); + + REST.set_response_payload(response, buffer, strlen((char *)buffer)); + } else { + REST.set_response_status(response, REST.status.NOT_ACCEPTABLE); + REST.set_response_payload(response, coap_server_supported_msg, + strlen(coap_server_supported_msg)); + } +} +/*---------------------------------------------------------------------------*/ +RESOURCE(res_parent_rssi, "title=\"Parent RSSI\";rt=\"dBm\"", + res_get_handler_parent_rssi, NULL, NULL, NULL); +/*---------------------------------------------------------------------------*/ +RESOURCE(res_parent_ip, "title=\"Preferred Parent\";rt=\"IPv6 address\"", + res_get_handler_pref_parent, NULL, NULL, NULL); +/*---------------------------------------------------------------------------*/ +/** @} */