Zev Weiss | dabd48d | 2022-08-03 15:43:17 -0700 | [diff] [blame] | 1 | #pragma once |
Andrew Jeffery | e73bd0a | 2023-01-25 10:39:57 +1030 | [diff] [blame] | 2 | |
| 3 | #include "Utils.hpp" |
| 4 | |
Zev Weiss | dabd48d | 2022-08-03 15:43:17 -0700 | [diff] [blame] | 5 | #include <boost/container/flat_map.hpp> |
| 6 | |
| 7 | #include <functional> |
| 8 | #include <optional> |
| 9 | #include <string_view> |
| 10 | |
| 11 | struct I2CDeviceType |
| 12 | { |
| 13 | const char* name; |
| 14 | bool createsHWMon; |
| 15 | }; |
| 16 | |
Matt Simmering | 786efb8 | 2023-01-18 14:09:21 -0800 | [diff] [blame] | 17 | struct I2CDeviceComparator |
| 18 | { |
| 19 | bool operator()(const std::string& a, const std::string& b) const noexcept |
| 20 | { |
| 21 | return strcasecmp(a.c_str(), b.c_str()) < 0; |
| 22 | } |
| 23 | }; |
| 24 | |
Zev Weiss | dabd48d | 2022-08-03 15:43:17 -0700 | [diff] [blame] | 25 | using I2CDeviceTypeMap = |
Matt Simmering | 786efb8 | 2023-01-18 14:09:21 -0800 | [diff] [blame] | 26 | boost::container::flat_map<std::string, I2CDeviceType, I2CDeviceComparator>; |
Zev Weiss | dabd48d | 2022-08-03 15:43:17 -0700 | [diff] [blame] | 27 | |
Zev Weiss | 3ee959a | 2022-09-21 17:16:28 -0700 | [diff] [blame] | 28 | struct I2CDeviceParams |
Zev Weiss | dabd48d | 2022-08-03 15:43:17 -0700 | [diff] [blame] | 29 | { |
Zev Weiss | 3ee959a | 2022-09-21 17:16:28 -0700 | [diff] [blame] | 30 | I2CDeviceParams(const I2CDeviceType& type, uint64_t bus, uint64_t address) : |
Zev Weiss | dabd48d | 2022-08-03 15:43:17 -0700 | [diff] [blame] | 31 | type(&type), bus(bus), address(address){}; |
| 32 | |
| 33 | const I2CDeviceType* type; |
| 34 | uint64_t bus; |
| 35 | uint64_t address; |
| 36 | |
Zev Weiss | 3ee959a | 2022-09-21 17:16:28 -0700 | [diff] [blame] | 37 | bool devicePresent(void) const; |
Zev Weiss | 41f49c0 | 2022-09-21 17:27:18 -0700 | [diff] [blame] | 38 | bool deviceStatic(void) const; |
Zev Weiss | 3ee959a | 2022-09-21 17:16:28 -0700 | [diff] [blame] | 39 | }; |
| 40 | |
| 41 | std::optional<I2CDeviceParams> |
| 42 | getI2CDeviceParams(const I2CDeviceTypeMap& dtmap, |
| 43 | const SensorBaseConfigMap& cfg); |
| 44 | |
| 45 | class I2CDevice |
| 46 | { |
| 47 | public: |
| 48 | explicit I2CDevice(I2CDeviceParams params); |
| 49 | ~I2CDevice(); |
| 50 | |
| 51 | private: |
| 52 | I2CDeviceParams params; |
| 53 | |
Zev Weiss | dabd48d | 2022-08-03 15:43:17 -0700 | [diff] [blame] | 54 | int create(void) const; |
| 55 | int destroy(void) const; |
| 56 | }; |
| 57 | |
Zev Weiss | dabd48d | 2022-08-03 15:43:17 -0700 | [diff] [blame] | 58 | // HACK: this declaration "should" live in Utils.hpp, but that leads to a |
| 59 | // tangle of header-dependency hell because each header needs types declared |
| 60 | // in the other. |
| 61 | std::vector<std::unique_ptr<sdbusplus::bus::match_t>> |
| 62 | setupPropertiesChangedMatches( |
| 63 | sdbusplus::asio::connection& bus, const I2CDeviceTypeMap& typeMap, |
| 64 | const std::function<void(sdbusplus::message_t&)>& handler); |