Dataloger

Committer:
jhon309
Date:
Thu Aug 20 00:37:14 2015 +0000
Revision:
0:666850d06c9f
ok

Who changed what in which revision?

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