mbed Connector Interface simplification API on top of mbed-client

Fork of mbedConnectorInterfaceV3 by Doug Anson

NOTE:

This repo has been replaced with https://github.com/ARMmbed/mbedConnectorInterface. No further updates will occur with this repo. Please use the github repo instead. Thanks!

Committer:
ansond
Date:
Tue Jun 14 18:40:15 2016 +0000
Revision:
30:db367366b1f5
Parent:
14:d9ce4e56684e
Child:
48:c02f2665cf76
updates

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 13:9edad7677211 37 this->m_reboot_responder_fn = NULL;
ansond 13:9edad7677211 38 this->m_reset_responder_fn = NULL;
ansond 13:9edad7677211 39 this->m_fota_invocation_fn = NULL;
ansond 30:db367366b1f5 40 this->m_manifest = NULL;
ansond 30:db367366b1f5 41 this->m_fota_image = NULL;
ansond 30:db367366b1f5 42 this->m_fota_image_length = 0;
ansond 13:9edad7677211 43 }
ansond 13:9edad7677211 44
ansond 13:9edad7677211 45 // copy constructor
ansond 13:9edad7677211 46 DeviceManagementResponder::DeviceManagementResponder(const DeviceManagementResponder &responder) {
ansond 13:9edad7677211 47 this->m_logger = responder.m_logger;
ansond 13:9edad7677211 48 this->m_authenticator = responder.m_authenticator;
ansond 13:9edad7677211 49 this->m_endpoint = responder.m_endpoint;
ansond 13:9edad7677211 50 this->m_reboot_responder_fn = responder.m_reboot_responder_fn;
ansond 13:9edad7677211 51 this->m_reset_responder_fn = responder.m_reset_responder_fn;
ansond 13:9edad7677211 52 this->m_fota_invocation_fn = responder.m_fota_invocation_fn;
ansond 30:db367366b1f5 53 this->m_manifest = responder.m_manifest;
ansond 30:db367366b1f5 54 this->m_fota_image = responder.m_fota_image;
ansond 30:db367366b1f5 55 this->m_fota_image_length = responder.m_fota_image_length;
ansond 13:9edad7677211 56 }
ansond 13:9edad7677211 57
ansond 13:9edad7677211 58 // destructor
ansond 13:9edad7677211 59 DeviceManagementResponder::~DeviceManagementResponder() {
ansond 13:9edad7677211 60 }
ansond 13:9edad7677211 61
ansond 13:9edad7677211 62 // set the Endpoint
ansond 13:9edad7677211 63 void DeviceManagementResponder::setEndpoint(const void *ep) {
ansond 13:9edad7677211 64 this->m_endpoint = (void *)ep;
ansond 13:9edad7677211 65 }
ansond 13:9edad7677211 66
ansond 13:9edad7677211 67 // authenticate
ansond 13:9edad7677211 68 bool DeviceManagementResponder::authenticate(const void *challenge) {
ansond 13:9edad7677211 69 if (this->m_authenticator != NULL) {
ansond 13:9edad7677211 70 return this->m_authenticator->authenticate((void *)challenge);
ansond 13:9edad7677211 71 }
ansond 13:9edad7677211 72 return false;
ansond 13:9edad7677211 73 }
ansond 13:9edad7677211 74
ansond 13:9edad7677211 75 // set the Reboot Responder handler
ansond 13:9edad7677211 76 void DeviceManagementResponder::setRebootResponderHandler(responder_fn reboot_responder_fn) {
ansond 13:9edad7677211 77 this->m_reboot_responder_fn = reboot_responder_fn;
ansond 13:9edad7677211 78 }
ansond 13:9edad7677211 79
ansond 13:9edad7677211 80 // set the Reset Responder handler
ansond 13:9edad7677211 81 void DeviceManagementResponder::setResetResponderHandler(responder_fn reset_responder_fn) {
ansond 13:9edad7677211 82 this->m_reset_responder_fn = reset_responder_fn;
ansond 13:9edad7677211 83 }
ansond 13:9edad7677211 84
ansond 13:9edad7677211 85 // set the FOTA invocation handler
ansond 13:9edad7677211 86 void DeviceManagementResponder::setFOTAInvocationHandler(responder_fn fota_invocation_fn) {
ansond 13:9edad7677211 87 this->m_fota_invocation_fn = fota_invocation_fn;
ansond 13:9edad7677211 88 }
ansond 13:9edad7677211 89
ansond 13:9edad7677211 90 // set the FOTA manifest
ansond 30:db367366b1f5 91 void DeviceManagementResponder::setFOTAManifest(char *manifest) {
ansond 30:db367366b1f5 92 this->m_manifest = manifest;
ansond 13:9edad7677211 93 }
ansond 13:9edad7677211 94
ansond 14:d9ce4e56684e 95 // get the FOTA manifest
ansond 30:db367366b1f5 96 char *DeviceManagementResponder::getFOTAManifest() {
ansond 30:db367366b1f5 97 return this->m_manifest;
ansond 30:db367366b1f5 98 }
ansond 30:db367366b1f5 99
ansond 30:db367366b1f5 100 // set the FOTA Image
ansond 30:db367366b1f5 101 void DeviceManagementResponder::setFOTAImage(void *fota_image,uint32_t fota_image_length) {
ansond 30:db367366b1f5 102 this->m_fota_image = fota_image;
ansond 30:db367366b1f5 103 this->m_fota_image_length = fota_image_length;
ansond 30:db367366b1f5 104 }
ansond 30:db367366b1f5 105
ansond 30:db367366b1f5 106 // get the FOTA Image
ansond 30:db367366b1f5 107 void *DeviceManagementResponder::getFOTAImage() {
ansond 30:db367366b1f5 108 return this->m_fota_image;
ansond 30:db367366b1f5 109 }
ansond 30:db367366b1f5 110
ansond 30:db367366b1f5 111 // get the FOTA Image Length
ansond 30:db367366b1f5 112 uint32_t DeviceManagementResponder::getFOTAImageLength() {
ansond 30:db367366b1f5 113 return this->m_fota_image_length;
ansond 30:db367366b1f5 114 }
ansond 13:9edad7677211 115
ansond 13:9edad7677211 116 // ACTION: deregister device
ansond 13:9edad7677211 117 void DeviceManagementResponder::deregisterDevice(const void *challenge) {
ansond 13:9edad7677211 118 // check endpoint
ansond 13:9edad7677211 119 if (this->m_endpoint != NULL) {
ansond 13:9edad7677211 120 // authenticate
ansond 13:9edad7677211 121 if (this->authenticate(challenge)) {
ansond 13:9edad7677211 122 // DEBUG
ansond 13:9edad7677211 123 this->m_logger->log("DeviceManagementResponder(deregister): de-registering device...");
ansond 13:9edad7677211 124
ansond 13:9edad7677211 125 // act
ansond 13:9edad7677211 126 ((Connector::Endpoint *)this->m_endpoint)->de_register_endpoint();
ansond 13:9edad7677211 127 }
ansond 13:9edad7677211 128 else {
ansond 13:9edad7677211 129 // authentication failure
ansond 13:9edad7677211 130 this->m_logger->log("DeviceManagementResponder(deregister): authentication failed. No action taken.");
ansond 13:9edad7677211 131 }
ansond 13:9edad7677211 132 }
ansond 13:9edad7677211 133 else {
ansond 13:9edad7677211 134 // no endpoint
ansond 13:9edad7677211 135 this->m_logger->log("DeviceManagementResponder(deregister): No endpoint instance. No action taken.");
ansond 13:9edad7677211 136 }
ansond 13:9edad7677211 137 }
ansond 13:9edad7677211 138
ansond 13:9edad7677211 139 // ACTION: reboot device
ansond 13:9edad7677211 140 void DeviceManagementResponder::rebootDevice(const void *challenge) {
ansond 13:9edad7677211 141 // check our Reboot Responder handler pointer
ansond 13:9edad7677211 142 if (this->m_reboot_responder_fn != NULL) {
ansond 13:9edad7677211 143 // check endpoint
ansond 13:9edad7677211 144 if (this->m_endpoint != NULL) {
ansond 13:9edad7677211 145 // authenticate
ansond 13:9edad7677211 146 if (this->authenticate(challenge)) {
ansond 13:9edad7677211 147 // act
ansond 13:9edad7677211 148 (*this->m_reboot_responder_fn)((const void *)this->m_endpoint,(const void *)this->m_logger,NULL);
ansond 13:9edad7677211 149 }
ansond 13:9edad7677211 150 else {
ansond 13:9edad7677211 151 // authentication failure
ansond 13:9edad7677211 152 this->m_logger->log("DeviceManagementResponder(reboot): authentication failed. No action taken.");
ansond 13:9edad7677211 153 }
ansond 13:9edad7677211 154 }
ansond 13:9edad7677211 155 else {
ansond 13:9edad7677211 156 // no endpoint
ansond 13:9edad7677211 157 this->m_logger->log("DeviceManagementResponder(reboot): No endpoint instance. No action taken.");
ansond 13:9edad7677211 158 }
ansond 13:9edad7677211 159 }
ansond 13:9edad7677211 160 else {
ansond 13:9edad7677211 161 // no reset responder handler
ansond 13:9edad7677211 162 this->m_logger->log("DeviceManagementResponder(reboot): No reboot responder handler pointer. No action taken.");
ansond 13:9edad7677211 163 }
ansond 13:9edad7677211 164 }
ansond 13:9edad7677211 165
ansond 13:9edad7677211 166 // ACTION: reset device
ansond 13:9edad7677211 167 void DeviceManagementResponder::resetDevice(const void *challenge) {
ansond 13:9edad7677211 168 // check our Reset Responder handler pointer
ansond 13:9edad7677211 169 if (this->m_reset_responder_fn != NULL) {
ansond 13:9edad7677211 170 // check endpoint
ansond 13:9edad7677211 171 if (this->m_endpoint != NULL) {
ansond 13:9edad7677211 172 // authenticate
ansond 13:9edad7677211 173 if (this->authenticate(challenge)) {
ansond 13:9edad7677211 174 // act
ansond 13:9edad7677211 175 (*this->m_reset_responder_fn)((const void *)this->m_endpoint,(const void *)this->m_logger,NULL);
ansond 13:9edad7677211 176 }
ansond 13:9edad7677211 177 else {
ansond 13:9edad7677211 178 // authentication failure
ansond 13:9edad7677211 179 this->m_logger->log("DeviceManagementResponder(reset): authentication failed. No action taken.");
ansond 13:9edad7677211 180 }
ansond 13:9edad7677211 181 }
ansond 13:9edad7677211 182 else {
ansond 13:9edad7677211 183 // no endpoint
ansond 13:9edad7677211 184 this->m_logger->log("DeviceManagementResponder(reset): No endpoint instance. No action taken.");
ansond 13:9edad7677211 185 }
ansond 13:9edad7677211 186 }
ansond 13:9edad7677211 187 else {
ansond 13:9edad7677211 188 // no reset responder handler
ansond 13:9edad7677211 189 this->m_logger->log("DeviceManagementResponder(reset): No reset responder handler pointer. No action taken.");
ansond 13:9edad7677211 190 }
ansond 13:9edad7677211 191 }
ansond 13:9edad7677211 192
ansond 13:9edad7677211 193 // ACTION: invoke FOTA
ansond 13:9edad7677211 194 void DeviceManagementResponder::invokeFOTA(const void *challenge) {
ansond 13:9edad7677211 195 // check our FOTA invocation handler pointer
ansond 13:9edad7677211 196 if (this->m_fota_invocation_fn != NULL) {
ansond 13:9edad7677211 197 // check endpoint
ansond 13:9edad7677211 198 if (this->m_endpoint != NULL) {
ansond 13:9edad7677211 199 // authenticate
ansond 13:9edad7677211 200 if (this->authenticate(challenge)) {
ansond 13:9edad7677211 201 // act
ansond 30:db367366b1f5 202 (*this->m_fota_invocation_fn)((const void *)this->m_endpoint,(const void *)this->m_logger,this->getFOTAManifest());
ansond 13:9edad7677211 203 }
ansond 13:9edad7677211 204 else {
ansond 13:9edad7677211 205 // authentication failure
ansond 13:9edad7677211 206 this->m_logger->log("DeviceManagementResponder(FOTA): authentication failed. No action taken.");
ansond 13:9edad7677211 207 }
ansond 13:9edad7677211 208 }
ansond 13:9edad7677211 209 else {
ansond 13:9edad7677211 210 // no endpoint
ansond 13:9edad7677211 211 this->m_logger->log("DeviceManagementResponder(FOTA): No endpoint instance. No action taken.");
ansond 13:9edad7677211 212 }
ansond 13:9edad7677211 213 }
ansond 13:9edad7677211 214 else {
ansond 13:9edad7677211 215 // no FOTA invocation handler
ansond 13:9edad7677211 216 this->m_logger->log("DeviceManagementResponder(FOTA): No FOTA invocation handler pointer. No action taken.");
ansond 13:9edad7677211 217 }
ansond 30:db367366b1f5 218 }