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.
Dependencies: nRF51_Vdd TextLCD BME280
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 nonblocking 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 nonblocking set, return -EAGAIN 00069 * * if some data can be written, and nonblocking 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 * @deprecated Replaced by `off_t FileHandle::seek(off_t offset, int whence = SEEK_SET)' 00150 * 00151 */ 00152 MBED_DEPRECATED_SINCE("mbed-os-5.4", "Replaced by FileHandle::seek") 00153 virtual off_t lseek(off_t offset, int whence) 00154 { 00155 return seek(offset, whence); 00156 } 00157 00158 /** Flush any buffers associated with the FileHandle, ensuring it 00159 * is up to date on disk 00160 * 00161 * @returns 00162 * 0 on success or un-needed, 00163 * -1 on error 00164 * @deprecated Replaced by `int FileHandle::sync()' 00165 */ 00166 MBED_DEPRECATED_SINCE("mbed-os-5.4", "Replaced by FileHandle::sync") 00167 virtual int fsync() 00168 { 00169 return sync(); 00170 } 00171 00172 /** Find the length of the file 00173 * 00174 * @returns 00175 * Length of the file 00176 * @deprecated Replaced by `off_t FileHandle::size()' 00177 */ 00178 MBED_DEPRECATED_SINCE("mbed-os-5.4", "Replaced by FileHandle::size") 00179 virtual off_t flen() 00180 { 00181 return size(); 00182 } 00183 00184 /** Set blocking or nonblocking mode of the file operation like read/write. 00185 * Definition depends on the subclass implementing FileHandle. 00186 * The default is blocking. 00187 * 00188 * @param blocking true for blocking mode, false for nonblocking mode. 00189 * 00190 * @return 0 on success 00191 * @return Negative error code on failure 00192 */ 00193 virtual int set_blocking(bool blocking) 00194 { 00195 return blocking ? 0 : -ENOTTY; 00196 } 00197 00198 /** Check current blocking or nonblocking mode for file operations. 00199 * 00200 * @return true for blocking mode, false for nonblocking mode. 00201 */ 00202 virtual bool is_blocking() const 00203 { 00204 return true; 00205 } 00206 00207 /** Check for poll event flags 00208 * You can use or ignore the input parameter. You can return all events 00209 * or check just the events listed in events. 00210 * Call is nonblocking - returns instantaneous state of events. 00211 * Whenever an event occurs, the derived class should call the sigio() callback). 00212 * 00213 * @param events bitmask of poll events we're interested in - POLLIN/POLLOUT etc. 00214 * 00215 * @returns bitmask of poll events that have occurred. 00216 */ 00217 virtual short poll(short events) const 00218 { 00219 // Possible default for real files 00220 return POLLIN | POLLOUT; 00221 } 00222 00223 /** Definition depends on the subclass implementing FileHandle. 00224 * For example, if the FileHandle is of type Stream, writable() could return 00225 * true when there is ample buffer space available for write() calls. 00226 * 00227 * @returns true if the FileHandle is writable. 00228 */ 00229 bool writable() const 00230 { 00231 return poll(POLLOUT) & POLLOUT; 00232 } 00233 00234 /** Definition depends on the subclass implementing FileHandle. 00235 * For example, if the FileHandle is of type Stream, readable() could return 00236 * true when there is something available to read. 00237 * 00238 * @returns true when there is something available to read. 00239 */ 00240 bool readable() const 00241 { 00242 return poll(POLLIN) & POLLIN; 00243 } 00244 00245 /** Register a callback on state change of the file. 00246 * 00247 * The specified callback will be called on state changes such as when 00248 * the file can be written to or read from. 00249 * 00250 * The callback may be called in an interrupt context and should not 00251 * perform expensive operations. 00252 * 00253 * Note! This is not intended as an attach-like asynchronous API, but rather 00254 * as a building block for constructing such functionality. 00255 * 00256 * The exact timing of when the registered function 00257 * is called is not guaranteed and is susceptible to change. It should be used 00258 * as a cue to make read/write/poll calls to find the current state. 00259 * 00260 * @param func Function to call on state change 00261 */ 00262 virtual void sigio(Callback<void()> func) 00263 { 00264 //Default for real files. Do nothing for real files. 00265 } 00266 }; 00267 00268 /**@}*/ 00269 00270 /**@}*/ 00271 00272 00273 } // namespace mbed 00274 00275 #endif
Generated on Tue Jul 12 2022 15:15:45 by
