Timothy Beight / Mbed 2 deprecated 6_songs-from-the-cloud

Dependencies:   mbed Socket lwip-eth lwip-sys lwip

Fork of 6_songs-from-the-cloud by MakingMusicWorkshop

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 "mbed-client/m2mconfig.h"
00020 #include "mbed-client/m2minterface.h"
00021 #include "mbed-client/m2mconnectionobserver.h"
00022 #include "mbed-client/m2mconnectionsecurity.h"
00023 #include "nsdl-c/sn_nsdl.h"
00024 
00025 #include "Socket/Socket.h"
00026 #include "Socket/Endpoint.h"
00027 #include "Socket/UDPSocket.h"
00028 #include "threadwrapper.h"
00029 #include "Thread.h"
00030 #include "Queue.h"
00031 #include <string>
00032 
00033 
00034 class M2MConnectionSecurity;
00035 class M2MConnectionHandler;
00036 class M2MSecurity;
00037 
00038 /**
00039  * @brief M2MConnectionHandlerPimpl.
00040  * This class handles the socket connection for LWM2M Client
00041  */
00042 
00043 
00044 class M2MConnectionHandlerPimpl {
00045 public:
00046 
00047     /**
00048     * @brief Constructor
00049     */
00050     M2MConnectionHandlerPimpl(M2MConnectionHandler* base, M2MConnectionObserver &observer,
00051                               M2MConnectionSecurity* sec,
00052                               M2MInterface::BindingMode mode,
00053                               M2MInterface::NetworkStack stack);
00054 
00055     /**
00056     * @brief Destructor
00057     */
00058     ~M2MConnectionHandlerPimpl();
00059 
00060     /**
00061     * @brief This binds the socket connection.
00062     * @param listen_port Port to listen for incoming connection.
00063     * @return true if successful else false.
00064     */
00065     bool bind_connection(const uint16_t listen_port);
00066 
00067     /**
00068     * @brief This resolves the server address. Output is
00069     * returned through callback
00070     * @param String server address.
00071     * @param uint16_t Server port.
00072     * @param ServerType, Server Type to be resolved.
00073     * @return true if address is valid else false.
00074     */
00075     bool resolve_server_address(const String& server_address,
00076                                 const uint16_t server_port,
00077                                 M2MConnectionObserver::ServerType server_type,
00078                                 const M2MSecurity* security);
00079 
00080     /**
00081     * @brief Sends data, to the connected sent to server.
00082     * @param data, Data to be sent.
00083     */
00084     bool send_data(uint8_t *data_ptr,
00085                    uint16_t data_len,
00086                    sn_nsdl_addr_s *address_ptr);
00087 
00088     /**
00089     * @brief Listens for incoming data from remote server
00090     * @return true if successful else false.
00091     */
00092     bool start_listening_for_data();
00093 
00094     /**
00095     * @brief Stops listening for incoming data
00096     */
00097     void stop_listening();
00098 
00099     /**
00100      * @brief send_to_socket Sends directly to socket. This is used by
00101      * security classes to send after data has been encrypted.
00102      * @param buf Buffer to send
00103      * @param len Length of a buffer
00104      * @return Number of bytes sent or -1 if failed
00105      */
00106     int send_to_socket(const unsigned char *buf, size_t len);
00107 
00108     /**
00109      * @brief receive_from_socket Receives directly from a socket. This
00110      * is used by security classes to receive raw data to be decrypted.
00111      * @param buf Buffer to send
00112      * @param len Length of a buffer
00113      * @return Number of bytes read or -1 if failed.
00114      */
00115     int receive_from_socket(unsigned char *buf, size_t len);
00116 
00117     /**
00118     * @brief Error handling for DTLS connectivity.
00119     * @param error, Error code from TLS library
00120     */
00121     void handle_connection_error(int error);
00122 
00123 private:
00124     /**
00125     * @brief Internal handler for recieving data
00126     */
00127     void listen_handler();
00128     
00129     /**
00130     * @brief Internal handlers for managing the ethernet driver
00131     */
00132     void recv_handler();
00133     void send_handler();
00134 
00135 private:
00136     M2MConnectionHandler                        *_base;
00137     M2MConnectionObserver                       &_observer;
00138     M2MConnectionSecurity                       *_security_impl; //owned
00139     bool                                        _use_secure_connection;
00140     String                                      _server_address;
00141     unsigned char                               _address_buffer[4];
00142     M2MInterface::BindingMode                   _binding_mode;
00143     M2MInterface::NetworkStack                  _network_stack;
00144     M2MConnectionObserver::SocketAddress        _address;
00145     bool                                        _resolved;
00146     bool                                        _is_handshaking;
00147     
00148     Endpoint                                    _endpoint;
00149     UDPSocket                                   _socket;
00150     bool                                        _listening;
00151     rtos::Thread                                *_listen_thread;
00152     unsigned char                               _listen_buffer[1024];
00153     
00154     bool                                        _running;
00155     unsigned char                               _recv_buffer[1024];
00156     
00157     rtos::Thread                                *_recv_thread;
00158     rtos::Queue<std::string, 16>                 _recv_queue;
00159     rtos::Thread                                *_send_thread;
00160     rtos::Queue<std::string, 16>                 _send_queue;
00161     
00162 
00163 friend class Test_M2MConnectionHandlerPimpl;
00164 friend class Test_M2MConnectionHandlerPimpl_mbed;
00165 friend class M2MConnection_TestObserver;
00166 };
00167 
00168 #endif //M2M_CONNECTION_HANDLER_PIMPL_H__
00169 
00170