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 | |
Benjamin Fair | f7ccadb | 2019-10-11 17:55:27 -0700 | [diff] [blame] | 10 | #include <netdb.h> |
Patrick Venture | 7b78aa2 | 2018-12-14 13:56:15 -0800 | [diff] [blame] | 11 | #include <poll.h> |
Patrick Venture | 8b58856 | 2018-11-18 08:44:33 -0800 | [diff] [blame] | 12 | #include <sys/mman.h> |
Benjamin Fair | f7ccadb | 2019-10-11 17:55:27 -0700 | [diff] [blame] | 13 | #include <sys/socket.h> |
| 14 | #include <sys/types.h> |
Patrick Venture | 8b58856 | 2018-11-18 08:44:33 -0800 | [diff] [blame] | 15 | |
| 16 | #include <cinttypes> |
| 17 | #include <cstddef> |
Patrick Venture | cf9b219 | 2019-06-27 12:09:52 -0700 | [diff] [blame] | 18 | #include <cstdint> |
Patrick Venture | 8b58856 | 2018-11-18 08:44:33 -0800 | [diff] [blame] | 19 | |
Patrick Venture | 8b58856 | 2018-11-18 08:44:33 -0800 | [diff] [blame] | 20 | namespace internal |
| 21 | { |
| 22 | |
| 23 | /** |
| 24 | * @class Sys |
| 25 | * @brief Overridable direct syscall interface |
| 26 | */ |
| 27 | class Sys |
| 28 | { |
| 29 | public: |
| 30 | virtual ~Sys() = default; |
| 31 | |
| 32 | virtual int open(const char* pathname, int flags) const = 0; |
Patrick Venture | cec0490 | 2019-01-15 13:04:44 -0800 | [diff] [blame] | 33 | virtual int read(int fd, void* buf, std::size_t count) const = 0; |
Brandon Kim | 493b3af | 2019-11-05 16:28:40 -0800 | [diff] [blame] | 34 | virtual int pread(int fd, void* buf, std::size_t count, |
| 35 | off_t offset) const = 0; |
| 36 | virtual int pwrite(int fd, const void* buf, std::size_t count, |
| 37 | off_t offset) const = 0; |
Patrick Venture | 8b58856 | 2018-11-18 08:44:33 -0800 | [diff] [blame] | 38 | virtual int close(int fd) const = 0; |
Patrick Venture | 28abae7 | 2018-12-14 09:44:02 -0800 | [diff] [blame] | 39 | virtual void* mmap(void* addr, std::size_t length, int prot, int flags, |
| 40 | int fd, off_t offset) const = 0; |
| 41 | virtual int munmap(void* addr, std::size_t length) const = 0; |
Patrick Venture | 8b58856 | 2018-11-18 08:44:33 -0800 | [diff] [blame] | 42 | virtual int getpagesize() const = 0; |
Patrick Venture | 7b91cbc | 2018-11-28 14:24:41 -0800 | [diff] [blame] | 43 | virtual int ioctl(int fd, unsigned long request, void* param) const = 0; |
Patrick Venture | 7b78aa2 | 2018-12-14 13:56:15 -0800 | [diff] [blame] | 44 | virtual int poll(struct pollfd* fds, nfds_t nfds, int timeout) const = 0; |
Benjamin Fair | f7ccadb | 2019-10-11 17:55:27 -0700 | [diff] [blame] | 45 | virtual int socket(int domain, int type, int protocol) const = 0; |
| 46 | virtual int connect(int sockfd, const struct sockaddr* addr, |
| 47 | socklen_t addrlen) const = 0; |
| 48 | virtual ssize_t sendfile(int out_fd, int in_fd, off_t* offset, |
| 49 | size_t count) const = 0; |
| 50 | virtual int getaddrinfo(const char* node, const char* service, |
| 51 | const struct addrinfo* hints, |
| 52 | struct addrinfo** res) const = 0; |
| 53 | virtual void freeaddrinfo(struct addrinfo* res) const = 0; |
Patrick Venture | cf9b219 | 2019-06-27 12:09:52 -0700 | [diff] [blame] | 54 | virtual std::int64_t getSize(const char* pathname) const = 0; |
Patrick Venture | 8b58856 | 2018-11-18 08:44:33 -0800 | [diff] [blame] | 55 | }; |
| 56 | |
| 57 | /** |
| 58 | * @class SysImpl |
| 59 | * @brief syscall concrete implementation |
| 60 | * @details Passes through all calls to the normal linux syscalls |
| 61 | */ |
| 62 | class SysImpl : public Sys |
| 63 | { |
| 64 | public: |
| 65 | int open(const char* pathname, int flags) const override; |
Patrick Venture | cec0490 | 2019-01-15 13:04:44 -0800 | [diff] [blame] | 66 | int read(int fd, void* buf, std::size_t count) const override; |
Brandon Kim | 493b3af | 2019-11-05 16:28:40 -0800 | [diff] [blame] | 67 | int pread(int fd, void* buf, std::size_t count, |
| 68 | off_t offset) const override; |
| 69 | int pwrite(int fd, const void* buf, std::size_t count, |
| 70 | off_t offset) const override; |
Patrick Venture | 8b58856 | 2018-11-18 08:44:33 -0800 | [diff] [blame] | 71 | int close(int fd) const override; |
Patrick Venture | 28abae7 | 2018-12-14 09:44:02 -0800 | [diff] [blame] | 72 | 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] | 73 | off_t offset) const override; |
Patrick Venture | 28abae7 | 2018-12-14 09:44:02 -0800 | [diff] [blame] | 74 | int munmap(void* addr, std::size_t length) const override; |
Patrick Venture | 8b58856 | 2018-11-18 08:44:33 -0800 | [diff] [blame] | 75 | int getpagesize() const override; |
Patrick Venture | 7b91cbc | 2018-11-28 14:24:41 -0800 | [diff] [blame] | 76 | int ioctl(int fd, unsigned long request, void* param) const override; |
Patrick Venture | 7b78aa2 | 2018-12-14 13:56:15 -0800 | [diff] [blame] | 77 | int poll(struct pollfd* fds, nfds_t nfds, int timeout) const override; |
Benjamin Fair | f7ccadb | 2019-10-11 17:55:27 -0700 | [diff] [blame] | 78 | int socket(int domain, int type, int protocol) const override; |
| 79 | int connect(int sockfd, const struct sockaddr* addr, |
| 80 | socklen_t addrlen) const override; |
| 81 | ssize_t sendfile(int out_fd, int in_fd, off_t* offset, |
| 82 | size_t count) const override; |
| 83 | int getaddrinfo(const char* node, const char* service, |
| 84 | const struct addrinfo* hints, |
| 85 | struct addrinfo** res) const override; |
| 86 | void freeaddrinfo(struct addrinfo* res) const override; |
Patrick Venture | cf9b219 | 2019-06-27 12:09:52 -0700 | [diff] [blame] | 87 | /* returns 0 on failure, or if the file is zero bytes. */ |
| 88 | std::int64_t getSize(const char* pathname) const override; |
Patrick Venture | 8b58856 | 2018-11-18 08:44:33 -0800 | [diff] [blame] | 89 | }; |
| 90 | |
| 91 | /** @brief Default instantiation of sys */ |
| 92 | extern SysImpl sys_impl; |
| 93 | |
| 94 | } // namespace internal |