|
| 1 | +/** |
| 2 | + * @file |
| 3 | + * LightWeightM2M LWM2M client button application. |
| 4 | + * |
| 5 | + * @author Imagination Technologies |
| 6 | + * |
| 7 | + * @copyright Copyright (c) 2016, Imagination Technologies Limited and/or its affiliated group |
| 8 | + * companies and/or licensors. |
| 9 | + * |
| 10 | + * All rights reserved. |
| 11 | + * |
| 12 | + * Redistribution and use in source and binary forms, with or without modification, are permitted |
| 13 | + * provided that the following conditions are met: |
| 14 | + * |
| 15 | + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions |
| 16 | + * and the following disclaimer. |
| 17 | + * |
| 18 | + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of |
| 19 | + * conditions and the following disclaimer in the documentation and/or other materials provided |
| 20 | + * with the distribution. |
| 21 | + * |
| 22 | + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to |
| 23 | + * endorse or promote products derived from this software without specific prior written |
| 24 | + * permission. |
| 25 | + * |
| 26 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR |
| 27 | + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 28 | + * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR |
| 29 | + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 30 | + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 31 | + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 32 | + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY |
| 33 | + * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 34 | + */ |
| 35 | + |
| 36 | +/*************************************************************************************************** |
| 37 | + * Includes |
| 38 | + **************************************************************************************************/ |
| 39 | + |
| 40 | +#include <stdio.h> |
| 41 | +#include <stdlib.h> |
| 42 | +#include <string.h> |
| 43 | +#include "contiki.h" |
| 44 | +#include "contiki-net.h" |
| 45 | + |
| 46 | +#include "lib/sensors.h" |
| 47 | +#include "button-sensor.h" |
| 48 | + |
| 49 | + |
| 50 | +#include "awa/static.h" |
| 51 | + |
| 52 | + |
| 53 | +#include "lwm2m-client-flow-object.h" |
| 54 | +#include "lwm2m-client-flow-access-object.h" |
| 55 | +#include "lwm2m-client-ipso-digital-input.h" |
| 56 | +#include "lwm2m-client-device-object.h" |
| 57 | +/*************************************************************************************************** |
| 58 | + * Definitions |
| 59 | + **************************************************************************************************/ |
| 60 | + |
| 61 | +//! \{ |
| 62 | +#define COAP_PORT (6000) |
| 63 | +#define IPC_PORT (12345) |
| 64 | +#define BOOTSTRAP_PORT "15683" |
| 65 | +#define END_POINT_NAME "ButtonDevice" |
| 66 | +//! \} |
| 67 | + |
| 68 | +/*************************************************************************************************** |
| 69 | + * Typedefs |
| 70 | + **************************************************************************************************/ |
| 71 | + |
| 72 | +/** |
| 73 | + * A structure to contain lwm2m client's options. |
| 74 | + */ |
| 75 | +typedef struct |
| 76 | +{ |
| 77 | + //! \{ |
| 78 | + int CoapPort; |
| 79 | + int IpcPort; |
| 80 | + bool Verbose; |
| 81 | + char * EndPointName; |
| 82 | + char * BootStrap; |
| 83 | + //! \} |
| 84 | +} Options; |
| 85 | + |
| 86 | +/*************************************************************************************************** |
| 87 | + * Globals |
| 88 | + **************************************************************************************************/ |
| 89 | + |
| 90 | +//! \{ |
| 91 | + |
| 92 | +Options options = |
| 93 | +{ |
| 94 | + .CoapPort = COAP_PORT, |
| 95 | + .IpcPort = IPC_PORT, |
| 96 | + .Verbose = true, |
| 97 | + .BootStrap = "coap://["BOOTSTRAP_IPv6_ADDR"]:"BOOTSTRAP_PORT"/", |
| 98 | + .EndPointName = END_POINT_NAME, |
| 99 | +}; |
| 100 | + |
| 101 | +/*************************************************************************************************** |
| 102 | + * Implementation |
| 103 | + **************************************************************************************************/ |
| 104 | + |
| 105 | + |
| 106 | +void ConstructObjectTree(AwaStaticClient *client) |
| 107 | +{ |
| 108 | + DefineDeviceObject(client); |
| 109 | + DefineFlowObject(client); |
| 110 | + DefineFlowAccessObject(client); |
| 111 | + DefineDigitalInputObject(client); |
| 112 | +} |
| 113 | + |
| 114 | +void AwaStaticClient_Start(AwaStaticClient *client) |
| 115 | +{ |
| 116 | + AwaStaticClient_SetLogLevel((options.Verbose) ? AwaLogLevel_Debug : AwaLogLevel_Warning); |
| 117 | + printf("LWM2M client - CoAP port %d\n", options.CoapPort); |
| 118 | + printf("LWM2M client - IPC port %d\n", options.IpcPort); |
| 119 | + AwaStaticClient_SetEndPointName(client, options.EndPointName); |
| 120 | + AwaStaticClient_SetCoAPListenAddressPort(client, "0.0.0.0", options.CoapPort); |
| 121 | + AwaStaticClient_SetBootstrapServerURI(client, options.BootStrap); |
| 122 | + AwaStaticClient_Init(client); |
| 123 | + ConstructObjectTree(client); |
| 124 | +} |
| 125 | + |
| 126 | +PROCESS(lwm2m_client, "LwM2M Client"); |
| 127 | +#ifndef BUTTON_PRESS_SIMULATION |
| 128 | +AUTOSTART_PROCESSES(&lwm2m_client); |
| 129 | +#else |
| 130 | +PROCESS(button_press_simulator, "Button Press Simulator"); |
| 131 | +AUTOSTART_PROCESSES(&lwm2m_client, &button_press_simulator); |
| 132 | +#endif |
| 133 | + |
| 134 | +PROCESS_THREAD(lwm2m_client, ev, data) |
| 135 | +{ |
| 136 | + PROCESS_BEGIN(); |
| 137 | + |
| 138 | + PROCESS_PAUSE(); |
| 139 | + |
| 140 | + printf("Starting LWM2M Client for lwm2m-client-button-sensor\n"); |
| 141 | + |
| 142 | +#ifdef RF_CHANNEL |
| 143 | + printf("RF channel: %u\n", RF_CHANNEL); |
| 144 | +#endif |
| 145 | +#ifdef IEEE802154_PANID |
| 146 | + printf("PAN ID: 0x%04X\n", IEEE802154_PANID); |
| 147 | +#endif |
| 148 | + |
| 149 | + printf("uIP buffer: %u\n", UIP_BUFSIZE); |
| 150 | + printf("LL header: %u\n", UIP_LLH_LEN); |
| 151 | + printf("IP+UDP header: %u\n", UIP_IPUDPH_LEN); |
| 152 | +#ifdef REST_MAX_CHUNK_SIZE |
| 153 | + printf("REST max chunk: %u\n", REST_MAX_CHUNK_SIZE); |
| 154 | +#endif |
| 155 | + |
| 156 | + uip_ipaddr_t ipaddr; |
| 157 | + uip_ip6addr(&ipaddr, BOOTSTRAP_IPv6_ADDR1, BOOTSTRAP_IPv6_ADDR2, BOOTSTRAP_IPv6_ADDR3, |
| 158 | + BOOTSTRAP_IPv6_ADDR4, BOOTSTRAP_IPv6_ADDR5, BOOTSTRAP_IPv6_ADDR6, BOOTSTRAP_IPv6_ADDR7, |
| 159 | + BOOTSTRAP_IPv6_ADDR8); |
| 160 | + uip_ds6_defrt_add(&ipaddr, 0); |
| 161 | + |
| 162 | + static AwaStaticClient *client; |
| 163 | + client = AwaStaticClient_New(); |
| 164 | + AwaStaticClient_Start(client); |
| 165 | + |
| 166 | + |
| 167 | + /* Define application-specific events here. */ |
| 168 | + while(1) |
| 169 | + { |
| 170 | + static struct etimer et; |
| 171 | + static int WaitTime; |
| 172 | + WaitTime = AwaStaticClient_Process(client); |
| 173 | + etimer_set(&et, (WaitTime * CLOCK_SECOND) / 1000); |
| 174 | + PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&et) || (ev == sensors_event)); |
| 175 | + |
| 176 | + if (data == &button_sensor) |
| 177 | + { |
| 178 | + printf("Button press event received\n"); |
| 179 | + DigitalInput_IncrementCounter(client, 0); |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | + PROCESS_END(); |
| 184 | +} |
| 185 | + |
| 186 | +/*---------------------------------------------------------------------------*/ |
| 187 | +#ifdef BUTTON_PRESS_SIMULATION |
| 188 | +/** |
| 189 | + * @brief Simulate button presses periodically (required for automated testing). |
| 190 | + */ |
| 191 | +PROCESS_THREAD(button_press_simulator, ev, data) |
| 192 | +{ |
| 193 | + PROCESS_BEGIN(); |
| 194 | + static struct etimer button_timer; |
| 195 | + int wait_time = BUTTON_PRESS_PERIOD * CLOCK_SECOND; |
| 196 | + etimer_set(&button_timer, wait_time); |
| 197 | + while(1) { |
| 198 | + PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&button_timer)); |
| 199 | + process_post(PROCESS_BROADCAST, sensors_event, (void *)&button_sensor); |
| 200 | + etimer_reset(&button_timer); |
| 201 | + } |
| 202 | + PROCESS_END(); |
| 203 | +} |
| 204 | +#endif |
| 205 | + |
| 206 | +/*---------------------------------------------------------------------------*/ |
| 207 | +//! \} |
0 commit comments