test test test

Dependencies:   mbed

Committer:
mohamedmoawya
Date:
Mon May 25 19:06:11 2020 +0000
Revision:
0:e4c5e6ec922e
snake game tteest

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mohamedmoawya 0:e4c5e6ec922e 1 /* mbed Microcontroller Library
mohamedmoawya 0:e4c5e6ec922e 2 * Copyright (c) 2017 ARM Limited
mohamedmoawya 0:e4c5e6ec922e 3 *
mohamedmoawya 0:e4c5e6ec922e 4 * Licensed under the Apache License, Version 2.0 (the "License");
mohamedmoawya 0:e4c5e6ec922e 5 * you may not use this file except in compliance with the License.
mohamedmoawya 0:e4c5e6ec922e 6 * You may obtain a copy of the License at
mohamedmoawya 0:e4c5e6ec922e 7 *
mohamedmoawya 0:e4c5e6ec922e 8 * http://www.apache.org/licenses/LICENSE-2.0
mohamedmoawya 0:e4c5e6ec922e 9 *
mohamedmoawya 0:e4c5e6ec922e 10 * Unless required by applicable law or agreed to in writing, software
mohamedmoawya 0:e4c5e6ec922e 11 * distributed under the License is distributed on an "AS IS" BASIS,
mohamedmoawya 0:e4c5e6ec922e 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
mohamedmoawya 0:e4c5e6ec922e 13 * See the License for the specific language governing permissions and
mohamedmoawya 0:e4c5e6ec922e 14 * limitations under the License.
mohamedmoawya 0:e4c5e6ec922e 15 */
mohamedmoawya 0:e4c5e6ec922e 16 #ifndef MBED_FILEHANDLE_H
mohamedmoawya 0:e4c5e6ec922e 17 #define MBED_FILEHANDLE_H
mohamedmoawya 0:e4c5e6ec922e 18
mohamedmoawya 0:e4c5e6ec922e 19 typedef int FILEHANDLE;
mohamedmoawya 0:e4c5e6ec922e 20
mohamedmoawya 0:e4c5e6ec922e 21 #include <cstdio>
mohamedmoawya 0:e4c5e6ec922e 22 #include "Callback.h"
mohamedmoawya 0:e4c5e6ec922e 23 #include "platform/mbed_poll.h"
mohamedmoawya 0:e4c5e6ec922e 24 #include "platform/platform.h"
mohamedmoawya 0:e4c5e6ec922e 25 #include "platform/NonCopyable.h"
mohamedmoawya 0:e4c5e6ec922e 26
mohamedmoawya 0:e4c5e6ec922e 27 namespace mbed {
mohamedmoawya 0:e4c5e6ec922e 28 /** \addtogroup platform */
mohamedmoawya 0:e4c5e6ec922e 29 /** @{*/
mohamedmoawya 0:e4c5e6ec922e 30 /**
mohamedmoawya 0:e4c5e6ec922e 31 * \defgroup platform_FileHandle FileHandle functions
mohamedmoawya 0:e4c5e6ec922e 32 * @{
mohamedmoawya 0:e4c5e6ec922e 33 */
mohamedmoawya 0:e4c5e6ec922e 34
mohamedmoawya 0:e4c5e6ec922e 35
mohamedmoawya 0:e4c5e6ec922e 36 /** Class FileHandle
mohamedmoawya 0:e4c5e6ec922e 37 *
mohamedmoawya 0:e4c5e6ec922e 38 * An abstract interface that represents operations on a file-like
mohamedmoawya 0:e4c5e6ec922e 39 * object. The core functions are read, write and seek, but only
mohamedmoawya 0:e4c5e6ec922e 40 * a subset of these operations can be provided.
mohamedmoawya 0:e4c5e6ec922e 41 *
mohamedmoawya 0:e4c5e6ec922e 42 * @note to create a file, @see File
mohamedmoawya 0:e4c5e6ec922e 43 * @note Synchronization level: Set by subclass
mohamedmoawya 0:e4c5e6ec922e 44 */
mohamedmoawya 0:e4c5e6ec922e 45 class FileHandle : private NonCopyable<FileHandle> {
mohamedmoawya 0:e4c5e6ec922e 46 public:
mohamedmoawya 0:e4c5e6ec922e 47 virtual ~FileHandle() {}
mohamedmoawya 0:e4c5e6ec922e 48
mohamedmoawya 0:e4c5e6ec922e 49 /** Read the contents of a file into a buffer
mohamedmoawya 0:e4c5e6ec922e 50 *
mohamedmoawya 0:e4c5e6ec922e 51 * Devices acting as FileHandles should follow POSIX semantics:
mohamedmoawya 0:e4c5e6ec922e 52 *
mohamedmoawya 0:e4c5e6ec922e 53 * * if no data is available, and nonblocking set, return -EAGAIN
mohamedmoawya 0:e4c5e6ec922e 54 * * if no data is available, and blocking set, wait until some data is available
mohamedmoawya 0:e4c5e6ec922e 55 * * If any data is available, call returns immediately
mohamedmoawya 0:e4c5e6ec922e 56 *
mohamedmoawya 0:e4c5e6ec922e 57 * @param buffer The buffer to read in to
mohamedmoawya 0:e4c5e6ec922e 58 * @param size The number of bytes to read
mohamedmoawya 0:e4c5e6ec922e 59 * @return The number of bytes read, 0 at end of file, negative error on failure
mohamedmoawya 0:e4c5e6ec922e 60 */
mohamedmoawya 0:e4c5e6ec922e 61 virtual ssize_t read(void *buffer, size_t size) = 0;
mohamedmoawya 0:e4c5e6ec922e 62
mohamedmoawya 0:e4c5e6ec922e 63 /** Write the contents of a buffer to a file
mohamedmoawya 0:e4c5e6ec922e 64 *
mohamedmoawya 0:e4c5e6ec922e 65 * Devices acting as FileHandles should follow POSIX semantics:
mohamedmoawya 0:e4c5e6ec922e 66 *
mohamedmoawya 0:e4c5e6ec922e 67 * * if blocking, block until all data is written
mohamedmoawya 0:e4c5e6ec922e 68 * * if no data can be written, and nonblocking set, return -EAGAIN
mohamedmoawya 0:e4c5e6ec922e 69 * * if some data can be written, and nonblocking set, write partial
mohamedmoawya 0:e4c5e6ec922e 70 *
mohamedmoawya 0:e4c5e6ec922e 71 * @param buffer The buffer to write from
mohamedmoawya 0:e4c5e6ec922e 72 * @param size The number of bytes to write
mohamedmoawya 0:e4c5e6ec922e 73 * @return The number of bytes written, negative error on failure
mohamedmoawya 0:e4c5e6ec922e 74 */
mohamedmoawya 0:e4c5e6ec922e 75 virtual ssize_t write(const void *buffer, size_t size) = 0;
mohamedmoawya 0:e4c5e6ec922e 76
mohamedmoawya 0:e4c5e6ec922e 77 /** Move the file position to a given offset from from a given location
mohamedmoawya 0:e4c5e6ec922e 78 *
mohamedmoawya 0:e4c5e6ec922e 79 * @param offset The offset from whence to move to
mohamedmoawya 0:e4c5e6ec922e 80 * @param whence The start of where to seek
mohamedmoawya 0:e4c5e6ec922e 81 * SEEK_SET to start from beginning of file,
mohamedmoawya 0:e4c5e6ec922e 82 * SEEK_CUR to start from current position in file,
mohamedmoawya 0:e4c5e6ec922e 83 * SEEK_END to start from end of file
mohamedmoawya 0:e4c5e6ec922e 84 * @return The new offset of the file, negative error code on failure
mohamedmoawya 0:e4c5e6ec922e 85 */
mohamedmoawya 0:e4c5e6ec922e 86 virtual off_t seek(off_t offset, int whence = SEEK_SET) = 0;
mohamedmoawya 0:e4c5e6ec922e 87
mohamedmoawya 0:e4c5e6ec922e 88 /** Close a file
mohamedmoawya 0:e4c5e6ec922e 89 *
mohamedmoawya 0:e4c5e6ec922e 90 * @return 0 on success, negative error code on failure
mohamedmoawya 0:e4c5e6ec922e 91 */
mohamedmoawya 0:e4c5e6ec922e 92 virtual int close() = 0;
mohamedmoawya 0:e4c5e6ec922e 93
mohamedmoawya 0:e4c5e6ec922e 94 /** Flush any buffers associated with the file
mohamedmoawya 0:e4c5e6ec922e 95 *
mohamedmoawya 0:e4c5e6ec922e 96 * @return 0 on success, negative error code on failure
mohamedmoawya 0:e4c5e6ec922e 97 */
mohamedmoawya 0:e4c5e6ec922e 98 virtual int sync()
mohamedmoawya 0:e4c5e6ec922e 99 {
mohamedmoawya 0:e4c5e6ec922e 100 return 0;
mohamedmoawya 0:e4c5e6ec922e 101 }
mohamedmoawya 0:e4c5e6ec922e 102
mohamedmoawya 0:e4c5e6ec922e 103 /** Check if the file in an interactive terminal device
mohamedmoawya 0:e4c5e6ec922e 104 *
mohamedmoawya 0:e4c5e6ec922e 105 * @return True if the file is a terminal
mohamedmoawya 0:e4c5e6ec922e 106 * @return False if the file is not a terminal
mohamedmoawya 0:e4c5e6ec922e 107 * @return Negative error code on failure
mohamedmoawya 0:e4c5e6ec922e 108 */
mohamedmoawya 0:e4c5e6ec922e 109 virtual int isatty()
mohamedmoawya 0:e4c5e6ec922e 110 {
mohamedmoawya 0:e4c5e6ec922e 111 return false;
mohamedmoawya 0:e4c5e6ec922e 112 }
mohamedmoawya 0:e4c5e6ec922e 113
mohamedmoawya 0:e4c5e6ec922e 114 /** Get the file position of the file
mohamedmoawya 0:e4c5e6ec922e 115 *
mohamedmoawya 0:e4c5e6ec922e 116 * @note This is equivalent to seek(0, SEEK_CUR)
mohamedmoawya 0:e4c5e6ec922e 117 *
mohamedmoawya 0:e4c5e6ec922e 118 * @return The current offset in the file, negative error code on failure
mohamedmoawya 0:e4c5e6ec922e 119 */
mohamedmoawya 0:e4c5e6ec922e 120 virtual off_t tell()
mohamedmoawya 0:e4c5e6ec922e 121 {
mohamedmoawya 0:e4c5e6ec922e 122 return seek(0, SEEK_CUR);
mohamedmoawya 0:e4c5e6ec922e 123 }
mohamedmoawya 0:e4c5e6ec922e 124
mohamedmoawya 0:e4c5e6ec922e 125 /** Rewind the file position to the beginning of the file
mohamedmoawya 0:e4c5e6ec922e 126 *
mohamedmoawya 0:e4c5e6ec922e 127 * @note This is equivalent to seek(0, SEEK_SET)
mohamedmoawya 0:e4c5e6ec922e 128 */
mohamedmoawya 0:e4c5e6ec922e 129 virtual void rewind()
mohamedmoawya 0:e4c5e6ec922e 130 {
mohamedmoawya 0:e4c5e6ec922e 131 seek(0, SEEK_SET);
mohamedmoawya 0:e4c5e6ec922e 132 }
mohamedmoawya 0:e4c5e6ec922e 133
mohamedmoawya 0:e4c5e6ec922e 134 /** Get the size of the file
mohamedmoawya 0:e4c5e6ec922e 135 *
mohamedmoawya 0:e4c5e6ec922e 136 * @return Size of the file in bytes
mohamedmoawya 0:e4c5e6ec922e 137 */
mohamedmoawya 0:e4c5e6ec922e 138 virtual off_t size();
mohamedmoawya 0:e4c5e6ec922e 139
mohamedmoawya 0:e4c5e6ec922e 140 /** Move the file position to a given offset from a given location.
mohamedmoawya 0:e4c5e6ec922e 141 *
mohamedmoawya 0:e4c5e6ec922e 142 * @param offset The offset from whence to move to
mohamedmoawya 0:e4c5e6ec922e 143 * @param whence SEEK_SET for the start of the file, SEEK_CUR for the
mohamedmoawya 0:e4c5e6ec922e 144 * current file position, or SEEK_END for the end of the file.
mohamedmoawya 0:e4c5e6ec922e 145 *
mohamedmoawya 0:e4c5e6ec922e 146 * @returns
mohamedmoawya 0:e4c5e6ec922e 147 * new file position on success,
mohamedmoawya 0:e4c5e6ec922e 148 * -1 on failure or unsupported
mohamedmoawya 0:e4c5e6ec922e 149 * @deprecated Replaced by `off_t FileHandle::seek(off_t offset, int whence = SEEK_SET)'
mohamedmoawya 0:e4c5e6ec922e 150 *
mohamedmoawya 0:e4c5e6ec922e 151 */
mohamedmoawya 0:e4c5e6ec922e 152 MBED_DEPRECATED_SINCE("mbed-os-5.4", "Replaced by FileHandle::seek")
mohamedmoawya 0:e4c5e6ec922e 153 virtual off_t lseek(off_t offset, int whence)
mohamedmoawya 0:e4c5e6ec922e 154 {
mohamedmoawya 0:e4c5e6ec922e 155 return seek(offset, whence);
mohamedmoawya 0:e4c5e6ec922e 156 }
mohamedmoawya 0:e4c5e6ec922e 157
mohamedmoawya 0:e4c5e6ec922e 158 /** Flush any buffers associated with the FileHandle, ensuring it
mohamedmoawya 0:e4c5e6ec922e 159 * is up to date on disk
mohamedmoawya 0:e4c5e6ec922e 160 *
mohamedmoawya 0:e4c5e6ec922e 161 * @returns
mohamedmoawya 0:e4c5e6ec922e 162 * 0 on success or un-needed,
mohamedmoawya 0:e4c5e6ec922e 163 * -1 on error
mohamedmoawya 0:e4c5e6ec922e 164 * @deprecated Replaced by `int FileHandle::sync()'
mohamedmoawya 0:e4c5e6ec922e 165 */
mohamedmoawya 0:e4c5e6ec922e 166 MBED_DEPRECATED_SINCE("mbed-os-5.4", "Replaced by FileHandle::sync")
mohamedmoawya 0:e4c5e6ec922e 167 virtual int fsync()
mohamedmoawya 0:e4c5e6ec922e 168 {
mohamedmoawya 0:e4c5e6ec922e 169 return sync();
mohamedmoawya 0:e4c5e6ec922e 170 }
mohamedmoawya 0:e4c5e6ec922e 171
mohamedmoawya 0:e4c5e6ec922e 172 /** Find the length of the file
mohamedmoawya 0:e4c5e6ec922e 173 *
mohamedmoawya 0:e4c5e6ec922e 174 * @returns
mohamedmoawya 0:e4c5e6ec922e 175 * Length of the file
mohamedmoawya 0:e4c5e6ec922e 176 * @deprecated Replaced by `off_t FileHandle::size()'
mohamedmoawya 0:e4c5e6ec922e 177 */
mohamedmoawya 0:e4c5e6ec922e 178 MBED_DEPRECATED_SINCE("mbed-os-5.4", "Replaced by FileHandle::size")
mohamedmoawya 0:e4c5e6ec922e 179 virtual off_t flen()
mohamedmoawya 0:e4c5e6ec922e 180 {
mohamedmoawya 0:e4c5e6ec922e 181 return size();
mohamedmoawya 0:e4c5e6ec922e 182 }
mohamedmoawya 0:e4c5e6ec922e 183
mohamedmoawya 0:e4c5e6ec922e 184 /** Set blocking or nonblocking mode of the file operation like read/write.
mohamedmoawya 0:e4c5e6ec922e 185 * Definition depends on the subclass implementing FileHandle.
mohamedmoawya 0:e4c5e6ec922e 186 * The default is blocking.
mohamedmoawya 0:e4c5e6ec922e 187 *
mohamedmoawya 0:e4c5e6ec922e 188 * @param blocking true for blocking mode, false for nonblocking mode.
mohamedmoawya 0:e4c5e6ec922e 189 *
mohamedmoawya 0:e4c5e6ec922e 190 * @return 0 on success
mohamedmoawya 0:e4c5e6ec922e 191 * @return Negative error code on failure
mohamedmoawya 0:e4c5e6ec922e 192 */
mohamedmoawya 0:e4c5e6ec922e 193 virtual int set_blocking(bool blocking)
mohamedmoawya 0:e4c5e6ec922e 194 {
mohamedmoawya 0:e4c5e6ec922e 195 return blocking ? 0 : -ENOTTY;
mohamedmoawya 0:e4c5e6ec922e 196 }
mohamedmoawya 0:e4c5e6ec922e 197
mohamedmoawya 0:e4c5e6ec922e 198 /** Check current blocking or nonblocking mode for file operations.
mohamedmoawya 0:e4c5e6ec922e 199 *
mohamedmoawya 0:e4c5e6ec922e 200 * @return true for blocking mode, false for nonblocking mode.
mohamedmoawya 0:e4c5e6ec922e 201 */
mohamedmoawya 0:e4c5e6ec922e 202 virtual bool is_blocking() const
mohamedmoawya 0:e4c5e6ec922e 203 {
mohamedmoawya 0:e4c5e6ec922e 204 return true;
mohamedmoawya 0:e4c5e6ec922e 205 }
mohamedmoawya 0:e4c5e6ec922e 206
mohamedmoawya 0:e4c5e6ec922e 207 /** Check for poll event flags
mohamedmoawya 0:e4c5e6ec922e 208 * You can use or ignore the input parameter. You can return all events
mohamedmoawya 0:e4c5e6ec922e 209 * or check just the events listed in events.
mohamedmoawya 0:e4c5e6ec922e 210 * Call is nonblocking - returns instantaneous state of events.
mohamedmoawya 0:e4c5e6ec922e 211 * Whenever an event occurs, the derived class should call the sigio() callback).
mohamedmoawya 0:e4c5e6ec922e 212 *
mohamedmoawya 0:e4c5e6ec922e 213 * @param events bitmask of poll events we're interested in - POLLIN/POLLOUT etc.
mohamedmoawya 0:e4c5e6ec922e 214 *
mohamedmoawya 0:e4c5e6ec922e 215 * @returns bitmask of poll events that have occurred.
mohamedmoawya 0:e4c5e6ec922e 216 */
mohamedmoawya 0:e4c5e6ec922e 217 virtual short poll(short events) const
mohamedmoawya 0:e4c5e6ec922e 218 {
mohamedmoawya 0:e4c5e6ec922e 219 // Possible default for real files
mohamedmoawya 0:e4c5e6ec922e 220 return POLLIN | POLLOUT;
mohamedmoawya 0:e4c5e6ec922e 221 }
mohamedmoawya 0:e4c5e6ec922e 222
mohamedmoawya 0:e4c5e6ec922e 223 /** Definition depends on the subclass implementing FileHandle.
mohamedmoawya 0:e4c5e6ec922e 224 * For example, if the FileHandle is of type Stream, writable() could return
mohamedmoawya 0:e4c5e6ec922e 225 * true when there is ample buffer space available for write() calls.
mohamedmoawya 0:e4c5e6ec922e 226 *
mohamedmoawya 0:e4c5e6ec922e 227 * @returns true if the FileHandle is writable.
mohamedmoawya 0:e4c5e6ec922e 228 */
mohamedmoawya 0:e4c5e6ec922e 229 bool writable() const
mohamedmoawya 0:e4c5e6ec922e 230 {
mohamedmoawya 0:e4c5e6ec922e 231 return poll(POLLOUT) & POLLOUT;
mohamedmoawya 0:e4c5e6ec922e 232 }
mohamedmoawya 0:e4c5e6ec922e 233
mohamedmoawya 0:e4c5e6ec922e 234 /** Definition depends on the subclass implementing FileHandle.
mohamedmoawya 0:e4c5e6ec922e 235 * For example, if the FileHandle is of type Stream, readable() could return
mohamedmoawya 0:e4c5e6ec922e 236 * true when there is something available to read.
mohamedmoawya 0:e4c5e6ec922e 237 *
mohamedmoawya 0:e4c5e6ec922e 238 * @returns true when there is something available to read.
mohamedmoawya 0:e4c5e6ec922e 239 */
mohamedmoawya 0:e4c5e6ec922e 240 bool readable() const
mohamedmoawya 0:e4c5e6ec922e 241 {
mohamedmoawya 0:e4c5e6ec922e 242 return poll(POLLIN) & POLLIN;
mohamedmoawya 0:e4c5e6ec922e 243 }
mohamedmoawya 0:e4c5e6ec922e 244
mohamedmoawya 0:e4c5e6ec922e 245 /** Register a callback on state change of the file.
mohamedmoawya 0:e4c5e6ec922e 246 *
mohamedmoawya 0:e4c5e6ec922e 247 * The specified callback will be called on state changes such as when
mohamedmoawya 0:e4c5e6ec922e 248 * the file can be written to or read from.
mohamedmoawya 0:e4c5e6ec922e 249 *
mohamedmoawya 0:e4c5e6ec922e 250 * The callback may be called in an interrupt context and should not
mohamedmoawya 0:e4c5e6ec922e 251 * perform expensive operations.
mohamedmoawya 0:e4c5e6ec922e 252 *
mohamedmoawya 0:e4c5e6ec922e 253 * Note! This is not intended as an attach-like asynchronous API, but rather
mohamedmoawya 0:e4c5e6ec922e 254 * as a building block for constructing such functionality.
mohamedmoawya 0:e4c5e6ec922e 255 *
mohamedmoawya 0:e4c5e6ec922e 256 * The exact timing of when the registered function
mohamedmoawya 0:e4c5e6ec922e 257 * is called is not guaranteed and is susceptible to change. It should be used
mohamedmoawya 0:e4c5e6ec922e 258 * as a cue to make read/write/poll calls to find the current state.
mohamedmoawya 0:e4c5e6ec922e 259 *
mohamedmoawya 0:e4c5e6ec922e 260 * @param func Function to call on state change
mohamedmoawya 0:e4c5e6ec922e 261 */
mohamedmoawya 0:e4c5e6ec922e 262 virtual void sigio(Callback<void()> func)
mohamedmoawya 0:e4c5e6ec922e 263 {
mohamedmoawya 0:e4c5e6ec922e 264 //Default for real files. Do nothing for real files.
mohamedmoawya 0:e4c5e6ec922e 265 }
mohamedmoawya 0:e4c5e6ec922e 266 };
mohamedmoawya 0:e4c5e6ec922e 267
mohamedmoawya 0:e4c5e6ec922e 268 /**@}*/
mohamedmoawya 0:e4c5e6ec922e 269
mohamedmoawya 0:e4c5e6ec922e 270 /**@}*/
mohamedmoawya 0:e4c5e6ec922e 271
mohamedmoawya 0:e4c5e6ec922e 272
mohamedmoawya 0:e4c5e6ec922e 273 } // namespace mbed
mohamedmoawya 0:e4c5e6ec922e 274
mohamedmoawya 0:e4c5e6ec922e 275 #endif