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.
m2mconnectionhandler.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_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 ERROR_NONE = 0, 00044 SSL_CONNECTION_ERROR, 00045 SOCKET_READ_ERROR, 00046 SOCKET_SEND_ERROR, 00047 SOCKET_ABORT, 00048 DNS_RESOLVING_ERROR, 00049 SSL_HANDSHAKE_ERROR 00050 }ConnectionError; 00051 00052 00053 public: 00054 00055 /** 00056 * \brief Constructor 00057 */ 00058 M2MConnectionHandler(M2MConnectionObserver &observer, 00059 M2MConnectionSecurity* sec, 00060 M2MInterface::BindingMode mode, 00061 M2MInterface::NetworkStack stack); 00062 00063 /** 00064 * \brief Destructor 00065 */ 00066 ~M2MConnectionHandler(); 00067 00068 /** 00069 * \brief This binds the socket connection. 00070 * \param listen_port The port to be listened to for an 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. The output is 00077 * returned through a callback. 00078 * \param String The server address. 00079 * \param uint16_t The server port. 00080 * \param ServerType The server type to be resolved. 00081 * \param security The M2MSecurity object that determines which 00082 * type of secure connection is used by the socket. 00083 * \return True if the 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 server. 00092 * \param data_ptr The data to be sent. 00093 * \param data_len The length of data to be sent. 00094 * \param address_ptr The address structure to which the data needs to be sent. 00095 * \return True if data is sent successfully, else false. 00096 */ 00097 bool send_data(uint8_t *data_ptr, 00098 uint16_t data_len, 00099 sn_nsdl_addr_s *address_ptr); 00100 00101 /** 00102 * \brief Listens to the incoming data from a remote server. 00103 * \return True if successful, else false. 00104 */ 00105 bool start_listening_for_data(); 00106 00107 /** 00108 * \brief Stops listening to the incoming data. 00109 */ 00110 void stop_listening(); 00111 00112 /** 00113 * \brief Sends directly to the socket. This is used by the 00114 * security classes to send the data after it has been encrypted. 00115 * \param buf The buffer to send to. 00116 * \param len The length of the buffer. 00117 * \return The number of bytes sent, or -1 if failed. 00118 */ 00119 int send_to_socket(const unsigned char *buf, size_t len); 00120 00121 /** 00122 * \brief Receives directly from the socket. This 00123 * is used by the security classes to receive raw data to be decrypted. 00124 * \param buf The buffer to send to. 00125 * \param len The length of the buffer. 00126 * \return The number of bytes read, or -1 if failed. 00127 */ 00128 int receive_from_socket(unsigned char *buf, size_t len); 00129 00130 /** 00131 * \brief Closes the open connection. 00132 */ 00133 void close_connection(); 00134 00135 /** 00136 * \brief Error handling for DTLS connectivity. 00137 * \param error An error code from the TLS library. 00138 */ 00139 void handle_connection_error(int error); 00140 00141 /** 00142 * \brief Sets the network interface handler that is used by the client to connect 00143 * to a network over IP. 00144 * \param handler A network interface handler that is used by the client to connect. 00145 * This API is optional but it provides a mechanism for different platforms to 00146 * manage the usage of underlying network interface by client. 00147 */ 00148 void set_platform_network_handler(void *handler = NULL); 00149 00150 /** 00151 * \brief Claims mutex to prevent thread clashes 00152 * in multithreaded environment. 00153 */ 00154 void claim_mutex(); 00155 00156 /** 00157 * \brief Releases mutex to prevent thread clashes 00158 * in multithreaded environment. 00159 */ 00160 void release_mutex(); 00161 00162 private: 00163 00164 M2MConnectionObserver &_observer; 00165 M2MConnectionHandlerPimpl *_private_impl; 00166 00167 friend class Test_M2MConnectionHandler; 00168 friend class Test_M2MConnectionHandler_mbed; 00169 friend class Test_M2MConnectionHandler_linux; 00170 friend class M2MConnection_TestObserver; 00171 }; 00172 00173 #endif //M2M_CONNECTION_HANDLER_H__ 00174
Generated on Tue Jul 12 2022 21:20:26 by
