diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..96c03b3 --- /dev/null +++ b/Makefile @@ -0,0 +1,44 @@ +PACKAGES_DIR=../../packages +LWM2M_DIR=$(PACKAGES_DIR)/AwaLWM2M +CONTIKI_PROJECT=lwm2m-client-button-sensor +VERSION?=$(shell git describe --abbrev=4 --dirty --always --tags) +CONTIKI=../../constrained-os/contiki +CONTIKI_WITH_IPV6 = 1 +CONTIKI_WITH_RPL = 0 + +CFLAGS += -DPROJECT_CONF_H=\"project-conf.h\" -DVERSION='\"$(VERSION)\"' +CFLAGS += -Wall -Wno-pointer-sign -DLWM2M_CLIENT +CFLAGS += -I$(LWM2M_DIR)/api/include + +ifneq (,$(filter $(TARGET),seedeye mikro-e)) + CFLAGS += -fno-short-double + LDFLAGS += -Wl,--defsym,_min_heap_size=64000 +endif + +APPS += er-coap +APPS += rest-engine +APPS += client +APPS += common +APPS += jsmn +APPS += b64 +APPS += hmac +APPS += libobjects + +ifeq ($(TARGET),minimal-net) + APPS += xml +endif + +APPDIRS += $(LWM2M_DIR)/lib +APPDIRS += $(LWM2M_DIR)/core/src +APPDIRS += $(PACKAGES_DIR) + +all: $(CONTIKI_PROJECT) + xc32-bin2hex $(CONTIKI_PROJECT).$(TARGET) + +distclean: cleanall + +cleanall: + rm -f $(CONTIKI_PROJECT).hex + rm -f symbols.* + +include $(CONTIKI)/Makefile.include diff --git a/README.md b/README.md index 10f64fd..204bc6f 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,19 @@ -# button-sensor -MikroE 6loWPAN Clicker based lwm2m client button sensor application +#Button Sensor Application + +This application registers Flow, Flow Access and Digital Input object to Flow server and sends Button press events. + +How To Compile + +Assuming you have creator-contiki source code with directories constrained-os, packages/button-sensor, packages/libobjects and packages/AwaLWM2M + +``` +$ cd button-sensor +$ make TARGET=mikro-e +``` + +This will generate hex file which can be flashed onto the MikroE clicker. + +## Revision History +| Revision | Changes from previous revision | +| :---- | :------------------------------| +| 0.9.0 | External Beta Trial Release | diff --git a/lwm2m-client-button-sensor.c b/lwm2m-client-button-sensor.c new file mode 100644 index 0000000..f824ef9 --- /dev/null +++ b/lwm2m-client-button-sensor.c @@ -0,0 +1,207 @@ +/** + * @file + * LightWeightM2M LWM2M client button application. + * + * @author Imagination Technologies + * + * @copyright Copyright (c) 2016, Imagination Technologies Limited and/or its affiliated group + * companies and/or licensors. + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, are permitted + * provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions + * and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of + * conditions and the following disclaimer in the documentation and/or other materials provided + * with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to + * endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY + * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/*************************************************************************************************** + * Includes + **************************************************************************************************/ + +#include +#include +#include +#include "contiki.h" +#include "contiki-net.h" + +#include "lib/sensors.h" +#include "button-sensor.h" + + +#include "awa/static.h" + + +#include "lwm2m-client-flow-object.h" +#include "lwm2m-client-flow-access-object.h" +#include "lwm2m-client-ipso-digital-input.h" +#include "lwm2m-client-device-object.h" +/*************************************************************************************************** + * Definitions + **************************************************************************************************/ + +//! \{ +#define COAP_PORT (6000) +#define IPC_PORT (12345) +#define BOOTSTRAP_PORT "15683" +#define END_POINT_NAME "ButtonDevice" +//! \} + +/*************************************************************************************************** + * Typedefs + **************************************************************************************************/ + +/** + * A structure to contain lwm2m client's options. + */ +typedef struct +{ + //! \{ + int CoapPort; + int IpcPort; + bool Verbose; + char * EndPointName; + char * BootStrap; + //! \} +} Options; + +/*************************************************************************************************** + * Globals + **************************************************************************************************/ + +//! \{ + +Options options = +{ + .CoapPort = COAP_PORT, + .IpcPort = IPC_PORT, + .Verbose = true, + .BootStrap = "coap://["BOOTSTRAP_IPv6_ADDR"]:"BOOTSTRAP_PORT"/", + .EndPointName = END_POINT_NAME, +}; + +/*************************************************************************************************** + * Implementation + **************************************************************************************************/ + + +void ConstructObjectTree(AwaStaticClient *client) +{ + DefineDeviceObject(client); + DefineFlowObject(client); + DefineFlowAccessObject(client); + DefineDigitalInputObject(client); +} + +void AwaStaticClient_Start(AwaStaticClient *client) +{ + AwaStaticClient_SetLogLevel((options.Verbose) ? AwaLogLevel_Debug : AwaLogLevel_Warning); + printf("LWM2M client - CoAP port %d\n", options.CoapPort); + printf("LWM2M client - IPC port %d\n", options.IpcPort); + AwaStaticClient_SetEndPointName(client, options.EndPointName); + AwaStaticClient_SetCoAPListenAddressPort(client, "0.0.0.0", options.CoapPort); + AwaStaticClient_SetBootstrapServerURI(client, options.BootStrap); + AwaStaticClient_Init(client); + ConstructObjectTree(client); +} + +PROCESS(lwm2m_client, "LwM2M Client"); +#ifndef BUTTON_PRESS_SIMULATION +AUTOSTART_PROCESSES(&lwm2m_client); +#else +PROCESS(button_press_simulator, "Button Press Simulator"); +AUTOSTART_PROCESSES(&lwm2m_client, &button_press_simulator); +#endif + +PROCESS_THREAD(lwm2m_client, ev, data) +{ + PROCESS_BEGIN(); + + PROCESS_PAUSE(); + + printf("Starting LWM2M Client for lwm2m-client-button-sensor\n"); + +#ifdef RF_CHANNEL + printf("RF channel: %u\n", RF_CHANNEL); +#endif +#ifdef IEEE802154_PANID + printf("PAN ID: 0x%04X\n", IEEE802154_PANID); +#endif + + printf("uIP buffer: %u\n", UIP_BUFSIZE); + printf("LL header: %u\n", UIP_LLH_LEN); + printf("IP+UDP header: %u\n", UIP_IPUDPH_LEN); +#ifdef REST_MAX_CHUNK_SIZE + printf("REST max chunk: %u\n", REST_MAX_CHUNK_SIZE); +#endif + + uip_ipaddr_t ipaddr; + uip_ip6addr(&ipaddr, BOOTSTRAP_IPv6_ADDR1, BOOTSTRAP_IPv6_ADDR2, BOOTSTRAP_IPv6_ADDR3, + BOOTSTRAP_IPv6_ADDR4, BOOTSTRAP_IPv6_ADDR5, BOOTSTRAP_IPv6_ADDR6, BOOTSTRAP_IPv6_ADDR7, + BOOTSTRAP_IPv6_ADDR8); + uip_ds6_defrt_add(&ipaddr, 0); + + static AwaStaticClient *client; + client = AwaStaticClient_New(); + AwaStaticClient_Start(client); + + + /* Define application-specific events here. */ + while(1) + { + static struct etimer et; + static int WaitTime; + WaitTime = AwaStaticClient_Process(client); + etimer_set(&et, (WaitTime * CLOCK_SECOND) / 1000); + PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&et) || (ev == sensors_event)); + + if (data == &button_sensor) + { + printf("Button press event received\n"); + DigitalInput_IncrementCounter(client, 0); + } + } + + PROCESS_END(); +} + +/*---------------------------------------------------------------------------*/ +#ifdef BUTTON_PRESS_SIMULATION +/** + * @brief Simulate button presses periodically (required for automated testing). + */ +PROCESS_THREAD(button_press_simulator, ev, data) +{ + PROCESS_BEGIN(); + static struct etimer button_timer; + int wait_time = BUTTON_PRESS_PERIOD * CLOCK_SECOND; + etimer_set(&button_timer, wait_time); + while(1) { + PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&button_timer)); + process_post(PROCESS_BROADCAST, sensors_event, (void *)&button_sensor); + etimer_reset(&button_timer); + } + PROCESS_END(); +} +#endif + +/*---------------------------------------------------------------------------*/ +//! \} diff --git a/project-conf.h b/project-conf.h new file mode 100644 index 0000000..9746412 --- /dev/null +++ b/project-conf.h @@ -0,0 +1,118 @@ +/** + * @file + * LightWeightM2M Project Config. + * + * @author Imagination Technologies + * + * @copyright Copyright (c) 2016, Imagination Technologies Limited and/or its affiliated group + * companies and/or licensors. + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, are permitted + * provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions + * and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of + * conditions and the following disclaimer in the documentation and/or other materials provided + * with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to + * endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY + * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef __PROJECT_LWM2M_CONF_H__ +#define __PROJECT_LWM2M_CONF_H__ + +/* Custom channel and PAN ID configuration for your project. */ +/* + #define RF_CHANNEL 26 + #define IEEE802154_CONF_PANID 0xABCD + */ + +/* IP buffer size must match all other hops, in particular the border router. */ +/* + #define UIP_CONF_BUFFER_SIZE 256 + */ + +/* Disabling RDC and CSMA for demo purposes. Core updates often + require more memory. */ +/* For projects, optimize memory and enable RDC and CSMA again. */ +#define NETSTACK_CONF_RDC nullrdc_driver + +/* Disabling TCP on CoAP nodes. */ +#define UIP_CONF_TCP 0 + +/* + #define NETSTACK_CONF_MAC nullmac_driver +*/ + +/* Increase rpl-border-router IP-buffer when using more than 64. */ +#define REST_MAX_CHUNK_SIZE 512 + +/* Estimate your header size, especially when using Proxy-Uri. */ +/* + #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. */ +/* + #define COAP_MAX_OBSERVERS 2 + */ + +/* Filtering .well-known/core per query can be disabled to save space. */ +#define COAP_LINK_FORMAT_FILTERING 0 +#define COAP_PROXY_OPTION_PROCESSING 0 + +/* Enable client-side support for COAP observe */ +#define COAP_OBSERVE_CLIENT 1 + +#define UIP_CONF_TCP 0 + +#define UIP_CONF_BUFFER_SIZE 4096 + +/* IP address of device */ +#define GLOBAL_IPv6_ADDR +#define GLOBAL_IPv6_ADDR1 0x2001 +#define GLOBAL_IPv6_ADDR2 0x1418 +#define GLOBAL_IPv6_ADDR3 0x0100 +#define GLOBAL_IPv6_ADDR4 0x823c +#define GLOBAL_IPv6_ADDR5 0x0 +#define GLOBAL_IPv6_ADDR6 0x0 +#define GLOBAL_IPv6_ADDR7 0x0 +#define GLOBAL_IPv6_ADDR8 0x0 + +#define BOOTSTRAP_IPv6_ADDR1 0x2001 +#define BOOTSTRAP_IPv6_ADDR2 0x1418 +#define BOOTSTRAP_IPv6_ADDR3 0x0100 +#define BOOTSTRAP_IPv6_ADDR4 0x0 +#define BOOTSTRAP_IPv6_ADDR5 0x0 +#define BOOTSTRAP_IPv6_ADDR6 0x0 +#define BOOTSTRAP_IPv6_ADDR7 0x0 +#define BOOTSTRAP_IPv6_ADDR8 0x1 + +#define BOOTSTRAP_IPv6_ADDR "2001:1418:100::1" + +/* Enable button press simulation */ +/* + #define BUTTON_PRESS_SIMULATION + #define BUTTON_PRESS_PERIOD 5 + */ + +#endif /* __PROJECT_LWM2M_CONF_H__ */