FRDM K64F Metronome

Committer:
ram54288
Date:
Sun May 14 18:37:05 2017 +0000
Revision:
0:dbad57390bd1
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ram54288 0:dbad57390bd1 1 /*
ram54288 0:dbad57390bd1 2 * Copyright (c) 2015 ARM Limited. All rights reserved.
ram54288 0:dbad57390bd1 3 * SPDX-License-Identifier: Apache-2.0
ram54288 0:dbad57390bd1 4 * Licensed under the Apache License, Version 2.0 (the License); you may
ram54288 0:dbad57390bd1 5 * not use this file except in compliance with the License.
ram54288 0:dbad57390bd1 6 * You may obtain a copy of the License at
ram54288 0:dbad57390bd1 7 *
ram54288 0:dbad57390bd1 8 * http://www.apache.org/licenses/LICENSE-2.0
ram54288 0:dbad57390bd1 9 *
ram54288 0:dbad57390bd1 10 * Unless required by applicable law or agreed to in writing, software
ram54288 0:dbad57390bd1 11 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
ram54288 0:dbad57390bd1 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
ram54288 0:dbad57390bd1 13 * See the License for the specific language governing permissions and
ram54288 0:dbad57390bd1 14 * limitations under the License.
ram54288 0:dbad57390bd1 15 */
ram54288 0:dbad57390bd1 16
ram54288 0:dbad57390bd1 17 #include "mbed-client/m2mfirmware.h"
ram54288 0:dbad57390bd1 18 #include "mbed-client/m2mconstants.h"
ram54288 0:dbad57390bd1 19 #include "mbed-client/m2mobject.h"
ram54288 0:dbad57390bd1 20 #include "mbed-client/m2mobjectinstance.h"
ram54288 0:dbad57390bd1 21 #include "mbed-client/m2mresource.h"
ram54288 0:dbad57390bd1 22 #include "include/nsdlaccesshelper.h"
ram54288 0:dbad57390bd1 23
ram54288 0:dbad57390bd1 24 #define BUFFER_SIZE 21
ram54288 0:dbad57390bd1 25 #define TRACE_GROUP "mClt"
ram54288 0:dbad57390bd1 26
ram54288 0:dbad57390bd1 27 M2MFirmware* M2MFirmware::_instance = NULL;
ram54288 0:dbad57390bd1 28
ram54288 0:dbad57390bd1 29 M2MFirmware* M2MFirmware::get_instance()
ram54288 0:dbad57390bd1 30 {
ram54288 0:dbad57390bd1 31 if(_instance == NULL) {
ram54288 0:dbad57390bd1 32 _instance = new M2MFirmware();
ram54288 0:dbad57390bd1 33 }
ram54288 0:dbad57390bd1 34 return _instance;
ram54288 0:dbad57390bd1 35 }
ram54288 0:dbad57390bd1 36
ram54288 0:dbad57390bd1 37 void M2MFirmware::delete_instance()
ram54288 0:dbad57390bd1 38 {
ram54288 0:dbad57390bd1 39 delete _instance;
ram54288 0:dbad57390bd1 40 _instance = NULL;
ram54288 0:dbad57390bd1 41 }
ram54288 0:dbad57390bd1 42
ram54288 0:dbad57390bd1 43 M2MFirmware::M2MFirmware()
ram54288 0:dbad57390bd1 44 : M2MObject(M2M_FIRMWARE_ID, stringdup(M2M_FIRMWARE_ID))
ram54288 0:dbad57390bd1 45 {
ram54288 0:dbad57390bd1 46 M2MBase::set_register_uri(false);
ram54288 0:dbad57390bd1 47 M2MBase::set_operation(M2MBase::GET_PUT_ALLOWED);
ram54288 0:dbad57390bd1 48 _firmware_instance = M2MObject::create_object_instance();
ram54288 0:dbad57390bd1 49 if(_firmware_instance) {
ram54288 0:dbad57390bd1 50 _firmware_instance->set_operation(M2MBase::GET_PUT_ALLOWED);
ram54288 0:dbad57390bd1 51 create_mandatory_resources();
ram54288 0:dbad57390bd1 52 }
ram54288 0:dbad57390bd1 53 }
ram54288 0:dbad57390bd1 54
ram54288 0:dbad57390bd1 55 M2MFirmware::~M2MFirmware()
ram54288 0:dbad57390bd1 56 {
ram54288 0:dbad57390bd1 57 }
ram54288 0:dbad57390bd1 58
ram54288 0:dbad57390bd1 59 // Conditionally put the static part of parameter struct into flash.
ram54288 0:dbad57390bd1 60 // Unfortunately this can't be done yet by default as there is old API which
ram54288 0:dbad57390bd1 61 // may be used to modify the values in sn_nsdl_static_resource_parameters_s.
ram54288 0:dbad57390bd1 62 #ifdef MEMORY_OPTIMIZED_API
ram54288 0:dbad57390bd1 63 #define STATIC_PARAM_TYPE const
ram54288 0:dbad57390bd1 64 #else
ram54288 0:dbad57390bd1 65 #define STATIC_PARAM_TYPE
ram54288 0:dbad57390bd1 66 #endif
ram54288 0:dbad57390bd1 67
ram54288 0:dbad57390bd1 68 #define PACKAGE_PATH FIRMWARE_PATH_PREFIX FIRMWARE_PACKAGE
ram54288 0:dbad57390bd1 69
ram54288 0:dbad57390bd1 70 STATIC_PARAM_TYPE
ram54288 0:dbad57390bd1 71 static sn_nsdl_static_resource_parameters_s firmware_package_params_static = {
ram54288 0:dbad57390bd1 72 (char*)OMA_RESOURCE_TYPE, // resource_type_ptr
ram54288 0:dbad57390bd1 73 (char*)"", // interface_description_ptr
ram54288 0:dbad57390bd1 74 (uint8_t*)PACKAGE_PATH, // path
ram54288 0:dbad57390bd1 75 (uint8_t*)"", // resource
ram54288 0:dbad57390bd1 76 5, // strlen("5/0/0")
ram54288 0:dbad57390bd1 77 0, // resourcelen
ram54288 0:dbad57390bd1 78 false, // external_memory_block
ram54288 0:dbad57390bd1 79 SN_GRS_DYNAMIC, // mode
ram54288 0:dbad57390bd1 80 false // free_on_delete
ram54288 0:dbad57390bd1 81 };
ram54288 0:dbad57390bd1 82
ram54288 0:dbad57390bd1 83 #define PACKAGE_URI_PATH FIRMWARE_PATH_PREFIX FIRMWARE_PACKAGE_URI
ram54288 0:dbad57390bd1 84
ram54288 0:dbad57390bd1 85 STATIC_PARAM_TYPE
ram54288 0:dbad57390bd1 86 static sn_nsdl_static_resource_parameters_s firmware_package_uri_params_static = {
ram54288 0:dbad57390bd1 87 (char*)OMA_RESOURCE_TYPE, // resource_type_ptr
ram54288 0:dbad57390bd1 88 (char*)"", // interface_description_ptr
ram54288 0:dbad57390bd1 89 (uint8_t*)PACKAGE_URI_PATH, // path
ram54288 0:dbad57390bd1 90 (uint8_t*)"", // resource
ram54288 0:dbad57390bd1 91 5, // strlen("5/0/1")
ram54288 0:dbad57390bd1 92 0, // resourcelen
ram54288 0:dbad57390bd1 93 false, // external_memory_block
ram54288 0:dbad57390bd1 94 SN_GRS_DYNAMIC, // mode
ram54288 0:dbad57390bd1 95 false // free_on_delete
ram54288 0:dbad57390bd1 96 };
ram54288 0:dbad57390bd1 97
ram54288 0:dbad57390bd1 98 #define UPDATE_PATH FIRMWARE_PATH_PREFIX FIRMWARE_UPDATE
ram54288 0:dbad57390bd1 99
ram54288 0:dbad57390bd1 100 STATIC_PARAM_TYPE
ram54288 0:dbad57390bd1 101 static sn_nsdl_static_resource_parameters_s firmware_update_params_static = {
ram54288 0:dbad57390bd1 102 (char*)OMA_RESOURCE_TYPE, // resource_type_ptr
ram54288 0:dbad57390bd1 103 (char*)"", // interface_description_ptr
ram54288 0:dbad57390bd1 104 (uint8_t*)UPDATE_PATH, // path
ram54288 0:dbad57390bd1 105 (uint8_t*)"", // resource
ram54288 0:dbad57390bd1 106 5, // strlen(5/0/2)
ram54288 0:dbad57390bd1 107 0, // resourcelen
ram54288 0:dbad57390bd1 108 false, // external_memory_block
ram54288 0:dbad57390bd1 109 SN_GRS_DYNAMIC, // mode
ram54288 0:dbad57390bd1 110 false // free_on_delete
ram54288 0:dbad57390bd1 111 };
ram54288 0:dbad57390bd1 112
ram54288 0:dbad57390bd1 113 #define STATE_URI_PATH FIRMWARE_PATH_PREFIX FIRMWARE_STATE
ram54288 0:dbad57390bd1 114
ram54288 0:dbad57390bd1 115 STATIC_PARAM_TYPE
ram54288 0:dbad57390bd1 116 static sn_nsdl_static_resource_parameters_s firmware_state_params_static = {
ram54288 0:dbad57390bd1 117 (char*)OMA_RESOURCE_TYPE, // resource_type_ptr
ram54288 0:dbad57390bd1 118 (char*)"", // interface_description_ptr
ram54288 0:dbad57390bd1 119 (uint8_t*)STATE_URI_PATH, // path
ram54288 0:dbad57390bd1 120 (uint8_t*)"0", // resource
ram54288 0:dbad57390bd1 121 5, // strlen("5/0/3")
ram54288 0:dbad57390bd1 122 1, // resourcelen
ram54288 0:dbad57390bd1 123 false, // external_memory_block
ram54288 0:dbad57390bd1 124 SN_GRS_DYNAMIC, // mode
ram54288 0:dbad57390bd1 125 false // free_on_delete
ram54288 0:dbad57390bd1 126 };
ram54288 0:dbad57390bd1 127
ram54288 0:dbad57390bd1 128 #define UPDATE_RESULT_PATH FIRMWARE_PATH_PREFIX FIRMWARE_UPDATE_RESULT
ram54288 0:dbad57390bd1 129
ram54288 0:dbad57390bd1 130 STATIC_PARAM_TYPE
ram54288 0:dbad57390bd1 131 static sn_nsdl_static_resource_parameters_s firmware_update_result_params_static = {
ram54288 0:dbad57390bd1 132 (char*)OMA_RESOURCE_TYPE, // resource_type_ptr
ram54288 0:dbad57390bd1 133 (char*)"", // interface_description_ptr
ram54288 0:dbad57390bd1 134 (uint8_t*)UPDATE_RESULT_PATH, // path
ram54288 0:dbad57390bd1 135 (uint8_t*)"0", // resource
ram54288 0:dbad57390bd1 136 5, // strlen(5/0/5)
ram54288 0:dbad57390bd1 137 1, // resourcelen
ram54288 0:dbad57390bd1 138 false, // external_memory_block
ram54288 0:dbad57390bd1 139 SN_GRS_DYNAMIC, // mode
ram54288 0:dbad57390bd1 140 false // free_on_delete
ram54288 0:dbad57390bd1 141 };
ram54288 0:dbad57390bd1 142
ram54288 0:dbad57390bd1 143 static sn_nsdl_dynamic_resource_parameters_s firmware_package_params_dynamic = {
ram54288 0:dbad57390bd1 144 __nsdl_c_callback,
ram54288 0:dbad57390bd1 145 &firmware_package_params_static,
ram54288 0:dbad57390bd1 146 {NULL, NULL}, // link
ram54288 0:dbad57390bd1 147 COAP_CONTENT_OMA_PLAIN_TEXT_TYPE, // coap_content_type
ram54288 0:dbad57390bd1 148 M2MBase::PUT_ALLOWED, // access
ram54288 0:dbad57390bd1 149 0, // registered
ram54288 0:dbad57390bd1 150 false, // publish_uri
ram54288 0:dbad57390bd1 151 false, // free_on_delete
ram54288 0:dbad57390bd1 152 true // observable
ram54288 0:dbad57390bd1 153 };
ram54288 0:dbad57390bd1 154
ram54288 0:dbad57390bd1 155 static sn_nsdl_dynamic_resource_parameters_s firmware_package_uri_params_dynamic = {
ram54288 0:dbad57390bd1 156 __nsdl_c_callback,
ram54288 0:dbad57390bd1 157 &firmware_package_uri_params_static,
ram54288 0:dbad57390bd1 158 {NULL, NULL}, // link
ram54288 0:dbad57390bd1 159 COAP_CONTENT_OMA_PLAIN_TEXT_TYPE, // coap_content_type
ram54288 0:dbad57390bd1 160 M2MBase::PUT_ALLOWED, // access
ram54288 0:dbad57390bd1 161 0, // registered
ram54288 0:dbad57390bd1 162 false, // publish_uri
ram54288 0:dbad57390bd1 163 false, // free_on_delete
ram54288 0:dbad57390bd1 164 true // observable
ram54288 0:dbad57390bd1 165 };
ram54288 0:dbad57390bd1 166
ram54288 0:dbad57390bd1 167 static sn_nsdl_dynamic_resource_parameters_s firmware_update_params_dynamic = {
ram54288 0:dbad57390bd1 168 __nsdl_c_callback,
ram54288 0:dbad57390bd1 169 &firmware_update_params_static,
ram54288 0:dbad57390bd1 170 {NULL, NULL}, // link
ram54288 0:dbad57390bd1 171 COAP_CONTENT_OMA_PLAIN_TEXT_TYPE, // coap_content_type
ram54288 0:dbad57390bd1 172 M2MBase::NOT_ALLOWED, // access
ram54288 0:dbad57390bd1 173 0, // registered
ram54288 0:dbad57390bd1 174 false, // publish_uri
ram54288 0:dbad57390bd1 175 false, // free_on_delete
ram54288 0:dbad57390bd1 176 true // observable
ram54288 0:dbad57390bd1 177 };
ram54288 0:dbad57390bd1 178
ram54288 0:dbad57390bd1 179 static sn_nsdl_dynamic_resource_parameters_s firmware_state_params_dynamic = {
ram54288 0:dbad57390bd1 180 __nsdl_c_callback,
ram54288 0:dbad57390bd1 181 &firmware_state_params_static,
ram54288 0:dbad57390bd1 182 {NULL, NULL}, // link
ram54288 0:dbad57390bd1 183 COAP_CONTENT_OMA_PLAIN_TEXT_TYPE, // coap_content_type
ram54288 0:dbad57390bd1 184 M2MBase::GET_ALLOWED, // access
ram54288 0:dbad57390bd1 185 0, // registered
ram54288 0:dbad57390bd1 186 false, // publish_uri
ram54288 0:dbad57390bd1 187 false, // free_on_delete
ram54288 0:dbad57390bd1 188 true // observable
ram54288 0:dbad57390bd1 189 };
ram54288 0:dbad57390bd1 190
ram54288 0:dbad57390bd1 191 static sn_nsdl_dynamic_resource_parameters_s firmware_update_result_params_dynamic = {
ram54288 0:dbad57390bd1 192 __nsdl_c_callback,
ram54288 0:dbad57390bd1 193 &firmware_update_result_params_static,
ram54288 0:dbad57390bd1 194 {NULL, NULL}, // link
ram54288 0:dbad57390bd1 195 COAP_CONTENT_OMA_PLAIN_TEXT_TYPE, // coap_content_type
ram54288 0:dbad57390bd1 196 M2MBase::GET_ALLOWED, // access
ram54288 0:dbad57390bd1 197 0, // registered
ram54288 0:dbad57390bd1 198 false, // publish_uri
ram54288 0:dbad57390bd1 199 false, // free_on_delete
ram54288 0:dbad57390bd1 200 true // observable
ram54288 0:dbad57390bd1 201 };
ram54288 0:dbad57390bd1 202 const static M2MBase::lwm2m_parameters firmware_package_params = {
ram54288 0:dbad57390bd1 203 0, // max_age
ram54288 0:dbad57390bd1 204 0, // instance_id
ram54288 0:dbad57390bd1 205 0, // name_id
ram54288 0:dbad57390bd1 206 (char*)FIRMWARE_PACKAGE, // name
ram54288 0:dbad57390bd1 207 &firmware_package_params_dynamic,
ram54288 0:dbad57390bd1 208 M2MBase::Resource, // base_type
ram54288 0:dbad57390bd1 209 false // free_on_delete
ram54288 0:dbad57390bd1 210 };
ram54288 0:dbad57390bd1 211
ram54288 0:dbad57390bd1 212 const static M2MBase::lwm2m_parameters firmware_package_uri_params = {
ram54288 0:dbad57390bd1 213 0, // max_age
ram54288 0:dbad57390bd1 214 0, // instance_id
ram54288 0:dbad57390bd1 215 0, // name_id
ram54288 0:dbad57390bd1 216 (char*)FIRMWARE_PACKAGE_URI, // name
ram54288 0:dbad57390bd1 217 &firmware_package_uri_params_dynamic,
ram54288 0:dbad57390bd1 218 M2MBase::Resource, // base_type
ram54288 0:dbad57390bd1 219 false // free_on_delete
ram54288 0:dbad57390bd1 220 };
ram54288 0:dbad57390bd1 221
ram54288 0:dbad57390bd1 222 const static M2MBase::lwm2m_parameters firmware_update_params = {
ram54288 0:dbad57390bd1 223 0, // max_age
ram54288 0:dbad57390bd1 224 0, // instance_id
ram54288 0:dbad57390bd1 225 0, // name_id
ram54288 0:dbad57390bd1 226 (char*)FIRMWARE_UPDATE, // name
ram54288 0:dbad57390bd1 227 &firmware_update_params_dynamic,
ram54288 0:dbad57390bd1 228 M2MBase::Resource, // base_type
ram54288 0:dbad57390bd1 229 false // free_on_delete
ram54288 0:dbad57390bd1 230 };
ram54288 0:dbad57390bd1 231
ram54288 0:dbad57390bd1 232 const static M2MBase::lwm2m_parameters firmware_state_params = {
ram54288 0:dbad57390bd1 233 0, // max_age
ram54288 0:dbad57390bd1 234 0, // instance_id
ram54288 0:dbad57390bd1 235 0, // name_id
ram54288 0:dbad57390bd1 236 (char*)FIRMWARE_STATE, // name
ram54288 0:dbad57390bd1 237 &firmware_state_params_dynamic,
ram54288 0:dbad57390bd1 238 M2MBase::Resource, // base_type
ram54288 0:dbad57390bd1 239 false // free_on_delete
ram54288 0:dbad57390bd1 240 };
ram54288 0:dbad57390bd1 241
ram54288 0:dbad57390bd1 242 const static M2MBase::lwm2m_parameters firmware_update_result_params = {
ram54288 0:dbad57390bd1 243 0, // max_age
ram54288 0:dbad57390bd1 244 0, // instance_id
ram54288 0:dbad57390bd1 245 0, // name_id
ram54288 0:dbad57390bd1 246 (char*)FIRMWARE_UPDATE_RESULT, // name
ram54288 0:dbad57390bd1 247 &firmware_update_result_params_dynamic,
ram54288 0:dbad57390bd1 248 M2MBase::Resource, // base_type
ram54288 0:dbad57390bd1 249 false // free_on_delete
ram54288 0:dbad57390bd1 250 };
ram54288 0:dbad57390bd1 251
ram54288 0:dbad57390bd1 252 void M2MFirmware::create_mandatory_resources()
ram54288 0:dbad57390bd1 253 {
ram54288 0:dbad57390bd1 254 _firmware_instance->set_coap_content_type(COAP_CONTENT_OMA_TLV_TYPE);
ram54288 0:dbad57390bd1 255
ram54288 0:dbad57390bd1 256 M2MResource* res;
ram54288 0:dbad57390bd1 257
ram54288 0:dbad57390bd1 258 // todo:
ram54288 0:dbad57390bd1 259 // perhaps we should have a API for batch creation of objects by using a array
ram54288 0:dbad57390bd1 260 // of lwm2m_parameters.
ram54288 0:dbad57390bd1 261
ram54288 0:dbad57390bd1 262 res = _firmware_instance->create_dynamic_resource(&firmware_package_params,
ram54288 0:dbad57390bd1 263 M2MResourceInstance::OPAQUE,
ram54288 0:dbad57390bd1 264 false);
ram54288 0:dbad57390bd1 265
ram54288 0:dbad57390bd1 266 res = _firmware_instance->create_dynamic_resource(&firmware_package_uri_params,
ram54288 0:dbad57390bd1 267 M2MResourceInstance::STRING,
ram54288 0:dbad57390bd1 268 false);
ram54288 0:dbad57390bd1 269
ram54288 0:dbad57390bd1 270 res = _firmware_instance->create_dynamic_resource(&firmware_update_params,
ram54288 0:dbad57390bd1 271 M2MResourceInstance::OPAQUE,
ram54288 0:dbad57390bd1 272 false);
ram54288 0:dbad57390bd1 273
ram54288 0:dbad57390bd1 274 res = _firmware_instance->create_dynamic_resource(&firmware_state_params,
ram54288 0:dbad57390bd1 275 M2MResourceInstance::INTEGER,
ram54288 0:dbad57390bd1 276 true);
ram54288 0:dbad57390bd1 277
ram54288 0:dbad57390bd1 278 res = _firmware_instance->create_dynamic_resource(&firmware_update_result_params,
ram54288 0:dbad57390bd1 279 M2MResourceInstance::INTEGER,
ram54288 0:dbad57390bd1 280 true);
ram54288 0:dbad57390bd1 281 }
ram54288 0:dbad57390bd1 282
ram54288 0:dbad57390bd1 283 M2MResource* M2MFirmware::create_resource(FirmwareResource resource, const String &value)
ram54288 0:dbad57390bd1 284 {
ram54288 0:dbad57390bd1 285 M2MResource* res = NULL;
ram54288 0:dbad57390bd1 286 const char* firmware_id_ptr = "";
ram54288 0:dbad57390bd1 287 M2MBase::Operation operation = M2MBase::GET_ALLOWED;
ram54288 0:dbad57390bd1 288 if(!is_resource_present(resource)) {
ram54288 0:dbad57390bd1 289 switch(resource) {
ram54288 0:dbad57390bd1 290 case PackageName:
ram54288 0:dbad57390bd1 291 firmware_id_ptr = FIRMWARE_PACKAGE_NAME;
ram54288 0:dbad57390bd1 292 break;
ram54288 0:dbad57390bd1 293 case PackageVersion:
ram54288 0:dbad57390bd1 294 firmware_id_ptr = FIRMWARE_PACKAGE_VERSION;
ram54288 0:dbad57390bd1 295 break;
ram54288 0:dbad57390bd1 296 default:
ram54288 0:dbad57390bd1 297 break;
ram54288 0:dbad57390bd1 298 }
ram54288 0:dbad57390bd1 299 }
ram54288 0:dbad57390bd1 300 String firmware_id(firmware_id_ptr);
ram54288 0:dbad57390bd1 301
ram54288 0:dbad57390bd1 302 if(!firmware_id.empty() && value.size() < 256) {
ram54288 0:dbad57390bd1 303 if(_firmware_instance) {
ram54288 0:dbad57390bd1 304 res = _firmware_instance->create_dynamic_resource(firmware_id,
ram54288 0:dbad57390bd1 305 OMA_RESOURCE_TYPE,
ram54288 0:dbad57390bd1 306 M2MResourceInstance::STRING,
ram54288 0:dbad57390bd1 307 false);
ram54288 0:dbad57390bd1 308
ram54288 0:dbad57390bd1 309 if(res) {
ram54288 0:dbad57390bd1 310 res->set_register_uri(false);
ram54288 0:dbad57390bd1 311 res->set_operation(operation);
ram54288 0:dbad57390bd1 312 if(value.empty()) {
ram54288 0:dbad57390bd1 313 res->clear_value();
ram54288 0:dbad57390bd1 314 } else {
ram54288 0:dbad57390bd1 315 res->set_value((const uint8_t*)value.c_str(),
ram54288 0:dbad57390bd1 316 (uint32_t)value.length());
ram54288 0:dbad57390bd1 317 }
ram54288 0:dbad57390bd1 318 }
ram54288 0:dbad57390bd1 319 }
ram54288 0:dbad57390bd1 320 }
ram54288 0:dbad57390bd1 321 return res;
ram54288 0:dbad57390bd1 322 }
ram54288 0:dbad57390bd1 323
ram54288 0:dbad57390bd1 324 M2MResource* M2MFirmware::create_resource(FirmwareResource resource, int64_t value)
ram54288 0:dbad57390bd1 325 {
ram54288 0:dbad57390bd1 326 M2MResource* res = NULL;
ram54288 0:dbad57390bd1 327 const char* firmware_id_ptr = "";
ram54288 0:dbad57390bd1 328 M2MBase::Operation operation = M2MBase::GET_ALLOWED;
ram54288 0:dbad57390bd1 329 if(!is_resource_present(resource)) {
ram54288 0:dbad57390bd1 330 switch(resource) {
ram54288 0:dbad57390bd1 331 case UpdateSupportedObjects:
ram54288 0:dbad57390bd1 332 if(check_value_range(resource, value)) {
ram54288 0:dbad57390bd1 333 firmware_id_ptr = FIRMWARE_UPDATE_SUPPORTED_OBJECTS;
ram54288 0:dbad57390bd1 334 operation = M2MBase::GET_PUT_ALLOWED;
ram54288 0:dbad57390bd1 335 }
ram54288 0:dbad57390bd1 336 break;
ram54288 0:dbad57390bd1 337 default:
ram54288 0:dbad57390bd1 338 break;
ram54288 0:dbad57390bd1 339 }
ram54288 0:dbad57390bd1 340 }
ram54288 0:dbad57390bd1 341
ram54288 0:dbad57390bd1 342 const String firmware_id(firmware_id_ptr);
ram54288 0:dbad57390bd1 343
ram54288 0:dbad57390bd1 344 if(!firmware_id.empty()) {
ram54288 0:dbad57390bd1 345 if(_firmware_instance) {
ram54288 0:dbad57390bd1 346 res = _firmware_instance->create_dynamic_resource(firmware_id,
ram54288 0:dbad57390bd1 347 OMA_RESOURCE_TYPE,
ram54288 0:dbad57390bd1 348 M2MResourceInstance::INTEGER,
ram54288 0:dbad57390bd1 349 false);
ram54288 0:dbad57390bd1 350
ram54288 0:dbad57390bd1 351 if(res) {
ram54288 0:dbad57390bd1 352 res->set_register_uri(false);
ram54288 0:dbad57390bd1 353
ram54288 0:dbad57390bd1 354 res->set_operation(operation);
ram54288 0:dbad57390bd1 355 res->set_value(value);
ram54288 0:dbad57390bd1 356 }
ram54288 0:dbad57390bd1 357 }
ram54288 0:dbad57390bd1 358 }
ram54288 0:dbad57390bd1 359 return res;
ram54288 0:dbad57390bd1 360 }
ram54288 0:dbad57390bd1 361
ram54288 0:dbad57390bd1 362 bool M2MFirmware::set_resource_value(FirmwareResource resource,
ram54288 0:dbad57390bd1 363 const String &value)
ram54288 0:dbad57390bd1 364 {
ram54288 0:dbad57390bd1 365 bool success = false;
ram54288 0:dbad57390bd1 366 M2MResource* res = get_resource(resource);
ram54288 0:dbad57390bd1 367 if(res) {
ram54288 0:dbad57390bd1 368 if(M2MFirmware::PackageUri == resource ||
ram54288 0:dbad57390bd1 369 M2MFirmware::PackageName == resource ||
ram54288 0:dbad57390bd1 370 M2MFirmware::PackageVersion == resource) {
ram54288 0:dbad57390bd1 371 if (value.size() < 256) {
ram54288 0:dbad57390bd1 372 if(value.empty()) {
ram54288 0:dbad57390bd1 373 res->clear_value();
ram54288 0:dbad57390bd1 374 success = true;
ram54288 0:dbad57390bd1 375 } else {
ram54288 0:dbad57390bd1 376 success = res->set_value((const uint8_t*)value.c_str(),(uint32_t)value.length());
ram54288 0:dbad57390bd1 377 }
ram54288 0:dbad57390bd1 378 }
ram54288 0:dbad57390bd1 379 }
ram54288 0:dbad57390bd1 380 }
ram54288 0:dbad57390bd1 381 return success;
ram54288 0:dbad57390bd1 382 }
ram54288 0:dbad57390bd1 383
ram54288 0:dbad57390bd1 384 bool M2MFirmware::set_resource_value(FirmwareResource resource,
ram54288 0:dbad57390bd1 385 int64_t value)
ram54288 0:dbad57390bd1 386 {
ram54288 0:dbad57390bd1 387 bool success = false;
ram54288 0:dbad57390bd1 388 M2MResource* res = get_resource(resource);
ram54288 0:dbad57390bd1 389 if(res) {
ram54288 0:dbad57390bd1 390 if(M2MFirmware::State == resource ||
ram54288 0:dbad57390bd1 391 M2MFirmware::UpdateSupportedObjects == resource ||
ram54288 0:dbad57390bd1 392 M2MFirmware::UpdateResult == resource) {
ram54288 0:dbad57390bd1 393 // If it is any of the above resource
ram54288 0:dbad57390bd1 394 // set the value of the resource.
ram54288 0:dbad57390bd1 395 if (check_value_range(resource, value)) {
ram54288 0:dbad57390bd1 396
ram54288 0:dbad57390bd1 397 success = res->set_value(value);
ram54288 0:dbad57390bd1 398 }
ram54288 0:dbad57390bd1 399 }
ram54288 0:dbad57390bd1 400 }
ram54288 0:dbad57390bd1 401 return success;
ram54288 0:dbad57390bd1 402 }
ram54288 0:dbad57390bd1 403
ram54288 0:dbad57390bd1 404 bool M2MFirmware::set_resource_value(FirmwareResource resource,
ram54288 0:dbad57390bd1 405 const uint8_t *value,
ram54288 0:dbad57390bd1 406 const uint32_t length)
ram54288 0:dbad57390bd1 407 {
ram54288 0:dbad57390bd1 408 bool success = false;
ram54288 0:dbad57390bd1 409 M2MResource* res = get_resource(resource);
ram54288 0:dbad57390bd1 410 if(res) {
ram54288 0:dbad57390bd1 411 if(M2MFirmware::Package == resource) {
ram54288 0:dbad57390bd1 412 success = res->set_value(value,length);
ram54288 0:dbad57390bd1 413 }
ram54288 0:dbad57390bd1 414 }
ram54288 0:dbad57390bd1 415 return success;
ram54288 0:dbad57390bd1 416 }
ram54288 0:dbad57390bd1 417
ram54288 0:dbad57390bd1 418 bool M2MFirmware::is_resource_present(FirmwareResource resource) const
ram54288 0:dbad57390bd1 419 {
ram54288 0:dbad57390bd1 420 bool success = false;
ram54288 0:dbad57390bd1 421 M2MResource* res = get_resource(resource);
ram54288 0:dbad57390bd1 422 if(res) {
ram54288 0:dbad57390bd1 423 success = true;
ram54288 0:dbad57390bd1 424 }
ram54288 0:dbad57390bd1 425 return success;
ram54288 0:dbad57390bd1 426 }
ram54288 0:dbad57390bd1 427
ram54288 0:dbad57390bd1 428 const char* M2MFirmware::resource_name(FirmwareResource resource)
ram54288 0:dbad57390bd1 429 {
ram54288 0:dbad57390bd1 430 const char* res_name = "";
ram54288 0:dbad57390bd1 431 switch(resource) {
ram54288 0:dbad57390bd1 432 case Package:
ram54288 0:dbad57390bd1 433 res_name = FIRMWARE_PACKAGE;
ram54288 0:dbad57390bd1 434 break;
ram54288 0:dbad57390bd1 435 case PackageUri:
ram54288 0:dbad57390bd1 436 res_name = FIRMWARE_PACKAGE_URI;
ram54288 0:dbad57390bd1 437 break;
ram54288 0:dbad57390bd1 438 case Update:
ram54288 0:dbad57390bd1 439 res_name = FIRMWARE_UPDATE;
ram54288 0:dbad57390bd1 440 break;
ram54288 0:dbad57390bd1 441 case State:
ram54288 0:dbad57390bd1 442 res_name = FIRMWARE_STATE;
ram54288 0:dbad57390bd1 443 break;
ram54288 0:dbad57390bd1 444 case UpdateSupportedObjects:
ram54288 0:dbad57390bd1 445 res_name = FIRMWARE_UPDATE_SUPPORTED_OBJECTS;
ram54288 0:dbad57390bd1 446 break;
ram54288 0:dbad57390bd1 447 case UpdateResult:
ram54288 0:dbad57390bd1 448 res_name = FIRMWARE_UPDATE_RESULT;
ram54288 0:dbad57390bd1 449 break;
ram54288 0:dbad57390bd1 450 case PackageName:
ram54288 0:dbad57390bd1 451 res_name = FIRMWARE_PACKAGE_NAME;
ram54288 0:dbad57390bd1 452 break;
ram54288 0:dbad57390bd1 453 case PackageVersion:
ram54288 0:dbad57390bd1 454 res_name = FIRMWARE_PACKAGE_VERSION;
ram54288 0:dbad57390bd1 455 break;
ram54288 0:dbad57390bd1 456 }
ram54288 0:dbad57390bd1 457 return res_name;
ram54288 0:dbad57390bd1 458 }
ram54288 0:dbad57390bd1 459
ram54288 0:dbad57390bd1 460 uint16_t M2MFirmware::per_resource_count(FirmwareResource res) const
ram54288 0:dbad57390bd1 461 {
ram54288 0:dbad57390bd1 462 uint16_t count = 0;
ram54288 0:dbad57390bd1 463 if(_firmware_instance) {
ram54288 0:dbad57390bd1 464 count = _firmware_instance->resource_count(resource_name(res));
ram54288 0:dbad57390bd1 465 }
ram54288 0:dbad57390bd1 466 return count;
ram54288 0:dbad57390bd1 467 }
ram54288 0:dbad57390bd1 468
ram54288 0:dbad57390bd1 469 uint16_t M2MFirmware::total_resource_count() const
ram54288 0:dbad57390bd1 470 {
ram54288 0:dbad57390bd1 471 uint16_t count = 0;
ram54288 0:dbad57390bd1 472 if(_firmware_instance) {
ram54288 0:dbad57390bd1 473 count = _firmware_instance->resources().size();
ram54288 0:dbad57390bd1 474 }
ram54288 0:dbad57390bd1 475 return count;
ram54288 0:dbad57390bd1 476 }
ram54288 0:dbad57390bd1 477
ram54288 0:dbad57390bd1 478 uint32_t M2MFirmware::resource_value_buffer(FirmwareResource resource,
ram54288 0:dbad57390bd1 479 uint8_t *&data) const
ram54288 0:dbad57390bd1 480 {
ram54288 0:dbad57390bd1 481 uint32_t size = 0;
ram54288 0:dbad57390bd1 482 M2MResource* res = get_resource(resource);
ram54288 0:dbad57390bd1 483 if(res) {
ram54288 0:dbad57390bd1 484 if(M2MFirmware::Package == resource) {
ram54288 0:dbad57390bd1 485 res->get_value(data,size);
ram54288 0:dbad57390bd1 486 }
ram54288 0:dbad57390bd1 487 }
ram54288 0:dbad57390bd1 488 return size;
ram54288 0:dbad57390bd1 489 }
ram54288 0:dbad57390bd1 490
ram54288 0:dbad57390bd1 491 M2MResource* M2MFirmware::get_resource(FirmwareResource res) const
ram54288 0:dbad57390bd1 492 {
ram54288 0:dbad57390bd1 493 M2MResource* res_object = NULL;
ram54288 0:dbad57390bd1 494 if(_firmware_instance) {
ram54288 0:dbad57390bd1 495 const char* res_name_ptr = "";
ram54288 0:dbad57390bd1 496 switch(res) {
ram54288 0:dbad57390bd1 497 case Package:
ram54288 0:dbad57390bd1 498 res_name_ptr = FIRMWARE_PACKAGE;
ram54288 0:dbad57390bd1 499 break;
ram54288 0:dbad57390bd1 500 case PackageUri:
ram54288 0:dbad57390bd1 501 res_name_ptr = FIRMWARE_PACKAGE_URI;
ram54288 0:dbad57390bd1 502 break;
ram54288 0:dbad57390bd1 503 case Update:
ram54288 0:dbad57390bd1 504 res_name_ptr = FIRMWARE_UPDATE;
ram54288 0:dbad57390bd1 505 break;
ram54288 0:dbad57390bd1 506 case State:
ram54288 0:dbad57390bd1 507 res_name_ptr = FIRMWARE_STATE;
ram54288 0:dbad57390bd1 508 break;
ram54288 0:dbad57390bd1 509 case UpdateSupportedObjects:
ram54288 0:dbad57390bd1 510 res_name_ptr = FIRMWARE_UPDATE_SUPPORTED_OBJECTS;
ram54288 0:dbad57390bd1 511 break;
ram54288 0:dbad57390bd1 512 case UpdateResult:
ram54288 0:dbad57390bd1 513 res_name_ptr = FIRMWARE_UPDATE_RESULT;
ram54288 0:dbad57390bd1 514 break;
ram54288 0:dbad57390bd1 515 case PackageName:
ram54288 0:dbad57390bd1 516 res_name_ptr = FIRMWARE_PACKAGE_NAME;
ram54288 0:dbad57390bd1 517 break;
ram54288 0:dbad57390bd1 518 case PackageVersion:
ram54288 0:dbad57390bd1 519 res_name_ptr = FIRMWARE_PACKAGE_VERSION;
ram54288 0:dbad57390bd1 520 break;
ram54288 0:dbad57390bd1 521 }
ram54288 0:dbad57390bd1 522
ram54288 0:dbad57390bd1 523 res_object = _firmware_instance->resource(res_name_ptr);
ram54288 0:dbad57390bd1 524 }
ram54288 0:dbad57390bd1 525 return res_object;
ram54288 0:dbad57390bd1 526 }
ram54288 0:dbad57390bd1 527
ram54288 0:dbad57390bd1 528 bool M2MFirmware::delete_resource(FirmwareResource resource)
ram54288 0:dbad57390bd1 529 {
ram54288 0:dbad57390bd1 530 bool success = false;
ram54288 0:dbad57390bd1 531 if(M2MFirmware::UpdateSupportedObjects == resource ||
ram54288 0:dbad57390bd1 532 M2MFirmware::PackageName == resource ||
ram54288 0:dbad57390bd1 533 M2MFirmware::PackageVersion == resource) {
ram54288 0:dbad57390bd1 534 if(_firmware_instance) {
ram54288 0:dbad57390bd1 535 success = _firmware_instance->remove_resource(resource_name(resource));
ram54288 0:dbad57390bd1 536 }
ram54288 0:dbad57390bd1 537 }
ram54288 0:dbad57390bd1 538 return success;
ram54288 0:dbad57390bd1 539 }
ram54288 0:dbad57390bd1 540
ram54288 0:dbad57390bd1 541 int64_t M2MFirmware::resource_value_int(FirmwareResource resource) const
ram54288 0:dbad57390bd1 542 {
ram54288 0:dbad57390bd1 543 int64_t value = -1;
ram54288 0:dbad57390bd1 544 M2MResource* res = get_resource(resource);
ram54288 0:dbad57390bd1 545 if(res) {
ram54288 0:dbad57390bd1 546 if(M2MFirmware::State == resource ||
ram54288 0:dbad57390bd1 547 M2MFirmware::UpdateSupportedObjects == resource ||
ram54288 0:dbad57390bd1 548 M2MFirmware::UpdateResult == resource) {
ram54288 0:dbad57390bd1 549
ram54288 0:dbad57390bd1 550 value = res->get_value_int();
ram54288 0:dbad57390bd1 551 }
ram54288 0:dbad57390bd1 552 }
ram54288 0:dbad57390bd1 553 return value;
ram54288 0:dbad57390bd1 554 }
ram54288 0:dbad57390bd1 555
ram54288 0:dbad57390bd1 556 String M2MFirmware::resource_value_string(FirmwareResource resource) const
ram54288 0:dbad57390bd1 557 {
ram54288 0:dbad57390bd1 558 String value = "";
ram54288 0:dbad57390bd1 559 M2MResource* res = get_resource(resource);
ram54288 0:dbad57390bd1 560 if(res) {
ram54288 0:dbad57390bd1 561 if(M2MFirmware::PackageUri == resource ||
ram54288 0:dbad57390bd1 562 M2MFirmware::PackageName == resource ||
ram54288 0:dbad57390bd1 563 M2MFirmware::PackageVersion == resource) {
ram54288 0:dbad57390bd1 564
ram54288 0:dbad57390bd1 565 value = res->get_value_string();
ram54288 0:dbad57390bd1 566 }
ram54288 0:dbad57390bd1 567 }
ram54288 0:dbad57390bd1 568 return value;
ram54288 0:dbad57390bd1 569 }
ram54288 0:dbad57390bd1 570
ram54288 0:dbad57390bd1 571 bool M2MFirmware::check_value_range(FirmwareResource resource, int64_t value) const
ram54288 0:dbad57390bd1 572 {
ram54288 0:dbad57390bd1 573 bool success = false;
ram54288 0:dbad57390bd1 574 switch (resource) {
ram54288 0:dbad57390bd1 575 case UpdateSupportedObjects:
ram54288 0:dbad57390bd1 576 if(value == 0 || value == 1) {
ram54288 0:dbad57390bd1 577 success = true;
ram54288 0:dbad57390bd1 578 }
ram54288 0:dbad57390bd1 579 break;
ram54288 0:dbad57390bd1 580 case State:
ram54288 0:dbad57390bd1 581 if (value >= 0 && value <= 3) {
ram54288 0:dbad57390bd1 582 success = true;
ram54288 0:dbad57390bd1 583 M2MResource* updateRes = get_resource(M2MFirmware::Update);
ram54288 0:dbad57390bd1 584 if (updateRes){
ram54288 0:dbad57390bd1 585 if (value == M2MFirmware::Downloaded) {
ram54288 0:dbad57390bd1 586 updateRes->set_operation(M2MBase::POST_ALLOWED);
ram54288 0:dbad57390bd1 587 }
ram54288 0:dbad57390bd1 588 else {
ram54288 0:dbad57390bd1 589 updateRes->set_operation(M2MBase::NOT_ALLOWED);
ram54288 0:dbad57390bd1 590 }
ram54288 0:dbad57390bd1 591 }
ram54288 0:dbad57390bd1 592 }
ram54288 0:dbad57390bd1 593 break;
ram54288 0:dbad57390bd1 594 case UpdateResult:
ram54288 0:dbad57390bd1 595 if (value >= 0 && value <= 7) {
ram54288 0:dbad57390bd1 596 success = true;
ram54288 0:dbad57390bd1 597 }
ram54288 0:dbad57390bd1 598 break;
ram54288 0:dbad57390bd1 599 default:
ram54288 0:dbad57390bd1 600 break;
ram54288 0:dbad57390bd1 601 }
ram54288 0:dbad57390bd1 602 return success;
ram54288 0:dbad57390bd1 603 }
ram54288 0:dbad57390bd1 604