![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
This is a simple mbed client example demonstrating, registration of a device with mbed Device Connector and reading and writing values as well as deregistering on different Network Interfaces including Ethernet, WiFi, 6LoWPAN ND and Thread respectively.
Fork of mbed-os-example-client by
main.cpp@13:c44e318674db, 2016-08-05 (annotated)
- Committer:
- mbed_official
- Date:
- Fri Aug 05 04:30:15 2016 +0100
- Revision:
- 13:c44e318674db
- Parent:
- 10:4a3aaf2e130a
- Child:
- 19:91eb6f58fd77
Merge pull request #71 from geky/wifi-config-pins
Add configurable serial-pins to support USBTX/RX == D1/D0 devices
Commit copied from https://github.com/ARMmbed/mbed-os-example-client
Who changed what in which revision?
User | Revision | Line number | New 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" |
mbed_official | 13:c44e318674db | 36 | ESP8266Interface esp(MBED_CONF_APP_WIFI_TX, MBED_CONF_APP_WIFI_RX); |
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 | |
mbed_official | 5:b7d7ca715fdb | 71 | |
mbed_official | 5:b7d7ca715fdb | 72 | // In case of K64F board , there is button resource available |
mbed_official | 5:b7d7ca715fdb | 73 | // to change resource value and unregister |
mbed_official | 5:b7d7ca715fdb | 74 | #ifdef TARGET_K64F |
Yogesh Pande |
0:7d5ec759888b | 75 | // Set up Hardware interrupt button. |
Yogesh Pande |
0:7d5ec759888b | 76 | InterruptIn obs_button(SW2); |
Yogesh Pande |
0:7d5ec759888b | 77 | InterruptIn unreg_button(SW3); |
mbed_official | 5:b7d7ca715fdb | 78 | #else |
mbed_official | 5:b7d7ca715fdb | 79 | //In non K64F boards , set up a timer to simulate updating resource, |
mbed_official | 5:b7d7ca715fdb | 80 | // there is no functionality to unregister. |
mbed_official | 5:b7d7ca715fdb | 81 | Ticker timer; |
mbed_official | 5:b7d7ca715fdb | 82 | #endif |
Yogesh Pande |
0:7d5ec759888b | 83 | |
Yogesh Pande |
0:7d5ec759888b | 84 | // LED Output |
Yogesh Pande |
0:7d5ec759888b | 85 | DigitalOut led1(LED1); |
Yogesh Pande |
0:7d5ec759888b | 86 | |
Yogesh Pande |
0:7d5ec759888b | 87 | /* |
Yogesh Pande |
0:7d5ec759888b | 88 | * The Led contains one property (pattern) and a function (blink). |
Yogesh Pande |
0:7d5ec759888b | 89 | * When the function blink is executed, the pattern is read, and the LED |
Yogesh Pande |
0:7d5ec759888b | 90 | * will blink based on the pattern. |
Yogesh Pande |
0:7d5ec759888b | 91 | */ |
Yogesh Pande |
0:7d5ec759888b | 92 | class LedResource { |
Yogesh Pande |
0:7d5ec759888b | 93 | public: |
Yogesh Pande |
0:7d5ec759888b | 94 | LedResource() { |
Yogesh Pande |
0:7d5ec759888b | 95 | // create ObjectID with metadata tag of '3201', which is 'digital output' |
Yogesh Pande |
0:7d5ec759888b | 96 | led_object = M2MInterfaceFactory::create_object("3201"); |
Yogesh Pande |
0:7d5ec759888b | 97 | M2MObjectInstance* led_inst = led_object->create_object_instance(); |
Yogesh Pande |
0:7d5ec759888b | 98 | |
Yogesh Pande |
0:7d5ec759888b | 99 | // 5853 = Multi-state output |
Yogesh Pande |
0:7d5ec759888b | 100 | M2MResource* pattern_res = led_inst->create_dynamic_resource("5853", "Pattern", |
Yogesh Pande |
0:7d5ec759888b | 101 | M2MResourceInstance::STRING, false); |
Yogesh Pande |
0:7d5ec759888b | 102 | // read and write |
Yogesh Pande |
0:7d5ec759888b | 103 | pattern_res->set_operation(M2MBase::GET_PUT_ALLOWED); |
Yogesh Pande |
0:7d5ec759888b | 104 | // set initial pattern (toggle every 200ms. 7 toggles in total) |
Yogesh Pande |
0:7d5ec759888b | 105 | pattern_res->set_value((const uint8_t*)"500:500:500:500:500:500:500", 27); |
Yogesh Pande |
0:7d5ec759888b | 106 | |
Yogesh Pande |
0:7d5ec759888b | 107 | // there's not really an execute LWM2M ID that matches... hmm... |
Yogesh Pande |
0:7d5ec759888b | 108 | M2MResource* led_res = led_inst->create_dynamic_resource("5850", "Blink", |
Yogesh Pande |
0:7d5ec759888b | 109 | M2MResourceInstance::OPAQUE, false); |
Yogesh Pande |
0:7d5ec759888b | 110 | // we allow executing a function here... |
Yogesh Pande |
0:7d5ec759888b | 111 | led_res->set_operation(M2MBase::POST_ALLOWED); |
Yogesh Pande |
0:7d5ec759888b | 112 | // when a POST comes in, we want to execute the led_execute_callback |
Yogesh Pande |
0:7d5ec759888b | 113 | led_res->set_execute_function(execute_callback(this, &LedResource::blink)); |
Yogesh Pande |
0:7d5ec759888b | 114 | } |
Yogesh Pande |
0:7d5ec759888b | 115 | |
Yogesh Pande |
0:7d5ec759888b | 116 | M2MObject* get_object() { |
Yogesh Pande |
0:7d5ec759888b | 117 | return led_object; |
Yogesh Pande |
0:7d5ec759888b | 118 | } |
Yogesh Pande |
0:7d5ec759888b | 119 | |
Yogesh Pande |
0:7d5ec759888b | 120 | void blink(void *) { |
Yogesh Pande |
0:7d5ec759888b | 121 | // read the value of 'Pattern' |
Yogesh Pande |
0:7d5ec759888b | 122 | M2MObjectInstance* inst = led_object->object_instance(); |
Yogesh Pande |
0:7d5ec759888b | 123 | M2MResource* res = inst->resource("5853"); |
Yogesh Pande |
0:7d5ec759888b | 124 | |
Yogesh Pande |
0:7d5ec759888b | 125 | // values in mbed Client are all buffers, and we need a vector of int's |
Yogesh Pande |
0:7d5ec759888b | 126 | uint8_t* buffIn = NULL; |
Yogesh Pande |
0:7d5ec759888b | 127 | uint32_t sizeIn; |
Yogesh Pande |
0:7d5ec759888b | 128 | res->get_value(buffIn, sizeIn); |
Yogesh Pande |
0:7d5ec759888b | 129 | |
Yogesh Pande |
0:7d5ec759888b | 130 | // turn the buffer into a string, and initialize a vector<int> on the heap |
Yogesh Pande |
0:7d5ec759888b | 131 | std::string s((char*)buffIn, sizeIn); |
Yogesh Pande |
0:7d5ec759888b | 132 | std::vector<uint32_t>* v = new std::vector<uint32_t>; |
Yogesh Pande |
0:7d5ec759888b | 133 | |
Yogesh Pande |
0:7d5ec759888b | 134 | output.printf("led_execute_callback pattern=%s\r\n", s.c_str()); |
Yogesh Pande |
0:7d5ec759888b | 135 | |
Yogesh Pande |
0:7d5ec759888b | 136 | // our pattern is something like 500:200:500, so parse that |
Yogesh Pande |
0:7d5ec759888b | 137 | std::size_t found = s.find_first_of(":"); |
Yogesh Pande |
0:7d5ec759888b | 138 | while (found!=std::string::npos) { |
Yogesh Pande |
0:7d5ec759888b | 139 | |
Yogesh Pande |
0:7d5ec759888b | 140 | v->push_back(atoi((const char*)s.substr(0,found).c_str())); |
Yogesh Pande |
0:7d5ec759888b | 141 | s = s.substr(found+1); |
Yogesh Pande |
0:7d5ec759888b | 142 | found=s.find_first_of(":"); |
Yogesh Pande |
0:7d5ec759888b | 143 | if(found == std::string::npos) { |
Yogesh Pande |
0:7d5ec759888b | 144 | v->push_back(atoi((const char*)s.c_str())); |
Yogesh Pande |
0:7d5ec759888b | 145 | } |
Yogesh Pande |
0:7d5ec759888b | 146 | } |
Yogesh Pande |
0:7d5ec759888b | 147 | |
Yogesh Pande |
0:7d5ec759888b | 148 | |
Yogesh Pande |
0:7d5ec759888b | 149 | // do_blink is called with the vector, and starting at -1 |
Yogesh Pande |
0:7d5ec759888b | 150 | do_blink(v, 0); |
Yogesh Pande |
0:7d5ec759888b | 151 | } |
Yogesh Pande |
0:7d5ec759888b | 152 | |
Yogesh Pande |
0:7d5ec759888b | 153 | private: |
Yogesh Pande |
0:7d5ec759888b | 154 | M2MObject* led_object; |
Yogesh Pande |
0:7d5ec759888b | 155 | |
Yogesh Pande |
0:7d5ec759888b | 156 | void do_blink(std::vector<uint32_t>* pattern, uint16_t position) { |
Yogesh Pande |
0:7d5ec759888b | 157 | // blink the LED |
Yogesh Pande |
0:7d5ec759888b | 158 | led1 = !led1; |
Yogesh Pande |
0:7d5ec759888b | 159 | |
Yogesh Pande |
0:7d5ec759888b | 160 | // up the position, if we reached the end of the vector |
Yogesh Pande |
0:7d5ec759888b | 161 | if (position >= pattern->size()) { |
Yogesh Pande |
0:7d5ec759888b | 162 | // free memory, and exit this function |
Yogesh Pande |
0:7d5ec759888b | 163 | delete pattern; |
Yogesh Pande |
0:7d5ec759888b | 164 | return; |
Yogesh Pande |
0:7d5ec759888b | 165 | } |
Yogesh Pande |
0:7d5ec759888b | 166 | |
Yogesh Pande |
0:7d5ec759888b | 167 | // how long do we need to wait before the next blink? |
Yogesh Pande |
0:7d5ec759888b | 168 | uint32_t delay_ms = pattern->at(position); |
Yogesh Pande |
0:7d5ec759888b | 169 | |
Yogesh Pande |
0:7d5ec759888b | 170 | // Invoke same function after `delay_ms` (upping position) |
Yogesh Pande |
0:7d5ec759888b | 171 | Thread::wait(delay_ms); |
Yogesh Pande |
0:7d5ec759888b | 172 | do_blink(pattern, ++position); |
Yogesh Pande |
0:7d5ec759888b | 173 | } |
Yogesh Pande |
0:7d5ec759888b | 174 | }; |
Yogesh Pande |
0:7d5ec759888b | 175 | |
Yogesh Pande |
0:7d5ec759888b | 176 | /* |
Yogesh Pande |
0:7d5ec759888b | 177 | * The button contains one property (click count). |
Yogesh Pande |
0:7d5ec759888b | 178 | * When `handle_button_click` is executed, the counter updates. |
Yogesh Pande |
0:7d5ec759888b | 179 | */ |
Yogesh Pande |
0:7d5ec759888b | 180 | class ButtonResource { |
Yogesh Pande |
0:7d5ec759888b | 181 | public: |
Yogesh Pande |
0:7d5ec759888b | 182 | ButtonResource(): counter(0) { |
Yogesh Pande |
0:7d5ec759888b | 183 | // create ObjectID with metadata tag of '3200', which is 'digital input' |
Yogesh Pande |
0:7d5ec759888b | 184 | btn_object = M2MInterfaceFactory::create_object("3200"); |
Yogesh Pande |
0:7d5ec759888b | 185 | M2MObjectInstance* btn_inst = btn_object->create_object_instance(); |
Yogesh Pande |
0:7d5ec759888b | 186 | // create resource with ID '5501', which is digital input counter |
Yogesh Pande |
0:7d5ec759888b | 187 | M2MResource* btn_res = btn_inst->create_dynamic_resource("5501", "Button", |
Yogesh Pande |
0:7d5ec759888b | 188 | M2MResourceInstance::INTEGER, true /* observable */); |
Yogesh Pande |
0:7d5ec759888b | 189 | // we can read this value |
Yogesh Pande |
0:7d5ec759888b | 190 | btn_res->set_operation(M2MBase::GET_ALLOWED); |
Yogesh Pande |
0:7d5ec759888b | 191 | // set initial value (all values in mbed Client are buffers) |
Yogesh Pande |
0:7d5ec759888b | 192 | // to be able to read this data easily in the Connector console, we'll use a string |
Yogesh Pande |
0:7d5ec759888b | 193 | btn_res->set_value((uint8_t*)"0", 1); |
Yogesh Pande |
0:7d5ec759888b | 194 | } |
Yogesh Pande |
0:7d5ec759888b | 195 | |
Yogesh Pande |
0:7d5ec759888b | 196 | ~ButtonResource() { |
Yogesh Pande |
0:7d5ec759888b | 197 | } |
Yogesh Pande |
0:7d5ec759888b | 198 | |
Yogesh Pande |
0:7d5ec759888b | 199 | M2MObject* get_object() { |
Yogesh Pande |
0:7d5ec759888b | 200 | return btn_object; |
Yogesh Pande |
0:7d5ec759888b | 201 | } |
Yogesh Pande |
0:7d5ec759888b | 202 | |
Yogesh Pande |
0:7d5ec759888b | 203 | /* |
Yogesh Pande |
0:7d5ec759888b | 204 | * When you press the button, we read the current value of the click counter |
Yogesh Pande |
0:7d5ec759888b | 205 | * from mbed Device Connector, then up the value with one. |
Yogesh Pande |
0:7d5ec759888b | 206 | */ |
Yogesh Pande |
0:7d5ec759888b | 207 | void handle_button_click() { |
Yogesh Pande |
0:7d5ec759888b | 208 | M2MObjectInstance* inst = btn_object->object_instance(); |
Yogesh Pande |
0:7d5ec759888b | 209 | M2MResource* res = inst->resource("5501"); |
Yogesh Pande |
0:7d5ec759888b | 210 | |
Yogesh Pande |
0:7d5ec759888b | 211 | // up counter |
Yogesh Pande |
0:7d5ec759888b | 212 | counter++; |
mbed_official | 5:b7d7ca715fdb | 213 | #ifdef TARGET_K64F |
Yogesh Pande |
0:7d5ec759888b | 214 | printf("handle_button_click, new value of counter is %d\r\n", counter); |
mbed_official | 5:b7d7ca715fdb | 215 | #else |
mbed_official | 5:b7d7ca715fdb | 216 | printf("simulate button_click, new value of counter is %d\r\n", counter); |
mbed_official | 5:b7d7ca715fdb | 217 | #endif |
Yogesh Pande |
0:7d5ec759888b | 218 | // serialize the value of counter as a string, and tell connector |
Yogesh Pande |
0:7d5ec759888b | 219 | char buffer[20]; |
Yogesh Pande |
0:7d5ec759888b | 220 | int size = sprintf(buffer,"%d",counter); |
Yogesh Pande |
0:7d5ec759888b | 221 | res->set_value((uint8_t*)buffer, size); |
Yogesh Pande |
0:7d5ec759888b | 222 | } |
Yogesh Pande |
0:7d5ec759888b | 223 | |
Yogesh Pande |
0:7d5ec759888b | 224 | private: |
Yogesh Pande |
0:7d5ec759888b | 225 | M2MObject* btn_object; |
Yogesh Pande |
0:7d5ec759888b | 226 | uint16_t counter; |
Yogesh Pande |
0:7d5ec759888b | 227 | }; |
Yogesh Pande |
0:7d5ec759888b | 228 | |
Yogesh Pande |
0:7d5ec759888b | 229 | // Network interaction must be performed outside of interrupt context |
Yogesh Pande |
0:7d5ec759888b | 230 | Semaphore updates(0); |
Yogesh Pande |
0:7d5ec759888b | 231 | volatile bool registered = false; |
Yogesh Pande |
0:7d5ec759888b | 232 | volatile bool clicked = false; |
Yogesh Pande |
0:7d5ec759888b | 233 | osThreadId mainThread; |
Yogesh Pande |
0:7d5ec759888b | 234 | |
Yogesh Pande |
0:7d5ec759888b | 235 | void unregister() { |
Yogesh Pande |
0:7d5ec759888b | 236 | registered = false; |
Yogesh Pande |
0:7d5ec759888b | 237 | updates.release(); |
Yogesh Pande |
0:7d5ec759888b | 238 | } |
Yogesh Pande |
0:7d5ec759888b | 239 | |
Yogesh Pande |
0:7d5ec759888b | 240 | void button_clicked() { |
Yogesh Pande |
0:7d5ec759888b | 241 | clicked = true; |
Yogesh Pande |
0:7d5ec759888b | 242 | updates.release(); |
Yogesh Pande |
0:7d5ec759888b | 243 | } |
Yogesh Pande |
0:7d5ec759888b | 244 | |
Yogesh Pande |
0:7d5ec759888b | 245 | // debug printf function |
Yogesh Pande |
0:7d5ec759888b | 246 | void trace_printer(const char* str) { |
Yogesh Pande |
0:7d5ec759888b | 247 | printf("%s\r\n", str); |
Yogesh Pande |
0:7d5ec759888b | 248 | } |
Yogesh Pande |
0:7d5ec759888b | 249 | |
Yogesh Pande |
0:7d5ec759888b | 250 | // Status indication |
Yogesh Pande |
0:7d5ec759888b | 251 | Ticker status_ticker; |
Yogesh Pande |
0:7d5ec759888b | 252 | DigitalOut status_led(LED1); |
Yogesh Pande |
0:7d5ec759888b | 253 | void blinky() { status_led = !status_led; } |
Yogesh Pande |
0:7d5ec759888b | 254 | |
Yogesh Pande |
0:7d5ec759888b | 255 | |
Yogesh Pande |
0:7d5ec759888b | 256 | // Entry point to the program |
Yogesh Pande |
0:7d5ec759888b | 257 | int main() { |
mbed_official | 10:4a3aaf2e130a | 258 | |
mbed_official | 10:4a3aaf2e130a | 259 | #ifndef MBEDTLS_ENTROPY_HARDWARE_ALT |
mbed_official | 10:4a3aaf2e130a | 260 | |
mbed_official | 10:4a3aaf2e130a | 261 | #ifdef MBEDTLS_TEST_NULL_ENTROPY |
mbed_official | 10:4a3aaf2e130a | 262 | #warning "mbedTLS security feature is disabled. Connection will not be secure !! Implement proper hardware entropy for your selected hardware." |
mbed_official | 10:4a3aaf2e130a | 263 | |
mbed_official | 10:4a3aaf2e130a | 264 | #else |
mbed_official | 10:4a3aaf2e130a | 265 | |
mbed_official | 10:4a3aaf2e130a | 266 | #error "This hardware does not have entropy, endpoint will not register to Connector.\ |
mbed_official | 10:4a3aaf2e130a | 267 | You need to enable NULL ENTROPY for your application, but if this configuration change is made then no security is offered by mbed TLS.\ |
mbed_official | 10:4a3aaf2e130a | 268 | Add MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES and MBEDTLS_TEST_NULL_ENTROPY in mbed_app.json macros to register your endpoint." |
mbed_official | 10:4a3aaf2e130a | 269 | #endif |
mbed_official | 10:4a3aaf2e130a | 270 | |
mbed_official | 10:4a3aaf2e130a | 271 | #endif |
Yogesh Pande |
0:7d5ec759888b | 272 | status_ticker.attach_us(blinky, 250000); |
Yogesh Pande |
0:7d5ec759888b | 273 | |
Yogesh Pande |
0:7d5ec759888b | 274 | // Keep track of the main thread |
Yogesh Pande |
0:7d5ec759888b | 275 | mainThread = osThreadGetId(); |
Yogesh Pande |
0:7d5ec759888b | 276 | |
Yogesh Pande |
0:7d5ec759888b | 277 | // Sets the console baud-rate |
Yogesh Pande |
0:7d5ec759888b | 278 | output.baud(115200); |
Yogesh Pande |
0:7d5ec759888b | 279 | |
Yogesh Pande |
0:7d5ec759888b | 280 | output.printf("Starting mbed Client example...\r\n"); |
Yogesh Pande |
0:7d5ec759888b | 281 | |
Yogesh Pande |
0:7d5ec759888b | 282 | mbed_trace_init(); |
Yogesh Pande |
0:7d5ec759888b | 283 | mbed_trace_print_function_set(trace_printer); |
Yogesh Pande |
0:7d5ec759888b | 284 | |
Yogesh Pande |
0:7d5ec759888b | 285 | NetworkInterface *network_interface = 0; |
Yogesh Pande |
0:7d5ec759888b | 286 | int connect_success = -1; |
Yogesh Pande |
0:7d5ec759888b | 287 | #if MBED_CONF_APP_NETWORK_INTERFACE == WIFI |
Yogesh Pande |
0:7d5ec759888b | 288 | output.printf("\n\rUsing WiFi \r\n"); |
Yogesh Pande |
0:7d5ec759888b | 289 | output.printf("\n\rConnecting to WiFi..\r\n"); |
Yogesh Pande |
0:7d5ec759888b | 290 | connect_success = esp.connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD); |
Yogesh Pande |
0:7d5ec759888b | 291 | network_interface = &esp; |
Yogesh Pande |
0:7d5ec759888b | 292 | #elif MBED_CONF_APP_NETWORK_INTERFACE == ETHERNET |
Yogesh Pande |
0:7d5ec759888b | 293 | output.printf("Using Ethernet\r\n"); |
Yogesh Pande |
0:7d5ec759888b | 294 | connect_success = eth.connect(); |
Yogesh Pande |
0:7d5ec759888b | 295 | network_interface = ð |
Yogesh Pande |
0:7d5ec759888b | 296 | #endif |
Yogesh Pande |
0:7d5ec759888b | 297 | #ifdef MESH |
Yogesh Pande |
0:7d5ec759888b | 298 | output.printf("Using Mesh\r\n"); |
Yogesh Pande |
0:7d5ec759888b | 299 | output.printf("\n\rConnecting to Mesh..\r\n"); |
Yogesh Pande |
0:7d5ec759888b | 300 | connect_success = mesh.connect(); |
Yogesh Pande |
0:7d5ec759888b | 301 | network_interface = &mesh; |
Yogesh Pande |
0:7d5ec759888b | 302 | #endif |
Yogesh Pande |
0:7d5ec759888b | 303 | if(connect_success == 0) { |
Yogesh Pande |
0:7d5ec759888b | 304 | output.printf("\n\rConnected to Network successfully\r\n"); |
Yogesh Pande |
0:7d5ec759888b | 305 | } else { |
Yogesh Pande |
0:7d5ec759888b | 306 | output.printf("\n\rConnection to Network Failed %d! Exiting application....\r\n", connect_success); |
Yogesh Pande |
0:7d5ec759888b | 307 | return 0; |
Yogesh Pande |
0:7d5ec759888b | 308 | } |
Yogesh Pande |
0:7d5ec759888b | 309 | const char *ip_addr = network_interface->get_ip_address(); |
Yogesh Pande |
0:7d5ec759888b | 310 | if (ip_addr) { |
Yogesh Pande |
0:7d5ec759888b | 311 | output.printf("IP address %s\r\n", ip_addr); |
Yogesh Pande |
0:7d5ec759888b | 312 | } else { |
Yogesh Pande |
0:7d5ec759888b | 313 | output.printf("No IP address\r\n"); |
Yogesh Pande |
0:7d5ec759888b | 314 | } |
Yogesh Pande |
0:7d5ec759888b | 315 | |
Yogesh Pande |
0:7d5ec759888b | 316 | // we create our button and LED resources |
Yogesh Pande |
0:7d5ec759888b | 317 | ButtonResource button_resource; |
Yogesh Pande |
0:7d5ec759888b | 318 | LedResource led_resource; |
Yogesh Pande |
0:7d5ec759888b | 319 | |
mbed_official | 5:b7d7ca715fdb | 320 | #ifdef TARGET_K64F |
Yogesh Pande |
0:7d5ec759888b | 321 | // On press of SW3 button on K64F board, example application |
Yogesh Pande |
0:7d5ec759888b | 322 | // will call unregister API towards mbed Device Connector |
Yogesh Pande |
0:7d5ec759888b | 323 | //unreg_button.fall(&mbed_client,&MbedClient::test_unregister); |
Yogesh Pande |
0:7d5ec759888b | 324 | unreg_button.fall(&unregister); |
Yogesh Pande |
0:7d5ec759888b | 325 | |
Yogesh Pande |
0:7d5ec759888b | 326 | // Observation Button (SW2) press will send update of endpoint resource values to connector |
Yogesh Pande |
0:7d5ec759888b | 327 | obs_button.fall(&button_clicked); |
mbed_official | 5:b7d7ca715fdb | 328 | #else |
mbed_official | 5:b7d7ca715fdb | 329 | // Send update of endpoint resource values to connector every 15 seconds periodically |
mbed_official | 5:b7d7ca715fdb | 330 | timer.attach(&button_clicked, 15.0); |
mbed_official | 5:b7d7ca715fdb | 331 | #endif |
Yogesh Pande |
0:7d5ec759888b | 332 | |
Yogesh Pande |
0:7d5ec759888b | 333 | // Create endpoint interface to manage register and unregister |
Yogesh Pande |
0:7d5ec759888b | 334 | mbed_client.create_interface(MBED_SERVER_ADDRESS, network_interface); |
Yogesh Pande |
0:7d5ec759888b | 335 | |
Yogesh Pande |
0:7d5ec759888b | 336 | // Create Objects of varying types, see simpleclient.h for more details on implementation. |
Yogesh Pande |
0:7d5ec759888b | 337 | M2MSecurity* register_object = mbed_client.create_register_object(); // server object specifying connector info |
Yogesh Pande |
0:7d5ec759888b | 338 | M2MDevice* device_object = mbed_client.create_device_object(); // device resources object |
Yogesh Pande |
0:7d5ec759888b | 339 | |
Yogesh Pande |
0:7d5ec759888b | 340 | // Create list of Objects to register |
Yogesh Pande |
0:7d5ec759888b | 341 | M2MObjectList object_list; |
Yogesh Pande |
0:7d5ec759888b | 342 | |
Yogesh Pande |
0:7d5ec759888b | 343 | // Add objects to list |
Yogesh Pande |
0:7d5ec759888b | 344 | object_list.push_back(device_object); |
Yogesh Pande |
0:7d5ec759888b | 345 | object_list.push_back(button_resource.get_object()); |
Yogesh Pande |
0:7d5ec759888b | 346 | object_list.push_back(led_resource.get_object()); |
Yogesh Pande |
0:7d5ec759888b | 347 | |
Yogesh Pande |
0:7d5ec759888b | 348 | // Set endpoint registration object |
Yogesh Pande |
0:7d5ec759888b | 349 | mbed_client.set_register_object(register_object); |
Yogesh Pande |
0:7d5ec759888b | 350 | |
Yogesh Pande |
0:7d5ec759888b | 351 | // Register with mbed Device Connector |
Yogesh Pande |
0:7d5ec759888b | 352 | mbed_client.test_register(register_object, object_list); |
Yogesh Pande |
0:7d5ec759888b | 353 | registered = true; |
Yogesh Pande |
0:7d5ec759888b | 354 | |
Yogesh Pande |
0:7d5ec759888b | 355 | while (true) { |
Yogesh Pande |
0:7d5ec759888b | 356 | updates.wait(25000); |
Yogesh Pande |
0:7d5ec759888b | 357 | if(registered) { |
Yogesh Pande |
0:7d5ec759888b | 358 | if(!clicked) { |
Yogesh Pande |
0:7d5ec759888b | 359 | mbed_client.test_update_register(); |
Yogesh Pande |
0:7d5ec759888b | 360 | } |
Yogesh Pande |
0:7d5ec759888b | 361 | }else { |
Yogesh Pande |
0:7d5ec759888b | 362 | break; |
Yogesh Pande |
0:7d5ec759888b | 363 | } |
Yogesh Pande |
0:7d5ec759888b | 364 | if(clicked) { |
Yogesh Pande |
0:7d5ec759888b | 365 | clicked = false; |
Yogesh Pande |
0:7d5ec759888b | 366 | button_resource.handle_button_click(); |
Yogesh Pande |
0:7d5ec759888b | 367 | } |
Yogesh Pande |
0:7d5ec759888b | 368 | } |
Yogesh Pande |
0:7d5ec759888b | 369 | |
Yogesh Pande |
0:7d5ec759888b | 370 | mbed_client.test_unregister(); |
Yogesh Pande |
0:7d5ec759888b | 371 | status_ticker.detach(); |
Yogesh Pande |
0:7d5ec759888b | 372 | } |