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