Mbed Client sample for GR-LYCHEE where ZXing is incorporated.
Dependencies: DisplayApp AsciiFont
Fork of GR-PEACH_mbed-os-client-ZXingSample by
Overview
This sample program shows how to use mbed Client together with ZXing which is an open-source, multi-format 1D/2D barcode image processing library. For more info on ZXing, please refer to https://github.com/zxing/zxing.
Required hardware
- GR-LYCHEE ( https://developer.mbed.org/platforms/Renesas-GR-LYCHEE/ )
Application setup
- Select the connection type. For details, please refer to the following wiki:
https://os.mbed.com/users/1050186/code/GR-LYCHEE_mbed-os-client-ZXingSample/wiki/Connection-type. - Set the client credentials. For details, please refer to the following wiki:
https://os.mbed.com/users/1050186/code/GR-LYCHEE_mbed-os-client-ZXingSample/wiki/Client-credentials. - Change Wifi settings. For details, please refer to the following wiki:
https://os.mbed.com/users/1050186/code/GR-LYCHEE_mbed-os-client-ZXingSample/wiki/Wifi-settings.
Building the example
To build this example:
- Import this example onto mbed Compiler.
- Configure the example in accordance with Application setup.
- Compile the example on mbed Compiler and download the resultant binary file.
- Plug the micro-USB cable into the OpenSDA port which lies on the next to the RESET button.
- Copy the binary previously downloaded to your PC to GR-LYCHEE to flash this example. When the copy is successfully completed, the board is ready to work.
- Press the RESET button on the board to run the example.
- For verification, please refer to the following wiki:
https://os.mbed.com/users/1050186/code/GR-LYCHEE_mbed-os-client-ZXingSample/wiki/Monitoring-the-application.
Application resources
This example exposes four resources listed below:
- 3202/0/5700. Decode result of barcode data input from camera (GET).
- 3201/0/5850. Blink function, blinks LED when executed (POST).
- 3201/0/5853. Blink pattern, used by the blink function to determine how to blink. In the format of 1000:500:1000:500:1000:500 (PUT).
- 3201/0/5855. Blink color, used by the blink function. Any of green, yellow, orange and red is acceptable (PUT).
main.cpp@12:7a199eff6988, 2018-12-10 (annotated)
- Committer:
- 1050186
- Date:
- Mon Dec 10 11:04:55 2018 +0000
- Revision:
- 12:7a199eff6988
- Parent:
- 6:4e43b6f8f772
- Child:
- 18:0ab91e767950
Update the related mbed-os-client library, and Replace Renesas libs to mbed-gr-libs.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
<> | 0:eb73febb2bba | 1 | /* |
dkato | 2:6ec5c1c1d41c | 2 | * Copyright (c) 2015, 2016 ARM Limited. All rights reserved. |
<> | 0:eb73febb2bba | 3 | * SPDX-License-Identifier: Apache-2.0 |
<> | 0:eb73febb2bba | 4 | * Licensed under the Apache License, Version 2.0 (the License); you may |
<> | 0:eb73febb2bba | 5 | * not use this file except in compliance with the License. |
<> | 0:eb73febb2bba | 6 | * You may obtain a copy of the License at |
<> | 0:eb73febb2bba | 7 | * |
<> | 0:eb73febb2bba | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
<> | 0:eb73febb2bba | 9 | * |
<> | 0:eb73febb2bba | 10 | * Unless required by applicable law or agreed to in writing, software |
<> | 0:eb73febb2bba | 11 | * distributed under the License is distributed on an AS IS BASIS, WITHOUT |
<> | 0:eb73febb2bba | 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
<> | 0:eb73febb2bba | 13 | * See the License for the specific language governing permissions and |
<> | 0:eb73febb2bba | 14 | * limitations under the License. |
<> | 0:eb73febb2bba | 15 | */ |
dkato | 2:6ec5c1c1d41c | 16 | #include "mbed-trace/mbed_trace.h" |
dkato | 2:6ec5c1c1d41c | 17 | #include "mbed.h" |
dkato | 2:6ec5c1c1d41c | 18 | #include "zxing_main.h" |
dkato | 2:6ec5c1c1d41c | 19 | |
dkato | 2:6ec5c1c1d41c | 20 | // Network interaction must be performed outside of interrupt context |
dkato | 2:6ec5c1c1d41c | 21 | osThreadId mainThread; |
dkato | 2:6ec5c1c1d41c | 22 | |
dkato | 2:6ec5c1c1d41c | 23 | #define NO_CONNECT (-1) |
dkato | 2:6ec5c1c1d41c | 24 | |
dkato | 2:6ec5c1c1d41c | 25 | #if MBED_CONF_APP_NETWORK_INTERFACE == NO_CONNECT |
dkato | 2:6ec5c1c1d41c | 26 | |
dkato | 2:6ec5c1c1d41c | 27 | static void callback_zxing(char * addr, int size) { |
dkato | 2:6ec5c1c1d41c | 28 | printf("%s\r\n", addr); |
dkato | 2:6ec5c1c1d41c | 29 | } |
dkato | 2:6ec5c1c1d41c | 30 | |
dkato | 2:6ec5c1c1d41c | 31 | // Entry point to the program |
dkato | 2:6ec5c1c1d41c | 32 | int main() { |
dkato | 2:6ec5c1c1d41c | 33 | // Keep track of the main thread |
dkato | 2:6ec5c1c1d41c | 34 | mainThread = osThreadGetId(); |
dkato | 2:6ec5c1c1d41c | 35 | printf("no connect\n"); |
dkato | 2:6ec5c1c1d41c | 36 | mbed_trace_init(); |
dkato | 2:6ec5c1c1d41c | 37 | zxing_init(&callback_zxing); |
dkato | 2:6ec5c1c1d41c | 38 | |
dkato | 2:6ec5c1c1d41c | 39 | while (1) { |
dkato | 2:6ec5c1c1d41c | 40 | zxing_loop(); |
dkato | 2:6ec5c1c1d41c | 41 | Thread::wait(5); |
dkato | 2:6ec5c1c1d41c | 42 | } |
dkato | 2:6ec5c1c1d41c | 43 | } |
dkato | 2:6ec5c1c1d41c | 44 | |
dkato | 2:6ec5c1c1d41c | 45 | #else // MBED_CONF_APP_NETWORK_INTERFACE != NO_CONNECT |
dkato | 2:6ec5c1c1d41c | 46 | |
dkato | 2:6ec5c1c1d41c | 47 | #define __STDC_FORMAT_MACROS |
1050186 | 12:7a199eff6988 | 48 | #include <inttypes.h> |
<> | 0:eb73febb2bba | 49 | #include <string> |
<> | 0:eb73febb2bba | 50 | #include <sstream> |
<> | 0:eb73febb2bba | 51 | #include <vector> |
1050186 | 12:7a199eff6988 | 52 | #include "mbed-trace/mbed_trace.h" |
1050186 | 12:7a199eff6988 | 53 | #include "mbedtls/entropy_poll.h" |
1050186 | 12:7a199eff6988 | 54 | |
<> | 0:eb73febb2bba | 55 | #include "security.h" |
1050186 | 12:7a199eff6988 | 56 | |
1050186 | 12:7a199eff6988 | 57 | #include "mbed.h" |
<> | 0:eb73febb2bba | 58 | |
dkato | 2:6ec5c1c1d41c | 59 | // easy-connect compliancy, it has 2 sets of wifi pins we have only one |
dkato | 2:6ec5c1c1d41c | 60 | #define MBED_CONF_APP_ESP8266_TX MBED_CONF_APP_WIFI_TX |
dkato | 2:6ec5c1c1d41c | 61 | #define MBED_CONF_APP_ESP8266_RX MBED_CONF_APP_WIFI_RX |
1050186 | 12:7a199eff6988 | 62 | #ifdef TARGET_GR_LYCHEE |
1050186 | 12:7a199eff6988 | 63 | #include "easy-connect-gr-lychee/easy-connect.h" |
1050186 | 12:7a199eff6988 | 64 | #else |
1050186 | 12:7a199eff6988 | 65 | #include "easy-connect/easy-connect.h" |
1050186 | 12:7a199eff6988 | 66 | #endif |
1050186 | 12:7a199eff6988 | 67 | |
1050186 | 12:7a199eff6988 | 68 | // Should be defined after easy-connect.h |
1050186 | 12:7a199eff6988 | 69 | #include "simpleclient.h" |
1050186 | 12:7a199eff6988 | 70 | |
1050186 | 12:7a199eff6988 | 71 | #ifdef TARGET_STM |
1050186 | 12:7a199eff6988 | 72 | #define RED_LED (LED3) |
1050186 | 12:7a199eff6988 | 73 | #define GREEN_LED (LED1) |
1050186 | 12:7a199eff6988 | 74 | #define BLUE_LED (LED2) |
1050186 | 12:7a199eff6988 | 75 | #define LED_ON (1) |
1050186 | 12:7a199eff6988 | 76 | #elif TARGET_GR_LYCHEE |
1050186 | 12:7a199eff6988 | 77 | #define GREEN_LED (LED1) |
1050186 | 12:7a199eff6988 | 78 | #define YELLOW_LED (LED2) |
1050186 | 12:7a199eff6988 | 79 | #define ORANGE_LED (LED3) |
1050186 | 12:7a199eff6988 | 80 | #define RED_LED (LED4) |
1050186 | 12:7a199eff6988 | 81 | #define LED_ON (1) |
1050186 | 12:7a199eff6988 | 82 | #else |
1050186 | 12:7a199eff6988 | 83 | #define RED_LED (LED1) |
1050186 | 12:7a199eff6988 | 84 | #define GREEN_LED (LED2) |
1050186 | 12:7a199eff6988 | 85 | #define BLUE_LED (LED3) |
1050186 | 12:7a199eff6988 | 86 | #define LED_ON (0) |
1050186 | 12:7a199eff6988 | 87 | #endif |
1050186 | 12:7a199eff6988 | 88 | #define LED_OFF (!LED_ON) |
1050186 | 12:7a199eff6988 | 89 | |
1050186 | 12:7a199eff6988 | 90 | #define BLINK_SIGNAL 0x1 |
1050186 | 12:7a199eff6988 | 91 | |
1050186 | 12:7a199eff6988 | 92 | // Status indication |
1050186 | 12:7a199eff6988 | 93 | DigitalOut red_led(RED_LED); |
1050186 | 12:7a199eff6988 | 94 | DigitalOut green_led(GREEN_LED); |
1050186 | 12:7a199eff6988 | 95 | #ifdef TARGET_GR_LYCHEE |
1050186 | 12:7a199eff6988 | 96 | DigitalOut yellow_led(YELLOW_LED); |
1050186 | 12:7a199eff6988 | 97 | DigitalOut orange_led(ORANGE_LED); |
1050186 | 12:7a199eff6988 | 98 | #else |
1050186 | 12:7a199eff6988 | 99 | DigitalOut blue_led(BLUE_LED); |
1050186 | 12:7a199eff6988 | 100 | #endif |
1050186 | 12:7a199eff6988 | 101 | |
1050186 | 12:7a199eff6988 | 102 | Ticker status_ticker; |
1050186 | 12:7a199eff6988 | 103 | void blinky() { |
1050186 | 12:7a199eff6988 | 104 | green_led = !green_led; |
1050186 | 12:7a199eff6988 | 105 | } |
<> | 0:eb73febb2bba | 106 | |
<> | 0:eb73febb2bba | 107 | // These are example resource values for the Device Object |
<> | 0:eb73febb2bba | 108 | struct MbedClientDevice device = { |
<> | 0:eb73febb2bba | 109 | "Manufacturer_String", // Manufacturer |
<> | 0:eb73febb2bba | 110 | "Type_String", // Type |
<> | 0:eb73febb2bba | 111 | "ModelNumber_String", // ModelNumber |
<> | 0:eb73febb2bba | 112 | "SerialNumber_String" // SerialNumber |
<> | 0:eb73febb2bba | 113 | }; |
<> | 0:eb73febb2bba | 114 | |
<> | 0:eb73febb2bba | 115 | // Instantiate the class which implements LWM2M Client API (from simpleclient.h) |
<> | 0:eb73febb2bba | 116 | MbedClient mbed_client(device); |
<> | 0:eb73febb2bba | 117 | |
1050186 | 12:7a199eff6988 | 118 | // Set up a button interrupt for user interaction |
1050186 | 12:7a199eff6988 | 119 | #ifdef MBED_CONF_APP_BUTTON1 |
1050186 | 12:7a199eff6988 | 120 | InterruptIn counter_btn(MBED_CONF_APP_BUTTON1); |
1050186 | 12:7a199eff6988 | 121 | #endif |
1050186 | 12:7a199eff6988 | 122 | |
1050186 | 12:7a199eff6988 | 123 | |
1050186 | 12:7a199eff6988 | 124 | /** |
1050186 | 12:7a199eff6988 | 125 | * User interaction handler / simulator. Sets up physical button handler and a ticker |
1050186 | 12:7a199eff6988 | 126 | * for regular updates for the resources. |
1050186 | 12:7a199eff6988 | 127 | * |
1050186 | 12:7a199eff6988 | 128 | * MBED_CONF_APP_BUTTON1 is mapped to actual button pin the mbed_app.json file, where you need to |
1050186 | 12:7a199eff6988 | 129 | * specify board-specific value or leave it undefined if the board does not have buttons. |
1050186 | 12:7a199eff6988 | 130 | */ |
1050186 | 12:7a199eff6988 | 131 | class InteractionProvider { |
1050186 | 12:7a199eff6988 | 132 | |
1050186 | 12:7a199eff6988 | 133 | public: |
1050186 | 12:7a199eff6988 | 134 | InteractionProvider(Semaphore& updates_sem) : updates(updates_sem) { |
1050186 | 12:7a199eff6988 | 135 | |
1050186 | 12:7a199eff6988 | 136 | timer_ticked = false; |
1050186 | 12:7a199eff6988 | 137 | clicked = false; |
1050186 | 12:7a199eff6988 | 138 | |
1050186 | 12:7a199eff6988 | 139 | // Set up handler function for the interaction button, if available |
1050186 | 12:7a199eff6988 | 140 | |
1050186 | 12:7a199eff6988 | 141 | #ifdef MBED_CONF_APP_BUTTON1 |
1050186 | 12:7a199eff6988 | 142 | counter_btn.fall(this, &InteractionProvider::counter_button_handler); |
1050186 | 6:4e43b6f8f772 | 143 | #endif |
1050186 | 12:7a199eff6988 | 144 | |
1050186 | 12:7a199eff6988 | 145 | // Use the counter button handler to send an update of endpoint resource values |
1050186 | 12:7a199eff6988 | 146 | // to connector every 15 seconds periodically. |
1050186 | 12:7a199eff6988 | 147 | timer.attach(this, &InteractionProvider::timer_handler, 15.0); |
1050186 | 12:7a199eff6988 | 148 | } |
1050186 | 12:7a199eff6988 | 149 | |
1050186 | 12:7a199eff6988 | 150 | // flags for interaction, these are read from outside interrupt context |
1050186 | 12:7a199eff6988 | 151 | volatile bool timer_ticked; |
1050186 | 12:7a199eff6988 | 152 | volatile bool clicked; |
1050186 | 12:7a199eff6988 | 153 | |
1050186 | 12:7a199eff6988 | 154 | |
1050186 | 12:7a199eff6988 | 155 | private: |
1050186 | 12:7a199eff6988 | 156 | |
1050186 | 12:7a199eff6988 | 157 | void timer_handler() { |
1050186 | 12:7a199eff6988 | 158 | timer_ticked = true; |
1050186 | 12:7a199eff6988 | 159 | updates.release(); |
1050186 | 12:7a199eff6988 | 160 | } |
1050186 | 12:7a199eff6988 | 161 | |
1050186 | 12:7a199eff6988 | 162 | void counter_button_handler() { |
1050186 | 12:7a199eff6988 | 163 | clicked = true; |
1050186 | 12:7a199eff6988 | 164 | updates.release(); |
1050186 | 12:7a199eff6988 | 165 | } |
1050186 | 12:7a199eff6988 | 166 | |
1050186 | 12:7a199eff6988 | 167 | // time-based event source for regular resource updates |
1050186 | 12:7a199eff6988 | 168 | Ticker timer; |
1050186 | 12:7a199eff6988 | 169 | |
1050186 | 12:7a199eff6988 | 170 | // Network interaction must be performed outside of interrupt context |
1050186 | 12:7a199eff6988 | 171 | Semaphore& updates; |
1050186 | 12:7a199eff6988 | 172 | |
1050186 | 12:7a199eff6988 | 173 | }; |
1050186 | 12:7a199eff6988 | 174 | |
1050186 | 12:7a199eff6988 | 175 | /* |
1050186 | 12:7a199eff6988 | 176 | * Arguments for running "blink" in it's own thread. |
1050186 | 12:7a199eff6988 | 177 | */ |
1050186 | 12:7a199eff6988 | 178 | class BlinkArgs { |
1050186 | 12:7a199eff6988 | 179 | public: |
1050186 | 12:7a199eff6988 | 180 | BlinkArgs() { |
1050186 | 12:7a199eff6988 | 181 | clear(); |
1050186 | 12:7a199eff6988 | 182 | } |
1050186 | 12:7a199eff6988 | 183 | void clear() { |
1050186 | 12:7a199eff6988 | 184 | position = 0; |
1050186 | 12:7a199eff6988 | 185 | blink_pattern.clear(); |
1050186 | 12:7a199eff6988 | 186 | } |
1050186 | 12:7a199eff6988 | 187 | uint16_t position; |
1050186 | 12:7a199eff6988 | 188 | std::vector<uint32_t> blink_pattern; |
1050186 | 12:7a199eff6988 | 189 | }; |
1050186 | 12:7a199eff6988 | 190 | |
<> | 0:eb73febb2bba | 191 | /* |
<> | 0:eb73febb2bba | 192 | * The Led contains one property (pattern) and a function (blink). |
<> | 0:eb73febb2bba | 193 | * When the function blink is executed, the pattern is read, and the LED |
<> | 0:eb73febb2bba | 194 | * will blink based on the pattern. |
<> | 0:eb73febb2bba | 195 | */ |
<> | 0:eb73febb2bba | 196 | class LedResource { |
<> | 0:eb73febb2bba | 197 | public: |
<> | 0:eb73febb2bba | 198 | LedResource() { |
<> | 0:eb73febb2bba | 199 | // create ObjectID with metadata tag of '3201', which is 'digital output' |
1050186 | 12:7a199eff6988 | 200 | // blinky_thread.start(callback(this, &LedResource::do_blink)); |
<> | 0:eb73febb2bba | 201 | led_object = M2MInterfaceFactory::create_object("3201"); |
<> | 0:eb73febb2bba | 202 | M2MObjectInstance* led_inst = led_object->create_object_instance(); |
<> | 0:eb73febb2bba | 203 | |
<> | 0:eb73febb2bba | 204 | // 5855 = Multi-state output |
<> | 0:eb73febb2bba | 205 | M2MResource* color_res = led_inst->create_dynamic_resource("5855", "Color", |
<> | 0:eb73febb2bba | 206 | M2MResourceInstance::STRING, false); |
<> | 0:eb73febb2bba | 207 | // read and write |
<> | 0:eb73febb2bba | 208 | color_res->set_operation(M2MBase::GET_PUT_ALLOWED); |
<> | 0:eb73febb2bba | 209 | // set red as initial color |
<> | 0:eb73febb2bba | 210 | color_res->set_value((const uint8_t*)"red", 3); |
<> | 0:eb73febb2bba | 211 | |
<> | 0:eb73febb2bba | 212 | // 5853 = Multi-state output |
<> | 0:eb73febb2bba | 213 | M2MResource* pattern_res = led_inst->create_dynamic_resource("5853", "Pattern", |
<> | 0:eb73febb2bba | 214 | M2MResourceInstance::STRING, false); |
<> | 0:eb73febb2bba | 215 | // read and write |
<> | 0:eb73febb2bba | 216 | pattern_res->set_operation(M2MBase::GET_PUT_ALLOWED); |
<> | 0:eb73febb2bba | 217 | // set initial pattern (toggle every 200ms. 7 toggles in total) |
<> | 0:eb73febb2bba | 218 | pattern_res->set_value((const uint8_t*)"500:500:500:500:500:500:500", 27); |
<> | 0:eb73febb2bba | 219 | |
<> | 0:eb73febb2bba | 220 | // there's not really an execute LWM2M ID that matches... hmm... |
<> | 0:eb73febb2bba | 221 | M2MResource* led_res = led_inst->create_dynamic_resource("5850", "Blink", |
<> | 0:eb73febb2bba | 222 | M2MResourceInstance::OPAQUE, false); |
<> | 0:eb73febb2bba | 223 | // we allow executing a function here... |
<> | 0:eb73febb2bba | 224 | led_res->set_operation(M2MBase::POST_ALLOWED); |
<> | 0:eb73febb2bba | 225 | // when a POST comes in, we want to execute the led_execute_callback |
<> | 0:eb73febb2bba | 226 | led_res->set_execute_function(execute_callback(this, &LedResource::blink)); |
dkato | 2:6ec5c1c1d41c | 227 | |
<> | 0:eb73febb2bba | 228 | } |
<> | 0:eb73febb2bba | 229 | |
<> | 0:eb73febb2bba | 230 | M2MObject* get_object() { |
<> | 0:eb73febb2bba | 231 | return led_object; |
<> | 0:eb73febb2bba | 232 | } |
<> | 0:eb73febb2bba | 233 | |
dkato | 2:6ec5c1c1d41c | 234 | void blink(void *argument) { |
<> | 0:eb73febb2bba | 235 | // read the value of 'Pattern' |
<> | 0:eb73febb2bba | 236 | M2MObjectInstance* inst = led_object->object_instance(); |
<> | 0:eb73febb2bba | 237 | M2MResource* res = inst->resource("5853"); |
<> | 0:eb73febb2bba | 238 | // read the value of 'Color' |
<> | 0:eb73febb2bba | 239 | M2MObjectInstance* instC = led_object->object_instance(); |
<> | 0:eb73febb2bba | 240 | M2MResource* resC = instC->resource("5855"); |
<> | 0:eb73febb2bba | 241 | |
<> | 0:eb73febb2bba | 242 | // values in mbed Client are all buffers, and we need a vector of int's |
<> | 0:eb73febb2bba | 243 | uint8_t* buffIn = NULL; |
<> | 0:eb73febb2bba | 244 | uint32_t sizeIn; |
<> | 0:eb73febb2bba | 245 | res->get_value(buffIn, sizeIn); |
1050186 | 12:7a199eff6988 | 246 | |
<> | 0:eb73febb2bba | 247 | uint8_t* cbuffIn = NULL; |
<> | 0:eb73febb2bba | 248 | uint32_t csizeIn; |
<> | 0:eb73febb2bba | 249 | resC->get_value(cbuffIn, csizeIn); |
<> | 0:eb73febb2bba | 250 | |
<> | 0:eb73febb2bba | 251 | // turn the buffer into a string, and initialize a vector<int> on the heap |
<> | 0:eb73febb2bba | 252 | std::string s((char*)buffIn, sizeIn); |
<> | 0:eb73febb2bba | 253 | std::vector<uint32_t>* v = new std::vector<uint32_t>; |
<> | 0:eb73febb2bba | 254 | |
1050186 | 12:7a199eff6988 | 255 | printf("led_execute_callback pattern=%s\n", s.c_str()); |
<> | 0:eb73febb2bba | 256 | |
<> | 0:eb73febb2bba | 257 | // our pattern is something like 500:200:500, so parse that |
<> | 0:eb73febb2bba | 258 | std::size_t found = s.find_first_of(":"); |
<> | 0:eb73febb2bba | 259 | while (found!=std::string::npos) { |
<> | 0:eb73febb2bba | 260 | v->push_back(atoi((const char*)s.substr(0,found).c_str())); |
<> | 0:eb73febb2bba | 261 | s = s.substr(found+1); |
<> | 0:eb73febb2bba | 262 | found=s.find_first_of(":"); |
<> | 0:eb73febb2bba | 263 | if(found == std::string::npos) { |
<> | 0:eb73febb2bba | 264 | v->push_back(atoi((const char*)s.c_str())); |
<> | 0:eb73febb2bba | 265 | } |
<> | 0:eb73febb2bba | 266 | } |
dkato | 2:6ec5c1c1d41c | 267 | int position = 0; |
dkato | 2:6ec5c1c1d41c | 268 | while (1) { |
dkato | 2:6ec5c1c1d41c | 269 | do_blink(cbuffIn); |
dkato | 2:6ec5c1c1d41c | 270 | if (position >= v->size()) { |
dkato | 2:6ec5c1c1d41c | 271 | break; |
dkato | 2:6ec5c1c1d41c | 272 | } |
dkato | 2:6ec5c1c1d41c | 273 | // how long do we need to wait before the next blink? |
dkato | 2:6ec5c1c1d41c | 274 | Thread::wait(v->at(position)); |
dkato | 2:6ec5c1c1d41c | 275 | position++; |
dkato | 2:6ec5c1c1d41c | 276 | } |
dkato | 2:6ec5c1c1d41c | 277 | free(buffIn); |
dkato | 2:6ec5c1c1d41c | 278 | free(cbuffIn); |
dkato | 2:6ec5c1c1d41c | 279 | delete v; |
<> | 0:eb73febb2bba | 280 | } |
<> | 0:eb73febb2bba | 281 | |
<> | 0:eb73febb2bba | 282 | private: |
<> | 0:eb73febb2bba | 283 | M2MObject* led_object; |
dkato | 2:6ec5c1c1d41c | 284 | void do_blink(uint8_t* color) { |
1050186 | 12:7a199eff6988 | 285 | |
1050186 | 6:4e43b6f8f772 | 286 | #if defined(TARGET_RZ_A1H) |
<> | 0:eb73febb2bba | 287 | if (!strcmp((char *)color, "red")) { |
<> | 0:eb73febb2bba | 288 | // blink the LED in red color |
1050186 | 12:7a199eff6988 | 289 | red_led = !red_led; |
<> | 0:eb73febb2bba | 290 | } |
<> | 0:eb73febb2bba | 291 | else if (!strcmp((char *)color, "green")) { |
<> | 0:eb73febb2bba | 292 | // blink in green color |
1050186 | 12:7a199eff6988 | 293 | green_led = !green_led; |
<> | 0:eb73febb2bba | 294 | } |
<> | 0:eb73febb2bba | 295 | else if (!strcmp((char *)color, "blue")) { |
<> | 0:eb73febb2bba | 296 | // blink in blue color |
1050186 | 12:7a199eff6988 | 297 | blue_led = !blue_led; |
<> | 0:eb73febb2bba | 298 | } |
<> | 0:eb73febb2bba | 299 | else if (!strcmp((char *)color, "cyan")) { |
<> | 0:eb73febb2bba | 300 | // blink in cyan color |
1050186 | 12:7a199eff6988 | 301 | green_led = !green_led; |
1050186 | 12:7a199eff6988 | 302 | blue_led = !blue_led; |
<> | 0:eb73febb2bba | 303 | } |
<> | 0:eb73febb2bba | 304 | else if (!strcmp((char *)color, "yellow")) { |
<> | 0:eb73febb2bba | 305 | // blink in yellow color |
1050186 | 12:7a199eff6988 | 306 | red_led = !red_led; |
1050186 | 12:7a199eff6988 | 307 | green_led = !green_led; |
<> | 0:eb73febb2bba | 308 | } |
<> | 0:eb73febb2bba | 309 | else if (!strcmp((char *)color, "magenta")) { |
<> | 0:eb73febb2bba | 310 | // blink in magenta color |
1050186 | 12:7a199eff6988 | 311 | red_led = !red_led; |
1050186 | 12:7a199eff6988 | 312 | blue_led = !blue_led; |
<> | 0:eb73febb2bba | 313 | } |
<> | 0:eb73febb2bba | 314 | else if (!strcmp((char *)color, "white")) { |
<> | 0:eb73febb2bba | 315 | // blink in white color |
1050186 | 12:7a199eff6988 | 316 | red_led = !red_led; |
1050186 | 12:7a199eff6988 | 317 | green_led = !green_led; |
1050186 | 12:7a199eff6988 | 318 | blue_led = !blue_led; |
<> | 0:eb73febb2bba | 319 | } |
<> | 0:eb73febb2bba | 320 | else { |
<> | 0:eb73febb2bba | 321 | // no operation |
<> | 0:eb73febb2bba | 322 | } |
1050186 | 6:4e43b6f8f772 | 323 | #elif defined(TARGET_GR_LYCHEE) |
1050186 | 6:4e43b6f8f772 | 324 | if (!strcmp((char *)color, "green")) { |
1050186 | 6:4e43b6f8f772 | 325 | // blink the LED1(green color) |
1050186 | 12:7a199eff6988 | 326 | green_led = !green_led; |
1050186 | 6:4e43b6f8f772 | 327 | } |
1050186 | 6:4e43b6f8f772 | 328 | else if (!strcmp((char *)color, "yellow")) { |
1050186 | 6:4e43b6f8f772 | 329 | // blink the LED2(yellow color) |
1050186 | 12:7a199eff6988 | 330 | yellow_led = !yellow_led; |
1050186 | 6:4e43b6f8f772 | 331 | } |
1050186 | 6:4e43b6f8f772 | 332 | else if (!strcmp((char *)color, "orange")) { |
1050186 | 6:4e43b6f8f772 | 333 | // blink the LED3(orange color) |
1050186 | 12:7a199eff6988 | 334 | orange_led = !orange_led; |
1050186 | 6:4e43b6f8f772 | 335 | } |
1050186 | 6:4e43b6f8f772 | 336 | else if (!strcmp((char *)color, "red")) { |
1050186 | 6:4e43b6f8f772 | 337 | // blink the LED4(red color) |
1050186 | 12:7a199eff6988 | 338 | red_led = !red_led; |
1050186 | 6:4e43b6f8f772 | 339 | } |
1050186 | 6:4e43b6f8f772 | 340 | else { |
1050186 | 6:4e43b6f8f772 | 341 | // no operation |
1050186 | 6:4e43b6f8f772 | 342 | } |
1050186 | 6:4e43b6f8f772 | 343 | #endif |
<> | 0:eb73febb2bba | 344 | } |
<> | 0:eb73febb2bba | 345 | }; |
<> | 0:eb73febb2bba | 346 | |
<> | 0:eb73febb2bba | 347 | /* |
1050186 | 12:7a199eff6988 | 348 | * The button contains one property (click count). |
1050186 | 12:7a199eff6988 | 349 | * When `handle_button_click` is executed, the counter updates. |
1050186 | 12:7a199eff6988 | 350 | */ |
1050186 | 12:7a199eff6988 | 351 | class ButtonResource { |
1050186 | 12:7a199eff6988 | 352 | public: |
1050186 | 12:7a199eff6988 | 353 | ButtonResource(): counter(0) { |
1050186 | 12:7a199eff6988 | 354 | // create ObjectID with metadata tag of '3200', which is 'digital input' |
1050186 | 12:7a199eff6988 | 355 | btn_object = M2MInterfaceFactory::create_object("3200"); |
1050186 | 12:7a199eff6988 | 356 | M2MObjectInstance* btn_inst = btn_object->create_object_instance(); |
1050186 | 12:7a199eff6988 | 357 | // create resource with ID '5501', which is digital input counter |
1050186 | 12:7a199eff6988 | 358 | M2MResource* btn_res = btn_inst->create_dynamic_resource("5501", "Button", |
1050186 | 12:7a199eff6988 | 359 | M2MResourceInstance::INTEGER, true /* observable */); |
1050186 | 12:7a199eff6988 | 360 | // we can read this value |
1050186 | 12:7a199eff6988 | 361 | btn_res->set_operation(M2MBase::GET_ALLOWED); |
1050186 | 12:7a199eff6988 | 362 | // set initial value (all values in mbed Client are buffers) |
1050186 | 12:7a199eff6988 | 363 | // to be able to read this data easily in the Connector console, we'll use a string |
1050186 | 12:7a199eff6988 | 364 | btn_res->set_value((uint8_t*)"0", 1); |
1050186 | 12:7a199eff6988 | 365 | } |
1050186 | 12:7a199eff6988 | 366 | |
1050186 | 12:7a199eff6988 | 367 | ~ButtonResource() { |
1050186 | 12:7a199eff6988 | 368 | } |
1050186 | 12:7a199eff6988 | 369 | |
1050186 | 12:7a199eff6988 | 370 | M2MObject* get_object() { |
1050186 | 12:7a199eff6988 | 371 | return btn_object; |
1050186 | 12:7a199eff6988 | 372 | } |
1050186 | 12:7a199eff6988 | 373 | |
1050186 | 12:7a199eff6988 | 374 | /* |
1050186 | 12:7a199eff6988 | 375 | * When you press the button, we read the current value of the click counter |
1050186 | 12:7a199eff6988 | 376 | * from mbed Device Connector, then up the value with one. |
1050186 | 12:7a199eff6988 | 377 | */ |
1050186 | 12:7a199eff6988 | 378 | void handle_button_click() { |
1050186 | 12:7a199eff6988 | 379 | if (mbed_client.register_successful()) { |
1050186 | 12:7a199eff6988 | 380 | M2MObjectInstance* inst = btn_object->object_instance(); |
1050186 | 12:7a199eff6988 | 381 | M2MResource* res = inst->resource("5501"); |
1050186 | 12:7a199eff6988 | 382 | |
1050186 | 12:7a199eff6988 | 383 | // up counter |
1050186 | 12:7a199eff6988 | 384 | counter++; |
1050186 | 12:7a199eff6988 | 385 | printf("handle_button_click, new value of counter is %d\n", counter); |
1050186 | 12:7a199eff6988 | 386 | // serialize the value of counter as a string, and tell connector |
1050186 | 12:7a199eff6988 | 387 | char buffer[20]; |
1050186 | 12:7a199eff6988 | 388 | int size = sprintf(buffer,"%d",counter); |
1050186 | 12:7a199eff6988 | 389 | res->set_value((uint8_t*)buffer, size); |
1050186 | 12:7a199eff6988 | 390 | } else { |
1050186 | 12:7a199eff6988 | 391 | printf("simulate button_click, device not registered\n"); |
1050186 | 12:7a199eff6988 | 392 | } |
1050186 | 12:7a199eff6988 | 393 | } |
1050186 | 12:7a199eff6988 | 394 | |
1050186 | 12:7a199eff6988 | 395 | private: |
1050186 | 12:7a199eff6988 | 396 | M2MObject* btn_object; |
1050186 | 12:7a199eff6988 | 397 | uint16_t counter; |
1050186 | 12:7a199eff6988 | 398 | }; |
1050186 | 12:7a199eff6988 | 399 | |
1050186 | 12:7a199eff6988 | 400 | /* |
1050186 | 12:7a199eff6988 | 401 | * The timer contains one property: counter. |
1050186 | 12:7a199eff6988 | 402 | * When `handle_timer_tick` is executed, the counter updates. |
1050186 | 12:7a199eff6988 | 403 | */ |
1050186 | 12:7a199eff6988 | 404 | class TimerResource { |
1050186 | 12:7a199eff6988 | 405 | public: |
1050186 | 12:7a199eff6988 | 406 | TimerResource(): counter(0) { |
1050186 | 12:7a199eff6988 | 407 | // create ObjectID with metadata tag of '3200', which is 'digital input' |
1050186 | 12:7a199eff6988 | 408 | btn_object = M2MInterfaceFactory::create_object("3200"); |
1050186 | 12:7a199eff6988 | 409 | M2MObjectInstance* btn_inst = btn_object->create_object_instance(); |
1050186 | 12:7a199eff6988 | 410 | // create resource with ID '5502', which is digital input counter |
1050186 | 12:7a199eff6988 | 411 | M2MResource* btn_res = btn_inst->create_dynamic_resource("5502", "Timer", |
1050186 | 12:7a199eff6988 | 412 | M2MResourceInstance::INTEGER, true /* observable */); |
1050186 | 12:7a199eff6988 | 413 | // we can read this value |
1050186 | 12:7a199eff6988 | 414 | btn_res->set_operation(M2MBase::GET_ALLOWED); |
1050186 | 12:7a199eff6988 | 415 | // set initial value (all values in mbed Client are buffers) |
1050186 | 12:7a199eff6988 | 416 | // to be able to read this data easily in the Connector console, we'll use a string |
1050186 | 12:7a199eff6988 | 417 | btn_res->set_value((uint8_t*)"0", 1); |
1050186 | 12:7a199eff6988 | 418 | } |
1050186 | 12:7a199eff6988 | 419 | |
1050186 | 12:7a199eff6988 | 420 | ~TimerResource() { |
1050186 | 12:7a199eff6988 | 421 | } |
1050186 | 12:7a199eff6988 | 422 | |
1050186 | 12:7a199eff6988 | 423 | M2MObject* get_object() { |
1050186 | 12:7a199eff6988 | 424 | return btn_object; |
1050186 | 12:7a199eff6988 | 425 | } |
1050186 | 12:7a199eff6988 | 426 | |
1050186 | 12:7a199eff6988 | 427 | /* |
1050186 | 12:7a199eff6988 | 428 | * When the timer ticks, we read the current value of the click counter |
1050186 | 12:7a199eff6988 | 429 | * from mbed Device Connector, then up the value with one.l |
1050186 | 12:7a199eff6988 | 430 | */ |
1050186 | 12:7a199eff6988 | 431 | void handle_timer_tick() { |
1050186 | 12:7a199eff6988 | 432 | if (mbed_client.register_successful()) { |
1050186 | 12:7a199eff6988 | 433 | M2MObjectInstance* inst = btn_object->object_instance(); |
1050186 | 12:7a199eff6988 | 434 | M2MResource* res = inst->resource("5502"); |
1050186 | 12:7a199eff6988 | 435 | |
1050186 | 12:7a199eff6988 | 436 | // up counter |
1050186 | 12:7a199eff6988 | 437 | counter++; |
1050186 | 12:7a199eff6988 | 438 | printf("handle_timer_click, new value of counter is %d\n", counter); |
1050186 | 12:7a199eff6988 | 439 | // serialize the value of counter as a string, and tell connector |
1050186 | 12:7a199eff6988 | 440 | char buffer[20]; |
1050186 | 12:7a199eff6988 | 441 | int size = sprintf(buffer,"%d",counter); |
1050186 | 12:7a199eff6988 | 442 | res->set_value((uint8_t*)buffer, size); |
1050186 | 12:7a199eff6988 | 443 | } else { |
1050186 | 12:7a199eff6988 | 444 | printf("handle_timer_tick, device not registered\n"); |
1050186 | 12:7a199eff6988 | 445 | } |
1050186 | 12:7a199eff6988 | 446 | } |
1050186 | 12:7a199eff6988 | 447 | |
1050186 | 12:7a199eff6988 | 448 | private: |
1050186 | 12:7a199eff6988 | 449 | M2MObject* btn_object; |
1050186 | 12:7a199eff6988 | 450 | uint16_t counter; |
1050186 | 12:7a199eff6988 | 451 | }; |
1050186 | 12:7a199eff6988 | 452 | |
1050186 | 12:7a199eff6988 | 453 | |
1050186 | 12:7a199eff6988 | 454 | |
1050186 | 12:7a199eff6988 | 455 | class BigPayloadResource { |
1050186 | 12:7a199eff6988 | 456 | public: |
1050186 | 12:7a199eff6988 | 457 | BigPayloadResource() { |
1050186 | 12:7a199eff6988 | 458 | big_payload = M2MInterfaceFactory::create_object("1000"); |
1050186 | 12:7a199eff6988 | 459 | M2MObjectInstance* payload_inst = big_payload->create_object_instance(); |
1050186 | 12:7a199eff6988 | 460 | M2MResource* payload_res = payload_inst->create_dynamic_resource("1", "BigData", |
1050186 | 12:7a199eff6988 | 461 | M2MResourceInstance::STRING, true /* observable */); |
1050186 | 12:7a199eff6988 | 462 | payload_res->set_operation(M2MBase::GET_PUT_ALLOWED); |
1050186 | 12:7a199eff6988 | 463 | payload_res->set_value((uint8_t*)"0", 1); |
1050186 | 12:7a199eff6988 | 464 | payload_res->set_incoming_block_message_callback( |
1050186 | 12:7a199eff6988 | 465 | incoming_block_message_callback(this, &BigPayloadResource::block_message_received)); |
1050186 | 12:7a199eff6988 | 466 | payload_res->set_outgoing_block_message_callback( |
1050186 | 12:7a199eff6988 | 467 | outgoing_block_message_callback(this, &BigPayloadResource::block_message_requested)); |
1050186 | 12:7a199eff6988 | 468 | } |
1050186 | 12:7a199eff6988 | 469 | |
1050186 | 12:7a199eff6988 | 470 | M2MObject* get_object() { |
1050186 | 12:7a199eff6988 | 471 | return big_payload; |
1050186 | 12:7a199eff6988 | 472 | } |
1050186 | 12:7a199eff6988 | 473 | |
1050186 | 12:7a199eff6988 | 474 | void block_message_received(M2MBlockMessage *argument) { |
1050186 | 12:7a199eff6988 | 475 | if (argument) { |
1050186 | 12:7a199eff6988 | 476 | if (M2MBlockMessage::ErrorNone == argument->error_code()) { |
1050186 | 12:7a199eff6988 | 477 | if (argument->is_last_block()) { |
1050186 | 12:7a199eff6988 | 478 | printf("Last block received\n"); |
1050186 | 12:7a199eff6988 | 479 | } |
1050186 | 12:7a199eff6988 | 480 | printf("Block number: %d\n", argument->block_number()); |
1050186 | 12:7a199eff6988 | 481 | // First block received |
1050186 | 12:7a199eff6988 | 482 | if (argument->block_number() == 0) { |
1050186 | 12:7a199eff6988 | 483 | // Store block |
1050186 | 12:7a199eff6988 | 484 | // More blocks coming |
1050186 | 12:7a199eff6988 | 485 | } else { |
1050186 | 12:7a199eff6988 | 486 | // Store blocks |
1050186 | 12:7a199eff6988 | 487 | } |
1050186 | 12:7a199eff6988 | 488 | } else { |
1050186 | 12:7a199eff6988 | 489 | printf("Error when receiving block message! - EntityTooLarge\n"); |
1050186 | 12:7a199eff6988 | 490 | } |
1050186 | 12:7a199eff6988 | 491 | // printf("Total message size: %" PRIu32 "\n", argument->total_message_size()); |
1050186 | 12:7a199eff6988 | 492 | printf("Total message size: %PRIu32\n", argument->total_message_size()); |
1050186 | 12:7a199eff6988 | 493 | } |
1050186 | 12:7a199eff6988 | 494 | } |
1050186 | 12:7a199eff6988 | 495 | |
1050186 | 12:7a199eff6988 | 496 | void block_message_requested(const String& resource, uint8_t *&/*data*/, uint32_t &/*len*/) { |
1050186 | 12:7a199eff6988 | 497 | printf("GET request received for resource: %s\n", resource.c_str()); |
1050186 | 12:7a199eff6988 | 498 | // Copy data and length to coap response |
1050186 | 12:7a199eff6988 | 499 | } |
1050186 | 12:7a199eff6988 | 500 | |
1050186 | 12:7a199eff6988 | 501 | private: |
1050186 | 12:7a199eff6988 | 502 | M2MObject* big_payload; |
1050186 | 12:7a199eff6988 | 503 | }; |
1050186 | 12:7a199eff6988 | 504 | |
1050186 | 12:7a199eff6988 | 505 | /* |
<> | 0:eb73febb2bba | 506 | * The Zxing contains a function (send string). |
<> | 0:eb73febb2bba | 507 | * When `handle_string_send` is executed, the string after decoding is sent. |
<> | 0:eb73febb2bba | 508 | */ |
<> | 0:eb73febb2bba | 509 | class ZxingResource { |
<> | 0:eb73febb2bba | 510 | public: |
<> | 0:eb73febb2bba | 511 | ZxingResource() { |
<> | 0:eb73febb2bba | 512 | // create ObjectID with metadata tag of '3202', which is 'send string' |
<> | 0:eb73febb2bba | 513 | zxing_object = M2MInterfaceFactory::create_object("3202"); |
<> | 0:eb73febb2bba | 514 | M2MObjectInstance* zxing_inst = zxing_object->create_object_instance(); |
<> | 0:eb73febb2bba | 515 | // create resource with ID '5700', which is 'send string' |
<> | 0:eb73febb2bba | 516 | M2MResource* zxing_res = zxing_inst->create_dynamic_resource("5700", "zxing", |
<> | 0:eb73febb2bba | 517 | M2MResourceInstance::STRING, true); |
<> | 0:eb73febb2bba | 518 | // we can read this value |
<> | 0:eb73febb2bba | 519 | zxing_res->set_operation(M2MBase::GET_ALLOWED); |
<> | 0:eb73febb2bba | 520 | // set initial value (all values in mbed Client are buffers) |
<> | 0:eb73febb2bba | 521 | // to be able to read this data easily in the Connector console, we'll use a string |
dkato | 2:6ec5c1c1d41c | 522 | zxing_res->set_value((uint8_t*)"0", 1); |
<> | 0:eb73febb2bba | 523 | } |
<> | 0:eb73febb2bba | 524 | |
<> | 0:eb73febb2bba | 525 | ~ZxingResource() { |
<> | 0:eb73febb2bba | 526 | } |
<> | 0:eb73febb2bba | 527 | |
<> | 0:eb73febb2bba | 528 | M2MObject* get_object() { |
<> | 0:eb73febb2bba | 529 | return zxing_object; |
<> | 0:eb73febb2bba | 530 | } |
<> | 0:eb73febb2bba | 531 | |
<> | 0:eb73febb2bba | 532 | /* |
<> | 0:eb73febb2bba | 533 | * When you success the decode process of barcode, we send the string after decoding to mbed Device Connector. |
<> | 0:eb73febb2bba | 534 | */ |
<> | 0:eb73febb2bba | 535 | void handle_string_send(char * addr, int size) { |
1050186 | 12:7a199eff6988 | 536 | if (mbed_client.register_successful()) { |
1050186 | 12:7a199eff6988 | 537 | M2MObjectInstance* inst = zxing_object->object_instance(); |
1050186 | 12:7a199eff6988 | 538 | M2MResource* res = inst->resource("5700"); |
1050186 | 12:7a199eff6988 | 539 | |
1050186 | 12:7a199eff6988 | 540 | printf("%s\r\n", addr); |
<> | 0:eb73febb2bba | 541 | |
1050186 | 12:7a199eff6988 | 542 | // tell the string to connector |
1050186 | 12:7a199eff6988 | 543 | res->set_value((uint8_t *)addr, size); |
1050186 | 12:7a199eff6988 | 544 | } else { |
1050186 | 12:7a199eff6988 | 545 | printf("handle_string_send, device not registered\n"); |
1050186 | 12:7a199eff6988 | 546 | } |
<> | 0:eb73febb2bba | 547 | } |
<> | 0:eb73febb2bba | 548 | |
<> | 0:eb73febb2bba | 549 | private: |
<> | 0:eb73febb2bba | 550 | M2MObject* zxing_object; |
<> | 0:eb73febb2bba | 551 | }; |
<> | 0:eb73febb2bba | 552 | |
<> | 0:eb73febb2bba | 553 | ZxingResource zxing_resource; |
<> | 0:eb73febb2bba | 554 | |
<> | 0:eb73febb2bba | 555 | static void callback_zxing(char * addr, int size) { |
<> | 0:eb73febb2bba | 556 | zxing_resource.handle_string_send(addr, size); |
<> | 0:eb73febb2bba | 557 | } |
dkato | 2:6ec5c1c1d41c | 558 | |
dkato | 2:6ec5c1c1d41c | 559 | |
1050186 | 12:7a199eff6988 | 560 | // debug printf function |
1050186 | 12:7a199eff6988 | 561 | void trace_printer(const char* str) { |
1050186 | 12:7a199eff6988 | 562 | printf("%s\r\n", str); |
dkato | 2:6ec5c1c1d41c | 563 | } |
<> | 0:eb73febb2bba | 564 | |
<> | 0:eb73febb2bba | 565 | // Entry point to the program |
<> | 0:eb73febb2bba | 566 | int main() { |
<> | 0:eb73febb2bba | 567 | |
dkato | 2:6ec5c1c1d41c | 568 | unsigned int seed; |
dkato | 2:6ec5c1c1d41c | 569 | size_t len; |
<> | 0:eb73febb2bba | 570 | |
dkato | 2:6ec5c1c1d41c | 571 | #ifdef MBEDTLS_ENTROPY_HARDWARE_ALT |
dkato | 2:6ec5c1c1d41c | 572 | // Used to randomize source port |
dkato | 2:6ec5c1c1d41c | 573 | mbedtls_hardware_poll(NULL, (unsigned char *) &seed, sizeof seed, &len); |
dkato | 2:6ec5c1c1d41c | 574 | |
dkato | 2:6ec5c1c1d41c | 575 | #elif defined MBEDTLS_TEST_NULL_ENTROPY |
dkato | 2:6ec5c1c1d41c | 576 | |
<> | 0:eb73febb2bba | 577 | #warning "mbedTLS security feature is disabled. Connection will not be secure !! Implement proper hardware entropy for your selected hardware." |
dkato | 2:6ec5c1c1d41c | 578 | // Used to randomize source port |
dkato | 2:6ec5c1c1d41c | 579 | mbedtls_null_entropy_poll( NULL,(unsigned char *) &seed, sizeof seed, &len); |
<> | 0:eb73febb2bba | 580 | |
<> | 0:eb73febb2bba | 581 | #else |
<> | 0:eb73febb2bba | 582 | |
<> | 0:eb73febb2bba | 583 | #error "This hardware does not have entropy, endpoint will not register to Connector.\ |
<> | 0:eb73febb2bba | 584 | You need to enable NULL ENTROPY for your application, but if this configuration change is made then no security is offered by mbed TLS.\ |
<> | 0:eb73febb2bba | 585 | Add MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES and MBEDTLS_TEST_NULL_ENTROPY in mbed_app.json macros to register your endpoint." |
<> | 0:eb73febb2bba | 586 | |
<> | 0:eb73febb2bba | 587 | #endif |
<> | 0:eb73febb2bba | 588 | |
dkato | 2:6ec5c1c1d41c | 589 | srand(seed); |
1050186 | 12:7a199eff6988 | 590 | red_led = LED_OFF; |
1050186 | 12:7a199eff6988 | 591 | #ifdef TARGET_GR_LYCHEE |
1050186 | 12:7a199eff6988 | 592 | orange_led = LED_OFF; |
dkato | 2:6ec5c1c1d41c | 593 | #else |
1050186 | 12:7a199eff6988 | 594 | blue_led = LED_OFF; |
dkato | 2:6ec5c1c1d41c | 595 | #endif |
<> | 0:eb73febb2bba | 596 | |
1050186 | 12:7a199eff6988 | 597 | status_ticker.attach_us(blinky, 250000); |
1050186 | 12:7a199eff6988 | 598 | // Keep track of the main thread |
1050186 | 12:7a199eff6988 | 599 | osThreadId mainThread = osThreadGetId(); |
1050186 | 12:7a199eff6988 | 600 | |
1050186 | 12:7a199eff6988 | 601 | printf("\nStarting mbed Client example\n"); |
1050186 | 12:7a199eff6988 | 602 | |
<> | 0:eb73febb2bba | 603 | mbed_trace_init(); |
1050186 | 12:7a199eff6988 | 604 | mbed_trace_print_function_set(trace_printer); |
1050186 | 12:7a199eff6988 | 605 | mbed_trace_config_set(TRACE_MODE_COLOR | TRACE_ACTIVE_LEVEL_INFO | TRACE_CARRIAGE_RETURN); |
<> | 0:eb73febb2bba | 606 | |
1050186 | 6:4e43b6f8f772 | 607 | #if defined(TARGET_RZ_A1H) && (MBED_CONF_APP_NETWORK_INTERFACE == WIFI_BP3595) |
dkato | 2:6ec5c1c1d41c | 608 | DigitalOut usb1en(P3_8); |
<> | 0:eb73febb2bba | 609 | usb1en = 1; |
<> | 0:eb73febb2bba | 610 | Thread::wait(5); |
<> | 0:eb73febb2bba | 611 | usb1en = 0; |
<> | 0:eb73febb2bba | 612 | Thread::wait(5); |
<> | 0:eb73febb2bba | 613 | #endif |
dkato | 2:6ec5c1c1d41c | 614 | |
dkato | 2:6ec5c1c1d41c | 615 | NetworkInterface* network = easy_connect(true); |
dkato | 2:6ec5c1c1d41c | 616 | if(network == NULL) { |
dkato | 2:6ec5c1c1d41c | 617 | printf("\nConnection to Network Failed - exiting application...\n"); |
dkato | 2:6ec5c1c1d41c | 618 | return -1; |
<> | 0:eb73febb2bba | 619 | } |
1050186 | 12:7a199eff6988 | 620 | |
1050186 | 12:7a199eff6988 | 621 | // we create our button, timer and LED resources |
1050186 | 12:7a199eff6988 | 622 | ButtonResource button_resource; |
<> | 0:eb73febb2bba | 623 | LedResource led_resource; |
1050186 | 12:7a199eff6988 | 624 | BigPayloadResource big_payload_resource; |
1050186 | 12:7a199eff6988 | 625 | TimerResource timer_resource; |
1050186 | 12:7a199eff6988 | 626 | |
1050186 | 12:7a199eff6988 | 627 | // Network interaction must be performed outside of interrupt context |
1050186 | 12:7a199eff6988 | 628 | Semaphore updates(0); |
1050186 | 12:7a199eff6988 | 629 | |
1050186 | 12:7a199eff6988 | 630 | InteractionProvider interaction_provider(updates); |
1050186 | 12:7a199eff6988 | 631 | |
<> | 0:eb73febb2bba | 632 | |
<> | 0:eb73febb2bba | 633 | // Create endpoint interface to manage register and unregister |
dkato | 2:6ec5c1c1d41c | 634 | mbed_client.create_interface(MBED_SERVER_ADDRESS, network); |
<> | 0:eb73febb2bba | 635 | |
<> | 0:eb73febb2bba | 636 | // Create Objects of varying types, see simpleclient.h for more details on implementation. |
<> | 0:eb73febb2bba | 637 | M2MSecurity* register_object = mbed_client.create_register_object(); // server object specifying connector info |
<> | 0:eb73febb2bba | 638 | M2MDevice* device_object = mbed_client.create_device_object(); // device resources object |
<> | 0:eb73febb2bba | 639 | |
<> | 0:eb73febb2bba | 640 | // Create list of Objects to register |
<> | 0:eb73febb2bba | 641 | M2MObjectList object_list; |
<> | 0:eb73febb2bba | 642 | |
<> | 0:eb73febb2bba | 643 | // Add objects to list |
<> | 0:eb73febb2bba | 644 | object_list.push_back(device_object); |
1050186 | 12:7a199eff6988 | 645 | object_list.push_back(button_resource.get_object()); |
dkato | 2:6ec5c1c1d41c | 646 | object_list.push_back(led_resource.get_object()); |
1050186 | 12:7a199eff6988 | 647 | object_list.push_back(big_payload_resource.get_object()); |
1050186 | 12:7a199eff6988 | 648 | object_list.push_back(timer_resource.get_object()); |
<> | 0:eb73febb2bba | 649 | object_list.push_back(zxing_resource.get_object()); |
<> | 0:eb73febb2bba | 650 | |
<> | 0:eb73febb2bba | 651 | // Set endpoint registration object |
<> | 0:eb73febb2bba | 652 | mbed_client.set_register_object(register_object); |
<> | 0:eb73febb2bba | 653 | |
<> | 0:eb73febb2bba | 654 | // Register with mbed Device Connector |
<> | 0:eb73febb2bba | 655 | mbed_client.test_register(register_object, object_list); |
1050186 | 12:7a199eff6988 | 656 | volatile bool registered = true; |
<> | 0:eb73febb2bba | 657 | |
1050186 | 12:7a199eff6988 | 658 | #ifdef TARGET_GR_LYCHEE |
<> | 0:eb73febb2bba | 659 | zxing_init(&callback_zxing); |
dkato | 2:6ec5c1c1d41c | 660 | |
<> | 0:eb73febb2bba | 661 | Timer update_timer; |
<> | 0:eb73febb2bba | 662 | update_timer.reset(); |
<> | 0:eb73febb2bba | 663 | update_timer.start(); |
<> | 0:eb73febb2bba | 664 | |
1050186 | 12:7a199eff6988 | 665 | while (true) { |
<> | 0:eb73febb2bba | 666 | if (zxing_loop() == 0) { |
<> | 0:eb73febb2bba | 667 | update_timer.reset(); |
<> | 0:eb73febb2bba | 668 | } else if (update_timer.read() >= 25) { |
<> | 0:eb73febb2bba | 669 | mbed_client.test_update_register(); |
<> | 0:eb73febb2bba | 670 | update_timer.reset(); |
<> | 0:eb73febb2bba | 671 | } else { |
<> | 0:eb73febb2bba | 672 | // do nothing |
<> | 0:eb73febb2bba | 673 | } |
<> | 0:eb73febb2bba | 674 | Thread::wait(5); |
<> | 0:eb73febb2bba | 675 | } |
1050186 | 12:7a199eff6988 | 676 | #else |
1050186 | 12:7a199eff6988 | 677 | while (true) { |
1050186 | 12:7a199eff6988 | 678 | updates.wait(25000); |
1050186 | 12:7a199eff6988 | 679 | if(registered) { |
1050186 | 12:7a199eff6988 | 680 | if(!interaction_provider.clicked) { |
1050186 | 12:7a199eff6988 | 681 | mbed_client.test_update_register(); |
1050186 | 12:7a199eff6988 | 682 | } |
1050186 | 12:7a199eff6988 | 683 | }else { |
1050186 | 12:7a199eff6988 | 684 | break; |
1050186 | 12:7a199eff6988 | 685 | } |
1050186 | 12:7a199eff6988 | 686 | if(interaction_provider.clicked) { |
1050186 | 12:7a199eff6988 | 687 | interaction_provider.clicked = false; |
1050186 | 12:7a199eff6988 | 688 | button_resource.handle_button_click(); |
1050186 | 12:7a199eff6988 | 689 | } |
1050186 | 12:7a199eff6988 | 690 | if(interaction_provider.timer_ticked) { |
1050186 | 12:7a199eff6988 | 691 | interaction_provider.timer_ticked = false; |
1050186 | 12:7a199eff6988 | 692 | timer_resource.handle_timer_tick(); |
1050186 | 12:7a199eff6988 | 693 | } |
1050186 | 12:7a199eff6988 | 694 | } |
1050186 | 12:7a199eff6988 | 695 | #endif |
<> | 0:eb73febb2bba | 696 | |
<> | 0:eb73febb2bba | 697 | mbed_client.test_unregister(); |
1050186 | 12:7a199eff6988 | 698 | status_ticker.detach(); |
<> | 0:eb73febb2bba | 699 | } |
dkato | 2:6ec5c1c1d41c | 700 | |
dkato | 2:6ec5c1c1d41c | 701 | #endif // MBED_CONF_APP_NETWORK_INTERFACE != NO_CONNECT |
dkato | 2:6ec5c1c1d41c | 702 | |
dkato | 2:6ec5c1c1d41c | 703 |