TUKS MCU Introductory course / TUKS-COURSE-TIMER
Committer:
elmot
Date:
Fri Feb 24 21:13:56 2017 +0000
Revision:
1:d0dfbce63a89
Ready-to-copy

Who changed what in which revision?

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