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:
Wed Feb 24 22:04:19 2016 -0600
Branch:
api-changes
Revision:
43:09ea32f2eb54
Parent:
42:49893d13c432
Child:
45:c8aca7c1e93f
Responded to feedback in API decisions

- Removed small .cpp files
- Added null _iface checks
- Matched mbed coding standard
- Removed setTimeout/getTimeout

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