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 25 19:00:39 2016 -0600
Revision:
57:3c873fab4207
Parent:
54:0c764f654352
Child:
58:1caa187fa5af
Standardized prefix to NS for enums and constants

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 #include "Socket.h"
geky 29:7bcec3189a93 18 #include <string.h>
Christopher Haster 25:ed7b2a52e8ac 19
Christopher Haster 57:3c873fab4207 20 Socket::Socket(NetworkInterface *iface, ns_protocol_t proto)
geky 30:3cc78f5db99d 21 : _iface(iface)
geky 30:3cc78f5db99d 22 , _proto(proto)
geky 30:3cc78f5db99d 23 , _socket(0)
geky 30:3cc78f5db99d 24 {
Christopher Haster 42:49893d13c432 25 memset(_ip_address, 0, NS_IP_SIZE);
Christopher Haster 28:163fbe3263f4 26 _port = 0;
Christopher Haster 25:ed7b2a52e8ac 27 }
Christopher Haster 25:ed7b2a52e8ac 28
geky 30:3cc78f5db99d 29 Socket::~Socket()
geky 30:3cc78f5db99d 30 {
Christopher Haster 43:09ea32f2eb54 31 if (_socket) {
Christopher Haster 43:09ea32f2eb54 32 close();
Christopher Haster 43:09ea32f2eb54 33 }
Christopher Haster 25:ed7b2a52e8ac 34 }
Christopher Haster 25:ed7b2a52e8ac 35
Christopher Haster 34:c17745683385 36 int32_t Socket::setURL(const char *url)
geky 30:3cc78f5db99d 37 {
geky 31:7f15b95f2a1d 38 int32_t err = _iface->getHostByName(url, _ip_address);
Christopher Haster 43:09ea32f2eb54 39 if (err) {
Christopher Haster 43:09ea32f2eb54 40 return err;
Christopher Haster 43:09ea32f2eb54 41 }
geky 31:7f15b95f2a1d 42
geky 31:7f15b95f2a1d 43 if (_socket) {
geky 31:7f15b95f2a1d 44 _socket->setIPAddress(_ip_address);
geky 31:7f15b95f2a1d 45 }
Christopher Haster 28:163fbe3263f4 46
Christopher Haster 28:163fbe3263f4 47 return 0;
Christopher Haster 25:ed7b2a52e8ac 48 }
Christopher Haster 25:ed7b2a52e8ac 49
Christopher Haster 34:c17745683385 50 void Socket::setIPAddress(const char *ip)
geky 30:3cc78f5db99d 51 {
Christopher Haster 28:163fbe3263f4 52 strcpy(_ip_address, ip);
Christopher Haster 28:163fbe3263f4 53
Christopher Haster 28:163fbe3263f4 54 if (_socket) {
geky 31:7f15b95f2a1d 55 _socket->setIPAddress(_ip_address);
Christopher Haster 27:d7ed39727306 56 }
Christopher Haster 25:ed7b2a52e8ac 57 }
Christopher Haster 25:ed7b2a52e8ac 58
geky 30:3cc78f5db99d 59 void Socket::setPort(uint16_t port)
geky 30:3cc78f5db99d 60 {
Christopher Haster 28:163fbe3263f4 61 _port = port;
Christopher Haster 28:163fbe3263f4 62
Christopher Haster 28:163fbe3263f4 63 if (_socket) {
geky 31:7f15b95f2a1d 64 _socket->setPort(_port);
Christopher Haster 27:d7ed39727306 65 }
Christopher Haster 25:ed7b2a52e8ac 66 }
Christopher Haster 25:ed7b2a52e8ac 67
geky 30:3cc78f5db99d 68 const char *Socket::getIPAddress() const
geky 30:3cc78f5db99d 69 {
Christopher Haster 39:47138420ea42 70 if (_ip_address[0]) {
Christopher Haster 39:47138420ea42 71 return _ip_address;
Christopher Haster 39:47138420ea42 72 } else {
Christopher Haster 39:47138420ea42 73 return 0;
Christopher Haster 39:47138420ea42 74 }
Christopher Haster 25:ed7b2a52e8ac 75 }
Christopher Haster 25:ed7b2a52e8ac 76
geky 30:3cc78f5db99d 77 uint16_t Socket::getPort() const
geky 30:3cc78f5db99d 78 {
Christopher Haster 28:163fbe3263f4 79 return _port;
Christopher Haster 25:ed7b2a52e8ac 80 }
Christopher Haster 25:ed7b2a52e8ac 81
Christopher Haster 38:157fb2ab965f 82 bool Socket::isConnected()
Christopher Haster 38:157fb2ab965f 83 {
Christopher Haster 38:157fb2ab965f 84 if (!_socket) {
Christopher Haster 38:157fb2ab965f 85 return false;
Christopher Haster 38:157fb2ab965f 86 }
Christopher Haster 38:157fb2ab965f 87
Christopher Haster 38:157fb2ab965f 88 return _socket->isConnected();
Christopher Haster 38:157fb2ab965f 89 }
Christopher Haster 38:157fb2ab965f 90
Christopher Haster 43:09ea32f2eb54 91 int32_t Socket::open(const char *address, uint16_t port)
Christopher Haster 32:2c5fc105fc50 92 {
Christopher Haster 41:3ec1c97e9bbf 93 int32_t err;
Christopher Haster 41:3ec1c97e9bbf 94
Christopher Haster 41:3ec1c97e9bbf 95 err = close();
Christopher Haster 43:09ea32f2eb54 96 if (err) {
Christopher Haster 43:09ea32f2eb54 97 return err;
Christopher Haster 43:09ea32f2eb54 98 }
Christopher Haster 34:c17745683385 99
Christopher Haster 43:09ea32f2eb54 100 if (address) {
Christopher Haster 43:09ea32f2eb54 101 err = setURL(address);
Christopher Haster 43:09ea32f2eb54 102 if (err) {
Christopher Haster 43:09ea32f2eb54 103 return err;
Christopher Haster 43:09ea32f2eb54 104 }
Christopher Haster 34:c17745683385 105 }
Christopher Haster 34:c17745683385 106
Christopher Haster 34:c17745683385 107 if (port) {
Christopher Haster 34:c17745683385 108 setPort(port);
Christopher Haster 34:c17745683385 109 }
Christopher Haster 34:c17745683385 110
Christopher Haster 39:47138420ea42 111 if (!getIPAddress() || !getPort()) {
Christopher Haster 45:c8aca7c1e93f 112 return NS_ERROR_NO_ADDRESS;
Christopher Haster 35:838393fbc2ca 113 }
Christopher Haster 32:2c5fc105fc50 114
Christopher Haster 32:2c5fc105fc50 115 _socket = _iface->createSocket(_proto);
Christopher Haster 43:09ea32f2eb54 116 if (!_socket) {
Christopher Haster 45:c8aca7c1e93f 117 return NS_ERROR_NO_SOCKET;
Christopher Haster 43:09ea32f2eb54 118 }
Christopher Haster 41:3ec1c97e9bbf 119
Christopher Haster 41:3ec1c97e9bbf 120 err = _socket->open(_ip_address, _port);
Christopher Haster 34:c17745683385 121
Christopher Haster 32:2c5fc105fc50 122 if (err) {
Christopher Haster 32:2c5fc105fc50 123 _iface->destroySocket(_socket);
Christopher Haster 54:0c764f654352 124 _socket = 0;
Christopher Haster 32:2c5fc105fc50 125 }
Christopher Haster 32:2c5fc105fc50 126
Christopher Haster 32:2c5fc105fc50 127 return err;
Christopher Haster 32:2c5fc105fc50 128 }
Christopher Haster 32:2c5fc105fc50 129
Christopher Haster 32:2c5fc105fc50 130 int32_t Socket::close()
Christopher Haster 32:2c5fc105fc50 131 {
Christopher Haster 43:09ea32f2eb54 132 if (!_socket) {
Christopher Haster 43:09ea32f2eb54 133 return 0;
Christopher Haster 43:09ea32f2eb54 134 }
Christopher Haster 32:2c5fc105fc50 135
Christopher Haster 32:2c5fc105fc50 136 int32_t err = _socket->close();
Christopher Haster 32:2c5fc105fc50 137
Christopher Haster 32:2c5fc105fc50 138 if (!err) {
Christopher Haster 32:2c5fc105fc50 139 _iface->destroySocket(_socket);
Christopher Haster 54:0c764f654352 140 _socket = 0;
Christopher Haster 32:2c5fc105fc50 141 }
Christopher Haster 32:2c5fc105fc50 142
Christopher Haster 32:2c5fc105fc50 143 return err;
Christopher Haster 32:2c5fc105fc50 144 }
Christopher Haster 32:2c5fc105fc50 145
Christopher Haster 48:b3bbe28a7963 146 int32_t Socket::send(const void *data, uint32_t size)
geky 30:3cc78f5db99d 147 {
Christopher Haster 43:09ea32f2eb54 148 if (!_socket) {
Christopher Haster 45:c8aca7c1e93f 149 return NS_ERROR_NO_CONNECTION;
Christopher Haster 43:09ea32f2eb54 150 }
Christopher Haster 48:b3bbe28a7963 151 return _socket->send(data, size);
Christopher Haster 25:ed7b2a52e8ac 152 }
Christopher Haster 25:ed7b2a52e8ac 153
Christopher Haster 48:b3bbe28a7963 154 int32_t Socket::recv(void *data, uint32_t size, bool blocking)
geky 30:3cc78f5db99d 155 {
Christopher Haster 43:09ea32f2eb54 156 if (!_socket) {
Christopher Haster 45:c8aca7c1e93f 157 return NS_ERROR_NO_CONNECTION;
Christopher Haster 43:09ea32f2eb54 158 }
Christopher Haster 48:b3bbe28a7963 159
Christopher Haster 48:b3bbe28a7963 160 while (true) {
Christopher Haster 49:85fe0b99948d 161 int32_t recv = _socket->recv(data, size);
Christopher Haster 48:b3bbe28a7963 162
Christopher Haster 49:85fe0b99948d 163 if (recv != 0 || !blocking) {
Christopher Haster 49:85fe0b99948d 164 return recv;
Christopher Haster 48:b3bbe28a7963 165 }
Christopher Haster 48:b3bbe28a7963 166 }
Christopher Haster 25:ed7b2a52e8ac 167 }
Christopher Haster 25:ed7b2a52e8ac 168