18b58856fSPatrick Venture #pragma once
28b58856fSPatrick Venture
38b58856fSPatrick Venture /* NOTE: IIRC, wak@ is working on exposing some of this in stdplus, so we can
48b58856fSPatrick Venture * transition when that's ready.
58b58856fSPatrick Venture *
68b58856fSPatrick Venture * Copied some from gpioplus to enable unit-testing of lpc nuvoton and later
78b58856fSPatrick Venture * other pieces.
88b58856fSPatrick Venture */
98b58856fSPatrick Venture
10f7ccadb0SBenjamin Fair #include <netdb.h>
117b78aa2cSPatrick Venture #include <poll.h>
128b58856fSPatrick Venture #include <sys/mman.h>
13f7ccadb0SBenjamin Fair #include <sys/socket.h>
14f7ccadb0SBenjamin Fair #include <sys/types.h>
158b58856fSPatrick Venture
168b58856fSPatrick Venture #include <cinttypes>
178b58856fSPatrick Venture #include <cstddef>
18cf9b2195SPatrick Venture #include <cstdint>
19c04c2c5cSBenjamin Fair #include <system_error>
208b58856fSPatrick Venture
218b58856fSPatrick Venture namespace internal
228b58856fSPatrick Venture {
238b58856fSPatrick Venture
248b58856fSPatrick Venture /**
258b58856fSPatrick Venture * @class Sys
268b58856fSPatrick Venture * @brief Overridable direct syscall interface
278b58856fSPatrick Venture */
288b58856fSPatrick Venture class Sys
298b58856fSPatrick Venture {
308b58856fSPatrick Venture public:
318b58856fSPatrick Venture virtual ~Sys() = default;
328b58856fSPatrick Venture
338b58856fSPatrick Venture virtual int open(const char* pathname, int flags) const = 0;
34cec04901SPatrick Venture virtual int read(int fd, void* buf, std::size_t count) const = 0;
35493b3af0SBrandon Kim virtual int pread(int fd, void* buf, std::size_t count,
36493b3af0SBrandon Kim off_t offset) const = 0;
37493b3af0SBrandon Kim virtual int pwrite(int fd, const void* buf, std::size_t count,
38493b3af0SBrandon Kim off_t offset) const = 0;
398b58856fSPatrick Venture virtual int close(int fd) const = 0;
4028abae7cSPatrick Venture virtual void* mmap(void* addr, std::size_t length, int prot, int flags,
4128abae7cSPatrick Venture int fd, off_t offset) const = 0;
4228abae7cSPatrick Venture virtual int munmap(void* addr, std::size_t length) const = 0;
438b58856fSPatrick Venture virtual int getpagesize() const = 0;
447b91cbc1SPatrick Venture virtual int ioctl(int fd, unsigned long request, void* param) const = 0;
457b78aa2cSPatrick Venture virtual int poll(struct pollfd* fds, nfds_t nfds, int timeout) const = 0;
46f7ccadb0SBenjamin Fair virtual int socket(int domain, int type, int protocol) const = 0;
47f7ccadb0SBenjamin Fair virtual int connect(int sockfd, const struct sockaddr* addr,
48f7ccadb0SBenjamin Fair socklen_t addrlen) const = 0;
49*9bb21e3eSWilliam A. Kennington III virtual ssize_t send(int sockfd, const void* buf, size_t len,
50*9bb21e3eSWilliam A. Kennington III int flags) const = 0;
51f7ccadb0SBenjamin Fair virtual ssize_t sendfile(int out_fd, int in_fd, off_t* offset,
52f7ccadb0SBenjamin Fair size_t count) const = 0;
53f7ccadb0SBenjamin Fair virtual int getaddrinfo(const char* node, const char* service,
54f7ccadb0SBenjamin Fair const struct addrinfo* hints,
55f7ccadb0SBenjamin Fair struct addrinfo** res) const = 0;
56f7ccadb0SBenjamin Fair virtual void freeaddrinfo(struct addrinfo* res) const = 0;
57cf9b2195SPatrick Venture virtual std::int64_t getSize(const char* pathname) const = 0;
588b58856fSPatrick Venture };
598b58856fSPatrick Venture
608b58856fSPatrick Venture /**
618b58856fSPatrick Venture * @class SysImpl
628b58856fSPatrick Venture * @brief syscall concrete implementation
638b58856fSPatrick Venture * @details Passes through all calls to the normal linux syscalls
648b58856fSPatrick Venture */
658b58856fSPatrick Venture class SysImpl : public Sys
668b58856fSPatrick Venture {
678b58856fSPatrick Venture public:
688b58856fSPatrick Venture int open(const char* pathname, int flags) const override;
69cec04901SPatrick Venture int read(int fd, void* buf, std::size_t count) const override;
70493b3af0SBrandon Kim int pread(int fd, void* buf, std::size_t count,
71493b3af0SBrandon Kim off_t offset) const override;
72493b3af0SBrandon Kim int pwrite(int fd, const void* buf, std::size_t count,
73493b3af0SBrandon Kim off_t offset) const override;
748b58856fSPatrick Venture int close(int fd) const override;
7528abae7cSPatrick Venture void* mmap(void* addr, std::size_t length, int prot, int flags, int fd,
768b58856fSPatrick Venture off_t offset) const override;
7728abae7cSPatrick Venture int munmap(void* addr, std::size_t length) const override;
788b58856fSPatrick Venture int getpagesize() const override;
797b91cbc1SPatrick Venture int ioctl(int fd, unsigned long request, void* param) const override;
807b78aa2cSPatrick Venture int poll(struct pollfd* fds, nfds_t nfds, int timeout) const override;
81f7ccadb0SBenjamin Fair int socket(int domain, int type, int protocol) const override;
82f7ccadb0SBenjamin Fair int connect(int sockfd, const struct sockaddr* addr,
83f7ccadb0SBenjamin Fair socklen_t addrlen) const override;
84*9bb21e3eSWilliam A. Kennington III ssize_t send(int sockfd, const void* buf, size_t len,
85*9bb21e3eSWilliam A. Kennington III int flags) const override;
86f7ccadb0SBenjamin Fair ssize_t sendfile(int out_fd, int in_fd, off_t* offset,
87f7ccadb0SBenjamin Fair size_t count) const override;
88f7ccadb0SBenjamin Fair int getaddrinfo(const char* node, const char* service,
89f7ccadb0SBenjamin Fair const struct addrinfo* hints,
90f7ccadb0SBenjamin Fair struct addrinfo** res) const override;
91f7ccadb0SBenjamin Fair void freeaddrinfo(struct addrinfo* res) const override;
92cf9b2195SPatrick Venture /* returns 0 on failure, or if the file is zero bytes. */
93cf9b2195SPatrick Venture std::int64_t getSize(const char* pathname) const override;
948b58856fSPatrick Venture };
958b58856fSPatrick Venture
errnoException(const std::string & message)96c04c2c5cSBenjamin Fair inline std::system_error errnoException(const std::string& message)
97c04c2c5cSBenjamin Fair {
98c04c2c5cSBenjamin Fair return std::system_error(errno, std::generic_category(), message);
99c04c2c5cSBenjamin Fair }
100c04c2c5cSBenjamin Fair
1018b58856fSPatrick Venture /** @brief Default instantiation of sys */
1028b58856fSPatrick Venture extern SysImpl sys_impl;
1038b58856fSPatrick Venture
1048b58856fSPatrick Venture } // namespace internal
105