-
Notifications
You must be signed in to change notification settings - Fork 10
/
sensorhandler.cpp
1547 lines (1365 loc) · 51.4 KB
/
sensorhandler.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
#include "config.h"
#include "sensorhandler.hpp"
#include "fruread.hpp"
#include <systemd/sd-bus.h>
#include <ipmid/api.hpp>
#include <ipmid/entity_map_json.hpp>
#include <ipmid/types.hpp>
#include <ipmid/utils.hpp>
#include <phosphor-logging/elog-errors.hpp>
#include <phosphor-logging/lg2.hpp>
#include <sdbusplus/message/types.hpp>
#include <xyz/openbmc_project/Common/error.hpp>
#include <xyz/openbmc_project/Sensor/Value/server.hpp>
#include <bitset>
#include <cmath>
#include <cstring>
#include <set>
static constexpr uint8_t fruInventoryDevice = 0x10;
static constexpr uint8_t IPMIFruInventory = 0x02;
static constexpr uint8_t BMCTargetAddress = 0x20;
extern int updateSensorRecordFromSSRAESC(const void*);
extern sd_bus* bus;
namespace ipmi
{
namespace sensor
{
extern const IdInfoMap sensors;
} // namespace sensor
} // namespace ipmi
extern const FruMap frus;
using namespace phosphor::logging;
using InternalFailure =
sdbusplus::error::xyz::openbmc_project::common::InternalFailure;
void register_netfn_sen_functions() __attribute__((constructor));
struct sensorTypemap_t
{
uint8_t number;
uint8_t typecode;
char dbusname[32];
};
sensorTypemap_t g_SensorTypeMap[] = {
{0x01, 0x6F, "Temp"},
{0x0C, 0x6F, "DIMM"},
{0x0C, 0x6F, "MEMORY_BUFFER"},
{0x07, 0x6F, "PROC"},
{0x07, 0x6F, "CORE"},
{0x07, 0x6F, "CPU"},
{0x0F, 0x6F, "BootProgress"},
{0xe9, 0x09, "OccStatus"}, // E9 is an internal mapping to handle sensor
// type code os 0x09
{0xC3, 0x6F, "BootCount"},
{0x1F, 0x6F, "OperatingSystemStatus"},
{0x12, 0x6F, "SYSTEM_EVENT"},
{0xC7, 0x03, "SYSTEM"},
{0xC7, 0x03, "MAIN_PLANAR"},
{0xC2, 0x6F, "PowerCap"},
{0x0b, 0xCA, "PowerSupplyRedundancy"},
{0xDA, 0x03, "TurboAllowed"},
{0xD8, 0xC8, "PowerSupplyDerating"},
{0xFF, 0x00, ""},
};
struct sensor_data_t
{
uint8_t sennum;
} __attribute__((packed));
using SDRCacheMap = std::unordered_map<uint8_t, get_sdr::SensorDataFullRecord>;
SDRCacheMap sdrCacheMap __attribute__((init_priority(101)));
using SensorThresholdMap =
std::unordered_map<uint8_t, get_sdr::GetSensorThresholdsResponse>;
SensorThresholdMap sensorThresholdMap __attribute__((init_priority(101)));
#ifdef FEATURE_SENSORS_CACHE
std::map<uint8_t, std::unique_ptr<sdbusplus::bus::match_t>> sensorAddedMatches
__attribute__((init_priority(101)));
std::map<uint8_t, std::unique_ptr<sdbusplus::bus::match_t>> sensorUpdatedMatches
__attribute__((init_priority(101)));
std::map<uint8_t, std::unique_ptr<sdbusplus::bus::match_t>> sensorRemovedMatches
__attribute__((init_priority(101)));
std::unique_ptr<sdbusplus::bus::match_t> sensorsOwnerMatch
__attribute__((init_priority(101)));
ipmi::sensor::SensorCacheMap sensorCacheMap __attribute__((init_priority(101)));
// It is needed to know which objects belong to which service, so that when a
// service exits without interfacesRemoved signal, we could invaildate the cache
// that is related to the service. It uses below two variables:
// - idToServiceMap records which sensors are known to have a related service;
// - serviceToIdMap maps a service to the sensors.
using sensorIdToServiceMap = std::unordered_map<uint8_t, std::string>;
sensorIdToServiceMap idToServiceMap __attribute__((init_priority(101)));
using sensorServiceToIdMap = std::unordered_map<std::string, std::set<uint8_t>>;
sensorServiceToIdMap serviceToIdMap __attribute__((init_priority(101)));
static void fillSensorIdServiceMap(const std::string&,
const std::string& /*intf*/, uint8_t id,
const std::string& service)
{
if (idToServiceMap.find(id) != idToServiceMap.end())
{
return;
}
idToServiceMap[id] = service;
serviceToIdMap[service].insert(id);
}
static void fillSensorIdServiceMap(const std::string& obj,
const std::string& intf, uint8_t id)
{
if (idToServiceMap.find(id) != idToServiceMap.end())
{
return;
}
try
{
sdbusplus::bus_t bus{ipmid_get_sd_bus_connection()};
auto service = ipmi::getService(bus, intf, obj);
idToServiceMap[id] = service;
serviceToIdMap[service].insert(id);
}
catch (...)
{
// Ignore
}
}
void initSensorMatches()
{
using namespace sdbusplus::bus::match::rules;
sdbusplus::bus_t bus{ipmid_get_sd_bus_connection()};
for (const auto& s : ipmi::sensor::sensors)
{
sensorAddedMatches.emplace(
s.first,
std::make_unique<sdbusplus::bus::match_t>(
bus, interfacesAdded() + argNpath(0, s.second.sensorPath),
[id = s.first, obj = s.second.sensorPath,
intf = s.second.propertyInterfaces.begin()->first](
auto& /*msg*/) { fillSensorIdServiceMap(obj, intf, id); }));
sensorRemovedMatches.emplace(
s.first,
std::make_unique<sdbusplus::bus::match_t>(
bus, interfacesRemoved() + argNpath(0, s.second.sensorPath),
[id = s.first](auto& /*msg*/) {
// Ideally this should work.
// But when a service is terminated or crashed, it does not
// emit interfacesRemoved signal. In that case it's handled
// by sensorsOwnerMatch
sensorCacheMap[id].reset();
}));
sensorUpdatedMatches.emplace(
s.first,
std::make_unique<sdbusplus::bus::match_t>(
bus,
type::signal() + path(s.second.sensorPath) +
member("PropertiesChanged"s) +
interface("org.freedesktop.DBus.Properties"s),
[&s](auto& msg) {
fillSensorIdServiceMap(
s.second.sensorPath,
s.second.propertyInterfaces.begin()->first, s.first);
try
{
// This is signal callback
std::string interfaceName;
msg.read(interfaceName);
ipmi::PropertyMap props;
msg.read(props);
s.second.getFunc(s.first, s.second, props);
}
catch (const std::exception& e)
{
sensorCacheMap[s.first].reset();
}
}));
}
sensorsOwnerMatch = std::make_unique<sdbusplus::bus::match_t>(
bus, nameOwnerChanged(), [](auto& msg) {
std::string name;
std::string oldOwner;
std::string newOwner;
msg.read(name, oldOwner, newOwner);
if (!name.empty() && newOwner.empty())
{
// The service exits
const auto it = serviceToIdMap.find(name);
if (it == serviceToIdMap.end())
{
return;
}
for (const auto& id : it->second)
{
// Invalidate cache
sensorCacheMap[id].reset();
}
}
});
}
#endif
// Use a lookup table to find the interface name of a specific sensor
// This will be used until an alternative is found. this is the first
// step for mapping IPMI
int find_openbmc_path(uint8_t num, dbus_interface_t* interface)
{
const auto& sensor_it = ipmi::sensor::sensors.find(num);
if (sensor_it == ipmi::sensor::sensors.end())
{
// The sensor map does not contain the sensor requested
return -EINVAL;
}
const auto& info = sensor_it->second;
std::string serviceName{};
try
{
sdbusplus::bus_t bus{ipmid_get_sd_bus_connection()};
serviceName =
ipmi::getService(bus, info.sensorInterface, info.sensorPath);
}
catch (const sdbusplus::exception_t&)
{
std::fprintf(stderr, "Failed to get %s busname: %s\n",
info.sensorPath.c_str(), serviceName.c_str());
return -EINVAL;
}
interface->sensortype = info.sensorType;
strcpy(interface->bus, serviceName.c_str());
strcpy(interface->path, info.sensorPath.c_str());
// Take the interface name from the beginning of the DbusInterfaceMap. This
// works for the Value interface but may not suffice for more complex
// sensors.
// tracked https://github.com/openbmc/phosphor-host-ipmid/issues/103
strcpy(interface->interface,
info.propertyInterfaces.begin()->first.c_str());
interface->sensornumber = num;
return 0;
}
/////////////////////////////////////////////////////////////////////
//
// Routines used by ipmi commands wanting to interact on the dbus
//
/////////////////////////////////////////////////////////////////////
int set_sensor_dbus_state_s(uint8_t number, const char* method,
const char* value)
{
dbus_interface_t a;
int r;
sd_bus_error error = SD_BUS_ERROR_NULL;
sd_bus_message* m = NULL;
r = find_openbmc_path(number, &a);
if (r < 0)
{
std::fprintf(stderr, "Failed to find Sensor 0x%02x\n", number);
return 0;
}
r = sd_bus_message_new_method_call(bus, &m, a.bus, a.path, a.interface,
method);
if (r < 0)
{
std::fprintf(stderr, "Failed to create a method call: %s",
strerror(-r));
goto final;
}
r = sd_bus_message_append(m, "v", "s", value);
if (r < 0)
{
std::fprintf(stderr, "Failed to create a input parameter: %s",
strerror(-r));
goto final;
}
r = sd_bus_call(bus, m, 0, &error, NULL);
if (r < 0)
{
std::fprintf(stderr, "Failed to call the method: %s", strerror(-r));
}
final:
sd_bus_error_free(&error);
m = sd_bus_message_unref(m);
return 0;
}
int set_sensor_dbus_state_y(uint8_t number, const char* method,
const uint8_t value)
{
dbus_interface_t a;
int r;
sd_bus_error error = SD_BUS_ERROR_NULL;
sd_bus_message* m = NULL;
r = find_openbmc_path(number, &a);
if (r < 0)
{
std::fprintf(stderr, "Failed to find Sensor 0x%02x\n", number);
return 0;
}
r = sd_bus_message_new_method_call(bus, &m, a.bus, a.path, a.interface,
method);
if (r < 0)
{
std::fprintf(stderr, "Failed to create a method call: %s",
strerror(-r));
goto final;
}
r = sd_bus_message_append(m, "v", "i", value);
if (r < 0)
{
std::fprintf(stderr, "Failed to create a input parameter: %s",
strerror(-r));
goto final;
}
r = sd_bus_call(bus, m, 0, &error, NULL);
if (r < 0)
{
std::fprintf(stderr, "12 Failed to call the method: %s", strerror(-r));
}
final:
sd_bus_error_free(&error);
m = sd_bus_message_unref(m);
return 0;
}
uint8_t dbus_to_sensor_type(char* p)
{
sensorTypemap_t* s = g_SensorTypeMap;
char r = 0;
while (s->number != 0xFF)
{
if (!strcmp(s->dbusname, p))
{
r = s->typecode;
break;
}
s++;
}
if (s->number == 0xFF)
printf("Failed to find Sensor Type %s\n", p);
return r;
}
uint8_t get_type_from_interface(dbus_interface_t dbus_if)
{
uint8_t type;
// This is where sensors that do not exist in dbus but do
// exist in the host code stop. This should indicate it
// is not a supported sensor
if (dbus_if.interface[0] == 0)
{
return 0;
}
// Fetch type from interface itself.
if (dbus_if.sensortype != 0)
{
type = dbus_if.sensortype;
}
else
{
// Non InventoryItems
char* p = strrchr(dbus_if.path, '/');
type = dbus_to_sensor_type(p + 1);
}
return type;
}
// Replaces find_sensor
uint8_t find_type_for_sensor_number(uint8_t num)
{
int r;
dbus_interface_t dbus_if;
r = find_openbmc_path(num, &dbus_if);
if (r < 0)
{
std::fprintf(stderr, "Could not find sensor %d\n", num);
return 0;
}
return get_type_from_interface(dbus_if);
}
/**
* @brief implements the get sensor type command.
* @param - sensorNumber
*
* @return IPMI completion code plus response data on success.
* - sensorType
* - eventType
**/
ipmi::RspType<uint8_t, // sensorType
uint8_t // eventType
>
ipmiGetSensorType(uint8_t sensorNumber)
{
const auto it = ipmi::sensor::sensors.find(sensorNumber);
if (it == ipmi::sensor::sensors.end())
{
// The sensor map does not contain the sensor requested
return ipmi::responseSensorInvalid();
}
const auto& info = it->second;
uint8_t sensorType = info.sensorType;
uint8_t eventType = info.sensorReadingType;
return ipmi::responseSuccess(sensorType, eventType);
}
const std::set<std::string> analogSensorInterfaces = {
"xyz.openbmc_project.Sensor.Value",
"xyz.openbmc_project.Control.FanPwm",
};
bool isAnalogSensor(const std::string& interface)
{
return (analogSensorInterfaces.count(interface));
}
/**
@brief This command is used to set sensorReading.
@param
- sensorNumber
- operation
- reading
- assertOffset0_7
- assertOffset8_14
- deassertOffset0_7
- deassertOffset8_14
- eventData1
- eventData2
- eventData3
@return completion code on success.
**/
ipmi::RspType<> ipmiSetSensorReading(
uint8_t sensorNumber, uint8_t operation, uint8_t reading,
uint8_t assertOffset0_7, uint8_t assertOffset8_14,
uint8_t deassertOffset0_7, uint8_t deassertOffset8_14, uint8_t eventData1,
uint8_t eventData2, uint8_t eventData3)
{
lg2::debug("IPMI SET_SENSOR, sensorNumber: {SENSOR_NUM}", "SENSOR_NUM",
lg2::hex, sensorNumber);
if (sensorNumber == 0xFF)
{
return ipmi::responseInvalidFieldRequest();
}
ipmi::sensor::SetSensorReadingReq cmdData;
cmdData.number = sensorNumber;
cmdData.operation = operation;
cmdData.reading = reading;
cmdData.assertOffset0_7 = assertOffset0_7;
cmdData.assertOffset8_14 = assertOffset8_14;
cmdData.deassertOffset0_7 = deassertOffset0_7;
cmdData.deassertOffset8_14 = deassertOffset8_14;
cmdData.eventData1 = eventData1;
cmdData.eventData2 = eventData2;
cmdData.eventData3 = eventData3;
// Check if the Sensor Number is present
const auto iter = ipmi::sensor::sensors.find(sensorNumber);
if (iter == ipmi::sensor::sensors.end())
{
updateSensorRecordFromSSRAESC(&sensorNumber);
return ipmi::responseSuccess();
}
try
{
if (ipmi::sensor::Mutability::Write !=
(iter->second.mutability & ipmi::sensor::Mutability::Write))
{
lg2::error("Sensor Set operation is not allowed, "
"sensorNumber: {SENSOR_NUM}",
"SENSOR_NUM", lg2::hex, sensorNumber);
return ipmi::responseIllegalCommand();
}
auto ipmiRC = iter->second.updateFunc(cmdData, iter->second);
return ipmi::response(ipmiRC);
}
catch (const InternalFailure& e)
{
lg2::error("Set sensor failed, sensorNumber: {SENSOR_NUM}",
"SENSOR_NUM", lg2::hex, sensorNumber);
commit<InternalFailure>();
return ipmi::responseUnspecifiedError();
}
catch (const std::runtime_error& e)
{
lg2::error("runtime error: {ERROR}", "ERROR", e);
return ipmi::responseUnspecifiedError();
}
}
/** @brief implements the get sensor reading command
* @param sensorNum - sensor number
*
* @returns IPMI completion code plus response data
* - senReading - sensor reading
* - reserved
* - readState - sensor reading state enabled
* - senScanState - sensor scan state disabled
* - allEventMessageState - all Event message state disabled
* - assertionStatesLsb - threshold levels states
* - assertionStatesMsb - discrete reading sensor states
*/
ipmi::RspType<uint8_t, // sensor reading
uint5_t, // reserved
bool, // reading state
bool, // 0 = sensor scanning state disabled
bool, // 0 = all event messages disabled
uint8_t, // threshold levels states
uint8_t // discrete reading sensor states
>
ipmiSensorGetSensorReading([[maybe_unused]] ipmi::Context::ptr& ctx,
uint8_t sensorNum)
{
if (sensorNum == 0xFF)
{
return ipmi::responseInvalidFieldRequest();
}
const auto iter = ipmi::sensor::sensors.find(sensorNum);
if (iter == ipmi::sensor::sensors.end())
{
return ipmi::responseSensorInvalid();
}
if (ipmi::sensor::Mutability::Read !=
(iter->second.mutability & ipmi::sensor::Mutability::Read))
{
return ipmi::responseIllegalCommand();
}
try
{
#ifdef FEATURE_SENSORS_CACHE
auto& sensorData = sensorCacheMap[sensorNum];
if (!sensorData.has_value())
{
// No cached value, try read it
std::string service;
boost::system::error_code ec;
const auto& sensorInfo = iter->second;
ec = ipmi::getService(ctx, sensorInfo.sensorInterface,
sensorInfo.sensorPath, service);
if (ec)
{
return ipmi::responseUnspecifiedError();
}
fillSensorIdServiceMap(sensorInfo.sensorPath,
sensorInfo.propertyInterfaces.begin()->first,
iter->first, service);
ipmi::PropertyMap props;
ec = ipmi::getAllDbusProperties(
ctx, service, sensorInfo.sensorPath,
sensorInfo.propertyInterfaces.begin()->first, props);
if (ec)
{
fprintf(stderr, "Failed to get sensor %s, %d: %s\n",
sensorInfo.sensorPath.c_str(), ec.value(),
ec.message().c_str());
// Intitilizing with default values
constexpr uint8_t senReading = 0;
constexpr uint5_t reserved{0};
constexpr bool readState = true;
constexpr bool senScanState = false;
constexpr bool allEventMessageState = false;
constexpr uint8_t assertionStatesLsb = 0;
constexpr uint8_t assertionStatesMsb = 0;
return ipmi::responseSuccess(
senReading, reserved, readState, senScanState,
allEventMessageState, assertionStatesLsb,
assertionStatesMsb);
}
sensorInfo.getFunc(sensorNum, sensorInfo, props);
}
return ipmi::responseSuccess(
sensorData->response.reading, uint5_t(0),
sensorData->response.readingOrStateUnavailable,
sensorData->response.scanningEnabled,
sensorData->response.allEventMessagesEnabled,
sensorData->response.thresholdLevelsStates,
sensorData->response.discreteReadingSensorStates);
#else
ipmi::sensor::GetSensorResponse getResponse =
iter->second.getFunc(iter->second);
return ipmi::responseSuccess(
getResponse.reading, uint5_t(0),
getResponse.readingOrStateUnavailable, getResponse.scanningEnabled,
getResponse.allEventMessagesEnabled,
getResponse.thresholdLevelsStates,
getResponse.discreteReadingSensorStates);
#endif
}
#ifdef UPDATE_FUNCTIONAL_ON_FAIL
catch (const SensorFunctionalError& e)
{
return ipmi::responseResponseError();
}
#endif
catch (const std::exception& e)
{
// Intitilizing with default values
constexpr uint8_t senReading = 0;
constexpr uint5_t reserved{0};
constexpr bool readState = true;
constexpr bool senScanState = false;
constexpr bool allEventMessageState = false;
constexpr uint8_t assertionStatesLsb = 0;
constexpr uint8_t assertionStatesMsb = 0;
return ipmi::responseSuccess(senReading, reserved, readState,
senScanState, allEventMessageState,
assertionStatesLsb, assertionStatesMsb);
}
}
get_sdr::GetSensorThresholdsResponse
getSensorThresholds(ipmi::Context::ptr& ctx, uint8_t sensorNum)
{
get_sdr::GetSensorThresholdsResponse resp{};
constexpr auto warningThreshIntf =
"xyz.openbmc_project.Sensor.Threshold.Warning";
constexpr auto criticalThreshIntf =
"xyz.openbmc_project.Sensor.Threshold.Critical";
const auto iter = ipmi::sensor::sensors.find(sensorNum);
const auto info = iter->second;
std::string service;
boost::system::error_code ec;
ec = ipmi::getService(ctx, info.sensorInterface, info.sensorPath, service);
if (ec)
{
return resp;
}
ipmi::PropertyMap warnThresholds;
ec = ipmi::getAllDbusProperties(ctx, service, info.sensorPath,
warningThreshIntf, warnThresholds);
int32_t minClamp;
int32_t maxClamp;
int32_t rawData;
constexpr uint8_t sensorUnitsSignedBits = 2 << 6;
constexpr uint8_t signedDataFormat = 0x80;
if ((info.sensorUnits1 & sensorUnitsSignedBits) == signedDataFormat)
{
minClamp = std::numeric_limits<int8_t>::lowest();
maxClamp = std::numeric_limits<int8_t>::max();
}
else
{
minClamp = std::numeric_limits<uint8_t>::lowest();
maxClamp = std::numeric_limits<uint8_t>::max();
}
if (!ec)
{
double warnLow = ipmi::mappedVariant<double>(
warnThresholds, "WarningLow",
std::numeric_limits<double>::quiet_NaN());
double warnHigh = ipmi::mappedVariant<double>(
warnThresholds, "WarningHigh",
std::numeric_limits<double>::quiet_NaN());
if (std::isfinite(warnLow))
{
warnLow *= std::pow(10, info.scale - info.exponentR);
rawData = round((warnLow - info.scaledOffset) / info.coefficientM);
resp.lowerNonCritical =
static_cast<uint8_t>(std::clamp(rawData, minClamp, maxClamp));
resp.validMask |= static_cast<uint8_t>(
ipmi::sensor::ThresholdMask::NON_CRITICAL_LOW_MASK);
}
if (std::isfinite(warnHigh))
{
warnHigh *= std::pow(10, info.scale - info.exponentR);
rawData = round((warnHigh - info.scaledOffset) / info.coefficientM);
resp.upperNonCritical =
static_cast<uint8_t>(std::clamp(rawData, minClamp, maxClamp));
resp.validMask |= static_cast<uint8_t>(
ipmi::sensor::ThresholdMask::NON_CRITICAL_HIGH_MASK);
}
}
ipmi::PropertyMap critThresholds;
ec = ipmi::getAllDbusProperties(ctx, service, info.sensorPath,
criticalThreshIntf, critThresholds);
if (!ec)
{
double critLow = ipmi::mappedVariant<double>(
critThresholds, "CriticalLow",
std::numeric_limits<double>::quiet_NaN());
double critHigh = ipmi::mappedVariant<double>(
critThresholds, "CriticalHigh",
std::numeric_limits<double>::quiet_NaN());
if (std::isfinite(critLow))
{
critLow *= std::pow(10, info.scale - info.exponentR);
rawData = round((critLow - info.scaledOffset) / info.coefficientM);
resp.lowerCritical =
static_cast<uint8_t>(std::clamp(rawData, minClamp, maxClamp));
resp.validMask |= static_cast<uint8_t>(
ipmi::sensor::ThresholdMask::CRITICAL_LOW_MASK);
}
if (std::isfinite(critHigh))
{
critHigh *= std::pow(10, info.scale - info.exponentR);
rawData = round((critHigh - info.scaledOffset) / info.coefficientM);
resp.upperCritical =
static_cast<uint8_t>(std::clamp(rawData, minClamp, maxClamp));
resp.validMask |= static_cast<uint8_t>(
ipmi::sensor::ThresholdMask::CRITICAL_HIGH_MASK);
}
}
return resp;
}
/** @brief implements the get sensor thresholds command
* @param ctx - IPMI context pointer
* @param sensorNum - sensor number
*
* @returns IPMI completion code plus response data
* - validMask - threshold mask
* - lower non-critical threshold - IPMI messaging state
* - lower critical threshold - link authentication state
* - lower non-recoverable threshold - callback state
* - upper non-critical threshold
* - upper critical
* - upper non-recoverable
*/
ipmi::RspType<uint8_t, // validMask
uint8_t, // lowerNonCritical
uint8_t, // lowerCritical
uint8_t, // lowerNonRecoverable
uint8_t, // upperNonCritical
uint8_t, // upperCritical
uint8_t // upperNonRecoverable
>
ipmiSensorGetSensorThresholds(ipmi::Context::ptr& ctx, uint8_t sensorNum)
{
constexpr auto valueInterface = "xyz.openbmc_project.Sensor.Value";
const auto iter = ipmi::sensor::sensors.find(sensorNum);
if (iter == ipmi::sensor::sensors.end())
{
return ipmi::responseSensorInvalid();
}
const auto info = iter->second;
// Proceed only if the sensor value interface is implemented.
if (info.propertyInterfaces.find(valueInterface) ==
info.propertyInterfaces.end())
{
// return with valid mask as 0
return ipmi::responseSuccess();
}
auto it = sensorThresholdMap.find(sensorNum);
if (it == sensorThresholdMap.end())
{
auto resp = getSensorThresholds(ctx, sensorNum);
if (resp.validMask == 0)
{
return ipmi::responseSensorInvalid();
}
sensorThresholdMap[sensorNum] = std::move(resp);
}
const auto& resp = sensorThresholdMap[sensorNum];
return ipmi::responseSuccess(
resp.validMask, resp.lowerNonCritical, resp.lowerCritical,
resp.lowerNonRecoverable, resp.upperNonCritical, resp.upperCritical,
resp.upperNonRecoverable);
}
/** @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(
ipmi::Context::ptr& ctx, 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, uint8_t upperNonCritical,
uint8_t upperCritical, uint8_t)
{
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();
}
constexpr auto valueInterface = "xyz.openbmc_project.Sensor.Value";
const auto iter = ipmi::sensor::sensors.find(sensorNum);
if (iter == ipmi::sensor::sensors.end())
{
return ipmi::responseSensorInvalid();
}
const auto& info = iter->second;
// Proceed only if the sensor value interface is implemented.
if (info.propertyInterfaces.find(valueInterface) ==
info.propertyInterfaces.end())
{
// return with valid mask as 0
return ipmi::responseSuccess();
}
constexpr auto warningThreshIntf =
"xyz.openbmc_project.Sensor.Threshold.Warning";
constexpr auto criticalThreshIntf =
"xyz.openbmc_project.Sensor.Threshold.Critical";
std::string service;
boost::system::error_code ec;
ec = ipmi::getService(ctx, info.sensorInterface, info.sensorPath, service);
if (ec)
{
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)
{
ipmi::PropertyMap findThreshold;
ec = ipmi::getAllDbusProperties(ctx, service, info.sensorPath,
criticalThreshIntf, findThreshold);
if (!ec)
{
if (lowerCriticalThreshMask)
{
auto findLower = findThreshold.find("CriticalLow");
if (findLower == findThreshold.end())
{
return ipmi::responseInvalidFieldRequest();
}
thresholdsToSet.emplace_back("CriticalLow", lowerCritical,
criticalThreshIntf);
}
if (upperCriticalThreshMask)
{
auto findUpper = findThreshold.find("CriticalHigh");
if (findUpper == findThreshold.end())
{
return ipmi::responseInvalidFieldRequest();
}
thresholdsToSet.emplace_back("CriticalHigh", upperCritical,
criticalThreshIntf);
}
}
}
if (lowerNonCriticalThreshMask || upperNonCriticalThreshMask)
{
ipmi::PropertyMap findThreshold;
ec = ipmi::getAllDbusProperties(ctx, service, info.sensorPath,
warningThreshIntf, findThreshold);
if (!ec)
{
if (lowerNonCriticalThreshMask)
{
auto findLower = findThreshold.find("WarningLow");
if (findLower == findThreshold.end())
{
return ipmi::responseInvalidFieldRequest();
}
thresholdsToSet.emplace_back("WarningLow", lowerNonCritical,
warningThreshIntf);
}
if (upperNonCriticalThreshMask)
{
auto findUpper = findThreshold.find("WarningHigh");
if (findUpper == findThreshold.end())
{
return ipmi::responseInvalidFieldRequest();
}
thresholdsToSet.emplace_back("WarningHigh", upperNonCritical,
warningThreshIntf);
}
}
}
for (const auto& property : thresholdsToSet)
{
// from section 36.3 in the IPMI Spec, assume all linear
double valueToSet =
((info.coefficientM * std::get<thresholdValue>(property)) +
(info.scaledOffset * std::pow(10.0, info.scale))) *
std::pow(10.0, info.exponentR);
ipmi::setDbusProperty(
ctx, service, info.sensorPath, std::get<interface>(property),
std::get<propertyName>(property), ipmi::Value(valueToSet));
}
// Invalidate the cache
sensorThresholdMap.erase(sensorNum);
return ipmi::responseSuccess();
}
/** @brief implements the get SDR Info command
* @param count - Operation
*
* @returns IPMI completion code plus response data
* - sdrCount - sensor/SDR count
* - lunsAndDynamicPopulation - static/Dynamic sensor population flag
*/
ipmi::RspType<uint8_t, // respcount
uint8_t // dynamic population flags
>
ipmiSensorGetDeviceSdrInfo(std::optional<uint8_t> count)
{
uint8_t sdrCount;