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