Base class for IP Based Networking Libraries

Dependencies:   DnsQuery

Dependents:   TempTower BSDInterfaceTests HelloBSDInterface ESP8266InterfaceTests ... more

For a complete getting started guide see the wiki...

Network Socket API

The Network Socket API provides a common interface for using sockets on network devices. The API provides a simple class-based interface that should be familiar to users experienced with other socket APIs. Additionally, the API provides a simple interface for implementing network devices, making it easy to connect hardware agnostic programs to new devices.

Network Interfaces

The NetworkInterface provides an abstract class for network devices that support sockets. Devices should provide a DeviceInterface class that inherits this interface and adds implementation specific methods for using the device. A NetworkInterface must be provided to a Socket constructor to open a socket on the interface. Currently two subclasses are defined for common devices, EthernetInterface and WiFiInterface.

Sockets

The Socket class is used for managing network sockets. Once opened, the socket provides a pipe through which data can sent and recieved to a specific endpoint. The socket class can be instantiated as either a TCPSocket or a UDPSocket which defines the protocol used for the connection.

Committer:
Christopher Haster
Date:
Thu Feb 18 06:01:55 2016 -0600
Branch:
api-changes
Revision:
25:ed7b2a52e8ac
Child:
27:d7ed39727306
Added abstract base case for Sockets

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Christopher Haster 25:ed7b2a52e8ac 1 /* Socket
Christopher Haster 25:ed7b2a52e8ac 2 * Copyright (c) 2015 ARM Limited
Christopher Haster 25:ed7b2a52e8ac 3 *
Christopher Haster 25:ed7b2a52e8ac 4 * Licensed under the Apache License, Version 2.0 (the "License");
Christopher Haster 25:ed7b2a52e8ac 5 * you may not use this file except in compliance with the License.
Christopher Haster 25:ed7b2a52e8ac 6 * You may obtain a copy of the License at
Christopher Haster 25:ed7b2a52e8ac 7 *
Christopher Haster 25:ed7b2a52e8ac 8 * http://www.apache.org/licenses/LICENSE-2.0
Christopher Haster 25:ed7b2a52e8ac 9 *
Christopher Haster 25:ed7b2a52e8ac 10 * Unless required by applicable law or agreed to in writing, software
Christopher Haster 25:ed7b2a52e8ac 11 * distributed under the License is distributed on an "AS IS" BASIS,
Christopher Haster 25:ed7b2a52e8ac 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Christopher Haster 25:ed7b2a52e8ac 13 * See the License for the specific language governing permissions and
Christopher Haster 25:ed7b2a52e8ac 14 * limitations under the License.
Christopher Haster 25:ed7b2a52e8ac 15 */
Christopher Haster 25:ed7b2a52e8ac 16
Christopher Haster 25:ed7b2a52e8ac 17 #ifndef SOCKET_H
Christopher Haster 25:ed7b2a52e8ac 18 #define SOCKET_H
Christopher Haster 25:ed7b2a52e8ac 19
Christopher Haster 25:ed7b2a52e8ac 20 #include "NetworkInterface.h"
Christopher Haster 25:ed7b2a52e8ac 21
Christopher Haster 25:ed7b2a52e8ac 22 /** Abstract socket class
Christopher Haster 25:ed7b2a52e8ac 23 * API for handling general sockets. Supports IP address operations
Christopher Haster 25:ed7b2a52e8ac 24 * and sending/recieving data.
Christopher Haster 25:ed7b2a52e8ac 25 */
Christopher Haster 25:ed7b2a52e8ac 26 class Socket
Christopher Haster 25:ed7b2a52e8ac 27 {
Christopher Haster 25:ed7b2a52e8ac 28 public:
Christopher Haster 25:ed7b2a52e8ac 29 /** Set the URL of the socket
Christopher Haster 25:ed7b2a52e8ac 30 * Performs DNS lookup if necessary
Christopher Haster 25:ed7b2a52e8ac 31 * @param url URL to connect to
Christopher Haster 25:ed7b2a52e8ac 32 */
Christopher Haster 25:ed7b2a52e8ac 33 void setURL(const char *url);
Christopher Haster 25:ed7b2a52e8ac 34
Christopher Haster 25:ed7b2a52e8ac 35 /** Set the IP address of the socket
Christopher Haster 25:ed7b2a52e8ac 36 * @param ip IP address to connect to, copied internally
Christopher Haster 25:ed7b2a52e8ac 37 */
Christopher Haster 25:ed7b2a52e8ac 38 void setIPAddress(const char *ip);
Christopher Haster 25:ed7b2a52e8ac 39
Christopher Haster 25:ed7b2a52e8ac 40 /** Set the port of the socket
Christopher Haster 25:ed7b2a52e8ac 41 * @param port Port to connect to
Christopher Haster 25:ed7b2a52e8ac 42 */
Christopher Haster 25:ed7b2a52e8ac 43 void setPort(uint16_t port);
Christopher Haster 25:ed7b2a52e8ac 44
Christopher Haster 25:ed7b2a52e8ac 45 /** Gets the IP address
Christopher Haster 25:ed7b2a52e8ac 46 * @return IP address to connect to
Christopher Haster 25:ed7b2a52e8ac 47 */
Christopher Haster 25:ed7b2a52e8ac 48 const char *getIPAddress();
Christopher Haster 25:ed7b2a52e8ac 49
Christopher Haster 25:ed7b2a52e8ac 50 /** Gets the port
Christopher Haster 25:ed7b2a52e8ac 51 * @return Port to connect to
Christopher Haster 25:ed7b2a52e8ac 52 */
Christopher Haster 25:ed7b2a52e8ac 53 uint16_t getPort();
Christopher Haster 25:ed7b2a52e8ac 54
Christopher Haster 25:ed7b2a52e8ac 55
Christopher Haster 25:ed7b2a52e8ac 56 /** Send data over the socket
Christopher Haster 25:ed7b2a52e8ac 57 * @param data Buffer of data to send
Christopher Haster 25:ed7b2a52e8ac 58 * @param len Size of data to send
Christopher Haster 25:ed7b2a52e8ac 59 * @param timeout_ms Maximum amount of time to wait
Christopher Haster 25:ed7b2a52e8ac 60 * @return 0 on success
Christopher Haster 25:ed7b2a52e8ac 61 */
Christopher Haster 25:ed7b2a52e8ac 62 int32_t send(const void *data, uint32_t len, uint32_t timeout_ms = 15000);
Christopher Haster 25:ed7b2a52e8ac 63
Christopher Haster 25:ed7b2a52e8ac 64 /** Recieve data over the socket
Christopher Haster 25:ed7b2a52e8ac 65 * @param data Buffer to store recieved data
Christopher Haster 25:ed7b2a52e8ac 66 * @param len Size of provided buffer
Christopher Haster 25:ed7b2a52e8ac 67 * @param timeout_ms Maximum amount of time to wait
Christopher Haster 25:ed7b2a52e8ac 68 * @return Number of bytes sent or a negative value on failure
Christopher Haster 25:ed7b2a52e8ac 69 */
Christopher Haster 25:ed7b2a52e8ac 70 int32_t recv(void *data, uint32_t len, uint32_t timeout_ms = 15000);
Christopher Haster 25:ed7b2a52e8ac 71
Christopher Haster 25:ed7b2a52e8ac 72
Christopher Haster 25:ed7b2a52e8ac 73 protected:
Christopher Haster 25:ed7b2a52e8ac 74 Socket( NetworkInterface *iface,
Christopher Haster 25:ed7b2a52e8ac 75 socket_protocol_t proto,
Christopher Haster 25:ed7b2a52e8ac 76 const char *url = 0,
Christopher Haster 25:ed7b2a52e8ac 77 uint16_t port = 0);
Christopher Haster 25:ed7b2a52e8ac 78 ~Socket();
Christopher Haster 25:ed7b2a52e8ac 79
Christopher Haster 25:ed7b2a52e8ac 80 SocketInterface *_get_socket();
Christopher Haster 25:ed7b2a52e8ac 81
Christopher Haster 25:ed7b2a52e8ac 82 private:
Christopher Haster 25:ed7b2a52e8ac 83 NetworkInterface *_iface;
Christopher Haster 25:ed7b2a52e8ac 84 socket_protocol_t _proto;
Christopher Haster 25:ed7b2a52e8ac 85 SocketInterface *_socket;
Christopher Haster 25:ed7b2a52e8ac 86 };
Christopher Haster 25:ed7b2a52e8ac 87
Christopher Haster 25:ed7b2a52e8ac 88 #endif