summaryrefslogtreecommitdiffhomepage
path: root/Private/KernelKit/DeviceManager.hpp
blob: 10d236046a2521002cf8ebcfd1dd6b446d09991f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/* -------------------------------------------

    Copyright Mahrouss Logic

------------------------------------------- */

/* -------------------------------------------

 Revision History:

     31/01/24: Add kDeviceCnt (amlel)

 ------------------------------------------- */

#pragma once

/* NewOS device interface manager. */
/* @file KernelKit/DeviceManager.hpp */
/* @brief Device abstraction and I/O buffer. */

#include <NewKit/ErrorOr.hpp>
#include <NewKit/Ref.hpp>

// Last Rev
// Sat Feb 24 CET 2024

namespace NewOS {
template <typename T>
class DeviceInterface;

template <typename T>
class DeviceInterface {
 public:
  explicit DeviceInterface(void (*Out)(T), void (*In)(T))
      : m_Out(Out), m_In(In) {}

  virtual ~DeviceInterface() = default;

 public:
  DeviceInterface &operator=(const DeviceInterface<T> &) = default;
  DeviceInterface(const DeviceInterface<T> &) = default;

 public:
  DeviceInterface<T> &operator<<(T Data) {
    m_Out(Data);
    return *this;
  }

  DeviceInterface<T> &operator>>(T Data) {
    m_In(Data);
    return *this;
  }

  virtual const char *Name() const { return "DeviceInterface"; }

  operator bool() { return m_Out && m_In; }
  bool operator!() { return !m_Out && !m_In; }

 private:
  void (*m_Out)(T Data);
  void (*m_In)(T Data);
};

///
/// @brief Input Output Buffer
/// Used mainly to communicate between hardware.
///
template <typename T>
class IOBuf final {
 public:
  explicit IOBuf(T Dat) : m_Data(Dat) {
    // at least pass something valid when instancating this struct.
    MUST_PASS(Dat);
  }

  IOBuf &operator=(const IOBuf<T> &) = default;
  IOBuf(const IOBuf<T> &) = default;

  ~IOBuf() = default;

 public:
  template <typename R>
  R operator->() const {
    return m_Data;
  }

  template <typename R>
  R &operator[](Size index) const {
    return m_Data[index];
  }

 private:
  T m_Data;
};

///! @brief Device enum types.
enum {
  kDeviceTypeIDE,
  kDeviceTypeEthernet,
  kDeviceTypeWiFi,
  kDeviceTypeRS232,
  kDeviceTypeSCSI,
  kDeviceTypeSHCI,
  kDeviceTypeUSB,
  kDeviceTypeMedia,
  kDeviceTypeCount,
};
}  // namespace NewOS