mbed-dev-f303

Committer:
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4
Date:
Tue Jun 14 09:21:18 2022 +0000
Revision:
0:bdf663c61a82
lib

Who changed what in which revision?

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