NetworkSocketAPI
Dependents: HelloWizFi250Interface
Fork of NetworkSocketAPI by
NetworkInterface.h@78:0914f9b9b24b, 2016-04-05 (annotated)
- Committer:
- Christopher Haster
- Date:
- Tue Apr 05 09:20:32 2016 -0500
- Revision:
- 78:0914f9b9b24b
- Parent:
- 77:b66a6984ed2d
- Child:
- 79:43a7e8c0d6cc
Dragged over license from nsapi
Who changed what in which revision?
User | Revision | Line number | New 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 |
77:b66a6984ed2d | 20 | /** |
Christopher Haster |
77:b66a6984ed2d | 21 | * @enum ns_error_t |
Christopher Haster |
77:b66a6984ed2d | 22 | * @brief enum of standardized error codes |
Christopher Haster |
77:b66a6984ed2d | 23 | */ |
Christopher Haster |
77:b66a6984ed2d | 24 | enum ns_error_t { |
Christopher Haster |
77:b66a6984ed2d | 25 | NSAPI_ERROR_WOULD_BLOCK = -3001, /*!< no data is not available but call is non-blocking */ |
Christopher Haster |
77:b66a6984ed2d | 26 | NSAPI_ERROR_UNSUPPORTED = -3002, /*!< unsupported configuration */ |
Christopher Haster |
77:b66a6984ed2d | 27 | NSAPI_ERROR_NO_CONNECTION = -3003, /*!< not connected to a network */ |
Christopher Haster |
77:b66a6984ed2d | 28 | NSAPI_ERROR_NO_SOCKET = -3004, /*!< socket not available for use */ |
Christopher Haster |
77:b66a6984ed2d | 29 | NSAPI_ERROR_NO_ADDRESS = -3005, /*!< IP address is not known */ |
Christopher Haster |
77:b66a6984ed2d | 30 | NSAPI_ERROR_NO_MEMORY = -3006, /*!< memory resource not available */ |
Christopher Haster |
77:b66a6984ed2d | 31 | NSAPI_ERROR_DNS_FAILURE = -3007, /*!< DNS failed to complete successfully */ |
Christopher Haster |
77:b66a6984ed2d | 32 | NSAPI_ERROR_DHCP_FAILURE = -3008, /*!< DHCP failed to complete successfully */ |
Christopher Haster |
77:b66a6984ed2d | 33 | NSAPI_ERROR_AUTH_FAILURE = -3009, /*!< connection to access point faield */ |
Christopher Haster |
77:b66a6984ed2d | 34 | NSAPI_ERROR_DEVICE_ERROR = -3010, /*!< failure interfacing with the network procesor */ |
Christopher Haster |
77:b66a6984ed2d | 35 | }; |
Christopher Haster |
77:b66a6984ed2d | 36 | |
Christopher Haster |
77:b66a6984ed2d | 37 | /** |
Christopher Haster |
77:b66a6984ed2d | 38 | * @enum ns_opt_t |
Christopher Haster |
77:b66a6984ed2d | 39 | * @brief enum of available options |
Christopher Haster |
77:b66a6984ed2d | 40 | */ |
Christopher Haster |
77:b66a6984ed2d | 41 | enum ns_opt_t { |
Christopher Haster |
77:b66a6984ed2d | 42 | }; |
Christopher Haster |
77:b66a6984ed2d | 43 | |
Christopher Haster |
76:bbe51641f405 | 44 | /** NetworkInterface class |
Christopher Haster |
76:bbe51641f405 | 45 | * Common interface that is shared between all hardware that |
Christopher Haster |
76:bbe51641f405 | 46 | * can connect to a network over IP. |
Christopher Haster |
76:bbe51641f405 | 47 | */ |
Christopher Haster |
76:bbe51641f405 | 48 | class NetworkInterface |
Christopher Haster |
76:bbe51641f405 | 49 | { |
Christopher Haster |
76:bbe51641f405 | 50 | public: |
Christopher Haster |
76:bbe51641f405 | 51 | virtual ~NetworkInterface() {}; |
Christopher Haster |
76:bbe51641f405 | 52 | |
Christopher Haster |
76:bbe51641f405 | 53 | /** Get the internally stored IP address |
Christopher Haster |
76:bbe51641f405 | 54 | /return IP address of the interface or null if not yet connected |
Christopher Haster |
76:bbe51641f405 | 55 | */ |
Christopher Haster |
76:bbe51641f405 | 56 | virtual const char *get_ip_address() = 0; |
Christopher Haster |
76:bbe51641f405 | 57 | |
Christopher Haster |
76:bbe51641f405 | 58 | /** Get the internally stored MAC address |
Christopher Haster |
76:bbe51641f405 | 59 | /return MAC address of the interface |
Christopher Haster |
76:bbe51641f405 | 60 | */ |
Christopher Haster |
76:bbe51641f405 | 61 | virtual const char *get_mac_address() = 0; |
Christopher Haster |
76:bbe51641f405 | 62 | |
Christopher Haster |
76:bbe51641f405 | 63 | /** Get the current status of the interface |
Christopher Haster |
76:bbe51641f405 | 64 | /return true if connected |
Christopher Haster |
76:bbe51641f405 | 65 | */ |
Christopher Haster |
76:bbe51641f405 | 66 | virtual bool is_connected() { |
Christopher Haster |
76:bbe51641f405 | 67 | return get_ip_address() != NULL; |
Christopher Haster |
76:bbe51641f405 | 68 | } |
Christopher Haster |
76:bbe51641f405 | 69 | |
Christopher Haster |
76:bbe51641f405 | 70 | /** Looks up the specified host's IP address |
Christopher Haster |
76:bbe51641f405 | 71 | /param name Hostname to lookup |
Christopher Haster |
76:bbe51641f405 | 72 | /param port Optional port to pass to SocketAddress |
Christopher Haster |
76:bbe51641f405 | 73 | /return Resolved IP address, SocketAddress with null IP address on failure |
Christopher Haster |
76:bbe51641f405 | 74 | */ |
Christopher Haster |
76:bbe51641f405 | 75 | virtual SocketAddress gethostbyname(const char *name, uint16_t port=0); |
Christopher Haster |
76:bbe51641f405 | 76 | |
Christopher Haster |
76:bbe51641f405 | 77 | protected: |
Christopher Haster |
76:bbe51641f405 | 78 | /** Enum of socket protocols |
Christopher Haster |
76:bbe51641f405 | 79 | /enum protocol_t |
Christopher Haster |
76:bbe51641f405 | 80 | */ |
Christopher Haster |
76:bbe51641f405 | 81 | enum protocol_t { |
Christopher Haster |
76:bbe51641f405 | 82 | TCP, /*!< Socket is of TCP type */ |
Christopher Haster |
76:bbe51641f405 | 83 | UDP, /*!< Socket is of UDP type */ |
Christopher Haster |
76:bbe51641f405 | 84 | }; |
Christopher Haster |
76:bbe51641f405 | 85 | |
Christopher Haster |
76:bbe51641f405 | 86 | /** Create a socket |
Christopher Haster |
76:bbe51641f405 | 87 | /param proto The type of socket to open, TCP or UDP |
Christopher Haster |
76:bbe51641f405 | 88 | /return The alocated socket or null on failure |
Christopher Haster |
76:bbe51641f405 | 89 | */ |
Christopher Haster |
76:bbe51641f405 | 90 | virtual void *socket_create(protocol_t proto) = 0; |
Christopher Haster |
76:bbe51641f405 | 91 | |
Christopher Haster |
76:bbe51641f405 | 92 | /** Destroy a socket |
Christopher Haster |
76:bbe51641f405 | 93 | /param socket Previously allocated socket |
Christopher Haster |
76:bbe51641f405 | 94 | */ |
Christopher Haster |
76:bbe51641f405 | 95 | virtual void socket_destroy(void *handle) = 0; |
Christopher Haster |
76:bbe51641f405 | 96 | |
Christopher Haster |
76:bbe51641f405 | 97 | /** Set socket options |
Christopher Haster |
76:bbe51641f405 | 98 | \param handle Socket handle |
Christopher Haster |
76:bbe51641f405 | 99 | \param optname Option ID |
Christopher Haster |
76:bbe51641f405 | 100 | \param optval Option value |
Christopher Haster |
76:bbe51641f405 | 101 | \param optlen Length of the option value |
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_set_option(void *handle, int optname, const void *optval, unsigned int optlen) = 0; |
Christopher Haster |
76:bbe51641f405 | 105 | |
Christopher Haster |
76:bbe51641f405 | 106 | /** Get socket options |
Christopher Haster |
76:bbe51641f405 | 107 | \param handle Socket handle |
Christopher Haster |
76:bbe51641f405 | 108 | \param optname Option ID |
Christopher Haster |
76:bbe51641f405 | 109 | \param optval Buffer pointer where to write the option value |
Christopher Haster |
76:bbe51641f405 | 110 | \param optlen Length of the option value |
Christopher Haster |
76:bbe51641f405 | 111 | \return 0 on success, negative on failure |
Christopher Haster |
76:bbe51641f405 | 112 | */ |
Christopher Haster |
76:bbe51641f405 | 113 | virtual int socket_get_option(void *handle, int optname, void *optval, unsigned int *optlen) = 0; |
Christopher Haster |
76:bbe51641f405 | 114 | |
Christopher Haster |
76:bbe51641f405 | 115 | /** Bind a server socket to a specific port |
Christopher Haster |
76:bbe51641f405 | 116 | \param handle Socket handle |
Christopher Haster |
76:bbe51641f405 | 117 | \param port The port to listen for incoming connections on |
Christopher Haster |
76:bbe51641f405 | 118 | \return 0 on success, negative on failure. |
Christopher Haster |
76:bbe51641f405 | 119 | */ |
Christopher Haster |
76:bbe51641f405 | 120 | virtual int socket_bind(void *handle, int port) = 0; |
Christopher Haster |
76:bbe51641f405 | 121 | |
Christopher Haster |
76:bbe51641f405 | 122 | /** Start listening for incoming connections |
Christopher Haster |
76:bbe51641f405 | 123 | \param handle Socket handle |
Christopher Haster |
76:bbe51641f405 | 124 | \param backlog Number of pending connections that can be queued up at any |
Christopher Haster |
76:bbe51641f405 | 125 | one time [Default: 1] |
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_listen(void *handle, int backlog=1) = 0; |
Christopher Haster |
76:bbe51641f405 | 129 | |
Christopher Haster |
76:bbe51641f405 | 130 | /** Accept a new connection. |
Christopher Haster |
76:bbe51641f405 | 131 | \param handle Socket handle |
Christopher Haster |
76:bbe51641f405 | 132 | \param socket A TCPSocket instance that will handle the incoming connection. |
Christopher Haster |
76:bbe51641f405 | 133 | \return 0 on success, negative on failure. |
Christopher Haster |
76:bbe51641f405 | 134 | */ |
Christopher Haster |
76:bbe51641f405 | 135 | virtual int socket_accept(void *handle, void **connection) = 0; |
Christopher Haster |
76:bbe51641f405 | 136 | |
Christopher Haster |
76:bbe51641f405 | 137 | /** Connects this TCP socket to the server |
Christopher Haster |
76:bbe51641f405 | 138 | \param handle Socket handle |
Christopher Haster |
76:bbe51641f405 | 139 | \param address SocketAddress to connect to |
Christopher Haster |
76:bbe51641f405 | 140 | \return 0 on success, negative on failure |
Christopher Haster |
76:bbe51641f405 | 141 | */ |
Christopher Haster |
76:bbe51641f405 | 142 | virtual int socket_connect(void *handle, SocketAddress address) = 0; |
Christopher Haster |
76:bbe51641f405 | 143 | |
Christopher Haster |
76:bbe51641f405 | 144 | /** Check if the socket is connected |
Christopher Haster |
76:bbe51641f405 | 145 | \param handle Socket handle |
Christopher Haster |
76:bbe51641f405 | 146 | \return true if connected, false otherwise |
Christopher Haster |
76:bbe51641f405 | 147 | */ |
Christopher Haster |
76:bbe51641f405 | 148 | virtual bool socket_is_connected(void *handle); |
Christopher Haster |
76:bbe51641f405 | 149 | |
Christopher Haster |
76:bbe51641f405 | 150 | /** Send data to the remote host |
Christopher Haster |
76:bbe51641f405 | 151 | \param handle Socket handle |
Christopher Haster |
76:bbe51641f405 | 152 | \param data The buffer to send to the host |
Christopher Haster |
76:bbe51641f405 | 153 | \param size The length of the buffer to send |
Christopher Haster |
76:bbe51641f405 | 154 | \return Number of written bytes on success, negative on failure |
Christopher Haster |
76:bbe51641f405 | 155 | */ |
Christopher Haster |
76:bbe51641f405 | 156 | virtual int socket_send(void *handle, const void *data, unsigned size) = 0; |
Christopher Haster |
76:bbe51641f405 | 157 | |
Christopher Haster |
76:bbe51641f405 | 158 | /** Receive data from the remote host |
Christopher Haster |
76:bbe51641f405 | 159 | \param handle Socket handle |
Christopher Haster |
76:bbe51641f405 | 160 | \param data The buffer in which to store the data received from the host |
Christopher Haster |
76:bbe51641f405 | 161 | \param size The maximum length of the buffer |
Christopher Haster |
76:bbe51641f405 | 162 | \return Number of received bytes on success, negative on failure |
Christopher Haster |
76:bbe51641f405 | 163 | */ |
Christopher Haster |
76:bbe51641f405 | 164 | virtual int socket_recv(void *handle, void *data, unsigned size) = 0; |
Christopher Haster |
76:bbe51641f405 | 165 | |
Christopher Haster |
76:bbe51641f405 | 166 | /** Send a packet to a remote endpoint |
Christopher Haster |
76:bbe51641f405 | 167 | \param handle Socket handle |
Christopher Haster |
76:bbe51641f405 | 168 | \param address The remote SocketAddress |
Christopher Haster |
76:bbe51641f405 | 169 | \param data The packet to be sent |
Christopher Haster |
76:bbe51641f405 | 170 | \param size The length of the packet to be sent |
Christopher Haster |
76:bbe51641f405 | 171 | \return the number of written bytes on success, negative on failure |
Christopher Haster |
76:bbe51641f405 | 172 | */ |
Christopher Haster |
76:bbe51641f405 | 173 | virtual int socket_sendto(void *handle, SocketAddress address, const void *data, unsigned size) = 0; |
Christopher Haster |
76:bbe51641f405 | 174 | |
Christopher Haster |
76:bbe51641f405 | 175 | /** Receive a packet from a remote endpoint |
Christopher Haster |
76:bbe51641f405 | 176 | \param handle Socket handle |
Christopher Haster |
76:bbe51641f405 | 177 | \param address Destination for the remote SocketAddress or null |
Christopher Haster |
76:bbe51641f405 | 178 | \param buffer The buffer for storing the incoming packet data |
Christopher Haster |
76:bbe51641f405 | 179 | If a packet is too long to fit in the supplied buffer, |
Christopher Haster |
76:bbe51641f405 | 180 | excess bytes are discarded |
Christopher Haster |
76:bbe51641f405 | 181 | \param size The length of the buffer |
Christopher Haster |
76:bbe51641f405 | 182 | \return the number of received bytes on success, negative on failure |
Christopher Haster |
76:bbe51641f405 | 183 | */ |
Christopher Haster |
76:bbe51641f405 | 184 | virtual int socket_recvfrom(void *handle, SocketAddress *address, void *buffer, unsigned size) = 0; |
Christopher Haster |
76:bbe51641f405 | 185 | |
Christopher Haster |
76:bbe51641f405 | 186 | /** Close the socket |
Christopher Haster |
76:bbe51641f405 | 187 | \param handle Socket handle |
Christopher Haster |
76:bbe51641f405 | 188 | \param shutdown free the left-over data in message queues |
Christopher Haster |
76:bbe51641f405 | 189 | */ |
Christopher Haster |
76:bbe51641f405 | 190 | virtual int socket_close(void *handle, bool shutdown) = 0; |
Christopher Haster |
76:bbe51641f405 | 191 | |
Christopher Haster |
76:bbe51641f405 | 192 | /** Register a callback on when a new connection is ready |
Christopher Haster |
76:bbe51641f405 | 193 | \param handle Socket handle |
Christopher Haster |
76:bbe51641f405 | 194 | \param callback Function to call when accept will succeed, may be called in |
Christopher Haster |
76:bbe51641f405 | 195 | interrupt context. |
Christopher Haster |
76:bbe51641f405 | 196 | */ |
Christopher Haster |
76:bbe51641f405 | 197 | virtual void socket_attach_accept(void *handle, FuncPtr<void()> callback) = 0; |
Christopher Haster |
76:bbe51641f405 | 198 | |
Christopher Haster |
76:bbe51641f405 | 199 | /** Register a callback on when send is ready |
Christopher Haster |
76:bbe51641f405 | 200 | \param handle Socket handle |
Christopher Haster |
76:bbe51641f405 | 201 | \param callback Function to call when send will succeed, may be called in |
Christopher Haster |
76:bbe51641f405 | 202 | interrupt context. |
Christopher Haster |
76:bbe51641f405 | 203 | */ |
Christopher Haster |
76:bbe51641f405 | 204 | virtual void socket_attach_send(void *handle, FuncPtr<void()> callback) = 0; |
Christopher Haster |
76:bbe51641f405 | 205 | |
Christopher Haster |
76:bbe51641f405 | 206 | /** Register a callback on when recv is ready |
Christopher Haster |
76:bbe51641f405 | 207 | \param handle Socket handle |
Christopher Haster |
76:bbe51641f405 | 208 | \param callback Function to call when recv will succeed, may be called in |
Christopher Haster |
76:bbe51641f405 | 209 | interrupt context. |
Christopher Haster |
76:bbe51641f405 | 210 | */ |
Christopher Haster |
76:bbe51641f405 | 211 | virtual void socket_attach_recv(void *handle, FuncPtr<void()> callback) = 0; |
Christopher Haster |
76:bbe51641f405 | 212 | }; |
Christopher Haster |
76:bbe51641f405 | 213 | |
Christopher Haster |
76:bbe51641f405 | 214 | #endif |