This is an example of BLE GATT Client, which receives broadcast data from BLE_Server_BME280 ( a GATT server) , then transfers values up to mbed Device Connector (cloud).
Please refer details about BLEClient_mbedDevConn below. https://github.com/soramame21/BLEClient_mbedDevConn
The location of required BLE GATT server, BLE_Server_BME280, is at here. https://developer.mbed.org/users/edamame22/code/BLE_Server_BME280/
mbed-client/test/lwm2mtestapplication/lwm2mtest.cpp@0:29983394c6b6, 2017-04-13 (annotated)
- Committer:
- edamame22
- Date:
- Thu Apr 13 04:48:11 2017 +0000
- Revision:
- 0:29983394c6b6
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
edamame22 | 0:29983394c6b6 | 1 | /* |
edamame22 | 0:29983394c6b6 | 2 | * Copyright (c) 2015 ARM Limited. All rights reserved. |
edamame22 | 0:29983394c6b6 | 3 | * SPDX-License-Identifier: Apache-2.0 |
edamame22 | 0:29983394c6b6 | 4 | * Licensed under the Apache License, Version 2.0 (the License); you may |
edamame22 | 0:29983394c6b6 | 5 | * not use this file except in compliance with the License. |
edamame22 | 0:29983394c6b6 | 6 | * You may obtain a copy of the License at |
edamame22 | 0:29983394c6b6 | 7 | * |
edamame22 | 0:29983394c6b6 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
edamame22 | 0:29983394c6b6 | 9 | * |
edamame22 | 0:29983394c6b6 | 10 | * Unless required by applicable law or agreed to in writing, software |
edamame22 | 0:29983394c6b6 | 11 | * distributed under the License is distributed on an AS IS BASIS, WITHOUT |
edamame22 | 0:29983394c6b6 | 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
edamame22 | 0:29983394c6b6 | 13 | * See the License for the specific language governing permissions and |
edamame22 | 0:29983394c6b6 | 14 | * limitations under the License. |
edamame22 | 0:29983394c6b6 | 15 | */ |
edamame22 | 0:29983394c6b6 | 16 | #include "lwm2mtest.h" |
edamame22 | 0:29983394c6b6 | 17 | #include "ns_types.h" |
edamame22 | 0:29983394c6b6 | 18 | #include "ns_cmdline.h" |
edamame22 | 0:29983394c6b6 | 19 | |
edamame22 | 0:29983394c6b6 | 20 | M2MLWClient::M2MLWClient() |
edamame22 | 0:29983394c6b6 | 21 | : _security(NULL), |
edamame22 | 0:29983394c6b6 | 22 | _interface(NULL), |
edamame22 | 0:29983394c6b6 | 23 | _device(NULL), |
edamame22 | 0:29983394c6b6 | 24 | _bootstrapped(false), |
edamame22 | 0:29983394c6b6 | 25 | _error(false), |
edamame22 | 0:29983394c6b6 | 26 | _registered(false), |
edamame22 | 0:29983394c6b6 | 27 | _unregistered(false), |
edamame22 | 0:29983394c6b6 | 28 | _registration_updated(false) |
edamame22 | 0:29983394c6b6 | 29 | { |
edamame22 | 0:29983394c6b6 | 30 | } |
edamame22 | 0:29983394c6b6 | 31 | |
edamame22 | 0:29983394c6b6 | 32 | M2MLWClient::~M2MLWClient() |
edamame22 | 0:29983394c6b6 | 33 | { |
edamame22 | 0:29983394c6b6 | 34 | if(_interface) { |
edamame22 | 0:29983394c6b6 | 35 | delete _interface; |
edamame22 | 0:29983394c6b6 | 36 | } |
edamame22 | 0:29983394c6b6 | 37 | if(_security) { |
edamame22 | 0:29983394c6b6 | 38 | delete _security; |
edamame22 | 0:29983394c6b6 | 39 | } |
edamame22 | 0:29983394c6b6 | 40 | if( _register_security){ |
edamame22 | 0:29983394c6b6 | 41 | delete _register_security; |
edamame22 | 0:29983394c6b6 | 42 | } |
edamame22 | 0:29983394c6b6 | 43 | } |
edamame22 | 0:29983394c6b6 | 44 | |
edamame22 | 0:29983394c6b6 | 45 | |
edamame22 | 0:29983394c6b6 | 46 | bool M2MLWClient::create_interface(int32_t param_count, |
edamame22 | 0:29983394c6b6 | 47 | const char *endpoint, |
edamame22 | 0:29983394c6b6 | 48 | const char *resource_type, |
edamame22 | 0:29983394c6b6 | 49 | const int32_t lifetime, |
edamame22 | 0:29983394c6b6 | 50 | const uint16_t listen_port, |
edamame22 | 0:29983394c6b6 | 51 | const char *domain, |
edamame22 | 0:29983394c6b6 | 52 | const uint8_t binding_mode, |
edamame22 | 0:29983394c6b6 | 53 | const uint8_t network_interface) |
edamame22 | 0:29983394c6b6 | 54 | { |
edamame22 | 0:29983394c6b6 | 55 | if(_interface) { |
edamame22 | 0:29983394c6b6 | 56 | delete _interface; |
edamame22 | 0:29983394c6b6 | 57 | _interface = NULL; |
edamame22 | 0:29983394c6b6 | 58 | } |
edamame22 | 0:29983394c6b6 | 59 | String ep; |
edamame22 | 0:29983394c6b6 | 60 | String rt; |
edamame22 | 0:29983394c6b6 | 61 | String dmn; |
edamame22 | 0:29983394c6b6 | 62 | if(endpoint) { |
edamame22 | 0:29983394c6b6 | 63 | ep += endpoint; |
edamame22 | 0:29983394c6b6 | 64 | } |
edamame22 | 0:29983394c6b6 | 65 | if(resource_type) { |
edamame22 | 0:29983394c6b6 | 66 | rt += resource_type; |
edamame22 | 0:29983394c6b6 | 67 | } |
edamame22 | 0:29983394c6b6 | 68 | if(domain) { |
edamame22 | 0:29983394c6b6 | 69 | dmn += domain; |
edamame22 | 0:29983394c6b6 | 70 | } |
edamame22 | 0:29983394c6b6 | 71 | |
edamame22 | 0:29983394c6b6 | 72 | // Binding mode cannot be higher than 0x07 since it is an enum, check M2MInterface::BindingMode |
edamame22 | 0:29983394c6b6 | 73 | if(binding_mode > 0x07) { |
edamame22 | 0:29983394c6b6 | 74 | return false; |
edamame22 | 0:29983394c6b6 | 75 | } |
edamame22 | 0:29983394c6b6 | 76 | |
edamame22 | 0:29983394c6b6 | 77 | switch (param_count) { |
edamame22 | 0:29983394c6b6 | 78 | case 0: |
edamame22 | 0:29983394c6b6 | 79 | _interface = M2MInterfaceFactory::create_interface(*this, ep, rt); |
edamame22 | 0:29983394c6b6 | 80 | break; |
edamame22 | 0:29983394c6b6 | 81 | case 1: |
edamame22 | 0:29983394c6b6 | 82 | _interface = M2MInterfaceFactory::create_interface(*this, ep, rt, lifetime); |
edamame22 | 0:29983394c6b6 | 83 | break; |
edamame22 | 0:29983394c6b6 | 84 | case 2: |
edamame22 | 0:29983394c6b6 | 85 | _interface = M2MInterfaceFactory::create_interface(*this, ep, rt, lifetime, listen_port); |
edamame22 | 0:29983394c6b6 | 86 | break; |
edamame22 | 0:29983394c6b6 | 87 | case 3: |
edamame22 | 0:29983394c6b6 | 88 | _interface = M2MInterfaceFactory::create_interface(*this, ep, rt, lifetime, listen_port, dmn); |
edamame22 | 0:29983394c6b6 | 89 | break; |
edamame22 | 0:29983394c6b6 | 90 | case 4: |
edamame22 | 0:29983394c6b6 | 91 | _interface = M2MInterfaceFactory::create_interface(*this, ep, rt, lifetime, listen_port, dmn, (M2MInterface::BindingMode)binding_mode); |
edamame22 | 0:29983394c6b6 | 92 | break; |
edamame22 | 0:29983394c6b6 | 93 | case 5: |
edamame22 | 0:29983394c6b6 | 94 | _interface = M2MInterfaceFactory::create_interface(*this, ep, rt, lifetime, listen_port, dmn, (M2MInterface::BindingMode)binding_mode, (M2MInterface::NetworkStack)network_interface); |
edamame22 | 0:29983394c6b6 | 95 | break; |
edamame22 | 0:29983394c6b6 | 96 | } |
edamame22 | 0:29983394c6b6 | 97 | return (_interface == NULL) ? false : true; |
edamame22 | 0:29983394c6b6 | 98 | } |
edamame22 | 0:29983394c6b6 | 99 | |
edamame22 | 0:29983394c6b6 | 100 | bool M2MLWClient::create_bootstrap_object(const char *coap_bootstrap_address) |
edamame22 | 0:29983394c6b6 | 101 | { |
edamame22 | 0:29983394c6b6 | 102 | bool success = false; |
edamame22 | 0:29983394c6b6 | 103 | String address; |
edamame22 | 0:29983394c6b6 | 104 | if(coap_bootstrap_address) { |
edamame22 | 0:29983394c6b6 | 105 | address += coap_bootstrap_address; |
edamame22 | 0:29983394c6b6 | 106 | } |
edamame22 | 0:29983394c6b6 | 107 | if(_security) { |
edamame22 | 0:29983394c6b6 | 108 | delete _security; |
edamame22 | 0:29983394c6b6 | 109 | } |
edamame22 | 0:29983394c6b6 | 110 | _security = M2MInterfaceFactory::create_security(M2MSecurity::Bootstrap); |
edamame22 | 0:29983394c6b6 | 111 | if(_security) { |
edamame22 | 0:29983394c6b6 | 112 | if(_security->set_resource_value(M2MSecurity::M2MServerUri, address) && |
edamame22 | 0:29983394c6b6 | 113 | _security->set_resource_value(M2MSecurity::SecurityMode, M2MSecurity::NoSecurity)) { |
edamame22 | 0:29983394c6b6 | 114 | success = true; |
edamame22 | 0:29983394c6b6 | 115 | } |
edamame22 | 0:29983394c6b6 | 116 | } |
edamame22 | 0:29983394c6b6 | 117 | return success; |
edamame22 | 0:29983394c6b6 | 118 | } |
edamame22 | 0:29983394c6b6 | 119 | |
edamame22 | 0:29983394c6b6 | 120 | bool M2MLWClient::create_register_object(const char *coap_register_address, bool useSecureConn) |
edamame22 | 0:29983394c6b6 | 121 | { |
edamame22 | 0:29983394c6b6 | 122 | bool success = false; |
edamame22 | 0:29983394c6b6 | 123 | String address; |
edamame22 | 0:29983394c6b6 | 124 | if(coap_register_address) { |
edamame22 | 0:29983394c6b6 | 125 | address += coap_register_address; |
edamame22 | 0:29983394c6b6 | 126 | } |
edamame22 | 0:29983394c6b6 | 127 | if(_register_security) { |
edamame22 | 0:29983394c6b6 | 128 | delete _register_security; |
edamame22 | 0:29983394c6b6 | 129 | } |
edamame22 | 0:29983394c6b6 | 130 | _register_security = M2MInterfaceFactory::create_security(M2MSecurity::M2MServer); |
edamame22 | 0:29983394c6b6 | 131 | if(_register_security) { |
edamame22 | 0:29983394c6b6 | 132 | if( !useSecureConn ){ |
edamame22 | 0:29983394c6b6 | 133 | if(_register_security->set_resource_value(M2MSecurity::M2MServerUri, address) && |
edamame22 | 0:29983394c6b6 | 134 | _register_security->set_resource_value(M2MSecurity::SecurityMode, M2MSecurity::NoSecurity)) { |
edamame22 | 0:29983394c6b6 | 135 | success = true; |
edamame22 | 0:29983394c6b6 | 136 | } |
edamame22 | 0:29983394c6b6 | 137 | }else{ |
edamame22 | 0:29983394c6b6 | 138 | if(_register_security->set_resource_value(M2MSecurity::M2MServerUri, address) && |
edamame22 | 0:29983394c6b6 | 139 | _register_security->set_resource_value(M2MSecurity::SecurityMode, M2MSecurity::Certificate) && |
edamame22 | 0:29983394c6b6 | 140 | _register_security->set_resource_value(M2MSecurity::ServerPublicKey,server_cert,server_cert_len) && |
edamame22 | 0:29983394c6b6 | 141 | _register_security->set_resource_value(M2MSecurity::PublicKey,cert,cert_len) && |
edamame22 | 0:29983394c6b6 | 142 | _register_security->set_resource_value(M2MSecurity::Secretkey,key,key_len) ){ |
edamame22 | 0:29983394c6b6 | 143 | success = true; |
edamame22 | 0:29983394c6b6 | 144 | } |
edamame22 | 0:29983394c6b6 | 145 | } |
edamame22 | 0:29983394c6b6 | 146 | } |
edamame22 | 0:29983394c6b6 | 147 | return success; |
edamame22 | 0:29983394c6b6 | 148 | } |
edamame22 | 0:29983394c6b6 | 149 | |
edamame22 | 0:29983394c6b6 | 150 | bool M2MLWClient::test_bootstrap() |
edamame22 | 0:29983394c6b6 | 151 | { |
edamame22 | 0:29983394c6b6 | 152 | bool success = false; |
edamame22 | 0:29983394c6b6 | 153 | if(_interface) { |
edamame22 | 0:29983394c6b6 | 154 | _interface->bootstrap(_security); |
edamame22 | 0:29983394c6b6 | 155 | success = true; |
edamame22 | 0:29983394c6b6 | 156 | } |
edamame22 | 0:29983394c6b6 | 157 | return success; |
edamame22 | 0:29983394c6b6 | 158 | } |
edamame22 | 0:29983394c6b6 | 159 | |
edamame22 | 0:29983394c6b6 | 160 | bool M2MLWClient::create_device_object(M2MDevice::DeviceResource resource, |
edamame22 | 0:29983394c6b6 | 161 | const char *value) |
edamame22 | 0:29983394c6b6 | 162 | { |
edamame22 | 0:29983394c6b6 | 163 | bool success = false; |
edamame22 | 0:29983394c6b6 | 164 | String value_string; |
edamame22 | 0:29983394c6b6 | 165 | if(value) { |
edamame22 | 0:29983394c6b6 | 166 | value_string += value; |
edamame22 | 0:29983394c6b6 | 167 | } |
edamame22 | 0:29983394c6b6 | 168 | if(!_device) { |
edamame22 | 0:29983394c6b6 | 169 | _device = M2MInterfaceFactory::create_device(); |
edamame22 | 0:29983394c6b6 | 170 | } |
edamame22 | 0:29983394c6b6 | 171 | if(_device) { |
edamame22 | 0:29983394c6b6 | 172 | |
edamame22 | 0:29983394c6b6 | 173 | if(_device->create_resource(resource,value_string)){ |
edamame22 | 0:29983394c6b6 | 174 | success = true; |
edamame22 | 0:29983394c6b6 | 175 | } else { |
edamame22 | 0:29983394c6b6 | 176 | success = _device->set_resource_value(resource,value); |
edamame22 | 0:29983394c6b6 | 177 | } |
edamame22 | 0:29983394c6b6 | 178 | } |
edamame22 | 0:29983394c6b6 | 179 | return success; |
edamame22 | 0:29983394c6b6 | 180 | } |
edamame22 | 0:29983394c6b6 | 181 | |
edamame22 | 0:29983394c6b6 | 182 | bool M2MLWClient::create_device_object() |
edamame22 | 0:29983394c6b6 | 183 | { |
edamame22 | 0:29983394c6b6 | 184 | bool success = false; |
edamame22 | 0:29983394c6b6 | 185 | if(!_device) { |
edamame22 | 0:29983394c6b6 | 186 | _device = M2MInterfaceFactory::create_device(); |
edamame22 | 0:29983394c6b6 | 187 | success = true; |
edamame22 | 0:29983394c6b6 | 188 | } |
edamame22 | 0:29983394c6b6 | 189 | return success; |
edamame22 | 0:29983394c6b6 | 190 | } |
edamame22 | 0:29983394c6b6 | 191 | |
edamame22 | 0:29983394c6b6 | 192 | bool M2MLWClient::create_device_object(M2MDevice::DeviceResource resource, |
edamame22 | 0:29983394c6b6 | 193 | int64_t value) |
edamame22 | 0:29983394c6b6 | 194 | { |
edamame22 | 0:29983394c6b6 | 195 | bool success = false; |
edamame22 | 0:29983394c6b6 | 196 | if(!_device) { |
edamame22 | 0:29983394c6b6 | 197 | _device = M2MInterfaceFactory::create_device(); |
edamame22 | 0:29983394c6b6 | 198 | } |
edamame22 | 0:29983394c6b6 | 199 | if(_device) { |
edamame22 | 0:29983394c6b6 | 200 | if(_device->create_resource(resource,value)) { |
edamame22 | 0:29983394c6b6 | 201 | success = true; |
edamame22 | 0:29983394c6b6 | 202 | } else { |
edamame22 | 0:29983394c6b6 | 203 | success = _device->set_resource_value(resource, value); |
edamame22 | 0:29983394c6b6 | 204 | } |
edamame22 | 0:29983394c6b6 | 205 | } |
edamame22 | 0:29983394c6b6 | 206 | return success; |
edamame22 | 0:29983394c6b6 | 207 | } |
edamame22 | 0:29983394c6b6 | 208 | |
edamame22 | 0:29983394c6b6 | 209 | bool M2MLWClient::create_device_object(M2MDevice::DeviceResource resource, |
edamame22 | 0:29983394c6b6 | 210 | int64_t value, |
edamame22 | 0:29983394c6b6 | 211 | uint16_t instance_id) |
edamame22 | 0:29983394c6b6 | 212 | { |
edamame22 | 0:29983394c6b6 | 213 | bool success = false; |
edamame22 | 0:29983394c6b6 | 214 | if(!_device) { |
edamame22 | 0:29983394c6b6 | 215 | _device = M2MInterfaceFactory::create_device(); |
edamame22 | 0:29983394c6b6 | 216 | } |
edamame22 | 0:29983394c6b6 | 217 | if(_device) { |
edamame22 | 0:29983394c6b6 | 218 | if(_device->create_resource_instance(resource,value,instance_id)) { |
edamame22 | 0:29983394c6b6 | 219 | success = true; |
edamame22 | 0:29983394c6b6 | 220 | } else { |
edamame22 | 0:29983394c6b6 | 221 | success = _device->set_resource_value(resource, |
edamame22 | 0:29983394c6b6 | 222 | value, |
edamame22 | 0:29983394c6b6 | 223 | instance_id); |
edamame22 | 0:29983394c6b6 | 224 | } |
edamame22 | 0:29983394c6b6 | 225 | } |
edamame22 | 0:29983394c6b6 | 226 | return success; |
edamame22 | 0:29983394c6b6 | 227 | } |
edamame22 | 0:29983394c6b6 | 228 | |
edamame22 | 0:29983394c6b6 | 229 | bool M2MLWClient::create_firmware_object(M2MFirmware::FirmwareResource resource, |
edamame22 | 0:29983394c6b6 | 230 | const char *value) |
edamame22 | 0:29983394c6b6 | 231 | { |
edamame22 | 0:29983394c6b6 | 232 | bool success = false; |
edamame22 | 0:29983394c6b6 | 233 | String value_string; |
edamame22 | 0:29983394c6b6 | 234 | if(value) { |
edamame22 | 0:29983394c6b6 | 235 | value_string += value; |
edamame22 | 0:29983394c6b6 | 236 | } |
edamame22 | 0:29983394c6b6 | 237 | if(!_firmware) { |
edamame22 | 0:29983394c6b6 | 238 | _firmware = M2MInterfaceFactory::create_firmware(); |
edamame22 | 0:29983394c6b6 | 239 | } |
edamame22 | 0:29983394c6b6 | 240 | if(_firmware) { |
edamame22 | 0:29983394c6b6 | 241 | if(_firmware->create_resource(resource,value_string)){ |
edamame22 | 0:29983394c6b6 | 242 | success = true; |
edamame22 | 0:29983394c6b6 | 243 | } else { |
edamame22 | 0:29983394c6b6 | 244 | success = _firmware->set_resource_value(resource,value); |
edamame22 | 0:29983394c6b6 | 245 | } |
edamame22 | 0:29983394c6b6 | 246 | } |
edamame22 | 0:29983394c6b6 | 247 | return success; |
edamame22 | 0:29983394c6b6 | 248 | } |
edamame22 | 0:29983394c6b6 | 249 | |
edamame22 | 0:29983394c6b6 | 250 | bool M2MLWClient::create_firmware_object() |
edamame22 | 0:29983394c6b6 | 251 | { |
edamame22 | 0:29983394c6b6 | 252 | bool success = false; |
edamame22 | 0:29983394c6b6 | 253 | if(!_firmware) { |
edamame22 | 0:29983394c6b6 | 254 | _firmware = M2MInterfaceFactory::create_firmware(); |
edamame22 | 0:29983394c6b6 | 255 | success = true; |
edamame22 | 0:29983394c6b6 | 256 | } |
edamame22 | 0:29983394c6b6 | 257 | return success; |
edamame22 | 0:29983394c6b6 | 258 | } |
edamame22 | 0:29983394c6b6 | 259 | |
edamame22 | 0:29983394c6b6 | 260 | bool M2MLWClient::create_firmware_object(M2MFirmware::FirmwareResource resource, |
edamame22 | 0:29983394c6b6 | 261 | int64_t value) |
edamame22 | 0:29983394c6b6 | 262 | { |
edamame22 | 0:29983394c6b6 | 263 | bool success = false; |
edamame22 | 0:29983394c6b6 | 264 | if(!_firmware) { |
edamame22 | 0:29983394c6b6 | 265 | _firmware = M2MInterfaceFactory::create_firmware(); |
edamame22 | 0:29983394c6b6 | 266 | } |
edamame22 | 0:29983394c6b6 | 267 | if(_firmware) { |
edamame22 | 0:29983394c6b6 | 268 | if(_firmware->create_resource(resource,value)) { |
edamame22 | 0:29983394c6b6 | 269 | success = true; |
edamame22 | 0:29983394c6b6 | 270 | } else { |
edamame22 | 0:29983394c6b6 | 271 | success = _firmware->set_resource_value(resource, value); |
edamame22 | 0:29983394c6b6 | 272 | } |
edamame22 | 0:29983394c6b6 | 273 | } |
edamame22 | 0:29983394c6b6 | 274 | return success; |
edamame22 | 0:29983394c6b6 | 275 | } |
edamame22 | 0:29983394c6b6 | 276 | |
edamame22 | 0:29983394c6b6 | 277 | bool M2MLWClient::create_firmware_object(M2MFirmware::FirmwareResource resource, |
edamame22 | 0:29983394c6b6 | 278 | const uint8_t *value, |
edamame22 | 0:29983394c6b6 | 279 | const uint32_t length) |
edamame22 | 0:29983394c6b6 | 280 | { |
edamame22 | 0:29983394c6b6 | 281 | bool success = false; |
edamame22 | 0:29983394c6b6 | 282 | if(!_firmware) { |
edamame22 | 0:29983394c6b6 | 283 | _firmware = M2MInterfaceFactory::create_firmware(); |
edamame22 | 0:29983394c6b6 | 284 | } |
edamame22 | 0:29983394c6b6 | 285 | if(_firmware) { |
edamame22 | 0:29983394c6b6 | 286 | success = _firmware->set_resource_value(resource, value, length); |
edamame22 | 0:29983394c6b6 | 287 | } |
edamame22 | 0:29983394c6b6 | 288 | return success; |
edamame22 | 0:29983394c6b6 | 289 | } |
edamame22 | 0:29983394c6b6 | 290 | |
edamame22 | 0:29983394c6b6 | 291 | void M2MLWClient::set_fw_execute_function() |
edamame22 | 0:29983394c6b6 | 292 | { |
edamame22 | 0:29983394c6b6 | 293 | if(_firmware) { |
edamame22 | 0:29983394c6b6 | 294 | M2MObjectInstance *inst = _firmware->object_instance(0); |
edamame22 | 0:29983394c6b6 | 295 | if(inst) { |
edamame22 | 0:29983394c6b6 | 296 | M2MResource *res = inst->resource("2"); |
edamame22 | 0:29983394c6b6 | 297 | if (res) { |
edamame22 | 0:29983394c6b6 | 298 | res->set_execute_function(execute_callback( |
edamame22 | 0:29983394c6b6 | 299 | this, |
edamame22 | 0:29983394c6b6 | 300 | &M2MLWClient::fw_execute_function)); |
edamame22 | 0:29983394c6b6 | 301 | } |
edamame22 | 0:29983394c6b6 | 302 | } |
edamame22 | 0:29983394c6b6 | 303 | } |
edamame22 | 0:29983394c6b6 | 304 | } |
edamame22 | 0:29983394c6b6 | 305 | |
edamame22 | 0:29983394c6b6 | 306 | bool M2MLWClient::create_object(const char *name, |
edamame22 | 0:29983394c6b6 | 307 | bool new_instance, |
edamame22 | 0:29983394c6b6 | 308 | uint8_t object_operation, |
edamame22 | 0:29983394c6b6 | 309 | uint8_t object_instance_operation, |
edamame22 | 0:29983394c6b6 | 310 | uint16_t object_instance_id, |
edamame22 | 0:29983394c6b6 | 311 | bool object_observable, |
edamame22 | 0:29983394c6b6 | 312 | bool object_instance_observable) |
edamame22 | 0:29983394c6b6 | 313 | { |
edamame22 | 0:29983394c6b6 | 314 | bool success = false; |
edamame22 | 0:29983394c6b6 | 315 | M2MObjectInstance *inst = NULL; |
edamame22 | 0:29983394c6b6 | 316 | if(!_object) { |
edamame22 | 0:29983394c6b6 | 317 | _object = M2MInterfaceFactory::create_object(name); |
edamame22 | 0:29983394c6b6 | 318 | if(_object) { |
edamame22 | 0:29983394c6b6 | 319 | _object->set_operation(int_to_operation(object_operation)); |
edamame22 | 0:29983394c6b6 | 320 | _object->set_observable(object_observable); |
edamame22 | 0:29983394c6b6 | 321 | inst = _object->create_object_instance(object_instance_id); |
edamame22 | 0:29983394c6b6 | 322 | if(inst) { |
edamame22 | 0:29983394c6b6 | 323 | success = true; |
edamame22 | 0:29983394c6b6 | 324 | inst->set_operation(int_to_operation(object_instance_operation)); |
edamame22 | 0:29983394c6b6 | 325 | inst->set_observable(object_instance_observable); |
edamame22 | 0:29983394c6b6 | 326 | } |
edamame22 | 0:29983394c6b6 | 327 | } |
edamame22 | 0:29983394c6b6 | 328 | } else { |
edamame22 | 0:29983394c6b6 | 329 | if(new_instance) { |
edamame22 | 0:29983394c6b6 | 330 | inst = _object->create_object_instance(object_instance_id); |
edamame22 | 0:29983394c6b6 | 331 | if(inst) { |
edamame22 | 0:29983394c6b6 | 332 | success = true; |
edamame22 | 0:29983394c6b6 | 333 | inst->set_operation(int_to_operation(object_instance_operation)); |
edamame22 | 0:29983394c6b6 | 334 | inst->set_observable(object_instance_observable); |
edamame22 | 0:29983394c6b6 | 335 | } |
edamame22 | 0:29983394c6b6 | 336 | } |
edamame22 | 0:29983394c6b6 | 337 | } |
edamame22 | 0:29983394c6b6 | 338 | return success; |
edamame22 | 0:29983394c6b6 | 339 | } |
edamame22 | 0:29983394c6b6 | 340 | |
edamame22 | 0:29983394c6b6 | 341 | bool M2MLWClient::create_static_resource_string(const char *name, |
edamame22 | 0:29983394c6b6 | 342 | const char *value, |
edamame22 | 0:29983394c6b6 | 343 | bool multiple_instance, |
edamame22 | 0:29983394c6b6 | 344 | uint16_t object_instance) |
edamame22 | 0:29983394c6b6 | 345 | { |
edamame22 | 0:29983394c6b6 | 346 | bool success = false; |
edamame22 | 0:29983394c6b6 | 347 | String name_string; |
edamame22 | 0:29983394c6b6 | 348 | if(name) { |
edamame22 | 0:29983394c6b6 | 349 | name_string += name; |
edamame22 | 0:29983394c6b6 | 350 | } |
edamame22 | 0:29983394c6b6 | 351 | String value_string; |
edamame22 | 0:29983394c6b6 | 352 | if(value) { |
edamame22 | 0:29983394c6b6 | 353 | value_string += value; |
edamame22 | 0:29983394c6b6 | 354 | } |
edamame22 | 0:29983394c6b6 | 355 | if(_object) { |
edamame22 | 0:29983394c6b6 | 356 | M2MObjectInstance *inst = _object->object_instance(object_instance); |
edamame22 | 0:29983394c6b6 | 357 | if(inst) { |
edamame22 | 0:29983394c6b6 | 358 | if(inst->create_static_resource(name,"resource", |
edamame22 | 0:29983394c6b6 | 359 | M2MResourceInstance::STRING, |
edamame22 | 0:29983394c6b6 | 360 | (const uint8_t*)value_string.c_str(), |
edamame22 | 0:29983394c6b6 | 361 | value_string.size()) != NULL) { |
edamame22 | 0:29983394c6b6 | 362 | success = true; |
edamame22 | 0:29983394c6b6 | 363 | } |
edamame22 | 0:29983394c6b6 | 364 | } |
edamame22 | 0:29983394c6b6 | 365 | } |
edamame22 | 0:29983394c6b6 | 366 | return success; |
edamame22 | 0:29983394c6b6 | 367 | } |
edamame22 | 0:29983394c6b6 | 368 | |
edamame22 | 0:29983394c6b6 | 369 | bool M2MLWClient::create_static_resource_int(const char *name, |
edamame22 | 0:29983394c6b6 | 370 | int64_t value, |
edamame22 | 0:29983394c6b6 | 371 | bool multiple_instance, |
edamame22 | 0:29983394c6b6 | 372 | uint16_t object_instance) |
edamame22 | 0:29983394c6b6 | 373 | { |
edamame22 | 0:29983394c6b6 | 374 | bool success = false; |
edamame22 | 0:29983394c6b6 | 375 | String name_string; |
edamame22 | 0:29983394c6b6 | 376 | String value_string; |
edamame22 | 0:29983394c6b6 | 377 | |
edamame22 | 0:29983394c6b6 | 378 | if(name) { |
edamame22 | 0:29983394c6b6 | 379 | name_string += name; |
edamame22 | 0:29983394c6b6 | 380 | } |
edamame22 | 0:29983394c6b6 | 381 | |
edamame22 | 0:29983394c6b6 | 382 | char value_buffer[20]; |
edamame22 | 0:29983394c6b6 | 383 | sprintf(value_buffer,"%ld",value); |
edamame22 | 0:29983394c6b6 | 384 | value_string += value_buffer; |
edamame22 | 0:29983394c6b6 | 385 | |
edamame22 | 0:29983394c6b6 | 386 | if(_object) { |
edamame22 | 0:29983394c6b6 | 387 | M2MObjectInstance *inst = _object->object_instance(object_instance); |
edamame22 | 0:29983394c6b6 | 388 | if(inst) { |
edamame22 | 0:29983394c6b6 | 389 | if(inst->create_static_resource(name,"resource", |
edamame22 | 0:29983394c6b6 | 390 | M2MResourceInstance::INTEGER, |
edamame22 | 0:29983394c6b6 | 391 | (const uint8_t*)value_string.c_str(), |
edamame22 | 0:29983394c6b6 | 392 | value_string.size()) != NULL) { |
edamame22 | 0:29983394c6b6 | 393 | success = true; |
edamame22 | 0:29983394c6b6 | 394 | } |
edamame22 | 0:29983394c6b6 | 395 | } |
edamame22 | 0:29983394c6b6 | 396 | } |
edamame22 | 0:29983394c6b6 | 397 | return success; |
edamame22 | 0:29983394c6b6 | 398 | } |
edamame22 | 0:29983394c6b6 | 399 | |
edamame22 | 0:29983394c6b6 | 400 | bool M2MLWClient::create_dynamic_resource_string(const char *name, |
edamame22 | 0:29983394c6b6 | 401 | bool observable, |
edamame22 | 0:29983394c6b6 | 402 | bool multiple_instance, |
edamame22 | 0:29983394c6b6 | 403 | uint16_t object_instance, |
edamame22 | 0:29983394c6b6 | 404 | uint8_t resource_operation) |
edamame22 | 0:29983394c6b6 | 405 | { |
edamame22 | 0:29983394c6b6 | 406 | bool success = false; |
edamame22 | 0:29983394c6b6 | 407 | String name_string; |
edamame22 | 0:29983394c6b6 | 408 | if(name) { |
edamame22 | 0:29983394c6b6 | 409 | name_string += name; |
edamame22 | 0:29983394c6b6 | 410 | } |
edamame22 | 0:29983394c6b6 | 411 | if(_object) { |
edamame22 | 0:29983394c6b6 | 412 | M2MObjectInstance *inst = _object->object_instance(object_instance); |
edamame22 | 0:29983394c6b6 | 413 | if(inst) { |
edamame22 | 0:29983394c6b6 | 414 | M2MResource *res = inst->create_dynamic_resource(name,"resource", |
edamame22 | 0:29983394c6b6 | 415 | M2MResourceInstance::STRING, |
edamame22 | 0:29983394c6b6 | 416 | observable, multiple_instance); |
edamame22 | 0:29983394c6b6 | 417 | if(res) { |
edamame22 | 0:29983394c6b6 | 418 | success = true; |
edamame22 | 0:29983394c6b6 | 419 | res->set_operation(int_to_operation(resource_operation)); |
edamame22 | 0:29983394c6b6 | 420 | } |
edamame22 | 0:29983394c6b6 | 421 | } |
edamame22 | 0:29983394c6b6 | 422 | } |
edamame22 | 0:29983394c6b6 | 423 | return success; |
edamame22 | 0:29983394c6b6 | 424 | } |
edamame22 | 0:29983394c6b6 | 425 | |
edamame22 | 0:29983394c6b6 | 426 | bool M2MLWClient::create_dynamic_resource_int(const char *name, |
edamame22 | 0:29983394c6b6 | 427 | bool observable, |
edamame22 | 0:29983394c6b6 | 428 | bool multiple_instance, |
edamame22 | 0:29983394c6b6 | 429 | uint16_t object_instance, |
edamame22 | 0:29983394c6b6 | 430 | uint8_t resource_operation) |
edamame22 | 0:29983394c6b6 | 431 | { |
edamame22 | 0:29983394c6b6 | 432 | bool success = false; |
edamame22 | 0:29983394c6b6 | 433 | String name_string; |
edamame22 | 0:29983394c6b6 | 434 | if(name) { |
edamame22 | 0:29983394c6b6 | 435 | name_string += name; |
edamame22 | 0:29983394c6b6 | 436 | } |
edamame22 | 0:29983394c6b6 | 437 | if(_object) { |
edamame22 | 0:29983394c6b6 | 438 | M2MObjectInstance *inst = _object->object_instance(object_instance); |
edamame22 | 0:29983394c6b6 | 439 | if(inst) { |
edamame22 | 0:29983394c6b6 | 440 | M2MResource *res = inst->create_dynamic_resource(name,"resource", |
edamame22 | 0:29983394c6b6 | 441 | M2MResourceInstance::INTEGER, |
edamame22 | 0:29983394c6b6 | 442 | observable, multiple_instance); |
edamame22 | 0:29983394c6b6 | 443 | if(res) { |
edamame22 | 0:29983394c6b6 | 444 | success = true; |
edamame22 | 0:29983394c6b6 | 445 | res->set_operation(int_to_operation(resource_operation)); |
edamame22 | 0:29983394c6b6 | 446 | } |
edamame22 | 0:29983394c6b6 | 447 | } |
edamame22 | 0:29983394c6b6 | 448 | } |
edamame22 | 0:29983394c6b6 | 449 | return success; |
edamame22 | 0:29983394c6b6 | 450 | } |
edamame22 | 0:29983394c6b6 | 451 | |
edamame22 | 0:29983394c6b6 | 452 | bool M2MLWClient::set_resource_value(const char *name, |
edamame22 | 0:29983394c6b6 | 453 | int32_t value, |
edamame22 | 0:29983394c6b6 | 454 | uint16_t object_instance) |
edamame22 | 0:29983394c6b6 | 455 | { |
edamame22 | 0:29983394c6b6 | 456 | bool success = false; |
edamame22 | 0:29983394c6b6 | 457 | String name_string; |
edamame22 | 0:29983394c6b6 | 458 | String value_string; |
edamame22 | 0:29983394c6b6 | 459 | if(name) { |
edamame22 | 0:29983394c6b6 | 460 | name_string += name; |
edamame22 | 0:29983394c6b6 | 461 | } |
edamame22 | 0:29983394c6b6 | 462 | |
edamame22 | 0:29983394c6b6 | 463 | char value_buffer[20]; |
edamame22 | 0:29983394c6b6 | 464 | sprintf(value_buffer,"%d",value); |
edamame22 | 0:29983394c6b6 | 465 | value_string += value_buffer; |
edamame22 | 0:29983394c6b6 | 466 | |
edamame22 | 0:29983394c6b6 | 467 | if(_object && name_string.length() > 0) { |
edamame22 | 0:29983394c6b6 | 468 | M2MObjectInstance *inst = _object->object_instance(object_instance); |
edamame22 | 0:29983394c6b6 | 469 | if(inst) { |
edamame22 | 0:29983394c6b6 | 470 | M2MResource *res = inst->resource(name_string); |
edamame22 | 0:29983394c6b6 | 471 | if (res) { |
edamame22 | 0:29983394c6b6 | 472 | if (res->set_value((const uint8_t*)value_string.c_str(), value_string.size())) { |
edamame22 | 0:29983394c6b6 | 473 | success = true; |
edamame22 | 0:29983394c6b6 | 474 | } |
edamame22 | 0:29983394c6b6 | 475 | } |
edamame22 | 0:29983394c6b6 | 476 | } |
edamame22 | 0:29983394c6b6 | 477 | } |
edamame22 | 0:29983394c6b6 | 478 | return success; |
edamame22 | 0:29983394c6b6 | 479 | } |
edamame22 | 0:29983394c6b6 | 480 | |
edamame22 | 0:29983394c6b6 | 481 | bool M2MLWClient::set_resource_value(const char *name, |
edamame22 | 0:29983394c6b6 | 482 | const char *value, |
edamame22 | 0:29983394c6b6 | 483 | uint16_t object_instance) |
edamame22 | 0:29983394c6b6 | 484 | { |
edamame22 | 0:29983394c6b6 | 485 | bool success = false; |
edamame22 | 0:29983394c6b6 | 486 | String name_string; |
edamame22 | 0:29983394c6b6 | 487 | String value_string; |
edamame22 | 0:29983394c6b6 | 488 | if(name) { |
edamame22 | 0:29983394c6b6 | 489 | name_string += name; |
edamame22 | 0:29983394c6b6 | 490 | } |
edamame22 | 0:29983394c6b6 | 491 | if(value) { |
edamame22 | 0:29983394c6b6 | 492 | value_string += value; |
edamame22 | 0:29983394c6b6 | 493 | } |
edamame22 | 0:29983394c6b6 | 494 | |
edamame22 | 0:29983394c6b6 | 495 | if(_object && name_string.length() > 0) { |
edamame22 | 0:29983394c6b6 | 496 | M2MObjectInstance *inst = _object->object_instance(object_instance); |
edamame22 | 0:29983394c6b6 | 497 | if(inst) { |
edamame22 | 0:29983394c6b6 | 498 | M2MResource *res = inst->resource(name_string); |
edamame22 | 0:29983394c6b6 | 499 | if (res) { |
edamame22 | 0:29983394c6b6 | 500 | if (res->set_value((const uint8_t*)value_string.c_str(), value_string.size())) { |
edamame22 | 0:29983394c6b6 | 501 | success = true; |
edamame22 | 0:29983394c6b6 | 502 | } |
edamame22 | 0:29983394c6b6 | 503 | } |
edamame22 | 0:29983394c6b6 | 504 | } |
edamame22 | 0:29983394c6b6 | 505 | } |
edamame22 | 0:29983394c6b6 | 506 | return success; |
edamame22 | 0:29983394c6b6 | 507 | } |
edamame22 | 0:29983394c6b6 | 508 | |
edamame22 | 0:29983394c6b6 | 509 | bool M2MLWClient::create_static_resource_instance_string(const char *name, |
edamame22 | 0:29983394c6b6 | 510 | const char *value, |
edamame22 | 0:29983394c6b6 | 511 | bool multiple_instance, |
edamame22 | 0:29983394c6b6 | 512 | uint16_t object_instance, |
edamame22 | 0:29983394c6b6 | 513 | uint16_t resource_instance) |
edamame22 | 0:29983394c6b6 | 514 | { |
edamame22 | 0:29983394c6b6 | 515 | bool success = false; |
edamame22 | 0:29983394c6b6 | 516 | String name_string; |
edamame22 | 0:29983394c6b6 | 517 | if(name) { |
edamame22 | 0:29983394c6b6 | 518 | name_string += name; |
edamame22 | 0:29983394c6b6 | 519 | } |
edamame22 | 0:29983394c6b6 | 520 | String value_string; |
edamame22 | 0:29983394c6b6 | 521 | if(value) { |
edamame22 | 0:29983394c6b6 | 522 | value_string += value; |
edamame22 | 0:29983394c6b6 | 523 | } |
edamame22 | 0:29983394c6b6 | 524 | if(_object) { |
edamame22 | 0:29983394c6b6 | 525 | M2MObjectInstance *inst = _object->object_instance(object_instance); |
edamame22 | 0:29983394c6b6 | 526 | if(inst) { |
edamame22 | 0:29983394c6b6 | 527 | if(inst->create_static_resource_instance(name,"resource", |
edamame22 | 0:29983394c6b6 | 528 | M2MResourceInstance::STRING, |
edamame22 | 0:29983394c6b6 | 529 | (const uint8_t*)value_string.c_str(), |
edamame22 | 0:29983394c6b6 | 530 | value_string.size(), |
edamame22 | 0:29983394c6b6 | 531 | resource_instance) != NULL) { |
edamame22 | 0:29983394c6b6 | 532 | success = true; |
edamame22 | 0:29983394c6b6 | 533 | } |
edamame22 | 0:29983394c6b6 | 534 | } |
edamame22 | 0:29983394c6b6 | 535 | } |
edamame22 | 0:29983394c6b6 | 536 | return success; |
edamame22 | 0:29983394c6b6 | 537 | } |
edamame22 | 0:29983394c6b6 | 538 | |
edamame22 | 0:29983394c6b6 | 539 | bool M2MLWClient::create_static_resource_instance_int(const char *name, |
edamame22 | 0:29983394c6b6 | 540 | int32_t value, |
edamame22 | 0:29983394c6b6 | 541 | bool multiple_instance, |
edamame22 | 0:29983394c6b6 | 542 | uint16_t object_instance, |
edamame22 | 0:29983394c6b6 | 543 | uint16_t resource_instance) |
edamame22 | 0:29983394c6b6 | 544 | { |
edamame22 | 0:29983394c6b6 | 545 | bool success = false; |
edamame22 | 0:29983394c6b6 | 546 | String name_string; |
edamame22 | 0:29983394c6b6 | 547 | String value_string; |
edamame22 | 0:29983394c6b6 | 548 | |
edamame22 | 0:29983394c6b6 | 549 | if(name) { |
edamame22 | 0:29983394c6b6 | 550 | name_string += name; |
edamame22 | 0:29983394c6b6 | 551 | } |
edamame22 | 0:29983394c6b6 | 552 | |
edamame22 | 0:29983394c6b6 | 553 | char value_buffer[20]; |
edamame22 | 0:29983394c6b6 | 554 | sprintf(value_buffer,"%ld",value); |
edamame22 | 0:29983394c6b6 | 555 | value_string += value_buffer; |
edamame22 | 0:29983394c6b6 | 556 | |
edamame22 | 0:29983394c6b6 | 557 | if(_object) { |
edamame22 | 0:29983394c6b6 | 558 | M2MObjectInstance *inst = _object->object_instance(object_instance); |
edamame22 | 0:29983394c6b6 | 559 | if(inst) { |
edamame22 | 0:29983394c6b6 | 560 | if(inst->create_static_resource_instance(name,"resource", |
edamame22 | 0:29983394c6b6 | 561 | M2MResourceInstance::INTEGER, |
edamame22 | 0:29983394c6b6 | 562 | (const uint8_t*)value_string.c_str(), |
edamame22 | 0:29983394c6b6 | 563 | value_string.size(), |
edamame22 | 0:29983394c6b6 | 564 | resource_instance) != NULL) { |
edamame22 | 0:29983394c6b6 | 565 | success = true; |
edamame22 | 0:29983394c6b6 | 566 | } |
edamame22 | 0:29983394c6b6 | 567 | } |
edamame22 | 0:29983394c6b6 | 568 | } |
edamame22 | 0:29983394c6b6 | 569 | return success; |
edamame22 | 0:29983394c6b6 | 570 | } |
edamame22 | 0:29983394c6b6 | 571 | |
edamame22 | 0:29983394c6b6 | 572 | bool M2MLWClient::create_dynamic_resource_instance_int(const char *name, |
edamame22 | 0:29983394c6b6 | 573 | bool observable, |
edamame22 | 0:29983394c6b6 | 574 | bool multiple_instance, |
edamame22 | 0:29983394c6b6 | 575 | uint16_t object_instance, |
edamame22 | 0:29983394c6b6 | 576 | uint16_t resource_instance, |
edamame22 | 0:29983394c6b6 | 577 | uint8_t resource_instance_operation) |
edamame22 | 0:29983394c6b6 | 578 | { |
edamame22 | 0:29983394c6b6 | 579 | bool success = false; |
edamame22 | 0:29983394c6b6 | 580 | String name_string; |
edamame22 | 0:29983394c6b6 | 581 | if(name) { |
edamame22 | 0:29983394c6b6 | 582 | name_string += name; |
edamame22 | 0:29983394c6b6 | 583 | } |
edamame22 | 0:29983394c6b6 | 584 | if(_object) { |
edamame22 | 0:29983394c6b6 | 585 | M2MObjectInstance *inst = _object->object_instance(object_instance); |
edamame22 | 0:29983394c6b6 | 586 | if(inst) { |
edamame22 | 0:29983394c6b6 | 587 | M2MResourceInstance *res = inst->create_dynamic_resource_instance(name,"resource", |
edamame22 | 0:29983394c6b6 | 588 | M2MResourceInstance::INTEGER, |
edamame22 | 0:29983394c6b6 | 589 | observable, |
edamame22 | 0:29983394c6b6 | 590 | resource_instance); |
edamame22 | 0:29983394c6b6 | 591 | if( res) { |
edamame22 | 0:29983394c6b6 | 592 | success = true; |
edamame22 | 0:29983394c6b6 | 593 | res->set_operation(int_to_operation(resource_instance_operation)); |
edamame22 | 0:29983394c6b6 | 594 | } |
edamame22 | 0:29983394c6b6 | 595 | } |
edamame22 | 0:29983394c6b6 | 596 | } |
edamame22 | 0:29983394c6b6 | 597 | return success; |
edamame22 | 0:29983394c6b6 | 598 | } |
edamame22 | 0:29983394c6b6 | 599 | |
edamame22 | 0:29983394c6b6 | 600 | bool M2MLWClient::create_dynamic_resource_instance_string(const char *name, |
edamame22 | 0:29983394c6b6 | 601 | bool observable, |
edamame22 | 0:29983394c6b6 | 602 | bool multiple_instance, |
edamame22 | 0:29983394c6b6 | 603 | uint16_t object_instance, |
edamame22 | 0:29983394c6b6 | 604 | uint16_t resource_instance, |
edamame22 | 0:29983394c6b6 | 605 | uint8_t resource_instance_operation) |
edamame22 | 0:29983394c6b6 | 606 | { |
edamame22 | 0:29983394c6b6 | 607 | bool success = false; |
edamame22 | 0:29983394c6b6 | 608 | String name_string; |
edamame22 | 0:29983394c6b6 | 609 | if(name) { |
edamame22 | 0:29983394c6b6 | 610 | name_string += name; |
edamame22 | 0:29983394c6b6 | 611 | } |
edamame22 | 0:29983394c6b6 | 612 | if(_object) { |
edamame22 | 0:29983394c6b6 | 613 | M2MObjectInstance *inst = _object->object_instance(object_instance); |
edamame22 | 0:29983394c6b6 | 614 | if(inst) { |
edamame22 | 0:29983394c6b6 | 615 | M2MResourceInstance *res = inst->create_dynamic_resource_instance(name,"resource", |
edamame22 | 0:29983394c6b6 | 616 | M2MResourceInstance::STRING, |
edamame22 | 0:29983394c6b6 | 617 | observable, |
edamame22 | 0:29983394c6b6 | 618 | resource_instance); |
edamame22 | 0:29983394c6b6 | 619 | if( res) { |
edamame22 | 0:29983394c6b6 | 620 | success = true; |
edamame22 | 0:29983394c6b6 | 621 | res->set_operation(int_to_operation(resource_instance_operation)); |
edamame22 | 0:29983394c6b6 | 622 | } |
edamame22 | 0:29983394c6b6 | 623 | } |
edamame22 | 0:29983394c6b6 | 624 | } |
edamame22 | 0:29983394c6b6 | 625 | return success; |
edamame22 | 0:29983394c6b6 | 626 | } |
edamame22 | 0:29983394c6b6 | 627 | |
edamame22 | 0:29983394c6b6 | 628 | bool M2MLWClient::set_resource_instance_value(const char *name, |
edamame22 | 0:29983394c6b6 | 629 | int32_t value, |
edamame22 | 0:29983394c6b6 | 630 | uint16_t object_instance, |
edamame22 | 0:29983394c6b6 | 631 | uint16_t resource_instance) |
edamame22 | 0:29983394c6b6 | 632 | { |
edamame22 | 0:29983394c6b6 | 633 | bool success = false; |
edamame22 | 0:29983394c6b6 | 634 | String name_string; |
edamame22 | 0:29983394c6b6 | 635 | String value_string; |
edamame22 | 0:29983394c6b6 | 636 | if(name) { |
edamame22 | 0:29983394c6b6 | 637 | name_string += name; |
edamame22 | 0:29983394c6b6 | 638 | } |
edamame22 | 0:29983394c6b6 | 639 | |
edamame22 | 0:29983394c6b6 | 640 | char value_buffer[20]; |
edamame22 | 0:29983394c6b6 | 641 | sprintf(value_buffer,"%d",value); |
edamame22 | 0:29983394c6b6 | 642 | value_string += value_buffer; |
edamame22 | 0:29983394c6b6 | 643 | |
edamame22 | 0:29983394c6b6 | 644 | if(_object && name_string.length() > 0) { |
edamame22 | 0:29983394c6b6 | 645 | M2MObjectInstance *inst = _object->object_instance(object_instance); |
edamame22 | 0:29983394c6b6 | 646 | if(inst) { |
edamame22 | 0:29983394c6b6 | 647 | M2MResource *res = inst->resource(name_string); |
edamame22 | 0:29983394c6b6 | 648 | if (res) { |
edamame22 | 0:29983394c6b6 | 649 | M2MResourceInstance *res_inst = res->resource_instance(resource_instance); |
edamame22 | 0:29983394c6b6 | 650 | if(res_inst) { |
edamame22 | 0:29983394c6b6 | 651 | if (res_inst->set_value((const uint8_t*)value_string.c_str(), value_string.size())) { |
edamame22 | 0:29983394c6b6 | 652 | success = true; |
edamame22 | 0:29983394c6b6 | 653 | } |
edamame22 | 0:29983394c6b6 | 654 | } |
edamame22 | 0:29983394c6b6 | 655 | } |
edamame22 | 0:29983394c6b6 | 656 | } |
edamame22 | 0:29983394c6b6 | 657 | } |
edamame22 | 0:29983394c6b6 | 658 | return success; |
edamame22 | 0:29983394c6b6 | 659 | } |
edamame22 | 0:29983394c6b6 | 660 | |
edamame22 | 0:29983394c6b6 | 661 | bool M2MLWClient::set_resource_instance_value(const char *name, |
edamame22 | 0:29983394c6b6 | 662 | const char *value, |
edamame22 | 0:29983394c6b6 | 663 | uint16_t object_instance, |
edamame22 | 0:29983394c6b6 | 664 | uint16_t resource_instance) |
edamame22 | 0:29983394c6b6 | 665 | { |
edamame22 | 0:29983394c6b6 | 666 | bool success = false; |
edamame22 | 0:29983394c6b6 | 667 | String name_string; |
edamame22 | 0:29983394c6b6 | 668 | String value_string; |
edamame22 | 0:29983394c6b6 | 669 | if(name) { |
edamame22 | 0:29983394c6b6 | 670 | name_string += name; |
edamame22 | 0:29983394c6b6 | 671 | } |
edamame22 | 0:29983394c6b6 | 672 | if(value) { |
edamame22 | 0:29983394c6b6 | 673 | value_string += value; |
edamame22 | 0:29983394c6b6 | 674 | } |
edamame22 | 0:29983394c6b6 | 675 | |
edamame22 | 0:29983394c6b6 | 676 | if(_object && name_string.length() > 0) { |
edamame22 | 0:29983394c6b6 | 677 | M2MObjectInstance *inst = _object->object_instance(object_instance); |
edamame22 | 0:29983394c6b6 | 678 | if(inst) { |
edamame22 | 0:29983394c6b6 | 679 | M2MResource *res = inst->resource(name_string); |
edamame22 | 0:29983394c6b6 | 680 | if (res) { |
edamame22 | 0:29983394c6b6 | 681 | M2MResourceInstance *res_inst = res->resource_instance(resource_instance); |
edamame22 | 0:29983394c6b6 | 682 | if(res_inst) { |
edamame22 | 0:29983394c6b6 | 683 | if (res_inst->set_value((const uint8_t*)value_string.c_str(), value_string.size())) { |
edamame22 | 0:29983394c6b6 | 684 | success = true; |
edamame22 | 0:29983394c6b6 | 685 | } |
edamame22 | 0:29983394c6b6 | 686 | } |
edamame22 | 0:29983394c6b6 | 687 | } |
edamame22 | 0:29983394c6b6 | 688 | } |
edamame22 | 0:29983394c6b6 | 689 | } |
edamame22 | 0:29983394c6b6 | 690 | return success; |
edamame22 | 0:29983394c6b6 | 691 | } |
edamame22 | 0:29983394c6b6 | 692 | |
edamame22 | 0:29983394c6b6 | 693 | bool M2MLWClient::test_register() |
edamame22 | 0:29983394c6b6 | 694 | { |
edamame22 | 0:29983394c6b6 | 695 | bool success = false; |
edamame22 | 0:29983394c6b6 | 696 | M2MObjectList object_list; |
edamame22 | 0:29983394c6b6 | 697 | if(_device) { |
edamame22 | 0:29983394c6b6 | 698 | object_list.push_back(_device); |
edamame22 | 0:29983394c6b6 | 699 | } |
edamame22 | 0:29983394c6b6 | 700 | if(_firmware) { |
edamame22 | 0:29983394c6b6 | 701 | object_list.push_back(_firmware); |
edamame22 | 0:29983394c6b6 | 702 | } |
edamame22 | 0:29983394c6b6 | 703 | if(_object) { |
edamame22 | 0:29983394c6b6 | 704 | object_list.push_back(_object); |
edamame22 | 0:29983394c6b6 | 705 | } |
edamame22 | 0:29983394c6b6 | 706 | if(_interface) { |
edamame22 | 0:29983394c6b6 | 707 | _interface->register_object(_register_security,object_list); |
edamame22 | 0:29983394c6b6 | 708 | success = true; |
edamame22 | 0:29983394c6b6 | 709 | } |
edamame22 | 0:29983394c6b6 | 710 | return success; |
edamame22 | 0:29983394c6b6 | 711 | } |
edamame22 | 0:29983394c6b6 | 712 | |
edamame22 | 0:29983394c6b6 | 713 | bool M2MLWClient::test_update_register(const uint32_t lifetime) |
edamame22 | 0:29983394c6b6 | 714 | { |
edamame22 | 0:29983394c6b6 | 715 | bool success = false; |
edamame22 | 0:29983394c6b6 | 716 | if(_interface && _register_security) { |
edamame22 | 0:29983394c6b6 | 717 | success = true; |
edamame22 | 0:29983394c6b6 | 718 | _interface->update_registration(_register_security,lifetime); |
edamame22 | 0:29983394c6b6 | 719 | } |
edamame22 | 0:29983394c6b6 | 720 | return success; |
edamame22 | 0:29983394c6b6 | 721 | } |
edamame22 | 0:29983394c6b6 | 722 | |
edamame22 | 0:29983394c6b6 | 723 | bool M2MLWClient::test_unregister() |
edamame22 | 0:29983394c6b6 | 724 | { |
edamame22 | 0:29983394c6b6 | 725 | bool success = false; |
edamame22 | 0:29983394c6b6 | 726 | if(_interface) { |
edamame22 | 0:29983394c6b6 | 727 | success = true; |
edamame22 | 0:29983394c6b6 | 728 | _interface->unregister_object(_register_security); |
edamame22 | 0:29983394c6b6 | 729 | } |
edamame22 | 0:29983394c6b6 | 730 | return success; |
edamame22 | 0:29983394c6b6 | 731 | } |
edamame22 | 0:29983394c6b6 | 732 | |
edamame22 | 0:29983394c6b6 | 733 | void M2MLWClient::bootstrap_done(M2MSecurity *server_object) |
edamame22 | 0:29983394c6b6 | 734 | { |
edamame22 | 0:29983394c6b6 | 735 | if(server_object) { |
edamame22 | 0:29983394c6b6 | 736 | _register_security = server_object; |
edamame22 | 0:29983394c6b6 | 737 | _bootstrapped = true; |
edamame22 | 0:29983394c6b6 | 738 | cmd_printf("\nBootstrapped\n"); |
edamame22 | 0:29983394c6b6 | 739 | cmd_ready( CMDLINE_RETCODE_SUCCESS ); |
edamame22 | 0:29983394c6b6 | 740 | } |
edamame22 | 0:29983394c6b6 | 741 | } |
edamame22 | 0:29983394c6b6 | 742 | |
edamame22 | 0:29983394c6b6 | 743 | void M2MLWClient::object_registered(M2MSecurity *security_object, const M2MServer &server_object) |
edamame22 | 0:29983394c6b6 | 744 | { |
edamame22 | 0:29983394c6b6 | 745 | _registered = true; |
edamame22 | 0:29983394c6b6 | 746 | cmd_printf("\nRegistered\n"); |
edamame22 | 0:29983394c6b6 | 747 | cmd_ready( CMDLINE_RETCODE_SUCCESS ); |
edamame22 | 0:29983394c6b6 | 748 | } |
edamame22 | 0:29983394c6b6 | 749 | |
edamame22 | 0:29983394c6b6 | 750 | void M2MLWClient::object_unregistered(M2MSecurity *server_object) |
edamame22 | 0:29983394c6b6 | 751 | { |
edamame22 | 0:29983394c6b6 | 752 | _unregistered = true; |
edamame22 | 0:29983394c6b6 | 753 | if(_device) { |
edamame22 | 0:29983394c6b6 | 754 | M2MDevice::delete_instance(); |
edamame22 | 0:29983394c6b6 | 755 | _device = NULL; |
edamame22 | 0:29983394c6b6 | 756 | } |
edamame22 | 0:29983394c6b6 | 757 | if(_object) { |
edamame22 | 0:29983394c6b6 | 758 | delete _object; |
edamame22 | 0:29983394c6b6 | 759 | _object = NULL; |
edamame22 | 0:29983394c6b6 | 760 | } |
edamame22 | 0:29983394c6b6 | 761 | if(_security) { |
edamame22 | 0:29983394c6b6 | 762 | delete _security; |
edamame22 | 0:29983394c6b6 | 763 | _security = NULL; |
edamame22 | 0:29983394c6b6 | 764 | } |
edamame22 | 0:29983394c6b6 | 765 | if(_register_security) { |
edamame22 | 0:29983394c6b6 | 766 | delete _register_security; |
edamame22 | 0:29983394c6b6 | 767 | _register_security = NULL; |
edamame22 | 0:29983394c6b6 | 768 | } |
edamame22 | 0:29983394c6b6 | 769 | cmd_printf("\nUnregistered\n"); |
edamame22 | 0:29983394c6b6 | 770 | cmd_ready( CMDLINE_RETCODE_SUCCESS ); |
edamame22 | 0:29983394c6b6 | 771 | } |
edamame22 | 0:29983394c6b6 | 772 | |
edamame22 | 0:29983394c6b6 | 773 | void M2MLWClient::registration_updated(M2MSecurity *security_object, |
edamame22 | 0:29983394c6b6 | 774 | const M2MServer &server_object) |
edamame22 | 0:29983394c6b6 | 775 | { |
edamame22 | 0:29983394c6b6 | 776 | _registration_updated = true; |
edamame22 | 0:29983394c6b6 | 777 | cmd_printf("\nregistration updated\n"); |
edamame22 | 0:29983394c6b6 | 778 | cmd_ready( CMDLINE_RETCODE_SUCCESS ); |
edamame22 | 0:29983394c6b6 | 779 | } |
edamame22 | 0:29983394c6b6 | 780 | |
edamame22 | 0:29983394c6b6 | 781 | void M2MLWClient::error(M2MInterface::Error error) |
edamame22 | 0:29983394c6b6 | 782 | { |
edamame22 | 0:29983394c6b6 | 783 | _error = true; |
edamame22 | 0:29983394c6b6 | 784 | cmd_printf("\nError occured Error Code : %d\n", (int8_t)error); |
edamame22 | 0:29983394c6b6 | 785 | cmd_ready( CMDLINE_RETCODE_SUCCESS ); |
edamame22 | 0:29983394c6b6 | 786 | } |
edamame22 | 0:29983394c6b6 | 787 | |
edamame22 | 0:29983394c6b6 | 788 | void M2MLWClient::value_updated(M2MBase *base, M2MBase::BaseType type) |
edamame22 | 0:29983394c6b6 | 789 | { |
edamame22 | 0:29983394c6b6 | 790 | cmd_printf("\nValue updated of Object name %s and Type \n", |
edamame22 | 0:29983394c6b6 | 791 | base->name().c_str(), type); |
edamame22 | 0:29983394c6b6 | 792 | } |
edamame22 | 0:29983394c6b6 | 793 | |
edamame22 | 0:29983394c6b6 | 794 | M2MBase::Operation M2MLWClient::int_to_operation(uint8_t operation) |
edamame22 | 0:29983394c6b6 | 795 | { |
edamame22 | 0:29983394c6b6 | 796 | M2MBase::Operation op = M2MBase::NOT_ALLOWED; |
edamame22 | 0:29983394c6b6 | 797 | switch(operation) { |
edamame22 | 0:29983394c6b6 | 798 | case 0: |
edamame22 | 0:29983394c6b6 | 799 | op = M2MBase::NOT_ALLOWED; |
edamame22 | 0:29983394c6b6 | 800 | break; |
edamame22 | 0:29983394c6b6 | 801 | case 1: |
edamame22 | 0:29983394c6b6 | 802 | op = M2MBase::GET_ALLOWED; |
edamame22 | 0:29983394c6b6 | 803 | break; |
edamame22 | 0:29983394c6b6 | 804 | case 2: |
edamame22 | 0:29983394c6b6 | 805 | op = M2MBase::PUT_ALLOWED; |
edamame22 | 0:29983394c6b6 | 806 | break; |
edamame22 | 0:29983394c6b6 | 807 | case 3: |
edamame22 | 0:29983394c6b6 | 808 | op = M2MBase::GET_PUT_ALLOWED; |
edamame22 | 0:29983394c6b6 | 809 | break; |
edamame22 | 0:29983394c6b6 | 810 | case 4: |
edamame22 | 0:29983394c6b6 | 811 | op = M2MBase::POST_ALLOWED; |
edamame22 | 0:29983394c6b6 | 812 | break; |
edamame22 | 0:29983394c6b6 | 813 | case 5: |
edamame22 | 0:29983394c6b6 | 814 | op = M2MBase::GET_POST_ALLOWED; |
edamame22 | 0:29983394c6b6 | 815 | break; |
edamame22 | 0:29983394c6b6 | 816 | case 6: |
edamame22 | 0:29983394c6b6 | 817 | op = M2MBase::PUT_POST_ALLOWED; |
edamame22 | 0:29983394c6b6 | 818 | break; |
edamame22 | 0:29983394c6b6 | 819 | case 7: |
edamame22 | 0:29983394c6b6 | 820 | op = M2MBase::GET_PUT_POST_ALLOWED; |
edamame22 | 0:29983394c6b6 | 821 | break; |
edamame22 | 0:29983394c6b6 | 822 | case 8: |
edamame22 | 0:29983394c6b6 | 823 | op = M2MBase::DELETE_ALLOWED; |
edamame22 | 0:29983394c6b6 | 824 | break; |
edamame22 | 0:29983394c6b6 | 825 | default: |
edamame22 | 0:29983394c6b6 | 826 | break; |
edamame22 | 0:29983394c6b6 | 827 | } |
edamame22 | 0:29983394c6b6 | 828 | return op; |
edamame22 | 0:29983394c6b6 | 829 | } |
edamame22 | 0:29983394c6b6 | 830 | |
edamame22 | 0:29983394c6b6 | 831 | void M2MLWClient::fw_execute_function(void *argument) |
edamame22 | 0:29983394c6b6 | 832 | { |
edamame22 | 0:29983394c6b6 | 833 | if(argument) { |
edamame22 | 0:29983394c6b6 | 834 | char* arguments = (char*)argument; |
edamame22 | 0:29983394c6b6 | 835 | cmd_printf("Received %s!!\n", arguments); |
edamame22 | 0:29983394c6b6 | 836 | } |
edamame22 | 0:29983394c6b6 | 837 | cmd_printf("Firmware update executed\n"); |
edamame22 | 0:29983394c6b6 | 838 | } |
edamame22 | 0:29983394c6b6 | 839 | |
edamame22 | 0:29983394c6b6 | 840 | void M2MLWClient::firmware_resource_int(int resource) |
edamame22 | 0:29983394c6b6 | 841 | { |
edamame22 | 0:29983394c6b6 | 842 | cmd_printf("Firmware resource value int\n"); |
edamame22 | 0:29983394c6b6 | 843 | cmd_printf("%ld\n", _firmware->resource_value_int(static_cast<M2MFirmware::FirmwareResource>(resource))); |
edamame22 | 0:29983394c6b6 | 844 | } |
edamame22 | 0:29983394c6b6 | 845 | |
edamame22 | 0:29983394c6b6 | 846 | void M2MLWClient::firmware_resource_string(int resource) |
edamame22 | 0:29983394c6b6 | 847 | { |
edamame22 | 0:29983394c6b6 | 848 | cmd_printf("Firmware resource value string\n"); |
edamame22 | 0:29983394c6b6 | 849 | cmd_printf("%s\n", _firmware->resource_value_string(static_cast<M2MFirmware::FirmwareResource>(resource)).c_str()); |
edamame22 | 0:29983394c6b6 | 850 | } |
edamame22 | 0:29983394c6b6 | 851 | |
edamame22 | 0:29983394c6b6 | 852 | void M2MLWClient::firmware_resource_buffer() |
edamame22 | 0:29983394c6b6 | 853 | { |
edamame22 | 0:29983394c6b6 | 854 | cmd_printf("Firmware resource value buffer\n"); |
edamame22 | 0:29983394c6b6 | 855 | uint8_t *value = 0; |
edamame22 | 0:29983394c6b6 | 856 | uint32_t valueSize = _firmware->resource_value_buffer(M2MFirmware::Package, value); |
edamame22 | 0:29983394c6b6 | 857 | cmd_printf("%s\n", value); |
edamame22 | 0:29983394c6b6 | 858 | free(value); |
edamame22 | 0:29983394c6b6 | 859 | } |