2016-04-06 13:50:56 +00:00
|
|
|
/* Socket
|
|
|
|
* Copyright (c) 2015 ARM Limited
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "UDPSocket.h"
|
|
|
|
#include "Timer.h"
|
2016-07-19 16:07:39 +00:00
|
|
|
#include "mbed_assert.h"
|
2016-04-06 13:50:56 +00:00
|
|
|
|
2016-06-06 21:35:12 +00:00
|
|
|
UDPSocket::UDPSocket()
|
2016-12-06 11:43:52 +00:00
|
|
|
: _pending(1), _read_sem(0), _write_sem(0)
|
2016-03-13 12:08:27 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-06-06 21:35:12 +00:00
|
|
|
UDPSocket::~UDPSocket()
|
|
|
|
{
|
|
|
|
close();
|
|
|
|
}
|
|
|
|
|
2016-07-20 20:20:03 +00:00
|
|
|
nsapi_protocol_t UDPSocket::get_proto()
|
2016-05-27 04:54:05 +00:00
|
|
|
{
|
2016-07-20 20:20:03 +00:00
|
|
|
return NSAPI_UDP;
|
2016-05-27 04:54:05 +00:00
|
|
|
}
|
|
|
|
|
2016-10-18 19:48:46 +00:00
|
|
|
nsapi_size_or_error_t UDPSocket::sendto(const char *host, uint16_t port, const void *data, nsapi_size_t size)
|
2016-03-13 22:25:05 +00:00
|
|
|
{
|
2016-09-08 14:28:41 +00:00
|
|
|
SocketAddress address;
|
2016-10-18 19:48:46 +00:00
|
|
|
nsapi_size_or_error_t err = _stack->gethostbyname(host, &address);
|
2016-09-08 14:28:41 +00:00
|
|
|
if (err) {
|
2016-03-13 22:25:05 +00:00
|
|
|
return NSAPI_ERROR_DNS_FAILURE;
|
|
|
|
}
|
|
|
|
|
2016-09-08 14:28:41 +00:00
|
|
|
address.set_port(port);
|
|
|
|
|
2016-05-03 20:29:54 +00:00
|
|
|
// sendto is thread safe
|
2016-07-20 02:50:24 +00:00
|
|
|
return sendto(address, data, size);
|
2016-04-06 13:50:56 +00:00
|
|
|
}
|
|
|
|
|
2016-10-18 19:48:46 +00:00
|
|
|
nsapi_size_or_error_t UDPSocket::sendto(const SocketAddress &address, const void *data, nsapi_size_t size)
|
2016-04-06 13:50:56 +00:00
|
|
|
{
|
2016-05-03 20:29:54 +00:00
|
|
|
_lock.lock();
|
2016-10-18 19:48:46 +00:00
|
|
|
nsapi_size_or_error_t ret;
|
2016-07-20 02:50:24 +00:00
|
|
|
|
2016-04-06 13:50:56 +00:00
|
|
|
while (true) {
|
|
|
|
if (!_socket) {
|
2016-05-03 20:29:54 +00:00
|
|
|
ret = NSAPI_ERROR_NO_SOCKET;
|
|
|
|
break;
|
2016-04-06 13:50:56 +00:00
|
|
|
}
|
2016-05-03 20:29:54 +00:00
|
|
|
|
2016-06-06 21:35:12 +00:00
|
|
|
_pending = 0;
|
2016-10-18 19:48:46 +00:00
|
|
|
nsapi_size_or_error_t sent = _stack->socket_sendto(_socket, address, data, size);
|
2016-05-03 20:29:54 +00:00
|
|
|
if ((0 == _timeout) || (NSAPI_ERROR_WOULD_BLOCK != sent)) {
|
|
|
|
ret = sent;
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
int32_t count;
|
|
|
|
|
|
|
|
// Release lock before blocking so other threads
|
|
|
|
// accessing this object aren't blocked
|
|
|
|
_lock.unlock();
|
|
|
|
count = _write_sem.wait(_timeout);
|
|
|
|
_lock.lock();
|
2016-04-21 00:30:16 +00:00
|
|
|
|
2016-05-03 20:29:54 +00:00
|
|
|
if (count < 1) {
|
|
|
|
// Semaphore wait timed out so break out and return
|
|
|
|
ret = NSAPI_ERROR_WOULD_BLOCK;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-04-06 13:50:56 +00:00
|
|
|
}
|
2016-05-03 20:29:54 +00:00
|
|
|
|
|
|
|
_lock.unlock();
|
|
|
|
return ret;
|
2016-04-06 13:50:56 +00:00
|
|
|
}
|
|
|
|
|
2016-10-18 19:48:46 +00:00
|
|
|
nsapi_size_or_error_t UDPSocket::recvfrom(SocketAddress *address, void *buffer, nsapi_size_t size)
|
2016-04-06 13:50:56 +00:00
|
|
|
{
|
2016-05-03 20:29:54 +00:00
|
|
|
_lock.lock();
|
2016-10-18 19:48:46 +00:00
|
|
|
nsapi_size_or_error_t ret;
|
2016-07-20 02:50:24 +00:00
|
|
|
|
2016-04-06 13:50:56 +00:00
|
|
|
while (true) {
|
|
|
|
if (!_socket) {
|
2016-05-03 20:29:54 +00:00
|
|
|
ret = NSAPI_ERROR_NO_SOCKET;
|
|
|
|
break;
|
2016-04-06 13:50:56 +00:00
|
|
|
}
|
2016-05-03 20:29:54 +00:00
|
|
|
|
2016-06-06 21:35:12 +00:00
|
|
|
_pending = 0;
|
2016-10-18 19:48:46 +00:00
|
|
|
nsapi_size_or_error_t recv = _stack->socket_recvfrom(_socket, address, buffer, size);
|
2016-05-03 20:29:54 +00:00
|
|
|
if ((0 == _timeout) || (NSAPI_ERROR_WOULD_BLOCK != recv)) {
|
|
|
|
ret = recv;
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
int32_t count;
|
|
|
|
|
|
|
|
// Release lock before blocking so other threads
|
|
|
|
// accessing this object aren't blocked
|
|
|
|
_lock.unlock();
|
|
|
|
count = _read_sem.wait(_timeout);
|
|
|
|
_lock.lock();
|
|
|
|
|
|
|
|
if (count < 1) {
|
|
|
|
// Semaphore wait timed out so break out and return
|
|
|
|
ret = NSAPI_ERROR_WOULD_BLOCK;
|
|
|
|
break;
|
|
|
|
}
|
2016-04-06 13:50:56 +00:00
|
|
|
}
|
2016-05-03 20:29:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_lock.unlock();
|
|
|
|
return ret;
|
|
|
|
}
|
2016-04-21 00:30:16 +00:00
|
|
|
|
2016-06-06 21:35:12 +00:00
|
|
|
void UDPSocket::event()
|
2016-05-03 20:29:54 +00:00
|
|
|
{
|
2016-06-06 21:35:12 +00:00
|
|
|
int32_t wcount = _write_sem.wait(0);
|
|
|
|
if (wcount <= 1) {
|
2016-05-03 20:29:54 +00:00
|
|
|
_write_sem.release();
|
|
|
|
}
|
2016-06-06 21:35:12 +00:00
|
|
|
int32_t rcount = _read_sem.wait(0);
|
|
|
|
if (rcount <= 1) {
|
2016-05-03 20:29:54 +00:00
|
|
|
_read_sem.release();
|
2016-04-06 13:50:56 +00:00
|
|
|
}
|
2016-05-03 20:29:54 +00:00
|
|
|
|
2016-06-06 21:35:12 +00:00
|
|
|
_pending += 1;
|
|
|
|
if (_callback && _pending == 1) {
|
|
|
|
_callback();
|
|
|
|
}
|
2016-04-06 13:50:56 +00:00
|
|
|
}
|