esp_vfs.h 18 KB

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