Nicolas Borla / Mbed OS ROME2_Robot_Firmware
Committer:
boro
Date:
Mon Mar 16 13:12:31 2020 +0000
Revision:
0:4beb2ea291ec
a

Who changed what in which revision?

UserRevisionLine numberNew contents of line
boro 0:4beb2ea291ec 1 /* Socket
boro 0:4beb2ea291ec 2 * Copyright (c) 2015 ARM Limited
boro 0:4beb2ea291ec 3 *
boro 0:4beb2ea291ec 4 * Licensed under the Apache License, Version 2.0 (the "License");
boro 0:4beb2ea291ec 5 * you may not use this file except in compliance with the License.
boro 0:4beb2ea291ec 6 * You may obtain a copy of the License at
boro 0:4beb2ea291ec 7 *
boro 0:4beb2ea291ec 8 * http://www.apache.org/licenses/LICENSE-2.0
boro 0:4beb2ea291ec 9 *
boro 0:4beb2ea291ec 10 * Unless required by applicable law or agreed to in writing, software
boro 0:4beb2ea291ec 11 * distributed under the License is distributed on an "AS IS" BASIS,
boro 0:4beb2ea291ec 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
boro 0:4beb2ea291ec 13 * See the License for the specific language governing permissions and
boro 0:4beb2ea291ec 14 * limitations under the License.
boro 0:4beb2ea291ec 15 */
boro 0:4beb2ea291ec 16
boro 0:4beb2ea291ec 17 #include "UDPSocket.h"
boro 0:4beb2ea291ec 18 #include "Timer.h"
boro 0:4beb2ea291ec 19 #include "mbed_assert.h"
boro 0:4beb2ea291ec 20
boro 0:4beb2ea291ec 21 #define TCP_EVENT "UDP_Events"
boro 0:4beb2ea291ec 22 #define READ_FLAG 0x1u
boro 0:4beb2ea291ec 23 #define WRITE_FLAG 0x2u
boro 0:4beb2ea291ec 24
boro 0:4beb2ea291ec 25 UDPSocket::UDPSocket()
boro 0:4beb2ea291ec 26 : _pending(0), _event_flag()
boro 0:4beb2ea291ec 27 {
boro 0:4beb2ea291ec 28 }
boro 0:4beb2ea291ec 29
boro 0:4beb2ea291ec 30 UDPSocket::~UDPSocket()
boro 0:4beb2ea291ec 31 {
boro 0:4beb2ea291ec 32 close();
boro 0:4beb2ea291ec 33 }
boro 0:4beb2ea291ec 34
boro 0:4beb2ea291ec 35 nsapi_protocol_t UDPSocket::get_proto()
boro 0:4beb2ea291ec 36 {
boro 0:4beb2ea291ec 37 return NSAPI_UDP;
boro 0:4beb2ea291ec 38 }
boro 0:4beb2ea291ec 39
boro 0:4beb2ea291ec 40
boro 0:4beb2ea291ec 41 nsapi_size_or_error_t UDPSocket::sendto(const char *host, uint16_t port, const void *data, nsapi_size_t size)
boro 0:4beb2ea291ec 42 {
boro 0:4beb2ea291ec 43 SocketAddress address;
boro 0:4beb2ea291ec 44 nsapi_size_or_error_t err = _stack->gethostbyname(host, &address);
boro 0:4beb2ea291ec 45 if (err) {
boro 0:4beb2ea291ec 46 return NSAPI_ERROR_DNS_FAILURE;
boro 0:4beb2ea291ec 47 }
boro 0:4beb2ea291ec 48
boro 0:4beb2ea291ec 49 address.set_port(port);
boro 0:4beb2ea291ec 50
boro 0:4beb2ea291ec 51 // sendto is thread safe
boro 0:4beb2ea291ec 52 return sendto(address, data, size);
boro 0:4beb2ea291ec 53 }
boro 0:4beb2ea291ec 54
boro 0:4beb2ea291ec 55 nsapi_size_or_error_t UDPSocket::sendto(const SocketAddress &address, const void *data, nsapi_size_t size)
boro 0:4beb2ea291ec 56 {
boro 0:4beb2ea291ec 57 _lock.lock();
boro 0:4beb2ea291ec 58 nsapi_size_or_error_t ret;
boro 0:4beb2ea291ec 59
boro 0:4beb2ea291ec 60 while (true) {
boro 0:4beb2ea291ec 61 if (!_socket) {
boro 0:4beb2ea291ec 62 ret = NSAPI_ERROR_NO_SOCKET;
boro 0:4beb2ea291ec 63 break;
boro 0:4beb2ea291ec 64 }
boro 0:4beb2ea291ec 65
boro 0:4beb2ea291ec 66 _pending = 0;
boro 0:4beb2ea291ec 67 nsapi_size_or_error_t sent = _stack->socket_sendto(_socket, address, data, size);
boro 0:4beb2ea291ec 68 if ((0 == _timeout) || (NSAPI_ERROR_WOULD_BLOCK != sent)) {
boro 0:4beb2ea291ec 69 ret = sent;
boro 0:4beb2ea291ec 70 break;
boro 0:4beb2ea291ec 71 } else {
boro 0:4beb2ea291ec 72 uint32_t flag;
boro 0:4beb2ea291ec 73
boro 0:4beb2ea291ec 74 // Release lock before blocking so other threads
boro 0:4beb2ea291ec 75 // accessing this object aren't blocked
boro 0:4beb2ea291ec 76 _lock.unlock();
boro 0:4beb2ea291ec 77 flag = _event_flag.wait_any(WRITE_FLAG, _timeout);
boro 0:4beb2ea291ec 78 _lock.lock();
boro 0:4beb2ea291ec 79
boro 0:4beb2ea291ec 80 if (flag & osFlagsError) {
boro 0:4beb2ea291ec 81 // Timeout break
boro 0:4beb2ea291ec 82 ret = NSAPI_ERROR_WOULD_BLOCK;
boro 0:4beb2ea291ec 83 break;
boro 0:4beb2ea291ec 84 }
boro 0:4beb2ea291ec 85 }
boro 0:4beb2ea291ec 86 }
boro 0:4beb2ea291ec 87
boro 0:4beb2ea291ec 88 _lock.unlock();
boro 0:4beb2ea291ec 89 return ret;
boro 0:4beb2ea291ec 90 }
boro 0:4beb2ea291ec 91
boro 0:4beb2ea291ec 92 nsapi_size_or_error_t UDPSocket::recvfrom(SocketAddress *address, void *buffer, nsapi_size_t size)
boro 0:4beb2ea291ec 93 {
boro 0:4beb2ea291ec 94 _lock.lock();
boro 0:4beb2ea291ec 95 nsapi_size_or_error_t ret;
boro 0:4beb2ea291ec 96
boro 0:4beb2ea291ec 97 while (true) {
boro 0:4beb2ea291ec 98 if (!_socket) {
boro 0:4beb2ea291ec 99 ret = NSAPI_ERROR_NO_SOCKET;
boro 0:4beb2ea291ec 100 break;
boro 0:4beb2ea291ec 101 }
boro 0:4beb2ea291ec 102
boro 0:4beb2ea291ec 103 _pending = 0;
boro 0:4beb2ea291ec 104 nsapi_size_or_error_t recv = _stack->socket_recvfrom(_socket, address, buffer, size);
boro 0:4beb2ea291ec 105 if ((0 == _timeout) || (NSAPI_ERROR_WOULD_BLOCK != recv)) {
boro 0:4beb2ea291ec 106 ret = recv;
boro 0:4beb2ea291ec 107 break;
boro 0:4beb2ea291ec 108 } else {
boro 0:4beb2ea291ec 109 uint32_t flag;
boro 0:4beb2ea291ec 110
boro 0:4beb2ea291ec 111 // Release lock before blocking so other threads
boro 0:4beb2ea291ec 112 // accessing this object aren't blocked
boro 0:4beb2ea291ec 113 _lock.unlock();
boro 0:4beb2ea291ec 114 flag = _event_flag.wait_any(READ_FLAG, _timeout);
boro 0:4beb2ea291ec 115 _lock.lock();
boro 0:4beb2ea291ec 116
boro 0:4beb2ea291ec 117 if (flag & osFlagsError) {
boro 0:4beb2ea291ec 118 // Timeout break
boro 0:4beb2ea291ec 119 ret = NSAPI_ERROR_WOULD_BLOCK;
boro 0:4beb2ea291ec 120 break;
boro 0:4beb2ea291ec 121 }
boro 0:4beb2ea291ec 122 }
boro 0:4beb2ea291ec 123 }
boro 0:4beb2ea291ec 124
boro 0:4beb2ea291ec 125 _lock.unlock();
boro 0:4beb2ea291ec 126 return ret;
boro 0:4beb2ea291ec 127 }
boro 0:4beb2ea291ec 128
boro 0:4beb2ea291ec 129 void UDPSocket::event()
boro 0:4beb2ea291ec 130 {
boro 0:4beb2ea291ec 131 _event_flag.set(READ_FLAG|WRITE_FLAG);
boro 0:4beb2ea291ec 132
boro 0:4beb2ea291ec 133 _pending += 1;
boro 0:4beb2ea291ec 134 if (_callback && _pending == 1) {
boro 0:4beb2ea291ec 135 _callback();
boro 0:4beb2ea291ec 136 }
boro 0:4beb2ea291ec 137 }