Development mbed library for MAX32630FTHR

Dependents:   blinky_max32630fthr

Committer:
switches
Date:
Fri Nov 11 20:59:50 2016 +0000
Revision:
0:5c4d7b2438d3
Initial commit

Who changed what in which revision?

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