Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Socket.cpp Source File

Socket.cpp

00001 /* Socket
00002  * Copyright (c) 2015 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #include "Socket.h"
00018 #include "mbed.h"
00019 
00020 Socket::Socket()
00021     : _stack(0)
00022     , _socket(0)
00023     , _timeout(osWaitForever)
00024 {
00025 }
00026 
00027 nsapi_error_t Socket::open(NetworkStack *stack)
00028 {
00029     _lock.lock();
00030 
00031     if (_stack != NULL || stack == NULL) {
00032         _lock.unlock();
00033         return NSAPI_ERROR_PARAMETER ;
00034     }
00035     _stack = stack;
00036 
00037     nsapi_socket_t socket;
00038     nsapi_error_t err = _stack->socket_open(&socket, get_proto());
00039     if (err) {
00040         _lock.unlock();
00041         return err;
00042     }
00043 
00044     _socket = socket;
00045     _event = callback(this, &Socket::event);
00046     _stack->socket_attach(_socket, Callback<void()>::thunk, &_event);
00047 
00048     _lock.unlock();
00049     return NSAPI_ERROR_OK ;
00050 }
00051 
00052 nsapi_error_t Socket::close()
00053 {
00054     _lock.lock();
00055 
00056     nsapi_error_t ret = NSAPI_ERROR_OK ;
00057     if (_socket) {
00058         _stack->socket_attach(_socket, 0, 0);
00059         nsapi_socket_t socket = _socket;
00060         _socket = 0;
00061         ret = _stack->socket_close(socket);
00062     }
00063     _stack = 0;
00064 
00065     // Wakeup anything in a blocking operation
00066     // on this socket
00067     event();
00068 
00069     _lock.unlock();
00070     return ret;
00071 }
00072 
00073 int Socket::modify_multicast_group(const SocketAddress &address, nsapi_socket_option_t socketopt)
00074 {
00075     nsapi_ip_mreq_t mreq;
00076 
00077     // Set up group address
00078     mreq.imr_multiaddr = address.get_addr();
00079     mreq.imr_interface = nsapi_addr_t();   // Default address, NSAPI_UNSPEC
00080 
00081     return this->setsockopt(NSAPI_SOCKET , socketopt, &mreq, sizeof(mreq));
00082 }
00083 
00084 int Socket::join_multicast_group(const SocketAddress &address)
00085 {
00086     return modify_multicast_group(address, NSAPI_ADD_MEMBERSHIP );
00087 }
00088 
00089 int Socket::leave_multicast_group(const SocketAddress &address)
00090 {
00091     return modify_multicast_group(address, NSAPI_DROP_MEMBERSHIP );
00092 }
00093 
00094 
00095 nsapi_error_t Socket::bind(uint16_t port)
00096 {
00097     // Underlying bind is thread safe
00098     SocketAddress addr(0, port);
00099     return bind(addr);
00100 }
00101 
00102 nsapi_error_t Socket::bind(const char *address, uint16_t port)
00103 {
00104     // Underlying bind is thread safe
00105     SocketAddress addr(address, port);
00106     return bind(addr);
00107 }
00108 
00109 nsapi_error_t Socket::bind(const SocketAddress &address)
00110 {
00111     _lock.lock();
00112     nsapi_error_t ret;
00113 
00114     if (!_socket) {
00115         ret = NSAPI_ERROR_NO_SOCKET ;
00116     } else {
00117         ret = _stack->socket_bind(_socket, address);
00118     }
00119 
00120     _lock.unlock();
00121     return ret;
00122 }
00123 
00124 void Socket::set_blocking(bool blocking)
00125 {
00126     // Socket::set_timeout is thread safe
00127     set_timeout(blocking ? -1 : 0);
00128 }
00129 
00130 void Socket::set_timeout(int timeout)
00131 {
00132     _lock.lock();
00133 
00134     if (timeout >= 0) {
00135         _timeout = (uint32_t)timeout;
00136     } else {
00137         _timeout = osWaitForever;
00138     }
00139 
00140     _lock.unlock();
00141 }
00142 
00143 nsapi_error_t Socket::setsockopt(int level, int optname, const void *optval, unsigned optlen)
00144 {
00145     _lock.lock();
00146     nsapi_error_t ret;
00147 
00148     if (!_socket) {
00149         ret = NSAPI_ERROR_NO_SOCKET ;
00150     } else {
00151         ret = _stack->setsockopt(_socket, level, optname, optval, optlen);
00152     }
00153 
00154     _lock.unlock();
00155     return ret;
00156 }
00157 
00158 nsapi_error_t Socket::getsockopt(int level, int optname, void *optval, unsigned *optlen)
00159 {
00160     _lock.lock();
00161     nsapi_error_t ret;
00162 
00163     if (!_socket) {
00164         ret = NSAPI_ERROR_NO_SOCKET ;
00165     } else {
00166         ret = _stack->getsockopt(_socket, level, optname, optval, optlen);
00167     }
00168 
00169     _lock.unlock();
00170     return ret;
00171 
00172 }
00173 
00174 void Socket::sigio(Callback<void()> callback)
00175 {
00176     _lock.lock();
00177     _callback = callback;
00178     _lock.unlock();
00179 }
00180 
00181 void Socket::attach(Callback<void()> callback)
00182 {
00183     sigio(callback);
00184 }