2013-02-18 15:32:11 +00:00
|
|
|
/* mbed Microcontroller Library
|
2017-02-15 23:21:50 +00:00
|
|
|
* Copyright (c) 2017 ARM Limited
|
2013-02-18 15:32:11 +00:00
|
|
|
*
|
|
|
|
* 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_FILEHANDLE_H
|
|
|
|
#define MBED_FILEHANDLE_H
|
|
|
|
|
|
|
|
typedef int FILEHANDLE;
|
|
|
|
|
2017-02-15 23:21:50 +00:00
|
|
|
#include <cstdio>
|
|
|
|
#include "Callback.h"
|
|
|
|
#include "platform/mbed_poll.h"
|
2017-02-13 19:19:57 +00:00
|
|
|
#include "platform/platform.h"
|
2017-06-20 11:45:52 +00:00
|
|
|
#include "platform/NonCopyable.h"
|
2013-02-18 15:32:11 +00:00
|
|
|
|
|
|
|
namespace mbed {
|
2017-04-04 19:21:53 +00:00
|
|
|
/** \addtogroup platform */
|
2017-10-24 15:05:45 +00:00
|
|
|
/** @{*/
|
|
|
|
/**
|
|
|
|
* \defgroup platform_FileHandle FileHandle functions
|
|
|
|
* @{
|
|
|
|
*/
|
2013-02-18 15:32:11 +00:00
|
|
|
|
2017-03-01 22:23:03 +00:00
|
|
|
|
|
|
|
/** Class FileHandle
|
2013-02-18 15:32:11 +00:00
|
|
|
*
|
2017-03-01 22:23:03 +00:00
|
|
|
* An abstract interface that represents operations on a file-like
|
|
|
|
* object. The core functions are read, write, and seek, but only
|
|
|
|
* a subset of these operations can be provided.
|
2016-06-08 12:52:14 +00:00
|
|
|
*
|
2017-03-01 22:23:03 +00:00
|
|
|
* @note to create a file, @see File
|
|
|
|
* @note Synchronization level: Set by subclass
|
2013-02-18 15:32:11 +00:00
|
|
|
*/
|
2017-06-20 11:45:52 +00:00
|
|
|
class FileHandle : private NonCopyable<FileHandle> {
|
2013-02-18 15:32:11 +00:00
|
|
|
public:
|
2017-03-01 22:23:03 +00:00
|
|
|
virtual ~FileHandle() {}
|
2017-02-13 19:19:57 +00:00
|
|
|
|
2017-03-01 22:23:03 +00:00
|
|
|
/** Read the contents of a file into a buffer
|
2017-02-15 23:21:50 +00:00
|
|
|
*
|
|
|
|
* Devices acting as FileHandles should follow POSIX semantics:
|
|
|
|
*
|
|
|
|
* * if no data is available, and non-blocking set return -EAGAIN
|
Make UARTSerial send all data when blocking
Previously, write() was somewhat soft - it only ever made one attempt to
wait for buffer space, so it would take as much data as would fit in the
buffer in one call.
This is not the intent of a POSIX filehandle write. It should try to
send everything if blocking, and only send less if interrupted by a
signal:
- If the O_NONBLOCK flag is clear, write() shall block the calling
thread until the data can be accepted.
- If the O_NONBLOCK flag is set, write() shall not block the thread.
If some data can be written without blocking the thread, write()
shall write what it can and return the number of bytes written.
Otherwise, it shall return -1 and set errno to [EAGAIN].
This "send all" behaviour is of slightly limited usefulness in POSIX, as
you still usually have to worry about the interruption possibility:
- If write() is interrupted by a signal before it writes any data, it
shall return -1 with errno set to [EINTR].
- If write() is interrupted by a signal after it successfully writes
some data, it shall return the number of bytes written.
But as mbed OS does not have the possibility of signal interruption, if we
strengthen write to write everything, we can make applications' lives
easier - they can just do "write(large amount)" confident that it will
all go in one call (if no errors).
So, rework to make multiple writes to the buffer, blocking as necessary,
until all data is written.
This change does not apply to read(), which is correct in only blocking until
some data is available:
- If O_NONBLOCK is set, read() shall return -1 and set errno to [EAGAIN].
- If O_NONBLOCK is clear, read() shall block the calling thread until some
data becomes available.
- The use of the O_NONBLOCK flag has no effect if there is some data
available.
2017-11-09 12:30:55 +00:00
|
|
|
* * if no data is available, and blocking set, wait until some data is available
|
2017-02-15 23:21:50 +00:00
|
|
|
* * If any data is available, call returns immediately
|
2013-02-18 15:32:11 +00:00
|
|
|
*
|
2017-03-01 22:23:03 +00:00
|
|
|
* @param buffer The buffer to read in to
|
|
|
|
* @param size The number of bytes to read
|
|
|
|
* @return The number of bytes read, 0 at end of file, negative error on failure
|
|
|
|
*/
|
2017-03-14 16:04:22 +00:00
|
|
|
virtual ssize_t read(void *buffer, size_t size) = 0;
|
2017-03-01 22:23:03 +00:00
|
|
|
|
|
|
|
/** Write the contents of a buffer to a file
|
Make UARTSerial send all data when blocking
Previously, write() was somewhat soft - it only ever made one attempt to
wait for buffer space, so it would take as much data as would fit in the
buffer in one call.
This is not the intent of a POSIX filehandle write. It should try to
send everything if blocking, and only send less if interrupted by a
signal:
- If the O_NONBLOCK flag is clear, write() shall block the calling
thread until the data can be accepted.
- If the O_NONBLOCK flag is set, write() shall not block the thread.
If some data can be written without blocking the thread, write()
shall write what it can and return the number of bytes written.
Otherwise, it shall return -1 and set errno to [EAGAIN].
This "send all" behaviour is of slightly limited usefulness in POSIX, as
you still usually have to worry about the interruption possibility:
- If write() is interrupted by a signal before it writes any data, it
shall return -1 with errno set to [EINTR].
- If write() is interrupted by a signal after it successfully writes
some data, it shall return the number of bytes written.
But as mbed OS does not have the possibility of signal interruption, if we
strengthen write to write everything, we can make applications' lives
easier - they can just do "write(large amount)" confident that it will
all go in one call (if no errors).
So, rework to make multiple writes to the buffer, blocking as necessary,
until all data is written.
This change does not apply to read(), which is correct in only blocking until
some data is available:
- If O_NONBLOCK is set, read() shall return -1 and set errno to [EAGAIN].
- If O_NONBLOCK is clear, read() shall block the calling thread until some
data becomes available.
- The use of the O_NONBLOCK flag has no effect if there is some data
available.
2017-11-09 12:30:55 +00:00
|
|
|
*
|
|
|
|
* Devices acting as FileHandles should follow POSIX semantics:
|
|
|
|
*
|
|
|
|
* * if blocking, block until all data is written
|
|
|
|
* * if no data can be written, and non-blocking set, return -EAGAIN
|
|
|
|
* * if some data can be written, and non-blocking set, write partial
|
2013-02-18 15:32:11 +00:00
|
|
|
*
|
2017-03-01 22:23:03 +00:00
|
|
|
* @param buffer The buffer to write from
|
2018-06-27 14:09:15 +00:00
|
|
|
* @param size The number of bytes to write
|
2017-03-01 22:23:03 +00:00
|
|
|
* @return The number of bytes written, negative error on failure
|
2013-02-18 15:32:11 +00:00
|
|
|
*/
|
2017-03-14 16:04:22 +00:00
|
|
|
virtual ssize_t write(const void *buffer, size_t size) = 0;
|
2013-02-18 15:32:11 +00:00
|
|
|
|
2017-03-07 17:11:20 +00:00
|
|
|
/** Move the file position to a given offset from from a given location
|
|
|
|
*
|
|
|
|
* @param offset The offset from whence to move to
|
|
|
|
* @param whence The start of where to seek
|
|
|
|
* 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
|
2017-02-15 23:21:50 +00:00
|
|
|
* @return The new offset of the file, negative error code on failure
|
2017-03-07 17:11:20 +00:00
|
|
|
*/
|
|
|
|
virtual off_t seek(off_t offset, int whence = SEEK_SET) = 0;
|
|
|
|
|
2017-03-01 22:23:03 +00:00
|
|
|
/** Close a file
|
2013-02-18 15:32:11 +00:00
|
|
|
*
|
2017-03-01 22:23:03 +00:00
|
|
|
* @return 0 on success, negative error code on failure
|
2013-02-18 15:32:11 +00:00
|
|
|
*/
|
|
|
|
virtual int close() = 0;
|
|
|
|
|
2017-03-01 22:23:03 +00:00
|
|
|
/** Flush any buffers associated with the file
|
2013-02-18 15:32:11 +00:00
|
|
|
*
|
2017-03-01 22:23:03 +00:00
|
|
|
* @return 0 on success, negative error code on failure
|
2013-02-18 15:32:11 +00:00
|
|
|
*/
|
2017-03-07 17:11:20 +00:00
|
|
|
virtual int sync()
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2013-02-18 15:32:11 +00:00
|
|
|
|
2017-03-01 22:23:03 +00:00
|
|
|
/** Check if the file in an interactive terminal device
|
2013-02-18 15:32:11 +00:00
|
|
|
*
|
2017-03-01 22:23:03 +00:00
|
|
|
* @return True if the file is a terminal
|
2017-02-15 23:21:50 +00:00
|
|
|
* @return False if the file is not a terminal
|
|
|
|
* @return Negative error code on failure
|
2013-02-18 15:32:11 +00:00
|
|
|
*/
|
2017-03-07 17:11:20 +00:00
|
|
|
virtual int isatty()
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2017-03-01 22:23:03 +00:00
|
|
|
|
|
|
|
/** Get the file position of the file
|
|
|
|
*
|
|
|
|
* @note This is equivalent to seek(0, SEEK_CUR)
|
|
|
|
*
|
2017-02-15 23:21:50 +00:00
|
|
|
* @return The current offset in the file, negative error code on failure
|
2017-03-01 22:23:03 +00:00
|
|
|
*/
|
|
|
|
virtual off_t tell()
|
|
|
|
{
|
|
|
|
return seek(0, SEEK_CUR);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Rewind the file position to the beginning of the file
|
|
|
|
*
|
|
|
|
* @note This is equivalent to seek(0, SEEK_SET)
|
|
|
|
*/
|
|
|
|
virtual void rewind()
|
|
|
|
{
|
|
|
|
seek(0, SEEK_SET);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Get the size of the file
|
|
|
|
*
|
|
|
|
* @return Size of the file in bytes
|
|
|
|
*/
|
2017-02-15 23:21:50 +00:00
|
|
|
virtual off_t size();
|
2017-03-01 22:23:03 +00:00
|
|
|
|
2013-02-18 15:32:11 +00:00
|
|
|
/** Move the file position to a given offset from a given location.
|
|
|
|
*
|
|
|
|
* @param offset The offset from whence to move to
|
|
|
|
* @param whence SEEK_SET for the start of the file, SEEK_CUR for the
|
|
|
|
* current file position, or SEEK_END for the end of the file.
|
|
|
|
*
|
|
|
|
* @returns
|
|
|
|
* new file position on success,
|
|
|
|
* -1 on failure or unsupported
|
2018-04-02 10:01:21 +00:00
|
|
|
* @deprecated Replaced by `off_t FileHandle::seek(off_t offset, int whence = SEEK_SET)'
|
2018-03-29 09:05:26 +00:00
|
|
|
*
|
2013-02-18 15:32:11 +00:00
|
|
|
*/
|
2017-03-01 22:23:03 +00:00
|
|
|
MBED_DEPRECATED_SINCE("mbed-os-5.4", "Replaced by FileHandle::seek")
|
2017-02-15 23:21:50 +00:00
|
|
|
virtual off_t lseek(off_t offset, int whence)
|
|
|
|
{
|
|
|
|
return seek(offset, whence);
|
|
|
|
}
|
2013-02-18 15:32:11 +00:00
|
|
|
|
|
|
|
/** Flush any buffers associated with the FileHandle, ensuring it
|
|
|
|
* is up to date on disk
|
|
|
|
*
|
|
|
|
* @returns
|
|
|
|
* 0 on success or un-needed,
|
|
|
|
* -1 on error
|
2018-04-02 10:01:21 +00:00
|
|
|
* @deprecated Replaced by `int FileHandle::sync()'
|
2013-02-18 15:32:11 +00:00
|
|
|
*/
|
2017-03-01 22:23:03 +00:00
|
|
|
MBED_DEPRECATED_SINCE("mbed-os-5.4", "Replaced by FileHandle::sync")
|
2017-02-15 23:21:50 +00:00
|
|
|
virtual int fsync()
|
|
|
|
{
|
|
|
|
return sync();
|
|
|
|
}
|
2016-05-31 22:57:41 +00:00
|
|
|
|
2017-03-01 22:23:03 +00:00
|
|
|
/** Find the length of the file
|
|
|
|
*
|
|
|
|
* @returns
|
|
|
|
* Length of the file
|
2018-04-02 10:01:21 +00:00
|
|
|
* @deprecated Replaced by `off_t FileHandle::size()'
|
2016-05-31 22:57:41 +00:00
|
|
|
*/
|
2017-03-01 22:23:03 +00:00
|
|
|
MBED_DEPRECATED_SINCE("mbed-os-5.4", "Replaced by FileHandle::size")
|
2017-02-15 23:21:50 +00:00
|
|
|
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.
|
|
|
|
*
|
2017-06-02 18:18:40 +00:00
|
|
|
* @param blocking true for blocking mode, false for non-blocking mode.
|
|
|
|
*
|
|
|
|
* @return 0 on success
|
|
|
|
* @return Negative error code on failure
|
2017-02-15 23:21:50 +00:00
|
|
|
*/
|
|
|
|
virtual int set_blocking(bool blocking)
|
|
|
|
{
|
2018-05-02 10:52:02 +00:00
|
|
|
return blocking ? 0 : -ENOTTY;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Check current blocking or non-blocking mode for file operations.
|
|
|
|
*
|
|
|
|
* @return true for blocking mode, false for non-blocking mode.
|
|
|
|
*/
|
|
|
|
virtual bool is_blocking() const
|
|
|
|
{
|
|
|
|
return true;
|
2017-02-15 23:21:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** 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.
|
2017-05-10 09:09:36 +00:00
|
|
|
* Whenever an event occurs, the derived class should call the sigio() callback).
|
2017-05-08 10:11:12 +00:00
|
|
|
*
|
2017-06-02 18:18:40 +00:00
|
|
|
* @param events bitmask of poll events we're interested in - POLLIN/POLLOUT etc.
|
2017-02-15 23:21:50 +00:00
|
|
|
*
|
2017-06-02 18:18:40 +00:00
|
|
|
* @returns bitmask of poll events that have occurred.
|
2017-02-15 23:21:50 +00:00
|
|
|
*/
|
|
|
|
virtual short poll(short events) const
|
|
|
|
{
|
|
|
|
// Possible default for real files
|
|
|
|
return POLLIN | POLLOUT;
|
|
|
|
}
|
|
|
|
|
2017-06-02 18:18:40 +00:00
|
|
|
/** Definition depends upon the subclass implementing FileHandle.
|
2017-02-15 23:21:50 +00:00
|
|
|
* For example, if the FileHandle is of type Stream, writable() could return
|
|
|
|
* true when there is ample buffer space available for write() calls.
|
2017-06-02 18:18:40 +00:00
|
|
|
*
|
|
|
|
* @returns true if the FileHandle is writable.
|
2017-02-15 23:21:50 +00:00
|
|
|
*/
|
|
|
|
bool writable() const
|
|
|
|
{
|
|
|
|
return poll(POLLOUT) & POLLOUT;
|
|
|
|
}
|
|
|
|
|
2017-06-02 18:18:40 +00:00
|
|
|
/** Definition depends upon the subclass implementing FileHandle.
|
2017-02-15 23:21:50 +00:00
|
|
|
* For example, if the FileHandle is of type Stream, readable() could return
|
|
|
|
* true when there is something available to read.
|
2017-06-02 18:18:40 +00:00
|
|
|
*
|
|
|
|
* @returns true when there is something available to read.
|
2017-02-15 23:21:50 +00:00
|
|
|
*/
|
|
|
|
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
|
|
|
|
*/
|
2017-05-08 10:11:12 +00:00
|
|
|
virtual void sigio(Callback<void()> func)
|
2017-02-15 23:21:50 +00:00
|
|
|
{
|
2017-05-08 10:11:12 +00:00
|
|
|
//Default for real files. Do nothing for real files.
|
2017-02-15 23:21:50 +00:00
|
|
|
}
|
2013-02-18 15:32:11 +00:00
|
|
|
};
|
|
|
|
|
2017-10-24 15:05:45 +00:00
|
|
|
/**@}*/
|
|
|
|
|
|
|
|
/**@}*/
|
|
|
|
|
|
|
|
|
2013-02-18 15:32:11 +00:00
|
|
|
} // namespace mbed
|
|
|
|
|
|
|
|
#endif
|