Mbed Client sample for GR-LYCHEE where ZXing is incorporated.

Dependencies:   DisplayApp AsciiFont

Fork of GR-PEACH_mbed-os-client-ZXingSample by Renesas

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

Application setup

  1. 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.
  2. 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.
  3. 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:

  1. Import this example onto mbed Compiler.
  2. Configure the example in accordance with Application setup.
  3. Compile the example on mbed Compiler and download the resultant binary file.
  4. Plug the micro-USB cable into the OpenSDA port which lies on the next to the RESET button.
  5. 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.
  6. Press the RESET button on the board to run the example.
  7. 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:

  1. 3202/0/5700. Decode result of barcode data input from camera (GET).
  2. 3201/0/5850. Blink function, blinks LED when executed (POST).
  3. 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).
  4. 3201/0/5855. Blink color, used by the blink function. Any of green, yellow, orange and red is acceptable (PUT).
Committer:
<>
Date:
Thu Oct 06 18:00:30 2016 +0900
Revision:
0:eb73febb2bba
Child:
2:6ec5c1c1d41c
Initial Commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
<> 0:eb73febb2bba 1 /*
<> 0:eb73febb2bba 2 * Copyright (c) 2015 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 */
<> 0:eb73febb2bba 16
<> 0:eb73febb2bba 17 #ifndef __SIMPLECLIENT_H__
<> 0:eb73febb2bba 18 #define __SIMPLECLIENT_H__
<> 0:eb73febb2bba 19
<> 0:eb73febb2bba 20 #include "mbed-client/m2minterfacefactory.h"
<> 0:eb73febb2bba 21 #include "mbed-client/m2mdevice.h"
<> 0:eb73febb2bba 22 #include "mbed-client/m2minterfaceobserver.h"
<> 0:eb73febb2bba 23 #include "mbed-client/m2minterface.h"
<> 0:eb73febb2bba 24 #include "mbed-client/m2mobject.h"
<> 0:eb73febb2bba 25 #include "mbed-client/m2mobjectinstance.h"
<> 0:eb73febb2bba 26 #include "mbed-client/m2mresource.h"
<> 0:eb73febb2bba 27 #include "mbed-client/m2mconfig.h"
<> 0:eb73febb2bba 28 #include "security.h"
<> 0:eb73febb2bba 29 #include "mbed.h"
<> 0:eb73febb2bba 30
<> 0:eb73febb2bba 31 //Select binding mode: UDP or TCP
<> 0:eb73febb2bba 32 M2MInterface::BindingMode SOCKET_MODE = M2MInterface::UDP;
<> 0:eb73febb2bba 33
<> 0:eb73febb2bba 34 // MBED_DOMAIN and MBED_ENDPOINT_NAME come
<> 0:eb73febb2bba 35 // from the security.h file copied from connector.mbed.com
<> 0:eb73febb2bba 36
<> 0:eb73febb2bba 37 struct MbedClientDevice {
<> 0:eb73febb2bba 38 const char* Manufacturer;
<> 0:eb73febb2bba 39 const char* Type;
<> 0:eb73febb2bba 40 const char* ModelNumber;
<> 0:eb73febb2bba 41 const char* SerialNumber;
<> 0:eb73febb2bba 42 };
<> 0:eb73febb2bba 43
<> 0:eb73febb2bba 44 /*
<> 0:eb73febb2bba 45 * Wrapper for mbed client stack that handles all callbacks, error handling, and
<> 0:eb73febb2bba 46 * other shenanigans to make the mbed client stack easier to use.
<> 0:eb73febb2bba 47 *
<> 0:eb73febb2bba 48 * The end user should only have to care about configuring the parameters at the
<> 0:eb73febb2bba 49 * top of this file and making sure they add the security.h file correctly.
<> 0:eb73febb2bba 50 * To add resources you can copy the _TODO__ function and add as many instances as
<> 0:eb73febb2bba 51 * you want.
<> 0:eb73febb2bba 52 *
<> 0:eb73febb2bba 53 */
<> 0:eb73febb2bba 54 class MbedClient: public M2MInterfaceObserver {
<> 0:eb73febb2bba 55 public:
<> 0:eb73febb2bba 56
<> 0:eb73febb2bba 57 // constructor for MbedClient object, initialize private variables
<> 0:eb73febb2bba 58 MbedClient(struct MbedClientDevice device) {
<> 0:eb73febb2bba 59 _interface = NULL;
<> 0:eb73febb2bba 60 _bootstrapped = false;
<> 0:eb73febb2bba 61 _error = false;
<> 0:eb73febb2bba 62 _registered = false;
<> 0:eb73febb2bba 63 _unregistered = false;
<> 0:eb73febb2bba 64 _register_security = NULL;
<> 0:eb73febb2bba 65 _value = 0;
<> 0:eb73febb2bba 66 _object = NULL;
<> 0:eb73febb2bba 67 _device = device;
<> 0:eb73febb2bba 68 }
<> 0:eb73febb2bba 69
<> 0:eb73febb2bba 70 // de-constructor for MbedClient object, you can ignore this
<> 0:eb73febb2bba 71 ~MbedClient() {
<> 0:eb73febb2bba 72 if(_interface) {
<> 0:eb73febb2bba 73 delete _interface;
<> 0:eb73febb2bba 74 }
<> 0:eb73febb2bba 75 if(_register_security){
<> 0:eb73febb2bba 76 delete _register_security;
<> 0:eb73febb2bba 77 }
<> 0:eb73febb2bba 78 }
<> 0:eb73febb2bba 79
<> 0:eb73febb2bba 80 // debug printf function
<> 0:eb73febb2bba 81 void trace_printer(const char* str) {
<> 0:eb73febb2bba 82 printf("\r\n%s\r\n", str);
<> 0:eb73febb2bba 83 }
<> 0:eb73febb2bba 84
<> 0:eb73febb2bba 85 /*
<> 0:eb73febb2bba 86 * Creates M2MInterface using which endpoint can
<> 0:eb73febb2bba 87 * setup its name, resource type, life time, connection mode,
<> 0:eb73febb2bba 88 * Currently only LwIPv4 is supported.
<> 0:eb73febb2bba 89 */
<> 0:eb73febb2bba 90 void create_interface(const char *server_address,
<> 0:eb73febb2bba 91 void *handler=NULL) {
<> 0:eb73febb2bba 92 // Randomizing listening port for Certificate mode connectivity
<> 0:eb73febb2bba 93 _server_address = server_address;
<> 0:eb73febb2bba 94 srand(time(NULL));
<> 0:eb73febb2bba 95 uint16_t port = rand() % 65535 + 12345;
<> 0:eb73febb2bba 96
<> 0:eb73febb2bba 97 // create mDS interface object, this is the base object everything else attaches to
<> 0:eb73febb2bba 98 _interface = M2MInterfaceFactory::create_interface(*this,
<> 0:eb73febb2bba 99 MBED_ENDPOINT_NAME, // endpoint name string
<> 0:eb73febb2bba 100 "test", // endpoint type string
<> 0:eb73febb2bba 101 100, // lifetime
<> 0:eb73febb2bba 102 port, // listen port
<> 0:eb73febb2bba 103 MBED_DOMAIN, // domain string
<> 0:eb73febb2bba 104 SOCKET_MODE, // binding mode
<> 0:eb73febb2bba 105 M2MInterface::LwIP_IPv4, // network stack
<> 0:eb73febb2bba 106 ""); // context address string
<> 0:eb73febb2bba 107 const char *binding_mode = (SOCKET_MODE == M2MInterface::UDP) ? "UDP" : "TCP";
<> 0:eb73febb2bba 108 printf("\r\nSOCKET_MODE : %s\r\n", binding_mode);
<> 0:eb73febb2bba 109 printf("Connecting to %s\r\n", server_address);
<> 0:eb73febb2bba 110
<> 0:eb73febb2bba 111 if(_interface) {
<> 0:eb73febb2bba 112 _interface->set_platform_network_handler(handler);
<> 0:eb73febb2bba 113 }
<> 0:eb73febb2bba 114
<> 0:eb73febb2bba 115 }
<> 0:eb73febb2bba 116
<> 0:eb73febb2bba 117 /*
<> 0:eb73febb2bba 118 * check private variable to see if the registration was sucessful or not
<> 0:eb73febb2bba 119 */
<> 0:eb73febb2bba 120 bool register_successful() {
<> 0:eb73febb2bba 121 return _registered;
<> 0:eb73febb2bba 122 }
<> 0:eb73febb2bba 123
<> 0:eb73febb2bba 124 /*
<> 0:eb73febb2bba 125 * check private variable to see if un-registration was sucessful or not
<> 0:eb73febb2bba 126 */
<> 0:eb73febb2bba 127 bool unregister_successful() {
<> 0:eb73febb2bba 128 return _unregistered;
<> 0:eb73febb2bba 129 }
<> 0:eb73febb2bba 130
<> 0:eb73febb2bba 131 /*
<> 0:eb73febb2bba 132 * Creates register server object with mbed device server address and other parameters
<> 0:eb73febb2bba 133 * required for client to connect to mbed device server.
<> 0:eb73febb2bba 134 */
<> 0:eb73febb2bba 135 M2MSecurity* create_register_object() {
<> 0:eb73febb2bba 136 // create security object using the interface factory.
<> 0:eb73febb2bba 137 // this will generate a security ObjectID and ObjectInstance
<> 0:eb73febb2bba 138 M2MSecurity *security = M2MInterfaceFactory::create_security(M2MSecurity::M2MServer);
<> 0:eb73febb2bba 139
<> 0:eb73febb2bba 140 // make sure security ObjectID/ObjectInstance was created successfully
<> 0:eb73febb2bba 141 if(security) {
<> 0:eb73febb2bba 142 // Add ResourceID's and values to the security ObjectID/ObjectInstance
<> 0:eb73febb2bba 143 security->set_resource_value(M2MSecurity::M2MServerUri, _server_address);
<> 0:eb73febb2bba 144 security->set_resource_value(M2MSecurity::SecurityMode, M2MSecurity::Certificate);
<> 0:eb73febb2bba 145 security->set_resource_value(M2MSecurity::ServerPublicKey, SERVER_CERT, sizeof(SERVER_CERT));
<> 0:eb73febb2bba 146 security->set_resource_value(M2MSecurity::PublicKey, CERT, sizeof(CERT));
<> 0:eb73febb2bba 147 security->set_resource_value(M2MSecurity::Secretkey, KEY, sizeof(KEY));
<> 0:eb73febb2bba 148 }
<> 0:eb73febb2bba 149 return security;
<> 0:eb73febb2bba 150 }
<> 0:eb73febb2bba 151
<> 0:eb73febb2bba 152 /*
<> 0:eb73febb2bba 153 * Creates device object which contains mandatory resources linked with
<> 0:eb73febb2bba 154 * device endpoint.
<> 0:eb73febb2bba 155 */
<> 0:eb73febb2bba 156 M2MDevice* create_device_object() {
<> 0:eb73febb2bba 157 // create device objectID/ObjectInstance
<> 0:eb73febb2bba 158 M2MDevice *device = M2MInterfaceFactory::create_device();
<> 0:eb73febb2bba 159 // make sure device object was created successfully
<> 0:eb73febb2bba 160 if(device) {
<> 0:eb73febb2bba 161 // add resourceID's to device objectID/ObjectInstance
<> 0:eb73febb2bba 162 device->create_resource(M2MDevice::Manufacturer, _device.Manufacturer);
<> 0:eb73febb2bba 163 device->create_resource(M2MDevice::DeviceType, _device.Type);
<> 0:eb73febb2bba 164 device->create_resource(M2MDevice::ModelNumber, _device.ModelNumber);
<> 0:eb73febb2bba 165 device->create_resource(M2MDevice::SerialNumber, _device.SerialNumber);
<> 0:eb73febb2bba 166 }
<> 0:eb73febb2bba 167 return device;
<> 0:eb73febb2bba 168 }
<> 0:eb73febb2bba 169
<> 0:eb73febb2bba 170 /*
<> 0:eb73febb2bba 171 * register an object
<> 0:eb73febb2bba 172 */
<> 0:eb73febb2bba 173 void test_register(M2MSecurity *register_object, M2MObjectList object_list){
<> 0:eb73febb2bba 174 if(_interface) {
<> 0:eb73febb2bba 175 // Register function
<> 0:eb73febb2bba 176 _interface->register_object(register_object, object_list);
<> 0:eb73febb2bba 177 }
<> 0:eb73febb2bba 178 }
<> 0:eb73febb2bba 179
<> 0:eb73febb2bba 180 /*
<> 0:eb73febb2bba 181 * unregister all objects
<> 0:eb73febb2bba 182 */
<> 0:eb73febb2bba 183 void test_unregister() {
<> 0:eb73febb2bba 184 if(_interface) {
<> 0:eb73febb2bba 185 // Unregister function
<> 0:eb73febb2bba 186 _interface->unregister_object(NULL); // NULL will unregister all objects
<> 0:eb73febb2bba 187 }
<> 0:eb73febb2bba 188 }
<> 0:eb73febb2bba 189
<> 0:eb73febb2bba 190 //Callback from mbed client stack when the bootstrap
<> 0:eb73febb2bba 191 // is successful, it returns the mbed Device Server object
<> 0:eb73febb2bba 192 // which will be used for registering the resources to
<> 0:eb73febb2bba 193 // mbed Device server.
<> 0:eb73febb2bba 194 void bootstrap_done(M2MSecurity *server_object){
<> 0:eb73febb2bba 195 if(server_object) {
<> 0:eb73febb2bba 196 _bootstrapped = true;
<> 0:eb73febb2bba 197 _error = false;
<> 0:eb73febb2bba 198 trace_printer("Bootstrapped");
<> 0:eb73febb2bba 199 }
<> 0:eb73febb2bba 200 }
<> 0:eb73febb2bba 201
<> 0:eb73febb2bba 202 //Callback from mbed client stack when the registration
<> 0:eb73febb2bba 203 // is successful, it returns the mbed Device Server object
<> 0:eb73febb2bba 204 // to which the resources are registered and registered objects.
<> 0:eb73febb2bba 205 void object_registered(M2MSecurity */*security_object*/, const M2MServer &/*server_object*/){
<> 0:eb73febb2bba 206 _registered = true;
<> 0:eb73febb2bba 207 _unregistered = false;
<> 0:eb73febb2bba 208 trace_printer("Registered object successfully!");
<> 0:eb73febb2bba 209 }
<> 0:eb73febb2bba 210
<> 0:eb73febb2bba 211 //Callback from mbed client stack when the unregistration
<> 0:eb73febb2bba 212 // is successful, it returns the mbed Device Server object
<> 0:eb73febb2bba 213 // to which the resources were unregistered.
<> 0:eb73febb2bba 214 void object_unregistered(M2MSecurity */*server_object*/){
<> 0:eb73febb2bba 215 trace_printer("Unregistered Object Successfully");
<> 0:eb73febb2bba 216 _unregistered = true;
<> 0:eb73febb2bba 217 _registered = false;
<> 0:eb73febb2bba 218 }
<> 0:eb73febb2bba 219
<> 0:eb73febb2bba 220 /*
<> 0:eb73febb2bba 221 * Callback from mbed client stack when registration is updated
<> 0:eb73febb2bba 222 */
<> 0:eb73febb2bba 223 void registration_updated(M2MSecurity */*security_object*/, const M2MServer & /*server_object*/){
<> 0:eb73febb2bba 224 /* The registration is updated automatically and frequently by the
<> 0:eb73febb2bba 225 * mbed client stack. This print statement is turned off because it
<> 0:eb73febb2bba 226 * tends to happen alot.
<> 0:eb73febb2bba 227 */
<> 0:eb73febb2bba 228 //trace_printer("\r\nRegistration Updated\r\n");
<> 0:eb73febb2bba 229 }
<> 0:eb73febb2bba 230
<> 0:eb73febb2bba 231 // Callback from mbed client stack if any error is encountered
<> 0:eb73febb2bba 232 // during any of the LWM2M operations. Error type is passed in
<> 0:eb73febb2bba 233 // the callback.
<> 0:eb73febb2bba 234 void error(M2MInterface::Error error){
<> 0:eb73febb2bba 235 _error = true;
<> 0:eb73febb2bba 236 switch(error){
<> 0:eb73febb2bba 237 case M2MInterface::AlreadyExists:
<> 0:eb73febb2bba 238 trace_printer("[ERROR:] M2MInterface::AlreadyExist");
<> 0:eb73febb2bba 239 break;
<> 0:eb73febb2bba 240 case M2MInterface::BootstrapFailed:
<> 0:eb73febb2bba 241 trace_printer("[ERROR:] M2MInterface::BootstrapFailed");
<> 0:eb73febb2bba 242 break;
<> 0:eb73febb2bba 243 case M2MInterface::InvalidParameters:
<> 0:eb73febb2bba 244 trace_printer("[ERROR:] M2MInterface::InvalidParameters");
<> 0:eb73febb2bba 245 break;
<> 0:eb73febb2bba 246 case M2MInterface::NotRegistered:
<> 0:eb73febb2bba 247 trace_printer("[ERROR:] M2MInterface::NotRegistered");
<> 0:eb73febb2bba 248 break;
<> 0:eb73febb2bba 249 case M2MInterface::Timeout:
<> 0:eb73febb2bba 250 trace_printer("[ERROR:] M2MInterface::Timeout");
<> 0:eb73febb2bba 251 break;
<> 0:eb73febb2bba 252 case M2MInterface::NetworkError:
<> 0:eb73febb2bba 253 trace_printer("[ERROR:] M2MInterface::NetworkError");
<> 0:eb73febb2bba 254 break;
<> 0:eb73febb2bba 255 case M2MInterface::ResponseParseFailed:
<> 0:eb73febb2bba 256 trace_printer("[ERROR:] M2MInterface::ResponseParseFailed");
<> 0:eb73febb2bba 257 break;
<> 0:eb73febb2bba 258 case M2MInterface::UnknownError:
<> 0:eb73febb2bba 259 trace_printer("[ERROR:] M2MInterface::UnknownError");
<> 0:eb73febb2bba 260 break;
<> 0:eb73febb2bba 261 case M2MInterface::MemoryFail:
<> 0:eb73febb2bba 262 trace_printer("[ERROR:] M2MInterface::MemoryFail");
<> 0:eb73febb2bba 263 break;
<> 0:eb73febb2bba 264 case M2MInterface::NotAllowed:
<> 0:eb73febb2bba 265 trace_printer("[ERROR:] M2MInterface::NotAllowed");
<> 0:eb73febb2bba 266 break;
<> 0:eb73febb2bba 267 case M2MInterface::SecureConnectionFailed:
<> 0:eb73febb2bba 268 trace_printer("[ERROR:] M2MInterface::SecureConnectionFailed");
<> 0:eb73febb2bba 269 break;
<> 0:eb73febb2bba 270 case M2MInterface::DnsResolvingFailed:
<> 0:eb73febb2bba 271 trace_printer("[ERROR:] M2MInterface::DnsResolvingFailed");
<> 0:eb73febb2bba 272 break;
<> 0:eb73febb2bba 273
<> 0:eb73febb2bba 274 default:
<> 0:eb73febb2bba 275 break;
<> 0:eb73febb2bba 276 }
<> 0:eb73febb2bba 277 }
<> 0:eb73febb2bba 278
<> 0:eb73febb2bba 279 /* Callback from mbed client stack if any value has changed
<> 0:eb73febb2bba 280 * during PUT operation. Object and its type is passed in
<> 0:eb73febb2bba 281 * the callback.
<> 0:eb73febb2bba 282 * BaseType enum from m2mbase.h
<> 0:eb73febb2bba 283 * Object = 0x0, Resource = 0x1, ObjectInstance = 0x2, ResourceInstance = 0x3
<> 0:eb73febb2bba 284 */
<> 0:eb73febb2bba 285 void value_updated(M2MBase *base, M2MBase::BaseType type) {
<> 0:eb73febb2bba 286 printf("\r\nPUT Request Received!");
<> 0:eb73febb2bba 287 printf("\r\nName :'%s', \r\nType : '%d' (0 for Object, 1 for Resource), \r\nType : '%s'\r\n",
<> 0:eb73febb2bba 288 base->name().c_str(),
<> 0:eb73febb2bba 289 type,
<> 0:eb73febb2bba 290 base->resource_type().c_str()
<> 0:eb73febb2bba 291 );
<> 0:eb73febb2bba 292 }
<> 0:eb73febb2bba 293
<> 0:eb73febb2bba 294 /*
<> 0:eb73febb2bba 295 * update the registration period
<> 0:eb73febb2bba 296 */
<> 0:eb73febb2bba 297 void test_update_register() {
<> 0:eb73febb2bba 298 if (_registered) {
<> 0:eb73febb2bba 299 _interface->update_registration(_register_security, 100);
<> 0:eb73febb2bba 300 }
<> 0:eb73febb2bba 301 }
<> 0:eb73febb2bba 302
<> 0:eb73febb2bba 303 /*
<> 0:eb73febb2bba 304 * manually configure the security object private variable
<> 0:eb73febb2bba 305 */
<> 0:eb73febb2bba 306 void set_register_object(M2MSecurity *register_object) {
<> 0:eb73febb2bba 307 if (_register_security == NULL) {
<> 0:eb73febb2bba 308 _register_security = register_object;
<> 0:eb73febb2bba 309 }
<> 0:eb73febb2bba 310 }
<> 0:eb73febb2bba 311
<> 0:eb73febb2bba 312 private:
<> 0:eb73febb2bba 313
<> 0:eb73febb2bba 314 /*
<> 0:eb73febb2bba 315 * Private variables used in class
<> 0:eb73febb2bba 316 */
<> 0:eb73febb2bba 317 M2MInterface *_interface;
<> 0:eb73febb2bba 318 M2MSecurity *_register_security;
<> 0:eb73febb2bba 319 M2MObject *_object;
<> 0:eb73febb2bba 320 volatile bool _bootstrapped;
<> 0:eb73febb2bba 321 volatile bool _error;
<> 0:eb73febb2bba 322 volatile bool _registered;
<> 0:eb73febb2bba 323 volatile bool _unregistered;
<> 0:eb73febb2bba 324 int _value;
<> 0:eb73febb2bba 325 struct MbedClientDevice _device;
<> 0:eb73febb2bba 326 String _server_address;
<> 0:eb73febb2bba 327 };
<> 0:eb73febb2bba 328
<> 0:eb73febb2bba 329 #endif // __SIMPLECLIENT_H__