nrf52dk: added examples

This commit is contained in:
Wojciech Bober 2016-01-09 14:44:58 +01:00
parent d39ad95db5
commit 38481c513d
16 changed files with 1826 additions and 0 deletions

View file

@ -0,0 +1,35 @@
CONTIKI=../../..
CFLAGS += -DPROJECT_CONF_H=\"project-conf.h\"
ifeq ($(MAKECMDGOALS),)
$(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>)
else
CFLAGS += -DSERVER_IPV6_ADDR=\"$(SERVER_IPV6_ADDR)\"
CFLAGS += -DDEVICE_NAME=\"nRF52_DK_CoAP_Client\"
endif
else
CFLAGS += -DDEVICE_NAME=\"nRF52-DK-CoAP-Server\"
endif
# automatically build RESTful resources
REST_RESOURCES_DIR = ./resources
REST_RESOURCES_FILES = $(notdir $(shell find $(REST_RESOURCES_DIR) -name '*.c' ! -name 'res-plugtest*'))
PROJECTDIRS += $(REST_RESOURCES_DIR)
PROJECT_SOURCEFILES += $(REST_RESOURCES_FILES)
# linker optimizations
SMALL=1
# REST Engine shall use Erbium CoAP implementation
APPS += er-coap
APPS += rest-engine
CONTIKI_WITH_RPL = 0
include $(CONTIKI)/Makefile.include

View file

