Toyomasa Watarai / simple-mbed-cloud-client

Dependents:  

Committer:
MACRUM
Date:
Mon Jul 02 06:30:39 2018 +0000
Revision:
0:276e7a263c35
Initial commit

Who changed what in which revision?

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