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@6:4e43b6f8f772, 2018-02-15 (annotated)
- Committer:
- 1050186
- Date:
- Thu Feb 15 04:29:52 2018 +0000
- Revision:
- 6:4e43b6f8f772
- Parent:
- 2:6ec5c1c1d41c
- Child:
- 12:7a199eff6988
First commit
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 |
<> | 0:eb73febb2bba | 48 | #include <string> |
<> | 0:eb73febb2bba | 49 | #include <sstream> |
<> | 0:eb73febb2bba | 50 | #include <vector> |
<> | 0:eb73febb2bba | 51 | #include "security.h" |
dkato | 2:6ec5c1c1d41c | 52 | #include "simpleclient.h" |
dkato | 2:6ec5c1c1d41c | 53 | #include "mbedtls/entropy_poll.h" |
<> | 0:eb73febb2bba | 54 | |
dkato | 2:6ec5c1c1d41c | 55 | // easy-connect compliancy, it has 2 sets of wifi pins we have only one |
dkato | 2:6ec5c1c1d41c | 56 | #define MBED_CONF_APP_ESP8266_TX MBED_CONF_APP_WIFI_TX |
dkato | 2:6ec5c1c1d41c | 57 | #define MBED_CONF_APP_ESP8266_RX MBED_CONF_APP_WIFI_RX |
dkato | 2:6ec5c1c1d41c | 58 | #include "easy-connect.h" |
<> | 0:eb73febb2bba | 59 | |
<> | 0:eb73febb2bba | 60 | // These are example resource values for the Device Object |
<> | 0:eb73febb2bba | 61 | struct MbedClientDevice device = { |
<> | 0:eb73febb2bba | 62 | "Manufacturer_String", // Manufacturer |
<> | 0:eb73febb2bba | 63 | "Type_String", // Type |
<> | 0:eb73febb2bba | 64 | "ModelNumber_String", // ModelNumber |
<> | 0:eb73febb2bba | 65 | "SerialNumber_String" // SerialNumber |
<> | 0:eb73febb2bba | 66 | }; |
<> | 0:eb73febb2bba | 67 | |
<> | 0:eb73febb2bba | 68 | // Instantiate the class which implements LWM2M Client API (from simpleclient.h) |
<> | 0:eb73febb2bba | 69 | MbedClient mbed_client(device); |
<> | 0:eb73febb2bba | 70 | |
<> | 0:eb73febb2bba | 71 | // LED Output |
1050186 | 6:4e43b6f8f772 | 72 | #if defined(TARGET_RZ_A1H) |
<> | 0:eb73febb2bba | 73 | DigitalOut led1(LED1); |
<> | 0:eb73febb2bba | 74 | DigitalOut led2(LED2); |
<> | 0:eb73febb2bba | 75 | DigitalOut led3(LED3); |
1050186 | 6:4e43b6f8f772 | 76 | #elif defined(TARGET_GR_LYCHEE) |
1050186 | 6:4e43b6f8f772 | 77 | DigitalOut led1(LED1); |
1050186 | 6:4e43b6f8f772 | 78 | DigitalOut led2(LED2); |
1050186 | 6:4e43b6f8f772 | 79 | DigitalOut led3(LED3); |
1050186 | 6:4e43b6f8f772 | 80 | DigitalOut led4(LED4); |
1050186 | 6:4e43b6f8f772 | 81 | #endif |
<> | 0:eb73febb2bba | 82 | /* |
<> | 0:eb73febb2bba | 83 | * The Led contains one property (pattern) and a function (blink). |
<> | 0:eb73febb2bba | 84 | * When the function blink is executed, the pattern is read, and the LED |
<> | 0:eb73febb2bba | 85 | * will blink based on the pattern. |
<> | 0:eb73febb2bba | 86 | */ |
<> | 0:eb73febb2bba | 87 | class LedResource { |
<> | 0:eb73febb2bba | 88 | public: |
<> | 0:eb73febb2bba | 89 | LedResource() { |
<> | 0:eb73febb2bba | 90 | // create ObjectID with metadata tag of '3201', which is 'digital output' |
<> | 0:eb73febb2bba | 91 | led_object = M2MInterfaceFactory::create_object("3201"); |
<> | 0:eb73febb2bba | 92 | M2MObjectInstance* led_inst = led_object->create_object_instance(); |
<> | 0:eb73febb2bba | 93 | |
<> | 0:eb73febb2bba | 94 | // 5855 = Multi-state output |
<> | 0:eb73febb2bba | 95 | M2MResource* color_res = led_inst->create_dynamic_resource("5855", "Color", |
<> | 0:eb73febb2bba | 96 | M2MResourceInstance::STRING, false); |
<> | 0:eb73febb2bba | 97 | // read and write |
<> | 0:eb73febb2bba | 98 | color_res->set_operation(M2MBase::GET_PUT_ALLOWED); |
<> | 0:eb73febb2bba | 99 | // set red as initial color |
<> | 0:eb73febb2bba | 100 | color_res->set_value((const uint8_t*)"red", 3); |
<> | 0:eb73febb2bba | 101 | |
<> | 0:eb73febb2bba | 102 | // 5853 = Multi-state output |
<> | 0:eb73febb2bba | 103 | M2MResource* pattern_res = led_inst->create_dynamic_resource("5853", "Pattern", |
<> | 0:eb73febb2bba | 104 | M2MResourceInstance::STRING, false); |
<> | 0:eb73febb2bba | 105 | // read and write |
<> | 0:eb73febb2bba | 106 | pattern_res->set_operation(M2MBase::GET_PUT_ALLOWED); |
<> | 0:eb73febb2bba | 107 | // set initial pattern (toggle every 200ms. 7 toggles in total) |
<> | 0:eb73febb2bba | 108 | pattern_res->set_value((const uint8_t*)"500:500:500:500:500:500:500", 27); |
<> | 0:eb73febb2bba | 109 | |
<> | 0:eb73febb2bba | 110 | // there's not really an execute LWM2M ID that matches... hmm... |
<> | 0:eb73febb2bba | 111 | M2MResource* led_res = led_inst->create_dynamic_resource("5850", "Blink", |
<> | 0:eb73febb2bba | 112 | M2MResourceInstance::OPAQUE, false); |
<> | 0:eb73febb2bba | 113 | // we allow executing a function here... |
<> | 0:eb73febb2bba | 114 | led_res->set_operation(M2MBase::POST_ALLOWED); |
<> | 0:eb73febb2bba | 115 | // when a POST comes in, we want to execute the led_execute_callback |
<> | 0:eb73febb2bba | 116 | led_res->set_execute_function(execute_callback(this, &LedResource::blink)); |
dkato | 2:6ec5c1c1d41c | 117 | |
<> | 0:eb73febb2bba | 118 | } |
<> | 0:eb73febb2bba | 119 | |
<> | 0:eb73febb2bba | 120 | M2MObject* get_object() { |
<> | 0:eb73febb2bba | 121 | return led_object; |
<> | 0:eb73febb2bba | 122 | } |
<> | 0:eb73febb2bba | 123 | |
dkato | 2:6ec5c1c1d41c | 124 | void blink(void *argument) { |
dkato | 2:6ec5c1c1d41c | 125 | |
dkato | 2:6ec5c1c1d41c | 126 | |
<> | 0:eb73febb2bba | 127 | // read the value of 'Pattern' |
<> | 0:eb73febb2bba | 128 | M2MObjectInstance* inst = led_object->object_instance(); |
<> | 0:eb73febb2bba | 129 | M2MResource* res = inst->resource("5853"); |
<> | 0:eb73febb2bba | 130 | // read the value of 'Color' |
<> | 0:eb73febb2bba | 131 | M2MObjectInstance* instC = led_object->object_instance(); |
<> | 0:eb73febb2bba | 132 | M2MResource* resC = instC->resource("5855"); |
<> | 0:eb73febb2bba | 133 | |
<> | 0:eb73febb2bba | 134 | // values in mbed Client are all buffers, and we need a vector of int's |
<> | 0:eb73febb2bba | 135 | uint8_t* buffIn = NULL; |
<> | 0:eb73febb2bba | 136 | uint32_t sizeIn; |
<> | 0:eb73febb2bba | 137 | res->get_value(buffIn, sizeIn); |
<> | 0:eb73febb2bba | 138 | |
<> | 0:eb73febb2bba | 139 | uint8_t* cbuffIn = NULL; |
<> | 0:eb73febb2bba | 140 | uint32_t csizeIn; |
<> | 0:eb73febb2bba | 141 | resC->get_value(cbuffIn, csizeIn); |
<> | 0:eb73febb2bba | 142 | |
<> | 0:eb73febb2bba | 143 | // turn the buffer into a string, and initialize a vector<int> on the heap |
<> | 0:eb73febb2bba | 144 | std::string s((char*)buffIn, sizeIn); |
<> | 0:eb73febb2bba | 145 | std::vector<uint32_t>* v = new std::vector<uint32_t>; |
<> | 0:eb73febb2bba | 146 | |
dkato | 2:6ec5c1c1d41c | 147 | printf("led_execute_callback pattern=%s\r\n", s.c_str()); |
<> | 0:eb73febb2bba | 148 | |
<> | 0:eb73febb2bba | 149 | // our pattern is something like 500:200:500, so parse that |
<> | 0:eb73febb2bba | 150 | std::size_t found = s.find_first_of(":"); |
<> | 0:eb73febb2bba | 151 | while (found!=std::string::npos) { |
<> | 0:eb73febb2bba | 152 | |
<> | 0:eb73febb2bba | 153 | v->push_back(atoi((const char*)s.substr(0,found).c_str())); |
<> | 0:eb73febb2bba | 154 | s = s.substr(found+1); |
<> | 0:eb73febb2bba | 155 | found=s.find_first_of(":"); |
<> | 0:eb73febb2bba | 156 | if(found == std::string::npos) { |
<> | 0:eb73febb2bba | 157 | v->push_back(atoi((const char*)s.c_str())); |
<> | 0:eb73febb2bba | 158 | } |
<> | 0:eb73febb2bba | 159 | } |
dkato | 2:6ec5c1c1d41c | 160 | int position = 0; |
dkato | 2:6ec5c1c1d41c | 161 | while (1) { |
dkato | 2:6ec5c1c1d41c | 162 | do_blink(cbuffIn); |
dkato | 2:6ec5c1c1d41c | 163 | if (position >= v->size()) { |
dkato | 2:6ec5c1c1d41c | 164 | break; |
dkato | 2:6ec5c1c1d41c | 165 | } |
dkato | 2:6ec5c1c1d41c | 166 | // how long do we need to wait before the next blink? |
dkato | 2:6ec5c1c1d41c | 167 | Thread::wait(v->at(position)); |
dkato | 2:6ec5c1c1d41c | 168 | position++; |
dkato | 2:6ec5c1c1d41c | 169 | } |
dkato | 2:6ec5c1c1d41c | 170 | free(buffIn); |
dkato | 2:6ec5c1c1d41c | 171 | free(cbuffIn); |
dkato | 2:6ec5c1c1d41c | 172 | delete v; |
<> | 0:eb73febb2bba | 173 | } |
<> | 0:eb73febb2bba | 174 | |
<> | 0:eb73febb2bba | 175 | private: |
<> | 0:eb73febb2bba | 176 | M2MObject* led_object; |
dkato | 2:6ec5c1c1d41c | 177 | void do_blink(uint8_t* color) { |
<> | 0:eb73febb2bba | 178 | |
1050186 | 6:4e43b6f8f772 | 179 | #if defined(TARGET_RZ_A1H) |
<> | 0:eb73febb2bba | 180 | if (!strcmp((char *)color, "red")) { |
<> | 0:eb73febb2bba | 181 | // blink the LED in red color |
<> | 0:eb73febb2bba | 182 | led1 = !led1; |
<> | 0:eb73febb2bba | 183 | } |
<> | 0:eb73febb2bba | 184 | else if (!strcmp((char *)color, "green")) { |
<> | 0:eb73febb2bba | 185 | // blink in green color |
<> | 0:eb73febb2bba | 186 | led2 = !led2; |
<> | 0:eb73febb2bba | 187 | } |
<> | 0:eb73febb2bba | 188 | else if (!strcmp((char *)color, "blue")) { |
<> | 0:eb73febb2bba | 189 | // blink in blue color |
<> | 0:eb73febb2bba | 190 | led3 = !led3; |
<> | 0:eb73febb2bba | 191 | } |
<> | 0:eb73febb2bba | 192 | else if (!strcmp((char *)color, "cyan")) { |
<> | 0:eb73febb2bba | 193 | // blink in cyan color |
<> | 0:eb73febb2bba | 194 | led2 = !led2; |
<> | 0:eb73febb2bba | 195 | led3 = !led3; |
<> | 0:eb73febb2bba | 196 | } |
<> | 0:eb73febb2bba | 197 | else if (!strcmp((char *)color, "yellow")) { |
<> | 0:eb73febb2bba | 198 | // blink in yellow color |
<> | 0:eb73febb2bba | 199 | led1 = !led1; |
<> | 0:eb73febb2bba | 200 | led2 = !led2; |
<> | 0:eb73febb2bba | 201 | } |
<> | 0:eb73febb2bba | 202 | else if (!strcmp((char *)color, "magenta")) { |
<> | 0:eb73febb2bba | 203 | // blink in magenta color |
<> | 0:eb73febb2bba | 204 | led1 = !led1; |
<> | 0:eb73febb2bba | 205 | led3 = !led3; |
<> | 0:eb73febb2bba | 206 | } |
<> | 0:eb73febb2bba | 207 | else if (!strcmp((char *)color, "white")) { |
<> | 0:eb73febb2bba | 208 | // blink in white color |
<> | 0:eb73febb2bba | 209 | led1 = !led1; |
<> | 0:eb73febb2bba | 210 | led2 = !led2; |
<> | 0:eb73febb2bba | 211 | led3 = !led3; |
<> | 0:eb73febb2bba | 212 | } |
<> | 0:eb73febb2bba | 213 | else { |
<> | 0:eb73febb2bba | 214 | // no operation |
<> | 0:eb73febb2bba | 215 | } |
1050186 | 6:4e43b6f8f772 | 216 | #elif defined(TARGET_GR_LYCHEE) |
1050186 | 6:4e43b6f8f772 | 217 | if (!strcmp((char *)color, "green")) { |
1050186 | 6:4e43b6f8f772 | 218 | // blink the LED1(green color) |
1050186 | 6:4e43b6f8f772 | 219 | led1 = !led1; |
1050186 | 6:4e43b6f8f772 | 220 | } |
1050186 | 6:4e43b6f8f772 | 221 | else if (!strcmp((char *)color, "yellow")) { |
1050186 | 6:4e43b6f8f772 | 222 | // blink the LED2(yellow color) |
1050186 | 6:4e43b6f8f772 | 223 | led2 = !led2; |
1050186 | 6:4e43b6f8f772 | 224 | } |
1050186 | 6:4e43b6f8f772 | 225 | else if (!strcmp((char *)color, "orange")) { |
1050186 | 6:4e43b6f8f772 | 226 | // blink the LED3(orange color) |
1050186 | 6:4e43b6f8f772 | 227 | led3 = !led3; |
1050186 | 6:4e43b6f8f772 | 228 | } |
1050186 | 6:4e43b6f8f772 | 229 | else if (!strcmp((char *)color, "red")) { |
1050186 | 6:4e43b6f8f772 | 230 | // blink the LED4(red color) |
1050186 | 6:4e43b6f8f772 | 231 | led4 = !led4; |
1050186 | 6:4e43b6f8f772 | 232 | } |
1050186 | 6:4e43b6f8f772 | 233 | else { |
1050186 | 6:4e43b6f8f772 | 234 | // no operation |
1050186 | 6:4e43b6f8f772 | 235 | } |
1050186 | 6:4e43b6f8f772 | 236 | #endif |
<> | 0:eb73febb2bba | 237 | } |
<> | 0:eb73febb2bba | 238 | }; |
<> | 0:eb73febb2bba | 239 | |
<> | 0:eb73febb2bba | 240 | /* |
<> | 0:eb73febb2bba | 241 | * The Zxing contains a function (send string). |
<> | 0:eb73febb2bba | 242 | * When `handle_string_send` is executed, the string after decoding is sent. |
<> | 0:eb73febb2bba | 243 | */ |
<> | 0:eb73febb2bba | 244 | class ZxingResource { |
<> | 0:eb73febb2bba | 245 | public: |
<> | 0:eb73febb2bba | 246 | ZxingResource() { |
<> | 0:eb73febb2bba | 247 | // create ObjectID with metadata tag of '3202', which is 'send string' |
<> | 0:eb73febb2bba | 248 | zxing_object = M2MInterfaceFactory::create_object("3202"); |
<> | 0:eb73febb2bba | 249 | M2MObjectInstance* zxing_inst = zxing_object->create_object_instance(); |
<> | 0:eb73febb2bba | 250 | // create resource with ID '5700', which is 'send string' |
<> | 0:eb73febb2bba | 251 | M2MResource* zxing_res = zxing_inst->create_dynamic_resource("5700", "zxing", |
<> | 0:eb73febb2bba | 252 | M2MResourceInstance::STRING, true); |
<> | 0:eb73febb2bba | 253 | // we can read this value |
<> | 0:eb73febb2bba | 254 | zxing_res->set_operation(M2MBase::GET_ALLOWED); |
<> | 0:eb73febb2bba | 255 | // set initial value (all values in mbed Client are buffers) |
<> | 0:eb73febb2bba | 256 | // to be able to read this data easily in the Connector console, we'll use a string |
dkato | 2:6ec5c1c1d41c | 257 | zxing_res->set_value((uint8_t*)"0", 1); |
<> | 0:eb73febb2bba | 258 | } |
<> | 0:eb73febb2bba | 259 | |
<> | 0:eb73febb2bba | 260 | ~ZxingResource() { |
<> | 0:eb73febb2bba | 261 | } |
<> | 0:eb73febb2bba | 262 | |
<> | 0:eb73febb2bba | 263 | M2MObject* get_object() { |
<> | 0:eb73febb2bba | 264 | return zxing_object; |
<> | 0:eb73febb2bba | 265 | } |
<> | 0:eb73febb2bba | 266 | |
<> | 0:eb73febb2bba | 267 | /* |
<> | 0:eb73febb2bba | 268 | * When you success the decode process of barcode, we send the string after decoding to mbed Device Connector. |
<> | 0:eb73febb2bba | 269 | */ |
<> | 0:eb73febb2bba | 270 | void handle_string_send(char * addr, int size) { |
<> | 0:eb73febb2bba | 271 | M2MObjectInstance* inst = zxing_object->object_instance(); |
<> | 0:eb73febb2bba | 272 | M2MResource* res = inst->resource("5700"); |
<> | 0:eb73febb2bba | 273 | |
<> | 0:eb73febb2bba | 274 | printf("%s\r\n", addr); |
<> | 0:eb73febb2bba | 275 | |
<> | 0:eb73febb2bba | 276 | // tell the string to connector |
<> | 0:eb73febb2bba | 277 | res->set_value((uint8_t *)addr, size); |
<> | 0:eb73febb2bba | 278 | } |
<> | 0:eb73febb2bba | 279 | |
<> | 0:eb73febb2bba | 280 | private: |
<> | 0:eb73febb2bba | 281 | M2MObject* zxing_object; |
<> | 0:eb73febb2bba | 282 | }; |
<> | 0:eb73febb2bba | 283 | |
<> | 0:eb73febb2bba | 284 | ZxingResource zxing_resource; |
<> | 0:eb73febb2bba | 285 | |
<> | 0:eb73febb2bba | 286 | static void callback_zxing(char * addr, int size) { |
<> | 0:eb73febb2bba | 287 | zxing_resource.handle_string_send(addr, size); |
<> | 0:eb73febb2bba | 288 | } |
dkato | 2:6ec5c1c1d41c | 289 | |
dkato | 2:6ec5c1c1d41c | 290 | static volatile bool registered; |
dkato | 2:6ec5c1c1d41c | 291 | |
dkato | 2:6ec5c1c1d41c | 292 | void unregister() { |
dkato | 2:6ec5c1c1d41c | 293 | registered = false; |
dkato | 2:6ec5c1c1d41c | 294 | } |
<> | 0:eb73febb2bba | 295 | |
<> | 0:eb73febb2bba | 296 | // Entry point to the program |
<> | 0:eb73febb2bba | 297 | int main() { |
<> | 0:eb73febb2bba | 298 | |
dkato | 2:6ec5c1c1d41c | 299 | unsigned int seed; |
dkato | 2:6ec5c1c1d41c | 300 | size_t len; |
<> | 0:eb73febb2bba | 301 | |
dkato | 2:6ec5c1c1d41c | 302 | #ifdef MBEDTLS_ENTROPY_HARDWARE_ALT |
dkato | 2:6ec5c1c1d41c | 303 | // Used to randomize source port |
dkato | 2:6ec5c1c1d41c | 304 | mbedtls_hardware_poll(NULL, (unsigned char *) &seed, sizeof seed, &len); |
dkato | 2:6ec5c1c1d41c | 305 | |
dkato | 2:6ec5c1c1d41c | 306 | #elif defined MBEDTLS_TEST_NULL_ENTROPY |
dkato | 2:6ec5c1c1d41c | 307 | |
<> | 0:eb73febb2bba | 308 | #warning "mbedTLS security feature is disabled. Connection will not be secure !! Implement proper hardware entropy for your selected hardware." |
dkato | 2:6ec5c1c1d41c | 309 | // Used to randomize source port |
dkato | 2:6ec5c1c1d41c | 310 | mbedtls_null_entropy_poll( NULL,(unsigned char *) &seed, sizeof seed, &len); |
<> | 0:eb73febb2bba | 311 | |
<> | 0:eb73febb2bba | 312 | #else |
<> | 0:eb73febb2bba | 313 | |
<> | 0:eb73febb2bba | 314 | #error "This hardware does not have entropy, endpoint will not register to Connector.\ |
<> | 0:eb73febb2bba | 315 | 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 | 316 | Add MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES and MBEDTLS_TEST_NULL_ENTROPY in mbed_app.json macros to register your endpoint." |
<> | 0:eb73febb2bba | 317 | |
<> | 0:eb73febb2bba | 318 | #endif |
<> | 0:eb73febb2bba | 319 | |
dkato | 2:6ec5c1c1d41c | 320 | srand(seed); |
<> | 0:eb73febb2bba | 321 | // Keep track of the main thread |
<> | 0:eb73febb2bba | 322 | mainThread = osThreadGetId(); |
dkato | 2:6ec5c1c1d41c | 323 | printf("\nStarting mbed Client example in "); |
<> | 0:eb73febb2bba | 324 | |
dkato | 2:6ec5c1c1d41c | 325 | #if defined (MESH) || (MBED_CONF_LWIP_IPV6_ENABLED==true) |
dkato | 2:6ec5c1c1d41c | 326 | printf("IPv6 mode\n"); |
dkato | 2:6ec5c1c1d41c | 327 | #else |
dkato | 2:6ec5c1c1d41c | 328 | printf("IPv4 mode\n"); |
dkato | 2:6ec5c1c1d41c | 329 | #endif |
<> | 0:eb73febb2bba | 330 | |
<> | 0:eb73febb2bba | 331 | mbed_trace_init(); |
<> | 0:eb73febb2bba | 332 | |
1050186 | 6:4e43b6f8f772 | 333 | #if defined(TARGET_RZ_A1H) && (MBED_CONF_APP_NETWORK_INTERFACE == WIFI_BP3595) |
dkato | 2:6ec5c1c1d41c | 334 | DigitalOut usb1en(P3_8); |
<> | 0:eb73febb2bba | 335 | usb1en = 1; |
<> | 0:eb73febb2bba | 336 | Thread::wait(5); |
<> | 0:eb73febb2bba | 337 | usb1en = 0; |
<> | 0:eb73febb2bba | 338 | Thread::wait(5); |
<> | 0:eb73febb2bba | 339 | #endif |
dkato | 2:6ec5c1c1d41c | 340 | |
dkato | 2:6ec5c1c1d41c | 341 | NetworkInterface* network = easy_connect(true); |
dkato | 2:6ec5c1c1d41c | 342 | if(network == NULL) { |
dkato | 2:6ec5c1c1d41c | 343 | printf("\nConnection to Network Failed - exiting application...\n"); |
dkato | 2:6ec5c1c1d41c | 344 | return -1; |
<> | 0:eb73febb2bba | 345 | } |
<> | 0:eb73febb2bba | 346 | LedResource led_resource; |
<> | 0:eb73febb2bba | 347 | |
<> | 0:eb73febb2bba | 348 | // Create endpoint interface to manage register and unregister |
dkato | 2:6ec5c1c1d41c | 349 | mbed_client.create_interface(MBED_SERVER_ADDRESS, network); |
<> | 0:eb73febb2bba | 350 | |
<> | 0:eb73febb2bba | 351 | // Create Objects of varying types, see simpleclient.h for more details on implementation. |
<> | 0:eb73febb2bba | 352 | M2MSecurity* register_object = mbed_client.create_register_object(); // server object specifying connector info |
<> | 0:eb73febb2bba | 353 | M2MDevice* device_object = mbed_client.create_device_object(); // device resources object |
<> | 0:eb73febb2bba | 354 | |
<> | 0:eb73febb2bba | 355 | // Create list of Objects to register |
<> | 0:eb73febb2bba | 356 | M2MObjectList object_list; |
<> | 0:eb73febb2bba | 357 | |
<> | 0:eb73febb2bba | 358 | // Add objects to list |
<> | 0:eb73febb2bba | 359 | object_list.push_back(device_object); |
dkato | 2:6ec5c1c1d41c | 360 | object_list.push_back(led_resource.get_object()); |
<> | 0:eb73febb2bba | 361 | object_list.push_back(zxing_resource.get_object()); |
<> | 0:eb73febb2bba | 362 | |
<> | 0:eb73febb2bba | 363 | // Set endpoint registration object |
<> | 0:eb73febb2bba | 364 | mbed_client.set_register_object(register_object); |
<> | 0:eb73febb2bba | 365 | |
<> | 0:eb73febb2bba | 366 | // Register with mbed Device Connector |
<> | 0:eb73febb2bba | 367 | mbed_client.test_register(register_object, object_list); |
<> | 0:eb73febb2bba | 368 | |
<> | 0:eb73febb2bba | 369 | zxing_init(&callback_zxing); |
dkato | 2:6ec5c1c1d41c | 370 | |
<> | 0:eb73febb2bba | 371 | Timer update_timer; |
<> | 0:eb73febb2bba | 372 | update_timer.reset(); |
<> | 0:eb73febb2bba | 373 | update_timer.start(); |
<> | 0:eb73febb2bba | 374 | |
dkato | 2:6ec5c1c1d41c | 375 | registered = true; |
dkato | 2:6ec5c1c1d41c | 376 | InterruptIn unreg_button(USER_BUTTON0); |
dkato | 2:6ec5c1c1d41c | 377 | unreg_button.fall(&unregister); |
dkato | 2:6ec5c1c1d41c | 378 | |
<> | 0:eb73febb2bba | 379 | while (registered) { |
<> | 0:eb73febb2bba | 380 | if (zxing_loop() == 0) { |
<> | 0:eb73febb2bba | 381 | update_timer.reset(); |
<> | 0:eb73febb2bba | 382 | } else if (update_timer.read() >= 25) { |
<> | 0:eb73febb2bba | 383 | mbed_client.test_update_register(); |
<> | 0:eb73febb2bba | 384 | update_timer.reset(); |
<> | 0:eb73febb2bba | 385 | } else { |
<> | 0:eb73febb2bba | 386 | // do nothing |
<> | 0:eb73febb2bba | 387 | } |
<> | 0:eb73febb2bba | 388 | Thread::wait(5); |
<> | 0:eb73febb2bba | 389 | } |
<> | 0:eb73febb2bba | 390 | |
<> | 0:eb73febb2bba | 391 | mbed_client.test_unregister(); |
<> | 0:eb73febb2bba | 392 | } |
dkato | 2:6ec5c1c1d41c | 393 | |
dkato | 2:6ec5c1c1d41c | 394 | #endif // MBED_CONF_APP_NETWORK_INTERFACE != NO_CONNECT |
dkato | 2:6ec5c1c1d41c | 395 | |
dkato | 2:6ec5c1c1d41c | 396 |