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