xref: /openbmc/phosphor-fan-presence/presence/psensor.hpp (revision 61b7329603e737b76b04b98746d69c1f410761b8)
1878b964aSBrad Bishop #pragma once
261749511SBrad Bishop #include <cstdint>
3c65d91d6SMatt Spinler #include <string>
4878b964aSBrad Bishop 
5878b964aSBrad Bishop namespace phosphor
6878b964aSBrad Bishop {
7878b964aSBrad Bishop namespace fan
8878b964aSBrad Bishop {
9878b964aSBrad Bishop namespace presence
10878b964aSBrad Bishop {
11878b964aSBrad Bishop 
12878b964aSBrad Bishop /**
13878b964aSBrad Bishop  * @class PresenceSensor
14878b964aSBrad Bishop  * @brief PresenceSensor interface.
15878b964aSBrad Bishop  *
16878b964aSBrad Bishop  * Provide concrete implementations of PresenceSensor to realize
17878b964aSBrad Bishop  * new presence detection methods.
18878b964aSBrad Bishop  *
19878b964aSBrad Bishop  * Note that implementations drive the inventory update process via
20878b964aSBrad Bishop  * a redundancy policy (rpolicy.hpp) - it is not enough to implement
21878b964aSBrad Bishop  * the interfaces below.
22878b964aSBrad Bishop  */
23878b964aSBrad Bishop class PresenceSensor
24878b964aSBrad Bishop {
25878b964aSBrad Bishop   public:
26878b964aSBrad Bishop     PresenceSensor(const PresenceSensor&) = default;
27878b964aSBrad Bishop     PresenceSensor& operator=(const PresenceSensor&) = default;
28878b964aSBrad Bishop     PresenceSensor(PresenceSensor&&) = default;
29878b964aSBrad Bishop     PresenceSensor& operator=(PresenceSensor&&) = default;
30878b964aSBrad Bishop     virtual ~PresenceSensor() = default;
PresenceSensor()3161749511SBrad Bishop     PresenceSensor() : id(nextId)
3261749511SBrad Bishop     {
3361749511SBrad Bishop         nextId++;
3461749511SBrad Bishop     }
35878b964aSBrad Bishop 
36878b964aSBrad Bishop     /**
37878b964aSBrad Bishop      * @brief start
38878b964aSBrad Bishop      *
39878b964aSBrad Bishop      * Implementations should peform any preparation
40878b964aSBrad Bishop      * for detecting presence.  Typical implementations
41878b964aSBrad Bishop      * might register signal callbacks or start
42878b964aSBrad Bishop      * a polling loop.
43878b964aSBrad Bishop      *
44878b964aSBrad Bishop      * @return The state of the sensor.
45878b964aSBrad Bishop      */
46878b964aSBrad Bishop     virtual bool start() = 0;
47878b964aSBrad Bishop 
48878b964aSBrad Bishop     /**
49878b964aSBrad Bishop      * @brief stop
50878b964aSBrad Bishop      *
51878b964aSBrad Bishop      * Implementations should stop issuing presence
52878b964aSBrad Bishop      * state change notifications.  Typical implementations
53878b964aSBrad Bishop      * might de-register signal callbacks or terminate
54878b964aSBrad Bishop      * polling loops.
55878b964aSBrad Bishop      */
56878b964aSBrad Bishop     virtual void stop() = 0;
57878b964aSBrad Bishop 
58878b964aSBrad Bishop     /**
59878b964aSBrad Bishop      * @brief Check the sensor.
60878b964aSBrad Bishop      *
61878b964aSBrad Bishop      * Implementations should perform an offline (the start
62878b964aSBrad Bishop      * method has not been invoked) query of the presence
63878b964aSBrad Bishop      * state.
64878b964aSBrad Bishop      *
65878b964aSBrad Bishop      * @return The state of the sensor.
66878b964aSBrad Bishop      */
67878b964aSBrad Bishop     virtual bool present() = 0;
68878b964aSBrad Bishop 
69878b964aSBrad Bishop     /**
70878b964aSBrad Bishop      * @brief Mark the sensor as failed.
71878b964aSBrad Bishop      *
72878b964aSBrad Bishop      * Implementations should log an an event if the
73878b964aSBrad Bishop      * system policy requires it.
74878b964aSBrad Bishop      *
75878b964aSBrad Bishop      * Provide a default noop implementation.
76878b964aSBrad Bishop      */
fail()77*61b73296SPatrick Williams     virtual void fail() {}
7861749511SBrad Bishop 
7961749511SBrad Bishop     friend bool operator==(const PresenceSensor& l, const PresenceSensor& r);
8061749511SBrad Bishop 
81c65d91d6SMatt Spinler     /**
82c65d91d6SMatt Spinler      * @brief Called when this presence sensor doesn't agree with other ones.
83c65d91d6SMatt Spinler      *
84c65d91d6SMatt Spinler      * @param[in] fanInventoryPath - The fan inventory D-Bus object path.
85c65d91d6SMatt Spinler      */
86c65d91d6SMatt Spinler     virtual void logConflict(const std::string& fanInventoryPath) const = 0;
87c65d91d6SMatt Spinler 
8861749511SBrad Bishop   private:
8961749511SBrad Bishop     /** @brief Unique sensor ID. */
9061749511SBrad Bishop     std::size_t id;
9161749511SBrad Bishop 
9261749511SBrad Bishop     /** @brief The next unique sensor ID. */
9361749511SBrad Bishop     static std::size_t nextId;
94878b964aSBrad Bishop };
95878b964aSBrad Bishop 
operator ==(const PresenceSensor & l,const PresenceSensor & r)9661749511SBrad Bishop inline bool operator==(const PresenceSensor& l, const PresenceSensor& r)
9761749511SBrad Bishop {
9861749511SBrad Bishop     return l.id == r.id;
9961749511SBrad Bishop }
10061749511SBrad Bishop 
101878b964aSBrad Bishop } // namespace presence
102878b964aSBrad Bishop } // namespace fan
103878b964aSBrad Bishop } // namespace phosphor
104