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