mbed client lightswitch demo

Dependencies:   mbed Socket lwip-eth lwip-sys lwip

Fork of mbed-client-classic-example-lwip by Austin Blackstone

Committer:
mbedAustin
Date:
Thu Jun 09 17:08:36 2016 +0000
Revision:
11:cada08fc8a70
Commit for public Consumption

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbedAustin 11:cada08fc8a70 1 /*
mbedAustin 11:cada08fc8a70 2 * Copyright (c) 2015 ARM Limited. All rights reserved.
mbedAustin 11:cada08fc8a70 3 * SPDX-License-Identifier: Apache-2.0
mbedAustin 11:cada08fc8a70 4 * Licensed under the Apache License, Version 2.0 (the License); you may
mbedAustin 11:cada08fc8a70 5 * not use this file except in compliance with the License.
mbedAustin 11:cada08fc8a70 6 * You may obtain a copy of the License at
mbedAustin 11:cada08fc8a70 7 *
mbedAustin 11:cada08fc8a70 8 * http://www.apache.org/licenses/LICENSE-2.0
mbedAustin 11:cada08fc8a70 9 *
mbedAustin 11:cada08fc8a70 10 * Unless required by applicable law or agreed to in writing, software
mbedAustin 11:cada08fc8a70 11 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
mbedAustin 11:cada08fc8a70 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
mbedAustin 11:cada08fc8a70 13 * See the License for the specific language governing permissions and
mbedAustin 11:cada08fc8a70 14 * limitations under the License.
mbedAustin 11:cada08fc8a70 15 */
mbedAustin 11:cada08fc8a70 16 #ifndef M2M_CONNECTION_HANDLER_H__
mbedAustin 11:cada08fc8a70 17 #define M2M_CONNECTION_HANDLER_H__
mbedAustin 11:cada08fc8a70 18
mbedAustin 11:cada08fc8a70 19 #include "mbed-client/m2mconnectionobserver.h"
mbedAustin 11:cada08fc8a70 20 #include "mbed-client/m2mconfig.h"
mbedAustin 11:cada08fc8a70 21 #include "mbed-client/m2minterface.h"
mbedAustin 11:cada08fc8a70 22 #include "nsdl-c/sn_nsdl.h"
mbedAustin 11:cada08fc8a70 23
mbedAustin 11:cada08fc8a70 24 class M2MConnectionSecurity;
mbedAustin 11:cada08fc8a70 25 class M2MConnectionHandlerPimpl;
mbedAustin 11:cada08fc8a70 26
mbedAustin 11:cada08fc8a70 27 /**
mbedAustin 11:cada08fc8a70 28 * @brief M2MConnectionHandler.
mbedAustin 11:cada08fc8a70 29 * This class handles the socket connection for the LWM2M Client.
mbedAustin 11:cada08fc8a70 30 */
mbedAustin 11:cada08fc8a70 31
mbedAustin 11:cada08fc8a70 32 class M2MConnectionHandler {
mbedAustin 11:cada08fc8a70 33 public:
mbedAustin 11:cada08fc8a70 34
mbedAustin 11:cada08fc8a70 35 /**
mbedAustin 11:cada08fc8a70 36 * @enum ConnectionError
mbedAustin 11:cada08fc8a70 37 * This enum defines an error that can come from
mbedAustin 11:cada08fc8a70 38 * socket read and write operation.
mbedAustin 11:cada08fc8a70 39 */
mbedAustin 11:cada08fc8a70 40 typedef enum {
mbedAustin 11:cada08fc8a70 41 CONNECTION_ERROR_WANTS_READ = -1000,
mbedAustin 11:cada08fc8a70 42 CONNECTION_ERROR_WANTS_WRITE = -1001
mbedAustin 11:cada08fc8a70 43 }ConnectionError;
mbedAustin 11:cada08fc8a70 44
mbedAustin 11:cada08fc8a70 45 public:
mbedAustin 11:cada08fc8a70 46
mbedAustin 11:cada08fc8a70 47 /**
mbedAustin 11:cada08fc8a70 48 * @brief Constructor
mbedAustin 11:cada08fc8a70 49 */
mbedAustin 11:cada08fc8a70 50 M2MConnectionHandler(M2MConnectionObserver &observer,
mbedAustin 11:cada08fc8a70 51 M2MConnectionSecurity* sec,
mbedAustin 11:cada08fc8a70 52 M2MInterface::BindingMode mode,
mbedAustin 11:cada08fc8a70 53 M2MInterface::NetworkStack stack);
mbedAustin 11:cada08fc8a70 54
mbedAustin 11:cada08fc8a70 55 /**
mbedAustin 11:cada08fc8a70 56 * @brief Destructor
mbedAustin 11:cada08fc8a70 57 */
mbedAustin 11:cada08fc8a70 58 ~M2MConnectionHandler();
mbedAustin 11:cada08fc8a70 59
mbedAustin 11:cada08fc8a70 60 /**
mbedAustin 11:cada08fc8a70 61 * @brief This binds the socket connection.
mbedAustin 11:cada08fc8a70 62 * @param listen_port Port to be listened to for an incoming connection.
mbedAustin 11:cada08fc8a70 63 * @return True if successful, else false.
mbedAustin 11:cada08fc8a70 64 */
mbedAustin 11:cada08fc8a70 65 bool bind_connection(const uint16_t listen_port);
mbedAustin 11:cada08fc8a70 66
mbedAustin 11:cada08fc8a70 67 /**
mbedAustin 11:cada08fc8a70 68 * @brief This resolves the server address. Output is
mbedAustin 11:cada08fc8a70 69 * returned through a callback.
mbedAustin 11:cada08fc8a70 70 * @param String Server address.
mbedAustin 11:cada08fc8a70 71 * @param uint16_t Server port.
mbedAustin 11:cada08fc8a70 72 * @param ServerType, Server Type to be resolved.
mbedAustin 11:cada08fc8a70 73 * @param security, M2MSecurity object that determines what
mbedAustin 11:cada08fc8a70 74 * type of secure connection will be used by the socket.
mbedAustin 11:cada08fc8a70 75 * @return True if address is valid, else false.
mbedAustin 11:cada08fc8a70 76 */
mbedAustin 11:cada08fc8a70 77 bool resolve_server_address(const String& server_address,
mbedAustin 11:cada08fc8a70 78 const uint16_t server_port,
mbedAustin 11:cada08fc8a70 79 M2MConnectionObserver::ServerType server_type,
mbedAustin 11:cada08fc8a70 80 const M2MSecurity* security);
mbedAustin 11:cada08fc8a70 81
mbedAustin 11:cada08fc8a70 82 /**
mbedAustin 11:cada08fc8a70 83 * @brief Sends data to the connected server.
mbedAustin 11:cada08fc8a70 84 * @param data_ptr, Data to be sent.
mbedAustin 11:cada08fc8a70 85 * @param data_len, Length of data to be sent.
mbedAustin 11:cada08fc8a70 86 * @param address_ptr, Address structure where data has to be sent.
mbedAustin 11:cada08fc8a70 87 * @return True if data is sent successfully, else false.
mbedAustin 11:cada08fc8a70 88 */
mbedAustin 11:cada08fc8a70 89 bool send_data(uint8_t *data_ptr,
mbedAustin 11:cada08fc8a70 90 uint16_t data_len,
mbedAustin 11:cada08fc8a70 91 sn_nsdl_addr_s *address_ptr);
mbedAustin 11:cada08fc8a70 92
mbedAustin 11:cada08fc8a70 93 /**
mbedAustin 11:cada08fc8a70 94 * @brief Listens to the incoming data from a remote server.
mbedAustin 11:cada08fc8a70 95 * @return True if successful, else false.
mbedAustin 11:cada08fc8a70 96 */
mbedAustin 11:cada08fc8a70 97 bool start_listening_for_data();
mbedAustin 11:cada08fc8a70 98
mbedAustin 11:cada08fc8a70 99 /**
mbedAustin 11:cada08fc8a70 100 * @brief Stops listening to the incoming data.
mbedAustin 11:cada08fc8a70 101 */
mbedAustin 11:cada08fc8a70 102 void stop_listening();
mbedAustin 11:cada08fc8a70 103
mbedAustin 11:cada08fc8a70 104 /**
mbedAustin 11:cada08fc8a70 105 * @brief sendToSocket Sends directly to socket. This is used by
mbedAustin 11:cada08fc8a70 106 * security classes to send the data after it has been encrypted.
mbedAustin 11:cada08fc8a70 107 * @param buf Buffer to send.
mbedAustin 11:cada08fc8a70 108 * @param len Length of a buffer.
mbedAustin 11:cada08fc8a70 109 * @return Number of bytes sent or -1 if failed.
mbedAustin 11:cada08fc8a70 110 */
mbedAustin 11:cada08fc8a70 111 int send_to_socket(const unsigned char *buf, size_t len);
mbedAustin 11:cada08fc8a70 112
mbedAustin 11:cada08fc8a70 113 /**
mbedAustin 11:cada08fc8a70 114 * @brief receiveFromSocket Receives directly from a socket. This
mbedAustin 11:cada08fc8a70 115 * is used by the security classes to receive raw data to be decrypted.
mbedAustin 11:cada08fc8a70 116 * @param buf Buffer to send.
mbedAustin 11:cada08fc8a70 117 * @param len Length of a buffer.
mbedAustin 11:cada08fc8a70 118 * @return Number of bytes read or -1 if failed.
mbedAustin 11:cada08fc8a70 119 */
mbedAustin 11:cada08fc8a70 120 int receive_from_socket(unsigned char *buf, size_t len);
mbedAustin 11:cada08fc8a70 121
mbedAustin 11:cada08fc8a70 122 /**
mbedAustin 11:cada08fc8a70 123 * @brief Closes the open connection.
mbedAustin 11:cada08fc8a70 124 */
mbedAustin 11:cada08fc8a70 125 void close_connection();
mbedAustin 11:cada08fc8a70 126
mbedAustin 11:cada08fc8a70 127 /**
mbedAustin 11:cada08fc8a70 128 * @brief Error handling for DTLS connectivity.
mbedAustin 11:cada08fc8a70 129 * @param error, Error code from TLS library
mbedAustin 11:cada08fc8a70 130 */
mbedAustin 11:cada08fc8a70 131 void handle_connection_error(int error);
mbedAustin 11:cada08fc8a70 132
mbedAustin 11:cada08fc8a70 133 private:
mbedAustin 11:cada08fc8a70 134
mbedAustin 11:cada08fc8a70 135 M2MConnectionObserver &_observer;
mbedAustin 11:cada08fc8a70 136 M2MConnectionHandlerPimpl *_private_impl;
mbedAustin 11:cada08fc8a70 137
mbedAustin 11:cada08fc8a70 138 friend class Test_M2MConnectionHandler;
mbedAustin 11:cada08fc8a70 139 friend class Test_M2MConnectionHandler_mbed;
mbedAustin 11:cada08fc8a70 140 friend class Test_M2MConnectionHandler_linux;
mbedAustin 11:cada08fc8a70 141 friend class M2MConnection_TestObserver;
mbedAustin 11:cada08fc8a70 142 };
mbedAustin 11:cada08fc8a70 143
mbedAustin 11:cada08fc8a70 144 #endif //M2M_CONNECTION_HANDLER_H__
mbedAustin 11:cada08fc8a70 145