Simulated product dispenser

Dependencies:   HTS221

Fork of mbed-cloud-workshop-connect-HTS221 by Jim Carver

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers m2mconnectionhandler.h Source File

m2mconnectionhandler.h

Go to the documentation of this file.
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_H__
00017 #define M2M_CONNECTION_HANDLER_H__
00018 
00019 #include "mbed-client/m2mconnectionobserver.h"
00020 #include "mbed-client/m2mconfig.h"
00021 #include "mbed-client/m2minterface.h"
00022 #include "nsdl-c/sn_nsdl.h"
00023 
00024 class M2MConnectionSecurity;
00025 class M2MConnectionHandlerPimpl;
00026 
00027 /*! \file m2mconnectionhandler.h
00028  * \brief M2MConnectionHandler.
00029  * This class handles the socket connection for the LWM2M Client.
00030  */
00031 
00032 class M2MConnectionHandler {
00033 public:
00034 
00035     /**
00036      * @enum ConnectionError
00037      * This enum defines an error that can come from the
00038      * socket read and write operation.
00039      */
00040     typedef enum {
00041         CONNECTION_ERROR_WANTS_READ = -1000,
00042         CONNECTION_ERROR_WANTS_WRITE = -1001,
00043         SSL_PEER_CLOSE_NOTIFY = -1002,
00044         ERROR_NONE = 0,
00045         SSL_CONNECTION_ERROR,
00046         SOCKET_READ_ERROR,
00047         SOCKET_SEND_ERROR,
00048         SOCKET_ABORT,
00049         DNS_RESOLVING_ERROR,
00050         SSL_HANDSHAKE_ERROR,
00051         SSL_PEER_CLOSED
00052     }ConnectionError;
00053 
00054 
00055 public:
00056 
00057     /**
00058     * \brief Constructor
00059     */
00060     M2MConnectionHandler(M2MConnectionObserver &observer,
00061                          M2MConnectionSecurity* sec,
00062                          M2MInterface::BindingMode mode,
00063                          M2MInterface::NetworkStack stack);
00064 
00065     /**
00066     * \brief Destructor
00067     */
00068     ~M2MConnectionHandler();
00069 
00070     /**
00071     * \brief This binds the socket connection.
00072     * \param listen_port The port to be listened to for an 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. The output is
00079     * returned through a callback.
00080     * \param String The server address.
00081     * \param uint16_t The server port.
00082     * \param ServerType The server type to be resolved.
00083     * \param security The M2MSecurity object that determines which
00084     * type of secure connection is used by the socket.
00085     * \return True if the address is valid, else false.
00086     */
00087     bool resolve_server_address(const String& server_address,
00088                                 const uint16_t server_port,
00089                                 M2MConnectionObserver::ServerType server_type,
00090                                 const M2MSecurity* security);
00091 
00092     /**
00093     * \brief Sends data to the connected server.
00094     * \param data_ptr The data to be sent.
00095     * \param data_len The length of data to be sent.
00096     * \param address_ptr The address structure to which the data needs to be sent.
00097     * \return True if data is sent successfully, else false.
00098     */
00099     bool send_data(uint8_t *data_ptr,
00100                            uint16_t data_len,
00101                            sn_nsdl_addr_s *address_ptr);
00102 
00103     /**
00104     * \brief Listens to the incoming data from a remote server.
00105     * \return True if successful, else false.
00106     */
00107     bool start_listening_for_data();
00108 
00109     /**
00110     * \brief Stops listening to the incoming data.
00111     */
00112     void stop_listening();
00113 
00114     /**
00115     * \brief Closes the open connection.
00116     * \note This must be called from the same event loop context!
00117     */
00118     void force_close();
00119 
00120     /**
00121     * \brief Error handling for DTLS connectivity.
00122     * \param error An error code from the TLS library.
00123     */
00124     void handle_connection_error(int error);
00125 
00126     /**
00127      * \brief Sets the network interface handler that is used by the client to connect
00128      * to a network over IP.
00129      * \param handler A network interface handler that is used by the client to connect.
00130      *  This API is optional but it provides a mechanism for different platforms to
00131      * manage the usage of underlying network interface by client.
00132      */
00133     void set_platform_network_handler(void *handler = NULL);
00134 
00135     /**
00136     * \brief Claims mutex to prevent thread clashes
00137     * in multithreaded environment.
00138     */
00139     void claim_mutex();
00140 
00141     /**
00142     * \brief Releases mutex to prevent thread clashes
00143     * in multithreaded environment.
00144     */
00145     void release_mutex();
00146 
00147 private:
00148 
00149     M2MConnectionObserver                       &_observer;
00150     M2MConnectionHandlerPimpl                   *_private_impl;
00151 
00152 friend class Test_M2MConnectionHandler;
00153 friend class Test_M2MConnectionHandler_mbed;
00154 friend class Test_M2MConnectionHandler_linux;
00155 friend class M2MConnection_TestObserver;
00156 };
00157 
00158 #endif //M2M_CONNECTION_HANDLER_H__
00159