![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
Embed client working with Cell network.
Dependencies: C027Interface
Fork of U_Blox_DeviceConnector by
main.cpp@44:2b472e66a942, 2016-12-13 (annotated)
- Committer:
- mbed_official
- Date:
- Tue Dec 13 08:45:11 2016 +0000
- Revision:
- 44:2b472e66a942
- Parent:
- 42:d9a3043214a6
- Child:
- 49:10f84adec19e
Fix UBLOX ODIN Wifi Connectivity, add IPVx print
Turns out the UBLOX ODIN WiFi will not be able to connect, unless you
specify the security mode as well.
Stylize the connectivity prints a bit and add IPv4/IPv6 as the
to the starting print.
.
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 |
---|---|---|---|
mbed_official | 21:b88cdeb5b302 | 1 | /* |
mbed_official | 44:2b472e66a942 | 2 | * Copyright (c) 2015, 2016 ARM Limited. All rights reserved. |
mbed_official | 21:b88cdeb5b302 | 3 | * SPDX-License-Identifier: Apache-2.0 |
mbed_official | 21:b88cdeb5b302 | 4 | * Licensed under the Apache License, Version 2.0 (the License); you may |
mbed_official | 21:b88cdeb5b302 | 5 | * not use this file except in compliance with the License. |
mbed_official | 21:b88cdeb5b302 | 6 | * You may obtain a copy of the License at |
mbed_official | 21:b88cdeb5b302 | 7 | * |
mbed_official | 21:b88cdeb5b302 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
mbed_official | 21:b88cdeb5b302 | 9 | * |
mbed_official | 21:b88cdeb5b302 | 10 | * Unless required by applicable law or agreed to in writing, software |
mbed_official | 21:b88cdeb5b302 | 11 | * distributed under the License is distributed on an AS IS BASIS, WITHOUT |
mbed_official | 21:b88cdeb5b302 | 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
mbed_official | 21:b88cdeb5b302 | 13 | * See the License for the specific language governing permissions and |
mbed_official | 21:b88cdeb5b302 | 14 | * limitations under the License. |
mbed_official | 21:b88cdeb5b302 | 15 | */ |
mbed_official | 21:b88cdeb5b302 | 16 | #include "simpleclient.h" |
mbed_official | 21:b88cdeb5b302 | 17 | #include <string> |
mbed_official | 21:b88cdeb5b302 | 18 | #include <sstream> |
mbed_official | 21:b88cdeb5b302 | 19 | #include <vector> |
mbed_official | 21:b88cdeb5b302 | 20 | #include "mbed-trace/mbed_trace.h" |
mbed_official | 21:b88cdeb5b302 | 21 | #include "mbedtls/entropy_poll.h" |
mbed_official | 21:b88cdeb5b302 | 22 | |
mbed_official | 21:b88cdeb5b302 | 23 | #include "security.h" |
mbed_official | 21:b88cdeb5b302 | 24 | |
mbed_official | 21:b88cdeb5b302 | 25 | #include "mbed.h" |
mbed_official | 21:b88cdeb5b302 | 26 | #include "rtos.h" |
mbed_official | 21:b88cdeb5b302 | 27 | |
mbed_official | 21:b88cdeb5b302 | 28 | #if MBED_CONF_APP_NETWORK_INTERFACE == WIFI |
mbed_official | 41:496e5ec1f1bd | 29 | #if TARGET_UBLOX_EVK_ODIN_W2 |
mbed_official | 41:496e5ec1f1bd | 30 | #include "OdinWiFiInterface.h" |
mbed_official | 41:496e5ec1f1bd | 31 | OdinWiFiInterface wifi; |
mbed_official | 41:496e5ec1f1bd | 32 | #else |
mbed_official | 41:496e5ec1f1bd | 33 | #include "ESP8266Interface.h" |
mbed_official | 41:496e5ec1f1bd | 34 | ESP8266Interface wifi(MBED_CONF_APP_WIFI_TX, MBED_CONF_APP_WIFI_RX); |
mbed_official | 41:496e5ec1f1bd | 35 | #endif |
mbed_official | 21:b88cdeb5b302 | 36 | #elif MBED_CONF_APP_NETWORK_INTERFACE == ETHERNET |
mbed_official | 41:496e5ec1f1bd | 37 | #include "EthernetInterface.h" |
mbed_official | 41:496e5ec1f1bd | 38 | EthernetInterface eth; |
mbed_official | 21:b88cdeb5b302 | 39 | #elif MBED_CONF_APP_NETWORK_INTERFACE == MESH_LOWPAN_ND |
mbed_official | 41:496e5ec1f1bd | 40 | #define MESH |
mbed_official | 41:496e5ec1f1bd | 41 | #include "NanostackInterface.h" |
mbed_official | 41:496e5ec1f1bd | 42 | LoWPANNDInterface mesh; |
mbed_official | 21:b88cdeb5b302 | 43 | #elif MBED_CONF_APP_NETWORK_INTERFACE == MESH_THREAD |
mbed_official | 41:496e5ec1f1bd | 44 | #define MESH |
mbed_official | 41:496e5ec1f1bd | 45 | #include "NanostackInterface.h" |
mbed_official | 41:496e5ec1f1bd | 46 | ThreadInterface mesh; |
mbed_official | 21:b88cdeb5b302 | 47 | #endif |
mbed_official | 21:b88cdeb5b302 | 48 | |
mbed_official | 21:b88cdeb5b302 | 49 | #if defined(MESH) |
mbed_official | 21:b88cdeb5b302 | 50 | #if MBED_CONF_APP_MESH_RADIO_TYPE == ATMEL |
mbed_official | 21:b88cdeb5b302 | 51 | #include "NanostackRfPhyAtmel.h" |
mbed_official | 21:b88cdeb5b302 | 52 | NanostackRfPhyAtmel rf_phy(ATMEL_SPI_MOSI, ATMEL_SPI_MISO, ATMEL_SPI_SCLK, ATMEL_SPI_CS, |
mbed_official | 21:b88cdeb5b302 | 53 | ATMEL_SPI_RST, ATMEL_SPI_SLP, ATMEL_SPI_IRQ, ATMEL_I2C_SDA, ATMEL_I2C_SCL); |
mbed_official | 21:b88cdeb5b302 | 54 | #elif MBED_CONF_APP_MESH_RADIO_TYPE == MCR20 |
mbed_official | 21:b88cdeb5b302 | 55 | #include "NanostackRfPhyMcr20a.h" |
mbed_official | 21:b88cdeb5b302 | 56 | NanostackRfPhyMcr20a rf_phy(MCR20A_SPI_MOSI, MCR20A_SPI_MISO, MCR20A_SPI_SCLK, MCR20A_SPI_CS, MCR20A_SPI_RST, MCR20A_SPI_IRQ); |
mbed_official | 21:b88cdeb5b302 | 57 | #endif //MBED_CONF_APP_RADIO_TYPE |
mbed_official | 21:b88cdeb5b302 | 58 | #endif //MESH |
mbed_official | 21:b88cdeb5b302 | 59 | |
mbed_official | 42:d9a3043214a6 | 60 | #ifdef MESH |
mbed_official | 42:d9a3043214a6 | 61 | // Mesh does not have DNS, so must use direct IPV6 address |
mbed_official | 42:d9a3043214a6 | 62 | #define MBED_SERVER_ADDRESS "coaps://[2607:f0d0:2601:52::20]:5684" |
mbed_official | 21:b88cdeb5b302 | 63 | #else |
mbed_official | 42:d9a3043214a6 | 64 | // This is address to mbed Device Connector, name based |
mbed_official | 42:d9a3043214a6 | 65 | // assume all other stacks support DNS properly |
mbed_official | 42:d9a3043214a6 | 66 | #define MBED_SERVER_ADDRESS "coap://api.connector.mbed.com:5684" |
mbed_official | 21:b88cdeb5b302 | 67 | #endif |
mbed_official | 21:b88cdeb5b302 | 68 | |
mbed_official | 39:4a161de53cde | 69 | RawSerial output(USBTX, USBRX); |
mbed_official | 21:b88cdeb5b302 | 70 | |
mbed_official | 21:b88cdeb5b302 | 71 | // Status indication |
mbed_official | 21:b88cdeb5b302 | 72 | DigitalOut red_led(LED1); |
mbed_official | 21:b88cdeb5b302 | 73 | DigitalOut green_led(LED2); |
mbed_official | 21:b88cdeb5b302 | 74 | DigitalOut blue_led(LED3); |
mbed_official | 21:b88cdeb5b302 | 75 | Ticker status_ticker; |
mbed_official | 21:b88cdeb5b302 | 76 | void blinky() { |
mbed_official | 21:b88cdeb5b302 | 77 | green_led = !green_led; |
mbed_official | 21:b88cdeb5b302 | 78 | |
mbed_official | 21:b88cdeb5b302 | 79 | } |
mbed_official | 21:b88cdeb5b302 | 80 | |
mbed_official | 21:b88cdeb5b302 | 81 | // These are example resource values for the Device Object |
mbed_official | 21:b88cdeb5b302 | 82 | struct MbedClientDevice device = { |
mbed_official | 21:b88cdeb5b302 | 83 | "Manufacturer_String", // Manufacturer |
mbed_official | 21:b88cdeb5b302 | 84 | "Type_String", // Type |
mbed_official | 21:b88cdeb5b302 | 85 | "ModelNumber_String", // ModelNumber |
mbed_official | 21:b88cdeb5b302 | 86 | "SerialNumber_String" // SerialNumber |
mbed_official | 21:b88cdeb5b302 | 87 | }; |
mbed_official | 21:b88cdeb5b302 | 88 | |
mbed_official | 21:b88cdeb5b302 | 89 | // Instantiate the class which implements LWM2M Client API (from simpleclient.h) |
mbed_official | 21:b88cdeb5b302 | 90 | MbedClient mbed_client(device); |
mbed_official | 21:b88cdeb5b302 | 91 | |
mbed_official | 21:b88cdeb5b302 | 92 | |
mbed_official | 21:b88cdeb5b302 | 93 | // In case of K64F board , there is button resource available |
mbed_official | 21:b88cdeb5b302 | 94 | // to change resource value and unregister |
mbed_official | 21:b88cdeb5b302 | 95 | #ifdef TARGET_K64F |
mbed_official | 21:b88cdeb5b302 | 96 | // Set up Hardware interrupt button. |
mbed_official | 21:b88cdeb5b302 | 97 | InterruptIn obs_button(SW2); |
mbed_official | 21:b88cdeb5b302 | 98 | InterruptIn unreg_button(SW3); |
mbed_official | 21:b88cdeb5b302 | 99 | #else |
mbed_official | 21:b88cdeb5b302 | 100 | //In non K64F boards , set up a timer to simulate updating resource, |
mbed_official | 21:b88cdeb5b302 | 101 | // there is no functionality to unregister. |
mbed_official | 21:b88cdeb5b302 | 102 | Ticker timer; |
mbed_official | 21:b88cdeb5b302 | 103 | #endif |
mbed_official | 21:b88cdeb5b302 | 104 | |
mbed_official | 21:b88cdeb5b302 | 105 | /* |
mbed_official | 21:b88cdeb5b302 | 106 | * Arguments for running "blink" in it's own thread. |
mbed_official | 21:b88cdeb5b302 | 107 | */ |
mbed_official | 21:b88cdeb5b302 | 108 | class BlinkArgs { |
mbed_official | 21:b88cdeb5b302 | 109 | public: |
mbed_official | 21:b88cdeb5b302 | 110 | BlinkArgs() { |
mbed_official | 21:b88cdeb5b302 | 111 | clear(); |
mbed_official | 21:b88cdeb5b302 | 112 | } |
mbed_official | 21:b88cdeb5b302 | 113 | void clear() { |
mbed_official | 21:b88cdeb5b302 | 114 | position = 0; |
mbed_official | 21:b88cdeb5b302 | 115 | blink_pattern.clear(); |
mbed_official | 21:b88cdeb5b302 | 116 | } |
mbed_official | 21:b88cdeb5b302 | 117 | uint16_t position; |
mbed_official | 21:b88cdeb5b302 | 118 | std::vector<uint32_t> blink_pattern; |
mbed_official | 21:b88cdeb5b302 | 119 | }; |
mbed_official | 21:b88cdeb5b302 | 120 | |
mbed_official | 21:b88cdeb5b302 | 121 | /* |
mbed_official | 21:b88cdeb5b302 | 122 | * The Led contains one property (pattern) and a function (blink). |
mbed_official | 21:b88cdeb5b302 | 123 | * When the function blink is executed, the pattern is read, and the LED |
mbed_official | 21:b88cdeb5b302 | 124 | * will blink based on the pattern. |
mbed_official | 21:b88cdeb5b302 | 125 | */ |
mbed_official | 21:b88cdeb5b302 | 126 | class LedResource { |
mbed_official | 21:b88cdeb5b302 | 127 | public: |
mbed_official | 21:b88cdeb5b302 | 128 | LedResource() { |
mbed_official | 21:b88cdeb5b302 | 129 | // create ObjectID with metadata tag of '3201', which is 'digital output' |
mbed_official | 21:b88cdeb5b302 | 130 | led_object = M2MInterfaceFactory::create_object("3201"); |
mbed_official | 21:b88cdeb5b302 | 131 | M2MObjectInstance* led_inst = led_object->create_object_instance(); |
mbed_official | 21:b88cdeb5b302 | 132 | |
mbed_official | 21:b88cdeb5b302 | 133 | // 5853 = Multi-state output |
mbed_official | 21:b88cdeb5b302 | 134 | M2MResource* pattern_res = led_inst->create_dynamic_resource("5853", "Pattern", |
mbed_official | 21:b88cdeb5b302 | 135 | M2MResourceInstance::STRING, false); |
mbed_official | 21:b88cdeb5b302 | 136 | // read and write |
mbed_official | 21:b88cdeb5b302 | 137 | pattern_res->set_operation(M2MBase::GET_PUT_ALLOWED); |
mbed_official | 21:b88cdeb5b302 | 138 | // set initial pattern (toggle every 200ms. 7 toggles in total) |
mbed_official | 21:b88cdeb5b302 | 139 | pattern_res->set_value((const uint8_t*)"500:500:500:500:500:500:500", 27); |
mbed_official | 21:b88cdeb5b302 | 140 | |
mbed_official | 21:b88cdeb5b302 | 141 | // there's not really an execute LWM2M ID that matches... hmm... |
mbed_official | 21:b88cdeb5b302 | 142 | M2MResource* led_res = led_inst->create_dynamic_resource("5850", "Blink", |
mbed_official | 21:b88cdeb5b302 | 143 | M2MResourceInstance::OPAQUE, false); |
mbed_official | 21:b88cdeb5b302 | 144 | // we allow executing a function here... |
mbed_official | 21:b88cdeb5b302 | 145 | led_res->set_operation(M2MBase::POST_ALLOWED); |
mbed_official | 21:b88cdeb5b302 | 146 | // when a POST comes in, we want to execute the led_execute_callback |
mbed_official | 21:b88cdeb5b302 | 147 | led_res->set_execute_function(execute_callback(this, &LedResource::blink)); |
mbed_official | 21:b88cdeb5b302 | 148 | // Completion of execute function can take a time, that's why delayed response is used |
mbed_official | 21:b88cdeb5b302 | 149 | led_res->set_delayed_response(true); |
mbed_official | 21:b88cdeb5b302 | 150 | blink_args = new BlinkArgs(); |
mbed_official | 21:b88cdeb5b302 | 151 | } |
mbed_official | 21:b88cdeb5b302 | 152 | |
mbed_official | 21:b88cdeb5b302 | 153 | ~LedResource() { |
mbed_official | 21:b88cdeb5b302 | 154 | delete blink_args; |
mbed_official | 21:b88cdeb5b302 | 155 | } |
mbed_official | 21:b88cdeb5b302 | 156 | |
mbed_official | 21:b88cdeb5b302 | 157 | M2MObject* get_object() { |
mbed_official | 21:b88cdeb5b302 | 158 | return led_object; |
mbed_official | 21:b88cdeb5b302 | 159 | } |
mbed_official | 21:b88cdeb5b302 | 160 | |
mbed_official | 21:b88cdeb5b302 | 161 | void blink(void *argument) { |
mbed_official | 21:b88cdeb5b302 | 162 | // read the value of 'Pattern' |
mbed_official | 21:b88cdeb5b302 | 163 | status_ticker.detach(); |
mbed_official | 21:b88cdeb5b302 | 164 | green_led = 1; |
mbed_official | 21:b88cdeb5b302 | 165 | |
mbed_official | 21:b88cdeb5b302 | 166 | M2MObjectInstance* inst = led_object->object_instance(); |
mbed_official | 21:b88cdeb5b302 | 167 | M2MResource* res = inst->resource("5853"); |
mbed_official | 21:b88cdeb5b302 | 168 | // Clear previous blink data |
mbed_official | 21:b88cdeb5b302 | 169 | blink_args->clear(); |
mbed_official | 21:b88cdeb5b302 | 170 | |
mbed_official | 21:b88cdeb5b302 | 171 | // values in mbed Client are all buffers, and we need a vector of int's |
mbed_official | 21:b88cdeb5b302 | 172 | uint8_t* buffIn = NULL; |
mbed_official | 21:b88cdeb5b302 | 173 | uint32_t sizeIn; |
mbed_official | 21:b88cdeb5b302 | 174 | res->get_value(buffIn, sizeIn); |
mbed_official | 21:b88cdeb5b302 | 175 | |
mbed_official | 21:b88cdeb5b302 | 176 | // turn the buffer into a string, and initialize a vector<int> on the heap |
mbed_official | 21:b88cdeb5b302 | 177 | std::string s((char*)buffIn, sizeIn); |
mbed_official | 21:b88cdeb5b302 | 178 | free(buffIn); |
mbed_official | 21:b88cdeb5b302 | 179 | output.printf("led_execute_callback pattern=%s\r\n", s.c_str()); |
mbed_official | 21:b88cdeb5b302 | 180 | |
mbed_official | 21:b88cdeb5b302 | 181 | // our pattern is something like 500:200:500, so parse that |
mbed_official | 21:b88cdeb5b302 | 182 | std::size_t found = s.find_first_of(":"); |
mbed_official | 21:b88cdeb5b302 | 183 | while (found!=std::string::npos) { |
mbed_official | 21:b88cdeb5b302 | 184 | blink_args->blink_pattern.push_back(atoi((const char*)s.substr(0,found).c_str())); |
mbed_official | 21:b88cdeb5b302 | 185 | s = s.substr(found+1); |
mbed_official | 21:b88cdeb5b302 | 186 | found=s.find_first_of(":"); |
mbed_official | 21:b88cdeb5b302 | 187 | if(found == std::string::npos) { |
mbed_official | 21:b88cdeb5b302 | 188 | blink_args->blink_pattern.push_back(atoi((const char*)s.c_str())); |
mbed_official | 21:b88cdeb5b302 | 189 | } |
mbed_official | 21:b88cdeb5b302 | 190 | } |
mbed_official | 21:b88cdeb5b302 | 191 | // check if POST contains payload |
mbed_official | 21:b88cdeb5b302 | 192 | if (argument) { |
mbed_official | 21:b88cdeb5b302 | 193 | M2MResource::M2MExecuteParameter* param = (M2MResource::M2MExecuteParameter*)argument; |
mbed_official | 21:b88cdeb5b302 | 194 | String object_name = param->get_argument_object_name(); |
mbed_official | 21:b88cdeb5b302 | 195 | uint16_t object_instance_id = param->get_argument_object_instance_id(); |
mbed_official | 21:b88cdeb5b302 | 196 | String resource_name = param->get_argument_resource_name(); |
mbed_official | 21:b88cdeb5b302 | 197 | int payload_length = param->get_argument_value_length(); |
mbed_official | 21:b88cdeb5b302 | 198 | uint8_t* payload = param->get_argument_value(); |
mbed_official | 21:b88cdeb5b302 | 199 | output.printf("Resource: %s/%d/%s executed\r\n", object_name.c_str(), object_instance_id, resource_name.c_str()); |
mbed_official | 21:b88cdeb5b302 | 200 | output.printf("Payload: %.*s\r\n", payload_length, payload); |
mbed_official | 21:b88cdeb5b302 | 201 | } |
mbed_official | 21:b88cdeb5b302 | 202 | // do_blink is called with the vector, and starting at -1 |
mbed_official | 21:b88cdeb5b302 | 203 | blinky_thread.start(this, &LedResource::do_blink); |
mbed_official | 21:b88cdeb5b302 | 204 | } |
mbed_official | 21:b88cdeb5b302 | 205 | |
mbed_official | 21:b88cdeb5b302 | 206 | private: |
mbed_official | 21:b88cdeb5b302 | 207 | M2MObject* led_object; |
mbed_official | 21:b88cdeb5b302 | 208 | Thread blinky_thread; |
mbed_official | 21:b88cdeb5b302 | 209 | BlinkArgs *blink_args; |
mbed_official | 21:b88cdeb5b302 | 210 | void do_blink() { |
mbed_official | 21:b88cdeb5b302 | 211 | // blink the LED |
mbed_official | 21:b88cdeb5b302 | 212 | red_led = !red_led; |
mbed_official | 21:b88cdeb5b302 | 213 | // up the position, if we reached the end of the vector |
mbed_official | 21:b88cdeb5b302 | 214 | if (blink_args->position >= blink_args->blink_pattern.size()) { |
mbed_official | 21:b88cdeb5b302 | 215 | // send delayed response after blink is done |
mbed_official | 21:b88cdeb5b302 | 216 | M2MObjectInstance* inst = led_object->object_instance(); |
mbed_official | 21:b88cdeb5b302 | 217 | M2MResource* led_res = inst->resource("5850"); |
mbed_official | 21:b88cdeb5b302 | 218 | led_res->send_delayed_post_response(); |
mbed_official | 21:b88cdeb5b302 | 219 | red_led = 1; |
mbed_official | 21:b88cdeb5b302 | 220 | status_ticker.attach_us(blinky, 250000); |
mbed_official | 21:b88cdeb5b302 | 221 | return; |
mbed_official | 21:b88cdeb5b302 | 222 | } |
mbed_official | 21:b88cdeb5b302 | 223 | // Invoke same function after `delay_ms` (upping position) |
mbed_official | 21:b88cdeb5b302 | 224 | Thread::wait(blink_args->blink_pattern.at(blink_args->position)); |
mbed_official | 21:b88cdeb5b302 | 225 | blink_args->position++; |
mbed_official | 21:b88cdeb5b302 | 226 | do_blink(); |
mbed_official | 21:b88cdeb5b302 | 227 | } |
mbed_official | 21:b88cdeb5b302 | 228 | }; |
mbed_official | 21:b88cdeb5b302 | 229 | |
mbed_official | 21:b88cdeb5b302 | 230 | /* |
mbed_official | 21:b88cdeb5b302 | 231 | * The button contains one property (click count). |
mbed_official | 21:b88cdeb5b302 | 232 | * When `handle_button_click` is executed, the counter updates. |
mbed_official | 21:b88cdeb5b302 | 233 | */ |
mbed_official | 21:b88cdeb5b302 | 234 | class ButtonResource { |
mbed_official | 21:b88cdeb5b302 | 235 | public: |
mbed_official | 21:b88cdeb5b302 | 236 | ButtonResource(): counter(0) { |
mbed_official | 21:b88cdeb5b302 | 237 | // create ObjectID with metadata tag of '3200', which is 'digital input' |
mbed_official | 21:b88cdeb5b302 | 238 | btn_object = M2MInterfaceFactory::create_object("3200"); |
mbed_official | 21:b88cdeb5b302 | 239 | M2MObjectInstance* btn_inst = btn_object->create_object_instance(); |
mbed_official | 21:b88cdeb5b302 | 240 | // create resource with ID '5501', which is digital input counter |
mbed_official | 21:b88cdeb5b302 | 241 | M2MResource* btn_res = btn_inst->create_dynamic_resource("5501", "Button", |
mbed_official | 21:b88cdeb5b302 | 242 | M2MResourceInstance::INTEGER, true /* observable */); |
mbed_official | 21:b88cdeb5b302 | 243 | // we can read this value |
mbed_official | 21:b88cdeb5b302 | 244 | btn_res->set_operation(M2MBase::GET_ALLOWED); |
mbed_official | 21:b88cdeb5b302 | 245 | // set initial value (all values in mbed Client are buffers) |
mbed_official | 21:b88cdeb5b302 | 246 | // to be able to read this data easily in the Connector console, we'll use a string |
mbed_official | 21:b88cdeb5b302 | 247 | btn_res->set_value((uint8_t*)"0", 1); |
mbed_official | 21:b88cdeb5b302 | 248 | } |
mbed_official | 21:b88cdeb5b302 | 249 | |
mbed_official | 21:b88cdeb5b302 | 250 | ~ButtonResource() { |
mbed_official | 21:b88cdeb5b302 | 251 | } |
mbed_official | 21:b88cdeb5b302 | 252 | |
mbed_official | 21:b88cdeb5b302 | 253 | M2MObject* get_object() { |
mbed_official | 21:b88cdeb5b302 | 254 | return btn_object; |
mbed_official | 21:b88cdeb5b302 | 255 | } |
mbed_official | 21:b88cdeb5b302 | 256 | |
mbed_official | 21:b88cdeb5b302 | 257 | /* |
mbed_official | 21:b88cdeb5b302 | 258 | * When you press the button, we read the current value of the click counter |
mbed_official | 21:b88cdeb5b302 | 259 | * from mbed Device Connector, then up the value with one. |
mbed_official | 21:b88cdeb5b302 | 260 | */ |
mbed_official | 21:b88cdeb5b302 | 261 | void handle_button_click() { |
mbed_official | 21:b88cdeb5b302 | 262 | M2MObjectInstance* inst = btn_object->object_instance(); |
mbed_official | 21:b88cdeb5b302 | 263 | M2MResource* res = inst->resource("5501"); |
mbed_official | 21:b88cdeb5b302 | 264 | |
mbed_official | 21:b88cdeb5b302 | 265 | // up counter |
mbed_official | 21:b88cdeb5b302 | 266 | counter++; |
mbed_official | 21:b88cdeb5b302 | 267 | #ifdef TARGET_K64F |
mbed_official | 21:b88cdeb5b302 | 268 | printf("handle_button_click, new value of counter is %d\r\n", counter); |
mbed_official | 21:b88cdeb5b302 | 269 | #else |
mbed_official | 21:b88cdeb5b302 | 270 | printf("simulate button_click, new value of counter is %d\r\n", counter); |
mbed_official | 21:b88cdeb5b302 | 271 | #endif |
mbed_official | 21:b88cdeb5b302 | 272 | // serialize the value of counter as a string, and tell connector |
mbed_official | 21:b88cdeb5b302 | 273 | char buffer[20]; |
mbed_official | 21:b88cdeb5b302 | 274 | int size = sprintf(buffer,"%d",counter); |
mbed_official | 21:b88cdeb5b302 | 275 | res->set_value((uint8_t*)buffer, size); |
mbed_official | 21:b88cdeb5b302 | 276 | } |
mbed_official | 21:b88cdeb5b302 | 277 | |
mbed_official | 21:b88cdeb5b302 | 278 | private: |
mbed_official | 21:b88cdeb5b302 | 279 | M2MObject* btn_object; |
mbed_official | 21:b88cdeb5b302 | 280 | uint16_t counter; |
mbed_official | 21:b88cdeb5b302 | 281 | }; |
mbed_official | 21:b88cdeb5b302 | 282 | |
mbed_official | 21:b88cdeb5b302 | 283 | class BigPayloadResource { |
mbed_official | 21:b88cdeb5b302 | 284 | public: |
mbed_official | 21:b88cdeb5b302 | 285 | BigPayloadResource() { |
mbed_official | 21:b88cdeb5b302 | 286 | big_payload = M2MInterfaceFactory::create_object("1000"); |
mbed_official | 21:b88cdeb5b302 | 287 | M2MObjectInstance* payload_inst = big_payload->create_object_instance(); |
mbed_official | 21:b88cdeb5b302 | 288 | M2MResource* payload_res = payload_inst->create_dynamic_resource("1", "BigData", |
mbed_official | 21:b88cdeb5b302 | 289 | M2MResourceInstance::STRING, true /* observable */); |
mbed_official | 21:b88cdeb5b302 | 290 | payload_res->set_operation(M2MBase::GET_PUT_ALLOWED); |
mbed_official | 21:b88cdeb5b302 | 291 | payload_res->set_value((uint8_t*)"0", 1); |
mbed_official | 21:b88cdeb5b302 | 292 | payload_res->set_incoming_block_message_callback( |
mbed_official | 21:b88cdeb5b302 | 293 | incoming_block_message_callback(this, &BigPayloadResource::block_message_received)); |
mbed_official | 21:b88cdeb5b302 | 294 | payload_res->set_outgoing_block_message_callback( |
mbed_official | 21:b88cdeb5b302 | 295 | outgoing_block_message_callback(this, &BigPayloadResource::block_message_requested)); |
mbed_official | 21:b88cdeb5b302 | 296 | } |
mbed_official | 21:b88cdeb5b302 | 297 | |
mbed_official | 21:b88cdeb5b302 | 298 | M2MObject* get_object() { |
mbed_official | 21:b88cdeb5b302 | 299 | return big_payload; |
mbed_official | 21:b88cdeb5b302 | 300 | } |
mbed_official | 21:b88cdeb5b302 | 301 | |
mbed_official | 21:b88cdeb5b302 | 302 | void block_message_received(M2MBlockMessage *argument) { |
mbed_official | 21:b88cdeb5b302 | 303 | if (argument) { |
mbed_official | 21:b88cdeb5b302 | 304 | if (M2MBlockMessage::ErrorNone == argument->error_code()) { |
mbed_official | 21:b88cdeb5b302 | 305 | if (argument->is_last_block()) { |
mbed_official | 21:b88cdeb5b302 | 306 | output.printf("Last block received\r\n"); |
mbed_official | 21:b88cdeb5b302 | 307 | } |
mbed_official | 21:b88cdeb5b302 | 308 | output.printf("Block number: %d\r\n", argument->block_number()); |
mbed_official | 21:b88cdeb5b302 | 309 | // First block received |
mbed_official | 21:b88cdeb5b302 | 310 | if (argument->block_number() == 0) { |
mbed_official | 21:b88cdeb5b302 | 311 | // Store block |
mbed_official | 21:b88cdeb5b302 | 312 | // More blocks coming |
mbed_official | 21:b88cdeb5b302 | 313 | } else { |
mbed_official | 21:b88cdeb5b302 | 314 | // Store blocks |
mbed_official | 21:b88cdeb5b302 | 315 | } |
mbed_official | 21:b88cdeb5b302 | 316 | } else { |
mbed_official | 21:b88cdeb5b302 | 317 | output.printf("Error when receiving block message! - EntityTooLarge\r\n"); |
mbed_official | 21:b88cdeb5b302 | 318 | } |
mbed_official | 21:b88cdeb5b302 | 319 | output.printf("Total message size: %d\r\n", argument->total_message_size()); |
mbed_official | 21:b88cdeb5b302 | 320 | } |
mbed_official | 21:b88cdeb5b302 | 321 | } |
mbed_official | 21:b88cdeb5b302 | 322 | |
mbed_official | 21:b88cdeb5b302 | 323 | void block_message_requested(const String& resource, uint8_t *&/*data*/, uint32_t &/*len*/) { |
mbed_official | 21:b88cdeb5b302 | 324 | output.printf("GET request received for resource: %s\r\n", resource.c_str()); |
mbed_official | 21:b88cdeb5b302 | 325 | // Copy data and length to coap response |
mbed_official | 21:b88cdeb5b302 | 326 | } |
mbed_official | 21:b88cdeb5b302 | 327 | |
mbed_official | 21:b88cdeb5b302 | 328 | private: |
mbed_official | 21:b88cdeb5b302 | 329 | M2MObject* big_payload; |
mbed_official | 21:b88cdeb5b302 | 330 | }; |
mbed_official | 21:b88cdeb5b302 | 331 | |
mbed_official | 21:b88cdeb5b302 | 332 | // Network interaction must be performed outside of interrupt context |
mbed_official | 21:b88cdeb5b302 | 333 | Semaphore updates(0); |
mbed_official | 21:b88cdeb5b302 | 334 | volatile bool registered = false; |
mbed_official | 21:b88cdeb5b302 | 335 | volatile bool clicked = false; |
mbed_official | 21:b88cdeb5b302 | 336 | osThreadId mainThread; |
mbed_official | 21:b88cdeb5b302 | 337 | |
mbed_official | 21:b88cdeb5b302 | 338 | void unregister() { |
mbed_official | 21:b88cdeb5b302 | 339 | registered = false; |
mbed_official | 21:b88cdeb5b302 | 340 | updates.release(); |
mbed_official | 21:b88cdeb5b302 | 341 | } |
mbed_official | 21:b88cdeb5b302 | 342 | |
mbed_official | 21:b88cdeb5b302 | 343 | void button_clicked() { |
mbed_official | 21:b88cdeb5b302 | 344 | clicked = true; |
mbed_official | 21:b88cdeb5b302 | 345 | updates.release(); |
mbed_official | 21:b88cdeb5b302 | 346 | } |
mbed_official | 21:b88cdeb5b302 | 347 | |
mbed_official | 21:b88cdeb5b302 | 348 | // debug printf function |
mbed_official | 21:b88cdeb5b302 | 349 | void trace_printer(const char* str) { |
mbed_official | 21:b88cdeb5b302 | 350 | printf("%s\r\n", str); |
mbed_official | 21:b88cdeb5b302 | 351 | } |
mbed_official | 21:b88cdeb5b302 | 352 | |
mbed_official | 21:b88cdeb5b302 | 353 | // Entry point to the program |
mbed_official | 21:b88cdeb5b302 | 354 | int main() { |
mbed_official | 21:b88cdeb5b302 | 355 | |
mbed_official | 21:b88cdeb5b302 | 356 | unsigned int seed; |
mbed_official | 21:b88cdeb5b302 | 357 | size_t len; |
mbed_official | 21:b88cdeb5b302 | 358 | |
mbed_official | 21:b88cdeb5b302 | 359 | #ifdef MBEDTLS_ENTROPY_HARDWARE_ALT |
mbed_official | 21:b88cdeb5b302 | 360 | // Used to randomize source port |
mbed_official | 21:b88cdeb5b302 | 361 | mbedtls_hardware_poll(NULL, (unsigned char *) &seed, sizeof seed, &len); |
mbed_official | 21:b88cdeb5b302 | 362 | |
mbed_official | 21:b88cdeb5b302 | 363 | #elif defined MBEDTLS_TEST_NULL_ENTROPY |
mbed_official | 21:b88cdeb5b302 | 364 | |
mbed_official | 21:b88cdeb5b302 | 365 | #warning "mbedTLS security feature is disabled. Connection will not be secure !! Implement proper hardware entropy for your selected hardware." |
mbed_official | 21:b88cdeb5b302 | 366 | // Used to randomize source port |
mbed_official | 21:b88cdeb5b302 | 367 | mbedtls_null_entropy_poll( NULL,(unsigned char *) &seed, sizeof seed, &len); |
mbed_official | 21:b88cdeb5b302 | 368 | |
mbed_official | 21:b88cdeb5b302 | 369 | #else |
mbed_official | 21:b88cdeb5b302 | 370 | |
mbed_official | 21:b88cdeb5b302 | 371 | #error "This hardware does not have entropy, endpoint will not register to Connector.\ |
mbed_official | 21:b88cdeb5b302 | 372 | 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 | 21:b88cdeb5b302 | 373 | Add MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES and MBEDTLS_TEST_NULL_ENTROPY in mbed_app.json macros to register your endpoint." |
mbed_official | 21:b88cdeb5b302 | 374 | |
mbed_official | 21:b88cdeb5b302 | 375 | #endif |
mbed_official | 21:b88cdeb5b302 | 376 | |
mbed_official | 21:b88cdeb5b302 | 377 | srand(seed); |
mbed_official | 21:b88cdeb5b302 | 378 | red_led = 1; |
mbed_official | 21:b88cdeb5b302 | 379 | blue_led = 1; |
mbed_official | 21:b88cdeb5b302 | 380 | status_ticker.attach_us(blinky, 250000); |
mbed_official | 21:b88cdeb5b302 | 381 | // Keep track of the main thread |
mbed_official | 21:b88cdeb5b302 | 382 | mainThread = osThreadGetId(); |
mbed_official | 21:b88cdeb5b302 | 383 | |
mbed_official | 21:b88cdeb5b302 | 384 | // Sets the console baud-rate |
mbed_official | 21:b88cdeb5b302 | 385 | output.baud(115200); |
mbed_official | 21:b88cdeb5b302 | 386 | |
mbed_official | 44:2b472e66a942 | 387 | output.printf("\r\nStarting mbed Client example in "); |
mbed_official | 44:2b472e66a942 | 388 | #if defined (MESH) || (MBED_CONF_LWIP_IPV6_ENABLED==true) |
mbed_official | 44:2b472e66a942 | 389 | output.printf("IPv6 mode\r\n"); |
mbed_official | 44:2b472e66a942 | 390 | #else |
mbed_official | 44:2b472e66a942 | 391 | output.printf("IPv4 mode\r\n"); |
mbed_official | 44:2b472e66a942 | 392 | #endif |
mbed_official | 21:b88cdeb5b302 | 393 | |
mbed_official | 21:b88cdeb5b302 | 394 | mbed_trace_init(); |
mbed_official | 21:b88cdeb5b302 | 395 | mbed_trace_print_function_set(trace_printer); |
mbed_official | 44:2b472e66a942 | 396 | |
mbed_official | 21:b88cdeb5b302 | 397 | NetworkInterface *network_interface = 0; |
mbed_official | 21:b88cdeb5b302 | 398 | int connect_success = -1; |
mbed_official | 21:b88cdeb5b302 | 399 | #if MBED_CONF_APP_NETWORK_INTERFACE == WIFI |
mbed_official | 44:2b472e66a942 | 400 | output.printf("\n\rConnecting to WiFi...\r\n"); |
mbed_official | 44:2b472e66a942 | 401 | connect_success = wifi.connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2); |
mbed_official | 41:496e5ec1f1bd | 402 | network_interface = &wifi; |
mbed_official | 21:b88cdeb5b302 | 403 | #elif MBED_CONF_APP_NETWORK_INTERFACE == ETHERNET |
mbed_official | 44:2b472e66a942 | 404 | output.printf("\n\rConnecting to ethernet...\r\n"); |
mbed_official | 21:b88cdeb5b302 | 405 | connect_success = eth.connect(); |
mbed_official | 21:b88cdeb5b302 | 406 | network_interface = ð |
mbed_official | 21:b88cdeb5b302 | 407 | #endif |
mbed_official | 21:b88cdeb5b302 | 408 | #ifdef MESH |
mbed_official | 44:2b472e66a942 | 409 | output.printf("\n\rConnecting to Mesh...\r\n"); |
mbed_official | 21:b88cdeb5b302 | 410 | mesh.initialize(&rf_phy); |
mbed_official | 21:b88cdeb5b302 | 411 | connect_success = mesh.connect(); |
mbed_official | 21:b88cdeb5b302 | 412 | network_interface = &mesh; |
mbed_official | 21:b88cdeb5b302 | 413 | #endif |
mbed_official | 21:b88cdeb5b302 | 414 | if(connect_success == 0) { |
mbed_official | 21:b88cdeb5b302 | 415 | output.printf("\n\rConnected to Network successfully\r\n"); |
mbed_official | 21:b88cdeb5b302 | 416 | } else { |
mbed_official | 21:b88cdeb5b302 | 417 | output.printf("\n\rConnection to Network Failed %d! Exiting application....\r\n", connect_success); |
mbed_official | 21:b88cdeb5b302 | 418 | return 0; |
mbed_official | 21:b88cdeb5b302 | 419 | } |
mbed_official | 21:b88cdeb5b302 | 420 | const char *ip_addr = network_interface->get_ip_address(); |
mbed_official | 21:b88cdeb5b302 | 421 | if (ip_addr) { |
mbed_official | 21:b88cdeb5b302 | 422 | output.printf("IP address %s\r\n", ip_addr); |
mbed_official | 21:b88cdeb5b302 | 423 | } else { |
mbed_official | 21:b88cdeb5b302 | 424 | output.printf("No IP address\r\n"); |
mbed_official | 21:b88cdeb5b302 | 425 | } |
mbed_official | 21:b88cdeb5b302 | 426 | |
mbed_official | 21:b88cdeb5b302 | 427 | // we create our button and LED resources |
mbed_official | 21:b88cdeb5b302 | 428 | ButtonResource button_resource; |
mbed_official | 21:b88cdeb5b302 | 429 | LedResource led_resource; |
mbed_official | 21:b88cdeb5b302 | 430 | BigPayloadResource big_payload_resource; |
mbed_official | 21:b88cdeb5b302 | 431 | |
mbed_official | 21:b88cdeb5b302 | 432 | #ifdef TARGET_K64F |
mbed_official | 21:b88cdeb5b302 | 433 | // On press of SW3 button on K64F board, example application |
mbed_official | 21:b88cdeb5b302 | 434 | // will call unregister API towards mbed Device Connector |
mbed_official | 21:b88cdeb5b302 | 435 | //unreg_button.fall(&mbed_client,&MbedClient::test_unregister); |
mbed_official | 21:b88cdeb5b302 | 436 | unreg_button.fall(&unregister); |
mbed_official | 21:b88cdeb5b302 | 437 | |
mbed_official | 21:b88cdeb5b302 | 438 | // Observation Button (SW2) press will send update of endpoint resource values to connector |
mbed_official | 21:b88cdeb5b302 | 439 | obs_button.fall(&button_clicked); |
mbed_official | 21:b88cdeb5b302 | 440 | #else |
mbed_official | 21:b88cdeb5b302 | 441 | // Send update of endpoint resource values to connector every 15 seconds periodically |
mbed_official | 21:b88cdeb5b302 | 442 | timer.attach(&button_clicked, 15.0); |
mbed_official | 21:b88cdeb5b302 | 443 | #endif |
mbed_official | 21:b88cdeb5b302 | 444 | |
mbed_official | 21:b88cdeb5b302 | 445 | // Create endpoint interface to manage register and unregister |
mbed_official | 21:b88cdeb5b302 | 446 | mbed_client.create_interface(MBED_SERVER_ADDRESS, network_interface); |
mbed_official | 21:b88cdeb5b302 | 447 | |
mbed_official | 21:b88cdeb5b302 | 448 | // Create Objects of varying types, see simpleclient.h for more details on implementation. |
mbed_official | 21:b88cdeb5b302 | 449 | M2MSecurity* register_object = mbed_client.create_register_object(); // server object specifying connector info |
mbed_official | 21:b88cdeb5b302 | 450 | M2MDevice* device_object = mbed_client.create_device_object(); // device resources object |
mbed_official | 21:b88cdeb5b302 | 451 | |
mbed_official | 21:b88cdeb5b302 | 452 | // Create list of Objects to register |
mbed_official | 21:b88cdeb5b302 | 453 | M2MObjectList object_list; |
mbed_official | 21:b88cdeb5b302 | 454 | |
mbed_official | 21:b88cdeb5b302 | 455 | // Add objects to list |
mbed_official | 21:b88cdeb5b302 | 456 | object_list.push_back(device_object); |
mbed_official | 21:b88cdeb5b302 | 457 | object_list.push_back(button_resource.get_object()); |
mbed_official | 21:b88cdeb5b302 | 458 | object_list.push_back(led_resource.get_object()); |
mbed_official | 21:b88cdeb5b302 | 459 | object_list.push_back(big_payload_resource.get_object()); |
mbed_official | 21:b88cdeb5b302 | 460 | |
mbed_official | 21:b88cdeb5b302 | 461 | // Set endpoint registration object |
mbed_official | 21:b88cdeb5b302 | 462 | mbed_client.set_register_object(register_object); |
mbed_official | 21:b88cdeb5b302 | 463 | |
mbed_official | 21:b88cdeb5b302 | 464 | // Register with mbed Device Connector |
mbed_official | 21:b88cdeb5b302 | 465 | mbed_client.test_register(register_object, object_list); |
mbed_official | 21:b88cdeb5b302 | 466 | registered = true; |
mbed_official | 21:b88cdeb5b302 | 467 | |
mbed_official | 21:b88cdeb5b302 | 468 | while (true) { |
mbed_official | 21:b88cdeb5b302 | 469 | updates.wait(25000); |
mbed_official | 21:b88cdeb5b302 | 470 | if(registered) { |
mbed_official | 21:b88cdeb5b302 | 471 | if(!clicked) { |
mbed_official | 21:b88cdeb5b302 | 472 | mbed_client.test_update_register(); |
mbed_official | 21:b88cdeb5b302 | 473 | } |
mbed_official | 21:b88cdeb5b302 | 474 | }else { |
mbed_official | 21:b88cdeb5b302 | 475 | break; |
mbed_official | 21:b88cdeb5b302 | 476 | } |
mbed_official | 21:b88cdeb5b302 | 477 | if(clicked) { |
mbed_official | 21:b88cdeb5b302 | 478 | clicked = false; |
mbed_official | 21:b88cdeb5b302 | 479 | button_resource.handle_button_click(); |
mbed_official | 21:b88cdeb5b302 | 480 | } |
mbed_official | 21:b88cdeb5b302 | 481 | } |
mbed_official | 21:b88cdeb5b302 | 482 | |
mbed_official | 21:b88cdeb5b302 | 483 | mbed_client.test_unregister(); |
mbed_official | 21:b88cdeb5b302 | 484 | status_ticker.detach(); |
mbed_official | 21:b88cdeb5b302 | 485 | } |