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 
10 #include <poll.h>
11 #include <sys/mman.h>
12 
13 #include <cinttypes>
14 #include <cstddef>
15 #include <cstdint>
16 
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;
30     virtual int read(int fd, void* buf, std::size_t count) const = 0;
31     virtual int pread(int fd, void* buf, std::size_t count,
32                       off_t offset) const = 0;
33     virtual int pwrite(int fd, const void* buf, std::size_t count,
34                        off_t offset) const = 0;
35     virtual int close(int fd) const = 0;
36     virtual void* mmap(void* addr, std::size_t length, int prot, int flags,
37                        int fd, off_t offset) const = 0;
38     virtual int munmap(void* addr, std::size_t length) const = 0;
39     virtual int getpagesize() const = 0;
40     virtual int ioctl(int fd, unsigned long request, void* param) const = 0;
41     virtual int poll(struct pollfd* fds, nfds_t nfds, int timeout) const = 0;
42     virtual std::int64_t getSize(const char* pathname) const = 0;
43 };
44 
45 /**
46  * @class SysImpl
47  * @brief syscall concrete implementation
48  * @details Passes through all calls to the normal linux syscalls
49  */
50 class SysImpl : public Sys
51 {
52   public:
53     int open(const char* pathname, int flags) const override;
54     int read(int fd, void* buf, std::size_t count) const override;
55     int pread(int fd, void* buf, std::size_t count,
56               off_t offset) const override;
57     int pwrite(int fd, const void* buf, std::size_t count,
58                off_t offset) const override;
59     int close(int fd) const override;
60     void* mmap(void* addr, std::size_t length, int prot, int flags, int fd,
61                off_t offset) const override;
62     int munmap(void* addr, std::size_t length) const override;
63     int getpagesize() const override;
64     int ioctl(int fd, unsigned long request, void* param) const override;
65     int poll(struct pollfd* fds, nfds_t nfds, int timeout) const override;
66     /* returns 0 on failure, or if the file is zero bytes. */
67     std::int64_t getSize(const char* pathname) const override;
68 };
69 
70 /** @brief Default instantiation of sys */
71 extern SysImpl sys_impl;
72 
73 } // namespace internal
74