1 #pragma once
2 
3 #include <tuple>
4 #include <queue>
5 #include <sdbusplus/bus.hpp>
6 #include <timer.hpp>
7 #include <host-cmd-utils.hpp>
8 
9 namespace phosphor
10 {
11 namespace host
12 {
13 namespace command
14 {
15 
16 /** @class
17  *  @brief Manages commands that are to be sent to Host
18  */
19 class Manager
20 {
21     public:
22         Manager() = delete;
23         ~Manager() = default;
24         Manager(const Manager&) = delete;
25         Manager& operator=(const Manager&) = delete;
26         Manager(Manager&&) = delete;
27         Manager& operator=(Manager&&) = delete;
28 
29         /** @brief Constructs Manager object
30          *
31          *  @param[in] bus   - dbus handler
32          *  @param[in] event - pointer to sd_event
33          */
34         Manager(sdbusplus::bus::bus& bus, sd_event* event) :
35             bus(bus),
36             timer(event, std::bind(&Manager::hostTimeout, this))
37         {
38             // Nothing to do here.
39         }
40 
41         /** @brief  Extracts the next entry in the queue and returns
42          *          Command and data part of it.
43          *
44          *  @detail Also calls into the registered handlers so that they can now
45          *          send the CommandComplete signal since the interface contract
46          *          is that we emit this signal once the message has been
47          *          passed to the host (which is required when calling this)
48          *
49          *          Also, if the queue has more commands, then it will alert the
50          *          host
51          */
52         IpmiCmdData getNextCommand();
53 
54         /** @brief  Pushes the command onto the queue.
55          *
56          *  @detail If the queue is empty, then it alerts the Host. If not,
57          *          then it returns and the API documented above will handle
58          *          the commands in Queue.
59          *
60          *  @param[in] command - tuple of <IPMI command, data, callback>
61          */
62         void execute(CommandHandler command);
63 
64     private:
65         /** @brief Check if anything in queue and alert host if so */
66         void checkQueueAndAlertHost();
67 
68         /** @brief  Call back interface on message timeouts to host.
69          *
70          *  @detail When this happens, a failure message would be sent
71          *          to all the commands that are in the Queue and queue
72          *          will be purged
73          */
74         void hostTimeout();
75 
76         /** @brief Reference to the dbus handler */
77         sdbusplus::bus::bus& bus;
78 
79         /** @brief Queue to store the requested commands */
80         std::queue<CommandHandler> workQueue{};
81 
82         /** @brief Timer for commands to host */
83         phosphor::ipmi::Timer timer;
84 };
85 
86 } // namespace command
87 } // namespace host
88 } // namespace phosphor
89