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