| Alexander Hansen | 90e4f05 | 2025-10-10 22:58:01 +0200 | [diff] [blame] | 1 | #include "log_device_inventory.hpp" |
| 2 | |
| Alexander Hansen | f57a259 | 2025-06-27 15:07:07 +0200 | [diff] [blame] | 3 | #include "../utils.hpp" |
| 4 | |
| 5 | #include <systemd/sd-journal.h> |
| 6 | |
| Alexander Hansen | f57a259 | 2025-06-27 15:07:07 +0200 | [diff] [blame] | 7 | #include <nlohmann/json.hpp> |
| Alexander Hansen | c58af0b | 2025-09-12 15:47:19 +0200 | [diff] [blame] | 8 | #include <xyz/openbmc_project/Inventory/Decorator/Asset/common.hpp> |
| Alexander Hansen | f57a259 | 2025-06-27 15:07:07 +0200 | [diff] [blame] | 9 | |
| Ed Tanous | dbf95b2 | 2025-10-13 11:38:35 -0700 | [diff] [blame] | 10 | #include <flat_map> |
| Alexander Hansen | f57a259 | 2025-06-27 15:07:07 +0200 | [diff] [blame] | 11 | #include <string> |
| 12 | |
| Alexander Hansen | 90e4f05 | 2025-10-10 22:58:01 +0200 | [diff] [blame] | 13 | static void setStringIfFound(std::string& value, const std::string& key, |
| 14 | const nlohmann::json& record, bool dump = false) |
| Alexander Hansen | 71fbff7 | 2025-10-09 18:06:12 +0200 | [diff] [blame] | 15 | { |
| Alexander Hansen | 90e4f05 | 2025-10-10 22:58:01 +0200 | [diff] [blame] | 16 | const nlohmann::json::const_iterator find = record.find(key); |
| Alexander Hansen | 71fbff7 | 2025-10-09 18:06:12 +0200 | [diff] [blame] | 17 | |
| Alexander Hansen | 90e4f05 | 2025-10-10 22:58:01 +0200 | [diff] [blame] | 18 | if (find == record.end()) |
| Alexander Hansen | 71fbff7 | 2025-10-09 18:06:12 +0200 | [diff] [blame] | 19 | { |
| Alexander Hansen | 90e4f05 | 2025-10-10 22:58:01 +0200 | [diff] [blame] | 20 | return; |
| Alexander Hansen | 71fbff7 | 2025-10-09 18:06:12 +0200 | [diff] [blame] | 21 | } |
| Alexander Hansen | 90e4f05 | 2025-10-10 22:58:01 +0200 | [diff] [blame] | 22 | |
| 23 | const std::string* foundValue = find->get_ptr<const std::string*>(); |
| 24 | if (foundValue != nullptr) |
| 25 | { |
| 26 | value = *foundValue; |
| 27 | } |
| 28 | else if (dump) |
| 29 | { |
| 30 | value = find->dump(); |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | InvAddRemoveInfo queryInvInfo(const nlohmann::json& record) |
| 35 | { |
| 36 | InvAddRemoveInfo ret; |
| 37 | |
| 38 | setStringIfFound(ret.type, "Type", record); |
| 39 | setStringIfFound(ret.name, "Name", record); |
| 40 | |
| 41 | const nlohmann::json::const_iterator findAsset = record.find( |
| 42 | sdbusplus::common::xyz::openbmc_project::inventory::decorator::Asset:: |
| 43 | interface); |
| 44 | |
| Alexander Hansen | 71fbff7 | 2025-10-09 18:06:12 +0200 | [diff] [blame] | 45 | if (findAsset != record.end()) |
| 46 | { |
| Alexander Hansen | 90e4f05 | 2025-10-10 22:58:01 +0200 | [diff] [blame] | 47 | setStringIfFound(ret.model, "Model", *findAsset); |
| 48 | setStringIfFound(ret.sn, "SerialNumber", *findAsset, true); |
| Alexander Hansen | 71fbff7 | 2025-10-09 18:06:12 +0200 | [diff] [blame] | 49 | } |
| 50 | |
| Alexander Hansen | 90e4f05 | 2025-10-10 22:58:01 +0200 | [diff] [blame] | 51 | return ret; |
| Alexander Hansen | 71fbff7 | 2025-10-09 18:06:12 +0200 | [diff] [blame] | 52 | } |
| 53 | |
| Alexander Hansen | f57a259 | 2025-06-27 15:07:07 +0200 | [diff] [blame] | 54 | void logDeviceAdded(const nlohmann::json& record) |
| 55 | { |
| Alexander Hansen | e665185 | 2025-01-21 16:22:05 +0100 | [diff] [blame] | 56 | if (!EM_CACHE_CONFIGURATION) |
| 57 | { |
| 58 | return; |
| 59 | } |
| Alexander Hansen | f57a259 | 2025-06-27 15:07:07 +0200 | [diff] [blame] | 60 | if (!deviceHasLogging(record)) |
| 61 | { |
| 62 | return; |
| 63 | } |
| Alexander Hansen | f57a259 | 2025-06-27 15:07:07 +0200 | [diff] [blame] | 64 | |
| Alexander Hansen | 90e4f05 | 2025-10-10 22:58:01 +0200 | [diff] [blame] | 65 | const InvAddRemoveInfo info = queryInvInfo(record); |
| Alexander Hansen | f57a259 | 2025-06-27 15:07:07 +0200 | [diff] [blame] | 66 | |
| Alexander Hansen | 71fbff7 | 2025-10-09 18:06:12 +0200 | [diff] [blame] | 67 | sd_journal_send( |
| 68 | "MESSAGE=Inventory Added: %s", info.name.c_str(), "PRIORITY=%i", |
| 69 | LOG_INFO, "REDFISH_MESSAGE_ID=%s", "OpenBMC.0.1.InventoryAdded", |
| 70 | "REDFISH_MESSAGE_ARGS=%s,%s,%s", info.model.c_str(), info.type.c_str(), |
| 71 | info.sn.c_str(), "NAME=%s", info.name.c_str(), NULL); |
| Alexander Hansen | f57a259 | 2025-06-27 15:07:07 +0200 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | void logDeviceRemoved(const nlohmann::json& record) |
| 75 | { |
| 76 | if (!deviceHasLogging(record)) |
| 77 | { |
| 78 | return; |
| 79 | } |
| Alexander Hansen | f57a259 | 2025-06-27 15:07:07 +0200 | [diff] [blame] | 80 | |
| Alexander Hansen | 90e4f05 | 2025-10-10 22:58:01 +0200 | [diff] [blame] | 81 | const InvAddRemoveInfo info = queryInvInfo(record); |
| Alexander Hansen | f57a259 | 2025-06-27 15:07:07 +0200 | [diff] [blame] | 82 | |
| Alexander Hansen | 71fbff7 | 2025-10-09 18:06:12 +0200 | [diff] [blame] | 83 | sd_journal_send( |
| 84 | "MESSAGE=Inventory Removed: %s", info.name.c_str(), "PRIORITY=%i", |
| 85 | LOG_INFO, "REDFISH_MESSAGE_ID=%s", "OpenBMC.0.1.InventoryRemoved", |
| 86 | "REDFISH_MESSAGE_ARGS=%s,%s,%s", info.model.c_str(), info.type.c_str(), |
| 87 | info.sn.c_str(), "NAME=%s", info.name.c_str(), NULL); |
| Alexander Hansen | f57a259 | 2025-06-27 15:07:07 +0200 | [diff] [blame] | 88 | } |