aboutsummaryrefslogtreecommitdiff
path: root/kernel/include/kernel/filesystem/device_inode.hpp
blob: 2f79fca4a7e6a4639a36dcb5cbabfa9164b08489 (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
#ifndef TEACH_OS_KERNEL_FILESYSTEM_DEVICE_INODE_HPP
#define TEACH_OS_KERNEL_FILESYSTEM_DEVICE_INODE_HPP

#include <kernel/filesystem/inode.hpp>

#include <kapi/devices/device.hpp>

#include <kstd/memory>

#include <cstddef>

namespace kernel::filesystem
{
  /**
  @brief Inode implementation for device inodes in the filesystem. This inode represents a device file that provides
  access to a device registered in the system. The device inode allows reading from and writing to the associated
  device.
  */
  struct device_inode : inode
  {
    /**
    @brief Create a device inode with the given @p device.
    @param device The device to associate with the inode.
    */
    explicit device_inode(kstd::shared_ptr<kapi::devices::device> const & device);

    /**
    @brief Read data from the device inode (and in the background from the associated device) into a @p buffer, starting
    at @p offset and reading @p size bytes.
    @param buffer The buffer to read data into.
    @param offset The offset to read from.
    @param size The number of bytes to read.
    @return The number of bytes read.
    */
    auto read(void * buffer, size_t offset, size_t size) const -> size_t override;

    /**
    @brief Write data to the device inode (and in the background from the associated device) from a @p buffer, starting
    at @p offset and writing @p size bytes.
    @param buffer The buffer containing data to write.
    @param offset The offset to write to.
    @param size The number of bytes to write.
    @return The number of bytes written.
    */
    auto write(void const * buffer, size_t offset, size_t size) -> size_t override;

    /**
    @brief Get the associated device.
    @return A reference to the associated device.
    */
    [[nodiscard]] auto device() const -> kstd::shared_ptr<kapi::devices::device> const &;

  private:
    kstd::shared_ptr<kapi::devices::device> m_device;
  };
}  // namespace kernel::filesystem

#endif