mbed Connector Interface simplification API on top of mbed-client
Fork of mbedConnectorInterfaceV3 by
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!
source/DeviceManagementResponder.cpp@14:d9ce4e56684e, 2016-06-09 (annotated)
- Committer:
- ansond
- Date:
- Thu Jun 09 19:05:18 2016 +0000
- Revision:
- 14:d9ce4e56684e
- Parent:
- 13:9edad7677211
- Child:
- 30:db367366b1f5
updates for DM functions
Who changed what in which revision?
User | Revision | Line number | New 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 | 14:d9ce4e56684e | 38 | this->m_firmware_composite_resource = NULL; |
ansond | 13:9edad7677211 | 39 | } |
ansond | 13:9edad7677211 | 40 | |
ansond | 13:9edad7677211 | 41 | // copy constructor |
ansond | 13:9edad7677211 | 42 | DeviceManagementResponder::DeviceManagementResponder(const DeviceManagementResponder &responder) { |
ansond | 13:9edad7677211 | 43 | this->m_logger = responder.m_logger; |
ansond | 13:9edad7677211 | 44 | this->m_authenticator = responder.m_authenticator; |
ansond | 13:9edad7677211 | 45 | this->m_endpoint = responder.m_endpoint; |
ansond | 13:9edad7677211 | 46 | this->m_reboot_responder_fn = responder.m_reboot_responder_fn; |
ansond | 13:9edad7677211 | 47 | this->m_reset_responder_fn = responder.m_reset_responder_fn; |
ansond | 13:9edad7677211 | 48 | this->m_fota_invocation_fn = responder.m_fota_invocation_fn; |
ansond | 13:9edad7677211 | 49 | this->m_fota_manifest = responder.m_fota_manifest; |
ansond | 14:d9ce4e56684e | 50 | this->m_firmware_composite_resource = responder.m_firmware_composite_resource; |
ansond | 13:9edad7677211 | 51 | } |
ansond | 13:9edad7677211 | 52 | |
ansond | 13:9edad7677211 | 53 | // destructor |
ansond | 13:9edad7677211 | 54 | DeviceManagementResponder::~DeviceManagementResponder() { |
ansond | 13:9edad7677211 | 55 | } |
ansond | 13:9edad7677211 | 56 | |
ansond | 13:9edad7677211 | 57 | // set the Endpoint |
ansond | 13:9edad7677211 | 58 | void DeviceManagementResponder::setEndpoint(const void *ep) { |
ansond | 13:9edad7677211 | 59 | this->m_endpoint = (void *)ep; |
ansond | 13:9edad7677211 | 60 | } |
ansond | 13:9edad7677211 | 61 | |
ansond | 13:9edad7677211 | 62 | // authenticate |
ansond | 13:9edad7677211 | 63 | bool DeviceManagementResponder::authenticate(const void *challenge) { |
ansond | 13:9edad7677211 | 64 | if (this->m_authenticator != NULL) { |
ansond | 13:9edad7677211 | 65 | return this->m_authenticator->authenticate((void *)challenge); |
ansond | 13:9edad7677211 | 66 | } |
ansond | 13:9edad7677211 | 67 | return false; |
ansond | 13:9edad7677211 | 68 | } |
ansond | 13:9edad7677211 | 69 | |
ansond | 13:9edad7677211 | 70 | // set the Reboot Responder handler |
ansond | 13:9edad7677211 | 71 | void DeviceManagementResponder::setRebootResponderHandler(responder_fn reboot_responder_fn) { |
ansond | 13:9edad7677211 | 72 | this->m_reboot_responder_fn = reboot_responder_fn; |
ansond | 13:9edad7677211 | 73 | } |
ansond | 13:9edad7677211 | 74 | |
ansond | 13:9edad7677211 | 75 | // set the Reset Responder handler |
ansond | 13:9edad7677211 | 76 | void DeviceManagementResponder::setResetResponderHandler(responder_fn reset_responder_fn) { |
ansond | 13:9edad7677211 | 77 | this->m_reset_responder_fn = reset_responder_fn; |
ansond | 13:9edad7677211 | 78 | } |
ansond | 13:9edad7677211 | 79 | |
ansond | 13:9edad7677211 | 80 | // set the FOTA invocation handler |
ansond | 13:9edad7677211 | 81 | void DeviceManagementResponder::setFOTAInvocationHandler(responder_fn fota_invocation_fn) { |
ansond | 13:9edad7677211 | 82 | this->m_fota_invocation_fn = fota_invocation_fn; |
ansond | 13:9edad7677211 | 83 | } |
ansond | 13:9edad7677211 | 84 | |
ansond | 13:9edad7677211 | 85 | // set the FOTA manifest |
ansond | 13:9edad7677211 | 86 | void DeviceManagementResponder::setFOTAManifest(const char *fota_manifest) { |
ansond | 13:9edad7677211 | 87 | // DEBUG |
ansond | 13:9edad7677211 | 88 | this->m_logger->log("DeviceManagementResponder(Set FOTA Manifiest): Setting FOTA Manifest: %s",fota_manifest); |
ansond | 13:9edad7677211 | 89 | |
ansond | 13:9edad7677211 | 90 | // set the FOTA manifest for action later |
ansond | 13:9edad7677211 | 91 | this->m_fota_manifest = (char *)fota_manifest; |
ansond | 13:9edad7677211 | 92 | } |
ansond | 13:9edad7677211 | 93 | |
ansond | 14:d9ce4e56684e | 94 | // get the FOTA manifest |
ansond | 14:d9ce4e56684e | 95 | char *DeviceManagementResponder::getFOTAManifest() { return this->m_fota_manifest; } |
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 | // act |
ansond | 13:9edad7677211 | 129 | (*this->m_reboot_responder_fn)((const void *)this->m_endpoint,(const void *)this->m_logger,NULL); |
ansond | 13:9edad7677211 | 130 | } |
ansond | 13:9edad7677211 | 131 | else { |
ansond | 13:9edad7677211 | 132 | // authentication failure |
ansond | 13:9edad7677211 | 133 | this->m_logger->log("DeviceManagementResponder(reboot): authentication failed. No action taken."); |
ansond | 13:9edad7677211 | 134 | } |
ansond | 13:9edad7677211 | 135 | } |
ansond | 13:9edad7677211 | 136 | else { |
ansond | 13:9edad7677211 | 137 | // no endpoint |
ansond | 13:9edad7677211 | 138 | this->m_logger->log("DeviceManagementResponder(reboot): No endpoint instance. No action taken."); |
ansond | 13:9edad7677211 | 139 | } |
ansond | 13:9edad7677211 | 140 | } |
ansond | 13:9edad7677211 | 141 | else { |
ansond | 13:9edad7677211 | 142 | // no reset responder handler |
ansond | 13:9edad7677211 | 143 | this->m_logger->log("DeviceManagementResponder(reboot): No reboot responder handler pointer. No action taken."); |
ansond | 13:9edad7677211 | 144 | } |
ansond | 13:9edad7677211 | 145 | } |
ansond | 13:9edad7677211 | 146 | |
ansond | 13:9edad7677211 | 147 | // ACTION: reset device |
ansond | 13:9edad7677211 | 148 | void DeviceManagementResponder::resetDevice(const void *challenge) { |
ansond | 13:9edad7677211 | 149 | // check our Reset Responder handler pointer |
ansond | 13:9edad7677211 | 150 | if (this->m_reset_responder_fn != NULL) { |
ansond | 13:9edad7677211 | 151 | // check endpoint |
ansond | 13:9edad7677211 | 152 | if (this->m_endpoint != NULL) { |
ansond | 13:9edad7677211 | 153 | // authenticate |
ansond | 13:9edad7677211 | 154 | if (this->authenticate(challenge)) { |
ansond | 13:9edad7677211 | 155 | // act |
ansond | 13:9edad7677211 | 156 | (*this->m_reset_responder_fn)((const void *)this->m_endpoint,(const void *)this->m_logger,NULL); |
ansond | 13:9edad7677211 | 157 | } |
ansond | 13:9edad7677211 | 158 | else { |
ansond | 13:9edad7677211 | 159 | // authentication failure |
ansond | 13:9edad7677211 | 160 | this->m_logger->log("DeviceManagementResponder(reset): authentication failed. No action taken."); |
ansond | 13:9edad7677211 | 161 | } |
ansond | 13:9edad7677211 | 162 | } |
ansond | 13:9edad7677211 | 163 | else { |
ansond | 13:9edad7677211 | 164 | // no endpoint |
ansond | 13:9edad7677211 | 165 | this->m_logger->log("DeviceManagementResponder(reset): No endpoint instance. No action taken."); |
ansond | 13:9edad7677211 | 166 | } |
ansond | 13:9edad7677211 | 167 | } |
ansond | 13:9edad7677211 | 168 | else { |
ansond | 13:9edad7677211 | 169 | // no reset responder handler |
ansond | 13:9edad7677211 | 170 | this->m_logger->log("DeviceManagementResponder(reset): No reset responder handler pointer. No action taken."); |
ansond | 13:9edad7677211 | 171 | } |
ansond | 13:9edad7677211 | 172 | } |
ansond | 13:9edad7677211 | 173 | |
ansond | 13:9edad7677211 | 174 | // ACTION: invoke FOTA |
ansond | 13:9edad7677211 | 175 | void DeviceManagementResponder::invokeFOTA(const void *challenge) { |
ansond | 13:9edad7677211 | 176 | // check our FOTA invocation handler pointer |
ansond | 13:9edad7677211 | 177 | if (this->m_fota_invocation_fn != NULL) { |
ansond | 13:9edad7677211 | 178 | // check endpoint |
ansond | 13:9edad7677211 | 179 | if (this->m_endpoint != NULL) { |
ansond | 13:9edad7677211 | 180 | // authenticate |
ansond | 13:9edad7677211 | 181 | if (this->authenticate(challenge)) { |
ansond | 13:9edad7677211 | 182 | // act |
ansond | 13:9edad7677211 | 183 | (*this->m_fota_invocation_fn)((const void *)this->m_endpoint,(const void *)this->m_logger,this->m_fota_manifest); |
ansond | 13:9edad7677211 | 184 | |
ansond | 13:9edad7677211 | 185 | // reset the FOTA manifest |
ansond | 13:9edad7677211 | 186 | this->m_fota_manifest = 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(FOTA): 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(FOTA): No endpoint instance. No action taken."); |
ansond | 13:9edad7677211 | 196 | } |
ansond | 13:9edad7677211 | 197 | } |
ansond | 13:9edad7677211 | 198 | else { |
ansond | 13:9edad7677211 | 199 | // no FOTA invocation handler |
ansond | 13:9edad7677211 | 200 | this->m_logger->log("DeviceManagementResponder(FOTA): No FOTA invocation handler pointer. No action taken."); |
ansond | 13:9edad7677211 | 201 | } |
ansond | 13:9edad7677211 | 202 | } |
ansond | 13:9edad7677211 | 203 | |
ansond | 14:d9ce4e56684e | 204 | // set the firmware composite resource |
ansond | 14:d9ce4e56684e | 205 | void DeviceManagementResponder::setFirmwareCompositeResource(const void *firmware_composite_resource) { |
ansond | 14:d9ce4e56684e | 206 | this->m_firmware_composite_resource = (void *)firmware_composite_resource; |
ansond | 13:9edad7677211 | 207 | } |
ansond | 13:9edad7677211 | 208 | |
ansond | 14:d9ce4e56684e | 209 | // get the firmware composie resource |
ansond | 14:d9ce4e56684e | 210 | void *DeviceManagementResponder::getFirmwareCompositeResource() { return this->m_firmware_composite_resource; } |