James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1 | /* |
| 2 | // Copyright (c) 2018 Intel Corporation |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | */ |
| 16 | |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 17 | #include "EntityManager.hpp" |
| 18 | |
Patrick Venture | a49dc33 | 2019-10-26 08:32:02 -0700 | [diff] [blame] | 19 | #include "Overlay.hpp" |
| 20 | #include "Utils.hpp" |
James Feist | 481c5d5 | 2019-08-13 14:40:40 -0700 | [diff] [blame] | 21 | #include "VariantVisitors.hpp" |
| 22 | |
James Feist | 11be667 | 2018-04-06 14:05:32 -0700 | [diff] [blame] | 23 | #include <boost/algorithm/string/case_conv.hpp> |
James Feist | f5125b0 | 2019-06-06 11:27:43 -0700 | [diff] [blame] | 24 | #include <boost/algorithm/string/classification.hpp> |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 25 | #include <boost/algorithm/string/predicate.hpp> |
| 26 | #include <boost/algorithm/string/replace.hpp> |
James Feist | f5125b0 | 2019-06-06 11:27:43 -0700 | [diff] [blame] | 27 | #include <boost/algorithm/string/split.hpp> |
James Feist | 02d2b93 | 2020-02-06 16:28:48 -0800 | [diff] [blame] | 28 | #include <boost/asio/io_context.hpp> |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 29 | #include <boost/container/flat_map.hpp> |
| 30 | #include <boost/container/flat_set.hpp> |
James Feist | f5125b0 | 2019-06-06 11:27:43 -0700 | [diff] [blame] | 31 | #include <boost/range/iterator_range.hpp> |
James Feist | 637b3ef | 2019-04-15 16:35:30 -0700 | [diff] [blame] | 32 | #include <filesystem> |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 33 | #include <fstream> |
| 34 | #include <iostream> |
| 35 | #include <nlohmann/json.hpp> |
| 36 | #include <regex> |
| 37 | #include <sdbusplus/asio/connection.hpp> |
| 38 | #include <sdbusplus/asio/object_server.hpp> |
| 39 | #include <variant> |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 40 | |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 41 | constexpr const char* configurationDirectory = PACKAGE_DIR "configurations"; |
| 42 | constexpr const char* schemaDirectory = PACKAGE_DIR "configurations/schemas"; |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 43 | constexpr const char* tempConfigDir = "/tmp/configuration/"; |
| 44 | constexpr const char* lastConfiguration = "/tmp/configuration/last.json"; |
| 45 | constexpr const char* currentConfiguration = "/var/configuration/system.json"; |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 46 | constexpr const char* globalSchema = "global.json"; |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 47 | constexpr const int32_t MAX_MAPPER_DEPTH = 0; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 48 | |
James Feist | f1b1414 | 2019-04-10 15:22:09 -0700 | [diff] [blame] | 49 | constexpr const bool DEBUG = false; |
| 50 | |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 51 | struct cmp_str |
| 52 | { |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 53 | bool operator()(const char* a, const char* b) const |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 54 | { |
| 55 | return std::strcmp(a, b) < 0; |
| 56 | } |
| 57 | }; |
| 58 | |
| 59 | // underscore T for collison with dbus c api |
| 60 | enum class probe_type_codes |
| 61 | { |
| 62 | FALSE_T, |
| 63 | TRUE_T, |
| 64 | AND, |
| 65 | OR, |
James Feist | 6bd2a02 | 2018-03-13 12:30:58 -0700 | [diff] [blame] | 66 | FOUND, |
| 67 | MATCH_ONE |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 68 | }; |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 69 | const static boost::container::flat_map<const char*, probe_type_codes, cmp_str> |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 70 | PROBE_TYPES{{{"FALSE", probe_type_codes::FALSE_T}, |
| 71 | {"TRUE", probe_type_codes::TRUE_T}, |
| 72 | {"AND", probe_type_codes::AND}, |
| 73 | {"OR", probe_type_codes::OR}, |
James Feist | 6bd2a02 | 2018-03-13 12:30:58 -0700 | [diff] [blame] | 74 | {"FOUND", probe_type_codes::FOUND}, |
| 75 | {"MATCH_ONE", probe_type_codes::MATCH_ONE}}}; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 76 | |
James Feist | 4133426 | 2019-03-25 13:30:20 -0700 | [diff] [blame] | 77 | static constexpr std::array<const char*, 5> settableInterfaces = { |
| 78 | "FanProfile", "Pid", "Pid.Zone", "Stepwise", "Thresholds"}; |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 79 | using JsonVariantType = |
James Feist | 338b8a7 | 2019-03-01 10:16:45 -0800 | [diff] [blame] | 80 | std::variant<std::vector<std::string>, std::vector<double>, std::string, |
| 81 | int64_t, uint64_t, double, int32_t, uint32_t, int16_t, |
| 82 | uint16_t, uint8_t, bool>; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 83 | using GetSubTreeType = std::vector< |
| 84 | std::pair<std::string, |
| 85 | std::vector<std::pair<std::string, std::vector<std::string>>>>>; |
| 86 | |
| 87 | using ManagedObjectType = boost::container::flat_map< |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 88 | sdbusplus::message::object_path, |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 89 | boost::container::flat_map< |
| 90 | std::string, |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 91 | boost::container::flat_map<std::string, BasicVariantType>>>; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 92 | |
James Feist | d58879a | 2019-09-11 11:26:07 -0700 | [diff] [blame] | 93 | // store reference to all interfaces so we can destroy them later |
| 94 | boost::container::flat_map< |
James Feist | 02d2b93 | 2020-02-06 16:28:48 -0800 | [diff] [blame] | 95 | std::string, std::vector<std::weak_ptr<sdbusplus::asio::dbus_interface>>> |
James Feist | d58879a | 2019-09-11 11:26:07 -0700 | [diff] [blame] | 96 | inventory; |
| 97 | |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 98 | // todo: pass this through nicer |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 99 | std::shared_ptr<sdbusplus::asio::connection> SYSTEM_BUS; |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 100 | static nlohmann::json lastJson; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 101 | |
James Feist | 02d2b93 | 2020-02-06 16:28:48 -0800 | [diff] [blame] | 102 | boost::asio::io_context io; |
| 103 | |
Johnathan Mantey | 2015f75 | 2019-03-26 15:22:31 -0700 | [diff] [blame] | 104 | const std::regex ILLEGAL_DBUS_PATH_REGEX("[^A-Za-z0-9_.]"); |
| 105 | const std::regex ILLEGAL_DBUS_MEMBER_REGEX("[^A-Za-z0-9_]"); |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 106 | |
James Feist | 02d2b93 | 2020-02-06 16:28:48 -0800 | [diff] [blame] | 107 | void registerCallbacks(std::vector<sdbusplus::bus::match::match>& dbusMatches, |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 108 | nlohmann::json& systemConfiguration, |
James Feist | 733f765 | 2019-11-13 14:32:29 -0800 | [diff] [blame] | 109 | sdbusplus::asio::object_server& objServer, |
| 110 | const DBusProbeObjectT& dbusProbeObjects); |
James Feist | 75fdeeb | 2018-02-20 14:26:16 -0800 | [diff] [blame] | 111 | |
James Feist | d58879a | 2019-09-11 11:26:07 -0700 | [diff] [blame] | 112 | static std::shared_ptr<sdbusplus::asio::dbus_interface> |
| 113 | createInterface(sdbusplus::asio::object_server& objServer, |
| 114 | const std::string& path, const std::string& interface, |
James Feist | 02d2b93 | 2020-02-06 16:28:48 -0800 | [diff] [blame] | 115 | const std::string& parent, bool checkNull = false) |
James Feist | d58879a | 2019-09-11 11:26:07 -0700 | [diff] [blame] | 116 | { |
James Feist | 02d2b93 | 2020-02-06 16:28:48 -0800 | [diff] [blame] | 117 | // on first add we have no reason to check for null before add, as there |
| 118 | // won't be any. For dynamically added interfaces, we check for null so that |
| 119 | // a constant delete/add will not create a memory leak |
| 120 | |
| 121 | auto ptr = objServer.add_interface(path, interface); |
| 122 | auto& dataVector = inventory[parent]; |
| 123 | if (checkNull) |
| 124 | { |
| 125 | auto it = std::find_if(dataVector.begin(), dataVector.end(), |
| 126 | [](const auto& p) { return p.expired(); }); |
| 127 | if (it != dataVector.end()) |
| 128 | { |
| 129 | *it = ptr; |
| 130 | return ptr; |
| 131 | } |
| 132 | } |
| 133 | dataVector.emplace_back(ptr); |
| 134 | return ptr; |
James Feist | d58879a | 2019-09-11 11:26:07 -0700 | [diff] [blame] | 135 | } |
| 136 | |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 137 | // calls the mapper to find all exposed objects of an interface type |
| 138 | // and creates a vector<flat_map> that contains all the key value pairs |
| 139 | // getManagedObjects |
James Feist | 787c3c3 | 2019-11-07 14:42:58 -0800 | [diff] [blame] | 140 | void findDbusObjects(std::vector<std::shared_ptr<PerformProbe>>& probeVector, |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 141 | std::shared_ptr<sdbusplus::asio::connection> connection, |
James Feist | 733f765 | 2019-11-13 14:32:29 -0800 | [diff] [blame] | 142 | boost::container::flat_set<std::string>& interfaces, |
| 143 | std::shared_ptr<PerformScan> scan) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 144 | { |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 145 | |
James Feist | 733f765 | 2019-11-13 14:32:29 -0800 | [diff] [blame] | 146 | for (const auto& [interface, _] : scan->dbusProbeObjects) |
| 147 | { |
| 148 | interfaces.erase(interface); |
| 149 | } |
| 150 | if (interfaces.empty()) |
| 151 | { |
| 152 | return; |
| 153 | } |
| 154 | |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 155 | // find all connections in the mapper that expose a specific type |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 156 | connection->async_method_call( |
James Feist | 733f765 | 2019-11-13 14:32:29 -0800 | [diff] [blame] | 157 | [connection, interfaces, probeVector, |
| 158 | scan](boost::system::error_code& ec, |
| 159 | const GetSubTreeType& interfaceSubtree) { |
James Feist | 0de4015 | 2018-07-25 11:56:12 -0700 | [diff] [blame] | 160 | boost::container::flat_set<std::string> interfaceConnections; |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 161 | if (ec) |
James Feist | 494155a | 2018-03-14 16:23:24 -0700 | [diff] [blame] | 162 | { |
James Feist | 0de4015 | 2018-07-25 11:56:12 -0700 | [diff] [blame] | 163 | if (ec.value() == ENOENT) |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 164 | { |
James Feist | 0de4015 | 2018-07-25 11:56:12 -0700 | [diff] [blame] | 165 | return; // wasn't found by mapper |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 166 | } |
James Feist | 0de4015 | 2018-07-25 11:56:12 -0700 | [diff] [blame] | 167 | std::cerr << "Error communicating to mapper.\n"; |
| 168 | |
| 169 | // if we can't communicate to the mapper something is very wrong |
| 170 | std::exit(EXIT_FAILURE); |
James Feist | 494155a | 2018-03-14 16:23:24 -0700 | [diff] [blame] | 171 | } |
James Feist | 787c3c3 | 2019-11-07 14:42:58 -0800 | [diff] [blame] | 172 | |
| 173 | for (auto& object : interfaceSubtree) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 174 | { |
James Feist | 787c3c3 | 2019-11-07 14:42:58 -0800 | [diff] [blame] | 175 | for (auto& connPair : object.second) |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 176 | { |
James Feist | 787c3c3 | 2019-11-07 14:42:58 -0800 | [diff] [blame] | 177 | interfaceConnections.insert(connPair.first); |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 178 | } |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 179 | } |
James Feist | 787c3c3 | 2019-11-07 14:42:58 -0800 | [diff] [blame] | 180 | |
James Feist | 63845bf | 2019-01-24 12:19:51 -0800 | [diff] [blame] | 181 | if (interfaceConnections.empty()) |
| 182 | { |
James Feist | 63845bf | 2019-01-24 12:19:51 -0800 | [diff] [blame] | 183 | return; |
| 184 | } |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 185 | // get managed objects for all interfaces |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 186 | for (const auto& conn : interfaceConnections) |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 187 | { |
| 188 | connection->async_method_call( |
James Feist | 733f765 | 2019-11-13 14:32:29 -0800 | [diff] [blame] | 189 | [conn, interfaces, probeVector, |
| 190 | scan](boost::system::error_code& errc, |
| 191 | const ManagedObjectType& managedInterface) { |
James Feist | 9813279 | 2019-07-09 13:29:09 -0700 | [diff] [blame] | 192 | if (errc) |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 193 | { |
| 194 | std::cerr |
| 195 | << "error getting managed object for device " |
| 196 | << conn << "\n"; |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 197 | return; |
| 198 | } |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 199 | for (auto& interfaceManagedObj : managedInterface) |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 200 | { |
James Feist | 787c3c3 | 2019-11-07 14:42:58 -0800 | [diff] [blame] | 201 | // we could match multiple interfaces with one owner |
| 202 | for (auto& [interface, object] : |
| 203 | interfaceManagedObj.second) |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 204 | { |
James Feist | 787c3c3 | 2019-11-07 14:42:58 -0800 | [diff] [blame] | 205 | auto ifaceObjFind = interfaces.find(interface); |
| 206 | |
| 207 | if (ifaceObjFind != interfaces.end()) |
| 208 | { |
James Feist | 733f765 | 2019-11-13 14:32:29 -0800 | [diff] [blame] | 209 | scan->dbusProbeObjects[interface] |
| 210 | .emplace_back(object); |
James Feist | 787c3c3 | 2019-11-07 14:42:58 -0800 | [diff] [blame] | 211 | } |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 212 | } |
| 213 | } |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 214 | }, |
James Feist | 787c3c3 | 2019-11-07 14:42:58 -0800 | [diff] [blame] | 215 | conn, "/", "org.freedesktop.DBus.ObjectManager", |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 216 | "GetManagedObjects"); |
| 217 | } |
| 218 | }, |
| 219 | "xyz.openbmc_project.ObjectMapper", |
| 220 | "/xyz/openbmc_project/object_mapper", |
James Feist | 5131ac2 | 2018-10-25 12:22:23 -0700 | [diff] [blame] | 221 | "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", MAX_MAPPER_DEPTH, |
James Feist | 787c3c3 | 2019-11-07 14:42:58 -0800 | [diff] [blame] | 222 | interfaces); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 223 | } |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 224 | // probes dbus interface dictionary for a key with a value that matches a regex |
James Feist | 08a5b17 | 2019-08-28 14:47:47 -0700 | [diff] [blame] | 225 | bool probeDbus(const std::string& interface, |
| 226 | const std::map<std::string, nlohmann::json>& matches, |
James Feist | 733f765 | 2019-11-13 14:32:29 -0800 | [diff] [blame] | 227 | FoundDeviceT& devices, std::shared_ptr<PerformScan> scan, |
| 228 | bool& foundProbe) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 229 | { |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 230 | std::vector<boost::container::flat_map<std::string, BasicVariantType>>& |
James Feist | 733f765 | 2019-11-13 14:32:29 -0800 | [diff] [blame] | 231 | dbusObject = scan->dbusProbeObjects[interface]; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 232 | if (dbusObject.empty()) |
| 233 | { |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 234 | foundProbe = false; |
| 235 | return false; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 236 | } |
| 237 | foundProbe = true; |
| 238 | |
| 239 | bool foundMatch = false; |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 240 | for (auto& device : dbusObject) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 241 | { |
| 242 | bool deviceMatches = true; |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 243 | for (auto& match : matches) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 244 | { |
| 245 | auto deviceValue = device.find(match.first); |
| 246 | if (deviceValue != device.end()) |
| 247 | { |
| 248 | switch (match.second.type()) |
| 249 | { |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 250 | case nlohmann::json::value_t::string: |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 251 | { |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 252 | std::regex search(match.second.get<std::string>()); |
James Feist | 9813279 | 2019-07-09 13:29:09 -0700 | [diff] [blame] | 253 | std::smatch regMatch; |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 254 | |
| 255 | // convert value to string respresentation |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 256 | std::string probeValue = std::visit( |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 257 | VariantToStringVisitor(), deviceValue->second); |
James Feist | 9813279 | 2019-07-09 13:29:09 -0700 | [diff] [blame] | 258 | if (!std::regex_search(probeValue, regMatch, search)) |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 259 | { |
| 260 | deviceMatches = false; |
| 261 | break; |
| 262 | } |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 263 | break; |
| 264 | } |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 265 | case nlohmann::json::value_t::boolean: |
| 266 | case nlohmann::json::value_t::number_unsigned: |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 267 | { |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 268 | unsigned int probeValue = std::visit( |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 269 | VariantToUnsignedIntVisitor(), deviceValue->second); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 270 | |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 271 | if (probeValue != match.second.get<unsigned int>()) |
| 272 | { |
| 273 | deviceMatches = false; |
| 274 | } |
| 275 | break; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 276 | } |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 277 | case nlohmann::json::value_t::number_integer: |
| 278 | { |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 279 | int probeValue = std::visit(VariantToIntVisitor(), |
| 280 | deviceValue->second); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 281 | |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 282 | if (probeValue != match.second.get<int>()) |
| 283 | { |
| 284 | deviceMatches = false; |
| 285 | } |
| 286 | break; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 287 | } |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 288 | case nlohmann::json::value_t::number_float: |
| 289 | { |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 290 | float probeValue = std::visit(VariantToFloatVisitor(), |
| 291 | deviceValue->second); |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 292 | |
| 293 | if (probeValue != match.second.get<float>()) |
| 294 | { |
| 295 | deviceMatches = false; |
| 296 | } |
| 297 | break; |
| 298 | } |
James Feist | 0eb4035 | 2019-04-09 14:44:04 -0700 | [diff] [blame] | 299 | default: |
| 300 | { |
| 301 | std::cerr << "unexpected dbus probe type " |
| 302 | << match.second.type_name() << "\n"; |
| 303 | } |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 304 | } |
| 305 | } |
| 306 | else |
| 307 | { |
| 308 | deviceMatches = false; |
| 309 | break; |
| 310 | } |
| 311 | } |
| 312 | if (deviceMatches) |
| 313 | { |
James Feist | f5125b0 | 2019-06-06 11:27:43 -0700 | [diff] [blame] | 314 | devices.emplace_back(device); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 315 | foundMatch = true; |
| 316 | deviceMatches = false; // for next iteration |
| 317 | } |
| 318 | } |
| 319 | return foundMatch; |
| 320 | } |
| 321 | |
| 322 | // default probe entry point, iterates a list looking for specific types to |
| 323 | // call specific probe functions |
| 324 | bool probe( |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 325 | const std::vector<std::string>& probeCommand, |
James Feist | 733f765 | 2019-11-13 14:32:29 -0800 | [diff] [blame] | 326 | std::shared_ptr<PerformScan> scan, |
James Feist | 08a5b17 | 2019-08-28 14:47:47 -0700 | [diff] [blame] | 327 | std::vector<boost::container::flat_map<std::string, BasicVariantType>>& |
| 328 | foundDevs) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 329 | { |
| 330 | const static std::regex command(R"(\((.*)\))"); |
| 331 | std::smatch match; |
| 332 | bool ret = false; |
James Feist | 6bd2a02 | 2018-03-13 12:30:58 -0700 | [diff] [blame] | 333 | bool matchOne = false; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 334 | bool cur = true; |
| 335 | probe_type_codes lastCommand = probe_type_codes::FALSE_T; |
James Feist | 54a0dca | 2019-06-26 10:34:54 -0700 | [diff] [blame] | 336 | bool first = true; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 337 | |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 338 | for (auto& probe : probeCommand) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 339 | { |
| 340 | bool foundProbe = false; |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 341 | boost::container::flat_map<const char*, probe_type_codes, |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 342 | cmp_str>::const_iterator probeType; |
| 343 | |
| 344 | for (probeType = PROBE_TYPES.begin(); probeType != PROBE_TYPES.end(); |
Patrick Venture | 4b7a745 | 2019-10-28 08:41:02 -0700 | [diff] [blame] | 345 | ++probeType) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 346 | { |
| 347 | if (probe.find(probeType->first) != std::string::npos) |
| 348 | { |
| 349 | foundProbe = true; |
| 350 | break; |
| 351 | } |
| 352 | } |
| 353 | if (foundProbe) |
| 354 | { |
| 355 | switch (probeType->second) |
| 356 | { |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 357 | case probe_type_codes::FALSE_T: |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 358 | { |
James Feist | e31e00a | 2019-07-24 10:45:43 -0700 | [diff] [blame] | 359 | cur = false; |
| 360 | break; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 361 | } |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 362 | case probe_type_codes::TRUE_T: |
| 363 | { |
James Feist | e31e00a | 2019-07-24 10:45:43 -0700 | [diff] [blame] | 364 | cur = true; |
| 365 | break; |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 366 | } |
| 367 | case probe_type_codes::MATCH_ONE: |
| 368 | { |
| 369 | // set current value to last, this probe type shouldn't |
| 370 | // affect the outcome |
| 371 | cur = ret; |
| 372 | matchOne = true; |
| 373 | break; |
| 374 | } |
| 375 | /*case probe_type_codes::AND: |
| 376 | break; |
| 377 | case probe_type_codes::OR: |
| 378 | break; |
| 379 | // these are no-ops until the last command switch |
| 380 | */ |
| 381 | case probe_type_codes::FOUND: |
| 382 | { |
| 383 | if (!std::regex_search(probe, match, command)) |
| 384 | { |
James Feist | 0eb4035 | 2019-04-09 14:44:04 -0700 | [diff] [blame] | 385 | std::cerr << "found probe syntax error " << probe |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 386 | << "\n"; |
| 387 | return false; |
| 388 | } |
| 389 | std::string commandStr = *(match.begin() + 1); |
| 390 | boost::replace_all(commandStr, "'", ""); |
James Feist | 733f765 | 2019-11-13 14:32:29 -0800 | [diff] [blame] | 391 | cur = (std::find(scan->passedProbes.begin(), |
| 392 | scan->passedProbes.end(), |
| 393 | commandStr) != scan->passedProbes.end()); |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 394 | break; |
| 395 | } |
James Feist | 0eb4035 | 2019-04-09 14:44:04 -0700 | [diff] [blame] | 396 | default: |
| 397 | { |
| 398 | break; |
| 399 | } |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 400 | } |
| 401 | } |
| 402 | // look on dbus for object |
| 403 | else |
| 404 | { |
| 405 | if (!std::regex_search(probe, match, command)) |
| 406 | { |
James Feist | 0eb4035 | 2019-04-09 14:44:04 -0700 | [diff] [blame] | 407 | std::cerr << "dbus probe syntax error " << probe << "\n"; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 408 | return false; |
| 409 | } |
| 410 | std::string commandStr = *(match.begin() + 1); |
| 411 | // convert single ticks and single slashes into legal json |
Jae Hyun Yoo | 3936e7a | 2018-03-23 17:26:16 -0700 | [diff] [blame] | 412 | boost::replace_all(commandStr, "'", "\""); |
James Feist | 3f8a278 | 2018-02-12 09:24:42 -0800 | [diff] [blame] | 413 | boost::replace_all(commandStr, R"(\)", R"(\\)"); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 414 | auto json = nlohmann::json::parse(commandStr, nullptr, false); |
| 415 | if (json.is_discarded()) |
| 416 | { |
James Feist | 0eb4035 | 2019-04-09 14:44:04 -0700 | [diff] [blame] | 417 | std::cerr << "dbus command syntax error " << commandStr << "\n"; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 418 | return false; |
| 419 | } |
| 420 | // we can match any (string, variant) property. (string, string) |
| 421 | // does a regex |
| 422 | std::map<std::string, nlohmann::json> dbusProbeMap = |
| 423 | json.get<std::map<std::string, nlohmann::json>>(); |
| 424 | auto findStart = probe.find("("); |
| 425 | if (findStart == std::string::npos) |
| 426 | { |
| 427 | return false; |
| 428 | } |
| 429 | std::string probeInterface = probe.substr(0, findStart); |
James Feist | 733f765 | 2019-11-13 14:32:29 -0800 | [diff] [blame] | 430 | cur = probeDbus(probeInterface, dbusProbeMap, foundDevs, scan, |
| 431 | foundProbe); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | // some functions like AND and OR only take affect after the |
| 435 | // fact |
James Feist | 54a0dca | 2019-06-26 10:34:54 -0700 | [diff] [blame] | 436 | if (lastCommand == probe_type_codes::AND) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 437 | { |
James Feist | 54a0dca | 2019-06-26 10:34:54 -0700 | [diff] [blame] | 438 | ret = cur && ret; |
| 439 | } |
| 440 | else if (lastCommand == probe_type_codes::OR) |
| 441 | { |
| 442 | ret = cur || ret; |
| 443 | } |
| 444 | |
| 445 | if (first) |
| 446 | { |
| 447 | ret = cur; |
| 448 | first = false; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 449 | } |
| 450 | lastCommand = probeType != PROBE_TYPES.end() |
| 451 | ? probeType->second |
| 452 | : probe_type_codes::FALSE_T; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | // probe passed, but empty device |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 456 | if (ret && foundDevs.size() == 0) |
| 457 | { |
James Feist | 08a5b17 | 2019-08-28 14:47:47 -0700 | [diff] [blame] | 458 | foundDevs.emplace_back( |
| 459 | boost::container::flat_map<std::string, BasicVariantType>{}); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 460 | } |
James Feist | 0eb4035 | 2019-04-09 14:44:04 -0700 | [diff] [blame] | 461 | if (matchOne && ret) |
James Feist | 6bd2a02 | 2018-03-13 12:30:58 -0700 | [diff] [blame] | 462 | { |
James Feist | 71f295f | 2019-06-20 13:35:12 -0700 | [diff] [blame] | 463 | // match the last one |
| 464 | auto last = foundDevs.back(); |
James Feist | 0eb4035 | 2019-04-09 14:44:04 -0700 | [diff] [blame] | 465 | foundDevs.clear(); |
James Feist | 71f295f | 2019-06-20 13:35:12 -0700 | [diff] [blame] | 466 | |
| 467 | foundDevs.emplace_back(std::move(last)); |
James Feist | 6bd2a02 | 2018-03-13 12:30:58 -0700 | [diff] [blame] | 468 | } |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 469 | return ret; |
| 470 | } |
| 471 | |
James Feist | 7d80775 | 2019-11-13 14:47:57 -0800 | [diff] [blame] | 472 | PerformProbe::PerformProbe(const std::vector<std::string>& probeCommand, |
| 473 | std::shared_ptr<PerformScan>& scanPtr, |
| 474 | std::function<void(FoundDeviceT&)>&& callback) : |
| 475 | _probeCommand(probeCommand), |
| 476 | scan(scanPtr), _callback(std::move(callback)) |
| 477 | { |
| 478 | } |
| 479 | PerformProbe::~PerformProbe() |
| 480 | { |
| 481 | FoundDeviceT foundDevs; |
| 482 | if (probe(_probeCommand, scan, foundDevs)) |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 483 | { |
James Feist | 7d80775 | 2019-11-13 14:47:57 -0800 | [diff] [blame] | 484 | _callback(foundDevs); |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 485 | } |
James Feist | 7d80775 | 2019-11-13 14:47:57 -0800 | [diff] [blame] | 486 | } |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 487 | |
| 488 | // writes output files to persist data |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 489 | bool writeJsonFiles(const nlohmann::json& systemConfiguration) |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 490 | { |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 491 | std::filesystem::create_directory(configurationOutDir); |
| 492 | std::ofstream output(currentConfiguration); |
James Feist | bb43d02 | 2018-06-12 15:44:33 -0700 | [diff] [blame] | 493 | if (!output.good()) |
| 494 | { |
| 495 | return false; |
| 496 | } |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 497 | output << systemConfiguration.dump(4); |
| 498 | output.close(); |
James Feist | bb43d02 | 2018-06-12 15:44:33 -0700 | [diff] [blame] | 499 | return true; |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 500 | } |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 501 | |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 502 | template <typename JsonType> |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 503 | bool setJsonFromPointer(const std::string& ptrStr, const JsonType& value, |
| 504 | nlohmann::json& systemConfiguration) |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 505 | { |
| 506 | try |
| 507 | { |
| 508 | nlohmann::json::json_pointer ptr(ptrStr); |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 509 | nlohmann::json& ref = systemConfiguration[ptr]; |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 510 | ref = value; |
| 511 | return true; |
| 512 | } |
James Feist | 9813279 | 2019-07-09 13:29:09 -0700 | [diff] [blame] | 513 | catch (const std::out_of_range&) |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 514 | { |
| 515 | return false; |
| 516 | } |
| 517 | } |
James Feist | bb43d02 | 2018-06-12 15:44:33 -0700 | [diff] [blame] | 518 | |
James Feist | ebcc26b | 2019-03-22 12:30:43 -0700 | [diff] [blame] | 519 | // template function to add array as dbus property |
| 520 | template <typename PropertyType> |
| 521 | void addArrayToDbus(const std::string& name, const nlohmann::json& array, |
| 522 | sdbusplus::asio::dbus_interface* iface, |
| 523 | sdbusplus::asio::PropertyPermission permission, |
| 524 | nlohmann::json& systemConfiguration, |
| 525 | const std::string& jsonPointerString) |
| 526 | { |
| 527 | std::vector<PropertyType> values; |
| 528 | for (const auto& property : array) |
| 529 | { |
| 530 | auto ptr = property.get_ptr<const PropertyType*>(); |
| 531 | if (ptr != nullptr) |
| 532 | { |
| 533 | values.emplace_back(*ptr); |
| 534 | } |
| 535 | } |
| 536 | |
| 537 | if (permission == sdbusplus::asio::PropertyPermission::readOnly) |
| 538 | { |
| 539 | iface->register_property(name, values); |
| 540 | } |
| 541 | else |
| 542 | { |
| 543 | iface->register_property( |
| 544 | name, values, |
| 545 | [&systemConfiguration, |
| 546 | jsonPointerString{std::string(jsonPointerString)}]( |
| 547 | const std::vector<PropertyType>& newVal, |
| 548 | std::vector<PropertyType>& val) { |
| 549 | val = newVal; |
| 550 | if (!setJsonFromPointer(jsonPointerString, val, |
| 551 | systemConfiguration)) |
| 552 | { |
| 553 | std::cerr << "error setting json field\n"; |
| 554 | return -1; |
| 555 | } |
| 556 | if (!writeJsonFiles(systemConfiguration)) |
| 557 | { |
| 558 | std::cerr << "error setting json file\n"; |
| 559 | return -1; |
| 560 | } |
| 561 | return 1; |
| 562 | }); |
| 563 | } |
| 564 | } |
| 565 | |
James Feist | bb43d02 | 2018-06-12 15:44:33 -0700 | [diff] [blame] | 566 | template <typename PropertyType> |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 567 | void addProperty(const std::string& propertyName, const PropertyType& value, |
| 568 | sdbusplus::asio::dbus_interface* iface, |
| 569 | nlohmann::json& systemConfiguration, |
| 570 | const std::string& jsonPointerString, |
James Feist | bb43d02 | 2018-06-12 15:44:33 -0700 | [diff] [blame] | 571 | sdbusplus::asio::PropertyPermission permission) |
| 572 | { |
| 573 | if (permission == sdbusplus::asio::PropertyPermission::readOnly) |
| 574 | { |
| 575 | iface->register_property(propertyName, value); |
| 576 | return; |
| 577 | } |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 578 | iface->register_property( |
| 579 | propertyName, value, |
| 580 | [&systemConfiguration, |
| 581 | jsonPointerString{std::string(jsonPointerString)}]( |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 582 | const PropertyType& newVal, PropertyType& val) { |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 583 | val = newVal; |
| 584 | if (!setJsonFromPointer(jsonPointerString, val, |
| 585 | systemConfiguration)) |
| 586 | { |
| 587 | std::cerr << "error setting json field\n"; |
| 588 | return -1; |
| 589 | } |
James Feist | c6248a5 | 2018-08-14 10:09:45 -0700 | [diff] [blame] | 590 | if (!writeJsonFiles(systemConfiguration)) |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 591 | { |
| 592 | std::cerr << "error setting json file\n"; |
James Feist | c6248a5 | 2018-08-14 10:09:45 -0700 | [diff] [blame] | 593 | return -1; |
| 594 | } |
| 595 | return 1; |
| 596 | }); |
| 597 | } |
| 598 | |
| 599 | void createDeleteObjectMethod( |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 600 | const std::string& jsonPointerPath, |
| 601 | const std::shared_ptr<sdbusplus::asio::dbus_interface>& iface, |
| 602 | sdbusplus::asio::object_server& objServer, |
| 603 | nlohmann::json& systemConfiguration) |
James Feist | c6248a5 | 2018-08-14 10:09:45 -0700 | [diff] [blame] | 604 | { |
| 605 | std::weak_ptr<sdbusplus::asio::dbus_interface> interface = iface; |
| 606 | iface->register_method( |
| 607 | "Delete", [&objServer, &systemConfiguration, interface, |
| 608 | jsonPointerPath{std::string(jsonPointerPath)}]() { |
James Feist | 9813279 | 2019-07-09 13:29:09 -0700 | [diff] [blame] | 609 | std::shared_ptr<sdbusplus::asio::dbus_interface> dbusInterface = |
James Feist | c6248a5 | 2018-08-14 10:09:45 -0700 | [diff] [blame] | 610 | interface.lock(); |
James Feist | 9813279 | 2019-07-09 13:29:09 -0700 | [diff] [blame] | 611 | if (!dbusInterface) |
James Feist | c6248a5 | 2018-08-14 10:09:45 -0700 | [diff] [blame] | 612 | { |
| 613 | // this technically can't happen as the pointer is pointing to |
| 614 | // us |
| 615 | throw DBusInternalError(); |
| 616 | } |
| 617 | nlohmann::json::json_pointer ptr(jsonPointerPath); |
James Feist | c6248a5 | 2018-08-14 10:09:45 -0700 | [diff] [blame] | 618 | systemConfiguration[ptr] = nullptr; |
| 619 | |
James Feist | 02d2b93 | 2020-02-06 16:28:48 -0800 | [diff] [blame] | 620 | // todo(james): dig through sdbusplus to find out why we can't |
| 621 | // delete it in a method call |
| 622 | io.post([&objServer, dbusInterface]() mutable { |
| 623 | objServer.remove_interface(dbusInterface); |
| 624 | }); |
| 625 | |
James Feist | c6248a5 | 2018-08-14 10:09:45 -0700 | [diff] [blame] | 626 | if (!writeJsonFiles(systemConfiguration)) |
| 627 | { |
| 628 | std::cerr << "error setting json file\n"; |
| 629 | throw DBusInternalError(); |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 630 | } |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 631 | }); |
James Feist | bb43d02 | 2018-06-12 15:44:33 -0700 | [diff] [blame] | 632 | } |
| 633 | |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 634 | // adds simple json types to interface's properties |
James Feist | bb43d02 | 2018-06-12 15:44:33 -0700 | [diff] [blame] | 635 | void populateInterfaceFromJson( |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 636 | nlohmann::json& systemConfiguration, const std::string& jsonPointerPath, |
| 637 | std::shared_ptr<sdbusplus::asio::dbus_interface>& iface, |
| 638 | nlohmann::json& dict, sdbusplus::asio::object_server& objServer, |
James Feist | bb43d02 | 2018-06-12 15:44:33 -0700 | [diff] [blame] | 639 | sdbusplus::asio::PropertyPermission permission = |
| 640 | sdbusplus::asio::PropertyPermission::readOnly) |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 641 | { |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 642 | for (auto& dictPair : dict.items()) |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 643 | { |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 644 | auto type = dictPair.value().type(); |
| 645 | bool array = false; |
| 646 | if (dictPair.value().type() == nlohmann::json::value_t::array) |
| 647 | { |
| 648 | array = true; |
| 649 | if (!dictPair.value().size()) |
| 650 | { |
| 651 | continue; |
| 652 | } |
| 653 | type = dictPair.value()[0].type(); |
| 654 | bool isLegal = true; |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 655 | for (const auto& arrayItem : dictPair.value()) |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 656 | { |
| 657 | if (arrayItem.type() != type) |
| 658 | { |
| 659 | isLegal = false; |
| 660 | break; |
| 661 | } |
| 662 | } |
| 663 | if (!isLegal) |
| 664 | { |
| 665 | std::cerr << "dbus format error" << dictPair.value() << "\n"; |
| 666 | continue; |
| 667 | } |
James Feist | a218ddb | 2019-04-11 14:01:31 -0700 | [diff] [blame] | 668 | } |
| 669 | if (type == nlohmann::json::value_t::object) |
| 670 | { |
| 671 | continue; // handled elsewhere |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 672 | } |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 673 | std::string key = jsonPointerPath + "/" + dictPair.key(); |
James Feist | bb43d02 | 2018-06-12 15:44:33 -0700 | [diff] [blame] | 674 | if (permission == sdbusplus::asio::PropertyPermission::readWrite) |
| 675 | { |
| 676 | // all setable numbers are doubles as it is difficult to always |
| 677 | // create a configuration file with all whole numbers as decimals |
| 678 | // i.e. 1.0 |
James Feist | ebcc26b | 2019-03-22 12:30:43 -0700 | [diff] [blame] | 679 | if (array) |
| 680 | { |
| 681 | if (dictPair.value()[0].is_number()) |
| 682 | { |
| 683 | type = nlohmann::json::value_t::number_float; |
| 684 | } |
| 685 | } |
| 686 | else if (dictPair.value().is_number()) |
James Feist | bb43d02 | 2018-06-12 15:44:33 -0700 | [diff] [blame] | 687 | { |
| 688 | type = nlohmann::json::value_t::number_float; |
| 689 | } |
| 690 | } |
| 691 | |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 692 | switch (type) |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 693 | { |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 694 | case (nlohmann::json::value_t::boolean): |
| 695 | { |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 696 | if (array) |
| 697 | { |
| 698 | // todo: array of bool isn't detected correctly by |
| 699 | // sdbusplus, change it to numbers |
| 700 | addArrayToDbus<uint64_t>(dictPair.key(), dictPair.value(), |
James Feist | ebcc26b | 2019-03-22 12:30:43 -0700 | [diff] [blame] | 701 | iface.get(), permission, |
| 702 | systemConfiguration, key); |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 703 | } |
James Feist | bb43d02 | 2018-06-12 15:44:33 -0700 | [diff] [blame] | 704 | |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 705 | else |
| 706 | { |
James Feist | bb43d02 | 2018-06-12 15:44:33 -0700 | [diff] [blame] | 707 | addProperty(dictPair.key(), dictPair.value().get<bool>(), |
James Feist | c6248a5 | 2018-08-14 10:09:45 -0700 | [diff] [blame] | 708 | iface.get(), systemConfiguration, key, |
| 709 | permission); |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 710 | } |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 711 | break; |
| 712 | } |
| 713 | case (nlohmann::json::value_t::number_integer): |
| 714 | { |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 715 | if (array) |
| 716 | { |
| 717 | addArrayToDbus<int64_t>(dictPair.key(), dictPair.value(), |
James Feist | ebcc26b | 2019-03-22 12:30:43 -0700 | [diff] [blame] | 718 | iface.get(), permission, |
| 719 | systemConfiguration, key); |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 720 | } |
| 721 | else |
| 722 | { |
James Feist | bb43d02 | 2018-06-12 15:44:33 -0700 | [diff] [blame] | 723 | addProperty(dictPair.key(), dictPair.value().get<int64_t>(), |
James Feist | c6248a5 | 2018-08-14 10:09:45 -0700 | [diff] [blame] | 724 | iface.get(), systemConfiguration, key, |
James Feist | bb43d02 | 2018-06-12 15:44:33 -0700 | [diff] [blame] | 725 | sdbusplus::asio::PropertyPermission::readOnly); |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 726 | } |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 727 | break; |
| 728 | } |
| 729 | case (nlohmann::json::value_t::number_unsigned): |
| 730 | { |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 731 | if (array) |
| 732 | { |
| 733 | addArrayToDbus<uint64_t>(dictPair.key(), dictPair.value(), |
James Feist | ebcc26b | 2019-03-22 12:30:43 -0700 | [diff] [blame] | 734 | iface.get(), permission, |
| 735 | systemConfiguration, key); |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 736 | } |
| 737 | else |
| 738 | { |
James Feist | bb43d02 | 2018-06-12 15:44:33 -0700 | [diff] [blame] | 739 | addProperty(dictPair.key(), |
James Feist | c6248a5 | 2018-08-14 10:09:45 -0700 | [diff] [blame] | 740 | dictPair.value().get<uint64_t>(), iface.get(), |
James Feist | bb43d02 | 2018-06-12 15:44:33 -0700 | [diff] [blame] | 741 | systemConfiguration, key, |
| 742 | sdbusplus::asio::PropertyPermission::readOnly); |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 743 | } |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 744 | break; |
| 745 | } |
| 746 | case (nlohmann::json::value_t::number_float): |
| 747 | { |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 748 | if (array) |
| 749 | { |
| 750 | addArrayToDbus<double>(dictPair.key(), dictPair.value(), |
James Feist | ebcc26b | 2019-03-22 12:30:43 -0700 | [diff] [blame] | 751 | iface.get(), permission, |
| 752 | systemConfiguration, key); |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 753 | } |
James Feist | bb43d02 | 2018-06-12 15:44:33 -0700 | [diff] [blame] | 754 | |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 755 | else |
| 756 | { |
James Feist | bb43d02 | 2018-06-12 15:44:33 -0700 | [diff] [blame] | 757 | addProperty(dictPair.key(), dictPair.value().get<double>(), |
James Feist | c6248a5 | 2018-08-14 10:09:45 -0700 | [diff] [blame] | 758 | iface.get(), systemConfiguration, key, |
| 759 | permission); |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 760 | } |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 761 | break; |
| 762 | } |
| 763 | case (nlohmann::json::value_t::string): |
| 764 | { |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 765 | if (array) |
| 766 | { |
James Feist | ebcc26b | 2019-03-22 12:30:43 -0700 | [diff] [blame] | 767 | addArrayToDbus<std::string>( |
| 768 | dictPair.key(), dictPair.value(), iface.get(), |
| 769 | permission, systemConfiguration, key); |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 770 | } |
| 771 | else |
| 772 | { |
James Feist | c6248a5 | 2018-08-14 10:09:45 -0700 | [diff] [blame] | 773 | addProperty( |
| 774 | dictPair.key(), dictPair.value().get<std::string>(), |
| 775 | iface.get(), systemConfiguration, key, permission); |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 776 | } |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 777 | break; |
| 778 | } |
James Feist | 0eb4035 | 2019-04-09 14:44:04 -0700 | [diff] [blame] | 779 | default: |
| 780 | { |
James Feist | a218ddb | 2019-04-11 14:01:31 -0700 | [diff] [blame] | 781 | std::cerr << "Unexpected json type in system configuration " |
| 782 | << dictPair.key() << ": " |
| 783 | << dictPair.value().type_name() << "\n"; |
James Feist | 0eb4035 | 2019-04-09 14:44:04 -0700 | [diff] [blame] | 784 | break; |
| 785 | } |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 786 | } |
| 787 | } |
James Feist | c6248a5 | 2018-08-14 10:09:45 -0700 | [diff] [blame] | 788 | if (permission == sdbusplus::asio::PropertyPermission::readWrite) |
| 789 | { |
| 790 | createDeleteObjectMethod(jsonPointerPath, iface, objServer, |
| 791 | systemConfiguration); |
| 792 | } |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 793 | iface->initialize(); |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 794 | } |
| 795 | |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 796 | sdbusplus::asio::PropertyPermission getPermission(const std::string& interface) |
James Feist | c6248a5 | 2018-08-14 10:09:45 -0700 | [diff] [blame] | 797 | { |
| 798 | return std::find(settableInterfaces.begin(), settableInterfaces.end(), |
| 799 | interface) != settableInterfaces.end() |
| 800 | ? sdbusplus::asio::PropertyPermission::readWrite |
| 801 | : sdbusplus::asio::PropertyPermission::readOnly; |
| 802 | } |
| 803 | |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 804 | void createAddObjectMethod(const std::string& jsonPointerPath, |
| 805 | const std::string& path, |
| 806 | nlohmann::json& systemConfiguration, |
James Feist | d58879a | 2019-09-11 11:26:07 -0700 | [diff] [blame] | 807 | sdbusplus::asio::object_server& objServer, |
| 808 | const std::string& board) |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 809 | { |
James Feist | d58879a | 2019-09-11 11:26:07 -0700 | [diff] [blame] | 810 | std::shared_ptr<sdbusplus::asio::dbus_interface> iface = createInterface( |
| 811 | objServer, path, "xyz.openbmc_project.AddObject", board); |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 812 | |
| 813 | iface->register_method( |
| 814 | "AddObject", |
| 815 | [&systemConfiguration, &objServer, |
James Feist | d58879a | 2019-09-11 11:26:07 -0700 | [diff] [blame] | 816 | jsonPointerPath{std::string(jsonPointerPath)}, path{std::string(path)}, |
| 817 | board](const boost::container::flat_map<std::string, JsonVariantType>& |
| 818 | data) { |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 819 | nlohmann::json::json_pointer ptr(jsonPointerPath); |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 820 | nlohmann::json& base = systemConfiguration[ptr]; |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 821 | auto findExposes = base.find("Exposes"); |
| 822 | |
| 823 | if (findExposes == base.end()) |
| 824 | { |
| 825 | throw std::invalid_argument("Entity must have children."); |
| 826 | } |
| 827 | |
| 828 | // this will throw invalid-argument to sdbusplus if invalid json |
| 829 | nlohmann::json newData{}; |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 830 | for (const auto& item : data) |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 831 | { |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 832 | nlohmann::json& newJson = newData[item.first]; |
| 833 | std::visit([&newJson](auto&& val) { newJson = std::move(val); }, |
| 834 | item.second); |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 835 | } |
| 836 | |
| 837 | auto findName = newData.find("Name"); |
| 838 | auto findType = newData.find("Type"); |
| 839 | if (findName == newData.end() || findType == newData.end()) |
| 840 | { |
| 841 | throw std::invalid_argument("AddObject missing Name or Type"); |
| 842 | } |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 843 | const std::string* type = findType->get_ptr<const std::string*>(); |
| 844 | const std::string* name = findName->get_ptr<const std::string*>(); |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 845 | if (type == nullptr || name == nullptr) |
| 846 | { |
| 847 | throw std::invalid_argument("Type and Name must be a string."); |
| 848 | } |
| 849 | |
James Feist | 02d2b93 | 2020-02-06 16:28:48 -0800 | [diff] [blame] | 850 | bool foundNull = false; |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 851 | size_t lastIndex = 0; |
| 852 | // we add in the "exposes" |
James Feist | 02d2b93 | 2020-02-06 16:28:48 -0800 | [diff] [blame] | 853 | for (const auto& expose : *findExposes) |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 854 | { |
James Feist | 02d2b93 | 2020-02-06 16:28:48 -0800 | [diff] [blame] | 855 | if (expose.is_null()) |
| 856 | { |
| 857 | foundNull = true; |
| 858 | continue; |
| 859 | } |
| 860 | |
| 861 | if (expose["Name"] == *name && expose["Type"] == *type) |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 862 | { |
| 863 | throw std::invalid_argument( |
| 864 | "Field already in JSON, not adding"); |
| 865 | } |
James Feist | 02d2b93 | 2020-02-06 16:28:48 -0800 | [diff] [blame] | 866 | |
| 867 | if (foundNull) |
| 868 | { |
| 869 | continue; |
| 870 | } |
| 871 | |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 872 | lastIndex++; |
| 873 | } |
| 874 | |
| 875 | std::ifstream schemaFile(std::string(schemaDirectory) + "/" + |
| 876 | *type + ".json"); |
| 877 | // todo(james) we might want to also make a list of 'can add' |
| 878 | // interfaces but for now I think the assumption if there is a |
| 879 | // schema avaliable that it is allowed to update is fine |
| 880 | if (!schemaFile.good()) |
| 881 | { |
| 882 | throw std::invalid_argument( |
| 883 | "No schema avaliable, cannot validate."); |
| 884 | } |
| 885 | nlohmann::json schema = |
| 886 | nlohmann::json::parse(schemaFile, nullptr, false); |
| 887 | if (schema.is_discarded()) |
| 888 | { |
| 889 | std::cerr << "Schema not legal" << *type << ".json\n"; |
| 890 | throw DBusInternalError(); |
| 891 | } |
| 892 | if (!validateJson(schema, newData)) |
| 893 | { |
| 894 | throw std::invalid_argument("Data does not match schema"); |
| 895 | } |
James Feist | 02d2b93 | 2020-02-06 16:28:48 -0800 | [diff] [blame] | 896 | if (foundNull) |
| 897 | { |
| 898 | findExposes->at(lastIndex) = newData; |
| 899 | } |
| 900 | else |
| 901 | { |
| 902 | findExposes->push_back(newData); |
| 903 | } |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 904 | if (!writeJsonFiles(systemConfiguration)) |
| 905 | { |
| 906 | std::cerr << "Error writing json files\n"; |
| 907 | throw DBusInternalError(); |
| 908 | } |
| 909 | std::string dbusName = *name; |
| 910 | |
| 911 | std::regex_replace(dbusName.begin(), dbusName.begin(), |
Johnathan Mantey | 2015f75 | 2019-03-26 15:22:31 -0700 | [diff] [blame] | 912 | dbusName.end(), ILLEGAL_DBUS_MEMBER_REGEX, "_"); |
James Feist | d58879a | 2019-09-11 11:26:07 -0700 | [diff] [blame] | 913 | |
| 914 | std::shared_ptr<sdbusplus::asio::dbus_interface> interface = |
| 915 | createInterface(objServer, path + "/" + dbusName, |
| 916 | "xyz.openbmc_project.Configuration." + *type, |
James Feist | 02d2b93 | 2020-02-06 16:28:48 -0800 | [diff] [blame] | 917 | board, true); |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 918 | // permission is read-write, as since we just created it, must be |
| 919 | // runtime modifiable |
| 920 | populateInterfaceFromJson( |
| 921 | systemConfiguration, |
James Feist | 16a02f2 | 2019-05-13 15:21:37 -0700 | [diff] [blame] | 922 | jsonPointerPath + "/Exposes/" + std::to_string(lastIndex), |
James Feist | 9813279 | 2019-07-09 13:29:09 -0700 | [diff] [blame] | 923 | interface, newData, objServer, |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 924 | sdbusplus::asio::PropertyPermission::readWrite); |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 925 | }); |
| 926 | iface->initialize(); |
| 927 | } |
| 928 | |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 929 | void postToDbus(const nlohmann::json& newConfiguration, |
| 930 | nlohmann::json& systemConfiguration, |
| 931 | sdbusplus::asio::object_server& objServer) |
James Feist | 75fdeeb | 2018-02-20 14:26:16 -0800 | [diff] [blame] | 932 | |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 933 | { |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 934 | // iterate through boards |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 935 | for (auto& boardPair : newConfiguration.items()) |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 936 | { |
James Feist | f1b1414 | 2019-04-10 15:22:09 -0700 | [diff] [blame] | 937 | std::string boardKey = boardPair.value()["Name"]; |
James Feist | d58879a | 2019-09-11 11:26:07 -0700 | [diff] [blame] | 938 | std::string boardKeyOrig = boardPair.value()["Name"]; |
James Feist | f1b1414 | 2019-04-10 15:22:09 -0700 | [diff] [blame] | 939 | std::string jsonPointerPath = "/" + boardPair.key(); |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 940 | // loop through newConfiguration, but use values from system |
| 941 | // configuration to be able to modify via dbus later |
James Feist | f1b1414 | 2019-04-10 15:22:09 -0700 | [diff] [blame] | 942 | auto boardValues = systemConfiguration[boardPair.key()]; |
James Feist | d63d18a | 2018-07-19 15:23:45 -0700 | [diff] [blame] | 943 | auto findBoardType = boardValues.find("Type"); |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 944 | std::string boardType; |
| 945 | if (findBoardType != boardValues.end() && |
| 946 | findBoardType->type() == nlohmann::json::value_t::string) |
| 947 | { |
| 948 | boardType = findBoardType->get<std::string>(); |
| 949 | std::regex_replace(boardType.begin(), boardType.begin(), |
Johnathan Mantey | 2015f75 | 2019-03-26 15:22:31 -0700 | [diff] [blame] | 950 | boardType.end(), ILLEGAL_DBUS_MEMBER_REGEX, "_"); |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 951 | } |
| 952 | else |
| 953 | { |
| 954 | std::cerr << "Unable to find type for " << boardKey |
| 955 | << " reverting to Chassis.\n"; |
| 956 | boardType = "Chassis"; |
| 957 | } |
James Feist | 11be667 | 2018-04-06 14:05:32 -0700 | [diff] [blame] | 958 | std::string boardtypeLower = boost::algorithm::to_lower_copy(boardType); |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 959 | |
| 960 | std::regex_replace(boardKey.begin(), boardKey.begin(), boardKey.end(), |
Johnathan Mantey | 2015f75 | 2019-03-26 15:22:31 -0700 | [diff] [blame] | 961 | ILLEGAL_DBUS_MEMBER_REGEX, "_"); |
James Feist | 11be667 | 2018-04-06 14:05:32 -0700 | [diff] [blame] | 962 | std::string boardName = "/xyz/openbmc_project/inventory/system/" + |
| 963 | boardtypeLower + "/" + boardKey; |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 964 | |
James Feist | d58879a | 2019-09-11 11:26:07 -0700 | [diff] [blame] | 965 | std::shared_ptr<sdbusplus::asio::dbus_interface> inventoryIface = |
| 966 | createInterface(objServer, boardName, |
| 967 | "xyz.openbmc_project.Inventory.Item", boardKey); |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 968 | |
James Feist | d58879a | 2019-09-11 11:26:07 -0700 | [diff] [blame] | 969 | std::shared_ptr<sdbusplus::asio::dbus_interface> boardIface = |
| 970 | createInterface(objServer, boardName, |
| 971 | "xyz.openbmc_project.Inventory.Item." + boardType, |
| 972 | boardKeyOrig); |
James Feist | 11be667 | 2018-04-06 14:05:32 -0700 | [diff] [blame] | 973 | |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 974 | createAddObjectMethod(jsonPointerPath, boardName, systemConfiguration, |
James Feist | d58879a | 2019-09-11 11:26:07 -0700 | [diff] [blame] | 975 | objServer, boardKeyOrig); |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 976 | |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 977 | populateInterfaceFromJson(systemConfiguration, jsonPointerPath, |
James Feist | c6248a5 | 2018-08-14 10:09:45 -0700 | [diff] [blame] | 978 | boardIface, boardValues, objServer); |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 979 | jsonPointerPath += "/"; |
| 980 | // iterate through board properties |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 981 | for (auto& boardField : boardValues.items()) |
James Feist | 11be667 | 2018-04-06 14:05:32 -0700 | [diff] [blame] | 982 | { |
| 983 | if (boardField.value().type() == nlohmann::json::value_t::object) |
| 984 | { |
James Feist | d58879a | 2019-09-11 11:26:07 -0700 | [diff] [blame] | 985 | std::shared_ptr<sdbusplus::asio::dbus_interface> iface = |
| 986 | createInterface(objServer, boardName, boardField.key(), |
| 987 | boardKeyOrig); |
| 988 | |
James Feist | c6248a5 | 2018-08-14 10:09:45 -0700 | [diff] [blame] | 989 | populateInterfaceFromJson(systemConfiguration, |
| 990 | jsonPointerPath + boardField.key(), |
| 991 | iface, boardField.value(), objServer); |
James Feist | 11be667 | 2018-04-06 14:05:32 -0700 | [diff] [blame] | 992 | } |
| 993 | } |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 994 | |
James Feist | 1e3e698 | 2018-08-03 16:09:28 -0700 | [diff] [blame] | 995 | auto exposes = boardValues.find("Exposes"); |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 996 | if (exposes == boardValues.end()) |
| 997 | { |
| 998 | continue; |
| 999 | } |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 1000 | // iterate through exposes |
James Feist | 1e3e698 | 2018-08-03 16:09:28 -0700 | [diff] [blame] | 1001 | jsonPointerPath += "Exposes/"; |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 1002 | |
| 1003 | // store the board level pointer so we can modify it on the way down |
| 1004 | std::string jsonPointerPathBoard = jsonPointerPath; |
| 1005 | size_t exposesIndex = -1; |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 1006 | for (auto& item : *exposes) |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 1007 | { |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 1008 | exposesIndex++; |
| 1009 | jsonPointerPath = jsonPointerPathBoard; |
| 1010 | jsonPointerPath += std::to_string(exposesIndex); |
| 1011 | |
James Feist | d63d18a | 2018-07-19 15:23:45 -0700 | [diff] [blame] | 1012 | auto findName = item.find("Name"); |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 1013 | if (findName == item.end()) |
| 1014 | { |
| 1015 | std::cerr << "cannot find name in field " << item << "\n"; |
| 1016 | continue; |
| 1017 | } |
James Feist | 1e3e698 | 2018-08-03 16:09:28 -0700 | [diff] [blame] | 1018 | auto findStatus = item.find("Status"); |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 1019 | // if status is not found it is assumed to be status = 'okay' |
| 1020 | if (findStatus != item.end()) |
| 1021 | { |
| 1022 | if (*findStatus == "disabled") |
| 1023 | { |
| 1024 | continue; |
| 1025 | } |
| 1026 | } |
James Feist | d63d18a | 2018-07-19 15:23:45 -0700 | [diff] [blame] | 1027 | auto findType = item.find("Type"); |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 1028 | std::string itemType; |
| 1029 | if (findType != item.end()) |
| 1030 | { |
| 1031 | itemType = findType->get<std::string>(); |
| 1032 | std::regex_replace(itemType.begin(), itemType.begin(), |
Johnathan Mantey | 2015f75 | 2019-03-26 15:22:31 -0700 | [diff] [blame] | 1033 | itemType.end(), ILLEGAL_DBUS_PATH_REGEX, |
| 1034 | "_"); |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 1035 | } |
| 1036 | else |
| 1037 | { |
| 1038 | itemType = "unknown"; |
| 1039 | } |
| 1040 | std::string itemName = findName->get<std::string>(); |
| 1041 | std::regex_replace(itemName.begin(), itemName.begin(), |
Johnathan Mantey | 2015f75 | 2019-03-26 15:22:31 -0700 | [diff] [blame] | 1042 | itemName.end(), ILLEGAL_DBUS_MEMBER_REGEX, "_"); |
James Feist | c6248a5 | 2018-08-14 10:09:45 -0700 | [diff] [blame] | 1043 | |
James Feist | d58879a | 2019-09-11 11:26:07 -0700 | [diff] [blame] | 1044 | std::shared_ptr<sdbusplus::asio::dbus_interface> itemIface = |
| 1045 | createInterface(objServer, boardName + "/" + itemName, |
| 1046 | "xyz.openbmc_project.Configuration." + itemType, |
| 1047 | boardKeyOrig); |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 1048 | |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 1049 | populateInterfaceFromJson(systemConfiguration, jsonPointerPath, |
James Feist | c6248a5 | 2018-08-14 10:09:45 -0700 | [diff] [blame] | 1050 | itemIface, item, objServer, |
| 1051 | getPermission(itemType)); |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 1052 | |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 1053 | for (auto& objectPair : item.items()) |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 1054 | { |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 1055 | jsonPointerPath = jsonPointerPathBoard + |
| 1056 | std::to_string(exposesIndex) + "/" + |
| 1057 | objectPair.key(); |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 1058 | if (objectPair.value().type() == |
| 1059 | nlohmann::json::value_t::object) |
| 1060 | { |
James Feist | d58879a | 2019-09-11 11:26:07 -0700 | [diff] [blame] | 1061 | std::shared_ptr<sdbusplus::asio::dbus_interface> |
| 1062 | objectIface = createInterface( |
| 1063 | objServer, boardName + "/" + itemName, |
| 1064 | "xyz.openbmc_project.Configuration." + itemType + |
| 1065 | "." + objectPair.key(), |
| 1066 | boardKeyOrig); |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 1067 | |
| 1068 | populateInterfaceFromJson( |
James Feist | c6248a5 | 2018-08-14 10:09:45 -0700 | [diff] [blame] | 1069 | systemConfiguration, jsonPointerPath, objectIface, |
| 1070 | objectPair.value(), objServer, getPermission(itemType)); |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 1071 | } |
| 1072 | else if (objectPair.value().type() == |
| 1073 | nlohmann::json::value_t::array) |
| 1074 | { |
| 1075 | size_t index = 0; |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1076 | if (!objectPair.value().size()) |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 1077 | { |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1078 | continue; |
| 1079 | } |
| 1080 | bool isLegal = true; |
| 1081 | auto type = objectPair.value()[0].type(); |
| 1082 | if (type != nlohmann::json::value_t::object) |
| 1083 | { |
| 1084 | continue; |
| 1085 | } |
| 1086 | |
| 1087 | // verify legal json |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 1088 | for (const auto& arrayItem : objectPair.value()) |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1089 | { |
| 1090 | if (arrayItem.type() != type) |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 1091 | { |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1092 | isLegal = false; |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 1093 | break; |
| 1094 | } |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1095 | } |
| 1096 | if (!isLegal) |
| 1097 | { |
| 1098 | std::cerr << "dbus format error" << objectPair.value() |
| 1099 | << "\n"; |
| 1100 | break; |
| 1101 | } |
| 1102 | |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 1103 | for (auto& arrayItem : objectPair.value()) |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1104 | { |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 1105 | |
James Feist | d58879a | 2019-09-11 11:26:07 -0700 | [diff] [blame] | 1106 | std::shared_ptr<sdbusplus::asio::dbus_interface> |
| 1107 | objectIface = createInterface( |
| 1108 | objServer, boardName + "/" + itemName, |
| 1109 | "xyz.openbmc_project.Configuration." + |
| 1110 | itemType + "." + objectPair.key() + |
| 1111 | std::to_string(index), |
| 1112 | boardKeyOrig); |
| 1113 | |
James Feist | c6248a5 | 2018-08-14 10:09:45 -0700 | [diff] [blame] | 1114 | populateInterfaceFromJson( |
| 1115 | systemConfiguration, |
| 1116 | jsonPointerPath + "/" + std::to_string(index), |
| 1117 | objectIface, arrayItem, objServer, |
| 1118 | getPermission(objectPair.key())); |
James Feist | bb43d02 | 2018-06-12 15:44:33 -0700 | [diff] [blame] | 1119 | index++; |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 1120 | } |
| 1121 | } |
| 1122 | } |
| 1123 | } |
| 1124 | } |
| 1125 | } |
| 1126 | |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1127 | // reads json files out of the filesystem |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 1128 | bool findJsonFiles(std::list<nlohmann::json>& configurations) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1129 | { |
| 1130 | // find configuration files |
Ed Tanous | 072e25d | 2018-12-16 21:45:20 -0800 | [diff] [blame] | 1131 | std::vector<std::filesystem::path> jsonPaths; |
Johnathan Mantey | 2015f75 | 2019-03-26 15:22:31 -0700 | [diff] [blame] | 1132 | if (!findFiles(std::filesystem::path(configurationDirectory), R"(.*\.json)", |
| 1133 | jsonPaths)) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1134 | { |
| 1135 | std::cerr << "Unable to find any configuration files in " |
James Feist | b4383f4 | 2018-08-06 16:54:10 -0700 | [diff] [blame] | 1136 | << configurationDirectory << "\n"; |
James Feist | 75fdeeb | 2018-02-20 14:26:16 -0800 | [diff] [blame] | 1137 | return false; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1138 | } |
James Feist | b4383f4 | 2018-08-06 16:54:10 -0700 | [diff] [blame] | 1139 | |
| 1140 | std::ifstream schemaStream(std::string(schemaDirectory) + "/" + |
| 1141 | globalSchema); |
| 1142 | if (!schemaStream.good()) |
| 1143 | { |
| 1144 | std::cerr |
| 1145 | << "Cannot open schema file, cannot validate JSON, exiting\n\n"; |
| 1146 | std::exit(EXIT_FAILURE); |
Ed Tanous | 072e25d | 2018-12-16 21:45:20 -0800 | [diff] [blame] | 1147 | return false; |
James Feist | b4383f4 | 2018-08-06 16:54:10 -0700 | [diff] [blame] | 1148 | } |
| 1149 | nlohmann::json schema = nlohmann::json::parse(schemaStream, nullptr, false); |
| 1150 | if (schema.is_discarded()) |
| 1151 | { |
| 1152 | std::cerr |
| 1153 | << "Illegal schema file detected, cannot validate JSON, exiting\n"; |
| 1154 | std::exit(EXIT_FAILURE); |
Ed Tanous | 072e25d | 2018-12-16 21:45:20 -0800 | [diff] [blame] | 1155 | return false; |
James Feist | b4383f4 | 2018-08-06 16:54:10 -0700 | [diff] [blame] | 1156 | } |
| 1157 | |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 1158 | for (auto& jsonPath : jsonPaths) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1159 | { |
| 1160 | std::ifstream jsonStream(jsonPath.c_str()); |
| 1161 | if (!jsonStream.good()) |
| 1162 | { |
| 1163 | std::cerr << "unable to open " << jsonPath.string() << "\n"; |
| 1164 | continue; |
| 1165 | } |
| 1166 | auto data = nlohmann::json::parse(jsonStream, nullptr, false); |
| 1167 | if (data.is_discarded()) |
| 1168 | { |
| 1169 | std::cerr << "syntax error in " << jsonPath.string() << "\n"; |
| 1170 | continue; |
| 1171 | } |
James Feist | 8da9919 | 2019-01-24 08:20:16 -0800 | [diff] [blame] | 1172 | /* |
| 1173 | * todo(james): reenable this once less things are in flight |
| 1174 | * |
James Feist | b4383f4 | 2018-08-06 16:54:10 -0700 | [diff] [blame] | 1175 | if (!validateJson(schema, data)) |
| 1176 | { |
| 1177 | std::cerr << "Error validating " << jsonPath.string() << "\n"; |
| 1178 | continue; |
| 1179 | } |
James Feist | 8da9919 | 2019-01-24 08:20:16 -0800 | [diff] [blame] | 1180 | */ |
James Feist | b4383f4 | 2018-08-06 16:54:10 -0700 | [diff] [blame] | 1181 | |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1182 | if (data.type() == nlohmann::json::value_t::array) |
| 1183 | { |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 1184 | for (auto& d : data) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1185 | { |
| 1186 | configurations.emplace_back(d); |
| 1187 | } |
| 1188 | } |
| 1189 | else |
| 1190 | { |
| 1191 | configurations.emplace_back(data); |
| 1192 | } |
| 1193 | } |
Ed Tanous | 072e25d | 2018-12-16 21:45:20 -0800 | [diff] [blame] | 1194 | return true; |
James Feist | 75fdeeb | 2018-02-20 14:26:16 -0800 | [diff] [blame] | 1195 | } |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1196 | |
James Feist | 94219d1 | 2020-03-03 11:52:25 -0800 | [diff] [blame] | 1197 | std::string getRecordName( |
| 1198 | const boost::container::flat_map<std::string, BasicVariantType>& probe, |
| 1199 | const std::string& probeName) |
| 1200 | { |
| 1201 | if (probe.empty()) |
| 1202 | { |
| 1203 | return probeName; |
| 1204 | } |
| 1205 | |
| 1206 | // use an array so alphabetical order from the |
| 1207 | // flat_map is maintained |
| 1208 | auto device = nlohmann::json::array(); |
| 1209 | for (auto& devPair : probe) |
| 1210 | { |
| 1211 | device.push_back(devPair.first); |
| 1212 | std::visit([&device](auto&& v) { device.push_back(v); }, |
| 1213 | devPair.second); |
| 1214 | } |
| 1215 | size_t hash = std::hash<std::string>{}(probeName + device.dump()); |
| 1216 | // hashes are hard to distinguish, use the |
| 1217 | // non-hashed version if we want debug |
| 1218 | if constexpr (DEBUG) |
| 1219 | { |
| 1220 | return probeName + device.dump(); |
| 1221 | } |
| 1222 | else |
| 1223 | { |
| 1224 | return std::to_string(hash); |
| 1225 | } |
| 1226 | } |
| 1227 | |
James Feist | 733f765 | 2019-11-13 14:32:29 -0800 | [diff] [blame] | 1228 | PerformScan::PerformScan( |
| 1229 | nlohmann::json& systemConfiguration, nlohmann::json& missingConfigurations, |
| 1230 | std::list<nlohmann::json>& configurations, |
| 1231 | std::function<void(const DBusProbeObjectT&)>&& callback) : |
| 1232 | _systemConfiguration(systemConfiguration), |
| 1233 | _missingConfigurations(missingConfigurations), |
| 1234 | _configurations(configurations), _callback(std::move(callback)) |
James Feist | 75fdeeb | 2018-02-20 14:26:16 -0800 | [diff] [blame] | 1235 | { |
James Feist | 733f765 | 2019-11-13 14:32:29 -0800 | [diff] [blame] | 1236 | } |
| 1237 | void PerformScan::run() |
| 1238 | { |
| 1239 | boost::container::flat_set<std::string> dbusProbeInterfaces; |
| 1240 | std::vector<std::shared_ptr<PerformProbe>> dbusProbePointers; |
James Feist | 75fdeeb | 2018-02-20 14:26:16 -0800 | [diff] [blame] | 1241 | |
James Feist | 733f765 | 2019-11-13 14:32:29 -0800 | [diff] [blame] | 1242 | for (auto it = _configurations.begin(); it != _configurations.end();) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1243 | { |
James Feist | 733f765 | 2019-11-13 14:32:29 -0800 | [diff] [blame] | 1244 | auto findProbe = it->find("Probe"); |
| 1245 | auto findName = it->find("Name"); |
James Feist | 787c3c3 | 2019-11-07 14:42:58 -0800 | [diff] [blame] | 1246 | |
James Feist | 733f765 | 2019-11-13 14:32:29 -0800 | [diff] [blame] | 1247 | nlohmann::json probeCommand; |
| 1248 | // check for poorly formatted fields, probe must be an array |
| 1249 | if (findProbe == it->end()) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1250 | { |
James Feist | 733f765 | 2019-11-13 14:32:29 -0800 | [diff] [blame] | 1251 | std::cerr << "configuration file missing probe:\n " << *it << "\n"; |
| 1252 | it = _configurations.erase(it); |
| 1253 | continue; |
| 1254 | } |
| 1255 | else if ((*findProbe).type() != nlohmann::json::value_t::array) |
| 1256 | { |
| 1257 | probeCommand = nlohmann::json::array(); |
| 1258 | probeCommand.push_back(*findProbe); |
| 1259 | } |
| 1260 | else |
| 1261 | { |
| 1262 | probeCommand = *findProbe; |
| 1263 | } |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1264 | |
James Feist | 733f765 | 2019-11-13 14:32:29 -0800 | [diff] [blame] | 1265 | if (findName == it->end()) |
| 1266 | { |
| 1267 | std::cerr << "configuration file missing name:\n " << *it << "\n"; |
| 1268 | it = _configurations.erase(it); |
| 1269 | continue; |
| 1270 | } |
| 1271 | std::string probeName = *findName; |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 1272 | |
James Feist | 733f765 | 2019-11-13 14:32:29 -0800 | [diff] [blame] | 1273 | if (std::find(passedProbes.begin(), passedProbes.end(), probeName) != |
| 1274 | passedProbes.end()) |
| 1275 | { |
| 1276 | it = _configurations.erase(it); |
| 1277 | continue; |
| 1278 | } |
| 1279 | nlohmann::json* recordPtr = &(*it); |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 1280 | |
James Feist | 733f765 | 2019-11-13 14:32:29 -0800 | [diff] [blame] | 1281 | // store reference to this to children to makes sure we don't get |
| 1282 | // destroyed too early |
| 1283 | auto thisRef = shared_from_this(); |
| 1284 | auto probePointer = std::make_shared<PerformProbe>( |
| 1285 | probeCommand, thisRef, |
| 1286 | [&, recordPtr, probeName](FoundDeviceT& foundDevices) { |
James Feist | 08a5b17 | 2019-08-28 14:47:47 -0700 | [diff] [blame] | 1287 | _passed = true; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1288 | |
James Feist | 35f5e0e | 2020-03-16 14:02:27 -0700 | [diff] [blame] | 1289 | std::set<nlohmann::json> usedNames; |
James Feist | 733f765 | 2019-11-13 14:32:29 -0800 | [diff] [blame] | 1290 | passedProbes.push_back(probeName); |
James Feist | 94219d1 | 2020-03-03 11:52:25 -0800 | [diff] [blame] | 1291 | std::list<size_t> indexes(foundDevices.size()); |
| 1292 | std::iota(indexes.begin(), indexes.end(), 1); |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1293 | |
James Feist | 35f5e0e | 2020-03-16 14:02:27 -0700 | [diff] [blame] | 1294 | size_t indexIdx = probeName.find("$"); |
| 1295 | bool hasTemplateName = (indexIdx != std::string::npos); |
James Feist | 94219d1 | 2020-03-03 11:52:25 -0800 | [diff] [blame] | 1296 | |
| 1297 | // copy over persisted configurations and make sure we remove |
| 1298 | // indexes that are already used |
| 1299 | for (auto itr = foundDevices.begin(); |
| 1300 | itr != foundDevices.end();) |
James Feist | 08a5b17 | 2019-08-28 14:47:47 -0700 | [diff] [blame] | 1301 | { |
James Feist | 94219d1 | 2020-03-03 11:52:25 -0800 | [diff] [blame] | 1302 | std::string recordName = getRecordName(*itr, probeName); |
James Feist | f1b1414 | 2019-04-10 15:22:09 -0700 | [diff] [blame] | 1303 | |
James Feist | 08a5b17 | 2019-08-28 14:47:47 -0700 | [diff] [blame] | 1304 | auto fromLastJson = lastJson.find(recordName); |
| 1305 | if (fromLastJson != lastJson.end()) |
| 1306 | { |
James Feist | 02d2b93 | 2020-02-06 16:28:48 -0800 | [diff] [blame] | 1307 | auto findExposes = fromLastJson->find("Exposes"); |
| 1308 | // delete nulls from any updates |
| 1309 | if (findExposes != fromLastJson->end()) |
| 1310 | { |
| 1311 | auto copy = nlohmann::json::array(); |
| 1312 | for (auto& expose : *findExposes) |
| 1313 | { |
| 1314 | if (expose.is_null()) |
| 1315 | { |
| 1316 | continue; |
| 1317 | } |
| 1318 | copy.emplace_back(expose); |
| 1319 | } |
| 1320 | *findExposes = copy; |
| 1321 | } |
| 1322 | |
James Feist | 08a5b17 | 2019-08-28 14:47:47 -0700 | [diff] [blame] | 1323 | // keep user changes |
| 1324 | _systemConfiguration[recordName] = *fromLastJson; |
James Feist | 899e17f | 2019-09-13 11:46:29 -0700 | [diff] [blame] | 1325 | _missingConfigurations.erase(recordName); |
James Feist | 94219d1 | 2020-03-03 11:52:25 -0800 | [diff] [blame] | 1326 | itr = foundDevices.erase(itr); |
James Feist | 35f5e0e | 2020-03-16 14:02:27 -0700 | [diff] [blame] | 1327 | if (hasTemplateName) |
James Feist | 94219d1 | 2020-03-03 11:52:25 -0800 | [diff] [blame] | 1328 | { |
| 1329 | auto nameIt = fromLastJson->find("Name"); |
| 1330 | if (nameIt == fromLastJson->end()) |
| 1331 | { |
| 1332 | std::cerr << "Last JSON Illegal\n"; |
| 1333 | continue; |
| 1334 | } |
| 1335 | |
| 1336 | int index = std::stoi( |
| 1337 | nameIt->get<std::string>().substr(indexIdx), |
| 1338 | nullptr, 0); |
James Feist | 35f5e0e | 2020-03-16 14:02:27 -0700 | [diff] [blame] | 1339 | usedNames.insert(nameIt.value()); |
James Feist | 94219d1 | 2020-03-03 11:52:25 -0800 | [diff] [blame] | 1340 | auto usedIt = std::find(indexes.begin(), |
| 1341 | indexes.end(), index); |
| 1342 | |
| 1343 | if (usedIt == indexes.end()) |
| 1344 | { |
| 1345 | continue; // less items now |
| 1346 | } |
| 1347 | indexes.erase(usedIt); |
| 1348 | } |
| 1349 | |
James Feist | 08a5b17 | 2019-08-28 14:47:47 -0700 | [diff] [blame] | 1350 | continue; |
| 1351 | } |
James Feist | 94219d1 | 2020-03-03 11:52:25 -0800 | [diff] [blame] | 1352 | itr++; |
| 1353 | } |
James Feist | 35f5e0e | 2020-03-16 14:02:27 -0700 | [diff] [blame] | 1354 | |
| 1355 | std::optional<std::string> replaceStr; |
| 1356 | |
James Feist | 94219d1 | 2020-03-03 11:52:25 -0800 | [diff] [blame] | 1357 | for (auto& foundDevice : foundDevices) |
| 1358 | { |
| 1359 | nlohmann::json record = *recordPtr; |
| 1360 | std::string recordName = |
| 1361 | getRecordName(foundDevice, probeName); |
| 1362 | size_t foundDeviceIdx = indexes.front(); |
| 1363 | indexes.pop_front(); |
James Feist | f1b1414 | 2019-04-10 15:22:09 -0700 | [diff] [blame] | 1364 | |
James Feist | 35f5e0e | 2020-03-16 14:02:27 -0700 | [diff] [blame] | 1365 | // check name first so we have no duplicate names |
| 1366 | auto getName = record.find("Name"); |
| 1367 | if (getName == record.end()) |
| 1368 | { |
| 1369 | std::cerr << "Record Missing Name! " << record.dump(); |
| 1370 | continue; // this should be impossible at this level |
| 1371 | } |
| 1372 | |
| 1373 | nlohmann::json copyForName = {{"Name", getName.value()}}; |
| 1374 | nlohmann::json::iterator copyIt = copyForName.begin(); |
| 1375 | std::optional<std::string> replaceVal = templateCharReplace( |
| 1376 | copyIt, foundDevice, foundDeviceIdx, replaceStr); |
| 1377 | |
| 1378 | if (!replaceStr && replaceVal) |
| 1379 | { |
| 1380 | if (usedNames.find(copyIt.value()) != usedNames.end()) |
| 1381 | { |
| 1382 | replaceStr = replaceVal; |
| 1383 | copyForName = {{"Name", getName.value()}}; |
| 1384 | copyIt = copyForName.begin(); |
| 1385 | templateCharReplace(copyIt, foundDevice, |
| 1386 | foundDeviceIdx, replaceStr); |
| 1387 | } |
| 1388 | } |
| 1389 | |
| 1390 | if (replaceStr) |
| 1391 | { |
| 1392 | std::cerr << "Duplicates found, replacing " |
| 1393 | << *replaceStr |
| 1394 | << " with found device index.\n Consider " |
| 1395 | "fixing template to not have duplicates\n"; |
| 1396 | } |
James Feist | 08a5b17 | 2019-08-28 14:47:47 -0700 | [diff] [blame] | 1397 | |
| 1398 | for (auto keyPair = record.begin(); keyPair != record.end(); |
| 1399 | keyPair++) |
| 1400 | { |
James Feist | 35f5e0e | 2020-03-16 14:02:27 -0700 | [diff] [blame] | 1401 | if (keyPair.key() == "Name") |
| 1402 | { |
| 1403 | keyPair.value() = copyIt.value(); |
| 1404 | usedNames.insert(copyIt.value()); |
| 1405 | |
| 1406 | continue; // already covered above |
| 1407 | } |
James Feist | 08a5b17 | 2019-08-28 14:47:47 -0700 | [diff] [blame] | 1408 | templateCharReplace(keyPair, foundDevice, |
James Feist | 35f5e0e | 2020-03-16 14:02:27 -0700 | [diff] [blame] | 1409 | foundDeviceIdx, replaceStr); |
James Feist | 08a5b17 | 2019-08-28 14:47:47 -0700 | [diff] [blame] | 1410 | } |
| 1411 | |
James Feist | 35f5e0e | 2020-03-16 14:02:27 -0700 | [diff] [blame] | 1412 | // insert into configuration temporarily to be able to |
| 1413 | // reference ourselves |
| 1414 | |
| 1415 | _systemConfiguration[recordName] = record; |
| 1416 | |
James Feist | 08a5b17 | 2019-08-28 14:47:47 -0700 | [diff] [blame] | 1417 | auto findExpose = record.find("Exposes"); |
| 1418 | if (findExpose == record.end()) |
| 1419 | { |
James Feist | f1b1414 | 2019-04-10 15:22:09 -0700 | [diff] [blame] | 1420 | _systemConfiguration[recordName] = record; |
James Feist | 08a5b17 | 2019-08-28 14:47:47 -0700 | [diff] [blame] | 1421 | continue; |
| 1422 | } |
James Feist | f1b1414 | 2019-04-10 15:22:09 -0700 | [diff] [blame] | 1423 | |
James Feist | 08a5b17 | 2019-08-28 14:47:47 -0700 | [diff] [blame] | 1424 | for (auto& expose : *findExpose) |
| 1425 | { |
| 1426 | for (auto keyPair = expose.begin(); |
| 1427 | keyPair != expose.end(); keyPair++) |
James Feist | f1b1414 | 2019-04-10 15:22:09 -0700 | [diff] [blame] | 1428 | { |
James Feist | 08a5b17 | 2019-08-28 14:47:47 -0700 | [diff] [blame] | 1429 | |
| 1430 | templateCharReplace(keyPair, foundDevice, |
James Feist | 35f5e0e | 2020-03-16 14:02:27 -0700 | [diff] [blame] | 1431 | foundDeviceIdx, replaceStr); |
James Feist | 08a5b17 | 2019-08-28 14:47:47 -0700 | [diff] [blame] | 1432 | |
James Feist | 668bbb1 | 2020-02-05 14:27:26 -0800 | [diff] [blame] | 1433 | bool isBind = |
| 1434 | boost::starts_with(keyPair.key(), "Bind"); |
| 1435 | bool isDisable = keyPair.key() == "DisableNode"; |
| 1436 | |
| 1437 | // special cases |
| 1438 | if (!(isBind || isDisable)) |
James Feist | f1b1414 | 2019-04-10 15:22:09 -0700 | [diff] [blame] | 1439 | { |
James Feist | 668bbb1 | 2020-02-05 14:27:26 -0800 | [diff] [blame] | 1440 | continue; |
| 1441 | } |
| 1442 | |
| 1443 | if (keyPair.value().type() != |
| 1444 | nlohmann::json::value_t::string && |
| 1445 | keyPair.value().type() != |
| 1446 | nlohmann::json::value_t::array) |
| 1447 | { |
| 1448 | std::cerr << "Value is invalid type " |
| 1449 | << keyPair.key() << "\n"; |
| 1450 | continue; |
| 1451 | } |
| 1452 | |
| 1453 | std::vector<std::string> matches; |
| 1454 | if (keyPair.value().type() == |
| 1455 | nlohmann::json::value_t::string) |
| 1456 | { |
| 1457 | matches.emplace_back(keyPair.value()); |
| 1458 | } |
| 1459 | else |
| 1460 | { |
| 1461 | for (const auto& value : keyPair.value()) |
James Feist | f1b1414 | 2019-04-10 15:22:09 -0700 | [diff] [blame] | 1462 | { |
James Feist | 668bbb1 | 2020-02-05 14:27:26 -0800 | [diff] [blame] | 1463 | if (value.type() != |
| 1464 | nlohmann::json::value_t::string) |
| 1465 | { |
| 1466 | std::cerr << "Value is invalid type " |
| 1467 | << value << "\n"; |
| 1468 | break; |
| 1469 | } |
| 1470 | matches.emplace_back(value); |
James Feist | f1b1414 | 2019-04-10 15:22:09 -0700 | [diff] [blame] | 1471 | } |
James Feist | 668bbb1 | 2020-02-05 14:27:26 -0800 | [diff] [blame] | 1472 | } |
James Feist | 08a5b17 | 2019-08-28 14:47:47 -0700 | [diff] [blame] | 1473 | |
James Feist | 668bbb1 | 2020-02-05 14:27:26 -0800 | [diff] [blame] | 1474 | std::set<std::string> foundMatches; |
| 1475 | for (auto& configurationPair : |
| 1476 | _systemConfiguration.items()) |
| 1477 | { |
| 1478 | if (isDisable) |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1479 | { |
James Feist | 668bbb1 | 2020-02-05 14:27:26 -0800 | [diff] [blame] | 1480 | // don't disable ourselves |
| 1481 | if (configurationPair.key() == recordName) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1482 | { |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 1483 | continue; |
| 1484 | } |
James Feist | 668bbb1 | 2020-02-05 14:27:26 -0800 | [diff] [blame] | 1485 | } |
| 1486 | auto configListFind = |
| 1487 | configurationPair.value().find("Exposes"); |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1488 | |
James Feist | 668bbb1 | 2020-02-05 14:27:26 -0800 | [diff] [blame] | 1489 | if (configListFind == |
| 1490 | configurationPair.value().end() || |
| 1491 | configListFind->type() != |
| 1492 | nlohmann::json::value_t::array) |
James Feist | 08a5b17 | 2019-08-28 14:47:47 -0700 | [diff] [blame] | 1493 | { |
James Feist | 668bbb1 | 2020-02-05 14:27:26 -0800 | [diff] [blame] | 1494 | continue; |
James Feist | 08a5b17 | 2019-08-28 14:47:47 -0700 | [diff] [blame] | 1495 | } |
James Feist | 668bbb1 | 2020-02-05 14:27:26 -0800 | [diff] [blame] | 1496 | for (auto& exposedObject : *configListFind) |
| 1497 | { |
| 1498 | auto matchIt = std::find_if( |
| 1499 | matches.begin(), matches.end(), |
| 1500 | [name = (exposedObject)["Name"] |
| 1501 | .get<std::string>()]( |
| 1502 | const std::string& s) { |
| 1503 | return s == name; |
| 1504 | }); |
| 1505 | if (matchIt == matches.end()) |
| 1506 | { |
| 1507 | continue; |
| 1508 | } |
| 1509 | foundMatches.insert(*matchIt); |
| 1510 | |
| 1511 | if (isBind) |
| 1512 | { |
| 1513 | std::string bind = keyPair.key().substr( |
| 1514 | sizeof("Bind") - 1); |
| 1515 | |
| 1516 | exposedObject["Status"] = "okay"; |
| 1517 | expose[bind] = exposedObject; |
| 1518 | } |
| 1519 | else if (isDisable) |
| 1520 | { |
| 1521 | exposedObject["Status"] = "disabled"; |
| 1522 | } |
| 1523 | } |
| 1524 | } |
| 1525 | if (foundMatches.size() != matches.size()) |
| 1526 | { |
| 1527 | std::cerr << "configuration file " |
| 1528 | "dependency error, " |
| 1529 | "could not find " |
| 1530 | << keyPair.key() << " " |
| 1531 | << keyPair.value() << "\n"; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1532 | } |
| 1533 | } |
| 1534 | } |
James Feist | 08a5b17 | 2019-08-28 14:47:47 -0700 | [diff] [blame] | 1535 | // overwrite ourselves with cleaned up version |
| 1536 | _systemConfiguration[recordName] = record; |
James Feist | 899e17f | 2019-09-13 11:46:29 -0700 | [diff] [blame] | 1537 | _missingConfigurations.erase(recordName); |
James Feist | 08a5b17 | 2019-08-28 14:47:47 -0700 | [diff] [blame] | 1538 | } |
| 1539 | }); |
James Feist | 787c3c3 | 2019-11-07 14:42:58 -0800 | [diff] [blame] | 1540 | |
James Feist | 733f765 | 2019-11-13 14:32:29 -0800 | [diff] [blame] | 1541 | // parse out dbus probes by discarding other probe types, store in a |
| 1542 | // map |
| 1543 | for (const std::string& probe : probeCommand) |
| 1544 | { |
| 1545 | bool found = false; |
| 1546 | boost::container::flat_map<const char*, probe_type_codes, |
| 1547 | cmp_str>::const_iterator probeType; |
| 1548 | for (probeType = PROBE_TYPES.begin(); |
| 1549 | probeType != PROBE_TYPES.end(); ++probeType) |
James Feist | 787c3c3 | 2019-11-07 14:42:58 -0800 | [diff] [blame] | 1550 | { |
James Feist | 733f765 | 2019-11-13 14:32:29 -0800 | [diff] [blame] | 1551 | if (probe.find(probeType->first) != std::string::npos) |
James Feist | 787c3c3 | 2019-11-07 14:42:58 -0800 | [diff] [blame] | 1552 | { |
James Feist | 733f765 | 2019-11-13 14:32:29 -0800 | [diff] [blame] | 1553 | found = true; |
| 1554 | break; |
James Feist | 787c3c3 | 2019-11-07 14:42:58 -0800 | [diff] [blame] | 1555 | } |
James Feist | 787c3c3 | 2019-11-07 14:42:58 -0800 | [diff] [blame] | 1556 | } |
James Feist | 733f765 | 2019-11-13 14:32:29 -0800 | [diff] [blame] | 1557 | if (found) |
| 1558 | { |
| 1559 | continue; |
| 1560 | } |
| 1561 | // syntax requires probe before first open brace |
| 1562 | auto findStart = probe.find("("); |
| 1563 | std::string interface = probe.substr(0, findStart); |
| 1564 | dbusProbeInterfaces.emplace(interface); |
| 1565 | dbusProbePointers.emplace_back(probePointer); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1566 | } |
James Feist | 733f765 | 2019-11-13 14:32:29 -0800 | [diff] [blame] | 1567 | it++; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1568 | } |
James Feist | 75fdeeb | 2018-02-20 14:26:16 -0800 | [diff] [blame] | 1569 | |
James Feist | 733f765 | 2019-11-13 14:32:29 -0800 | [diff] [blame] | 1570 | // probe vector stores a shared_ptr to each PerformProbe that cares |
| 1571 | // about a dbus interface |
| 1572 | findDbusObjects(dbusProbePointers, SYSTEM_BUS, dbusProbeInterfaces, |
| 1573 | shared_from_this()); |
| 1574 | } |
| 1575 | |
| 1576 | PerformScan::~PerformScan() |
| 1577 | { |
| 1578 | if (_passed) |
James Feist | 75fdeeb | 2018-02-20 14:26:16 -0800 | [diff] [blame] | 1579 | { |
James Feist | 733f765 | 2019-11-13 14:32:29 -0800 | [diff] [blame] | 1580 | auto nextScan = std::make_shared<PerformScan>( |
| 1581 | _systemConfiguration, _missingConfigurations, _configurations, |
| 1582 | std::move(_callback)); |
| 1583 | nextScan->passedProbes = std::move(passedProbes); |
| 1584 | nextScan->dbusProbeObjects = std::move(dbusProbeObjects); |
| 1585 | nextScan->run(); |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1586 | } |
James Feist | 733f765 | 2019-11-13 14:32:29 -0800 | [diff] [blame] | 1587 | else |
| 1588 | { |
| 1589 | _callback(dbusProbeObjects); |
| 1590 | } |
| 1591 | } |
James Feist | c95cb14 | 2018-02-26 10:41:42 -0800 | [diff] [blame] | 1592 | |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 1593 | void startRemovedTimer(boost::asio::deadline_timer& timer, |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 1594 | nlohmann::json& systemConfiguration) |
| 1595 | { |
| 1596 | static bool scannedPowerOff = false; |
| 1597 | static bool scannedPowerOn = false; |
| 1598 | |
James Feist | fb00f39 | 2019-06-25 14:16:48 -0700 | [diff] [blame] | 1599 | if (systemConfiguration.empty() || lastJson.empty()) |
| 1600 | { |
| 1601 | return; // not ready yet |
| 1602 | } |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 1603 | if (scannedPowerOn) |
| 1604 | { |
| 1605 | return; |
| 1606 | } |
| 1607 | |
| 1608 | if (!isPowerOn() && scannedPowerOff) |
| 1609 | { |
| 1610 | return; |
| 1611 | } |
| 1612 | |
| 1613 | timer.expires_from_now(boost::posix_time::seconds(10)); |
James Feist | 1a99658 | 2019-05-14 15:10:06 -0700 | [diff] [blame] | 1614 | timer.async_wait( |
| 1615 | [&systemConfiguration](const boost::system::error_code& ec) { |
| 1616 | if (ec == boost::asio::error::operation_aborted) |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 1617 | { |
James Feist | 1a99658 | 2019-05-14 15:10:06 -0700 | [diff] [blame] | 1618 | // we were cancelled |
| 1619 | return; |
| 1620 | } |
| 1621 | |
| 1622 | bool powerOff = !isPowerOn(); |
| 1623 | for (const auto& item : lastJson.items()) |
| 1624 | { |
| 1625 | if (systemConfiguration.find(item.key()) == |
| 1626 | systemConfiguration.end()) |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 1627 | { |
James Feist | 1a99658 | 2019-05-14 15:10:06 -0700 | [diff] [blame] | 1628 | bool isDetectedPowerOn = false; |
| 1629 | auto powerState = item.value().find("PowerState"); |
| 1630 | if (powerState != item.value().end()) |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 1631 | { |
James Feist | 1a99658 | 2019-05-14 15:10:06 -0700 | [diff] [blame] | 1632 | auto ptr = powerState->get_ptr<const std::string*>(); |
| 1633 | if (ptr) |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 1634 | { |
James Feist | 1a99658 | 2019-05-14 15:10:06 -0700 | [diff] [blame] | 1635 | if (*ptr == "On" || *ptr == "BiosPost") |
| 1636 | { |
| 1637 | isDetectedPowerOn = true; |
| 1638 | } |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 1639 | } |
| 1640 | } |
James Feist | 1a99658 | 2019-05-14 15:10:06 -0700 | [diff] [blame] | 1641 | if (powerOff && isDetectedPowerOn) |
| 1642 | { |
| 1643 | // power not on yet, don't know if it's there or not |
| 1644 | continue; |
| 1645 | } |
| 1646 | if (!powerOff && scannedPowerOff && isDetectedPowerOn) |
| 1647 | { |
| 1648 | // already logged it when power was off |
| 1649 | continue; |
| 1650 | } |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 1651 | |
James Feist | 1a99658 | 2019-05-14 15:10:06 -0700 | [diff] [blame] | 1652 | logDeviceRemoved(item.value()); |
| 1653 | } |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 1654 | } |
James Feist | 1a99658 | 2019-05-14 15:10:06 -0700 | [diff] [blame] | 1655 | scannedPowerOff = true; |
| 1656 | if (!powerOff) |
| 1657 | { |
| 1658 | scannedPowerOn = true; |
| 1659 | } |
| 1660 | }); |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 1661 | } |
| 1662 | |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1663 | // main properties changed entry |
| 1664 | void propertiesChangedCallback( |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 1665 | std::vector<sdbusplus::bus::match::match>& dbusMatches, |
| 1666 | nlohmann::json& systemConfiguration, |
| 1667 | sdbusplus::asio::object_server& objServer) |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1668 | { |
| 1669 | static boost::asio::deadline_timer timer(io); |
James Feist | 899e17f | 2019-09-13 11:46:29 -0700 | [diff] [blame] | 1670 | static size_t instance = 0; |
| 1671 | instance++; |
| 1672 | size_t count = instance; |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 1673 | |
James Feist | 899e17f | 2019-09-13 11:46:29 -0700 | [diff] [blame] | 1674 | timer.expires_from_now(boost::posix_time::seconds(5)); |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1675 | |
| 1676 | // setup an async wait as we normally get flooded with new requests |
James Feist | 02d2b93 | 2020-02-06 16:28:48 -0800 | [diff] [blame] | 1677 | timer.async_wait([&systemConfiguration, &dbusMatches, &objServer, |
James Feist | 899e17f | 2019-09-13 11:46:29 -0700 | [diff] [blame] | 1678 | count](const boost::system::error_code& ec) { |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1679 | if (ec == boost::asio::error::operation_aborted) |
| 1680 | { |
| 1681 | // we were cancelled |
| 1682 | return; |
| 1683 | } |
| 1684 | else if (ec) |
| 1685 | { |
| 1686 | std::cerr << "async wait error " << ec << "\n"; |
| 1687 | return; |
| 1688 | } |
| 1689 | |
| 1690 | nlohmann::json oldConfiguration = systemConfiguration; |
James Feist | 899e17f | 2019-09-13 11:46:29 -0700 | [diff] [blame] | 1691 | auto missingConfigurations = std::make_shared<nlohmann::json>(); |
| 1692 | *missingConfigurations = systemConfiguration; |
| 1693 | |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1694 | std::list<nlohmann::json> configurations; |
| 1695 | if (!findJsonFiles(configurations)) |
| 1696 | { |
| 1697 | std::cerr << "cannot find json files\n"; |
| 1698 | return; |
| 1699 | } |
| 1700 | |
| 1701 | auto perfScan = std::make_shared<PerformScan>( |
James Feist | 899e17f | 2019-09-13 11:46:29 -0700 | [diff] [blame] | 1702 | systemConfiguration, *missingConfigurations, configurations, |
James Feist | 02d2b93 | 2020-02-06 16:28:48 -0800 | [diff] [blame] | 1703 | [&systemConfiguration, &objServer, &dbusMatches, count, |
James Feist | 733f765 | 2019-11-13 14:32:29 -0800 | [diff] [blame] | 1704 | oldConfiguration, |
| 1705 | missingConfigurations](const DBusProbeObjectT& dbusProbeObjects) { |
James Feist | 899e17f | 2019-09-13 11:46:29 -0700 | [diff] [blame] | 1706 | // this is something that since ac has been applied to the bmc |
| 1707 | // we saw, and we no longer see it |
| 1708 | bool powerOff = !isPowerOn(); |
| 1709 | for (const auto& item : missingConfigurations->items()) |
| 1710 | { |
| 1711 | bool isDetectedPowerOn = false; |
| 1712 | auto powerState = item.value().find("PowerState"); |
| 1713 | if (powerState != item.value().end()) |
| 1714 | { |
| 1715 | auto ptr = powerState->get_ptr<const std::string*>(); |
| 1716 | if (ptr) |
| 1717 | { |
| 1718 | if (*ptr == "On" || *ptr == "BiosPost") |
| 1719 | { |
| 1720 | isDetectedPowerOn = true; |
| 1721 | } |
| 1722 | } |
| 1723 | } |
| 1724 | if (powerOff && isDetectedPowerOn) |
| 1725 | { |
| 1726 | // power not on yet, don't know if it's there or not |
| 1727 | continue; |
| 1728 | } |
| 1729 | std::string name = item.value()["Name"].get<std::string>(); |
James Feist | 02d2b93 | 2020-02-06 16:28:48 -0800 | [diff] [blame] | 1730 | std::vector<std::weak_ptr<sdbusplus::asio::dbus_interface>>& |
James Feist | 899e17f | 2019-09-13 11:46:29 -0700 | [diff] [blame] | 1731 | ifaces = inventory[name]; |
| 1732 | for (auto& iface : ifaces) |
| 1733 | { |
James Feist | 02d2b93 | 2020-02-06 16:28:48 -0800 | [diff] [blame] | 1734 | auto sharedPtr = iface.lock(); |
| 1735 | if (!sharedPtr) |
| 1736 | { |
| 1737 | continue; // was already deleted elsewhere |
| 1738 | } |
| 1739 | objServer.remove_interface(sharedPtr); |
James Feist | 899e17f | 2019-09-13 11:46:29 -0700 | [diff] [blame] | 1740 | } |
| 1741 | ifaces.clear(); |
| 1742 | systemConfiguration.erase(item.key()); |
| 1743 | logDeviceRemoved(item.value()); |
| 1744 | } |
| 1745 | |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1746 | nlohmann::json newConfiguration = systemConfiguration; |
James Feist | 4131aea | 2018-03-09 09:47:30 -0800 | [diff] [blame] | 1747 | for (auto it = newConfiguration.begin(); |
| 1748 | it != newConfiguration.end();) |
| 1749 | { |
| 1750 | auto findKey = oldConfiguration.find(it.key()); |
| 1751 | if (findKey != oldConfiguration.end()) |
| 1752 | { |
| 1753 | it = newConfiguration.erase(it); |
| 1754 | } |
| 1755 | else |
| 1756 | { |
| 1757 | it++; |
| 1758 | } |
| 1759 | } |
James Feist | 899e17f | 2019-09-13 11:46:29 -0700 | [diff] [blame] | 1760 | for (const auto& item : newConfiguration.items()) |
| 1761 | { |
| 1762 | logDeviceAdded(item.value()); |
| 1763 | } |
| 1764 | |
James Feist | 02d2b93 | 2020-02-06 16:28:48 -0800 | [diff] [blame] | 1765 | registerCallbacks(dbusMatches, systemConfiguration, objServer, |
| 1766 | dbusProbeObjects); |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1767 | io.post([&, newConfiguration]() { |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1768 | loadOverlays(newConfiguration); |
James Feist | ce4367c | 2018-10-16 09:19:57 -0700 | [diff] [blame] | 1769 | |
James Feist | bb43d02 | 2018-06-12 15:44:33 -0700 | [diff] [blame] | 1770 | io.post([&]() { |
| 1771 | if (!writeJsonFiles(systemConfiguration)) |
| 1772 | { |
| 1773 | std::cerr << "Error writing json files\n"; |
| 1774 | } |
| 1775 | }); |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1776 | io.post([&, newConfiguration]() { |
James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 1777 | postToDbus(newConfiguration, systemConfiguration, |
| 1778 | objServer); |
James Feist | 899e17f | 2019-09-13 11:46:29 -0700 | [diff] [blame] | 1779 | if (count != instance) |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 1780 | { |
James Feist | 899e17f | 2019-09-13 11:46:29 -0700 | [diff] [blame] | 1781 | return; |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 1782 | } |
James Feist | 899e17f | 2019-09-13 11:46:29 -0700 | [diff] [blame] | 1783 | startRemovedTimer(timer, systemConfiguration); |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1784 | }); |
| 1785 | }); |
| 1786 | }); |
| 1787 | perfScan->run(); |
| 1788 | }); |
James Feist | 75fdeeb | 2018-02-20 14:26:16 -0800 | [diff] [blame] | 1789 | } |
| 1790 | |
James Feist | 02d2b93 | 2020-02-06 16:28:48 -0800 | [diff] [blame] | 1791 | void registerCallbacks(std::vector<sdbusplus::bus::match::match>& dbusMatches, |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 1792 | nlohmann::json& systemConfiguration, |
James Feist | 733f765 | 2019-11-13 14:32:29 -0800 | [diff] [blame] | 1793 | sdbusplus::asio::object_server& objServer, |
| 1794 | const DBusProbeObjectT& dbusProbeObjects) |
James Feist | 75fdeeb | 2018-02-20 14:26:16 -0800 | [diff] [blame] | 1795 | { |
| 1796 | static boost::container::flat_set<std::string> watchedObjects; |
| 1797 | |
James Feist | 733f765 | 2019-11-13 14:32:29 -0800 | [diff] [blame] | 1798 | for (const auto& objectMap : dbusProbeObjects) |
James Feist | 75fdeeb | 2018-02-20 14:26:16 -0800 | [diff] [blame] | 1799 | { |
James Feist | 787c3c3 | 2019-11-07 14:42:58 -0800 | [diff] [blame] | 1800 | auto [_, inserted] = watchedObjects.insert(objectMap.first); |
| 1801 | if (!inserted) |
James Feist | 75fdeeb | 2018-02-20 14:26:16 -0800 | [diff] [blame] | 1802 | { |
| 1803 | continue; |
| 1804 | } |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1805 | std::function<void(sdbusplus::message::message & message)> |
| 1806 | eventHandler = |
James Feist | 75fdeeb | 2018-02-20 14:26:16 -0800 | [diff] [blame] | 1807 | |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 1808 | [&](sdbusplus::message::message&) { |
James Feist | 02d2b93 | 2020-02-06 16:28:48 -0800 | [diff] [blame] | 1809 | propertiesChangedCallback(dbusMatches, systemConfiguration, |
| 1810 | objServer); |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1811 | }; |
| 1812 | |
| 1813 | sdbusplus::bus::match::match match( |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 1814 | static_cast<sdbusplus::bus::bus&>(*SYSTEM_BUS), |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1815 | "type='signal',member='PropertiesChanged',arg0='" + |
| 1816 | objectMap.first + "'", |
| 1817 | eventHandler); |
| 1818 | dbusMatches.emplace_back(std::move(match)); |
James Feist | 75fdeeb | 2018-02-20 14:26:16 -0800 | [diff] [blame] | 1819 | } |
| 1820 | } |
| 1821 | |
James Feist | 9813279 | 2019-07-09 13:29:09 -0700 | [diff] [blame] | 1822 | int main() |
James Feist | 75fdeeb | 2018-02-20 14:26:16 -0800 | [diff] [blame] | 1823 | { |
| 1824 | // setup connection to dbus |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1825 | SYSTEM_BUS = std::make_shared<sdbusplus::asio::connection>(io); |
James Feist | 75fdeeb | 2018-02-20 14:26:16 -0800 | [diff] [blame] | 1826 | SYSTEM_BUS->request_name("xyz.openbmc_project.EntityManager"); |
James Feist | 4131aea | 2018-03-09 09:47:30 -0800 | [diff] [blame] | 1827 | |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1828 | sdbusplus::asio::object_server objServer(SYSTEM_BUS); |
James Feist | fd1264a | 2018-05-03 12:10:00 -0700 | [diff] [blame] | 1829 | |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1830 | std::shared_ptr<sdbusplus::asio::dbus_interface> entityIface = |
| 1831 | objServer.add_interface("/xyz/openbmc_project/EntityManager", |
| 1832 | "xyz.openbmc_project.EntityManager"); |
James Feist | fd1264a | 2018-05-03 12:10:00 -0700 | [diff] [blame] | 1833 | |
James Feist | 4131aea | 2018-03-09 09:47:30 -0800 | [diff] [blame] | 1834 | // to keep reference to the match / filter objects so they don't get |
| 1835 | // destroyed |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1836 | std::vector<sdbusplus::bus::match::match> dbusMatches; |
| 1837 | |
| 1838 | nlohmann::json systemConfiguration = nlohmann::json::object(); |
| 1839 | |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1840 | io.post([&]() { |
James Feist | 02d2b93 | 2020-02-06 16:28:48 -0800 | [diff] [blame] | 1841 | propertiesChangedCallback(dbusMatches, systemConfiguration, objServer); |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1842 | }); |
James Feist | 4131aea | 2018-03-09 09:47:30 -0800 | [diff] [blame] | 1843 | |
James Feist | fd1264a | 2018-05-03 12:10:00 -0700 | [diff] [blame] | 1844 | entityIface->register_method("ReScan", [&]() { |
James Feist | 02d2b93 | 2020-02-06 16:28:48 -0800 | [diff] [blame] | 1845 | propertiesChangedCallback(dbusMatches, systemConfiguration, objServer); |
James Feist | 75fdeeb | 2018-02-20 14:26:16 -0800 | [diff] [blame] | 1846 | }); |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 1847 | entityIface->initialize(); |
| 1848 | |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 1849 | if (fwVersionIsSame()) |
| 1850 | { |
| 1851 | if (std::filesystem::is_regular_file(currentConfiguration)) |
| 1852 | { |
| 1853 | // this file could just be deleted, but it's nice for debug |
| 1854 | std::filesystem::create_directory(tempConfigDir); |
| 1855 | std::filesystem::remove(lastConfiguration); |
| 1856 | std::filesystem::copy(currentConfiguration, lastConfiguration); |
| 1857 | std::filesystem::remove(currentConfiguration); |
| 1858 | |
| 1859 | std::ifstream jsonStream(lastConfiguration); |
| 1860 | if (jsonStream.good()) |
| 1861 | { |
| 1862 | auto data = nlohmann::json::parse(jsonStream, nullptr, false); |
| 1863 | if (data.is_discarded()) |
| 1864 | { |
| 1865 | std::cerr << "syntax error in " << lastConfiguration |
| 1866 | << "\n"; |
| 1867 | } |
| 1868 | else |
| 1869 | { |
| 1870 | lastJson = std::move(data); |
| 1871 | } |
| 1872 | } |
| 1873 | else |
| 1874 | { |
| 1875 | std::cerr << "unable to open " << lastConfiguration << "\n"; |
| 1876 | } |
| 1877 | } |
| 1878 | } |
| 1879 | else |
| 1880 | { |
| 1881 | // not an error, just logging at this level to make it in the journal |
| 1882 | std::cerr << "Clearing previous configuration\n"; |
| 1883 | std::filesystem::remove(currentConfiguration); |
| 1884 | } |
| 1885 | |
| 1886 | // some boards only show up after power is on, we want to not say they are |
| 1887 | // removed until the same state happens |
| 1888 | setupPowerMatch(SYSTEM_BUS); |
| 1889 | |
James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 1890 | io.run(); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1891 | |
| 1892 | return 0; |
James Feist | 75fdeeb | 2018-02-20 14:26:16 -0800 | [diff] [blame] | 1893 | } |