mbed-os5 only for TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Committer:
kenjiArai
Date:
Tue Dec 31 06:02:27 2019 +0000
Revision:
1:9db0e321a9f4
updated based on mbed-os5.15.0

Who changed what in which revision?

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