COAP: adapted CoAP examples to new API
This commit is contained in:
parent
379a3fac2b
commit
6fe4026686
|
@ -42,6 +42,7 @@
|
|||
#include "contiki.h"
|
||||
#include "contiki-net.h"
|
||||
#include "coap-engine.h"
|
||||
#include "coap-blocking-api.h"
|
||||
#include "dev/button-sensor.h"
|
||||
|
||||
#define DEBUG 0
|
||||
|
@ -60,6 +61,8 @@
|
|||
#define SERVER_NODE(ipaddr) uip_ip6addr(ipaddr, 0xfe80, 0, 0, 0, 0x0212, 0x7402, 0x0002, 0x0202) /* cooja2 */
|
||||
/* #define SERVER_NODE(ipaddr) uip_ip6addr(ipaddr, 0xbbbb, 0, 0, 0, 0, 0, 0, 0x1) */
|
||||
|
||||
#define SERVER_EP "coap://[fe80::212:7402:0002:0202]"
|
||||
|
||||
#define LOCAL_PORT UIP_HTONS(COAP_DEFAULT_PORT + 1)
|
||||
#define REMOTE_PORT UIP_HTONS(COAP_DEFAULT_PORT)
|
||||
|
||||
|
@ -82,7 +85,7 @@ static int uri_switch = 0;
|
|||
|
||||
/* This function is will be passed to COAP_BLOCKING_REQUEST() to handle responses. */
|
||||
void
|
||||
client_chunk_handler(void *response)
|
||||
client_chunk_handler(coap_packet_t *response)
|
||||
{
|
||||
const uint8_t *chunk;
|
||||
|
||||
|
@ -92,14 +95,18 @@ client_chunk_handler(void *response)
|
|||
}
|
||||
PROCESS_THREAD(er_example_client, ev, data)
|
||||
{
|
||||
coap_endpoint_t server_ep;
|
||||
PROCESS_BEGIN();
|
||||
|
||||
static coap_packet_t request[1]; /* This way the packet can be treated as pointer as usual. */
|
||||
|
||||
SERVER_NODE(&server_ipaddr);
|
||||
/* SERVER_NODE(&server_ipaddr); */
|
||||
|
||||
coap_endpoint_parse(SERVER_EP, strlen(SERVER_EP),
|
||||
&server_ep);
|
||||
|
||||
/* receives all CoAP messages */
|
||||
coap_init_engine();
|
||||
coap_engine_init();
|
||||
|
||||
etimer_set(&et, TOGGLE_INTERVAL * CLOCK_SECOND);
|
||||
|
||||
|
@ -122,11 +129,10 @@ PROCESS_THREAD(er_example_client, ev, data)
|
|||
|
||||
coap_set_payload(request, (uint8_t *)msg, sizeof(msg) - 1);
|
||||
|
||||
PRINT6ADDR(&server_ipaddr);
|
||||
coap_endpoint_print(&server_ep);
|
||||
PRINTF(" : %u\n", UIP_HTONS(REMOTE_PORT));
|
||||
|
||||
COAP_BLOCKING_REQUEST(&server_ipaddr, REMOTE_PORT, request,
|
||||
client_chunk_handler);
|
||||
COAP_BLOCKING_REQUEST(&server_ep, request, client_chunk_handler);
|
||||
|
||||
printf("\n--Done--\n");
|
||||
|
||||
|
@ -145,7 +151,7 @@ PROCESS_THREAD(er_example_client, ev, data)
|
|||
PRINT6ADDR(&server_ipaddr);
|
||||
PRINTF(" : %u\n", UIP_HTONS(REMOTE_PORT));
|
||||
|
||||
COAP_BLOCKING_REQUEST(&server_ipaddr, REMOTE_PORT, request,
|
||||
COAP_BLOCKING_REQUEST(&server_ep, request,
|
||||
client_chunk_handler);
|
||||
|
||||
printf("\n--Done--\n");
|
||||
|
|
|
@ -31,7 +31,7 @@
|
|||
|
||||
/**
|
||||
* \file
|
||||
* Erbium (Er) REST Engine example.
|
||||
* Erbium (Er) CoAP Engine example.
|
||||
* \author
|
||||
* Matthias Kovatsch <kovatsch@inf.ethz.ch>
|
||||
*/
|
||||
|
@ -41,7 +41,7 @@
|
|||
#include <string.h>
|
||||
#include "contiki.h"
|
||||
#include "contiki-net.h"
|
||||
#include "rest-engine.h"
|
||||
#include "coap-engine.h"
|
||||
|
||||
#if PLATFORM_HAS_BUTTON
|
||||
#include "dev/button-sensor.h"
|
||||
|
@ -63,7 +63,7 @@
|
|||
* Resources to be activated need to be imported through the extern keyword.
|
||||
* The build system automatically compiles the resources in the corresponding sub-directory.
|
||||
*/
|
||||
extern resource_t
|
||||
extern coap_resource_t
|
||||
res_hello,
|
||||
res_mirror,
|
||||
res_chunks,
|
||||
|
@ -73,29 +73,29 @@ extern resource_t
|
|||
res_sub,
|
||||
res_b1_sep_b2;
|
||||
#if PLATFORM_HAS_LEDS
|
||||
extern resource_t res_leds, res_toggle;
|
||||
extern coap_resource_t res_leds, res_toggle;
|
||||
#endif
|
||||
#if PLATFORM_HAS_LIGHT
|
||||
#include "dev/light-sensor.h"
|
||||
extern resource_t res_light;
|
||||
extern coap_resource_t res_light;
|
||||
#endif
|
||||
#if PLATFORM_HAS_BATTERY
|
||||
#include "dev/battery-sensor.h"
|
||||
extern resource_t res_battery;
|
||||
extern coap_resource_t res_battery;
|
||||
#endif
|
||||
#if PLATFORM_HAS_TEMPERATURE
|
||||
#include "dev/temperature-sensor.h"
|
||||
extern resource_t res_temperature;
|
||||
extern coap_resource_t res_temperature;
|
||||
#endif
|
||||
/*
|
||||
extern resource_t res_battery;
|
||||
extern coap_resource_t res_battery;
|
||||
#endif
|
||||
#if PLATFORM_HAS_RADIO
|
||||
extern resource_t res_radio;
|
||||
extern coap_resource_t res_radio;
|
||||
#endif
|
||||
#if PLATFORM_HAS_SHT11
|
||||
#include "dev/sht11/sht11-sensor.h"
|
||||
extern resource_t res_sht11;
|
||||
extern coap_resource_t res_sht11;
|
||||
#endif
|
||||
*/
|
||||
|
||||
|
@ -120,46 +120,46 @@ PROCESS_THREAD(er_example_server, ev, data)
|
|||
PRINTF("uIP buffer: %u\n", UIP_BUFSIZE);
|
||||
PRINTF("LL header: %u\n", UIP_LLH_LEN);
|
||||
PRINTF("IP+UDP header: %u\n", UIP_IPUDPH_LEN);
|
||||
PRINTF("REST max chunk: %u\n", REST_MAX_CHUNK_SIZE);
|
||||
PRINTF("CoAP max chunk: %u\n", COAP_MAX_CHUNK_SIZE);
|
||||
|
||||
/* Initialize the REST engine. */
|
||||
rest_init_engine();
|
||||
coap_engine_init();
|
||||
|
||||
/*
|
||||
* Bind the resources to their Uri-Path.
|
||||
* WARNING: Activating twice only means alternate path, not two instances!
|
||||
* All static variables are the same for each URI path.
|
||||
*/
|
||||
rest_activate_resource(&res_hello, "test/hello");
|
||||
/* rest_activate_resource(&res_mirror, "debug/mirror"); */
|
||||
/* rest_activate_resource(&res_chunks, "test/chunks"); */
|
||||
/* rest_activate_resource(&res_separate, "test/separate"); */
|
||||
rest_activate_resource(&res_push, "test/push");
|
||||
/* rest_activate_resource(&res_event, "sensors/button"); */
|
||||
/* rest_activate_resource(&res_sub, "test/sub"); */
|
||||
/* rest_activate_resource(&res_b1_sep_b2, "test/b1sepb2"); */
|
||||
coap_activate_resource(&res_hello, "test/hello");
|
||||
/* coap_activate_resource(&res_mirror, "debug/mirror"); */
|
||||
/* coap_activate_resource(&res_chunks, "test/chunks"); */
|
||||
/* coap_activate_resource(&res_separate, "test/separate"); */
|
||||
coap_activate_resource(&res_push, "test/push");
|
||||
/* coap_activate_resource(&res_event, "sensors/button"); */
|
||||
/* coap_activate_resource(&res_sub, "test/sub"); */
|
||||
/* coap_activate_resource(&res_b1_sep_b2, "test/b1sepb2"); */
|
||||
#if PLATFORM_HAS_LEDS
|
||||
/* rest_activate_resource(&res_leds, "actuators/leds"); */
|
||||
rest_activate_resource(&res_toggle, "actuators/toggle");
|
||||
/* coap_activate_resource(&res_leds, "actuators/leds"); */
|
||||
coap_activate_resource(&res_toggle, "actuators/toggle");
|
||||
#endif
|
||||
#if PLATFORM_HAS_LIGHT
|
||||
rest_activate_resource(&res_light, "sensors/light");
|
||||
coap_activate_resource(&res_light, "sensors/light");
|
||||
SENSORS_ACTIVATE(light_sensor);
|
||||
#endif
|
||||
#if PLATFORM_HAS_BATTERY
|
||||
rest_activate_resource(&res_battery, "sensors/battery");
|
||||
coap_activate_resource(&res_battery, "sensors/battery");
|
||||
SENSORS_ACTIVATE(battery_sensor);
|
||||
#endif
|
||||
#if PLATFORM_HAS_TEMPERATURE
|
||||
rest_activate_resource(&res_temperature, "sensors/temperature");
|
||||
coap_activate_resource(&res_temperature, "sensors/temperature");
|
||||
SENSORS_ACTIVATE(temperature_sensor);
|
||||
#endif
|
||||
/*
|
||||
#if PLATFORM_HAS_RADIO
|
||||
rest_activate_resource(&res_radio, "sensors/radio");
|
||||
coap_activate_resource(&res_radio, "sensors/radio");
|
||||
#endif
|
||||
#if PLATFORM_HAS_SHT11
|
||||
rest_activate_resource(&res_sht11, "sensors/sht11");
|
||||
coap_activate_resource(&res_sht11, "sensors/sht11");
|
||||
SENSORS_ACTIVATE(sht11_sensor);
|
||||
#endif
|
||||
*/
|
||||
|
|
|
@ -44,7 +44,7 @@
|
|||
#include "coap.h"
|
||||
#include "coap-transactions.h"
|
||||
#include "coap-separate.h"
|
||||
#include "rest-engine.h"
|
||||
#include "coap-engine.h"
|
||||
#include "plugtest.h"
|
||||
|
||||
/*
|
||||
|
@ -52,7 +52,7 @@
|
|||
* The build system automatically compiles the resources in the corresponding
|
||||
* sub-directory.
|
||||
*/
|
||||
extern resource_t
|
||||
extern coap_resource_t
|
||||
res_plugtest_test,
|
||||
res_plugtest_validate,
|
||||
res_plugtest_create1,
|
||||
|
@ -95,29 +95,29 @@ PROCESS_THREAD(plugtest_server, ev, data)
|
|||
PRINTF("REST max chunk: %u\n", REST_MAX_CHUNK_SIZE);
|
||||
|
||||
/* Initialize the REST engine. */
|
||||
rest_init_engine();
|
||||
coap_engine_init();
|
||||
|
||||
/* Activate the application-specific resources. */
|
||||
rest_activate_resource(&res_plugtest_test, "test");
|
||||
rest_activate_resource(&res_plugtest_validate, "validate");
|
||||
rest_activate_resource(&res_plugtest_create1, "create1");
|
||||
rest_activate_resource(&res_plugtest_create2, "create2");
|
||||
rest_activate_resource(&res_plugtest_create3, "create3");
|
||||
rest_activate_resource(&res_plugtest_longpath, "seg1/seg2/seg3");
|
||||
rest_activate_resource(&res_plugtest_query, "query");
|
||||
rest_activate_resource(&res_plugtest_locquery, "location-query");
|
||||
rest_activate_resource(&res_plugtest_multi, "multi-format");
|
||||
rest_activate_resource(&res_plugtest_link1, "link1");
|
||||
rest_activate_resource(&res_plugtest_link2, "link2");
|
||||
rest_activate_resource(&res_plugtest_link3, "link3");
|
||||
rest_activate_resource(&res_plugtest_path, "path");
|
||||
rest_activate_resource(&res_plugtest_separate, "separate");
|
||||
rest_activate_resource(&res_plugtest_large, "large");
|
||||
rest_activate_resource(&res_plugtest_large_update, "large-update");
|
||||
rest_activate_resource(&res_plugtest_large_create, "large-create");
|
||||
rest_activate_resource(&res_plugtest_obs, "obs");
|
||||
coap_activate_resource(&res_plugtest_test, "test");
|
||||
coap_activate_resource(&res_plugtest_validate, "validate");
|
||||
coap_activate_resource(&res_plugtest_create1, "create1");
|
||||
coap_activate_resource(&res_plugtest_create2, "create2");
|
||||
coap_activate_resource(&res_plugtest_create3, "create3");
|
||||
coap_activate_resource(&res_plugtest_longpath, "seg1/seg2/seg3");
|
||||
coap_activate_resource(&res_plugtest_query, "query");
|
||||
coap_activate_resource(&res_plugtest_locquery, "location-query");
|
||||
coap_activate_resource(&res_plugtest_multi, "multi-format");
|
||||
coap_activate_resource(&res_plugtest_link1, "link1");
|
||||
coap_activate_resource(&res_plugtest_link2, "link2");
|
||||
coap_activate_resource(&res_plugtest_link3, "link3");
|
||||
coap_activate_resource(&res_plugtest_path, "path");
|
||||
coap_activate_resource(&res_plugtest_separate, "separate");
|
||||
coap_activate_resource(&res_plugtest_large, "large");
|
||||
coap_activate_resource(&res_plugtest_large_update, "large-update");
|
||||
coap_activate_resource(&res_plugtest_large_create, "large-create");
|
||||
coap_activate_resource(&res_plugtest_obs, "obs");
|
||||
|
||||
rest_activate_resource(&res_mirror, "mirror");
|
||||
coap_activate_resource(&res_mirror, "mirror");
|
||||
|
||||
/* Define application-specific events here. */
|
||||
while(1) {
|
||||
|
|
|
@ -37,12 +37,12 @@
|
|||
*/
|
||||
|
||||
#include <string.h>
|
||||
#include "rest-engine.h"
|
||||
#include "coap-engine.h"
|
||||
#include "coap.h"
|
||||
#include "plugtest.h"
|
||||
|
||||
static void res_put_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
static void res_delete_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
static void res_put_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
static void res_delete_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
|
||||
RESOURCE(res_plugtest_create1,
|
||||
"title=\"Creates on PUT\"",
|
||||
|
@ -54,27 +54,27 @@ RESOURCE(res_plugtest_create1,
|
|||
static uint8_t create1_exists = 0;
|
||||
|
||||
static void
|
||||
res_put_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
res_put_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
{
|
||||
PRINTF("/create1 PUT");
|
||||
|
||||
if(coap_get_header_if_none_match(request)) {
|
||||
if(!create1_exists) {
|
||||
REST.set_response_status(response, REST.status.CREATED);
|
||||
coap_set_status_code(response, CREATED_2_01);
|
||||
|
||||
create1_exists = 1;
|
||||
} else {
|
||||
REST.set_response_status(response, PRECONDITION_FAILED_4_12);
|
||||
coap_set_status_code(response, PRECONDITION_FAILED_4_12);
|
||||
}
|
||||
} else {
|
||||
REST.set_response_status(response, REST.status.CHANGED);
|
||||
coap_set_status_code(response, CHANGED_2_04);
|
||||
}
|
||||
}
|
||||
static void
|
||||
res_delete_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
res_delete_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
{
|
||||
PRINTF("/create1 DELETE ");
|
||||
REST.set_response_status(response, REST.status.DELETED);
|
||||
coap_set_status_code(response, DELETED_2_02);
|
||||
|
||||
create1_exists = 0;
|
||||
}
|
||||
|
|
|
@ -37,11 +37,11 @@
|
|||
*/
|
||||
|
||||
#include <string.h>
|
||||
#include "rest-engine.h"
|
||||
#include "coap-engine.h"
|
||||
#include "coap.h"
|
||||
#include "plugtest.h"
|
||||
|
||||
static void res_post_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
static void res_post_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
|
||||
RESOURCE(res_plugtest_create2,
|
||||
"title=\"Creates on POST\"",
|
||||
|
@ -51,10 +51,10 @@ RESOURCE(res_plugtest_create2,
|
|||
NULL);
|
||||
|
||||
static void
|
||||
res_post_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
res_post_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
{
|
||||
PRINTF("/create2 ");
|
||||
|
||||
REST.set_response_status(response, REST.status.CREATED);
|
||||
REST.set_header_location(response, "/location1/location2/location3");
|
||||
coap_set_status_code(response, CREATED_2_01);
|
||||
coap_set_header_location_path(response, "/location1/location2/location3");
|
||||
}
|
||||
|
|
|
@ -37,12 +37,12 @@
|
|||
*/
|
||||
|
||||
#include <string.h>
|
||||
#include "rest-engine.h"
|
||||
#include "coap-engine.h"
|
||||
#include "coap.h"
|
||||
#include "plugtest.h"
|
||||
|
||||
static void res_put_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
static void res_delete_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
static void res_put_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
static void res_delete_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
|
||||
RESOURCE(res_plugtest_create3,
|
||||
"title=\"Default test resource\"",
|
||||
|
@ -54,27 +54,27 @@ RESOURCE(res_plugtest_create3,
|
|||
static uint8_t create3_exists = 0;
|
||||
|
||||
static void
|
||||
res_put_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
res_put_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
{
|
||||
PRINTF("/create3 PUT ");
|
||||
|
||||
if(coap_get_header_if_none_match(request)) {
|
||||
if(!create3_exists) {
|
||||
REST.set_response_status(response, REST.status.CREATED);
|
||||
coap_set_status_code(response, CREATED_2_01);
|
||||
|
||||
create3_exists = 1;
|
||||
} else {
|
||||
REST.set_response_status(response, PRECONDITION_FAILED_4_12);
|
||||
coap_set_status_code(response, PRECONDITION_FAILED_4_12);
|
||||
}
|
||||
} else {
|
||||
REST.set_response_status(response, REST.status.CHANGED);
|
||||
coap_set_status_code(response, CHANGED_2_04);
|
||||
}
|
||||
}
|
||||
static void
|
||||
res_delete_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
res_delete_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
{
|
||||
PRINTF("/create3 DELETE ");
|
||||
REST.set_response_status(response, REST.status.DELETED);
|
||||
coap_set_status_code(response, DELETED_2_02);
|
||||
|
||||
create3_exists = 0;
|
||||
}
|
||||
|
|
|
@ -37,11 +37,11 @@
|
|||
*/
|
||||
|
||||
#include <string.h>
|
||||
#include "rest-engine.h"
|
||||
#include "coap-engine.h"
|
||||
#include "coap.h"
|
||||
#include "plugtest.h"
|
||||
|
||||
static void res_post_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
static void res_post_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
|
||||
/*
|
||||
* Large resource that can be created using POST method
|
||||
|
@ -54,7 +54,7 @@ RESOURCE(res_plugtest_large_create,
|
|||
NULL);
|
||||
|
||||
static void
|
||||
res_post_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
res_post_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
{
|
||||
coap_packet_t *const coap_req = (coap_packet_t *)request;
|
||||
|
||||
|
@ -63,29 +63,29 @@ res_post_handler(void *request, void *response, uint8_t *buffer, uint16_t prefer
|
|||
|
||||
unsigned int ct = -1;
|
||||
|
||||
if(!REST.get_header_content_type(request, &ct)) {
|
||||
REST.set_response_status(response, REST.status.BAD_REQUEST);
|
||||
if(!coap_get_header_content_format(request, &ct)) {
|
||||
coap_set_status_code(response, BAD_REQUEST_4_00);
|
||||
const char *error_msg = "NoContentType";
|
||||
REST.set_response_payload(response, error_msg, strlen(error_msg));
|
||||
coap_set_payload(response, error_msg, strlen(error_msg));
|
||||
return;
|
||||
}
|
||||
|
||||
if((len = REST.get_request_payload(request, (const uint8_t **)&incoming))) {
|
||||
if((len = coap_get_payload(request, (const uint8_t **)&incoming))) {
|
||||
if(coap_req->block1_num * coap_req->block1_size + len <= 2048) {
|
||||
REST.set_response_status(response, REST.status.CREATED);
|
||||
REST.set_header_location(response, "/nirvana");
|
||||
coap_set_status_code(response, CREATED_2_01);
|
||||
coap_set_header_location_path(response, "/nirvana");
|
||||
coap_set_header_block1(response, coap_req->block1_num, 0,
|
||||
coap_req->block1_size);
|
||||
} else {
|
||||
REST.set_response_status(response, REST.status.REQUEST_ENTITY_TOO_LARGE);
|
||||
coap_set_status_code(response, REQUEST_ENTITY_TOO_LARGE_4_13);
|
||||
const char *error_msg = "2048B max.";
|
||||
REST.set_response_payload(response, error_msg, strlen(error_msg));
|
||||
coap_set_payload(response, error_msg, strlen(error_msg));
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
REST.set_response_status(response, REST.status.BAD_REQUEST);
|
||||
coap_set_status_code(response, BAD_REQUEST_4_00);
|
||||
const char *error_msg = "NoPayload";
|
||||
REST.set_response_payload(response, error_msg, strlen(error_msg));
|
||||
coap_set_payload(response, error_msg, strlen(error_msg));
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -36,14 +36,15 @@
|
|||
* Matthias Kovatsch <kovatsch@inf.ethz.ch>
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "sys/cc.h"
|
||||
#include "rest-engine.h"
|
||||
#include "coap-engine.h"
|
||||
#include "coap.h"
|
||||
#include "plugtest.h"
|
||||
|
||||
static void res_get_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
static void res_put_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
static void res_get_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
static void res_put_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
|
||||
RESOURCE(
|
||||
res_plugtest_large_update,
|
||||
|
@ -58,21 +59,21 @@ static uint8_t large_update_store[MAX_PLUGFEST_BODY] = { 0 };
|
|||
static unsigned int large_update_ct = APPLICATION_OCTET_STREAM;
|
||||
|
||||
static void
|
||||
res_get_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
res_get_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
{
|
||||
/* Check the offset for boundaries of the resource data. */
|
||||
if(*offset >= large_update_size) {
|
||||
REST.set_response_status(response, REST.status.BAD_OPTION);
|
||||
coap_set_status_code(response, BAD_OPTION_4_02);
|
||||
/* A block error message should not exceed the minimum block size (16). */
|
||||
|
||||
const char *error_msg = "BlockOutOfScope";
|
||||
REST.set_response_payload(response, error_msg, strlen(error_msg));
|
||||
coap_set_payload(response, error_msg, strlen(error_msg));
|
||||
return;
|
||||
}
|
||||
|
||||
REST.set_response_payload(response, large_update_store + *offset,
|
||||
coap_set_payload(response, large_update_store + *offset,
|
||||
MIN(large_update_size - *offset, preferred_size));
|
||||
REST.set_header_content_type(response, large_update_ct);
|
||||
coap_set_header_content_format(response, large_update_ct);
|
||||
|
||||
/* IMPORTANT for chunk-wise resources: Signal chunk awareness to REST engine. */
|
||||
*offset += preferred_size;
|
||||
|
@ -83,7 +84,7 @@ res_get_handler(void *request, void *response, uint8_t *buffer, uint16_t preferr
|
|||
}
|
||||
}
|
||||
static void
|
||||
res_put_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
res_put_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
{
|
||||
coap_packet_t *const coap_req = (coap_packet_t *)request;
|
||||
uint8_t *incoming = NULL;
|
||||
|
@ -91,14 +92,14 @@ res_put_handler(void *request, void *response, uint8_t *buffer, uint16_t preferr
|
|||
|
||||
unsigned int ct = -1;
|
||||
|
||||
if(!REST.get_header_content_type(request, &ct)) {
|
||||
REST.set_response_status(response, REST.status.BAD_REQUEST);
|
||||
if(!coap_get_header_content_format(request, &ct)) {
|
||||
coap_set_status_code(response, BAD_REQUEST_4_00);
|
||||
const char *error_msg = "NoContentType";
|
||||
REST.set_response_payload(response, error_msg, strlen(error_msg));
|
||||
coap_set_payload(response, error_msg, strlen(error_msg));
|
||||
return;
|
||||
}
|
||||
|
||||
if((len = REST.get_request_payload(request, (const uint8_t **)&incoming))) {
|
||||
if((len = coap_get_payload(request, (const uint8_t **)&incoming))) {
|
||||
if(coap_req->block1_num * coap_req->block1_size + len <= sizeof(large_update_store)) {
|
||||
memcpy(
|
||||
large_update_store + coap_req->block1_num * coap_req->block1_size,
|
||||
|
@ -106,13 +107,13 @@ res_put_handler(void *request, void *response, uint8_t *buffer, uint16_t preferr
|
|||
large_update_size = coap_req->block1_num * coap_req->block1_size + len;
|
||||
large_update_ct = ct;
|
||||
|
||||
REST.set_response_status(response, REST.status.CHANGED);
|
||||
coap_set_status_code(response, CHANGED_2_04);
|
||||
coap_set_header_block1(response, coap_req->block1_num, 0,
|
||||
coap_req->block1_size);
|
||||
} else {
|
||||
REST.set_response_status(response,
|
||||
REST.status.REQUEST_ENTITY_TOO_LARGE);
|
||||
REST.set_response_payload(
|
||||
coap_set_status_code(response,
|
||||
REQUEST_ENTITY_TOO_LARGE_4_13);
|
||||
coap_set_payload(
|
||||
response,
|
||||
buffer,
|
||||
snprintf((char *)buffer, MAX_PLUGFEST_PAYLOAD, "%uB max.",
|
||||
|
@ -120,9 +121,9 @@ res_put_handler(void *request, void *response, uint8_t *buffer, uint16_t preferr
|
|||
return;
|
||||
}
|
||||
} else {
|
||||
REST.set_response_status(response, REST.status.BAD_REQUEST);
|
||||
coap_set_status_code(response, BAD_REQUEST_4_00);
|
||||
const char *error_msg = "NoPayload";
|
||||
REST.set_response_payload(response, error_msg, strlen(error_msg));
|
||||
coap_set_payload(response, error_msg, strlen(error_msg));
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -36,12 +36,13 @@
|
|||
* Matthias Kovatsch <kovatsch@inf.ethz.ch>
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "rest-engine.h"
|
||||
#include "coap-engine.h"
|
||||
#include "coap.h"
|
||||
#include "plugtest.h"
|
||||
|
||||
static void res_get_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
static void res_get_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
|
||||
RESOURCE(res_plugtest_large,
|
||||
"title=\"Large resource\";rt=\"block\";sz=\"" TO_STRING(CHUNKS_TOTAL) "\"",
|
||||
|
@ -51,17 +52,17 @@ RESOURCE(res_plugtest_large,
|
|||
NULL);
|
||||
|
||||
static void
|
||||
res_get_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
res_get_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
{
|
||||
int32_t strpos = 0;
|
||||
|
||||
/* Check the offset for boundaries of the resource data. */
|
||||
if(*offset >= CHUNKS_TOTAL) {
|
||||
REST.set_response_status(response, REST.status.BAD_OPTION);
|
||||
coap_set_status_code(response, BAD_OPTION_4_02);
|
||||
/* A block error message should not exceed the minimum block size (16). */
|
||||
|
||||
const char *error_msg = "BlockOutOfScope";
|
||||
REST.set_response_payload(response, error_msg, strlen(error_msg));
|
||||
coap_set_payload(response, error_msg, strlen(error_msg));
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -79,8 +80,8 @@ res_get_handler(void *request, void *response, uint8_t *buffer, uint16_t preferr
|
|||
if(*offset + (int32_t)strpos > CHUNKS_TOTAL) {
|
||||
strpos = CHUNKS_TOTAL - *offset;
|
||||
}
|
||||
REST.set_response_payload(response, buffer, strpos);
|
||||
REST.set_header_content_type(response, REST.type.TEXT_PLAIN);
|
||||
coap_set_payload(response, buffer, strpos);
|
||||
coap_set_header_content_format(response, TEXT_PLAIN);
|
||||
|
||||
/* IMPORTANT for chunk-wise resources: Signal chunk awareness to REST engine. */
|
||||
*offset += strpos;
|
||||
|
|
|
@ -37,11 +37,11 @@
|
|||
*/
|
||||
|
||||
#include <string.h>
|
||||
#include "rest-engine.h"
|
||||
#include "coap-engine.h"
|
||||
#include "coap.h"
|
||||
#include "plugtest.h"
|
||||
|
||||
static void res_get_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
static void res_get_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
|
||||
RESOURCE(res_plugtest_link1,
|
||||
"rt=\"Type1 Type2\";if=\"If1\"",
|
||||
|
@ -63,9 +63,9 @@ RESOURCE(res_plugtest_link3,
|
|||
NULL);
|
||||
|
||||
static void
|
||||
res_get_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
res_get_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
{
|
||||
const char *msg = "Dummy link";
|
||||
REST.set_header_content_type(response, REST.type.TEXT_PLAIN);
|
||||
REST.set_response_payload(response, msg, strlen(msg));
|
||||
coap_set_header_content_format(response, TEXT_PLAIN);
|
||||
coap_set_payload(response, msg, strlen(msg));
|
||||
}
|
||||
|
|
|
@ -37,11 +37,11 @@
|
|||
*/
|
||||
|
||||
#include <string.h>
|
||||
#include "rest-engine.h"
|
||||
#include "coap-engine.h"
|
||||
#include "coap.h"
|
||||
#include "plugtest.h"
|
||||
|
||||
static void res_post_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
static void res_post_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
|
||||
RESOURCE(res_plugtest_locquery,
|
||||
"title=\"Resource accepting query parameters\"",
|
||||
|
@ -51,7 +51,7 @@ RESOURCE(res_plugtest_locquery,
|
|||
NULL);
|
||||
|
||||
static void
|
||||
res_post_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
res_post_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
{
|
||||
#if DEBUG
|
||||
coap_packet_t *const coap_req = (coap_packet_t *)request;
|
||||
|
@ -60,6 +60,6 @@ res_post_handler(void *request, void *response, uint8_t *buffer, uint16_t prefer
|
|||
PRINTF(
|
||||
"/location-query POST (%s %u)\n", coap_req->type == COAP_TYPE_CON ? "CON" : "NON", coap_req->mid);
|
||||
|
||||
REST.set_response_status(response, REST.status.CREATED);
|
||||
REST.set_header_location(response, "?first=1&second=2");
|
||||
coap_set_status_code(response, CREATED_2_01);
|
||||
coap_set_header_location_query(response, "?first=1&second=2");
|
||||
}
|
||||
|
|
|
@ -36,12 +36,14 @@
|
|||
* Matthias Kovatsch <kovatsch@inf.ethz.ch>
|
||||
*/
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "rest-engine.h"
|
||||
#include "coap-engine.h"
|
||||
#include "coap.h"
|
||||
#include "plugtest.h"
|
||||
|
||||
static void res_get_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
static void res_get_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
|
||||
RESOURCE(res_plugtest_longpath,
|
||||
"title=\"Long path resource\"",
|
||||
|
@ -51,14 +53,14 @@ RESOURCE(res_plugtest_longpath,
|
|||
NULL);
|
||||
|
||||
static void
|
||||
res_get_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
res_get_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
{
|
||||
coap_packet_t *const coap_req = (coap_packet_t *)request;
|
||||
|
||||
PRINTF("/seg1/seg2/seg3 GET ");
|
||||
/* Code 2.05 CONTENT is default. */
|
||||
REST.set_header_content_type(response, REST.type.TEXT_PLAIN);
|
||||
REST.set_response_payload(
|
||||
coap_set_header_content_format(response, TEXT_PLAIN);
|
||||
coap_set_payload(
|
||||
response,
|
||||
buffer,
|
||||
snprintf((char *)buffer, MAX_PLUGFEST_PAYLOAD,
|
||||
|
|
|
@ -36,12 +36,13 @@
|
|||
* Matthias Kovatsch <kovatsch@inf.ethz.ch>
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "rest-engine.h"
|
||||
#include "coap-engine.h"
|
||||
#include "coap.h"
|
||||
#include "plugtest.h"
|
||||
|
||||
static void res_get_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
static void res_get_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
|
||||
RESOURCE(res_plugtest_multi,
|
||||
"title=\"Resource providing text/plain and application/xml\";ct=\"0 41\"",
|
||||
|
@ -51,27 +52,27 @@ RESOURCE(res_plugtest_multi,
|
|||
NULL);
|
||||
|
||||
static void
|
||||
res_get_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
res_get_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
{
|
||||
coap_packet_t *const coap_req = (coap_packet_t *)request;
|
||||
|
||||
unsigned int accept = -1;
|
||||
REST.get_header_accept(request, &accept);
|
||||
coap_get_header_accept(request, &accept);
|
||||
|
||||
PRINTF("/multi-format GET (%s %u) ", coap_req->type == COAP_TYPE_CON ? "CON" : "NON", coap_req->mid);
|
||||
|
||||
if(accept == -1 || accept == REST.type.TEXT_PLAIN) {
|
||||
REST.set_header_content_type(response, REST.type.TEXT_PLAIN);
|
||||
REST.set_response_payload(
|
||||
if(accept == -1 || accept == TEXT_PLAIN) {
|
||||
coap_set_header_content_format(response, TEXT_PLAIN);
|
||||
coap_set_payload(
|
||||
response,
|
||||
buffer,
|
||||
snprintf((char *)buffer, MAX_PLUGFEST_PAYLOAD,
|
||||
"Type: %u\nCode: %u\nMID: %u%s", coap_req->type, coap_req->code,
|
||||
coap_req->mid, accept != -1 ? "\nAccept: 0" : ""));
|
||||
PRINTF("PLAIN\n");
|
||||
} else if(accept == REST.type.APPLICATION_XML) {
|
||||
REST.set_header_content_type(response, REST.type.APPLICATION_XML);
|
||||
REST.set_response_payload(
|
||||
} else if(accept == APPLICATION_XML) {
|
||||
coap_set_header_content_format(response, APPLICATION_XML);
|
||||
coap_set_payload(
|
||||
response,
|
||||
buffer,
|
||||
snprintf((char *)buffer, MAX_PLUGFEST_PAYLOAD,
|
||||
|
@ -79,9 +80,9 @@ res_get_handler(void *request, void *response, uint8_t *buffer, uint16_t preferr
|
|||
coap_req->type, coap_req->code, coap_req->mid, accept));
|
||||
PRINTF("XML\n");
|
||||
} else {
|
||||
REST.set_response_status(response, REST.status.NOT_ACCEPTABLE);
|
||||
coap_set_status_code(response, NOT_ACCEPTABLE_4_06);
|
||||
const char *msg = "Supporting content-types text/plain and application/xml";
|
||||
REST.set_response_payload(response, msg, strlen(msg));
|
||||
coap_set_payload(response, msg, strlen(msg));
|
||||
PRINTF("ERROR\n");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -36,15 +36,16 @@
|
|||
* Matthias Kovatsch <kovatsch@inf.ethz.ch>
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "rest-engine.h"
|
||||
#include "coap-engine.h"
|
||||
#include "coap.h"
|
||||
#include "coap-observe.h"
|
||||
#include "plugtest.h"
|
||||
|
||||
static void res_get_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
static void res_put_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
static void res_delete_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
static void res_get_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
static void res_put_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
static void res_delete_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
static void res_periodic_handler(void);
|
||||
|
||||
PERIODIC_RESOURCE(res_plugtest_obs,
|
||||
|
@ -67,35 +68,35 @@ static void
|
|||
obs_purge_list()
|
||||
{
|
||||
PRINTF("### SERVER ACTION ### Purging obs list");
|
||||
coap_remove_observer_by_uri(NULL, 0, res_plugtest_obs.url);
|
||||
coap_remove_observer_by_uri(NULL, res_plugtest_obs.url);
|
||||
}
|
||||
static void
|
||||
res_get_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
res_get_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
{
|
||||
/* Keep server log clean from ticking events */
|
||||
if(request != NULL) {
|
||||
PRINTF("/obs GET\n");
|
||||
}
|
||||
REST.set_header_content_type(response, obs_format);
|
||||
REST.set_header_max_age(response, 5);
|
||||
coap_set_header_content_format(response, obs_format);
|
||||
coap_set_header_max_age(response, 5);
|
||||
|
||||
if(obs_content_len) {
|
||||
REST.set_header_content_type(response, obs_format);
|
||||
REST.set_response_payload(response, obs_content, obs_content_len);
|
||||
coap_set_header_content_format(response, obs_format);
|
||||
coap_set_payload(response, obs_content, obs_content_len);
|
||||
} else {
|
||||
REST.set_header_content_type(response, REST.type.TEXT_PLAIN);
|
||||
REST.set_response_payload(response, obs_content,
|
||||
coap_set_header_content_format(response, TEXT_PLAIN);
|
||||
coap_set_payload(response, obs_content,
|
||||
snprintf(obs_content, MAX_PLUGFEST_PAYLOAD, "TICK %lu", (unsigned long) obs_counter));
|
||||
}
|
||||
/* A post_handler that handles subscriptions will be called for periodic resources by the REST framework. */
|
||||
/* A post_handler that handles subscriptions will be called for periodic resources by the CoAP framework. */
|
||||
}
|
||||
static void
|
||||
res_put_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
res_put_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
{
|
||||
uint8_t *incoming = NULL;
|
||||
unsigned int ct = -1;
|
||||
|
||||
REST.get_header_content_type(request, &ct);
|
||||
coap_get_header_content_format(request, &ct);
|
||||
|
||||
PRINTF("/obs PUT\n");
|
||||
|
||||
|
@ -103,26 +104,26 @@ res_put_handler(void *request, void *response, uint8_t *buffer, uint16_t preferr
|
|||
obs_status = 1;
|
||||
obs_format = ct;
|
||||
} else {
|
||||
obs_content_len = REST.get_request_payload(request,
|
||||
obs_content_len = coap_get_payload(request,
|
||||
(const uint8_t **)&incoming);
|
||||
memcpy(obs_content, incoming, obs_content_len);
|
||||
res_periodic_handler();
|
||||
}
|
||||
|
||||
REST.set_response_status(response, REST.status.CHANGED);
|
||||
coap_set_status_code(response, CHANGED_2_04);
|
||||
}
|
||||
static void
|
||||
res_delete_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
res_delete_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
{
|
||||
PRINTF("/obs DELETE\n");
|
||||
|
||||
obs_status = 2;
|
||||
|
||||
REST.set_response_status(response, REST.status.DELETED);
|
||||
coap_set_status_code(response, DELETED_2_02);
|
||||
}
|
||||
/*
|
||||
* Additionally, a handler function named [resource name]_handler must be implemented for each PERIODIC_RESOURCE.
|
||||
* It will be called by the REST manager process with the defined period.
|
||||
* It will be called by the CoAP manager process with the defined period.
|
||||
*/
|
||||
static void
|
||||
res_periodic_handler()
|
||||
|
@ -134,7 +135,7 @@ res_periodic_handler()
|
|||
if(obs_status == 1) {
|
||||
|
||||
/* Notify the registered observers with the given message type, observe option, and payload. */
|
||||
REST.notify_subscribers(&res_plugtest_obs);
|
||||
coap_notify_observers(&res_plugtest_obs);
|
||||
|
||||
PRINTF("######### sending 5.00\n");
|
||||
|
||||
|
@ -142,7 +143,7 @@ res_periodic_handler()
|
|||
} else if(obs_status == 2) {
|
||||
|
||||
/* Notify the registered observers with the given message type, observe option, and payload. */
|
||||
REST.notify_subscribers(&res_plugtest_obs);
|
||||
coap_notify_observers(&res_plugtest_obs);
|
||||
|
||||
obs_purge_list();
|
||||
|
||||
|
@ -150,6 +151,6 @@ res_periodic_handler()
|
|||
obs_content_len = 0;
|
||||
} else {
|
||||
/* Notify the registered observers with the given message type, observe option, and payload. */
|
||||
REST.notify_subscribers(&res_plugtest_obs);
|
||||
coap_notify_observers(&res_plugtest_obs);
|
||||
} obs_status = 0;
|
||||
}
|
||||
|
|
|
@ -36,12 +36,13 @@
|
|||
* Matthias Kovatsch <kovatsch@inf.ethz.ch>
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "rest-engine.h"
|
||||
#include "coap-engine.h"
|
||||
#include "coap.h"
|
||||
#include "plugtest.h"
|
||||
|
||||
static void res_get_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
static void res_get_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
|
||||
PARENT_RESOURCE(res_plugtest_path,
|
||||
"title=\"Path test resource\";ct=\"40\"",
|
||||
|
@ -51,22 +52,22 @@ PARENT_RESOURCE(res_plugtest_path,
|
|||
NULL);
|
||||
|
||||
static void
|
||||
res_get_handler(void *request, void *response, uint8_t *buffer,
|
||||
res_get_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer,
|
||||
uint16_t preferred_size, int32_t *offset)
|
||||
{
|
||||
|
||||
const char *uri_path = NULL;
|
||||
int len = REST.get_url(request, &uri_path);
|
||||
int len = coap_get_header_uri_path(request, &uri_path);
|
||||
int base_len = strlen(res_plugtest_path.url);
|
||||
|
||||
if(len == base_len) {
|
||||
REST.set_header_content_type(response, REST.type.APPLICATION_LINK_FORMAT);
|
||||
coap_set_header_content_format(response, APPLICATION_LINK_FORMAT);
|
||||
snprintf((char *)buffer, MAX_PLUGFEST_PAYLOAD,
|
||||
"</path/sub1>,</path/sub2>,</path/sub3>");
|
||||
} else {
|
||||
REST.set_header_content_type(response, REST.type.TEXT_PLAIN);
|
||||
coap_set_header_content_format(response, TEXT_PLAIN);
|
||||
snprintf((char *)buffer, MAX_PLUGFEST_PAYLOAD, "/%.*s", len, uri_path);
|
||||
}
|
||||
|
||||
REST.set_response_payload(response, buffer, strlen((char *)buffer));
|
||||
coap_set_payload(response, buffer, strlen((char *)buffer));
|
||||
}
|
||||
|
|
|
@ -36,12 +36,13 @@
|
|||
* Matthias Kovatsch <kovatsch@inf.ethz.ch>
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "rest-engine.h"
|
||||
#include "coap-engine.h"
|
||||
#include "coap.h"
|
||||
#include "plugtest.h"
|
||||
|
||||
static void res_get_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
static void res_get_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
|
||||
RESOURCE(res_plugtest_query,
|
||||
"title=\"Resource accepting query parameters\"",
|
||||
|
@ -51,7 +52,7 @@ RESOURCE(res_plugtest_query,
|
|||
NULL);
|
||||
|
||||
static void
|
||||
res_get_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
res_get_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
{
|
||||
coap_packet_t *const coap_req = (coap_packet_t *)request;
|
||||
int len = 0;
|
||||
|
@ -60,13 +61,13 @@ res_get_handler(void *request, void *response, uint8_t *buffer, uint16_t preferr
|
|||
PRINTF(
|
||||
"/query GET (%s %u)\n", coap_req->type == COAP_TYPE_CON ? "CON" : "NON", coap_req->mid);
|
||||
|
||||
if((len = REST.get_query(request, &query))) {
|
||||
if((len = coap_get_header_uri_query(request, &query))) {
|
||||
PRINTF("Query: %.*s\n", len, query);
|
||||
/* Code 2.05 CONTENT is default. */
|
||||
}
|
||||
REST.set_header_content_type(response,
|
||||
REST.type.TEXT_PLAIN);
|
||||
REST.set_response_payload(
|
||||
coap_set_header_content_format(response,
|
||||
TEXT_PLAIN);
|
||||
coap_set_payload(
|
||||
response,
|
||||
buffer,
|
||||
snprintf((char *)buffer, MAX_PLUGFEST_PAYLOAD,
|
||||
|
|
|
@ -36,14 +36,15 @@
|
|||
* Matthias Kovatsch <kovatsch@inf.ethz.ch>
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "rest-engine.h"
|
||||
#include "coap-engine.h"
|
||||
#include "coap.h"
|
||||
#include "coap-transactions.h"
|
||||
#include "coap-separate.h"
|
||||
#include "plugtest.h"
|
||||
|
||||
static void res_get_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
static void res_get_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
static void res_resume_handler(void);
|
||||
|
||||
PERIODIC_RESOURCE(res_plugtest_separate,
|
||||
|
@ -67,7 +68,7 @@ static uint8_t separate_active = 0;
|
|||
static application_separate_store_t separate_store[1];
|
||||
|
||||
void
|
||||
res_get_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
res_get_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
{
|
||||
coap_packet_t *const coap_req = (coap_packet_t *)request;
|
||||
|
||||
|
@ -97,8 +98,7 @@ res_resume_handler()
|
|||
PRINTF("/separate ");
|
||||
coap_transaction_t *transaction = NULL;
|
||||
if((transaction = coap_new_transaction(separate_store->request_metadata.mid,
|
||||
&separate_store->request_metadata.addr,
|
||||
separate_store->request_metadata.port))) {
|
||||
&separate_store->request_metadata.endpoint))) {
|
||||
PRINTF(
|
||||
"RESPONSE (%s %u)\n", separate_store->request_metadata.type == COAP_TYPE_CON ? "CON" : "NON", separate_store->request_metadata.mid);
|
||||
|
||||
|
@ -107,7 +107,7 @@ res_resume_handler()
|
|||
/* Restore the request information for the response. */
|
||||
coap_separate_resume(response, &separate_store->request_metadata, CONTENT_2_05);
|
||||
|
||||
REST.set_header_content_type(response, REST.type.TEXT_PLAIN);
|
||||
coap_set_header_content_format(response, TEXT_PLAIN);
|
||||
coap_set_payload(response, separate_store->buffer,
|
||||
strlen(separate_store->buffer));
|
||||
|
||||
|
|
|
@ -36,15 +36,17 @@
|
|||
* Matthias Kovatsch <kovatsch@inf.ethz.ch>
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "rest-engine.h"
|
||||
#include "coap-engine.h"
|
||||
#include "coap.h"
|
||||
#include "plugtest.h"
|
||||
#include "random.h"
|
||||
|
||||
static void res_get_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
static void res_post_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
static void res_put_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
static void res_delete_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
static void res_get_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
static void res_post_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
static void res_put_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
static void res_delete_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
|
||||
RESOURCE(res_plugtest_test, "title=\"Default test resource\"", res_get_handler, res_post_handler, res_put_handler, res_delete_handler);
|
||||
|
||||
|
@ -69,7 +71,7 @@ test_update_etag()
|
|||
PRINTF("### SERVER ACTION ### Changed ETag %u [0x%02X%02X%02X%02X%02X%02X%02X%02X]\n", test_etag_len, test_etag[0], test_etag[1], test_etag[2], test_etag[3], test_etag[4], test_etag[5], test_etag[6], test_etag[7]);
|
||||
}
|
||||
static void
|
||||
res_get_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
res_get_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
{
|
||||
coap_packet_t *const coap_req = (coap_packet_t *)request;
|
||||
|
||||
|
@ -82,34 +84,34 @@ res_get_handler(void *request, void *response, uint8_t *buffer, uint16_t preferr
|
|||
&& len == test_etag_len
|
||||
&& memcmp(test_etag, bytes, len) == 0) {
|
||||
PRINTF("validate ");
|
||||
REST.set_response_status(response, REST.status.NOT_MODIFIED);
|
||||
REST.set_header_etag(response, test_etag, test_etag_len);
|
||||
coap_set_status_code(response, VALID_2_03);
|
||||
coap_set_header_etag(response, test_etag, test_etag_len);
|
||||
|
||||
test_change = 1;
|
||||
PRINTF("### SERVER ACTION ### Resource will change\n");
|
||||
} else {
|
||||
/* Code 2.05 CONTENT is default. */
|
||||
REST.set_header_content_type(response, REST.type.TEXT_PLAIN);
|
||||
REST.set_header_etag(response, test_etag, test_etag_len);
|
||||
REST.set_header_max_age(response, 30);
|
||||
REST.set_response_payload(
|
||||
coap_set_header_content_format(response, TEXT_PLAIN);
|
||||
coap_set_header_etag(response, test_etag, test_etag_len);
|
||||
coap_set_header_max_age(response, 30);
|
||||
coap_set_payload(
|
||||
response,
|
||||
buffer,
|
||||
snprintf((char *)buffer, MAX_PLUGFEST_PAYLOAD, "Type: %u\nCode: %u\nMID: %u", coap_req->type, coap_req->code, coap_req->mid));
|
||||
}
|
||||
}
|
||||
static void
|
||||
res_post_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
res_post_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
{
|
||||
#if DEBUG
|
||||
coap_packet_t *const coap_req = (coap_packet_t *)request;
|
||||
PRINTF("/test POST (%s %u)\n", coap_req->type == COAP_TYPE_CON ? "CON" : "NON", coap_req->mid);
|
||||
#endif
|
||||
REST.set_response_status(response, REST.status.CREATED);
|
||||
REST.set_header_location(response, "/location1/location2/location3");
|
||||
coap_set_status_code(response, CREATED_2_01);
|
||||
coap_set_header_location_path(response, "/location1/location2/location3");
|
||||
}
|
||||
static void
|
||||
res_put_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
res_put_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
{
|
||||
#if DEBUG
|
||||
coap_packet_t *const coap_req = (coap_packet_t *)request;
|
||||
|
@ -118,12 +120,12 @@ res_put_handler(void *request, void *response, uint8_t *buffer, uint16_t preferr
|
|||
|
||||
if(coap_get_header_if_none_match(request)) {
|
||||
if(test_none_match_okay) {
|
||||
REST.set_response_status(response, REST.status.CREATED);
|
||||
coap_set_status_code(response, CREATED_2_01);
|
||||
|
||||
test_none_match_okay = 0;
|
||||
PRINTF("### SERVER ACTION ### If-None-Match will FAIL\n");
|
||||
} else {
|
||||
REST.set_response_status(response, PRECONDITION_FAILED_4_12);
|
||||
coap_set_status_code(response, PRECONDITION_FAILED_4_12);
|
||||
|
||||
test_none_match_okay = 1;
|
||||
PRINTF("### SERVER ACTION ### If-None-Match will SUCCEED\n");
|
||||
|
@ -133,9 +135,9 @@ res_put_handler(void *request, void *response, uint8_t *buffer, uint16_t preferr
|
|||
&& memcmp(test_etag, bytes, len) == 0))
|
||||
|| len == 0) {
|
||||
test_update_etag();
|
||||
REST.set_header_etag(response, test_etag, test_etag_len);
|
||||
coap_set_header_etag(response, test_etag, test_etag_len);
|
||||
|
||||
REST.set_response_status(response, REST.status.CHANGED);
|
||||
coap_set_status_code(response, CHANGED_2_04);
|
||||
|
||||
if(len > 0) {
|
||||
test_change = 1;
|
||||
|
@ -148,15 +150,15 @@ res_put_handler(void *request, void *response, uint8_t *buffer, uint16_t preferr
|
|||
bytes[0], bytes[1], bytes[2], bytes[3], bytes[4], bytes[5], bytes[6], bytes[7],
|
||||
test_etag[0], test_etag[1], test_etag[2], test_etag[3], test_etag[4], test_etag[5], test_etag[6], test_etag[7]);
|
||||
|
||||
REST.set_response_status(response, PRECONDITION_FAILED_4_12);
|
||||
coap_set_status_code(response, PRECONDITION_FAILED_4_12);
|
||||
}
|
||||
}
|
||||
static void
|
||||
res_delete_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
res_delete_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
{
|
||||
#if DEBUG
|
||||
coap_packet_t *const coap_req = (coap_packet_t *)request;
|
||||
PRINTF("/test DELETE (%s %u)\n", coap_req->type == COAP_TYPE_CON ? "CON" : "NON", coap_req->mid);
|
||||
#endif
|
||||
REST.set_response_status(response, REST.status.DELETED);
|
||||
coap_set_status_code(response, DELETED_2_02);
|
||||
}
|
||||
|
|
|
@ -36,13 +36,15 @@
|
|||
* Matthias Kovatsch <kovatsch@inf.ethz.ch>
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "rest-engine.h"
|
||||
#include "coap-engine.h"
|
||||
#include "coap.h"
|
||||
#include "plugtest.h"
|
||||
#include "random.h"
|
||||
|
||||
static void res_get_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
static void res_put_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
static void res_get_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
static void res_put_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
|
||||
RESOURCE(res_plugtest_validate,
|
||||
"title=\"Validation test resource\"",
|
||||
|
@ -72,7 +74,7 @@ validate_update_etag()
|
|||
validate_etag_len, validate_etag[0], validate_etag[1], validate_etag[2], validate_etag[3], validate_etag[4], validate_etag[5], validate_etag[6], validate_etag[7]);
|
||||
}
|
||||
static void
|
||||
res_get_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
res_get_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
{
|
||||
coap_packet_t *const coap_req = (coap_packet_t *)request;
|
||||
|
||||
|
@ -85,17 +87,17 @@ res_get_handler(void *request, void *response, uint8_t *buffer, uint16_t preferr
|
|||
if((len = coap_get_header_etag(request, &bytes)) > 0
|
||||
&& len == validate_etag_len && memcmp(validate_etag, bytes, len) == 0) {
|
||||
PRINTF("validate ");
|
||||
REST.set_response_status(response, REST.status.NOT_MODIFIED);
|
||||
REST.set_header_etag(response, validate_etag, validate_etag_len);
|
||||
coap_set_status_code(response, VALID_2_03);
|
||||
coap_set_header_etag(response, validate_etag, validate_etag_len);
|
||||
|
||||
validate_change = 1;
|
||||
PRINTF("### SERVER ACTION ### Resouce will change\n");
|
||||
} else {
|
||||
/* Code 2.05 CONTENT is default. */
|
||||
REST.set_header_content_type(response, REST.type.TEXT_PLAIN);
|
||||
REST.set_header_etag(response, validate_etag, validate_etag_len);
|
||||
REST.set_header_max_age(response, 30);
|
||||
REST.set_response_payload(
|
||||
coap_set_header_content_format(response, TEXT_PLAIN);
|
||||
coap_set_header_etag(response, validate_etag, validate_etag_len);
|
||||
coap_set_header_max_age(response, 30);
|
||||
coap_set_payload(
|
||||
response,
|
||||
buffer,
|
||||
snprintf((char *)buffer, MAX_PLUGFEST_PAYLOAD,
|
||||
|
@ -104,7 +106,7 @@ res_get_handler(void *request, void *response, uint8_t *buffer, uint16_t preferr
|
|||
}
|
||||
}
|
||||
static void
|
||||
res_put_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
res_put_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
{
|
||||
#if DEBUG
|
||||
coap_packet_t *const coap_req = (coap_packet_t *)request;
|
||||
|
@ -118,9 +120,9 @@ res_put_handler(void *request, void *response, uint8_t *buffer, uint16_t preferr
|
|||
&& memcmp(validate_etag, bytes, len) == 0))
|
||||
|| len == 0) {
|
||||
validate_update_etag();
|
||||
REST.set_header_etag(response, validate_etag, validate_etag_len);
|
||||
coap_set_header_etag(response, validate_etag, validate_etag_len);
|
||||
|
||||
REST.set_response_status(response, REST.status.CHANGED);
|
||||
coap_set_status_code(response, CHANGED_2_04);
|
||||
|
||||
if(len > 0) {
|
||||
validate_change = 1;
|
||||
|
@ -134,6 +136,6 @@ res_put_handler(void *request, void *response, uint8_t *buffer, uint16_t preferr
|
|||
bytes[0], bytes[1], bytes[2], bytes[3], bytes[4], bytes[5], bytes[6], bytes[7],
|
||||
validate_etag[0], validate_etag[1], validate_etag[2], validate_etag[3], validate_etag[4], validate_etag[5], validate_etag[6], validate_etag[7]);
|
||||
|
||||
REST.set_response_status(response, PRECONDITION_FAILED_4_12);
|
||||
coap_set_status_code(response, PRECONDITION_FAILED_4_12);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -36,13 +36,14 @@
|
|||
* Lars Schmertmann <SmallLars@t-online.de>
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "rest-engine.h"
|
||||
#include "coap-engine.h"
|
||||
#include "coap-block1.h"
|
||||
#include "coap-separate.h"
|
||||
#include "coap-transactions.h"
|
||||
|
||||
static void res_post_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
static void res_post_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
SEPARATE_RESOURCE(res_b1_sep_b2, "title=\"Block1 + Separate + Block2 demo\"", NULL, res_post_handler, NULL, NULL, NULL);
|
||||
|
||||
#define MAX_DATA_LEN 256
|
||||
|
@ -52,7 +53,7 @@ static size_t big_msg_len = 0;
|
|||
static coap_separate_t request_metadata;
|
||||
|
||||
static void
|
||||
res_post_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
res_post_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
{
|
||||
/* Example allows only one request on time. There are no checks for multiply access !!! */
|
||||
if(*offset == 0) {
|
||||
|
@ -75,7 +76,7 @@ res_post_handler(void *request, void *response, uint8_t *buffer, uint16_t prefer
|
|||
|
||||
/* Send first block */
|
||||
coap_transaction_t *transaction = NULL;
|
||||
if((transaction = coap_new_transaction(request_metadata.mid, &request_metadata.addr, request_metadata.port))) {
|
||||
if((transaction = coap_new_transaction(request_metadata.mid, &request_metadata.endpoint))) {
|
||||
coap_packet_t resp[1]; /* This way the packet can be treated as pointer as usual. */
|
||||
|
||||
/* Restore the request information for the response. */
|
||||
|
|
|
@ -41,10 +41,11 @@
|
|||
#if PLATFORM_HAS_BATTERY
|
||||
|
||||
#include <string.h>
|
||||
#include "rest-engine.h"
|
||||
#include <stdio.h>
|
||||
#include "coap-engine.h"
|
||||
#include "dev/battery-sensor.h"
|
||||
|
||||
static void res_get_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
static void res_get_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
|
||||
/* A simple getter example. Returns the reading from light sensor with a simple etag */
|
||||
RESOURCE(res_battery,
|
||||
|
@ -55,27 +56,27 @@ RESOURCE(res_battery,
|
|||
NULL);
|
||||
|
||||
static void
|
||||
res_get_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
res_get_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
{
|
||||
int battery = battery_sensor.value(0);
|
||||
|
||||
unsigned int accept = -1;
|
||||
REST.get_header_accept(request, &accept);
|
||||
coap_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", battery);
|
||||
if(accept == -1 || accept == TEXT_PLAIN) {
|
||||
coap_set_header_content_format(response, TEXT_PLAIN);
|
||||
snprintf((char *)buffer, COAP_MAX_CHUNK_SIZE, "%d", battery);
|
||||
|
||||
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, "{'battery':%d}", battery);
|
||||
coap_set_payload(response, (uint8_t *)buffer, strlen((char *)buffer));
|
||||
} else if(accept == APPLICATION_JSON) {
|
||||
coap_set_header_content_format(response, APPLICATION_JSON);
|
||||
snprintf((char *)buffer, COAP_MAX_CHUNK_SIZE, "{'battery':%d}", battery);
|
||||
|
||||
REST.set_response_payload(response, buffer, strlen((char *)buffer));
|
||||
coap_set_payload(response, buffer, strlen((char *)buffer));
|
||||
} else {
|
||||
REST.set_response_status(response, REST.status.NOT_ACCEPTABLE);
|
||||
coap_set_status_code(response, NOT_ACCEPTABLE_4_06);
|
||||
const char *msg = "Supporting content-types text/plain and application/json";
|
||||
REST.set_response_payload(response, msg, strlen(msg));
|
||||
coap_set_payload(response, msg, strlen(msg));
|
||||
}
|
||||
}
|
||||
#endif /* PLATFORM_HAS_BATTERY */
|
||||
|
|
|
@ -36,13 +36,14 @@
|
|||
* Matthias Kovatsch <kovatsch@inf.ethz.ch>
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "rest-engine.h"
|
||||
#include "coap-engine.h"
|
||||
|
||||
static void res_get_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
static void res_get_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
|
||||
/*
|
||||
* For data larger than REST_MAX_CHUNK_SIZE (e.g., when stored in flash) resources must be aware of the buffer limitation
|
||||
* For data larger than COAP_MAX_CHUNK_SIZE (e.g., when stored in flash) resources must be aware of the buffer limitation
|
||||
* and split their responses by themselves. To transfer the complete resource through a TCP stream or CoAP's blockwise transfer,
|
||||
* the byte offset where to continue is provided to the handler as int32_t pointer.
|
||||
* These chunk-wise resources must set the offset value to its new position or -1 of the end is reached.
|
||||
|
@ -58,17 +59,17 @@ RESOURCE(res_chunks,
|
|||
#define CHUNKS_TOTAL 2050
|
||||
|
||||
static void
|
||||
res_get_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
res_get_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
{
|
||||
int32_t strpos = 0;
|
||||
|
||||
/* Check the offset for boundaries of the resource data. */
|
||||
if(*offset >= CHUNKS_TOTAL) {
|
||||
REST.set_response_status(response, REST.status.BAD_OPTION);
|
||||
coap_set_status_code(response, BAD_OPTION_4_02);
|
||||
/* A block error message should not exceed the minimum block size (16). */
|
||||
|
||||
const char *error_msg = "BlockOutOfScope";
|
||||
REST.set_response_payload(response, error_msg, strlen(error_msg));
|
||||
coap_set_payload(response, error_msg, strlen(error_msg));
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -85,7 +86,7 @@ res_get_handler(void *request, void *response, uint8_t *buffer, uint16_t preferr
|
|||
if(*offset + (int32_t)strpos > CHUNKS_TOTAL) {
|
||||
strpos = CHUNKS_TOTAL - *offset;
|
||||
}
|
||||
REST.set_response_payload(response, buffer, strpos);
|
||||
coap_set_payload(response, buffer, strpos);
|
||||
|
||||
/* IMPORTANT for chunk-wise resources: Signal chunk awareness to REST engine. */
|
||||
*offset += strpos;
|
||||
|
|
|
@ -36,8 +36,9 @@
|
|||
* Matthias Kovatsch <kovatsch@inf.ethz.ch>
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "rest-engine.h"
|
||||
#include "coap-engine.h"
|
||||
#include "coap.h"
|
||||
|
||||
#define DEBUG 0
|
||||
|
@ -52,7 +53,7 @@
|
|||
#define PRINTLLADDR(addr)
|
||||
#endif
|
||||
|
||||
static void res_get_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
static void res_get_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
static void res_event_handler(void);
|
||||
|
||||
/*
|
||||
|
@ -74,10 +75,10 @@ EVENT_RESOURCE(res_event,
|
|||
static int32_t event_counter = 0;
|
||||
|
||||
static void
|
||||
res_get_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
res_get_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
{
|
||||
REST.set_header_content_type(response, REST.type.TEXT_PLAIN);
|
||||
REST.set_response_payload(response, buffer, snprintf((char *)buffer, preferred_size, "EVENT %lu", (unsigned long) event_counter));
|
||||
coap_set_header_content_format(response, TEXT_PLAIN);
|
||||
coap_set_payload(response, buffer, snprintf((char *)buffer, preferred_size, "EVENT %lu", (unsigned long) event_counter));
|
||||
|
||||
/* A post_handler that handles subscriptions/observing will be called for periodic resources by the framework. */
|
||||
}
|
||||
|
@ -96,6 +97,6 @@ res_event_handler(void)
|
|||
PRINTF("TICK %u for /%s\n", event_counter, res_event.url);
|
||||
|
||||
/* Notify the registered observers which will trigger the res_get_handler to create the response. */
|
||||
REST.notify_subscribers(&res_event);
|
||||
coap_notify_observers(&res_event);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -38,9 +38,9 @@
|
|||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "rest-engine.h"
|
||||
#include "coap-engine.h"
|
||||
|
||||
static void res_get_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
static void res_get_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
|
||||
/*
|
||||
* A handler function named [resource name]_handler must be implemented for each RESOURCE.
|
||||
|
@ -56,7 +56,7 @@ RESOURCE(res_hello,
|
|||
NULL);
|
||||
|
||||
static void
|
||||
res_get_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
res_get_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
{
|
||||
const char *len = NULL;
|
||||
/* Some data that has the length up to REST_MAX_CHUNK_SIZE. For more, see the chunk resource. */
|
||||
|
@ -64,7 +64,7 @@ res_get_handler(void *request, void *response, uint8_t *buffer, uint16_t preferr
|
|||
int length = 12; /* |<-------->| */
|
||||
|
||||
/* The query string can be retrieved by rest_get_query() or parsed for its key-value pairs. */
|
||||
if(REST.get_query_variable(request, "len", &len)) {
|
||||
if(coap_get_query_variable(request, "len", &len)) {
|
||||
length = atoi(len);
|
||||
if(length < 0) {
|
||||
length = 0;
|
||||
|
@ -75,7 +75,7 @@ res_get_handler(void *request, void *response, uint8_t *buffer, uint16_t preferr
|
|||
memcpy(buffer, message, length);
|
||||
} else {
|
||||
memcpy(buffer, message, length);
|
||||
} REST.set_header_content_type(response, REST.type.TEXT_PLAIN); /* text/plain is the default, hence this option could be omitted. */
|
||||
REST.set_header_etag(response, (uint8_t *)&length, 1);
|
||||
REST.set_response_payload(response, buffer, length);
|
||||
} coap_set_header_content_format(response, TEXT_PLAIN); /* text/plain is the default, hence this option could be omitted. */
|
||||
coap_set_header_etag(response, (uint8_t *)&length, 1);
|
||||
coap_set_payload(response, buffer, length);
|
||||
}
|
||||
|
|
|
@ -39,9 +39,8 @@
|
|||
#include "contiki.h"
|
||||
|
||||
#if PLATFORM_HAS_LEDS
|
||||
|
||||
#include <string.h>
|
||||
#include "rest-engine.h"
|
||||
#include "coap-engine.h"
|
||||
#include "dev/leds.h"
|
||||
|
||||
#define DEBUG 0
|
||||
|
@ -56,9 +55,9 @@
|
|||
#define PRINTLLADDR(addr)
|
||||
#endif
|
||||
|
||||
static void res_post_put_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
static void res_post_put_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
|
||||
/*A simple actuator example, depending on the color query parameter and post variable mode, corresponding led is activated or deactivated*/
|
||||
/* A simple actuator example, depending on the color query parameter and post variable mode, corresponding led is activated or deactivated */
|
||||
RESOURCE(res_leds,
|
||||
"title=\"LEDs: ?color=r|g|b, POST/PUT mode=on|off\";rt=\"Control\"",
|
||||
NULL,
|
||||
|
@ -67,7 +66,7 @@ RESOURCE(res_leds,
|
|||
NULL);
|
||||
|
||||
static void
|
||||
res_post_put_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
res_post_put_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
{
|
||||
size_t len = 0;
|
||||
const char *color = NULL;
|
||||
|
@ -75,7 +74,7 @@ res_post_put_handler(void *request, void *response, uint8_t *buffer, uint16_t pr
|
|||
uint8_t led = 0;
|
||||
int success = 1;
|
||||
|
||||
if((len = REST.get_query_variable(request, "color", &color))) {
|
||||
if((len = coap_get_query_variable(request, "color", &color))) {
|
||||
PRINTF("color %.*s\n", len, color);
|
||||
|
||||
if(strncmp(color, "r", len) == 0) {
|
||||
|
@ -89,7 +88,7 @@ res_post_put_handler(void *request, void *response, uint8_t *buffer, uint16_t pr
|
|||
}
|
||||
} else {
|
||||
success = 0;
|
||||
} if(success && (len = REST.get_post_variable(request, "mode", &mode))) {
|
||||
} if(success && (len = coap_get_post_variable(request, "mode", &mode))) {
|
||||
PRINTF("mode %s\n", mode);
|
||||
|
||||
if(strncmp(mode, "on", len) == 0) {
|
||||
|
@ -102,7 +101,7 @@ res_post_put_handler(void *request, void *response, uint8_t *buffer, uint16_t pr
|
|||
} else {
|
||||
success = 0;
|
||||
} if(!success) {
|
||||
REST.set_response_status(response, REST.status.BAD_REQUEST);
|
||||
coap_set_status_code(response, BAD_REQUEST_4_00);
|
||||
}
|
||||
}
|
||||
#endif /* PLATFORM_HAS_LEDS */
|
||||
|
|
|
@ -40,11 +40,12 @@
|
|||
|
||||
#if PLATFORM_HAS_LIGHT
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "rest-engine.h"
|
||||
#include "dev/light-sensor.h"
|
||||
#include "coap-engine.h"
|
||||
//#include "dev/light-sensor.h"
|
||||
|
||||
static void res_get_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
static void res_get_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
|
||||
/* A simple getter example. Returns the reading from light sensor with a simple etag */
|
||||
RESOURCE(res_light,
|
||||
|
@ -55,33 +56,33 @@ RESOURCE(res_light,
|
|||
NULL);
|
||||
|
||||
static void
|
||||
res_get_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
res_get_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
{
|
||||
uint16_t light_photosynthetic = light_sensor.value(LIGHT_SENSOR_PHOTOSYNTHETIC);
|
||||
uint16_t light_solar = light_sensor.value(LIGHT_SENSOR_TOTAL_SOLAR);
|
||||
|
||||
unsigned int accept = -1;
|
||||
REST.get_header_accept(request, &accept);
|
||||
coap_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, "%u;%u", light_photosynthetic, light_solar);
|
||||
if(accept == -1 || accept == TEXT_PLAIN) {
|
||||
coap_set_header_content_format(response, TEXT_PLAIN);
|
||||
snprintf((char *)buffer, COAP_MAX_CHUNK_SIZE, "%u;%u", light_photosynthetic, light_solar);
|
||||
|
||||
REST.set_response_payload(response, (uint8_t *)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, "<light photosynthetic=\"%u\" solar=\"%u\"/>", light_photosynthetic, light_solar);
|
||||
coap_set_payload(response, (uint8_t *)buffer, strlen((char *)buffer));
|
||||
} else if(accept == APPLICATION_XML) {
|
||||
coap_set_header_content_format(response, APPLICATION_XML);
|
||||
snprintf((char *)buffer, COAP_MAX_CHUNK_SIZE, "<light photosynthetic=\"%u\" solar=\"%u\"/>", light_photosynthetic, light_solar);
|
||||
|
||||
REST.set_response_payload(response, 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, "{'light':{'photosynthetic':%u,'solar':%u}}", light_photosynthetic, light_solar);
|
||||
coap_set_payload(response, buffer, strlen((char *)buffer));
|
||||
} else if(accept == APPLICATION_JSON) {
|
||||
coap_set_header_content_format(response, APPLICATION_JSON);
|
||||
snprintf((char *)buffer, COAP_MAX_CHUNK_SIZE, "{'light':{'photosynthetic':%u,'solar':%u}}", light_photosynthetic, light_solar);
|
||||
|
||||
REST.set_response_payload(response, buffer, strlen((char *)buffer));
|
||||
coap_set_payload(response, buffer, strlen((char *)buffer));
|
||||
} else {
|
||||
REST.set_response_status(response, REST.status.NOT_ACCEPTABLE);
|
||||
coap_set_status_code(response, NOT_ACCEPTABLE_4_06);
|
||||
const char *msg = "Supporting content-types text/plain, application/xml, and application/json";
|
||||
REST.set_response_payload(response, msg, strlen(msg));
|
||||
coap_set_payload(response, msg, strlen(msg));
|
||||
}
|
||||
}
|
||||
#endif /* PLATFORM_HAS_LIGHT */
|
||||
|
|
|
@ -36,8 +36,9 @@
|
|||
* Matthias Kovatsch <kovatsch@inf.ethz.ch>
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "rest-engine.h"
|
||||
#include "coap-engine.h"
|
||||
#include "coap.h"
|
||||
|
||||
#define DEBUG 0
|
||||
|
@ -52,7 +53,7 @@
|
|||
#define PRINTLLADDR(addr)
|
||||
#endif
|
||||
|
||||
static void res_any_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
static void res_any_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
|
||||
/* This resource mirrors the incoming request. It shows how to access the options and how to set them for the response. */
|
||||
RESOURCE(res_mirror,
|
||||
|
@ -63,7 +64,7 @@ RESOURCE(res_mirror,
|
|||
res_any_handler);
|
||||
|
||||
static void
|
||||
res_any_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
res_any_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
{
|
||||
/* The ETag and Token is copied to the header. */
|
||||
uint8_t opaque[] = { 0x0A, 0xBC, 0xDE };
|
||||
|
@ -89,28 +90,28 @@ res_any_handler(void *request, void *response, uint8_t *buffer, uint16_t preferr
|
|||
/* snprintf() counts the terminating '\0' to the size parameter.
|
||||
* The additional byte is taken care of by allocating REST_MAX_CHUNK_SIZE+1 bytes in the REST framework.
|
||||
* Add +1 to fill the complete buffer, as the payload does not need a terminating '\0'. */
|
||||
if(REST.get_header_content_type(request, &content_format)) {
|
||||
if(coap_get_header_content_format(request, &content_format)) {
|
||||
strpos += snprintf((char *)buffer, REST_MAX_CHUNK_SIZE + 1, "CF %u\n", content_format);
|
||||
}
|
||||
if(strpos <= REST_MAX_CHUNK_SIZE && (len = REST.get_header_accept(request, &content_format))) {
|
||||
if(strpos <= REST_MAX_CHUNK_SIZE && (len = coap_get_header_accept(request, &content_format))) {
|
||||
strpos += snprintf((char *)buffer + strpos, REST_MAX_CHUNK_SIZE - strpos + 1, "Ac %u\n", content_format);
|
||||
/* Some getters such as for ETag or Location are omitted, as these options should not appear in a request.
|
||||
* Max-Age might appear in HTTP requests or used for special purposes in CoAP. */
|
||||
}
|
||||
if(strpos <= REST_MAX_CHUNK_SIZE && REST.get_header_max_age(request, &longint)) {
|
||||
if(strpos <= REST_MAX_CHUNK_SIZE && coap_get_header_max_age(request, &longint)) {
|
||||
strpos += snprintf((char *)buffer + strpos, REST_MAX_CHUNK_SIZE - strpos + 1, "MA %lu\n", (unsigned long) longint);
|
||||
/* For HTTP this is the Length option, for CoAP it is the Size option. */
|
||||
}
|
||||
if(strpos <= REST_MAX_CHUNK_SIZE && REST.get_header_length(request, &longint)) {
|
||||
if(strpos <= REST_MAX_CHUNK_SIZE && coap_get_header_size1(request, &longint)) {
|
||||
strpos += snprintf((char *)buffer + strpos, REST_MAX_CHUNK_SIZE - strpos + 1, "SZ %lu\n", (unsigned long) longint);
|
||||
}
|
||||
if(strpos <= REST_MAX_CHUNK_SIZE && (len = REST.get_header_host(request, &str))) {
|
||||
if(strpos <= REST_MAX_CHUNK_SIZE && (len = coap_get_header_uri_host(request, &str))) {
|
||||
strpos += snprintf((char *)buffer + strpos, REST_MAX_CHUNK_SIZE - strpos + 1, "UH %.*s\n", len, str);
|
||||
}
|
||||
if(strpos <= REST_MAX_CHUNK_SIZE && (len = REST.get_url(request, &str))) {
|
||||
if(strpos <= REST_MAX_CHUNK_SIZE && (len = coap_get_header_uri_path(request, &str))) {
|
||||
strpos += snprintf((char *)buffer + strpos, REST_MAX_CHUNK_SIZE - strpos + 1, "UP %.*s\n", len, str);
|
||||
}
|
||||
if(strpos <= REST_MAX_CHUNK_SIZE && (len = REST.get_query(request, &str))) {
|
||||
if(strpos <= REST_MAX_CHUNK_SIZE && (len = coap_get_header_uri_query(request, &str))) {
|
||||
strpos += snprintf((char *)buffer + strpos, REST_MAX_CHUNK_SIZE - strpos + 1, "UQ %.*s\n", len, str);
|
||||
/* Undefined request options for debugging: actions not required for normal RESTful Web service. */
|
||||
}
|
||||
|
@ -132,10 +133,10 @@ res_any_handler(void *request, void *response, uint8_t *buffer, uint16_t preferr
|
|||
strpos += snprintf((char *)buffer + strpos, REST_MAX_CHUNK_SIZE - strpos + 1, "\n");
|
||||
}
|
||||
|
||||
if(strpos <= REST_MAX_CHUNK_SIZE && IS_OPTION(coap_pkt, COAP_OPTION_OBSERVE)) {
|
||||
if(strpos <= REST_MAX_CHUNK_SIZE && coap_is_option(coap_pkt, COAP_OPTION_OBSERVE)) {
|
||||
strpos += snprintf((char *)buffer + strpos, REST_MAX_CHUNK_SIZE - strpos + 1, "Ob %lu\n", (unsigned long) coap_pkt->observe);
|
||||
}
|
||||
if(strpos <= REST_MAX_CHUNK_SIZE && IS_OPTION(coap_pkt, COAP_OPTION_ETAG)) {
|
||||
if(strpos <= REST_MAX_CHUNK_SIZE && coap_is_option(coap_pkt, COAP_OPTION_ETAG)) {
|
||||
strpos += snprintf((char *)buffer + strpos, REST_MAX_CHUNK_SIZE - strpos + 1, "ET 0x");
|
||||
int index = 0;
|
||||
for(index = 0; index < coap_pkt->etag_len; ++index) {
|
||||
|
@ -149,22 +150,22 @@ res_any_handler(void *request, void *response, uint8_t *buffer, uint16_t preferr
|
|||
if(strpos <= REST_MAX_CHUNK_SIZE && coap_get_header_block1(request, &block_num, &block_more, &block_size, NULL)) {
|
||||
strpos += snprintf((char *)buffer + strpos, REST_MAX_CHUNK_SIZE - strpos + 1, "B1 %lu%s (%u)\n", (unsigned long) block_num, block_more ? "+" : "", block_size);
|
||||
}
|
||||
if(strpos <= REST_MAX_CHUNK_SIZE && (len = REST.get_request_payload(request, &bytes))) {
|
||||
if(strpos <= REST_MAX_CHUNK_SIZE && (len = coap_get_payload(request, &bytes))) {
|
||||
strpos += snprintf((char *)buffer + strpos, REST_MAX_CHUNK_SIZE - strpos + 1, "%.*s", len, bytes);
|
||||
}
|
||||
if(strpos >= REST_MAX_CHUNK_SIZE) {
|
||||
buffer[REST_MAX_CHUNK_SIZE - 1] = 0xBB; /* '»' to indicate truncation */
|
||||
}
|
||||
REST.set_response_payload(response, buffer, strpos);
|
||||
coap_set_payload(response, buffer, strpos);
|
||||
|
||||
PRINTF("/mirror options received: %s\n", buffer);
|
||||
|
||||
/* Set dummy header options for response. Like getters, some setters are not implemented for HTTP and have no effect. */
|
||||
REST.set_header_content_type(response, REST.type.TEXT_PLAIN);
|
||||
REST.set_header_max_age(response, 17); /* For HTTP, browsers will not re-request the page for 17 seconds. */
|
||||
REST.set_header_etag(response, opaque, 2);
|
||||
REST.set_header_location(response, location); /* Initial slash is omitted by framework */
|
||||
REST.set_header_length(response, strpos); /* For HTTP, browsers will not re-request the page for 10 seconds. CoAP action depends on the client. */
|
||||
coap_set_header_content_format(response, TEXT_PLAIN);
|
||||
coap_set_header_max_age(response, 17); /* For HTTP, browsers will not re-request the page for 17 seconds. */
|
||||
coap_set_header_etag(response, opaque, 2);
|
||||
coap_set_header_location_path(response, location); /* Initial slash is omitted by framework */
|
||||
coap_set_header_size1(response, strpos); /* For HTTP, browsers will not re-request the page for 10 s. CoAP action depends on the client. */
|
||||
|
||||
/* CoAP-specific example: actions not required for normal RESTful Web service. */
|
||||
coap_set_header_uri_host(response, "tiki");
|
||||
|
|
|
@ -36,11 +36,12 @@
|
|||
* Matthias Kovatsch <kovatsch@inf.ethz.ch>
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "rest-engine.h"
|
||||
#include "coap-engine.h"
|
||||
#include "coap.h"
|
||||
|
||||
static void res_get_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
static void res_get_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
static void res_periodic_handler(void);
|
||||
|
||||
PERIODIC_RESOURCE(res_push,
|
||||
|
@ -58,18 +59,18 @@ PERIODIC_RESOURCE(res_push,
|
|||
static int32_t event_counter = 0;
|
||||
|
||||
static void
|
||||
res_get_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
res_get_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
{
|
||||
/*
|
||||
* For minimal complexity, request query and options should be ignored for GET on observable resources.
|
||||
* Otherwise the requests must be stored with the observer list and passed by REST.notify_subscribers().
|
||||
* Otherwise the requests must be stored with the observer list and passed by coap_notify_subscribers().
|
||||
* This would be a TODO in the corresponding files in contiki/apps/erbium/!
|
||||
*/
|
||||
REST.set_header_content_type(response, REST.type.TEXT_PLAIN);
|
||||
REST.set_header_max_age(response, res_push.periodic->period / CLOCK_SECOND);
|
||||
REST.set_response_payload(response, buffer, snprintf((char *)buffer, preferred_size, "VERY LONG EVENT %lu", (unsigned long) event_counter));
|
||||
coap_set_header_content_format(response, TEXT_PLAIN);
|
||||
coap_set_header_max_age(response, res_push.periodic->period / CLOCK_SECOND);
|
||||
coap_set_payload(response, buffer, snprintf((char *)buffer, preferred_size, "VERY LONG EVENT %lu", (unsigned long) event_counter));
|
||||
|
||||
/* The REST.subscription_handler() will be called for observable resources by the REST framework. */
|
||||
/* The coap_subscription_handler() will be called for observable resources by the REST framework. */
|
||||
}
|
||||
/*
|
||||
* Additionally, a handler function named [resource name]_handler must be implemented for each PERIODIC_RESOURCE.
|
||||
|
@ -84,6 +85,6 @@ res_periodic_handler()
|
|||
/* Usually a condition is defined under with subscribers are notified, e.g., large enough delta in sensor reading. */
|
||||
if(1) {
|
||||
/* Notify the registered observers which will trigger the res_get_handler to create the response. */
|
||||
REST.notify_subscribers(&res_push);
|
||||
coap_notify_observers(&res_push);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -39,12 +39,12 @@
|
|||
#include "contiki.h"
|
||||
|
||||
#if PLATFORM_HAS_RADIO
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "rest-engine.h"
|
||||
#include "coap-engine.h"
|
||||
#include "net/netstack.h"
|
||||
|
||||
static void res_get_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
static void res_get_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
|
||||
/* A simple getter example. Returns the reading of the rssi/lqi from radio sensor */
|
||||
RESOURCE(res_radio,
|
||||
|
@ -55,7 +55,7 @@ RESOURCE(res_radio,
|
|||
NULL);
|
||||
|
||||
static void
|
||||
res_get_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
res_get_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
{
|
||||
size_t len = 0;
|
||||
const char *p = NULL;
|
||||
|
@ -64,9 +64,9 @@ res_get_handler(void *request, void *response, uint8_t *buffer, uint16_t preferr
|
|||
int success = 0;
|
||||
unsigned int accept = -1;
|
||||
|
||||
REST.get_header_accept(request, &accept);
|
||||
coap_get_header_accept(request, &accept);
|
||||
|
||||
if((len = REST.get_query_variable(request, "p", &p))) {
|
||||
if((len = coap_get_query_variable(request, "p", &p))) {
|
||||
if(strncmp(p, "rssi", len) == 0) {
|
||||
if(NETSTACK_RADIO.get_value(RADIO_PARAM_RSSI, &value) ==
|
||||
RADIO_RESULT_OK) {
|
||||
|
@ -77,23 +77,23 @@ res_get_handler(void *request, void *response, uint8_t *buffer, uint16_t preferr
|
|||
}
|
||||
|
||||
if(success) {
|
||||
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", rssi);
|
||||
if(accept == -1 || accept == TEXT_PLAIN) {
|
||||
coap_set_header_content_format(response, TEXT_PLAIN);
|
||||
snprintf((char *)buffer, COAP_MAX_CHUNK_SIZE, "%d", 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);
|
||||
coap_set_payload(response, (uint8_t *)buffer, strlen((char *)buffer));
|
||||
} else if(accept == APPLICATION_JSON) {
|
||||
coap_set_header_content_format(response, APPLICATION_JSON);
|
||||
|
||||
snprintf((char *)buffer, REST_MAX_CHUNK_SIZE, "{'rssi':%d}", rssi);
|
||||
REST.set_response_payload(response, buffer, strlen((char *)buffer));
|
||||
snprintf((char *)buffer, COAP_MAX_CHUNK_SIZE, "{'rssi':%d}", rssi);
|
||||
coap_set_payload(response, buffer, strlen((char *)buffer));
|
||||
} else {
|
||||
REST.set_response_status(response, REST.status.NOT_ACCEPTABLE);
|
||||
coap_set_status_code(response, NOT_ACCEPTABLE_4_06);
|
||||
const char *msg = "Supporting content-types text/plain and application/json";
|
||||
REST.set_response_payload(response, msg, strlen(msg));
|
||||
coap_set_payload(response, msg, strlen(msg));
|
||||
}
|
||||
} else {
|
||||
REST.set_response_status(response, REST.status.BAD_REQUEST);
|
||||
coap_set_status_code(response, BAD_REQUEST_4_00);
|
||||
}
|
||||
}
|
||||
#endif /* PLATFORM_HAS_RADIO */
|
||||
|
|
|
@ -37,11 +37,12 @@
|
|||
*/
|
||||
|
||||
#include <string.h>
|
||||
#include "rest-engine.h"
|
||||
#include <stdio.h>
|
||||
#include "coap-engine.h"
|
||||
#include "coap-separate.h"
|
||||
#include "coap-transactions.h"
|
||||
|
||||
static void res_get_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
static void res_get_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
static void res_resume_handler(void);
|
||||
|
||||
SEPARATE_RESOURCE(res_separate,
|
||||
|
@ -68,7 +69,7 @@ static uint8_t separate_active = 0;
|
|||
static application_separate_store_t separate_store[COAP_MAX_OPEN_SEPARATE];
|
||||
|
||||
static void
|
||||
res_get_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
res_get_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
{
|
||||
/*
|
||||
* Example allows only one open separate response.
|
||||
|
@ -96,11 +97,11 @@ res_resume_handler()
|
|||
{
|
||||
if(separate_active) {
|
||||
coap_transaction_t *transaction = NULL;
|
||||
if((transaction = coap_new_transaction(separate_store->request_metadata.mid, &separate_store->request_metadata.addr, separate_store->request_metadata.port))) {
|
||||
if((transaction = coap_new_transaction(separate_store->request_metadata.mid, &separate_store->request_metadata.endpoint))) {
|
||||
coap_packet_t response[1]; /* This way the packet can be treated as pointer as usual. */
|
||||
|
||||
/* Restore the request information for the response. */
|
||||
coap_separate_resume(response, &separate_store->request_metadata, REST.status.OK);
|
||||
coap_separate_resume(response, &separate_store->request_metadata, CONTENT_2_05);
|
||||
|
||||
coap_set_payload(response, separate_store->buffer, strlen(separate_store->buffer));
|
||||
|
||||
|
|
|
@ -44,10 +44,10 @@
|
|||
#if PLATFORM_HAS_SHT11
|
||||
|
||||
#include <string.h>
|
||||
#include "rest-engine.h"
|
||||
#include "coap-engine.h"
|
||||
#include "dev/sht11/sht11-sensor.h"
|
||||
|
||||
static void res_get_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
static void res_get_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
|
||||
/* Get Method Example. Returns the reading from temperature and humidity sensors. */
|
||||
RESOURCE(res_sht11,
|
||||
|
@ -58,7 +58,7 @@ RESOURCE(res_sht11,
|
|||
NULL);
|
||||
|
||||
static void
|
||||
res_get_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
res_get_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
{
|
||||
/* Temperature in Celsius (t in 14 bits resolution at 3 Volts)
|
||||
* T = -39.60 + 0.01*t
|
||||
|
@ -70,27 +70,27 @@ res_get_handler(void *request, void *response, uint8_t *buffer, uint16_t preferr
|
|||
uint16_t rh = sht11_sensor.value(SHT11_SENSOR_HUMIDITY);
|
||||
|
||||
unsigned int accept = -1;
|
||||
REST.get_header_accept(request, &accept);
|
||||
coap_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, "%u;%u", temperature, rh);
|
||||
if(accept == -1 || accept == TEXT_PLAIN) {
|
||||
coap_set_header_content_format(response, TEXT_PLAIN);
|
||||
snprintf((char *)buffer, COAP_MAX_CHUNK_SIZE, "%u;%u", temperature, rh);
|
||||
|
||||
REST.set_response_payload(response, (uint8_t *)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, "<Temperature =\"%u\" Humidity=\"%u\"/>", temperature, rh);
|
||||
coap_set_payload(response, (uint8_t *)buffer, strlen((char *)buffer));
|
||||
} else if(accept == APPLICATION_XML) {
|
||||
coap_set_header_content_format(response, APPLICATION_XML);
|
||||
snprintf((char *)buffer, COAP_MAX_CHUNK_SIZE, "<Temperature =\"%u\" Humidity=\"%u\"/>", temperature, rh);
|
||||
|
||||
REST.set_response_payload(response, 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, "{'Sht11':{'Temperature':%u,'Humidity':%u}}", temperature, rh);
|
||||
coap_set_payload(response, buffer, strlen((char *)buffer));
|
||||
} else if(accept == APPLICATION_JSON) {
|
||||
coap_set_header_content_format(response, APPLICATION_JSON);
|
||||
snprintf((char *)buffer, COAP_MAX_CHUNK_SIZE, "{'Sht11':{'Temperature':%u,'Humidity':%u}}", temperature, rh);
|
||||
|
||||
REST.set_response_payload(response, buffer, strlen((char *)buffer));
|
||||
coap_set_payload(response, buffer, strlen((char *)buffer));
|
||||
} else {
|
||||
REST.set_response_status(response, REST.status.NOT_ACCEPTABLE);
|
||||
coap_set_status_code(response, NOT_ACCEPTABLE_4_06);
|
||||
const char *msg = "Supporting content-types text/plain, application/xml, and application/json";
|
||||
REST.set_response_payload(response, msg, strlen(msg));
|
||||
coap_set_payload(response, msg, strlen(msg));
|
||||
}
|
||||
}
|
||||
#endif /* PLATFORM_HAS_SHT11 */
|
||||
|
|
|
@ -36,14 +36,15 @@
|
|||
* Matthias Kovatsch <kovatsch@inf.ethz.ch>
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "rest-engine.h"
|
||||
#include "coap-engine.h"
|
||||
|
||||
static void res_get_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
static void res_get_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
|
||||
/*
|
||||
* Example for a resource that also handles all its sub-resources.
|
||||
* Use REST.get_url() to multiplex the handling of the request depending on the Uri-Path.
|
||||
* Use coap_get_url() to multiplex the handling of the request depending on the Uri-Path.
|
||||
*/
|
||||
PARENT_RESOURCE(res_sub,
|
||||
"title=\"Sub-resource demo\"",
|
||||
|
@ -53,17 +54,17 @@ PARENT_RESOURCE(res_sub,
|
|||
NULL);
|
||||
|
||||
static void
|
||||
res_get_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
res_get_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
{
|
||||
REST.set_header_content_type(response, REST.type.TEXT_PLAIN);
|
||||
coap_set_header_content_format(response, TEXT_PLAIN);
|
||||
|
||||
const char *uri_path = NULL;
|
||||
int len = REST.get_url(request, &uri_path);
|
||||
int len = coap_get_header_uri_path(request, &uri_path);
|
||||
int base_len = strlen(res_sub.url);
|
||||
|
||||
if(len == base_len) {
|
||||
snprintf((char *)buffer, REST_MAX_CHUNK_SIZE, "Request any sub-resource of /%s", res_sub.url);
|
||||
snprintf((char *)buffer, COAP_MAX_CHUNK_SIZE, "Request any sub-resource of /%s", res_sub.url);
|
||||
} else {
|
||||
snprintf((char *)buffer, REST_MAX_CHUNK_SIZE, ".%.*s", len - base_len, uri_path + base_len);
|
||||
} REST.set_response_payload(response, buffer, strlen((char *)buffer));
|
||||
snprintf((char *)buffer, COAP_MAX_CHUNK_SIZE, ".%.*s", len - base_len, uri_path + base_len);
|
||||
} coap_set_payload(response, buffer, strlen((char *)buffer));
|
||||
}
|
||||
|
|
|
@ -44,11 +44,12 @@
|
|||
|
||||
#include <limits.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "rest-engine.h"
|
||||
#include "coap-engine.h"
|
||||
#include "dev/temperature-sensor.h"
|
||||
|
||||
static void res_get_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
static void res_get_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
static void res_periodic_handler(void);
|
||||
|
||||
#define MAX_AGE 60
|
||||
|
@ -69,43 +70,43 @@ PERIODIC_RESOURCE(res_temperature,
|
|||
res_periodic_handler);
|
||||
|
||||
static void
|
||||
res_get_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
res_get_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
{
|
||||
/*
|
||||
* For minimal complexity, request query and options should be ignored for GET on observable resources.
|
||||
* Otherwise the requests must be stored with the observer list and passed by REST.notify_subscribers().
|
||||
* Otherwise the requests must be stored with the observer list and passed by coap_notify_observers().
|
||||
* This would be a TODO in the corresponding files in contiki/apps/erbium/!
|
||||
*/
|
||||
|
||||
int temperature = temperature_sensor.value(0);
|
||||
|
||||
unsigned int accept = -1;
|
||||
REST.get_header_accept(request, &accept);
|
||||
coap_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", temperature);
|
||||
if(accept == -1 || accept == TEXT_PLAIN) {
|
||||
coap_set_header_content_format(response, TEXT_PLAIN);
|
||||
snprintf((char *)buffer, COAP_MAX_CHUNK_SIZE, "%d", temperature);
|
||||
|
||||
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, "{'temperature':%d}", temperature);
|
||||
coap_set_payload(response, (uint8_t *)buffer, strlen((char *)buffer));
|
||||
} else if(accept == APPLICATION_JSON) {
|
||||
coap_set_header_content_format(response, APPLICATION_JSON);
|
||||
snprintf((char *)buffer, COAP_MAX_CHUNK_SIZE, "{'temperature':%d}", temperature);
|
||||
|
||||
REST.set_response_payload(response, buffer, strlen((char *)buffer));
|
||||
coap_set_payload(response, buffer, strlen((char *)buffer));
|
||||
} else {
|
||||
REST.set_response_status(response, REST.status.NOT_ACCEPTABLE);
|
||||
coap_set_status_code(response, NOT_ACCEPTABLE_4_06);
|
||||
const char *msg = "Supporting content-types text/plain and application/json";
|
||||
REST.set_response_payload(response, msg, strlen(msg));
|
||||
coap_set_payload(response, msg, strlen(msg));
|
||||
}
|
||||
|
||||
REST.set_header_max_age(response, MAX_AGE);
|
||||
coap_set_header_max_age(response, MAX_AGE);
|
||||
|
||||
/* The REST.subscription_handler() will be called for observable resources by the REST framework. */
|
||||
/* The coap_subscription_handler() will be called for observable resources by the coap_framework. */
|
||||
}
|
||||
|
||||
/*
|
||||
* Additionally, a handler function named [resource name]_handler must be implemented for each PERIODIC_RESOURCE.
|
||||
* It will be called by the REST manager process with the defined period.
|
||||
* It will be called by the coap_manager process with the defined period.
|
||||
*/
|
||||
static void
|
||||
res_periodic_handler()
|
||||
|
@ -119,7 +120,7 @@ res_periodic_handler()
|
|||
interval_counter = 0;
|
||||
temperature_old = temperature;
|
||||
/* Notify the registered observers which will trigger the res_get_handler to create the response. */
|
||||
REST.notify_subscribers(&res_temperature);
|
||||
coap_notify_observers(&res_temperature);
|
||||
}
|
||||
}
|
||||
#endif /* PLATFORM_HAS_TEMPERATURE */
|
||||
|
|
|
@ -42,10 +42,10 @@
|
|||
|
||||
#include <string.h>
|
||||
#include "contiki.h"
|
||||
#include "rest-engine.h"
|
||||
#include "coap-engine.h"
|
||||
#include "dev/leds.h"
|
||||
|
||||
static void res_post_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
static void res_post_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
|
||||
/* A simple actuator example. Toggles the red led */
|
||||
RESOURCE(res_toggle,
|
||||
|
@ -56,7 +56,7 @@ RESOURCE(res_toggle,
|
|||
NULL);
|
||||
|
||||
static void
|
||||
res_post_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
res_post_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
{
|
||||
leds_toggle(LEDS_RED);
|
||||
}
|
||||
|
|
|
@ -39,7 +39,7 @@
|
|||
/*---------------------------------------------------------------------------*/
|
||||
#include "contiki.h"
|
||||
#include "contiki-net.h"
|
||||
#include "rest-engine.h"
|
||||
#include "coap-engine.h"
|
||||
#include "board-peripherals.h"
|
||||
#include "lib/sensors.h"
|
||||
#include "lib/list.h"
|
||||
|
|
|
@ -37,7 +37,7 @@
|
|||
/*---------------------------------------------------------------------------*/
|
||||
#include "contiki.h"
|
||||
#include "contiki-net.h"
|
||||
#include "rest-engine.h"
|
||||
#include "coap-engine.h"
|
||||
#include "board-peripherals.h"
|
||||
#include "rf-core/rf-ble.h"
|
||||
#include "cc26xx-web-demo.h"
|
||||
|
@ -47,48 +47,48 @@
|
|||
#include <string.h>
|
||||
/*---------------------------------------------------------------------------*/
|
||||
/* Common resources */
|
||||
extern resource_t res_leds;
|
||||
extern coap_resource_t res_leds;
|
||||
|
||||
extern resource_t res_batmon_temp;
|
||||
extern resource_t res_batmon_volt;
|
||||
extern coap_resource_t res_batmon_temp;
|
||||
extern coap_resource_t res_batmon_volt;
|
||||
|
||||
extern resource_t res_device_sw;
|
||||
extern resource_t res_device_hw;
|
||||
extern resource_t res_device_uptime;
|
||||
extern resource_t res_device_cfg_reset;
|
||||
extern coap_resource_t res_device_sw;
|
||||
extern coap_resource_t res_device_hw;
|
||||
extern coap_resource_t res_device_uptime;
|
||||
extern coap_resource_t res_device_cfg_reset;
|
||||
|
||||
extern resource_t res_parent_rssi;
|
||||
extern resource_t res_parent_ip;
|
||||
extern coap_resource_t res_parent_rssi;
|
||||
extern coap_resource_t res_parent_ip;
|
||||
|
||||
#if RF_BLE_ENABLED
|
||||
extern resource_t res_ble_advd;
|
||||
extern coap_resource_t res_ble_advd;
|
||||
#endif
|
||||
|
||||
extern resource_t res_toggle_red;
|
||||
extern resource_t res_toggle_green;
|
||||
extern coap_resource_t res_toggle_red;
|
||||
extern coap_resource_t res_toggle_green;
|
||||
|
||||
/* Board-specific resources */
|
||||
#if BOARD_SENSORTAG
|
||||
extern resource_t res_bmp280_temp;
|
||||
extern resource_t res_bmp280_press;
|
||||
extern resource_t res_tmp007_amb;
|
||||
extern resource_t res_tmp007_obj;
|
||||
extern resource_t res_hdc1000_temp;
|
||||
extern resource_t res_hdc1000_hum;
|
||||
extern resource_t res_opt3001_light;
|
||||
extern resource_t res_mpu_acc_x;
|
||||
extern resource_t res_mpu_acc_y;
|
||||
extern resource_t res_mpu_acc_z;
|
||||
extern resource_t res_mpu_gyro_x;
|
||||
extern resource_t res_mpu_gyro_y;
|
||||
extern resource_t res_mpu_gyro_z;
|
||||
extern coap_resource_t res_bmp280_temp;
|
||||
extern coap_resource_t res_bmp280_press;
|
||||
extern coap_resource_t res_tmp007_amb;
|
||||
extern coap_resource_t res_tmp007_obj;
|
||||
extern coap_resource_t res_hdc1000_temp;
|
||||
extern coap_resource_t res_hdc1000_hum;
|
||||
extern coap_resource_t res_opt3001_light;
|
||||
extern coap_resource_t res_mpu_acc_x;
|
||||
extern coap_resource_t res_mpu_acc_y;
|
||||
extern coap_resource_t res_mpu_acc_z;
|
||||
extern coap_resource_t res_mpu_gyro_x;
|
||||
extern coap_resource_t res_mpu_gyro_y;
|
||||
extern coap_resource_t res_mpu_gyro_z;
|
||||
#else
|
||||
extern resource_t res_toggle_orange;
|
||||
extern resource_t res_toggle_yellow;
|
||||
extern coap_resource_t res_toggle_orange;
|
||||
extern coap_resource_t res_toggle_yellow;
|
||||
#endif
|
||||
|
||||
#if CC26XX_WEB_DEMO_ADC_DEMO
|
||||
extern resource_t res_adc_dio23;
|
||||
extern coap_resource_t res_adc_dio23;
|
||||
#endif
|
||||
/*---------------------------------------------------------------------------*/
|
||||
const char *coap_server_not_found_msg = "Resource not found";
|
||||
|
@ -101,27 +101,27 @@ static void
|
|||
start_board_resources(void)
|
||||
{
|
||||
|
||||
rest_activate_resource(&res_toggle_green, "lt/g");
|
||||
rest_activate_resource(&res_toggle_red, "lt/r");
|
||||
rest_activate_resource(&res_leds, "lt");
|
||||
coap_activate_resource(&res_toggle_green, "lt/g");
|
||||
coap_activate_resource(&res_toggle_red, "lt/r");
|
||||
coap_activate_resource(&res_leds, "lt");
|
||||
|
||||
#if BOARD_SENSORTAG
|
||||
rest_activate_resource(&res_bmp280_temp, "sen/bar/temp");
|
||||
rest_activate_resource(&res_bmp280_press, "sen/bar/pres");
|
||||
rest_activate_resource(&res_tmp007_amb, "sen/tmp/amb");
|
||||
rest_activate_resource(&res_tmp007_obj, "sen/tmp/obj");
|
||||
rest_activate_resource(&res_hdc1000_temp, "sen/hdc/t");
|
||||
rest_activate_resource(&res_hdc1000_hum, "sen/hdc/h");
|
||||
rest_activate_resource(&res_opt3001_light, "sen/opt/light");
|
||||
rest_activate_resource(&res_mpu_acc_x, "sen/mpu/acc/x");
|
||||
rest_activate_resource(&res_mpu_acc_y, "sen/mpu/acc/y");
|
||||
rest_activate_resource(&res_mpu_acc_z, "sen/mpu/acc/z");
|
||||
rest_activate_resource(&res_mpu_gyro_x, "sen/mpu/gyro/x");
|
||||
rest_activate_resource(&res_mpu_gyro_y, "sen/mpu/gyro/y");
|
||||
rest_activate_resource(&res_mpu_gyro_z, "sen/mpu/gyro/z");
|
||||
coap_activate_resource(&res_bmp280_temp, "sen/bar/temp");
|
||||
coap_activate_resource(&res_bmp280_press, "sen/bar/pres");
|
||||
coap_activate_resource(&res_tmp007_amb, "sen/tmp/amb");
|
||||
coap_activate_resource(&res_tmp007_obj, "sen/tmp/obj");
|
||||
coap_activate_resource(&res_hdc1000_temp, "sen/hdc/t");
|
||||
coap_activate_resource(&res_hdc1000_hum, "sen/hdc/h");
|
||||
coap_activate_resource(&res_opt3001_light, "sen/opt/light");
|
||||
coap_activate_resource(&res_mpu_acc_x, "sen/mpu/acc/x");
|
||||
coap_activate_resource(&res_mpu_acc_y, "sen/mpu/acc/y");
|
||||
coap_activate_resource(&res_mpu_acc_z, "sen/mpu/acc/z");
|
||||
coap_activate_resource(&res_mpu_gyro_x, "sen/mpu/gyro/x");
|
||||
coap_activate_resource(&res_mpu_gyro_y, "sen/mpu/gyro/y");
|
||||
coap_activate_resource(&res_mpu_gyro_z, "sen/mpu/gyro/z");
|
||||
#elif BOARD_SMARTRF06EB
|
||||
rest_activate_resource(&res_toggle_yellow, "lt/y");
|
||||
rest_activate_resource(&res_toggle_orange, "lt/o");
|
||||
coap_activate_resource(&res_toggle_yellow, "lt/y");
|
||||
coap_activate_resource(&res_toggle_orange, "lt/o");
|
||||
#endif
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
|
@ -134,25 +134,25 @@ PROCESS_THREAD(coap_server_process, ev, data)
|
|||
printf("CC26XX CoAP Server\n");
|
||||
|
||||
/* Initialize the REST engine. */
|
||||
rest_init_engine();
|
||||
coap_engine_init();
|
||||
|
||||
rest_activate_resource(&res_batmon_temp, "sen/batmon/temp");
|
||||
rest_activate_resource(&res_batmon_volt, "sen/batmon/voltage");
|
||||
coap_activate_resource(&res_batmon_temp, "sen/batmon/temp");
|
||||
coap_activate_resource(&res_batmon_volt, "sen/batmon/voltage");
|
||||
|
||||
#if CC26XX_WEB_DEMO_ADC_DEMO
|
||||
rest_activate_resource(&res_adc_dio23, "sen/adc/dio23");
|
||||
coap_activate_resource(&res_adc_dio23, "sen/adc/dio23");
|
||||
#endif
|
||||
|
||||
rest_activate_resource(&res_device_hw, "dev/mdl/hw");
|
||||
rest_activate_resource(&res_device_sw, "dev/mdl/sw");
|
||||
rest_activate_resource(&res_device_uptime, "dev/uptime");
|
||||
rest_activate_resource(&res_device_cfg_reset, "dev/cfg_reset");
|
||||
coap_activate_resource(&res_device_hw, "dev/mdl/hw");
|
||||
coap_activate_resource(&res_device_sw, "dev/mdl/sw");
|
||||
coap_activate_resource(&res_device_uptime, "dev/uptime");
|
||||
coap_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");
|
||||
coap_activate_resource(&res_parent_rssi, "net/parent/RSSI");
|
||||
coap_activate_resource(&res_parent_ip, "net/parent/IPv6");
|
||||
|
||||
#if RF_BLE_ENABLED
|
||||
rest_activate_resource(&res_ble_advd, "dev/ble_advd");
|
||||
coap_activate_resource(&res_ble_advd, "dev/ble_advd");
|
||||
#endif
|
||||
|
||||
start_board_resources();
|
||||
|
|
|
@ -36,7 +36,7 @@
|
|||
*/
|
||||
/*---------------------------------------------------------------------------*/
|
||||
#include "contiki.h"
|
||||
#include "rest-engine.h"
|
||||
#include "coap-engine.h"
|
||||
#include "coap.h"
|
||||
#include "rf-core/rf-ble.h"
|
||||
|
||||
|
@ -49,7 +49,8 @@ const char *forbidden_payload = "Name to advertise unspecified.\n"
|
|||
"Use name=<name> in the request";
|
||||
/*---------------------------------------------------------------------------*/
|
||||
static void
|
||||
res_ble_post_put_handler(void *request, void *response, uint8_t *buffer,
|
||||
res_ble_post_put_handler(coap_packet_t *request, coap_packet_t *response,
|
||||
uint8_t *buffer,
|
||||
uint16_t preferred_size, int32_t *offset)
|
||||
{
|
||||
size_t len = 0;
|
||||
|
@ -60,7 +61,7 @@ res_ble_post_put_handler(void *request, void *response, uint8_t *buffer,
|
|||
|
||||
memset(name, 0, BLE_NAME_BUF_LEN);
|
||||
|
||||
len = REST.get_post_variable(request, "name", &text);
|
||||
len = coap_get_post_variable(request, "name", &text);
|
||||
|
||||
if(len > 0 && len < BLE_NAME_BUF_LEN) {
|
||||
memcpy(name, text, len);
|
||||
|
@ -68,7 +69,7 @@ res_ble_post_put_handler(void *request, void *response, uint8_t *buffer,
|
|||
success = 1;
|
||||
}
|
||||
|
||||
len = REST.get_post_variable(request, "interval", &text);
|
||||
len = coap_get_post_variable(request, "interval", &text);
|
||||
|
||||
rv = atoi(text);
|
||||
|
||||
|
@ -77,15 +78,15 @@ res_ble_post_put_handler(void *request, void *response, uint8_t *buffer,
|
|||
success = 1;
|
||||
}
|
||||
|
||||
len = REST.get_post_variable(request, "mode", &text);
|
||||
len = coap_get_post_variable(request, "mode", &text);
|
||||
|
||||
if(len) {
|
||||
if(strncmp(text, "on", len) == 0) {
|
||||
if(rf_ble_beacond_start()) {
|
||||
success = 1;
|
||||
} else {
|
||||
REST.set_response_status(response, REST.status.FORBIDDEN);
|
||||
REST.set_response_payload(response, forbidden_payload,
|
||||
coap_set_status_code(response, FORBIDDEN_4_03);
|
||||
coap_set_payload(response, forbidden_payload,
|
||||
strlen(forbidden_payload));
|
||||
return;
|
||||
}
|
||||
|
@ -98,7 +99,7 @@ res_ble_post_put_handler(void *request, void *response, uint8_t *buffer,
|
|||
}
|
||||
|
||||
if(!success) {
|
||||
REST.set_response_status(response, REST.status.BAD_REQUEST);
|
||||
coap_set_status_code(response, BAD_REQUEST_4_00);
|
||||
}
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
|
|
|
@ -37,7 +37,7 @@
|
|||
*/
|
||||
/*---------------------------------------------------------------------------*/
|
||||
#include "contiki.h"
|
||||
#include "rest-engine.h"
|
||||
#include "coap-engine.h"
|
||||
#include "coap.h"
|
||||
#include "sys/clock.h"
|
||||
#include "coap-server.h"
|
||||
|
@ -46,6 +46,7 @@
|
|||
#include "ti-lib.h"
|
||||
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
/*---------------------------------------------------------------------------*/
|
||||
static uint16_t
|
||||
detect_chip(void)
|
||||
|
@ -74,106 +75,110 @@ detect_chip(void)
|
|||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
static void
|
||||
res_get_handler_hw(void *request, void *response, uint8_t *buffer,
|
||||
res_get_handler_hw(coap_packet_t *request, coap_packet_t *response,
|
||||
uint8_t *buffer,
|
||||
uint16_t preferred_size, int32_t *offset)
|
||||
{
|
||||
unsigned int accept = -1;
|
||||
uint16_t chip = detect_chip();
|
||||
|
||||
REST.get_header_accept(request, &accept);
|
||||
coap_get_header_accept(request, &accept);
|
||||
|
||||
if(accept == -1 || accept == REST.type.TEXT_PLAIN) {
|
||||
REST.set_header_content_type(response, REST.type.TEXT_PLAIN);
|
||||
if(accept == -1 || accept == TEXT_PLAIN) {
|
||||
coap_set_header_content_format(response, TEXT_PLAIN);
|
||||
snprintf((char *)buffer, REST_MAX_CHUNK_SIZE, "%s on CC%u", BOARD_STRING,
|
||||
chip);
|
||||
|
||||
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);
|
||||
coap_set_payload(response, (uint8_t *)buffer, strlen((char *)buffer));
|
||||
} else if(accept == APPLICATION_JSON) {
|
||||
coap_set_header_content_format(response, APPLICATION_JSON);
|
||||
snprintf((char *)buffer, REST_MAX_CHUNK_SIZE, "{\"HW Ver\":\"%s on CC%u\"}",
|
||||
BOARD_STRING, chip);
|
||||
|
||||
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);
|
||||
coap_set_payload(response, buffer, strlen((char *)buffer));
|
||||
} else if(accept == APPLICATION_XML) {
|
||||
coap_set_header_content_format(response, APPLICATION_XML);
|
||||
snprintf((char *)buffer, REST_MAX_CHUNK_SIZE,
|
||||
"<hw-ver val=\"%s on CC%u\"/>", BOARD_STRING,
|
||||
chip);
|
||||
|
||||
REST.set_response_payload(response, buffer, strlen((char *)buffer));
|
||||
coap_set_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,
|
||||
coap_set_status_code(response, NOT_ACCEPTABLE_4_06);
|
||||
coap_set_payload(response, coap_server_supported_msg,
|
||||
strlen(coap_server_supported_msg));
|
||||
}
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
static void
|
||||
res_get_handler_sw(void *request, void *response, uint8_t *buffer,
|
||||
res_get_handler_sw(coap_packet_t *request, coap_packet_t *response,
|
||||
uint8_t *buffer,
|
||||
uint16_t preferred_size, int32_t *offset)
|
||||
{
|
||||
unsigned int accept = -1;
|
||||
|
||||
REST.get_header_accept(request, &accept);
|
||||
coap_get_header_accept(request, &accept);
|
||||
|
||||
if(accept == -1 || accept == REST.type.TEXT_PLAIN) {
|
||||
REST.set_header_content_type(response, REST.type.TEXT_PLAIN);
|
||||
if(accept == -1 || accept == TEXT_PLAIN) {
|
||||
coap_set_header_content_format(response, TEXT_PLAIN);
|
||||
snprintf((char *)buffer, REST_MAX_CHUNK_SIZE, "%s", CONTIKI_VERSION_STRING);
|
||||
|
||||
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);
|
||||
coap_set_payload(response, (uint8_t *)buffer, strlen((char *)buffer));
|
||||
} else if(accept == APPLICATION_JSON) {
|
||||
coap_set_header_content_format(response, APPLICATION_JSON);
|
||||
snprintf((char *)buffer, REST_MAX_CHUNK_SIZE, "{\"SW Ver\":\"%s\"}",
|
||||
CONTIKI_VERSION_STRING);
|
||||
|
||||
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);
|
||||
coap_set_payload(response, buffer, strlen((char *)buffer));
|
||||
} else if(accept == APPLICATION_XML) {
|
||||
coap_set_header_content_format(response, APPLICATION_XML);
|
||||
snprintf((char *)buffer, REST_MAX_CHUNK_SIZE,
|
||||
"<sw-ver val=\"%s\"/>", CONTIKI_VERSION_STRING);
|
||||
|
||||
REST.set_response_payload(response, buffer, strlen((char *)buffer));
|
||||
coap_set_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,
|
||||
coap_set_status_code(response, NOT_ACCEPTABLE_4_06);
|
||||
coap_set_payload(response, coap_server_supported_msg,
|
||||
strlen(coap_server_supported_msg));
|
||||
}
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
static void
|
||||
res_get_handler_uptime(void *request, void *response, uint8_t *buffer,
|
||||
res_get_handler_uptime(coap_packet_t *request, coap_packet_t *response,
|
||||
uint8_t *buffer,
|
||||
uint16_t preferred_size, int32_t *offset)
|
||||
{
|
||||
unsigned int accept = -1;
|
||||
|
||||
REST.get_header_accept(request, &accept);
|
||||
coap_get_header_accept(request, &accept);
|
||||
|
||||
if(accept == -1 || accept == REST.type.TEXT_PLAIN) {
|
||||
REST.set_header_content_type(response, REST.type.TEXT_PLAIN);
|
||||
if(accept == -1 || accept == TEXT_PLAIN) {
|
||||
coap_set_header_content_format(response, TEXT_PLAIN);
|
||||
snprintf((char *)buffer, REST_MAX_CHUNK_SIZE, "%lu", clock_seconds());
|
||||
|
||||
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);
|
||||
coap_set_payload(response, (uint8_t *)buffer, strlen((char *)buffer));
|
||||
} else if(accept == APPLICATION_JSON) {
|
||||
coap_set_header_content_format(response, APPLICATION_JSON);
|
||||
snprintf((char *)buffer, REST_MAX_CHUNK_SIZE, "{\"uptime\":%lu}",
|
||||
clock_seconds());
|
||||
|
||||
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);
|
||||
coap_set_payload(response, buffer, strlen((char *)buffer));
|
||||
} else if(accept == APPLICATION_XML) {
|
||||
coap_set_header_content_format(response, APPLICATION_XML);
|
||||
snprintf((char *)buffer, REST_MAX_CHUNK_SIZE,
|
||||
"<uptime val=\"%lu\" unit=\"sec\"/>", clock_seconds());
|
||||
|
||||
REST.set_response_payload(response, buffer, strlen((char *)buffer));
|
||||
coap_set_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,
|
||||
coap_set_status_code(response, NOT_ACCEPTABLE_4_06);
|
||||
coap_set_payload(response, coap_server_supported_msg,
|
||||
strlen(coap_server_supported_msg));
|
||||
}
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
static void
|
||||
res_post_handler_cfg_reset(void *request, void *response, uint8_t *buffer,
|
||||
res_post_handler_cfg_reset(coap_packet_t *request, coap_packet_t *response,
|
||||
uint8_t *buffer,
|
||||
uint16_t preferred_size, int32_t *offset)
|
||||
{
|
||||
cc26xx_web_demo_restore_defaults();
|
||||
|
|
|
@ -40,13 +40,14 @@
|
|||
*/
|
||||
/*---------------------------------------------------------------------------*/
|
||||
#include "contiki.h"
|
||||
#include "rest-engine.h"
|
||||
#include "coap-engine.h"
|
||||
#include "dev/leds.h"
|
||||
|
||||
#include <string.h>
|
||||
/*---------------------------------------------------------------------------*/
|
||||
static void
|
||||
res_post_put_handler(void *request, void *response, uint8_t *buffer,
|
||||
res_post_put_handler(coap_packet_t *request, coap_packet_t *response,
|
||||
uint8_t *buffer,
|
||||
uint16_t preferred_size, int32_t *offset)
|
||||
{
|
||||
size_t len = 0;
|
||||
|
@ -55,7 +56,7 @@ res_post_put_handler(void *request, void *response, uint8_t *buffer,
|
|||
uint8_t led = 0;
|
||||
int success = 1;
|
||||
|
||||
if((len = REST.get_query_variable(request, "color", &color))) {
|
||||
if((len = coap_get_query_variable(request, "color", &color))) {
|
||||
if(strncmp(color, "r", len) == 0) {
|
||||
led = LEDS_RED;
|
||||
} else if(strncmp(color, "g", len) == 0) {
|
||||
|
@ -73,7 +74,7 @@ res_post_put_handler(void *request, void *response, uint8_t *buffer,
|
|||
success = 0;
|
||||
}
|
||||
|
||||
if(success && (len = REST.get_post_variable(request, "mode", &mode))) {
|
||||
if(success && (len = coap_get_post_variable(request, "mode", &mode))) {
|
||||
if(strncmp(mode, "on", len) == 0) {
|
||||
leds_on(led);
|
||||
} else if(strncmp(mode, "off", len) == 0) {
|
||||
|
@ -86,7 +87,7 @@ res_post_put_handler(void *request, void *response, uint8_t *buffer,
|
|||
}
|
||||
|
||||
if(!success) {
|
||||
REST.set_response_status(response, REST.status.BAD_REQUEST);
|
||||
coap_set_status_code(response, BAD_REQUEST_4_00);
|
||||
}
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
|
|
|
@ -37,82 +37,86 @@
|
|||
*/
|
||||
/*---------------------------------------------------------------------------*/
|
||||
#include "contiki.h"
|
||||
#include "rest-engine.h"
|
||||
#include "coap-engine.h"
|
||||
#include "coap.h"
|
||||
#include "net/ipv6/uip-ds6.h"
|
||||
#include "coap-server.h"
|
||||
#include "cc26xx-web-demo.h"
|
||||
|
||||
#include "ti-lib.h"
|
||||
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
/*---------------------------------------------------------------------------*/
|
||||
extern int def_rt_rssi;
|
||||
/*---------------------------------------------------------------------------*/
|
||||
static void
|
||||
res_get_handler_parent_rssi(void *request, void *response, uint8_t *buffer,
|
||||
res_get_handler_parent_rssi(coap_packet_t *request, coap_packet_t *response,
|
||||
uint8_t *buffer,
|
||||
uint16_t preferred_size, int32_t *offset)
|
||||
{
|
||||
unsigned int accept = -1;
|
||||
|
||||
REST.get_header_accept(request, &accept);
|
||||
coap_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);
|
||||
if(accept == -1 || accept == TEXT_PLAIN) {
|
||||
coap_set_header_content_format(response, TEXT_PLAIN);
|
||||
snprintf((char *)buffer, COAP_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\"}",
|
||||
coap_set_payload(response, (uint8_t *)buffer, strlen((char *)buffer));
|
||||
} else if(accept == APPLICATION_JSON) {
|
||||
coap_set_header_content_format(response, APPLICATION_JSON);
|
||||
snprintf((char *)buffer, COAP_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,
|
||||
coap_set_payload(response, buffer, strlen((char *)buffer));
|
||||
} else if(accept == APPLICATION_XML) {
|
||||
coap_set_header_content_format(response, APPLICATION_XML);
|
||||
snprintf((char *)buffer, COAP_MAX_CHUNK_SIZE,
|
||||
"<parent-rssi val=\"%d\"/>", def_rt_rssi);
|
||||
|
||||
REST.set_response_payload(response, buffer, strlen((char *)buffer));
|
||||
coap_set_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,
|
||||
coap_set_status_code(response, NOT_ACCEPTABLE_4_06);
|
||||
coap_set_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,
|
||||
res_get_handler_pref_parent(coap_packet_t *request, coap_packet_t *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);
|
||||
coap_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);
|
||||
if(accept == -1 || accept == TEXT_PLAIN) {
|
||||
coap_set_header_content_format(response, TEXT_PLAIN);
|
||||
snprintf((char *)buffer, COAP_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\"}",
|
||||
coap_set_payload(response, (uint8_t *)buffer, strlen((char *)buffer));
|
||||
} else if(accept == APPLICATION_JSON) {
|
||||
coap_set_header_content_format(response, APPLICATION_JSON);
|
||||
snprintf((char *)buffer, COAP_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,
|
||||
coap_set_payload(response, buffer, strlen((char *)buffer));
|
||||
} else if(accept == APPLICATION_XML) {
|
||||
coap_set_header_content_format(response, APPLICATION_XML);
|
||||
snprintf((char *)buffer, COAP_MAX_CHUNK_SIZE,
|
||||
"<parent=\"%s\"/>", def_rt_str);
|
||||
|
||||
REST.set_response_payload(response, buffer, strlen((char *)buffer));
|
||||
coap_set_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,
|
||||
coap_set_status_code(response, NOT_ACCEPTABLE_4_06);
|
||||
coap_set_payload(response, coap_server_supported_msg,
|
||||
strlen(coap_server_supported_msg));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -37,7 +37,7 @@
|
|||
*/
|
||||
/*---------------------------------------------------------------------------*/
|
||||
#include "contiki.h"
|
||||
#include "rest-engine.h"
|
||||
#include "coap-engine.h"
|
||||
#include "coap.h"
|
||||
#include "cc26xx-web-demo.h"
|
||||
#include "coap-server.h"
|
||||
|
@ -51,7 +51,8 @@
|
|||
* called by all handlers and populates the CoAP response
|
||||
*/
|
||||
static void
|
||||
res_get_handler_all(int sens_type, void *request, void *response,
|
||||
res_get_handler_all(int sens_type, coap_packet_t *request,
|
||||
coap_packet_t *response,
|
||||
uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
{
|
||||
unsigned int accept = -1;
|
||||
|
@ -60,43 +61,44 @@ res_get_handler_all(int sens_type, void *request, void *response,
|
|||
reading = cc26xx_web_demo_sensor_lookup(sens_type);
|
||||
|
||||
if(reading == NULL) {
|
||||
REST.set_response_status(response, REST.status.NOT_FOUND);
|
||||
REST.set_response_payload(response, coap_server_not_found_msg,
|
||||
coap_set_status_code(response, NOT_FOUND_4_04);
|
||||
coap_set_payload(response, coap_server_not_found_msg,
|
||||
strlen(coap_server_not_found_msg));
|
||||
return;
|
||||
}
|
||||
|
||||
REST.get_header_accept(request, &accept);
|
||||
coap_get_header_accept(request, &accept);
|
||||
|
||||
if(accept == -1 || accept == REST.type.TEXT_PLAIN) {
|
||||
REST.set_header_content_type(response, REST.type.TEXT_PLAIN);
|
||||
if(accept == -1 || accept == TEXT_PLAIN) {
|
||||
coap_set_header_content_format(response, TEXT_PLAIN);
|
||||
snprintf((char *)buffer, REST_MAX_CHUNK_SIZE, "%s", reading->converted);
|
||||
|
||||
REST.set_response_payload(response, (uint8_t *)buffer,
|
||||
coap_set_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);
|
||||
} else if(accept == APPLICATION_JSON) {
|
||||
coap_set_header_content_format(response, APPLICATION_JSON);
|
||||
snprintf((char *)buffer, REST_MAX_CHUNK_SIZE, "{\"%s\":%s}",
|
||||
reading->descr, reading->converted);
|
||||
|
||||
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);
|
||||
coap_set_payload(response, buffer, strlen((char *)buffer));
|
||||
} else if(accept == APPLICATION_XML) {
|
||||
coap_set_header_content_format(response, APPLICATION_XML);
|
||||
snprintf((char *)buffer, REST_MAX_CHUNK_SIZE,
|
||||
"<%s val=\"%s\" unit=\"%s\"/>", reading->xml_element,
|
||||
reading->converted, reading->units);
|
||||
|
||||
REST.set_response_payload(response, buffer, strlen((char *)buffer));
|
||||
coap_set_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,
|
||||
coap_set_status_code(response, NOT_ACCEPTABLE_4_06);
|
||||
coap_set_payload(response, coap_server_supported_msg,
|
||||
strlen(coap_server_supported_msg));
|
||||
}
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
/* BatMon resources and handler: Temperature, Voltage */
|
||||
static void
|
||||
res_get_handler_batmon_temp(void *request, void *response, uint8_t *buffer,
|
||||
res_get_handler_batmon_temp(coap_packet_t *request, coap_packet_t *response,
|
||||
uint8_t *buffer,
|
||||
uint16_t preferred_size, int32_t *offset)
|
||||
{
|
||||
res_get_handler_all(CC26XX_WEB_DEMO_SENSOR_BATMON_TEMP, request, response,
|
||||
|
@ -104,7 +106,8 @@ res_get_handler_batmon_temp(void *request, void *response, uint8_t *buffer,
|
|||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
static void
|
||||
res_get_handler_batmon_volt(void *request, void *response, uint8_t *buffer,
|
||||
res_get_handler_batmon_volt(coap_packet_t *request, coap_packet_t *response,
|
||||
uint8_t *buffer,
|
||||
uint16_t preferred_size, int32_t *offset)
|
||||
{
|
||||
res_get_handler_all(CC26XX_WEB_DEMO_SENSOR_BATMON_VOLT, request, response,
|
||||
|
@ -120,7 +123,8 @@ RESOURCE(res_batmon_volt, "title=\"Battery Voltage\";rt=\"mV\"",
|
|||
#if CC26XX_WEB_DEMO_ADC_DEMO
|
||||
/*---------------------------------------------------------------------------*/
|
||||
static void
|
||||
res_get_handler_adc_dio23(void *request, void *response, uint8_t *buffer,
|
||||
res_get_handler_adc_dio23(coap_packet_t *request, coap_packet_t *response,
|
||||
uint8_t *buffer,
|
||||
uint16_t preferred_size, int32_t *offset)
|
||||
{
|
||||
res_get_handler_all(CC26XX_WEB_DEMO_SENSOR_ADC_DIO23, request, response,
|
||||
|
@ -136,7 +140,8 @@ RESOURCE(res_adc_dio23, "title=\"ADC DIO23\";rt=\"mV\"",
|
|||
/*---------------------------------------------------------------------------*/
|
||||
/* MPU resources and handler: Accelerometer and Gyro */
|
||||
static void
|
||||
res_get_handler_mpu_acc_x(void *request, void *response, uint8_t *buffer,
|
||||
res_get_handler_mpu_acc_x(coap_packet_t *request, coap_packet_t *response,
|
||||
uint8_t *buffer,
|
||||
uint16_t preferred_size, int32_t *offset)
|
||||
{
|
||||
res_get_handler_all(CC26XX_WEB_DEMO_SENSOR_MPU_ACC_X, request, response,
|
||||
|
@ -144,7 +149,8 @@ res_get_handler_mpu_acc_x(void *request, void *response, uint8_t *buffer,
|
|||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
static void
|
||||
res_get_handler_mpu_acc_y(void *request, void *response, uint8_t *buffer,
|
||||
res_get_handler_mpu_acc_y(coap_packet_t *request, coap_packet_t *response,
|
||||
uint8_t *buffer,
|
||||
uint16_t preferred_size, int32_t *offset)
|
||||
{
|
||||
res_get_handler_all(CC26XX_WEB_DEMO_SENSOR_MPU_ACC_Y, request, response,
|
||||
|
@ -152,7 +158,8 @@ res_get_handler_mpu_acc_y(void *request, void *response, uint8_t *buffer,
|
|||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
static void
|
||||
res_get_handler_mpu_acc_z(void *request, void *response, uint8_t *buffer,
|
||||
res_get_handler_mpu_acc_z(coap_packet_t *request, coap_packet_t *response,
|
||||
uint8_t *buffer,
|
||||
uint16_t preferred_size, int32_t *offset)
|
||||
{
|
||||
res_get_handler_all(CC26XX_WEB_DEMO_SENSOR_MPU_ACC_Z, request, response,
|
||||
|
@ -160,7 +167,8 @@ res_get_handler_mpu_acc_z(void *request, void *response, uint8_t *buffer,
|
|||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
static void
|
||||
res_get_handler_mpu_gyro_x(void *request, void *response, uint8_t *buffer,
|
||||
res_get_handler_mpu_gyro_x(coap_packet_t *request, coap_packet_t *response,
|
||||
uint8_t *buffer,
|
||||
uint16_t preferred_size, int32_t *offset)
|
||||
{
|
||||
res_get_handler_all(CC26XX_WEB_DEMO_SENSOR_MPU_GYRO_X, request, response,
|
||||
|
@ -168,7 +176,8 @@ res_get_handler_mpu_gyro_x(void *request, void *response, uint8_t *buffer,
|
|||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
static void
|
||||
res_get_handler_mpu_gyro_y(void *request, void *response, uint8_t *buffer,
|
||||
res_get_handler_mpu_gyro_y(coap_packet_t *request, coap_packet_t *response,
|
||||
uint8_t *buffer,
|
||||
uint16_t preferred_size, int32_t *offset)
|
||||
{
|
||||
res_get_handler_all(CC26XX_WEB_DEMO_SENSOR_MPU_GYRO_Y, request, response,
|
||||
|
@ -176,7 +185,8 @@ res_get_handler_mpu_gyro_y(void *request, void *response, uint8_t *buffer,
|
|||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
static void
|
||||
res_get_handler_mpu_gyro_z(void *request, void *response, uint8_t *buffer,
|
||||
res_get_handler_mpu_gyro_z(coap_packet_t *request, coap_packet_t *response,
|
||||
uint8_t *buffer,
|
||||
uint16_t preferred_size, int32_t *offset)
|
||||
{
|
||||
res_get_handler_all(CC26XX_WEB_DEMO_SENSOR_MPU_GYRO_Z, request, response,
|
||||
|
@ -199,7 +209,8 @@ RESOURCE(res_mpu_gyro_z, "title=\"Gyro Z\";rt=\"deg/sec\"",
|
|||
/*---------------------------------------------------------------------------*/
|
||||
/* TMP sensor resources and handlers: Object, Ambient */
|
||||
static void
|
||||
res_get_handler_obj_temp(void *request, void *response, uint8_t *buffer,
|
||||
res_get_handler_obj_temp(coap_packet_t *request, coap_packet_t *response,
|
||||
uint8_t *buffer,
|
||||
uint16_t preferred_size, int32_t *offset)
|
||||
{
|
||||
res_get_handler_all(CC26XX_WEB_DEMO_SENSOR_TMP_OBJECT, request, response,
|
||||
|
@ -207,7 +218,8 @@ res_get_handler_obj_temp(void *request, void *response, uint8_t *buffer,
|
|||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
static void
|
||||
res_get_handler_amb_temp(void *request, void *response, uint8_t *buffer,
|
||||
res_get_handler_amb_temp(coap_packet_t *request, coap_packet_t *response,
|
||||
uint8_t *buffer,
|
||||
uint16_t preferred_size, int32_t *offset)
|
||||
{
|
||||
res_get_handler_all(CC26XX_WEB_DEMO_SENSOR_TMP_AMBIENT, request, response,
|
||||
|
@ -222,7 +234,8 @@ RESOURCE(res_tmp007_amb, "title=\"Temperature (Ambient)\";rt=\"C\"",
|
|||
/*---------------------------------------------------------------------------*/
|
||||
/* BMP sensor resources: Temperature, Pressure */
|
||||
static void
|
||||
res_get_handler_bmp_temp(void *request, void *response, uint8_t *buffer,
|
||||
res_get_handler_bmp_temp(coap_packet_t *request, coap_packet_t *response,
|
||||
uint8_t *buffer,
|
||||
uint16_t preferred_size, int32_t *offset)
|
||||
{
|
||||
res_get_handler_all(CC26XX_WEB_DEMO_SENSOR_BMP_TEMP, request, response,
|
||||
|
@ -230,7 +243,8 @@ res_get_handler_bmp_temp(void *request, void *response, uint8_t *buffer,
|
|||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
static void
|
||||
res_get_handler_bmp_press(void *request, void *response, uint8_t *buffer,
|
||||
res_get_handler_bmp_press(coap_packet_t *request, coap_packet_t *response,
|
||||
uint8_t *buffer,
|
||||
uint16_t preferred_size, int32_t *offset)
|
||||
{
|
||||
res_get_handler_all(CC26XX_WEB_DEMO_SENSOR_BMP_PRES, request, response,
|
||||
|
@ -246,7 +260,8 @@ RESOURCE(res_bmp280_press,
|
|||
/*---------------------------------------------------------------------------*/
|
||||
/* HDC1000 sensor resources and handler: Temperature, Pressure */
|
||||
static void
|
||||
res_get_handler_hdc_temp(void *request, void *response, uint8_t *buffer,
|
||||
res_get_handler_hdc_temp(coap_packet_t *request, coap_packet_t *response,
|
||||
uint8_t *buffer,
|
||||
uint16_t preferred_size, int32_t *offset)
|
||||
{
|
||||
res_get_handler_all(CC26XX_WEB_DEMO_SENSOR_HDC_TEMP, request, response,
|
||||
|
@ -254,7 +269,8 @@ res_get_handler_hdc_temp(void *request, void *response, uint8_t *buffer,
|
|||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
static void
|
||||
res_get_handler_hdc_humidity(void *request, void *response, uint8_t *buffer,
|
||||
res_get_handler_hdc_humidity(coap_packet_t *request, coap_packet_t *response,
|
||||
uint8_t *buffer,
|
||||
uint16_t preferred_size, int32_t *offset)
|
||||
{
|
||||
res_get_handler_all(CC26XX_WEB_DEMO_SENSOR_HDC_HUMIDITY, request, response,
|
||||
|
@ -269,7 +285,8 @@ RESOURCE(res_hdc1000_hum, "title=\"Humidity\";rt=\"%RH\"",
|
|||
/*---------------------------------------------------------------------------*/
|
||||
/* Illuminance resources and handler */
|
||||
static void
|
||||
res_get_handler_opt(void *request, void *response, uint8_t *buffer,
|
||||
res_get_handler_opt(coap_packet_t *request, coap_packet_t *response,
|
||||
uint8_t *buffer,
|
||||
uint16_t preferred_size, int32_t *offset)
|
||||
{
|
||||
res_get_handler_all(CC26XX_WEB_DEMO_SENSOR_OPT_LIGHT, request, response,
|
||||
|
|
|
@ -42,20 +42,22 @@
|
|||
*/
|
||||
/*---------------------------------------------------------------------------*/
|
||||
#include "contiki.h"
|
||||
#include "rest-engine.h"
|
||||
#include "coap-engine.h"
|
||||
#include "dev/leds.h"
|
||||
|
||||
#include <string.h>
|
||||
/*---------------------------------------------------------------------------*/
|
||||
static void
|
||||
res_post_handler_red(void *request, void *response, uint8_t *buffer,
|
||||
res_post_handler_red(coap_packet_t *request, coap_packet_t *response,
|
||||
uint8_t *buffer,
|
||||
uint16_t preferred_size, int32_t *offset)
|
||||
{
|
||||
leds_toggle(LEDS_RED);
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
static void
|
||||
res_post_handler_green(void *request, void *response, uint8_t *buffer,
|
||||
res_post_handler_green(coap_packet_t *request, coap_packet_t *response,
|
||||
uint8_t *buffer,
|
||||
uint16_t preferred_size, int32_t *offset)
|
||||
{
|
||||
leds_toggle(LEDS_GREEN);
|
||||
|
@ -81,14 +83,16 @@ RESOURCE(res_toggle_green,
|
|||
#if BOARD_SMARTRF06EB
|
||||
/*---------------------------------------------------------------------------*/
|
||||
static void
|
||||
res_post_handler_yellow(void *request, void *response, uint8_t *buffer,
|
||||
res_post_handler_yellow(coap_packet_t *request, coap_packet_t *response,
|
||||
uint8_t *buffer,
|
||||
uint16_t preferred_size, int32_t *offset)
|
||||
{
|
||||
leds_toggle(LEDS_YELLOW);
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
static void
|
||||
res_post_handler_orange(void *request, void *response, uint8_t *buffer,
|
||||
res_post_handler_orange(coap_packet_t *request, coap_packet_t *response,
|
||||
uint8_t *buffer,
|
||||
uint16_t preferred_size, int32_t *offset)
|
||||
{
|
||||
leds_toggle(LEDS_ORANGE);
|
||||
|
|
|
@ -44,7 +44,7 @@
|
|||
#if UIP_CONF_IPV6_RPL_LITE == 0
|
||||
#include "rpl-private.h"
|
||||
#endif /* UIP_CONF_IPV6_RPL_LITE == 0 */
|
||||
#include "rest-engine.h"
|
||||
#include "coap-engine.h"
|
||||
#include "coap.h"
|
||||
|
||||
#include "ti-lib.h"
|
||||
|
@ -104,38 +104,38 @@ PROCESS(very_sleepy_demo_process, "CC13xx/CC26xx very sleepy process");
|
|||
AUTOSTART_PROCESSES(&very_sleepy_demo_process);
|
||||
/*---------------------------------------------------------------------------*/
|
||||
static void
|
||||
readings_get_handler(void *request, void *response, uint8_t *buffer,
|
||||
uint16_t preferred_size, int32_t *offset)
|
||||
readings_get_handler(coap_packet_t *request, coap_packet_t *response,
|
||||
uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
{
|
||||
unsigned int accept = -1;
|
||||
int temp;
|
||||
int voltage;
|
||||
|
||||
if(request != NULL) {
|
||||
REST.get_header_accept(request, &accept);
|
||||
coap_get_header_accept(request, &accept);
|
||||
}
|
||||
|
||||
temp = batmon_sensor.value(BATMON_SENSOR_TYPE_TEMP);
|
||||
|
||||
voltage = batmon_sensor.value(BATMON_SENSOR_TYPE_VOLT);
|
||||
|
||||
if(accept == -1 || accept == REST.type.APPLICATION_JSON) {
|
||||
REST.set_header_content_type(response, REST.type.APPLICATION_JSON);
|
||||
snprintf((char *)buffer, REST_MAX_CHUNK_SIZE,
|
||||
if(accept == -1 || accept == APPLICATION_JSON) {
|
||||
coap_set_header_content_format(response, APPLICATION_JSON);
|
||||
snprintf((char *)buffer, COAP_MAX_CHUNK_SIZE,
|
||||
"{\"temp\":{\"v\":%d,\"u\":\"C\"},"
|
||||
"\"voltage\":{\"v\":%d,\"u\":\"mV\"}}",
|
||||
temp, (voltage * 125) >> 5);
|
||||
|
||||
REST.set_response_payload(response, buffer, strlen((char *)buffer));
|
||||
} else if(accept == REST.type.TEXT_PLAIN) {
|
||||
REST.set_header_content_type(response, REST.type.TEXT_PLAIN);
|
||||
snprintf((char *)buffer, REST_MAX_CHUNK_SIZE, "Temp=%dC, Voltage=%dmV",
|
||||
coap_set_payload(response, buffer, strlen((char *)buffer));
|
||||
} else if(accept == TEXT_PLAIN) {
|
||||
coap_set_header_content_format(response, TEXT_PLAIN);
|
||||
snprintf((char *)buffer, COAP_MAX_CHUNK_SIZE, "Temp=%dC, Voltage=%dmV",
|
||||
temp, (voltage * 125) >> 5);
|
||||
|
||||
REST.set_response_payload(response, buffer, strlen((char *)buffer));
|
||||
coap_set_payload(response, buffer, strlen((char *)buffer));
|
||||
} else {
|
||||
REST.set_response_status(response, REST.status.NOT_ACCEPTABLE);
|
||||
REST.set_response_payload(response, not_supported_msg,
|
||||
coap_set_status_code(response, NOT_ACCEPTABLE_4_06);
|
||||
coap_set_payload(response, not_supported_msg,
|
||||
strlen(not_supported_msg));
|
||||
}
|
||||
}
|
||||
|
@ -144,39 +144,39 @@ RESOURCE(readings_resource, "title=\"Sensor Readings\";obs",
|
|||
readings_get_handler, NULL, NULL, NULL);
|
||||
/*---------------------------------------------------------------------------*/
|
||||
static void
|
||||
conf_get_handler(void *request, void *response, uint8_t *buffer,
|
||||
uint16_t preferred_size, int32_t *offset)
|
||||
conf_get_handler(coap_packet_t *request, coap_packet_t *response,
|
||||
uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
{
|
||||
unsigned int accept = -1;
|
||||
|
||||
if(request != NULL) {
|
||||
REST.get_header_accept(request, &accept);
|
||||
coap_get_header_accept(request, &accept);
|
||||
}
|
||||
|
||||
if(accept == -1 || accept == REST.type.APPLICATION_JSON) {
|
||||
REST.set_header_content_type(response, REST.type.APPLICATION_JSON);
|
||||
snprintf((char *)buffer, REST_MAX_CHUNK_SIZE,
|
||||
if(accept == -1 || accept == APPLICATION_JSON) {
|
||||
coap_set_header_content_format(response, APPLICATION_JSON);
|
||||
snprintf((char *)buffer, COAP_MAX_CHUNK_SIZE,
|
||||
"{\"config\":{\"mode\":%u,\"duration\":%lu,\"interval\":%lu}}",
|
||||
config.mode, config.duration, config.interval);
|
||||
|
||||
REST.set_response_payload(response, buffer, strlen((char *)buffer));
|
||||
} else if(accept == REST.type.TEXT_PLAIN) {
|
||||
REST.set_header_content_type(response, REST.type.TEXT_PLAIN);
|
||||
snprintf((char *)buffer, REST_MAX_CHUNK_SIZE,
|
||||
coap_set_payload(response, buffer, strlen((char *)buffer));
|
||||
} else if(accept == TEXT_PLAIN) {
|
||||
coap_set_header_content_format(response, TEXT_PLAIN);
|
||||
snprintf((char *)buffer, COAP_MAX_CHUNK_SIZE,
|
||||
"Mode=%u, Duration=%lusecs, Interval=%lusecs",
|
||||
config.mode, config.duration, config.interval);
|
||||
|
||||
REST.set_response_payload(response, buffer, strlen((char *)buffer));
|
||||
coap_set_payload(response, buffer, strlen((char *)buffer));
|
||||
} else {
|
||||
REST.set_response_status(response, REST.status.NOT_ACCEPTABLE);
|
||||
REST.set_response_payload(response, not_supported_msg,
|
||||
coap_set_status_code(response, NOT_ACCEPTABLE_4_06);
|
||||
coap_set_payload(response, not_supported_msg,
|
||||
strlen(not_supported_msg));
|
||||
}
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
static void
|
||||
conf_post_handler(void *request, void *response, uint8_t *buffer,
|
||||
uint16_t preferred_size, int32_t *offset)
|
||||
conf_post_handler(coap_packet_t *request, coap_packet_t *response,
|
||||
uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
{
|
||||
const char *ptr = NULL;
|
||||
char tmp_buf[16];
|
||||
|
@ -186,7 +186,7 @@ conf_post_handler(void *request, void *response, uint8_t *buffer,
|
|||
uint8_t post_status = POST_STATUS_NONE;
|
||||
int rv;
|
||||
|
||||
rv = REST.get_post_variable(request, "mode", &ptr);
|
||||
rv = coap_get_post_variable(request, "mode", &ptr);
|
||||
if(rv && rv < 16) {
|
||||
memset(tmp_buf, 0, sizeof(tmp_buf));
|
||||
memcpy(tmp_buf, ptr, rv);
|
||||
|
@ -203,7 +203,7 @@ conf_post_handler(void *request, void *response, uint8_t *buffer,
|
|||
}
|
||||
}
|
||||
|
||||
rv = REST.get_post_variable(request, "duration", &ptr);
|
||||
rv = coap_get_post_variable(request, "duration", &ptr);
|
||||
if(rv && rv < 16) {
|
||||
memset(tmp_buf, 0, sizeof(tmp_buf));
|
||||
memcpy(tmp_buf, ptr, rv);
|
||||
|
@ -217,7 +217,7 @@ conf_post_handler(void *request, void *response, uint8_t *buffer,
|
|||
}
|
||||
}
|
||||
|
||||
rv = REST.get_post_variable(request, "interval", &ptr);
|
||||
rv = coap_get_post_variable(request, "interval", &ptr);
|
||||
if(rv && rv < 16) {
|
||||
memset(tmp_buf, 0, sizeof(tmp_buf));
|
||||
memcpy(tmp_buf, ptr, rv);
|
||||
|
@ -232,13 +232,13 @@ conf_post_handler(void *request, void *response, uint8_t *buffer,
|
|||
|
||||
if((post_status & POST_STATUS_BAD) == POST_STATUS_BAD ||
|
||||
post_status == POST_STATUS_NONE) {
|
||||
REST.set_response_status(response, REST.status.BAD_REQUEST);
|
||||
snprintf((char *)buffer, REST_MAX_CHUNK_SIZE,
|
||||
coap_set_status_code(response, BAD_REQUEST_4_00);
|
||||
snprintf((char *)buffer, COAP_MAX_CHUNK_SIZE,
|
||||
"mode=0|1&duration=[%u,%u]&interval=[%u,%u]",
|
||||
NORMAL_OP_DURATION_MIN, NORMAL_OP_DURATION_MAX,
|
||||
PERIODIC_INTERVAL_MIN, PERIODIC_INTERVAL_MAX);
|
||||
|
||||
REST.set_response_payload(response, buffer, strlen((char *)buffer));
|
||||
coap_set_payload(response, buffer, strlen((char *)buffer));
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -345,11 +345,11 @@ PROCESS_THREAD(very_sleepy_demo_process, ev, data)
|
|||
|
||||
event_new_config = process_alloc_event();
|
||||
|
||||
rest_init_engine();
|
||||
coap_engine_init();
|
||||
|
||||
readings_resource.flags += IS_OBSERVABLE;
|
||||
rest_activate_resource(&readings_resource, "sen/readings");
|
||||
rest_activate_resource(&very_sleepy_conf, "very_sleepy_config");
|
||||
coap_activate_resource(&readings_resource, "sen/readings");
|
||||
coap_activate_resource(&very_sleepy_conf, "very_sleepy_config");
|
||||
|
||||
printf("Very Sleepy Demo Process\n");
|
||||
|
||||
|
@ -390,7 +390,7 @@ PROCESS_THREAD(very_sleepy_demo_process, ev, data)
|
|||
* send notifications to observers as required.
|
||||
*/
|
||||
if(state == STATE_NOTIFY_OBSERVERS) {
|
||||
REST.notify_subscribers(&readings_resource);
|
||||
coap_notify_observers(&readings_resource);
|
||||
state = STATE_NORMAL;
|
||||
}
|
||||
|
||||
|
|
|
@ -35,15 +35,15 @@
|
|||
#include "net/ipv6/uip.h"
|
||||
#include "net/ipv6/uip-ds6.h"
|
||||
#include "tools/rpl-tools.h"
|
||||
#include "rest-engine.h"
|
||||
#include "coap-engine.h"
|
||||
#include "sys/ctimer.h"
|
||||
#include <stdio.h>
|
||||
#include "dev/leds.h"
|
||||
|
||||
static void ct_callback(void *ptr);
|
||||
static void put_post_led_toggle_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
static void put_post_led_toggle_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
|
||||
static char content[REST_MAX_CHUNK_SIZE];
|
||||
static char content[COAP_MAX_CHUNK_SIZE];
|
||||
static int content_len = 0;
|
||||
static struct ctimer ct;
|
||||
|
||||
|
@ -77,7 +77,7 @@ RESOURCE(resource_led_toggle,
|
|||
put_post_led_toggle_handler,
|
||||
NULL);
|
||||
static void
|
||||
put_post_led_toggle_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
put_post_led_toggle_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
{
|
||||
static int led_state = 0;
|
||||
unsigned int accept = -1;
|
||||
|
@ -110,10 +110,10 @@ put_post_led_toggle_handler(void *request, void *response, uint8_t *buffer, uint
|
|||
break;
|
||||
}
|
||||
/* Return message */
|
||||
REST.get_header_accept(request, &accept);
|
||||
if(accept == -1 || accept == REST.type.TEXT_PLAIN) {
|
||||
REST.set_header_content_type(response, REST.type.TEXT_PLAIN);
|
||||
REST.set_response_payload(response, (uint8_t *)content, content_len);
|
||||
coap_get_header_accept(request, &accept);
|
||||
if(accept == -1 || accept == TEXT_PLAIN) {
|
||||
coap_set_header_content_format(response, TEXT_PLAIN);
|
||||
coap_set_payload(response, (uint8_t *)content, content_len);
|
||||
}
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
|
@ -137,8 +137,8 @@ PROCESS_THREAD(start_app, ev, data)
|
|||
rpl_tools_init(NULL);
|
||||
} printf("Starting RPL node\n");
|
||||
|
||||
rest_init_engine();
|
||||
rest_activate_resource(&resource_led_toggle, "Dongle/LED-toggle");
|
||||
coap_engine_init();
|
||||
coap_activate_resource(&resource_led_toggle, "Dongle/LED-toggle");
|
||||
|
||||
PROCESS_END();
|
||||
}
|
||||
|
|
|
@ -36,7 +36,7 @@
|
|||
#include "net/ipv6/uip.h"
|
||||
#include "net/ipv6/uip-ds6.h"
|
||||
#include "tools/rpl-tools.h"
|
||||
#include "rest-engine.h"
|
||||
#include "coap-engine.h"
|
||||
#include "light-sensor.h"
|
||||
#include "ht-sensor.h"
|
||||
#include "dev/leds.h"
|
||||
|
@ -46,19 +46,19 @@
|
|||
#include <stdlib.h>
|
||||
|
||||
static void event_sensors_dr1175_handler(void);
|
||||
static void get_sensors_dr1175_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
static void get_light_sensor_value_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
static void get_light_sensor_unit_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
static void get_temperature_value_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
static void get_temperature_unit_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
static void get_humidity_value_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
static void get_humidity_unit_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
static void put_post_white_led_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
static void put_post_rgb_led_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
static void put_post_led_d3_1174_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
static void put_post_led_d6_1174_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
static void get_sensors_dr1175_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
static void get_light_sensor_value_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
static void get_light_sensor_unit_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
static void get_temperature_value_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
static void get_temperature_unit_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
static void get_humidity_value_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
static void get_humidity_unit_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
static void put_post_white_led_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
static void put_post_rgb_led_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
static void put_post_led_d3_1174_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
static void put_post_led_d6_1174_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
|
||||
static char content[REST_MAX_CHUNK_SIZE];
|
||||
static char content[COAP_MAX_CHUNK_SIZE];
|
||||
static int content_len = 0;
|
||||
|
||||
#define CONTENT_PRINTF(...) { if(content_len < sizeof(content)) { content_len += snprintf(content + content_len, sizeof(content) - content_len, __VA_ARGS__); } }
|
||||
|
@ -90,26 +90,26 @@ EVENT_RESOURCE(resource_sensors_dr1175, /* name */
|
|||
NULL, /* DELETE handler */
|
||||
event_sensors_dr1175_handler); /* event handler */
|
||||
static void
|
||||
get_sensors_dr1175_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
get_sensors_dr1175_handler(coap_packet_t *request, coap_packet_t *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.APPLICATION_JSON) {
|
||||
coap_get_header_accept(request, &accept);
|
||||
if(accept == -1 || accept == APPLICATION_JSON) {
|
||||
content_len = 0;
|
||||
CONTENT_PRINTF("{\"DR1175\":[");
|
||||
CONTENT_PRINTF("{\"Humidity\":\"%d\"},", ht_sensor.value(HT_SENSOR_HUM));
|
||||
CONTENT_PRINTF("{\"Light\":\"%d\"},", light_sensor.value(0));
|
||||
CONTENT_PRINTF("{\"Temp\":\"%d\"}", ht_sensor.value(HT_SENSOR_TEMP));
|
||||
CONTENT_PRINTF("]}");
|
||||
REST.set_header_content_type(response, REST.type.APPLICATION_JSON);
|
||||
REST.set_response_payload(response, (uint8_t *)content, content_len);
|
||||
coap_set_header_content_format(response, APPLICATION_JSON);
|
||||
coap_set_payload(response, (uint8_t *)content, content_len);
|
||||
}
|
||||
}
|
||||
static void
|
||||
event_sensors_dr1175_handler()
|
||||
{
|
||||
/* Registered observers are notified and will trigger the GET handler to create the response. */
|
||||
REST.notify_subscribers(&resource_sensors_dr1175);
|
||||
coap_notify_observers(&resource_sensors_dr1175);
|
||||
}
|
||||
/*****************************************************/
|
||||
/* Resource and handler to obtain light sensor value */
|
||||
|
@ -121,15 +121,15 @@ RESOURCE(resource_light_sensor_value,
|
|||
NULL,
|
||||
NULL);
|
||||
static void
|
||||
get_light_sensor_value_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
get_light_sensor_value_handler(coap_packet_t *request, coap_packet_t *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) {
|
||||
coap_get_header_accept(request, &accept);
|
||||
if(accept == -1 || accept == TEXT_PLAIN) {
|
||||
content_len = 0;
|
||||
CONTENT_PRINTF("%d", light_sensor.value(0));
|
||||
REST.set_header_content_type(response, REST.type.TEXT_PLAIN);
|
||||
REST.set_response_payload(response, (uint8_t *)content, content_len);
|
||||
coap_set_header_content_format(response, TEXT_PLAIN);
|
||||
coap_set_payload(response, (uint8_t *)content, content_len);
|
||||
}
|
||||
}
|
||||
/***************************************************/
|
||||
|
@ -142,15 +142,15 @@ RESOURCE(resource_light_sensor_unit,
|
|||
NULL,
|
||||
NULL);
|
||||
static void
|
||||
get_light_sensor_unit_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
get_light_sensor_unit_handler(coap_packet_t *request, coap_packet_t *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) {
|
||||
coap_get_header_accept(request, &accept);
|
||||
if(accept == -1 || accept == TEXT_PLAIN) {
|
||||
content_len = 0;
|
||||
CONTENT_PRINTF("Lux");
|
||||
REST.set_header_content_type(response, REST.type.TEXT_PLAIN);
|
||||
REST.set_response_payload(response, (uint8_t *)content, content_len);
|
||||
coap_set_header_content_format(response, TEXT_PLAIN);
|
||||
coap_set_payload(response, (uint8_t *)content, content_len);
|
||||
}
|
||||
}
|
||||
/***********************************************************/
|
||||
|
@ -163,15 +163,15 @@ RESOURCE(resource_temperature_value,
|
|||
NULL,
|
||||
NULL);
|
||||
static void
|
||||
get_temperature_value_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
get_temperature_value_handler(coap_packet_t *request, coap_packet_t *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) {
|
||||
coap_get_header_accept(request, &accept);
|
||||
if(accept == -1 || accept == TEXT_PLAIN) {
|
||||
content_len = 0;
|
||||
CONTENT_PRINTF("%d", ht_sensor.value(HT_SENSOR_TEMP));
|
||||
REST.set_header_content_type(response, REST.type.TEXT_PLAIN);
|
||||
REST.set_response_payload(response, (uint8_t *)content, content_len);
|
||||
coap_set_header_content_format(response, TEXT_PLAIN);
|
||||
coap_set_payload(response, (uint8_t *)content, content_len);
|
||||
}
|
||||
}
|
||||
/*********************************************************/
|
||||
|
@ -184,15 +184,15 @@ RESOURCE(resource_temperature_unit,
|
|||
NULL,
|
||||
NULL);
|
||||
static void
|
||||
get_temperature_unit_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
get_temperature_unit_handler(coap_packet_t *request, coap_packet_t *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) {
|
||||
coap_get_header_accept(request, &accept);
|
||||
if(accept == -1 || accept == TEXT_PLAIN) {
|
||||
content_len = 0;
|
||||
CONTENT_PRINTF("degrees C");
|
||||
REST.set_header_content_type(response, REST.type.TEXT_PLAIN);
|
||||
REST.set_response_payload(response, (uint8_t *)content, content_len);
|
||||
coap_set_header_content_format(response, TEXT_PLAIN);
|
||||
coap_set_payload(response, (uint8_t *)content, content_len);
|
||||
}
|
||||
}
|
||||
/********************************************************/
|
||||
|
@ -205,15 +205,15 @@ RESOURCE(resource_humidity_value,
|
|||
NULL,
|
||||
NULL);
|
||||
static void
|
||||
get_humidity_value_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
get_humidity_value_handler(coap_packet_t *request, coap_packet_t *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) {
|
||||
coap_get_header_accept(request, &accept);
|
||||
if(accept == -1 || accept == TEXT_PLAIN) {
|
||||
content_len = 0;
|
||||
CONTENT_PRINTF("%d", ht_sensor.value(HT_SENSOR_HUM));
|
||||
REST.set_header_content_type(response, REST.type.TEXT_PLAIN);
|
||||
REST.set_response_payload(response, (uint8_t *)content, content_len);
|
||||
coap_set_header_content_format(response, TEXT_PLAIN);
|
||||
coap_set_payload(response, (uint8_t *)content, content_len);
|
||||
}
|
||||
}
|
||||
/******************************************************/
|
||||
|
@ -226,15 +226,15 @@ RESOURCE(resource_humidity_unit,
|
|||
NULL,
|
||||
NULL);
|
||||
static void
|
||||
get_humidity_unit_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
get_humidity_unit_handler(coap_packet_t *request, coap_packet_t *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) {
|
||||
coap_get_header_accept(request, &accept);
|
||||
if(accept == -1 || accept == TEXT_PLAIN) {
|
||||
content_len = 0;
|
||||
CONTENT_PRINTF("relative %%");
|
||||
REST.set_header_content_type(response, REST.type.TEXT_PLAIN);
|
||||
REST.set_response_payload(response, (uint8_t *)content, content_len);
|
||||
coap_set_header_content_format(response, TEXT_PLAIN);
|
||||
coap_set_payload(response, (uint8_t *)content, content_len);
|
||||
}
|
||||
}
|
||||
/***************************************************/
|
||||
|
@ -247,15 +247,15 @@ RESOURCE(resource_white_led,
|
|||
put_post_white_led_handler,
|
||||
NULL);
|
||||
static void
|
||||
put_post_white_led_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
put_post_white_led_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
{
|
||||
const uint8_t *request_content = NULL;
|
||||
int level;
|
||||
|
||||
unsigned int accept = -1;
|
||||
REST.get_header_accept(request, &accept);
|
||||
if(accept == -1 || accept == REST.type.TEXT_PLAIN) {
|
||||
REST.get_request_payload(request, &request_content);
|
||||
coap_get_header_accept(request, &accept);
|
||||
if(accept == -1 || accept == TEXT_PLAIN) {
|
||||
coap_get_payload(request, &request_content);
|
||||
level = atoi((const char *)request_content);
|
||||
CLIP(level, 255)
|
||||
leds_set_level(level, LEDS_WHITE);
|
||||
|
@ -271,7 +271,7 @@ RESOURCE(resource_rgb_led,
|
|||
put_post_rgb_led_handler,
|
||||
NULL);
|
||||
static void
|
||||
put_post_rgb_led_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
put_post_rgb_led_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
{
|
||||
const uint8_t *request_content = NULL;
|
||||
char *pch;
|
||||
|
@ -279,9 +279,9 @@ put_post_rgb_led_handler(void *request, void *response, uint8_t *buffer, uint16_
|
|||
int index = 0;
|
||||
|
||||
unsigned int accept = -1;
|
||||
REST.get_header_accept(request, &accept);
|
||||
if(accept == -1 || accept == REST.type.TEXT_PLAIN) {
|
||||
REST.get_request_payload(request, &request_content);
|
||||
coap_get_header_accept(request, &accept);
|
||||
if(accept == -1 || accept == TEXT_PLAIN) {
|
||||
coap_get_payload(request, &request_content);
|
||||
pch = strtok((char *)request_content, " ");
|
||||
while((pch != NULL) && (index != sizeof(RGB) / sizeof(int))) {
|
||||
/* Convert token to int */
|
||||
|
@ -306,13 +306,13 @@ RESOURCE(resource_led_d3_1174,
|
|||
put_post_led_d3_1174_handler,
|
||||
NULL);
|
||||
static void
|
||||
put_post_led_d3_1174_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
put_post_led_d3_1174_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
{
|
||||
const uint8_t *request_content;
|
||||
unsigned int accept = -1;
|
||||
REST.get_header_accept(request, &accept);
|
||||
if(accept == -1 || accept == REST.type.TEXT_PLAIN) {
|
||||
REST.get_request_payload(request, &request_content);
|
||||
coap_get_header_accept(request, &accept);
|
||||
if(accept == -1 || accept == TEXT_PLAIN) {
|
||||
coap_get_payload(request, &request_content);
|
||||
SET_LED(LEDS_GP0);
|
||||
}
|
||||
}
|
||||
|
@ -326,13 +326,13 @@ RESOURCE(resource_led_d6_1174,
|
|||
put_post_led_d6_1174_handler,
|
||||
NULL);
|
||||
static void
|
||||
put_post_led_d6_1174_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
put_post_led_d6_1174_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
{
|
||||
const uint8_t *request_content;
|
||||
unsigned int accept = -1;
|
||||
REST.get_header_accept(request, &accept);
|
||||
if(accept == -1 || accept == REST.type.TEXT_PLAIN) {
|
||||
REST.get_request_payload(request, &request_content);
|
||||
coap_get_header_accept(request, &accept);
|
||||
if(accept == -1 || accept == TEXT_PLAIN) {
|
||||
coap_get_payload(request, &request_content);
|
||||
SET_LED(LEDS_GP1);
|
||||
}
|
||||
}
|
||||
|
@ -357,18 +357,18 @@ PROCESS_THREAD(start_app, ev, data)
|
|||
rpl_tools_init(NULL);
|
||||
} printf("Starting RPL node\n");
|
||||
|
||||
rest_init_engine();
|
||||
rest_activate_resource(&resource_light_sensor_value, "DR1175/LightSensor/Value");
|
||||
rest_activate_resource(&resource_light_sensor_unit, "DR1175/LightSensor/Unit");
|
||||
rest_activate_resource(&resource_temperature_unit, "DR1175/Temperature/Unit");
|
||||
rest_activate_resource(&resource_temperature_value, "DR1175/Temperature/Value");
|
||||
rest_activate_resource(&resource_humidity_unit, "DR1175/Humidity/Unit");
|
||||
rest_activate_resource(&resource_humidity_value, "DR1175/Humidity/Value");
|
||||
rest_activate_resource(&resource_white_led, "DR1175/WhiteLED");
|
||||
rest_activate_resource(&resource_rgb_led, "DR1175/ColorLED/RGBValue");
|
||||
rest_activate_resource(&resource_led_d3_1174, "DR1175/LED/D3On1174");
|
||||
rest_activate_resource(&resource_led_d6_1174, "DR1175/LED/D6On1174");
|
||||
rest_activate_resource(&resource_sensors_dr1175, "DR1175/AllSensors");
|
||||
coap_engine_init();
|
||||
coap_activate_resource(&resource_light_sensor_value, "DR1175/LightSensor/Value");
|
||||
coap_activate_resource(&resource_light_sensor_unit, "DR1175/LightSensor/Unit");
|
||||
coap_activate_resource(&resource_temperature_unit, "DR1175/Temperature/Unit");
|
||||
coap_activate_resource(&resource_temperature_value, "DR1175/Temperature/Value");
|
||||
coap_activate_resource(&resource_humidity_unit, "DR1175/Humidity/Unit");
|
||||
coap_activate_resource(&resource_humidity_value, "DR1175/Humidity/Value");
|
||||
coap_activate_resource(&resource_white_led, "DR1175/WhiteLED");
|
||||
coap_activate_resource(&resource_rgb_led, "DR1175/ColorLED/RGBValue");
|
||||
coap_activate_resource(&resource_led_d3_1174, "DR1175/LED/D3On1174");
|
||||
coap_activate_resource(&resource_led_d6_1174, "DR1175/LED/D6On1174");
|
||||
coap_activate_resource(&resource_sensors_dr1175, "DR1175/AllSensors");
|
||||
|
||||
/* Level of LEDS=0, so no light after start-up */
|
||||
leds_on(LEDS_WHITE | LEDS_RED | LEDS_GREEN | LEDS_BLUE);
|
||||
|
|
|
@ -35,30 +35,30 @@
|
|||
#include "net/ipv6/uip.h"
|
||||
#include "net/ipv6/uip-ds6.h"
|
||||
#include "tools/rpl-tools.h"
|
||||
#include "rest-engine.h"
|
||||
#include "coap-engine.h"
|
||||
#include "dev/leds.h"
|
||||
#include "button-sensor.h"
|
||||
#include "pot-sensor.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
static char content[REST_MAX_CHUNK_SIZE];
|
||||
static char content[COAP_MAX_CHUNK_SIZE];
|
||||
static int content_len = 0;
|
||||
|
||||
static void event_sensors_dr1199_handler();
|
||||
static void get_sensors_dr1199_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
static void get_switch_sw1_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
static void get_switch_sw2_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
static void get_switch_sw3_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
static void get_switch_sw4_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
static void get_switch_dio8_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
static void get_pot_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
static void put_post_led_d1_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
static void put_post_led_d2_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
static void put_post_led_d3_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
static void put_post_led_d3_1174_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
static void put_post_led_d6_1174_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
static void put_post_led_all_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
static void get_sensors_dr1199_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
static void get_switch_sw1_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
static void get_switch_sw2_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
static void get_switch_sw3_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
static void get_switch_sw4_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
static void get_switch_dio8_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
static void get_pot_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
static void put_post_led_d1_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
static void put_post_led_d2_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
static void put_post_led_d3_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
static void put_post_led_d3_1174_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
static void put_post_led_d6_1174_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
static void put_post_led_all_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
|
||||
#define CONTENT_PRINTF(...) { if(content_len < sizeof(content)) { content_len += snprintf(content + content_len, sizeof(content) - content_len, __VA_ARGS__); } }
|
||||
|
||||
|
@ -93,25 +93,25 @@ EVENT_RESOURCE(resource_sensors_dr1199, /* name */
|
|||
NULL, /* DELETE handler */
|
||||
event_sensors_dr1199_handler); /* event handler */
|
||||
static void
|
||||
get_sensors_dr1199_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
get_sensors_dr1199_handler(coap_packet_t *request, coap_packet_t *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.APPLICATION_JSON) {
|
||||
coap_get_header_accept(request, &accept);
|
||||
if(accept == -1 || accept == APPLICATION_JSON) {
|
||||
content_len = 0;
|
||||
CONTENT_PRINTF("{\"DR1199\":[");
|
||||
CONTENT_PRINTF("{\"Switch\":\"0x%X\"},", button_sensor.value(0));
|
||||
CONTENT_PRINTF("{\"Pot\":\"%d\"}", pot_sensor.value(0));
|
||||
CONTENT_PRINTF("]}");
|
||||
REST.set_header_content_type(response, REST.type.APPLICATION_JSON);
|
||||
REST.set_response_payload(response, (uint8_t *)content, content_len);
|
||||
coap_set_header_content_format(response, APPLICATION_JSON);
|
||||
coap_set_payload(response, (uint8_t *)content, content_len);
|
||||
}
|
||||
}
|
||||
static void
|
||||
event_sensors_dr1199_handler()
|
||||
{
|
||||
/* Registered observers are notified and will trigger the GET handler to create the response. */
|
||||
REST.notify_subscribers(&resource_sensors_dr1199);
|
||||
coap_notify_observers(&resource_sensors_dr1199);
|
||||
}
|
||||
/***********************************************/
|
||||
/* Resource and handler to obtain switch value */
|
||||
|
@ -123,15 +123,15 @@ RESOURCE(resource_switch_sw1,
|
|||
NULL,
|
||||
NULL);
|
||||
static void
|
||||
get_switch_sw1_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
get_switch_sw1_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
{
|
||||
content_len = 0;
|
||||
unsigned int accept = -1;
|
||||
REST.get_header_accept(request, &accept);
|
||||
if(accept == -1 || accept == REST.type.TEXT_PLAIN) {
|
||||
coap_get_header_accept(request, &accept);
|
||||
if(accept == -1 || accept == TEXT_PLAIN) {
|
||||
PARSE_SWITCH(1)
|
||||
REST.set_header_content_type(response, REST.type.TEXT_PLAIN);
|
||||
REST.set_response_payload(response, (uint8_t *)content, content_len);
|
||||
coap_set_header_content_format(response, TEXT_PLAIN);
|
||||
coap_set_payload(response, (uint8_t *)content, content_len);
|
||||
}
|
||||
}
|
||||
RESOURCE(resource_switch_sw2,
|
||||
|
@ -141,15 +141,15 @@ RESOURCE(resource_switch_sw2,
|
|||
NULL,
|
||||
NULL);
|
||||
static void
|
||||
get_switch_sw2_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
get_switch_sw2_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
{
|
||||
content_len = 0;
|
||||
unsigned int accept = -1;
|
||||
REST.get_header_accept(request, &accept);
|
||||
if(accept == -1 || accept == REST.type.TEXT_PLAIN) {
|
||||
coap_get_header_accept(request, &accept);
|
||||
if(accept == -1 || accept == TEXT_PLAIN) {
|
||||
PARSE_SWITCH(2)
|
||||
REST.set_header_content_type(response, REST.type.TEXT_PLAIN);
|
||||
REST.set_response_payload(response, (uint8_t *)content, content_len);
|
||||
coap_set_header_content_format(response, TEXT_PLAIN);
|
||||
coap_set_payload(response, (uint8_t *)content, content_len);
|
||||
}
|
||||
}
|
||||
RESOURCE(resource_switch_sw3,
|
||||
|
@ -159,15 +159,15 @@ RESOURCE(resource_switch_sw3,
|
|||
NULL,
|
||||
NULL);
|
||||
static void
|
||||
get_switch_sw3_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
get_switch_sw3_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
{
|
||||
content_len = 0;
|
||||
unsigned int accept = -1;
|
||||
REST.get_header_accept(request, &accept);
|
||||
if(accept == -1 || accept == REST.type.TEXT_PLAIN) {
|
||||
coap_get_header_accept(request, &accept);
|
||||
if(accept == -1 || accept == TEXT_PLAIN) {
|
||||
PARSE_SWITCH(3)
|
||||
REST.set_header_content_type(response, REST.type.TEXT_PLAIN);
|
||||
REST.set_response_payload(response, (uint8_t *)content, content_len);
|
||||
coap_set_header_content_format(response, TEXT_PLAIN);
|
||||
coap_set_payload(response, (uint8_t *)content, content_len);
|
||||
}
|
||||
}
|
||||
RESOURCE(resource_switch_sw4,
|
||||
|
@ -177,15 +177,15 @@ RESOURCE(resource_switch_sw4,
|
|||
NULL,
|
||||
NULL);
|
||||
static void
|
||||
get_switch_sw4_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
get_switch_sw4_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
{
|
||||
content_len = 0;
|
||||
unsigned int accept = -1;
|
||||
REST.get_header_accept(request, &accept);
|
||||
if(accept == -1 || accept == REST.type.TEXT_PLAIN) {
|
||||
coap_get_header_accept(request, &accept);
|
||||
if(accept == -1 || accept == TEXT_PLAIN) {
|
||||
PARSE_SWITCH(4)
|
||||
REST.set_header_content_type(response, REST.type.TEXT_PLAIN);
|
||||
REST.set_response_payload(response, (uint8_t *)content, content_len);
|
||||
coap_set_header_content_format(response, TEXT_PLAIN);
|
||||
coap_set_payload(response, (uint8_t *)content, content_len);
|
||||
}
|
||||
}
|
||||
RESOURCE(resource_switch_dio8,
|
||||
|
@ -195,15 +195,15 @@ RESOURCE(resource_switch_dio8,
|
|||
NULL,
|
||||
NULL);
|
||||
static void
|
||||
get_switch_dio8_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
get_switch_dio8_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
{
|
||||
content_len = 0;
|
||||
unsigned int accept = -1;
|
||||
REST.get_header_accept(request, &accept);
|
||||
if(accept == -1 || accept == REST.type.TEXT_PLAIN) {
|
||||
coap_get_header_accept(request, &accept);
|
||||
if(accept == -1 || accept == TEXT_PLAIN) {
|
||||
PARSE_SWITCH(0)
|
||||
REST.set_header_content_type(response, REST.type.TEXT_PLAIN);
|
||||
REST.set_response_payload(response, (uint8_t *)content, content_len);
|
||||
coap_set_header_content_format(response, TEXT_PLAIN);
|
||||
coap_set_payload(response, (uint8_t *)content, content_len);
|
||||
}
|
||||
}
|
||||
/*******************************************************/
|
||||
|
@ -216,15 +216,15 @@ RESOURCE(resource_pot,
|
|||
NULL,
|
||||
NULL);
|
||||
static void
|
||||
get_pot_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
get_pot_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
{
|
||||
content_len = 0;
|
||||
unsigned int accept = -1;
|
||||
REST.get_header_accept(request, &accept);
|
||||
if(accept == -1 || accept == REST.type.TEXT_PLAIN) {
|
||||
coap_get_header_accept(request, &accept);
|
||||
if(accept == -1 || accept == TEXT_PLAIN) {
|
||||
CONTENT_PRINTF("%d", pot_sensor.value(0));
|
||||
REST.set_header_content_type(response, REST.type.TEXT_PLAIN);
|
||||
REST.set_response_payload(response, (uint8_t *)content, content_len);
|
||||
coap_set_header_content_format(response, TEXT_PLAIN);
|
||||
coap_set_payload(response, (uint8_t *)content, content_len);
|
||||
}
|
||||
}
|
||||
/************************************/
|
||||
|
@ -237,13 +237,13 @@ RESOURCE(resource_led_d1,
|
|||
put_post_led_d1_handler,
|
||||
NULL);
|
||||
static void
|
||||
put_post_led_d1_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
put_post_led_d1_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
{
|
||||
const uint8_t *request_content;
|
||||
unsigned int accept = -1;
|
||||
REST.get_header_accept(request, &accept);
|
||||
if(accept == -1 || accept == REST.type.TEXT_PLAIN) {
|
||||
REST.get_request_payload(request, &request_content);
|
||||
coap_get_header_accept(request, &accept);
|
||||
if(accept == -1 || accept == TEXT_PLAIN) {
|
||||
coap_get_payload(request, &request_content);
|
||||
SET_LED(LEDS_GREEN)
|
||||
}
|
||||
}
|
||||
|
@ -254,13 +254,13 @@ RESOURCE(resource_led_d2,
|
|||
put_post_led_d2_handler,
|
||||
NULL);
|
||||
static void
|
||||
put_post_led_d2_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
put_post_led_d2_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
{
|
||||
const uint8_t *request_content;
|
||||
unsigned int accept = -1;
|
||||
REST.get_header_accept(request, &accept);
|
||||
if(accept == -1 || accept == REST.type.TEXT_PLAIN) {
|
||||
REST.get_request_payload(request, &request_content);
|
||||
coap_get_header_accept(request, &accept);
|
||||
if(accept == -1 || accept == TEXT_PLAIN) {
|
||||
coap_get_payload(request, &request_content);
|
||||
SET_LED(LEDS_BLUE)
|
||||
}
|
||||
}
|
||||
|
@ -271,13 +271,13 @@ RESOURCE(resource_led_d3,
|
|||
put_post_led_d3_handler,
|
||||
NULL);
|
||||
static void
|
||||
put_post_led_d3_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
put_post_led_d3_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
{
|
||||
const uint8_t *request_content;
|
||||
unsigned int accept = -1;
|
||||
REST.get_header_accept(request, &accept);
|
||||
if(accept == -1 || accept == REST.type.TEXT_PLAIN) {
|
||||
REST.get_request_payload(request, &request_content);
|
||||
coap_get_header_accept(request, &accept);
|
||||
if(accept == -1 || accept == TEXT_PLAIN) {
|
||||
coap_get_payload(request, &request_content);
|
||||
SET_LED(LEDS_RED)
|
||||
}
|
||||
}
|
||||
|
@ -288,13 +288,13 @@ RESOURCE(resource_led_d3_1174,
|
|||
put_post_led_d3_1174_handler,
|
||||
NULL);
|
||||
static void
|
||||
put_post_led_d3_1174_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
put_post_led_d3_1174_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
{
|
||||
const uint8_t *request_content;
|
||||
unsigned int accept = -1;
|
||||
REST.get_header_accept(request, &accept);
|
||||
if(accept == -1 || accept == REST.type.TEXT_PLAIN) {
|
||||
REST.get_request_payload(request, &request_content);
|
||||
coap_get_header_accept(request, &accept);
|
||||
if(accept == -1 || accept == TEXT_PLAIN) {
|
||||
coap_get_payload(request, &request_content);
|
||||
SET_LED(LEDS_GP0);
|
||||
}
|
||||
}
|
||||
|
@ -305,13 +305,13 @@ RESOURCE(resource_led_d6_1174,
|
|||
put_post_led_d6_1174_handler,
|
||||
NULL);
|
||||
static void
|
||||
put_post_led_d6_1174_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
put_post_led_d6_1174_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
{
|
||||
const uint8_t *request_content;
|
||||
unsigned int accept = -1;
|
||||
REST.get_header_accept(request, &accept);
|
||||
if(accept == -1 || accept == REST.type.TEXT_PLAIN) {
|
||||
REST.get_request_payload(request, &request_content);
|
||||
coap_get_header_accept(request, &accept);
|
||||
if(accept == -1 || accept == TEXT_PLAIN) {
|
||||
coap_get_payload(request, &request_content);
|
||||
SET_LED(LEDS_GP1);
|
||||
}
|
||||
}
|
||||
|
@ -322,13 +322,13 @@ RESOURCE(resource_led_all,
|
|||
put_post_led_all_handler,
|
||||
NULL);
|
||||
static void
|
||||
put_post_led_all_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
put_post_led_all_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
{
|
||||
const uint8_t *request_content;
|
||||
unsigned int accept = -1;
|
||||
REST.get_header_accept(request, &accept);
|
||||
if(accept == -1 || accept == REST.type.TEXT_PLAIN) {
|
||||
REST.get_request_payload(request, &request_content);
|
||||
coap_get_header_accept(request, &accept);
|
||||
if(accept == -1 || accept == TEXT_PLAIN) {
|
||||
coap_get_payload(request, &request_content);
|
||||
if(atoi((const char *)request_content) != 0) {
|
||||
leds_on(LEDS_ALL);
|
||||
} else {
|
||||
|
@ -358,20 +358,20 @@ PROCESS_THREAD(start_app, ev, data)
|
|||
rpl_tools_init(NULL);
|
||||
} printf("Starting RPL node\n");
|
||||
|
||||
rest_init_engine();
|
||||
rest_activate_resource(&resource_switch_sw1, "DR1199/Switch/SW1");
|
||||
rest_activate_resource(&resource_switch_sw2, "DR1199/Switch/SW2");
|
||||
rest_activate_resource(&resource_switch_sw3, "DR1199/Switch/SW3");
|
||||
rest_activate_resource(&resource_switch_sw4, "DR1199/Switch/SW4");
|
||||
rest_activate_resource(&resource_switch_dio8, "DR1199/Switch/DIO8");
|
||||
rest_activate_resource(&resource_pot, "DR1199/Potentiometer");
|
||||
rest_activate_resource(&resource_led_d1, "DR1199/LED/D1");
|
||||
rest_activate_resource(&resource_led_d2, "DR1199/LED/D2");
|
||||
rest_activate_resource(&resource_led_d3, "DR1199/LED/D3");
|
||||
rest_activate_resource(&resource_led_d3_1174, "DR1199/LED/D3On1174");
|
||||
rest_activate_resource(&resource_led_d6_1174, "DR1199/LED/D6On1174");
|
||||
rest_activate_resource(&resource_led_all, "DR1199/LED/All");
|
||||
rest_activate_resource(&resource_sensors_dr1199, "DR1199/AllSensors");
|
||||
coap_engine_init();
|
||||
coap_activate_resource(&resource_switch_sw1, "DR1199/Switch/SW1");
|
||||
coap_activate_resource(&resource_switch_sw2, "DR1199/Switch/SW2");
|
||||
coap_activate_resource(&resource_switch_sw3, "DR1199/Switch/SW3");
|
||||
coap_activate_resource(&resource_switch_sw4, "DR1199/Switch/SW4");
|
||||
coap_activate_resource(&resource_switch_dio8, "DR1199/Switch/DIO8");
|
||||
coap_activate_resource(&resource_pot, "DR1199/Potentiometer");
|
||||
coap_activate_resource(&resource_led_d1, "DR1199/LED/D1");
|
||||
coap_activate_resource(&resource_led_d2, "DR1199/LED/D2");
|
||||
coap_activate_resource(&resource_led_d3, "DR1199/LED/D3");
|
||||
coap_activate_resource(&resource_led_d3_1174, "DR1199/LED/D3On1174");
|
||||
coap_activate_resource(&resource_led_d6_1174, "DR1199/LED/D6On1174");
|
||||
coap_activate_resource(&resource_led_all, "DR1199/LED/All");
|
||||
coap_activate_resource(&resource_sensors_dr1199, "DR1199/AllSensors");
|
||||
/* If sensor process generates an event, call event_handler of resource.
|
||||
This will make this resource observable by the client */
|
||||
while(1) {
|
||||
|
@ -382,4 +382,3 @@ PROCESS_THREAD(start_app, ev, data)
|
|||
|
||||
PROCESS_END();
|
||||
}
|
||||
|
||||
|
|
|
@ -37,15 +37,15 @@
|
|||
#include "net/ipv6/uip.h"
|
||||
#include "net/linkaddr.h"
|
||||
#include "rpl-tools.h"
|
||||
#include "rest-engine.h"
|
||||
#include "coap-engine.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <AppHardwareApi.h>
|
||||
|
||||
static void set_tx_power_handler(void* request, void* response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
static void get_tx_power_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
static void set_tx_power_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
static void get_tx_power_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
|
||||
static char content[REST_MAX_CHUNK_SIZE];
|
||||
static char content[COAP_MAX_CHUNK_SIZE];
|
||||
static int content_len = 0;
|
||||
|
||||
#define CONTENT_PRINTF(...) { if(content_len < sizeof(content)) content_len += snprintf(content+content_len, sizeof(content)-content_len, __VA_ARGS__); }
|
||||
|
@ -63,15 +63,15 @@ RESOURCE(resource_set_tx_power,
|
|||
set_tx_power_handler,
|
||||
NULL);
|
||||
static void
|
||||
set_tx_power_handler(void* request, void* response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
set_tx_power_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
{
|
||||
const uint8_t *request_content = NULL;
|
||||
int tx_level;
|
||||
|
||||
unsigned int accept = -1;
|
||||
REST.get_header_accept(request, &accept);
|
||||
if(accept == -1 || accept == REST.type.TEXT_PLAIN) {
|
||||
REST.get_request_payload(request, &request_content);
|
||||
coap_get_header_accept(request, &accept);
|
||||
if(accept == -1 || accept == TEXT_PLAIN) {
|
||||
coap_get_payload(request, &request_content);
|
||||
tx_level = atoi((const char *)request_content);
|
||||
NETSTACK_RADIO.set_value(RADIO_PARAM_TXPOWER, tx_level);
|
||||
}
|
||||
|
@ -84,17 +84,17 @@ RESOURCE(resource_get_tx_power,
|
|||
NULL,
|
||||
NULL);
|
||||
static void
|
||||
get_tx_power_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
get_tx_power_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
{
|
||||
int tx_level;
|
||||
unsigned int accept = -1;
|
||||
REST.get_header_accept(request, &accept);
|
||||
if(accept == -1 || accept == REST.type.TEXT_PLAIN) {
|
||||
coap_get_header_accept(request, &accept);
|
||||
if(accept == -1 || accept == TEXT_PLAIN) {
|
||||
content_len = 0;
|
||||
NETSTACK_RADIO.get_value(RADIO_PARAM_TXPOWER, &tx_level);
|
||||
CONTENT_PRINTF("%d", tx_level);
|
||||
REST.set_header_content_type(response, REST.type.TEXT_PLAIN);
|
||||
REST.set_response_payload(response, (uint8_t *)content, content_len);
|
||||
coap_set_header_content_format(response, TEXT_PLAIN);
|
||||
coap_set_payload(response, (uint8_t *)content, content_len);
|
||||
}
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
|
@ -113,9 +113,9 @@ PROCESS_THREAD(start_app, ev, data)
|
|||
}
|
||||
printf("Starting RPL node\n");
|
||||
|
||||
rest_init_engine();
|
||||
rest_activate_resource(&resource_set_tx_power, "Set-TX-Power");
|
||||
rest_activate_resource(&resource_get_tx_power, "Get-TX-Power");
|
||||
coap_engine_init();
|
||||
coap_activate_resource(&resource_set_tx_power, "Set-TX-Power");
|
||||
coap_activate_resource(&resource_get_tx_power, "Get-TX-Power");
|
||||
|
||||
PROCESS_END();
|
||||
}
|
||||
|
|
|
@ -42,7 +42,7 @@
|
|||
#include "net/mac/tsch/tsch-schedule.h"
|
||||
#include "net/netstack.h"
|
||||
#include "dev/slip.h"
|
||||
#include "rest-engine.h"
|
||||
#include "coap-engine.h"
|
||||
#include "rpl-tools.h"
|
||||
|
||||
#include <stdio.h>
|
||||
|
@ -56,10 +56,10 @@
|
|||
static uip_ipaddr_t prefix;
|
||||
static uint8_t prefix_set;
|
||||
|
||||
static void get_rssi_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
static void get_last_rssi_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
static void get_rssi_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
static void get_last_rssi_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
|
||||
static char content[REST_MAX_CHUNK_SIZE];
|
||||
static char content[COAP_MAX_CHUNK_SIZE];
|
||||
static int content_len = 0;
|
||||
|
||||
#define CONTENT_PRINTF(...) { if(content_len < sizeof(content)) content_len += snprintf(content+content_len, sizeof(content)-content_len, __VA_ARGS__); }
|
||||
|
@ -74,17 +74,17 @@ RESOURCE(resource_get_rssi,
|
|||
NULL,
|
||||
NULL);
|
||||
static void
|
||||
get_rssi_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
get_rssi_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
{
|
||||
int rssi_level;
|
||||
unsigned int accept = -1;
|
||||
REST.get_header_accept(request, &accept);
|
||||
if(accept == -1 || accept == REST.type.TEXT_PLAIN) {
|
||||
coap_get_header_accept(request, &accept);
|
||||
if(accept == -1 || accept == TEXT_PLAIN) {
|
||||
content_len = 0;
|
||||
NETSTACK_RADIO.get_value(RADIO_PARAM_RSSI, &rssi_level);
|
||||
CONTENT_PRINTF("%d", rssi_level);
|
||||
REST.set_header_content_type(response, REST.type.TEXT_PLAIN);
|
||||
REST.set_response_payload(response, (uint8_t *)content, content_len);
|
||||
coap_set_header_content_format(response, TEXT_PLAIN);
|
||||
coap_set_payload(response, (uint8_t *)content, content_len);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -95,17 +95,17 @@ RESOURCE(resource_get_last_rssi,
|
|||
NULL,
|
||||
NULL);
|
||||
static void
|
||||
get_last_rssi_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
get_last_rssi_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
{
|
||||
int last_rssi_level;
|
||||
unsigned int accept = -1;
|
||||
REST.get_header_accept(request, &accept);
|
||||
if(accept == -1 || accept == REST.type.TEXT_PLAIN) {
|
||||
coap_get_header_accept(request, &accept);
|
||||
if(accept == -1 || accept == TEXT_PLAIN) {
|
||||
content_len = 0;
|
||||
NETSTACK_RADIO.get_value(RADIO_PARAM_LAST_RSSI, &last_rssi_level);
|
||||
CONTENT_PRINTF("%d", last_rssi_level);
|
||||
REST.set_header_content_type(response, REST.type.TEXT_PLAIN);
|
||||
REST.set_response_payload(response, (uint8_t *)content, content_len);
|
||||
coap_set_header_content_format(response, TEXT_PLAIN);
|
||||
coap_set_payload(response, (uint8_t *)content, content_len);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -159,9 +159,9 @@ PROCESS_THREAD(border_router_process, ev, data)
|
|||
|
||||
rpl_tools_init(&prefix);
|
||||
|
||||
rest_init_engine();
|
||||
rest_activate_resource(&resource_get_rssi, "Get-RSSI");
|
||||
rest_activate_resource(&resource_get_last_rssi, "Get-Last-RSSI");
|
||||
coap_engine_init();
|
||||
coap_activate_resource(&resource_get_rssi, "Get-RSSI");
|
||||
coap_activate_resource(&resource_get_last_rssi, "Get-Last-RSSI");
|
||||
|
||||
|
||||
PROCESS_END();
|
||||
|
|
|
@ -36,22 +36,22 @@
|
|||
#include "net/ipv6/uip.h"
|
||||
#include "net/linkaddr.h"
|
||||
#include "rpl-tools.h"
|
||||
#include "rest-engine.h"
|
||||
#include "coap-engine.h"
|
||||
#include "sys/ctimer.h"
|
||||
#include "dev/uart-driver.h"
|
||||
#include "uart1.h"
|
||||
#include <AppHardwareApi.h>
|
||||
|
||||
static void get_coap_rx_uart1_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
static void get_coap_rx_uart1_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
static void event_coap_rx_uart1_handler(void);
|
||||
static void put_post_tx_uart1_handler(void* request, void* response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
static void put_post_tx_uart1_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
|
||||
static void string2uart1(uint8_t *c);
|
||||
static int handleRxChar(uint8_t c);
|
||||
static int get_ringbuf(uint8_t *c);
|
||||
static int put_ringbuf(uint8_t c);
|
||||
|
||||
/* COAP helpers */
|
||||
static char content[REST_MAX_CHUNK_SIZE];
|
||||
static char content[COAP_MAX_CHUNK_SIZE];
|
||||
static int content_len = 0;
|
||||
#define CONTENT_PRINTF(...) { if(content_len < sizeof(content)) content_len += snprintf(content+content_len, sizeof(content)-content_len, __VA_ARGS__); }
|
||||
|
||||
|
@ -85,15 +85,15 @@ EVENT_RESOURCE(resource_coap_rx_uart1, /* name */
|
|||
NULL, /* DELETE handler */
|
||||
event_coap_rx_uart1_handler); /* event handler */
|
||||
static void
|
||||
get_coap_rx_uart1_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
get_coap_rx_uart1_handler(coap_packet_t *request, coap_packet_t *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) {
|
||||
coap_get_header_accept(request, &accept);
|
||||
if(accept == -1 || accept == TEXT_PLAIN) {
|
||||
content_len = 0;
|
||||
CONTENT_PRINTF("%s", rx_buf);
|
||||
REST.set_header_content_type(response, REST.type.TEXT_PLAIN);
|
||||
REST.set_response_payload(response, (uint8_t *)content, content_len);
|
||||
coap_set_header_content_format(response, TEXT_PLAIN);
|
||||
coap_set_payload(response, (uint8_t *)content, content_len);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -101,7 +101,7 @@ static void
|
|||
event_coap_rx_uart1_handler(void)
|
||||
{
|
||||
/* Registered observers are notified and will trigger the GET handler to create the response. */
|
||||
REST.notify_subscribers(&resource_coap_rx_uart1);
|
||||
coap_notify_observers(&resource_coap_rx_uart1);
|
||||
}
|
||||
|
||||
/*****************************************************************************/
|
||||
|
@ -115,14 +115,14 @@ RESOURCE(resource_coap_tx_uart1, /* name */
|
|||
NULL); /* DELETE handler */
|
||||
|
||||
static void
|
||||
put_post_tx_uart1_handler(void* request, void* response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
put_post_tx_uart1_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
{
|
||||
const uint8_t *request_content = NULL;
|
||||
unsigned int accept = -1;
|
||||
|
||||
REST.get_header_accept(request, &accept);
|
||||
if(accept == -1 || accept == REST.type.TEXT_PLAIN) {
|
||||
REST.get_request_payload(request, &request_content);
|
||||
coap_get_header_accept(request, &accept);
|
||||
if(accept == -1 || accept == TEXT_PLAIN) {
|
||||
coap_get_payload(request, &request_content);
|
||||
string2uart1((uint8_t *)request_content);
|
||||
}
|
||||
}
|
||||
|
@ -154,9 +154,9 @@ PROCESS_THREAD(start_app, ev, data)
|
|||
}
|
||||
printf("Starting RPL node\n");
|
||||
|
||||
rest_init_engine();
|
||||
rest_activate_resource(&resource_coap_rx_uart1, "UART1-RX");
|
||||
rest_activate_resource(&resource_coap_tx_uart1, "UART1-TX");
|
||||
coap_engine_init();
|
||||
coap_activate_resource(&resource_coap_rx_uart1, "UART1-RX");
|
||||
coap_activate_resource(&resource_coap_tx_uart1, "UART1-TX");
|
||||
|
||||
PROCESS_END();
|
||||
}
|
||||
|
@ -256,4 +256,3 @@ put_ringbuf(uint8_t c)
|
|||
uart1_enable_interrupts();
|
||||
return return_val;
|
||||
}
|
||||
|
||||
|
|
|
@ -5,10 +5,10 @@ $(error Please specify whether coap-client or coap-server should be built)
|
|||
endif
|
||||
|
||||
ifneq ($(filter coap-client coap-client.flash, $(MAKECMDGOALS)),)
|
||||
ifeq ($(SERVER_IPV6_ADDR),)
|
||||
$(error Please define SERVER_IPV6_ADDR=<full ipv6 addr>)
|
||||
ifeq ($(SERVER_IPV6_EP),)
|
||||
$(error Please define SERVER_IPV6_EP=<full-coap-IPv6-URI>)
|
||||
else
|
||||
CFLAGS += -DSERVER_IPV6_ADDR=\"$(SERVER_IPV6_ADDR)\"
|
||||
CFLAGS += -DSERVER_IPV6_EP=\"$(SERVER_IPV6_EP)\"
|
||||
CFLAGS += -DDEVICE_NAME=\"nRF52_DK_CoAP_Client\"
|
||||
endif
|
||||
else
|
||||
|
|
|
@ -51,6 +51,7 @@
|
|||
#include "contiki.h"
|
||||
#include "contiki-net.h"
|
||||
#include "coap-engine.h"
|
||||
#include "coap-endpoint.h"
|
||||
#include "dev/button-sensor.h"
|
||||
#include "dev/leds.h"
|
||||
|
||||
|
@ -63,13 +64,11 @@
|
|||
#endif
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
#define REMOTE_PORT UIP_HTONS(COAP_DEFAULT_PORT)
|
||||
#define OBS_RESOURCE_URI "lights/led3"
|
||||
#define SUBS_LED LEDS_4
|
||||
#define OBS_LED LEDS_3
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
static uip_ipaddr_t server_ipaddr[1]; /* holds the server ip address */
|
||||
static coap_observee_t *obs;
|
||||
static struct ctimer ct;
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
@ -146,11 +145,11 @@ notification_callback(coap_observee_t *obs, void *notification,
|
|||
PROCESS_THREAD(er_example_observe_client, ev, data)
|
||||
{
|
||||
PROCESS_BEGIN();
|
||||
|
||||
uiplib_ipaddrconv(SERVER_IPV6_ADDR, server_ipaddr);
|
||||
static coap_endpoint_t server_endpoint;
|
||||
coap_endpoint_parse(SERVER_IPV6_EP, strlen(SERVER_IPV6_EP), &server_endpoint);
|
||||
|
||||
/* receives all CoAP messages */
|
||||
coap_init_engine();
|
||||
coap_engine_init();
|
||||
|
||||
#if PLATFORM_HAS_BUTTON
|
||||
SENSORS_ACTIVATE(button_1);
|
||||
|
@ -164,7 +163,7 @@ PROCESS_THREAD(er_example_observe_client, ev, data)
|
|||
if (ev == sensors_event) {
|
||||
if (data == &button_1 && button_1.value(BUTTON_SENSOR_VALUE_STATE) == 0) {
|
||||
PRINTF("Starting observation\n");
|
||||
obs = coap_obs_request_registration(server_ipaddr, REMOTE_PORT,
|
||||
obs = coap_obs_request_registration(&server_endpoint,
|
||||
OBS_RESOURCE_URI, notification_callback,
|
||||
NULL);
|
||||
}
|
||||
|
|
|
@ -51,7 +51,7 @@
|
|||
#include <string.h>
|
||||
#include "contiki.h"
|
||||
#include "contiki-net.h"
|
||||
#include "rest-engine.h"
|
||||
#include "coap-engine.h"
|
||||
#include "uip.h"
|
||||
#include "dev/button-sensor.h"
|
||||
#include "dev/leds.h"
|
||||
|
@ -63,7 +63,7 @@
|
|||
* Resources to be activated need to be imported through the extern keyword.
|
||||
* The build system automatically compiles the resources in the corresponding sub-directory.
|
||||
*/
|
||||
extern resource_t res_led3;
|
||||
extern coap_resource_t res_led3;
|
||||
|
||||
static void
|
||||
print_local_addresses(void)
|
||||
|
@ -99,13 +99,13 @@ PROCESS_THREAD(er_example_server, ev, data)
|
|||
PRINTF("uIP buffer: %u\n", UIP_BUFSIZE);
|
||||
PRINTF("LL header: %u\n", UIP_LLH_LEN);
|
||||
PRINTF("IP+UDP header: %u\n", UIP_IPUDPH_LEN);
|
||||
PRINTF("REST max chunk: %u\n", REST_MAX_CHUNK_SIZE);
|
||||
PRINTF("REST max chunk: %u\n", COAP_MAX_CHUNK_SIZE);
|
||||
|
||||
print_local_addresses();
|
||||
|
||||
/* Initialize the REST engine. */
|
||||
rest_init_engine();
|
||||
rest_activate_resource(&res_led3, "lights/led3");
|
||||
coap_engine_init();
|
||||
coap_activate_resource(&res_led3, "lights/led3");
|
||||
|
||||
SENSORS_ACTIVATE(button_1);
|
||||
|
||||
|
|
|
@ -40,22 +40,25 @@
|
|||
|
||||
#include <string.h>
|
||||
#include "contiki.h"
|
||||
#include "rest-engine.h"
|
||||
#include "coap.h"
|
||||
#include "coap-engine.h"
|
||||
#include "dev/leds.h"
|
||||
|
||||
#define DEBUG DEBUG_NONE
|
||||
#include "net/ipv6/uip-debug.h"
|
||||
|
||||
static void
|
||||
res_post_put_handler(void *request, void *response, uint8_t *buffer,
|
||||
res_post_put_handler(coap_packet_t *request, coap_packet_t *response,
|
||||
uint8_t *buffer,
|
||||
uint16_t preferred_size, int32_t *offset);
|
||||
|
||||
static void
|
||||
res_get_handler(void *request, void *response, uint8_t *buffer,
|
||||
res_get_handler(coap_packet_t *request, coap_packet_t *response,
|
||||
uint8_t *buffer,
|
||||
uint16_t preferred_size, int32_t *offset);
|
||||
|
||||
static void
|
||||
res_event_handler();
|
||||
res_event_handler(void);
|
||||
|
||||
/*A simple actuator example, depending on the color query parameter and post variable mode, corresponding led is activated or deactivated*/
|
||||
EVENT_RESOURCE(res_led3,
|
||||
|
@ -68,30 +71,30 @@ EVENT_RESOURCE(res_led3,
|
|||
);
|
||||
|
||||
static void
|
||||
res_post_put_handler(void *request, void *response, uint8_t *buffer,
|
||||
uint16_t preferred_size, int32_t *offset)
|
||||
res_post_put_handler(coap_packet_t *request, coap_packet_t *response,
|
||||
uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
{
|
||||
const uint8_t *payload;
|
||||
REST.get_request_payload(request, &payload);
|
||||
coap_get_payload(request, &payload);
|
||||
|
||||
if (*payload == '0' || *payload == '1') {
|
||||
if (*payload == '1') {
|
||||
if(*payload == '0' || *payload == '1') {
|
||||
if(*payload == '1') {
|
||||
leds_on(LEDS_3);
|
||||
} else {
|
||||
leds_off(LEDS_3);
|
||||
}
|
||||
REST.notify_subscribers(&res_led3);
|
||||
REST.set_response_status(response, REST.status.CHANGED);
|
||||
coap_notify_observers(&res_led3);
|
||||
coap_set_status_code(response, CHANGED_2_04);
|
||||
} else {
|
||||
REST.set_response_status(response, REST.status.BAD_REQUEST);
|
||||
coap_set_status_code(response, BAD_REQUEST_4_00);
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
res_get_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
res_get_handler(coap_packet_t *request, coap_packet_t *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||
{
|
||||
REST.set_header_content_type(response, REST.type.TEXT_PLAIN);
|
||||
REST.set_response_payload(response, buffer, snprintf((char *)buffer, preferred_size, "%d", (leds_get() & LEDS_3) ? 1 : 0));
|
||||
coap_set_header_content_format(response, TEXT_PLAIN);
|
||||
coap_set_payload(response, buffer, snprintf((char *)buffer, preferred_size, "%d", (leds_get() & LEDS_3) ? 1 : 0));
|
||||
}
|
||||
|
||||
/*
|
||||
|
@ -102,5 +105,5 @@ static void
|
|||
res_event_handler()
|
||||
{
|
||||
/* Notify the registered observers which will trigger the res_get_handler to create the response. */
|
||||
REST.notify_subscribers(&res_led3);
|
||||
coap_notify_observers(&res_led3);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue