|
| 1 | +/******************************************************************************* |
| 2 | + * |
| 3 | + * Copyright (c) 2013, 2014, 2015 Intel Corporation and others. |
| 4 | + * All rights reserved. This program and the accompanying materials |
| 5 | + * are made available under the terms of the Eclipse Public License v1.0 |
| 6 | + * and Eclipse Distribution License v1.0 which accompany this distribution. |
| 7 | + * |
| 8 | + * The Eclipse Public License is available at |
| 9 | + * http://www.eclipse.org/legal/epl-v10.html |
| 10 | + * The Eclipse Distribution License is available at |
| 11 | + * http://www.eclipse.org/org/documents/edl-v10.php. |
| 12 | + * |
| 13 | + * Contributors: |
| 14 | + * David Navarro, Intel Corporation - initial API and implementation |
| 15 | + * domedambrosio - Please refer to git log |
| 16 | + * Fabien Fleutot - Please refer to git log |
| 17 | + * Axel Lorente - Please refer to git log |
| 18 | + * Bosch Software Innovations GmbH - Please refer to git log |
| 19 | + * Pascal Rieux - Please refer to git log |
| 20 | + * |
| 21 | + *******************************************************************************/ |
| 22 | + |
| 23 | +/* |
| 24 | + Copyright (c) 2013, 2014 Intel Corporation |
| 25 | +
|
| 26 | + Redistribution and use in source and binary forms, with or without modification, |
| 27 | + are permitted provided that the following conditions are met: |
| 28 | +
|
| 29 | + * Redistributions of source code must retain the above copyright notice, |
| 30 | + this list of conditions and the following disclaimer. |
| 31 | + * Redistributions in binary form must reproduce the above copyright notice, |
| 32 | + this list of conditions and the following disclaimer in the documentation |
| 33 | + and/or other materials provided with the distribution. |
| 34 | + * Neither the name of Intel Corporation nor the names of its contributors |
| 35 | + may be used to endorse or promote products derived from this software |
| 36 | + without specific prior written permission. |
| 37 | +
|
| 38 | + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 39 | + ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 40 | + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 41 | + IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, |
| 42 | + INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 43 | + BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 44 | + DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 45 | + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 46 | + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| 47 | + THE POSSIBILITY OF SUCH DAMAGE. |
| 48 | +
|
| 49 | + |
| 50 | +
|
| 51 | +*/ |
| 52 | + |
| 53 | +/* |
| 54 | + * This object is single instance only, and is mandatory to all LWM2M device as it describe the object such as its |
| 55 | + * manufacturer, model, etc... |
| 56 | + * |
| 57 | + * Here we implement only some of the optional resources. |
| 58 | + * |
| 59 | + */ |
| 60 | + |
| 61 | +#include "liblwm2m.h" |
| 62 | + |
| 63 | +#include <stdio.h> |
| 64 | +#include <stdlib.h> |
| 65 | +#include <string.h> |
| 66 | +#include <ctype.h> |
| 67 | + |
| 68 | + |
| 69 | +#define PRV_MANUFACTURER "Open Mobile Alliance" |
| 70 | +#define PRV_MODEL_NUMBER "Lightweight M2M Client" |
| 71 | +#define PRV_BINDING_MODE "U" |
| 72 | + |
| 73 | +// Resource Id's: |
| 74 | +#define RES_O_MANUFACTURER 0 |
| 75 | +#define RES_O_MODEL_NUMBER 1 |
| 76 | +#define RES_O_SERIAL_NUMBER 2 |
| 77 | +#define RES_O_FIRMWARE_VERSION 3 |
| 78 | +#define RES_M_REBOOT 4 |
| 79 | +#define RES_O_FACTORY_RESET 5 |
| 80 | +#define RES_O_AVL_POWER_SOURCES 6 |
| 81 | +#define RES_O_POWER_SOURCE_VOLTAGE 7 |
| 82 | +#define RES_O_POWER_SOURCE_CURRENT 8 |
| 83 | +#define RES_O_BATTERY_LEVEL 9 |
| 84 | +#define RES_O_MEMORY_FREE 10 |
| 85 | +#define RES_M_ERROR_CODE 11 |
| 86 | +#define RES_O_RESET_ERROR_CODE 12 |
| 87 | +#define RES_O_CURRENT_TIME 13 |
| 88 | +#define RES_O_UTC_OFFSET 14 |
| 89 | +#define RES_O_TIMEZONE 15 |
| 90 | +#define RES_M_BINDING_MODES 16 |
| 91 | +#define RES_O_DEVICE_TYPE 17 |
| 92 | +#define RES_O_HARDWARE_VERSION 18 |
| 93 | +#define RES_O_SOFTWARE_VERSION 19 |
| 94 | +#define RES_O_BATTERY_STATUS 20 |
| 95 | +#define RES_O_MEMORY_TOTAL 21 |
| 96 | + |
| 97 | + |
| 98 | +static uint8_t prv_set_value(lwm2m_data_t * dataP) |
| 99 | +{ |
| 100 | + // a simple switch structure is used to respond at the specified resource asked |
| 101 | + switch (dataP->id) |
| 102 | + { |
| 103 | + case RES_O_MANUFACTURER: |
| 104 | + lwm2m_data_encode_string(PRV_MANUFACTURER, dataP); |
| 105 | + return COAP_205_CONTENT; |
| 106 | + |
| 107 | + case RES_O_MODEL_NUMBER: |
| 108 | + lwm2m_data_encode_string(PRV_MODEL_NUMBER, dataP); |
| 109 | + return COAP_205_CONTENT; |
| 110 | + |
| 111 | + case RES_M_REBOOT: |
| 112 | + return COAP_405_METHOD_NOT_ALLOWED; |
| 113 | + |
| 114 | + case RES_M_BINDING_MODES: |
| 115 | + lwm2m_data_encode_string(PRV_BINDING_MODE, dataP); |
| 116 | + return COAP_205_CONTENT; |
| 117 | + |
| 118 | + |
| 119 | + default: |
| 120 | + return COAP_404_NOT_FOUND; |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +static uint8_t prv_device_read(uint16_t instanceId, |
| 125 | + int * numDataP, |
| 126 | + lwm2m_data_t ** dataArrayP, |
| 127 | + lwm2m_object_t * objectP) |
| 128 | +{ |
| 129 | + uint8_t result; |
| 130 | + int i; |
| 131 | + |
| 132 | + // this is a single instance object |
| 133 | + if (instanceId != 0) |
| 134 | + { |
| 135 | + return COAP_404_NOT_FOUND; |
| 136 | + } |
| 137 | + |
| 138 | + // is the server asking for the full object ? |
| 139 | + if (*numDataP == 0) |
| 140 | + { |
| 141 | + uint16_t resList[] = { |
| 142 | + RES_O_MANUFACTURER, |
| 143 | + RES_O_MODEL_NUMBER, |
| 144 | + RES_M_BINDING_MODES |
| 145 | + }; |
| 146 | + int nbRes = sizeof(resList)/sizeof(uint16_t); |
| 147 | + |
| 148 | + *dataArrayP = lwm2m_data_new(nbRes); |
| 149 | + if (*dataArrayP == NULL) return COAP_500_INTERNAL_SERVER_ERROR; |
| 150 | + *numDataP = nbRes; |
| 151 | + for (i = 0 ; i < nbRes ; i++) |
| 152 | + { |
| 153 | + (*dataArrayP)[i].id = resList[i]; |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + i = 0; |
| 158 | + do |
| 159 | + { |
| 160 | + result = prv_set_value((*dataArrayP) + i); |
| 161 | + i++; |
| 162 | + } while (i < *numDataP && result == COAP_205_CONTENT); |
| 163 | + |
| 164 | + return result; |
| 165 | +} |
| 166 | + |
| 167 | +static uint8_t prv_device_discover(uint16_t instanceId, |
| 168 | + int * numDataP, |
| 169 | + lwm2m_data_t ** dataArrayP, |
| 170 | + lwm2m_object_t * objectP) |
| 171 | +{ |
| 172 | + uint8_t result; |
| 173 | + int i; |
| 174 | + |
| 175 | + // this is a single instance object |
| 176 | + if (instanceId != 0) |
| 177 | + { |
| 178 | + return COAP_404_NOT_FOUND; |
| 179 | + } |
| 180 | + |
| 181 | + result = COAP_205_CONTENT; |
| 182 | + |
| 183 | + // is the server asking for the full object ? |
| 184 | + if (*numDataP == 0) |
| 185 | + { |
| 186 | + uint16_t resList[] = { |
| 187 | + RES_O_MANUFACTURER, |
| 188 | + RES_O_MODEL_NUMBER, |
| 189 | + RES_M_BINDING_MODES, |
| 190 | + RES_M_REBOOT |
| 191 | + }; |
| 192 | + int nbRes = sizeof(resList)/sizeof(uint16_t); |
| 193 | + |
| 194 | + *dataArrayP = lwm2m_data_new(nbRes); |
| 195 | + if (*dataArrayP == NULL) return COAP_500_INTERNAL_SERVER_ERROR; |
| 196 | + *numDataP = nbRes; |
| 197 | + for (i = 0 ; i < nbRes ; i++) |
| 198 | + { |
| 199 | + (*dataArrayP)[i].id = resList[i]; |
| 200 | + } |
| 201 | + } |
| 202 | + else |
| 203 | + { |
| 204 | + for (i = 0; i < *numDataP && result == COAP_205_CONTENT; i++) |
| 205 | + { |
| 206 | + switch ((*dataArrayP)[i].id) |
| 207 | + { |
| 208 | + case RES_O_MANUFACTURER: |
| 209 | + case RES_O_MODEL_NUMBER: |
| 210 | + case RES_M_BINDING_MODES: |
| 211 | + case RES_M_REBOOT: |
| 212 | + break; |
| 213 | + default: |
| 214 | + result = COAP_404_NOT_FOUND; |
| 215 | + } |
| 216 | + } |
| 217 | + } |
| 218 | + |
| 219 | + return result; |
| 220 | +} |
| 221 | + |
| 222 | +static uint8_t prv_device_execute(uint16_t instanceId, |
| 223 | + uint16_t resourceId, |
| 224 | + uint8_t * buffer, |
| 225 | + int length, |
| 226 | + lwm2m_object_t * objectP) |
| 227 | +{ |
| 228 | + // this is a single instance object |
| 229 | + if (instanceId != 0) |
| 230 | + { |
| 231 | + return COAP_404_NOT_FOUND; |
| 232 | + } |
| 233 | + |
| 234 | + if (length != 0) return COAP_400_BAD_REQUEST; |
| 235 | + |
| 236 | + if (resourceId == RES_M_REBOOT) |
| 237 | + { |
| 238 | + fprintf(stdout, "\n\t REBOOT\r\n\n"); |
| 239 | + return COAP_204_CHANGED; |
| 240 | + } |
| 241 | + |
| 242 | + return COAP_405_METHOD_NOT_ALLOWED; |
| 243 | +} |
| 244 | + |
| 245 | +lwm2m_object_t * get_object_device() |
| 246 | +{ |
| 247 | + /* |
| 248 | + * The get_object_device function create the object itself and return a pointer to the structure that represent it. |
| 249 | + */ |
| 250 | + lwm2m_object_t * deviceObj; |
| 251 | + |
| 252 | + deviceObj = (lwm2m_object_t *)lwm2m_malloc(sizeof(lwm2m_object_t)); |
| 253 | + |
| 254 | + if (NULL != deviceObj) |
| 255 | + { |
| 256 | + memset(deviceObj, 0, sizeof(lwm2m_object_t)); |
| 257 | + |
| 258 | + /* |
| 259 | + * It assigns his unique ID |
| 260 | + * The 3 is the standard ID for the mandatory object "Object device". |
| 261 | + */ |
| 262 | + deviceObj->objID = LWM2M_DEVICE_OBJECT_ID; |
| 263 | + |
| 264 | + /* |
| 265 | + * and its unique instance |
| 266 | + * |
| 267 | + */ |
| 268 | + deviceObj->instanceList = (lwm2m_list_t *)lwm2m_malloc(sizeof(lwm2m_list_t)); |
| 269 | + if (NULL != deviceObj->instanceList) |
| 270 | + { |
| 271 | + memset(deviceObj->instanceList, 0, sizeof(lwm2m_list_t)); |
| 272 | + } |
| 273 | + else |
| 274 | + { |
| 275 | + lwm2m_free(deviceObj); |
| 276 | + return NULL; |
| 277 | + } |
| 278 | + |
| 279 | + /* |
| 280 | + * And the private function that will access the object. |
| 281 | + * Those function will be called when a read/write/execute query is made by the server. In fact the library don't need to |
| 282 | + * know the resources of the object, only the server does. |
| 283 | + */ |
| 284 | + deviceObj->readFunc = prv_device_read; |
| 285 | + deviceObj->executeFunc = prv_device_execute; |
| 286 | + deviceObj->discoverFunc = prv_device_discover; |
| 287 | + |
| 288 | + } |
| 289 | + |
| 290 | + return deviceObj; |
| 291 | +} |
| 292 | + |
| 293 | +void free_object_device(lwm2m_object_t * objectP) |
| 294 | +{ |
| 295 | + if (NULL != objectP->userData) |
| 296 | + { |
| 297 | + lwm2m_free(objectP->userData); |
| 298 | + objectP->userData = NULL; |
| 299 | + } |
| 300 | + if (NULL != objectP->instanceList) |
| 301 | + { |
| 302 | + lwm2m_free(objectP->instanceList); |
| 303 | + objectP->instanceList = NULL; |
| 304 | + } |
| 305 | + |
| 306 | + lwm2m_free(objectP); |
| 307 | +} |
0 commit comments