Pinned to some recent date

Committer:
Simon Cooksey
Date:
Thu Nov 17 16:43:53 2016 +0000
Revision:
0:fb7af294d5d9
Initial commit

Who changed what in which revision?

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