joey shelton / LED_Demo

Dependencies:   MAX44000 PWM_Tone_Library nexpaq_mdk

Fork of LED_Demo by Maxim nexpaq

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 int 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     int err = _stack->socket_open(&socket, get_proto());
00039     if (err) {
00040         _lock.unlock();
00041         return err;
00042     }
00043 
00044     _socket = socket;
00045     _event.attach(this, &Socket::event);
00046     _stack->socket_attach(_socket, Callback<void()>::thunk, &_event);
00047 
00048     _lock.unlock();
00049     return 0;
00050 }
00051 
00052 int Socket::close()
00053 {
00054     _lock.lock();
00055 
00056     int ret = 0;
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 
00064     // Wakeup anything in a blocking operation
00065     // on this socket
00066     event();
00067 
00068     _lock.unlock();
00069     return ret;
00070 }
00071 
00072 int Socket::bind(uint16_t port)
00073 {
00074     // Underlying bind is thread safe
00075     SocketAddress addr(0, port);
00076     return bind(addr);
00077 }
00078 
00079 int Socket::bind(const char *address, uint16_t port)
00080 {
00081     // Underlying bind is thread safe
00082     SocketAddress addr(address, port);
00083     return bind(addr);
00084 }
00085 
00086 int Socket::bind(const SocketAddress &address)
00087 {
00088     _lock.lock();
00089     int ret;
00090 
00091     if (!_socket) {
00092         ret = NSAPI_ERROR_NO_SOCKET;
00093     } else {
00094         ret = _stack->socket_bind(_socket, address);
00095     }
00096 
00097     _lock.unlock();
00098     return ret;
00099 }
00100 
00101 void Socket::set_blocking(bool blocking)
00102 {
00103     // Socket::set_timeout is thread safe
00104     set_timeout(blocking ? -1 : 0);
00105 }
00106 
00107 void Socket::set_timeout(int timeout)
00108 {
00109     _lock.lock();
00110 
00111     if (timeout >= 0) {
00112         _timeout = (uint32_t)timeout;
00113     } else {
00114         _timeout = osWaitForever;
00115     }
00116 
00117     _lock.unlock();
00118 }
00119 
00120 int Socket::setsockopt(int level, int optname, const void *optval, unsigned optlen)
00121 {
00122     _lock.lock();
00123     int ret;
00124 
00125     if (!_socket) {
00126         ret = NSAPI_ERROR_NO_SOCKET;
00127     } else {
00128         ret = _stack->setsockopt(_socket, level, optname, optval, optlen);
00129     }
00130 
00131     _lock.unlock();
00132     return ret;
00133 }
00134 
00135 int Socket::getsockopt(int level, int optname, void *optval, unsigned *optlen)
00136 {
00137     _lock.lock();
00138     int ret;
00139 
00140     if (!_socket) {
00141         ret = NSAPI_ERROR_NO_SOCKET;
00142     } else {
00143         ret = _stack->getsockopt(_socket, level, optname, optval, optlen);
00144     }
00145 
00146     _lock.unlock();
00147     return ret;
00148 
00149 }
00150 
00151 void Socket::attach(Callback<void()> callback)
00152 {
00153     _lock.lock();
00154 
00155     _callback = callback;
00156 
00157     _lock.unlock();
00158 }