Preliminary main mbed library for nexpaq development

Committer:
nexpaq
Date:
Fri Nov 04 20:54:50 2016 +0000
Revision:
1:d96dbedaebdb
Parent:
0:6c56fb4bc5f0
Removed extra directories for other platforms

Who changed what in which revision?

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