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