summaryrefslogtreecommitdiff
path: root/meta-ibs/meta-common/recipes-phosphor/interfaces/bmcweb/0005-Add-Sensor-Warning-Threshold-Edit.patch
blob: 9326e5bb830dfbfe2007fc630e111e18d5f8c70c (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
From b4e619d9419fbccd818992330630af4920623796 Mon Sep 17 00:00:00 2001
From: "Evgeny Alekseev (IBS Group)" <ealekseev@ibs.sila.ru>
Date: Wed, 10 Aug 2022 15:28:47 +0300
Subject: [PATCH] [*]Add Sensor Warning Threshold Edit

---
 redfish-core/lib/power.hpp   |   6 +-
 redfish-core/lib/sensors.hpp | 150 +++++++++++++++++++++++++++++++++++
 redfish-core/lib/thermal.hpp |   2 +-
 3 files changed, 155 insertions(+), 3 deletions(-)

diff --git a/redfish-core/lib/power.hpp b/redfish-core/lib/power.hpp
index bc18157c..aff60ebf 100644
--- a/redfish-core/lib/power.hpp
+++ b/redfish-core/lib/power.hpp
@@ -22,6 +22,8 @@
 #include <dbus_utility.hpp>
 #include <query.hpp>
 #include <registries/privilege_registry.hpp>
+#include <iostream>
+#include <algorithm>
 
 namespace redfish
 {
@@ -331,11 +333,11 @@ inline void requestRoutesPower(App& app)
             setPowerCapOverride(sensorAsyncResp, *powerCtlCollections);
         }
         if (voltageCollections)
-        {
+        {            
             std::unordered_map<std::string, std::vector<nlohmann::json>>
                 allCollections;
             allCollections.emplace("Voltages", *std::move(voltageCollections));
-            setSensorsOverride(sensorAsyncResp, allCollections);
+            setSensorsWarning(sensorAsyncResp, allCollections);                        
         }
         });
 }
diff --git a/redfish-core/lib/sensors.hpp b/redfish-core/lib/sensors.hpp
index 04b5190f..1601e8ed 100644
--- a/redfish-core/lib/sensors.hpp
+++ b/redfish-core/lib/sensors.hpp
@@ -30,6 +30,8 @@
 #include <cmath>
 #include <iterator>
 #include <map>
+#include <unordered_map>
+#include <string>
 #include <set>
 #include <utility>
 #include <variant>
@@ -2762,6 +2764,7 @@ inline bool
     return false;
 }
 
+
 /**
  * @brief Entry point for overriding sensor values of given sensor
  *
@@ -2769,6 +2772,7 @@ inline bool
  * @param allCollections   Collections extract from sensors' request patch info
  * @param chassisSubNode   Chassis Node for which the query has to happen
  */
