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