mbed libraries for KL25Z
LocalFileSystem.h@0:8024c367e29f, 2012-10-05 (annotated)
- Committer:
- emilmont
- Date:
- Fri Oct 05 09:16:41 2012 +0000
- Revision:
- 0:8024c367e29f
- Child:
- 8:c14af7958ef5
First release of the mbed libraries for KL25Z
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
emilmont | 0:8024c367e29f | 1 | /* mbed Microcontroller Library - LocalFileSystem |
emilmont | 0:8024c367e29f | 2 | * Copyright (c) 2008-2009 ARM Limited. All rights reserved. |
emilmont | 0:8024c367e29f | 3 | */ |
emilmont | 0:8024c367e29f | 4 | |
emilmont | 0:8024c367e29f | 5 | #ifndef MBED_LOCALFILESYSTEM_H |
emilmont | 0:8024c367e29f | 6 | #define MBED_LOCALFILESYSTEM_H |
emilmont | 0:8024c367e29f | 7 | |
emilmont | 0:8024c367e29f | 8 | #include "device.h" |
emilmont | 0:8024c367e29f | 9 | |
emilmont | 0:8024c367e29f | 10 | #if DEVICE_LOCALFILESYSTEM |
emilmont | 0:8024c367e29f | 11 | |
emilmont | 0:8024c367e29f | 12 | #include "FileSystemLike.h" |
emilmont | 0:8024c367e29f | 13 | |
emilmont | 0:8024c367e29f | 14 | namespace mbed { |
emilmont | 0:8024c367e29f | 15 | |
emilmont | 0:8024c367e29f | 16 | FILEHANDLE local_file_open(const char* name, int flags); |
emilmont | 0:8024c367e29f | 17 | |
emilmont | 0:8024c367e29f | 18 | class LocalFileHandle : public FileHandle { |
emilmont | 0:8024c367e29f | 19 | |
emilmont | 0:8024c367e29f | 20 | public: |
emilmont | 0:8024c367e29f | 21 | LocalFileHandle(FILEHANDLE fh); |
emilmont | 0:8024c367e29f | 22 | |
emilmont | 0:8024c367e29f | 23 | virtual int close(); |
emilmont | 0:8024c367e29f | 24 | |
emilmont | 0:8024c367e29f | 25 | virtual ssize_t write(const void *buffer, size_t length); |
emilmont | 0:8024c367e29f | 26 | |
emilmont | 0:8024c367e29f | 27 | virtual ssize_t read(void *buffer, size_t length); |
emilmont | 0:8024c367e29f | 28 | |
emilmont | 0:8024c367e29f | 29 | virtual int isatty(); |
emilmont | 0:8024c367e29f | 30 | |
emilmont | 0:8024c367e29f | 31 | virtual off_t lseek(off_t position, int whence); |
emilmont | 0:8024c367e29f | 32 | |
emilmont | 0:8024c367e29f | 33 | virtual int fsync(); |
emilmont | 0:8024c367e29f | 34 | |
emilmont | 0:8024c367e29f | 35 | virtual off_t flen(); |
emilmont | 0:8024c367e29f | 36 | |
emilmont | 0:8024c367e29f | 37 | protected: |
emilmont | 0:8024c367e29f | 38 | FILEHANDLE _fh; |
emilmont | 0:8024c367e29f | 39 | int pos; |
emilmont | 0:8024c367e29f | 40 | }; |
emilmont | 0:8024c367e29f | 41 | |
emilmont | 0:8024c367e29f | 42 | /* Class: LocalFileSystem |
emilmont | 0:8024c367e29f | 43 | * A filesystem for accessing the local mbed Microcontroller USB disk drive |
emilmont | 0:8024c367e29f | 44 | * |
emilmont | 0:8024c367e29f | 45 | * This allows programs to read and write files on the same disk drive that is used to program the |
emilmont | 0:8024c367e29f | 46 | * mbed Microcontroller. Once created, the standard C file access functions are used to open, |
emilmont | 0:8024c367e29f | 47 | * read and write files. |
emilmont | 0:8024c367e29f | 48 | * |
emilmont | 0:8024c367e29f | 49 | * Example: |
emilmont | 0:8024c367e29f | 50 | * > #include "mbed.h" |
emilmont | 0:8024c367e29f | 51 | * > |
emilmont | 0:8024c367e29f | 52 | * > LocalFileSystem local("local"); // Create the local filesystem under the name "local" |
emilmont | 0:8024c367e29f | 53 | * > |
emilmont | 0:8024c367e29f | 54 | * > int main() { |
emilmont | 0:8024c367e29f | 55 | * > FILE *fp = fopen("/local/out.txt", "w"); // Open "out.txt" on the local file system for writing |
emilmont | 0:8024c367e29f | 56 | * > fprintf(fp, "Hello World!"); |
emilmont | 0:8024c367e29f | 57 | * > fclose(fp); |
emilmont | 0:8024c367e29f | 58 | * > remove("/local/out.txt"); // Removes the file "out.txt" from the local file system |
emilmont | 0:8024c367e29f | 59 | * > |
emilmont | 0:8024c367e29f | 60 | * > DIR *d = opendir("/local"); // Opens the root directory of the local file system |
emilmont | 0:8024c367e29f | 61 | * > struct dirent *p; |
emilmont | 0:8024c367e29f | 62 | * > while((p = readdir(d)) != NULL) { // Print the names of the files in the local file system |
emilmont | 0:8024c367e29f | 63 | * > printf("%s\n", p->d_name); // to stdout. |
emilmont | 0:8024c367e29f | 64 | * > } |
emilmont | 0:8024c367e29f | 65 | * > closedir(d); |
emilmont | 0:8024c367e29f | 66 | * > } |
emilmont | 0:8024c367e29f | 67 | * |
emilmont | 0:8024c367e29f | 68 | * Implementation Notes: |
emilmont | 0:8024c367e29f | 69 | * If the microcontroller program makes an access to the local drive, it will be marked as "removed" |
emilmont | 0:8024c367e29f | 70 | * on the Host computer. This means it is no longer accessible from the Host Computer. |
emilmont | 0:8024c367e29f | 71 | * |
emilmont | 0:8024c367e29f | 72 | * The drive will only re-appear when the microcontroller program exists. Note that if the program does |
emilmont | 0:8024c367e29f | 73 | * not exit, you will need to hold down reset on the mbed Microcontroller to be able to see the drive again! |
emilmont | 0:8024c367e29f | 74 | */ |
emilmont | 0:8024c367e29f | 75 | class LocalFileSystem : public FileSystemLike { |
emilmont | 0:8024c367e29f | 76 | |
emilmont | 0:8024c367e29f | 77 | public: |
emilmont | 0:8024c367e29f | 78 | |
emilmont | 0:8024c367e29f | 79 | LocalFileSystem(const char* n) : FileSystemLike(n) { |
emilmont | 0:8024c367e29f | 80 | |
emilmont | 0:8024c367e29f | 81 | } |
emilmont | 0:8024c367e29f | 82 | |
emilmont | 0:8024c367e29f | 83 | virtual FileHandle *open(const char* name, int flags); |
emilmont | 0:8024c367e29f | 84 | virtual int remove(const char *filename); |
emilmont | 0:8024c367e29f | 85 | virtual DirHandle *opendir(const char *name); |
emilmont | 0:8024c367e29f | 86 | }; |
emilmont | 0:8024c367e29f | 87 | |
emilmont | 0:8024c367e29f | 88 | } // namespace mbed |
emilmont | 0:8024c367e29f | 89 | |
emilmont | 0:8024c367e29f | 90 | #endif |
emilmont | 0:8024c367e29f | 91 | |
emilmont | 0:8024c367e29f | 92 | #endif |