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:
Sun May 15 20:02:58 2016 +0000
Revision:
0:9fa3f3028773
Child:
1:75015f627e89
Port of mbed-client-ethernet-c-style to mbed Classic

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 0:9fa3f3028773 167
janjongboom 0:9fa3f3028773 168 uint8_t* buffIn;
janjongboom 0:9fa3f3028773 169 uint32_t sizeIn;
janjongboom 0:9fa3f3028773 170 resources[route_str]->get_value(buffIn, sizeIn);
janjongboom 0:9fa3f3028773 171
janjongboom 0:9fa3f3028773 172 std::string s((char*)buffIn, sizeIn);
janjongboom 0:9fa3f3028773 173 return s;
janjongboom 0:9fa3f3028773 174 }
janjongboom 0:9fa3f3028773 175
janjongboom 0:9fa3f3028773 176 bool set(string route_str, string v) {
janjongboom 0:9fa3f3028773 177 if (!resources.count(route_str)) {
janjongboom 0:9fa3f3028773 178 output.printf("[SMC] [ERROR] No such route (%s)\r\n", route_str.c_str());
janjongboom 0:9fa3f3028773 179 return false;
janjongboom 0:9fa3f3028773 180 }
janjongboom 0:9fa3f3028773 181 resources[route_str]->set_value((uint8_t*)v.c_str(), v.length());
janjongboom 0:9fa3f3028773 182 return true;
janjongboom 0:9fa3f3028773 183 }
janjongboom 0:9fa3f3028773 184
janjongboom 0:9fa3f3028773 185 bool set(string route, const int& v) {
janjongboom 0:9fa3f3028773 186 stringstream ss;
janjongboom 0:9fa3f3028773 187 ss << v;
janjongboom 0:9fa3f3028773 188 std::string stringified = ss.str();
janjongboom 0:9fa3f3028773 189
janjongboom 0:9fa3f3028773 190 return set(route, stringified);
janjongboom 0:9fa3f3028773 191 }
janjongboom 0:9fa3f3028773 192
janjongboom 0:9fa3f3028773 193 bool define_resource_internal(const char* route, std::string v, M2MBase::Operation opr, bool observable) {
janjongboom 0:9fa3f3028773 194 if (client) {
janjongboom 0:9fa3f3028773 195 output.printf("[SMC] [ERROR] mbed_client_define_resource, Can only define resources before mbed_client_setup is called!\r\n");
janjongboom 0:9fa3f3028773 196 return false;
janjongboom 0:9fa3f3028773 197 }
janjongboom 0:9fa3f3028773 198
janjongboom 0:9fa3f3028773 199 vector<string> segments = parse_route(route);
janjongboom 0:9fa3f3028773 200 if (segments.size() != 3) {
janjongboom 0:9fa3f3028773 201 output.printf("[SMC] [ERROR] mbed_client_define_resource, Route needs to have three segments, split by '/' (%s)\r\n", route);
janjongboom 0:9fa3f3028773 202 return false;
janjongboom 0:9fa3f3028773 203 }
janjongboom 0:9fa3f3028773 204
janjongboom 0:9fa3f3028773 205 int inst_id = 0;
janjongboom 0:9fa3f3028773 206 // try {
janjongboom 0:9fa3f3028773 207 // inst_id = stoi(segments.at(1));
janjongboom 0:9fa3f3028773 208 // }
janjongboom 0:9fa3f3028773 209 // catch (const std::invalid_argument& ia) {
janjongboom 0:9fa3f3028773 210 // output.printf("[SMC] [ERROR] mbed_client_define_resource, second route segment should be numeric, but was not (%s)\r\n", route);
janjongboom 0:9fa3f3028773 211 // return false;
janjongboom 0:9fa3f3028773 212 // }
janjongboom 0:9fa3f3028773 213
janjongboom 0:9fa3f3028773 214
janjongboom 0:9fa3f3028773 215 M2MObjectInstance* inst;
janjongboom 0:9fa3f3028773 216 if (objectInstances.count(segments.at(0))) {
janjongboom 0:9fa3f3028773 217 output.printf("Found object... %s\r\n", segments.at(0).c_str());
janjongboom 0:9fa3f3028773 218 inst = objectInstances[segments.at(0)];
janjongboom 0:9fa3f3028773 219 }
janjongboom 0:9fa3f3028773 220 else {
janjongboom 0:9fa3f3028773 221 output.printf("Create new object... %s\r\n", segments.at(0).c_str());
janjongboom 0:9fa3f3028773 222 M2MObject* obj = M2MInterfaceFactory::create_object(segments.at(0).c_str());
janjongboom 0:9fa3f3028773 223 inst = obj->create_object_instance(inst_id);
janjongboom 0:9fa3f3028773 224 objects.insert(std::pair<string, M2MObject*>(segments.at(0), obj));
janjongboom 0:9fa3f3028773 225 objectInstances.insert(std::pair<string, M2MObjectInstance*>(segments.at(0), inst));
janjongboom 0:9fa3f3028773 226 }
janjongboom 0:9fa3f3028773 227
janjongboom 0:9fa3f3028773 228 // @todo check if the resource exists yet
janjongboom 0:9fa3f3028773 229 M2MResource* res = inst->create_dynamic_resource(segments.at(2).c_str(), "",
janjongboom 0:9fa3f3028773 230 M2MResourceInstance::STRING, observable);
janjongboom 0:9fa3f3028773 231 res->set_operation(opr);
janjongboom 0:9fa3f3028773 232 res->set_value((uint8_t*)v.c_str(), v.length());
janjongboom 0:9fa3f3028773 233
janjongboom 0:9fa3f3028773 234 string route_str(route);
janjongboom 0:9fa3f3028773 235 resources.insert(std::pair<string, M2MResource*>(route_str, res));
janjongboom 0:9fa3f3028773 236
janjongboom 0:9fa3f3028773 237 return true;
janjongboom 0:9fa3f3028773 238 }
janjongboom 0:9fa3f3028773 239
janjongboom 0:9fa3f3028773 240 private:
janjongboom 0:9fa3f3028773 241 vector<string> parse_route(const char* route) {
janjongboom 0:9fa3f3028773 242 string s(route);
janjongboom 0:9fa3f3028773 243 vector<string> v;
janjongboom 0:9fa3f3028773 244 stringstream ss(s);
janjongboom 0:9fa3f3028773 245 string item;
janjongboom 0:9fa3f3028773 246 while (getline(ss, item, '/')) {
janjongboom 0:9fa3f3028773 247 v.push_back(item);
janjongboom 0:9fa3f3028773 248 }
janjongboom 0:9fa3f3028773 249 return v;
janjongboom 0:9fa3f3028773 250 }
janjongboom 0:9fa3f3028773 251
janjongboom 0:9fa3f3028773 252 Serial output;
janjongboom 0:9fa3f3028773 253
janjongboom 0:9fa3f3028773 254 MbedClient* client;
janjongboom 0:9fa3f3028773 255 map<string, M2MObject*> objects;
janjongboom 0:9fa3f3028773 256 map<string, M2MObjectInstance*> objectInstances;
janjongboom 0:9fa3f3028773 257 map<string, M2MResource*> resources;
janjongboom 0:9fa3f3028773 258 };
janjongboom 0:9fa3f3028773 259
janjongboom 0:9fa3f3028773 260 class SimpleResourceString {
janjongboom 0:9fa3f3028773 261 public:
janjongboom 0:9fa3f3028773 262 SimpleResourceString(SimpleMbedClientBase* aSimpleClient, string aRoute) :
janjongboom 0:9fa3f3028773 263 simpleClient(aSimpleClient), route(aRoute) {}
janjongboom 0:9fa3f3028773 264
janjongboom 0:9fa3f3028773 265 string operator=(const string& newValue) {
janjongboom 0:9fa3f3028773 266 simpleClient->set(route, newValue);
janjongboom 0:9fa3f3028773 267 return newValue;
janjongboom 0:9fa3f3028773 268 };
janjongboom 0:9fa3f3028773 269 operator string() const {
janjongboom 0:9fa3f3028773 270 return simpleClient->get(route);
janjongboom 0:9fa3f3028773 271 };
janjongboom 0:9fa3f3028773 272
janjongboom 0:9fa3f3028773 273 private:
janjongboom 0:9fa3f3028773 274 SimpleMbedClientBase* simpleClient;
janjongboom 0:9fa3f3028773 275 string route;
janjongboom 0:9fa3f3028773 276 };
janjongboom 0:9fa3f3028773 277
janjongboom 0:9fa3f3028773 278 class SimpleResourceInt {
janjongboom 0:9fa3f3028773 279 public:
janjongboom 0:9fa3f3028773 280 SimpleResourceInt(SimpleMbedClientBase* aSimpleClient, string aRoute) :
janjongboom 0:9fa3f3028773 281 simpleClient(aSimpleClient), route(aRoute) {}
janjongboom 0:9fa3f3028773 282
janjongboom 0:9fa3f3028773 283 int operator=(int newValue) {
janjongboom 0:9fa3f3028773 284 // printf("newValue %s = %d\n", route.c_str(), newValue);
janjongboom 0:9fa3f3028773 285 simpleClient->set(route, newValue);
janjongboom 0:9fa3f3028773 286 return newValue;
janjongboom 0:9fa3f3028773 287 };
janjongboom 0:9fa3f3028773 288 operator int() const {
janjongboom 0:9fa3f3028773 289 // printf("receiving a value for %s\n", route.c_str());
janjongboom 0:9fa3f3028773 290 string v = simpleClient->get(route);
janjongboom 0:9fa3f3028773 291 // printf("string value is %s\n", v.c_str());
janjongboom 0:9fa3f3028773 292 if (v.empty()) return 0;
janjongboom 0:9fa3f3028773 293
janjongboom 0:9fa3f3028773 294 return atoi((const char*)v.c_str());
janjongboom 0:9fa3f3028773 295 };
janjongboom 0:9fa3f3028773 296
janjongboom 0:9fa3f3028773 297 private:
janjongboom 0:9fa3f3028773 298 SimpleMbedClientBase* simpleClient;
janjongboom 0:9fa3f3028773 299 string route;
janjongboom 0:9fa3f3028773 300 };
janjongboom 0:9fa3f3028773 301
janjongboom 0:9fa3f3028773 302 class SimpleMbedClient : public SimpleMbedClientBase {
janjongboom 0:9fa3f3028773 303 public:
janjongboom 0:9fa3f3028773 304 SimpleResourceString define_resource(const char* route, string v, M2MBase::Operation opr, bool observable) {
janjongboom 0:9fa3f3028773 305 bool res = define_resource_internal(route, v, opr, observable);
janjongboom 0:9fa3f3028773 306 if (!res) printf("Error while creating %s\n", route);
janjongboom 0:9fa3f3028773 307 return *(new SimpleResourceString(this, route));
janjongboom 0:9fa3f3028773 308 }
janjongboom 0:9fa3f3028773 309
janjongboom 0:9fa3f3028773 310 SimpleResourceInt define_resource(const char* route, int v, M2MBase::Operation opr, bool observable) {
janjongboom 0:9fa3f3028773 311 stringstream ss;
janjongboom 0:9fa3f3028773 312 ss << v;
janjongboom 0:9fa3f3028773 313 std::string stringified = ss.str();
janjongboom 0:9fa3f3028773 314 bool res = define_resource_internal(route, stringified, opr, observable);
janjongboom 0:9fa3f3028773 315 if (!res) printf("Error while creating %s\n", route);
janjongboom 0:9fa3f3028773 316 return *(new SimpleResourceInt(this, route));
janjongboom 0:9fa3f3028773 317 }
janjongboom 0:9fa3f3028773 318 };
janjongboom 0:9fa3f3028773 319
janjongboom 0:9fa3f3028773 320 #endif // __SIMPLE_MBED_CLIENT_H__