Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: cobaLCDJoyMotor_Thread odometry_omni_3roda_v3 odometry_omni_3roda_v1 odometry_omni_3roda_v2 ... more
FileHandle.h
00001 /* mbed Microcontroller Library 00002 * Copyright (c) 2017 ARM Limited 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 #ifndef MBED_FILEHANDLE_H 00017 #define MBED_FILEHANDLE_H 00018 00019 typedef int FILEHANDLE; 00020 00021 #include <cstdio> 00022 #include "Callback.h" 00023 #include "platform/mbed_poll.h" 00024 #include "platform/platform.h" 00025 #include "platform/NonCopyable.h" 00026 00027 namespace mbed { 00028 /** \addtogroup platform */ 00029 /** @{*/ 00030 /** 00031 * \defgroup platform_FileHandle FileHandle functions 00032 * @{ 00033 */ 00034 00035 00036 /** Class FileHandle 00037 * 00038 * An abstract interface that represents operations on a file-like 00039 * object. The core functions are read, write, and seek, but only 00040 * a subset of these operations can be provided. 00041 * 00042 * @note to create a file, @see File 00043 * @note Synchronization level: Set by subclass 00044 */ 00045 class FileHandle : private NonCopyable<FileHandle> { 00046 public: 00047 virtual ~FileHandle() {} 00048 00049 /** Read the contents of a file into a buffer 00050 * 00051 * Devices acting as FileHandles should follow POSIX semantics: 00052 * 00053 * * if no data is available, and non-blocking set return -EAGAIN 00054 * * if no data is available, and blocking set, wait until some data is available 00055 * * If any data is available, call returns immediately 00056 * 00057 * @param buffer The buffer to read in to 00058 * @param size The number of bytes to read 00059 * @return The number of bytes read, 0 at end of file, negative error on failure 00060 */ 00061 virtual ssize_t read(void *buffer, size_t size) = 0; 00062 00063 /** Write the contents of a buffer to a file 00064 * 00065 * Devices acting as FileHandles should follow POSIX semantics: 00066 * 00067 * * if blocking, block until all data is written 00068 * * if no data can be written, and non-blocking set, return -EAGAIN 00069 * * if some data can be written, and non-blocking set, write partial 00070 * 00071 * @param buffer The buffer to write from 00072 * @param size The number of bytes to write 00073 * @return The number of bytes written, negative error on failure 00074 */ 00075 virtual ssize_t write(const void *buffer, size_t size) = 0; 00076 00077 /** Move the file position to a given offset from from a given location 00078 * 00079 * @param offset The offset from whence to move to 00080 * @param whence The start of where to seek 00081 * SEEK_SET to start from beginning of file, 00082 * SEEK_CUR to start from current position in file, 00083 * SEEK_END to start from end of file 00084 * @return The new offset of the file, negative error code on failure 00085 */ 00086 virtual off_t seek(off_t offset, int whence = SEEK_SET) = 0; 00087 00088 /** Close a file 00089 * 00090 * @return 0 on success, negative error code on failure 00091 */ 00092 virtual int close() = 0; 00093 00094 /** Flush any buffers associated with the file 00095 * 00096 * @return 0 on success, negative error code on failure 00097 */ 00098 virtual int sync() 00099 { 00100 return 0; 00101 } 00102 00103 /** Check if the file in an interactive terminal device 00104 * 00105 * @return True if the file is a terminal 00106 * @return False if the file is not a terminal 00107 * @return Negative error code on failure 00108 */ 00109 virtual int isatty() 00110 { 00111 return false; 00112 } 00113 00114 /** Get the file position of the file 00115 * 00116 * @note This is equivalent to seek(0, SEEK_CUR) 00117 * 00118 * @return The current offset in the file, negative error code on failure 00119 */ 00120 virtual off_t tell() 00121 { 00122 return seek(0, SEEK_CUR); 00123 } 00124 00125 /** Rewind the file position to the beginning of the file 00126 * 00127 * @note This is equivalent to seek(0, SEEK_SET) 00128 */ 00129 virtual void rewind() 00130 { 00131 seek(0, SEEK_SET); 00132 } 00133 00134 /** Get the size of the file 00135 * 00136 * @return Size of the file in bytes 00137 */ 00138 virtual off_t size(); 00139 00140 /** Move the file position to a given offset from a given location. 00141 * 00142 * @param offset The offset from whence to move to 00143 * @param whence SEEK_SET for the start of the file, SEEK_CUR for the 00144 * current file position, or SEEK_END for the end of the file. 00145 * 00146 * @returns 00147 * new file position on success, 00148 * -1 on failure or unsupported 00149 */ 00150 MBED_DEPRECATED_SINCE("mbed-os-5.4", "Replaced by FileHandle::seek") 00151 virtual off_t lseek(off_t offset, int whence) 00152 { 00153 return seek(offset, whence); 00154 } 00155 00156 /** Flush any buffers associated with the FileHandle, ensuring it 00157 * is up to date on disk 00158 * 00159 * @returns 00160 * 0 on success or un-needed, 00161 * -1 on error 00162 */ 00163 MBED_DEPRECATED_SINCE("mbed-os-5.4", "Replaced by FileHandle::sync") 00164 virtual int fsync() 00165 { 00166 return sync(); 00167 } 00168 00169 /** Find the length of the file 00170 * 00171 * @returns 00172 * Length of the file 00173 */ 00174 MBED_DEPRECATED_SINCE("mbed-os-5.4", "Replaced by FileHandle::size") 00175 virtual off_t flen() 00176 { 00177 return size(); 00178 } 00179 00180 /** Set blocking or non-blocking mode of the file operation like read/write. 00181 * Definition depends upon the subclass implementing FileHandle. 00182 * The default is blocking. 00183 * 00184 * @param blocking true for blocking mode, false for non-blocking mode. 00185 * 00186 * @return 0 on success 00187 * @return Negative error code on failure 00188 */ 00189 virtual int set_blocking(bool blocking) 00190 { 00191 return -1; 00192 } 00193 00194 /** Check for poll event flags 00195 * The input parameter can be used or ignored - the could always return all events, 00196 * or could check just the events listed in events. 00197 * Call is non-blocking - returns instantaneous state of events. 00198 * Whenever an event occurs, the derived class should call the sigio() callback). 00199 * 00200 * @param events bitmask of poll events we're interested in - POLLIN/POLLOUT etc. 00201 * 00202 * @returns bitmask of poll events that have occurred. 00203 */ 00204 virtual short poll(short events) const 00205 { 00206 // Possible default for real files 00207 return POLLIN | POLLOUT; 00208 } 00209 00210 /** Definition depends upon the subclass implementing FileHandle. 00211 * For example, if the FileHandle is of type Stream, writable() could return 00212 * true when there is ample buffer space available for write() calls. 00213 * 00214 * @returns true if the FileHandle is writable. 00215 */ 00216 bool writable() const 00217 { 00218 return poll(POLLOUT) & POLLOUT; 00219 } 00220 00221 /** Definition depends upon the subclass implementing FileHandle. 00222 * For example, if the FileHandle is of type Stream, readable() could return 00223 * true when there is something available to read. 00224 * 00225 * @returns true when there is something available to read. 00226 */ 00227 bool readable() const 00228 { 00229 return poll(POLLIN) & POLLIN; 00230 } 00231 00232 /** Register a callback on state change of the file. 00233 * 00234 * The specified callback will be called on state changes such as when 00235 * the file can be written to or read from. 00236 * 00237 * The callback may be called in an interrupt context and should not 00238 * perform expensive operations. 00239 * 00240 * Note! This is not intended as an attach-like asynchronous api, but rather 00241 * as a building block for constructing such functionality. 00242 * 00243 * The exact timing of when the registered function 00244 * is called is not guaranteed and susceptible to change. It should be used 00245 * as a cue to make read/write/poll calls to find the current state. 00246 * 00247 * @param func Function to call on state change 00248 */ 00249 virtual void sigio(Callback<void()> func) 00250 { 00251 //Default for real files. Do nothing for real files. 00252 } 00253 }; 00254 00255 /** Not a member function 00256 * This call is equivalent to posix fdopen(). 00257 * It associates a Stream to an already opened file descriptor (FileHandle) 00258 * 00259 * @param fh a pointer to an opened file descriptor 00260 * @param mode operation upon the file descriptor, e.g., 'wb+' 00261 * 00262 * @returns a pointer to std::FILE 00263 */ 00264 00265 std::FILE *fdopen(FileHandle *fh, const char *mode); 00266 00267 /**@}*/ 00268 00269 /**@}*/ 00270 00271 00272 } // namespace mbed 00273 00274 #endif
Generated on Tue Jul 12 2022 13:02:53 by
