1#ifndef MSTL_CORE_SYSTEM_DEVICE_DEVICE_HPP__
2#define MSTL_CORE_SYSTEM_DEVICE_DEVICE_HPP__
3#include "MSTL/core/file/file.hpp"
7#include "device_constants.hpp"
8#ifdef MSTL_PLATFORM_WINDOWS__
10#include <MSTL/core/config/undef_cmacro.hpp>
14class MSTL_API device {
16 using async_result = file::async_result;
17 using device_handle = file::file_handle;
21 DEVICE_TYPE device_type_{DEVICE_TYPE::UNKNOWN};
23 bool is_blocking_{
true};
25 mutable mutex io_mutex_;
27 FILE_ACCESS dmode_to_faccess(DEVICE_OPEN_MODE mode)
const;
28 FILE_CREATION dflags_to_fcreation(DEVICE_OPEN_FLAG flags)
const;
29 FILE_ATTRI dflags_to_fattri(DEVICE_OPEN_FLAG flags)
const;
31 static DEVICE_TYPE try_device_type(
const path& pth);
33#ifdef MSTL_PLATFORM_WINDOWS__
34 static DEVICE_TYPE try_device_type_from_guid(
const GUID& guid);
35 static string get_device_property(::HDEVINFO dev_info_set,
36 ::PSP_DEVINFO_DATA dev_info_data, ::DWORD property);
38 static DEVICE_TYPE guess_device_type_from_path(
const path& pth);
39 static string read_sysfs_attribute(
const string& device_path,
40 const string& attribute);
47 explicit device(path device_path,
48 DEVICE_OPEN_MODE mode = DEVICE_OPEN_MODE::READ_WRITE,
49 DEVICE_OPEN_FLAG flags = DEVICE_OPEN_FLAG::NONE);
51 device(
const device&) =
delete;
52 device& operator =(
const device&) =
delete;
54 device(device&& other)
noexcept;
55 device& operator =(device&& other)
noexcept;
57 void open(
const path& device_path,
58 DEVICE_OPEN_MODE mode = DEVICE_OPEN_MODE::READ_WRITE,
59 DEVICE_OPEN_FLAG flags = DEVICE_OPEN_FLAG::NONE);
61 void reopen(DEVICE_OPEN_MODE new_mode,
62 DEVICE_OPEN_FLAG new_flags = DEVICE_OPEN_FLAG::NONE);
64 void close() noexcept;
65 bool is_open() const noexcept;
70 async_result async_read(
string& buffer,
size_t size,
int64_t offset = -1);
71 async_result async_write(const
string&
data,
size_t size,
int64_t offset = -1);
72 bool wait_async(async_result& result,
uint32_t timeout_ms = 0xFFFFFFFF);
73 void cancel_async(async_result& result);
75 void ioctl(const ioctl_command& cmd);
84 milliseconds get_timeout() const noexcept {
return timeout_; }
85 void set_blocking(
bool blocking);
86 bool is_blocking() const noexcept {
return is_blocking_; }
88 _MSTL device_info device_info()
const;
89 const path& device_path() const noexcept {
return file_.path(); }
90 DEVICE_TYPE device_type() const noexcept {
return device_type_; }
92 void* map_memory(
size_t offset,
size_t size);
93 void unmap_memory() noexcept;
94 bool is_mapped() const noexcept {
return file_.is_mapped(); }
96 bool supports_direct_io() const noexcept;
98 device_handle native_handle() const noexcept {
return file_.native_handle(); }
99 file& file_handle() noexcept {
return file_; }
100 const file& file_handle() const noexcept {
return file_; }
102 static vector<_MSTL device_info> enumerate(
const string& filter =
"");
104 static vector<_MSTL device_info> find_by_vid_pid(
uint16_t vid,
uint16_t pid);
105 static optional<_MSTL device_info> find_by_path(path pth);
107 static bool exists(
const string& device_path) {
return path::exists(device_path); }
108 static bool is_device(
const string& path);
unsigned int uint32_t
32位无符号整数类型
long long int64_t
64位有符号整数类型
unsigned short uint16_t
16位无符号整数类型
duration< int64_t, milli > milliseconds
毫秒持续时间
#define _MSTL
全局命名空间MSTL前缀
#define MSTL_END_NAMESPACE__
结束全局命名空间MSTL
#define MSTL_BEGIN_NAMESPACE__
开始全局命名空间MSTL
MSTL_NODISCARD MSTL_ALWAYS_INLINE constexpr decltype(auto) size(const Container &cont) noexcept(noexcept(cont.size()))
获取容器的大小
MSTL_NODISCARD MSTL_ALWAYS_INLINE constexpr decltype(auto) data(Container &cont) noexcept(noexcept(cont.data()))
获取容器的底层数据指针