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:
Mon Feb 22 22:51:03 2016 +0000
Branch:
api-changes
Revision:
31:7f15b95f2a1d
Parent:
30:3cc78f5db99d
Child:
32:2c5fc105fc50
Added dependency on DnsQuery accessible through getHostByName

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 27:d7ed39727306 25
Christopher Haster 28:163fbe3263f4 26 memset(_ip_address, 0, SOCK_IP_SIZE);
Christopher Haster 28:163fbe3263f4 27 _port = 0;
Christopher Haster 25:ed7b2a52e8ac 28 }
Christopher Haster 25:ed7b2a52e8ac 29
geky 30:3cc78f5db99d 30 Socket::~Socket()
geky 30:3cc78f5db99d 31 {
Christopher Haster 28:163fbe3263f4 32 if (_socket) {
Christopher Haster 28:163fbe3263f4 33 _iface->destroySocket(_socket);
Christopher Haster 28:163fbe3263f4 34 }
Christopher Haster 25:ed7b2a52e8ac 35 }
Christopher Haster 25:ed7b2a52e8ac 36
geky 30:3cc78f5db99d 37 SocketInterface *Socket::_get_socket()
geky 30:3cc78f5db99d 38 {
Christopher Haster 27:d7ed39727306 39 if (!_socket) {
Christopher Haster 27:d7ed39727306 40 _socket = _iface->createSocket(_proto);
Christopher Haster 27:d7ed39727306 41
Christopher Haster 28:163fbe3263f4 42 if (_ip_address[0]) {
Christopher Haster 28:163fbe3263f4 43 _socket->setIPAddress(_ip_address);
Christopher Haster 27:d7ed39727306 44 }
Christopher Haster 27:d7ed39727306 45
Christopher Haster 28:163fbe3263f4 46 if (_port) {
Christopher Haster 28:163fbe3263f4 47 _socket->setPort(_port);
Christopher Haster 27:d7ed39727306 48 }
Christopher Haster 27:d7ed39727306 49 }
Christopher Haster 27:d7ed39727306 50
Christopher Haster 25:ed7b2a52e8ac 51 return _socket;
Christopher Haster 25:ed7b2a52e8ac 52 }
Christopher Haster 25:ed7b2a52e8ac 53
geky 30:3cc78f5db99d 54 int32_t Socket::setURL(const char *url, uint16_t port)
geky 30:3cc78f5db99d 55 {
Christopher Haster 25:ed7b2a52e8ac 56 SocketInterface *s = _get_socket();
Christopher Haster 27:d7ed39727306 57 if (!s) return -2;
Christopher Haster 28:163fbe3263f4 58
geky 31:7f15b95f2a1d 59 int32_t err = _iface->getHostByName(url, _ip_address);
geky 31:7f15b95f2a1d 60 if (err < 0) return err;
geky 31:7f15b95f2a1d 61
geky 31:7f15b95f2a1d 62 if (_socket) {
geky 31:7f15b95f2a1d 63 _socket->setIPAddress(_ip_address);
geky 31:7f15b95f2a1d 64 }
Christopher Haster 28:163fbe3263f4 65
Christopher Haster 28:163fbe3263f4 66 if (port) {
Christopher Haster 28:163fbe3263f4 67 setPort(port);
Christopher Haster 28:163fbe3263f4 68 }
Christopher Haster 28:163fbe3263f4 69
Christopher Haster 28:163fbe3263f4 70 return 0;
Christopher Haster 25:ed7b2a52e8ac 71 }
Christopher Haster 25:ed7b2a52e8ac 72
geky 30:3cc78f5db99d 73 void Socket::setIPAddress(const char *ip, uint16_t port)
geky 30:3cc78f5db99d 74 {
Christopher Haster 28:163fbe3263f4 75 strcpy(_ip_address, ip);
Christopher Haster 28:163fbe3263f4 76
Christopher Haster 28:163fbe3263f4 77 if (_socket) {
geky 31:7f15b95f2a1d 78 _socket->setIPAddress(_ip_address);
Christopher Haster 27:d7ed39727306 79 }
Christopher Haster 28:163fbe3263f4 80
Christopher Haster 28:163fbe3263f4 81 if (port) {
Christopher Haster 28:163fbe3263f4 82 setPort(port);
Christopher Haster 28:163fbe3263f4 83 }
Christopher Haster 25:ed7b2a52e8ac 84 }
Christopher Haster 25:ed7b2a52e8ac 85
geky 30:3cc78f5db99d 86 void Socket::setPort(uint16_t port)
geky 30:3cc78f5db99d 87 {
Christopher Haster 28:163fbe3263f4 88 _port = port;
Christopher Haster 28:163fbe3263f4 89
Christopher Haster 28:163fbe3263f4 90 if (_socket) {
geky 31:7f15b95f2a1d 91 _socket->setPort(_port);
Christopher Haster 27:d7ed39727306 92 }
Christopher Haster 25:ed7b2a52e8ac 93 }
Christopher Haster 25:ed7b2a52e8ac 94
geky 30:3cc78f5db99d 95 const char *Socket::getIPAddress() const
geky 30:3cc78f5db99d 96 {
Christopher Haster 28:163fbe3263f4 97 return _ip_address;
Christopher Haster 25:ed7b2a52e8ac 98 }
Christopher Haster 25:ed7b2a52e8ac 99
geky 30:3cc78f5db99d 100 uint16_t Socket::getPort() const
geky 30:3cc78f5db99d 101 {
Christopher Haster 28:163fbe3263f4 102 return _port;
Christopher Haster 25:ed7b2a52e8ac 103 }
Christopher Haster 25:ed7b2a52e8ac 104
geky 30:3cc78f5db99d 105 int32_t Socket::send(const void *data, uint32_t len, uint32_t timeout_ms)
geky 30:3cc78f5db99d 106 {
Christopher Haster 25:ed7b2a52e8ac 107 SocketInterface *s = _get_socket();
geky 30:3cc78f5db99d 108 if (!s) return -2;
geky 30:3cc78f5db99d 109 return s->send(data, len, timeout_ms);
Christopher Haster 25:ed7b2a52e8ac 110 }
Christopher Haster 25:ed7b2a52e8ac 111
geky 30:3cc78f5db99d 112 int32_t Socket::recv(void *data, uint32_t len, uint32_t timeout_ms)
geky 30:3cc78f5db99d 113 {
Christopher Haster 25:ed7b2a52e8ac 114 SocketInterface *s = _get_socket();
geky 30:3cc78f5db99d 115 if (!s) return -2;
geky 30:3cc78f5db99d 116 return s->recv(data, len, timeout_ms);
Christopher Haster 25:ed7b2a52e8ac 117 }
Christopher Haster 25:ed7b2a52e8ac 118