Skip to content

Commit

Permalink
Extending FileHandle & introducing mbed_poll
Browse files Browse the repository at this point in the history
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
Show file tree
Hide file tree
Showing 7 changed files with 335 additions and 18 deletions.
17 changes: 17 additions & 0 deletions drivers/Serial.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,23 @@ class Serial : public SerialBase, public Stream {
*/
Serial(PinName tx, PinName rx, int baud);

/* Stream gives us a FileHandle with non-functional poll()/readable()/writable. Pass through
* the calls from the SerialBase instead for backwards compatibility. This problem is
* part of why Stream and Serial should be deprecated.
*/
bool readable()
{
return SerialBase::readable();
}
bool writable()
{
return SerialBase::writeable();
}
bool writeable()
{
return SerialBase::writeable();
}

protected:
virtual int _getc();
virtual int _putc(int c);
Expand Down
53 changes: 53 additions & 0 deletions platform/FileHandle.cpp
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
127 changes: 113 additions & 14 deletions platform/FileHandle.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* mbed Microcontroller Library
* Copyright (c) 2006-2013 ARM Limited
* 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.
Expand All @@ -18,7 +18,9 @@

typedef int FILEHANDLE;

#include <stdio.h>
#include <cstdio>
#include "Callback.h"
#include "platform/mbed_poll.h"
#include "platform/platform.h"

namespace mbed {
Expand All @@ -40,6 +42,12 @@ class FileHandle {
virtual ~FileHandle() {}

/** Read the contents of a file into a buffer
*
* Devices acting as FileHandles should follow POSIX semantics:
*
* * if no data is available, and non-blocking set return -EAGAIN
* * if no data is available, and blocking set, wait until data is available
* * If any data is available, call returns immediately
*
* @param buffer The buffer to read in to
* @param size The number of bytes to read
Expand All @@ -62,7 +70,7 @@ class FileHandle {
* SEEK_SET to start from beginning of file,
* SEEK_CUR to start from current position in file,
* SEEK_END to start from end of file
* @return The new offset of the file
* @return The new offset of the file, negative error code on failure
*/
virtual off_t seek(off_t offset, int whence = SEEK_SET) = 0;

Expand All @@ -84,6 +92,8 @@ class FileHandle {
/** Check if the file in an interactive terminal device
*
* @return True if the file is a terminal
* @return False if the file is not a terminal
* @return Negative error code on failure
*/
virtual int isatty()
{
Expand All @@ -94,7 +104,7 @@ class FileHandle {
*
* @note This is equivalent to seek(0, SEEK_CUR)
*
* @return The current offset in the file
* @return The current offset in the file, negative error code on failure
*/
virtual off_t tell()
{
Expand All @@ -114,13 +124,7 @@ class FileHandle {
*
* @return Size of the file in bytes
*/
virtual off_t size()
{
off_t off = tell();
off_t size = seek(0, SEEK_END);
seek(off, SEEK_SET);
return size;
}
virtual off_t size();

/** Move the file position to a given offset from a given location.
*
Expand All @@ -133,7 +137,10 @@ class FileHandle {
* -1 on failure or unsupported
*/
MBED_DEPRECATED_SINCE("mbed-os-5.4", "Replaced by FileHandle::seek")
virtual off_t lseek(off_t offset, int whence) { return seek(offset, whence); }
virtual off_t lseek(off_t offset, int whence)
{
return seek(offset, whence);
}

/** Flush any buffers associated with the FileHandle, ensuring it
* is up to date on disk
Expand All @@ -143,17 +150,109 @@ class FileHandle {
* -1 on error
*/
MBED_DEPRECATED_SINCE("mbed-os-5.4", "Replaced by FileHandle::sync")
virtual int fsync() { return sync(); }
virtual int fsync()
{
return sync();
}

/** Find the length of the file
*
* @returns
* Length of the file
*/
MBED_DEPRECATED_SINCE("mbed-os-5.4", "Replaced by FileHandle::size")
virtual off_t flen() { return size(); }
virtual off_t flen()
{
return size();
}

/** Set blocking or non-blocking mode of the file operation like read/write.
* Definition depends upon the subclass implementing FileHandle.
* The default is blocking.
*
* @param blocking true for blocking mode, false for non-blocking mode.
*/
virtual int set_blocking(bool blocking)
{
return -1;
}

/** Check for poll event flags
* The input parameter can be used or ignored - the could always return all events,
* or could check just the events listed in events.
* Call is non-blocking - returns instantaneous state of events.
* Whenever an event occurs, the derived class must call _poll_change().
* @param events bitmask of poll events we're interested in - POLLIN/POLLOUT etc.
*
* @returns
* bitmask of poll events that have occurred.
*/
virtual short poll(short events) const
{
// Possible default for real files
return POLLIN | POLLOUT;
}

/** Returns true if the FileHandle is writable.
* Definition depends upon the subclass implementing FileHandle.
* For example, if the FileHandle is of type Stream, writable() could return
* true when there is ample buffer space available for write() calls.
*/
bool writable() const
{
return poll(POLLOUT) & POLLOUT;
}

/** Returns true if the FileHandle is readable.
* Definition depends upon the subclass implementing FileHandle.
* For example, if the FileHandle is of type Stream, readable() could return
* true when there is something available to read.
*/
bool readable() const
{
return poll(POLLIN) & POLLIN;
}

/** Register a callback on state change of the file.
*
* The specified callback will be called on state changes such as when
* the file can be written to or read from.
*
* The callback may be called in an interrupt context and should not
* perform expensive operations.
*
* Note! This is not intended as an attach-like asynchronous api, but rather
* as a building block for constructing such functionality.
*
* The exact timing of when the registered function
* is called is not guaranteed and susceptible to change. It should be used
* as a cue to make read/write/poll calls to find the current state.
*
* @param func Function to call on state change
*/
void sigio(Callback<void()> func);

/** Issue sigio to user - used by mbed::_poll_change */
void _send_sigio()
{
if (_callback) {
_callback();
}
}

private:
Callback<void()> _callback;
};

/** Not a member function
* This call is equivalent to posix fdopen().
* Returns a pointer to std::FILE.
* It associates a Stream to an already opened file descriptor (FileHandle)
*
* @param fh, a pointer to an opened file descriptor
* @param mode, operation upon the file descriptor, e.g., 'wb+'*/

std::FILE *fdopen(FileHandle *fh, const char *mode);

} // namespace mbed

Expand Down
83 changes: 83 additions & 0 deletions platform/mbed_poll.cpp
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
Loading

0 comments on commit b2408d8

Please sign in to comment.