Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
wakaama/object_device.c@0:f9d13e09cf11, 2017-04-24 (annotated)
- Committer:
- terencez
- Date:
- Mon Apr 24 23:03:31 2017 +0000
- Revision:
- 0:f9d13e09cf11
The first compiled submit.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
terencez | 0:f9d13e09cf11 | 1 | /******************************************************************************* |
terencez | 0:f9d13e09cf11 | 2 | * |
terencez | 0:f9d13e09cf11 | 3 | * Copyright (c) 2013, 2014 Intel Corporation and others. |
terencez | 0:f9d13e09cf11 | 4 | * All rights reserved. This program and the accompanying materials |
terencez | 0:f9d13e09cf11 | 5 | * are made available under the terms of the Eclipse Public License v1.0 |
terencez | 0:f9d13e09cf11 | 6 | * and Eclipse Distribution License v1.0 which accompany this distribution. |
terencez | 0:f9d13e09cf11 | 7 | * |
terencez | 0:f9d13e09cf11 | 8 | * The Eclipse Public License is available at |
terencez | 0:f9d13e09cf11 | 9 | * http://www.eclipse.org/legal/epl-v10.html |
terencez | 0:f9d13e09cf11 | 10 | * The Eclipse Distribution License is available at |
terencez | 0:f9d13e09cf11 | 11 | * http://www.eclipse.org/org/documents/edl-v10.php. |
terencez | 0:f9d13e09cf11 | 12 | * |
terencez | 0:f9d13e09cf11 | 13 | * Contributors: |
terencez | 0:f9d13e09cf11 | 14 | * David Navarro, Intel Corporation - initial API and implementation |
terencez | 0:f9d13e09cf11 | 15 | * domedambrosio - Please refer to git log |
terencez | 0:f9d13e09cf11 | 16 | * Fabien Fleutot - Please refer to git log |
terencez | 0:f9d13e09cf11 | 17 | * Axel Lorente - Please refer to git log |
terencez | 0:f9d13e09cf11 | 18 | * |
terencez | 0:f9d13e09cf11 | 19 | *******************************************************************************/ |
terencez | 0:f9d13e09cf11 | 20 | |
terencez | 0:f9d13e09cf11 | 21 | /* |
terencez | 0:f9d13e09cf11 | 22 | Copyright (c) 2013, 2014 Intel Corporation |
terencez | 0:f9d13e09cf11 | 23 | |
terencez | 0:f9d13e09cf11 | 24 | Redistribution and use in source and binary forms, with or without modification, |
terencez | 0:f9d13e09cf11 | 25 | are permitted provided that the following conditions are met: |
terencez | 0:f9d13e09cf11 | 26 | |
terencez | 0:f9d13e09cf11 | 27 | * Redistributions of source code must retain the above copyright notice, |
terencez | 0:f9d13e09cf11 | 28 | this list of conditions and the following disclaimer. |
terencez | 0:f9d13e09cf11 | 29 | * Redistributions in binary form must reproduce the above copyright notice, |
terencez | 0:f9d13e09cf11 | 30 | this list of conditions and the following disclaimer in the documentation |
terencez | 0:f9d13e09cf11 | 31 | and/or other materials provided with the distribution. |
terencez | 0:f9d13e09cf11 | 32 | * Neither the name of Intel Corporation nor the names of its contributors |
terencez | 0:f9d13e09cf11 | 33 | may be used to endorse or promote products derived from this software |
terencez | 0:f9d13e09cf11 | 34 | without specific prior written permission. |
terencez | 0:f9d13e09cf11 | 35 | |
terencez | 0:f9d13e09cf11 | 36 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
terencez | 0:f9d13e09cf11 | 37 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
terencez | 0:f9d13e09cf11 | 38 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
terencez | 0:f9d13e09cf11 | 39 | IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, |
terencez | 0:f9d13e09cf11 | 40 | INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
terencez | 0:f9d13e09cf11 | 41 | BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
terencez | 0:f9d13e09cf11 | 42 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
terencez | 0:f9d13e09cf11 | 43 | LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
terencez | 0:f9d13e09cf11 | 44 | OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
terencez | 0:f9d13e09cf11 | 45 | THE POSSIBILITY OF SUCH DAMAGE. |
terencez | 0:f9d13e09cf11 | 46 | |
terencez | 0:f9d13e09cf11 | 47 | David Navarro <david.navarro@intel.com> |
terencez | 0:f9d13e09cf11 | 48 | |
terencez | 0:f9d13e09cf11 | 49 | */ |
terencez | 0:f9d13e09cf11 | 50 | |
terencez | 0:f9d13e09cf11 | 51 | /* |
terencez | 0:f9d13e09cf11 | 52 | * This object is single instance only, and is mandatory to all LWM2M device as it describe the object such as its |
terencez | 0:f9d13e09cf11 | 53 | * manufacturer, model, etc... |
terencez | 0:f9d13e09cf11 | 54 | */ |
terencez | 0:f9d13e09cf11 | 55 | |
terencez | 0:f9d13e09cf11 | 56 | #include "liblwm2m.h" |
terencez | 0:f9d13e09cf11 | 57 | |
terencez | 0:f9d13e09cf11 | 58 | #include <stdio.h> |
terencez | 0:f9d13e09cf11 | 59 | #include <stdlib.h> |
terencez | 0:f9d13e09cf11 | 60 | #include <string.h> |
terencez | 0:f9d13e09cf11 | 61 | #include <ctype.h> |
terencez | 0:f9d13e09cf11 | 62 | |
terencez | 0:f9d13e09cf11 | 63 | |
terencez | 0:f9d13e09cf11 | 64 | #define PRV_MANUFACTURER "Open Mobile Alliance" |
terencez | 0:f9d13e09cf11 | 65 | #define PRV_MODEL_NUMBER "Lightweight M2M Client" |
terencez | 0:f9d13e09cf11 | 66 | #define PRV_SERIAL_NUMBER "345000123" |
terencez | 0:f9d13e09cf11 | 67 | #define PRV_FIRMWARE_VERSION "1.0" |
terencez | 0:f9d13e09cf11 | 68 | #define PRV_POWER_SOURCE_1 1 |
terencez | 0:f9d13e09cf11 | 69 | #define PRV_POWER_SOURCE_2 5 |
terencez | 0:f9d13e09cf11 | 70 | #define PRV_POWER_VOLTAGE_1 3800 |
terencez | 0:f9d13e09cf11 | 71 | #define PRV_POWER_VOLTAGE_2 5000 |
terencez | 0:f9d13e09cf11 | 72 | #define PRV_POWER_CURRENT_1 125 |
terencez | 0:f9d13e09cf11 | 73 | #define PRV_POWER_CURRENT_2 900 |
terencez | 0:f9d13e09cf11 | 74 | #define PRV_BATTERY_LEVEL 100 |
terencez | 0:f9d13e09cf11 | 75 | #define PRV_MEMORY_FREE 15 |
terencez | 0:f9d13e09cf11 | 76 | #define PRV_ERROR_CODE 0 |
terencez | 0:f9d13e09cf11 | 77 | #define PRV_BINDING_MODE "U" |
terencez | 0:f9d13e09cf11 | 78 | |
terencez | 0:f9d13e09cf11 | 79 | #define PRV_OFFSET_MAXLEN 7 //+HH:MM\0 at max |
terencez | 0:f9d13e09cf11 | 80 | #define PRV_TLV_BUFFER_SIZE 128 |
terencez | 0:f9d13e09cf11 | 81 | |
terencez | 0:f9d13e09cf11 | 82 | typedef struct |
terencez | 0:f9d13e09cf11 | 83 | { |
terencez | 0:f9d13e09cf11 | 84 | int64_t time; |
terencez | 0:f9d13e09cf11 | 85 | char time_offset[PRV_OFFSET_MAXLEN]; |
terencez | 0:f9d13e09cf11 | 86 | } device_data_t; |
terencez | 0:f9d13e09cf11 | 87 | |
terencez | 0:f9d13e09cf11 | 88 | |
terencez | 0:f9d13e09cf11 | 89 | // basic check that the time offset value is at ISO 8601 format |
terencez | 0:f9d13e09cf11 | 90 | // bug: +12:30 is considered a valid value by this function |
terencez | 0:f9d13e09cf11 | 91 | static int prv_check_time_offset(char * buffer, |
terencez | 0:f9d13e09cf11 | 92 | int length) |
terencez | 0:f9d13e09cf11 | 93 | { |
terencez | 0:f9d13e09cf11 | 94 | int min_index; |
terencez | 0:f9d13e09cf11 | 95 | |
terencez | 0:f9d13e09cf11 | 96 | if (length != 3 && length != 5 && length != 6) return 0; |
terencez | 0:f9d13e09cf11 | 97 | if (buffer[0] != '-' && buffer[0] != '+') return 0; |
terencez | 0:f9d13e09cf11 | 98 | switch (buffer[1]) |
terencez | 0:f9d13e09cf11 | 99 | { |
terencez | 0:f9d13e09cf11 | 100 | case '0': |
terencez | 0:f9d13e09cf11 | 101 | if (buffer[2] < '0' || buffer[2] > '9') return 0; |
terencez | 0:f9d13e09cf11 | 102 | break; |
terencez | 0:f9d13e09cf11 | 103 | case '1': |
terencez | 0:f9d13e09cf11 | 104 | if (buffer[2] < '0' || buffer[2] > '2') return 0; |
terencez | 0:f9d13e09cf11 | 105 | break; |
terencez | 0:f9d13e09cf11 | 106 | default: |
terencez | 0:f9d13e09cf11 | 107 | return 0; |
terencez | 0:f9d13e09cf11 | 108 | } |
terencez | 0:f9d13e09cf11 | 109 | switch (length) |
terencez | 0:f9d13e09cf11 | 110 | { |
terencez | 0:f9d13e09cf11 | 111 | case 3: |
terencez | 0:f9d13e09cf11 | 112 | return 1; |
terencez | 0:f9d13e09cf11 | 113 | case 5: |
terencez | 0:f9d13e09cf11 | 114 | min_index = 3; |
terencez | 0:f9d13e09cf11 | 115 | break; |
terencez | 0:f9d13e09cf11 | 116 | case 6: |
terencez | 0:f9d13e09cf11 | 117 | if (buffer[3] != ':') return 0; |
terencez | 0:f9d13e09cf11 | 118 | min_index = 4; |
terencez | 0:f9d13e09cf11 | 119 | break; |
terencez | 0:f9d13e09cf11 | 120 | default: |
terencez | 0:f9d13e09cf11 | 121 | // never happen |
terencez | 0:f9d13e09cf11 | 122 | return 0; |
terencez | 0:f9d13e09cf11 | 123 | } |
terencez | 0:f9d13e09cf11 | 124 | if (buffer[min_index] < '0' || buffer[min_index] > '5') return 0; |
terencez | 0:f9d13e09cf11 | 125 | if (buffer[min_index+1] < '0' || buffer[min_index+1] > '9') return 0; |
terencez | 0:f9d13e09cf11 | 126 | |
terencez | 0:f9d13e09cf11 | 127 | return 1; |
terencez | 0:f9d13e09cf11 | 128 | } |
terencez | 0:f9d13e09cf11 | 129 | |
terencez | 0:f9d13e09cf11 | 130 | static uint8_t prv_set_value(lwm2m_tlv_t * tlvP, |
terencez | 0:f9d13e09cf11 | 131 | device_data_t * devDataP) |
terencez | 0:f9d13e09cf11 | 132 | { |
terencez | 0:f9d13e09cf11 | 133 | // a simple switch structure is used to respond at the specified resource asked |
terencez | 0:f9d13e09cf11 | 134 | switch (tlvP->id) |
terencez | 0:f9d13e09cf11 | 135 | { |
terencez | 0:f9d13e09cf11 | 136 | case 0: |
terencez | 0:f9d13e09cf11 | 137 | tlvP->value = PRV_MANUFACTURER; |
terencez | 0:f9d13e09cf11 | 138 | tlvP->length = strlen(PRV_MANUFACTURER); |
terencez | 0:f9d13e09cf11 | 139 | tlvP->flags = LWM2M_TLV_FLAG_STATIC_DATA; |
terencez | 0:f9d13e09cf11 | 140 | tlvP->type = LWM2M_TYPE_RESSOURCE; |
terencez | 0:f9d13e09cf11 | 141 | return COAP_205_CONTENT; |
terencez | 0:f9d13e09cf11 | 142 | |
terencez | 0:f9d13e09cf11 | 143 | case 1: |
terencez | 0:f9d13e09cf11 | 144 | tlvP->value = PRV_MODEL_NUMBER; |
terencez | 0:f9d13e09cf11 | 145 | tlvP->length = strlen(PRV_MODEL_NUMBER); |
terencez | 0:f9d13e09cf11 | 146 | tlvP->flags = LWM2M_TLV_FLAG_STATIC_DATA; |
terencez | 0:f9d13e09cf11 | 147 | tlvP->type = LWM2M_TYPE_RESSOURCE; |
terencez | 0:f9d13e09cf11 | 148 | return COAP_205_CONTENT; |
terencez | 0:f9d13e09cf11 | 149 | |
terencez | 0:f9d13e09cf11 | 150 | case 2: |
terencez | 0:f9d13e09cf11 | 151 | tlvP->value = PRV_SERIAL_NUMBER; |
terencez | 0:f9d13e09cf11 | 152 | tlvP->length = strlen(PRV_SERIAL_NUMBER); |
terencez | 0:f9d13e09cf11 | 153 | tlvP->flags = LWM2M_TLV_FLAG_STATIC_DATA; |
terencez | 0:f9d13e09cf11 | 154 | tlvP->type = LWM2M_TYPE_RESSOURCE; |
terencez | 0:f9d13e09cf11 | 155 | return COAP_205_CONTENT; |
terencez | 0:f9d13e09cf11 | 156 | |
terencez | 0:f9d13e09cf11 | 157 | case 3: |
terencez | 0:f9d13e09cf11 | 158 | tlvP->value = PRV_FIRMWARE_VERSION; |
terencez | 0:f9d13e09cf11 | 159 | tlvP->length = strlen(PRV_FIRMWARE_VERSION); |
terencez | 0:f9d13e09cf11 | 160 | tlvP->flags = LWM2M_TLV_FLAG_STATIC_DATA; |
terencez | 0:f9d13e09cf11 | 161 | tlvP->type = LWM2M_TYPE_RESSOURCE; |
terencez | 0:f9d13e09cf11 | 162 | return COAP_205_CONTENT; |
terencez | 0:f9d13e09cf11 | 163 | |
terencez | 0:f9d13e09cf11 | 164 | case 4: |
terencez | 0:f9d13e09cf11 | 165 | return COAP_405_METHOD_NOT_ALLOWED; |
terencez | 0:f9d13e09cf11 | 166 | |
terencez | 0:f9d13e09cf11 | 167 | case 5: |
terencez | 0:f9d13e09cf11 | 168 | return COAP_405_METHOD_NOT_ALLOWED; |
terencez | 0:f9d13e09cf11 | 169 | |
terencez | 0:f9d13e09cf11 | 170 | case 6: |
terencez | 0:f9d13e09cf11 | 171 | { |
terencez | 0:f9d13e09cf11 | 172 | lwm2m_tlv_t * subTlvP; |
terencez | 0:f9d13e09cf11 | 173 | |
terencez | 0:f9d13e09cf11 | 174 | subTlvP = lwm2m_tlv_new(2); |
terencez | 0:f9d13e09cf11 | 175 | |
terencez | 0:f9d13e09cf11 | 176 | subTlvP[0].flags = 0; |
terencez | 0:f9d13e09cf11 | 177 | subTlvP[0].id = 0; |
terencez | 0:f9d13e09cf11 | 178 | subTlvP[0].type = LWM2M_TYPE_RESSOURCE_INSTANCE; |
terencez | 0:f9d13e09cf11 | 179 | lwm2m_tlv_encode_int(PRV_POWER_SOURCE_1, subTlvP); |
terencez | 0:f9d13e09cf11 | 180 | if (0 == subTlvP[0].length) |
terencez | 0:f9d13e09cf11 | 181 | { |
terencez | 0:f9d13e09cf11 | 182 | lwm2m_tlv_free(2, subTlvP); |
terencez | 0:f9d13e09cf11 | 183 | return COAP_500_INTERNAL_SERVER_ERROR; |
terencez | 0:f9d13e09cf11 | 184 | } |
terencez | 0:f9d13e09cf11 | 185 | |
terencez | 0:f9d13e09cf11 | 186 | subTlvP[1].flags = 0; |
terencez | 0:f9d13e09cf11 | 187 | subTlvP[1].id = 1; |
terencez | 0:f9d13e09cf11 | 188 | subTlvP[1].type = LWM2M_TYPE_RESSOURCE_INSTANCE; |
terencez | 0:f9d13e09cf11 | 189 | lwm2m_tlv_encode_int(PRV_POWER_SOURCE_2, subTlvP + 1); |
terencez | 0:f9d13e09cf11 | 190 | if (0 == subTlvP[1].length) |
terencez | 0:f9d13e09cf11 | 191 | { |
terencez | 0:f9d13e09cf11 | 192 | lwm2m_tlv_free(2, subTlvP); |
terencez | 0:f9d13e09cf11 | 193 | return COAP_500_INTERNAL_SERVER_ERROR; |
terencez | 0:f9d13e09cf11 | 194 | } |
terencez | 0:f9d13e09cf11 | 195 | |
terencez | 0:f9d13e09cf11 | 196 | tlvP->flags = 0; |
terencez | 0:f9d13e09cf11 | 197 | tlvP->type = LWM2M_TYPE_MULTIPLE_RESSOURCE; |
terencez | 0:f9d13e09cf11 | 198 | tlvP->length = 2; |
terencez | 0:f9d13e09cf11 | 199 | tlvP->value = (uint8_t *)subTlvP; |
terencez | 0:f9d13e09cf11 | 200 | |
terencez | 0:f9d13e09cf11 | 201 | return COAP_205_CONTENT; |
terencez | 0:f9d13e09cf11 | 202 | } |
terencez | 0:f9d13e09cf11 | 203 | |
terencez | 0:f9d13e09cf11 | 204 | case 7: |
terencez | 0:f9d13e09cf11 | 205 | { |
terencez | 0:f9d13e09cf11 | 206 | lwm2m_tlv_t * subTlvP; |
terencez | 0:f9d13e09cf11 | 207 | |
terencez | 0:f9d13e09cf11 | 208 | subTlvP = lwm2m_tlv_new(2); |
terencez | 0:f9d13e09cf11 | 209 | |
terencez | 0:f9d13e09cf11 | 210 | subTlvP[0].flags = 0; |
terencez | 0:f9d13e09cf11 | 211 | subTlvP[0].id = 0; |
terencez | 0:f9d13e09cf11 | 212 | subTlvP[0].type = LWM2M_TYPE_RESSOURCE_INSTANCE; |
terencez | 0:f9d13e09cf11 | 213 | lwm2m_tlv_encode_int(PRV_POWER_VOLTAGE_1, subTlvP); |
terencez | 0:f9d13e09cf11 | 214 | if (0 == subTlvP[0].length) |
terencez | 0:f9d13e09cf11 | 215 | { |
terencez | 0:f9d13e09cf11 | 216 | lwm2m_tlv_free(2, subTlvP); |
terencez | 0:f9d13e09cf11 | 217 | return COAP_500_INTERNAL_SERVER_ERROR; |
terencez | 0:f9d13e09cf11 | 218 | } |
terencez | 0:f9d13e09cf11 | 219 | |
terencez | 0:f9d13e09cf11 | 220 | subTlvP[1].flags = 0; |
terencez | 0:f9d13e09cf11 | 221 | subTlvP[1].id = 1; |
terencez | 0:f9d13e09cf11 | 222 | subTlvP[1].type = LWM2M_TYPE_RESSOURCE_INSTANCE; |
terencez | 0:f9d13e09cf11 | 223 | lwm2m_tlv_encode_int(PRV_POWER_VOLTAGE_2, subTlvP + 1); |
terencez | 0:f9d13e09cf11 | 224 | if (0 == subTlvP[1].length) |
terencez | 0:f9d13e09cf11 | 225 | { |
terencez | 0:f9d13e09cf11 | 226 | lwm2m_tlv_free(2, subTlvP); |
terencez | 0:f9d13e09cf11 | 227 | return COAP_500_INTERNAL_SERVER_ERROR; |
terencez | 0:f9d13e09cf11 | 228 | } |
terencez | 0:f9d13e09cf11 | 229 | |
terencez | 0:f9d13e09cf11 | 230 | tlvP->flags = 0; |
terencez | 0:f9d13e09cf11 | 231 | tlvP->type = LWM2M_TYPE_MULTIPLE_RESSOURCE; |
terencez | 0:f9d13e09cf11 | 232 | tlvP->length = 2; |
terencez | 0:f9d13e09cf11 | 233 | tlvP->value = (uint8_t *)subTlvP; |
terencez | 0:f9d13e09cf11 | 234 | |
terencez | 0:f9d13e09cf11 | 235 | return COAP_205_CONTENT; |
terencez | 0:f9d13e09cf11 | 236 | } |
terencez | 0:f9d13e09cf11 | 237 | |
terencez | 0:f9d13e09cf11 | 238 | case 8: |
terencez | 0:f9d13e09cf11 | 239 | { |
terencez | 0:f9d13e09cf11 | 240 | lwm2m_tlv_t * subTlvP; |
terencez | 0:f9d13e09cf11 | 241 | |
terencez | 0:f9d13e09cf11 | 242 | subTlvP = lwm2m_tlv_new(2); |
terencez | 0:f9d13e09cf11 | 243 | |
terencez | 0:f9d13e09cf11 | 244 | subTlvP[0].flags = 0; |
terencez | 0:f9d13e09cf11 | 245 | subTlvP[0].id = 0; |
terencez | 0:f9d13e09cf11 | 246 | subTlvP[0].type = LWM2M_TYPE_RESSOURCE_INSTANCE; |
terencez | 0:f9d13e09cf11 | 247 | lwm2m_tlv_encode_int(PRV_POWER_CURRENT_1, subTlvP); |
terencez | 0:f9d13e09cf11 | 248 | if (0 == subTlvP[0].length) |
terencez | 0:f9d13e09cf11 | 249 | { |
terencez | 0:f9d13e09cf11 | 250 | lwm2m_tlv_free(2, subTlvP); |
terencez | 0:f9d13e09cf11 | 251 | return COAP_500_INTERNAL_SERVER_ERROR; |
terencez | 0:f9d13e09cf11 | 252 | } |
terencez | 0:f9d13e09cf11 | 253 | |
terencez | 0:f9d13e09cf11 | 254 | subTlvP[1].flags = 0; |
terencez | 0:f9d13e09cf11 | 255 | subTlvP[1].id = 1; |
terencez | 0:f9d13e09cf11 | 256 | subTlvP[1].type = LWM2M_TYPE_RESSOURCE_INSTANCE; |
terencez | 0:f9d13e09cf11 | 257 | lwm2m_tlv_encode_int(PRV_POWER_CURRENT_2, subTlvP + 1); |
terencez | 0:f9d13e09cf11 | 258 | if (0 == subTlvP[1].length) |
terencez | 0:f9d13e09cf11 | 259 | { |
terencez | 0:f9d13e09cf11 | 260 | lwm2m_tlv_free(2, subTlvP); |
terencez | 0:f9d13e09cf11 | 261 | return COAP_500_INTERNAL_SERVER_ERROR; |
terencez | 0:f9d13e09cf11 | 262 | } |
terencez | 0:f9d13e09cf11 | 263 | |
terencez | 0:f9d13e09cf11 | 264 | tlvP->flags = 0; |
terencez | 0:f9d13e09cf11 | 265 | tlvP->type = LWM2M_TYPE_MULTIPLE_RESSOURCE; |
terencez | 0:f9d13e09cf11 | 266 | tlvP->length = 2; |
terencez | 0:f9d13e09cf11 | 267 | tlvP->value = (uint8_t *)subTlvP; |
terencez | 0:f9d13e09cf11 | 268 | |
terencez | 0:f9d13e09cf11 | 269 | return COAP_205_CONTENT; |
terencez | 0:f9d13e09cf11 | 270 | } |
terencez | 0:f9d13e09cf11 | 271 | |
terencez | 0:f9d13e09cf11 | 272 | case 9: |
terencez | 0:f9d13e09cf11 | 273 | lwm2m_tlv_encode_int(PRV_BATTERY_LEVEL, tlvP); |
terencez | 0:f9d13e09cf11 | 274 | tlvP->type = LWM2M_TYPE_RESSOURCE; |
terencez | 0:f9d13e09cf11 | 275 | |
terencez | 0:f9d13e09cf11 | 276 | if (0 != tlvP->length) return COAP_205_CONTENT; |
terencez | 0:f9d13e09cf11 | 277 | else return COAP_500_INTERNAL_SERVER_ERROR; |
terencez | 0:f9d13e09cf11 | 278 | |
terencez | 0:f9d13e09cf11 | 279 | case 10: |
terencez | 0:f9d13e09cf11 | 280 | lwm2m_tlv_encode_int(PRV_MEMORY_FREE, tlvP); |
terencez | 0:f9d13e09cf11 | 281 | tlvP->type = LWM2M_TYPE_RESSOURCE; |
terencez | 0:f9d13e09cf11 | 282 | |
terencez | 0:f9d13e09cf11 | 283 | if (0 != tlvP->length) return COAP_205_CONTENT; |
terencez | 0:f9d13e09cf11 | 284 | else return COAP_500_INTERNAL_SERVER_ERROR; |
terencez | 0:f9d13e09cf11 | 285 | |
terencez | 0:f9d13e09cf11 | 286 | case 11: |
terencez | 0:f9d13e09cf11 | 287 | lwm2m_tlv_encode_int(PRV_ERROR_CODE, tlvP); |
terencez | 0:f9d13e09cf11 | 288 | tlvP->type = LWM2M_TYPE_RESSOURCE; |
terencez | 0:f9d13e09cf11 | 289 | |
terencez | 0:f9d13e09cf11 | 290 | if (0 != tlvP->length) return COAP_205_CONTENT; |
terencez | 0:f9d13e09cf11 | 291 | else return COAP_500_INTERNAL_SERVER_ERROR; |
terencez | 0:f9d13e09cf11 | 292 | |
terencez | 0:f9d13e09cf11 | 293 | case 12: |
terencez | 0:f9d13e09cf11 | 294 | return COAP_405_METHOD_NOT_ALLOWED; |
terencez | 0:f9d13e09cf11 | 295 | |
terencez | 0:f9d13e09cf11 | 296 | case 13: |
terencez | 0:f9d13e09cf11 | 297 | lwm2m_tlv_encode_int(devDataP->time, tlvP); |
terencez | 0:f9d13e09cf11 | 298 | tlvP->type = LWM2M_TYPE_RESSOURCE; |
terencez | 0:f9d13e09cf11 | 299 | |
terencez | 0:f9d13e09cf11 | 300 | if (0 != tlvP->length) return COAP_205_CONTENT; |
terencez | 0:f9d13e09cf11 | 301 | else return COAP_500_INTERNAL_SERVER_ERROR; |
terencez | 0:f9d13e09cf11 | 302 | |
terencez | 0:f9d13e09cf11 | 303 | case 14: |
terencez | 0:f9d13e09cf11 | 304 | tlvP->value = devDataP->time_offset; |
terencez | 0:f9d13e09cf11 | 305 | tlvP->length = strlen(devDataP->time_offset); |
terencez | 0:f9d13e09cf11 | 306 | tlvP->flags = LWM2M_TLV_FLAG_STATIC_DATA; |
terencez | 0:f9d13e09cf11 | 307 | tlvP->type = LWM2M_TYPE_RESSOURCE; |
terencez | 0:f9d13e09cf11 | 308 | return COAP_205_CONTENT; |
terencez | 0:f9d13e09cf11 | 309 | |
terencez | 0:f9d13e09cf11 | 310 | case 15: |
terencez | 0:f9d13e09cf11 | 311 | tlvP->value = PRV_BINDING_MODE; |
terencez | 0:f9d13e09cf11 | 312 | tlvP->length = strlen(PRV_BINDING_MODE); |
terencez | 0:f9d13e09cf11 | 313 | tlvP->flags = LWM2M_TLV_FLAG_STATIC_DATA; |
terencez | 0:f9d13e09cf11 | 314 | tlvP->type = LWM2M_TYPE_RESSOURCE; |
terencez | 0:f9d13e09cf11 | 315 | return COAP_205_CONTENT; |
terencez | 0:f9d13e09cf11 | 316 | |
terencez | 0:f9d13e09cf11 | 317 | default: |
terencez | 0:f9d13e09cf11 | 318 | return COAP_404_NOT_FOUND; |
terencez | 0:f9d13e09cf11 | 319 | } |
terencez | 0:f9d13e09cf11 | 320 | } |
terencez | 0:f9d13e09cf11 | 321 | |
terencez | 0:f9d13e09cf11 | 322 | static uint8_t prv_device_read(uint16_t instanceId, |
terencez | 0:f9d13e09cf11 | 323 | int * numDataP, |
terencez | 0:f9d13e09cf11 | 324 | lwm2m_tlv_t ** dataArrayP, |
terencez | 0:f9d13e09cf11 | 325 | lwm2m_object_t * objectP) |
terencez | 0:f9d13e09cf11 | 326 | { |
terencez | 0:f9d13e09cf11 | 327 | uint8_t result; |
terencez | 0:f9d13e09cf11 | 328 | int i; |
terencez | 0:f9d13e09cf11 | 329 | |
terencez | 0:f9d13e09cf11 | 330 | // this is a single instance object |
terencez | 0:f9d13e09cf11 | 331 | if (instanceId != 0) |
terencez | 0:f9d13e09cf11 | 332 | { |
terencez | 0:f9d13e09cf11 | 333 | return COAP_404_NOT_FOUND; |
terencez | 0:f9d13e09cf11 | 334 | } |
terencez | 0:f9d13e09cf11 | 335 | |
terencez | 0:f9d13e09cf11 | 336 | // is the server asking for the full object ? |
terencez | 0:f9d13e09cf11 | 337 | if (*numDataP == 0) |
terencez | 0:f9d13e09cf11 | 338 | { |
terencez | 0:f9d13e09cf11 | 339 | uint16_t resList[] = {0, 1, 2, 3, 6, 7, 8, 9, 10, 11, 13, 14, 15}; |
terencez | 0:f9d13e09cf11 | 340 | int nbRes = sizeof(resList)/sizeof(uint16_t); |
terencez | 0:f9d13e09cf11 | 341 | |
terencez | 0:f9d13e09cf11 | 342 | *dataArrayP = lwm2m_tlv_new(nbRes); |
terencez | 0:f9d13e09cf11 | 343 | if (*dataArrayP == NULL) return COAP_500_INTERNAL_SERVER_ERROR; |
terencez | 0:f9d13e09cf11 | 344 | *numDataP = nbRes; |
terencez | 0:f9d13e09cf11 | 345 | for (i = 0 ; i < nbRes ; i++) |
terencez | 0:f9d13e09cf11 | 346 | { |
terencez | 0:f9d13e09cf11 | 347 | (*dataArrayP)[i].id = resList[i]; |
terencez | 0:f9d13e09cf11 | 348 | } |
terencez | 0:f9d13e09cf11 | 349 | } |
terencez | 0:f9d13e09cf11 | 350 | |
terencez | 0:f9d13e09cf11 | 351 | i = 0; |
terencez | 0:f9d13e09cf11 | 352 | do |
terencez | 0:f9d13e09cf11 | 353 | { |
terencez | 0:f9d13e09cf11 | 354 | result = prv_set_value((*dataArrayP) + i, (device_data_t*)(objectP->userData)); |
terencez | 0:f9d13e09cf11 | 355 | i++; |
terencez | 0:f9d13e09cf11 | 356 | } while (i < *numDataP && result == COAP_205_CONTENT); |
terencez | 0:f9d13e09cf11 | 357 | |
terencez | 0:f9d13e09cf11 | 358 | return result; |
terencez | 0:f9d13e09cf11 | 359 | } |
terencez | 0:f9d13e09cf11 | 360 | |
terencez | 0:f9d13e09cf11 | 361 | static uint8_t prv_device_write(uint16_t instanceId, |
terencez | 0:f9d13e09cf11 | 362 | int numData, |
terencez | 0:f9d13e09cf11 | 363 | lwm2m_tlv_t * dataArray, |
terencez | 0:f9d13e09cf11 | 364 | lwm2m_object_t * objectP) |
terencez | 0:f9d13e09cf11 | 365 | { |
terencez | 0:f9d13e09cf11 | 366 | int i; |
terencez | 0:f9d13e09cf11 | 367 | uint8_t result; |
terencez | 0:f9d13e09cf11 | 368 | |
terencez | 0:f9d13e09cf11 | 369 | // this is a single instance object |
terencez | 0:f9d13e09cf11 | 370 | if (instanceId != 0) |
terencez | 0:f9d13e09cf11 | 371 | { |
terencez | 0:f9d13e09cf11 | 372 | return COAP_404_NOT_FOUND; |
terencez | 0:f9d13e09cf11 | 373 | } |
terencez | 0:f9d13e09cf11 | 374 | |
terencez | 0:f9d13e09cf11 | 375 | i = 0; |
terencez | 0:f9d13e09cf11 | 376 | |
terencez | 0:f9d13e09cf11 | 377 | do |
terencez | 0:f9d13e09cf11 | 378 | { |
terencez | 0:f9d13e09cf11 | 379 | switch (dataArray[i].id) |
terencez | 0:f9d13e09cf11 | 380 | { |
terencez | 0:f9d13e09cf11 | 381 | case 13: |
terencez | 0:f9d13e09cf11 | 382 | if (1 == lwm2m_tlv_decode_int(dataArray + i, &((device_data_t*)(objectP->userData))->time)) |
terencez | 0:f9d13e09cf11 | 383 | { |
terencez | 0:f9d13e09cf11 | 384 | result = COAP_204_CHANGED; |
terencez | 0:f9d13e09cf11 | 385 | } |
terencez | 0:f9d13e09cf11 | 386 | else |
terencez | 0:f9d13e09cf11 | 387 | { |
terencez | 0:f9d13e09cf11 | 388 | result = COAP_400_BAD_REQUEST; |
terencez | 0:f9d13e09cf11 | 389 | } |
terencez | 0:f9d13e09cf11 | 390 | break; |
terencez | 0:f9d13e09cf11 | 391 | |
terencez | 0:f9d13e09cf11 | 392 | case 14: |
terencez | 0:f9d13e09cf11 | 393 | if (1 == prv_check_time_offset(dataArray[i].value, dataArray[i].length)) |
terencez | 0:f9d13e09cf11 | 394 | { |
terencez | 0:f9d13e09cf11 | 395 | strncpy(((device_data_t*)(objectP->userData))->time_offset, dataArray[i].value, dataArray[i].length); |
terencez | 0:f9d13e09cf11 | 396 | ((device_data_t*)(objectP->userData))->time_offset[dataArray[i].length] = 0; |
terencez | 0:f9d13e09cf11 | 397 | result = COAP_204_CHANGED; |
terencez | 0:f9d13e09cf11 | 398 | } |
terencez | 0:f9d13e09cf11 | 399 | else |
terencez | 0:f9d13e09cf11 | 400 | { |
terencez | 0:f9d13e09cf11 | 401 | result = COAP_400_BAD_REQUEST; |
terencez | 0:f9d13e09cf11 | 402 | } |
terencez | 0:f9d13e09cf11 | 403 | break; |
terencez | 0:f9d13e09cf11 | 404 | |
terencez | 0:f9d13e09cf11 | 405 | default: |
terencez | 0:f9d13e09cf11 | 406 | result = COAP_405_METHOD_NOT_ALLOWED; |
terencez | 0:f9d13e09cf11 | 407 | } |
terencez | 0:f9d13e09cf11 | 408 | |
terencez | 0:f9d13e09cf11 | 409 | i++; |
terencez | 0:f9d13e09cf11 | 410 | } while (i < numData && result == COAP_204_CHANGED); |
terencez | 0:f9d13e09cf11 | 411 | |
terencez | 0:f9d13e09cf11 | 412 | return result; |
terencez | 0:f9d13e09cf11 | 413 | } |
terencez | 0:f9d13e09cf11 | 414 | |
terencez | 0:f9d13e09cf11 | 415 | static uint8_t prv_device_execute(uint16_t instanceId, |
terencez | 0:f9d13e09cf11 | 416 | uint16_t resourceId, |
terencez | 0:f9d13e09cf11 | 417 | char * buffer, |
terencez | 0:f9d13e09cf11 | 418 | int length, |
terencez | 0:f9d13e09cf11 | 419 | lwm2m_object_t * objectP) |
terencez | 0:f9d13e09cf11 | 420 | { |
terencez | 0:f9d13e09cf11 | 421 | // this is a single instance object |
terencez | 0:f9d13e09cf11 | 422 | if (instanceId != 0) |
terencez | 0:f9d13e09cf11 | 423 | { |
terencez | 0:f9d13e09cf11 | 424 | return COAP_404_NOT_FOUND; |
terencez | 0:f9d13e09cf11 | 425 | } |
terencez | 0:f9d13e09cf11 | 426 | |
terencez | 0:f9d13e09cf11 | 427 | if (length != 0) return COAP_400_BAD_REQUEST; |
terencez | 0:f9d13e09cf11 | 428 | |
terencez | 0:f9d13e09cf11 | 429 | switch (resourceId) |
terencez | 0:f9d13e09cf11 | 430 | { |
terencez | 0:f9d13e09cf11 | 431 | case 4: |
terencez | 0:f9d13e09cf11 | 432 | fprintf(stdout, "\n\t REBOOT\r\n\n"); |
terencez | 0:f9d13e09cf11 | 433 | return COAP_204_CHANGED; |
terencez | 0:f9d13e09cf11 | 434 | case 5: |
terencez | 0:f9d13e09cf11 | 435 | fprintf(stdout, "\n\t FACTORY RESET\r\n\n"); |
terencez | 0:f9d13e09cf11 | 436 | return COAP_204_CHANGED; |
terencez | 0:f9d13e09cf11 | 437 | case 12: |
terencez | 0:f9d13e09cf11 | 438 | fprintf(stdout, "\n\t RESET ERROR CODE\r\n\n"); |
terencez | 0:f9d13e09cf11 | 439 | return COAP_204_CHANGED; |
terencez | 0:f9d13e09cf11 | 440 | default: |
terencez | 0:f9d13e09cf11 | 441 | return COAP_405_METHOD_NOT_ALLOWED; |
terencez | 0:f9d13e09cf11 | 442 | } |
terencez | 0:f9d13e09cf11 | 443 | } |
terencez | 0:f9d13e09cf11 | 444 | |
terencez | 0:f9d13e09cf11 | 445 | lwm2m_object_t * get_object_device() |
terencez | 0:f9d13e09cf11 | 446 | { |
terencez | 0:f9d13e09cf11 | 447 | /* |
terencez | 0:f9d13e09cf11 | 448 | * The get_object_device function create the object itself and return a pointer to the structure that represent it. |
terencez | 0:f9d13e09cf11 | 449 | */ |
terencez | 0:f9d13e09cf11 | 450 | lwm2m_object_t * deviceObj; |
terencez | 0:f9d13e09cf11 | 451 | |
terencez | 0:f9d13e09cf11 | 452 | deviceObj = (lwm2m_object_t *)malloc(sizeof(lwm2m_object_t)); |
terencez | 0:f9d13e09cf11 | 453 | |
terencez | 0:f9d13e09cf11 | 454 | if (NULL != deviceObj) |
terencez | 0:f9d13e09cf11 | 455 | { |
terencez | 0:f9d13e09cf11 | 456 | memset(deviceObj, 0, sizeof(lwm2m_object_t)); |
terencez | 0:f9d13e09cf11 | 457 | |
terencez | 0:f9d13e09cf11 | 458 | /* |
terencez | 0:f9d13e09cf11 | 459 | * It assign his unique ID |
terencez | 0:f9d13e09cf11 | 460 | * The 3 is the standard ID for the mandatory object "Object device". |
terencez | 0:f9d13e09cf11 | 461 | */ |
terencez | 0:f9d13e09cf11 | 462 | deviceObj->objID = 3; |
terencez | 0:f9d13e09cf11 | 463 | |
terencez | 0:f9d13e09cf11 | 464 | /* |
terencez | 0:f9d13e09cf11 | 465 | * And the private function that will access the object. |
terencez | 0:f9d13e09cf11 | 466 | * Those function will be called when a read/write/execute query is made by the server. In fact the library don't need to |
terencez | 0:f9d13e09cf11 | 467 | * know the resources of the object, only the server does. |
terencez | 0:f9d13e09cf11 | 468 | */ |
terencez | 0:f9d13e09cf11 | 469 | deviceObj->readFunc = prv_device_read; |
terencez | 0:f9d13e09cf11 | 470 | deviceObj->writeFunc = prv_device_write; |
terencez | 0:f9d13e09cf11 | 471 | deviceObj->executeFunc = prv_device_execute; |
terencez | 0:f9d13e09cf11 | 472 | deviceObj->userData = malloc(sizeof(device_data_t)); |
terencez | 0:f9d13e09cf11 | 473 | |
terencez | 0:f9d13e09cf11 | 474 | /* |
terencez | 0:f9d13e09cf11 | 475 | * Also some user data can be stored in the object with a private structure containing the needed variables |
terencez | 0:f9d13e09cf11 | 476 | */ |
terencez | 0:f9d13e09cf11 | 477 | if (NULL != deviceObj->userData) |
terencez | 0:f9d13e09cf11 | 478 | { |
terencez | 0:f9d13e09cf11 | 479 | ((device_data_t*)deviceObj->userData)->time = 1367491215; |
terencez | 0:f9d13e09cf11 | 480 | strcpy(((device_data_t*)deviceObj->userData)->time_offset, "+02:00"); |
terencez | 0:f9d13e09cf11 | 481 | } |
terencez | 0:f9d13e09cf11 | 482 | else |
terencez | 0:f9d13e09cf11 | 483 | { |
terencez | 0:f9d13e09cf11 | 484 | free(deviceObj); |
terencez | 0:f9d13e09cf11 | 485 | deviceObj = NULL; |
terencez | 0:f9d13e09cf11 | 486 | } |
terencez | 0:f9d13e09cf11 | 487 | } |
terencez | 0:f9d13e09cf11 | 488 | |
terencez | 0:f9d13e09cf11 | 489 | return deviceObj; |
terencez | 0:f9d13e09cf11 | 490 | } |