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:
Jan Jongboom
Date:
Tue Mar 21 13:03:49 2017 +0100
Revision:
23:c89df15e88d2
Parent:
12:26810c6b58e1
Update mbed-client, works with mbed OS 5.4.1

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_WRAPPER_H__
janjongboom 0:9fa3f3028773 18 #define __SIMPLE_MBED_CLIENT_WRAPPER_H__
janjongboom 0:9fa3f3028773 19
janjongboom 0:9fa3f3028773 20 #include "mbed-client/m2minterfacefactory.h"
janjongboom 0:9fa3f3028773 21 #include "mbed-client/m2mdevice.h"
janjongboom 0:9fa3f3028773 22 #include "mbed-client/m2minterfaceobserver.h"
janjongboom 0:9fa3f3028773 23 #include "mbed-client/m2minterface.h"
janjongboom 0:9fa3f3028773 24 #include "mbed-client/m2mobjectinstance.h"
janjongboom 0:9fa3f3028773 25 #include "mbed-client/m2mresource.h"
janjongboom 0:9fa3f3028773 26
janjongboom 0:9fa3f3028773 27 struct MbedClientOptions {
janjongboom 0:9fa3f3028773 28 const char* Manufacturer;
janjongboom 0:9fa3f3028773 29 const char* Type;
janjongboom 0:9fa3f3028773 30 const char* ModelNumber;
janjongboom 0:9fa3f3028773 31 const char* SerialNumber;
janjongboom 0:9fa3f3028773 32 const char* DeviceType;
janjongboom 0:9fa3f3028773 33 M2MInterface::BindingMode SocketMode;
janjongboom 0:9fa3f3028773 34 const char* ServerAddress;
janjongboom 0:9fa3f3028773 35 };
janjongboom 0:9fa3f3028773 36
janjongboom 0:9fa3f3028773 37 /*
janjongboom 0:9fa3f3028773 38 * Wrapper for mbed client stack that handles all callbacks, error handling, and
janjongboom 0:9fa3f3028773 39 * other schenanigans to make the mbed client stack easier to use.
janjongboom 0:9fa3f3028773 40 *
janjongboom 0:9fa3f3028773 41 * The end user should only have to care about configuring the parameters at the
janjongboom 0:9fa3f3028773 42 * top of this file and making sure they add the security.h file correctly.
janjongboom 0:9fa3f3028773 43 * To add resources you can copy the _TODO__ function and add as many instances as
janjongboom 0:9fa3f3028773 44 * you want.
janjongboom 0:9fa3f3028773 45 *
janjongboom 0:9fa3f3028773 46 */
janjongboom 0:9fa3f3028773 47 class MbedClient: public M2MInterfaceObserver {
janjongboom 0:9fa3f3028773 48 public:
janjongboom 0:9fa3f3028773 49 // constructor for MbedClient object, initialize private variables
Jan Jongboom 12:26810c6b58e1 50 MbedClient(struct MbedClientOptions options, Callback<void(string)> onValueChanged, bool debug) :
Jan Jongboom 3:ce2322965a27 51 _onValueChanged(onValueChanged), _debug(debug)
Jan Jongboom 3:ce2322965a27 52 {
janjongboom 0:9fa3f3028773 53 _interface = NULL;
janjongboom 0:9fa3f3028773 54 _bootstrapped = false;
janjongboom 0:9fa3f3028773 55 _error = false;
janjongboom 0:9fa3f3028773 56 _registered = false;
janjongboom 0:9fa3f3028773 57 _unregistered = false;
janjongboom 0:9fa3f3028773 58 _register_security = NULL;
janjongboom 0:9fa3f3028773 59 _value = 0;
janjongboom 0:9fa3f3028773 60 _object = NULL;
janjongboom 0:9fa3f3028773 61 _options = options;
janjongboom 0:9fa3f3028773 62 _onRegistered = NULL;
janjongboom 0:9fa3f3028773 63 _onUnregistered = NULL;
janjongboom 0:9fa3f3028773 64 }
janjongboom 0:9fa3f3028773 65
janjongboom 0:9fa3f3028773 66 // de-constructor for MbedClient object, you can ignore this
janjongboom 0:9fa3f3028773 67 ~MbedClient() {
janjongboom 0:9fa3f3028773 68 if(_interface) {
janjongboom 0:9fa3f3028773 69 delete _interface;
janjongboom 0:9fa3f3028773 70 }
janjongboom 0:9fa3f3028773 71 if(_register_security){
janjongboom 0:9fa3f3028773 72 delete _register_security;
janjongboom 0:9fa3f3028773 73 }
janjongboom 0:9fa3f3028773 74 }
janjongboom 0:9fa3f3028773 75
janjongboom 0:9fa3f3028773 76 // debug printf function
janjongboom 0:9fa3f3028773 77 void trace_printer(const char* str) {
Jan Jongboom 3:ce2322965a27 78 if (_debug) printf("[SMC] %s\r\n", str);
janjongboom 0:9fa3f3028773 79 }
janjongboom 0:9fa3f3028773 80
Jan Jongboom 12:26810c6b58e1 81 void set_registered_function(Callback<void()> onRegistered) {
janjongboom 0:9fa3f3028773 82 _onRegistered = onRegistered;
janjongboom 0:9fa3f3028773 83 }
janjongboom 0:9fa3f3028773 84
Jan Jongboom 12:26810c6b58e1 85 void set_unregistered_function(Callback<void()> onUnregistered) {
janjongboom 0:9fa3f3028773 86 _onUnregistered = onUnregistered;
janjongboom 0:9fa3f3028773 87 }
janjongboom 0:9fa3f3028773 88
janjongboom 0:9fa3f3028773 89 /*
janjongboom 0:9fa3f3028773 90 * Creates M2MInterface using which endpoint can
janjongboom 0:9fa3f3028773 91 * setup its name, resource type, life time, connection mode,
janjongboom 0:9fa3f3028773 92 * Currently only LwIPv4 is supported.
janjongboom 0:9fa3f3028773 93 */
Jan Jongboom 4:0f9eae5739dd 94 void create_interface(NetworkInterface* iface) {
Jan Jongboom 3:ce2322965a27 95 // Randomizing listening port for Certificate mode connectivity
Jan Jongboom 3:ce2322965a27 96 srand(time(NULL));
Jan Jongboom 3:ce2322965a27 97 uint16_t port = rand() % 65535 + 12345;
janjongboom 0:9fa3f3028773 98
Jan Jongboom 3:ce2322965a27 99 // create mDS interface object, this is the base object everything else attaches to
Jan Jongboom 3:ce2322965a27 100 _interface = M2MInterfaceFactory::create_interface(*this,
Jan Jongboom 3:ce2322965a27 101 MBED_ENDPOINT_NAME, // endpoint name string
Jan Jongboom 3:ce2322965a27 102 _options.DeviceType, // endpoint type string
Jan Jongboom 3:ce2322965a27 103 100, // lifetime
Jan Jongboom 3:ce2322965a27 104 port, // listen port
Jan Jongboom 3:ce2322965a27 105 MBED_DOMAIN, // domain string
Jan Jongboom 3:ce2322965a27 106 _options.SocketMode, // binding mode
Jan Jongboom 3:ce2322965a27 107 M2MInterface::LwIP_IPv4, // network stack
Jan Jongboom 3:ce2322965a27 108 ""); // context address string
Jan Jongboom 3:ce2322965a27 109
Jan Jongboom 3:ce2322965a27 110 _interface->set_platform_network_handler((void*)iface);
janjongboom 0:9fa3f3028773 111 }
janjongboom 0:9fa3f3028773 112
janjongboom 0:9fa3f3028773 113 /*
janjongboom 0:9fa3f3028773 114 * check private variable to see if the registration was sucessful or not
janjongboom 0:9fa3f3028773 115 */
janjongboom 0:9fa3f3028773 116 bool register_successful() {
janjongboom 0:9fa3f3028773 117 return _registered;
janjongboom 0:9fa3f3028773 118 }
janjongboom 0:9fa3f3028773 119
janjongboom 0:9fa3f3028773 120 /*
janjongboom 0:9fa3f3028773 121 * check private variable to see if un-registration was sucessful or not
janjongboom 0:9fa3f3028773 122 */
janjongboom 0:9fa3f3028773 123 bool unregister_successful() {
janjongboom 0:9fa3f3028773 124 return _unregistered;
janjongboom 0:9fa3f3028773 125 }
janjongboom 0:9fa3f3028773 126
janjongboom 0:9fa3f3028773 127 /*
janjongboom 0:9fa3f3028773 128 * Creates register server object with mbed device server address and other parameters
janjongboom 0:9fa3f3028773 129 * required for client to connect to mbed device server.
janjongboom 0:9fa3f3028773 130 */
janjongboom 0:9fa3f3028773 131 M2MSecurity* create_register_object() {
janjongboom 0:9fa3f3028773 132 // create security object using the interface factory.
janjongboom 0:9fa3f3028773 133 // this will generate a security ObjectID and ObjectInstance
janjongboom 0:9fa3f3028773 134 M2MSecurity *security = M2MInterfaceFactory::create_security(M2MSecurity::M2MServer);
janjongboom 0:9fa3f3028773 135
janjongboom 0:9fa3f3028773 136 // make sure security ObjectID/ObjectInstance was created successfully
janjongboom 0:9fa3f3028773 137 if(security) {
janjongboom 0:9fa3f3028773 138 // Add ResourceID's and values to the security ObjectID/ObjectInstance
janjongboom 0:9fa3f3028773 139 security->set_resource_value(M2MSecurity::M2MServerUri, _options.ServerAddress);
janjongboom 0:9fa3f3028773 140 security->set_resource_value(M2MSecurity::SecurityMode, M2MSecurity::Certificate);
janjongboom 0:9fa3f3028773 141 security->set_resource_value(M2MSecurity::ServerPublicKey, SERVER_CERT, sizeof(SERVER_CERT));
janjongboom 0:9fa3f3028773 142 security->set_resource_value(M2MSecurity::PublicKey, CERT, sizeof(CERT));
janjongboom 0:9fa3f3028773 143 security->set_resource_value(M2MSecurity::Secretkey, KEY, sizeof(KEY));
janjongboom 0:9fa3f3028773 144 }
janjongboom 0:9fa3f3028773 145 return security;
janjongboom 0:9fa3f3028773 146 }
janjongboom 0:9fa3f3028773 147
janjongboom 0:9fa3f3028773 148 /*
janjongboom 0:9fa3f3028773 149 * Creates device object which contains mandatory resources linked with
janjongboom 0:9fa3f3028773 150 * device endpoint.
janjongboom 0:9fa3f3028773 151 */
janjongboom 0:9fa3f3028773 152 M2MDevice* create_device_object() {
janjongboom 0:9fa3f3028773 153 // create device objectID/ObjectInstance
janjongboom 0:9fa3f3028773 154 M2MDevice *device = M2MInterfaceFactory::create_device();
janjongboom 0:9fa3f3028773 155 // make sure device object was created successfully
janjongboom 0:9fa3f3028773 156 if(device) {
janjongboom 0:9fa3f3028773 157 // add resourceID's to device objectID/ObjectInstance
janjongboom 0:9fa3f3028773 158 device->create_resource(M2MDevice::Manufacturer, _options.Manufacturer);
janjongboom 0:9fa3f3028773 159 device->create_resource(M2MDevice::DeviceType, _options.Type);
janjongboom 0:9fa3f3028773 160 device->create_resource(M2MDevice::ModelNumber, _options.ModelNumber);
janjongboom 0:9fa3f3028773 161 device->create_resource(M2MDevice::SerialNumber, _options.SerialNumber);
janjongboom 0:9fa3f3028773 162 }
janjongboom 0:9fa3f3028773 163 return device;
janjongboom 0:9fa3f3028773 164 }
janjongboom 0:9fa3f3028773 165
janjongboom 0:9fa3f3028773 166 /*
janjongboom 0:9fa3f3028773 167 * register an object
janjongboom 0:9fa3f3028773 168 */
janjongboom 0:9fa3f3028773 169 void test_register(M2MSecurity *register_object, M2MObjectList object_list){
janjongboom 0:9fa3f3028773 170 if(_interface) {
janjongboom 0:9fa3f3028773 171 // Register function
janjongboom 0:9fa3f3028773 172 _interface->register_object(register_object, object_list);
janjongboom 0:9fa3f3028773 173 _registered = true;
janjongboom 0:9fa3f3028773 174 }
janjongboom 0:9fa3f3028773 175 }
janjongboom 0:9fa3f3028773 176
janjongboom 0:9fa3f3028773 177 /*
janjongboom 0:9fa3f3028773 178 * unregister all objects
janjongboom 0:9fa3f3028773 179 */
janjongboom 0:9fa3f3028773 180 void test_unregister() {
janjongboom 0:9fa3f3028773 181 if(_interface) {
janjongboom 0:9fa3f3028773 182 // Unregister function
janjongboom 0:9fa3f3028773 183 _interface->unregister_object(NULL); // NULL will unregister all objects
janjongboom 0:9fa3f3028773 184 }
janjongboom 0:9fa3f3028773 185 }
janjongboom 0:9fa3f3028773 186
janjongboom 0:9fa3f3028773 187 //Callback from mbed client stack when the bootstrap
janjongboom 0:9fa3f3028773 188 // is successful, it returns the mbed Device Server object
janjongboom 0:9fa3f3028773 189 // which will be used for registering the resources to
janjongboom 0:9fa3f3028773 190 // mbed Device server.
janjongboom 0:9fa3f3028773 191 virtual void bootstrap_done(M2MSecurity *server_object){
janjongboom 0:9fa3f3028773 192 if(server_object) {
janjongboom 0:9fa3f3028773 193 _bootstrapped = true;
janjongboom 0:9fa3f3028773 194 _error = false;
janjongboom 0:9fa3f3028773 195 trace_printer("Bootstrapped");
janjongboom 0:9fa3f3028773 196 }
janjongboom 0:9fa3f3028773 197 }
janjongboom 0:9fa3f3028773 198
janjongboom 0:9fa3f3028773 199 //Callback from mbed client stack when the registration
janjongboom 0:9fa3f3028773 200 // is successful, it returns the mbed Device Server object
janjongboom 0:9fa3f3028773 201 // to which the resources are registered and registered objects.
janjongboom 0:9fa3f3028773 202 virtual void object_registered(M2MSecurity */*security_object*/, const M2MServer &/*server_object*/){
janjongboom 0:9fa3f3028773 203 _registered = true;
janjongboom 0:9fa3f3028773 204 _unregistered = false;
janjongboom 0:9fa3f3028773 205 trace_printer("Registered object successfully!");
janjongboom 0:9fa3f3028773 206
janjongboom 0:9fa3f3028773 207 if (_onRegistered) {
janjongboom 0:9fa3f3028773 208 _onRegistered.call();
janjongboom 0:9fa3f3028773 209 }
janjongboom 0:9fa3f3028773 210 }
janjongboom 0:9fa3f3028773 211
janjongboom 0:9fa3f3028773 212 //Callback from mbed client stack when the unregistration
janjongboom 0:9fa3f3028773 213 // is successful, it returns the mbed Device Server object
janjongboom 0:9fa3f3028773 214 // to which the resources were unregistered.
janjongboom 0:9fa3f3028773 215 virtual void object_unregistered(M2MSecurity */*security_object*/){
janjongboom 0:9fa3f3028773 216 _registered = false;
janjongboom 0:9fa3f3028773 217 _unregistered = true;
janjongboom 0:9fa3f3028773 218 trace_printer("Unregistered Object Successfully");
janjongboom 0:9fa3f3028773 219
janjongboom 0:9fa3f3028773 220 if (_onUnregistered) {
janjongboom 0:9fa3f3028773 221 _onUnregistered.call();
janjongboom 0:9fa3f3028773 222 }
janjongboom 0:9fa3f3028773 223 }
janjongboom 0:9fa3f3028773 224
janjongboom 0:9fa3f3028773 225 /*
janjongboom 0:9fa3f3028773 226 * Callback from mbed client stack when registration is updated
janjongboom 0:9fa3f3028773 227 */
janjongboom 0:9fa3f3028773 228 virtual void registration_updated(M2MSecurity */*security_object*/, const M2MServer & /*server_object*/){
janjongboom 0:9fa3f3028773 229 /* The registration is updated automatically and frequently by the
janjongboom 0:9fa3f3028773 230 * mbed client stack. This print statement is turned off because it
janjongboom 0:9fa3f3028773 231 * tends to happen alot.
janjongboom 0:9fa3f3028773 232 */
janjongboom 0:9fa3f3028773 233 //trace_printer("\r\nRegistration Updated\r\n");
janjongboom 0:9fa3f3028773 234 }
janjongboom 0:9fa3f3028773 235
janjongboom 0:9fa3f3028773 236 // Callback from mbed client stack if any error is encountered
janjongboom 0:9fa3f3028773 237 // during any of the LWM2M operations. Error type is passed in
janjongboom 0:9fa3f3028773 238 // the callback.
janjongboom 0:9fa3f3028773 239 virtual void error(M2MInterface::Error error){
janjongboom 0:9fa3f3028773 240 _error = true;
janjongboom 0:9fa3f3028773 241 switch(error){
janjongboom 0:9fa3f3028773 242 case M2MInterface::AlreadyExists:
janjongboom 0:9fa3f3028773 243 trace_printer("[ERROR:] M2MInterface::AlreadyExist");
janjongboom 0:9fa3f3028773 244 break;
janjongboom 0:9fa3f3028773 245 case M2MInterface::BootstrapFailed:
janjongboom 0:9fa3f3028773 246 trace_printer("[ERROR:] M2MInterface::BootstrapFailed");
janjongboom 0:9fa3f3028773 247 break;
janjongboom 0:9fa3f3028773 248 case M2MInterface::InvalidParameters:
janjongboom 0:9fa3f3028773 249 trace_printer("[ERROR:] M2MInterface::InvalidParameters");
janjongboom 0:9fa3f3028773 250 break;
janjongboom 0:9fa3f3028773 251 case M2MInterface::NotRegistered:
janjongboom 0:9fa3f3028773 252 trace_printer("[ERROR:] M2MInterface::NotRegistered");
janjongboom 0:9fa3f3028773 253 break;
janjongboom 0:9fa3f3028773 254 case M2MInterface::Timeout:
janjongboom 0:9fa3f3028773 255 trace_printer("[ERROR:] M2MInterface::Timeout");
janjongboom 0:9fa3f3028773 256 break;
janjongboom 0:9fa3f3028773 257 case M2MInterface::NetworkError:
janjongboom 0:9fa3f3028773 258 trace_printer("[ERROR:] M2MInterface::NetworkError");
janjongboom 0:9fa3f3028773 259 break;
janjongboom 0:9fa3f3028773 260 case M2MInterface::ResponseParseFailed:
janjongboom 0:9fa3f3028773 261 trace_printer("[ERROR:] M2MInterface::ResponseParseFailed");
janjongboom 0:9fa3f3028773 262 break;
janjongboom 0:9fa3f3028773 263 case M2MInterface::UnknownError:
janjongboom 0:9fa3f3028773 264 trace_printer("[ERROR:] M2MInterface::UnknownError");
janjongboom 0:9fa3f3028773 265 break;
janjongboom 0:9fa3f3028773 266 case M2MInterface::MemoryFail:
janjongboom 0:9fa3f3028773 267 trace_printer("[ERROR:] M2MInterface::MemoryFail");
janjongboom 0:9fa3f3028773 268 break;
janjongboom 0:9fa3f3028773 269 case M2MInterface::NotAllowed:
janjongboom 0:9fa3f3028773 270 trace_printer("[ERROR:] M2MInterface::NotAllowed");
janjongboom 0:9fa3f3028773 271 break;
janjongboom 0:9fa3f3028773 272 default:
janjongboom 0:9fa3f3028773 273 break;
janjongboom 0:9fa3f3028773 274 }
janjongboom 0:9fa3f3028773 275 }
janjongboom 0:9fa3f3028773 276
janjongboom 0:9fa3f3028773 277 /* Callback from mbed client stack if any value has changed
janjongboom 0:9fa3f3028773 278 * during PUT operation. Object and its type is passed in
janjongboom 0:9fa3f3028773 279 * the callback.
janjongboom 0:9fa3f3028773 280 * BaseType enum from m2mbase.h
janjongboom 0:9fa3f3028773 281 * Object = 0x0, Resource = 0x1, ObjectInstance = 0x2, ResourceInstance = 0x3
janjongboom 0:9fa3f3028773 282 */
janjongboom 0:9fa3f3028773 283 virtual void value_updated(M2MBase *base, M2MBase::BaseType type) {
Jan Jongboom 3:ce2322965a27 284 if (_debug) printf("[SMC] PUT Request Received, type=%d\n", type);
Jan Jongboom 23:c89df15e88d2 285 if (strcmp(base->uri_path(), "") != 0) {
Jan Jongboom 23:c89df15e88d2 286 if (_debug) printf("[SMC] PUT came in for %s\n", base->uri_path());
Jan Jongboom 3:ce2322965a27 287
Jan Jongboom 23:c89df15e88d2 288 _onValueChanged.call(string(base->uri_path()));
Jan Jongboom 3:ce2322965a27 289 }
janjongboom 0:9fa3f3028773 290 }
janjongboom 0:9fa3f3028773 291
janjongboom 0:9fa3f3028773 292 /*
janjongboom 0:9fa3f3028773 293 * update the registration period
janjongboom 0:9fa3f3028773 294 */
janjongboom 0:9fa3f3028773 295 void test_update_register() {
janjongboom 0:9fa3f3028773 296 if (_registered) {
janjongboom 0:9fa3f3028773 297 _interface->update_registration(_register_security, 100);
janjongboom 0:9fa3f3028773 298 }
janjongboom 0:9fa3f3028773 299 }
janjongboom 0:9fa3f3028773 300
janjongboom 0:9fa3f3028773 301 /*
janjongboom 0:9fa3f3028773 302 * manually configure the security object private variable
janjongboom 0:9fa3f3028773 303 */
janjongboom 0:9fa3f3028773 304 void set_register_object(M2MSecurity *register_object) {
janjongboom 0:9fa3f3028773 305 if (_register_security == NULL) {
janjongboom 0:9fa3f3028773 306 _register_security = register_object;
janjongboom 0:9fa3f3028773 307 }
janjongboom 0:9fa3f3028773 308 }
janjongboom 0:9fa3f3028773 309
janjongboom 0:9fa3f3028773 310 private:
janjongboom 0:9fa3f3028773 311
janjongboom 0:9fa3f3028773 312 /*
janjongboom 0:9fa3f3028773 313 * Private variables used in class
janjongboom 0:9fa3f3028773 314 */
janjongboom 0:9fa3f3028773 315 M2MInterface *_interface;
janjongboom 0:9fa3f3028773 316 M2MSecurity *_register_security;
janjongboom 0:9fa3f3028773 317 M2MObject *_object;
janjongboom 0:9fa3f3028773 318 volatile bool _bootstrapped;
janjongboom 0:9fa3f3028773 319 volatile bool _error;
janjongboom 0:9fa3f3028773 320 volatile bool _registered;
janjongboom 0:9fa3f3028773 321 volatile bool _unregistered;
janjongboom 0:9fa3f3028773 322 int _value;
janjongboom 0:9fa3f3028773 323 struct MbedClientOptions _options;
Jan Jongboom 12:26810c6b58e1 324 Callback<void()> _onRegistered;
Jan Jongboom 12:26810c6b58e1 325 Callback<void()> _onUnregistered;
Jan Jongboom 12:26810c6b58e1 326 Callback<void(string)> _onValueChanged;
Jan Jongboom 3:ce2322965a27 327 bool _debug;
janjongboom 0:9fa3f3028773 328 };
janjongboom 0:9fa3f3028773 329
janjongboom 0:9fa3f3028773 330 #endif // __SIMPLE_MBED_CLIENT_WRAPPER_H__