A simple interface to mbed Device Connector, where you just declare variables to push them to the cloud.

Dependents:   Wifi_Get_Test_V1 simple-mbed-client-example simple-client-app-shield simple-sensor-client

Fork of simple-mbed-client by Jan Jongboom

TL;DR? See simple-mbed-client-example to get started immediately.

This library is a simpler interface to mbed Client, making it trivial to expose sensors, actuators and other variables to the cloud. It does not require you to change how you write your code. You can take any local variable, swap it out for a call to Simple Mbed Client, and the variable will automatically be synchronised with mbed Cloud.

For example, here's how you expose the value of a light sensor to the cloud:

SimpleMbedClient client;

SimpleResourceInt light_value = client.define_resource("light/0/value", 0);     // create the var

AnalogIn light(A1);

void read_light_sensor() {
    // light_value behaves just like a normal variable that you can read and write to!
    light_value = light.read_u16();
}

// update every second
Ticker t;
t.attach(&read_light_sensor, 1.0f);

Setting up

First import this library to your project. As Simple Mbed Client also needs a way to talk to the outside world, you'll need a NetworkInterface-object. The easiest way is by using the easy-connect library, so add that to your project as well. See the easy-connect docs on how to specify the connectivity method.

We also need a way of authenticating with mbed Cloud. For this we need a security certificate. Go to mbed Cloud, and select 'GET MY DEVICE SECURITY CREDENTIALS'. Save the certificate as security.h in your project folder.

Now we can initiate Simple Mbed Client and connect it to the internet.

#include "mbed.h"
#include "security.h"
#include "easy-connect.h"
#include "simple-mbed-client.h"

SimpleMbedClient client;

DigitalOut led(LED1, 0);

void registered() {
    led = 1;
}

int main() {
    NetworkInterface* network = connect_to_network(); // if connection failed, network will be NULL
    client.setup(network); // returns a bool, check if it's true

    client.on_registered(&registered);

    while (1) {
        wait_ms(25000);
        client.keep_alive();
    }
}

Defining variables

You can define a new variable by a call to client.define_resource. This function takes five arguments:

  1. path - The URL on which your variable is exposed in mbed Cloud. Needs to be three (3) segments, split by a slash (/) in the form of 'sensor/0/value'. The second segment always needs to be numeric.
  2. defaultValue - The default value of the variable. Needs to be either a string or an integer. Depending on the type that you pass in here the type of the variable is defined.
  3. operation - Some variables might be read-only or write-only (seen from the cloud). Use the operation to define these constraints. It's of type M2MBase::Operation. Default is GET_PUT_ALLOWED.
  4. observable - If set to false, cloud applications cannot subscribe to updates on this variable. Default is true.
  5. callback - Function pointer which is called whenever the value of the variable is changed from the cloud.

The type returned by the function is either SimpleResourceInt or SimpleResourceString. You can assign and read from these variables like any normal local variable.

void name_updated(string new_value) {
    printf("Value is now %s\n", new_value.c_str());
}

SimpleResourceString name = client.define_resource("device/0/name", "jan", M2MBase::GET_PUT_ALLOWED, true, &name_updated);

// we can read and write to this variable, e.g.:
stringstream ss;
ss << name;

// or
name = "pietje";

// are all valid

Defining functions

You can define functions, which do not have a value, but can just be invoked from the cloud, by a call to client.define_function. This function takes two arguments:

  1. path - The URL on which your variable is exposed in mbed Cloud. Needs to be three (3) segments, split by a slash (/) in the form of 'sensor/0/value'. The second segment always needs to be numeric.
  2. callback - Function pointer which is invoked when the function is called. Takes in a pointer, which contains the data being passed in from the cloud.

void play(void* data) {
    if (data) { // data can be NULL!
        // cast it to something useful
    }
}

client.define_function("music/0/play", &play);

Accessing the underlying M2MResource

If you need access to the underlying M2MResource you can do so by calling get_resource on a variable, or by calling client.get_resource if it's a function.

SimpleResourceInt led = client.define_resource("led/0/value", true);

client.define_function("led/0/toggle", &toggleLed);

// now to get the resource
M2MResource* ledResource = led.get_resource();
M2MResource* toggleResource = client.get_resource("led/0/toggle");

Printing variables

