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:
Wed Jun 08 22:32:08 2016 +0000
Revision:
13:9edad7677211
Child:
14:d9ce4e56684e
updated to latest revision with new DM functions

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 13:9edad7677211 29 // constructor
ansond 13:9edad7677211 30 DeviceManagementResponder::DeviceManagementResponder(const Logger *logger,const Authenticator *authenticator) {
ansond 13:9edad7677211 31 this->m_logger = (Logger *)logger;
ansond 13:9edad7677211 32 this->m_authenticator = (Authenticator *)authenticator;
ansond 13:9edad7677211 33 this->m_endpoint = NULL;
ansond 13:9edad7677211 34 this->m_reboot_responder_fn = NULL;
ansond 13:9edad7677211 35 this->m_reset_responder_fn = NULL;
ansond 13:9edad7677211 36 this->m_fota_invocation_fn = NULL;
ansond 13:9edad7677211 37 this->m_fota_manifest = NULL;
ansond 13:9edad7677211 38 this->m_state_resource = NULL;
ansond 13:9edad7677211 39 this->m_result_resource = NULL;
ansond 13:9edad7677211 40 }
ansond 13:9edad7677211 41
ansond 13:9edad7677211 42 // copy constructor
ansond 13:9edad7677211 43 DeviceManagementResponder::DeviceManagementResponder(const DeviceManagementResponder &responder) {
ansond 13:9edad7677211 44 this->m_logger = responder.m_logger;
ansond 13:9edad7677211 45 this->m_authenticator = responder.m_authenticator;
ansond 13:9edad7677211 46 this->m_endpoint = responder.m_endpoint;
ansond 13:9edad7677211 47 this->m_reboot_responder_fn = responder.m_reboot_responder_fn;
ansond 13:9edad7677211 48 this->m_reset_responder_fn = responder.m_reset_responder_fn;
ansond 13:9edad7677211 49 this->m_fota_invocation_fn = responder.m_fota_invocation_fn;
ansond 13:9edad7677211 50 this->m_fota_manifest = responder.m_fota_manifest;
ansond 13:9edad7677211 51 this->m_state_resource = responder.m_state_resource;
ansond 13:9edad7677211 52 this->m_result_resource = responder.m_result_resource;
ansond 13:9edad7677211 53 }
ansond 13:9edad7677211 54
ansond 13:9edad7677211 55 // destructor
ansond 13:9edad7677211 56 DeviceManagementResponder::~DeviceManagementResponder() {
ansond 13:9edad7677211 57 }
ansond 13:9edad7677211 58
ansond 13:9edad7677211 59 // set the Endpoint
ansond 13:9edad7677211 60 void DeviceManagementResponder::setEndpoint(const void *ep) {
ansond 13:9edad7677211 61 this->m_endpoint = (void *)ep;
ansond 13:9edad7677211 62 }
ansond 13:9edad7677211 63
ansond 13:9edad7677211 64 // authenticate
ansond 13:9edad7677211 65 bool DeviceManagementResponder::authenticate(const void *challenge) {
ansond 13:9edad7677211 66 if (this->m_authenticator != NULL) {
ansond 13:9edad7677211 67 return this->m_authenticator->authenticate((void *)challenge);
ansond 13:9edad7677211 68 }
ansond 13:9edad7677211 69 return false;
ansond 13:9edad7677211 70 }
ansond 13:9edad7677211 71
ansond 13:9edad7677211 72 // set the Reboot Responder handler
ansond 13:9edad7677211 73 void DeviceManagementResponder::setRebootResponderHandler(responder_fn reboot_responder_fn) {
ansond 13:9edad7677211 74 this->m_reboot_responder_fn = reboot_responder_fn;
ansond 13:9edad7677211 75 }
ansond 13:9edad7677211 76
ansond 13:9edad7677211 77 // set the Reset Responder handler
ansond 13:9edad7677211 78 void DeviceManagementResponder::setResetResponderHandler(responder_fn reset_responder_fn) {
ansond 13:9edad7677211 79 this->m_reset_responder_fn = reset_responder_fn;
ansond 13:9edad7677211 80 }
ansond 13:9edad7677211 81
ansond 13:9edad7677211 82 // set the FOTA invocation handler
ansond 13:9edad7677211 83 void DeviceManagementResponder::setFOTAInvocationHandler(responder_fn fota_invocation_fn) {
ansond 13:9edad7677211 84 this->m_fota_invocation_fn = fota_invocation_fn;
ansond 13:9edad7677211 85 }
ansond 13:9edad7677211 86
ansond 13:9edad7677211 87 // set the FOTA manifest
ansond 13:9edad7677211 88 void DeviceManagementResponder::setFOTAManifest(const char *fota_manifest) {
ansond 13:9edad7677211 89 // DEBUG
ansond 13:9edad7677211 90 this->m_logger->log("DeviceManagementResponder(Set FOTA Manifiest): Setting FOTA Manifest: %s",fota_manifest);
ansond 13:9edad7677211 91
ansond 13:9edad7677211 92 // set the FOTA manifest for action later
ansond 13:9edad7677211 93 this->m_fota_manifest = (char *)fota_manifest;
ansond 13:9edad7677211 94 }
ansond 13:9edad7677211 95
ansond 13:9edad7677211 96
ansond 13:9edad7677211 97 // ACTION: deregister device
ansond 13:9edad7677211 98 void DeviceManagementResponder::deregisterDevice(const void *challenge) {
ansond 13:9edad7677211 99 // check endpoint
ansond 13:9edad7677211 100 if (this->m_endpoint != NULL) {
ansond 13:9edad7677211 101 // authenticate
ansond 13:9edad7677211 102 if (this->authenticate(challenge)) {
ansond 13:9edad7677211 103 // DEBUG
ansond 13:9edad7677211 104 this->m_logger->log("DeviceManagementResponder(deregister): de-registering device...");
ansond 13:9edad7677211 105
ansond 13:9edad7677211 106 // act
ansond 13:9edad7677211 107 ((Connector::Endpoint *)this->m_endpoint)->de_register_endpoint();
ansond 13:9edad7677211 108 }
ansond 13:9edad7677211 109 else {
ansond 13:9edad7677211 110 // authentication failure
ansond 13:9edad7677211 111 this->m_logger->log("DeviceManagementResponder(deregister): authentication failed. No action taken.");
ansond 13:9edad7677211 112 }
ansond 13:9edad7677211 113 }
ansond 13:9edad7677211 114 else {
ansond 13:9edad7677211 115 // no endpoint
ansond 13:9edad7677211 116 this->m_logger->log("DeviceManagementResponder(deregister): No endpoint instance. No action taken.");
ansond 13:9edad7677211 117 }
ansond 13:9edad7677211 118 }
ansond 13:9edad7677211 119
ansond 13:9edad7677211 120 // ACTION: reboot device
ansond 13:9edad7677211 121 void DeviceManagementResponder::rebootDevice(const void *challenge) {
ansond 13:9edad7677211 122 // check our Reboot Responder handler pointer
ansond 13:9edad7677211 123 if (this->m_reboot_responder_fn != NULL) {
ansond 13:9edad7677211 124 // check endpoint
ansond 13:9edad7677211 125 if (this->m_endpoint != NULL) {
ansond 13:9edad7677211 126 // authenticate
ansond 13:9edad7677211 127 if (this->authenticate(challenge)) {
ansond 13:9edad7677211 128 // DEBUG
ansond 13:9edad7677211 129 this->m_logger->log("DeviceManagementResponder(reboot): de-registering device...");
ansond 13:9edad7677211 130
ansond 13:9edad7677211 131 // act
ansond 13:9edad7677211 132 ((Connector::Endpoint *)this->m_endpoint)->de_register_endpoint();
ansond 13:9edad7677211 133
ansond 13:9edad7677211 134 // DEBUG
ansond 13:9edad7677211 135 this->m_logger->log("DeviceManagementResponder(reboot): rebooting device...");
ansond 13:9edad7677211 136
ansond 13:9edad7677211 137 // act
ansond 13:9edad7677211 138 (*this->m_reboot_responder_fn)((const void *)this->m_endpoint,(const void *)this->m_logger,NULL);
ansond 13:9edad7677211 139 }
ansond 13:9edad7677211 140 else {
ansond 13:9edad7677211 141 // authentication failure
ansond 13:9edad7677211 142 this->m_logger->log("DeviceManagementResponder(reboot): authentication failed. No action taken.");
ansond 13:9edad7677211 143 }
ansond 13:9edad7677211 144 }
ansond 13:9edad7677211 145 else {
ansond 13:9edad7677211 146 // no endpoint
ansond 13:9edad7677211 147 this->m_logger->log("DeviceManagementResponder(reboot): No endpoint instance. No action taken.");
ansond 13:9edad7677211 148 }
ansond 13:9edad7677211 149 }
ansond 13:9edad7677211 150 else {
ansond 13:9edad7677211 151 // no reset responder handler
ansond 13:9edad7677211 152 this->m_logger->log("DeviceManagementResponder(reboot): No reboot responder handler pointer. No action taken.");
ansond 13:9edad7677211 153 }
ansond 13:9edad7677211 154 }
ansond 13:9edad7677211 155
ansond 13:9edad7677211 156 // ACTION: reset device
ansond 13:9edad7677211 157 void DeviceManagementResponder::resetDevice(const void *challenge) {
ansond 13:9edad7677211 158 // check our Reset Responder handler pointer
ansond 13:9edad7677211 159 if (this->m_reset_responder_fn != NULL) {
ansond 13:9edad7677211 160 // check endpoint
ansond 13:9edad7677211 161 if (this->m_endpoint != NULL) {
ansond 13:9edad7677211 162 // authenticate
ansond 13:9edad7677211 163 if (this->authenticate(challenge)) {
ansond 13:9edad7677211 164 // DEBUG
ansond 13:9edad7677211 165 this->m_logger->log("DeviceManagementResponder(reset): de-registering device...");
ansond 13:9edad7677211 166
ansond 13:9edad7677211 167 // act
ansond 13:9edad7677211 168 ((Connector::Endpoint *)this->m_endpoint)->de_register_endpoint();
ansond 13:9edad7677211 169
ansond 13:9edad7677211 170 // DEBUG
ansond 13:9edad7677211 171 this->m_logger->log("DeviceManagementResponder(reset): resetting device...");
ansond 13:9edad7677211 172
ansond 13:9edad7677211 173 // act
ansond 13:9edad7677211 174 (*this->m_reset_responder_fn)((const void *)this->m_endpoint,(const void *)this->m_logger,NULL);
ansond 13:9edad7677211 175 }
ansond 13:9edad7677211 176 else {
ansond 13:9edad7677211 177 // authentication failure
ansond 13:9edad7677211 178 this->m_logger->log("DeviceManagementResponder(reset): authentication failed. No action taken.");
ansond 13:9edad7677211 179 }
ansond 13:9edad7677211 180 }
ansond 13:9edad7677211 181 else {
ansond 13:9edad7677211 182 // no endpoint
ansond 13:9edad7677211 183 this->m_logger->log("DeviceManagementResponder(reset): No endpoint instance. No action taken.");
ansond 13:9edad7677211 184 }
ansond 13:9edad7677211 185 }
ansond 13:9edad7677211 186 else {
ansond 13:9edad7677211 187 // no reset responder handler
ansond 13:9edad7677211 188 this->m_logger->log("DeviceManagementResponder(reset): No reset responder handler pointer. No action taken.");
ansond 13:9edad7677211 189 }
ansond 13:9edad7677211 190 }
ansond 13:9edad7677211 191
ansond 13:9edad7677211 192 // ACTION: invoke FOTA
ansond 13:9edad7677211 193 void DeviceManagementResponder::invokeFOTA(const void *challenge) {
ansond 13:9edad7677211 194 // check our FOTA invocation handler pointer
ansond 13:9edad7677211 195 if (this->m_fota_invocation_fn != NULL) {
ansond 13:9edad7677211 196 // check endpoint
ansond 13:9edad7677211 197 if (this->m_endpoint != NULL) {
ansond 13:9edad7677211 198 // authenticate
ansond 13:9edad7677211 199 if (this->authenticate(challenge)) {
ansond 13:9edad7677211 200 // DEBUG
ansond 13:9edad7677211 201 this->m_logger->log("DeviceManagementResponder(FOTA): de-registering device...");
ansond 13:9edad7677211 202
ansond 13:9edad7677211 203 // act
ansond 13:9edad7677211 204 ((Connector::Endpoint *)this->m_endpoint)->de_register_endpoint();
ansond 13:9edad7677211 205
ansond 13:9edad7677211 206 // DEBUG
ansond 13:9edad7677211 207 this->m_logger->log("DeviceManagementResponder(FOTA): invoking FOTA on device...");
ansond 13:9edad7677211 208
ansond 13:9edad7677211 209 // act
ansond 13:9edad7677211 210 (*this->m_fota_invocation_fn)((const void *)this->m_endpoint,(const void *)this->m_logger,this->m_fota_manifest);
ansond 13:9edad7677211 211
ansond 13:9edad7677211 212 // reset the FOTA manifest
ansond 13:9edad7677211 213 this->m_fota_manifest = 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(FOTA): 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(FOTA): No endpoint instance. No action taken.");
ansond 13:9edad7677211 223 }
ansond 13:9edad7677211 224 }
ansond 13:9edad7677211 225 else {
ansond 13:9edad7677211 226 // no FOTA invocation handler
ansond 13:9edad7677211 227 this->m_logger->log("DeviceManagementResponder(FOTA): No FOTA invocation handler pointer. No action taken.");
ansond 13:9edad7677211 228 }
ansond 13:9edad7677211 229 }
ansond 13:9edad7677211 230
ansond 13:9edad7677211 231 // set the state resource
ansond 13:9edad7677211 232 void DeviceManagementResponder::setStateResource(const void *state_resource) {
ansond 13:9edad7677211 233 this->m_state_resource = (void *)state_resource;
ansond 13:9edad7677211 234 }
ansond 13:9edad7677211 235
ansond 13:9edad7677211 236 // get the state resource
ansond 13:9edad7677211 237 void *DeviceManagementResponder::getStateResource() { return this->m_state_resource; }
ansond 13:9edad7677211 238
ansond 13:9edad7677211 239 // set the result resource
ansond 13:9edad7677211 240 void DeviceManagementResponder::setResultResource(const void *result_resource) {
ansond 13:9edad7677211 241 this->m_result_resource = (void *)result_resource;
ansond 13:9edad7677211 242 }
ansond 13:9edad7677211 243
ansond 13:9edad7677211 244 // get the result resource
ansond 13:9edad7677211 245 void *DeviceManagementResponder::getResultResource() { return this->m_result_resource; }
ansond 13:9edad7677211 246