blob: fdf757740a65cdc73e580c857cc56c543655bbaf [file] [log] [blame]
Zev Weissdabd48d2022-08-03 15:43:17 -07001#include <DeviceMgmt.hpp>
2
3#include <filesystem>
4#include <fstream>
5
6namespace fs = std::filesystem;
7
Zev Weiss3ee959a2022-09-21 17:16:28 -07008std::optional<I2CDeviceParams>
9 getI2CDeviceParams(const I2CDeviceTypeMap& dtmap,
10 const SensorBaseConfigMap& cfg)
Zev Weissdabd48d2022-08-03 15:43:17 -070011{
12 auto findType = cfg.find("Type");
13 auto findBus = cfg.find("Bus");
14 auto findAddr = cfg.find("Address");
15
16 if (findType == cfg.end() || findBus == cfg.end() || findAddr == cfg.end())
17 {
18 return std::nullopt;
19 }
20
21 const std::string* type = std::get_if<std::string>(&findType->second);
22 const uint64_t* bus = std::get_if<uint64_t>(&findBus->second);
23 const uint64_t* addr = std::get_if<uint64_t>(&findAddr->second);
24
25 if (type == nullptr || bus == nullptr || addr == nullptr)
26 {
27 return std::nullopt;
28 }
29
30 auto findDevType = dtmap.find(type->c_str());
31 if (findDevType == dtmap.end())
32 {
33 return std::nullopt;
34 }
35
Zev Weiss3ee959a2022-09-21 17:16:28 -070036 return I2CDeviceParams(findDevType->second, *bus, *addr);
Zev Weissdabd48d2022-08-03 15:43:17 -070037}
38
39static fs::path i2cBusPath(uint64_t bus)
40{
41 return {"/sys/bus/i2c/devices/i2c-" + std::to_string(bus)};
42}
43
44static std::string deviceDirName(uint64_t bus, uint64_t address)
45{
46 std::ostringstream name;
47 name << bus << "-" << std::hex << std::setw(4) << std::setfill('0')
48 << address;
49 return name.str();
50}
51
Zev Weiss3ee959a2022-09-21 17:16:28 -070052bool I2CDeviceParams::devicePresent(void) const
Zev Weissdabd48d2022-08-03 15:43:17 -070053{
54 fs::path path = i2cBusPath(bus) / deviceDirName(bus, address);
55
56 if (type->createsHWMon)
57 {
58 path /= "hwmon";
59 }
60
61 // Ignore errors; anything but a clean 'true' is fine as 'false'
62 std::error_code ec;
63 return fs::exists(path, ec);
64}
65
Zev Weiss3ee959a2022-09-21 17:16:28 -070066I2CDevice::I2CDevice(I2CDeviceParams params) : params(params)
67{
68 if (create() != 0)
69 {
70 throw std::runtime_error("failed to instantiate i2c device");
71 }
72}
73
74I2CDevice::~I2CDevice()
75{
76 destroy();
77}
78
Zev Weissdabd48d2022-08-03 15:43:17 -070079int I2CDevice::create(void) const
80{
81 // If it's already instantiated, there's nothing we need to do.
Zev Weiss3ee959a2022-09-21 17:16:28 -070082 if (params.devicePresent())
Zev Weissdabd48d2022-08-03 15:43:17 -070083 {
84 return 0;
85 }
86
87 // Try to create it: 'echo $devtype $addr > .../i2c-$bus/new_device'
Zev Weiss3ee959a2022-09-21 17:16:28 -070088 fs::path ctorPath = i2cBusPath(params.bus) / "new_device";
Zev Weissdabd48d2022-08-03 15:43:17 -070089 std::ofstream ctor(ctorPath);
90 if (!ctor.good())
91 {
92 std::cerr << "Failed to open " << ctorPath << "\n";
93 return -1;
94 }
95
Zev Weiss3ee959a2022-09-21 17:16:28 -070096 ctor << params.type->name << " " << params.address << "\n";
Zev Weissdabd48d2022-08-03 15:43:17 -070097 ctor.flush();
98 if (!ctor.good())
99 {
100 std::cerr << "Failed to write to " << ctorPath << "\n";
101 return -1;
102 }
103
104 // Check if that created the requisite sysfs directory
Zev Weiss3ee959a2022-09-21 17:16:28 -0700105 if (!params.devicePresent())
Zev Weissdabd48d2022-08-03 15:43:17 -0700106 {
107 destroy();
108 return -1;
109 }
110
111 return 0;
112}
113
114int I2CDevice::destroy(void) const
115{
Zev Weiss3ee959a2022-09-21 17:16:28 -0700116 // No params.devicePresent() check on this like in create(), since it
117 // might be used to clean up after a device instantiation that was only
118 // partially successful (i.e. when params.devicePresent() would return
119 // false but there's still a dummy i2c client device to remove)
Zev Weissdabd48d2022-08-03 15:43:17 -0700120
Zev Weiss3ee959a2022-09-21 17:16:28 -0700121 fs::path dtorPath = i2cBusPath(params.bus) / "delete_device";
Zev Weissdabd48d2022-08-03 15:43:17 -0700122 std::ofstream dtor(dtorPath);
123 if (!dtor.good())
124 {
125 std::cerr << "Failed to open " << dtorPath << "\n";
126 return -1;
127 }
128
Zev Weiss3ee959a2022-09-21 17:16:28 -0700129 dtor << params.address << "\n";
Zev Weissdabd48d2022-08-03 15:43:17 -0700130 dtor.flush();
131 if (!dtor.good())
132 {
133 std::cerr << "Failed to write to " << dtorPath << "\n";
134 return -1;
135 }
136
137 return 0;
138}