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