NetworkSocketAPI

Dependencies:   DnsQuery

Dependents:   HelloWizFi250Interface

Fork of NetworkSocketAPI by NetworkSocketAPI

Committer:
Christopher Haster
Date:
Tue Apr 05 12:02:56 2016 -0500
Revision:
80:9c6673c93082
Parent:
79:43a7e8c0d6cc
Child:
81:1600369a29dd
Added support for DNS resolution

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Christopher Haster 78:0914f9b9b24b 1 /* Socket
Christopher Haster 76:bbe51641f405 2 * Copyright (c) 2015 ARM Limited
Christopher Haster 76:bbe51641f405 3 *
Christopher Haster 76:bbe51641f405 4 * Licensed under the Apache License, Version 2.0 (the "License");
Christopher Haster 76:bbe51641f405 5 * you may not use this file except in compliance with the License.
Christopher Haster 76:bbe51641f405 6 * You may obtain a copy of the License at
Christopher Haster 76:bbe51641f405 7 *
Christopher Haster 76:bbe51641f405 8 * http://www.apache.org/licenses/LICENSE-2.0
Christopher Haster 76:bbe51641f405 9 *
Christopher Haster 76:bbe51641f405 10 * Unless required by applicable law or agreed to in writing, software
Christopher Haster 76:bbe51641f405 11 * distributed under the License is distributed on an "AS IS" BASIS,
Christopher Haster 76:bbe51641f405 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Christopher Haster 76:bbe51641f405 13 * See the License for the specific language governing permissions and
Christopher Haster 76:bbe51641f405 14 * limitations under the License.
Christopher Haster 76:bbe51641f405 15 */
Christopher Haster 76:bbe51641f405 16
Christopher Haster 76:bbe51641f405 17 #ifndef NETWORK_INTERFACE_H
Christopher Haster 76:bbe51641f405 18 #define NETWORK_INTERFACE_H
Christopher Haster 76:bbe51641f405 19
Christopher Haster 79:43a7e8c0d6cc 20 #include "FunctionPointer.h"
Christopher Haster 80:9c6673c93082 21 #include "SocketAddress.h"
Christopher Haster 79:43a7e8c0d6cc 22
Christopher Haster 77:b66a6984ed2d 23 /**
Christopher Haster 77:b66a6984ed2d 24 * @enum ns_error_t
Christopher Haster 77:b66a6984ed2d 25 * @brief enum of standardized error codes
Christopher Haster 77:b66a6984ed2d 26 */
Christopher Haster 77:b66a6984ed2d 27 enum ns_error_t {
Christopher Haster 77:b66a6984ed2d 28 NSAPI_ERROR_WOULD_BLOCK = -3001, /*!< no data is not available but call is non-blocking */
Christopher Haster 77:b66a6984ed2d 29 NSAPI_ERROR_UNSUPPORTED = -3002, /*!< unsupported configuration */
Christopher Haster 77:b66a6984ed2d 30 NSAPI_ERROR_NO_CONNECTION = -3003, /*!< not connected to a network */
Christopher Haster 77:b66a6984ed2d 31 NSAPI_ERROR_NO_SOCKET = -3004, /*!< socket not available for use */
Christopher Haster 77:b66a6984ed2d 32 NSAPI_ERROR_NO_ADDRESS = -3005, /*!< IP address is not known */
Christopher Haster 77:b66a6984ed2d 33 NSAPI_ERROR_NO_MEMORY = -3006, /*!< memory resource not available */
Christopher Haster 77:b66a6984ed2d 34 NSAPI_ERROR_DNS_FAILURE = -3007, /*!< DNS failed to complete successfully */
Christopher Haster 77:b66a6984ed2d 35 NSAPI_ERROR_DHCP_FAILURE = -3008, /*!< DHCP failed to complete successfully */
Christopher Haster 77:b66a6984ed2d 36 NSAPI_ERROR_AUTH_FAILURE = -3009, /*!< connection to access point faield */
Christopher Haster 77:b66a6984ed2d 37 NSAPI_ERROR_DEVICE_ERROR = -3010, /*!< failure interfacing with the network procesor */
Christopher Haster 77:b66a6984ed2d 38 };
Christopher Haster 77:b66a6984ed2d 39
Christopher Haster 77:b66a6984ed2d 40 /**
Christopher Haster 77:b66a6984ed2d 41 * @enum ns_opt_t
Christopher Haster 77:b66a6984ed2d 42 * @brief enum of available options
Christopher Haster 77:b66a6984ed2d 43 */
Christopher Haster 77:b66a6984ed2d 44 enum ns_opt_t {
Christopher Haster 77:b66a6984ed2d 45 };
Christopher Haster 77:b66a6984ed2d 46
Christopher Haster 76:bbe51641f405 47 /** NetworkInterface class
Christopher Haster 76:bbe51641f405 48 * Common interface that is shared between all hardware that
Christopher Haster 76:bbe51641f405 49 * can connect to a network over IP.
Christopher Haster 76:bbe51641f405 50 */
Christopher Haster 76:bbe51641f405 51 class NetworkInterface
Christopher Haster 76:bbe51641f405 52 {
Christopher Haster 76:bbe51641f405 53 public:
Christopher Haster 76:bbe51641f405 54 virtual ~NetworkInterface() {};
Christopher Haster 76:bbe51641f405 55
Christopher Haster 76:bbe51641f405 56 /** Get the internally stored IP address
Christopher Haster 76:bbe51641f405 57 /return IP address of the interface or null if not yet connected
Christopher Haster 76:bbe51641f405 58 */
Christopher Haster 76:bbe51641f405 59 virtual const char *get_ip_address() = 0;
Christopher Haster 76:bbe51641f405 60
Christopher Haster 76:bbe51641f405 61 /** Get the internally stored MAC address
Christopher Haster 76:bbe51641f405 62 /return MAC address of the interface
Christopher Haster 76:bbe51641f405 63 */
Christopher Haster 76:bbe51641f405 64 virtual const char *get_mac_address() = 0;
Christopher Haster 76:bbe51641f405 65
Christopher Haster 76:bbe51641f405 66 /** Get the current status of the interface
Christopher Haster 76:bbe51641f405 67 /return true if connected
Christopher Haster 76:bbe51641f405 68 */
Christopher Haster 76:bbe51641f405 69 virtual bool is_connected() {
Christopher Haster 76:bbe51641f405 70 return get_ip_address() != NULL;
Christopher Haster 76:bbe51641f405 71 }
Christopher Haster 76:bbe51641f405 72
Christopher Haster 76:bbe51641f405 73 /** Looks up the specified host's IP address
Christopher Haster 76:bbe51641f405 74 /param name Hostname to lookup
Christopher Haster 80:9c6673c93082 75 /param dest Destination for IP address, must have space for SocketAddress::IP_SIZE
Christopher Haster 80:9c6673c93082 76 /return 0 on success, negative on failure
Christopher Haster 76:bbe51641f405 77 */
Christopher Haster 80:9c6673c93082 78 virtual int gethostbyname(const char *name, char *dest);
Christopher Haster 76:bbe51641f405 79
Christopher Haster 76:bbe51641f405 80 protected:
Christopher Haster 79:43a7e8c0d6cc 81 friend class Socket;
Christopher Haster 79:43a7e8c0d6cc 82 friend class UDPSocket;
Christopher Haster 79:43a7e8c0d6cc 83 friend class TCPSocket;
Christopher Haster 79:43a7e8c0d6cc 84 friend class TCPServer;
Christopher Haster 79:43a7e8c0d6cc 85
Christopher Haster 76:bbe51641f405 86 /** Enum of socket protocols
Christopher Haster 76:bbe51641f405 87 /enum protocol_t
Christopher Haster 76:bbe51641f405 88 */
Christopher Haster 76:bbe51641f405 89 enum protocol_t {
Christopher Haster 76:bbe51641f405 90 TCP, /*!< Socket is of TCP type */
Christopher Haster 76:bbe51641f405 91 UDP, /*!< Socket is of UDP type */
Christopher Haster 76:bbe51641f405 92 };
Christopher Haster 76:bbe51641f405 93
Christopher Haster 76:bbe51641f405 94 /** Create a socket
Christopher Haster 76:bbe51641f405 95 /param proto The type of socket to open, TCP or UDP
Christopher Haster 76:bbe51641f405 96 /return The alocated socket or null on failure
Christopher Haster 76:bbe51641f405 97 */
Christopher Haster 76:bbe51641f405 98 virtual void *socket_create(protocol_t proto) = 0;
Christopher Haster 76:bbe51641f405 99
Christopher Haster 76:bbe51641f405 100 /** Destroy a socket
Christopher Haster 76:bbe51641f405 101 /param socket Previously allocated socket
Christopher Haster 76:bbe51641f405 102 */
Christopher Haster 76:bbe51641f405 103 virtual void socket_destroy(void *handle) = 0;
Christopher Haster 76:bbe51641f405 104
Christopher Haster 76:bbe51641f405 105 /** Set socket options
Christopher Haster 76:bbe51641f405 106 \param handle Socket handle
Christopher Haster 76:bbe51641f405 107 \param optname Option ID
Christopher Haster 76:bbe51641f405 108 \param optval Option value
Christopher Haster 76:bbe51641f405 109 \param optlen Length of the option value
Christopher Haster 76:bbe51641f405 110 \return 0 on success, negative on failure
Christopher Haster 76:bbe51641f405 111 */
Christopher Haster 76:bbe51641f405 112 virtual int socket_set_option(void *handle, int optname, const void *optval, unsigned int optlen) = 0;
Christopher Haster 76:bbe51641f405 113
Christopher Haster 76:bbe51641f405 114 /** Get socket options
Christopher Haster 76:bbe51641f405 115 \param handle Socket handle
Christopher Haster 76:bbe51641f405 116 \param optname Option ID
Christopher Haster 76:bbe51641f405 117 \param optval Buffer pointer where to write the option value
Christopher Haster 76:bbe51641f405 118 \param optlen Length of the option value
Christopher Haster 76:bbe51641f405 119 \return 0 on success, negative on failure
Christopher Haster 76:bbe51641f405 120 */
Christopher Haster 76:bbe51641f405 121 virtual int socket_get_option(void *handle, int optname, void *optval, unsigned int *optlen) = 0;
Christopher Haster 76:bbe51641f405 122
Christopher Haster 76:bbe51641f405 123 /** Bind a server socket to a specific port
Christopher Haster 76:bbe51641f405 124 \param handle Socket handle
Christopher Haster 76:bbe51641f405 125 \param port The port to listen for incoming connections on
Christopher Haster 76:bbe51641f405 126 \return 0 on success, negative on failure.
Christopher Haster 76:bbe51641f405 127 */
Christopher Haster 76:bbe51641f405 128 virtual int socket_bind(void *handle, int port) = 0;
Christopher Haster 76:bbe51641f405 129
Christopher Haster 76:bbe51641f405 130 /** Start listening for incoming connections
Christopher Haster 76:bbe51641f405 131 \param handle Socket handle
Christopher Haster 76:bbe51641f405 132 \param backlog Number of pending connections that can be queued up at any
Christopher Haster 76:bbe51641f405 133 one time [Default: 1]
Christopher Haster 76:bbe51641f405 134 \return 0 on success, negative on failure
Christopher Haster 76:bbe51641f405 135 */
Christopher Haster 80:9c6673c93082 136 virtual int socket_listen(void *handle, int backlog) = 0;
Christopher Haster 76:bbe51641f405 137
Christopher Haster 76:bbe51641f405 138 /** Accept a new connection.
Christopher Haster 76:bbe51641f405 139 \param handle Socket handle
Christopher Haster 76:bbe51641f405 140 \param socket A TCPSocket instance that will handle the incoming connection.
Christopher Haster 76:bbe51641f405 141 \return 0 on success, negative on failure.
Christopher Haster 76:bbe51641f405 142 */
Christopher Haster 76:bbe51641f405 143 virtual int socket_accept(void *handle, void **connection) = 0;
Christopher Haster 76:bbe51641f405 144
Christopher Haster 76:bbe51641f405 145 /** Connects this TCP socket to the server
Christopher Haster 76:bbe51641f405 146 \param handle Socket handle
Christopher Haster 76:bbe51641f405 147 \param address SocketAddress to connect to
Christopher Haster 76:bbe51641f405 148 \return 0 on success, negative on failure
Christopher Haster 76:bbe51641f405 149 */
Christopher Haster 80:9c6673c93082 150 virtual int socket_connect(void *handle, const SocketAddress &address) = 0;
Christopher Haster 76:bbe51641f405 151
Christopher Haster 76:bbe51641f405 152 /** Check if the socket is connected
Christopher Haster 76:bbe51641f405 153 \param handle Socket handle
Christopher Haster 76:bbe51641f405 154 \return true if connected, false otherwise
Christopher Haster 76:bbe51641f405 155 */
Christopher Haster 80:9c6673c93082 156 virtual bool socket_is_connected(void *handle) = 0;
Christopher Haster 76:bbe51641f405 157
Christopher Haster 76:bbe51641f405 158 /** Send data to the remote host
Christopher Haster 76:bbe51641f405 159 \param handle Socket handle
Christopher Haster 76:bbe51641f405 160 \param data The buffer to send to the host
Christopher Haster 76:bbe51641f405 161 \param size The length of the buffer to send
Christopher Haster 76:bbe51641f405 162 \return Number of written bytes on success, negative on failure
Christopher Haster 76:bbe51641f405 163 */
Christopher Haster 76:bbe51641f405 164 virtual int socket_send(void *handle, const void *data, unsigned size) = 0;
Christopher Haster 76:bbe51641f405 165
Christopher Haster 76:bbe51641f405 166 /** Receive data from the remote host
Christopher Haster 76:bbe51641f405 167 \param handle Socket handle
Christopher Haster 76:bbe51641f405 168 \param data The buffer in which to store the data received from the host
Christopher Haster 76:bbe51641f405 169 \param size The maximum length of the buffer
Christopher Haster 76:bbe51641f405 170 \return Number of received bytes on success, negative on failure
Christopher Haster 76:bbe51641f405 171 */
Christopher Haster 76:bbe51641f405 172 virtual int socket_recv(void *handle, void *data, unsigned size) = 0;
Christopher Haster 76:bbe51641f405 173
Christopher Haster 76:bbe51641f405 174 /** Send a packet to a remote endpoint
Christopher Haster 76:bbe51641f405 175 \param handle Socket handle
Christopher Haster 76:bbe51641f405 176 \param address The remote SocketAddress
Christopher Haster 76:bbe51641f405 177 \param data The packet to be sent
Christopher Haster 76:bbe51641f405 178 \param size The length of the packet to be sent
Christopher Haster 76:bbe51641f405 179 \return the number of written bytes on success, negative on failure
Christopher Haster 76:bbe51641f405 180 */
Christopher Haster 80:9c6673c93082 181 virtual int socket_sendto(void *handle, const SocketAddress &address, const void *data, unsigned size) = 0;
Christopher Haster 76:bbe51641f405 182
Christopher Haster 76:bbe51641f405 183 /** Receive a packet from a remote endpoint
Christopher Haster 76:bbe51641f405 184 \param handle Socket handle
Christopher Haster 76:bbe51641f405 185 \param address Destination for the remote SocketAddress or null
Christopher Haster 76:bbe51641f405 186 \param buffer The buffer for storing the incoming packet data
Christopher Haster 76:bbe51641f405 187 If a packet is too long to fit in the supplied buffer,
Christopher Haster 76:bbe51641f405 188 excess bytes are discarded
Christopher Haster 76:bbe51641f405 189 \param size The length of the buffer
Christopher Haster 76:bbe51641f405 190 \return the number of received bytes on success, negative on failure
Christopher Haster 76:bbe51641f405 191 */
Christopher Haster 76:bbe51641f405 192 virtual int socket_recvfrom(void *handle, SocketAddress *address, void *buffer, unsigned size) = 0;
Christopher Haster 76:bbe51641f405 193
Christopher Haster 76:bbe51641f405 194 /** Close the socket
Christopher Haster 76:bbe51641f405 195 \param handle Socket handle
Christopher Haster 76:bbe51641f405 196 \param shutdown free the left-over data in message queues
Christopher Haster 76:bbe51641f405 197 */
Christopher Haster 76:bbe51641f405 198 virtual int socket_close(void *handle, bool shutdown) = 0;
Christopher Haster 76:bbe51641f405 199
Christopher Haster 76:bbe51641f405 200 /** Register a callback on when a new connection is ready
Christopher Haster 76:bbe51641f405 201 \param handle Socket handle
Christopher Haster 76:bbe51641f405 202 \param callback Function to call when accept will succeed, may be called in
Christopher Haster 76:bbe51641f405 203 interrupt context.
Christopher Haster 76:bbe51641f405 204 */
Christopher Haster 79:43a7e8c0d6cc 205 virtual void socket_attach_accept(void *handle, mbed::FuncPtr<void()> callback) = 0;
Christopher Haster 76:bbe51641f405 206
Christopher Haster 76:bbe51641f405 207 /** Register a callback on when send is ready
Christopher Haster 76:bbe51641f405 208 \param handle Socket handle
Christopher Haster 76:bbe51641f405 209 \param callback Function to call when send will succeed, may be called in
Christopher Haster 76:bbe51641f405 210 interrupt context.
Christopher Haster 76:bbe51641f405 211 */
Christopher Haster 79:43a7e8c0d6cc 212 virtual void socket_attach_send(void *handle, mbed::FuncPtr<void()> callback) = 0;
Christopher Haster 76:bbe51641f405 213
Christopher Haster 76:bbe51641f405 214 /** Register a callback on when recv is ready
Christopher Haster 76:bbe51641f405 215 \param handle Socket handle
Christopher Haster 76:bbe51641f405 216 \param callback Function to call when recv will succeed, may be called in
Christopher Haster 76:bbe51641f405 217 interrupt context.
Christopher Haster 76:bbe51641f405 218 */
Christopher Haster 79:43a7e8c0d6cc 219 virtual void socket_attach_recv(void *handle, mbed::FuncPtr<void()> callback) = 0;
Christopher Haster 76:bbe51641f405 220 };
Christopher Haster 76:bbe51641f405 221
Christopher Haster 76:bbe51641f405 222 #endif