esp_vfs.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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 <stdarg.h>
  19. #include "esp_err.h"
  20. #include <sys/types.h>
  21. #include <sys/reent.h>
  22. #include <sys/stat.h>
  23. #include <dirent.h>
  24. #ifdef __cplusplus
  25. extern "C" {
  26. #endif
  27. /**
  28. * Maximum length of path prefix (not including zero terminator)
  29. */
  30. #define ESP_VFS_PATH_MAX 15
  31. /**
  32. * Default value of flags member in esp_vfs_t structure.
  33. */
  34. #define ESP_VFS_FLAG_DEFAULT 0
  35. /**
  36. * Flag which indicates that FS needs extra context pointer in syscalls.
  37. */
  38. #define ESP_VFS_FLAG_CONTEXT_PTR 1
  39. /**
  40. * Flag which indicates that the FD space of the VFS implementation should be made
  41. * the same as the FD space in newlib. This means that the normal masking off
  42. * of VFS-independent fd bits is ignored and the full user-facing fd is passed to
  43. * the VFS implementation.
  44. *
  45. * Set the p_minimum_fd & p_maximum_fd pointers when registering the socket in
  46. * order to know what range of FDs can be used with the registered VFS.
  47. *
  48. * This is mostly useful for LWIP which shares the socket FD space with
  49. * socket-specific functions.
  50. *
  51. */
  52. #define ESP_VFS_FLAG_SHARED_FD_SPACE 2
  53. /**
  54. * @brief VFS definition structure
  55. *
  56. * This structure should be filled with pointers to corresponding
  57. * FS driver functions.
  58. *
  59. * VFS component will translate all FDs so that the filesystem implementation
  60. * sees them starting at zero. The caller sees a global FD which is prefixed
  61. * with an pre-filesystem-implementation.
  62. *
  63. * Some FS implementations expect some state (e.g. pointer to some structure)
  64. * to be passed in as a first argument. For these implementations,
  65. * populate the members of this structure which have _p suffix, set
  66. * flags member to ESP_VFS_FLAG_CONTEXT_PTR and provide the context pointer
  67. * to esp_vfs_register function.
  68. * If the implementation doesn't use this extra argument, populate the
  69. * members without _p suffix and set flags member to ESP_VFS_FLAG_DEFAULT.
  70. *
  71. * If the FS driver doesn't provide some of the functions, set corresponding
  72. * members to NULL.
  73. */
  74. typedef struct
  75. {
  76. int flags; /*!< ESP_VFS_FLAG_CONTEXT_PTR or ESP_VFS_FLAG_DEFAULT, plus optionally ESP_VFS_FLAG_SHARED_FD_SPACE */
  77. union {
  78. ssize_t (*write_p)(void* p, int fd, const void * data, size_t size);
  79. ssize_t (*write)(int fd, const void * data, size_t size);
  80. };
  81. union {
  82. off_t (*lseek_p)(void* p, int fd, off_t size, int mode);
  83. off_t (*lseek)(int fd, off_t size, int mode);
  84. };
  85. union {
  86. ssize_t (*read_p)(void* ctx, int fd, void * dst, size_t size);
  87. ssize_t (*read)(int fd, void * dst, size_t size);
  88. };
  89. union {
  90. int (*open_p)(void* ctx, const char * path, int flags, int mode);
  91. int (*open)(const char * path, int flags, int mode);
  92. };
  93. union {
  94. int (*close_p)(void* ctx, int fd);
  95. int (*close)(int fd);
  96. };
  97. union {
  98. int (*fstat_p)(void* ctx, int fd, struct stat * st);
  99. int (*fstat)(int fd, struct stat * st);
  100. };
  101. union {
  102. int (*stat_p)(void* ctx, const char * path, struct stat * st);
  103. int (*stat)(const char * path, struct stat * st);
  104. };
  105. union {
  106. int (*link_p)(void* ctx, const char* n1, const char* n2);
  107. int (*link)(const char* n1, const char* n2);
  108. };
  109. union {
  110. int (*unlink_p)(void* ctx, const char *path);
  111. int (*unlink)(const char *path);
  112. };
  113. union {
  114. int (*rename_p)(void* ctx, const char *src, const char *dst);
  115. int (*rename)(const char *src, const char *dst);
  116. };
  117. union {
  118. DIR* (*opendir_p)(void* ctx, const char* name);
  119. DIR* (*opendir)(const char* name);
  120. };
  121. union {
  122. struct dirent* (*readdir_p)(void* ctx, DIR* pdir);
  123. struct dirent* (*readdir)(DIR* pdir);
  124. };
  125. union {
  126. int (*readdir_r_p)(void* ctx, DIR* pdir, struct dirent* entry, struct dirent** out_dirent);
  127. int (*readdir_r)(DIR* pdir, struct dirent* entry, struct dirent** out_dirent);
  128. };
  129. union {
  130. long (*telldir_p)(void* ctx, DIR* pdir);
  131. long (*telldir)(DIR* pdir);
  132. };
  133. union {
  134. void (*seekdir_p)(void* ctx, DIR* pdir, long offset);
  135. void (*seekdir)(DIR* pdir, long offset);
  136. };
  137. union {
  138. int (*closedir_p)(void* ctx, DIR* pdir);
  139. int (*closedir)(DIR* pdir);
  140. };
  141. union {
  142. int (*mkdir_p)(void* ctx, const char* name, mode_t mode);
  143. int (*mkdir)(const char* name, mode_t mode);
  144. };
  145. union {
  146. int (*rmdir_p)(void* ctx, const char* name);
  147. int (*rmdir)(const char* name);
  148. };
  149. union {
  150. int (*fcntl_p)(void* ctx, int fd, int cmd, va_list args);
  151. int (*fcntl)(int fd, int cmd, va_list args);
  152. };
  153. union {
  154. int (*ioctl_p)(void* ctx, int fd, int cmd, va_list args);
  155. int (*ioctl)(int fd, int cmd, va_list args);
  156. };
  157. union {
  158. int (*fsync_p)(void* ctx, int fd);
  159. int (*fsync)(int fd);
  160. };
  161. } esp_vfs_t;
  162. /**
  163. * Register a virtual filesystem for given path prefix.
  164. *
  165. * @param base_path file path prefix associated with the filesystem.
  166. * Must be a zero-terminated C string, up to ESP_VFS_PATH_MAX
  167. * characters long, and at least 2 characters long.
  168. * Name must start with a "/" and must not end with "/".
  169. * For example, "/data" or "/dev/spi" are valid.
  170. * These VFSes would then be called to handle file paths such as
  171. * "/data/myfile.txt" or "/dev/spi/0".
  172. * @param vfs Pointer to esp_vfs_t, a structure which maps syscalls to
  173. * the filesystem driver functions. VFS component doesn't
  174. * assume ownership of this pointer.
  175. * @param ctx If vfs->flags has ESP_VFS_FLAG_CONTEXT_PTR set, a pointer
  176. * which should be passed to VFS functions. Otherwise, NULL.
  177. *
  178. * @return ESP_OK if successful, ESP_ERR_NO_MEM if too many VFSes are
  179. * registered.
  180. */
  181. esp_err_t esp_vfs_register(const char* base_path, const esp_vfs_t* vfs, void* ctx);
  182. /**
  183. * Special case function for registering a VFS that uses a method other than
  184. * open() to open new file descriptors.
  185. *
  186. * This is a special-purpose function intended for registering LWIP sockets to VFS.
  187. *
  188. * @param vfs Pointer to esp_vfs_t. Meaning is the same as for esp_vfs_register().
  189. * @param ctx Pointer to context structure. Meaning is the same as for esp_vfs_register().
  190. * @param p_min_fd If non-NULL, on success this variable is written with the minimum (global/user-facing) FD that this VFS will use. This is useful when ESP_VFS_FLAG_SHARED_FD_SPACE is set in vfs->flags.
  191. * @param p_max_fd If non-NULL, on success this variable is written with one higher than the maximum (global/user-facing) FD that this VFS will use. This is useful when ESP_VFS_FLAG_SHARED_FD_SPACE is set in vfs->flags.
  192. *
  193. * @return ESP_OK if successful, ESP_ERR_NO_MEM if too many VFSes are
  194. * registered.
  195. */
  196. esp_err_t esp_vfs_register_socket_space(const esp_vfs_t *vfs, void *ctx, int *p_min_fd, int *p_max_fd);
  197. /**
  198. * Unregister a virtual filesystem for given path prefix
  199. *
  200. * @param base_path file prefix previously used in esp_vfs_register call
  201. * @return ESP_OK if successful, ESP_ERR_INVALID_STATE if VFS for given prefix
  202. * hasn't been registered
  203. */
  204. esp_err_t esp_vfs_unregister(const char* base_path);
  205. /**
  206. * These functions are to be used in newlib syscall table. They will be called by
  207. * newlib when it needs to use any of the syscalls.
  208. */
  209. /**@{*/
  210. ssize_t esp_vfs_write(struct _reent *r, int fd, const void * data, size_t size);
  211. off_t esp_vfs_lseek(struct _reent *r, int fd, off_t size, int mode);
  212. ssize_t esp_vfs_read(struct _reent *r, int fd, void * dst, size_t size);
  213. int esp_vfs_open(struct _reent *r, const char * path, int flags, int mode);
  214. int esp_vfs_close(struct _reent *r, int fd);
  215. int esp_vfs_fstat(struct _reent *r, int fd, struct stat * st);
  216. int esp_vfs_stat(struct _reent *r, const char * path, struct stat * st);
  217. int esp_vfs_link(struct _reent *r, const char* n1, const char* n2);
  218. int esp_vfs_unlink(struct _reent *r, const char *path);
  219. int esp_vfs_rename(struct _reent *r, const char *src, const char *dst);
  220. /**@}*/
  221. #ifdef __cplusplus
  222. } // extern "C"
  223. #endif
  224. #endif //__ESP_VFS_H__