leo hendrickson / Mbed OS example-Ethernet-mbed-Cloud-connect
Committer:
leothedragon
Date:
Tue May 04 08:55:12 2021 +0000
Revision:
0:8f0bb79ddd48
nmn

Who changed what in which revision?

UserRevisionLine numberNew contents of line
leothedragon 0:8f0bb79ddd48 1 /*
leothedragon 0:8f0bb79ddd48 2 * Copyright (c) 2015 ARM Limited. All rights reserved.
leothedragon 0:8f0bb79ddd48 3 * SPDX-License-Identifier: Apache-2.0
leothedragon 0:8f0bb79ddd48 4 * Licensed under the Apache License, Version 2.0 (the License); you may
leothedragon 0:8f0bb79ddd48 5 * not use this file except in compliance with the License.
leothedragon 0:8f0bb79ddd48 6 * You may obtain a copy of the License at
leothedragon 0:8f0bb79ddd48 7 *
leothedragon 0:8f0bb79ddd48 8 * http://www.apache.org/licenses/LICENSE-2.0
leothedragon 0:8f0bb79ddd48 9 *
leothedragon 0:8f0bb79ddd48 10 * Unless required by applicable law or agreed to in writing, software
leothedragon 0:8f0bb79ddd48 11 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
leothedragon 0:8f0bb79ddd48 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
leothedragon 0:8f0bb79ddd48 13 * See the License for the specific language governing permissions and
leothedragon 0:8f0bb79ddd48 14 * limitations under the License.
leothedragon 0:8f0bb79ddd48 15 */
leothedragon 0:8f0bb79ddd48 16 #ifndef M2M_CONNECTION_HANDLER_H__
leothedragon 0:8f0bb79ddd48 17 #define M2M_CONNECTION_HANDLER_H__
leothedragon 0:8f0bb79ddd48 18
leothedragon 0:8f0bb79ddd48 19 #include "mbed-client/m2mconnectionobserver.h"
leothedragon 0:8f0bb79ddd48 20 #include "mbed-client/m2mconfig.h"
leothedragon 0:8f0bb79ddd48 21 #include "mbed-client/m2minterface.h"
leothedragon 0:8f0bb79ddd48 22 #include "nsdl-c/sn_nsdl.h"
leothedragon 0:8f0bb79ddd48 23
leothedragon 0:8f0bb79ddd48 24 class M2MConnectionSecurity;
leothedragon 0:8f0bb79ddd48 25 class M2MConnectionHandlerPimpl;
leothedragon 0:8f0bb79ddd48 26
leothedragon 0:8f0bb79ddd48 27 /*! \file m2mconnectionhandler.h
leothedragon 0:8f0bb79ddd48 28 * \brief M2MConnectionHandler.
leothedragon 0:8f0bb79ddd48 29 * This class handles the socket connection for the LWM2M Client.
leothedragon 0:8f0bb79ddd48 30 */
leothedragon 0:8f0bb79ddd48 31
leothedragon 0:8f0bb79ddd48 32 class M2MConnectionHandler {
leothedragon 0:8f0bb79ddd48 33 public:
leothedragon 0:8f0bb79ddd48 34
leothedragon 0:8f0bb79ddd48 35 /**
leothedragon 0:8f0bb79ddd48 36 * @enum ConnectionError
leothedragon 0:8f0bb79ddd48 37 * This enum defines an error that can come from the
leothedragon 0:8f0bb79ddd48 38 * socket read and write operation.
leothedragon 0:8f0bb79ddd48 39 */
leothedragon 0:8f0bb79ddd48 40 typedef enum {
leothedragon 0:8f0bb79ddd48 41 ERROR_NONE = 0,
leothedragon 0:8f0bb79ddd48 42 ERROR_GENERIC = -1,
leothedragon 0:8f0bb79ddd48 43 CONNECTION_ERROR_WANTS_READ = -2,
leothedragon 0:8f0bb79ddd48 44 CONNECTION_ERROR_WANTS_WRITE = -3,
leothedragon 0:8f0bb79ddd48 45 SSL_PEER_CLOSE_NOTIFY = -4,
leothedragon 0:8f0bb79ddd48 46 MEMORY_ALLOCATION_FAILED = -5,
leothedragon 0:8f0bb79ddd48 47 SSL_CONNECTION_ERROR = -6,
leothedragon 0:8f0bb79ddd48 48 SOCKET_READ_ERROR = -7,
leothedragon 0:8f0bb79ddd48 49 SOCKET_SEND_ERROR = -8,
leothedragon 0:8f0bb79ddd48 50 SOCKET_ABORT = -9,
leothedragon 0:8f0bb79ddd48 51 DNS_RESOLVING_ERROR = -10,
leothedragon 0:8f0bb79ddd48 52 SSL_HANDSHAKE_ERROR = -11,
leothedragon 0:8f0bb79ddd48 53 FAILED_TO_READ_CREDENTIALS = -12,
leothedragon 0:8f0bb79ddd48 54 } ConnectionError;
leothedragon 0:8f0bb79ddd48 55
leothedragon 0:8f0bb79ddd48 56 public:
leothedragon 0:8f0bb79ddd48 57
leothedragon 0:8f0bb79ddd48 58 /**
leothedragon 0:8f0bb79ddd48 59 * \brief Constructor
leothedragon 0:8f0bb79ddd48 60 */
leothedragon 0:8f0bb79ddd48 61 M2MConnectionHandler(M2MConnectionObserver &observer,
leothedragon 0:8f0bb79ddd48 62 M2MConnectionSecurity* sec,
leothedragon 0:8f0bb79ddd48 63 M2MInterface::BindingMode mode,
leothedragon 0:8f0bb79ddd48 64 M2MInterface::NetworkStack stack);
leothedragon 0:8f0bb79ddd48 65
leothedragon 0:8f0bb79ddd48 66 /**
leothedragon 0:8f0bb79ddd48 67 * \brief Destructor
leothedragon 0:8f0bb79ddd48 68 */
leothedragon 0:8f0bb79ddd48 69 ~M2MConnectionHandler();
leothedragon 0:8f0bb79ddd48 70
leothedragon 0:8f0bb79ddd48 71 /**
leothedragon 0:8f0bb79ddd48 72 * \brief This binds the socket connection.
leothedragon 0:8f0bb79ddd48 73 * \param listen_port The port to be listened to for an incoming connection.
leothedragon 0:8f0bb79ddd48 74 * \return True if successful, else false.
leothedragon 0:8f0bb79ddd48 75 */
leothedragon 0:8f0bb79ddd48 76 bool bind_connection(const uint16_t listen_port);
leothedragon 0:8f0bb79ddd48 77
leothedragon 0:8f0bb79ddd48 78 /**
leothedragon 0:8f0bb79ddd48 79 * \brief This resolves the server address. The output is
leothedragon 0:8f0bb79ddd48 80 * returned through a callback.
leothedragon 0:8f0bb79ddd48 81 * \param String The server address.
leothedragon 0:8f0bb79ddd48 82 * \param uint16_t The server port.
leothedragon 0:8f0bb79ddd48 83 * \param ServerType The server type to be resolved.
leothedragon 0:8f0bb79ddd48 84 * \param security The M2MSecurity object that determines which
leothedragon 0:8f0bb79ddd48 85 * type of secure connection is used by the socket.
leothedragon 0:8f0bb79ddd48 86 * \return True if the address is valid, else false.
leothedragon 0:8f0bb79ddd48 87 */
leothedragon 0:8f0bb79ddd48 88 bool resolve_server_address(const String& server_address,
leothedragon 0:8f0bb79ddd48 89 const uint16_t server_port,
leothedragon 0:8f0bb79ddd48 90 M2MConnectionObserver::ServerType server_type,
leothedragon 0:8f0bb79ddd48 91 const M2MSecurity* security);
leothedragon 0:8f0bb79ddd48 92
leothedragon 0:8f0bb79ddd48 93 /**
leothedragon 0:8f0bb79ddd48 94 * \brief Sends data to the connected server.
leothedragon 0:8f0bb79ddd48 95 * \param data_ptr The data to be sent.
leothedragon 0:8f0bb79ddd48 96 * \param data_len The length of data to be sent.
leothedragon 0:8f0bb79ddd48 97 * \param address_ptr The address structure to which the data needs to be sent.
leothedragon 0:8f0bb79ddd48 98 * \return True if data is sent successfully, else false.
leothedragon 0:8f0bb79ddd48 99 */
leothedragon 0:8f0bb79ddd48 100 bool send_data(uint8_t *data_ptr,
leothedragon 0:8f0bb79ddd48 101 uint16_t data_len,
leothedragon 0:8f0bb79ddd48 102 sn_nsdl_addr_s *address_ptr);
leothedragon 0:8f0bb79ddd48 103
leothedragon 0:8f0bb79ddd48 104 /**
leothedragon 0:8f0bb79ddd48 105 * \brief Listens to the incoming data from a remote server.
leothedragon 0:8f0bb79ddd48 106 * \return True if successful, else false.
leothedragon 0:8f0bb79ddd48 107 */
leothedragon 0:8f0bb79ddd48 108 bool start_listening_for_data();
leothedragon 0:8f0bb79ddd48 109
leothedragon 0:8f0bb79ddd48 110 /**
leothedragon 0:8f0bb79ddd48 111 * \brief Stops listening to the incoming data.
leothedragon 0:8f0bb79ddd48 112 */
leothedragon 0:8f0bb79ddd48 113 void stop_listening();
leothedragon 0:8f0bb79ddd48 114
leothedragon 0:8f0bb79ddd48 115 /**
leothedragon 0:8f0bb79ddd48 116 * \brief Closes the open connection.
leothedragon 0:8f0bb79ddd48 117 * \note This must be called from the same event loop context!
leothedragon 0:8f0bb79ddd48 118 */
leothedragon 0:8f0bb79ddd48 119 void force_close();
leothedragon 0:8f0bb79ddd48 120
leothedragon 0:8f0bb79ddd48 121 /**
leothedragon 0:8f0bb79ddd48 122 * \brief Error handling for DTLS connectivity.
leothedragon 0:8f0bb79ddd48 123 * \param error An error code from the TLS library.
leothedragon 0:8f0bb79ddd48 124 */
leothedragon 0:8f0bb79ddd48 125 void handle_connection_error(int error);
leothedragon 0:8f0bb79ddd48 126
leothedragon 0:8f0bb79ddd48 127 /**
leothedragon 0:8f0bb79ddd48 128 * \brief Sets the network interface handler that is used by the client to connect
leothedragon 0:8f0bb79ddd48 129 * to a network over IP.
leothedragon 0:8f0bb79ddd48 130 * \param handler A network interface handler that is used by the client to connect.
leothedragon 0:8f0bb79ddd48 131 * This API is optional but it provides a mechanism for different platforms to
leothedragon 0:8f0bb79ddd48 132 * manage the usage of underlying network interface by client.
leothedragon 0:8f0bb79ddd48 133 */
leothedragon 0:8f0bb79ddd48 134 void set_platform_network_handler(void *handler = NULL);
leothedragon 0:8f0bb79ddd48 135
leothedragon 0:8f0bb79ddd48 136 /**
leothedragon 0:8f0bb79ddd48 137 * \brief Claims mutex to prevent thread clashes
leothedragon 0:8f0bb79ddd48 138 * in multithreaded environment.
leothedragon 0:8f0bb79ddd48 139 */
leothedragon 0:8f0bb79ddd48 140 void claim_mutex();
leothedragon 0:8f0bb79ddd48 141
leothedragon 0:8f0bb79ddd48 142 /**
leothedragon 0:8f0bb79ddd48 143 * \brief Releases mutex to prevent thread clashes
leothedragon 0:8f0bb79ddd48 144 * in multithreaded environment.
leothedragon 0:8f0bb79ddd48 145 */
leothedragon 0:8f0bb79ddd48 146 void release_mutex();
leothedragon 0:8f0bb79ddd48 147
leothedragon 0:8f0bb79ddd48 148 /**
leothedragon 0:8f0bb79ddd48 149 * \brief Unregisters the network interface handler that is set in 'set_platform_network_handler'.
leothedragon 0:8f0bb79ddd48 150 */
leothedragon 0:8f0bb79ddd48 151 void unregister_network_handler();
leothedragon 0:8f0bb79ddd48 152
leothedragon 0:8f0bb79ddd48 153 private:
leothedragon 0:8f0bb79ddd48 154
leothedragon 0:8f0bb79ddd48 155 M2MConnectionObserver &_observer;
leothedragon 0:8f0bb79ddd48 156 M2MConnectionHandlerPimpl *_private_impl;
leothedragon 0:8f0bb79ddd48 157
leothedragon 0:8f0bb79ddd48 158 friend class Test_M2MConnectionHandler;
leothedragon 0:8f0bb79ddd48 159 friend class Test_M2MConnectionHandler_mbed;
leothedragon 0:8f0bb79ddd48 160 friend class Test_M2MConnectionHandler_linux;
leothedragon 0:8f0bb79ddd48 161 friend class M2MConnection_TestObserver;
leothedragon 0:8f0bb79ddd48 162 };
leothedragon 0:8f0bb79ddd48 163
leothedragon 0:8f0bb79ddd48 164 #endif //M2M_CONNECTION_HANDLER_H__
leothedragon 0:8f0bb79ddd48 165