This is the sample program that can see the decode result of barcode data on Watson IoT.

Dependencies:   AsciiFont DisplayApp GR-PEACH_video LCD_shield_config LWIPBP3595Interface_STA_for_mbed-os USBDevice

Committer:
Osamu Nakamura
Date:
Thu Nov 10 20:23:55 2016 +0900
Revision:
1:67f8b5cfde75
Parent:
0:7d720671e6dc
Revised the initial value of /888/0/7700

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Osamu Nakamura 0:7d720671e6dc 1 /**
Osamu Nakamura 0:7d720671e6dc 2 * @file DeviceManagementResponder.cpp
Osamu Nakamura 0:7d720671e6dc 3 * @brief mbed CoAP Endpoint Device Management Responder class
Osamu Nakamura 0:7d720671e6dc 4 * @author Doug Anson
Osamu Nakamura 0:7d720671e6dc 5 * @version 1.0
Osamu Nakamura 0:7d720671e6dc 6 * @see
Osamu Nakamura 0:7d720671e6dc 7 *
Osamu Nakamura 0:7d720671e6dc 8 * Copyright (c) 2016
Osamu Nakamura 0:7d720671e6dc 9 *
Osamu Nakamura 0:7d720671e6dc 10 * Licensed under the Apache License, Version 2.0 (the "License");
Osamu Nakamura 0:7d720671e6dc 11 * you may not use this file except in compliance with the License.
Osamu Nakamura 0:7d720671e6dc 12 * You may obtain a copy of the License at
Osamu Nakamura 0:7d720671e6dc 13 *
Osamu Nakamura 0:7d720671e6dc 14 * http://www.apache.org/licenses/LICENSE-2.0
Osamu Nakamura 0:7d720671e6dc 15 *
Osamu Nakamura 0:7d720671e6dc 16 * Unless required by applicable law or agreed to in writing, software
Osamu Nakamura 0:7d720671e6dc 17 * distributed under the License is distributed on an "AS IS" BASIS,
Osamu Nakamura 0:7d720671e6dc 18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Osamu Nakamura 0:7d720671e6dc 19 * See the License for the specific language governing permissions and
Osamu Nakamura 0:7d720671e6dc 20 * limitations under the License.
Osamu Nakamura 0:7d720671e6dc 21 */
Osamu Nakamura 0:7d720671e6dc 22
Osamu Nakamura 0:7d720671e6dc 23 // Class support
Osamu Nakamura 0:7d720671e6dc 24 #include "mbed-connector-interface/DeviceManagementResponder.h"
Osamu Nakamura 0:7d720671e6dc 25
Osamu Nakamura 0:7d720671e6dc 26 // Endpoint support
Osamu Nakamura 0:7d720671e6dc 27 #include "mbed-connector-interface/ConnectorEndpoint.h"
Osamu Nakamura 0:7d720671e6dc 28
Osamu Nakamura 0:7d720671e6dc 29 // DeviceManager Support
Osamu Nakamura 0:7d720671e6dc 30 #include "mbed-connector-interface/DeviceManager.h"
Osamu Nakamura 0:7d720671e6dc 31
Osamu Nakamura 0:7d720671e6dc 32 // constructor
Osamu Nakamura 0:7d720671e6dc 33 DeviceManagementResponder::DeviceManagementResponder(const Logger *logger,const Authenticator *authenticator) {
Osamu Nakamura 0:7d720671e6dc 34 this->m_logger = (Logger *)logger;
Osamu Nakamura 0:7d720671e6dc 35 this->m_authenticator = (Authenticator *)authenticator;
Osamu Nakamura 0:7d720671e6dc 36 this->m_endpoint = NULL;
Osamu Nakamura 0:7d720671e6dc 37 this->m_initialize_fn = NULL;
Osamu Nakamura 0:7d720671e6dc 38 this->m_reboot_responder_fn = NULL;
Osamu Nakamura 0:7d720671e6dc 39 this->m_reset_responder_fn = NULL;
Osamu Nakamura 0:7d720671e6dc 40 this->m_fota_manifest_fn = NULL;
Osamu Nakamura 0:7d720671e6dc 41 this->m_fota_image_set_fn = NULL;
Osamu Nakamura 0:7d720671e6dc 42 this->m_fota_invocation_fn = NULL;
Osamu Nakamura 0:7d720671e6dc 43 this->m_manifest = NULL;
Osamu Nakamura 0:7d720671e6dc 44 this->m_manifest_length = 0;
Osamu Nakamura 0:7d720671e6dc 45 this->m_fota_image = NULL;
Osamu Nakamura 0:7d720671e6dc 46 this->m_fota_image_length = 0;
Osamu Nakamura 0:7d720671e6dc 47 }
Osamu Nakamura 0:7d720671e6dc 48
Osamu Nakamura 0:7d720671e6dc 49 // copy constructor
Osamu Nakamura 0:7d720671e6dc 50 DeviceManagementResponder::DeviceManagementResponder(const DeviceManagementResponder &responder) {
Osamu Nakamura 0:7d720671e6dc 51 this->m_logger = responder.m_logger;
Osamu Nakamura 0:7d720671e6dc 52 this->m_authenticator = responder.m_authenticator;
Osamu Nakamura 0:7d720671e6dc 53 this->m_endpoint = responder.m_endpoint;
Osamu Nakamura 0:7d720671e6dc 54 this->m_initialize_fn = responder.m_initialize_fn;
Osamu Nakamura 0:7d720671e6dc 55 this->m_reboot_responder_fn = responder.m_reboot_responder_fn;
Osamu Nakamura 0:7d720671e6dc 56 this->m_reset_responder_fn = responder.m_reset_responder_fn;
Osamu Nakamura 0:7d720671e6dc 57 this->m_fota_manifest_fn = responder.m_fota_manifest_fn;
Osamu Nakamura 0:7d720671e6dc 58 this->m_fota_image_set_fn = responder.m_fota_image_set_fn;
Osamu Nakamura 0:7d720671e6dc 59 this->m_fota_invocation_fn = responder.m_fota_invocation_fn;
Osamu Nakamura 0:7d720671e6dc 60 this->m_manifest = responder.m_manifest;
Osamu Nakamura 0:7d720671e6dc 61 this->m_manifest_length = responder.m_manifest_length;
Osamu Nakamura 0:7d720671e6dc 62 this->m_fota_image = responder.m_fota_image;
Osamu Nakamura 0:7d720671e6dc 63 this->m_fota_image_length = responder.m_fota_image_length;
Osamu Nakamura 0:7d720671e6dc 64 }
Osamu Nakamura 0:7d720671e6dc 65
Osamu Nakamura 0:7d720671e6dc 66 // destructor
Osamu Nakamura 0:7d720671e6dc 67 DeviceManagementResponder::~DeviceManagementResponder() {
Osamu Nakamura 0:7d720671e6dc 68 }
Osamu Nakamura 0:7d720671e6dc 69
Osamu Nakamura 0:7d720671e6dc 70 // set the Endpoint
Osamu Nakamura 0:7d720671e6dc 71 void DeviceManagementResponder::setEndpoint(const void *ep) {
Osamu Nakamura 0:7d720671e6dc 72 this->m_endpoint = (void *)ep;
Osamu Nakamura 0:7d720671e6dc 73 }
Osamu Nakamura 0:7d720671e6dc 74
Osamu Nakamura 0:7d720671e6dc 75 // authenticate
Osamu Nakamura 0:7d720671e6dc 76 bool DeviceManagementResponder::authenticate(const void *challenge) {
Osamu Nakamura 0:7d720671e6dc 77 if (this->m_authenticator != NULL) {
Osamu Nakamura 0:7d720671e6dc 78 return this->m_authenticator->authenticate((void *)challenge);
Osamu Nakamura 0:7d720671e6dc 79 }
Osamu Nakamura 0:7d720671e6dc 80 return false;
Osamu Nakamura 0:7d720671e6dc 81 }
Osamu Nakamura 0:7d720671e6dc 82
Osamu Nakamura 0:7d720671e6dc 83 // set the Initialize handler
Osamu Nakamura 0:7d720671e6dc 84 void DeviceManagementResponder::setInitializeHandler(initialize_fn initialize_fn) {
Osamu Nakamura 0:7d720671e6dc 85 this->m_initialize_fn = initialize_fn;
Osamu Nakamura 0:7d720671e6dc 86 if (this->m_initialize_fn != NULL) {
Osamu Nakamura 0:7d720671e6dc 87 (*this->m_initialize_fn)(this->m_logger);
Osamu Nakamura 0:7d720671e6dc 88 }
Osamu Nakamura 0:7d720671e6dc 89 }
Osamu Nakamura 0:7d720671e6dc 90
Osamu Nakamura 0:7d720671e6dc 91 // set the Reboot Responder handler
Osamu Nakamura 0:7d720671e6dc 92 void DeviceManagementResponder::setRebootResponderHandler(responder_fn reboot_responder_fn) {
Osamu Nakamura 0:7d720671e6dc 93 this->m_reboot_responder_fn = reboot_responder_fn;
Osamu Nakamura 0:7d720671e6dc 94 }
Osamu Nakamura 0:7d720671e6dc 95
Osamu Nakamura 0:7d720671e6dc 96 // set the Reset Responder handler
Osamu Nakamura 0:7d720671e6dc 97 void DeviceManagementResponder::setResetResponderHandler(responder_fn reset_responder_fn) {
Osamu Nakamura 0:7d720671e6dc 98 this->m_reset_responder_fn = reset_responder_fn;
Osamu Nakamura 0:7d720671e6dc 99 }
Osamu Nakamura 0:7d720671e6dc 100
Osamu Nakamura 0:7d720671e6dc 101 // set the FOTA manifest handler
Osamu Nakamura 0:7d720671e6dc 102 void DeviceManagementResponder::setFOTAManifestHandler(manifest_fn fota_manifest_fn) {
Osamu Nakamura 0:7d720671e6dc 103 this->m_fota_manifest_fn = fota_manifest_fn;
Osamu Nakamura 0:7d720671e6dc 104 }
Osamu Nakamura 0:7d720671e6dc 105
Osamu Nakamura 0:7d720671e6dc 106 // set the FOTA image set handler
Osamu Nakamura 0:7d720671e6dc 107 void DeviceManagementResponder::setFOTAImageHandler(image_set_fn fota_image_set_fn) {
Osamu Nakamura 0:7d720671e6dc 108 this->m_fota_image_set_fn = fota_image_set_fn;
Osamu Nakamura 0:7d720671e6dc 109 }
Osamu Nakamura 0:7d720671e6dc 110
Osamu Nakamura 0:7d720671e6dc 111 // set the FOTA invocation handler
Osamu Nakamura 0:7d720671e6dc 112 void DeviceManagementResponder::setFOTAInvocationHandler(responder_fn fota_invocation_fn) {
Osamu Nakamura 0:7d720671e6dc 113 this->m_fota_invocation_fn = fota_invocation_fn;
Osamu Nakamura 0:7d720671e6dc 114 }
Osamu Nakamura 0:7d720671e6dc 115
Osamu Nakamura 0:7d720671e6dc 116 // set the FOTA manifest
Osamu Nakamura 0:7d720671e6dc 117 void DeviceManagementResponder::setFOTAManifest(char *manifest,uint32_t manifest_length) {
Osamu Nakamura 0:7d720671e6dc 118 this->m_manifest = manifest;
Osamu Nakamura 0:7d720671e6dc 119 this->m_manifest_length = manifest_length;
Osamu Nakamura 0:7d720671e6dc 120 if (this->m_fota_manifest_fn != NULL) {
Osamu Nakamura 0:7d720671e6dc 121 (*this->m_fota_manifest_fn)(this->m_endpoint,this->m_logger,this->m_manifest,this->m_manifest_length);
Osamu Nakamura 0:7d720671e6dc 122 }
Osamu Nakamura 0:7d720671e6dc 123 }
Osamu Nakamura 0:7d720671e6dc 124
Osamu Nakamura 0:7d720671e6dc 125 // get the FOTA manifest
Osamu Nakamura 0:7d720671e6dc 126 char *DeviceManagementResponder::getFOTAManifest() {
Osamu Nakamura 0:7d720671e6dc 127 return this->m_manifest;
Osamu Nakamura 0:7d720671e6dc 128 }
Osamu Nakamura 0:7d720671e6dc 129
Osamu Nakamura 0:7d720671e6dc 130 // get the FOTA manifest
Osamu Nakamura 0:7d720671e6dc 131 uint32_t DeviceManagementResponder::getFOTAManifestLength() {
Osamu Nakamura 0:7d720671e6dc 132 return this->m_manifest_length;
Osamu Nakamura 0:7d720671e6dc 133 }
Osamu Nakamura 0:7d720671e6dc 134
Osamu Nakamura 0:7d720671e6dc 135 // set the FOTA Image
Osamu Nakamura 0:7d720671e6dc 136 void DeviceManagementResponder::setFOTAImage(void *fota_image,uint32_t fota_image_length) {
Osamu Nakamura 0:7d720671e6dc 137 this->m_fota_image = fota_image;
Osamu Nakamura 0:7d720671e6dc 138 this->m_fota_image_length = fota_image_length;
Osamu Nakamura 0:7d720671e6dc 139 if (this->m_fota_image_set_fn != NULL) {
Osamu Nakamura 0:7d720671e6dc 140 (*this->m_fota_image_set_fn)(this->m_endpoint,this->m_logger,this->m_fota_image,this->m_fota_image_length);
Osamu Nakamura 0:7d720671e6dc 141 }
Osamu Nakamura 0:7d720671e6dc 142 }
Osamu Nakamura 0:7d720671e6dc 143
Osamu Nakamura 0:7d720671e6dc 144 // get the FOTA Image
Osamu Nakamura 0:7d720671e6dc 145 void *DeviceManagementResponder::getFOTAImage() {
Osamu Nakamura 0:7d720671e6dc 146 return this->m_fota_image;
Osamu Nakamura 0:7d720671e6dc 147 }
Osamu Nakamura 0:7d720671e6dc 148
Osamu Nakamura 0:7d720671e6dc 149 // get the FOTA Image Length
Osamu Nakamura 0:7d720671e6dc 150 uint32_t DeviceManagementResponder::getFOTAImageLength() {
Osamu Nakamura 0:7d720671e6dc 151 return this->m_fota_image_length;
Osamu Nakamura 0:7d720671e6dc 152 }
Osamu Nakamura 0:7d720671e6dc 153
Osamu Nakamura 0:7d720671e6dc 154 // ACTION: deregister device
Osamu Nakamura 0:7d720671e6dc 155 void DeviceManagementResponder::deregisterDevice(const void *challenge) {
Osamu Nakamura 0:7d720671e6dc 156 // check endpoint
Osamu Nakamura 0:7d720671e6dc 157 if (this->m_endpoint != NULL) {
Osamu Nakamura 0:7d720671e6dc 158 // authenticate
Osamu Nakamura 0:7d720671e6dc 159 if (this->authenticate(challenge)) {
Osamu Nakamura 0:7d720671e6dc 160 // DEBUG
Osamu Nakamura 0:7d720671e6dc 161 this->m_logger->logging("DeviceManagementResponder(deregister): de-registering device...");
Osamu Nakamura 0:7d720671e6dc 162
Osamu Nakamura 0:7d720671e6dc 163 // act
Osamu Nakamura 0:7d720671e6dc 164 ((Connector::Endpoint *)this->m_endpoint)->de_register_endpoint();
Osamu Nakamura 0:7d720671e6dc 165 }
Osamu Nakamura 0:7d720671e6dc 166 else {
Osamu Nakamura 0:7d720671e6dc 167 // authentication failure
Osamu Nakamura 0:7d720671e6dc 168 this->m_logger->logging("DeviceManagementResponder(deregister): authentication failed. No action taken.");
Osamu Nakamura 0:7d720671e6dc 169 }
Osamu Nakamura 0:7d720671e6dc 170 }
Osamu Nakamura 0:7d720671e6dc 171 else {
Osamu Nakamura 0:7d720671e6dc 172 // no endpoint
Osamu Nakamura 0:7d720671e6dc 173 this->m_logger->logging("DeviceManagementResponder(deregister): No endpoint instance. No action taken.");
Osamu Nakamura 0:7d720671e6dc 174 }
Osamu Nakamura 0:7d720671e6dc 175 }
Osamu Nakamura 0:7d720671e6dc 176
Osamu Nakamura 0:7d720671e6dc 177 // ACTION: reboot device
Osamu Nakamura 0:7d720671e6dc 178 void DeviceManagementResponder::rebootDevice(const void *challenge) {
Osamu Nakamura 0:7d720671e6dc 179 // check our Reboot Responder handler pointer
Osamu Nakamura 0:7d720671e6dc 180 if (this->m_reboot_responder_fn != NULL) {
Osamu Nakamura 0:7d720671e6dc 181 // check endpoint
Osamu Nakamura 0:7d720671e6dc 182 if (this->m_endpoint != NULL) {
Osamu Nakamura 0:7d720671e6dc 183 // authenticate
Osamu Nakamura 0:7d720671e6dc 184 if (this->authenticate(challenge)) {
Osamu Nakamura 0:7d720671e6dc 185 // act
Osamu Nakamura 0:7d720671e6dc 186 (*this->m_reboot_responder_fn)((const void *)this->m_endpoint,(const void *)this->m_logger,NULL);
Osamu Nakamura 0:7d720671e6dc 187 }
Osamu Nakamura 0:7d720671e6dc 188 else {
Osamu Nakamura 0:7d720671e6dc 189 // authentication failure
Osamu Nakamura 0:7d720671e6dc 190 this->m_logger->logging("DeviceManagementResponder(reboot): authentication failed. No action taken.");
Osamu Nakamura 0:7d720671e6dc 191 }
Osamu Nakamura 0:7d720671e6dc 192 }
Osamu Nakamura 0:7d720671e6dc 193 else {
Osamu Nakamura 0:7d720671e6dc 194 // no endpoint
Osamu Nakamura 0:7d720671e6dc 195 this->m_logger->logging("DeviceManagementResponder(reboot): No endpoint instance. No action taken.");
Osamu Nakamura 0:7d720671e6dc 196 }
Osamu Nakamura 0:7d720671e6dc 197 }
Osamu Nakamura 0:7d720671e6dc 198 else {
Osamu Nakamura 0:7d720671e6dc 199 // no reset responder handler
Osamu Nakamura 0:7d720671e6dc 200 this->m_logger->logging("DeviceManagementResponder(reboot): No reboot responder handler pointer. No action taken.");
Osamu Nakamura 0:7d720671e6dc 201 }
Osamu Nakamura 0:7d720671e6dc 202 }
Osamu Nakamura 0:7d720671e6dc 203
Osamu Nakamura 0:7d720671e6dc 204 // ACTION: reset device
Osamu Nakamura 0:7d720671e6dc 205 void DeviceManagementResponder::resetDevice(const void *challenge) {
Osamu Nakamura 0:7d720671e6dc 206 // check our Reset Responder handler pointer
Osamu Nakamura 0:7d720671e6dc 207 if (this->m_reset_responder_fn != NULL) {
Osamu Nakamura 0:7d720671e6dc 208 // check endpoint
Osamu Nakamura 0:7d720671e6dc 209 if (this->m_endpoint != NULL) {
Osamu Nakamura 0:7d720671e6dc 210 // authenticate
Osamu Nakamura 0:7d720671e6dc 211 if (this->authenticate(challenge)) {
Osamu Nakamura 0:7d720671e6dc 212 // act
Osamu Nakamura 0:7d720671e6dc 213 (*this->m_reset_responder_fn)((const void *)this->m_endpoint,(const void *)this->m_logger,NULL);
Osamu Nakamura 0:7d720671e6dc 214 }
Osamu Nakamura 0:7d720671e6dc 215 else {
Osamu Nakamura 0:7d720671e6dc 216 // authentication failure
Osamu Nakamura 0:7d720671e6dc 217 this->m_logger->logging("DeviceManagementResponder(reset): authentication failed. No action taken.");
Osamu Nakamura 0:7d720671e6dc 218 }
Osamu Nakamura 0:7d720671e6dc 219 }
Osamu Nakamura 0:7d720671e6dc 220 else {
Osamu Nakamura 0:7d720671e6dc 221 // no endpoint
Osamu Nakamura 0:7d720671e6dc 222 this->m_logger->logging("DeviceManagementResponder(reset): No endpoint instance. No action taken.");
Osamu Nakamura 0:7d720671e6dc 223 }
Osamu Nakamura 0:7d720671e6dc 224 }
Osamu Nakamura 0:7d720671e6dc 225 else {
Osamu Nakamura 0:7d720671e6dc 226 // no reset responder handler
Osamu Nakamura 0:7d720671e6dc 227 this->m_logger->logging("DeviceManagementResponder(reset): No reset responder handler pointer. No action taken.");
Osamu Nakamura 0:7d720671e6dc 228 }
Osamu Nakamura 0:7d720671e6dc 229 }
Osamu Nakamura 0:7d720671e6dc 230
Osamu Nakamura 0:7d720671e6dc 231 // ACTION: invoke FOTA
Osamu Nakamura 0:7d720671e6dc 232 void DeviceManagementResponder::invokeFOTA(const void *challenge) {
Osamu Nakamura 0:7d720671e6dc 233 // check our FOTA invocation handler pointer
Osamu Nakamura 0:7d720671e6dc 234 if (this->m_fota_invocation_fn != NULL) {
Osamu Nakamura 0:7d720671e6dc 235 // check endpoint
Osamu Nakamura 0:7d720671e6dc 236 if (this->m_endpoint != NULL) {
Osamu Nakamura 0:7d720671e6dc 237 // authenticate
Osamu Nakamura 0:7d720671e6dc 238 if (this->authenticate(challenge)) {
Osamu Nakamura 0:7d720671e6dc 239 // act
Osamu Nakamura 0:7d720671e6dc 240 (*this->m_fota_invocation_fn)((const void *)this->m_endpoint,(const void *)this->m_logger,this->getFOTAManifest());
Osamu Nakamura 0:7d720671e6dc 241 }
Osamu Nakamura 0:7d720671e6dc 242 else {
Osamu Nakamura 0:7d720671e6dc 243 // authentication failure
Osamu Nakamura 0:7d720671e6dc 244 this->m_logger->logging("DeviceManagementResponder(FOTA): authentication failed. No action taken.");
Osamu Nakamura 0:7d720671e6dc 245 }
Osamu Nakamura 0:7d720671e6dc 246 }
Osamu Nakamura 0:7d720671e6dc 247 else {
Osamu Nakamura 0:7d720671e6dc 248 // no endpoint
Osamu Nakamura 0:7d720671e6dc 249 this->m_logger->logging("DeviceManagementResponder(FOTA): No endpoint instance. No action taken.");
Osamu Nakamura 0:7d720671e6dc 250 }
Osamu Nakamura 0:7d720671e6dc 251 }
Osamu Nakamura 0:7d720671e6dc 252 else {
Osamu Nakamura 0:7d720671e6dc 253 // no FOTA invocation handler
Osamu Nakamura 0:7d720671e6dc 254 this->m_logger->logging("DeviceManagementResponder(FOTA): No FOTA invocation handler pointer. No action taken.");
Osamu Nakamura 0:7d720671e6dc 255 }
Osamu Nakamura 0:7d720671e6dc 256 }