The prosthetic control(MIT)

Committer:
ganlikun
Date:
Thu Jun 23 05:23:34 2022 +0000
Revision:
0:20e0c61e0684
01

Who changed what in which revision?

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