+
 inline void setSensorsOverride(
     const std::shared_ptr<SensorsAsyncResp>& sensorAsyncResp,
     std::unordered_map<std::string, std::vector<nlohmann::json>>&
@@ -2898,6 +2902,152 @@ inline void setSensorsOverride(
     // get full sensor list for the given chassisId and cross verify the sensor.
     getChassis(sensorAsyncResp, std::move(getChassisSensorListCb));
 }
+ 
+
+
+/**
+ * @brief Entry point for setting sensor warning level of given sensor
+ *
+ * @param sensorAsyncResp   response object
+ * @param allCollections   Collections extract from sensors' request patch info
+ * @param chassisSubNode   Chassis Node for which the query has to happen
+ */
+inline void setSensorsWarning(
+    const std::shared_ptr<SensorsAsyncResp>& sensorAsyncResp,
+    std::unordered_map<std::string, std::vector<nlohmann::json>>&
+        allCollections)
+{
+    BMCWEB_LOG_INFO << "setSensorsWarning for subNode"
+                    << sensorAsyncResp->chassisSubNode << "\n";
+
+    const char* propertyValueName = nullptr;
+    std::unordered_map<std::string, std::tuple<double, std::string, std::string>> overrideMap;
+    std::string memberId;
+    std::string thresholdName;
+    static const std::unordered_map<std::string, std::string> validThresholdNames = 
+        {{std::string {"LowerThresholdNonCritical"},std::string {"WarningLow"}},
+        {std::string {"UpperThresholdNonCritical"},std::string {"WarningHigh"}}};
+    double value = 0.0;
+    for (auto& collectionItems : allCollections)
+    {
+        
+        for (auto& item : collectionItems.second)
+        {
+            for(auto const &elem: validThresholdNames)
+                {
+                    if(item.contains(elem.first))propertyValueName = elem.first.c_str();
+                }    
+            if (!json_util::readJson(item, sensorAsyncResp->asyncResp->res,
+                                     "MemberId", memberId, propertyValueName,
+                                     value))
+            {
+                return;
+            }
+            if(propertyValueName)
+                {
+                overrideMap.emplace(memberId,
+                                std::make_tuple(value, collectionItems.first, std::string(propertyValueName)));
+                }
+        }
+    }
+    for(auto [key, val] : overrideMap)
+        BMCWEB_LOG_INFO << "Override entry:" << key << " " 
+            << std::get<0>(val) << " " 
+            << std::get<1>(val)<< " " 
+            << std::get<2>(val);
+
+    auto getChassisSensorListCb =
+        [sensorAsyncResp, overrideMap](
+            const std::shared_ptr<std::set<std::string>>& sensorsList) {
+        // Match sensor names in the PATCH request to those managed by the
+        // chassis node
+        const std::shared_ptr<std::set<std::string>> sensorNames =
+            std::make_shared<std::set<std::string>>();
+        for (const auto& item : overrideMap)
+        {
+            const auto& sensor = item.first;
+            if (!findSensorNameUsingSensorPath(sensor, *sensorsList,
+                                               *sensorNames))
+            {
+                BMCWEB_LOG_INFO << "Unable to find memberId " << item.first;
+                messages::resourceNotFound(sensorAsyncResp->asyncResp->res,
+                                           std::get<1>(item.second), item.first);
+                return;
+            }
+        }
+        // Get the connection to which the memberId belongs
+        auto getObjectsWithConnectionCb =
+            [sensorAsyncResp,
+             overrideMap](const std::set<std::string>& /*connections*/,
+                          const std::set<std::pair<std::string, std::string>>&
+                              objectsWithConnection) {
+            if (objectsWithConnection.size() != overrideMap.size())
+            {
+                BMCWEB_LOG_INFO
+                    << "Unable to find all objects with proper connection "
+                    << objectsWithConnection.size() << " requested "
+                    << overrideMap.size() << "\n";
+                messages::resourceNotFound(sensorAsyncResp->asyncResp->res,
+                                           sensorAsyncResp->chassisSubNode ==
+                                                   sensors::node::thermal
+                                               ? "Temperatures"
+                                               : "Voltages",
+                                           "Count");
+                return;
+            }
+            for (const auto& item : objectsWithConnection)
+            {
+                sdbusplus::message::object_path path(item.first);
+                std::string sensorName = path.filename();
+                if (sensorName.empty())
+                {
+                    messages::internalError(sensorAsyncResp->asyncResp->res);
+                    return;
+                }
+
+                const auto& iterator = overrideMap.find(sensorName);
+                if (iterator == overrideMap.end())
+                {
+                    BMCWEB_LOG_INFO << "Unable to find sensor object"
+                                    << item.first << "\n";
+                    messages::internalError(sensorAsyncResp->asyncResp->res);
+                    return;
+                }
+                BMCWEB_LOG_INFO << "Tryong to set warning property: " <<validThresholdNames.at(std::get<2>(iterator->second));
+
+                crow::connections::systemBus->async_method_call(
+                    [sensorAsyncResp](const boost::system::error_code ec) {
+                    if (ec)
+                    {
+                        if (ec.value() ==
+                            boost::system::errc::permission_denied)
+                        {
+                            BMCWEB_LOG_WARNING
+                                << "Manufacturing mode is not Enabled...can't "
+                                   "Override the sensor value. ";
+
+                            messages::insufficientPrivilege(
+                                sensorAsyncResp->asyncResp->res);
+                            return;
+                        }
+                        BMCWEB_LOG_DEBUG
+                            << "setOverrideValueStatus DBUS error: " << ec;
+                        messages::internalError(
+                            sensorAsyncResp->asyncResp->res);
+                    }
+                    },
+                    item.second, item.first, "org.freedesktop.DBus.Properties",
+                    "Set", "xyz.openbmc_project.Sensor.Threshold.Warning", validThresholdNames.at(std::get<2>(iterator->second)),
+                    dbus::utility::DbusVariantType(std::get<0>(iterator->second)));
+            }
+        };
+        // Get object with connection for the given sensor name
+        getObjectsWithConnection(sensorAsyncResp, sensorNames,
+                                 std::move(getObjectsWithConnectionCb));
+    };
+    // get full sensor list for the given chassisId and cross verify the sensor.
+    getChassis(sensorAsyncResp, std::move(getChassisSensorListCb));
+}
 
 /**
  * @brief Retrieves mapping of Redfish URIs to sensor value property to D-Bus
diff --git a/redfish-core/lib/thermal.hpp b/redfish-core/lib/thermal.hpp
index 83f79af3..a44a4791 100644
--- a/redfish-core/lib/thermal.hpp
+++ b/redfish-core/lib/thermal.hpp
@@ -86,7 +86,7 @@ inline void requestRoutesThermal(App& app)
         {
             allCollections.emplace("Fans", *std::move(fanCollections));
         }
-        setSensorsOverride(sensorsAsyncResp, allCollections);
+        setSensorsWarning(sensorsAsyncResp, allCollections);
         });
 }