leo hendrickson / Mbed OS example-Ethernet-mbed-Cloud-connect
Committer:
leothedragon
Date:
Tue May 04 08:55:12 2021 +0000
Revision:
0:8f0bb79ddd48
nmn

Who changed what in which revision?

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