SPKT

Dependents:   WAV

Committer:
phungductung
Date:
Tue Jun 04 21:51:46 2019 +0000
Revision:
0:e87aa4c49e95
libray

Who changed what in which revision?

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