blob: 41fb46bfd8dacd5eafae8fba954d31542c2a902e [file] [log] [blame]
Patrick Venture8b588562018-11-18 08:44:33 -08001#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 Venture7b78aa22018-12-14 13:56:15 -080010#include <poll.h>
Patrick Venture8b588562018-11-18 08:44:33 -080011#include <sys/mman.h>
12
13#include <cinttypes>
14#include <cstddef>
Patrick Venturecf9b2192019-06-27 12:09:52 -070015#include <cstdint>
Patrick Venture8b588562018-11-18 08:44:33 -080016
Patrick Venture8b588562018-11-18 08:44:33 -080017namespace internal
18{
19
20/**
21 * @class Sys
22 * @brief Overridable direct syscall interface
23 */
24class Sys
25{
26 public:
27 virtual ~Sys() = default;
28
29 virtual int open(const char* pathname, int flags) const = 0;
Patrick Venturecec04902019-01-15 13:04:44 -080030 virtual int read(int fd, void* buf, std::size_t count) const = 0;
Brandon Kim493b3af2019-11-05 16:28:40 -080031 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;
Patrick Venture8b588562018-11-18 08:44:33 -080035 virtual int close(int fd) const = 0;
Patrick Venture28abae72018-12-14 09:44:02 -080036 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;
Patrick Venture8b588562018-11-18 08:44:33 -080039 virtual int getpagesize() const = 0;
Patrick Venture7b91cbc2018-11-28 14:24:41 -080040 virtual int ioctl(int fd, unsigned long request, void* param) const = 0;
Patrick Venture7b78aa22018-12-14 13:56:15 -080041 virtual int poll(struct pollfd* fds, nfds_t nfds, int timeout) const = 0;
Patrick Venturecf9b2192019-06-27 12:09:52 -070042 virtual std::int64_t getSize(const char* pathname) const = 0;
Patrick Venture8b588562018-11-18 08:44:33 -080043};
44
45/**
46 * @class SysImpl
47 * @brief syscall concrete implementation
48 * @details Passes through all calls to the normal linux syscalls
49 */
50class SysImpl : public Sys
51{
52 public:
53 int open(const char* pathname, int flags) const override;
Patrick Venturecec04902019-01-15 13:04:44 -080054 int read(int fd, void* buf, std::size_t count) const override;
Brandon Kim493b3af2019-11-05 16:28:40 -080055 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;
Patrick Venture8b588562018-11-18 08:44:33 -080059 int close(int fd) const override;
Patrick Venture28abae72018-12-14 09:44:02 -080060 void* mmap(void* addr, std::size_t length, int prot, int flags, int fd,
Patrick Venture8b588562018-11-18 08:44:33 -080061 off_t offset) const override;
Patrick Venture28abae72018-12-14 09:44:02 -080062 int munmap(void* addr, std::size_t length) const override;
Patrick Venture8b588562018-11-18 08:44:33 -080063 int getpagesize() const override;
Patrick Venture7b91cbc2018-11-28 14:24:41 -080064 int ioctl(int fd, unsigned long request, void* param) const override;
Patrick Venture7b78aa22018-12-14 13:56:15 -080065 int poll(struct pollfd* fds, nfds_t nfds, int timeout) const override;
Patrick Venturecf9b2192019-06-27 12:09:52 -070066 /* returns 0 on failure, or if the file is zero bytes. */
67 std::int64_t getSize(const char* pathname) const override;
Patrick Venture8b588562018-11-18 08:44:33 -080068};
69
70/** @brief Default instantiation of sys */
71extern SysImpl sys_impl;
72
73} // namespace internal