Jim Carver / Mbed OS mbed-cloud-workshop-connect-PIR

Dependencies:   HTS221

Fork of mbed-cloud-workshop-connect-HTS221 by Jim Carver

Committer:
JimCarver
Date:
Thu Oct 25 14:00:12 2018 +0000
Revision:
4:e518dde96e59
Parent:
0:6b753f761943
Simulated dispenser

Who changed what in which revision?

UserRevisionLine numberNew contents of line
JimCarver 0:6b753f761943 1 // ----------------------------------------------------------------------------
JimCarver 0:6b753f761943 2 // Copyright 2016-2017 ARM Ltd.
JimCarver 0:6b753f761943 3 //
JimCarver 0:6b753f761943 4 // SPDX-License-Identifier: Apache-2.0
JimCarver 0:6b753f761943 5 //
JimCarver 0:6b753f761943 6 // Licensed under the Apache License, Version 2.0 (the "License");
JimCarver 0:6b753f761943 7 // you may not use this file except in compliance with the License.
JimCarver 0:6b753f761943 8 // You may obtain a copy of the License at
JimCarver 0:6b753f761943 9 //
JimCarver 0:6b753f761943 10 // http://www.apache.org/licenses/LICENSE-2.0
JimCarver 0:6b753f761943 11 //
JimCarver 0:6b753f761943 12 // Unless required by applicable law or agreed to in writing, software
JimCarver 0:6b753f761943 13 // distributed under the License is distributed on an "AS IS" BASIS,
JimCarver 0:6b753f761943 14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
JimCarver 0:6b753f761943 15 // See the License for the specific language governing permissions and
JimCarver 0:6b753f761943 16 // limitations under the License.
JimCarver 0:6b753f761943 17 // ----------------------------------------------------------------------------
JimCarver 0:6b753f761943 18
JimCarver 0:6b753f761943 19 #ifndef __SERVICE_CLIENT_H__
JimCarver 0:6b753f761943 20 #define __SERVICE_CLIENT_H__
JimCarver 0:6b753f761943 21
JimCarver 0:6b753f761943 22 #include "mbed-cloud-client/MbedCloudClientConfig.h"
JimCarver 0:6b753f761943 23 #ifdef MBED_CLOUD_CLIENT_SUPPORT_UPDATE
JimCarver 0:6b753f761943 24 #include "UpdateClient.h"
JimCarver 0:6b753f761943 25 #endif
JimCarver 0:6b753f761943 26 #include "mbed-client/m2minterface.h"
JimCarver 0:6b753f761943 27 #include "mbed-client/m2mdevice.h"
JimCarver 0:6b753f761943 28 #include "ConnectorClient.h"
JimCarver 0:6b753f761943 29
JimCarver 0:6b753f761943 30 #include <string>
JimCarver 0:6b753f761943 31
JimCarver 0:6b753f761943 32 class M2MSecurity;
JimCarver 0:6b753f761943 33 class ConnectorClientCallback;
JimCarver 0:6b753f761943 34 struct MbedClientDeviceInfo;
JimCarver 0:6b753f761943 35 struct MBedClientInterfaceInfo;
JimCarver 0:6b753f761943 36
JimCarver 0:6b753f761943 37 /**
JimCarver 0:6b753f761943 38 * \brief ServiceClientCallback
JimCarver 0:6b753f761943 39 * A callback class for passing the client progress and error condition to the
JimCarver 0:6b753f761943 40 * MbedCloudClient class object.
JimCarver 0:6b753f761943 41 */
JimCarver 0:6b753f761943 42 class ServiceClientCallback {
JimCarver 0:6b753f761943 43 public:
JimCarver 0:6b753f761943 44
JimCarver 0:6b753f761943 45 typedef enum {
JimCarver 0:6b753f761943 46 Service_Client_Status_Failure = -1,
JimCarver 0:6b753f761943 47 Service_Client_Status_Registered = 0,
JimCarver 0:6b753f761943 48 Service_Client_Status_Unregistered = 1,
JimCarver 0:6b753f761943 49 Service_Client_Status_Register_Updated = 2
JimCarver 0:6b753f761943 50 } ServiceClientCallbackStatus;
JimCarver 0:6b753f761943 51
JimCarver 0:6b753f761943 52 /**
JimCarver 0:6b753f761943 53 * \brief Indicates that the setup or close operation is complete
JimCarver 0:6b753f761943 54 * with success or failure.
JimCarver 0:6b753f761943 55 * \param status, Indicates success or failure in terms of status code.
JimCarver 0:6b753f761943 56 */
JimCarver 0:6b753f761943 57 virtual void complete(ServiceClientCallbackStatus status) = 0;
JimCarver 0:6b753f761943 58
JimCarver 0:6b753f761943 59 /**
JimCarver 0:6b753f761943 60 * \brief Indicates an error condition from one of the underlying clients, including
JimCarver 0:6b753f761943 61 * identity, connector or update client.
JimCarver 0:6b753f761943 62 * \param error, Indicates an error code translated to MbedCloudClient::Error.
JimCarver 0:6b753f761943 63 * \param reason, Indicates human readable text for error description.
JimCarver 0:6b753f761943 64 */
JimCarver 0:6b753f761943 65 virtual void error(int error, const char *reason) = 0;
JimCarver 0:6b753f761943 66
JimCarver 0:6b753f761943 67 /**
JimCarver 0:6b753f761943 68 * \brief A callback indicating that the value of the resource object is updated
JimCarver 0:6b753f761943 69 * by the LWM2M Cloud server.
JimCarver 0:6b753f761943 70 * \param base, The object whose value is updated.
JimCarver 0:6b753f761943 71 * \param type, The type of the object.
JimCarver 0:6b753f761943 72 */
JimCarver 0:6b753f761943 73 virtual void value_updated(M2MBase *base, M2MBase::BaseType type) = 0;
JimCarver 0:6b753f761943 74 };
JimCarver 0:6b753f761943 75
JimCarver 0:6b753f761943 76
JimCarver 0:6b753f761943 77 /**
JimCarver 0:6b753f761943 78 * \brief ServiceClient
JimCarver 0:6b753f761943 79 * This class handles all internal interactions between various client
JimCarver 0:6b753f761943 80 * components including connector, identity and update.
JimCarver 0:6b753f761943 81 * This class maintains the state machine for the use case flow of mbed Cloud
JimCarver 0:6b753f761943 82 * Client.
JimCarver 0:6b753f761943 83 */
JimCarver 0:6b753f761943 84
JimCarver 0:6b753f761943 85 class ServiceClient : private ConnectorClientCallback
JimCarver 0:6b753f761943 86 {
JimCarver 0:6b753f761943 87 public:
JimCarver 0:6b753f761943 88
JimCarver 0:6b753f761943 89 /**
JimCarver 0:6b753f761943 90 * \brief An enum defining the different states of
JimCarver 0:6b753f761943 91 * ServiceClient during the client flow.
JimCarver 0:6b753f761943 92 */
JimCarver 0:6b753f761943 93 enum StartupMainState {
JimCarver 0:6b753f761943 94 State_Init,
JimCarver 0:6b753f761943 95 State_Bootstrap,
JimCarver 0:6b753f761943 96 State_Register,
JimCarver 0:6b753f761943 97 State_Success,
JimCarver 0:6b753f761943 98 State_Failure,
JimCarver 0:6b753f761943 99 State_Unregister
JimCarver 0:6b753f761943 100 };
JimCarver 0:6b753f761943 101
JimCarver 0:6b753f761943 102 public:
JimCarver 0:6b753f761943 103
JimCarver 0:6b753f761943 104 /**
JimCarver 0:6b753f761943 105 * \brief Constructor.
JimCarver 0:6b753f761943 106 * \param interface, Takes the structure that contains the
JimCarver 0:6b753f761943 107 * needed information for an endpoint client to register.
JimCarver 0:6b753f761943 108 */
JimCarver 0:6b753f761943 109 ServiceClient(ServiceClientCallback& callback);
JimCarver 0:6b753f761943 110
JimCarver 0:6b753f761943 111 /**
JimCarver 0:6b753f761943 112 * \brief Destructor.
JimCarver 0:6b753f761943 113 */
JimCarver 0:6b753f761943 114 virtual ~ServiceClient();
JimCarver 0:6b753f761943 115
JimCarver 0:6b753f761943 116 /**
JimCarver 0:6b753f761943 117 * \brief Starts the registration or bootstrap sequence from MbedCloudClient.
JimCarver 0:6b753f761943 118 * \param callback, Takes the callback for the status from ConnectorClient.
JimCarver 0:6b753f761943 119 * \param client_objs, A list of objects to be registered to Cloud.
JimCarver 0:6b753f761943 120 */
JimCarver 0:6b753f761943 121 void initialize_and_register(M2MBaseList& reg_objs);
JimCarver 0:6b753f761943 122
JimCarver 0:6b753f761943 123 /**
JimCarver 0:6b753f761943 124 * \brief Returns the ConnectorClient handler.
JimCarver 0:6b753f761943 125 * \return ConnectorClient, handled for ConnectorClient.
JimCarver 0:6b753f761943 126 */
JimCarver 0:6b753f761943 127 ConnectorClient &connector_client();
JimCarver 0:6b753f761943 128
JimCarver 0:6b753f761943 129 /**
JimCarver 0:6b753f761943 130 * \brief Returns const ConnectorClient handler.
JimCarver 0:6b753f761943 131 * \return const ConnectorClient, handled for ConnectorClient.
JimCarver 0:6b753f761943 132 */
JimCarver 0:6b753f761943 133 const ConnectorClient &connector_client() const;
JimCarver 0:6b753f761943 134
JimCarver 0:6b753f761943 135 /**
JimCarver 0:6b753f761943 136 * \brief Set resource value in the Device Object
JimCarver 0:6b753f761943 137 *
JimCarver 0:6b753f761943 138 * \param resource Device enum to have value set.
JimCarver 0:6b753f761943 139 * \param value String object.
JimCarver 0:6b753f761943 140 * \return True if successful, false otherwise.
JimCarver 0:6b753f761943 141 */
JimCarver 0:6b753f761943 142 bool set_device_resource_value(M2MDevice::DeviceResource resource,
JimCarver 0:6b753f761943 143 const std::string& value);
JimCarver 0:6b753f761943 144
JimCarver 0:6b753f761943 145 /**
JimCarver 0:6b753f761943 146 * \brief Set resource value in the Device Object
JimCarver 0:6b753f761943 147 *
JimCarver 0:6b753f761943 148 * \param resource Device enum to have value set.
JimCarver 0:6b753f761943 149 * \param value Byte buffer.
JimCarver 0:6b753f761943 150 * \param length Buffer length.
JimCarver 0:6b753f761943 151 * \return True if successful, false otherwise.
JimCarver 0:6b753f761943 152 */
JimCarver 0:6b753f761943 153 bool set_device_resource_value(M2MDevice::DeviceResource resource,
JimCarver 0:6b753f761943 154 const char* value,
JimCarver 0:6b753f761943 155 uint32_t length);
JimCarver 0:6b753f761943 156
JimCarver 0:6b753f761943 157 #ifdef MBED_CLOUD_CLIENT_SUPPORT_UPDATE
JimCarver 0:6b753f761943 158 /**
JimCarver 0:6b753f761943 159 * \brief Registers a callback function for authorizing firmware downloads and reboots.
JimCarver 0:6b753f761943 160 * \param handler Callback function.
JimCarver 0:6b753f761943 161 */
JimCarver 0:6b753f761943 162 void set_update_authorize_handler(void (*handler)(int32_t request));
JimCarver 0:6b753f761943 163
JimCarver 0:6b753f761943 164 /**
JimCarver 0:6b753f761943 165 * \brief Authorize request passed to authorization handler.
JimCarver 0:6b753f761943 166 * \param request Request being authorized.
JimCarver 0:6b753f761943 167 */
JimCarver 0:6b753f761943 168 void update_authorize(int32_t request);
JimCarver 0:6b753f761943 169
JimCarver 0:6b753f761943 170 /**
JimCarver 0:6b753f761943 171 * \brief Registers a callback function for monitoring download progress.
JimCarver 0:6b753f761943 172 * \param handler Callback function.
JimCarver 0:6b753f761943 173 */
JimCarver 0:6b753f761943 174 void set_update_progress_handler(void (*handler)(uint32_t progress, uint32_t total));
JimCarver 0:6b753f761943 175
JimCarver 0:6b753f761943 176 /**
JimCarver 0:6b753f761943 177 * \brief Callback function for the Update Client.
JimCarver 0:6b753f761943 178 * \param error Internal Update Client error code.
JimCarver 0:6b753f761943 179 */
JimCarver 0:6b753f761943 180 void update_error_callback(int32_t error);
JimCarver 0:6b753f761943 181 #endif
JimCarver 0:6b753f761943 182
JimCarver 0:6b753f761943 183 protected :
JimCarver 0:6b753f761943 184
JimCarver 0:6b753f761943 185 // Implementation of ConnectorClientCallback
JimCarver 0:6b753f761943 186 /**
JimCarver 0:6b753f761943 187 * \brief Indicates that the registration or unregistration operation is complete
JimCarver 0:6b753f761943 188 * with success or failure.
JimCarver 0:6b753f761943 189 * \param status, Indicates success or failure in terms of status code.
JimCarver 0:6b753f761943 190 */
JimCarver 0:6b753f761943 191 virtual void registration_process_result(ConnectorClient::StartupSubStateRegistration status);
JimCarver 0:6b753f761943 192
JimCarver 0:6b753f761943 193 /**
JimCarver 0:6b753f761943 194 * \brief Indicates a connector error condition from an underlying M2MInterface client.
JimCarver 0:6b753f761943 195 * \param error, Indicates an error code translated from M2MInterface::Error.
JimCarver 0:6b753f761943 196 */
JimCarver 0:6b753f761943 197 virtual void connector_error(M2MInterface::Error error, const char *reason);
JimCarver 0:6b753f761943 198
JimCarver 0:6b753f761943 199 /**
JimCarver 0:6b753f761943 200 * \brief A callback indicating that the value of the resource object is updated
JimCarver 0:6b753f761943 201 * by the LWM2M Cloud server.
JimCarver 0:6b753f761943 202 * \param base, The object whose value is updated.
JimCarver 0:6b753f761943 203 * \param type, The type of the object.
JimCarver 0:6b753f761943 204 */
JimCarver 0:6b753f761943 205 virtual void value_updated(M2MBase *base, M2MBase::BaseType type);
JimCarver 0:6b753f761943 206
JimCarver 0:6b753f761943 207 /**
JimCarver 0:6b753f761943 208 * \brief Redirects the state machine to the right function.
JimCarver 0:6b753f761943 209 * \param current_state, The current state to be set.
JimCarver 0:6b753f761943 210 * \param data, The data to be passed to the state function.
JimCarver 0:6b753f761943 211 */
JimCarver 0:6b753f761943 212 void state_function(StartupMainState current_state);
JimCarver 0:6b753f761943 213
JimCarver 0:6b753f761943 214 /**
JimCarver 0:6b753f761943 215 * \brief The state engine maintaining the state machine logic.
JimCarver 0:6b753f761943 216 */
JimCarver 0:6b753f761943 217 void state_engine(void);
JimCarver 0:6b753f761943 218
JimCarver 0:6b753f761943 219 /**
JimCarver 0:6b753f761943 220 * An external event that can trigger the state machine.
JimCarver 0:6b753f761943 221 * \param new_state, The new state to which the state machine should go.
JimCarver 0:6b753f761943 222 * \param data, The data to be passed to the state machine.
JimCarver 0:6b753f761943 223 */
JimCarver 0:6b753f761943 224 void external_event(StartupMainState new_state);
JimCarver 0:6b753f761943 225
JimCarver 0:6b753f761943 226 /**
JimCarver 0:6b753f761943 227 * An internal event generated by the state machine.
JimCarver 0:6b753f761943 228 * \param new_state, The new state to which the state machine should go.
JimCarver 0:6b753f761943 229 * \param data, The data to be passed to the state machine.
JimCarver 0:6b753f761943 230 */
JimCarver 0:6b753f761943 231 void internal_event(StartupMainState new_state);
JimCarver 0:6b753f761943 232
JimCarver 0:6b753f761943 233 /**
JimCarver 0:6b753f761943 234 * When the bootstrap is started.
JimCarver 0:6b753f761943 235 */
JimCarver 0:6b753f761943 236 void state_bootstrap();
JimCarver 0:6b753f761943 237
JimCarver 0:6b753f761943 238 /**
JimCarver 0:6b753f761943 239 * When the registration is started.
JimCarver 0:6b753f761943 240 */
JimCarver 0:6b753f761943 241 void state_register();
JimCarver 0:6b753f761943 242
JimCarver 0:6b753f761943 243 /**
JimCarver 0:6b753f761943 244 * When the registration is successful.
JimCarver 0:6b753f761943 245 */
JimCarver 0:6b753f761943 246 void state_success();
JimCarver 0:6b753f761943 247
JimCarver 0:6b753f761943 248 /**
JimCarver 0:6b753f761943 249 * When the registration has failed.
JimCarver 0:6b753f761943 250 */
JimCarver 0:6b753f761943 251
JimCarver 0:6b753f761943 252 void state_failure();
JimCarver 0:6b753f761943 253
JimCarver 0:6b753f761943 254 /**
JimCarver 0:6b753f761943 255 * When the client unregisters.
JimCarver 0:6b753f761943 256 */
JimCarver 0:6b753f761943 257 void state_unregister();
JimCarver 0:6b753f761943 258
JimCarver 0:6b753f761943 259 private:
JimCarver 0:6b753f761943 260 M2MDevice* device_object_from_storage();
JimCarver 0:6b753f761943 261
JimCarver 0:6b753f761943 262 /* lookup table for printing hexadecimal values */
JimCarver 0:6b753f761943 263 static const uint8_t hex_table[16];
JimCarver 0:6b753f761943 264
JimCarver 0:6b753f761943 265 ServiceClientCallback &_service_callback;
JimCarver 0:6b753f761943 266 // data which is pending for the registration
JimCarver 0:6b753f761943 267 const char *_service_uri;
JimCarver 0:6b753f761943 268 void *_stack;
JimCarver 0:6b753f761943 269 M2MBaseList *_client_objs;
JimCarver 0:6b753f761943 270 StartupMainState _current_state;
JimCarver 0:6b753f761943 271 bool _event_generated;
JimCarver 0:6b753f761943 272 bool _state_engine_running;
JimCarver 0:6b753f761943 273 #ifdef MBED_CLOUD_CLIENT_SUPPORT_UPDATE
JimCarver 0:6b753f761943 274 bool _setup_update_client;
JimCarver 0:6b753f761943 275 #endif
JimCarver 0:6b753f761943 276 ConnectorClient _connector_client;
JimCarver 0:6b753f761943 277 };
JimCarver 0:6b753f761943 278
JimCarver 0:6b753f761943 279 #endif // !__SERVICE_CLIENT_H__