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.
Fork of simple-mbed-client by
simple-mbed-client.h@12:26810c6b58e1, 2016-09-28 (annotated)
- Committer:
- Jan Jongboom
- Date:
- Wed Sep 28 11:16:43 2016 +0300
- Revision:
- 12:26810c6b58e1
- Parent:
- 11:4da431479ce3
- Child:
- 14:3fd6afc63e6a
Move away from FunctionPointer in favor of Callback
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| janjongboom | 0:9fa3f3028773 | 1 | /* |
| janjongboom | 0:9fa3f3028773 | 2 | * Copyright (c) 2015 ARM Limited. All rights reserved. |
| janjongboom | 0:9fa3f3028773 | 3 | * SPDX-License-Identifier: Apache-2.0 |
| janjongboom | 0:9fa3f3028773 | 4 | * Licensed under the Apache License, Version 2.0 (the License); you may |
| janjongboom | 0:9fa3f3028773 | 5 | * not use this file except in compliance with the License. |
| janjongboom | 0:9fa3f3028773 | 6 | * You may obtain a copy of the License at |
| janjongboom | 0:9fa3f3028773 | 7 | * |
| janjongboom | 0:9fa3f3028773 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| janjongboom | 0:9fa3f3028773 | 9 | * |
| janjongboom | 0:9fa3f3028773 | 10 | * Unless required by applicable law or agreed to in writing, software |
| janjongboom | 0:9fa3f3028773 | 11 | * distributed under the License is distributed on an AS IS BASIS, WITHOUT |
| janjongboom | 0:9fa3f3028773 | 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| janjongboom | 0:9fa3f3028773 | 13 | * See the License for the specific language governing permissions and |
| janjongboom | 0:9fa3f3028773 | 14 | * limitations under the License. |
| janjongboom | 0:9fa3f3028773 | 15 | */ |
| janjongboom | 0:9fa3f3028773 | 16 | |
| janjongboom | 0:9fa3f3028773 | 17 | #ifndef __SIMPLE_MBED_CLIENT_H__ |
| janjongboom | 0:9fa3f3028773 | 18 | #define __SIMPLE_MBED_CLIENT_H__ |
| janjongboom | 0:9fa3f3028773 | 19 | |
| Jan Jongboom |
3:ce2322965a27 | 20 | #define debug_msg(...) if (debug) output.printf(__VA_ARGS__) |
| Jan Jongboom |
3:ce2322965a27 | 21 | |
| janjongboom | 0:9fa3f3028773 | 22 | #include <map> |
| janjongboom | 0:9fa3f3028773 | 23 | #include <string> |
| janjongboom | 0:9fa3f3028773 | 24 | #include <sstream> |
| janjongboom | 0:9fa3f3028773 | 25 | #include <vector> |
| janjongboom | 0:9fa3f3028773 | 26 | #include "mbed-client-wrapper.h" |
| janjongboom | 0:9fa3f3028773 | 27 | |
| janjongboom | 0:9fa3f3028773 | 28 | using namespace std; |
| janjongboom | 0:9fa3f3028773 | 29 | |
| Jan Jongboom |
3:ce2322965a27 | 30 | class SimpleResourceBase { |
| Jan Jongboom |
3:ce2322965a27 | 31 | public: |
| janjongboom | 10:3fecd642d506 | 32 | virtual void update(string v) {} |
| Jan Jongboom |
3:ce2322965a27 | 33 | }; |
| Jan Jongboom |
3:ce2322965a27 | 34 | |
| janjongboom | 0:9fa3f3028773 | 35 | class SimpleMbedClientBase { |
| janjongboom | 0:9fa3f3028773 | 36 | public: |
| Jan Jongboom |
3:ce2322965a27 | 37 | SimpleMbedClientBase(bool aDebug = true) |
| Jan Jongboom |
3:ce2322965a27 | 38 | : output(USBTX, USBRX), debug(aDebug) |
| Jan Jongboom |
3:ce2322965a27 | 39 | { |
| Jan Jongboom |
3:ce2322965a27 | 40 | |
| janjongboom | 0:9fa3f3028773 | 41 | } |
| Jan Jongboom |
3:ce2322965a27 | 42 | |
| janjongboom | 0:9fa3f3028773 | 43 | ~SimpleMbedClientBase() {} |
| janjongboom | 0:9fa3f3028773 | 44 | |
| janjongboom | 0:9fa3f3028773 | 45 | struct MbedClientOptions get_default_options() { |
| janjongboom | 0:9fa3f3028773 | 46 | struct MbedClientOptions options; |
| janjongboom | 0:9fa3f3028773 | 47 | options.Manufacturer = "Manufacturer_String"; |
| janjongboom | 0:9fa3f3028773 | 48 | options.Type = "Type_String"; |
| janjongboom | 0:9fa3f3028773 | 49 | options.ModelNumber = "ModelNumber_String"; |
| janjongboom | 0:9fa3f3028773 | 50 | options.SerialNumber = "SerialNumber_String"; |
| janjongboom | 0:9fa3f3028773 | 51 | options.DeviceType = "test"; |
| janjongboom | 0:9fa3f3028773 | 52 | options.SocketMode = M2MInterface::UDP; |
| janjongboom | 0:9fa3f3028773 | 53 | options.ServerAddress = "coap://api.connector.mbed.com:5684"; |
| Jan Jongboom |
3:ce2322965a27 | 54 | |
| janjongboom | 0:9fa3f3028773 | 55 | return options; |
| janjongboom | 0:9fa3f3028773 | 56 | } |
| janjongboom | 0:9fa3f3028773 | 57 | |
| Jan Jongboom |
4:0f9eae5739dd | 58 | bool init(NetworkInterface* iface) { |
| Jan Jongboom |
3:ce2322965a27 | 59 | debug_msg("[SMC] Device name %s\r\n", MBED_ENDPOINT_NAME); |
| Jan Jongboom |
3:ce2322965a27 | 60 | |
| janjongboom | 0:9fa3f3028773 | 61 | // Create endpoint interface to manage register and unregister |
| Jan Jongboom |
3:ce2322965a27 | 62 | client->create_interface(iface); |
| Jan Jongboom |
3:ce2322965a27 | 63 | |
| janjongboom | 0:9fa3f3028773 | 64 | // Create Objects of varying types, see simpleclient.h for more details on implementation. |
| janjongboom | 0:9fa3f3028773 | 65 | M2MSecurity* register_object = client->create_register_object(); // server object specifying connector info |
| janjongboom | 0:9fa3f3028773 | 66 | M2MDevice* device_object = client->create_device_object(); // device resources object |
| Jan Jongboom |
3:ce2322965a27 | 67 | |
| janjongboom | 0:9fa3f3028773 | 68 | // Create list of Objects to register |
| janjongboom | 0:9fa3f3028773 | 69 | M2MObjectList object_list; |
| Jan Jongboom |
3:ce2322965a27 | 70 | |
| janjongboom | 0:9fa3f3028773 | 71 | // Add objects to list |
| janjongboom | 0:9fa3f3028773 | 72 | object_list.push_back(device_object); |
| Jan Jongboom |
3:ce2322965a27 | 73 | |
| janjongboom | 0:9fa3f3028773 | 74 | map<string, M2MObject*>::iterator it; |
| janjongboom | 0:9fa3f3028773 | 75 | for (it = objects.begin(); it != objects.end(); it++) |
| janjongboom | 0:9fa3f3028773 | 76 | { |
| janjongboom | 0:9fa3f3028773 | 77 | object_list.push_back(it->second); |
| janjongboom | 0:9fa3f3028773 | 78 | } |
| Jan Jongboom |
3:ce2322965a27 | 79 | |
| janjongboom | 0:9fa3f3028773 | 80 | // Set endpoint registration object |
| janjongboom | 0:9fa3f3028773 | 81 | client->set_register_object(register_object); |
| Jan Jongboom |
3:ce2322965a27 | 82 | |
| janjongboom | 0:9fa3f3028773 | 83 | // Issue register command. |
| janjongboom | 0:9fa3f3028773 | 84 | client->test_register(register_object, object_list); |
| Jan Jongboom |
3:ce2322965a27 | 85 | |
| janjongboom | 0:9fa3f3028773 | 86 | // @todo: no idea if this works |
| janjongboom | 0:9fa3f3028773 | 87 | Ticker updateRegister; |
| janjongboom | 0:9fa3f3028773 | 88 | updateRegister.attach(client, &MbedClient::test_update_register, 25.0f); |
| Jan Jongboom |
3:ce2322965a27 | 89 | |
| janjongboom | 0:9fa3f3028773 | 90 | return true; |
| janjongboom | 0:9fa3f3028773 | 91 | } |
| janjongboom | 0:9fa3f3028773 | 92 | |
| Jan Jongboom |
4:0f9eae5739dd | 93 | bool setup(NetworkInterface* iface) { |
| Jan Jongboom |
3:ce2322965a27 | 94 | debug_msg("[SMC] In mbed_client_setup\r\n"); |
| janjongboom | 0:9fa3f3028773 | 95 | if (client) { |
| Jan Jongboom |
3:ce2322965a27 | 96 | debug_msg("[SMC] [ERROR] mbed_client_setup called, but mbed_client is already instantiated\r\n"); |
| janjongboom | 0:9fa3f3028773 | 97 | return false; |
| janjongboom | 0:9fa3f3028773 | 98 | } |
| Jan Jongboom |
3:ce2322965a27 | 99 | |
| janjongboom | 0:9fa3f3028773 | 100 | struct MbedClientOptions options = get_default_options(); |
| Jan Jongboom |
3:ce2322965a27 | 101 | |
| Jan Jongboom |
12:26810c6b58e1 | 102 | Callback<void(string)> updateFp(this, &SimpleMbedClientBase::resource_updated); |
| Jan Jongboom |
3:ce2322965a27 | 103 | client = new MbedClient(options, updateFp, debug); |
| Jan Jongboom |
3:ce2322965a27 | 104 | |
| janjongboom | 0:9fa3f3028773 | 105 | return init(iface); |
| janjongboom | 0:9fa3f3028773 | 106 | } |
| janjongboom | 0:9fa3f3028773 | 107 | |
| Jan Jongboom |
4:0f9eae5739dd | 108 | bool setup(MbedClientOptions options, NetworkInterface* iface) { |
| janjongboom | 0:9fa3f3028773 | 109 | if (client) { |
| Jan Jongboom |
3:ce2322965a27 | 110 | debug_msg("[SMC] [ERROR] mbed_client_setup called, but mbed_client is already instantiated\r\n"); |
| janjongboom | 0:9fa3f3028773 | 111 | return false; |
| janjongboom | 0:9fa3f3028773 | 112 | } |
| Jan Jongboom |
3:ce2322965a27 | 113 | |
| Jan Jongboom |
12:26810c6b58e1 | 114 | Callback<void(string)> updateFp(this, &SimpleMbedClientBase::resource_updated); |
| Jan Jongboom |
3:ce2322965a27 | 115 | client = new MbedClient(options, updateFp, debug); |
| Jan Jongboom |
3:ce2322965a27 | 116 | |
| janjongboom | 0:9fa3f3028773 | 117 | return init(iface); |
| janjongboom | 0:9fa3f3028773 | 118 | } |
| janjongboom | 0:9fa3f3028773 | 119 | |
| janjongboom | 0:9fa3f3028773 | 120 | void on_registered(void(*fn)(void)) { |
| Jan Jongboom |
12:26810c6b58e1 | 121 | Callback<void()> fp(fn); |
| janjongboom | 0:9fa3f3028773 | 122 | client->set_registered_function(fp); |
| janjongboom | 0:9fa3f3028773 | 123 | } |
| janjongboom | 0:9fa3f3028773 | 124 | |
| janjongboom | 0:9fa3f3028773 | 125 | template<typename T> |
| Jan Jongboom |
3:ce2322965a27 | 126 | void on_registered(T *object, void (T::*member)(void)) { |
| Jan Jongboom |
12:26810c6b58e1 | 127 | Callback<void()> fp(object, member); |
| janjongboom | 0:9fa3f3028773 | 128 | client->set_registered_function(fp); |
| janjongboom | 0:9fa3f3028773 | 129 | } |
| janjongboom | 0:9fa3f3028773 | 130 | |
| janjongboom | 0:9fa3f3028773 | 131 | void on_unregistered(void(*fn)(void)) { |
| Jan Jongboom |
12:26810c6b58e1 | 132 | Callback<void()> fp(fn); |
| janjongboom | 0:9fa3f3028773 | 133 | client->set_unregistered_function(fp); |
| janjongboom | 0:9fa3f3028773 | 134 | } |
| janjongboom | 0:9fa3f3028773 | 135 | |
| janjongboom | 0:9fa3f3028773 | 136 | template<typename T> |
| Jan Jongboom |
3:ce2322965a27 | 137 | void on_unregistered(T *object, void (T::*member)(void)) { |
| Jan Jongboom |
12:26810c6b58e1 | 138 | Callback<void()> fp(object, member); |
| janjongboom | 0:9fa3f3028773 | 139 | client->set_unregistered_function(fp); |
| janjongboom | 0:9fa3f3028773 | 140 | } |
| janjongboom | 0:9fa3f3028773 | 141 | |
| janjongboom | 0:9fa3f3028773 | 142 | bool define_function(const char* route, void(*fn)(void*)) { |
| janjongboom | 0:9fa3f3028773 | 143 | if (!define_resource_internal(route, string(), M2MBase::POST_ALLOWED, false)) { |
| janjongboom | 0:9fa3f3028773 | 144 | return false; |
| janjongboom | 0:9fa3f3028773 | 145 | } |
| Jan Jongboom |
3:ce2322965a27 | 146 | |
| janjongboom | 0:9fa3f3028773 | 147 | string route_str(route); |
| janjongboom | 0:9fa3f3028773 | 148 | if (!resources.count(route_str)) { |
| Jan Jongboom |
3:ce2322965a27 | 149 | debug_msg("[SMC] [ERROR] Should be created, but no such route (%s)\r\n", route); |
| janjongboom | 0:9fa3f3028773 | 150 | return false; |
| janjongboom | 0:9fa3f3028773 | 151 | } |
| Jan Jongboom |
3:ce2322965a27 | 152 | |
| Jan Jongboom |
3:ce2322965a27 | 153 | resources[route_str]->set_execute_function(execute_callback_2(fn)); |
| janjongboom | 0:9fa3f3028773 | 154 | return true; |
| janjongboom | 0:9fa3f3028773 | 155 | } |
| janjongboom | 0:9fa3f3028773 | 156 | |
| janjongboom | 0:9fa3f3028773 | 157 | bool define_function(const char* route, execute_callback fn) { |
| janjongboom | 0:9fa3f3028773 | 158 | if (!define_resource_internal(route, string(), M2MBase::POST_ALLOWED, false)) { |
| janjongboom | 0:9fa3f3028773 | 159 | return false; |
| janjongboom | 0:9fa3f3028773 | 160 | } |
| Jan Jongboom |
3:ce2322965a27 | 161 | |
| janjongboom | 0:9fa3f3028773 | 162 | string route_str(route); |
| janjongboom | 0:9fa3f3028773 | 163 | if (!resources.count(route_str)) { |
| Jan Jongboom |
3:ce2322965a27 | 164 | debug_msg("[SMC] [ERROR] Should be created, but no such route (%s)\r\n", route); |
| janjongboom | 0:9fa3f3028773 | 165 | return false; |
| janjongboom | 0:9fa3f3028773 | 166 | } |
| janjongboom | 0:9fa3f3028773 | 167 | // No clue why this is not working?! It works with class member, but not with static function... |
| janjongboom | 0:9fa3f3028773 | 168 | resources[route_str]->set_execute_function(fn); |
| janjongboom | 0:9fa3f3028773 | 169 | return true; |
| janjongboom | 0:9fa3f3028773 | 170 | } |
| Jan Jongboom |
3:ce2322965a27 | 171 | |
| janjongboom | 0:9fa3f3028773 | 172 | string get(string route_str) { |
| janjongboom | 0:9fa3f3028773 | 173 | if (!resources.count(route_str)) { |
| Jan Jongboom |
3:ce2322965a27 | 174 | debug_msg("[SMC] [ERROR] No such route (%s)\r\n", route_str.c_str()); |
| janjongboom | 0:9fa3f3028773 | 175 | return string(); |
| janjongboom | 0:9fa3f3028773 | 176 | } |
| Jan Jongboom |
3:ce2322965a27 | 177 | |
| janjongboom | 2:0a015df677a4 | 178 | // otherwise ask mbed Client... |
| janjongboom | 2:0a015df677a4 | 179 | uint8_t* buffIn = NULL; |
| janjongboom | 0:9fa3f3028773 | 180 | uint32_t sizeIn; |
| janjongboom | 0:9fa3f3028773 | 181 | resources[route_str]->get_value(buffIn, sizeIn); |
| Jan Jongboom |
3:ce2322965a27 | 182 | |
| janjongboom | 2:0a015df677a4 | 183 | string s((char*)buffIn, sizeIn); |
| janjongboom | 0:9fa3f3028773 | 184 | return s; |
| janjongboom | 0:9fa3f3028773 | 185 | } |
| janjongboom | 0:9fa3f3028773 | 186 | |
| janjongboom | 0:9fa3f3028773 | 187 | bool set(string route_str, string v) { |
| janjongboom | 2:0a015df677a4 | 188 | // Potentially set() happens in InterruptContext. That's not good. |
| janjongboom | 0:9fa3f3028773 | 189 | if (!resources.count(route_str)) { |
| Jan Jongboom |
3:ce2322965a27 | 190 | debug_msg("[SMC] [ERROR] No such route (%s)\r\n", route_str.c_str()); |
| janjongboom | 0:9fa3f3028773 | 191 | return false; |
| janjongboom | 0:9fa3f3028773 | 192 | } |
| Jan Jongboom |
3:ce2322965a27 | 193 | |
| Jan Jongboom |
4:0f9eae5739dd | 194 | if (v.length() == 0) { |
| Jan Jongboom |
4:0f9eae5739dd | 195 | resources[route_str]->clear_value(); |
| Jan Jongboom |
4:0f9eae5739dd | 196 | } |
| Jan Jongboom |
4:0f9eae5739dd | 197 | else { |
| Jan Jongboom |
4:0f9eae5739dd | 198 | resources[route_str]->set_value((uint8_t*)v.c_str(), v.length()); |
| Jan Jongboom |
4:0f9eae5739dd | 199 | } |
| janjongboom | 2:0a015df677a4 | 200 | |
| janjongboom | 0:9fa3f3028773 | 201 | return true; |
| janjongboom | 0:9fa3f3028773 | 202 | } |
| janjongboom | 0:9fa3f3028773 | 203 | |
| janjongboom | 0:9fa3f3028773 | 204 | bool set(string route, const int& v) { |
| janjongboom | 0:9fa3f3028773 | 205 | stringstream ss; |
| janjongboom | 0:9fa3f3028773 | 206 | ss << v; |
| janjongboom | 0:9fa3f3028773 | 207 | std::string stringified = ss.str(); |
| Jan Jongboom |
3:ce2322965a27 | 208 | |
| janjongboom | 0:9fa3f3028773 | 209 | return set(route, stringified); |
| janjongboom | 0:9fa3f3028773 | 210 | } |
| janjongboom | 0:9fa3f3028773 | 211 | |
| janjongboom | 0:9fa3f3028773 | 212 | bool define_resource_internal(const char* route, std::string v, M2MBase::Operation opr, bool observable) { |
| janjongboom | 0:9fa3f3028773 | 213 | if (client) { |
| Jan Jongboom |
3:ce2322965a27 | 214 | debug_msg("[SMC] [ERROR] mbed_client_define_resource, Can only define resources before mbed_client_setup is called!\r\n"); |
| janjongboom | 0:9fa3f3028773 | 215 | return false; |
| janjongboom | 0:9fa3f3028773 | 216 | } |
| Jan Jongboom |
3:ce2322965a27 | 217 | |
| janjongboom | 0:9fa3f3028773 | 218 | vector<string> segments = parse_route(route); |
| janjongboom | 0:9fa3f3028773 | 219 | if (segments.size() != 3) { |
| Jan Jongboom |
3:ce2322965a27 | 220 | debug_msg("[SMC] [ERROR] mbed_client_define_resource, Route needs to have three segments, split by '/' (%s)\r\n", route); |
| janjongboom | 0:9fa3f3028773 | 221 | return false; |
| janjongboom | 0:9fa3f3028773 | 222 | } |
| janjongboom | 0:9fa3f3028773 | 223 | |
| Jan Jongboom |
3:ce2322965a27 | 224 | // segments[1] should be one digit and numeric |
| janjongboom | 10:3fecd642d506 | 225 | char n = segments.at(1).c_str()[0]; |
| janjongboom | 10:3fecd642d506 | 226 | if (n < '0' || n > '9') { |
| Jan Jongboom |
3:ce2322965a27 | 227 | debug_msg("[SMC] [ERROR] mbed_client_define_resource, second route segment should be numeric, but was not (%s)\r\n", route); |
| Jan Jongboom |
3:ce2322965a27 | 228 | return false; |
| Jan Jongboom |
3:ce2322965a27 | 229 | } |
| Jan Jongboom |
3:ce2322965a27 | 230 | |
| Jan Jongboom |
3:ce2322965a27 | 231 | int inst_id = atoi(segments.at(1).c_str()); |
| Jan Jongboom |
3:ce2322965a27 | 232 | |
| janjongboom | 0:9fa3f3028773 | 233 | M2MObjectInstance* inst; |
| janjongboom | 0:9fa3f3028773 | 234 | if (objectInstances.count(segments.at(0))) { |
| janjongboom | 0:9fa3f3028773 | 235 | inst = objectInstances[segments.at(0)]; |
| janjongboom | 0:9fa3f3028773 | 236 | } |
| janjongboom | 0:9fa3f3028773 | 237 | else { |
| janjongboom | 0:9fa3f3028773 | 238 | M2MObject* obj = M2MInterfaceFactory::create_object(segments.at(0).c_str()); |
| janjongboom | 0:9fa3f3028773 | 239 | inst = obj->create_object_instance(inst_id); |
| janjongboom | 0:9fa3f3028773 | 240 | objects.insert(std::pair<string, M2MObject*>(segments.at(0), obj)); |
| janjongboom | 0:9fa3f3028773 | 241 | objectInstances.insert(std::pair<string, M2MObjectInstance*>(segments.at(0), inst)); |
| janjongboom | 0:9fa3f3028773 | 242 | } |
| Jan Jongboom |
3:ce2322965a27 | 243 | |
| janjongboom | 0:9fa3f3028773 | 244 | // @todo check if the resource exists yet |
| janjongboom | 0:9fa3f3028773 | 245 | M2MResource* res = inst->create_dynamic_resource(segments.at(2).c_str(), "", |
| janjongboom | 0:9fa3f3028773 | 246 | M2MResourceInstance::STRING, observable); |
| janjongboom | 0:9fa3f3028773 | 247 | res->set_operation(opr); |
| janjongboom | 0:9fa3f3028773 | 248 | res->set_value((uint8_t*)v.c_str(), v.length()); |
| Jan Jongboom |
3:ce2322965a27 | 249 | |
| janjongboom | 0:9fa3f3028773 | 250 | string route_str(route); |
| janjongboom | 2:0a015df677a4 | 251 | resources.insert(pair<string, M2MResource*>(route_str, res)); |
| Jan Jongboom |
3:ce2322965a27 | 252 | |
| janjongboom | 0:9fa3f3028773 | 253 | return true; |
| janjongboom | 0:9fa3f3028773 | 254 | } |
| Jan Jongboom |
3:ce2322965a27 | 255 | |
| janjongboom | 2:0a015df677a4 | 256 | void keep_alive() { |
| janjongboom | 2:0a015df677a4 | 257 | client->test_update_register(); |
| janjongboom | 2:0a015df677a4 | 258 | } |
| Jan Jongboom |
3:ce2322965a27 | 259 | |
| Jan Jongboom |
3:ce2322965a27 | 260 | void register_update_callback(string route, SimpleResourceBase* simpleResource) { |
| Jan Jongboom |
3:ce2322965a27 | 261 | updateValues[route] = simpleResource; |
| Jan Jongboom |
3:ce2322965a27 | 262 | } |
| Jan Jongboom |
3:ce2322965a27 | 263 | |
| Jan Jongboom |
6:a1a766d45957 | 264 | M2MResource* get_resource(string route) { |
| Jan Jongboom |
6:a1a766d45957 | 265 | if (!resources.count(route)) { |
| Jan Jongboom |
6:a1a766d45957 | 266 | debug_msg("[SMC] [ERROR] No such route (%s)\r\n", route.c_str()); |
| Jan Jongboom |
6:a1a766d45957 | 267 | return NULL; |
| Jan Jongboom |
6:a1a766d45957 | 268 | } |
| Jan Jongboom |
6:a1a766d45957 | 269 | |
| Jan Jongboom |
6:a1a766d45957 | 270 | return resources[route]; |
| Jan Jongboom |
6:a1a766d45957 | 271 | } |
| Jan Jongboom |
6:a1a766d45957 | 272 | |
| janjongboom | 0:9fa3f3028773 | 273 | private: |
| janjongboom | 0:9fa3f3028773 | 274 | vector<string> parse_route(const char* route) { |
| janjongboom | 0:9fa3f3028773 | 275 | string s(route); |
| janjongboom | 0:9fa3f3028773 | 276 | vector<string> v; |
| janjongboom | 0:9fa3f3028773 | 277 | stringstream ss(s); |
| janjongboom | 0:9fa3f3028773 | 278 | string item; |
| janjongboom | 0:9fa3f3028773 | 279 | while (getline(ss, item, '/')) { |
| janjongboom | 0:9fa3f3028773 | 280 | v.push_back(item); |
| janjongboom | 0:9fa3f3028773 | 281 | } |
| janjongboom | 0:9fa3f3028773 | 282 | return v; |
| janjongboom | 0:9fa3f3028773 | 283 | } |
| janjongboom | 0:9fa3f3028773 | 284 | |
| Jan Jongboom |
3:ce2322965a27 | 285 | void resource_updated(string uri) { |
| Jan Jongboom |
3:ce2322965a27 | 286 | if (updateValues.count(uri) == 0) return; |
| Jan Jongboom |
3:ce2322965a27 | 287 | |
| Jan Jongboom |
3:ce2322965a27 | 288 | string v = get(uri); |
| Jan Jongboom |
3:ce2322965a27 | 289 | if (v.empty()) return; |
| Jan Jongboom |
3:ce2322965a27 | 290 | |
| Jan Jongboom |
3:ce2322965a27 | 291 | updateValues[uri]->update(v); |
| Jan Jongboom |
3:ce2322965a27 | 292 | } |
| Jan Jongboom |
3:ce2322965a27 | 293 | |
| janjongboom | 0:9fa3f3028773 | 294 | Serial output; |
| Jan Jongboom |
3:ce2322965a27 | 295 | |
| janjongboom | 0:9fa3f3028773 | 296 | MbedClient* client; |
| janjongboom | 0:9fa3f3028773 | 297 | map<string, M2MObject*> objects; |
| janjongboom | 0:9fa3f3028773 | 298 | map<string, M2MObjectInstance*> objectInstances; |
| janjongboom | 0:9fa3f3028773 | 299 | map<string, M2MResource*> resources; |
| janjongboom | 2:0a015df677a4 | 300 | |
| Jan Jongboom |
3:ce2322965a27 | 301 | bool debug; |
| Jan Jongboom |
3:ce2322965a27 | 302 | |
| Jan Jongboom |
3:ce2322965a27 | 303 | map<string, SimpleResourceBase*> updateValues; |
| janjongboom | 0:9fa3f3028773 | 304 | }; |
| janjongboom | 0:9fa3f3028773 | 305 | |
| Jan Jongboom |
3:ce2322965a27 | 306 | class SimpleResourceString : public SimpleResourceBase { |
| janjongboom | 0:9fa3f3028773 | 307 | public: |
| Jan Jongboom |
12:26810c6b58e1 | 308 | SimpleResourceString(SimpleMbedClientBase* aSimpleClient, string aRoute, Callback<void(string)> aOnUpdate) : |
| Jan Jongboom |
3:ce2322965a27 | 309 | simpleClient(aSimpleClient), route(aRoute), onUpdate(aOnUpdate) {} |
| janjongboom | 0:9fa3f3028773 | 310 | |
| janjongboom | 0:9fa3f3028773 | 311 | string operator=(const string& newValue) { |
| janjongboom | 0:9fa3f3028773 | 312 | simpleClient->set(route, newValue); |
| janjongboom | 0:9fa3f3028773 | 313 | return newValue; |
| janjongboom | 0:9fa3f3028773 | 314 | }; |
| janjongboom | 0:9fa3f3028773 | 315 | operator string() const { |
| janjongboom | 0:9fa3f3028773 | 316 | return simpleClient->get(route); |
| janjongboom | 0:9fa3f3028773 | 317 | }; |
| janjongboom | 0:9fa3f3028773 | 318 | |
| Jan Jongboom |
3:ce2322965a27 | 319 | virtual void update(string v) { |
| Jan Jongboom |
3:ce2322965a27 | 320 | if (onUpdate) onUpdate(v); |
| Jan Jongboom |
3:ce2322965a27 | 321 | } |
| Jan Jongboom |
3:ce2322965a27 | 322 | |
| Jan Jongboom |
6:a1a766d45957 | 323 | M2MResource* get_resource() { |
| Jan Jongboom |
6:a1a766d45957 | 324 | return simpleClient->get_resource(route); |
| Jan Jongboom |
6:a1a766d45957 | 325 | } |
| Jan Jongboom |
6:a1a766d45957 | 326 | |
| janjongboom | 0:9fa3f3028773 | 327 | private: |
| janjongboom | 0:9fa3f3028773 | 328 | SimpleMbedClientBase* simpleClient; |
| janjongboom | 0:9fa3f3028773 | 329 | string route; |
| Jan Jongboom |
12:26810c6b58e1 | 330 | Callback<void(string)> onUpdate; |
| janjongboom | 0:9fa3f3028773 | 331 | }; |
| janjongboom | 0:9fa3f3028773 | 332 | |
| Jan Jongboom |
3:ce2322965a27 | 333 | class SimpleResourceInt : public SimpleResourceBase { |
| janjongboom | 0:9fa3f3028773 | 334 | public: |
| Jan Jongboom |
12:26810c6b58e1 | 335 | SimpleResourceInt(SimpleMbedClientBase* aSimpleClient, string aRoute, Callback<void(int)> aOnUpdate) : |
| Jan Jongboom |
3:ce2322965a27 | 336 | simpleClient(aSimpleClient), route(aRoute), onUpdate(aOnUpdate) {} |
| janjongboom | 0:9fa3f3028773 | 337 | |
| janjongboom | 0:9fa3f3028773 | 338 | int operator=(int newValue) { |
| janjongboom | 0:9fa3f3028773 | 339 | simpleClient->set(route, newValue); |
| janjongboom | 0:9fa3f3028773 | 340 | return newValue; |
| janjongboom | 0:9fa3f3028773 | 341 | }; |
| janjongboom | 0:9fa3f3028773 | 342 | operator int() const { |
| janjongboom | 0:9fa3f3028773 | 343 | string v = simpleClient->get(route); |
| janjongboom | 0:9fa3f3028773 | 344 | if (v.empty()) return 0; |
| Jan Jongboom |
3:ce2322965a27 | 345 | |
| janjongboom | 0:9fa3f3028773 | 346 | return atoi((const char*)v.c_str()); |
| janjongboom | 0:9fa3f3028773 | 347 | }; |
| janjongboom | 0:9fa3f3028773 | 348 | |
| Jan Jongboom |
3:ce2322965a27 | 349 | virtual void update(string v) { |
| Jan Jongboom |
3:ce2322965a27 | 350 | if (!onUpdate) return; |
| Jan Jongboom |
3:ce2322965a27 | 351 | |
| Jan Jongboom |
3:ce2322965a27 | 352 | onUpdate(atoi((const char*)v.c_str())); |
| Jan Jongboom |
3:ce2322965a27 | 353 | } |
| Jan Jongboom |
3:ce2322965a27 | 354 | |
| Jan Jongboom |
6:a1a766d45957 | 355 | M2MResource* get_resource() { |
| Jan Jongboom |
6:a1a766d45957 | 356 | return simpleClient->get_resource(route); |
| Jan Jongboom |
6:a1a766d45957 | 357 | } |
| Jan Jongboom |
6:a1a766d45957 | 358 | |
| janjongboom | 0:9fa3f3028773 | 359 | private: |
| janjongboom | 0:9fa3f3028773 | 360 | SimpleMbedClientBase* simpleClient; |
| janjongboom | 0:9fa3f3028773 | 361 | string route; |
| Jan Jongboom |
12:26810c6b58e1 | 362 | Callback<void(int)> onUpdate; |
| janjongboom | 0:9fa3f3028773 | 363 | }; |
| janjongboom | 0:9fa3f3028773 | 364 | |
| janjongboom | 0:9fa3f3028773 | 365 | class SimpleMbedClient : public SimpleMbedClientBase { |
| janjongboom | 0:9fa3f3028773 | 366 | public: |
| janjongboom | 1:75015f627e89 | 367 | |
| janjongboom | 1:75015f627e89 | 368 | // @todo: macro this up |
| janjongboom | 1:75015f627e89 | 369 | |
| janjongboom | 1:75015f627e89 | 370 | SimpleResourceString define_resource( |
| Jan Jongboom |
3:ce2322965a27 | 371 | const char* route, |
| Jan Jongboom |
3:ce2322965a27 | 372 | string v, |
| Jan Jongboom |
3:ce2322965a27 | 373 | M2MBase::Operation opr = M2MBase::GET_PUT_ALLOWED, |
| janjongboom | 1:75015f627e89 | 374 | bool observable = true, |
| Jan Jongboom |
12:26810c6b58e1 | 375 | Callback<void(string)> onUpdate = NULL) |
| janjongboom | 1:75015f627e89 | 376 | { |
| Jan Jongboom |
3:ce2322965a27 | 377 | SimpleResourceString* simpleResource = new SimpleResourceString(this, route, onUpdate); |
| janjongboom | 0:9fa3f3028773 | 378 | bool res = define_resource_internal(route, v, opr, observable); |
| Jan Jongboom |
3:ce2322965a27 | 379 | if (!res) { |
| Jan Jongboom |
3:ce2322965a27 | 380 | printf("Error while creating %s\n", route); |
| Jan Jongboom |
3:ce2322965a27 | 381 | } |
| Jan Jongboom |
3:ce2322965a27 | 382 | else { |
| Jan Jongboom |
3:ce2322965a27 | 383 | register_update_callback(route, simpleResource); |
| Jan Jongboom |
3:ce2322965a27 | 384 | } |
| Jan Jongboom |
3:ce2322965a27 | 385 | return *simpleResource; |
| janjongboom | 0:9fa3f3028773 | 386 | } |
| janjongboom | 1:75015f627e89 | 387 | |
| janjongboom | 1:75015f627e89 | 388 | SimpleResourceString define_resource( |
| Jan Jongboom |
3:ce2322965a27 | 389 | const char* route, |
| Jan Jongboom |
3:ce2322965a27 | 390 | string v, |
| Jan Jongboom |
3:ce2322965a27 | 391 | M2MBase::Operation opr, |
| janjongboom | 1:75015f627e89 | 392 | bool observable, |
| Jan Jongboom |
3:ce2322965a27 | 393 | void(*onUpdate)(string)) |
| janjongboom | 1:75015f627e89 | 394 | { |
| Jan Jongboom |
12:26810c6b58e1 | 395 | Callback<void(string)> fp; |
| janjongboom | 1:75015f627e89 | 396 | fp.attach(onUpdate); |
| janjongboom | 1:75015f627e89 | 397 | return define_resource(route, v, opr, observable, fp); |
| janjongboom | 1:75015f627e89 | 398 | } |
| Jan Jongboom |
3:ce2322965a27 | 399 | |
| janjongboom | 1:75015f627e89 | 400 | SimpleResourceString define_resource( |
| Jan Jongboom |
3:ce2322965a27 | 401 | const char* route, |
| janjongboom | 1:75015f627e89 | 402 | string v, |
| Jan Jongboom |
12:26810c6b58e1 | 403 | Callback<void(string)> onUpdate) |
| janjongboom | 1:75015f627e89 | 404 | { |
| janjongboom | 1:75015f627e89 | 405 | return define_resource(route, v, M2MBase::GET_PUT_ALLOWED, true, onUpdate); |
| janjongboom | 1:75015f627e89 | 406 | } |
| janjongboom | 1:75015f627e89 | 407 | |
| janjongboom | 1:75015f627e89 | 408 | SimpleResourceString define_resource( |
| Jan Jongboom |
3:ce2322965a27 | 409 | const char* route, |
| janjongboom | 1:75015f627e89 | 410 | string v, |
| Jan Jongboom |
3:ce2322965a27 | 411 | void(*onUpdate)(string)) |
| janjongboom | 1:75015f627e89 | 412 | { |
| Jan Jongboom |
12:26810c6b58e1 | 413 | Callback<void(string)> fp; |
| janjongboom | 1:75015f627e89 | 414 | fp.attach(onUpdate); |
| janjongboom | 1:75015f627e89 | 415 | return define_resource(route, v, M2MBase::GET_PUT_ALLOWED, true, fp); |
| janjongboom | 1:75015f627e89 | 416 | } |
| Jan Jongboom |
3:ce2322965a27 | 417 | |
| janjongboom | 1:75015f627e89 | 418 | SimpleResourceInt define_resource( |
| Jan Jongboom |
3:ce2322965a27 | 419 | const char* route, |
| Jan Jongboom |
3:ce2322965a27 | 420 | int v, |
| Jan Jongboom |
3:ce2322965a27 | 421 | M2MBase::Operation opr = M2MBase::GET_PUT_ALLOWED, |
| janjongboom | 1:75015f627e89 | 422 | bool observable = true, |
| Jan Jongboom |
12:26810c6b58e1 | 423 | Callback<void(int)> onUpdate = NULL) |
| janjongboom | 1:75015f627e89 | 424 | { |
| Jan Jongboom |
3:ce2322965a27 | 425 | SimpleResourceInt* simpleResource = new SimpleResourceInt(this, route, onUpdate); |
| Jan Jongboom |
3:ce2322965a27 | 426 | |
| janjongboom | 0:9fa3f3028773 | 427 | stringstream ss; |
| janjongboom | 0:9fa3f3028773 | 428 | ss << v; |
| janjongboom | 0:9fa3f3028773 | 429 | std::string stringified = ss.str(); |
| janjongboom | 0:9fa3f3028773 | 430 | bool res = define_resource_internal(route, stringified, opr, observable); |
| Jan Jongboom |
3:ce2322965a27 | 431 | if (!res) { |
| Jan Jongboom |
3:ce2322965a27 | 432 | printf("Error while creating %s\n", route); |
| Jan Jongboom |
3:ce2322965a27 | 433 | } |
| Jan Jongboom |
3:ce2322965a27 | 434 | else { |
| Jan Jongboom |
3:ce2322965a27 | 435 | register_update_callback(route, simpleResource); |
| Jan Jongboom |
3:ce2322965a27 | 436 | } |
| Jan Jongboom |
3:ce2322965a27 | 437 | return *simpleResource; |
| janjongboom | 0:9fa3f3028773 | 438 | } |
| Jan Jongboom |
3:ce2322965a27 | 439 | |
| janjongboom | 1:75015f627e89 | 440 | SimpleResourceInt define_resource( |
| Jan Jongboom |
3:ce2322965a27 | 441 | const char* route, |
| Jan Jongboom |
3:ce2322965a27 | 442 | int v, |
| Jan Jongboom |
3:ce2322965a27 | 443 | M2MBase::Operation opr, |
| janjongboom | 1:75015f627e89 | 444 | bool observable, |
| Jan Jongboom |
3:ce2322965a27 | 445 | void(*onUpdate)(int)) |
| janjongboom | 1:75015f627e89 | 446 | { |
| Jan Jongboom |
12:26810c6b58e1 | 447 | Callback<void(int)> fp; |
| janjongboom | 1:75015f627e89 | 448 | fp.attach(onUpdate); |
| janjongboom | 1:75015f627e89 | 449 | return define_resource(route, v, opr, observable, fp); |
| janjongboom | 1:75015f627e89 | 450 | } |
| Jan Jongboom |
3:ce2322965a27 | 451 | |
| janjongboom | 1:75015f627e89 | 452 | SimpleResourceInt define_resource( |
| Jan Jongboom |
3:ce2322965a27 | 453 | const char* route, |
| janjongboom | 1:75015f627e89 | 454 | int v, |
| Jan Jongboom |
12:26810c6b58e1 | 455 | Callback<void(int)> onUpdate) |
| janjongboom | 1:75015f627e89 | 456 | { |
| janjongboom | 1:75015f627e89 | 457 | return define_resource(route, v, M2MBase::GET_PUT_ALLOWED, true, onUpdate); |
| janjongboom | 1:75015f627e89 | 458 | } |
| janjongboom | 1:75015f627e89 | 459 | |
| janjongboom | 1:75015f627e89 | 460 | SimpleResourceInt define_resource( |
| Jan Jongboom |
3:ce2322965a27 | 461 | const char* route, |
| janjongboom | 1:75015f627e89 | 462 | int v, |
| Jan Jongboom |
3:ce2322965a27 | 463 | void(*onUpdate)(int)) |
| janjongboom | 1:75015f627e89 | 464 | { |
| Jan Jongboom |
12:26810c6b58e1 | 465 | Callback<void(int)> fp; |
| janjongboom | 1:75015f627e89 | 466 | fp.attach(onUpdate); |
| janjongboom | 1:75015f627e89 | 467 | return define_resource(route, v, M2MBase::GET_PUT_ALLOWED, true, fp); |
| janjongboom | 1:75015f627e89 | 468 | } |
| janjongboom | 0:9fa3f3028773 | 469 | }; |
| janjongboom | 0:9fa3f3028773 | 470 | |
| Jan Jongboom |
3:ce2322965a27 | 471 | #endif // __SIMPLE_MBED_CLIENT_H__ |
