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