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