blob: 6d6c296aaa304612ede6d3427cdbf673b092d010 [file] [log] [blame]
Andrew Jefferye73bd0a2023-01-25 10:39:57 +10301#include "DeviceMgmt.hpp"
Zev Weissdabd48d2022-08-03 15:43:17 -07002
Ed Tanouseacbfdd2024-04-04 12:00:24 -07003#include "Utils.hpp"
4
5#include <cstdint>
Zev Weissdabd48d2022-08-03 15:43:17 -07006#include <filesystem>
7#include <fstream>
Ed Tanouseacbfdd2024-04-04 12:00:24 -07008#include <iomanip>
9#include <ios>
10#include <iostream>
11#include <optional>
12#include <sstream>
13#include <stdexcept>
14#include <string>
15#include <system_error>
16#include <variant>
Zev Weissdabd48d2022-08-03 15:43:17 -070017
18namespace fs = std::filesystem;
19
Patrick Williams2aaf7172024-08-16 15:20:40 -040020std::optional<I2CDeviceParams> getI2CDeviceParams(
21 const I2CDeviceTypeMap& dtmap, const SensorBaseConfigMap& cfg)
Zev Weissdabd48d2022-08-03 15:43:17 -070022{
23 auto findType = cfg.find("Type");
24 auto findBus = cfg.find("Bus");
25 auto findAddr = cfg.find("Address");
26
27 if (findType == cfg.end() || findBus == cfg.end() || findAddr == cfg.end())
28 {
29 return std::nullopt;
30 }
31
32 const std::string* type = std::get_if<std::string>(&findType->second);
33 const uint64_t* bus = std::get_if<uint64_t>(&findBus->second);
34 const uint64_t* addr = std::get_if<uint64_t>(&findAddr->second);
35
36 if (type == nullptr || bus == nullptr || addr == nullptr)
37 {
38 return std::nullopt;
39 }
40
41 auto findDevType = dtmap.find(type->c_str());
42 if (findDevType == dtmap.end())
43 {
44 return std::nullopt;
45 }
46
Zev Weiss3ee959a2022-09-21 17:16:28 -070047 return I2CDeviceParams(findDevType->second, *bus, *addr);
Zev Weissdabd48d2022-08-03 15:43:17 -070048}
49
50static fs::path i2cBusPath(uint64_t bus)
51{
52 return {"/sys/bus/i2c/devices/i2c-" + std::to_string(bus)};
53}
54
55static std::string deviceDirName(uint64_t bus, uint64_t address)
56{
57 std::ostringstream name;
58 name << bus << "-" << std::hex << std::setw(4) << std::setfill('0')
59 << address;
60 return name.str();
61}
62
Ed Tanous201a1012024-04-03 18:07:28 -070063bool I2CDeviceParams::devicePresent() const
Zev Weissdabd48d2022-08-03 15:43:17 -070064{
65 fs::path path = i2cBusPath(bus) / deviceDirName(bus, address);
66
67 if (type->createsHWMon)
68 {
69 path /= "hwmon";
70 }
71
72 // Ignore errors; anything but a clean 'true' is fine as 'false'
73 std::error_code ec;
74 return fs::exists(path, ec);
75}
76
Ed Tanous201a1012024-04-03 18:07:28 -070077bool I2CDeviceParams::deviceStatic() const
Zev Weiss41f49c02022-09-21 17:27:18 -070078{
79 if (!devicePresent())
80 {
81 return false;
82 }
83
84 fs::path ofNode = i2cBusPath(bus) / deviceDirName(bus, address) / "of_node";
85
86 // Ignore errors -- if of_node is present the device is a static DT node;
87 // otherwise we can assume we created it from userspace.
88 std::error_code ec;
89 return fs::exists(ofNode, ec);
90}
91
Zev Weiss3ee959a2022-09-21 17:16:28 -070092I2CDevice::I2CDevice(I2CDeviceParams params) : params(params)
93{
94 if (create() != 0)
95 {
96 throw std::runtime_error("failed to instantiate i2c device");
97 }
98}
99
100I2CDevice::~I2CDevice()
101{
102 destroy();
103}
104
Ed Tanous201a1012024-04-03 18:07:28 -0700105int I2CDevice::create() const
Zev Weissdabd48d2022-08-03 15:43:17 -0700106{
107 // If it's already instantiated, there's nothing we need to do.
Zev Weiss3ee959a2022-09-21 17:16:28 -0700108 if (params.devicePresent())
Zev Weissdabd48d2022-08-03 15:43:17 -0700109 {
110 return 0;
111 }
112
113 // Try to create it: 'echo $devtype $addr > .../i2c-$bus/new_device'
Zev Weiss3ee959a2022-09-21 17:16:28 -0700114 fs::path ctorPath = i2cBusPath(params.bus) / "new_device";
Zev Weissdabd48d2022-08-03 15:43:17 -0700115 std::ofstream ctor(ctorPath);
116 if (!ctor.good())
117 {
118 std::cerr << "Failed to open " << ctorPath << "\n";
119 return -1;
120 }
121
Zev Weiss3ee959a2022-09-21 17:16:28 -0700122 ctor << params.type->name << " " << params.address << "\n";
Zev Weissdabd48d2022-08-03 15:43:17 -0700123 ctor.flush();
124 if (!ctor.good())
125 {
126 std::cerr << "Failed to write to " << ctorPath << "\n";
127 return -1;
128 }
129
130 // Check if that created the requisite sysfs directory
Zev Weiss3ee959a2022-09-21 17:16:28 -0700131 if (!params.devicePresent())
Zev Weissdabd48d2022-08-03 15:43:17 -0700132 {
133 destroy();
134 return -1;
135 }
136
137 return 0;
138}
139
Ed Tanous201a1012024-04-03 18:07:28 -0700140int I2CDevice::destroy() const
Zev Weissdabd48d2022-08-03 15:43:17 -0700141{
Zev Weiss3ee959a2022-09-21 17:16:28 -0700142 // No params.devicePresent() check on this like in create(), since it
143 // might be used to clean up after a device instantiation that was only
144 // partially successful (i.e. when params.devicePresent() would return
145 // false but there's still a dummy i2c client device to remove)
Zev Weissdabd48d2022-08-03 15:43:17 -0700146
Zev Weiss3ee959a2022-09-21 17:16:28 -0700147 fs::path dtorPath = i2cBusPath(params.bus) / "delete_device";
Zev Weissdabd48d2022-08-03 15:43:17 -0700148 std::ofstream dtor(dtorPath);
149 if (!dtor.good())
150 {
151 std::cerr << "Failed to open " << dtorPath << "\n";
152 return -1;
153 }
154
Zev Weiss3ee959a2022-09-21 17:16:28 -0700155 dtor << params.address << "\n";
Zev Weissdabd48d2022-08-03 15:43:17 -0700156 dtor.flush();
157 if (!dtor.good())
158 {
159 std::cerr << "Failed to write to " << dtorPath << "\n";
160 return -1;
161 }
162
163 return 0;
164}