Backup 1

Committer:
borlanic
Date:
Tue Apr 24 11:45:18 2018 +0000
Revision:
0:02dd72d1d465
BaBoRo_test2 - backup 1

Who changed what in which revision?

UserRevisionLine numberNew contents of line
borlanic 0:02dd72d1d465 1 /* mbed Microcontroller Library
borlanic 0:02dd72d1d465 2 * Copyright (c) 2017 ARM Limited
borlanic 0:02dd72d1d465 3 *
borlanic 0:02dd72d1d465 4 * Licensed under the Apache License, Version 2.0 (the "License");
borlanic 0:02dd72d1d465 5 * you may not use this file except in compliance with the License.
borlanic 0:02dd72d1d465 6 * You may obtain a copy of the License at
borlanic 0:02dd72d1d465 7 *
borlanic 0:02dd72d1d465 8 * http://www.apache.org/licenses/LICENSE-2.0
borlanic 0:02dd72d1d465 9 *
borlanic 0:02dd72d1d465 10 * Unless required by applicable law or agreed to in writing, software
borlanic 0:02dd72d1d465 11 * distributed under the License is distributed on an "AS IS" BASIS,
borlanic 0:02dd72d1d465 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
borlanic 0:02dd72d1d465 13 * See the License for the specific language governing permissions and
borlanic 0:02dd72d1d465 14 * limitations under the License.
borlanic 0:02dd72d1d465 15 */
borlanic 0:02dd72d1d465 16 #include "mbed_poll.h"
borlanic 0:02dd72d1d465 17 #include "FileHandle.h"
borlanic 0:02dd72d1d465 18 #include "Timer.h"
borlanic 0:02dd72d1d465 19 #include "LowPowerTimer.h"
borlanic 0:02dd72d1d465 20 #ifdef MBED_CONF_RTOS_PRESENT
borlanic 0:02dd72d1d465 21 #include "rtos/Thread.h"
borlanic 0:02dd72d1d465 22 #endif
borlanic 0:02dd72d1d465 23
borlanic 0:02dd72d1d465 24 namespace mbed {
borlanic 0:02dd72d1d465 25
borlanic 0:02dd72d1d465 26 // timeout -1 forever, or milliseconds
borlanic 0:02dd72d1d465 27 int poll(pollfh fhs[], unsigned nfhs, int timeout)
borlanic 0:02dd72d1d465 28 {
borlanic 0:02dd72d1d465 29 /**
borlanic 0:02dd72d1d465 30 * TODO Proper wake-up mechanism.
borlanic 0:02dd72d1d465 31 * In order to correctly detect availability of read/write a FileHandle, we needed
borlanic 0:02dd72d1d465 32 * a select or poll mechanisms. We opted for poll as POSIX defines in
borlanic 0:02dd72d1d465 33 * http://pubs.opengroup.org/onlinepubs/009695399/functions/poll.html Currently,
borlanic 0:02dd72d1d465 34 * mbed::poll() just spins and scans filehandles looking for any events we are
borlanic 0:02dd72d1d465 35 * interested in. In future, his spinning behaviour will be replaced with
borlanic 0:02dd72d1d465 36 * condition variables.
borlanic 0:02dd72d1d465 37 */
borlanic 0:02dd72d1d465 38 #if MBED_CONF_PLATFORM_POLL_USE_LOWPOWER_TIMER
borlanic 0:02dd72d1d465 39 LowPowerTimer timer;
borlanic 0:02dd72d1d465 40 #else
borlanic 0:02dd72d1d465 41 Timer timer;
borlanic 0:02dd72d1d465 42 #endif
borlanic 0:02dd72d1d465 43 if (timeout > 0) {
borlanic 0:02dd72d1d465 44 timer.start();
borlanic 0:02dd72d1d465 45 }
borlanic 0:02dd72d1d465 46
borlanic 0:02dd72d1d465 47 int count = 0;
borlanic 0:02dd72d1d465 48 for (;;) {
borlanic 0:02dd72d1d465 49 /* Scan the file handles */
borlanic 0:02dd72d1d465 50 for (unsigned n = 0; n < nfhs; n++) {
borlanic 0:02dd72d1d465 51 FileHandle *fh = fhs[n].fh;
borlanic 0:02dd72d1d465 52 short mask = fhs[n].events | POLLERR | POLLHUP | POLLNVAL;
borlanic 0:02dd72d1d465 53 if (fh) {
borlanic 0:02dd72d1d465 54 fhs[n].revents = fh->poll(mask) & mask;
borlanic 0:02dd72d1d465 55 } else {
borlanic 0:02dd72d1d465 56 fhs[n].revents = POLLNVAL;
borlanic 0:02dd72d1d465 57 }
borlanic 0:02dd72d1d465 58 if (fhs[n].revents) {
borlanic 0:02dd72d1d465 59 count++;
borlanic 0:02dd72d1d465 60 }
borlanic 0:02dd72d1d465 61 }
borlanic 0:02dd72d1d465 62
borlanic 0:02dd72d1d465 63 if (count) {
borlanic 0:02dd72d1d465 64 break;
borlanic 0:02dd72d1d465 65 }
borlanic 0:02dd72d1d465 66
borlanic 0:02dd72d1d465 67 /* Nothing selected - this is where timeout handling would be needed */
borlanic 0:02dd72d1d465 68 if (timeout == 0 || (timeout > 0 && timer.read_ms() > timeout)) {
borlanic 0:02dd72d1d465 69 break;
borlanic 0:02dd72d1d465 70 }
borlanic 0:02dd72d1d465 71 #ifdef MBED_CONF_RTOS_PRESENT
borlanic 0:02dd72d1d465 72 // TODO - proper blocking
borlanic 0:02dd72d1d465 73 // wait for condition variable, wait queue whatever here
borlanic 0:02dd72d1d465 74 rtos::Thread::wait(1);
borlanic 0:02dd72d1d465 75 #endif
borlanic 0:02dd72d1d465 76 }
borlanic 0:02dd72d1d465 77 return count;
borlanic 0:02dd72d1d465 78 }
borlanic 0:02dd72d1d465 79
borlanic 0:02dd72d1d465 80 } // namespace mbed