mbed os with nrf51 internal bandgap enabled to read battery level

Dependents:   BLE_file_test BLE_Blink ExternalEncoder

Committer:
elessair
Date:
Sun Oct 23 15:10:02 2016 +0000
Revision:
0:f269e3021894
Initial commit

Who changed what in which revision?

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