Entrega 3er corte - sistemas embebidos

Committer:
Bethory
Date:
Wed May 30 00:01:50 2018 +0000
Revision:
0:6ad07c9019fd
Codigo de tales para todos los pasculaes

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Bethory 0:6ad07c9019fd 1 /* Socket
Bethory 0:6ad07c9019fd 2 * Copyright (c) 2015 ARM Limited
Bethory 0:6ad07c9019fd 3 *
Bethory 0:6ad07c9019fd 4 * Licensed under the Apache License, Version 2.0 (the "License");
Bethory 0:6ad07c9019fd 5 * you may not use this file except in compliance with the License.
Bethory 0:6ad07c9019fd 6 * You may obtain a copy of the License at
Bethory 0:6ad07c9019fd 7 *
Bethory 0:6ad07c9019fd 8 * http://www.apache.org/licenses/LICENSE-2.0
Bethory 0:6ad07c9019fd 9 *
Bethory 0:6ad07c9019fd 10 * Unless required by applicable law or agreed to in writing, software
Bethory 0:6ad07c9019fd 11 * distributed under the License is distributed on an "AS IS" BASIS,
Bethory 0:6ad07c9019fd 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Bethory 0:6ad07c9019fd 13 * See the License for the specific language governing permissions and
Bethory 0:6ad07c9019fd 14 * limitations under the License.
Bethory 0:6ad07c9019fd 15 */
Bethory 0:6ad07c9019fd 16
Bethory 0:6ad07c9019fd 17 #include "TCPSocket.h"
Bethory 0:6ad07c9019fd 18 #include "Timer.h"
Bethory 0:6ad07c9019fd 19 #include "mbed_assert.h"
Bethory 0:6ad07c9019fd 20
Bethory 0:6ad07c9019fd 21 #define READ_FLAG 0x1u
Bethory 0:6ad07c9019fd 22 #define WRITE_FLAG 0x2u
Bethory 0:6ad07c9019fd 23
Bethory 0:6ad07c9019fd 24 TCPSocket::TCPSocket()
Bethory 0:6ad07c9019fd 25 : _pending(0), _event_flag(),
Bethory 0:6ad07c9019fd 26 _read_in_progress(false), _write_in_progress(false)
Bethory 0:6ad07c9019fd 27 {
Bethory 0:6ad07c9019fd 28 }
Bethory 0:6ad07c9019fd 29
Bethory 0:6ad07c9019fd 30 TCPSocket::~TCPSocket()
Bethory 0:6ad07c9019fd 31 {
Bethory 0:6ad07c9019fd 32 close();
Bethory 0:6ad07c9019fd 33 }
Bethory 0:6ad07c9019fd 34
Bethory 0:6ad07c9019fd 35 nsapi_protocol_t TCPSocket::get_proto()
Bethory 0:6ad07c9019fd 36 {
Bethory 0:6ad07c9019fd 37 return NSAPI_TCP;
Bethory 0:6ad07c9019fd 38 }
Bethory 0:6ad07c9019fd 39
Bethory 0:6ad07c9019fd 40 nsapi_error_t TCPSocket::connect(const SocketAddress &address)
Bethory 0:6ad07c9019fd 41 {
Bethory 0:6ad07c9019fd 42 _lock.lock();
Bethory 0:6ad07c9019fd 43 nsapi_error_t ret;
Bethory 0:6ad07c9019fd 44
Bethory 0:6ad07c9019fd 45 // If this assert is hit then there are two threads
Bethory 0:6ad07c9019fd 46 // performing a send at the same time which is undefined
Bethory 0:6ad07c9019fd 47 // behavior
Bethory 0:6ad07c9019fd 48 MBED_ASSERT(!_write_in_progress);
Bethory 0:6ad07c9019fd 49 _write_in_progress = true;
Bethory 0:6ad07c9019fd 50
Bethory 0:6ad07c9019fd 51 bool blocking_connect_in_progress = false;
Bethory 0:6ad07c9019fd 52
Bethory 0:6ad07c9019fd 53 while (true) {
Bethory 0:6ad07c9019fd 54 if (!_socket) {
Bethory 0:6ad07c9019fd 55 ret = NSAPI_ERROR_NO_SOCKET;
Bethory 0:6ad07c9019fd 56 break;
Bethory 0:6ad07c9019fd 57 }
Bethory 0:6ad07c9019fd 58
Bethory 0:6ad07c9019fd 59 _pending = 0;
Bethory 0:6ad07c9019fd 60 ret = _stack->socket_connect(_socket, address);
Bethory 0:6ad07c9019fd 61 if ((_timeout == 0) || !(ret == NSAPI_ERROR_IN_PROGRESS || ret == NSAPI_ERROR_ALREADY)) {
Bethory 0:6ad07c9019fd 62 break;
Bethory 0:6ad07c9019fd 63 } else {
Bethory 0:6ad07c9019fd 64 blocking_connect_in_progress = true;
Bethory 0:6ad07c9019fd 65
Bethory 0:6ad07c9019fd 66 uint32_t flag;
Bethory 0:6ad07c9019fd 67
Bethory 0:6ad07c9019fd 68 // Release lock before blocking so other threads
Bethory 0:6ad07c9019fd 69 // accessing this object aren't blocked
Bethory 0:6ad07c9019fd 70 _lock.unlock();
Bethory 0:6ad07c9019fd 71 flag = _event_flag.wait_any(WRITE_FLAG, _timeout);
Bethory 0:6ad07c9019fd 72 _lock.lock();
Bethory 0:6ad07c9019fd 73 if (flag & osFlagsError) {
Bethory 0:6ad07c9019fd 74 // Timeout break
Bethory 0:6ad07c9019fd 75 break;
Bethory 0:6ad07c9019fd 76 }
Bethory 0:6ad07c9019fd 77 }
Bethory 0:6ad07c9019fd 78 }
Bethory 0:6ad07c9019fd 79
Bethory 0:6ad07c9019fd 80 _write_in_progress = false;
Bethory 0:6ad07c9019fd 81
Bethory 0:6ad07c9019fd 82 /* Non-blocking connect gives "EISCONN" once done - convert to OK for blocking mode if we became connected during this call */
Bethory 0:6ad07c9019fd 83 if (ret == NSAPI_ERROR_IS_CONNECTED && blocking_connect_in_progress) {
Bethory 0:6ad07c9019fd 84 ret = NSAPI_ERROR_OK;
Bethory 0:6ad07c9019fd 85 }
Bethory 0:6ad07c9019fd 86
Bethory 0:6ad07c9019fd 87 _lock.unlock();
Bethory 0:6ad07c9019fd 88 return ret;
Bethory 0:6ad07c9019fd 89 }
Bethory 0:6ad07c9019fd 90
Bethory 0:6ad07c9019fd 91 nsapi_error_t TCPSocket::connect(const char *host, uint16_t port)
Bethory 0:6ad07c9019fd 92 {
Bethory 0:6ad07c9019fd 93 SocketAddress address;
Bethory 0:6ad07c9019fd 94 nsapi_error_t err = _stack->gethostbyname(host, &address);
Bethory 0:6ad07c9019fd 95 if (err) {
Bethory 0:6ad07c9019fd 96 return NSAPI_ERROR_DNS_FAILURE;
Bethory 0:6ad07c9019fd 97 }
Bethory 0:6ad07c9019fd 98
Bethory 0:6ad07c9019fd 99 address.set_port(port);
Bethory 0:6ad07c9019fd 100
Bethory 0:6ad07c9019fd 101 // connect is thread safe
Bethory 0:6ad07c9019fd 102 return connect(address);
Bethory 0:6ad07c9019fd 103 }
Bethory 0:6ad07c9019fd 104
Bethory 0:6ad07c9019fd 105 nsapi_size_or_error_t TCPSocket::send(const void *data, nsapi_size_t size)
Bethory 0:6ad07c9019fd 106 {
Bethory 0:6ad07c9019fd 107 _lock.lock();
Bethory 0:6ad07c9019fd 108 const uint8_t *data_ptr = static_cast<const uint8_t *>(data);
Bethory 0:6ad07c9019fd 109 nsapi_size_or_error_t ret;
Bethory 0:6ad07c9019fd 110 nsapi_size_t written = 0;
Bethory 0:6ad07c9019fd 111
Bethory 0:6ad07c9019fd 112 // If this assert is hit then there are two threads
Bethory 0:6ad07c9019fd 113 // performing a send at the same time which is undefined
Bethory 0:6ad07c9019fd 114 // behavior
Bethory 0:6ad07c9019fd 115 MBED_ASSERT(!_write_in_progress);
Bethory 0:6ad07c9019fd 116 _write_in_progress = true;
Bethory 0:6ad07c9019fd 117
Bethory 0:6ad07c9019fd 118 // Unlike recv, we should write the whole thing if blocking. POSIX only
Bethory 0:6ad07c9019fd 119 // allows partial as a side-effect of signal handling; it normally tries to
Bethory 0:6ad07c9019fd 120 // write everything if blocking. Without signals we can always write all.
Bethory 0:6ad07c9019fd 121 while (true) {
Bethory 0:6ad07c9019fd 122 if (!_socket) {
Bethory 0:6ad07c9019fd 123 ret = NSAPI_ERROR_NO_SOCKET;
Bethory 0:6ad07c9019fd 124 break;
Bethory 0:6ad07c9019fd 125 }
Bethory 0:6ad07c9019fd 126
Bethory 0:6ad07c9019fd 127 _pending = 0;
Bethory 0:6ad07c9019fd 128 ret = _stack->socket_send(_socket, data_ptr + written, size - written);
Bethory 0:6ad07c9019fd 129 if (ret >= 0) {
Bethory 0:6ad07c9019fd 130 written += ret;
Bethory 0:6ad07c9019fd 131 if (written >= size) {
Bethory 0:6ad07c9019fd 132 break;
Bethory 0:6ad07c9019fd 133 }
Bethory 0:6ad07c9019fd 134 }
Bethory 0:6ad07c9019fd 135 if (_timeout == 0) {
Bethory 0:6ad07c9019fd 136 break;
Bethory 0:6ad07c9019fd 137 } else if (ret == NSAPI_ERROR_WOULD_BLOCK) {
Bethory 0:6ad07c9019fd 138 uint32_t flag;
Bethory 0:6ad07c9019fd 139
Bethory 0:6ad07c9019fd 140 // Release lock before blocking so other threads
Bethory 0:6ad07c9019fd 141 // accessing this object aren't blocked
Bethory 0:6ad07c9019fd 142 _lock.unlock();
Bethory 0:6ad07c9019fd 143 flag = _event_flag.wait_any(WRITE_FLAG, _timeout);
Bethory 0:6ad07c9019fd 144 _lock.lock();
Bethory 0:6ad07c9019fd 145
Bethory 0:6ad07c9019fd 146 if (flag & osFlagsError) {
Bethory 0:6ad07c9019fd 147 // Timeout break
Bethory 0:6ad07c9019fd 148 break;
Bethory 0:6ad07c9019fd 149 }
Bethory 0:6ad07c9019fd 150 } else if (ret < 0) {
Bethory 0:6ad07c9019fd 151 break;
Bethory 0:6ad07c9019fd 152 }
Bethory 0:6ad07c9019fd 153 }
Bethory 0:6ad07c9019fd 154
Bethory 0:6ad07c9019fd 155 _write_in_progress = false;
Bethory 0:6ad07c9019fd 156 _lock.unlock();
Bethory 0:6ad07c9019fd 157 if (ret <= 0 && ret != NSAPI_ERROR_WOULD_BLOCK) {
Bethory 0:6ad07c9019fd 158 return ret;
Bethory 0:6ad07c9019fd 159 } else if (written == 0) {
Bethory 0:6ad07c9019fd 160 return NSAPI_ERROR_WOULD_BLOCK;
Bethory 0:6ad07c9019fd 161 } else {
Bethory 0:6ad07c9019fd 162 return written;
Bethory 0:6ad07c9019fd 163 }
Bethory 0:6ad07c9019fd 164 }
Bethory 0:6ad07c9019fd 165
Bethory 0:6ad07c9019fd 166 nsapi_size_or_error_t TCPSocket::recv(void *data, nsapi_size_t size)
Bethory 0:6ad07c9019fd 167 {
Bethory 0:6ad07c9019fd 168 _lock.lock();
Bethory 0:6ad07c9019fd 169 nsapi_size_or_error_t ret;
Bethory 0:6ad07c9019fd 170
Bethory 0:6ad07c9019fd 171 // If this assert is hit then there are two threads
Bethory 0:6ad07c9019fd 172 // performing a recv at the same time which is undefined
Bethory 0:6ad07c9019fd 173 // behavior
Bethory 0:6ad07c9019fd 174 MBED_ASSERT(!_read_in_progress);
Bethory 0:6ad07c9019fd 175 _read_in_progress = true;
Bethory 0:6ad07c9019fd 176
Bethory 0:6ad07c9019fd 177 while (true) {
Bethory 0:6ad07c9019fd 178 if (!_socket) {
Bethory 0:6ad07c9019fd 179 ret = NSAPI_ERROR_NO_SOCKET;
Bethory 0:6ad07c9019fd 180 break;
Bethory 0:6ad07c9019fd 181 }
Bethory 0:6ad07c9019fd 182
Bethory 0:6ad07c9019fd 183 _pending = 0;
Bethory 0:6ad07c9019fd 184 ret = _stack->socket_recv(_socket, data, size);
Bethory 0:6ad07c9019fd 185 if ((_timeout == 0) || (ret != NSAPI_ERROR_WOULD_BLOCK)) {
Bethory 0:6ad07c9019fd 186 break;
Bethory 0:6ad07c9019fd 187 } else {
Bethory 0:6ad07c9019fd 188 uint32_t flag;
Bethory 0:6ad07c9019fd 189
Bethory 0:6ad07c9019fd 190 // Release lock before blocking so other threads
Bethory 0:6ad07c9019fd 191 // accessing this object aren't blocked
Bethory 0:6ad07c9019fd 192 _lock.unlock();
Bethory 0:6ad07c9019fd 193 flag = _event_flag.wait_any(READ_FLAG, _timeout);
Bethory 0:6ad07c9019fd 194 _lock.lock();
Bethory 0:6ad07c9019fd 195
Bethory 0:6ad07c9019fd 196 if (flag & osFlagsError) {
Bethory 0:6ad07c9019fd 197 // Timeout break
Bethory 0:6ad07c9019fd 198 ret = NSAPI_ERROR_WOULD_BLOCK;
Bethory 0:6ad07c9019fd 199 break;
Bethory 0:6ad07c9019fd 200 }
Bethory 0:6ad07c9019fd 201 }
Bethory 0:6ad07c9019fd 202 }
Bethory 0:6ad07c9019fd 203
Bethory 0:6ad07c9019fd 204 _read_in_progress = false;
Bethory 0:6ad07c9019fd 205 _lock.unlock();
Bethory 0:6ad07c9019fd 206 return ret;
Bethory 0:6ad07c9019fd 207 }
Bethory 0:6ad07c9019fd 208
Bethory 0:6ad07c9019fd 209 void TCPSocket::event()
Bethory 0:6ad07c9019fd 210 {
Bethory 0:6ad07c9019fd 211 _event_flag.set(READ_FLAG|WRITE_FLAG);
Bethory 0:6ad07c9019fd 212
Bethory 0:6ad07c9019fd 213 _pending += 1;
Bethory 0:6ad07c9019fd 214 if (_callback && _pending == 1) {
Bethory 0:6ad07c9019fd 215 _callback();
Bethory 0:6ad07c9019fd 216 }
Bethory 0:6ad07c9019fd 217 }