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 03:13:26 2016 -0600
Branch:
api-changes
Revision:
48:b3bbe28a7963
Parent:
45:c8aca7c1e93f
Child:
49:85fe0b99948d
Added non-blocking recv

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 28:163fbe3263f4 20 Socket::Socket(NetworkInterface *iface, socket_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 32:2c5fc105fc50 124 }
Christopher Haster 32:2c5fc105fc50 125
Christopher Haster 32:2c5fc105fc50 126 return err;
Christopher Haster 32:2c5fc105fc50 127 }
Christopher Haster 32:2c5fc105fc50 128
Christopher Haster 32:2c5fc105fc50 129 int32_t Socket::close()
Christopher Haster 32:2c5fc105fc50 130 {
Christopher Haster 43:09ea32f2eb54 131 if (!_socket) {
Christopher Haster 43:09ea32f2eb54 132 return 0;
Christopher Haster 43:09ea32f2eb54 133 }
Christopher Haster 32:2c5fc105fc50 134
Christopher Haster 32:2c5fc105fc50 135 int32_t err = _socket->close();
Christopher Haster 32:2c5fc105fc50 136
Christopher Haster 32:2c5fc105fc50 137 if (!err) {
Christopher Haster 32:2c5fc105fc50 138 _iface->destroySocket(_socket);
Christopher Haster 32:2c5fc105fc50 139 }
Christopher Haster 32:2c5fc105fc50 140
Christopher Haster 32:2c5fc105fc50 141 return err;
Christopher Haster 32:2c5fc105fc50 142 }
Christopher Haster 32:2c5fc105fc50 143
Christopher Haster 48:b3bbe28a7963 144 int32_t Socket::send(const void *data, uint32_t size)
geky 30:3cc78f5db99d 145 {
Christopher Haster 43:09ea32f2eb54 146 if (!_socket) {
Christopher Haster 45:c8aca7c1e93f 147 return NS_ERROR_NO_CONNECTION;
Christopher Haster 43:09ea32f2eb54 148 }
Christopher Haster 48:b3bbe28a7963 149 return _socket->send(data, size);
Christopher Haster 25:ed7b2a52e8ac 150 }
Christopher Haster 25:ed7b2a52e8ac 151
Christopher Haster 48:b3bbe28a7963 152 int32_t Socket::recv(void *data, uint32_t size, bool blocking)
geky 30:3cc78f5db99d 153 {
Christopher Haster 43:09ea32f2eb54 154 if (!_socket) {
Christopher Haster 45:c8aca7c1e93f 155 return NS_ERROR_NO_CONNECTION;
Christopher Haster 43:09ea32f2eb54 156 }
Christopher Haster 48:b3bbe28a7963 157
Christopher Haster 48:b3bbe28a7963 158 while (true) {
Christopher Haster 48:b3bbe28a7963 159 int32_t size = _socket->recv(data, size);
Christopher Haster 48:b3bbe28a7963 160
Christopher Haster 48:b3bbe28a7963 161 if (size != 0 || !blocking) {
Christopher Haster 48:b3bbe28a7963 162 return size;
Christopher Haster 48:b3bbe28a7963 163 }
Christopher Haster 48:b3bbe28a7963 164 }
Christopher Haster 25:ed7b2a52e8ac 165 }
Christopher Haster 25:ed7b2a52e8ac 166