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