-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathDeviceMgmt.cpp
164 lines (136 loc) · 3.97 KB
/
DeviceMgmt.cpp
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include "DeviceMgmt.hpp"
#include "Utils.hpp"
#include <phosphor-logging/lg2.hpp>
#include <cstdint>
#include <filesystem>
#include <fstream>
#include <iomanip>
#include <ios>
#include <optional>
#include <sstream>
#include <stdexcept>
#include <string>
#include <system_error>
#include <variant>
std::optional<I2CDeviceParams> getI2CDeviceParams(
const I2CDeviceTypeMap& dtmap, const SensorBaseConfigMap& cfg)
{
auto findType = cfg.find("Type");
auto findBus = cfg.find("Bus");
auto findAddr = cfg.find("Address");
if (findType == cfg.end() || findBus == cfg.end() || findAddr == cfg.end())
{
return std::nullopt;
}
const std::string* type = std::get_if<std::string>(&findType->second);
const uint64_t* bus = std::get_if<uint64_t>(&findBus->second);
const uint64_t* addr = std::get_if<uint64_t>(&findAddr->second);
if (type == nullptr || bus == nullptr || addr == nullptr)
{
return std::nullopt;
}
auto findDevType = dtmap.find(type->c_str());
if (findDevType == dtmap.end())
{
return std::nullopt;
}
return I2CDeviceParams(findDevType->second, *bus, *addr);
}
static std::filesystem::path i2cBusPath(uint64_t bus)
{
return {"/sys/bus/i2c/devices/i2c-" + std::to_string(bus)};
}
static std::string deviceDirName(uint64_t bus, uint64_t address)
{
std::ostringstream name;
name << bus << "-" << std::hex << std::setw(4) << std::setfill('0')
<< address;
return name.str();
}
bool I2CDeviceParams::devicePresent() const
{
std::filesystem::path path = i2cBusPath(bus) / deviceDirName(bus, address);
if (type->createsHWMon)
{
path /= "hwmon";
}
// Ignore errors; anything but a clean 'true' is fine as 'false'
std::error_code ec;
return std::filesystem::exists(path, ec);
}
bool I2CDeviceParams::deviceStatic() const
{
if (!devicePresent())
{
return false;
}
std::filesystem::path ofNode =
i2cBusPath(bus) / deviceDirName(bus, address) / "of_node";
// Ignore errors -- if of_node is present the device is a static DT node;
// otherwise we can assume we created it from userspace.
std::error_code ec;
return std::filesystem::exists(ofNode, ec);
}
I2CDevice::I2CDevice(I2CDeviceParams params) : params(params)
{
if (create() != 0)
{
throw std::runtime_error("failed to instantiate i2c device");
}
}
I2CDevice::~I2CDevice()
{
destroy();
}
int I2CDevice::create() const
{
// If it's already instantiated, there's nothing we need to do.
if (params.devicePresent())
{
return 0;
}
// Try to create it: 'echo $devtype $addr > .../i2c-$bus/new_device'
std::filesystem::path ctorPath = i2cBusPath(params.bus) / "new_device";
std::ofstream ctor(ctorPath);
if (!ctor.good())
{
lg2::error("Failed to open '{PATH}'", "PATH", ctorPath);
return -1;
}
ctor << params.type->name << " " << params.address << "\n";
ctor.flush();
if (!ctor.good())
{
lg2::error("Failed to write to '{PATH}'", "PATH", ctorPath);
return -1;
}
// Check if that created the requisite sysfs directory
if (!params.devicePresent())
{
destroy();
return -1;
}
return 0;
}
int I2CDevice::destroy() const
{
// No params.devicePresent() check on this like in create(), since it
// might be used to clean up after a device instantiation that was only
// partially successful (i.e. when params.devicePresent() would return
// false but there's still a dummy i2c client device to remove)
std::filesystem::path dtorPath = i2cBusPath(params.bus) / "delete_device";
std::ofstream dtor(dtorPath);
if (!dtor.good())
{
lg2::error("Failed to open '{PATH}'", "PATH", dtorPath);
return -1;
}
dtor << params.address << "\n";
dtor.flush();
if (!dtor.good())
{
lg2::error("Failed to write to '{PATH}'", "PATH", dtorPath);
return -1;
}
return 0;
}