Unfortunately printf is kind of dumb, and does not automatically cast the variables. If you want to print any of the Simple Mbed Client variables you'll need to cast yourself.

SimpleResourceInt led = client.define_resource("led/0/value", true);

printf("Value is currently %d\n", static_cast<int>(led));

Event Queue

Simple Mbed Client uses an mbed-events EventQueue - running on a separate RTOS thread - to handle incoming events without blocking the main loop. Both the thread and event queue are created when initializing the library. You can override this behavior by providing your own event queue. In this case no thread is created.

EventQueue myQueue;
SimpleMbedClient client(&myQueue);

You can also use the queue to process your own events, which is very useful when dealing with ISRs. The queue is accessible through the eventQueue() function on the client object and returns a pointer to the queue.

SimpleMbedClient client;

InterruptIn btn(D2);

int main() {
  btn.fall(client.eventQueue()->event(&fall));
}
Committer:
janjongboom
Date:
Tue May 17 00:15:11 2016 +0000
Revision:
2:0a015df677a4
Parent:
1:75015f627e89
Child:
3:ce2322965a27
Fix getting variables, and add keep_alive function.

Who changed what in which revision?

UserRevisionLine numberNew 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
janjongboom 0:9fa3f3028773 20 #include <map>
janjongboom 0:9fa3f3028773 21 #include <string>
janjongboom 0:9fa3f3028773 22 #include <sstream>
janjongboom 0:9fa3f3028773 23 #include <vector>
janjongboom 0:9fa3f3028773 24 #include "mbed-client-classic/m2mnetwork.h"
janjongboom 0:9fa3f3028773 25 #include "mbed-client-wrapper.h"
janjongboom 0:9fa3f3028773 26
janjongboom 0:9fa3f3028773 27 using namespace std;
janjongboom 0:9fa3f3028773 28
janjongboom 0:9fa3f3028773 29 class SimpleMbedClientBase {
janjongboom 0:9fa3f3028773 30 public:
janjongboom 0:9fa3f3028773 31 SimpleMbedClientBase() : output(USBTX, USBRX) {
janjongboom 0:9fa3f3028773 32
janjongboom 0:9fa3f3028773 33 }
janjongboom 0:9fa3f3028773 34 ~SimpleMbedClientBase() {}
janjongboom 0:9fa3f3028773 35
janjongboom 0:9fa3f3028773 36 struct MbedClientOptions get_default_options() {
janjongboom 0:9fa3f3028773 37 struct MbedClientOptions options;
janjongboom 0:9fa3f3028773 38 options.Manufacturer = "Manufacturer_String";
janjongboom 0:9fa3f3028773 39 options.Type = "Type_String";
janjongboom 0:9fa3f3028773 40 options.ModelNumber = "ModelNumber_String";
janjongboom 0:9fa3f3028773 41 options.SerialNumber = "SerialNumber_String";
janjongboom 0:9fa3f3028773 42 options.DeviceType = "test";
janjongboom 0:9fa3f3028773 43 options.SocketMode = M2MInterface::UDP;
janjongboom 0:9fa3f3028773 44 options.ServerAddress = "coap://api.connector.mbed.com:5684";
janjongboom 0:9fa3f3028773 45
janjongboom 0:9fa3f3028773 46 return options;
janjongboom 0:9fa3f3028773 47 }
janjongboom 0:9fa3f3028773 48
janjongboom 0:9fa3f3028773 49 bool init(NetworkInterface* iface) {
janjongboom 0:9fa3f3028773 50 // does this automatically handle everything? weird yo.
janjongboom 0:9fa3f3028773 51 M2MNetwork network(iface);
janjongboom 0:9fa3f3028773 52
janjongboom 0:9fa3f3028773 53 output.printf("[SMC] Device name %s\r\n", MBED_ENDPOINT_NAME);
janjongboom 0:9fa3f3028773 54
janjongboom 0:9fa3f3028773 55 // Create endpoint interface to manage register and unregister
janjongboom 0:9fa3f3028773 56 client->create_interface();
janjongboom 0:9fa3f3028773 57
janjongboom 0:9fa3f3028773 58 // Create Objects of varying types, see simpleclient.h for more details on implementation.
janjongboom 0:9fa3f3028773 59 M2MSecurity* register_object = client->create_register_object(); // server object specifying connector info
janjongboom 0:9fa3f3028773 60 M2MDevice* device_object = client->create_device_object(); // device resources object
janjongboom 0:9fa3f3028773 61
janjongboom 0:9fa3f3028773 62 // Create list of Objects to register
janjongboom 0:9fa3f3028773 63 M2MObjectList object_list;
janjongboom 0:9fa3f3028773 64
janjongboom 0:9fa3f3028773 65 // Add objects to list
janjongboom 0:9fa3f3028773 66 object_list.push_back(device_object);
janjongboom 0:9fa3f3028773 67
janjongboom 0:9fa3f3028773 68 map<string, M2MObject*>::iterator it;
janjongboom 0:9fa3f3028773 69 for (it = objects.begin(); it != objects.end(); it++)
janjongboom 0:9fa3f3028773 70 {
janjongboom 0:9fa3f3028773 71 object_list.push_back(it->second);
janjongboom 0:9fa3f3028773 72 }
janjongboom 0:9fa3f3028773 73
janjongboom 0:9fa3f3028773 74 // Set endpoint registration object
janjongboom 0:9fa3f3028773 75 client->set_register_object(register_object);
janjongboom 0:9fa3f3028773 76
janjongboom 0:9fa3f3028773 77 // Issue register command.
janjongboom 0:9fa3f3028773 78 client->test_register(register_object, object_list);
janjongboom 0:9fa3f3028773 79
janjongboom 0:9fa3f3028773 80 // @todo: no idea if this works
janjongboom 0:9fa3f3028773 81 Ticker updateRegister;
janjongboom 0:9fa3f3028773 82 updateRegister.attach(client, &MbedClient::test_update_register, 25.0f);
janjongboom 0:9fa3f3028773 83
janjongboom 0:9fa3f3028773 84 return true;
janjongboom 0:9fa3f3028773 85 }
janjongboom 0:9fa3f3028773 86
janjongboom 0:9fa3f3028773 87 bool setup(NetworkInterface* iface) {
janjongboom 0:9fa3f3028773 88 output.printf("[SMC] In mbed_client_setup\r\n");
janjongboom 0:9fa3f3028773 89 if (client) {
janjongboom 0:9fa3f3028773 90 output.printf("[SMC] [ERROR] mbed_client_setup called, but mbed_client is already instantiated\r\n");
janjongboom 0:9fa3f3028773 91 return false;
janjongboom 0:9fa3f3028773 92 }
janjongboom 0:9fa3f3028773 93
janjongboom 0:9fa3f3028773 94 struct MbedClientOptions options = get_default_options();
janjongboom 0:9fa3f3028773 95
janjongboom 0:9fa3f3028773 96 client = new MbedClient(options);
janjongboom 0:9fa3f3028773 97 return init(iface);
janjongboom 0:9fa3f3028773 98 }
janjongboom 0:9fa3f3028773 99
janjongboom 0:9fa3f3028773 100 bool setup(MbedClientOptions options, NetworkInterface* iface) {
janjongboom 0:9fa3f3028773 101 if (client) {
janjongboom 0:9fa3f3028773 102 output.printf("[SMC] [ERROR] mbed_client_setup called, but mbed_client is already instantiated\r\n");
janjongboom 0:9fa3f3028773 103 return false;
janjongboom 0:9fa3f3028773 104 }
janjongboom 0:9fa3f3028773 105 client = new MbedClient(options);
janjongboom 0:9fa3f3028773 106 return init(iface);
janjongboom 0:9fa3f3028773 107 }
janjongboom 0:9fa3f3028773 108
janjongboom 0:9fa3f3028773 109 void on_registered(void(*fn)(void)) {
janjongboom 0:9fa3f3028773 110 FunctionPointer fp(fn);
janjongboom 0:9fa3f3028773 111 client->set_registered_function(fp);
janjongboom 0:9fa3f3028773 112 }
janjongboom 0:9fa3f3028773 113
janjongboom 0:9fa3f3028773 114 template<typename T>
janjongboom 0:9fa3f3028773 115 void on_registered(T *object, void (T::*member)(void)) {
janjongboom 0:9fa3f3028773 116 FunctionPointer fp(object, member);
janjongboom 0:9fa3f3028773 117 client->set_registered_function(fp);
janjongboom 0:9fa3f3028773 118 }
janjongboom 0:9fa3f3028773 119
janjongboom 0:9fa3f3028773 120 void on_unregistered(void(*fn)(void)) {
janjongboom 0:9fa3f3028773 121 FunctionPointer fp(fn);
janjongboom 0:9fa3f3028773 122 client->set_unregistered_function(fp);
janjongboom 0:9fa3f3028773 123 }
janjongboom 0:9fa3f3028773 124
janjongboom 0:9fa3f3028773 125 template<typename T>
janjongboom 0:9fa3f3028773 126 void on_unregistered(T *object, void (T::*member)(void)) {
janjongboom 0:9fa3f3028773 127 FunctionPointer fp(object, member);
janjongboom 0:9fa3f3028773 128 client->set_unregistered_function(fp);
janjongboom 0:9fa3f3028773 129 }
janjongboom 0:9fa3f3028773 130
janjongboom 0:9fa3f3028773 131 bool define_function(const char* route, void(*fn)(void*)) {
janjongboom 0:9fa3f3028773 132 if (!define_resource_internal(route, string(), M2MBase::POST_ALLOWED, false)) {
janjongboom 0:9fa3f3028773 133 return false;
janjongboom 0:9fa3f3028773 134 }
janjongboom 0:9fa3f3028773 135
janjongboom 0:9fa3f3028773 136 string route_str(route);
janjongboom 0:9fa3f3028773 137 if (!resources.count(route_str)) {
janjongboom 0:9fa3f3028773 138 output.printf("[SMC] [ERROR] Should be created, but no such route (%s)\r\n", route);
janjongboom 0:9fa3f3028773 139 return false;
janjongboom 0:9fa3f3028773 140 }
janjongboom 0:9fa3f3028773 141
janjongboom 0:9fa3f3028773 142 FunctionPointerArg1<void, void*>* fp = new FunctionPointerArg1<void, void*>(fn);
janjongboom 0:9fa3f3028773 143 resources[route_str]->set_execute_function(execute_callback(fp, &FunctionPointerArg1<void, void*>::call));
janjongboom 0:9fa3f3028773 144 return true;
janjongboom 0:9fa3f3028773 145 }
janjongboom 0:9fa3f3028773 146
janjongboom 0:9fa3f3028773 147 bool define_function(const char* route, execute_callback fn) {
janjongboom 0:9fa3f3028773 148 if (!define_resource_internal(route, string(), M2MBase::POST_ALLOWED, false)) {
janjongboom 0:9fa3f3028773 149 return false;
janjongboom 0:9fa3f3028773 150 }
janjongboom 0:9fa3f3028773 151
janjongboom 0:9fa3f3028773 152 string route_str(route);
janjongboom 0:9fa3f3028773 153 if (!resources.count(route_str)) {
janjongboom 0:9fa3f3028773 154 output.printf("[SMC] [ERROR] Should be created, but no such route (%s)\r\n", route);
janjongboom 0:9fa3f3028773 155 return false;
janjongboom 0:9fa3f3028773 156 }
janjongboom 0:9fa3f3028773 157 // No clue why this is not working?! It works with class member, but not with static function...
janjongboom 0:9fa3f3028773 158 resources[route_str]->set_execute_function(fn);
janjongboom 0:9fa3f3028773 159 return true;
janjongboom 0:9fa3f3028773 160 }
janjongboom 0:9fa3f3028773 161 //
janjongboom 0:9fa3f3028773 162 string get(string route_str) {
janjongboom 0:9fa3f3028773 163 if (!resources.count(route_str)) {
janjongboom 0:9fa3f3028773 164 output.printf("[SMC] [ERROR] No such route (%s)\r\n", route_str.c_str());
janjongboom 0:9fa3f3028773 165 return string();
janjongboom 0:9fa3f3028773 166 }
janjongboom 2:0a015df677a4 167
janjongboom 2:0a015df677a4 168 // otherwise ask mbed Client...
janjongboom 2:0a015df677a4 169 uint8_t* buffIn = NULL;
janjongboom 0:9fa3f3028773 170 uint32_t sizeIn;
janjongboom 0:9fa3f3028773 171 resources[route_str]->get_value(buffIn, sizeIn);
janjongboom 0:9fa3f3028773 172
janjongboom 2:0a015df677a4 173 string s((char*)buffIn, sizeIn);
janjongboom 0:9fa3f3028773 174 return s;
janjongboom 0:9fa3f3028773 175 }
janjongboom 0:9fa3f3028773 176
janjongboom 0:9fa3f3028773 177 bool set(string route_str, string v) {
janjongboom 2:0a015df677a4 178 // Potentially set() happens in InterruptContext. That's not good.
janjongboom 2:0a015df677a4 179 // so we put the set() in an updateQueue and then we'll see...
janjongboom 2:0a015df677a4 180
janjongboom 0:9fa3f3028773 181 if (!resources.count(route_str)) {
janjongboom 0:9fa3f3028773 182 output.printf("[SMC] [ERROR] No such route (%s)\r\n", route_str.c_str());
janjongboom 0:9fa3f3028773 183 return false;
janjongboom 0:9fa3f3028773 184 }
janjongboom 2:0a015df677a4 185
janjongboom 0:9fa3f3028773 186 resources[route_str]->set_value((uint8_t*)v.c_str(), v.length());
janjongboom 2:0a015df677a4 187
janjongboom 0:9fa3f3028773 188 return true;
janjongboom 0:9fa3f3028773 189 }
janjongboom 0:9fa3f3028773 190
janjongboom 0:9fa3f3028773 191 bool set(string route, const int& v) {
janjongboom 0:9fa3f3028773 192 stringstream ss;
janjongboom 0:9fa3f3028773 193 ss << v;
janjongboom 0:9fa3f3028773 194 std::string stringified = ss.str();
janjongboom 0:9fa3f3028773 195
janjongboom 0:9fa3f3028773 196 return set(route, stringified);
janjongboom 0:9fa3f3028773 197 }
janjongboom 0:9fa3f3028773 198
janjongboom 0:9fa3f3028773 199 bool define_resource_internal(const char* route, std::string v, M2MBase::Operation opr, bool observable) {
janjongboom 0:9fa3f3028773 200 if (client) {
janjongboom 0:9fa3f3028773 201 output.printf("[SMC] [ERROR] mbed_client_define_resource, Can only define resources before mbed_client_setup is called!\r\n");
janjongboom 0:9fa3f3028773 202 return false;
janjongboom 0:9fa3f3028773 203 }
janjongboom 0:9fa3f3028773 204
janjongboom 0:9fa3f3028773 205 vector<string> segments = parse_route(route);
janjongboom 0:9fa3f3028773 206 if (segments.size() != 3) {
janjongboom 0:9fa3f3028773 207 output.printf("[SMC] [ERROR] mbed_client_define_resource, Route needs to have three segments, split by '/' (%s)\r\n", route);
janjongboom 0:9fa3f3028773 208 return false;
janjongboom 0:9fa3f3028773 209 }
janjongboom 0:9fa3f3028773 210
janjongboom 0:9fa3f3028773 211 int inst_id = 0;
janjongboom 0:9fa3f3028773 212 // try {
janjongboom 0:9fa3f3028773 213 // inst_id = stoi(segments.at(1));
janjongboom 0:9fa3f3028773 214 // }
janjongboom 0:9fa3f3028773 215 // catch (const std::invalid_argument& ia) {
janjongboom 0:9fa3f3028773 216 // output.printf("[SMC] [ERROR] mbed_client_define_resource, second route segment should be numeric, but was not (%s)\r\n", route);
janjongboom 0:9fa3f3028773 217 // return false;
janjongboom 0:9fa3f3028773 218 // }
janjongboom 0:9fa3f3028773 219
janjongboom 0:9fa3f3028773 220
janjongboom 0:9fa3f3028773 221 M2MObjectInstance* inst;
janjongboom 0:9fa3f3028773 222 if (objectInstances.count(segments.at(0))) {
janjongboom 0:9fa3f3028773 223 output.printf("Found object... %s\r\n", segments.at(0).c_str());
janjongboom 0:9fa3f3028773 224 inst = objectInstances[segments.at(0)];
janjongboom 0:9fa3f3028773 225 }
janjongboom 0:9fa3f3028773 226 else {
janjongboom 0:9fa3f3028773 227 output.printf("Create new object... %s\r\n", segments.at(0).c_str());
janjongboom 0:9fa3f3028773 228 M2MObject* obj = M2MInterfaceFactory::create_object(segments.at(0).c_str());
janjongboom 0:9fa3f3028773 229 inst = obj->create_object_instance(inst_id);
janjongboom 0:9fa3f3028773 230 objects.insert(std::pair<string, M2MObject*>(segments.at(0), obj));
janjongboom 0:9fa3f3028773 231 objectInstances.insert(std::pair<string, M2MObjectInstance*>(segments.at(0), inst));
janjongboom 0:9fa3f3028773 232 }
janjongboom 0:9fa3f3028773 233
janjongboom 0:9fa3f3028773 234 // @todo check if the resource exists yet
janjongboom 0:9fa3f3028773 235 M2MResource* res = inst->create_dynamic_resource(segments.at(2).c_str(), "",
janjongboom 0:9fa3f3028773 236 M2MResourceInstance::STRING, observable);
janjongboom 0:9fa3f3028773 237 res->set_operation(opr);
janjongboom 0:9fa3f3028773 238 res->set_value((uint8_t*)v.c_str(), v.length());
janjongboom 0:9fa3f3028773 239
janjongboom 0:9fa3f3028773 240 string route_str(route);
janjongboom 2:0a015df677a4 241 resources.insert(pair<string, M2MResource*>(route_str, res));
janjongboom 2:0a015df677a4 242
janjongboom 0:9fa3f3028773 243 return true;
janjongboom 0:9fa3f3028773 244 }
janjongboom 2:0a015df677a4 245
janjongboom 2:0a015df677a4 246 void keep_alive() {
janjongboom 2:0a015df677a4 247 client->test_update_register();
janjongboom 2:0a015df677a4 248 }
janjongboom 2:0a015df677a4 249
janjongboom 0:9fa3f3028773 250 private:
janjongboom 0:9fa3f3028773 251 vector<string> parse_route(const char* route) {
janjongboom 0:9fa3f3028773 252 string s(route);
janjongboom 0:9fa3f3028773 253 vector<string> v;
janjongboom 0:9fa3f3028773 254 stringstream ss(s);
janjongboom 0:9fa3f3028773 255 string item;
janjongboom 0:9fa3f3028773 256 while (getline(ss, item, '/')) {
janjongboom 0:9fa3f3028773 257 v.push_back(item);
janjongboom 0:9fa3f3028773 258 }
janjongboom 0:9fa3f3028773 259 return v;
janjongboom 0:9fa3f3028773 260 }
janjongboom 0:9fa3f3028773 261
janjongboom 0:9fa3f3028773 262 Serial output;
janjongboom 0:9fa3f3028773 263
janjongboom 0:9fa3f3028773 264 MbedClient* client;
janjongboom 0:9fa3f3028773 265 map<string, M2MObject*> objects;
janjongboom 0:9fa3f3028773 266 map<string, M2MObjectInstance*> objectInstances;
janjongboom 0:9fa3f3028773 267 map<string, M2MResource*> resources;
janjongboom 2:0a015df677a4 268
janjongboom 1:75015f627e89 269 // @todo: write this
janjongboom 1:75015f627e89 270 // map<string, FunctionPointerArg1<void, void*> updateValues;
janjongboom 0:9fa3f3028773 271 };
janjongboom 0:9fa3f3028773 272
janjongboom 0:9fa3f3028773 273 class SimpleResourceString {
janjongboom 0:9fa3f3028773 274 public:
janjongboom 0:9fa3f3028773 275 SimpleResourceString(SimpleMbedClientBase* aSimpleClient, string aRoute) :
janjongboom 0:9fa3f3028773 276 simpleClient(aSimpleClient), route(aRoute) {}
janjongboom 0:9fa3f3028773 277
janjongboom 0:9fa3f3028773 278 string operator=(const string& newValue) {
janjongboom 0:9fa3f3028773 279 simpleClient->set(route, newValue);
janjongboom 0:9fa3f3028773 280 return newValue;
janjongboom 0:9fa3f3028773 281 };
janjongboom 0:9fa3f3028773 282 operator string() const {
janjongboom 0:9fa3f3028773 283 return simpleClient->get(route);
janjongboom 0:9fa3f3028773 284 };
janjongboom 0:9fa3f3028773 285
janjongboom 0:9fa3f3028773 286 private:
janjongboom 0:9fa3f3028773 287 SimpleMbedClientBase* simpleClient;
janjongboom 0:9fa3f3028773 288 string route;
janjongboom 0:9fa3f3028773 289 };
janjongboom 0:9fa3f3028773 290
janjongboom 0:9fa3f3028773 291 class SimpleResourceInt {
janjongboom 0:9fa3f3028773 292 public:
janjongboom 0:9fa3f3028773 293 SimpleResourceInt(SimpleMbedClientBase* aSimpleClient, string aRoute) :
janjongboom 0:9fa3f3028773 294 simpleClient(aSimpleClient), route(aRoute) {}
janjongboom 0:9fa3f3028773 295
janjongboom 0:9fa3f3028773 296 int operator=(int newValue) {
janjongboom 0:9fa3f3028773 297 simpleClient->set(route, newValue);
janjongboom 0:9fa3f3028773 298 return newValue;
janjongboom 0:9fa3f3028773 299 };
janjongboom 0:9fa3f3028773 300 operator int() const {
janjongboom 0:9fa3f3028773 301 string v = simpleClient->get(route);
janjongboom 0:9fa3f3028773 302 if (v.empty()) return 0;
janjongboom 0:9fa3f3028773 303
janjongboom 0:9fa3f3028773 304 return atoi((const char*)v.c_str());
janjongboom 0:9fa3f3028773 305 };
janjongboom 0:9fa3f3028773 306
janjongboom 0:9fa3f3028773 307 private:
janjongboom 0:9fa3f3028773 308 SimpleMbedClientBase* simpleClient;
janjongboom 0:9fa3f3028773 309 string route;
janjongboom 0:9fa3f3028773 310 };
janjongboom 0:9fa3f3028773 311
janjongboom 0:9fa3f3028773 312 class SimpleMbedClient : public SimpleMbedClientBase {
janjongboom 0:9fa3f3028773 313 public:
janjongboom 1:75015f627e89 314
janjongboom 1:75015f627e89 315 // @todo: macro this up
janjongboom 1:75015f627e89 316
janjongboom 1:75015f627e89 317 SimpleResourceString define_resource(
janjongboom 1:75015f627e89 318 const char* route,
janjongboom 1:75015f627e89 319 string v,
janjongboom 1:75015f627e89 320 M2MBase::Operation opr = M2MBase::GET_PUT_ALLOWED,
janjongboom 1:75015f627e89 321 bool observable = true,
janjongboom 1:75015f627e89 322 FunctionPointerArg1<void, string> onUpdate = NULL)
janjongboom 1:75015f627e89 323 {
janjongboom 0:9fa3f3028773 324 bool res = define_resource_internal(route, v, opr, observable);
janjongboom 0:9fa3f3028773 325 if (!res) printf("Error while creating %s\n", route);
janjongboom 0:9fa3f3028773 326 return *(new SimpleResourceString(this, route));
janjongboom 0:9fa3f3028773 327 }
janjongboom 1:75015f627e89 328
janjongboom 1:75015f627e89 329 SimpleResourceString define_resource(
janjongboom 1:75015f627e89 330 const char* route,
janjongboom 1:75015f627e89 331 string v,
janjongboom 1:75015f627e89 332 M2MBase::Operation opr,
janjongboom 1:75015f627e89 333 bool observable,
janjongboom 1:75015f627e89 334 void(*onUpdate)(string))
janjongboom 1:75015f627e89 335 {
janjongboom 1:75015f627e89 336 FunctionPointerArg1<void, string> fp;
janjongboom 1:75015f627e89 337 fp.attach(onUpdate);
janjongboom 1:75015f627e89 338 return define_resource(route, v, opr, observable, fp);
janjongboom 1:75015f627e89 339 }
janjongboom 0:9fa3f3028773 340
janjongboom 1:75015f627e89 341 SimpleResourceString define_resource(
janjongboom 1:75015f627e89 342 const char* route,
janjongboom 1:75015f627e89 343 string v,
janjongboom 1:75015f627e89 344 FunctionPointerArg1<void, string> onUpdate = NULL)
janjongboom 1:75015f627e89 345 {
janjongboom 1:75015f627e89 346 return define_resource(route, v, M2MBase::GET_PUT_ALLOWED, true, onUpdate);
janjongboom 1:75015f627e89 347 }
janjongboom 1:75015f627e89 348
janjongboom 1:75015f627e89 349 SimpleResourceString define_resource(
janjongboom 1:75015f627e89 350 const char* route,
janjongboom 1:75015f627e89 351 string v,
janjongboom 1:75015f627e89 352 void(*onUpdate)(string))
janjongboom 1:75015f627e89 353 {
janjongboom 1:75015f627e89 354 FunctionPointerArg1<void, string> fp;
janjongboom 1:75015f627e89 355 fp.attach(onUpdate);
janjongboom 1:75015f627e89 356 return define_resource(route, v, M2MBase::GET_PUT_ALLOWED, true, fp);
janjongboom 1:75015f627e89 357 }
janjongboom 1:75015f627e89 358
janjongboom 1:75015f627e89 359 SimpleResourceInt define_resource(
janjongboom 1:75015f627e89 360 const char* route,
janjongboom 1:75015f627e89 361 int v,
janjongboom 1:75015f627e89 362 M2MBase::Operation opr = M2MBase::GET_PUT_ALLOWED,
janjongboom 1:75015f627e89 363 bool observable = true,
janjongboom 1:75015f627e89 364 FunctionPointerArg1<void, int> onUpdate = NULL)
janjongboom 1:75015f627e89 365 {
janjongboom 0:9fa3f3028773 366 stringstream ss;
janjongboom 0:9fa3f3028773 367 ss << v;
janjongboom 0:9fa3f3028773 368 std::string stringified = ss.str();
janjongboom 0:9fa3f3028773 369 bool res = define_resource_internal(route, stringified, opr, observable);
janjongboom 0:9fa3f3028773 370 if (!res) printf("Error while creating %s\n", route);
janjongboom 0:9fa3f3028773 371 return *(new SimpleResourceInt(this, route));
janjongboom 0:9fa3f3028773 372 }
janjongboom 1:75015f627e89 373
janjongboom 1:75015f627e89 374 SimpleResourceInt define_resource(
janjongboom 1:75015f627e89 375 const char* route,
janjongboom 1:75015f627e89 376 int v,
janjongboom 1:75015f627e89 377 M2MBase::Operation opr,
janjongboom 1:75015f627e89 378 bool observable,
janjongboom 1:75015f627e89 379 void(*onUpdate)(int))
janjongboom 1:75015f627e89 380 {
janjongboom 1:75015f627e89 381 FunctionPointerArg1<void, int> fp;
janjongboom 1:75015f627e89 382 fp.attach(onUpdate);
janjongboom 1:75015f627e89 383 return define_resource(route, v, opr, observable, fp);
janjongboom 1:75015f627e89 384 }
janjongboom 1:75015f627e89 385
janjongboom 1:75015f627e89 386 SimpleResourceInt define_resource(
janjongboom 1:75015f627e89 387 const char* route,
janjongboom 1:75015f627e89 388 int v,
janjongboom 1:75015f627e89 389 FunctionPointerArg1<void, int> onUpdate = NULL)
janjongboom 1:75015f627e89 390 {
janjongboom 1:75015f627e89 391 return define_resource(route, v, M2MBase::GET_PUT_ALLOWED, true, onUpdate);
janjongboom 1:75015f627e89 392 }
janjongboom 1:75015f627e89 393
janjongboom 1:75015f627e89 394 SimpleResourceInt define_resource(
janjongboom 1:75015f627e89 395 const char* route,
janjongboom 1:75015f627e89 396 int v,
janjongboom 1:75015f627e89 397 void(*onUpdate)(int))
janjongboom 1:75015f627e89 398 {
janjongboom 1:75015f627e89 399 FunctionPointerArg1<void, int> fp;
janjongboom 1:75015f627e89 400 fp.attach(onUpdate);
janjongboom 1:75015f627e89 401 return define_resource(route, v, M2MBase::GET_PUT_ALLOWED, true, fp);
janjongboom 1:75015f627e89 402 }
janjongboom 0:9fa3f3028773 403 };
janjongboom 0:9fa3f3028773 404
janjongboom 0:9fa3f3028773 405 #endif // __SIMPLE_MBED_CLIENT_H__