SDCard version

Fork of gr-peach-opencv-project-sd-card by the do

Committer:
thedo
Date:
Fri Jul 21 01:26:02 2017 +0000
Revision:
166:240bc5a0f42a
gr-peach-opencv-project-sd-card

Who changed what in which revision?

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