-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathchassis_status_monitor.hpp
More file actions
806 lines (723 loc) · 23.3 KB
/
chassis_status_monitor.hpp
File metadata and controls
806 lines (723 loc) · 23.3 KB
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
/**
* Copyright © 2025 IBM 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.
*/
#pragma once
#include "types.hpp"
#include "utility.hpp"
#include <stddef.h> // for size_t
#include <sdbusplus/bus.hpp>
#include <sdbusplus/bus/match.hpp>
#include <sdbusplus/message.hpp>
#include <xyz/openbmc_project/State/Decorator/PowerSystemInputs/server.hpp>
#include <functional>
#include <memory>
#include <optional>
#include <stdexcept>
#include <string>
#include <vector>
namespace phosphor::power::util
{
using PowerSystemInputs = sdbusplus::server::xyz::openbmc_project::state::
decorator::PowerSystemInputs;
/**
* @struct ChassisStatusMonitorOptions
*
* Options that define what types of monitoring are enabled in a
* ChassisStatusMonitor object.
*/
struct ChassisStatusMonitorOptions
{
/**
* Specifies whether to monitor the Present property in the
* xyz.openbmc_project.Inventory.Item interface for the chassis inventory
* path.
*/
bool isPresentMonitored{false};
/**
* Specifies whether to monitor the Available property in the
* xyz.openbmc_project.State.Decorator.Availability interface for the
* chassis inventory path.
*/
bool isAvailableMonitored{false};
/**
* Specifies whether to monitor the Enabled property in the
* xyz.openbmc_project.Object.Enable interface for the chassis inventory
* path.
*/
bool isEnabledMonitored{false};
/**
* Specifies whether to monitor the state property in the
* org.openbmc.control.Power interface for the chassis.
*/
bool isPowerStateMonitored{false};
/**
* Specifies whether to monitor the pgood property in the
* org.openbmc.control.Power interface for the chassis.
*/
bool isPowerGoodMonitored{false};
/**
* Specifies whether to monitor the Status property in the
* xyz.openbmc_project.State.Decorator.PowerSystemInputs interface for the
* status of input power to the chassis.
*/
bool isInputPowerStatusMonitored{false};
/**
* Specifies whether to monitor the Status property in the
* xyz.openbmc_project.State.Decorator.PowerSystemInputs interface for the
* status of power supplies power to the chassis.
*/
bool isPowerSuppliesStatusMonitored{false};
};
/**
* @class ChassisStatusMonitor
*
* Abstract base class for monitoring the state of a chassis.
*
* The chassis state is monitored using D-Bus interfaces and properties. The
* types of monitoring are configured by specifying a
* ChassisStatusMonitorOptions instance to the sub-class constructor.
*/
class ChassisStatusMonitor
{
public:
ChassisStatusMonitor() = default;
ChassisStatusMonitor(const ChassisStatusMonitor&) = delete;
ChassisStatusMonitor(ChassisStatusMonitor&&) = delete;
ChassisStatusMonitor& operator=(const ChassisStatusMonitor&) = delete;
ChassisStatusMonitor& operator=(ChassisStatusMonitor&&) = delete;
virtual ~ChassisStatusMonitor() = default;
/**
* Returns the chassis number being monitored.
*
* Chassis numbers start at 1 since chassis 0 represents the entire system.
*
* @return chassis number
*/
virtual size_t getNumber() const = 0;
/**
* Returns the chassis inventory path being monitored.
*
* @return chassis inventory path
*/
virtual const std::string& getInventoryPath() const = 0;
/**
* Returns the options defining which types of monitoring are enabled.
*
* @return monitoring options
*/
virtual const ChassisStatusMonitorOptions& getOptions() const = 0;
/**
* Returns whether the chassis is present.
*
* Returns true if monitoring this property is not enabled.
*
* Throws an exception if the property value could not be obtained.
*
* @return true is chassis is present, false otherwise
*/
virtual bool isPresent() = 0;
/**
* Returns whether the chassis is available.
*
* If the Available property is false, it means that communication to the
* chassis is not possible. For example, the chassis does not have any input
* power or communication cables to the BMC are disconnected.
*
* Returns true if monitoring this property is not enabled.
*
* Throws an exception if the property value could not be obtained.
*
* @return true is chassis is available, false otherwise
*/
virtual bool isAvailable() = 0;
/**
* Returns whether the chassis is enabled.
*
* If the Enabled property is false, it means that the chassis has been put
* in hardware isolation (guarded).
*
* Returns true if monitoring this property is not enabled.
*
* Throws an exception if the property value could not be obtained.
*
* @return true is chassis is enabled, false otherwise
*/
virtual bool isEnabled() = 0;
/**
* Returns the chassis power state.
*
* This is the last requested power state.
*
* Throws an exception if monitoring this property is not enabled or the
* property value could not be obtained.
*
* @return 0 if power off requested, 1 if power on requested
*/
virtual int getPowerState() = 0;
/**
* Returns the chassis power good status.
*
* This indicates whether the chassis has been successfully powered on
* from a hardware perspective (chassis pgood asserted).
*
* Throws an exception if monitoring this property is not enabled or the
* property value could not be obtained.
*
* @return 0 if chassis is powered off, 1 is chassis powered on
*/
virtual int getPowerGood() = 0;
/**
* Returns whether this chassis is powered on.
*
* Requires both power good and power state monitoring.
*
* Throws an exception if power good or power state monitoring is not
* enabled. Throws an exception if the property values could not be
* obtained.
*
* @return true if chassis is powered on, false otherwise
*/
virtual bool isPoweredOn() = 0;
/**
* Returns whether this chassis is powered off.
*
* Requires both power good and power state monitoring.
*
* Throws an exception if power good or power state monitoring is not
* enabled. Throws an exception if the property values could not be
* obtained.
*
* @return true if chassis is powered off, false otherwise
*/
virtual bool isPoweredOff() = 0;
/**
* Returns the chassis input power status.
*
* Returns PowerSystemInputs::Status::Good if this monitoring is not
* enabled.
*
* Throws an exception if the property value could not be obtained.
*
* @return chassis input power status
*/
virtual PowerSystemInputs::Status getInputPowerStatus() = 0;
/**
* Returns whether the chassis input power status is good.
*
* Returns true if this monitoring is not enabled.
*
* Throws an exception if the property value could not be obtained.
*
* @return true if chassis input power is good, false otherwise
*/
virtual bool isInputPowerGood() = 0;
/**
* Returns the power supplies power status.
*
* Returns PowerSystemInputs::Status::Good if this monitoring is not
* enabled.
*
* Throws an exception if the property value could not be obtained.
*
* @return power supplies power status
*/
virtual PowerSystemInputs::Status getPowerSuppliesStatus() = 0;
/**
* Returns whether the power supplies power status is good.
*
* Returns true if this monitoring is not enabled.
*
* Throws an exception if the property value could not be obtained.
*
* @return true if power supplies power is good, false otherwise
*/
virtual bool isPowerSuppliesPowerGood() = 0;
};
/**
* @class BMCChassisStatusMonitor
*
* Implementation of the ChassisStatusMonitor interface using the standard BMC
* APIs.
*/
class BMCChassisStatusMonitor : public ChassisStatusMonitor
{
public:
BMCChassisStatusMonitor() = delete;
BMCChassisStatusMonitor(const BMCChassisStatusMonitor&) = delete;
BMCChassisStatusMonitor(BMCChassisStatusMonitor&&) = delete;
BMCChassisStatusMonitor& operator=(const BMCChassisStatusMonitor&) = delete;
BMCChassisStatusMonitor& operator=(BMCChassisStatusMonitor&&) = delete;
virtual ~BMCChassisStatusMonitor() = default;
/**
* Constructor.
*
* @param bus D-Bus bus object.
* @param number Chassis number within the system. Must be >= 1.
* @param inventoryPath D-Bus inventory path of the chassis.
* @param options Options that specify what types of monitoring are enabled.
*/
BMCChassisStatusMonitor(sdbusplus::bus_t& bus, size_t number,
const std::string& inventoryPath,
const ChassisStatusMonitorOptions& options);
/** @copydoc ChassisStatusMonitor::getNumber() */
virtual size_t getNumber() const override
{
return number;
}
/** @copydoc ChassisStatusMonitor::getInventoryPath() */
virtual const std::string& getInventoryPath() const override
{
return inventoryPath;
}
/** @copydoc ChassisStatusMonitor::getOptions() */
virtual const ChassisStatusMonitorOptions& getOptions() const override
{
return options;
}
/** @copydoc ChassisStatusMonitor::isPresent() */
virtual bool isPresent() override
{
if (!options.isPresentMonitored)
{
return true;
}
if (!isPresentValue)
{
throw std::runtime_error{
"Present property value could not be obtained."};
}
return *isPresentValue;
}
/** @copydoc ChassisStatusMonitor::isAvailable() */
virtual bool isAvailable() override
{
if (!options.isAvailableMonitored)
{
return true;
}
if (!isAvailableValue)
{
throw std::runtime_error{
"Available property value could not be obtained."};
}
return *isAvailableValue;
}
/** @copydoc ChassisStatusMonitor::isEnabled() */
virtual bool isEnabled() override
{
if (!options.isEnabledMonitored)
{
return true;
}
if (!isEnabledValue)
{
throw std::runtime_error{
"Enabled property value could not be obtained."};
}
return *isEnabledValue;
}
/** @copydoc ChassisStatusMonitor::getPowerState() */
virtual int getPowerState() override
{
if (!options.isPowerStateMonitored)
{
throw std::runtime_error{
"Power state property value is not being monitored."};
}
if (!powerStateValue)
{
throw std::runtime_error{
"Power state property value could not be obtained."};
}
return *powerStateValue;
}
/** @copydoc ChassisStatusMonitor::getPowerGood() */
virtual int getPowerGood() override
{
if (!options.isPowerGoodMonitored)
{
throw std::runtime_error{
"Power good property value is not being monitored."};
}
if (!powerGoodValue)
{
throw std::runtime_error{
"Power good property value could not be obtained."};
}
return *powerGoodValue;
}
/** @copydoc ChassisStatusMonitor::isPoweredOn() */
virtual bool isPoweredOn() override
{
return ((getPowerState() == 1) && (getPowerGood() == 1));
}
/** @copydoc ChassisStatusMonitor::isPoweredOff() */
virtual bool isPoweredOff() override
{
return ((getPowerState() == 0) && (getPowerGood() == 0));
}
/** @copydoc ChassisStatusMonitor::getInputPowerStatus() */
virtual PowerSystemInputs::Status getInputPowerStatus() override
{
if (!options.isInputPowerStatusMonitored)
{
return PowerSystemInputs::Status::Good;
}
if (!inputPowerStatusValue)
{
throw std::runtime_error{
"Input power Status property value could not be obtained."};
}
return PowerSystemInputs::convertStatusFromString(
*inputPowerStatusValue);
}
/** @copydoc ChassisStatusMonitor::isInputPowerGood() */
virtual bool isInputPowerGood() override
{
return (getInputPowerStatus() == PowerSystemInputs::Status::Good);
}
/** @copydoc ChassisStatusMonitor::getPowerSuppliesStatus() */
virtual PowerSystemInputs::Status getPowerSuppliesStatus() override
{
if (!options.isPowerSuppliesStatusMonitored)
{
return PowerSystemInputs::Status::Good;
}
if (!powerSuppliesStatusValue)
{
throw std::runtime_error{
"Power supplies power Status property value could not be obtained."};
}
return PowerSystemInputs::convertStatusFromString(
*powerSuppliesStatusValue);
}
/** @copydoc ChassisStatusMonitor::isPowerSuppliesPowerGood() */
virtual bool isPowerSuppliesPowerGood() override
{
return (getPowerSuppliesStatus() == PowerSystemInputs::Status::Good);
}
/**
* Returns the D-Bus interface for the Present property.
*
* @return D-Bus interface
*/
const std::string& getPresentInterface() const
{
static const std::string inventoryIface{INVENTORY_IFACE};
return inventoryIface;
}
/**
* Returns the D-Bus interface for the Available property.
*
* @return D-Bus interface
*/
const std::string& getAvailableInterface() const
{
static const std::string availabilityIface{AVAILABILITY_IFACE};
return availabilityIface;
}
/**
* Returns the D-Bus interface for the Enabled property.
*
* @return D-Bus interface
*/
const std::string& getEnabledInterface() const
{
static const std::string enableIface{ENABLE_IFACE};
return enableIface;
}
/**
* Returns the D-Bus chassis power path.
*
* This path is used for both PowerState and PowerGood properties.
*
* @return D-Bus chassis power path
*/
const std::string& getChassisPowerPath() const
{
return chassisPowerPath;
}
/**
* Returns the D-Bus power interface.
*
* This interface is used for both PowerState and PowerGood properties.
*
* @return D-Bus power interface
*/
const std::string& getPowerInterface() const
{
static const std::string powerIface{POWER_IFACE};
return powerIface;
}
/**
* Returns the D-Bus object path for the InputPowerStatus property.
*
* @return D-Bus object path
*/
const std::string& getInputPowerStatusPath() const
{
return chassisInputPowerStatusPath;
}
/**
* Returns the D-Bus object path for the PowerSuppliesStatus property.
*
* @return D-Bus object path
*/
const std::string& getPowerSuppliesStatusPath() const
{
return powerSuppliesStatusPath;
}
/**
* Returns the D-Bus interface for the PowerSystemInputs properties.
*
* This interface is used for both InputPowerStatus and
* PowerSuppliesStatus properties.
*
* @return D-Bus interface
*/
const std::string& getPowerSystemInputsInterface() const
{
static const std::string powerSystemInputsIface{
POWER_SYSTEM_INPUTS_IFACE};
return powerSystemInputsIface;
}
protected:
/**
* Add a NameOwnerChanged match for the specified service.
*
* @param service D-Bus service
*/
void addNameOwnerChangedMatch(const std::string& service)
{
matches.emplace_back(std::make_unique<sdbusplus::bus::match_t>(
bus, sdbusplus::bus::match::rules::nameOwnerChanged(service),
std::bind_front(&BMCChassisStatusMonitor::nameOwnerChangedCallback,
this)));
}
/**
* Add an InterfacesAdded match for the specified D-Bus path.
*
* @param path D-Bus path
*/
void addInterfacesAddedMatch(const std::string& path)
{
matches.emplace_back(std::make_unique<sdbusplus::bus::match_t>(
bus, sdbusplus::bus::match::rules::interfacesAddedAtPath(path),
std::bind_front(&BMCChassisStatusMonitor::interfacesAddedCallback,
this)));
}
/**
* Add an PropertiesChanged match for the specified D-Bus path and
* interface.
*
* @param path D-Bus path
* @param interface D-Bus interface
*/
void addPropertiesChangedMatch(const std::string& path,
const std::string& interface)
{
matches.emplace_back(std::make_unique<sdbusplus::bus::match_t>(
bus,
sdbusplus::bus::match::rules::propertiesChanged(path, interface),
std::bind_front(&BMCChassisStatusMonitor::propertiesChangedCallback,
this)));
}
/**
* Add D-Bus matches to get signals for NameOwnerChanged, InterfacesAdded,
* and PropertiesChanged.
*/
void addMatches();
/**
* Try to get the specified D-Bus property.
*
* If an error occurs, it is ignored and the specified optionalValue is not
* modified.
*
* @param service D-Bus service
* @param path D-Bus path
* @param interface D-Bus interface
* @param propertyName D-Bus property name
* @param optionalValue Property value is stored here if obtained
*/
template <typename T>
void getProperty(const std::string& service, const std::string& path,
const std::string& interface,
const std::string& propertyName,
std::optional<T>& optionalValue);
/**
* Try to get properties from the inventory manager service.
*
* If an error occurs, it is ignored and the property values remain
* undefined.
*/
void getInventoryManagerProperties();
/**
* Try to get properties from the power sequencer service.
*
* If an error occurs, it is ignored and the property values remain
* undefined.
*/
void getPowerSequencerProperties();
/**
* Try to get properties from the chassis input power service.
*
* If an error occurs, it is ignored and the property values remain
* undefined.
*/
void getChassisInputPowerProperties();
/**
* Try to get properties from the power supply service.
*
* If an error occurs, it is ignored and the property values remain
* undefined.
*/
void getPowerSupplyProperties();
/**
* Try to get all properties that are being monitored.
*
* If an error occurs, it is ignored and the property values remain
* undefined.
*/
void getAllProperties()
{
getInventoryManagerProperties();
getPowerSequencerProperties();
getChassisInputPowerProperties();
getPowerSupplyProperties();
}
/**
* Stores the value of the specified property.
*
* Does nothing if the property name is not found in the properties map.
*
* @param properties Map of D-Bus property names and values
* @param propertyName Property name to store
* @param optionalValue Property value is stored in this parameter
*/
template <typename T>
void storeProperty(const DbusPropertyMap& properties,
const std::string& propertyName,
std::optional<T>& optionalValue);
/**
* Stores the values of all relevant interface properties found in the
* properties map.
*
* Does nothing if no relevant properties found.
*
* @param properties Map of D-Bus property names and values
* @param path D-Bus path
* @param interface D-Bus interface
*/
void storeProperties(const DbusPropertyMap& properties,
const std::string& path, const std::string& interface);
/**
* Callback function for NameOwnerChanged D-Bus signals.
*
* @param message D-Bus message
*/
void nameOwnerChangedCallback(sdbusplus::message_t& message);
/**
* Callback function for InterfacesAdded D-Bus signals.
*
* @param message D-Bus message
*/
void interfacesAddedCallback(sdbusplus::message_t& message);
/**
* Callback function for PropertiesChanged D-Bus signals.
*
* @param message D-Bus message
*/
void propertiesChangedCallback(sdbusplus::message_t& message);
/**
* D-Bus bus object.
*/
sdbusplus::bus_t& bus;
/**
* Chassis number within the system.
*
* Chassis numbers start at 1 because chassis 0 represents the entire
* system.
*/
size_t number{1};
/**
* D-Bus inventory path of the chassis.
*/
std::string inventoryPath{};
/**
* Options that define what types of monitoring are enabled.
*/
ChassisStatusMonitorOptions options{};
/**
* D-Bus path where the state and pgood properties exist for this chassis.
*/
std::string chassisPowerPath{};
/**
* D-Bus path where the input power Status property exists for this chassis.
*/
std::string chassisInputPowerStatusPath{};
/**
* D-Bus path where the power supplies power Status property exists for this
* chassis.
*/
std::string powerSuppliesStatusPath{};
/**
* Present property value.
*
* Contains no value if the property value has not been obtained.
*/
std::optional<bool> isPresentValue{};
/**
* Available property value.
*
* Contains no value if the property value has not been obtained.
*/
std::optional<bool> isAvailableValue{};
/**
* Enabled property value.
*
* Contains no value if the property value has not been obtained.
*/
std::optional<bool> isEnabledValue{};
/**
* Power state property value.
*
* Contains no value if the property value has not been obtained.
*/
std::optional<int> powerStateValue{};
/**
* Power good (pgood) property value.
*
* Contains no value if the property value has not been obtained.
*/
std::optional<int> powerGoodValue{};
/**
* Input power Status property value represented as a string.
*
* Contains no value if the property value has not been obtained.
*/
std::optional<std::string> inputPowerStatusValue{};
/**
* Power supplies power Status property value represented as a string.
*
* Contains no value if the property value has not been obtained.
*/
std::optional<std::string> powerSuppliesStatusValue{};
/**
* Match objects created to get NameOwnerChanged, InterfacesAdded, and
* PropertiesChanged signals.
*/
std::vector<std::unique_ptr<sdbusplus::bus::match_t>> matches{};
};
} // namespace phosphor::power::util