esp_vfs.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. // Copyright 2015-2020 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 <unistd.h>
  20. #include <utime.h>
  21. #include "freertos/FreeRTOS.h"
  22. #include "freertos/semphr.h"
  23. #include "esp_err.h"
  24. #include <sys/types.h>
  25. #include <sys/reent.h>
  26. #include <sys/stat.h>
  27. #include <sys/time.h>
  28. #include <sys/termios.h>
  29. #include <sys/poll.h>
  30. #include <sys/dirent.h>
  31. #include <string.h>
  32. #include "sdkconfig.h"
  33. #ifdef __cplusplus
  34. extern "C" {
  35. #endif
  36. #ifndef _SYS_TYPES_FD_SET
  37. #error "VFS should be used with FD_SETSIZE and FD_SET from sys/types.h"
  38. #endif
  39. /**
  40. * Maximum number of (global) file descriptors.
  41. */
  42. #define MAX_FDS FD_SETSIZE /* for compatibility with fd_set and select() */
  43. /**
  44. * Maximum length of path prefix (not including zero terminator)
  45. */
  46. #define ESP_VFS_PATH_MAX 15
  47. /**
  48. * Default value of flags member in esp_vfs_t structure.
  49. */
  50. #define ESP_VFS_FLAG_DEFAULT 0
  51. /**
  52. * Flag which indicates that FS needs extra context pointer in syscalls.
  53. */
  54. #define ESP_VFS_FLAG_CONTEXT_PTR 1
  55. /*
  56. * @brief VFS identificator used for esp_vfs_register_with_id()
  57. */
  58. typedef int esp_vfs_id_t;
  59. /**
  60. * @brief VFS semaphore type for select()
  61. *
  62. */
  63. typedef struct
  64. {
  65. bool is_sem_local; /*!< type of "sem" is SemaphoreHandle_t when true, defined by socket driver otherwise */
  66. void *sem; /*!< semaphore instance */
  67. } esp_vfs_select_sem_t;
  68. /**
  69. * @brief VFS definition structure
  70. *
  71. * This structure should be filled with pointers to corresponding
  72. * FS driver functions.
  73. *
  74. * VFS component will translate all FDs so that the filesystem implementation
  75. * sees them starting at zero. The caller sees a global FD which is prefixed
  76. * with an pre-filesystem-implementation.
  77. *
  78. * Some FS implementations expect some state (e.g. pointer to some structure)
  79. * to be passed in as a first argument. For these implementations,
  80. * populate the members of this structure which have _p suffix, set
  81. * flags member to ESP_VFS_FLAG_CONTEXT_PTR and provide the context pointer
  82. * to esp_vfs_register function.
  83. * If the implementation doesn't use this extra argument, populate the
  84. * members without _p suffix and set flags member to ESP_VFS_FLAG_DEFAULT.
  85. *
  86. * If the FS driver doesn't provide some of the functions, set corresponding
  87. * members to NULL.
  88. */
  89. typedef struct
  90. {
  91. int flags; /*!< ESP_VFS_FLAG_CONTEXT_PTR or ESP_VFS_FLAG_DEFAULT */
  92. union {
  93. ssize_t (*write_p)(void* p, int fd, const void * data, size_t size); /*!< Write with context pointer */
  94. ssize_t (*write)(int fd, const void * data, size_t size); /*!< Write without context pointer */
  95. };
  96. union {
  97. off_t (*lseek_p)(void* p, int fd, off_t size, int mode); /*!< Seek with context pointer */
  98. off_t (*lseek)(int fd, off_t size, int mode); /*!< Seek without context pointer */
  99. };
  100. union {
  101. ssize_t (*read_p)(void* ctx, int fd, void * dst, size_t size); /*!< Read with context pointer */
  102. ssize_t (*read)(int fd, void * dst, size_t size); /*!< Read without context pointer */
  103. };
  104. union {
  105. ssize_t (*pread_p)(void *ctx, int fd, void * dst, size_t size, off_t offset); /*!< pread with context pointer */
  106. ssize_t (*pread)(int fd, void * dst, size_t size, off_t offset); /*!< pread without context pointer */
  107. };
  108. union {
  109. ssize_t (*pwrite_p)(void *ctx, int fd, const void *src, size_t size, off_t offset); /*!< pwrite with context pointer */
  110. ssize_t (*pwrite)(int fd, const void *src, size_t size, off_t offset); /*!< pwrite without context pointer */
  111. };
  112. union {
  113. int (*open_p)(void* ctx, const char * path, int flags, int mode); /*!< open with context pointer */
  114. int (*open)(const char * path, int flags, int mode); /*!< open without context pointer */
  115. };
  116. union {
  117. int (*close_p)(void* ctx, int fd); /*!< close with context pointer */
  118. int (*close)(int fd); /*!< close without context pointer */
  119. };
  120. union {
  121. int (*fstat_p)(void* ctx, int fd, struct stat * st); /*!< fstat with context pointer */
  122. int (*fstat)(int fd, struct stat * st); /*!< fstat without context pointer */
  123. };
  124. #ifdef CONFIG_VFS_SUPPORT_DIR
  125. union {
  126. int (*stat_p)(void* ctx, const char * path, struct stat * st); /*!< stat with context pointer */
  127. int (*stat)(const char * path, struct stat * st); /*!< stat without context pointer */
  128. };
  129. union {
  130. int (*link_p)(void* ctx, const char* n1, const char* n2); /*!< link with context pointer */
  131. int (*link)(const char* n1, const char* n2); /*!< link without context pointer */
  132. };
  133. union {
  134. int (*unlink_p)(void* ctx, const char *path); /*!< unlink with context pointer */
  135. int (*unlink)(const char *path); /*!< unlink without context pointer */
  136. };
  137. union {
  138. int (*rename_p)(void* ctx, const char *src, const char *dst); /*!< rename with context pointer */
  139. int (*rename)(const char *src, const char *dst); /*!< rename without context pointer */
  140. };
  141. union {
  142. DIR* (*opendir_p)(void* ctx, const char* name); /*!< opendir with context pointer */
  143. DIR* (*opendir)(const char* name); /*!< opendir without context pointer */
  144. };
  145. union {
  146. struct dirent* (*readdir_p)(void* ctx, DIR* pdir); /*!< readdir with context pointer */
  147. struct dirent* (*readdir)(DIR* pdir); /*!< readdir without context pointer */
  148. };
  149. union {
  150. int (*readdir_r_p)(void* ctx, DIR* pdir, struct dirent* entry, struct dirent** out_dirent); /*!< readdir_r with context pointer */
  151. int (*readdir_r)(DIR* pdir, struct dirent* entry, struct dirent** out_dirent); /*!< readdir_r without context pointer */
  152. };
  153. union {
  154. long (*telldir_p)(void* ctx, DIR* pdir); /*!< telldir with context pointer */
  155. long (*telldir)(DIR* pdir); /*!< telldir without context pointer */
  156. };
  157. union {
  158. void (*seekdir_p)(void* ctx, DIR* pdir, long offset); /*!< seekdir with context pointer */
  159. void (*seekdir)(DIR* pdir, long offset); /*!< seekdir without context pointer */
  160. };
  161. union {
  162. int (*closedir_p)(void* ctx, DIR* pdir); /*!< closedir with context pointer */
  163. int (*closedir)(DIR* pdir); /*!< closedir without context pointer */
  164. };
  165. union {
  166. int (*mkdir_p)(void* ctx, const char* name, mode_t mode); /*!< mkdir with context pointer */
  167. int (*mkdir)(const char* name, mode_t mode); /*!< mkdir without context pointer */
  168. };
  169. union {
  170. int (*rmdir_p)(void* ctx, const char* name); /*!< rmdir with context pointer */
  171. int (*rmdir)(const char* name); /*!< rmdir without context pointer */
  172. };
  173. #endif // CONFIG_VFS_SUPPORT_DIR
  174. union {
  175. int (*fcntl_p)(void* ctx, int fd, int cmd, int arg); /*!< fcntl with context pointer */
  176. int (*fcntl)(int fd, int cmd, int arg); /*!< fcntl without context pointer */
  177. };
  178. union {
  179. int (*ioctl_p)(void* ctx, int fd, int cmd, va_list args); /*!< ioctl with context pointer */
  180. int (*ioctl)(int fd, int cmd, va_list args); /*!< ioctl without context pointer */
  181. };
  182. union {
  183. int (*fsync_p)(void* ctx, int fd); /*!< fsync with context pointer */
  184. int (*fsync)(int fd); /*!< fsync without context pointer */
  185. };
  186. #ifdef CONFIG_VFS_SUPPORT_DIR
  187. union {
  188. int (*access_p)(void* ctx, const char *path, int amode); /*!< access with context pointer */
  189. int (*access)(const char *path, int amode); /*!< access without context pointer */
  190. };
  191. union {
  192. int (*truncate_p)(void* ctx, const char *path, off_t length); /*!< truncate with context pointer */
  193. int (*truncate)(const char *path, off_t length); /*!< truncate without context pointer */
  194. };
  195. union {
  196. int (*utime_p)(void* ctx, const char *path, const struct utimbuf *times); /*!< utime with context pointer */
  197. int (*utime)(const char *path, const struct utimbuf *times); /*!< utime without context pointer */
  198. };
  199. #endif // CONFIG_VFS_SUPPORT_DIR
  200. #ifdef CONFIG_VFS_SUPPORT_TERMIOS
  201. union {
  202. int (*tcsetattr_p)(void *ctx, int fd, int optional_actions, const struct termios *p); /*!< tcsetattr with context pointer */
  203. int (*tcsetattr)(int fd, int optional_actions, const struct termios *p); /*!< tcsetattr without context pointer */
  204. };
  205. union {
  206. int (*tcgetattr_p)(void *ctx, int fd, struct termios *p); /*!< tcgetattr with context pointer */
  207. int (*tcgetattr)(int fd, struct termios *p); /*!< tcgetattr without context pointer */
  208. };
  209. union {
  210. int (*tcdrain_p)(void *ctx, int fd); /*!< tcdrain with context pointer */
  211. int (*tcdrain)(int fd); /*!< tcdrain without context pointer */
  212. };
  213. union {
  214. int (*tcflush_p)(void *ctx, int fd, int select); /*!< tcflush with context pointer */
  215. int (*tcflush)(int fd, int select); /*!< tcflush without context pointer */
  216. };
  217. union {
  218. int (*tcflow_p)(void *ctx, int fd, int action); /*!< tcflow with context pointer */
  219. int (*tcflow)(int fd, int action); /*!< tcflow without context pointer */
  220. };
  221. union {
  222. pid_t (*tcgetsid_p)(void *ctx, int fd); /*!< tcgetsid with context pointer */
  223. pid_t (*tcgetsid)(int fd); /*!< tcgetsid without context pointer */
  224. };
  225. union {
  226. int (*tcsendbreak_p)(void *ctx, int fd, int duration); /*!< tcsendbreak with context pointer */
  227. int (*tcsendbreak)(int fd, int duration); /*!< tcsendbreak without context pointer */
  228. };
  229. #endif // CONFIG_VFS_SUPPORT_TERMIOS
  230. #ifdef CONFIG_VFS_SUPPORT_SELECT
  231. /** start_select is called for setting up synchronous I/O multiplexing of the desired file descriptors in the given VFS */
  232. esp_err_t (*start_select)(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, esp_vfs_select_sem_t sem, void **end_select_args);
  233. /** socket select function for socket FDs with the functionality of POSIX select(); this should be set only for the socket VFS */
  234. int (*socket_select)(int nfds, fd_set *readfds, fd_set *writefds, fd_set *errorfds, struct timeval *timeout);
  235. /** called by VFS to interrupt the socket_select call when select is activated from a non-socket VFS driver; set only for the socket driver */
  236. void (*stop_socket_select)(void *sem);
  237. /** stop_socket_select which can be called from ISR; set only for the socket driver */
  238. void (*stop_socket_select_isr)(void *sem, BaseType_t *woken);
  239. /** end_select is called to stop the I/O multiplexing and deinitialize the environment created by start_select for the given VFS */
  240. void* (*get_socket_select_semaphore)(void);
  241. /** get_socket_select_semaphore returns semaphore allocated in the socket driver; set only for the socket driver */
  242. esp_err_t (*end_select)(void *end_select_args);
  243. #endif // CONFIG_VFS_SUPPORT_SELECT
  244. } esp_vfs_t;
  245. /**
  246. * Register a virtual filesystem for given path prefix.
  247. *
  248. * @param base_path file path prefix associated with the filesystem.
  249. * Must be a zero-terminated C string, may be empty.
  250. * If not empty, must be up to ESP_VFS_PATH_MAX
  251. * characters long, and at least 2 characters long.
  252. * Name must start with a "/" and must not end with "/".
  253. * For example, "/data" or "/dev/spi" are valid.
  254. * These VFSes would then be called to handle file paths such as
  255. * "/data/myfile.txt" or "/dev/spi/0".
  256. * In the special case of an empty base_path, a "fallback"
  257. * VFS is registered. Such VFS will handle paths which are not
  258. * matched by any other registered VFS.
  259. * @param vfs Pointer to esp_vfs_t, a structure which maps syscalls to
  260. * the filesystem driver functions. VFS component doesn't
  261. * assume ownership of this pointer.
  262. * @param ctx If vfs->flags has ESP_VFS_FLAG_CONTEXT_PTR set, a pointer
  263. * which should be passed to VFS functions. Otherwise, NULL.
  264. *
  265. * @return ESP_OK if successful, ESP_ERR_NO_MEM if too many VFSes are
  266. * registered.
  267. */
  268. esp_err_t esp_vfs_register(const char* base_path, const esp_vfs_t* vfs, void* ctx);
  269. /**
  270. * Special case function for registering a VFS that uses a method other than
  271. * open() to open new file descriptors from the interval <min_fd; max_fd).
  272. *
  273. * This is a special-purpose function intended for registering LWIP sockets to VFS.
  274. *
  275. * @param vfs Pointer to esp_vfs_t. Meaning is the same as for esp_vfs_register().
  276. * @param ctx Pointer to context structure. Meaning is the same as for esp_vfs_register().
  277. * @param min_fd The smallest file descriptor this VFS will use.
  278. * @param max_fd Upper boundary for file descriptors this VFS will use (the biggest file descriptor plus one).
  279. *
  280. * @return ESP_OK if successful, ESP_ERR_NO_MEM if too many VFSes are
  281. * registered, ESP_ERR_INVALID_ARG if the file descriptor boundaries
  282. * are incorrect.
  283. */
  284. esp_err_t esp_vfs_register_fd_range(const esp_vfs_t *vfs, void *ctx, int min_fd, int max_fd);
  285. /**
  286. * Special case function for registering a VFS that uses a method other than
  287. * open() to open new file descriptors. In comparison with
  288. * esp_vfs_register_fd_range, this function doesn't pre-registers an interval
  289. * of file descriptors. File descriptors can be registered later, by using
  290. * esp_vfs_register_fd.
  291. *
  292. * @param vfs Pointer to esp_vfs_t. Meaning is the same as for esp_vfs_register().
  293. * @param ctx Pointer to context structure. Meaning is the same as for esp_vfs_register().
  294. * @param vfs_id Here will be written the VFS ID which can be passed to
  295. * esp_vfs_register_fd for registering file descriptors.
  296. *
  297. * @return ESP_OK if successful, ESP_ERR_NO_MEM if too many VFSes are
  298. * registered, ESP_ERR_INVALID_ARG if the file descriptor boundaries
  299. * are incorrect.
  300. */
  301. esp_err_t esp_vfs_register_with_id(const esp_vfs_t *vfs, void *ctx, esp_vfs_id_t *vfs_id);
  302. /**
  303. * Unregister a virtual filesystem for given path prefix
  304. *
  305. * @param base_path file prefix previously used in esp_vfs_register call
  306. * @return ESP_OK if successful, ESP_ERR_INVALID_STATE if VFS for given prefix
  307. * hasn't been registered
  308. */
  309. esp_err_t esp_vfs_unregister(const char* base_path);
  310. /**
  311. * Unregister a virtual filesystem with the given index
  312. *
  313. * @param vfs_id The VFS ID returned by esp_vfs_register_with_id
  314. * @return ESP_OK if successful, ESP_ERR_INVALID_STATE if VFS for the given index
  315. * hasn't been registered
  316. */
  317. esp_err_t esp_vfs_unregister_with_id(esp_vfs_id_t vfs_id);
  318. /**
  319. * Special function for registering another file descriptor for a VFS registered
  320. * by esp_vfs_register_with_id.
  321. *
  322. * @param vfs_id VFS identificator returned by esp_vfs_register_with_id.
  323. * @param fd The registered file descriptor will be written to this address.
  324. *
  325. * @return ESP_OK if the registration is successful,
  326. * ESP_ERR_NO_MEM if too many file descriptors are registered,
  327. * ESP_ERR_INVALID_ARG if the arguments are incorrect.
  328. */
  329. esp_err_t esp_vfs_register_fd(esp_vfs_id_t vfs_id, int *fd);
  330. /**
  331. * Special function for registering another file descriptor with given local_fd
  332. * for a VFS registered by esp_vfs_register_with_id.
  333. *
  334. * @param vfs_id VFS identificator returned by esp_vfs_register_with_id.
  335. * @param local_fd The fd in the local vfs. Passing -1 will set the local fd as the (*fd) value.
  336. * @param permanent Whether the fd should be treated as permannet (not removed after close())
  337. * @param fd The registered file descriptor will be written to this address.
  338. *
  339. * @return ESP_OK if the registration is successful,
  340. * ESP_ERR_NO_MEM if too many file descriptors are registered,
  341. * ESP_ERR_INVALID_ARG if the arguments are incorrect.
  342. */
  343. esp_err_t esp_vfs_register_fd_with_local_fd(esp_vfs_id_t vfs_id, int local_fd, bool permanent, int *fd);
  344. /**
  345. * Special function for unregistering a file descriptor belonging to a VFS
  346. * registered by esp_vfs_register_with_id.
  347. *
  348. * @param vfs_id VFS identificator returned by esp_vfs_register_with_id.
  349. * @param fd File descriptor which should be unregistered.
  350. *
  351. * @return ESP_OK if the registration is successful,
  352. * ESP_ERR_INVALID_ARG if the arguments are incorrect.
  353. */
  354. esp_err_t esp_vfs_unregister_fd(esp_vfs_id_t vfs_id, int fd);
  355. /**
  356. * These functions are to be used in newlib syscall table. They will be called by
  357. * newlib when it needs to use any of the syscalls.
  358. */
  359. /**@{*/
  360. ssize_t esp_vfs_write(struct _reent *r, int fd, const void * data, size_t size);
  361. off_t esp_vfs_lseek(struct _reent *r, int fd, off_t size, int mode);
  362. ssize_t esp_vfs_read(struct _reent *r, int fd, void * dst, size_t size);
  363. int esp_vfs_open(struct _reent *r, const char * path, int flags, int mode);
  364. int esp_vfs_close(struct _reent *r, int fd);
  365. int esp_vfs_fstat(struct _reent *r, int fd, struct stat * st);
  366. int esp_vfs_stat(struct _reent *r, const char * path, struct stat * st);
  367. int esp_vfs_link(struct _reent *r, const char* n1, const char* n2);
  368. int esp_vfs_unlink(struct _reent *r, const char *path);
  369. int esp_vfs_rename(struct _reent *r, const char *src, const char *dst);
  370. int esp_vfs_utime(const char *path, const struct utimbuf *times);
  371. /**@}*/
  372. /**
  373. * @brief Synchronous I/O multiplexing which implements the functionality of POSIX select() for VFS
  374. * @param nfds Specifies the range of descriptors which should be checked.
  375. * The first nfds descriptors will be checked in each set.
  376. * @param readfds If not NULL, then points to a descriptor set that on input
  377. * specifies which descriptors should be checked for being
  378. * ready to read, and on output indicates which descriptors
  379. * are ready to read.
  380. * @param writefds If not NULL, then points to a descriptor set that on input
  381. * specifies which descriptors should be checked for being
  382. * ready to write, and on output indicates which descriptors
  383. * are ready to write.
  384. * @param errorfds If not NULL, then points to a descriptor set that on input
  385. * specifies which descriptors should be checked for error
  386. * conditions, and on output indicates which descriptors
  387. * have error conditions.
  388. * @param timeout If not NULL, then points to timeval structure which
  389. * specifies the time period after which the functions should
  390. * time-out and return. If it is NULL, then the function will
  391. * not time-out.
  392. *
  393. * @return The number of descriptors set in the descriptor sets, or -1
  394. * when an error (specified by errno) have occurred.
  395. */
  396. int esp_vfs_select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *errorfds, struct timeval *timeout);
  397. /**
  398. * @brief Notification from a VFS driver about a read/write/error condition
  399. *
  400. * This function is called when the VFS driver detects a read/write/error
  401. * condition as it was requested by the previous call to start_select.
  402. *
  403. * @param sem semaphore structure which was passed to the driver by the start_select call
  404. */
  405. void esp_vfs_select_triggered(esp_vfs_select_sem_t sem);
  406. /**
  407. * @brief Notification from a VFS driver about a read/write/error condition (ISR version)
  408. *
  409. * This function is called when the VFS driver detects a read/write/error
  410. * condition as it was requested by the previous call to start_select.
  411. *
  412. * @param sem semaphore structure which was passed to the driver by the start_select call
  413. * @param woken is set to pdTRUE if the function wakes up a task with higher priority
  414. */
  415. void esp_vfs_select_triggered_isr(esp_vfs_select_sem_t sem, BaseType_t *woken);
  416. /**
  417. *
  418. * @brief Implements the VFS layer of POSIX pread()
  419. *
  420. * @param fd File descriptor used for read
  421. * @param dst Pointer to the buffer where the output will be written
  422. * @param size Number of bytes to be read
  423. * @param offset Starting offset of the read
  424. *
  425. * @return A positive return value indicates the number of bytes read. -1 is return on failure and errno is
  426. * set accordingly.
  427. */
  428. ssize_t esp_vfs_pread(int fd, void *dst, size_t size, off_t offset);
  429. /**
  430. *
  431. * @brief Implements the VFS layer of POSIX pwrite()
  432. *
  433. * @param fd File descriptor used for write
  434. * @param src Pointer to the buffer from where the output will be read
  435. * @param size Number of bytes to write
  436. * @param offset Starting offset of the write
  437. *
  438. * @return A positive return value indicates the number of bytes written. -1 is return on failure and errno is
  439. * set accordingly.
  440. */
  441. ssize_t esp_vfs_pwrite(int fd, const void *src, size_t size, off_t offset);
  442. #ifdef __cplusplus
  443. } // extern "C"
  444. #endif
  445. #endif //__ESP_VFS_H__