mirror of https://github.com/ARMmbed/mbed-os.git
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.pull/4119/head
parent
fb7cbdf356
commit
b2408d8a16
|
@ -81,6 +81,23 @@ public:
|
|||
*/
|
||||
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);
|
||||
|
|
|
@ -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
|
|
@ -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.
|
||||
|
@ -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 {
|
||||
|
@ -40,6 +42,12 @@ public:
|
|||
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
|
||||
|
@ -62,7 +70,7 @@ public:
|
|||
* 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;
|
||||
|
||||
|
@ -84,6 +92,8 @@ public:
|
|||
/** 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()
|
||||
{
|
||||
|
@ -94,7 +104,7 @@ public:
|
|||
*
|
||||
* @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()
|
||||
{
|
||||
|
@ -114,13 +124,7 @@ public:
|
|||
*
|
||||
* @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.
|
||||
*
|
||||
|
@ -133,7 +137,10 @@ public:
|
|||
* -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
|
||||
|
@ -143,7 +150,10 @@ public:
|
|||
* -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
|
||||
*
|
||||
|
@ -151,9 +161,98 @@ public:
|
|||
* 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
|
||||
|
||||
|
|
|
@ -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
|
|
@ -0,0 +1,58 @@
|
|||
/* 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.
|
||||
*/
|
||||
#ifndef MBED_POLL_H
|
||||
#define MBED_POLL_H
|
||||
|
||||
#define POLLIN 0x0001 ///< Data may be read without blocking
|
||||
#define POLLOUT 0x0010 ///< Data may be written without blocking
|
||||
#define POLLERR 0x1000 ///< An error has occurred on the device or stream
|
||||
#define POLLHUP 0x2000 ///< The device has been disconnected
|
||||
#define POLLNVAL 0x4000 ///< The specified file handle value is invalid
|
||||
|
||||
namespace mbed {
|
||||
|
||||
class FileHandle;
|
||||
|
||||
/** \addtogroup platform */
|
||||
|
||||
|
||||
struct pollfh {
|
||||
FileHandle *fh;
|
||||
short events;
|
||||
short revents;
|
||||
};
|
||||
|
||||
/** A mechanism to multiplex input/output over a set of file handles(file descriptors).
|
||||
* For every file handle provided, poll() examines it for any events registered for that particular
|
||||
* file handle.
|
||||
*
|
||||
* @param fhs, an array of PollFh struct carrying a FileHandle and bitmasks of events
|
||||
* @param nhfs, number of file handles
|
||||
* @param timeout, timer value to timeout or -1 for loop forever
|
||||
*
|
||||
* @return number of file handles selected (for which revents is non-zero).
|
||||
* @return 0 if timed out with nothing selected.
|
||||
* @return -1 for error.
|
||||
*/
|
||||
int poll(pollfh fhs[], unsigned nfhs, int timeout);
|
||||
|
||||
/** To be called by device when poll state changes - must be called for poll() and sigio() to work
|
||||
* @param fh A pointer to the file handle*/
|
||||
void _poll_change(FileHandle *fh);
|
||||
|
||||
} // namespace mbed
|
||||
|
||||
#endif //MBED_POLL_H
|
|
@ -252,7 +252,7 @@ extern "C" FILEHANDLE PREFIX(_open)(const char* name, int openmode) {
|
|||
/* FILENAME: ":0x12345678" describes a FileHandle* */
|
||||
if (name[0] == ':') {
|
||||
void *p;
|
||||
sscanf(name, ":%p", &p);
|
||||
std::sscanf(name, ":%p", &p);
|
||||
res = (FileHandle*)p;
|
||||
|
||||
/* FILENAME: "/file_system/file_name" */
|
||||
|
@ -408,6 +408,7 @@ extern "C" int PREFIX(_read)(FILEHANDLE fh, unsigned char *buffer, unsigned int
|
|||
#endif
|
||||
}
|
||||
|
||||
|
||||
#ifdef __ARMCC_VERSION
|
||||
extern "C" int PREFIX(_istty)(FILEHANDLE fh)
|
||||
#else
|
||||
|
@ -940,10 +941,10 @@ int __wrap_atexit(void (*func)()) {
|
|||
|
||||
namespace mbed {
|
||||
|
||||
void mbed_set_unbuffered_stream(FILE *_file) {
|
||||
void mbed_set_unbuffered_stream(std::FILE *_file) {
|
||||
#if defined (__ICCARM__)
|
||||
char buf[2];
|
||||
std::setvbuf(_file,buf,_IONBF,NULL);
|
||||
std::setvbuf(_file,buf,_IONBF,NULL);
|
||||
#else
|
||||
setbuf(_file, NULL);
|
||||
#endif
|
||||
|
@ -982,7 +983,7 @@ int mbed_getc(std::FILE *_file){
|
|||
#endif
|
||||
}
|
||||
|
||||
char* mbed_gets(char*s, int size, FILE *_file){
|
||||
char* mbed_gets(char*s, int size, std::FILE *_file){
|
||||
#if defined (__ICCARM__)
|
||||
/*This is only valid for unbuffered streams*/
|
||||
char *str = fgets(s,size,_file);
|
||||
|
|
|
@ -99,6 +99,12 @@ extern "C" {
|
|||
#undef EBADF
|
||||
#define EBADF 9 /* Bad file number */
|
||||
|
||||
#undef EAGAIN
|
||||
#define EAGAIN 11 /* Resource unavailable, try again */
|
||||
|
||||
#undef EWOULDBLOCK
|
||||
#define EWOULDBLOCK EAGAIN /* Operation would block */
|
||||
|
||||
#undef ENOMEM
|
||||
#define ENOMEM 12 /* Not enough space */
|
||||
|
||||
|
|
Loading…
Reference in New Issue