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 20:58:50 2016 +0000
Branch:
api-changes
Revision:
29:7bcec3189a93
Parent:
28:163fbe3263f4
Child:
30:3cc78f5db99d
Fixed include statements

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