Ram Gandikota / Mbed OS ABCD
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers m2mconnectionhandlerpimpl.h Source File

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 "mbed-client/m2mconfig.h"
00021 #include "mbed-client/m2mconstants.h"
00022 #include "mbed-client/m2minterface.h"
00023 #include "mbed-client/m2mconnectionobserver.h"
00024 #include "mbed-client/m2mconnectionsecurity.h"
00025 #include "nsdl-c/sn_nsdl.h"
00026 
00027 #include "pal_network.h"
00028 
00029 
00030 class M2MConnectionSecurity;
00031 class M2MConnectionHandler;
00032 class M2MSecurity;
00033 
00034 /**
00035  * @brief M2MConnectionHandlerPimpl.
00036  * This class handles the socket connection for LWM2M Client
00037  */
00038 
00039 
00040 class M2MConnectionHandlerPimpl {
00041 public:
00042 
00043     enum SocketEvent {
00044         ESocketIdle         = 0x00,
00045         ESocketReadytoRead  = 0x02,
00046         ESocketDnsHandler   = 0x04,
00047         ESocketSend         = 0x08
00048     };
00049 
00050     struct TaskIdentifier {
00051         M2MConnectionHandlerPimpl *pimpl;
00052         void                      *data_ptr;
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 send_to_socket Sends directly to socket. This is used by
00111      * security classes to send after data has been encrypted.
00112      * @param buf Buffer to send
00113      * @param len Length of a buffer
00114      * @return Number of bytes sent or -1 if failed
00115      */
00116     int send_to_socket(const unsigned char *buf, size_t len);
00117 
00118     /**
00119      * \brief Receives directly from the socket. This
00120      * is used by the security classes to receive raw data to be decrypted.
00121      * \param buf Buffer to send.
00122      * \param len The length of the buffer.
00123      * \param timeout Timeout defined from DTLS to wait for blocking receive calls
00124      * before timing out, by default value is 0.
00125      * \return Number of bytes read or negative number if failed.
00126      */
00127     int receive_from_socket(unsigned char *buf, size_t len);
00128 
00129     /**
00130     * @brief Error handling for DTLS connectivity.
00131     * @param error, Error code from TLS library
00132     */
00133     void handle_connection_error(int error);
00134 
00135     /**
00136      * \brief Sets the network interface handler that is used by client to connect
00137      * to a network over IP..
00138      * \param handler A network interface handler that is used by client to connect.
00139      *  This API is optional but provides a mechanism for different platforms to
00140      * manage usage of underlying network interface by client.
00141      */
00142     void set_platform_network_handler(void *handler = NULL);
00143 
00144     /**
00145     * \brief Claims mutex to prevent thread clashes
00146     * in multithreaded environment.
00147     */
00148     void claim_mutex();
00149 
00150     /**
00151     * \brief Releases mutex to prevent thread clashes
00152     * in multithreaded environment.
00153     */
00154     void release_mutex();
00155 
00156     /**
00157     * @brief Callback handler for sending data over socket.
00158     */
00159     void send_handler();
00160 
00161     /**
00162     * @brief Callback handler for receiving data over socket.
00163     */
00164     void receive_handler();
00165 
00166     /**
00167     * @brief Callback handler for receiving data for secured connection.
00168     */
00169     void receive_handshake_handler();
00170 
00171     /**
00172     * @brief Returns true if DTLS handshake is still ongoing.
00173     */
00174     bool is_handshake_ongoing();
00175 
00176     /**
00177     * @brief Returns connection handler tasklet ID.
00178     */
00179     int8_t connection_tasklet_handler();
00180 
00181     /**
00182     * @brief Handles DNS resolving through event loop.
00183     */
00184     void dns_handler();
00185 
00186     /**
00187     * @brief Sends data to socket through event loop.
00188     */
00189     void send_socket_data(uint8_t *data, uint16_t data_len);
00190     
00191     void send_receive_event(void);
00192 
00193 
00194 private:
00195 
00196     /**
00197     * @brief Callback handler for socket events.
00198     */
00199     void socket_event();
00200 
00201     /**
00202     * @brief Initialize mbed OS socket
00203     */
00204     bool init_socket();
00205 
00206     /**
00207     * @brief Check socket type
00208     * @return True if TCP connection otherwise false
00209     */
00210     bool is_tcp_connection();
00211 
00212     /**
00213     * @brief Close and delete socket
00214     */
00215     void close_socket();
00216 
00217     /**
00218     * @brief Enables keepalive for TCP connections.
00219     */
00220     void enable_keepalive();
00221 
00222 private:
00223     M2MConnectionHandler                        *_base;
00224     M2MConnectionObserver                       &_observer;
00225     M2MConnectionSecurity                       *_security_impl; //owned
00226     const M2MSecurity                           *_security; //non-owned
00227     bool                                        _use_secure_connection;
00228     M2MInterface::BindingMode                   _binding_mode;
00229     M2MInterface::NetworkStack                  _network_stack;
00230     M2MConnectionObserver::SocketAddress        _address;
00231 
00232     // _address._address will point to one of these two
00233     palIpV4Addr_t                               _ipV4Addr; 
00234     palIpV6Addr_t                               _ipV6Addr;
00235 
00236     palSocket_t                                 _socket;
00237     bool                                        _is_handshaking;
00238     bool                                        _listening;
00239     M2MConnectionObserver::ServerType           _server_type;
00240     uint16_t                                    _server_port;
00241     uint16_t                                    _listen_port;
00242     bool                                        _running;
00243     unsigned char                               _recv_buffer[BUFFER_LENGTH];
00244     uint32_t                                    _net_iface;
00245     palSocketAddress_t                          _socket_address;
00246     static int8_t                               _tasklet_id;
00247     String                                      _server_address;
00248 
00249 friend class Test_M2MConnectionHandlerPimpl;
00250 friend class Test_M2MConnectionHandlerPimpl_mbed;
00251 friend class Test_M2MConnectionHandlerPimpl_classic;
00252 friend class M2MConnection_TestObserver;
00253 };
00254 
00255 #endif //M2M_CONNECTION_HANDLER_PIMPL_H__