Entrega 3er corte - sistemas embebidos

Committer:
Bethory
Date:
Wed May 30 04:46:28 2018 +0000
Revision:
1:fcdb45ee95b9
Parent:
0:6ad07c9019fd
Entrega Final

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 "Socket.h"
Bethory 0:6ad07c9019fd 18 #include "mbed.h"
Bethory 0:6ad07c9019fd 19
Bethory 0:6ad07c9019fd 20 Socket::Socket()
Bethory 0:6ad07c9019fd 21 : _stack(0)
Bethory 0:6ad07c9019fd 22 , _socket(0)
Bethory 0:6ad07c9019fd 23 , _timeout(osWaitForever)
Bethory 0:6ad07c9019fd 24 {
Bethory 0:6ad07c9019fd 25 }
Bethory 0:6ad07c9019fd 26
Bethory 0:6ad07c9019fd 27 nsapi_error_t Socket::open(NetworkStack *stack)
Bethory 0:6ad07c9019fd 28 {
Bethory 0:6ad07c9019fd 29 _lock.lock();
Bethory 0:6ad07c9019fd 30
Bethory 0:6ad07c9019fd 31 if (_stack != NULL || stack == NULL) {
Bethory 0:6ad07c9019fd 32 _lock.unlock();
Bethory 0:6ad07c9019fd 33 return NSAPI_ERROR_PARAMETER;
Bethory 0:6ad07c9019fd 34 }
Bethory 0:6ad07c9019fd 35 _stack = stack;
Bethory 0:6ad07c9019fd 36
Bethory 0:6ad07c9019fd 37 nsapi_socket_t socket;
Bethory 0:6ad07c9019fd 38 nsapi_error_t err = _stack->socket_open(&socket, get_proto());
Bethory 0:6ad07c9019fd 39 if (err) {
Bethory 0:6ad07c9019fd 40 _lock.unlock();
Bethory 0:6ad07c9019fd 41 return err;
Bethory 0:6ad07c9019fd 42 }
Bethory 0:6ad07c9019fd 43
Bethory 0:6ad07c9019fd 44 _socket = socket;
Bethory 0:6ad07c9019fd 45 _event = callback(this, &Socket::event);
Bethory 0:6ad07c9019fd 46 _stack->socket_attach(_socket, Callback<void()>::thunk, &_event);
Bethory 0:6ad07c9019fd 47
Bethory 0:6ad07c9019fd 48 _lock.unlock();
Bethory 0:6ad07c9019fd 49 return NSAPI_ERROR_OK;
Bethory 0:6ad07c9019fd 50 }
Bethory 0:6ad07c9019fd 51
Bethory 0:6ad07c9019fd 52 nsapi_error_t Socket::close()
Bethory 0:6ad07c9019fd 53 {
Bethory 0:6ad07c9019fd 54 _lock.lock();
Bethory 0:6ad07c9019fd 55
Bethory 0:6ad07c9019fd 56 nsapi_error_t ret = NSAPI_ERROR_OK;
Bethory 0:6ad07c9019fd 57 if (_socket) {
Bethory 0:6ad07c9019fd 58 _stack->socket_attach(_socket, 0, 0);
Bethory 0:6ad07c9019fd 59 nsapi_socket_t socket = _socket;
Bethory 0:6ad07c9019fd 60 _socket = 0;
Bethory 0:6ad07c9019fd 61 ret = _stack->socket_close(socket);
Bethory 0:6ad07c9019fd 62 }
Bethory 0:6ad07c9019fd 63 _stack = 0;
Bethory 0:6ad07c9019fd 64
Bethory 0:6ad07c9019fd 65 // Wakeup anything in a blocking operation
Bethory 0:6ad07c9019fd 66 // on this socket
Bethory 0:6ad07c9019fd 67 event();
Bethory 0:6ad07c9019fd 68
Bethory 0:6ad07c9019fd 69 _lock.unlock();
Bethory 0:6ad07c9019fd 70 return ret;
Bethory 0:6ad07c9019fd 71 }
Bethory 0:6ad07c9019fd 72
Bethory 0:6ad07c9019fd 73 int Socket::modify_multicast_group(const SocketAddress &address, nsapi_socket_option_t socketopt)
Bethory 0:6ad07c9019fd 74 {
Bethory 0:6ad07c9019fd 75 nsapi_ip_mreq_t mreq;
Bethory 0:6ad07c9019fd 76
Bethory 0:6ad07c9019fd 77 // Set up group address
Bethory 0:6ad07c9019fd 78 mreq.imr_multiaddr = address.get_addr();
Bethory 0:6ad07c9019fd 79 mreq.imr_interface = nsapi_addr_t(); // Default address, NSAPI_UNSPEC
Bethory 0:6ad07c9019fd 80
Bethory 0:6ad07c9019fd 81 return this->setsockopt(NSAPI_SOCKET, socketopt, &mreq, sizeof(mreq));
Bethory 0:6ad07c9019fd 82 }
Bethory 0:6ad07c9019fd 83
Bethory 0:6ad07c9019fd 84 int Socket::join_multicast_group(const SocketAddress &address)
Bethory 0:6ad07c9019fd 85 {
Bethory 0:6ad07c9019fd 86 return modify_multicast_group(address, NSAPI_ADD_MEMBERSHIP);
Bethory 0:6ad07c9019fd 87 }
Bethory 0:6ad07c9019fd 88
Bethory 0:6ad07c9019fd 89 int Socket::leave_multicast_group(const SocketAddress &address)
Bethory 0:6ad07c9019fd 90 {
Bethory 0:6ad07c9019fd 91 return modify_multicast_group(address, NSAPI_DROP_MEMBERSHIP);
Bethory 0:6ad07c9019fd 92 }
Bethory 0:6ad07c9019fd 93
Bethory 0:6ad07c9019fd 94
Bethory 0:6ad07c9019fd 95 nsapi_error_t Socket::bind(uint16_t port)
Bethory 0:6ad07c9019fd 96 {
Bethory 0:6ad07c9019fd 97 // Underlying bind is thread safe
Bethory 0:6ad07c9019fd 98 SocketAddress addr(0, port);
Bethory 0:6ad07c9019fd 99 return bind(addr);
Bethory 0:6ad07c9019fd 100 }
Bethory 0:6ad07c9019fd 101
Bethory 0:6ad07c9019fd 102 nsapi_error_t Socket::bind(const char *address, uint16_t port)
Bethory 0:6ad07c9019fd 103 {
Bethory 0:6ad07c9019fd 104 // Underlying bind is thread safe
Bethory 0:6ad07c9019fd 105 SocketAddress addr(address, port);
Bethory 0:6ad07c9019fd 106 return bind(addr);
Bethory 0:6ad07c9019fd 107 }
Bethory 0:6ad07c9019fd 108
Bethory 0:6ad07c9019fd 109 nsapi_error_t Socket::bind(const SocketAddress &address)
Bethory 0:6ad07c9019fd 110 {
Bethory 0:6ad07c9019fd 111 _lock.lock();
Bethory 0:6ad07c9019fd 112 nsapi_error_t ret;
Bethory 0:6ad07c9019fd 113
Bethory 0:6ad07c9019fd 114 if (!_socket) {
Bethory 0:6ad07c9019fd 115 ret = NSAPI_ERROR_NO_SOCKET;
Bethory 0:6ad07c9019fd 116 } else {
Bethory 0:6ad07c9019fd 117 ret = _stack->socket_bind(_socket, address);
Bethory 0:6ad07c9019fd 118 }
Bethory 0:6ad07c9019fd 119
Bethory 0:6ad07c9019fd 120 _lock.unlock();
Bethory 0:6ad07c9019fd 121 return ret;
Bethory 0:6ad07c9019fd 122 }
Bethory 0:6ad07c9019fd 123
Bethory 0:6ad07c9019fd 124 void Socket::set_blocking(bool blocking)
Bethory 0:6ad07c9019fd 125 {
Bethory 0:6ad07c9019fd 126 // Socket::set_timeout is thread safe
Bethory 0:6ad07c9019fd 127 set_timeout(blocking ? -1 : 0);
Bethory 0:6ad07c9019fd 128 }
Bethory 0:6ad07c9019fd 129
Bethory 0:6ad07c9019fd 130 void Socket::set_timeout(int timeout)
Bethory 0:6ad07c9019fd 131 {
Bethory 0:6ad07c9019fd 132 _lock.lock();
Bethory 0:6ad07c9019fd 133
Bethory 0:6ad07c9019fd 134 if (timeout >= 0) {
Bethory 0:6ad07c9019fd 135 _timeout = (uint32_t)timeout;
Bethory 0:6ad07c9019fd 136 } else {
Bethory 0:6ad07c9019fd 137 _timeout = osWaitForever;
Bethory 0:6ad07c9019fd 138 }
Bethory 0:6ad07c9019fd 139
Bethory 0:6ad07c9019fd 140 _lock.unlock();
Bethory 0:6ad07c9019fd 141 }
Bethory 0:6ad07c9019fd 142
Bethory 0:6ad07c9019fd 143 nsapi_error_t Socket::setsockopt(int level, int optname, const void *optval, unsigned optlen)
Bethory 0:6ad07c9019fd 144 {
Bethory 0:6ad07c9019fd 145 _lock.lock();
Bethory 0:6ad07c9019fd 146 nsapi_error_t ret;
Bethory 0:6ad07c9019fd 147
Bethory 0:6ad07c9019fd 148 if (!_socket) {
Bethory 0:6ad07c9019fd 149 ret = NSAPI_ERROR_NO_SOCKET;
Bethory 0:6ad07c9019fd 150 } else {
Bethory 0:6ad07c9019fd 151 ret = _stack->setsockopt(_socket, level, optname, optval, optlen);
Bethory 0:6ad07c9019fd 152 }
Bethory 0:6ad07c9019fd 153
Bethory 0:6ad07c9019fd 154 _lock.unlock();
Bethory 0:6ad07c9019fd 155 return ret;
Bethory 0:6ad07c9019fd 156 }
Bethory 0:6ad07c9019fd 157
Bethory 0:6ad07c9019fd 158 nsapi_error_t Socket::getsockopt(int level, int optname, void *optval, unsigned *optlen)
Bethory 0:6ad07c9019fd 159 {
Bethory 0:6ad07c9019fd 160 _lock.lock();
Bethory 0:6ad07c9019fd 161 nsapi_error_t ret;
Bethory 0:6ad07c9019fd 162
Bethory 0:6ad07c9019fd 163 if (!_socket) {
Bethory 0:6ad07c9019fd 164 ret = NSAPI_ERROR_NO_SOCKET;
Bethory 0:6ad07c9019fd 165 } else {
Bethory 0:6ad07c9019fd 166 ret = _stack->getsockopt(_socket, level, optname, optval, optlen);
Bethory 0:6ad07c9019fd 167 }
Bethory 0:6ad07c9019fd 168
Bethory 0:6ad07c9019fd 169 _lock.unlock();
Bethory 0:6ad07c9019fd 170 return ret;
Bethory 0:6ad07c9019fd 171
Bethory 0:6ad07c9019fd 172 }
Bethory 0:6ad07c9019fd 173
Bethory 0:6ad07c9019fd 174 void Socket::sigio(Callback<void()> callback)
Bethory 0:6ad07c9019fd 175 {
Bethory 0:6ad07c9019fd 176 _lock.lock();
Bethory 0:6ad07c9019fd 177 _callback = callback;
Bethory 0:6ad07c9019fd 178 _lock.unlock();
Bethory 0:6ad07c9019fd 179 }
Bethory 0:6ad07c9019fd 180
Bethory 0:6ad07c9019fd 181 void Socket::attach(Callback<void()> callback)
Bethory 0:6ad07c9019fd 182 {
Bethory 0:6ad07c9019fd 183 sigio(callback);
Bethory 0:6ad07c9019fd 184 }