blob: 98b6e5cc29134993bdc82da91d5112d00f26e750 [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>
15
Patrick Venture8b588562018-11-18 08:44:33 -080016namespace internal
17{
18
19/**
20 * @class Sys
21 * @brief Overridable direct syscall interface
22 */
23class Sys
24{
25 public:
26 virtual ~Sys() = default;
27
28 virtual int open(const char* pathname, int flags) const = 0;
29 virtual int close(int fd) const = 0;
Patrick Venture28abae72018-12-14 09:44:02 -080030 virtual void* mmap(void* addr, std::size_t length, int prot, int flags,
31 int fd, off_t offset) const = 0;
32 virtual int munmap(void* addr, std::size_t length) const = 0;
Patrick Venture8b588562018-11-18 08:44:33 -080033 virtual int getpagesize() const = 0;
Patrick Venture7b91cbc2018-11-28 14:24:41 -080034 virtual int ioctl(int fd, unsigned long request, void* param) const = 0;
Patrick Venture7b78aa22018-12-14 13:56:15 -080035 virtual int poll(struct pollfd* fds, nfds_t nfds, int timeout) const = 0;
Patrick Venture8b588562018-11-18 08:44:33 -080036};
37
38/**
39 * @class SysImpl
40 * @brief syscall concrete implementation
41 * @details Passes through all calls to the normal linux syscalls
42 */
43class SysImpl : public Sys
44{
45 public:
46 int open(const char* pathname, int flags) const override;
47 int close(int fd) const override;
Patrick Venture28abae72018-12-14 09:44:02 -080048 void* mmap(void* addr, std::size_t length, int prot, int flags, int fd,
Patrick Venture8b588562018-11-18 08:44:33 -080049 off_t offset) const override;
Patrick Venture28abae72018-12-14 09:44:02 -080050 int munmap(void* addr, std::size_t length) const override;
Patrick Venture8b588562018-11-18 08:44:33 -080051 int getpagesize() const override;
Patrick Venture7b91cbc2018-11-28 14:24:41 -080052 int ioctl(int fd, unsigned long request, void* param) const override;
Patrick Venture7b78aa22018-12-14 13:56:15 -080053 int poll(struct pollfd* fds, nfds_t nfds, int timeout) const override;
Patrick Venture8b588562018-11-18 08:44:33 -080054};
55
56/** @brief Default instantiation of sys */
57extern SysImpl sys_impl;
58
59} // namespace internal