Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
mbed-os/platform/mbed_poll.cpp@0:fb8047b156bb, 2020-01-15 (annotated)
- Committer:
- rahul_dahiya
- Date:
- Wed Jan 15 15:57:15 2020 +0530
- Revision:
- 0:fb8047b156bb
STM32F7 LWIP
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rahul_dahiya |
0:fb8047b156bb | 1 | /* mbed Microcontroller Library |
rahul_dahiya |
0:fb8047b156bb | 2 | * Copyright (c) 2017 ARM Limited |
rahul_dahiya |
0:fb8047b156bb | 3 | * |
rahul_dahiya |
0:fb8047b156bb | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
rahul_dahiya |
0:fb8047b156bb | 5 | * you may not use this file except in compliance with the License. |
rahul_dahiya |
0:fb8047b156bb | 6 | * You may obtain a copy of the License at |
rahul_dahiya |
0:fb8047b156bb | 7 | * |
rahul_dahiya |
0:fb8047b156bb | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
rahul_dahiya |
0:fb8047b156bb | 9 | * |
rahul_dahiya |
0:fb8047b156bb | 10 | * Unless required by applicable law or agreed to in writing, software |
rahul_dahiya |
0:fb8047b156bb | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
rahul_dahiya |
0:fb8047b156bb | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
rahul_dahiya |
0:fb8047b156bb | 13 | * See the License for the specific language governing permissions and |
rahul_dahiya |
0:fb8047b156bb | 14 | * limitations under the License. |
rahul_dahiya |
0:fb8047b156bb | 15 | */ |
rahul_dahiya |
0:fb8047b156bb | 16 | #include "mbed_poll.h" |
rahul_dahiya |
0:fb8047b156bb | 17 | #include "FileHandle.h" |
rahul_dahiya |
0:fb8047b156bb | 18 | #include "Timer.h" |
rahul_dahiya |
0:fb8047b156bb | 19 | #ifdef MBED_CONF_RTOS_PRESENT |
rahul_dahiya |
0:fb8047b156bb | 20 | #include "rtos/Thread.h" |
rahul_dahiya |
0:fb8047b156bb | 21 | #endif |
rahul_dahiya |
0:fb8047b156bb | 22 | |
rahul_dahiya |
0:fb8047b156bb | 23 | namespace mbed { |
rahul_dahiya |
0:fb8047b156bb | 24 | |
rahul_dahiya |
0:fb8047b156bb | 25 | // timeout -1 forever, or milliseconds |
rahul_dahiya |
0:fb8047b156bb | 26 | int poll(pollfh fhs[], unsigned nfhs, int timeout) |
rahul_dahiya |
0:fb8047b156bb | 27 | { |
rahul_dahiya |
0:fb8047b156bb | 28 | /** |
rahul_dahiya |
0:fb8047b156bb | 29 | * TODO Proper wake-up mechanism. |
rahul_dahiya |
0:fb8047b156bb | 30 | * In order to correctly detect availability of read/write a FileHandle, we needed |
rahul_dahiya |
0:fb8047b156bb | 31 | * a select or poll mechanisms. We opted for poll as POSIX defines in |
rahul_dahiya |
0:fb8047b156bb | 32 | * http://pubs.opengroup.org/onlinepubs/009695399/functions/poll.html Currently, |
rahul_dahiya |
0:fb8047b156bb | 33 | * mbed::poll() just spins and scans filehandles looking for any events we are |
rahul_dahiya |
0:fb8047b156bb | 34 | * interested in. In future, his spinning behaviour will be replaced with |
rahul_dahiya |
0:fb8047b156bb | 35 | * condition variables. |
rahul_dahiya |
0:fb8047b156bb | 36 | */ |
rahul_dahiya |
0:fb8047b156bb | 37 | Timer timer; |
rahul_dahiya |
0:fb8047b156bb | 38 | if (timeout > 0) { |
rahul_dahiya |
0:fb8047b156bb | 39 | timer.start(); |
rahul_dahiya |
0:fb8047b156bb | 40 | } |
rahul_dahiya |
0:fb8047b156bb | 41 | |
rahul_dahiya |
0:fb8047b156bb | 42 | int count = 0; |
rahul_dahiya |
0:fb8047b156bb | 43 | for (;;) { |
rahul_dahiya |
0:fb8047b156bb | 44 | /* Scan the file handles */ |
rahul_dahiya |
0:fb8047b156bb | 45 | for (unsigned n = 0; n < nfhs; n++) { |
rahul_dahiya |
0:fb8047b156bb | 46 | FileHandle *fh = fhs[n].fh; |
rahul_dahiya |
0:fb8047b156bb | 47 | short mask = fhs[n].events | POLLERR | POLLHUP | POLLNVAL; |
rahul_dahiya |
0:fb8047b156bb | 48 | if (fh) { |
rahul_dahiya |
0:fb8047b156bb | 49 | fhs[n].revents = fh->poll(mask) & mask; |
rahul_dahiya |
0:fb8047b156bb | 50 | } else { |
rahul_dahiya |
0:fb8047b156bb | 51 | fhs[n].revents = POLLNVAL; |
rahul_dahiya |
0:fb8047b156bb | 52 | } |
rahul_dahiya |
0:fb8047b156bb | 53 | if (fhs[n].revents) { |
rahul_dahiya |
0:fb8047b156bb | 54 | count++; |
rahul_dahiya |
0:fb8047b156bb | 55 | } |
rahul_dahiya |
0:fb8047b156bb | 56 | } |
rahul_dahiya |
0:fb8047b156bb | 57 | |
rahul_dahiya |
0:fb8047b156bb | 58 | if (count) { |
rahul_dahiya |
0:fb8047b156bb | 59 | break; |
rahul_dahiya |
0:fb8047b156bb | 60 | } |
rahul_dahiya |
0:fb8047b156bb | 61 | |
rahul_dahiya |
0:fb8047b156bb | 62 | /* Nothing selected - this is where timeout handling would be needed */ |
rahul_dahiya |
0:fb8047b156bb | 63 | if (timeout == 0 || (timeout > 0 && timer.read_ms() > timeout)) { |
rahul_dahiya |
0:fb8047b156bb | 64 | break; |
rahul_dahiya |
0:fb8047b156bb | 65 | } |
rahul_dahiya |
0:fb8047b156bb | 66 | #ifdef MBED_CONF_RTOS_PRESENT |
rahul_dahiya |
0:fb8047b156bb | 67 | // TODO - proper blocking |
rahul_dahiya |
0:fb8047b156bb | 68 | // wait for condition variable, wait queue whatever here |
rahul_dahiya |
0:fb8047b156bb | 69 | rtos::Thread::wait(1); |
rahul_dahiya |
0:fb8047b156bb | 70 | #endif |
rahul_dahiya |
0:fb8047b156bb | 71 | } |
rahul_dahiya |
0:fb8047b156bb | 72 | return count; |
rahul_dahiya |
0:fb8047b156bb | 73 | } |
rahul_dahiya |
0:fb8047b156bb | 74 | |
rahul_dahiya |
0:fb8047b156bb | 75 | } // namespace mbed |