joey shelton / LED_Demo

Dependencies:   MAX44000 PWM_Tone_Library nexpaq_mdk

Fork of LED_Demo by Maxim nexpaq

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 "Socket.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     /**
00069     * @brief This binds the socket connection.
00070     * @param listen_port Port to listen for incoming connection.
00071     * @return true if successful else false.
00072     */
00073     bool bind_connection(const uint16_t listen_port);
00074 
00075     /**
00076     * @brief This resolves the server address. Output is
00077     * returned through callback
00078     * @param String server address.
00079     * @param uint16_t Server port.
00080     * @param ServerType, Server Type to be resolved.
00081     * @return true if address is valid else false.
00082     */
00083     bool resolve_server_address(const String& server_address,
00084                                 const uint16_t server_port,
00085                                 M2MConnectionObserver::ServerType server_type,
00086                                 const M2MSecurity* security);
00087 
00088     /**
00089     * @brief Sends data, to the connected sent to server.
00090     * @param data, Data to be sent.
00091     */
00092     bool send_data(uint8_t *data_ptr,
00093                    uint16_t data_len,
00094                    sn_nsdl_addr_s *address_ptr);
00095 
00096     /**
00097     * @brief Listens for incoming data from remote server
00098     * @return true if successful else false.
00099     */
00100     bool start_listening_for_data();
00101 
00102     /**
00103     * @brief Stops listening for incoming data
00104     */
00105     void stop_listening();
00106 
00107     /**
00108      * @brief send_to_socket Sends directly to socket. This is used by
00109      * security classes to send after data has been encrypted.
00110      * @param buf Buffer to send
00111      * @param len Length of a buffer
00112      * @return Number of bytes sent or -1 if failed
00113      */
00114     int send_to_socket(const unsigned char *buf, size_t len);
00115 
00116     /**
00117      * \brief Receives directly from the socket. This
00118      * is used by the security classes to receive raw data to be decrypted.
00119      * \param buf Buffer to send.
00120      * \param len The length of the buffer.
00121      * \param timeout Timeout defined from DTLS to wait for blocking receive calls
00122      * before timing out, by default value is 0.
00123      * \return Number of bytes read or negative number if failed.
00124      */
00125     int receive_from_socket(unsigned char *buf, size_t len);
00126 
00127     /**
00128     * @brief Error handling for DTLS connectivity.
00129     * @param error, Error code from TLS library
00130     */
00131     void handle_connection_error(int error);
00132 
00133     /**
00134      * \brief Sets the network interface handler that is used by client to connect
00135      * to a network over IP..
00136      * \param handler A network interface handler that is used by client to connect.
00137      *  This API is optional but provides a mechanism for different platforms to
00138      * manage usage of underlying network interface by client.
00139      */
00140     void set_platform_network_handler(void *handler = NULL);
00141 
00142     /**
00143     * \brief Claims mutex to prevent thread clashes
00144     * in multithreaded environment.
00145     */
00146     void claim_mutex();
00147 
00148     /**
00149     * \brief Releases mutex to prevent thread clashes
00150     * in multithreaded environment.
00151     */
00152     void release_mutex();
00153 
00154     /**
00155     * @brief Callback handler for sending data over socket.
00156     */
00157     void send_handler();
00158 
00159     /**
00160     * @brief Callback handler for receiving data over socket.
00161     */
00162     void receive_handler();
00163 
00164     /**
00165     * @brief Callback handler for receiving data for secured connection.
00166     */
00167     void receive_handshake_handler();
00168 
00169     /**
00170     * @brief Returns true if DTLS handshake is still ongoing.
00171     */
00172     bool is_handshake_ongoing();
00173 
00174     /**
00175     * @brief Returns connection handler tasklet ID.
00176     */
00177     int8_t connection_tasklet_handler();
00178 
00179     /**
00180     * @brief Handles DNS resolving through event loop.
00181     */
00182     void dns_handler();
00183 
00184     /**
00185     * @brief Sends data to socket through event loop.
00186     */
00187     void send_socket_data(uint8_t *data, uint16_t data_len);
00188 
00189 
00190 private:
00191 
00192     /**
00193     * @brief Callback handler for socket events.
00194     */
00195     void socket_event();
00196 
00197     /**
00198     * @brief Initialize mbed OS socket
00199     */
00200     void init_socket();
00201 
00202     /**
00203     * @brief Check socket type
00204     * @return True if TCP connection otherwise false
00205     */
00206     bool is_tcp_connection();
00207 
00208     /**
00209     * @brief Close and delete socket
00210     */
00211     void close_socket();
00212 
00213     /**
00214     * @brief Enables keepalive for TCP connections.
00215     */
00216     void enable_keepalive();
00217 
00218 private:
00219     M2MConnectionHandler                        *_base;
00220     M2MConnectionObserver                       &_observer;
00221     M2MConnectionSecurity                       *_security_impl; //owned
00222     const M2MSecurity                           *_security; //non-owned
00223     bool                                        _use_secure_connection;
00224     M2MInterface::BindingMode                   _binding_mode;
00225     M2MInterface::NetworkStack                  _network_stack;
00226     M2MConnectionObserver::SocketAddress        _address;
00227     unsigned char                               _address_buffer[NSAPI_IP_SIZE];
00228     Socket                                      *_socket;
00229     bool                                        _is_handshaking;
00230     bool                                        _listening;
00231     M2MConnectionObserver::ServerType           _server_type;
00232     uint16_t                                    _server_port;
00233     uint16_t                                    _listen_port;
00234     bool                                        _running;
00235     unsigned char                               _recv_buffer[BUFFER_LENGTH];
00236     NetworkInterface                            *_net_iface;  //doesn't own
00237     SocketAddress                               *_socket_address;
00238     static int8_t                                _tasklet_id;
00239     String                                      _server_address;
00240 
00241 friend class Test_M2MConnectionHandlerPimpl;
00242 friend class Test_M2MConnectionHandlerPimpl_mbed;
00243 friend class M2MConnection_TestObserver;
00244 };
00245 
00246 #endif //M2M_CONNECTION_HANDLER_PIMPL_H__