Example

Dependencies:   FXAS21002 FXOS8700Q

Committer:
maygup01
Date:
Tue Nov 19 09:49:38 2019 +0000
Revision:
0:11cc2b7889af
Example

Who changed what in which revision?

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