Preliminary main mbed library for nexpaq development

Committer:
nexpaq
Date:
Fri Nov 04 20:27:58 2016 +0000
Revision:
0:6c56fb4bc5f0
Moving to library for sharing updates

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nexpaq 0:6c56fb4bc5f0 1 /*
nexpaq 0:6c56fb4bc5f0 2 * Copyright (c) 2015 ARM Limited. All rights reserved.
nexpaq 0:6c56fb4bc5f0 3 * SPDX-License-Identifier: Apache-2.0
nexpaq 0:6c56fb4bc5f0 4 * Licensed under the Apache License, Version 2.0 (the License); you may
nexpaq 0:6c56fb4bc5f0 5 * not use this file except in compliance with the License.
nexpaq 0:6c56fb4bc5f0 6 * You may obtain a copy of the License at
nexpaq 0:6c56fb4bc5f0 7 *
nexpaq 0:6c56fb4bc5f0 8 * http://www.apache.org/licenses/LICENSE-2.0
nexpaq 0:6c56fb4bc5f0 9 *
nexpaq 0:6c56fb4bc5f0 10 * Unless required by applicable law or agreed to in writing, software
nexpaq 0:6c56fb4bc5f0 11 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
nexpaq 0:6c56fb4bc5f0 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
nexpaq 0:6c56fb4bc5f0 13 * See the License for the specific language governing permissions and
nexpaq 0:6c56fb4bc5f0 14 * limitations under the License.
nexpaq 0:6c56fb4bc5f0 15 */
nexpaq 0:6c56fb4bc5f0 16 #include <cstdio>
nexpaq 0:6c56fb4bc5f0 17 #include "mbed-client/m2mfirmware.h"
nexpaq 0:6c56fb4bc5f0 18 #include "mbed-client/m2mconstants.h"
nexpaq 0:6c56fb4bc5f0 19 #include "mbed-client/m2mobject.h"
nexpaq 0:6c56fb4bc5f0 20 #include "mbed-client/m2mobjectinstance.h"
nexpaq 0:6c56fb4bc5f0 21 #include "mbed-client/m2mresource.h"
nexpaq 0:6c56fb4bc5f0 22
nexpaq 0:6c56fb4bc5f0 23 #define BUFFER_SIZE 21
nexpaq 0:6c56fb4bc5f0 24 #define TRACE_GROUP "mClt"
nexpaq 0:6c56fb4bc5f0 25
nexpaq 0:6c56fb4bc5f0 26 M2MFirmware* M2MFirmware::_instance = NULL;
nexpaq 0:6c56fb4bc5f0 27
nexpaq 0:6c56fb4bc5f0 28 M2MFirmware* M2MFirmware::get_instance()
nexpaq 0:6c56fb4bc5f0 29 {
nexpaq 0:6c56fb4bc5f0 30 if(_instance == NULL) {
nexpaq 0:6c56fb4bc5f0 31 _instance = new M2MFirmware();
nexpaq 0:6c56fb4bc5f0 32 }
nexpaq 0:6c56fb4bc5f0 33 return _instance;
nexpaq 0:6c56fb4bc5f0 34 }
nexpaq 0:6c56fb4bc5f0 35
nexpaq 0:6c56fb4bc5f0 36 void M2MFirmware::delete_instance()
nexpaq 0:6c56fb4bc5f0 37 {
nexpaq 0:6c56fb4bc5f0 38 delete _instance;
nexpaq 0:6c56fb4bc5f0 39 _instance = NULL;
nexpaq 0:6c56fb4bc5f0 40 }
nexpaq 0:6c56fb4bc5f0 41
nexpaq 0:6c56fb4bc5f0 42 M2MFirmware::M2MFirmware()
nexpaq 0:6c56fb4bc5f0 43 : M2MObject(M2M_FIRMWARE_ID)
nexpaq 0:6c56fb4bc5f0 44 {
nexpaq 0:6c56fb4bc5f0 45 M2MBase::set_register_uri(false);
nexpaq 0:6c56fb4bc5f0 46 M2MBase::set_operation(M2MBase::GET_PUT_ALLOWED);
nexpaq 0:6c56fb4bc5f0 47 _firmware_instance = M2MObject::create_object_instance();
nexpaq 0:6c56fb4bc5f0 48 if(_firmware_instance) {
nexpaq 0:6c56fb4bc5f0 49 _firmware_instance->set_operation(M2MBase::GET_PUT_ALLOWED);
nexpaq 0:6c56fb4bc5f0 50 create_mandatory_resources();
nexpaq 0:6c56fb4bc5f0 51 }
nexpaq 0:6c56fb4bc5f0 52 }
nexpaq 0:6c56fb4bc5f0 53
nexpaq 0:6c56fb4bc5f0 54 M2MFirmware::~M2MFirmware()
nexpaq 0:6c56fb4bc5f0 55 {
nexpaq 0:6c56fb4bc5f0 56 }
nexpaq 0:6c56fb4bc5f0 57
nexpaq 0:6c56fb4bc5f0 58 void M2MFirmware::create_mandatory_resources()
nexpaq 0:6c56fb4bc5f0 59 {
nexpaq 0:6c56fb4bc5f0 60 _firmware_instance->set_coap_content_type(COAP_CONTENT_OMA_TLV_TYPE);
nexpaq 0:6c56fb4bc5f0 61 M2MResource* res = _firmware_instance->create_dynamic_resource(FIRMWARE_PACKAGE,
nexpaq 0:6c56fb4bc5f0 62 OMA_RESOURCE_TYPE,
nexpaq 0:6c56fb4bc5f0 63 M2MResourceInstance::OPAQUE,
nexpaq 0:6c56fb4bc5f0 64 false);
nexpaq 0:6c56fb4bc5f0 65 if(res) {
nexpaq 0:6c56fb4bc5f0 66 res->set_operation(M2MBase::PUT_ALLOWED);
nexpaq 0:6c56fb4bc5f0 67 res->set_register_uri(false);
nexpaq 0:6c56fb4bc5f0 68 }
nexpaq 0:6c56fb4bc5f0 69
nexpaq 0:6c56fb4bc5f0 70 res = _firmware_instance->create_dynamic_resource(FIRMWARE_PACKAGE_URI,
nexpaq 0:6c56fb4bc5f0 71 OMA_RESOURCE_TYPE,
nexpaq 0:6c56fb4bc5f0 72 M2MResourceInstance::STRING,
nexpaq 0:6c56fb4bc5f0 73 false);
nexpaq 0:6c56fb4bc5f0 74 if(res) {
nexpaq 0:6c56fb4bc5f0 75 res->set_operation(M2MBase::PUT_ALLOWED);
nexpaq 0:6c56fb4bc5f0 76 res->set_register_uri(false);
nexpaq 0:6c56fb4bc5f0 77 }
nexpaq 0:6c56fb4bc5f0 78
nexpaq 0:6c56fb4bc5f0 79 res = _firmware_instance->create_dynamic_resource(FIRMWARE_UPDATE,
nexpaq 0:6c56fb4bc5f0 80 OMA_RESOURCE_TYPE,
nexpaq 0:6c56fb4bc5f0 81 M2MResourceInstance::OPAQUE,
nexpaq 0:6c56fb4bc5f0 82 false);
nexpaq 0:6c56fb4bc5f0 83 if(res) {
nexpaq 0:6c56fb4bc5f0 84 res->set_operation(M2MBase::NOT_ALLOWED);
nexpaq 0:6c56fb4bc5f0 85 res->set_register_uri(false);
nexpaq 0:6c56fb4bc5f0 86 }
nexpaq 0:6c56fb4bc5f0 87
nexpaq 0:6c56fb4bc5f0 88 res = _firmware_instance->create_dynamic_resource(FIRMWARE_STATE,
nexpaq 0:6c56fb4bc5f0 89 OMA_RESOURCE_TYPE,
nexpaq 0:6c56fb4bc5f0 90 M2MResourceInstance::INTEGER,
nexpaq 0:6c56fb4bc5f0 91 true);
nexpaq 0:6c56fb4bc5f0 92 set_zero_value(res);
nexpaq 0:6c56fb4bc5f0 93 if(res) {
nexpaq 0:6c56fb4bc5f0 94 res->set_operation(M2MBase::GET_ALLOWED);
nexpaq 0:6c56fb4bc5f0 95 res->set_register_uri(false);
nexpaq 0:6c56fb4bc5f0 96 }
nexpaq 0:6c56fb4bc5f0 97 res = _firmware_instance->create_dynamic_resource(FIRMWARE_UPDATE_RESULT,
nexpaq 0:6c56fb4bc5f0 98 OMA_RESOURCE_TYPE,
nexpaq 0:6c56fb4bc5f0 99 M2MResourceInstance::INTEGER,
nexpaq 0:6c56fb4bc5f0 100 true);
nexpaq 0:6c56fb4bc5f0 101 set_zero_value(res);
nexpaq 0:6c56fb4bc5f0 102 if(res) {
nexpaq 0:6c56fb4bc5f0 103 res->set_operation(M2MBase::GET_ALLOWED);
nexpaq 0:6c56fb4bc5f0 104 res->set_register_uri(false);
nexpaq 0:6c56fb4bc5f0 105 }
nexpaq 0:6c56fb4bc5f0 106 }
nexpaq 0:6c56fb4bc5f0 107
nexpaq 0:6c56fb4bc5f0 108 M2MResource* M2MFirmware::create_resource(FirmwareResource resource, const String &value)
nexpaq 0:6c56fb4bc5f0 109 {
nexpaq 0:6c56fb4bc5f0 110 M2MResource* res = NULL;
nexpaq 0:6c56fb4bc5f0 111 const char* firmware_id_ptr = "";
nexpaq 0:6c56fb4bc5f0 112 M2MBase::Operation operation = M2MBase::GET_ALLOWED;
nexpaq 0:6c56fb4bc5f0 113 if(!is_resource_present(resource)) {
nexpaq 0:6c56fb4bc5f0 114 switch(resource) {
nexpaq 0:6c56fb4bc5f0 115 case PackageName:
nexpaq 0:6c56fb4bc5f0 116 firmware_id_ptr = FIRMWARE_PACKAGE_NAME;
nexpaq 0:6c56fb4bc5f0 117 break;
nexpaq 0:6c56fb4bc5f0 118 case PackageVersion:
nexpaq 0:6c56fb4bc5f0 119 firmware_id_ptr = FIRMWARE_PACKAGE_VERSION;
nexpaq 0:6c56fb4bc5f0 120 break;
nexpaq 0:6c56fb4bc5f0 121 default:
nexpaq 0:6c56fb4bc5f0 122 break;
nexpaq 0:6c56fb4bc5f0 123 }
nexpaq 0:6c56fb4bc5f0 124 }
nexpaq 0:6c56fb4bc5f0 125 String firmware_id(firmware_id_ptr);
nexpaq 0:6c56fb4bc5f0 126
nexpaq 0:6c56fb4bc5f0 127 if(!firmware_id.empty() && value.size() < 256) {
nexpaq 0:6c56fb4bc5f0 128 if(_firmware_instance) {
nexpaq 0:6c56fb4bc5f0 129 res = _firmware_instance->create_dynamic_resource(firmware_id,
nexpaq 0:6c56fb4bc5f0 130 OMA_RESOURCE_TYPE,
nexpaq 0:6c56fb4bc5f0 131 M2MResourceInstance::STRING,
nexpaq 0:6c56fb4bc5f0 132 false);
nexpaq 0:6c56fb4bc5f0 133
nexpaq 0:6c56fb4bc5f0 134 if(res) {
nexpaq 0:6c56fb4bc5f0 135 res->set_register_uri(false);
nexpaq 0:6c56fb4bc5f0 136 res->set_operation(operation);
nexpaq 0:6c56fb4bc5f0 137 if(value.empty()) {
nexpaq 0:6c56fb4bc5f0 138 res->clear_value();
nexpaq 0:6c56fb4bc5f0 139 } else {
nexpaq 0:6c56fb4bc5f0 140 res->set_value((const uint8_t*)value.c_str(),
nexpaq 0:6c56fb4bc5f0 141 (uint32_t)value.length());
nexpaq 0:6c56fb4bc5f0 142 }
nexpaq 0:6c56fb4bc5f0 143 }
nexpaq 0:6c56fb4bc5f0 144 }
nexpaq 0:6c56fb4bc5f0 145 }
nexpaq 0:6c56fb4bc5f0 146 return res;
nexpaq 0:6c56fb4bc5f0 147 }
nexpaq 0:6c56fb4bc5f0 148
nexpaq 0:6c56fb4bc5f0 149 M2MResource* M2MFirmware::create_resource(FirmwareResource resource, int64_t value)
nexpaq 0:6c56fb4bc5f0 150 {
nexpaq 0:6c56fb4bc5f0 151 M2MResource* res = NULL;
nexpaq 0:6c56fb4bc5f0 152 const char* firmware_id_ptr = "";
nexpaq 0:6c56fb4bc5f0 153 M2MBase::Operation operation = M2MBase::GET_ALLOWED;
nexpaq 0:6c56fb4bc5f0 154 if(!is_resource_present(resource)) {
nexpaq 0:6c56fb4bc5f0 155 switch(resource) {
nexpaq 0:6c56fb4bc5f0 156 case UpdateSupportedObjects:
nexpaq 0:6c56fb4bc5f0 157 if(check_value_range(resource, value)) {
nexpaq 0:6c56fb4bc5f0 158 firmware_id_ptr = FIRMWARE_UPDATE_SUPPORTED_OBJECTS;
nexpaq 0:6c56fb4bc5f0 159 operation = M2MBase::GET_PUT_ALLOWED;
nexpaq 0:6c56fb4bc5f0 160 }
nexpaq 0:6c56fb4bc5f0 161 break;
nexpaq 0:6c56fb4bc5f0 162 default:
nexpaq 0:6c56fb4bc5f0 163 break;
nexpaq 0:6c56fb4bc5f0 164 }
nexpaq 0:6c56fb4bc5f0 165 }
nexpaq 0:6c56fb4bc5f0 166
nexpaq 0:6c56fb4bc5f0 167 const String firmware_id(firmware_id_ptr);
nexpaq 0:6c56fb4bc5f0 168
nexpaq 0:6c56fb4bc5f0 169 if(!firmware_id.empty()) {
nexpaq 0:6c56fb4bc5f0 170 if(_firmware_instance) {
nexpaq 0:6c56fb4bc5f0 171 res = _firmware_instance->create_dynamic_resource(firmware_id,
nexpaq 0:6c56fb4bc5f0 172 OMA_RESOURCE_TYPE,
nexpaq 0:6c56fb4bc5f0 173 M2MResourceInstance::INTEGER,
nexpaq 0:6c56fb4bc5f0 174 false);
nexpaq 0:6c56fb4bc5f0 175
nexpaq 0:6c56fb4bc5f0 176 if(res) {
nexpaq 0:6c56fb4bc5f0 177 res->set_register_uri(false);
nexpaq 0:6c56fb4bc5f0 178
nexpaq 0:6c56fb4bc5f0 179 res->set_operation(operation);
nexpaq 0:6c56fb4bc5f0 180 res->set_value(value);
nexpaq 0:6c56fb4bc5f0 181 }
nexpaq 0:6c56fb4bc5f0 182 }
nexpaq 0:6c56fb4bc5f0 183 }
nexpaq 0:6c56fb4bc5f0 184 return res;
nexpaq 0:6c56fb4bc5f0 185 }
nexpaq 0:6c56fb4bc5f0 186
nexpaq 0:6c56fb4bc5f0 187 bool M2MFirmware::set_resource_value(FirmwareResource resource,
nexpaq 0:6c56fb4bc5f0 188 const String &value)
nexpaq 0:6c56fb4bc5f0 189 {
nexpaq 0:6c56fb4bc5f0 190 bool success = false;
nexpaq 0:6c56fb4bc5f0 191 M2MResource* res = get_resource(resource);
nexpaq 0:6c56fb4bc5f0 192 if(res) {
nexpaq 0:6c56fb4bc5f0 193 if(M2MFirmware::PackageUri == resource ||
nexpaq 0:6c56fb4bc5f0 194 M2MFirmware::PackageName == resource ||
nexpaq 0:6c56fb4bc5f0 195 M2MFirmware::PackageVersion == resource) {
nexpaq 0:6c56fb4bc5f0 196 if (value.size() < 256) {
nexpaq 0:6c56fb4bc5f0 197 if(value.empty()) {
nexpaq 0:6c56fb4bc5f0 198 res->clear_value();
nexpaq 0:6c56fb4bc5f0 199 success = true;
nexpaq 0:6c56fb4bc5f0 200 } else {
nexpaq 0:6c56fb4bc5f0 201 success = res->set_value((const uint8_t*)value.c_str(),(uint32_t)value.length());
nexpaq 0:6c56fb4bc5f0 202 }
nexpaq 0:6c56fb4bc5f0 203 }
nexpaq 0:6c56fb4bc5f0 204 }
nexpaq 0:6c56fb4bc5f0 205 }
nexpaq 0:6c56fb4bc5f0 206 return success;
nexpaq 0:6c56fb4bc5f0 207 }
nexpaq 0:6c56fb4bc5f0 208
nexpaq 0:6c56fb4bc5f0 209 bool M2MFirmware::set_resource_value(FirmwareResource resource,
nexpaq 0:6c56fb4bc5f0 210 int64_t value)
nexpaq 0:6c56fb4bc5f0 211 {
nexpaq 0:6c56fb4bc5f0 212 bool success = false;
nexpaq 0:6c56fb4bc5f0 213 M2MResource* res = get_resource(resource);
nexpaq 0:6c56fb4bc5f0 214 if(res) {
nexpaq 0:6c56fb4bc5f0 215 if(M2MFirmware::State == resource ||
nexpaq 0:6c56fb4bc5f0 216 M2MFirmware::UpdateSupportedObjects == resource ||
nexpaq 0:6c56fb4bc5f0 217 M2MFirmware::UpdateResult == resource) {
nexpaq 0:6c56fb4bc5f0 218 // If it is any of the above resource
nexpaq 0:6c56fb4bc5f0 219 // set the value of the resource.
nexpaq 0:6c56fb4bc5f0 220 if (check_value_range(resource, value)) {
nexpaq 0:6c56fb4bc5f0 221
nexpaq 0:6c56fb4bc5f0 222 success = res->set_value(value);
nexpaq 0:6c56fb4bc5f0 223 }
nexpaq 0:6c56fb4bc5f0 224 }
nexpaq 0:6c56fb4bc5f0 225 }
nexpaq 0:6c56fb4bc5f0 226 return success;
nexpaq 0:6c56fb4bc5f0 227 }
nexpaq 0:6c56fb4bc5f0 228
nexpaq 0:6c56fb4bc5f0 229 bool M2MFirmware::set_resource_value(FirmwareResource resource,
nexpaq 0:6c56fb4bc5f0 230 const uint8_t *value,
nexpaq 0:6c56fb4bc5f0 231 const uint32_t length)
nexpaq 0:6c56fb4bc5f0 232 {
nexpaq 0:6c56fb4bc5f0 233 bool success = false;
nexpaq 0:6c56fb4bc5f0 234 M2MResource* res = get_resource(resource);
nexpaq 0:6c56fb4bc5f0 235 if(res) {
nexpaq 0:6c56fb4bc5f0 236 if(M2MFirmware::Package == resource) {
nexpaq 0:6c56fb4bc5f0 237 success = res->set_value(value,length);
nexpaq 0:6c56fb4bc5f0 238 }
nexpaq 0:6c56fb4bc5f0 239 }
nexpaq 0:6c56fb4bc5f0 240 return success;
nexpaq 0:6c56fb4bc5f0 241 }
nexpaq 0:6c56fb4bc5f0 242
nexpaq 0:6c56fb4bc5f0 243 bool M2MFirmware::is_resource_present(FirmwareResource resource) const
nexpaq 0:6c56fb4bc5f0 244 {
nexpaq 0:6c56fb4bc5f0 245 bool success = false;
nexpaq 0:6c56fb4bc5f0 246 M2MResource* res = get_resource(resource);
nexpaq 0:6c56fb4bc5f0 247 if(res) {
nexpaq 0:6c56fb4bc5f0 248 success = true;
nexpaq 0:6c56fb4bc5f0 249 }
nexpaq 0:6c56fb4bc5f0 250 return success;
nexpaq 0:6c56fb4bc5f0 251 }
nexpaq 0:6c56fb4bc5f0 252
nexpaq 0:6c56fb4bc5f0 253 const String M2MFirmware::resource_name(FirmwareResource resource)
nexpaq 0:6c56fb4bc5f0 254 {
nexpaq 0:6c56fb4bc5f0 255 const char* res_name = "";
nexpaq 0:6c56fb4bc5f0 256 switch(resource) {
nexpaq 0:6c56fb4bc5f0 257 case Package:
nexpaq 0:6c56fb4bc5f0 258 res_name = FIRMWARE_PACKAGE;
nexpaq 0:6c56fb4bc5f0 259 break;
nexpaq 0:6c56fb4bc5f0 260 case PackageUri:
nexpaq 0:6c56fb4bc5f0 261 res_name = FIRMWARE_PACKAGE_URI;
nexpaq 0:6c56fb4bc5f0 262 break;
nexpaq 0:6c56fb4bc5f0 263 case Update:
nexpaq 0:6c56fb4bc5f0 264 res_name = FIRMWARE_UPDATE;
nexpaq 0:6c56fb4bc5f0 265 break;
nexpaq 0:6c56fb4bc5f0 266 case State:
nexpaq 0:6c56fb4bc5f0 267 res_name = FIRMWARE_STATE;
nexpaq 0:6c56fb4bc5f0 268 break;
nexpaq 0:6c56fb4bc5f0 269 case UpdateSupportedObjects:
nexpaq 0:6c56fb4bc5f0 270 res_name = FIRMWARE_UPDATE_SUPPORTED_OBJECTS;
nexpaq 0:6c56fb4bc5f0 271 break;
nexpaq 0:6c56fb4bc5f0 272 case UpdateResult:
nexpaq 0:6c56fb4bc5f0 273 res_name = FIRMWARE_UPDATE_RESULT;
nexpaq 0:6c56fb4bc5f0 274 break;
nexpaq 0:6c56fb4bc5f0 275 case PackageName:
nexpaq 0:6c56fb4bc5f0 276 res_name = FIRMWARE_PACKAGE_NAME;
nexpaq 0:6c56fb4bc5f0 277 break;
nexpaq 0:6c56fb4bc5f0 278 case PackageVersion:
nexpaq 0:6c56fb4bc5f0 279 res_name = FIRMWARE_PACKAGE_VERSION;
nexpaq 0:6c56fb4bc5f0 280 break;
nexpaq 0:6c56fb4bc5f0 281 }
nexpaq 0:6c56fb4bc5f0 282 return String(res_name);
nexpaq 0:6c56fb4bc5f0 283 }
nexpaq 0:6c56fb4bc5f0 284
nexpaq 0:6c56fb4bc5f0 285 uint16_t M2MFirmware::per_resource_count(FirmwareResource res) const
nexpaq 0:6c56fb4bc5f0 286 {
nexpaq 0:6c56fb4bc5f0 287 uint16_t count = 0;
nexpaq 0:6c56fb4bc5f0 288 if(_firmware_instance) {
nexpaq 0:6c56fb4bc5f0 289 count = _firmware_instance->resource_count(resource_name(res));
nexpaq 0:6c56fb4bc5f0 290 }
nexpaq 0:6c56fb4bc5f0 291 return count;
nexpaq 0:6c56fb4bc5f0 292 }
nexpaq 0:6c56fb4bc5f0 293
nexpaq 0:6c56fb4bc5f0 294 uint16_t M2MFirmware::total_resource_count() const
nexpaq 0:6c56fb4bc5f0 295 {
nexpaq 0:6c56fb4bc5f0 296 uint16_t count = 0;
nexpaq 0:6c56fb4bc5f0 297 if(_firmware_instance) {
nexpaq 0:6c56fb4bc5f0 298 count = _firmware_instance->resources().size();
nexpaq 0:6c56fb4bc5f0 299 }
nexpaq 0:6c56fb4bc5f0 300 return count;
nexpaq 0:6c56fb4bc5f0 301 }
nexpaq 0:6c56fb4bc5f0 302
nexpaq 0:6c56fb4bc5f0 303 uint32_t M2MFirmware::resource_value_buffer(FirmwareResource resource,
nexpaq 0:6c56fb4bc5f0 304 uint8_t *&data) const
nexpaq 0:6c56fb4bc5f0 305 {
nexpaq 0:6c56fb4bc5f0 306 uint32_t size = 0;
nexpaq 0:6c56fb4bc5f0 307 M2MResource* res = get_resource(resource);
nexpaq 0:6c56fb4bc5f0 308 if(res) {
nexpaq 0:6c56fb4bc5f0 309 if(M2MFirmware::Package == resource) {
nexpaq 0:6c56fb4bc5f0 310 res->get_value(data,size);
nexpaq 0:6c56fb4bc5f0 311 }
nexpaq 0:6c56fb4bc5f0 312 }
nexpaq 0:6c56fb4bc5f0 313 return size;
nexpaq 0:6c56fb4bc5f0 314 }
nexpaq 0:6c56fb4bc5f0 315
nexpaq 0:6c56fb4bc5f0 316 M2MResource* M2MFirmware::get_resource(FirmwareResource res) const
nexpaq 0:6c56fb4bc5f0 317 {
nexpaq 0:6c56fb4bc5f0 318 M2MResource* res_object = NULL;
nexpaq 0:6c56fb4bc5f0 319 if(_firmware_instance) {
nexpaq 0:6c56fb4bc5f0 320 const char* res_name_ptr = "";
nexpaq 0:6c56fb4bc5f0 321 switch(res) {
nexpaq 0:6c56fb4bc5f0 322 case Package:
nexpaq 0:6c56fb4bc5f0 323 res_name_ptr = FIRMWARE_PACKAGE;
nexpaq 0:6c56fb4bc5f0 324 break;
nexpaq 0:6c56fb4bc5f0 325 case PackageUri:
nexpaq 0:6c56fb4bc5f0 326 res_name_ptr = FIRMWARE_PACKAGE_URI;
nexpaq 0:6c56fb4bc5f0 327 break;
nexpaq 0:6c56fb4bc5f0 328 case Update:
nexpaq 0:6c56fb4bc5f0 329 res_name_ptr = FIRMWARE_UPDATE;
nexpaq 0:6c56fb4bc5f0 330 break;
nexpaq 0:6c56fb4bc5f0 331 case State:
nexpaq 0:6c56fb4bc5f0 332 res_name_ptr = FIRMWARE_STATE;
nexpaq 0:6c56fb4bc5f0 333 break;
nexpaq 0:6c56fb4bc5f0 334 case UpdateSupportedObjects:
nexpaq 0:6c56fb4bc5f0 335 res_name_ptr = FIRMWARE_UPDATE_SUPPORTED_OBJECTS;
nexpaq 0:6c56fb4bc5f0 336 break;
nexpaq 0:6c56fb4bc5f0 337 case UpdateResult:
nexpaq 0:6c56fb4bc5f0 338 res_name_ptr = FIRMWARE_UPDATE_RESULT;
nexpaq 0:6c56fb4bc5f0 339 break;
nexpaq 0:6c56fb4bc5f0 340 case PackageName:
nexpaq 0:6c56fb4bc5f0 341 res_name_ptr = FIRMWARE_PACKAGE_NAME;
nexpaq 0:6c56fb4bc5f0 342 break;
nexpaq 0:6c56fb4bc5f0 343 case PackageVersion:
nexpaq 0:6c56fb4bc5f0 344 res_name_ptr = FIRMWARE_PACKAGE_VERSION;
nexpaq 0:6c56fb4bc5f0 345 break;
nexpaq 0:6c56fb4bc5f0 346 }
nexpaq 0:6c56fb4bc5f0 347 const String res_name(res_name_ptr);
nexpaq 0:6c56fb4bc5f0 348
nexpaq 0:6c56fb4bc5f0 349 res_object = _firmware_instance->resource(res_name);
nexpaq 0:6c56fb4bc5f0 350 }
nexpaq 0:6c56fb4bc5f0 351 return res_object;
nexpaq 0:6c56fb4bc5f0 352 }
nexpaq 0:6c56fb4bc5f0 353
nexpaq 0:6c56fb4bc5f0 354 bool M2MFirmware::delete_resource(FirmwareResource resource)
nexpaq 0:6c56fb4bc5f0 355 {
nexpaq 0:6c56fb4bc5f0 356 bool success = false;
nexpaq 0:6c56fb4bc5f0 357 if(M2MFirmware::UpdateSupportedObjects == resource ||
nexpaq 0:6c56fb4bc5f0 358 M2MFirmware::PackageName == resource ||
nexpaq 0:6c56fb4bc5f0 359 M2MFirmware::PackageVersion == resource) {
nexpaq 0:6c56fb4bc5f0 360 if(_firmware_instance) {
nexpaq 0:6c56fb4bc5f0 361 success = _firmware_instance->remove_resource(resource_name(resource));
nexpaq 0:6c56fb4bc5f0 362 }
nexpaq 0:6c56fb4bc5f0 363 }
nexpaq 0:6c56fb4bc5f0 364 return success;
nexpaq 0:6c56fb4bc5f0 365 }
nexpaq 0:6c56fb4bc5f0 366
nexpaq 0:6c56fb4bc5f0 367 int64_t M2MFirmware::resource_value_int(FirmwareResource resource) const
nexpaq 0:6c56fb4bc5f0 368 {
nexpaq 0:6c56fb4bc5f0 369 int64_t value = -1;
nexpaq 0:6c56fb4bc5f0 370 M2MResource* res = get_resource(resource);
nexpaq 0:6c56fb4bc5f0 371 if(res) {
nexpaq 0:6c56fb4bc5f0 372 if(M2MFirmware::State == resource ||
nexpaq 0:6c56fb4bc5f0 373 M2MFirmware::UpdateSupportedObjects == resource ||
nexpaq 0:6c56fb4bc5f0 374 M2MFirmware::UpdateResult == resource) {
nexpaq 0:6c56fb4bc5f0 375
nexpaq 0:6c56fb4bc5f0 376 value = res->get_value_int();
nexpaq 0:6c56fb4bc5f0 377 }
nexpaq 0:6c56fb4bc5f0 378 }
nexpaq 0:6c56fb4bc5f0 379 return value;
nexpaq 0:6c56fb4bc5f0 380 }
nexpaq 0:6c56fb4bc5f0 381
nexpaq 0:6c56fb4bc5f0 382 String M2MFirmware::resource_value_string(FirmwareResource resource) const
nexpaq 0:6c56fb4bc5f0 383 {
nexpaq 0:6c56fb4bc5f0 384 String value = "";
nexpaq 0:6c56fb4bc5f0 385 M2MResource* res = get_resource(resource);
nexpaq 0:6c56fb4bc5f0 386 if(res) {
nexpaq 0:6c56fb4bc5f0 387 if(M2MFirmware::PackageUri == resource ||
nexpaq 0:6c56fb4bc5f0 388 M2MFirmware::PackageName == resource ||
nexpaq 0:6c56fb4bc5f0 389 M2MFirmware::PackageVersion == resource) {
nexpaq 0:6c56fb4bc5f0 390
nexpaq 0:6c56fb4bc5f0 391 value = res->get_value_string();
nexpaq 0:6c56fb4bc5f0 392 }
nexpaq 0:6c56fb4bc5f0 393 }
nexpaq 0:6c56fb4bc5f0 394 return value;
nexpaq 0:6c56fb4bc5f0 395 }
nexpaq 0:6c56fb4bc5f0 396
nexpaq 0:6c56fb4bc5f0 397 bool M2MFirmware::check_value_range(FirmwareResource resource, int64_t value) const
nexpaq 0:6c56fb4bc5f0 398 {
nexpaq 0:6c56fb4bc5f0 399 bool success = false;
nexpaq 0:6c56fb4bc5f0 400 switch (resource) {
nexpaq 0:6c56fb4bc5f0 401 case UpdateSupportedObjects:
nexpaq 0:6c56fb4bc5f0 402 if(value == 0 || value == 1) {
nexpaq 0:6c56fb4bc5f0 403 success = true;
nexpaq 0:6c56fb4bc5f0 404 }
nexpaq 0:6c56fb4bc5f0 405 break;
nexpaq 0:6c56fb4bc5f0 406 case State:
nexpaq 0:6c56fb4bc5f0 407 if (value >= 0 && value <= 3) {
nexpaq 0:6c56fb4bc5f0 408 success = true;
nexpaq 0:6c56fb4bc5f0 409 M2MResource* updateRes = get_resource(M2MFirmware::Update);
nexpaq 0:6c56fb4bc5f0 410 if (updateRes){
nexpaq 0:6c56fb4bc5f0 411 if (value == M2MFirmware::Downloaded) {
nexpaq 0:6c56fb4bc5f0 412 updateRes->set_operation(M2MBase::POST_ALLOWED);
nexpaq 0:6c56fb4bc5f0 413 }
nexpaq 0:6c56fb4bc5f0 414 else {
nexpaq 0:6c56fb4bc5f0 415 updateRes->set_operation(M2MBase::NOT_ALLOWED);
nexpaq 0:6c56fb4bc5f0 416 }
nexpaq 0:6c56fb4bc5f0 417 }
nexpaq 0:6c56fb4bc5f0 418 }
nexpaq 0:6c56fb4bc5f0 419 break;
nexpaq 0:6c56fb4bc5f0 420 case UpdateResult:
nexpaq 0:6c56fb4bc5f0 421 if (value >= 0 && value <= 7) {
nexpaq 0:6c56fb4bc5f0 422 success = true;
nexpaq 0:6c56fb4bc5f0 423 }
nexpaq 0:6c56fb4bc5f0 424 break;
nexpaq 0:6c56fb4bc5f0 425 default:
nexpaq 0:6c56fb4bc5f0 426 break;
nexpaq 0:6c56fb4bc5f0 427 }
nexpaq 0:6c56fb4bc5f0 428 return success;
nexpaq 0:6c56fb4bc5f0 429 }
nexpaq 0:6c56fb4bc5f0 430
nexpaq 0:6c56fb4bc5f0 431 void M2MFirmware::set_zero_value(M2MResource *resource)
nexpaq 0:6c56fb4bc5f0 432 {
nexpaq 0:6c56fb4bc5f0 433 resource->set_value((const uint8_t*)"0", 1);
nexpaq 0:6c56fb4bc5f0 434 }