U_Blox device connector

Dependencies:   mbed

Fork of mbed-os-example-client by mbed-os-examples

Committer:
Yogesh Pande
Date:
Mon Jul 25 16:32:20 2016 +0300
Revision:
0:7d5ec759888b
Child:
5:b7d7ca715fdb
Update example home repo at https://github.com/ARMmbed/mbed-os-example-client

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Yogesh Pande 0:7d5ec759888b 1 /*
Yogesh Pande 0:7d5ec759888b 2 * Copyright (c) 2015 ARM Limited. All rights reserved.
Yogesh Pande 0:7d5ec759888b 3 * SPDX-License-Identifier: Apache-2.0
Yogesh Pande 0:7d5ec759888b 4 * Licensed under the Apache License, Version 2.0 (the License); you may
Yogesh Pande 0:7d5ec759888b 5 * not use this file except in compliance with the License.
Yogesh Pande 0:7d5ec759888b 6 * You may obtain a copy of the License at
Yogesh Pande 0:7d5ec759888b 7 *
Yogesh Pande 0:7d5ec759888b 8 * http://www.apache.org/licenses/LICENSE-2.0
Yogesh Pande 0:7d5ec759888b 9 *
Yogesh Pande 0:7d5ec759888b 10 * Unless required by applicable law or agreed to in writing, software
Yogesh Pande 0:7d5ec759888b 11 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
Yogesh Pande 0:7d5ec759888b 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Yogesh Pande 0:7d5ec759888b 13 * See the License for the specific language governing permissions and
Yogesh Pande 0:7d5ec759888b 14 * limitations under the License.
Yogesh Pande 0:7d5ec759888b 15 */
Yogesh Pande 0:7d5ec759888b 16 #include "simpleclient.h"
Yogesh Pande 0:7d5ec759888b 17 #include <string>
Yogesh Pande 0:7d5ec759888b 18 #include <sstream>
Yogesh Pande 0:7d5ec759888b 19 #include <vector>
Yogesh Pande 0:7d5ec759888b 20 #include "mbed-trace/mbed_trace.h"
Yogesh Pande 0:7d5ec759888b 21
Yogesh Pande 0:7d5ec759888b 22 #include "security.h"
Yogesh Pande 0:7d5ec759888b 23
Yogesh Pande 0:7d5ec759888b 24 #include "mbed.h"
Yogesh Pande 0:7d5ec759888b 25 #include "rtos.h"
Yogesh Pande 0:7d5ec759888b 26
Yogesh Pande 0:7d5ec759888b 27 #define ETHERNET 1
Yogesh Pande 0:7d5ec759888b 28 #define WIFI 2
Yogesh Pande 0:7d5ec759888b 29 #define MESH_LOWPAN_ND 3
Yogesh Pande 0:7d5ec759888b 30 #define MESH_THREAD 4
Yogesh Pande 0:7d5ec759888b 31
Yogesh Pande 0:7d5ec759888b 32 #define STRINGIFY(s) #s
Yogesh Pande 0:7d5ec759888b 33
Yogesh Pande 0:7d5ec759888b 34 #if MBED_CONF_APP_NETWORK_INTERFACE == WIFI
Yogesh Pande 0:7d5ec759888b 35 #include "ESP8266Interface.h"
Yogesh Pande 0:7d5ec759888b 36 ESP8266Interface esp(D1, D0);
Yogesh Pande 0:7d5ec759888b 37 #elif MBED_CONF_APP_NETWORK_INTERFACE == ETHERNET
Yogesh Pande 0:7d5ec759888b 38 #include "EthernetInterface.h"
Yogesh Pande 0:7d5ec759888b 39 EthernetInterface eth;
Yogesh Pande 0:7d5ec759888b 40 #elif MBED_CONF_APP_NETWORK_INTERFACE == MESH_LOWPAN_ND
Yogesh Pande 0:7d5ec759888b 41 #define MESH
Yogesh Pande 0:7d5ec759888b 42 #include "NanostackInterface.h"
Yogesh Pande 0:7d5ec759888b 43 LoWPANNDInterface mesh;
Yogesh Pande 0:7d5ec759888b 44 #elif MBED_CONF_APP_NETWORK_INTERFACE == MESH_THREAD
Yogesh Pande 0:7d5ec759888b 45 #define MESH
Yogesh Pande 0:7d5ec759888b 46 #include "NanostackInterface.h"
Yogesh Pande 0:7d5ec759888b 47 ThreadInterface mesh;
Yogesh Pande 0:7d5ec759888b 48 #endif
Yogesh Pande 0:7d5ec759888b 49
Yogesh Pande 0:7d5ec759888b 50 #ifndef MESH
Yogesh Pande 0:7d5ec759888b 51 // This is address to mbed Device Connector
Yogesh Pande 0:7d5ec759888b 52 #define MBED_SERVER_ADDRESS "coap://api.connector.mbed.com:5684"
Yogesh Pande 0:7d5ec759888b 53 #else
Yogesh Pande 0:7d5ec759888b 54 // This is address to mbed Device Connector
Yogesh Pande 0:7d5ec759888b 55 #define MBED_SERVER_ADDRESS "coaps://[2607:f0d0:2601:52::20]:5684"
Yogesh Pande 0:7d5ec759888b 56 #endif
Yogesh Pande 0:7d5ec759888b 57
Yogesh Pande 0:7d5ec759888b 58 Serial output(USBTX, USBRX);
Yogesh Pande 0:7d5ec759888b 59
Yogesh Pande 0:7d5ec759888b 60 // These are example resource values for the Device Object
Yogesh Pande 0:7d5ec759888b 61 struct MbedClientDevice device = {
Yogesh Pande 0:7d5ec759888b 62 "Manufacturer_String", // Manufacturer
Yogesh Pande 0:7d5ec759888b 63 "Type_String", // Type
Yogesh Pande 0:7d5ec759888b 64 "ModelNumber_String", // ModelNumber
Yogesh Pande 0:7d5ec759888b 65 "SerialNumber_String" // SerialNumber
Yogesh Pande 0:7d5ec759888b 66 };
Yogesh Pande 0:7d5ec759888b 67
Yogesh Pande 0:7d5ec759888b 68 // Instantiate the class which implements LWM2M Client API (from simpleclient.h)
Yogesh Pande 0:7d5ec759888b 69 MbedClient mbed_client(device);
Yogesh Pande 0:7d5ec759888b 70
Yogesh Pande 0:7d5ec759888b 71 // Set up Hardware interrupt button.
Yogesh Pande 0:7d5ec759888b 72 InterruptIn obs_button(SW2);
Yogesh Pande 0:7d5ec759888b 73 InterruptIn unreg_button(SW3);
Yogesh Pande 0:7d5ec759888b 74
Yogesh Pande 0:7d5ec759888b 75 // LED Output
Yogesh Pande 0:7d5ec759888b 76 DigitalOut led1(LED1);
Yogesh Pande 0:7d5ec759888b 77
Yogesh Pande 0:7d5ec759888b 78 /*
Yogesh Pande 0:7d5ec759888b 79 * The Led contains one property (pattern) and a function (blink).
Yogesh Pande 0:7d5ec759888b 80 * When the function blink is executed, the pattern is read, and the LED
Yogesh Pande 0:7d5ec759888b 81 * will blink based on the pattern.
Yogesh Pande 0:7d5ec759888b 82 */
Yogesh Pande 0:7d5ec759888b 83 class LedResource {
Yogesh Pande 0:7d5ec759888b 84 public:
Yogesh Pande 0:7d5ec759888b 85 LedResource() {
Yogesh Pande 0:7d5ec759888b 86 // create ObjectID with metadata tag of '3201', which is 'digital output'
Yogesh Pande 0:7d5ec759888b 87 led_object = M2MInterfaceFactory::create_object("3201");
Yogesh Pande 0:7d5ec759888b 88 M2MObjectInstance* led_inst = led_object->create_object_instance();
Yogesh Pande 0:7d5ec759888b 89
Yogesh Pande 0:7d5ec759888b 90 // 5853 = Multi-state output
Yogesh Pande 0:7d5ec759888b 91 M2MResource* pattern_res = led_inst->create_dynamic_resource("5853", "Pattern",
Yogesh Pande 0:7d5ec759888b 92 M2MResourceInstance::STRING, false);
Yogesh Pande 0:7d5ec759888b 93 // read and write
Yogesh Pande 0:7d5ec759888b 94 pattern_res->set_operation(M2MBase::GET_PUT_ALLOWED);
Yogesh Pande 0:7d5ec759888b 95 // set initial pattern (toggle every 200ms. 7 toggles in total)
Yogesh Pande 0:7d5ec759888b 96 pattern_res->set_value((const uint8_t*)"500:500:500:500:500:500:500", 27);
Yogesh Pande 0:7d5ec759888b 97
Yogesh Pande 0:7d5ec759888b 98 // there's not really an execute LWM2M ID that matches... hmm...
Yogesh Pande 0:7d5ec759888b 99 M2MResource* led_res = led_inst->create_dynamic_resource("5850", "Blink",
Yogesh Pande 0:7d5ec759888b 100 M2MResourceInstance::OPAQUE, false);
Yogesh Pande 0:7d5ec759888b 101 // we allow executing a function here...
Yogesh Pande 0:7d5ec759888b 102 led_res->set_operation(M2MBase::POST_ALLOWED);
Yogesh Pande 0:7d5ec759888b 103 // when a POST comes in, we want to execute the led_execute_callback
Yogesh Pande 0:7d5ec759888b 104 led_res->set_execute_function(execute_callback(this, &LedResource::blink));
Yogesh Pande 0:7d5ec759888b 105 }
Yogesh Pande 0:7d5ec759888b 106
Yogesh Pande 0:7d5ec759888b 107 M2MObject* get_object() {
Yogesh Pande 0:7d5ec759888b 108 return led_object;
Yogesh Pande 0:7d5ec759888b 109 }
Yogesh Pande 0:7d5ec759888b 110
Yogesh Pande 0:7d5ec759888b 111 void blink(void *) {
Yogesh Pande 0:7d5ec759888b 112 // read the value of 'Pattern'
Yogesh Pande 0:7d5ec759888b 113 M2MObjectInstance* inst = led_object->object_instance();
Yogesh Pande 0:7d5ec759888b 114 M2MResource* res = inst->resource("5853");
Yogesh Pande 0:7d5ec759888b 115
Yogesh Pande 0:7d5ec759888b 116 // values in mbed Client are all buffers, and we need a vector of int's
Yogesh Pande 0:7d5ec759888b 117 uint8_t* buffIn = NULL;
Yogesh Pande 0:7d5ec759888b 118 uint32_t sizeIn;
Yogesh Pande 0:7d5ec759888b 119 res->get_value(buffIn, sizeIn);
Yogesh Pande 0:7d5ec759888b 120
Yogesh Pande 0:7d5ec759888b 121 // turn the buffer into a string, and initialize a vector<int> on the heap
Yogesh Pande 0:7d5ec759888b 122 std::string s((char*)buffIn, sizeIn);
Yogesh Pande 0:7d5ec759888b 123 std::vector<uint32_t>* v = new std::vector<uint32_t>;
Yogesh Pande 0:7d5ec759888b 124
Yogesh Pande 0:7d5ec759888b 125 output.printf("led_execute_callback pattern=%s\r\n", s.c_str());
Yogesh Pande 0:7d5ec759888b 126
Yogesh Pande 0:7d5ec759888b 127 // our pattern is something like 500:200:500, so parse that
Yogesh Pande 0:7d5ec759888b 128 std::size_t found = s.find_first_of(":");
Yogesh Pande 0:7d5ec759888b 129 while (found!=std::string::npos) {
Yogesh Pande 0:7d5ec759888b 130
Yogesh Pande 0:7d5ec759888b 131 v->push_back(atoi((const char*)s.substr(0,found).c_str()));
Yogesh Pande 0:7d5ec759888b 132 s = s.substr(found+1);
Yogesh Pande 0:7d5ec759888b 133 found=s.find_first_of(":");
Yogesh Pande 0:7d5ec759888b 134 if(found == std::string::npos) {
Yogesh Pande 0:7d5ec759888b 135 v->push_back(atoi((const char*)s.c_str()));
Yogesh Pande 0:7d5ec759888b 136 }
Yogesh Pande 0:7d5ec759888b 137 }
Yogesh Pande 0:7d5ec759888b 138
Yogesh Pande 0:7d5ec759888b 139
Yogesh Pande 0:7d5ec759888b 140 // do_blink is called with the vector, and starting at -1
Yogesh Pande 0:7d5ec759888b 141 do_blink(v, 0);
Yogesh Pande 0:7d5ec759888b 142 }
Yogesh Pande 0:7d5ec759888b 143
Yogesh Pande 0:7d5ec759888b 144 private:
Yogesh Pande 0:7d5ec759888b 145 M2MObject* led_object;
Yogesh Pande 0:7d5ec759888b 146
Yogesh Pande 0:7d5ec759888b 147 void do_blink(std::vector<uint32_t>* pattern, uint16_t position) {
Yogesh Pande 0:7d5ec759888b 148 // blink the LED
Yogesh Pande 0:7d5ec759888b 149 led1 = !led1;
Yogesh Pande 0:7d5ec759888b 150
Yogesh Pande 0:7d5ec759888b 151 // up the position, if we reached the end of the vector
Yogesh Pande 0:7d5ec759888b 152 if (position >= pattern->size()) {
Yogesh Pande 0:7d5ec759888b 153 // free memory, and exit this function
Yogesh Pande 0:7d5ec759888b 154 delete pattern;
Yogesh Pande 0:7d5ec759888b 155 return;
Yogesh Pande 0:7d5ec759888b 156 }
Yogesh Pande 0:7d5ec759888b 157
Yogesh Pande 0:7d5ec759888b 158 // how long do we need to wait before the next blink?
Yogesh Pande 0:7d5ec759888b 159 uint32_t delay_ms = pattern->at(position);
Yogesh Pande 0:7d5ec759888b 160
Yogesh Pande 0:7d5ec759888b 161 // Invoke same function after `delay_ms` (upping position)
Yogesh Pande 0:7d5ec759888b 162 Thread::wait(delay_ms);
Yogesh Pande 0:7d5ec759888b 163 do_blink(pattern, ++position);
Yogesh Pande 0:7d5ec759888b 164 }
Yogesh Pande 0:7d5ec759888b 165 };
Yogesh Pande 0:7d5ec759888b 166
Yogesh Pande 0:7d5ec759888b 167 /*
Yogesh Pande 0:7d5ec759888b 168 * The button contains one property (click count).
Yogesh Pande 0:7d5ec759888b 169 * When `handle_button_click` is executed, the counter updates.
Yogesh Pande 0:7d5ec759888b 170 */
Yogesh Pande 0:7d5ec759888b 171 class ButtonResource {
Yogesh Pande 0:7d5ec759888b 172 public:
Yogesh Pande 0:7d5ec759888b 173 ButtonResource(): counter(0) {
Yogesh Pande 0:7d5ec759888b 174 // create ObjectID with metadata tag of '3200', which is 'digital input'
Yogesh Pande 0:7d5ec759888b 175 btn_object = M2MInterfaceFactory::create_object("3200");
Yogesh Pande 0:7d5ec759888b 176 M2MObjectInstance* btn_inst = btn_object->create_object_instance();
Yogesh Pande 0:7d5ec759888b 177 // create resource with ID '5501', which is digital input counter
Yogesh Pande 0:7d5ec759888b 178 M2MResource* btn_res = btn_inst->create_dynamic_resource("5501", "Button",
Yogesh Pande 0:7d5ec759888b 179 M2MResourceInstance::INTEGER, true /* observable */);
Yogesh Pande 0:7d5ec759888b 180 // we can read this value
Yogesh Pande 0:7d5ec759888b 181 btn_res->set_operation(M2MBase::GET_ALLOWED);
Yogesh Pande 0:7d5ec759888b 182 // set initial value (all values in mbed Client are buffers)
Yogesh Pande 0:7d5ec759888b 183 // to be able to read this data easily in the Connector console, we'll use a string
Yogesh Pande 0:7d5ec759888b 184 btn_res->set_value((uint8_t*)"0", 1);
Yogesh Pande 0:7d5ec759888b 185 }
Yogesh Pande 0:7d5ec759888b 186
Yogesh Pande 0:7d5ec759888b 187 ~ButtonResource() {
Yogesh Pande 0:7d5ec759888b 188 }
Yogesh Pande 0:7d5ec759888b 189
Yogesh Pande 0:7d5ec759888b 190 M2MObject* get_object() {
Yogesh Pande 0:7d5ec759888b 191 return btn_object;
Yogesh Pande 0:7d5ec759888b 192 }
Yogesh Pande 0:7d5ec759888b 193
Yogesh Pande 0:7d5ec759888b 194 /*
Yogesh Pande 0:7d5ec759888b 195 * When you press the button, we read the current value of the click counter
Yogesh Pande 0:7d5ec759888b 196 * from mbed Device Connector, then up the value with one.
Yogesh Pande 0:7d5ec759888b 197 */
Yogesh Pande 0:7d5ec759888b 198 void handle_button_click() {
Yogesh Pande 0:7d5ec759888b 199 M2MObjectInstance* inst = btn_object->object_instance();
Yogesh Pande 0:7d5ec759888b 200 M2MResource* res = inst->resource("5501");
Yogesh Pande 0:7d5ec759888b 201
Yogesh Pande 0:7d5ec759888b 202 // up counter
Yogesh Pande 0:7d5ec759888b 203 counter++;
Yogesh Pande 0:7d5ec759888b 204
Yogesh Pande 0:7d5ec759888b 205 printf("handle_button_click, new value of counter is %d\r\n", counter);
Yogesh Pande 0:7d5ec759888b 206
Yogesh Pande 0:7d5ec759888b 207 // serialize the value of counter as a string, and tell connector
Yogesh Pande 0:7d5ec759888b 208 char buffer[20];
Yogesh Pande 0:7d5ec759888b 209 int size = sprintf(buffer,"%d",counter);
Yogesh Pande 0:7d5ec759888b 210 res->set_value((uint8_t*)buffer, size);
Yogesh Pande 0:7d5ec759888b 211 }
Yogesh Pande 0:7d5ec759888b 212
Yogesh Pande 0:7d5ec759888b 213 private:
Yogesh Pande 0:7d5ec759888b 214 M2MObject* btn_object;
Yogesh Pande 0:7d5ec759888b 215 uint16_t counter;
Yogesh Pande 0:7d5ec759888b 216 };
Yogesh Pande 0:7d5ec759888b 217
Yogesh Pande 0:7d5ec759888b 218 // Network interaction must be performed outside of interrupt context
Yogesh Pande 0:7d5ec759888b 219 Semaphore updates(0);
Yogesh Pande 0:7d5ec759888b 220 volatile bool registered = false;
Yogesh Pande 0:7d5ec759888b 221 volatile bool clicked = false;
Yogesh Pande 0:7d5ec759888b 222 osThreadId mainThread;
Yogesh Pande 0:7d5ec759888b 223
Yogesh Pande 0:7d5ec759888b 224 void unregister() {
Yogesh Pande 0:7d5ec759888b 225 registered = false;
Yogesh Pande 0:7d5ec759888b 226 updates.release();
Yogesh Pande 0:7d5ec759888b 227 }
Yogesh Pande 0:7d5ec759888b 228
Yogesh Pande 0:7d5ec759888b 229 void button_clicked() {
Yogesh Pande 0:7d5ec759888b 230 clicked = true;
Yogesh Pande 0:7d5ec759888b 231 updates.release();
Yogesh Pande 0:7d5ec759888b 232 }
Yogesh Pande 0:7d5ec759888b 233
Yogesh Pande 0:7d5ec759888b 234 // debug printf function
Yogesh Pande 0:7d5ec759888b 235 void trace_printer(const char* str) {
Yogesh Pande 0:7d5ec759888b 236 printf("%s\r\n", str);
Yogesh Pande 0:7d5ec759888b 237 }
Yogesh Pande 0:7d5ec759888b 238
Yogesh Pande 0:7d5ec759888b 239 // Status indication
Yogesh Pande 0:7d5ec759888b 240 Ticker status_ticker;
Yogesh Pande 0:7d5ec759888b 241 DigitalOut status_led(LED1);
Yogesh Pande 0:7d5ec759888b 242 void blinky() { status_led = !status_led; }
Yogesh Pande 0:7d5ec759888b 243
Yogesh Pande 0:7d5ec759888b 244
Yogesh Pande 0:7d5ec759888b 245 // Entry point to the program
Yogesh Pande 0:7d5ec759888b 246 int main() {
Yogesh Pande 0:7d5ec759888b 247 status_ticker.attach_us(blinky, 250000);
Yogesh Pande 0:7d5ec759888b 248
Yogesh Pande 0:7d5ec759888b 249 // Keep track of the main thread
Yogesh Pande 0:7d5ec759888b 250 mainThread = osThreadGetId();
Yogesh Pande 0:7d5ec759888b 251
Yogesh Pande 0:7d5ec759888b 252 // Sets the console baud-rate
Yogesh Pande 0:7d5ec759888b 253 output.baud(115200);
Yogesh Pande 0:7d5ec759888b 254
Yogesh Pande 0:7d5ec759888b 255 output.printf("Starting mbed Client example...\r\n");
Yogesh Pande 0:7d5ec759888b 256
Yogesh Pande 0:7d5ec759888b 257 mbed_trace_init();
Yogesh Pande 0:7d5ec759888b 258 mbed_trace_print_function_set(trace_printer);
Yogesh Pande 0:7d5ec759888b 259
Yogesh Pande 0:7d5ec759888b 260 NetworkInterface *network_interface = 0;
Yogesh Pande 0:7d5ec759888b 261 int connect_success = -1;
Yogesh Pande 0:7d5ec759888b 262 #if MBED_CONF_APP_NETWORK_INTERFACE == WIFI
Yogesh Pande 0:7d5ec759888b 263 output.printf("\n\rUsing WiFi \r\n");
Yogesh Pande 0:7d5ec759888b 264 output.printf("\n\rConnecting to WiFi..\r\n");
Yogesh Pande 0:7d5ec759888b 265 connect_success = esp.connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD);
Yogesh Pande 0:7d5ec759888b 266 network_interface = &esp;
Yogesh Pande 0:7d5ec759888b 267 #elif MBED_CONF_APP_NETWORK_INTERFACE == ETHERNET
Yogesh Pande 0:7d5ec759888b 268 output.printf("Using Ethernet\r\n");
Yogesh Pande 0:7d5ec759888b 269 connect_success = eth.connect();
Yogesh Pande 0:7d5ec759888b 270 network_interface = &eth;
Yogesh Pande 0:7d5ec759888b 271 #endif
Yogesh Pande 0:7d5ec759888b 272 #ifdef MESH
Yogesh Pande 0:7d5ec759888b 273 output.printf("Using Mesh\r\n");
Yogesh Pande 0:7d5ec759888b 274 output.printf("\n\rConnecting to Mesh..\r\n");
Yogesh Pande 0:7d5ec759888b 275 connect_success = mesh.connect();
Yogesh Pande 0:7d5ec759888b 276 network_interface = &mesh;
Yogesh Pande 0:7d5ec759888b 277 #endif
Yogesh Pande 0:7d5ec759888b 278 if(connect_success == 0) {
Yogesh Pande 0:7d5ec759888b 279 output.printf("\n\rConnected to Network successfully\r\n");
Yogesh Pande 0:7d5ec759888b 280 } else {
Yogesh Pande 0:7d5ec759888b 281 output.printf("\n\rConnection to Network Failed %d! Exiting application....\r\n", connect_success);
Yogesh Pande 0:7d5ec759888b 282 return 0;
Yogesh Pande 0:7d5ec759888b 283 }
Yogesh Pande 0:7d5ec759888b 284 const char *ip_addr = network_interface->get_ip_address();
Yogesh Pande 0:7d5ec759888b 285 if (ip_addr) {
Yogesh Pande 0:7d5ec759888b 286 output.printf("IP address %s\r\n", ip_addr);
Yogesh Pande 0:7d5ec759888b 287 } else {
Yogesh Pande 0:7d5ec759888b 288 output.printf("No IP address\r\n");
Yogesh Pande 0:7d5ec759888b 289 }
Yogesh Pande 0:7d5ec759888b 290
Yogesh Pande 0:7d5ec759888b 291 // we create our button and LED resources
Yogesh Pande 0:7d5ec759888b 292 ButtonResource button_resource;
Yogesh Pande 0:7d5ec759888b 293 LedResource led_resource;
Yogesh Pande 0:7d5ec759888b 294
Yogesh Pande 0:7d5ec759888b 295 // On press of SW3 button on K64F board, example application
Yogesh Pande 0:7d5ec759888b 296 // will call unregister API towards mbed Device Connector
Yogesh Pande 0:7d5ec759888b 297 //unreg_button.fall(&mbed_client,&MbedClient::test_unregister);
Yogesh Pande 0:7d5ec759888b 298 unreg_button.fall(&unregister);
Yogesh Pande 0:7d5ec759888b 299
Yogesh Pande 0:7d5ec759888b 300 // Observation Button (SW2) press will send update of endpoint resource values to connector
Yogesh Pande 0:7d5ec759888b 301 obs_button.fall(&button_clicked);
Yogesh Pande 0:7d5ec759888b 302
Yogesh Pande 0:7d5ec759888b 303 // Create endpoint interface to manage register and unregister
Yogesh Pande 0:7d5ec759888b 304 mbed_client.create_interface(MBED_SERVER_ADDRESS, network_interface);
Yogesh Pande 0:7d5ec759888b 305
Yogesh Pande 0:7d5ec759888b 306 // Create Objects of varying types, see simpleclient.h for more details on implementation.
Yogesh Pande 0:7d5ec759888b 307 M2MSecurity* register_object = mbed_client.create_register_object(); // server object specifying connector info
Yogesh Pande 0:7d5ec759888b 308 M2MDevice* device_object = mbed_client.create_device_object(); // device resources object
Yogesh Pande 0:7d5ec759888b 309
Yogesh Pande 0:7d5ec759888b 310 // Create list of Objects to register
Yogesh Pande 0:7d5ec759888b 311 M2MObjectList object_list;
Yogesh Pande 0:7d5ec759888b 312
Yogesh Pande 0:7d5ec759888b 313 // Add objects to list
Yogesh Pande 0:7d5ec759888b 314 object_list.push_back(device_object);
Yogesh Pande 0:7d5ec759888b 315 object_list.push_back(button_resource.get_object());
Yogesh Pande 0:7d5ec759888b 316 object_list.push_back(led_resource.get_object());
Yogesh Pande 0:7d5ec759888b 317
Yogesh Pande 0:7d5ec759888b 318 // Set endpoint registration object
Yogesh Pande 0:7d5ec759888b 319 mbed_client.set_register_object(register_object);
Yogesh Pande 0:7d5ec759888b 320
Yogesh Pande 0:7d5ec759888b 321 // Register with mbed Device Connector
Yogesh Pande 0:7d5ec759888b 322 mbed_client.test_register(register_object, object_list);
Yogesh Pande 0:7d5ec759888b 323 registered = true;
Yogesh Pande 0:7d5ec759888b 324
Yogesh Pande 0:7d5ec759888b 325 while (true) {
Yogesh Pande 0:7d5ec759888b 326 updates.wait(25000);
Yogesh Pande 0:7d5ec759888b 327 if(registered) {
Yogesh Pande 0:7d5ec759888b 328 if(!clicked) {
Yogesh Pande 0:7d5ec759888b 329 mbed_client.test_update_register();
Yogesh Pande 0:7d5ec759888b 330 }
Yogesh Pande 0:7d5ec759888b 331 }else {
Yogesh Pande 0:7d5ec759888b 332 break;
Yogesh Pande 0:7d5ec759888b 333 }
Yogesh Pande 0:7d5ec759888b 334 if(clicked) {
Yogesh Pande 0:7d5ec759888b 335 clicked = false;
Yogesh Pande 0:7d5ec759888b 336 button_resource.handle_button_click();
Yogesh Pande 0:7d5ec759888b 337 }
Yogesh Pande 0:7d5ec759888b 338 }
Yogesh Pande 0:7d5ec759888b 339
Yogesh Pande 0:7d5ec759888b 340 mbed_client.test_unregister();
Yogesh Pande 0:7d5ec759888b 341 status_ticker.detach();
Yogesh Pande 0:7d5ec759888b 342 }