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