BBR 1 Ebene

Committer:
borlanic
Date:
Mon May 14 11:29:06 2018 +0000
Revision:
0:fbdae7e6d805
BBR

Who changed what in which revision?

UserRevisionLine numberNew contents of line
borlanic 0:fbdae7e6d805 1 /* Socket
borlanic 0:fbdae7e6d805 2 * Copyright (c) 2015 ARM Limited
borlanic 0:fbdae7e6d805 3 *
borlanic 0:fbdae7e6d805 4 * Licensed under the Apache License, Version 2.0 (the "License");
borlanic 0:fbdae7e6d805 5 * you may not use this file except in compliance with the License.
borlanic 0:fbdae7e6d805 6 * You may obtain a copy of the License at
borlanic 0:fbdae7e6d805 7 *
borlanic 0:fbdae7e6d805 8 * http://www.apache.org/licenses/LICENSE-2.0
borlanic 0:fbdae7e6d805 9 *
borlanic 0:fbdae7e6d805 10 * Unless required by applicable law or agreed to in writing, software
borlanic 0:fbdae7e6d805 11 * distributed under the License is distributed on an "AS IS" BASIS,
borlanic 0:fbdae7e6d805 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
borlanic 0:fbdae7e6d805 13 * See the License for the specific language governing permissions and
borlanic 0:fbdae7e6d805 14 * limitations under the License.
borlanic 0:fbdae7e6d805 15 */
borlanic 0:fbdae7e6d805 16
borlanic 0:fbdae7e6d805 17 #include "Socket.h"
borlanic 0:fbdae7e6d805 18 #include "mbed.h"
borlanic 0:fbdae7e6d805 19
borlanic 0:fbdae7e6d805 20 Socket::Socket()
borlanic 0:fbdae7e6d805 21 : _stack(0)
borlanic 0:fbdae7e6d805 22 , _socket(0)
borlanic 0:fbdae7e6d805 23 , _timeout(osWaitForever)
borlanic 0:fbdae7e6d805 24 {
borlanic 0:fbdae7e6d805 25 }
borlanic 0:fbdae7e6d805 26
borlanic 0:fbdae7e6d805 27 nsapi_error_t Socket::open(NetworkStack *stack)
borlanic 0:fbdae7e6d805 28 {
borlanic 0:fbdae7e6d805 29 _lock.lock();
borlanic 0:fbdae7e6d805 30
borlanic 0:fbdae7e6d805 31 if (_stack != NULL || stack == NULL) {
borlanic 0:fbdae7e6d805 32 _lock.unlock();
borlanic 0:fbdae7e6d805 33 return NSAPI_ERROR_PARAMETER;
borlanic 0:fbdae7e6d805 34 }
borlanic 0:fbdae7e6d805 35 _stack = stack;
borlanic 0:fbdae7e6d805 36
borlanic 0:fbdae7e6d805 37 nsapi_socket_t socket;
borlanic 0:fbdae7e6d805 38 nsapi_error_t err = _stack->socket_open(&socket, get_proto());
borlanic 0:fbdae7e6d805 39 if (err) {
borlanic 0:fbdae7e6d805 40 _lock.unlock();
borlanic 0:fbdae7e6d805 41 return err;
borlanic 0:fbdae7e6d805 42 }
borlanic 0:fbdae7e6d805 43
borlanic 0:fbdae7e6d805 44 _socket = socket;
borlanic 0:fbdae7e6d805 45 _event = callback(this, &Socket::event);
borlanic 0:fbdae7e6d805 46 _stack->socket_attach(_socket, Callback<void()>::thunk, &_event);
borlanic 0:fbdae7e6d805 47
borlanic 0:fbdae7e6d805 48 _lock.unlock();
borlanic 0:fbdae7e6d805 49 return NSAPI_ERROR_OK;
borlanic 0:fbdae7e6d805 50 }
borlanic 0:fbdae7e6d805 51
borlanic 0:fbdae7e6d805 52 nsapi_error_t Socket::close()
borlanic 0:fbdae7e6d805 53 {
borlanic 0:fbdae7e6d805 54 _lock.lock();
borlanic 0:fbdae7e6d805 55
borlanic 0:fbdae7e6d805 56 nsapi_error_t ret = NSAPI_ERROR_OK;
borlanic 0:fbdae7e6d805 57 if (_socket) {
borlanic 0:fbdae7e6d805 58 _stack->socket_attach(_socket, 0, 0);
borlanic 0:fbdae7e6d805 59 nsapi_socket_t socket = _socket;
borlanic 0:fbdae7e6d805 60 _socket = 0;
borlanic 0:fbdae7e6d805 61 ret = _stack->socket_close(socket);
borlanic 0:fbdae7e6d805 62 }
borlanic 0:fbdae7e6d805 63 _stack = 0;
borlanic 0:fbdae7e6d805 64
borlanic 0:fbdae7e6d805 65 // Wakeup anything in a blocking operation
borlanic 0:fbdae7e6d805 66 // on this socket
borlanic 0:fbdae7e6d805 67 event();
borlanic 0:fbdae7e6d805 68
borlanic 0:fbdae7e6d805 69 _lock.unlock();
borlanic 0:fbdae7e6d805 70 return ret;
borlanic 0:fbdae7e6d805 71 }
borlanic 0:fbdae7e6d805 72
borlanic 0:fbdae7e6d805 73 int Socket::modify_multicast_group(const SocketAddress &address, nsapi_socket_option_t socketopt)
borlanic 0:fbdae7e6d805 74 {
borlanic 0:fbdae7e6d805 75 nsapi_ip_mreq_t mreq;
borlanic 0:fbdae7e6d805 76
borlanic 0:fbdae7e6d805 77 // Set up group address
borlanic 0:fbdae7e6d805 78 mreq.imr_multiaddr = address.get_addr();
borlanic 0:fbdae7e6d805 79 mreq.imr_interface = nsapi_addr_t(); // Default address, NSAPI_UNSPEC
borlanic 0:fbdae7e6d805 80
borlanic 0:fbdae7e6d805 81 return this->setsockopt(NSAPI_SOCKET, socketopt, &mreq, sizeof(mreq));
borlanic 0:fbdae7e6d805 82 }
borlanic 0:fbdae7e6d805 83
borlanic 0:fbdae7e6d805 84 int Socket::join_multicast_group(const SocketAddress &address)
borlanic 0:fbdae7e6d805 85 {
borlanic 0:fbdae7e6d805 86 return modify_multicast_group(address, NSAPI_ADD_MEMBERSHIP);
borlanic 0:fbdae7e6d805 87 }
borlanic 0:fbdae7e6d805 88
borlanic 0:fbdae7e6d805 89 int Socket::leave_multicast_group(const SocketAddress &address)
borlanic 0:fbdae7e6d805 90 {
borlanic 0:fbdae7e6d805 91 return modify_multicast_group(address, NSAPI_DROP_MEMBERSHIP);
borlanic 0:fbdae7e6d805 92 }
borlanic 0:fbdae7e6d805 93
borlanic 0:fbdae7e6d805 94
borlanic 0:fbdae7e6d805 95 nsapi_error_t Socket::bind(uint16_t port)
borlanic 0:fbdae7e6d805 96 {
borlanic 0:fbdae7e6d805 97 // Underlying bind is thread safe
borlanic 0:fbdae7e6d805 98 SocketAddress addr(0, port);
borlanic 0:fbdae7e6d805 99 return bind(addr);
borlanic 0:fbdae7e6d805 100 }
borlanic 0:fbdae7e6d805 101
borlanic 0:fbdae7e6d805 102 nsapi_error_t Socket::bind(const char *address, uint16_t port)
borlanic 0:fbdae7e6d805 103 {
borlanic 0:fbdae7e6d805 104 // Underlying bind is thread safe
borlanic 0:fbdae7e6d805 105 SocketAddress addr(address, port);
borlanic 0:fbdae7e6d805 106 return bind(addr);
borlanic 0:fbdae7e6d805 107 }
borlanic 0:fbdae7e6d805 108
borlanic 0:fbdae7e6d805 109 nsapi_error_t Socket::bind(const SocketAddress &address)
borlanic 0:fbdae7e6d805 110 {
borlanic 0:fbdae7e6d805 111 _lock.lock();
borlanic 0:fbdae7e6d805 112 nsapi_error_t ret;
borlanic 0:fbdae7e6d805 113
borlanic 0:fbdae7e6d805 114 if (!_socket) {
borlanic 0:fbdae7e6d805 115 ret = NSAPI_ERROR_NO_SOCKET;
borlanic 0:fbdae7e6d805 116 } else {
borlanic 0:fbdae7e6d805 117 ret = _stack->socket_bind(_socket, address);
borlanic 0:fbdae7e6d805 118 }
borlanic 0:fbdae7e6d805 119
borlanic 0:fbdae7e6d805 120 _lock.unlock();
borlanic 0:fbdae7e6d805 121 return ret;
borlanic 0:fbdae7e6d805 122 }
borlanic 0:fbdae7e6d805 123
borlanic 0:fbdae7e6d805 124 void Socket::set_blocking(bool blocking)
borlanic 0:fbdae7e6d805 125 {
borlanic 0:fbdae7e6d805 126 // Socket::set_timeout is thread safe
borlanic 0:fbdae7e6d805 127 set_timeout(blocking ? -1 : 0);
borlanic 0:fbdae7e6d805 128 }
borlanic 0:fbdae7e6d805 129
borlanic 0:fbdae7e6d805 130 void Socket::set_timeout(int timeout)
borlanic 0:fbdae7e6d805 131 {
borlanic 0:fbdae7e6d805 132 _lock.lock();
borlanic 0:fbdae7e6d805 133
borlanic 0:fbdae7e6d805 134 if (timeout >= 0) {
borlanic 0:fbdae7e6d805 135 _timeout = (uint32_t)timeout;
borlanic 0:fbdae7e6d805 136 } else {
borlanic 0:fbdae7e6d805 137 _timeout = osWaitForever;
borlanic 0:fbdae7e6d805 138 }
borlanic 0:fbdae7e6d805 139
borlanic 0:fbdae7e6d805 140 _lock.unlock();
borlanic 0:fbdae7e6d805 141 }
borlanic 0:fbdae7e6d805 142
borlanic 0:fbdae7e6d805 143 nsapi_error_t Socket::setsockopt(int level, int optname, const void *optval, unsigned optlen)
borlanic 0:fbdae7e6d805 144 {
borlanic 0:fbdae7e6d805 145 _lock.lock();
borlanic 0:fbdae7e6d805 146 nsapi_error_t ret;
borlanic 0:fbdae7e6d805 147
borlanic 0:fbdae7e6d805 148 if (!_socket) {
borlanic 0:fbdae7e6d805 149 ret = NSAPI_ERROR_NO_SOCKET;
borlanic 0:fbdae7e6d805 150 } else {
borlanic 0:fbdae7e6d805 151 ret = _stack->setsockopt(_socket, level, optname, optval, optlen);
borlanic 0:fbdae7e6d805 152 }
borlanic 0:fbdae7e6d805 153
borlanic 0:fbdae7e6d805 154 _lock.unlock();
borlanic 0:fbdae7e6d805 155 return ret;
borlanic 0:fbdae7e6d805 156 }
borlanic 0:fbdae7e6d805 157
borlanic 0:fbdae7e6d805 158 nsapi_error_t Socket::getsockopt(int level, int optname, void *optval, unsigned *optlen)
borlanic 0:fbdae7e6d805 159 {
borlanic 0:fbdae7e6d805 160 _lock.lock();
borlanic 0:fbdae7e6d805 161 nsapi_error_t ret;
borlanic 0:fbdae7e6d805 162
borlanic 0:fbdae7e6d805 163 if (!_socket) {
borlanic 0:fbdae7e6d805 164 ret = NSAPI_ERROR_NO_SOCKET;
borlanic 0:fbdae7e6d805 165 } else {
borlanic 0:fbdae7e6d805 166 ret = _stack->getsockopt(_socket, level, optname, optval, optlen);
borlanic 0:fbdae7e6d805 167 }
borlanic 0:fbdae7e6d805 168
borlanic 0:fbdae7e6d805 169 _lock.unlock();
borlanic 0:fbdae7e6d805 170 return ret;
borlanic 0:fbdae7e6d805 171
borlanic 0:fbdae7e6d805 172 }
borlanic 0:fbdae7e6d805 173
borlanic 0:fbdae7e6d805 174 void Socket::sigio(Callback<void()> callback)
borlanic 0:fbdae7e6d805 175 {
borlanic 0:fbdae7e6d805 176 _lock.lock();
borlanic 0:fbdae7e6d805 177 _callback = callback;
borlanic 0:fbdae7e6d805 178 _lock.unlock();
borlanic 0:fbdae7e6d805 179 }
borlanic 0:fbdae7e6d805 180
borlanic 0:fbdae7e6d805 181 void Socket::attach(Callback<void()> callback)
borlanic 0:fbdae7e6d805 182 {
borlanic 0:fbdae7e6d805 183 sigio(callback);
borlanic 0:fbdae7e6d805 184 }