summaryrefslogtreecommitdiff
path: root/meta-openbmc-mods/meta-common/recipes-phosphor/configuration/entity-manager/0002-Entity-manager-Add-support-to-update-assetTag.patch
blob: 88e86f68c1f48df6f6d80846b6f504b268162402 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
From 1e8b3e0fe4ce9aa87a35f6c3ba6e506873d5455c Mon Sep 17 00:00:00 2001
From: mansijos <mansi.joshi@intel.com>
Date: Fri, 12 Feb 2021 11:26:57 +0530
Subject: [PATCH] Entity-manager: Add support to update assetTag

Asset tag is an updateable property from User level interface like
Redfish. User-level interface will update Asset tag in entity-manager,
which will further update the needed FRU interface property exposed.

Tested:
Successfully updated in assetTag interface as well as in fru interface
while using set-property and using redfish as well.
The new value is preserved after BMC resets.

Change-Id: Iab8b24344d1a27486ceafe21aa2b4496706a2944
Signed-off-by: mansijos <mansi.joshi@intel.com>
---
 include/EntityManager.hpp |  15 ++--
 src/EntityManager.cpp     | 148 ++++++++++++++++++++++++++++++++------
 2 files changed, 135 insertions(+), 28 deletions(-)

diff --git a/include/EntityManager.hpp b/include/EntityManager.hpp
index 5867556..a4e9922 100644
--- a/include/EntityManager.hpp
+++ b/include/EntityManager.hpp
@@ -29,13 +29,16 @@
 #include <list>
 #include <string>
 
-using DBusProbeObjectT = boost::container::flat_map<
-    std::string,
-    std::vector<boost::container::flat_map<std::string, BasicVariantType>>>;
-
 using FoundDeviceT =
     std::vector<boost::container::flat_map<std::string, BasicVariantType>>;
 
+using FoundDeviceInfoT = std::vector<
+    std::tuple<boost::container::flat_map<std::string, BasicVariantType>,
+               std::string, std::string, std::string>>;
+
+using DBusProbeObjectT =
+    boost::container::flat_map<std::string, FoundDeviceInfoT>;
+
 struct PerformScan : std::enable_shared_from_this<PerformScan>
 {
 
@@ -62,12 +65,12 @@ struct PerformProbe : std::enable_shared_from_this<PerformProbe>
 {
     PerformProbe(const std::vector<std::string>& probeCommand,
                  std::shared_ptr<PerformScan>& scanPtr,
-                 std::function<void(FoundDeviceT&)>&& callback);
+                 std::function<void(FoundDeviceInfoT&)>&& callback);
     virtual ~PerformProbe();
 
     std::vector<std::string> _probeCommand;
     std::shared_ptr<PerformScan> scan;
-    std::function<void(FoundDeviceT&)> _callback;
+    std::function<void(FoundDeviceInfoT&)> _callback;
 };
 
 inline void logDeviceAdded(const nlohmann::json& record)
diff --git a/src/EntityManager.cpp b/src/EntityManager.cpp
index aa1df20..cd73675 100644
--- a/src/EntityManager.cpp
+++ b/src/EntityManager.cpp
@@ -47,9 +47,14 @@ constexpr const char* lastConfiguration = "/tmp/configuration/last.json";
 constexpr const char* currentConfiguration = "/var/configuration/system.json";
 constexpr const char* globalSchema = "global.json";
 constexpr const int32_t MAX_MAPPER_DEPTH = 0;
+constexpr const char* foundObject = "FoundProbe";
 
 constexpr const bool DEBUG = false;
 
+using foundProbeData = std::map<std::string, std::string>;
+static foundProbeData foundData;
+static std::map<std::string, foundProbeData> mapFoundData;
+
 struct cmp_str
 {
     bool operator()(const char* a, const char* b) const
@@ -169,7 +174,10 @@ void getInterfaces(
                 return;
             }
 
-            scan->dbusProbeObjects[std::get<2>(call)].emplace_back(resp);
+            // Save the dbus info along with device info
+            scan->dbusProbeObjects[std::get<2>(call)].emplace_back(
+                std::make_tuple(resp, std::get<0>(call), std::get<1>(call),
+                                std::get<2>(call)));
         },
         std::get<0>(call), std::get<1>(call), "org.freedesktop.DBus.Properties",
         "GetAll", std::get<2>(call));
