LocalFileSystem.h 3.6 KB

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