forked from ARMmbed/mbed-os
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extending FileHandle & introducing mbed_poll
This has been an attempt to extend existing FileHandle to behave like POSIX file descriptor for input/output resources/devices too. This will now provide an abstract indicator/handle any arbitrary file or device type resource. May be in future, sockets too. In order to correctly detect availability of read/write a FileHandle, we needed a select or poll mechanisms. We opted for poll as POSIX defines in http://pubs.opengroup.org/onlinepubs/009695399/functions/poll.html Currently, mbed::poll() just spins and scans filehandles looking for any events we are interested in. In future, his spinning behaviour will be replaced with condition variables. In retarget.cpp we have introduced an mbed::fdopen() function which is equivalent to C fdopen(). It attaches a std stream to our FileHandle stream. newlib-nano somehow does not seem to call isatty() so retarget doesn't work for device type file handles. We handle this by checking ourselves in mbed::fdopen() if we wish to attach our stream to std stream. We also turn off buffering by C library as our stuff will be buffered already. sigio() is also provided, matching the API of the Socket class, with a view to future unification. As well as unblocking poll(), _poll_change calls the user sigio callback if an event happens, i.e., when FileHandle becomes readable/writable.
- Loading branch information
Hasnain Virk
committed
May 31, 2017
1 parent
fb7cbdf
commit b2408d8
Showing
7 changed files
with
335 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* mbed Microcontroller Library | ||
* Copyright (c) 2006-2016 ARM Limited | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
#include "FileHandle.h" | ||
#include "platform/mbed_retarget.h" | ||
#include "platform/mbed_critical.h" | ||
|
||
namespace mbed { | ||
|
||
off_t FileHandle::size() | ||
{ | ||
/* remember our current position */ | ||
off_t off = seek(0, SEEK_CUR); | ||
if (off < 0) { | ||
return off; | ||
} | ||
/* seek to the end to get the file length */ | ||
off_t size = seek(0, SEEK_END); | ||
/* return to our old position */ | ||
seek(off, SEEK_SET); | ||
return size; | ||
} | ||
|
||
void FileHandle::sigio(Callback<void()> func) { | ||
core_util_critical_section_enter(); | ||
_callback = func; | ||
if (_callback) { | ||
short current_events = poll(0x7FFF); | ||
if (current_events) { | ||
_callback(); | ||
} | ||
} | ||
core_util_critical_section_exit(); | ||
} | ||
|
||
std::FILE *fdopen(FileHandle *fh, const char *mode) | ||
{ | ||
return mbed_fdopen(fh, mode); | ||
} | ||
|
||
} // namespace mbed |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* mbed Microcontroller Library | ||
* Copyright (c) 2017 ARM Limited | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
#include "mbed_poll.h" | ||
#include "FileHandle.h" | ||
#include "Timer.h" | ||
#ifdef MBED_CONF_RTOS_PRESENT | ||
#include "rtos/Thread.h" | ||
#endif | ||
|
||
namespace mbed { | ||
|
||
// timeout -1 forever, or milliseconds | ||
int poll(pollfh fhs[], unsigned nfhs, int timeout) | ||
{ | ||
/** | ||
* TODO Proper wake-up mechanism. | ||
* In order to correctly detect availability of read/write a FileHandle, we needed | ||
* a select or poll mechanisms. We opted for poll as POSIX defines in | ||
* http://pubs.opengroup.org/onlinepubs/009695399/functions/poll.html Currently, | ||
* mbed::poll() just spins and scans filehandles looking for any events we are | ||
* interested in. In future, his spinning behaviour will be replaced with | ||
* condition variables. | ||
*/ | ||
Timer timer; | ||
if (timeout > 0) { | ||
timer.start(); | ||
} | ||
|
||
int count = 0; | ||
for (;;) { | ||
/* Scan the file handles */ | ||
for (unsigned n = 0; n < nfhs; n++) { | ||
FileHandle *fh = fhs[n].fh; | ||
short mask = fhs[n].events | POLLERR | POLLHUP | POLLNVAL; | ||
if (fh) { | ||
fhs[n].revents = fh->poll(mask) & mask; | ||
} else { | ||
fhs[n].revents = POLLNVAL; | ||
} | ||
if (fhs[n].revents) { | ||
count++; | ||
} | ||
} | ||
|
||
if (count) { | ||
break; | ||
} | ||
|
||
/* Nothing selected - this is where timeout handling would be needed */ | ||
if (timeout == 0 || (timeout > 0 && timer.read_ms() > timeout)) { | ||
break; | ||
} | ||
#ifdef MBED_CONF_RTOS_PRESENT | ||
// TODO - proper blocking | ||
// wait for condition variable, wait queue whatever here | ||
rtos::Thread::yield(); | ||
#endif | ||
} | ||
return count; | ||
} | ||
|
||
void _poll_change(FileHandle *fh) | ||
{ | ||
// TODO, will depend on how we implement poll | ||
|
||
// Also, do the user callback | ||
fh->_send_sigio(); | ||
} | ||
|
||
} // namespace mbed |
Oops, something went wrong.