1 #pragma once
2 
3 #include "libpldm/platform.h"
4 #include "libpldm/pldm.h"
5 
6 #include "common/types.hpp"
7 #include "requester/handler.hpp"
8 #include "terminus.hpp"
9 #include "terminus_manager.hpp"
10 
11 #include <map>
12 #include <memory>
13 #include <optional>
14 #include <utility>
15 #include <vector>
16 
17 namespace pldm
18 {
19 namespace platform_mc
20 {
21 
22 /**
23  * @brief SensorManager
24  *
25  * This class manages the sensors found in terminus and provides
26  * function calls for other classes to start/stop sensor monitoring.
27  *
28  */
29 class SensorManager
30 {
31   public:
32     SensorManager() = delete;
33     SensorManager(const SensorManager&) = delete;
34     SensorManager(SensorManager&&) = delete;
35     SensorManager& operator=(const SensorManager&) = delete;
36     SensorManager& operator=(SensorManager&&) = delete;
37     virtual ~SensorManager() = default;
38 
39     explicit SensorManager(sdeventplus::Event& event,
40                            TerminusManager& terminusManager,
41                            TerminiMapper& termini);
42 
43     /** @brief starting sensor polling task
44      */
45     void startPolling(pldm_tid_t tid);
46 
47     /** @brief stopping sensor polling task
48      */
49     void stopPolling(pldm_tid_t tid);
50 
51     /** @brief Set available state of terminus for pldm request.
52      */
53     void updateAvailableState(pldm_tid_t tid, Availability state)
54     {
55         availableState[tid] = state;
56     };
57 
58     /** @brief Get available state of terminus for pldm request.
59      */
60     bool getAvailableState(pldm_tid_t tid)
61     {
62         if (!availableState.contains(tid))
63         {
64             return false;
65         }
66         return availableState[tid];
67     };
68 
69   protected:
70     /** @brief start a coroutine for polling all sensors.
71      */
72     virtual void doSensorPolling(pldm_tid_t tid);
73 
74     /** @brief polling all sensors in each terminus
75      *
76      *  @param[in] tid - Destination TID
77      *  @return coroutine return_value - PLDM completion code
78      */
79     exec::task<int> doSensorPollingTask(pldm_tid_t tid);
80 
81     /** @brief Sending getSensorReading command for the sensor
82      *
83      *  @param[in] sensor - the sensor to be updated
84      *  @return coroutine return_value - PLDM completion code
85      */
86     exec::task<int> getSensorReading(std::shared_ptr<NumericSensor> sensor);
87 
88     /** @brief Reference to to PLDM daemon's main event loop.
89      */
90     sdeventplus::Event& event;
91 
92     /** @brief reference of terminusManager */
93     TerminusManager& terminusManager;
94 
95     /** @brief List of discovered termini */
96     TerminiMapper& termini;
97 
98     /** @brief sensor polling interval in ms. */
99     uint32_t pollingTime;
100 
101     /** @brief sensor polling timers */
102     std::map<pldm_tid_t, std::unique_ptr<sdbusplus::Timer>> sensorPollTimers;
103 
104     /** @brief coroutine handle of doSensorPollingTasks */
105     std::map<pldm_tid_t, std::pair<exec::async_scope, std::optional<int>>>
106         doSensorPollingTaskHandles;
107 
108     /** @brief Available state for pldm request of terminus */
109     std::map<pldm_tid_t, Availability> availableState;
110 
111     /** @brief round robin sensor list */
112     std::map<pldm_tid_t, std::queue<std::shared_ptr<NumericSensor>>>
113         roundRobinSensors;
114 };
115 } // namespace platform_mc
116 } // namespace pldm
117