/
sensorcommands.cpp
1547 lines (1370 loc) · 51.7 KB
/
sensorcommands.cpp
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
// Copyright (c) 2017 2018 Intel Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
*/
#include <boost/algorithm/string.hpp>
#include <boost/container/flat_map.hpp>
#include <chrono>
#include <cmath>
#include <commandutils.hpp>
#include <iostream>
#include <ipmi_to_redfish_hooks.hpp>
#include <ipmid/api.hpp>
#include <ipmid/utils.hpp>
#include <phosphor-logging/log.hpp>
#include <sdbusplus/bus.hpp>
#include <sdrutils.hpp>
#include <sensorcommands.hpp>
#include <sensorutils.hpp>
#include <storagecommands.hpp>
#include <string>
namespace ipmi
{
using ManagedObjectType =
std::map<sdbusplus::message::object_path,
std::map<std::string, std::map<std::string, DbusVariant>>>;
using SensorMap = std::map<std::string, std::map<std::string, DbusVariant>>;
static constexpr int sensorListUpdatePeriod = 10;
static constexpr int sensorMapUpdatePeriod = 2;
constexpr size_t maxSDRTotalSize =
76; // Largest SDR Record Size (type 01) + SDR Overheader Size
constexpr static const uint32_t noTimestamp = 0xFFFFFFFF;
static uint16_t sdrReservationID;
static uint32_t sdrLastAdd = noTimestamp;
static uint32_t sdrLastRemove = noTimestamp;
SensorSubTree sensorTree;
static boost::container::flat_map<std::string, ManagedObjectType> SensorCache;
// Specify the comparison required to sort and find char* map objects
struct CmpStr
{
bool operator()(const char *a, const char *b) const
{
return std::strcmp(a, b) < 0;
}
};
const static boost::container::flat_map<const char *, SensorUnits, CmpStr>
sensorUnits{{{"temperature", SensorUnits::degreesC},
{"voltage", SensorUnits::volts},
{"current", SensorUnits::amps},
{"fan_tach", SensorUnits::rpm},
{"power", SensorUnits::watts}}};
void registerSensorFunctions() __attribute__((constructor));
static sdbusplus::bus::match::match sensorAdded(
*getSdBus(),
"type='signal',member='InterfacesAdded',arg0path='/xyz/openbmc_project/"
"sensors/'",
[](sdbusplus::message::message &m) {
sensorTree.clear();
sdrLastAdd = std::chrono::duration_cast<std::chrono::seconds>(
std::chrono::system_clock::now().time_since_epoch())
.count();
});
static sdbusplus::bus::match::match sensorRemoved(
*getSdBus(),
"type='signal',member='InterfacesRemoved',arg0path='/xyz/openbmc_project/"
"sensors/'",
[](sdbusplus::message::message &m) {
sensorTree.clear();
sdrLastRemove = std::chrono::duration_cast<std::chrono::seconds>(
std::chrono::system_clock::now().time_since_epoch())
.count();
});
// this keeps track of deassertions for sensor event status command. A
// deasertion can only happen if an assertion was seen first.
static boost::container::flat_map<
std::string, boost::container::flat_map<std::string, std::optional<bool>>>
thresholdDeassertMap;
static sdbusplus::bus::match::match thresholdChanged(
*getSdBus(),
"type='signal',member='PropertiesChanged',interface='org.freedesktop.DBus."
"Properties',arg0namespace='xyz.openbmc_project.Sensor.Threshold'",
[](sdbusplus::message::message &m) {
boost::container::flat_map<std::string, std::variant<bool, double>>
values;
m.read(std::string(), values);
auto findAssert =
std::find_if(values.begin(), values.end(), [](const auto &pair) {
return pair.first.find("Alarm") != std::string::npos;
});
if (findAssert != values.end())
{
auto ptr = std::get_if<bool>(&(findAssert->second));
if (ptr == nullptr)
{
phosphor::logging::log<phosphor::logging::level::ERR>(
"thresholdChanged: Assert non bool");
return;
}
if (*ptr)
{
phosphor::logging::log<phosphor::logging::level::INFO>(
"thresholdChanged: Assert",
phosphor::logging::entry("SENSOR=%s", m.get_path()));
thresholdDeassertMap[m.get_path()][findAssert->first] = *ptr;
}
else
{
auto &value =
thresholdDeassertMap[m.get_path()][findAssert->first];
if (value)
{
phosphor::logging::log<phosphor::logging::level::INFO>(
"thresholdChanged: deassert",
phosphor::logging::entry("SENSOR=%s", m.get_path()));
value = *ptr;
}
}
}
});
static void getSensorMaxMin(const SensorMap &sensorMap, double &max,
double &min)
{
max = 127;
min = -128;
auto sensorObject = sensorMap.find("xyz.openbmc_project.Sensor.Value");
auto critical =
sensorMap.find("xyz.openbmc_project.Sensor.Threshold.Critical");
auto warning =
sensorMap.find("xyz.openbmc_project.Sensor.Threshold.Warning");
if (sensorObject != sensorMap.end())
{
auto maxMap = sensorObject->second.find("MaxValue");
auto minMap = sensorObject->second.find("MinValue");
if (maxMap != sensorObject->second.end())
{
max = std::visit(VariantToDoubleVisitor(), maxMap->second);
}
if (minMap != sensorObject->second.end())
{
min = std::visit(VariantToDoubleVisitor(), minMap->second);
}
}
if (critical != sensorMap.end())
{
auto lower = critical->second.find("CriticalLow");
auto upper = critical->second.find("CriticalHigh");
if (lower != critical->second.end())
{
double value = std::visit(VariantToDoubleVisitor(), lower->second);
min = std::min(value, min);
}
if (upper != critical->second.end())
{
double value = std::visit(VariantToDoubleVisitor(), upper->second);
max = std::max(value, max);
}
}
if (warning != sensorMap.end())
{
auto lower = warning->second.find("WarningLow");
auto upper = warning->second.find("WarningHigh");
if (lower != warning->second.end())
{
double value = std::visit(VariantToDoubleVisitor(), lower->second);
min = std::min(value, min);
}
if (upper != warning->second.end())
{
double value = std::visit(VariantToDoubleVisitor(), upper->second);
max = std::max(value, max);
}
}
}
static bool getSensorMap(std::string sensorConnection, std::string sensorPath,
SensorMap &sensorMap)
{
static boost::container::flat_map<
std::string, std::chrono::time_point<std::chrono::steady_clock>>
updateTimeMap;
auto updateFind = updateTimeMap.find(sensorConnection);
auto lastUpdate = std::chrono::time_point<std::chrono::steady_clock>();
if (updateFind != updateTimeMap.end())
{
lastUpdate = updateFind->second;
}
auto now = std::chrono::steady_clock::now();
if (std::chrono::duration_cast<std::chrono::seconds>(now - lastUpdate)
.count() > sensorMapUpdatePeriod)
{
updateTimeMap[sensorConnection] = now;
std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
auto managedObj = dbus->new_method_call(
sensorConnection.c_str(), "/", "org.freedesktop.DBus.ObjectManager",
"GetManagedObjects");
ManagedObjectType managedObjects;
try
{
auto reply = dbus->call(managedObj);
reply.read(managedObjects);
}
catch (sdbusplus::exception_t &)
{
phosphor::logging::log<phosphor::logging::level::ERR>(
"Error getting managed objects from connection",
phosphor::logging::entry("CONNECTION=%s",
sensorConnection.c_str()));
return false;
}
SensorCache[sensorConnection] = managedObjects;
}
auto connection = SensorCache.find(sensorConnection);
if (connection == SensorCache.end())
{
return false;
}
auto path = connection->second.find(sensorPath);
if (path == connection->second.end())
{
return false;
}
sensorMap = path->second;
return true;
}
/* sensor commands */
ipmi_ret_t ipmiSensorWildcardHandler(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
ipmi_request_t request,
ipmi_response_t response,
ipmi_data_len_t dataLen,
ipmi_context_t context)
{
*dataLen = 0;
printCommand(+netfn, +cmd);
return IPMI_CC_INVALID;
}
namespace meHealth
{
constexpr const char *busname = "xyz.openbmc_project.NodeManagerProxy";
constexpr const char *path = "/xyz/openbmc_project/status/me";
constexpr const char *interface = "xyz.openbmc_project.SetHealth";
constexpr const char *method = "SetHealth";
constexpr const char *critical = "critical";
constexpr const char *warning = "warning";
constexpr const char *ok = "ok";
} // namespace meHealth
static void setMeStatus(uint8_t eventData2, uint8_t eventData3, bool disable)
{
constexpr const std::array<uint8_t, 10> critical = {
0x1, 0x2, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xD, 0xE};
constexpr const std::array<uint8_t, 5> warning = {0x3, 0xA, 0x13, 0x19,
0x1A};
std::string state;
if (std::find(critical.begin(), critical.end(), eventData2) !=
critical.end())
{
state = meHealth::critical;
}
// special case 0x3 as we only care about a few states
else if (eventData2 == 0x3)
{
if (eventData3 <= 0x2)
{
state = meHealth::warning;
}
else
{
return;
}
}
else if (std::find(warning.begin(), warning.end(), eventData2) !=
warning.end())
{
state = meHealth::warning;
}
else
{
return;
}
if (disable)
{
state = meHealth::ok;
}
std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
auto setHealth =
dbus->new_method_call(meHealth::busname, meHealth::path,
meHealth::interface, meHealth::method);
setHealth.append(std::to_string(static_cast<size_t>(eventData2)), state);
try
{
dbus->call(setHealth);
}
catch (sdbusplus::exception_t &)
{
phosphor::logging::log<phosphor::logging::level::ERR>(
"Failed to set ME Health");
}
}
ipmi::RspType<> ipmiSenPlatformEvent(ipmi::message::Payload &p)
{
constexpr const uint8_t meId = 0x2C;
constexpr const uint8_t meSensorNum = 0x17;
constexpr const uint8_t disabled = 0x80;
uint8_t generatorID = 0;
uint8_t evmRev = 0;
uint8_t sensorType = 0;
uint8_t sensorNum = 0;
uint8_t eventType = 0;
uint8_t eventData1 = 0;
std::optional<uint8_t> eventData2 = 0;
std::optional<uint8_t> eventData3 = 0;
// todo: This check is supposed to be based on the incoming channel.
// e.g. system channel will provide upto 8 bytes including generator
// ID, but ipmb channel will provide only up to 7 bytes without the
// generator ID.
// Support for this check is coming in future patches, so for now just base
// it on if the first byte is the EvMRev (0x04).
if (p.size() && p.data()[0] == 0x04)
{
p.unpack(evmRev, sensorType, sensorNum, eventType, eventData1,
eventData2, eventData3);
// todo: the generator ID for this channel is supposed to come from the
// IPMB requesters slave address. Support for this is coming in future
// patches, so for now just assume it is coming from the ME (0x2C).
generatorID = 0x2C;
}
else
{
p.unpack(generatorID, evmRev, sensorType, sensorNum, eventType,
eventData1, eventData2, eventData3);
}
if (!p.fullyUnpacked())
{
return ipmi::responseReqDataLenInvalid();
}
// Send this request to the Redfish hooks to log it as a Redfish message
// instead. There is no need to add it to the SEL, so just return success.
intel_oem::ipmi::sel::checkRedfishHooks(
generatorID, evmRev, sensorType, sensorNum, eventType, eventData1,
eventData2.value_or(0xFF), eventData3.value_or(0xFF));
if (generatorID == meId && sensorNum == meSensorNum && eventData2 &&
eventData3)
{
setMeStatus(*eventData2, *eventData3, (eventType & disabled));
}
return ipmi::responseSuccess();
}
ipmi::RspType<uint8_t, uint8_t, uint8_t, std::optional<uint8_t>>
ipmiSenGetSensorReading(uint8_t sensnum)
{
std::string connection;
std::string path;
auto status = getSensorConnection(sensnum, connection, path);
if (status)
{
return ipmi::response(status);
}
SensorMap sensorMap;
if (!getSensorMap(connection, path, sensorMap))
{
return ipmi::responseResponseError();
}
auto sensorObject = sensorMap.find("xyz.openbmc_project.Sensor.Value");
if (sensorObject == sensorMap.end() ||
sensorObject->second.find("Value") == sensorObject->second.end())
{
return ipmi::responseResponseError();
}
auto &valueVariant = sensorObject->second["Value"];
double reading = std::visit(VariantToDoubleVisitor(), valueVariant);
double max = 0;
double min = 0;
getSensorMaxMin(sensorMap, max, min);
int16_t mValue = 0;
int16_t bValue = 0;
int8_t rExp = 0;
int8_t bExp = 0;
bool bSigned = false;
if (!getSensorAttributes(max, min, mValue, rExp, bValue, bExp, bSigned))
{
return ipmi::responseResponseError();
}
uint8_t value =
scaleIPMIValueFromDouble(reading, mValue, rExp, bValue, bExp, bSigned);
uint8_t operation =
static_cast<uint8_t>(IPMISensorReadingByte2::sensorScanningEnable);
operation |=
static_cast<uint8_t>(IPMISensorReadingByte2::eventMessagesEnable);
uint8_t thresholds = 0;
auto warningObject =
sensorMap.find("xyz.openbmc_project.Sensor.Threshold.Warning");
if (warningObject != sensorMap.end())
{
auto alarmHigh = warningObject->second.find("WarningAlarmHigh");
auto alarmLow = warningObject->second.find("WarningAlarmLow");
if (alarmHigh != warningObject->second.end())
{
if (std::get<bool>(alarmHigh->second))
{
thresholds |= static_cast<uint8_t>(
IPMISensorReadingByte3::upperNonCritical);
}
}
if (alarmLow != warningObject->second.end())
{
if (std::get<bool>(alarmLow->second))
{
thresholds |= static_cast<uint8_t>(
IPMISensorReadingByte3::lowerNonCritical);
}
}
}
auto criticalObject =
sensorMap.find("xyz.openbmc_project.Sensor.Threshold.Critical");
if (criticalObject != sensorMap.end())
{
auto alarmHigh = criticalObject->second.find("CriticalAlarmHigh");
auto alarmLow = criticalObject->second.find("CriticalAlarmLow");
if (alarmHigh != criticalObject->second.end())
{
if (std::get<bool>(alarmHigh->second))
{
thresholds |=
static_cast<uint8_t>(IPMISensorReadingByte3::upperCritical);
}
}
if (alarmLow != criticalObject->second.end())
{
if (std::get<bool>(alarmLow->second))
{
thresholds |=
static_cast<uint8_t>(IPMISensorReadingByte3::lowerCritical);
}
}
}
// no discrete as of today so optional byte is never returned
return ipmi::responseSuccess(value, operation, thresholds, std::nullopt);
}
/** @brief implements the Set Sensor threshold command
* @param sensorNumber - sensor number
* @param lowerNonCriticalThreshMask
* @param lowerCriticalThreshMask
* @param lowerNonRecovThreshMask
* @param upperNonCriticalThreshMask
* @param upperCriticalThreshMask
* @param upperNonRecovThreshMask
* @param reserved
* @param lowerNonCritical - lower non-critical threshold
* @param lowerCritical - Lower critical threshold
* @param lowerNonRecoverable - Lower non recovarable threshold
* @param upperNonCritical - Upper non-critical threshold
* @param upperCritical - Upper critical
* @param upperNonRecoverable - Upper Non-recoverable
*
* @returns IPMI completion code
*/
ipmi::RspType<> ipmiSenSetSensorThresholds(
uint8_t sensorNum, bool lowerNonCriticalThreshMask,
bool lowerCriticalThreshMask, bool lowerNonRecovThreshMask,
bool upperNonCriticalThreshMask, bool upperCriticalThreshMask,
bool upperNonRecovThreshMask, uint2_t reserved, uint8_t lowerNonCritical,
uint8_t lowerCritical, uint8_t lowerNonRecoverable,
uint8_t upperNonCritical, uint8_t upperCritical,
uint8_t upperNonRecoverable)
{
constexpr uint8_t thresholdMask = 0xFF;
if (reserved)
{
return ipmi::responseInvalidFieldRequest();
}
// lower nc and upper nc not suppported on any sensor
if (lowerNonRecovThreshMask || upperNonRecovThreshMask)
{
return ipmi::responseInvalidFieldRequest();
}
// if none of the threshold mask are set, nothing to do
if (!(lowerNonCriticalThreshMask | lowerCriticalThreshMask |
lowerNonRecovThreshMask | upperNonCriticalThreshMask |
upperCriticalThreshMask | upperNonRecovThreshMask))
{
return ipmi::responseSuccess();
}
std::string connection;
std::string path;
ipmi::Cc status = getSensorConnection(sensorNum, connection, path);
if (status)
{
return ipmi::response(status);
}
SensorMap sensorMap;
if (!getSensorMap(connection, path, sensorMap))
{
return ipmi::responseResponseError();
}
double max = 0;
double min = 0;
getSensorMaxMin(sensorMap, max, min);
int16_t mValue = 0;
int16_t bValue = 0;
int8_t rExp = 0;
int8_t bExp = 0;
bool bSigned = false;
if (!getSensorAttributes(max, min, mValue, rExp, bValue, bExp, bSigned))
{
return ipmi::responseResponseError();
}
// store a vector of property name, value to set, and interface
std::vector<std::tuple<std::string, uint8_t, std::string>> thresholdsToSet;
// define the indexes of the tuple
constexpr uint8_t propertyName = 0;
constexpr uint8_t thresholdValue = 1;
constexpr uint8_t interface = 2;
// verifiy all needed fields are present
if (lowerCriticalThreshMask || upperCriticalThreshMask)
{
auto findThreshold =
sensorMap.find("xyz.openbmc_project.Sensor.Threshold.Critical");
if (findThreshold == sensorMap.end())
{
return ipmi::responseInvalidFieldRequest();
}
if (lowerCriticalThreshMask)
{
auto findLower = findThreshold->second.find("CriticalLow");
if (findLower == findThreshold->second.end())
{
return ipmi::responseInvalidFieldRequest();
}
thresholdsToSet.emplace_back("CriticalLow", lowerCritical,
findThreshold->first);
}
if (upperCriticalThreshMask)
{
auto findUpper = findThreshold->second.find("CriticalHigh");
if (findUpper == findThreshold->second.end())
{
return ipmi::responseInvalidFieldRequest();
}
thresholdsToSet.emplace_back("CriticalHigh", upperCritical,
findThreshold->first);
}
}
if (lowerNonCriticalThreshMask || upperNonCriticalThreshMask)
{
auto findThreshold =
sensorMap.find("xyz.openbmc_project.Sensor.Threshold.Warning");
if (findThreshold == sensorMap.end())
{
return ipmi::responseInvalidFieldRequest();
}
if (lowerNonCriticalThreshMask)
{
auto findLower = findThreshold->second.find("WarningLow");
if (findLower == findThreshold->second.end())
{
return ipmi::responseInvalidFieldRequest();
}
thresholdsToSet.emplace_back("WarningLow", lowerNonCritical,
findThreshold->first);
}
if (upperNonCriticalThreshMask)
{
auto findUpper = findThreshold->second.find("WarningHigh");
if (findUpper == findThreshold->second.end())
{
return ipmi::responseInvalidFieldRequest();
}
thresholdsToSet.emplace_back("WarningHigh", upperNonCritical,
findThreshold->first);
}
}
for (const auto &property : thresholdsToSet)
{
// from section 36.3 in the IPMI Spec, assume all linear
double valueToSet = ((mValue * std::get<thresholdValue>(property)) +
(bValue * std::pow(10, bExp))) *
std::pow(10, rExp);
setDbusProperty(
*getSdBus(), connection, path, std::get<interface>(property),
std::get<propertyName>(property), ipmi::Value(valueToSet));
}
return ipmi::responseSuccess();
}
IPMIThresholds getIPMIThresholds(const SensorMap &sensorMap)
{
IPMIThresholds resp;
auto warningInterface =
sensorMap.find("xyz.openbmc_project.Sensor.Threshold.Warning");
auto criticalInterface =
sensorMap.find("xyz.openbmc_project.Sensor.Threshold.Critical");
if ((warningInterface != sensorMap.end()) ||
(criticalInterface != sensorMap.end()))
{
auto sensorPair = sensorMap.find("xyz.openbmc_project.Sensor.Value");
if (sensorPair == sensorMap.end())
{
// should not have been able to find a sensor not implementing
// the sensor object
throw std::runtime_error("Invalid sensor map");
}
double max = 0;
double min = 0;
getSensorMaxMin(sensorMap, max, min);
int16_t mValue = 0;
int16_t bValue = 0;
int8_t rExp = 0;
int8_t bExp = 0;
bool bSigned = false;
if (!getSensorAttributes(max, min, mValue, rExp, bValue, bExp, bSigned))
{
throw std::runtime_error("Invalid sensor atrributes");
}
if (warningInterface != sensorMap.end())
{
auto &warningMap = warningInterface->second;
auto warningHigh = warningMap.find("WarningHigh");
auto warningLow = warningMap.find("WarningLow");
if (warningHigh != warningMap.end())
{
double value =
std::visit(VariantToDoubleVisitor(), warningHigh->second);
resp.warningHigh = scaleIPMIValueFromDouble(
value, mValue, rExp, bValue, bExp, bSigned);
}
if (warningLow != warningMap.end())
{
double value =
std::visit(VariantToDoubleVisitor(), warningLow->second);
resp.warningLow = scaleIPMIValueFromDouble(
value, mValue, rExp, bValue, bExp, bSigned);
}
}
if (criticalInterface != sensorMap.end())
{
auto &criticalMap = criticalInterface->second;
auto criticalHigh = criticalMap.find("CriticalHigh");
auto criticalLow = criticalMap.find("CriticalLow");
if (criticalHigh != criticalMap.end())
{
double value =
std::visit(VariantToDoubleVisitor(), criticalHigh->second);
resp.criticalHigh = scaleIPMIValueFromDouble(
value, mValue, rExp, bValue, bExp, bSigned);
}
if (criticalLow != criticalMap.end())
{
double value =
std::visit(VariantToDoubleVisitor(), criticalLow->second);
resp.criticalLow = scaleIPMIValueFromDouble(
value, mValue, rExp, bValue, bExp, bSigned);
}
}
}
return resp;
}
ipmi::RspType<uint8_t, // readable
uint8_t, // lowerNCrit
uint8_t, // lowerCrit
uint8_t, // lowerNrecoverable
uint8_t, // upperNC
uint8_t, // upperCrit
uint8_t> // upperNRecoverable
ipmiSenGetSensorThresholds(uint8_t sensorNumber)
{
std::string connection;
std::string path;
auto status = getSensorConnection(sensorNumber, connection, path);
if (status)
{
return ipmi::response(status);
}
SensorMap sensorMap;
if (!getSensorMap(connection, path, sensorMap))
{
return ipmi::responseResponseError();
}
IPMIThresholds thresholdData;
try
{
thresholdData = getIPMIThresholds(sensorMap);
}
catch (std::exception &)
{
return ipmi::responseResponseError();
}
uint8_t readable = 0;
uint8_t lowerNC = 0;
uint8_t lowerCritical = 0;
uint8_t lowerNonRecoverable = 0;
uint8_t upperNC = 0;
uint8_t upperCritical = 0;
uint8_t upperNonRecoverable = 0;
if (thresholdData.warningHigh)
{
readable |=
1 << static_cast<uint8_t>(IPMIThresholdRespBits::upperNonCritical);
upperNC = *thresholdData.warningHigh;
}
if (thresholdData.warningLow)
{
readable |=
1 << static_cast<uint8_t>(IPMIThresholdRespBits::lowerNonCritical);
lowerNC = *thresholdData.warningLow;
}
if (thresholdData.criticalHigh)
{
readable |=
1 << static_cast<uint8_t>(IPMIThresholdRespBits::upperCritical);
upperCritical = *thresholdData.criticalHigh;
}
if (thresholdData.criticalLow)
{
readable |=
1 << static_cast<uint8_t>(IPMIThresholdRespBits::lowerCritical);
lowerCritical = *thresholdData.criticalLow;
}
return ipmi::responseSuccess(readable, lowerNC, lowerCritical,
lowerNonRecoverable, upperNC, upperCritical,
upperNonRecoverable);
}
/** @brief implements the get Sensor event enable command
* @param sensorNumber - sensor number
*
* @returns IPMI completion code plus response data
* - enabled - Sensor Event messages
* - assertionEnabledLsb - Assertion event messages
* - assertionEnabledMsb - Assertion event messages
* - deassertionEnabledLsb - Deassertion event messages
* - deassertionEnabledMsb - Deassertion event messages
*/
ipmi::RspType<uint8_t, // enabled
uint8_t, // assertionEnabledLsb
uint8_t, // assertionEnabledMsb
uint8_t, // deassertionEnabledLsb
uint8_t> // deassertionEnabledMsb
ipmiSenGetSensorEventEnable(uint8_t sensorNum)
{
std::string connection;
std::string path;
uint8_t enabled;
uint8_t assertionEnabledLsb;
uint8_t assertionEnabledMsb;
uint8_t deassertionEnabledLsb;
uint8_t deassertionEnabledMsb;
auto status = getSensorConnection(sensorNum, connection, path);
if (status)
{
return ipmi::response(status);
}
SensorMap sensorMap;
if (!getSensorMap(connection, path, sensorMap))
{
return ipmi::responseResponseError();
}
auto warningInterface =
sensorMap.find("xyz.openbmc_project.Sensor.Threshold.Warning");
auto criticalInterface =
sensorMap.find("xyz.openbmc_project.Sensor.Threshold.Critical");
if ((warningInterface != sensorMap.end()) ||
(criticalInterface != sensorMap.end()))
{
enabled = static_cast<uint8_t>(
IPMISensorEventEnableByte2::sensorScanningEnable);
if (warningInterface != sensorMap.end())
{
auto &warningMap = warningInterface->second;
auto warningHigh = warningMap.find("WarningHigh");
auto warningLow = warningMap.find("WarningLow");
if (warningHigh != warningMap.end())
{
assertionEnabledLsb |= static_cast<uint8_t>(
IPMISensorEventEnableThresholds::upperNonCriticalGoingHigh);
deassertionEnabledLsb |= static_cast<uint8_t>(
IPMISensorEventEnableThresholds::upperNonCriticalGoingLow);
}
if (warningLow != warningMap.end())
{
assertionEnabledLsb |= static_cast<uint8_t>(
IPMISensorEventEnableThresholds::lowerNonCriticalGoingLow);
deassertionEnabledLsb |= static_cast<uint8_t>(
IPMISensorEventEnableThresholds::lowerNonCriticalGoingHigh);
}
}
if (criticalInterface != sensorMap.end())
{
auto &criticalMap = criticalInterface->second;
auto criticalHigh = criticalMap.find("CriticalHigh");
auto criticalLow = criticalMap.find("CriticalLow");
if (criticalHigh != criticalMap.end())
{
assertionEnabledMsb |= static_cast<uint8_t>(
IPMISensorEventEnableThresholds::upperCriticalGoingHigh);
deassertionEnabledMsb |= static_cast<uint8_t>(
IPMISensorEventEnableThresholds::upperCriticalGoingLow);
}
if (criticalLow != criticalMap.end())
{
assertionEnabledLsb |= static_cast<uint8_t>(
IPMISensorEventEnableThresholds::lowerCriticalGoingLow);
deassertionEnabledLsb |= static_cast<uint8_t>(
IPMISensorEventEnableThresholds::lowerCriticalGoingHigh);
}
}
}
return ipmi::responseSuccess(enabled, assertionEnabledLsb,
assertionEnabledMsb, deassertionEnabledLsb,
deassertionEnabledMsb);
}
ipmi_ret_t ipmiSenGetSensorEventStatus(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
ipmi_request_t request,
ipmi_response_t response,
ipmi_data_len_t dataLen,
ipmi_context_t context)
{
if (*dataLen != 1)
{
*dataLen = 0;
return IPMI_CC_REQ_DATA_LEN_INVALID;
}
*dataLen = 0; // default to 0 in case of an error
uint8_t sensnum = *(static_cast<uint8_t *>(request));
std::string connection;
std::string path;
auto status = getSensorConnection(sensnum, connection, path);
if (status)
{
return status;
}
SensorMap sensorMap;
if (!getSensorMap(connection, path, sensorMap))
{
return IPMI_CC_RESPONSE_ERROR;
}
auto warningInterface =
sensorMap.find("xyz.openbmc_project.Sensor.Threshold.Warning");
auto criticalInterface =
sensorMap.find("xyz.openbmc_project.Sensor.Threshold.Critical");
// zero out response buff
auto responseClear = static_cast<uint8_t *>(response);
std::fill(responseClear, responseClear + sizeof(SensorEventStatusResp), 0);
auto resp = static_cast<SensorEventStatusResp *>(response);
resp->enabled =
static_cast<uint8_t>(IPMISensorEventEnableByte2::sensorScanningEnable);
std::optional<bool> criticalDeassertHigh =
thresholdDeassertMap[path]["CriticalAlarmHigh"];
std::optional<bool> criticalDeassertLow =
thresholdDeassertMap[path]["CriticalAlarmLow"];
std::optional<bool> warningDeassertHigh =
thresholdDeassertMap[path]["WarningAlarmHigh"];
std::optional<bool> warningDeassertLow =
thresholdDeassertMap[path]["WarningAlarmLow"];
if (criticalDeassertHigh && !*criticalDeassertHigh)
{
resp->deassertionsMSB |= static_cast<uint8_t>(
IPMISensorEventEnableThresholds::upperCriticalGoingHigh);
}
if (criticalDeassertLow && !*criticalDeassertLow)
{
resp->deassertionsMSB |= static_cast<uint8_t>(
IPMISensorEventEnableThresholds::upperCriticalGoingLow);
}
if (warningDeassertHigh && !*warningDeassertHigh)
{
resp->deassertionsLSB |= static_cast<uint8_t>(
IPMISensorEventEnableThresholds::upperNonCriticalGoingHigh);
}
if (warningDeassertLow && !*warningDeassertLow)
{
resp->deassertionsLSB |= static_cast<uint8_t>(
IPMISensorEventEnableThresholds::lowerNonCriticalGoingHigh);
}
if ((warningInterface != sensorMap.end()) ||
(criticalInterface != sensorMap.end()))
{
resp->enabled = static_cast<uint8_t>(
IPMISensorEventEnableByte2::eventMessagesEnable);
if (warningInterface != sensorMap.end())
{
auto &warningMap = warningInterface->second;
auto warningHigh = warningMap.find("WarningAlarmHigh");
auto warningLow = warningMap.find("WarningAlarmLow");
auto warningHighAlarm = false;
auto warningLowAlarm = false;
if (warningHigh != warningMap.end())
{
warningHighAlarm = std::get<bool>(warningHigh->second);
}
if (warningLow != warningMap.end())
{
warningLowAlarm = std::get<bool>(warningLow->second);