00

Committer:
ganlikun
Date:
Sun Jun 12 14:02:44 2022 +0000
Revision:
0:13413ea9a877
00

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ganlikun 0:13413ea9a877 1 /* mbed Microcontroller Library
ganlikun 0:13413ea9a877 2 * Copyright (c) 2006-2013 ARM Limited
ganlikun 0:13413ea9a877 3 *
ganlikun 0:13413ea9a877 4 * Licensed under the Apache License, Version 2.0 (the "License");
ganlikun 0:13413ea9a877 5 * you may not use this file except in compliance with the License.
ganlikun 0:13413ea9a877 6 * You may obtain a copy of the License at
ganlikun 0:13413ea9a877 7 *
ganlikun 0:13413ea9a877 8 * http://www.apache.org/licenses/LICENSE-2.0
ganlikun 0:13413ea9a877 9 *
ganlikun 0:13413ea9a877 10 * Unless required by applicable law or agreed to in writing, software
ganlikun 0:13413ea9a877 11 * distributed under the License is distributed on an "AS IS" BASIS,
ganlikun 0:13413ea9a877 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
ganlikun 0:13413ea9a877 13 * See the License for the specific language governing permissions and
ganlikun 0:13413ea9a877 14 * limitations under the License.
ganlikun 0:13413ea9a877 15 */
ganlikun 0:13413ea9a877 16 #ifndef MBED_LOCALFILESYSTEM_H
ganlikun 0:13413ea9a877 17 #define MBED_LOCALFILESYSTEM_H
ganlikun 0:13413ea9a877 18
ganlikun 0:13413ea9a877 19 #include "platform/platform.h"
ganlikun 0:13413ea9a877 20
ganlikun 0:13413ea9a877 21 #if DEVICE_LOCALFILESYSTEM
ganlikun 0:13413ea9a877 22
ganlikun 0:13413ea9a877 23 #include "platform/FileSystemLike.h"
ganlikun 0:13413ea9a877 24 #include "platform/PlatformMutex.h"
ganlikun 0:13413ea9a877 25 #include "platform/NonCopyable.h"
ganlikun 0:13413ea9a877 26
ganlikun 0:13413ea9a877 27 namespace mbed {
ganlikun 0:13413ea9a877 28 /** \addtogroup platform */
ganlikun 0:13413ea9a877 29 /** @{*/
ganlikun 0:13413ea9a877 30
ganlikun 0:13413ea9a877 31 FILEHANDLE local_file_open(const char* name, int flags);
ganlikun 0:13413ea9a877 32 /** @}*/
ganlikun 0:13413ea9a877 33
ganlikun 0:13413ea9a877 34 /**
ganlikun 0:13413ea9a877 35 * @class LocalFileHandle
ganlikun 0:13413ea9a877 36 * @ingroup platform
ganlikun 0:13413ea9a877 37 */
ganlikun 0:13413ea9a877 38 class LocalFileHandle : public FileHandle, private NonCopyable<LocalFileHandle> {
ganlikun 0:13413ea9a877 39
ganlikun 0:13413ea9a877 40 public:
ganlikun 0:13413ea9a877 41 LocalFileHandle(FILEHANDLE fh);
ganlikun 0:13413ea9a877 42
ganlikun 0:13413ea9a877 43 virtual int close();
ganlikun 0:13413ea9a877 44
ganlikun 0:13413ea9a877 45 virtual ssize_t write(const void *buffer, size_t length);
ganlikun 0:13413ea9a877 46
ganlikun 0:13413ea9a877 47 virtual ssize_t read(void *buffer, size_t length);
ganlikun 0:13413ea9a877 48
ganlikun 0:13413ea9a877 49 virtual int isatty();
ganlikun 0:13413ea9a877 50
ganlikun 0:13413ea9a877 51 virtual off_t seek(off_t position, int whence);
ganlikun 0:13413ea9a877 52
ganlikun 0:13413ea9a877 53 virtual int sync();
ganlikun 0:13413ea9a877 54
ganlikun 0:13413ea9a877 55 virtual off_t size();
ganlikun 0:13413ea9a877 56
ganlikun 0:13413ea9a877 57 protected:
ganlikun 0:13413ea9a877 58 virtual void lock();
ganlikun 0:13413ea9a877 59 virtual void unlock();
ganlikun 0:13413ea9a877 60 FILEHANDLE _fh;
ganlikun 0:13413ea9a877 61 int pos;
ganlikun 0:13413ea9a877 62 PlatformMutex _mutex;
ganlikun 0:13413ea9a877 63 };
ganlikun 0:13413ea9a877 64
ganlikun 0:13413ea9a877 65 /** A filesystem for accessing the local mbed Microcontroller USB disk drive
ganlikun 0:13413ea9a877 66 *
ganlikun 0:13413ea9a877 67 * This allows programs to read and write files on the same disk drive that is used to program the
ganlikun 0:13413ea9a877 68 * mbed Microcontroller. Once created, the standard C file access functions are used to open,
ganlikun 0:13413ea9a877 69 * read and write files.
ganlikun 0:13413ea9a877 70 *
ganlikun 0:13413ea9a877 71 * @note Synchronization level: Thread safe
ganlikun 0:13413ea9a877 72 *
ganlikun 0:13413ea9a877 73 * Example:
ganlikun 0:13413ea9a877 74 * @code
ganlikun 0:13413ea9a877 75 * #include "mbed.h"
ganlikun 0:13413ea9a877 76 *
ganlikun 0:13413ea9a877 77 * LocalFileSystem local("local"); // Create the local filesystem under the name "local"
ganlikun 0:13413ea9a877 78 *
ganlikun 0:13413ea9a877 79 * int main() {
ganlikun 0:13413ea9a877 80 * FILE *fp = fopen("/local/out.txt", "w"); // Open "out.txt" on the local file system for writing
ganlikun 0:13413ea9a877 81 * fprintf(fp, "Hello World!");
ganlikun 0:13413ea9a877 82 * fclose(fp);
ganlikun 0:13413ea9a877 83 * remove("/local/out.txt"); // Removes the file "out.txt" from the local file system
ganlikun 0:13413ea9a877 84 *
ganlikun 0:13413ea9a877 85 * DIR *d = opendir("/local"); // Opens the root directory of the local file system
ganlikun 0:13413ea9a877 86 * struct dirent *p;
ganlikun 0:13413ea9a877 87 * while((p = readdir(d)) != NULL) { // Print the names of the files in the local file system
ganlikun 0:13413ea9a877 88 * printf("%s\n", p->d_name); // to stdout.
ganlikun 0:13413ea9a877 89 * }
ganlikun 0:13413ea9a877 90 * closedir(d);
ganlikun 0:13413ea9a877 91 * }
ganlikun 0:13413ea9a877 92 * @endcode
ganlikun 0:13413ea9a877 93 *
ganlikun 0:13413ea9a877 94 * @note
ganlikun 0:13413ea9a877 95 * If the microcontroller program makes an access to the local drive, it will be marked as "removed"
ganlikun 0:13413ea9a877 96 * on the Host computer. This means it is no longer accessible from the Host Computer.
ganlikun 0:13413ea9a877 97 *
ganlikun 0:13413ea9a877 98 * The drive will only re-appear when the microcontroller program exists. Note that if the program does
ganlikun 0:13413ea9a877 99 * not exit, you will need to hold down reset on the mbed Microcontroller to be able to see the drive again!
ganlikun 0:13413ea9a877 100 * @ingroup platform
ganlikun 0:13413ea9a877 101 */
ganlikun 0:13413ea9a877 102 class LocalFileSystem : public FileSystemLike, private NonCopyable<LocalFileSystem> {
ganlikun 0:13413ea9a877 103 // No modifiable state
ganlikun 0:13413ea9a877 104
ganlikun 0:13413ea9a877 105 public:
ganlikun 0:13413ea9a877 106 LocalFileSystem(const char* n) : FileSystemLike(n) {
ganlikun 0:13413ea9a877 107
ganlikun 0:13413ea9a877 108 }
ganlikun 0:13413ea9a877 109
ganlikun 0:13413ea9a877 110 virtual int open(FileHandle **file, const char *path, int flags);
ganlikun 0:13413ea9a877 111 virtual int open(DirHandle **dir, const char *name);
ganlikun 0:13413ea9a877 112 virtual int remove(const char *filename);
ganlikun 0:13413ea9a877 113 };
ganlikun 0:13413ea9a877 114
ganlikun 0:13413ea9a877 115 } // namespace mbed
ganlikun 0:13413ea9a877 116
ganlikun 0:13413ea9a877 117 #endif
ganlikun 0:13413ea9a877 118
ganlikun 0:13413ea9a877 119 #endif
ganlikun 0:13413ea9a877 120
ganlikun 0:13413ea9a877 121