use TCP to connect to mbed connector

Fork of mbedConnectorInterfaceWithDM by Doug Anson

Committer:
Maggie17
Date:
Mon Nov 28 15:52:56 2016 +0000
Revision:
90:b738617379a6
Parent:
48:c02f2665cf76
first release

Who changed what in which revision?

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