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 (c) 2015 ARM Limited. All rights reserved.
maygup01 0:11cc2b7889af 3 * SPDX-License-Identifier: Apache-2.0
maygup01 0:11cc2b7889af 4 * Licensed under the Apache License, Version 2.0 (the License); you may
maygup01 0:11cc2b7889af 5 * not use this file except in compliance with the License.
maygup01 0:11cc2b7889af 6 * You may obtain a copy of the License at
maygup01 0:11cc2b7889af 7 *
maygup01 0:11cc2b7889af 8 * http://www.apache.org/licenses/LICENSE-2.0
maygup01 0:11cc2b7889af 9 *
maygup01 0:11cc2b7889af 10 * Unless required by applicable law or agreed to in writing, software
maygup01 0:11cc2b7889af 11 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
maygup01 0:11cc2b7889af 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
maygup01 0:11cc2b7889af 13 * See the License for the specific language governing permissions and
maygup01 0:11cc2b7889af 14 * limitations under the License.
maygup01 0:11cc2b7889af 15 */
maygup01 0:11cc2b7889af 16 #ifndef M2M_INTERFACE_IMPL_H
maygup01 0:11cc2b7889af 17 #define M2M_INTERFACE_IMPL_H
maygup01 0:11cc2b7889af 18
maygup01 0:11cc2b7889af 19 #include "mbed-client/m2minterface.h"
maygup01 0:11cc2b7889af 20 #include "mbed-client/m2mserver.h"
maygup01 0:11cc2b7889af 21 #include "mbed-client/m2mconnectionobserver.h"
maygup01 0:11cc2b7889af 22 #include "mbed-client/m2mconnectionsecurity.h"
maygup01 0:11cc2b7889af 23 #include "include/m2mnsdlobserver.h"
maygup01 0:11cc2b7889af 24 #include "include/m2mnsdlinterface.h"
maygup01 0:11cc2b7889af 25 #include "mbed-client/m2mtimerobserver.h"
maygup01 0:11cc2b7889af 26 #include "mbed-client/m2mtimer.h"
maygup01 0:11cc2b7889af 27 #include "mbed-client/m2mconnectionhandler.h"
maygup01 0:11cc2b7889af 28 #include "mbed-client/m2mconstants.h"
maygup01 0:11cc2b7889af 29
maygup01 0:11cc2b7889af 30 //FORWARD DECLARATION
maygup01 0:11cc2b7889af 31 class M2MConnectionSecurity;
maygup01 0:11cc2b7889af 32 class EventData;
maygup01 0:11cc2b7889af 33 class M2MUpdateRegisterData;
maygup01 0:11cc2b7889af 34 /**
maygup01 0:11cc2b7889af 35 * @brief M2MInterfaceImpl.
maygup01 0:11cc2b7889af 36 * This class implements handling of all mbed Client Interface operations
maygup01 0:11cc2b7889af 37 * defined in OMA LWM2M specifications.
maygup01 0:11cc2b7889af 38 * This includes Bootstrapping, Client Registration, Device Management &
maygup01 0:11cc2b7889af 39 * Service Enablement and Information Reporting.
maygup01 0:11cc2b7889af 40 */
maygup01 0:11cc2b7889af 41
maygup01 0:11cc2b7889af 42 class M2MInterfaceImpl : public M2MInterface,
maygup01 0:11cc2b7889af 43 public M2MNsdlObserver,
maygup01 0:11cc2b7889af 44 public M2MConnectionObserver,
maygup01 0:11cc2b7889af 45 public M2MTimerObserver
maygup01 0:11cc2b7889af 46 {
maygup01 0:11cc2b7889af 47 private:
maygup01 0:11cc2b7889af 48 // Prevents the use of assignment operator by accident.
maygup01 0:11cc2b7889af 49 M2MInterfaceImpl& operator=( const M2MInterfaceImpl& /*other*/ );
maygup01 0:11cc2b7889af 50
maygup01 0:11cc2b7889af 51 // Prevents the use of copy constructor by accident
maygup01 0:11cc2b7889af 52 M2MInterfaceImpl( const M2MInterfaceImpl& /*other*/ );
maygup01 0:11cc2b7889af 53
maygup01 0:11cc2b7889af 54 friend class M2MInterfaceFactory;
maygup01 0:11cc2b7889af 55
maygup01 0:11cc2b7889af 56 private:
maygup01 0:11cc2b7889af 57
maygup01 0:11cc2b7889af 58 /**
maygup01 0:11cc2b7889af 59 * @brief Constructor
maygup01 0:11cc2b7889af 60 * @param observer, Observer to pass the event callbacks for various
maygup01 0:11cc2b7889af 61 * interface operations.
maygup01 0:11cc2b7889af 62 * @param endpoint_name Endpoint name of the client.
maygup01 0:11cc2b7889af 63 * @param endpoint_type Endpoint type of the client.
maygup01 0:11cc2b7889af 64 * @param life_time Life time of the client in seconds
maygup01 0:11cc2b7889af 65 * @param listen_port Listening port for the endpoint, default is 8000.
maygup01 0:11cc2b7889af 66 * @param domain Domain of the client.
maygup01 0:11cc2b7889af 67 * @param mode Binding mode of the client, default is UDP
maygup01 0:11cc2b7889af 68 * @param stack Network stack to be used for connection, default is LwIP_IPv4.
maygup01 0:11cc2b7889af 69 * @param context_address Context address, default is empty.
maygup01 0:11cc2b7889af 70 */
maygup01 0:11cc2b7889af 71 M2MInterfaceImpl(M2MInterfaceObserver& observer,
maygup01 0:11cc2b7889af 72 const String &endpoint_name,
maygup01 0:11cc2b7889af 73 const String &endpoint_type,
maygup01 0:11cc2b7889af 74 const int32_t life_time,
maygup01 0:11cc2b7889af 75 const uint16_t listen_port,
maygup01 0:11cc2b7889af 76 const String &domain = "",
maygup01 0:11cc2b7889af 77 BindingMode mode = M2MInterface::NOT_SET,
maygup01 0:11cc2b7889af 78 M2MInterface::NetworkStack stack = M2MInterface::LwIP_IPv4,
maygup01 0:11cc2b7889af 79 const String &context_address = "");
maygup01 0:11cc2b7889af 80
maygup01 0:11cc2b7889af 81 public:
maygup01 0:11cc2b7889af 82
maygup01 0:11cc2b7889af 83 /**
maygup01 0:11cc2b7889af 84 * @brief Destructor
maygup01 0:11cc2b7889af 85 */
maygup01 0:11cc2b7889af 86 virtual ~M2MInterfaceImpl();
maygup01 0:11cc2b7889af 87
maygup01 0:11cc2b7889af 88 /**
maygup01 0:11cc2b7889af 89 * @brief Initiates bootstrapping of the client with the provided Bootstrap
maygup01 0:11cc2b7889af 90 * server information.
maygup01 0:11cc2b7889af 91 * @param security_object Security object which contains information
maygup01 0:11cc2b7889af 92 * required for successful bootstrapping of the client.
maygup01 0:11cc2b7889af 93 */
maygup01 0:11cc2b7889af 94 virtual void bootstrap(M2MSecurity *security);
maygup01 0:11cc2b7889af 95
maygup01 0:11cc2b7889af 96 /**
maygup01 0:11cc2b7889af 97 * @brief Cancels on going bootstrapping operation of the client. If the client has
maygup01 0:11cc2b7889af 98 * already successfully bootstrapped then this function deletes existing
maygup01 0:11cc2b7889af 99 * bootstrap information from the client.
maygup01 0:11cc2b7889af 100 */
maygup01 0:11cc2b7889af 101 virtual void cancel_bootstrap();
maygup01 0:11cc2b7889af 102
maygup01 0:11cc2b7889af 103 /**
maygup01 0:11cc2b7889af 104 * @brief Finishes on going bootstrap in cases where client is the one to finish it.
maygup01 0:11cc2b7889af 105 */
maygup01 0:11cc2b7889af 106 virtual void finish_bootstrap();
maygup01 0:11cc2b7889af 107
maygup01 0:11cc2b7889af 108 /**
maygup01 0:11cc2b7889af 109 * @brief Initiates registration of the provided Security object to the
maygup01 0:11cc2b7889af 110 * corresponding LWM2M server.
maygup01 0:11cc2b7889af 111 * @param security_object Security object which contains information
maygup01 0:11cc2b7889af 112 * required for registering to the LWM2M server.
maygup01 0:11cc2b7889af 113 * If client wants to register to multiple LWM2M servers then it has call
maygup01 0:11cc2b7889af 114 * this function once for each of LWM2M server object separately.
maygup01 0:11cc2b7889af 115 * @param object_list Objects which contains information
maygup01 0:11cc2b7889af 116 * which the client want to register to the LWM2M server.
maygup01 0:11cc2b7889af 117 */
maygup01 0:11cc2b7889af 118 virtual void register_object(M2MSecurity *security_object, const M2MBaseList &list);
maygup01 0:11cc2b7889af 119
maygup01 0:11cc2b7889af 120 /**
maygup01 0:11cc2b7889af 121 * @brief Initiates registration of the provided Security object to the
maygup01 0:11cc2b7889af 122 * corresponding LWM2M server.
maygup01 0:11cc2b7889af 123 * @param security_object Security object which contains information
maygup01 0:11cc2b7889af 124 * required for registering to the LWM2M server.
maygup01 0:11cc2b7889af 125 * If client wants to register to multiple LWM2M servers then it has call
maygup01 0:11cc2b7889af 126 * this function once for each of LWM2M server object separately.
maygup01 0:11cc2b7889af 127 * @param object_list Objects which contains information
maygup01 0:11cc2b7889af 128 * which the client want to register to the LWM2M server.
maygup01 0:11cc2b7889af 129 */
maygup01 0:11cc2b7889af 130 virtual void register_object(M2MSecurity *security_object, const M2MObjectList &object_list);
maygup01 0:11cc2b7889af 131
maygup01 0:11cc2b7889af 132 /**
maygup01 0:11cc2b7889af 133 * @brief Updates or refreshes the client's registration on the LWM2M
maygup01 0:11cc2b7889af 134 * server.
maygup01 0:11cc2b7889af 135 * @param security_object Security object from which the device object
maygup01 0:11cc2b7889af 136 * needs to update registration, if there is only one LWM2M server registered
maygup01 0:11cc2b7889af 137 * then this parameter can be NULL.
maygup01 0:11cc2b7889af 138 * @param lifetime Lifetime for the endpoint client in seconds.
maygup01 0:11cc2b7889af 139 */
maygup01 0:11cc2b7889af 140 virtual void update_registration(M2MSecurity *security_object, const uint32_t lifetime = 0);
maygup01 0:11cc2b7889af 141
maygup01 0:11cc2b7889af 142 /**
maygup01 0:11cc2b7889af 143 * @brief Updates or refreshes the client's registration on the LWM2M
maygup01 0:11cc2b7889af 144 * server. Use this function to publish new objects to LWM2M server.
maygup01 0:11cc2b7889af 145 * @param security_object The security object from which the device object
maygup01 0:11cc2b7889af 146 * needs to update the registration. If there is only one LWM2M server registered,
maygup01 0:11cc2b7889af 147 * this parameter can be NULL.
maygup01 0:11cc2b7889af 148 * @param object_list Objects that contain information about the
maygup01 0:11cc2b7889af 149 * client attempting to register to the LWM2M server.
maygup01 0:11cc2b7889af 150 * @param lifetime The lifetime of the endpoint client in seconds. If the same value
maygup01 0:11cc2b7889af 151 * has to be passed, set the default value to 0.
maygup01 0:11cc2b7889af 152 */
maygup01 0:11cc2b7889af 153 virtual void update_registration(M2MSecurity *security_object, const M2MBaseList &list,
maygup01 0:11cc2b7889af 154 const uint32_t lifetime = 0);
maygup01 0:11cc2b7889af 155
maygup01 0:11cc2b7889af 156 /**
maygup01 0:11cc2b7889af 157 * @brief Updates or refreshes the client's registration on the LWM2M
maygup01 0:11cc2b7889af 158 * server. Use this function to publish new objects to LWM2M server.
maygup01 0:11cc2b7889af 159 * @param security_object The security object from which the device object
maygup01 0:11cc2b7889af 160 * needs to update the registration. If there is only one LWM2M server registered,
maygup01 0:11cc2b7889af 161 * this parameter can be NULL.
maygup01 0:11cc2b7889af 162 * @param object_list Objects that contain information about the
maygup01 0:11cc2b7889af 163 * client attempting to register to the LWM2M server.
maygup01 0:11cc2b7889af 164 * @param lifetime The lifetime of the endpoint client in seconds. If the same value
maygup01 0:11cc2b7889af 165 * has to be passed, set the default value to 0.
maygup01 0:11cc2b7889af 166 */
maygup01 0:11cc2b7889af 167 virtual void update_registration(M2MSecurity *security_object, const M2MObjectList &object_list,
maygup01 0:11cc2b7889af 168 const uint32_t lifetime = 0);
maygup01 0:11cc2b7889af 169
maygup01 0:11cc2b7889af 170 /**
maygup01 0:11cc2b7889af 171 * @brief Unregisters the registered object from the LWM2M server
maygup01 0:11cc2b7889af 172 * @param security_object Security object from which the device object
maygup01 0:11cc2b7889af 173 * needs to be unregistered. If there is only one LWM2M server registered
maygup01 0:11cc2b7889af 174 * this parameter can be NULL.
maygup01 0:11cc2b7889af 175 */
maygup01 0:11cc2b7889af 176 virtual void unregister_object(M2MSecurity* security = NULL);
maygup01 0:11cc2b7889af 177
maygup01 0:11cc2b7889af 178 /**
maygup01 0:11cc2b7889af 179 * @brief Sets the function which will be called indicating client
maygup01 0:11cc2b7889af 180 * is going to sleep when the Binding mode is selected with Queue mode.
maygup01 0:11cc2b7889af 181 * @param callback A function pointer that will be called when client
maygup01 0:11cc2b7889af 182 * goes to sleep.
maygup01 0:11cc2b7889af 183 */
maygup01 0:11cc2b7889af 184 virtual void set_queue_sleep_handler(callback_handler handler);
maygup01 0:11cc2b7889af 185
maygup01 0:11cc2b7889af 186 /**
maygup01 0:11cc2b7889af 187 * @brief Sets the network interface handler that is used by client to connect
maygup01 0:11cc2b7889af 188 * to a network over IP.
maygup01 0:11cc2b7889af 189 * @param handler A network interface handler that is used by client to connect.
maygup01 0:11cc2b7889af 190 * This API is optional but provides a mechanism for different platforms to
maygup01 0:11cc2b7889af 191 * manage usage of underlying network interface by client.
maygup01 0:11cc2b7889af 192 */
maygup01 0:11cc2b7889af 193 virtual void set_platform_network_handler(void *handler = NULL);
maygup01 0:11cc2b7889af 194
maygup01 0:11cc2b7889af 195 /**
maygup01 0:11cc2b7889af 196 * \brief Sets the function callback that will be called by mbed-client for
maygup01 0:11cc2b7889af 197 * fetching random number from application for ensuring strong entropy.
maygup01 0:11cc2b7889af 198 * \param random_callback A function pointer that will be called by mbed-client
maygup01 0:11cc2b7889af 199 * while performing secure handshake.
maygup01 0:11cc2b7889af 200 * Function signature should be uint32_t (*random_number_callback)(void);
maygup01 0:11cc2b7889af 201 */
maygup01 0:11cc2b7889af 202 virtual void set_random_number_callback(random_number_cb callback);
maygup01 0:11cc2b7889af 203
maygup01 0:11cc2b7889af 204 /**
maygup01 0:11cc2b7889af 205 * \brief Sets the function callback that will be called by mbed-client for
maygup01 0:11cc2b7889af 206 * providing entropy source from application for ensuring strong entropy.
maygup01 0:11cc2b7889af 207 * \param entropy_callback A function pointer that will be called by mbed-client
maygup01 0:11cc2b7889af 208 * while performing secure handshake.
maygup01 0:11cc2b7889af 209 * Function signature , if using mbed-client-mbedtls should be
maygup01 0:11cc2b7889af 210 * int (*mbedtls_entropy_f_source_ptr)(void *data, unsigned char *output,
maygup01 0:11cc2b7889af 211 * size_t len, size_t *olen);
maygup01 0:11cc2b7889af 212 */
maygup01 0:11cc2b7889af 213 virtual void set_entropy_callback(entropy_cb callback);
maygup01 0:11cc2b7889af 214
maygup01 0:11cc2b7889af 215 /**
maygup01 0:11cc2b7889af 216 * \brief Removes an object from M2MInterfaceImpl.
maygup01 0:11cc2b7889af 217 * Does not call delete on the object though.
maygup01 0:11cc2b7889af 218 * \return true if the object was found and false if the object was not found.
maygup01 0:11cc2b7889af 219 */
maygup01 0:11cc2b7889af 220 virtual bool remove_object(M2MBase *object);
maygup01 0:11cc2b7889af 221
maygup01 0:11cc2b7889af 222 /**
maygup01 0:11cc2b7889af 223 * @brief Updates the endpoint name.
maygup01 0:11cc2b7889af 224 * @param name New endpoint name
maygup01 0:11cc2b7889af 225 */
maygup01 0:11cc2b7889af 226 virtual void update_endpoint(const String &name);
maygup01 0:11cc2b7889af 227
maygup01 0:11cc2b7889af 228 /**
maygup01 0:11cc2b7889af 229 * @brief Updates the domain name.
maygup01 0:11cc2b7889af 230 * @param domain New domain name
maygup01 0:11cc2b7889af 231 */
maygup01 0:11cc2b7889af 232 virtual void update_domain(const String &domain);
maygup01 0:11cc2b7889af 233
maygup01 0:11cc2b7889af 234 /**
maygup01 0:11cc2b7889af 235 * @brief Return internal endpoint name
maygup01 0:11cc2b7889af 236 * @return internal endpoint name
maygup01 0:11cc2b7889af 237 */
maygup01 0:11cc2b7889af 238 virtual const String internal_endpoint_name() const;
maygup01 0:11cc2b7889af 239
maygup01 0:11cc2b7889af 240 /**
maygup01 0:11cc2b7889af 241 * @brief Return error description for the latest error code
maygup01 0:11cc2b7889af 242 * @return Error description string
maygup01 0:11cc2b7889af 243 */
maygup01 0:11cc2b7889af 244 virtual const char *error_description() const;
maygup01 0:11cc2b7889af 245
maygup01 0:11cc2b7889af 246 /**
maygup01 0:11cc2b7889af 247 * @brief Sends the CoAP GET request to the server.
maygup01 0:11cc2b7889af 248 * @type Download type.
maygup01 0:11cc2b7889af 249 * @uri Uri path to the data.
maygup01 0:11cc2b7889af 250 * @offset Data offset.
maygup01 0:11cc2b7889af 251 * @async In async mode application must call this API again with the updated offset.
maygup01 0:11cc2b7889af 252 * If set to false then client will automatically download the whole package.
maygup01 0:11cc2b7889af 253 * @get_data_cb Callback which is triggered once there is data available.
maygup01 0:11cc2b7889af 254 * @get_data_error_cb Callback which is trigged in case of any error.
maygup01 0:11cc2b7889af 255 */
maygup01 0:11cc2b7889af 256 virtual void get_data_request(DownloadType type,
maygup01 0:11cc2b7889af 257 const char *uri,
maygup01 0:11cc2b7889af 258 const size_t offset,
maygup01 0:11cc2b7889af 259 const bool async,
maygup01 0:11cc2b7889af 260 get_data_cb data_cb,
maygup01 0:11cc2b7889af 261 get_data_error_cb error_cb,
maygup01 0:11cc2b7889af 262 void *context);
maygup01 0:11cc2b7889af 263
maygup01 0:11cc2b7889af 264 /**
maygup01 0:11cc2b7889af 265 * @brief Sends the CoAP POST request to the server.
maygup01 0:11cc2b7889af 266 * @uri Uri path to the data.
maygup01 0:11cc2b7889af 267 * @async In async mode application must call this API again with the updated offset.
maygup01 0:11cc2b7889af 268 * If set to false then client will automatically download the whole package.
maygup01 0:11cc2b7889af 269 * @payload_len Length of payload.
maygup01 0:11cc2b7889af 270 * @payload_ptr, Pointer to payload buffer.
maygup01 0:11cc2b7889af 271 * @get_data_cb Callback which is triggered once there is data available.
maygup01 0:11cc2b7889af 272 * @get_data_error_cb Callback which is trigged in case of any error.
maygup01 0:11cc2b7889af 273 */
maygup01 0:11cc2b7889af 274 virtual void post_data_request(const char *uri,
maygup01 0:11cc2b7889af 275 const bool async,
maygup01 0:11cc2b7889af 276 const uint16_t payload_len,
maygup01 0:11cc2b7889af 277 uint8_t *payload_ptr,
maygup01 0:11cc2b7889af 278 get_data_cb data_cb,
maygup01 0:11cc2b7889af 279 get_data_error_cb error_cb,
maygup01 0:11cc2b7889af 280 void *context);
maygup01 0:11cc2b7889af 281
maygup01 0:11cc2b7889af 282 /**
maygup01 0:11cc2b7889af 283 * @brief Set custom uri query paramaters used in LWM2M registration.
maygup01 0:11cc2b7889af 284 * @uri_query_params Uri query params. Parameters must be in key-value format:
maygup01 0:11cc2b7889af 285 * "a=100&b=200". Maximum length can be up to 64 bytes.
maygup01 0:11cc2b7889af 286 * @return False if maximum length exceeded otherwise True.
maygup01 0:11cc2b7889af 287 */
maygup01 0:11cc2b7889af 288 virtual bool set_uri_query_parameters(const char *uri_query_params);
maygup01 0:11cc2b7889af 289
maygup01 0:11cc2b7889af 290 /**
maygup01 0:11cc2b7889af 291 * \brief Pauses client's timed functionality and closes network connection
maygup01 0:11cc2b7889af 292 * to the Cloud. After successful call the operation is continued
maygup01 0:11cc2b7889af 293 * by calling resume().
maygup01 0:11cc2b7889af 294 *
maygup01 0:11cc2b7889af 295 * \note This operation does not unregister client from the Cloud.
maygup01 0:11cc2b7889af 296 * Closes the socket and removes interface from the interface list.
maygup01 0:11cc2b7889af 297 */
maygup01 0:11cc2b7889af 298 virtual void pause();
maygup01 0:11cc2b7889af 299
maygup01 0:11cc2b7889af 300 /**
maygup01 0:11cc2b7889af 301 * \brief Resumes client's timed functionality and network connection
maygup01 0:11cc2b7889af 302 * to the Cloud. Updates registration. Can be only called after
maygup01 0:11cc2b7889af 303 * a successful call to pause().
maygup01 0:11cc2b7889af 304 *
maygup01 0:11cc2b7889af 305 * \param iface A handler to the network interface.
maygup01 0:11cc2b7889af 306 */
maygup01 0:11cc2b7889af 307 virtual void resume(void *iface, const M2MBaseList &list);
maygup01 0:11cc2b7889af 308
maygup01 0:11cc2b7889af 309 protected: // From M2MNsdlObserver
maygup01 0:11cc2b7889af 310
maygup01 0:11cc2b7889af 311 virtual void coap_message_ready(uint8_t *data_ptr,
maygup01 0:11cc2b7889af 312 uint16_t data_len,
maygup01 0:11cc2b7889af 313 sn_nsdl_addr_s *address_ptr);
maygup01 0:11cc2b7889af 314
maygup01 0:11cc2b7889af 315 virtual void client_registered(M2MServer *server_object);
maygup01 0:11cc2b7889af 316
maygup01 0:11cc2b7889af 317 virtual void registration_updated(const M2MServer &server_object);
maygup01 0:11cc2b7889af 318
maygup01 0:11cc2b7889af 319 virtual void registration_error(uint8_t error_code, bool retry = false, bool full_registration = false);
maygup01 0:11cc2b7889af 320
maygup01 0:11cc2b7889af 321 virtual void client_unregistered();
maygup01 0:11cc2b7889af 322
maygup01 0:11cc2b7889af 323 virtual void bootstrap_done();
maygup01 0:11cc2b7889af 324
maygup01 0:11cc2b7889af 325 virtual void bootstrap_finish();
maygup01 0:11cc2b7889af 326
maygup01 0:11cc2b7889af 327 virtual void bootstrap_wait();
maygup01 0:11cc2b7889af 328
maygup01 0:11cc2b7889af 329 virtual void bootstrap_error_wait(const char *reason);
maygup01 0:11cc2b7889af 330
maygup01 0:11cc2b7889af 331 virtual void bootstrap_error(const char *reason);
maygup01 0:11cc2b7889af 332
maygup01 0:11cc2b7889af 333 virtual void coap_data_processed();
maygup01 0:11cc2b7889af 334
maygup01 0:11cc2b7889af 335 virtual void value_updated(M2MBase *base);
maygup01 0:11cc2b7889af 336
maygup01 0:11cc2b7889af 337 protected: // From M2MConnectionObserver
maygup01 0:11cc2b7889af 338
maygup01 0:11cc2b7889af 339 virtual void data_available(uint8_t* data,
maygup01 0:11cc2b7889af 340 uint16_t data_size,
maygup01 0:11cc2b7889af 341 const M2MConnectionObserver::SocketAddress &address);
maygup01 0:11cc2b7889af 342
maygup01 0:11cc2b7889af 343 virtual void socket_error(int error_code, bool retry = true);
maygup01 0:11cc2b7889af 344
maygup01 0:11cc2b7889af 345 virtual void address_ready(const M2MConnectionObserver::SocketAddress &address,
maygup01 0:11cc2b7889af 346 M2MConnectionObserver::ServerType server_type,
maygup01 0:11cc2b7889af 347 const uint16_t server_port);
maygup01 0:11cc2b7889af 348
maygup01 0:11cc2b7889af 349 virtual void data_sent();
maygup01 0:11cc2b7889af 350
maygup01 0:11cc2b7889af 351 protected: // from M2MTimerObserver
maygup01 0:11cc2b7889af 352
maygup01 0:11cc2b7889af 353 virtual void timer_expired(M2MTimerObserver::Type type);
maygup01 0:11cc2b7889af 354
maygup01 0:11cc2b7889af 355
maygup01 0:11cc2b7889af 356 private: // state machine state functions
maygup01 0:11cc2b7889af 357
maygup01 0:11cc2b7889af 358 /**
maygup01 0:11cc2b7889af 359 * When the state is Idle.
maygup01 0:11cc2b7889af 360 */
maygup01 0:11cc2b7889af 361 void state_idle(EventData* data);
maygup01 0:11cc2b7889af 362
maygup01 0:11cc2b7889af 363 /**
maygup01 0:11cc2b7889af 364 * When the client starts bootstrap.
maygup01 0:11cc2b7889af 365 */
maygup01 0:11cc2b7889af 366 void state_bootstrap( EventData *data);
maygup01 0:11cc2b7889af 367
maygup01 0:11cc2b7889af 368 /**
maygup01 0:11cc2b7889af 369 * When the bootstrap server address is resolved.
maygup01 0:11cc2b7889af 370 */
maygup01 0:11cc2b7889af 371 void state_bootstrap_address_resolved( EventData *data);
maygup01 0:11cc2b7889af 372
maygup01 0:11cc2b7889af 373 /**
maygup01 0:11cc2b7889af 374 * When the bootstrap resource is created.
maygup01 0:11cc2b7889af 375 */
maygup01 0:11cc2b7889af 376 void state_bootstrap_resource_created( EventData *data);
maygup01 0:11cc2b7889af 377
maygup01 0:11cc2b7889af 378 /**
maygup01 0:11cc2b7889af 379 * When the server has sent response and bootstrapping is done.
maygup01 0:11cc2b7889af 380 */
maygup01 0:11cc2b7889af 381 void state_bootstrapped( EventData *data);
maygup01 0:11cc2b7889af 382
maygup01 0:11cc2b7889af 383 /**
maygup01 0:11cc2b7889af 384 * When the client starts register.
maygup01 0:11cc2b7889af 385 */
maygup01 0:11cc2b7889af 386 void state_register( EventData *data);
maygup01 0:11cc2b7889af 387
maygup01 0:11cc2b7889af 388 /**
maygup01 0:11cc2b7889af 389 * When the server address for register is resolved.
maygup01 0:11cc2b7889af 390 */
maygup01 0:11cc2b7889af 391 void state_register_address_resolved( EventData *data);
maygup01 0:11cc2b7889af 392
maygup01 0:11cc2b7889af 393 /**
maygup01 0:11cc2b7889af 394 * When the client is registered.
maygup01 0:11cc2b7889af 395 */
maygup01 0:11cc2b7889af 396 void state_registered( EventData *data);
maygup01 0:11cc2b7889af 397
maygup01 0:11cc2b7889af 398 /**
maygup01 0:11cc2b7889af 399 * When the client is updating registration.
maygup01 0:11cc2b7889af 400 */
maygup01 0:11cc2b7889af 401 void state_update_registration( EventData *data);
maygup01 0:11cc2b7889af 402
maygup01 0:11cc2b7889af 403 /**
maygup01 0:11cc2b7889af 404 * When the client starts unregister.
maygup01 0:11cc2b7889af 405 */
maygup01 0:11cc2b7889af 406 void state_unregister( EventData *data);
maygup01 0:11cc2b7889af 407
maygup01 0:11cc2b7889af 408 /**
maygup01 0:11cc2b7889af 409 * When the client has been unregistered.
maygup01 0:11cc2b7889af 410 */
maygup01 0:11cc2b7889af 411 void state_unregistered( EventData *data);
maygup01 0:11cc2b7889af 412
maygup01 0:11cc2b7889af 413 /**
maygup01 0:11cc2b7889af 414 * When the coap data is been sent through socket.
maygup01 0:11cc2b7889af 415 */
maygup01 0:11cc2b7889af 416 void state_sending_coap_data( EventData *data);
maygup01 0:11cc2b7889af 417
maygup01 0:11cc2b7889af 418 /**
maygup01 0:11cc2b7889af 419 * When the coap data is sent successfully.
maygup01 0:11cc2b7889af 420 */
maygup01 0:11cc2b7889af 421 void state_coap_data_sent( EventData *data);
maygup01 0:11cc2b7889af 422
maygup01 0:11cc2b7889af 423 /**
maygup01 0:11cc2b7889af 424 * When the socket is receiving coap data.
maygup01 0:11cc2b7889af 425 */
maygup01 0:11cc2b7889af 426 void state_receiving_coap_data( EventData *data);
maygup01 0:11cc2b7889af 427
maygup01 0:11cc2b7889af 428 /**
maygup01 0:11cc2b7889af 429 * When the socket has received coap data.
maygup01 0:11cc2b7889af 430 */
maygup01 0:11cc2b7889af 431 void state_coap_data_received( EventData *data);
maygup01 0:11cc2b7889af 432
maygup01 0:11cc2b7889af 433 /**
maygup01 0:11cc2b7889af 434 * When the coap message is being processed.
maygup01 0:11cc2b7889af 435 */
maygup01 0:11cc2b7889af 436 void state_processing_coap_data( EventData *data);
maygup01 0:11cc2b7889af 437
maygup01 0:11cc2b7889af 438 /**
maygup01 0:11cc2b7889af 439 * When the coap message has been processed.
maygup01 0:11cc2b7889af 440 */
maygup01 0:11cc2b7889af 441 void state_coap_data_processed( EventData *data);
maygup01 0:11cc2b7889af 442
maygup01 0:11cc2b7889af 443 /**
maygup01 0:11cc2b7889af 444 * When the client is waiting to receive or send data.
maygup01 0:11cc2b7889af 445 */
maygup01 0:11cc2b7889af 446 void state_waiting( EventData *data);
maygup01 0:11cc2b7889af 447
maygup01 0:11cc2b7889af 448 /**
maygup01 0:11cc2b7889af 449 * Start registration update.
maygup01 0:11cc2b7889af 450 */
maygup01 0:11cc2b7889af 451 void start_register_update(M2MUpdateRegisterData *data);
maygup01 0:11cc2b7889af 452
maygup01 0:11cc2b7889af 453 /**
maygup01 0:11cc2b7889af 454 * State enumeration order must match the order of state
maygup01 0:11cc2b7889af 455 * method entries in the state map
maygup01 0:11cc2b7889af 456 */
maygup01 0:11cc2b7889af 457 enum E_States {
maygup01 0:11cc2b7889af 458 STATE_IDLE = 0,
maygup01 0:11cc2b7889af 459 STATE_BOOTSTRAP,
maygup01 0:11cc2b7889af 460 STATE_BOOTSTRAP_ADDRESS_RESOLVED,
maygup01 0:11cc2b7889af 461 STATE_BOOTSTRAP_RESOURCE_CREATED,
maygup01 0:11cc2b7889af 462 STATE_BOOTSTRAP_WAIT,
maygup01 0:11cc2b7889af 463 STATE_BOOTSTRAP_ERROR_WAIT, // 5
maygup01 0:11cc2b7889af 464 STATE_BOOTSTRAPPED,
maygup01 0:11cc2b7889af 465 STATE_REGISTER,
maygup01 0:11cc2b7889af 466 STATE_REGISTER_ADDRESS_RESOLVED,
maygup01 0:11cc2b7889af 467 STATE_REGISTERED,
maygup01 0:11cc2b7889af 468 STATE_UPDATE_REGISTRATION, // 10
maygup01 0:11cc2b7889af 469 STATE_UNREGISTER,
maygup01 0:11cc2b7889af 470 STATE_UNREGISTERED,
maygup01 0:11cc2b7889af 471 STATE_SENDING_COAP_DATA,
maygup01 0:11cc2b7889af 472 STATE_COAP_DATA_SENT,
maygup01 0:11cc2b7889af 473 STATE_COAP_DATA_RECEIVED, // 15
maygup01 0:11cc2b7889af 474 STATE_PROCESSING_COAP_DATA,
maygup01 0:11cc2b7889af 475 STATE_COAP_DATA_PROCESSED,
maygup01 0:11cc2b7889af 476 STATE_WAITING,
maygup01 0:11cc2b7889af 477 STATE_MAX_STATES
maygup01 0:11cc2b7889af 478 };
maygup01 0:11cc2b7889af 479
maygup01 0:11cc2b7889af 480 /**
maygup01 0:11cc2b7889af 481 * @brief Redirects the state machine to right function.
maygup01 0:11cc2b7889af 482 * @param current_state Current state to be set.
maygup01 0:11cc2b7889af 483 * @param data Data to be passed to the state function.
maygup01 0:11cc2b7889af 484 */
maygup01 0:11cc2b7889af 485 void state_function( uint8_t current_state, EventData* data );
maygup01 0:11cc2b7889af 486
maygup01 0:11cc2b7889af 487 /**
maygup01 0:11cc2b7889af 488 * @brief State Engine maintaining state machine logic.
maygup01 0:11cc2b7889af 489 */
maygup01 0:11cc2b7889af 490 void state_engine(void);
maygup01 0:11cc2b7889af 491
maygup01 0:11cc2b7889af 492 /**
maygup01 0:11cc2b7889af 493 * External event which can trigger the state machine.
maygup01 0:11cc2b7889af 494 * @param New The state to which the state machine should go.
maygup01 0:11cc2b7889af 495 * @param data The data to be passed to the state machine.
maygup01 0:11cc2b7889af 496 */
maygup01 0:11cc2b7889af 497 void external_event(uint8_t, EventData* = NULL);
maygup01 0:11cc2b7889af 498
maygup01 0:11cc2b7889af 499 /**
maygup01 0:11cc2b7889af 500 * Internal event generated by state machine.
maygup01 0:11cc2b7889af 501 * @param New State which the state machine should go to.
maygup01 0:11cc2b7889af 502 * @param data The data to be passed to the state machine.
maygup01 0:11cc2b7889af 503 */
maygup01 0:11cc2b7889af 504 void internal_event(uint8_t, EventData* = NULL);
maygup01 0:11cc2b7889af 505
maygup01 0:11cc2b7889af 506 /**
maygup01 0:11cc2b7889af 507 * Queue mode enabled or not.
maygup01 0:11cc2b7889af 508 * @return True if queue mode otherwise false.
maygup01 0:11cc2b7889af 509 */
maygup01 0:11cc2b7889af 510 bool queue_mode() const;
maygup01 0:11cc2b7889af 511
maygup01 0:11cc2b7889af 512 enum
maygup01 0:11cc2b7889af 513 {
maygup01 0:11cc2b7889af 514 EVENT_IGNORED = 0xFE,
maygup01 0:11cc2b7889af 515 CANNOT_HAPPEN
maygup01 0:11cc2b7889af 516 };
maygup01 0:11cc2b7889af 517
maygup01 0:11cc2b7889af 518 /**
maygup01 0:11cc2b7889af 519 * Helper method for extracting the IP address part and port from the
maygup01 0:11cc2b7889af 520 * given server address.
maygup01 0:11cc2b7889af 521 * @param server_address Source URL (without "coap" or "coaps" prefix).
maygup01 0:11cc2b7889af 522 * @param ip_address The extracted IP.
maygup01 0:11cc2b7889af 523 * @param port The extracted port.
maygup01 0:11cc2b7889af 524 */
maygup01 0:11cc2b7889af 525 static void process_address(const String& server_address, String& ip_address, uint16_t& port);
maygup01 0:11cc2b7889af 526
maygup01 0:11cc2b7889af 527 /**
maygup01 0:11cc2b7889af 528 * Helper method for storing the error description to _error_description if the feature
maygup01 0:11cc2b7889af 529 * has not been turned off.
maygup01 0:11cc2b7889af 530 * @param error description
maygup01 0:11cc2b7889af 531 */
maygup01 0:11cc2b7889af 532 void set_error_description(const char *description);
maygup01 0:11cc2b7889af 533
maygup01 0:11cc2b7889af 534 enum ReconnectionState{
maygup01 0:11cc2b7889af 535 None,
maygup01 0:11cc2b7889af 536 WithUpdate,
maygup01 0:11cc2b7889af 537 Unregistration
maygup01 0:11cc2b7889af 538 };
maygup01 0:11cc2b7889af 539
maygup01 0:11cc2b7889af 540 private:
maygup01 0:11cc2b7889af 541
maygup01 0:11cc2b7889af 542 EventData *_event_data;
maygup01 0:11cc2b7889af 543 M2MTimer *_registration_flow_timer;
maygup01 0:11cc2b7889af 544 uint16_t _server_port;
maygup01 0:11cc2b7889af 545 uint16_t _listen_port;
maygup01 0:11cc2b7889af 546 int32_t _life_time;
maygup01 0:11cc2b7889af 547 String _server_ip_address;
maygup01 0:11cc2b7889af 548 M2MSecurity *_register_server; //TODO: to be the list not owned
maygup01 0:11cc2b7889af 549 M2MTimer _queue_sleep_timer;
maygup01 0:11cc2b7889af 550 M2MTimer _retry_timer;
maygup01 0:11cc2b7889af 551 callback_handler _callback_handler;
maygup01 0:11cc2b7889af 552 const uint8_t _max_states;
maygup01 0:11cc2b7889af 553 bool _event_ignored;
maygup01 0:11cc2b7889af 554 bool _event_generated;
maygup01 0:11cc2b7889af 555 bool _reconnecting;
maygup01 0:11cc2b7889af 556 bool _retry_timer_expired;
maygup01 0:11cc2b7889af 557 bool _bootstrapped;
maygup01 0:11cc2b7889af 558 bool _bootstrap_finished;
maygup01 0:11cc2b7889af 559 bool _queue_mode_timer_ongoing;
maygup01 0:11cc2b7889af 560 uint8_t _current_state;
maygup01 0:11cc2b7889af 561 BindingMode _binding_mode;
maygup01 0:11cc2b7889af 562 ReconnectionState _reconnection_state;
maygup01 0:11cc2b7889af 563 M2MInterfaceObserver &_observer;
maygup01 0:11cc2b7889af 564 M2MConnectionSecurity *_security_connection; // Doesn't own
maygup01 0:11cc2b7889af 565 M2MConnectionHandler _connection_handler;
maygup01 0:11cc2b7889af 566 M2MNsdlInterface _nsdl_interface;
maygup01 0:11cc2b7889af 567 M2MSecurity *_security;
maygup01 0:11cc2b7889af 568
maygup01 0:11cc2b7889af 569 #ifndef DISABLE_ERROR_DESCRIPTION
maygup01 0:11cc2b7889af 570 // The DISABLE_ERROR_DESCRIPTION macro will reduce the flash usage by ~1800 bytes.
maygup01 0:11cc2b7889af 571 char _error_description[MAX_ALLOWED_ERROR_STRING_LENGTH];
maygup01 0:11cc2b7889af 572 #endif
maygup01 0:11cc2b7889af 573
maygup01 0:11cc2b7889af 574 uint8_t _initial_reconnection_time;
maygup01 0:11cc2b7889af 575 uint64_t _reconnection_time;
maygup01 0:11cc2b7889af 576
maygup01 0:11cc2b7889af 577 friend class Test_M2MInterfaceImpl;
maygup01 0:11cc2b7889af 578
maygup01 0:11cc2b7889af 579 };
maygup01 0:11cc2b7889af 580
maygup01 0:11cc2b7889af 581 #define BEGIN_TRANSITION_MAP \
maygup01 0:11cc2b7889af 582 static const uint8_t TRANSITIONS[] = {\
maygup01 0:11cc2b7889af 583
maygup01 0:11cc2b7889af 584 #define TRANSITION_MAP_ENTRY(entry)\
maygup01 0:11cc2b7889af 585 entry,
maygup01 0:11cc2b7889af 586
maygup01 0:11cc2b7889af 587 #define END_TRANSITION_MAP(data) \
maygup01 0:11cc2b7889af 588 0 };\
maygup01 0:11cc2b7889af 589 external_event(TRANSITIONS[_current_state], data);
maygup01 0:11cc2b7889af 590
maygup01 0:11cc2b7889af 591 #endif //M2M_INTERFACE_IMPL_H
maygup01 0:11cc2b7889af 592
maygup01 0:11cc2b7889af 593