Zev Weiss | dabd48d | 2022-08-03 15:43:17 -0700 | [diff] [blame^] | 1 | #include <DeviceMgmt.hpp> |
| 2 | |
| 3 | #include <filesystem> |
| 4 | #include <fstream> |
| 5 | |
| 6 | namespace fs = std::filesystem; |
| 7 | |
| 8 | std::optional<I2CDevice> getI2CDevice(const I2CDeviceTypeMap& dtmap, |
| 9 | const SensorBaseConfigMap& cfg) |
| 10 | { |
| 11 | auto findType = cfg.find("Type"); |
| 12 | auto findBus = cfg.find("Bus"); |
| 13 | auto findAddr = cfg.find("Address"); |
| 14 | |
| 15 | if (findType == cfg.end() || findBus == cfg.end() || findAddr == cfg.end()) |
| 16 | { |
| 17 | return std::nullopt; |
| 18 | } |
| 19 | |
| 20 | const std::string* type = std::get_if<std::string>(&findType->second); |
| 21 | const uint64_t* bus = std::get_if<uint64_t>(&findBus->second); |
| 22 | const uint64_t* addr = std::get_if<uint64_t>(&findAddr->second); |
| 23 | |
| 24 | if (type == nullptr || bus == nullptr || addr == nullptr) |
| 25 | { |
| 26 | return std::nullopt; |
| 27 | } |
| 28 | |
| 29 | auto findDevType = dtmap.find(type->c_str()); |
| 30 | if (findDevType == dtmap.end()) |
| 31 | { |
| 32 | return std::nullopt; |
| 33 | } |
| 34 | |
| 35 | return I2CDevice(findDevType->second, *bus, *addr); |
| 36 | } |
| 37 | |
| 38 | static fs::path i2cBusPath(uint64_t bus) |
| 39 | { |
| 40 | return {"/sys/bus/i2c/devices/i2c-" + std::to_string(bus)}; |
| 41 | } |
| 42 | |
| 43 | static std::string deviceDirName(uint64_t bus, uint64_t address) |
| 44 | { |
| 45 | std::ostringstream name; |
| 46 | name << bus << "-" << std::hex << std::setw(4) << std::setfill('0') |
| 47 | << address; |
| 48 | return name.str(); |
| 49 | } |
| 50 | |
| 51 | bool I2CDevice::present(void) const |
| 52 | { |
| 53 | fs::path path = i2cBusPath(bus) / deviceDirName(bus, address); |
| 54 | |
| 55 | if (type->createsHWMon) |
| 56 | { |
| 57 | path /= "hwmon"; |
| 58 | } |
| 59 | |
| 60 | // Ignore errors; anything but a clean 'true' is fine as 'false' |
| 61 | std::error_code ec; |
| 62 | return fs::exists(path, ec); |
| 63 | } |
| 64 | |
| 65 | int I2CDevice::create(void) const |
| 66 | { |
| 67 | // If it's already instantiated, there's nothing we need to do. |
| 68 | if (present()) |
| 69 | { |
| 70 | return 0; |
| 71 | } |
| 72 | |
| 73 | // Try to create it: 'echo $devtype $addr > .../i2c-$bus/new_device' |
| 74 | fs::path ctorPath = i2cBusPath(bus) / "new_device"; |
| 75 | std::ofstream ctor(ctorPath); |
| 76 | if (!ctor.good()) |
| 77 | { |
| 78 | std::cerr << "Failed to open " << ctorPath << "\n"; |
| 79 | return -1; |
| 80 | } |
| 81 | |
| 82 | ctor << type->name << " " << address << "\n"; |
| 83 | ctor.flush(); |
| 84 | if (!ctor.good()) |
| 85 | { |
| 86 | std::cerr << "Failed to write to " << ctorPath << "\n"; |
| 87 | return -1; |
| 88 | } |
| 89 | |
| 90 | // Check if that created the requisite sysfs directory |
| 91 | if (!present()) |
| 92 | { |
| 93 | destroy(); |
| 94 | return -1; |
| 95 | } |
| 96 | |
| 97 | return 0; |
| 98 | } |
| 99 | |
| 100 | int I2CDevice::destroy(void) const |
| 101 | { |
| 102 | // No present() check on this like in create(), since it might be used to |
| 103 | // clean up after a device instantiation that was only partially |
| 104 | // successful (i.e. when present() would return false but there's still a |
| 105 | // dummy i2c client device to remove) |
| 106 | |
| 107 | fs::path dtorPath = i2cBusPath(bus) / "delete_device"; |
| 108 | std::ofstream dtor(dtorPath); |
| 109 | if (!dtor.good()) |
| 110 | { |
| 111 | std::cerr << "Failed to open " << dtorPath << "\n"; |
| 112 | return -1; |
| 113 | } |
| 114 | |
| 115 | dtor << address << "\n"; |
| 116 | dtor.flush(); |
| 117 | if (!dtor.good()) |
| 118 | { |
| 119 | std::cerr << "Failed to write to " << dtorPath << "\n"; |
| 120 | return -1; |
| 121 | } |
| 122 | |
| 123 | return 0; |
| 124 | } |