test

Committer:
elijahsj
Date:
Mon Nov 09 00:02:47 2020 -0500
Revision:
1:8a094db1347f
test

Who changed what in which revision?

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