sandbox / NetworkSocketAPI

Dependencies:   DnsQuery

Fork of NetworkSocketAPI by Christopher Haster

Committer:
Christopher Haster
Date:
Tue Apr 05 10:40:34 2016 -0500
Revision:
7:43a7e8c0d6cc
Parent:
6:0914f9b9b24b
Child:
8:9c6673c93082
Added rough implementation of the API shim

Who changed what in which revision?

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