esp_vfs.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #ifndef __ESP_VFS_H__
  15. #define __ESP_VFS_H__
  16. #include <stdint.h>
  17. #include <stddef.h>
  18. #include "esp_err.h"
  19. #include <sys/types.h>
  20. #include <sys/reent.h>
  21. #include <sys/stat.h>
  22. #include <dirent.h>
  23. #ifdef __cplusplus
  24. extern "C" {
  25. #endif
  26. /**
  27. * Maximum length of path prefix (not including zero terminator)
  28. */
  29. #define ESP_VFS_PATH_MAX 15
  30. /**
  31. * Default value of flags member in esp_vfs_t structure.
  32. */
  33. #define ESP_VFS_FLAG_DEFAULT 0
  34. /**
  35. * Flag which indicates that FS needs extra context pointer in syscalls.
  36. */
  37. #define ESP_VFS_FLAG_CONTEXT_PTR 1
  38. /**
  39. * @brief VFS definition structure
  40. *
  41. * This structure should be filled with pointers to corresponding
  42. * FS driver functions.
  43. *
  44. * If the FS implementation has an option to use certain offset for
  45. * all file descriptors, this value should be passed into fd_offset
  46. * field. Otherwise VFS component will translate all FDs to start
  47. * at zero offset.
  48. *
  49. * Some FS implementations expect some state (e.g. pointer to some structure)
  50. * to be passed in as a first argument. For these implementations,
  51. * populate the members of this structure which have _p suffix, set
  52. * flags member to ESP_VFS_FLAG_CONTEXT_PTR and provide the context pointer
  53. * to esp_vfs_register function.
  54. * If the implementation doesn't use this extra argument, populate the
  55. * members without _p suffix and set flags member to ESP_VFS_FLAG_DEFAULT.
  56. *
  57. * If the FS driver doesn't provide some of the functions, set corresponding
  58. * members to NULL.
  59. */
  60. typedef struct
  61. {
  62. int fd_offset; /*!< file descriptor offset, determined by the FS driver */
  63. int flags; /*!< ESP_VFS_FLAG_CONTEXT_PTR or ESP_VFS_FLAG_DEFAULT */
  64. union {
  65. size_t (*write_p)(void* p, int fd, const void * data, size_t size);
  66. size_t (*write)(int fd, const void * data, size_t size);
  67. };
  68. union {
  69. off_t (*lseek_p)(void* p, int fd, off_t size, int mode);
  70. off_t (*lseek)(int fd, off_t size, int mode);
  71. };
  72. union {
  73. ssize_t (*read_p)(void* ctx, int fd, void * dst, size_t size);
  74. ssize_t (*read)(int fd, void * dst, size_t size);
  75. };
  76. union {
  77. int (*open_p)(void* ctx, const char * path, int flags, int mode);
  78. int (*open)(const char * path, int flags, int mode);
  79. };
  80. union {
  81. int (*close_p)(void* ctx, int fd);
  82. int (*close)(int fd);
  83. };
  84. union {
  85. int (*fstat_p)(void* ctx, int fd, struct stat * st);
  86. int (*fstat)(int fd, struct stat * st);
  87. };
  88. union {
  89. int (*stat_p)(void* ctx, const char * path, struct stat * st);
  90. int (*stat)(const char * path, struct stat * st);
  91. };
  92. union {
  93. int (*link_p)(void* ctx, const char* n1, const char* n2);
  94. int (*link)(const char* n1, const char* n2);
  95. };
  96. union {
  97. int (*unlink_p)(void* ctx, const char *path);
  98. int (*unlink)(const char *path);
  99. };
  100. union {
  101. int (*rename_p)(void* ctx, const char *src, const char *dst);
  102. int (*rename)(const char *src, const char *dst);
  103. };
  104. union {
  105. DIR* (*opendir_p)(void* ctx, const char* name);
  106. DIR* (*opendir)(const char* name);
  107. };
  108. union {
  109. struct dirent* (*readdir_p)(void* ctx, DIR* pdir);
  110. struct dirent* (*readdir)(DIR* pdir);
  111. };
  112. union {
  113. int (*readdir_r_p)(void* ctx, DIR* pdir, struct dirent* entry, struct dirent** out_dirent);
  114. int (*readdir_r)(DIR* pdir, struct dirent* entry, struct dirent** out_dirent);
  115. };
  116. union {
  117. long (*telldir_p)(void* ctx, DIR* pdir);
  118. long (*telldir)(DIR* pdir);
  119. };
  120. union {
  121. void (*seekdir_p)(void* ctx, DIR* pdir, long offset);
  122. void (*seekdir)(DIR* pdir, long offset);
  123. };
  124. union {
  125. int (*closedir_p)(void* ctx, DIR* pdir);
  126. int (*closedir)(DIR* pdir);
  127. };
  128. union {
  129. int (*mkdir_p)(void* ctx, const char* name, mode_t mode);
  130. int (*mkdir)(const char* name, mode_t mode);
  131. };
  132. union {
  133. int (*rmdir_p)(void* ctx, const char* name);
  134. int (*rmdir)(const char* name);
  135. };
  136. } esp_vfs_t;
  137. /**
  138. * Register a virtual filesystem for given path prefix.
  139. *
  140. * @param base_path file path prefix associated with the filesystem.
  141. * Must be a zero-terminated C string, up to ESP_VFS_PATH_MAX
  142. * characters long, and at least 2 characters long.
  143. * Name must start with a "/" and must not end with "/".
  144. * For example, "/data" or "/dev/spi" are valid.
  145. * These VFSes would then be called to handle file paths such as
  146. * "/data/myfile.txt" or "/dev/spi/0".
  147. * @param vfs Pointer to esp_vfs_t, a structure which maps syscalls to
  148. * the filesystem driver functions. VFS component doesn't
  149. * assume ownership of this pointer.
  150. * @param ctx If vfs->flags has ESP_VFS_FLAG_CONTEXT_PTR set, a pointer
  151. * which should be passed to VFS functions. Otherwise, NULL.
  152. *
  153. * @return ESP_OK if successful, ESP_ERR_NO_MEM if too many VFSes are
  154. * registered.
  155. */
  156. esp_err_t esp_vfs_register(const char* base_path, const esp_vfs_t* vfs, void* ctx);
  157. /**
  158. * Unregister a virtual filesystem for given path prefix
  159. *
  160. * @param base_path file prefix previously used in esp_vfs_register call
  161. * @return ESP_OK if successful, ESP_ERR_INVALID_STATE if VFS for given prefix
  162. * hasn't been registered
  163. */
  164. esp_err_t esp_vfs_unregister(const char* base_path);
  165. /**
  166. * These functions are to be used in newlib syscall table. They will be called by
  167. * newlib when it needs to use any of the syscalls.
  168. */
  169. /**@{*/
  170. ssize_t esp_vfs_write(struct _reent *r, int fd, const void * data, size_t size);
  171. off_t esp_vfs_lseek(struct _reent *r, int fd, off_t size, int mode);
  172. ssize_t esp_vfs_read(struct _reent *r, int fd, void * dst, size_t size);
  173. int esp_vfs_open(struct _reent *r, const char * path, int flags, int mode);
  174. int esp_vfs_close(struct _reent *r, int fd);
  175. int esp_vfs_fstat(struct _reent *r, int fd, struct stat * st);
  176. int esp_vfs_stat(struct _reent *r, const char * path, struct stat * st);
  177. int esp_vfs_link(struct _reent *r, const char* n1, const char* n2);
  178. int esp_vfs_unlink(struct _reent *r, const char *path);
  179. int esp_vfs_rename(struct _reent *r, const char *src, const char *dst);
  180. /**@}*/
  181. #ifdef __cplusplus
  182. } // extern "C"
  183. #endif
  184. #endif //__ESP_VFS_H__