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