@@ -274,11 +282,10 @@ void findDbusObjects(std::vector<std::shared_ptr<PerformProbe>>&& probeVector,
 // probes dbus interface dictionary for a key with a value that matches a regex
 bool probeDbus(const std::string& interface,
                const std::map<std::string, nlohmann::json>& matches,
-               FoundDeviceT& devices, std::shared_ptr<PerformScan> scan,
+               FoundDeviceInfoT& devices, std::shared_ptr<PerformScan> scan,
                bool& foundProbe)
 {
-    std::vector<boost::container::flat_map<std::string, BasicVariantType>>&
-        dbusObject = scan->dbusProbeObjects[interface];
+    FoundDeviceInfoT& dbusObject = scan->dbusProbeObjects[interface];
     if (dbusObject.empty())
     {
         foundProbe = false;
@@ -287,8 +294,9 @@ bool probeDbus(const std::string& interface,
     foundProbe = true;
 
     bool foundMatch = false;
-    for (auto& device : dbusObject)
+    for (auto& deviceInfo : dbusObject)
     {
+        auto& device = std::get<0>(deviceInfo);
         bool deviceMatches = true;
         for (auto& match : matches)
         {
@@ -305,7 +313,7 @@ bool probeDbus(const std::string& interface,
         }
         if (deviceMatches)
         {
-            devices.emplace_back(device);
+            devices.emplace_back(deviceInfo);
             foundMatch = true;
             deviceMatches = false; // for next iteration
         }
@@ -315,11 +323,8 @@ bool probeDbus(const std::string& interface,
 
 // default probe entry point, iterates a list looking for specific types to
 // call specific probe functions
-bool probe(
-    const std::vector<std::string>& probeCommand,
-    std::shared_ptr<PerformScan> scan,
-    std::vector<boost::container::flat_map<std::string, BasicVariantType>>&
-        foundDevs)
+bool probe(const std::vector<std::string>& probeCommand,
+           std::shared_ptr<PerformScan> scan, FoundDeviceInfoT& foundDevs)
 {
     const static std::regex command(R"(\((.*)\))");
     std::smatch match;
@@ -449,8 +454,9 @@ bool probe(
     // probe passed, but empty device
     if (ret && foundDevs.size() == 0)
     {
-        foundDevs.emplace_back(
-            boost::container::flat_map<std::string, BasicVariantType>{});
+        foundDevs.emplace_back(std::make_tuple(
+            boost::container::flat_map<std::string, BasicVariantType>{},
+            std::string{}, std::string{}, std::string{}));
     }
     if (matchOne && ret)
     {
@@ -465,13 +471,13 @@ bool probe(
 
 PerformProbe::PerformProbe(const std::vector<std::string>& probeCommand,
                            std::shared_ptr<PerformScan>& scanPtr,
-                           std::function<void(FoundDeviceT&)>&& callback) :
+                           std::function<void(FoundDeviceInfoT&)>&& callback) :
     _probeCommand(probeCommand),
     scan(scanPtr), _callback(std::move(callback))
 {}
 PerformProbe::~PerformProbe()
 {
-    FoundDeviceT foundDevs;
+    FoundDeviceInfoT foundDevs;
     if (probe(_probeCommand, scan, foundDevs))
     {
         _callback(foundDevs);
@@ -556,6 +562,47 @@ void addArrayToDbus(const std::string& name, const nlohmann::json& array,
     }
 }
 
+template <typename PropertyType>
+bool persistAssetTag(const PropertyType& newVal,
+                     const std::string& jsonPointerString)
+{
+    std::size_t found = jsonPointerString.find_last_of("/\\");
+    std::string jsonPointerPath = jsonPointerString.substr(0, found);
+
+    auto it = mapFoundData.find(jsonPointerPath);
+    if (it == mapFoundData.end())
+    {
+        std::cerr << "Error in finding jsonPointerPath in mapFoundData"
+                  << "\n";
+        return false;
+    }
+
+    foundProbeData& tmpMap = it->second;
+    auto foundConn = tmpMap.find("foundConn");
+    auto foundPath = tmpMap.find("foundPath");
+    auto foundIntf = tmpMap.find("foundIntf");
+    if (foundConn == tmpMap.end() || foundPath == tmpMap.end() ||
+        foundIntf == tmpMap.end())
+    {
+        std::cerr << "No prob object data is avaliable in foundProbeData"
+                  << "\n";
+        return false;
+    }
+
+    SYSTEM_BUS->async_method_call(
+        [](const boost::system::error_code& ec) {
+            if (ec)
+            {
+                std::cerr << "Error setting AssetTag in FRU interface " << ec
+                          << "\n";
+            }
+        },
+        foundConn->second, foundPath->second, "org.freedesktop.DBus.Properties",
+        "Set", foundIntf->second, "PRODUCT_ASSET_TAG",
+        std::variant<PropertyType>(newVal));
+    return true;
+}
+
 template <typename PropertyType>
 void addProperty(const std::string& propertyName, const PropertyType& value,
                  sdbusplus::asio::dbus_interface* iface,
@@ -568,11 +615,21 @@ void addProperty(const std::string& propertyName, const PropertyType& value,
         iface->register_property(propertyName, value);
         return;
     }
+
     iface->register_property(
         propertyName, value,
-        [&systemConfiguration,
+        [propertyName, &systemConfiguration,
          jsonPointerString{std::string(jsonPointerString)}](
             const PropertyType& newVal, PropertyType& val) {
+            if (propertyName == "AssetTag")
+            {
+                if (!persistAssetTag(newVal, jsonPointerString))
+                {
+                    std::cerr << "error setting AssetTag in FRU interface\n";
+                    return -1;
+                }
+            }
+
             val = newVal;
             if (!setJsonFromPointer(jsonPointerString, val,
                                     systemConfiguration))
@@ -970,6 +1027,11 @@ void postToDbus(const nlohmann::json& newConfiguration,
         populateInterfaceFromJson(systemConfiguration, jsonPointerPath,
                                   boardIface, boardValues, objServer);
         jsonPointerPath += "/";
+
+        std::string foundConn;
+        std::string foundPath;
+        std::string foundIntf;
+
         // iterate through board properties
         for (auto& boardField : boardValues.items())
         {
@@ -979,9 +1041,32 @@ void postToDbus(const nlohmann::json& newConfiguration,
                     createInterface(objServer, boardName, boardField.key(),
                                     boardKeyOrig);
 
-                populateInterfaceFromJson(systemConfiguration,
-                                          jsonPointerPath + boardField.key(),
-                                          iface, boardField.value(), objServer);
+                if (boardField.key() == "FoundProbe")
+                {
+                    foundConn = boardField.value()["Connection"];
+                    foundPath = boardField.value()["Path"];
+                    foundIntf = boardField.value()["Interface"];
+                }
+                if (boardField.key() ==
+                    "xyz.openbmc_project.Inventory.Decorator.AssetTag")
+                {
+                    foundData["foundConn"] = foundConn;
+                    foundData["foundPath"] = foundPath;
+                    foundData["foundIntf"] = foundIntf;
+                    mapFoundData[jsonPointerPath + boardField.key()] =
+                        foundData;
+
+                    populateInterfaceFromJson(
+                        systemConfiguration, jsonPointerPath + boardField.key(),
+                        iface, boardField.value(), objServer,
+                        sdbusplus::asio::PropertyPermission::readWrite);
+                }
+                else
+                {
+                    populateInterfaceFromJson(
+                        systemConfiguration, jsonPointerPath + boardField.key(),
+                        iface, boardField.value(), objServer);
+                }
             }
         }
 
@@ -1278,7 +1363,7 @@ void PerformScan::run()
         auto thisRef = shared_from_this();
         auto probePointer = std::make_shared<PerformProbe>(
             probeCommand, thisRef,
-            [&, recordPtr, probeName](FoundDeviceT& foundDevices) {
+            [&, recordPtr, probeName](FoundDeviceInfoT& foundDevices) {
                 _passed = true;
 
                 std::set<nlohmann::json> usedNames;
@@ -1294,7 +1379,8 @@ void PerformScan::run()
                 for (auto itr = foundDevices.begin();
                      itr != foundDevices.end();)
                 {
-                    std::string recordName = getRecordName(*itr, probeName);
+                    std::string recordName =
+                        getRecordName(std::get<0>(*itr), probeName);
 
                     auto fromLastJson = lastJson.find(recordName);
                     if (fromLastJson != lastJson.end())
@@ -1328,6 +1414,15 @@ void PerformScan::run()
                                 continue;
                             }
 
+                            nlohmann::json recordVal = *recordPtr;
+                            // Save the dbus connection, path and interface info
+                            // of the device
+                            recordVal[foundObject]["Connection"] =
+                                std::get<1>(*itr);
+                            recordVal[foundObject]["Path"] = std::get<2>(*itr);
+                            recordVal[foundObject]["Interface"] =
+                                std::get<3>(*itr);
+
                             int index = std::stoi(
                                 nameIt->get<std::string>().substr(indexIdx),
                                 nullptr, 0);
@@ -1349,8 +1444,9 @@ void PerformScan::run()
 
                 std::optional<std::string> replaceStr;
 
-                for (auto& foundDevice : foundDevices)
+                for (auto& foundDeviceInfo : foundDevices)
                 {
+                    auto& foundDevice = std::get<0>(foundDeviceInfo);
                     nlohmann::json record = *recordPtr;
                     std::string recordName =
                         getRecordName(foundDevice, probeName);
@@ -1382,6 +1478,14 @@ void PerformScan::run()
                         }
                     }
 
+                    // Save the dbus connection, path and interface info
+                    // of the device
+                    record[foundObject]["Connection"] =
+                        std::get<1>(foundDeviceInfo);
+                    record[foundObject]["Path"] = std::get<2>(foundDeviceInfo);
+                    record[foundObject]["Interface"] =
+                        std::get<3>(foundDeviceInfo);
+
                     if (replaceStr)
                     {
                         std::cerr << "Duplicates found, replacing "
-- 
2.17.1