mbed library sources. Supersedes mbed-src.

Dependents:   Nucleo_Hello_Encoder BLE_iBeaconScan AM1805_DEMO DISCO-F429ZI_ExportTemplate1 ... more

Committer:
AnnaBridge
Date:
Wed Feb 20 22:31:08 2019 +0000
Revision:
189:f392fc9709a3
Parent:
187:0387e8f68319
mbed library release version 165

Who changed what in which revision?

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