NetworkSocketAPI

Dependencies:   DnsQuery

Dependents:   HelloWizFi250Interface

Fork of NetworkSocketAPI by NetworkSocketAPI

Committer:
Christopher Haster
Date:
Tue Apr 05 09:07:28 2016 -0500
Revision:
76:bbe51641f405
Child:
77:b66a6984ed2d
Added rough NetworkInterface

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Christopher Haster 76:bbe51641f405 1 /* NetworkInterface Base Class
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 76:bbe51641f405 20 /** NetworkInterface class
Christopher Haster 76:bbe51641f405 21 * Common interface that is shared between all hardware that
Christopher Haster 76:bbe51641f405 22 * can connect to a network over IP.
Christopher Haster 76:bbe51641f405 23 */
Christopher Haster 76:bbe51641f405 24 class NetworkInterface
Christopher Haster 76:bbe51641f405 25 {
Christopher Haster 76:bbe51641f405 26 public:
Christopher Haster 76:bbe51641f405 27 virtual ~NetworkInterface() {};
Christopher Haster 76:bbe51641f405 28
Christopher Haster 76:bbe51641f405 29 /** Get the internally stored IP address
Christopher Haster 76:bbe51641f405 30 /return IP address of the interface or null if not yet connected
Christopher Haster 76:bbe51641f405 31 */
Christopher Haster 76:bbe51641f405 32 virtual const char *get_ip_address() = 0;
Christopher Haster 76:bbe51641f405 33
Christopher Haster 76:bbe51641f405 34 /** Get the internally stored MAC address
Christopher Haster 76:bbe51641f405 35 /return MAC address of the interface
Christopher Haster 76:bbe51641f405 36 */
Christopher Haster 76:bbe51641f405 37 virtual const char *get_mac_address() = 0;
Christopher Haster 76:bbe51641f405 38
Christopher Haster 76:bbe51641f405 39 /** Get the current status of the interface
Christopher Haster 76:bbe51641f405 40 /return true if connected
Christopher Haster 76:bbe51641f405 41 */
Christopher Haster 76:bbe51641f405 42 virtual bool is_connected() {
Christopher Haster 76:bbe51641f405 43 return get_ip_address() != NULL;
Christopher Haster 76:bbe51641f405 44 }
Christopher Haster 76:bbe51641f405 45
Christopher Haster 76:bbe51641f405 46 /** Looks up the specified host's IP address
Christopher Haster 76:bbe51641f405 47 /param name Hostname to lookup
Christopher Haster 76:bbe51641f405 48 /param port Optional port to pass to SocketAddress
Christopher Haster 76:bbe51641f405 49 /return Resolved IP address, SocketAddress with null IP address on failure
Christopher Haster 76:bbe51641f405 50 */
Christopher Haster 76:bbe51641f405 51 virtual SocketAddress gethostbyname(const char *name, uint16_t port=0);
Christopher Haster 76:bbe51641f405 52
Christopher Haster 76:bbe51641f405 53 protected:
Christopher Haster 76:bbe51641f405 54 /** Enum of socket protocols
Christopher Haster 76:bbe51641f405 55 /enum protocol_t
Christopher Haster 76:bbe51641f405 56 */
Christopher Haster 76:bbe51641f405 57 enum protocol_t {
Christopher Haster 76:bbe51641f405 58 TCP, /*!< Socket is of TCP type */
Christopher Haster 76:bbe51641f405 59 UDP, /*!< Socket is of UDP type */
Christopher Haster 76:bbe51641f405 60 };
Christopher Haster 76:bbe51641f405 61
Christopher Haster 76:bbe51641f405 62 /** Create a socket
Christopher Haster 76:bbe51641f405 63 /param proto The type of socket to open, TCP or UDP
Christopher Haster 76:bbe51641f405 64 /return The alocated socket or null on failure
Christopher Haster 76:bbe51641f405 65 */
Christopher Haster 76:bbe51641f405 66 virtual void *socket_create(protocol_t proto) = 0;
Christopher Haster 76:bbe51641f405 67
Christopher Haster 76:bbe51641f405 68 /** Destroy a socket
Christopher Haster 76:bbe51641f405 69 /param socket Previously allocated socket
Christopher Haster 76:bbe51641f405 70 */
Christopher Haster 76:bbe51641f405 71 virtual void socket_destroy(void *handle) = 0;
Christopher Haster 76:bbe51641f405 72
Christopher Haster 76:bbe51641f405 73 /** Set socket options
Christopher Haster 76:bbe51641f405 74 \param handle Socket handle
Christopher Haster 76:bbe51641f405 75 \param optname Option ID
Christopher Haster 76:bbe51641f405 76 \param optval Option value
Christopher Haster 76:bbe51641f405 77 \param optlen Length of the option value
Christopher Haster 76:bbe51641f405 78 \return 0 on success, negative on failure
Christopher Haster 76:bbe51641f405 79 */
Christopher Haster 76:bbe51641f405 80 virtual int socket_set_option(void *handle, int optname, const void *optval, unsigned int optlen) = 0;
Christopher Haster 76:bbe51641f405 81
Christopher Haster 76:bbe51641f405 82 /** Get socket options
Christopher Haster 76:bbe51641f405 83 \param handle Socket handle
Christopher Haster 76:bbe51641f405 84 \param optname Option ID
Christopher Haster 76:bbe51641f405 85 \param optval Buffer pointer where to write the option value
Christopher Haster 76:bbe51641f405 86 \param optlen Length of the option value
Christopher Haster 76:bbe51641f405 87 \return 0 on success, negative on failure
Christopher Haster 76:bbe51641f405 88 */
Christopher Haster 76:bbe51641f405 89 virtual int socket_get_option(void *handle, int optname, void *optval, unsigned int *optlen) = 0;
Christopher Haster 76:bbe51641f405 90
Christopher Haster 76:bbe51641f405 91 /** Bind a server socket to a specific port
Christopher Haster 76:bbe51641f405 92 \param handle Socket handle
Christopher Haster 76:bbe51641f405 93 \param port The port to listen for incoming connections on
Christopher Haster 76:bbe51641f405 94 \return 0 on success, negative on failure.
Christopher Haster 76:bbe51641f405 95 */
Christopher Haster 76:bbe51641f405 96 virtual int socket_bind(void *handle, int port) = 0;
Christopher Haster 76:bbe51641f405 97
Christopher Haster 76:bbe51641f405 98 /** Start listening for incoming connections
Christopher Haster 76:bbe51641f405 99 \param handle Socket handle
Christopher Haster 76:bbe51641f405 100 \param backlog Number of pending connections that can be queued up at any
Christopher Haster 76:bbe51641f405 101 one time [Default: 1]
Christopher Haster 76:bbe51641f405 102 \return 0 on success, negative on failure
Christopher Haster 76:bbe51641f405 103 */
Christopher Haster 76:bbe51641f405 104 virtual int socket_listen(void *handle, int backlog=1) = 0;
Christopher Haster 76:bbe51641f405 105
Christopher Haster 76:bbe51641f405 106 /** Accept a new connection.
Christopher Haster 76:bbe51641f405 107 \param handle Socket handle
Christopher Haster 76:bbe51641f405 108 \param socket A TCPSocket instance that will handle the incoming connection.
Christopher Haster 76:bbe51641f405 109 \return 0 on success, negative on failure.
Christopher Haster 76:bbe51641f405 110 */
Christopher Haster 76:bbe51641f405 111 virtual int socket_accept(void *handle, void **connection) = 0;
Christopher Haster 76:bbe51641f405 112
Christopher Haster 76:bbe51641f405 113 /** Connects this TCP socket to the server
Christopher Haster 76:bbe51641f405 114 \param handle Socket handle
Christopher Haster 76:bbe51641f405 115 \param address SocketAddress to connect to
Christopher Haster 76:bbe51641f405 116 \return 0 on success, negative on failure
Christopher Haster 76:bbe51641f405 117 */
Christopher Haster 76:bbe51641f405 118 virtual int socket_connect(void *handle, SocketAddress address) = 0;
Christopher Haster 76:bbe51641f405 119
Christopher Haster 76:bbe51641f405 120 /** Check if the socket is connected
Christopher Haster 76:bbe51641f405 121 \param handle Socket handle
Christopher Haster 76:bbe51641f405 122 \return true if connected, false otherwise
Christopher Haster 76:bbe51641f405 123 */
Christopher Haster 76:bbe51641f405 124 virtual bool socket_is_connected(void *handle);
Christopher Haster 76:bbe51641f405 125
Christopher Haster 76:bbe51641f405 126 /** Send data to the remote host
Christopher Haster 76:bbe51641f405 127 \param handle Socket handle
Christopher Haster 76:bbe51641f405 128 \param data The buffer to send to the host
Christopher Haster 76:bbe51641f405 129 \param size The length of the buffer to send
Christopher Haster 76:bbe51641f405 130 \return Number of written bytes on success, negative on failure
Christopher Haster 76:bbe51641f405 131 */
Christopher Haster 76:bbe51641f405 132 virtual int socket_send(void *handle, const void *data, unsigned size) = 0;
Christopher Haster 76:bbe51641f405 133
Christopher Haster 76:bbe51641f405 134 /** Receive data from the remote host
Christopher Haster 76:bbe51641f405 135 \param handle Socket handle
Christopher Haster 76:bbe51641f405 136 \param data The buffer in which to store the data received from the host
Christopher Haster 76:bbe51641f405 137 \param size The maximum length of the buffer
Christopher Haster 76:bbe51641f405 138 \return Number of received bytes on success, negative on failure
Christopher Haster 76:bbe51641f405 139 */
Christopher Haster 76:bbe51641f405 140 virtual int socket_recv(void *handle, void *data, unsigned size) = 0;
Christopher Haster 76:bbe51641f405 141
Christopher Haster 76:bbe51641f405 142 /** Send a packet to a remote endpoint
Christopher Haster 76:bbe51641f405 143 \param handle Socket handle
Christopher Haster 76:bbe51641f405 144 \param address The remote SocketAddress
Christopher Haster 76:bbe51641f405 145 \param data The packet to be sent
Christopher Haster 76:bbe51641f405 146 \param size The length of the packet to be sent
Christopher Haster 76:bbe51641f405 147 \return the number of written bytes on success, negative on failure
Christopher Haster 76:bbe51641f405 148 */
Christopher Haster 76:bbe51641f405 149 virtual int socket_sendto(void *handle, SocketAddress address, const void *data, unsigned size) = 0;
Christopher Haster 76:bbe51641f405 150
Christopher Haster 76:bbe51641f405 151 /** Receive a packet from a remote endpoint
Christopher Haster 76:bbe51641f405 152 \param handle Socket handle
Christopher Haster 76:bbe51641f405 153 \param address Destination for the remote SocketAddress or null
Christopher Haster 76:bbe51641f405 154 \param buffer The buffer for storing the incoming packet data
Christopher Haster 76:bbe51641f405 155 If a packet is too long to fit in the supplied buffer,
Christopher Haster 76:bbe51641f405 156 excess bytes are discarded
Christopher Haster 76:bbe51641f405 157 \param size The length of the buffer
Christopher Haster 76:bbe51641f405 158 \return the number of received bytes on success, negative on failure
Christopher Haster 76:bbe51641f405 159 */
Christopher Haster 76:bbe51641f405 160 virtual int socket_recvfrom(void *handle, SocketAddress *address, void *buffer, unsigned size) = 0;
Christopher Haster 76:bbe51641f405 161
Christopher Haster 76:bbe51641f405 162 /** Close the socket
Christopher Haster 76:bbe51641f405 163 \param handle Socket handle
Christopher Haster 76:bbe51641f405 164 \param shutdown free the left-over data in message queues
Christopher Haster 76:bbe51641f405 165 */
Christopher Haster 76:bbe51641f405 166 virtual int socket_close(void *handle, bool shutdown) = 0;
Christopher Haster 76:bbe51641f405 167
Christopher Haster 76:bbe51641f405 168 /** Register a callback on when a new connection is ready
Christopher Haster 76:bbe51641f405 169 \param handle Socket handle
Christopher Haster 76:bbe51641f405 170 \param callback Function to call when accept will succeed, may be called in
Christopher Haster 76:bbe51641f405 171 interrupt context.
Christopher Haster 76:bbe51641f405 172 */
Christopher Haster 76:bbe51641f405 173 virtual void socket_attach_accept(void *handle, FuncPtr<void()> callback) = 0;
Christopher Haster 76:bbe51641f405 174
Christopher Haster 76:bbe51641f405 175 /** Register a callback on when send is ready
Christopher Haster 76:bbe51641f405 176 \param handle Socket handle
Christopher Haster 76:bbe51641f405 177 \param callback Function to call when send will succeed, may be called in
Christopher Haster 76:bbe51641f405 178 interrupt context.
Christopher Haster 76:bbe51641f405 179 */
Christopher Haster 76:bbe51641f405 180 virtual void socket_attach_send(void *handle, FuncPtr<void()> callback) = 0;
Christopher Haster 76:bbe51641f405 181
Christopher Haster 76:bbe51641f405 182 /** Register a callback on when recv is ready
Christopher Haster 76:bbe51641f405 183 \param handle Socket handle
Christopher Haster 76:bbe51641f405 184 \param callback Function to call when recv will succeed, may be called in
Christopher Haster 76:bbe51641f405 185 interrupt context.
Christopher Haster 76:bbe51641f405 186 */
Christopher Haster 76:bbe51641f405 187 virtual void socket_attach_recv(void *handle, FuncPtr<void()> callback) = 0;
Christopher Haster 76:bbe51641f405 188 };
Christopher Haster 76:bbe51641f405 189
Christopher Haster 76:bbe51641f405 190 #endif