Patrick Venture | 8b58856 | 2018-11-18 08:44:33 -0800 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | /* NOTE: IIRC, wak@ is working on exposing some of this in stdplus, so we can |
| 4 | * transition when that's ready. |
| 5 | * |
| 6 | * Copied some from gpioplus to enable unit-testing of lpc nuvoton and later |
| 7 | * other pieces. |
| 8 | */ |
| 9 | |
Patrick Venture | 7b78aa2 | 2018-12-14 13:56:15 -0800 | [diff] [blame] | 10 | #include <poll.h> |
Patrick Venture | 8b58856 | 2018-11-18 08:44:33 -0800 | [diff] [blame] | 11 | #include <sys/mman.h> |
| 12 | |
| 13 | #include <cinttypes> |
| 14 | #include <cstddef> |
Patrick Venture | cf9b219 | 2019-06-27 12:09:52 -0700 | [diff] [blame] | 15 | #include <cstdint> |
Patrick Venture | 8b58856 | 2018-11-18 08:44:33 -0800 | [diff] [blame] | 16 | |
Patrick Venture | 8b58856 | 2018-11-18 08:44:33 -0800 | [diff] [blame] | 17 | namespace internal |
| 18 | { |
| 19 | |
| 20 | /** |
| 21 | * @class Sys |
| 22 | * @brief Overridable direct syscall interface |
| 23 | */ |
| 24 | class Sys |
| 25 | { |
| 26 | public: |
| 27 | virtual ~Sys() = default; |
| 28 | |
| 29 | virtual int open(const char* pathname, int flags) const = 0; |
Patrick Venture | cec0490 | 2019-01-15 13:04:44 -0800 | [diff] [blame] | 30 | virtual int read(int fd, void* buf, std::size_t count) const = 0; |
Patrick Venture | 8b58856 | 2018-11-18 08:44:33 -0800 | [diff] [blame] | 31 | virtual int close(int fd) const = 0; |
Patrick Venture | 28abae7 | 2018-12-14 09:44:02 -0800 | [diff] [blame] | 32 | virtual void* mmap(void* addr, std::size_t length, int prot, int flags, |
| 33 | int fd, off_t offset) const = 0; |
| 34 | virtual int munmap(void* addr, std::size_t length) const = 0; |
Patrick Venture | 8b58856 | 2018-11-18 08:44:33 -0800 | [diff] [blame] | 35 | virtual int getpagesize() const = 0; |
Patrick Venture | 7b91cbc | 2018-11-28 14:24:41 -0800 | [diff] [blame] | 36 | virtual int ioctl(int fd, unsigned long request, void* param) const = 0; |
Patrick Venture | 7b78aa2 | 2018-12-14 13:56:15 -0800 | [diff] [blame] | 37 | virtual int poll(struct pollfd* fds, nfds_t nfds, int timeout) const = 0; |
Patrick Venture | cf9b219 | 2019-06-27 12:09:52 -0700 | [diff] [blame] | 38 | virtual std::int64_t getSize(const char* pathname) const = 0; |
Patrick Venture | 8b58856 | 2018-11-18 08:44:33 -0800 | [diff] [blame] | 39 | }; |
| 40 | |
| 41 | /** |
| 42 | * @class SysImpl |
| 43 | * @brief syscall concrete implementation |
| 44 | * @details Passes through all calls to the normal linux syscalls |
| 45 | */ |
| 46 | class SysImpl : public Sys |
| 47 | { |
| 48 | public: |
| 49 | int open(const char* pathname, int flags) const override; |
Patrick Venture | cec0490 | 2019-01-15 13:04:44 -0800 | [diff] [blame] | 50 | int read(int fd, void* buf, std::size_t count) const override; |
Patrick Venture | 8b58856 | 2018-11-18 08:44:33 -0800 | [diff] [blame] | 51 | int close(int fd) const override; |
Patrick Venture | 28abae7 | 2018-12-14 09:44:02 -0800 | [diff] [blame] | 52 | void* mmap(void* addr, std::size_t length, int prot, int flags, int fd, |
Patrick Venture | 8b58856 | 2018-11-18 08:44:33 -0800 | [diff] [blame] | 53 | off_t offset) const override; |
Patrick Venture | 28abae7 | 2018-12-14 09:44:02 -0800 | [diff] [blame] | 54 | int munmap(void* addr, std::size_t length) const override; |
Patrick Venture | 8b58856 | 2018-11-18 08:44:33 -0800 | [diff] [blame] | 55 | int getpagesize() const override; |
Patrick Venture | 7b91cbc | 2018-11-28 14:24:41 -0800 | [diff] [blame] | 56 | int ioctl(int fd, unsigned long request, void* param) const override; |
Patrick Venture | 7b78aa2 | 2018-12-14 13:56:15 -0800 | [diff] [blame] | 57 | int poll(struct pollfd* fds, nfds_t nfds, int timeout) const override; |
Patrick Venture | cf9b219 | 2019-06-27 12:09:52 -0700 | [diff] [blame] | 58 | /* returns 0 on failure, or if the file is zero bytes. */ |
| 59 | std::int64_t getSize(const char* pathname) const override; |
Patrick Venture | 8b58856 | 2018-11-18 08:44:33 -0800 | [diff] [blame] | 60 | }; |
| 61 | |
| 62 | /** @brief Default instantiation of sys */ |
| 63 | extern SysImpl sys_impl; |
| 64 | |
| 65 | } // namespace internal |