Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
simple-mbed-cloud-client/mbed-cloud-client/source/SimpleM2MResource.cpp@0:8f0bb79ddd48, 2021-05-04 (annotated)
- Committer:
- leothedragon
- Date:
- Tue May 04 08:55:12 2021 +0000
- Revision:
- 0:8f0bb79ddd48
nmn
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
leothedragon | 0:8f0bb79ddd48 | 1 | // ---------------------------------------------------------------------------- |
leothedragon | 0:8f0bb79ddd48 | 2 | // Copyright 2016-2017 ARM Ltd. |
leothedragon | 0:8f0bb79ddd48 | 3 | // |
leothedragon | 0:8f0bb79ddd48 | 4 | // SPDX-License-Identifier: Apache-2.0 |
leothedragon | 0:8f0bb79ddd48 | 5 | // |
leothedragon | 0:8f0bb79ddd48 | 6 | // Licensed under the Apache License, Version 2.0 (the "License"); |
leothedragon | 0:8f0bb79ddd48 | 7 | // you may not use this file except in compliance with the License. |
leothedragon | 0:8f0bb79ddd48 | 8 | // You may obtain a copy of the License at |
leothedragon | 0:8f0bb79ddd48 | 9 | // |
leothedragon | 0:8f0bb79ddd48 | 10 | // http://www.apache.org/licenses/LICENSE-2.0 |
leothedragon | 0:8f0bb79ddd48 | 11 | // |
leothedragon | 0:8f0bb79ddd48 | 12 | // Unless required by applicable law or agreed to in writing, software |
leothedragon | 0:8f0bb79ddd48 | 13 | // distributed under the License is distributed on an "AS IS" BASIS, |
leothedragon | 0:8f0bb79ddd48 | 14 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
leothedragon | 0:8f0bb79ddd48 | 15 | // See the License for the specific language governing permissions and |
leothedragon | 0:8f0bb79ddd48 | 16 | // limitations under the License. |
leothedragon | 0:8f0bb79ddd48 | 17 | // ---------------------------------------------------------------------------- |
leothedragon | 0:8f0bb79ddd48 | 18 | |
leothedragon | 0:8f0bb79ddd48 | 19 | #include "mbed-cloud-client/SimpleM2MResource.h" |
leothedragon | 0:8f0bb79ddd48 | 20 | |
leothedragon | 0:8f0bb79ddd48 | 21 | #if MBED_CLOUD_CLIENT_STL_API |
leothedragon | 0:8f0bb79ddd48 | 22 | |
leothedragon | 0:8f0bb79ddd48 | 23 | #include "mbed-trace/mbed_trace.h" |
leothedragon | 0:8f0bb79ddd48 | 24 | |
leothedragon | 0:8f0bb79ddd48 | 25 | #include <ctype.h> |
leothedragon | 0:8f0bb79ddd48 | 26 | #include <stdio.h> |
leothedragon | 0:8f0bb79ddd48 | 27 | |
leothedragon | 0:8f0bb79ddd48 | 28 | #define TRACE_GROUP "mClt" |
leothedragon | 0:8f0bb79ddd48 | 29 | |
leothedragon | 0:8f0bb79ddd48 | 30 | |
leothedragon | 0:8f0bb79ddd48 | 31 | SimpleM2MResourceBase::SimpleM2MResourceBase() |
leothedragon | 0:8f0bb79ddd48 | 32 | : _client(NULL), _route("") |
leothedragon | 0:8f0bb79ddd48 | 33 | { |
leothedragon | 0:8f0bb79ddd48 | 34 | tr_debug("SimpleM2MResourceBase::SimpleM2MResourceBase()"); |
leothedragon | 0:8f0bb79ddd48 | 35 | } |
leothedragon | 0:8f0bb79ddd48 | 36 | |
leothedragon | 0:8f0bb79ddd48 | 37 | SimpleM2MResourceBase::SimpleM2MResourceBase(MbedCloudClient* client, string route) |
leothedragon | 0:8f0bb79ddd48 | 38 | : _client(client),_route(route) |
leothedragon | 0:8f0bb79ddd48 | 39 | { |
leothedragon | 0:8f0bb79ddd48 | 40 | tr_debug("SimpleM2MResourceBase::SimpleM2MResourceBase(), resource name %s\r\n", _route.c_str()); |
leothedragon | 0:8f0bb79ddd48 | 41 | } |
leothedragon | 0:8f0bb79ddd48 | 42 | |
leothedragon | 0:8f0bb79ddd48 | 43 | SimpleM2MResourceBase::~SimpleM2MResourceBase() |
leothedragon | 0:8f0bb79ddd48 | 44 | { |
leothedragon | 0:8f0bb79ddd48 | 45 | } |
leothedragon | 0:8f0bb79ddd48 | 46 | |
leothedragon | 0:8f0bb79ddd48 | 47 | bool SimpleM2MResourceBase::define_resource_internal(std::string v, M2MBase::Operation opr, bool observable) |
leothedragon | 0:8f0bb79ddd48 | 48 | { |
leothedragon | 0:8f0bb79ddd48 | 49 | tr_debug("SimpleM2MResourceBase::define_resource_internal(), resource name %s!\r\n", _route.c_str()); |
leothedragon | 0:8f0bb79ddd48 | 50 | |
leothedragon | 0:8f0bb79ddd48 | 51 | vector<string> segments = parse_route(_route.c_str()); |
leothedragon | 0:8f0bb79ddd48 | 52 | if (segments.size() != 3) { |
leothedragon | 0:8f0bb79ddd48 | 53 | tr_debug("[SimpleM2MResourceBase] [ERROR] define_resource_internal(), Route needs to have three segments, split by '/' (%s)\r\n", _route.c_str()); |
leothedragon | 0:8f0bb79ddd48 | 54 | return false; |
leothedragon | 0:8f0bb79ddd48 | 55 | } |
leothedragon | 0:8f0bb79ddd48 | 56 | |
leothedragon | 0:8f0bb79ddd48 | 57 | // segments[1] should be one digit and numeric |
leothedragon | 0:8f0bb79ddd48 | 58 | if (!isdigit(segments.at(1).c_str()[0])) { |
leothedragon | 0:8f0bb79ddd48 | 59 | tr_debug("[SimpleM2MResourceBase] [ERROR] define_resource_internal(), second route segment should be numeric, but was not (%s)\r\n", _route.c_str()); |
leothedragon | 0:8f0bb79ddd48 | 60 | return false; |
leothedragon | 0:8f0bb79ddd48 | 61 | } |
leothedragon | 0:8f0bb79ddd48 | 62 | |
leothedragon | 0:8f0bb79ddd48 | 63 | int inst_id = atoi(segments.at(1).c_str()); |
leothedragon | 0:8f0bb79ddd48 | 64 | |
leothedragon | 0:8f0bb79ddd48 | 65 | // Check if object exists |
leothedragon | 0:8f0bb79ddd48 | 66 | M2MObject* obj; |
leothedragon | 0:8f0bb79ddd48 | 67 | map<string,M2MObject*>::iterator obj_it = _client->_objects.find(segments[0]) ; |
leothedragon | 0:8f0bb79ddd48 | 68 | if(obj_it != _client->_objects.end()) { |
leothedragon | 0:8f0bb79ddd48 | 69 | tr_debug("Found object... %s\r\n", segments.at(0).c_str()); |
leothedragon | 0:8f0bb79ddd48 | 70 | obj = obj_it->second; |
leothedragon | 0:8f0bb79ddd48 | 71 | } else { |
leothedragon | 0:8f0bb79ddd48 | 72 | tr_debug("Create new object... %s\r\n", segments.at(0).c_str()); |
leothedragon | 0:8f0bb79ddd48 | 73 | obj = M2MInterfaceFactory::create_object(segments.at(0).c_str()); |
leothedragon | 0:8f0bb79ddd48 | 74 | if (!obj) { |
leothedragon | 0:8f0bb79ddd48 | 75 | return false; |
leothedragon | 0:8f0bb79ddd48 | 76 | } |
leothedragon | 0:8f0bb79ddd48 | 77 | _client->_objects.insert(std::pair<string, M2MObject*>(segments.at(0), obj)); |
leothedragon | 0:8f0bb79ddd48 | 78 | } |
leothedragon | 0:8f0bb79ddd48 | 79 | |
leothedragon | 0:8f0bb79ddd48 | 80 | // Check if object instance exists |
leothedragon | 0:8f0bb79ddd48 | 81 | M2MObjectInstance* inst = obj->object_instance(inst_id); |
leothedragon | 0:8f0bb79ddd48 | 82 | if(!inst) { |
leothedragon | 0:8f0bb79ddd48 | 83 | tr_debug("Create new object instance... %s\r\n", segments.at(1).c_str()); |
leothedragon | 0:8f0bb79ddd48 | 84 | inst = obj->create_object_instance(inst_id); |
leothedragon | 0:8f0bb79ddd48 | 85 | if(!inst) { |
leothedragon | 0:8f0bb79ddd48 | 86 | return false; |
leothedragon | 0:8f0bb79ddd48 | 87 | } |
leothedragon | 0:8f0bb79ddd48 | 88 | } |
leothedragon | 0:8f0bb79ddd48 | 89 | |
leothedragon | 0:8f0bb79ddd48 | 90 | // @todo check if the resource exists yet |
leothedragon | 0:8f0bb79ddd48 | 91 | M2MResource* res = inst->resource(segments.at(2).c_str()); |
leothedragon | 0:8f0bb79ddd48 | 92 | if(!res) { |
leothedragon | 0:8f0bb79ddd48 | 93 | res = inst->create_dynamic_resource(segments.at(2).c_str(), "", |
leothedragon | 0:8f0bb79ddd48 | 94 | M2MResourceInstance::STRING, observable); |
leothedragon | 0:8f0bb79ddd48 | 95 | if(!res) { |
leothedragon | 0:8f0bb79ddd48 | 96 | return false; |
leothedragon | 0:8f0bb79ddd48 | 97 | } |
leothedragon | 0:8f0bb79ddd48 | 98 | res->set_operation(opr); |
leothedragon | 0:8f0bb79ddd48 | 99 | res->set_value((uint8_t*)v.c_str(), v.length()); |
leothedragon | 0:8f0bb79ddd48 | 100 | |
leothedragon | 0:8f0bb79ddd48 | 101 | _client->_resources.insert(pair<string, M2MResource*>(_route, res)); |
leothedragon | 0:8f0bb79ddd48 | 102 | _client->register_update_callback(_route, this); |
leothedragon | 0:8f0bb79ddd48 | 103 | } |
leothedragon | 0:8f0bb79ddd48 | 104 | |
leothedragon | 0:8f0bb79ddd48 | 105 | return true; |
leothedragon | 0:8f0bb79ddd48 | 106 | } |
leothedragon | 0:8f0bb79ddd48 | 107 | |
leothedragon | 0:8f0bb79ddd48 | 108 | vector<string> SimpleM2MResourceBase::parse_route(const char* route) |
leothedragon | 0:8f0bb79ddd48 | 109 | { |
leothedragon | 0:8f0bb79ddd48 | 110 | string s(route); |
leothedragon | 0:8f0bb79ddd48 | 111 | vector<string> v; |
leothedragon | 0:8f0bb79ddd48 | 112 | std::size_t found = s.find_first_of("/"); |
leothedragon | 0:8f0bb79ddd48 | 113 | |
leothedragon | 0:8f0bb79ddd48 | 114 | while (found!=std::string::npos) { |
leothedragon | 0:8f0bb79ddd48 | 115 | v.push_back(s.substr(0,found)); |
leothedragon | 0:8f0bb79ddd48 | 116 | s = s.substr(found+1); |
leothedragon | 0:8f0bb79ddd48 | 117 | found=s.find_first_of("/"); |
leothedragon | 0:8f0bb79ddd48 | 118 | if(found == std::string::npos) { |
leothedragon | 0:8f0bb79ddd48 | 119 | v.push_back(s); |
leothedragon | 0:8f0bb79ddd48 | 120 | } |
leothedragon | 0:8f0bb79ddd48 | 121 | } |
leothedragon | 0:8f0bb79ddd48 | 122 | return v; |
leothedragon | 0:8f0bb79ddd48 | 123 | } |
leothedragon | 0:8f0bb79ddd48 | 124 | |
leothedragon | 0:8f0bb79ddd48 | 125 | string SimpleM2MResourceBase::get() const |
leothedragon | 0:8f0bb79ddd48 | 126 | { |
leothedragon | 0:8f0bb79ddd48 | 127 | tr_debug("SimpleM2MResourceBase::get() resource (%s)", _route.c_str()); |
leothedragon | 0:8f0bb79ddd48 | 128 | if (!_client->_resources.count(_route)) { |
leothedragon | 0:8f0bb79ddd48 | 129 | tr_debug("[SimpleM2MResourceBase] [ERROR] No such route (%s)\r\n", _route.c_str()); |
leothedragon | 0:8f0bb79ddd48 | 130 | return string(); |
leothedragon | 0:8f0bb79ddd48 | 131 | } |
leothedragon | 0:8f0bb79ddd48 | 132 | |
leothedragon | 0:8f0bb79ddd48 | 133 | // otherwise ask mbed Client... |
leothedragon | 0:8f0bb79ddd48 | 134 | uint8_t* buffIn = NULL; |
leothedragon | 0:8f0bb79ddd48 | 135 | uint32_t sizeIn; |
leothedragon | 0:8f0bb79ddd48 | 136 | _client->_resources[_route]->get_value(buffIn, sizeIn); |
leothedragon | 0:8f0bb79ddd48 | 137 | |
leothedragon | 0:8f0bb79ddd48 | 138 | string s((char*)buffIn, sizeIn); |
leothedragon | 0:8f0bb79ddd48 | 139 | tr_debug("SimpleM2MResourceBase::get() resource value (%s)", s.c_str()); |
leothedragon | 0:8f0bb79ddd48 | 140 | free(buffIn); |
leothedragon | 0:8f0bb79ddd48 | 141 | return s; |
leothedragon | 0:8f0bb79ddd48 | 142 | } |
leothedragon | 0:8f0bb79ddd48 | 143 | |
leothedragon | 0:8f0bb79ddd48 | 144 | bool SimpleM2MResourceBase::set(string v) |
leothedragon | 0:8f0bb79ddd48 | 145 | { |
leothedragon | 0:8f0bb79ddd48 | 146 | // Potentially set() happens in InterruptContext. That's not good. |
leothedragon | 0:8f0bb79ddd48 | 147 | tr_debug("SimpleM2MResourceBase::set() resource (%s)", _route.c_str()); |
leothedragon | 0:8f0bb79ddd48 | 148 | if (!_client->_resources.count(_route)) { |
leothedragon | 0:8f0bb79ddd48 | 149 | tr_debug("[SimpleM2MResourceBase] [ERROR] No such route (%s)\r\n", _route.c_str()); |
leothedragon | 0:8f0bb79ddd48 | 150 | return false; |
leothedragon | 0:8f0bb79ddd48 | 151 | } |
leothedragon | 0:8f0bb79ddd48 | 152 | |
leothedragon | 0:8f0bb79ddd48 | 153 | if (v.length() == 0) { |
leothedragon | 0:8f0bb79ddd48 | 154 | _client->_resources[_route]->clear_value(); |
leothedragon | 0:8f0bb79ddd48 | 155 | } |
leothedragon | 0:8f0bb79ddd48 | 156 | else { |
leothedragon | 0:8f0bb79ddd48 | 157 | _client->_resources[_route]->set_value((uint8_t*)v.c_str(), v.length()); |
leothedragon | 0:8f0bb79ddd48 | 158 | } |
leothedragon | 0:8f0bb79ddd48 | 159 | |
leothedragon | 0:8f0bb79ddd48 | 160 | return true; |
leothedragon | 0:8f0bb79ddd48 | 161 | } |
leothedragon | 0:8f0bb79ddd48 | 162 | |
leothedragon | 0:8f0bb79ddd48 | 163 | bool SimpleM2MResourceBase::set(const int& v) |
leothedragon | 0:8f0bb79ddd48 | 164 | { |
leothedragon | 0:8f0bb79ddd48 | 165 | char buffer[20]; |
leothedragon | 0:8f0bb79ddd48 | 166 | int size = sprintf(buffer,"%d",v); |
leothedragon | 0:8f0bb79ddd48 | 167 | std::string stringified(buffer,size); |
leothedragon | 0:8f0bb79ddd48 | 168 | |
leothedragon | 0:8f0bb79ddd48 | 169 | return set(stringified); |
leothedragon | 0:8f0bb79ddd48 | 170 | } |
leothedragon | 0:8f0bb79ddd48 | 171 | |
leothedragon | 0:8f0bb79ddd48 | 172 | bool SimpleM2MResourceBase::set_post_function(void(*fn)(void*)) |
leothedragon | 0:8f0bb79ddd48 | 173 | { |
leothedragon | 0:8f0bb79ddd48 | 174 | //TODO: Check the resource exists with right operation being set or append the operation into it. |
leothedragon | 0:8f0bb79ddd48 | 175 | M2MResource *resource = get_resource(); |
leothedragon | 0:8f0bb79ddd48 | 176 | if(!resource) { |
leothedragon | 0:8f0bb79ddd48 | 177 | return false; |
leothedragon | 0:8f0bb79ddd48 | 178 | } |
leothedragon | 0:8f0bb79ddd48 | 179 | M2MBase::Operation op = resource->operation(); |
leothedragon | 0:8f0bb79ddd48 | 180 | op = (M2MBase::Operation)(op | M2MBase::POST_ALLOWED); |
leothedragon | 0:8f0bb79ddd48 | 181 | resource->set_operation(op); |
leothedragon | 0:8f0bb79ddd48 | 182 | |
leothedragon | 0:8f0bb79ddd48 | 183 | _client->_resources[_route]->set_execute_function(execute_callback_2(fn)); |
leothedragon | 0:8f0bb79ddd48 | 184 | return true; |
leothedragon | 0:8f0bb79ddd48 | 185 | } |
leothedragon | 0:8f0bb79ddd48 | 186 | |
leothedragon | 0:8f0bb79ddd48 | 187 | bool SimpleM2MResourceBase::set_post_function(execute_callback fn) |
leothedragon | 0:8f0bb79ddd48 | 188 | { |
leothedragon | 0:8f0bb79ddd48 | 189 | //TODO: Check the resource exists with right operation being set or append the operation into it. |
leothedragon | 0:8f0bb79ddd48 | 190 | M2MResource *resource = get_resource(); |
leothedragon | 0:8f0bb79ddd48 | 191 | if(!resource) { |
leothedragon | 0:8f0bb79ddd48 | 192 | return false; |
leothedragon | 0:8f0bb79ddd48 | 193 | } |
leothedragon | 0:8f0bb79ddd48 | 194 | M2MBase::Operation op = resource->operation(); |
leothedragon | 0:8f0bb79ddd48 | 195 | op = (M2MBase::Operation)(op | M2MBase::POST_ALLOWED); |
leothedragon | 0:8f0bb79ddd48 | 196 | resource->set_operation(op); |
leothedragon | 0:8f0bb79ddd48 | 197 | |
leothedragon | 0:8f0bb79ddd48 | 198 | // No clue why this is not working?! It works with class member, but not with static function... |
leothedragon | 0:8f0bb79ddd48 | 199 | _client->_resources[_route]->set_execute_function(fn); |
leothedragon | 0:8f0bb79ddd48 | 200 | return true; |
leothedragon | 0:8f0bb79ddd48 | 201 | } |
leothedragon | 0:8f0bb79ddd48 | 202 | |
leothedragon | 0:8f0bb79ddd48 | 203 | M2MResource* SimpleM2MResourceBase::get_resource() |
leothedragon | 0:8f0bb79ddd48 | 204 | { |
leothedragon | 0:8f0bb79ddd48 | 205 | if (!_client->_resources.count(_route)) { |
leothedragon | 0:8f0bb79ddd48 | 206 | tr_debug("[SimpleM2MResourceBase] [ERROR] No such route (%s)\r\n", _route.c_str()); |
leothedragon | 0:8f0bb79ddd48 | 207 | return NULL; |
leothedragon | 0:8f0bb79ddd48 | 208 | } |
leothedragon | 0:8f0bb79ddd48 | 209 | return _client->_resources[_route]; |
leothedragon | 0:8f0bb79ddd48 | 210 | } |
leothedragon | 0:8f0bb79ddd48 | 211 | |
leothedragon | 0:8f0bb79ddd48 | 212 | SimpleM2MResourceString::SimpleM2MResourceString(MbedCloudClient* client, |
leothedragon | 0:8f0bb79ddd48 | 213 | const char* route, |
leothedragon | 0:8f0bb79ddd48 | 214 | string v, |
leothedragon | 0:8f0bb79ddd48 | 215 | M2MBase::Operation opr, |
leothedragon | 0:8f0bb79ddd48 | 216 | bool observable, |
leothedragon | 0:8f0bb79ddd48 | 217 | FP1<void, string> on_update) |
leothedragon | 0:8f0bb79ddd48 | 218 | : SimpleM2MResourceBase(client,route),_on_update(on_update) |
leothedragon | 0:8f0bb79ddd48 | 219 | { |
leothedragon | 0:8f0bb79ddd48 | 220 | tr_debug("SimpleM2MResourceString::SimpleM2MResourceString() creating (%s)\r\n", route); |
leothedragon | 0:8f0bb79ddd48 | 221 | define_resource_internal(v, opr, observable); |
leothedragon | 0:8f0bb79ddd48 | 222 | } |
leothedragon | 0:8f0bb79ddd48 | 223 | |
leothedragon | 0:8f0bb79ddd48 | 224 | SimpleM2MResourceString::SimpleM2MResourceString(MbedCloudClient* client, |
leothedragon | 0:8f0bb79ddd48 | 225 | const char* route, |
leothedragon | 0:8f0bb79ddd48 | 226 | string v, |
leothedragon | 0:8f0bb79ddd48 | 227 | M2MBase::Operation opr, |
leothedragon | 0:8f0bb79ddd48 | 228 | bool observable, |
leothedragon | 0:8f0bb79ddd48 | 229 | void(*on_update)(string)) |
leothedragon | 0:8f0bb79ddd48 | 230 | |
leothedragon | 0:8f0bb79ddd48 | 231 | : SimpleM2MResourceBase(client,route) |
leothedragon | 0:8f0bb79ddd48 | 232 | { |
leothedragon | 0:8f0bb79ddd48 | 233 | tr_debug("SimpleM2MResourceString::SimpleM2MResourceString() overloaded creating (%s)\r\n", route); |
leothedragon | 0:8f0bb79ddd48 | 234 | FP1<void, string> fp; |
leothedragon | 0:8f0bb79ddd48 | 235 | fp.attach(on_update); |
leothedragon | 0:8f0bb79ddd48 | 236 | _on_update = fp; |
leothedragon | 0:8f0bb79ddd48 | 237 | define_resource_internal(v, opr, observable); |
leothedragon | 0:8f0bb79ddd48 | 238 | } |
leothedragon | 0:8f0bb79ddd48 | 239 | |
leothedragon | 0:8f0bb79ddd48 | 240 | SimpleM2MResourceString::~SimpleM2MResourceString() |
leothedragon | 0:8f0bb79ddd48 | 241 | { |
leothedragon | 0:8f0bb79ddd48 | 242 | } |
leothedragon | 0:8f0bb79ddd48 | 243 | |
leothedragon | 0:8f0bb79ddd48 | 244 | string SimpleM2MResourceString::operator=(const string& new_value) |
leothedragon | 0:8f0bb79ddd48 | 245 | { |
leothedragon | 0:8f0bb79ddd48 | 246 | tr_debug("SimpleM2MResourceString::operator=()"); |
leothedragon | 0:8f0bb79ddd48 | 247 | set(new_value); |
leothedragon | 0:8f0bb79ddd48 | 248 | return new_value; |
leothedragon | 0:8f0bb79ddd48 | 249 | } |
leothedragon | 0:8f0bb79ddd48 | 250 | |
leothedragon | 0:8f0bb79ddd48 | 251 | SimpleM2MResourceString::operator string() const |
leothedragon | 0:8f0bb79ddd48 | 252 | { |
leothedragon | 0:8f0bb79ddd48 | 253 | tr_debug("SimpleM2MResourceString::operator string()"); |
leothedragon | 0:8f0bb79ddd48 | 254 | string value = get(); |
leothedragon | 0:8f0bb79ddd48 | 255 | return value; |
leothedragon | 0:8f0bb79ddd48 | 256 | } |
leothedragon | 0:8f0bb79ddd48 | 257 | |
leothedragon | 0:8f0bb79ddd48 | 258 | void SimpleM2MResourceString::update() |
leothedragon | 0:8f0bb79ddd48 | 259 | { |
leothedragon | 0:8f0bb79ddd48 | 260 | string v = get(); |
leothedragon | 0:8f0bb79ddd48 | 261 | _on_update(v); |
leothedragon | 0:8f0bb79ddd48 | 262 | } |
leothedragon | 0:8f0bb79ddd48 | 263 | |
leothedragon | 0:8f0bb79ddd48 | 264 | SimpleM2MResourceInt::SimpleM2MResourceInt(MbedCloudClient* client, |
leothedragon | 0:8f0bb79ddd48 | 265 | const char* route, |
leothedragon | 0:8f0bb79ddd48 | 266 | int v, |
leothedragon | 0:8f0bb79ddd48 | 267 | M2MBase::Operation opr, |
leothedragon | 0:8f0bb79ddd48 | 268 | bool observable, |
leothedragon | 0:8f0bb79ddd48 | 269 | FP1<void, int> on_update) |
leothedragon | 0:8f0bb79ddd48 | 270 | : SimpleM2MResourceBase(client,route),_on_update(on_update) |
leothedragon | 0:8f0bb79ddd48 | 271 | { |
leothedragon | 0:8f0bb79ddd48 | 272 | tr_debug("SimpleM2MResourceInt::SimpleM2MResourceInt() creating (%s)\r\n", route); |
leothedragon | 0:8f0bb79ddd48 | 273 | char buffer[20]; |
leothedragon | 0:8f0bb79ddd48 | 274 | int size = sprintf(buffer,"%d",v); |
leothedragon | 0:8f0bb79ddd48 | 275 | std::string stringified(buffer,size); |
leothedragon | 0:8f0bb79ddd48 | 276 | define_resource_internal(stringified, opr, observable); |
leothedragon | 0:8f0bb79ddd48 | 277 | } |
leothedragon | 0:8f0bb79ddd48 | 278 | |
leothedragon | 0:8f0bb79ddd48 | 279 | SimpleM2MResourceInt::SimpleM2MResourceInt(MbedCloudClient* client, |
leothedragon | 0:8f0bb79ddd48 | 280 | const char* route, |
leothedragon | 0:8f0bb79ddd48 | 281 | int v, |
leothedragon | 0:8f0bb79ddd48 | 282 | M2MBase::Operation opr, |
leothedragon | 0:8f0bb79ddd48 | 283 | bool observable, |
leothedragon | 0:8f0bb79ddd48 | 284 | void(*on_update)(int)) |
leothedragon | 0:8f0bb79ddd48 | 285 | : SimpleM2MResourceBase(client,route) |
leothedragon | 0:8f0bb79ddd48 | 286 | { |
leothedragon | 0:8f0bb79ddd48 | 287 | tr_debug("SimpleM2MResourceInt::SimpleM2MResourceInt() overloaded creating (%s)\r\n", route); |
leothedragon | 0:8f0bb79ddd48 | 288 | FP1<void, int> fp; |
leothedragon | 0:8f0bb79ddd48 | 289 | fp.attach(on_update); |
leothedragon | 0:8f0bb79ddd48 | 290 | _on_update = fp; |
leothedragon | 0:8f0bb79ddd48 | 291 | char buffer[20]; |
leothedragon | 0:8f0bb79ddd48 | 292 | int size = sprintf(buffer,"%d",v); |
leothedragon | 0:8f0bb79ddd48 | 293 | std::string stringified(buffer,size); |
leothedragon | 0:8f0bb79ddd48 | 294 | define_resource_internal(stringified, opr, observable); |
leothedragon | 0:8f0bb79ddd48 | 295 | } |
leothedragon | 0:8f0bb79ddd48 | 296 | |
leothedragon | 0:8f0bb79ddd48 | 297 | SimpleM2MResourceInt::~SimpleM2MResourceInt() |
leothedragon | 0:8f0bb79ddd48 | 298 | { |
leothedragon | 0:8f0bb79ddd48 | 299 | } |
leothedragon | 0:8f0bb79ddd48 | 300 | |
leothedragon | 0:8f0bb79ddd48 | 301 | int SimpleM2MResourceInt::operator=(int new_value) |
leothedragon | 0:8f0bb79ddd48 | 302 | { |
leothedragon | 0:8f0bb79ddd48 | 303 | set(new_value); |
leothedragon | 0:8f0bb79ddd48 | 304 | return new_value; |
leothedragon | 0:8f0bb79ddd48 | 305 | } |
leothedragon | 0:8f0bb79ddd48 | 306 | |
leothedragon | 0:8f0bb79ddd48 | 307 | SimpleM2MResourceInt::operator int() const |
leothedragon | 0:8f0bb79ddd48 | 308 | { |
leothedragon | 0:8f0bb79ddd48 | 309 | string v = get(); |
leothedragon | 0:8f0bb79ddd48 | 310 | if (v.empty()) return 0; |
leothedragon | 0:8f0bb79ddd48 | 311 | |
leothedragon | 0:8f0bb79ddd48 | 312 | return atoi((const char*)v.c_str()); |
leothedragon | 0:8f0bb79ddd48 | 313 | } |
leothedragon | 0:8f0bb79ddd48 | 314 | |
leothedragon | 0:8f0bb79ddd48 | 315 | void SimpleM2MResourceInt::update() |
leothedragon | 0:8f0bb79ddd48 | 316 | { |
leothedragon | 0:8f0bb79ddd48 | 317 | string v = get(); |
leothedragon | 0:8f0bb79ddd48 | 318 | if (v.empty()) { |
leothedragon | 0:8f0bb79ddd48 | 319 | _on_update(0); |
leothedragon | 0:8f0bb79ddd48 | 320 | } else { |
leothedragon | 0:8f0bb79ddd48 | 321 | _on_update(atoi((const char*)v.c_str())); |
leothedragon | 0:8f0bb79ddd48 | 322 | } |
leothedragon | 0:8f0bb79ddd48 | 323 | } |
leothedragon | 0:8f0bb79ddd48 | 324 | #endif |