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