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