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
m2mconnectionhandlerpimpl.h
00001 /* 00002 * Copyright (c) 2015 ARM Limited. All rights reserved. 00003 * SPDX-License-Identifier: Apache-2.0 00004 * Licensed under the Apache License, Version 2.0 (the License); you may 00005 * not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an AS IS BASIS, WITHOUT 00012 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 #ifndef M2M_CONNECTION_HANDLER_PIMPL_H__ 00017 #define M2M_CONNECTION_HANDLER_PIMPL_H__ 00018 00019 #include "ns_types.h" 00020 #include "ns_list.h" 00021 #include "eventOS_event.h" 00022 #include "mbed-client/m2mconfig.h" 00023 #include "mbed-client/m2mconstants.h" 00024 #include "mbed-client/m2minterface.h" 00025 #include "mbed-client/m2mconnectionobserver.h" 00026 #include "mbed-client/m2mconnectionsecurity.h" 00027 #include "nsdl-c/sn_nsdl.h" 00028 #include "pal.h" 00029 00030 00031 class M2MConnectionSecurity; 00032 class M2MConnectionHandler; 00033 class M2MSecurity; 00034 00035 /** 00036 * @brief M2MConnectionHandlerPimpl. 00037 * This class handles the socket connection for LWM2M Client 00038 */ 00039 00040 00041 class M2MConnectionHandlerPimpl { 00042 public: 00043 00044 enum SocketEvent { 00045 ESocketIdle = 0x00, 00046 ESocketCallback = 0x02, 00047 ESocketConnect = 0x04, 00048 ESocketSend = 0x08, 00049 ESocketDnsResolved = 0x10, 00050 ESocketDnsError = 0x20, 00051 ESocketClose = 0x40, 00052 ESocketTimerCallback = 0x80 00053 }; 00054 00055 /** 00056 * @brief Constructor 00057 */ 00058 M2MConnectionHandlerPimpl(M2MConnectionHandler* base, M2MConnectionObserver &observer, 00059 M2MConnectionSecurity* sec, 00060 M2MInterface::BindingMode mode, 00061 M2MInterface::NetworkStack /*stack*/); 00062 00063 /** 00064 * @brief Destructor 00065 */ 00066 ~M2MConnectionHandlerPimpl(); 00067 00068 void start_timer(void); 00069 00070 /** 00071 * @brief This binds the socket connection. 00072 * @param listen_port Port to listen for incoming connection. 00073 * @return true if successful else false. 00074 */ 00075 bool bind_connection(const uint16_t listen_port); 00076 00077 /** 00078 * @brief This resolves the server address. Output is 00079 * returned through callback 00080 * @param String server address. 00081 * @param uint16_t Server port. 00082 * @param ServerType, Server Type to be resolved. 00083 * @return true if address is valid else false. 00084 */ 00085 bool resolve_server_address(const String& server_address, 00086 const uint16_t server_port, 00087 M2MConnectionObserver::ServerType server_type, 00088 const M2MSecurity* security); 00089 00090 /** 00091 * @brief Sends data, to the connected sent to server. 00092 * @param data, Data to be sent. 00093 */ 00094 bool send_data(uint8_t *data_ptr, 00095 uint16_t data_len, 00096 sn_nsdl_addr_s *address_ptr); 00097 00098 /** 00099 * @brief Listens for incoming data from remote server 00100 * @return true if successful else false. 00101 */ 00102 bool start_listening_for_data(); 00103 00104 /** 00105 * @brief Stops listening for incoming data 00106 */ 00107 void stop_listening(); 00108 00109 /** 00110 * @brief Error handling for DTLS connectivity. 00111 * @param error, Error code from TLS library 00112 */ 00113 void handle_connection_error(int error); 00114 00115 /** 00116 * \brief Sets the network interface handler that is used by client to connect 00117 * to a network over IP.. 00118 * \param handler A network interface handler that is used by client to connect. 00119 * This API is optional but provides a mechanism for different platforms to 00120 * manage usage of underlying network interface by client. 00121 */ 00122 void set_platform_network_handler(void *handler = NULL); 00123 00124 /** 00125 * \brief Claims mutex to prevent thread clashes 00126 * in multithreaded environment. 00127 */ 00128 void claim_mutex(); 00129 00130 /** 00131 * \brief Releases mutex to prevent thread clashes 00132 * in multithreaded environment. 00133 */ 00134 void release_mutex(); 00135 00136 /** 00137 * @brief Callback handler for receiving data over socket. 00138 */ 00139 void receive_handler(); 00140 00141 /** 00142 * @brief Returns true if DTLS handshake is still ongoing. 00143 */ 00144 bool is_handshake_ongoing() const; 00145 00146 /** 00147 * @brief Sends data to socket through event loop. 00148 */ 00149 void send_socket_data(); 00150 00151 /** 00152 * @brief This function is used for generating socket events. 00153 */ 00154 void send_socket_event(SocketEvent event_type); 00155 00156 /** 00157 * @brief Does DNS resolving. Return true if DNS has been resolved 00158 * or triggered though DNS thread. 00159 */ 00160 bool address_resolver(void); 00161 00162 /** 00163 * @brief handler for eventloop events. Note, this needs to be public as it is called 00164 * from C wrapper. 00165 */ 00166 void event_handler(arm_event_s *event); 00167 00168 /** 00169 * \brief Closes the open connection. 00170 * \note This must be called from the same event loop context! 00171 */ 00172 void force_close(); 00173 00174 /** 00175 * \brief Unregisters the network interface handler that is set in 'set_platform_network_handler'. 00176 */ 00177 void unregister_network_handler(); 00178 00179 private: 00180 00181 /** 00182 * @brief Terminate the DNS thread, if any is used. The #ifdef magic is inside this 00183 * method, so it can be used without ifdef guards. 00184 */ 00185 void terminate_dns_thread(); 00186 00187 /** 00188 * @brief Handles socket initialization and connect phase. 00189 */ 00190 void socket_connect_handler(); 00191 00192 /** 00193 * @brief Callback handler for receiving data for secured connection. 00194 */ 00195 void receive_handshake_handler(); 00196 00197 /** 00198 * @brief Callback handler for socket events. 00199 */ 00200 void socket_event(); 00201 00202 /** 00203 * @brief Handles the result set by address_resolver. 00204 */ 00205 void handle_dns_result(bool success); 00206 00207 00208 /** 00209 * @brief Initialize socket 00210 */ 00211 bool init_socket(); 00212 00213 /** 00214 * @brief Check socket type 00215 * @return True if TCP connection otherwise false 00216 */ 00217 bool is_tcp_connection() const; 00218 00219 /** 00220 * @brief Close and delete socket 00221 */ 00222 void close_socket(); 00223 00224 public: 00225 00226 /** 00227 * @brief Internal helper for sending an event. 00228 */ 00229 bool send_event(SocketEvent event_type); 00230 00231 private: 00232 typedef struct send_data_queue { 00233 uint8_t *data; 00234 uint16_t offset; 00235 uint16_t data_len; 00236 ns_list_link_t link; 00237 } send_data_queue_s; 00238 00239 /** 00240 * @brief Get first item from the queue list. 00241 */ 00242 send_data_queue_s* get_item_from_list(); 00243 00244 /** 00245 * @brief Add queue data back to list. 00246 */ 00247 void add_item_to_list(send_data_queue_s* data); 00248 00249 private: 00250 enum SocketState { 00251 00252 /* NOTE: Order of these values does matter, do not make unplanned changes to it. */ 00253 00254 /** Socket has not been initialized/connected yet. */ 00255 ESocketStateDisconnected, 00256 00257 /** pal_close() is in progress. */ 00258 ESocketStateCloseBeingCalled, 00259 00260 /** DNS resolving in progress. */ 00261 ESocketStateDNSResolving, 00262 00263 /** DNS resolving done, connection needs to be initialized. */ 00264 EsocketStateInitializeConnection, 00265 00266 /** pal_connect() is in progress. */ 00267 ESocketStateConnectBeingCalled, 00268 00269 /** pal_connect() has been called and we are waiting for asynchronous response. */ 00270 ESocketStateConnecting, 00271 00272 /** pal_connect is complete and the (D)TLS handshake is to be done if in secure mode. */ 00273 ESocketStateConnected, 00274 00275 /** (D)TLS Handshaking in progress */ 00276 ESocketStateHandshaking, 00277 00278 /** Unsecure Connection to the server has been established */ 00279 ESocketStateUnsecureConnection, 00280 00281 /** Secure Connection to the server has been established */ 00282 ESocketStateSecureConnection 00283 }; 00284 00285 typedef NS_LIST_HEAD(send_data_queue_s, link) send_data_list_t; 00286 00287 M2MConnectionHandler *_base; 00288 M2MConnectionObserver &_observer; 00289 M2MConnectionSecurity *_security_impl; //owned 00290 const M2MSecurity *_security; //non-owned 00291 M2MInterface::BindingMode _binding_mode; 00292 M2MConnectionObserver::SocketAddress _address; 00293 00294 // _address._address will point to one of these two 00295 palIpV4Addr_t _ipV4Addr; 00296 palIpV6Addr_t _ipV6Addr; 00297 00298 palSocket_t _socket; 00299 M2MConnectionObserver::ServerType _server_type; 00300 uint16_t _server_port; 00301 uint16_t _listen_port; 00302 uint32_t _net_iface; 00303 #if (PAL_DNS_API_VERSION == 0) || (PAL_DNS_API_VERSION == 1) 00304 palSocketLength_t _socket_address_len; 00305 #else 00306 palDNSQuery_t _handler_async_DNS; 00307 #endif 00308 volatile palSocketAddress_t _socket_address; 00309 static int8_t _tasklet_id; 00310 String _server_address; 00311 00312 // A state variable for the socket itself, which is needed to handle the 00313 // asynchronous events and callbacks. Note: the state may be accessed from 00314 // event sender and receiver threads. 00315 SocketState _socket_state; 00316 uint8_t _handshake_retry; 00317 00318 /** 00319 * This is a flag which is set before sending a socket callback event 00320 * and cleared when the event handler side is started. It is meant to get rid of 00321 * event storm which happens whenever one socket transfers data and spurious 00322 * events are sent to all the other sockets. 00323 */ 00324 volatile bool _suppressable_event_in_flight; 00325 00326 send_data_list_t _linked_list_send_data; 00327 00328 bool _secure_connection; 00329 00330 friend class Test_M2MConnectionHandlerPimpl; 00331 friend class Test_M2MConnectionHandlerPimpl_mbed; 00332 friend class Test_M2MConnectionHandlerPimpl_classic; 00333 friend class M2MConnection_TestObserver; 00334 }; 00335 00336 #endif //M2M_CONNECTION_HANDLER_PIMPL_H__
Generated on Tue Jul 12 2022 20:21:00 by
