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

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.