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 "UDPSocket.h"
elessair 0:f269e3021894 18 #include "Timer.h"
elessair 0:f269e3021894 19 #include "mbed_assert.h"
elessair 0:f269e3021894 20
elessair 0:f269e3021894 21 UDPSocket::UDPSocket()
elessair 0:f269e3021894 22 : _pending(0), _read_sem(0), _write_sem(0),
elessair 0:f269e3021894 23 _read_in_progress(false), _write_in_progress(false)
elessair 0:f269e3021894 24 {
elessair 0:f269e3021894 25 }
elessair 0:f269e3021894 26
elessair 0:f269e3021894 27 UDPSocket::~UDPSocket()
elessair 0:f269e3021894 28 {
elessair 0:f269e3021894 29 close();
elessair 0:f269e3021894 30 }
elessair 0:f269e3021894 31
elessair 0:f269e3021894 32 nsapi_protocol_t UDPSocket::get_proto()
elessair 0:f269e3021894 33 {
elessair 0:f269e3021894 34 return NSAPI_UDP;
elessair 0:f269e3021894 35 }
elessair 0:f269e3021894 36
elessair 0:f269e3021894 37 int UDPSocket::sendto(const char *host, uint16_t port, const void *data, unsigned size)
elessair 0:f269e3021894 38 {
elessair 0:f269e3021894 39 SocketAddress address;
elessair 0:f269e3021894 40 int err = _stack->gethostbyname(host, &address);
elessair 0:f269e3021894 41 if (err) {
elessair 0:f269e3021894 42 return NSAPI_ERROR_DNS_FAILURE;
elessair 0:f269e3021894 43 }
elessair 0:f269e3021894 44
elessair 0:f269e3021894 45 address.set_port(port);
elessair 0:f269e3021894 46
elessair 0:f269e3021894 47 // sendto is thread safe
elessair 0:f269e3021894 48 return sendto(address, data, size);
elessair 0:f269e3021894 49 }
elessair 0:f269e3021894 50
elessair 0:f269e3021894 51 int UDPSocket::sendto(const SocketAddress &address, const void *data, unsigned size)
elessair 0:f269e3021894 52 {
elessair 0:f269e3021894 53 _lock.lock();
elessair 0:f269e3021894 54 int ret;
elessair 0:f269e3021894 55
elessair 0:f269e3021894 56 // If this assert is hit then there are two threads
elessair 0:f269e3021894 57 // performing a send at the same time which is undefined
elessair 0:f269e3021894 58 // behavior
elessair 0:f269e3021894 59 MBED_ASSERT(!_write_in_progress);
elessair 0:f269e3021894 60 _write_in_progress = true;
elessair 0:f269e3021894 61
elessair 0:f269e3021894 62 while (true) {
elessair 0:f269e3021894 63 if (!_socket) {
elessair 0:f269e3021894 64 ret = NSAPI_ERROR_NO_SOCKET;
elessair 0:f269e3021894 65 break;
elessair 0:f269e3021894 66 }
elessair 0:f269e3021894 67
elessair 0:f269e3021894 68 _pending = 0;
elessair 0:f269e3021894 69 int sent = _stack->socket_sendto(_socket, address, data, size);
elessair 0:f269e3021894 70 if ((0 == _timeout) || (NSAPI_ERROR_WOULD_BLOCK != sent)) {
elessair 0:f269e3021894 71 ret = sent;
elessair 0:f269e3021894 72 break;
elessair 0:f269e3021894 73 } else {
elessair 0:f269e3021894 74 int32_t count;
elessair 0:f269e3021894 75
elessair 0:f269e3021894 76 // Release lock before blocking so other threads
elessair 0:f269e3021894 77 // accessing this object aren't blocked
elessair 0:f269e3021894 78 _lock.unlock();
elessair 0:f269e3021894 79 count = _write_sem.wait(_timeout);
elessair 0:f269e3021894 80 _lock.lock();
elessair 0:f269e3021894 81
elessair 0:f269e3021894 82 if (count < 1) {
elessair 0:f269e3021894 83 // Semaphore wait timed out so break out and return
elessair 0:f269e3021894 84 ret = NSAPI_ERROR_WOULD_BLOCK;
elessair 0:f269e3021894 85 break;
elessair 0:f269e3021894 86 }
elessair 0:f269e3021894 87 }
elessair 0:f269e3021894 88 }
elessair 0:f269e3021894 89
elessair 0:f269e3021894 90 _write_in_progress = false;
elessair 0:f269e3021894 91 _lock.unlock();
elessair 0:f269e3021894 92 return ret;
elessair 0:f269e3021894 93 }
elessair 0:f269e3021894 94
elessair 0:f269e3021894 95 int UDPSocket::recvfrom(SocketAddress *address, void *buffer, unsigned size)
elessair 0:f269e3021894 96 {
elessair 0:f269e3021894 97 _lock.lock();
elessair 0:f269e3021894 98 int ret;
elessair 0:f269e3021894 99
elessair 0:f269e3021894 100 // If this assert is hit then there are two threads
elessair 0:f269e3021894 101 // performing a recv at the same time which is undefined
elessair 0:f269e3021894 102 // behavior
elessair 0:f269e3021894 103 MBED_ASSERT(!_read_in_progress);
elessair 0:f269e3021894 104 _read_in_progress = true;
elessair 0:f269e3021894 105
elessair 0:f269e3021894 106 while (true) {
elessair 0:f269e3021894 107 if (!_socket) {
elessair 0:f269e3021894 108 ret = NSAPI_ERROR_NO_SOCKET;
elessair 0:f269e3021894 109 break;
elessair 0:f269e3021894 110 }
elessair 0:f269e3021894 111
elessair 0:f269e3021894 112 _pending = 0;
elessair 0:f269e3021894 113 int recv = _stack->socket_recvfrom(_socket, address, buffer, size);
elessair 0:f269e3021894 114 if ((0 == _timeout) || (NSAPI_ERROR_WOULD_BLOCK != recv)) {
elessair 0:f269e3021894 115 ret = recv;
elessair 0:f269e3021894 116 break;
elessair 0:f269e3021894 117 } else {
elessair 0:f269e3021894 118 int32_t count;
elessair 0:f269e3021894 119
elessair 0:f269e3021894 120 // Release lock before blocking so other threads
elessair 0:f269e3021894 121 // accessing this object aren't blocked
elessair 0:f269e3021894 122 _lock.unlock();
elessair 0:f269e3021894 123 count = _read_sem.wait(_timeout);
elessair 0:f269e3021894 124 _lock.lock();
elessair 0:f269e3021894 125
elessair 0:f269e3021894 126 if (count < 1) {
elessair 0:f269e3021894 127 // Semaphore wait timed out so break out and return
elessair 0:f269e3021894 128 ret = NSAPI_ERROR_WOULD_BLOCK;
elessair 0:f269e3021894 129 break;
elessair 0:f269e3021894 130 }
elessair 0:f269e3021894 131 }
elessair 0:f269e3021894 132 }
elessair 0:f269e3021894 133
elessair 0:f269e3021894 134 _read_in_progress = false;
elessair 0:f269e3021894 135 _lock.unlock();
elessair 0:f269e3021894 136 return ret;
elessair 0:f269e3021894 137 }
elessair 0:f269e3021894 138
elessair 0:f269e3021894 139 void UDPSocket::event()
elessair 0:f269e3021894 140 {
elessair 0:f269e3021894 141 int32_t wcount = _write_sem.wait(0);
elessair 0:f269e3021894 142 if (wcount <= 1) {
elessair 0:f269e3021894 143 _write_sem.release();
elessair 0:f269e3021894 144 }
elessair 0:f269e3021894 145 int32_t rcount = _read_sem.wait(0);
elessair 0:f269e3021894 146 if (rcount <= 1) {
elessair 0:f269e3021894 147 _read_sem.release();
elessair 0:f269e3021894 148 }
elessair 0:f269e3021894 149
elessair 0:f269e3021894 150 _pending += 1;
elessair 0:f269e3021894 151 if (_callback && _pending == 1) {
elessair 0:f269e3021894 152 _callback();
elessair 0:f269e3021894 153 }
elessair 0:f269e3021894 154 }