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 18 03:29:39 2016 -0600
Branch:
api-changes
Revision:
22:4fca633c0633
Child:
24:a5e959bdd2dd
Added implementation for UDPSocket/TCPSocket

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Christopher Haster 22:4fca633c0633 1 /* TCPSocket
Christopher Haster 22:4fca633c0633 2 * Copyright (c) 2015 ARM Limited
Christopher Haster 22:4fca633c0633 3 *
Christopher Haster 22:4fca633c0633 4 * Licensed under the Apache License, Version 2.0 (the "License");
Christopher Haster 22:4fca633c0633 5 * you may not use this file except in compliance with the License.
Christopher Haster 22:4fca633c0633 6 * You may obtain a copy of the License at
Christopher Haster 22:4fca633c0633 7 *
Christopher Haster 22:4fca633c0633 8 * http://www.apache.org/licenses/LICENSE-2.0
Christopher Haster 22:4fca633c0633 9 *
Christopher Haster 22:4fca633c0633 10 * Unless required by applicable law or agreed to in writing, software
Christopher Haster 22:4fca633c0633 11 * distributed under the License is distributed on an "AS IS" BASIS,
Christopher Haster 22:4fca633c0633 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Christopher Haster 22:4fca633c0633 13 * See the License for the specific language governing permissions and
Christopher Haster 22:4fca633c0633 14 * limitations under the License.
Christopher Haster 22:4fca633c0633 15 */
Christopher Haster 22:4fca633c0633 16
Christopher Haster 22:4fca633c0633 17 #include "TCPSocket.h"
Christopher Haster 22:4fca633c0633 18
Christopher Haster 22:4fca633c0633 19 TCPSocket::TCPSocket(NetworkInterface *iface, const char *ip, uint16_t port)
Christopher Haster 22:4fca633c0633 20 : _iface(iface) {
Christopher Haster 22:4fca633c0633 21 _socket = _iface->createSocket(SOCK_TCP);
Christopher Haster 22:4fca633c0633 22
Christopher Haster 22:4fca633c0633 23 if (ip) setIPAddress(ip);
Christopher Haster 22:4fca633c0633 24 if (port) setPort(port);
Christopher Haster 22:4fca633c0633 25 }
Christopher Haster 22:4fca633c0633 26
Christopher Haster 22:4fca633c0633 27 TCPSocket::~TCPSocket() {
Christopher Haster 22:4fca633c0633 28 _iface->destroySocket(_socket);
Christopher Haster 22:4fca633c0633 29 }
Christopher Haster 22:4fca633c0633 30
Christopher Haster 22:4fca633c0633 31 void TCPSocket::setIPAddress(const char *ip) {
Christopher Haster 22:4fca633c0633 32 return _socket->setIPAddress(ip);
Christopher Haster 22:4fca633c0633 33 }
Christopher Haster 22:4fca633c0633 34
Christopher Haster 22:4fca633c0633 35 void TCPSocket::setPort(uint16_t port) {
Christopher Haster 22:4fca633c0633 36 return _socket->setPort(port);
Christopher Haster 22:4fca633c0633 37 }
Christopher Haster 22:4fca633c0633 38
Christopher Haster 22:4fca633c0633 39 const char *TCPSocket::getIPAddress() {
Christopher Haster 22:4fca633c0633 40 return _socket->getIPAddress();
Christopher Haster 22:4fca633c0633 41 }
Christopher Haster 22:4fca633c0633 42
Christopher Haster 22:4fca633c0633 43 uint16_t TCPSocket::getPort() {
Christopher Haster 22:4fca633c0633 44 return _socket->getPort();
Christopher Haster 22:4fca633c0633 45 }
Christopher Haster 22:4fca633c0633 46
Christopher Haster 22:4fca633c0633 47 int32_t TCPSocket::open() {
Christopher Haster 22:4fca633c0633 48 return _socket->open();
Christopher Haster 22:4fca633c0633 49 }
Christopher Haster 22:4fca633c0633 50
Christopher Haster 22:4fca633c0633 51 int32_t TCPSocket::close() {
Christopher Haster 22:4fca633c0633 52 return _socket->close();
Christopher Haster 22:4fca633c0633 53 }
Christopher Haster 22:4fca633c0633 54
Christopher Haster 22:4fca633c0633 55 int32_t TCPSocket::send(const void *data, uint32_t len, uint32_t timeout_ms) {
Christopher Haster 22:4fca633c0633 56 return _socket->send(data, len, timeout_ms);
Christopher Haster 22:4fca633c0633 57 }
Christopher Haster 22:4fca633c0633 58
Christopher Haster 22:4fca633c0633 59 int32_t TCPSocket::recv(void *data, uint32_t len, uint32_t timeout_ms) {
Christopher Haster 22:4fca633c0633 60 return _socket->recv(data, len, timeout_ms);
Christopher Haster 22:4fca633c0633 61 }
Christopher Haster 22:4fca633c0633 62