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:
geky
Date:
Tue Apr 05 23:47:04 2016 +0000
Revision:
87:94e2cf3a06be
Parent:
80:9c6673c93082
Child:
89:b1d417383c0d
Refactored enums

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Christopher Haster 78:0914f9b9b24b 1 /* Socket
Christopher Haster 78:0914f9b9b24b 2 * Copyright (c) 2015 ARM Limited
Christopher Haster 75:dea0cdb42241 3 *
Christopher Haster 78:0914f9b9b24b 4 * Licensed under the Apache License, Version 2.0 (the "License");
Christopher Haster 78:0914f9b9b24b 5 * you may not use this file except in compliance with the License.
Christopher Haster 78:0914f9b9b24b 6 * You may obtain a copy of the License at
Christopher Haster 75:dea0cdb42241 7 *
Christopher Haster 78:0914f9b9b24b 8 * http://www.apache.org/licenses/LICENSE-2.0
Christopher Haster 75:dea0cdb42241 9 *
Christopher Haster 78:0914f9b9b24b 10 * Unless required by applicable law or agreed to in writing, software
Christopher Haster 78:0914f9b9b24b 11 * distributed under the License is distributed on an "AS IS" BASIS,
Christopher Haster 78:0914f9b9b24b 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Christopher Haster 78:0914f9b9b24b 13 * See the License for the specific language governing permissions and
Christopher Haster 78:0914f9b9b24b 14 * limitations under the License.
Christopher Haster 75:dea0cdb42241 15 */
Christopher Haster 78:0914f9b9b24b 16
Christopher Haster 76:bbe51641f405 17 #ifndef SOCKET_ADDRESS_H
Christopher Haster 76:bbe51641f405 18 #define SOCKET_ADDRESS_H
Christopher Haster 75:dea0cdb42241 19
Christopher Haster 79:43a7e8c0d6cc 20 #include <stdint.h>
Christopher Haster 79:43a7e8c0d6cc 21
geky 87:94e2cf3a06be 22 /** Maximum size of IP address
geky 87:94e2cf3a06be 23 */
geky 87:94e2cf3a06be 24 #define NSAPI_IP_SIZE 16
geky 87:94e2cf3a06be 25
geky 87:94e2cf3a06be 26 /** Maximum size of MAC address
geky 87:94e2cf3a06be 27 */
geky 87:94e2cf3a06be 28 #define NSAPI_MAC_SIZE 18
geky 87:94e2cf3a06be 29
Christopher Haster 80:9c6673c93082 30 // Predeclared classes
Christopher Haster 80:9c6673c93082 31 class NetworkInterface;
Christopher Haster 80:9c6673c93082 32
Christopher Haster 75:dea0cdb42241 33 /**
Christopher Haster 75:dea0cdb42241 34 * A general socket address composed of the IP address and port
Christopher Haster 75:dea0cdb42241 35 */
Christopher Haster 75:dea0cdb42241 36 class SocketAddress {
Christopher Haster 75:dea0cdb42241 37 public:
Christopher Haster 80:9c6673c93082 38 /** SocketAddress construction using DNS resolution
Christopher Haster 80:9c6673c93082 39 /param iface NetworkInterface to use for DNS resolution
Christopher Haster 80:9c6673c93082 40 /param addr Null-terminated hostname that will be resolved
Christopher Haster 80:9c6673c93082 41 /param port 16-bit port
Christopher Haster 80:9c6673c93082 42 /note on failure, IP address and port will be set to null
Christopher Haster 80:9c6673c93082 43 */
Christopher Haster 80:9c6673c93082 44 SocketAddress(NetworkInterface *iface, const char *addr, uint16_t port = 0);
Christopher Haster 80:9c6673c93082 45
Christopher Haster 79:43a7e8c0d6cc 46 /** SocketAddress construction
Christopher Haster 80:9c6673c93082 47 /param addr Null-terminated IP address
Christopher Haster 80:9c6673c93082 48 /param port 16-bit port
Christopher Haster 80:9c6673c93082 49 /note on failure, IP address and port will be set to null
Christopher Haster 80:9c6673c93082 50 */
Christopher Haster 76:bbe51641f405 51 SocketAddress(const char *addr = 0, uint16_t port = 0);
Christopher Haster 80:9c6673c93082 52
Christopher Haster 80:9c6673c93082 53 /** SocketAddress construction
Christopher Haster 80:9c6673c93082 54 /param addr SocketAddress to copy
Christopher Haster 80:9c6673c93082 55 */
Christopher Haster 80:9c6673c93082 56 SocketAddress(const SocketAddress &addr);
Christopher Haster 75:dea0cdb42241 57
Christopher Haster 75:dea0cdb42241 58 /** Set the IP address
Christopher Haster 80:9c6673c93082 59 \param addr Null-terminated string representing the IP address
Christopher Haster 75:dea0cdb42241 60 */
Christopher Haster 79:43a7e8c0d6cc 61 void set_ip_address(const char *addr);
Christopher Haster 75:dea0cdb42241 62
Christopher Haster 75:dea0cdb42241 63 /** Set the port
Christopher Haster 80:9c6673c93082 64 \param port 16-bit port
Christopher Haster 75:dea0cdb42241 65 */
Christopher Haster 75:dea0cdb42241 66 void set_port(uint16_t port);
Christopher Haster 75:dea0cdb42241 67
Christopher Haster 75:dea0cdb42241 68 /** Get the IP address
Christopher Haster 80:9c6673c93082 69 \return The string representation of the IP Address
Christopher Haster 75:dea0cdb42241 70 */
Christopher Haster 79:43a7e8c0d6cc 71 const char *get_ip_address() const;
Christopher Haster 75:dea0cdb42241 72
Christopher Haster 75:dea0cdb42241 73 /** Get the port
Christopher Haster 80:9c6673c93082 74 \return The 16-bit port
Christopher Haster 75:dea0cdb42241 75 */
Christopher Haster 79:43a7e8c0d6cc 76 uint16_t get_port(void) const;
Christopher Haster 79:43a7e8c0d6cc 77
Christopher Haster 79:43a7e8c0d6cc 78 private:
geky 87:94e2cf3a06be 79 char _ip_address[NSAPI_IP_SIZE];
Christopher Haster 79:43a7e8c0d6cc 80 uint16_t _port;
Christopher Haster 75:dea0cdb42241 81 };
Christopher Haster 75:dea0cdb42241 82
Christopher Haster 75:dea0cdb42241 83 #endif