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:
dkato
Date:
Wed May 25 02:45:51 2016 +0000
Revision:
125:ea3a618e0818
Parent:
118:96627c4b83d5
Fix the path of DnsQuery.lib

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Christopher Haster 79:43a7e8c0d6cc 1 /* Socket
Christopher Haster 79:43a7e8c0d6cc 2 * Copyright (c) 2015 ARM Limited
Christopher Haster 79:43a7e8c0d6cc 3 *
Christopher Haster 79:43a7e8c0d6cc 4 * Licensed under the Apache License, Version 2.0 (the "License");
Christopher Haster 79:43a7e8c0d6cc 5 * you may not use this file except in compliance with the License.
Christopher Haster 79:43a7e8c0d6cc 6 * You may obtain a copy of the License at
Christopher Haster 79:43a7e8c0d6cc 7 *
Christopher Haster 79:43a7e8c0d6cc 8 * http://www.apache.org/licenses/LICENSE-2.0
Christopher Haster 79:43a7e8c0d6cc 9 *
Christopher Haster 79:43a7e8c0d6cc 10 * Unless required by applicable law or agreed to in writing, software
Christopher Haster 79:43a7e8c0d6cc 11 * distributed under the License is distributed on an "AS IS" BASIS,
Christopher Haster 79:43a7e8c0d6cc 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Christopher Haster 79:43a7e8c0d6cc 13 * See the License for the specific language governing permissions and
Christopher Haster 79:43a7e8c0d6cc 14 * limitations under the License.
Christopher Haster 79:43a7e8c0d6cc 15 */
Christopher Haster 79:43a7e8c0d6cc 16
Christopher Haster 79:43a7e8c0d6cc 17 #include "UDPSocket.h"
Christopher Haster 82:97d166c4a193 18 #include "Timer.h"
Christopher Haster 79:43a7e8c0d6cc 19
Christopher Haster 90:0a988e4abb72 20 UDPSocket::UDPSocket()
Christopher Haster 90:0a988e4abb72 21 {
Christopher Haster 90:0a988e4abb72 22 }
Christopher Haster 90:0a988e4abb72 23
Christopher Haster 105:2fd212f8da61 24 UDPSocket::UDPSocket(NetworkStack *iface)
Christopher Haster 79:43a7e8c0d6cc 25 {
Christopher Haster 90:0a988e4abb72 26 open(iface);
Christopher Haster 90:0a988e4abb72 27 }
Christopher Haster 90:0a988e4abb72 28
Christopher Haster 105:2fd212f8da61 29 int UDPSocket::open(NetworkStack *iface)
Christopher Haster 90:0a988e4abb72 30 {
Christopher Haster 90:0a988e4abb72 31 return Socket::open(iface, NSAPI_UDP);
Christopher Haster 79:43a7e8c0d6cc 32 }
Christopher Haster 79:43a7e8c0d6cc 33
Christopher Haster 96:656011e49d9f 34 int UDPSocket::sendto(const char *host, uint16_t port, const void *data, unsigned size)
Christopher Haster 96:656011e49d9f 35 {
Christopher Haster 96:656011e49d9f 36 SocketAddress addr(_iface, host, port);
Christopher Haster 96:656011e49d9f 37 if (!addr) {
Christopher Haster 96:656011e49d9f 38 return NSAPI_ERROR_DNS_FAILURE;
Christopher Haster 96:656011e49d9f 39 }
Christopher Haster 96:656011e49d9f 40
Christopher Haster 96:656011e49d9f 41 return sendto(addr, data, size);
Christopher Haster 79:43a7e8c0d6cc 42 }
Christopher Haster 79:43a7e8c0d6cc 43
Christopher Haster 80:9c6673c93082 44 int UDPSocket::sendto(const SocketAddress &address, const void *data, unsigned size)
Christopher Haster 79:43a7e8c0d6cc 45 {
Christopher Haster 82:97d166c4a193 46 mbed::Timer timer;
Christopher Haster 82:97d166c4a193 47 timer.start();
Christopher Haster 118:96627c4b83d5 48 mbed::Timeout timeout;
Christopher Haster 118:96627c4b83d5 49 if (_timeout >= 0) {
Christopher Haster 118:96627c4b83d5 50 timeout.attach_us(&Socket::wakeup, _timeout * 1000);
Christopher Haster 118:96627c4b83d5 51 }
Christopher Haster 82:97d166c4a193 52
Christopher Haster 82:97d166c4a193 53 while (true) {
Christopher Haster 82:97d166c4a193 54 if (!_socket) {
Christopher Haster 82:97d166c4a193 55 return NSAPI_ERROR_NO_SOCKET;
Christopher Haster 82:97d166c4a193 56 }
Christopher Haster 82:97d166c4a193 57
Christopher Haster 82:97d166c4a193 58 int sent = _iface->socket_sendto(_socket, address, data, size);
Christopher Haster 115:950b19eb0f02 59 if (sent != NSAPI_ERROR_WOULD_BLOCK
Christopher Haster 118:96627c4b83d5 60 || (_timeout >= 0 && timer.read_ms() >= _timeout)) {
Christopher Haster 82:97d166c4a193 61 return sent;
Christopher Haster 82:97d166c4a193 62 }
Christopher Haster 116:bc043343d753 63
Christopher Haster 116:bc043343d753 64 __WFI();
Christopher Haster 79:43a7e8c0d6cc 65 }
Christopher Haster 79:43a7e8c0d6cc 66 }
Christopher Haster 79:43a7e8c0d6cc 67
Christopher Haster 79:43a7e8c0d6cc 68 int UDPSocket::recvfrom(SocketAddress *address, void *buffer, unsigned size)
Christopher Haster 79:43a7e8c0d6cc 69 {
Christopher Haster 82:97d166c4a193 70 mbed::Timer timer;
Christopher Haster 82:97d166c4a193 71 timer.start();
Christopher Haster 118:96627c4b83d5 72 mbed::Timeout timeout;
Christopher Haster 118:96627c4b83d5 73 if (_timeout >= 0) {
Christopher Haster 118:96627c4b83d5 74 timeout.attach_us(&Socket::wakeup, _timeout * 1000);
Christopher Haster 118:96627c4b83d5 75 }
Christopher Haster 82:97d166c4a193 76
Christopher Haster 82:97d166c4a193 77 while (true) {
Christopher Haster 82:97d166c4a193 78 if (!_socket) {
Christopher Haster 82:97d166c4a193 79 return NSAPI_ERROR_NO_SOCKET;
Christopher Haster 82:97d166c4a193 80 }
Christopher Haster 82:97d166c4a193 81
Christopher Haster 82:97d166c4a193 82 int recv = _iface->socket_recvfrom(_socket, address, buffer, size);
Christopher Haster 115:950b19eb0f02 83 if (recv != NSAPI_ERROR_WOULD_BLOCK
Christopher Haster 118:96627c4b83d5 84 || (_timeout >= 0 && timer.read_ms() >= _timeout)) {
Christopher Haster 82:97d166c4a193 85 return recv;
Christopher Haster 82:97d166c4a193 86 }
Christopher Haster 116:bc043343d753 87
Christopher Haster 116:bc043343d753 88 __WFI();
Christopher Haster 79:43a7e8c0d6cc 89 }
Christopher Haster 79:43a7e8c0d6cc 90 }