mbed-os-example-client which can run on GR-PEACH as-is

Committer:
Osamu Nakamura
Date:
Mon Sep 04 17:13:29 2017 +0900
Revision:
1:297185d9bb5e
Parent:
0:c0b64bdd4f07
Update for compiling this program on online compiler

Who changed what in which revision?

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