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