@ -0,0 +1,63 @@
A CoAP demo for nRF52 DK
========================
This demo contains two applications: coap-server and coap-client which are similar to
[Coap Observable Server] and [Coap Observer Client] examples provided by the nRF5 IoT SDK.
Note that before any CoAP requests can be made you'll need to configure an IPv6 connection
to the device and assign a routable IPv6 address.
For details how to do this please refer to sections 'Establishing an IPv6 connection'
and 'Distributing routable IPv6 prefix' in `platform/nrf52dk/README-BLE-6LoWPAN.md`.
CoAP Server
===========
The server exposes the following resources:
host
|-- .well-known
| `-- core
`-- lights
`-- led3
The state of LED 3 can be set and queried via CoAP through the observable resource `lights/led3`. Current
state of LED 3 is returned as a text string in the payload. The value 0 means that LED 3 is off, 1 otherwise.
Button 1 can be used to toggle state of the LED 3. This will cause a notification to be sent to
any subscriber observing `lights/led3`. The state of the resource can also be changed by using POST/PUT to
the resource with desired state encoded as a text in the payload. Send 1 to switch on and 0 to switch off.
In order to compile and flash the CoAP server to a DK execute:
make TARGET=nrf52dk coap-server.flash
Note, if you haven't previously used a given device with Contiki it is recommended
to erase the device and flash SoftDevice before flashing CoAP application, i.e.,
make TARGET=nrf52dk erase
make TARGET=nrf52dk softdevice.flash
Please refer to the *Testing* and *Python Example* sections of [Coap Observable Server] tutorial for detailed description how to query the Coap Server using a PC.
CoAP Client
===========
CoAP client compliments the CoAP server application. When Button 1 on the DK is pressed the the
client subscribes to `lights/led3` resource. If successful the LED 4 will blink briefly. From this moment
any change of the `lights/led3` resource will be automatically reflected by the client's LED 3.
Note that the client must know the server's IPv6 address. The address is specified as a make variable
during compliation.
In order to compile and flash the CoAP client to DK execute:
make TARGET=nrf52dk SERVER_IPV6_ADDR=<full-ip6-address> coap-client.flash
Note, that you can use `NRF52_JLINK_SN=<SN>` to select a particular devkit in a case when
you have more than one boards connected to PC. Please refer to `platform/README.md` for
details.
Please refer to the *Testing* and *Python Server Example* sections of [Coap Observer Client] tutorial for detailed description how to use CoAP client demo with a PC.
Resources
=========
[Coap Observable Server] http://developer.nordicsemi.com/nRF5_IoT_SDK/doc/0.9.0/html/a00054.html
[Coap Observer Client] http://developer.nordicsemi.com/nRF5_IoT_SDK/doc/0.9.0/html/a00051.html

View file

@ -0,0 +1,185 @@
/*
* Copyright (c) 2014, Daniele Alessandrelli.
* Copyright (c) 2015, Nordic Semiconductor
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* This file is part of the Contiki operating system.
*/
/**
* \addtogroup nrf52dk-examples Demo projects for nRF52 DK
* @{
*
* \defgroup nrf52dk-coap-demo CoAP demo for nRF52 DK
* @{
*
* \file
* Erbium (Er) CoAP observe client example.
* \author
* Daniele Alessandrelli <daniele.alessandrelli@gmail.com>
* \author
* Wojciech Bober <wojciech.bober@nordicsemi.no>
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "contiki.h"
#include "contiki-net.h"
#include "er-coap-engine.h"
#include "dev/button-sensor.h"
#include "dev/leds.h"
#define DEBUG 0
#if DEBUG
#include <stdio.h>
#define PRINTF(...) printf(__VA_ARGS__)
#else
#define PRINTF(...)
#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;
/*----------------------------------------------------------------------------*/
PROCESS(er_example_observe_client, "nRF52 DK Coap Observer Client");
AUTOSTART_PROCESSES(&er_example_observe_client);
/*----------------------------------------------------------------------------*/
static void
observe_led_off(void *d)
{
leds_off(SUBS_LED);
}
/*----------------------------------------------------------------------------*/
/*
* Handle the response to the observe request and the following notifications
*/
static void
notification_callback(coap_observee_t *obs, void *notification,
coap_notification_flag_t flag)
{
int len = 0;
const uint8_t *payload = NULL;
PRINTF("Notification handler\n");
PRINTF("Observee URI: %s\n", obs->url);
if (notification) {
len = coap_get_payload(notification, &payload);
}
(void)len;
switch (flag) {
case NOTIFICATION_OK:
PRINTF("NOTIFICATION OK: %*s\n", len, (char *)payload);
if (*payload == '1') {
leds_on(OBS_LED);
} else {
leds_off(OBS_LED);
}
break;
case OBSERVE_OK: /* server accepeted observation request */
PRINTF("OBSERVE_OK: %*s\n", len, (char *)payload);
if (*payload == '1') {
leds_on(OBS_LED);
} else {
leds_off(OBS_LED);
}
leds_on(SUBS_LED);
ctimer_set(&ct, CLOCK_SECOND, observe_led_off, NULL);
break;
case OBSERVE_NOT_SUPPORTED:
PRINTF("OBSERVE_NOT_SUPPORTED: %*s\n", len, (char *)payload);
obs = NULL;
break;
case ERROR_RESPONSE_CODE:
PRINTF("ERROR_RESPONSE_CODE: %*s\n", len, (char *)payload);
obs = NULL;
break;
case NO_REPLY_FROM_SERVER:
PRINTF("NO_REPLY_FROM_SERVER: "
"removing observe registration with token %x%x\n",
obs->token[0], obs->token[1]);
obs = NULL;
break;
}
}
/*----------------------------------------------------------------------------*/
/*
* The main (proto-)thread. It starts/stops the observation of the remote
* resource every time the timer elapses or the button (if available) is
* pressed
*/
PROCESS_THREAD(er_example_observe_client, ev, data)
{
PROCESS_BEGIN();
uiplib_ipaddrconv(SERVER_IPV6_ADDR, server_ipaddr);
/* receives all CoAP messages */
coap_init_engine();
#if PLATFORM_HAS_BUTTON
SENSORS_ACTIVATE(button_1);
SENSORS_ACTIVATE(button_2);
#endif
/* toggle observation every time the timer elapses or the button is pressed */
while (1) {
PROCESS_YIELD();
#if PLATFORM_HAS_BUTTON
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_RESOURCE_URI, notification_callback,
NULL);
}
if (data == &button_2 && button_2.value(BUTTON_SENSOR_VALUE_STATE) == 0) {
PRINTF("Stopping observation\n");
coap_obs_remove_observee(obs);
obs = NULL;
}
}
#endif
}
PROCESS_END();
}
/**
* @}
* @}
*/

View file

@ -0,0 +1,130 @@
/*
* Copyright (c) 2013, Institute for Pervasive Computing, ETH Zurich
* Copyright (c) 2015, Nordic Semiconductor
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* This file is part of the Contiki operating system.
*/
/**
* \addtogroup nrf52dk-examples Demo projects for nRF52 DK
* @{
*
* \defgroup nrf52dk-coap-demo CoAP demo for nRF52dk
* @{
*
* \file
* Erbium (Er) REST Engine example.
* \author
* Matthias Kovatsch <kovatsch@inf.ethz.ch>
* \author
* Wojciech Bober <wojciech.bober@nordicsemi.no>
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "contiki.h"
#include "contiki-net.h"
#include "rest-engine.h"
#include "uip.h"
#include "dev/button-sensor.h"
#include "dev/leds.h"
#define DEBUG DEBUG_PRINT
#include "net/ip/uip-debug.h"
/*
* 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;
static void
print_local_addresses(void)
{
int i;
uint8_t state;
PRINTF("Server IPv6 addresses:\n");
for(i = 0; i < UIP_DS6_ADDR_NB; i++) {
state = uip_ds6_if.addr_list[i].state;
if(uip_ds6_if.addr_list[i].isused && (state == ADDR_TENTATIVE || state
== ADDR_PREFERRED)) {
PRINTF(" ");
PRINT6ADDR(&uip_ds6_if.addr_list[i].ipaddr);
PRINTF("\n");
if(state == ADDR_TENTATIVE) {
uip_ds6_if.addr_list[i].state = ADDR_PREFERRED;
}
}
}
}
PROCESS(er_example_server, "nRF52 DK Coap Server");
AUTOSTART_PROCESSES(&er_example_server);
PROCESS_THREAD(er_example_server, ev, data)
{
PROCESS_BEGIN();
PROCESS_PAUSE();
PRINTF("Starting Erbium Example Server\n");
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);
print_local_addresses();
/* Initialize the REST engine. */
rest_init_engine();
rest_activate_resource(&res_led3, "lights/led3");
SENSORS_ACTIVATE(button_1);
/* Define application-specific events here. */
while (1) {
PROCESS_WAIT_EVENT();
if (ev == sensors_event) {
if (data == &button_1 && button_1.value(BUTTON_SENSOR_VALUE_STATE) == 0) {
leds_toggle(LEDS_3);
res_led3.trigger();
}
}
}
PROCESS_END();
}
/**
* @}
* @}
*/

View file

@ -0,0 +1,85 @@
/*
* Copyright (c) 2013, Institute for Pervasive Computing, ETH Zurich
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* This file is part of the Contiki operating system.
*/
/**
* \addtogroup nrf52dk-examples Demo projects for nRF52 DK
* @{
*
* \defgroup nrf52dk-coap-demo CoAP demo for nRF52dk
* @{
*
* \file
* Erbium (Er) example project configuration.
* \author
* Matthias Kovatsch <kovatsch@inf.ethz.ch>
*/
#ifndef __PROJECT_ERBIUM_CONF_H__
#define __PROJECT_ERBIUM_CONF_H__
/* Disabling TCP on CoAP nodes. */
#undef UIP_CONF_TCP
#define UIP_CONF_TCP 0
/* Increase rpl-border-router IP-buffer when using more than 64. */
#undef REST_MAX_CHUNK_SIZE
#define REST_MAX_CHUNK_SIZE 48
/* Estimate your header size, especially when using Proxy-Uri. */
/*
#undef COAP_MAX_HEADER_SIZE
#define COAP_MAX_HEADER_SIZE 70
*/
/* Multiplies with chunk size, be aware of memory constraints. */
#undef COAP_MAX_OPEN_TRANSACTIONS
#define COAP_MAX_OPEN_TRANSACTIONS 4
/* Must be <= open transactions, default is COAP_MAX_OPEN_TRANSACTIONS-1. */
/*
#undef COAP_MAX_OBSERVERS
#define COAP_MAX_OBSERVERS 2
*/
/* Filtering .well-known/core per query can be disabled to save space. */
#undef COAP_LINK_FORMAT_FILTERING
#define COAP_LINK_FORMAT_FILTERING 0
#undef COAP_PROXY_OPTION_PROCESSING
#define COAP_PROXY_OPTION_PROCESSING 0
/* Enable client-side support for COAP observe */
#define COAP_OBSERVE_CLIENT 1
#endif /* __PROJECT_ERBIUM_CONF_H__ */
/**
* @}
* @}
*/

View file

@ -0,0 +1,106 @@
/*
* Copyright (c) 2013, Institute for Pervasive Computing, ETH Zurich
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* This file is part of the Contiki operating system.
*/
/**
* \file
* Example resource
* \author
* Matthias Kovatsch <kovatsch@inf.ethz.ch>
* \author
* Wojciech Bober <wojciech.bober@nordicsemi.no>
*/
#include <string.h>
#include "contiki.h"
#include "rest-engine.h"
#include "dev/leds.h"
#define DEBUG DEBUG_NONE
#include "net/ip/uip-debug.h"
static void
res_post_put_handler(void *request, void *response, uint8_t *buffer,
uint16_t preferred_size, int32_t *offset);
static void
res_get_handler(void *request, void *response, uint8_t *buffer,
uint16_t preferred_size, int32_t *offset);
static void
res_event_handler();
/*A simple actuator example, depending on the color query parameter and post variable mode, corresponding led is activated or deactivated*/
EVENT_RESOURCE(res_led3,
"title=\"LED3\"; obs",
res_get_handler,
res_post_put_handler,
res_post_put_handler,
NULL,
res_event_handler
);
static void
res_post_put_handler(void *request, void *response, uint8_t *buffer,
uint16_t preferred_size, int32_t *offset)
{
const uint8_t *payload;
REST.get_request_payload(request, &payload);
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);
} else {
REST.set_response_status(response, REST.status.BAD_REQUEST);
}
}
static void
res_get_handler(void *request, void *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));
}
/*
* Additionally, res_event_handler must be implemented for each EVENT_RESOURCE.
* It is called through <res_name>.trigger(), usually from the server process.
*/
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);
}