| Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 1 | #pragma once | 
 | 2 |  | 
 | 3 | #include "types.hpp" | 
 | 4 | #include "property_change_listener.hpp" | 
 | 5 |  | 
 | 6 | #include <sdbusplus/bus.hpp> | 
 | 7 | #include <sdbusplus/bus/match.hpp> | 
 | 8 |  | 
 | 9 | #include <set> | 
 | 10 | #include <string> | 
 | 11 |  | 
 | 12 | namespace phosphor | 
 | 13 | { | 
 | 14 | namespace time | 
 | 15 | { | 
 | 16 |  | 
 | 17 | /** @class Manager | 
 | 18 |  *  @brief The manager to handle OpenBMC time. | 
 | 19 |  *  @details It registers various time related settings and properties signals | 
 | 20 |  *  on DBus and handle the changes. | 
 | 21 |  *  For certain properties it also notifies the changed events to listeners. | 
 | 22 |  */ | 
 | 23 | class Manager | 
 | 24 | { | 
 | 25 |     public: | 
 | 26 |         friend class TestManager; | 
 | 27 |         explicit Manager(sdbusplus::bus::bus& bus); | 
| Lei YU | c6fe869 | 2017-02-23 15:12:07 +0800 | [diff] [blame^] | 28 |         Manager(const Manager&) = delete; | 
 | 29 |         Manager& operator=(const Manager&) = delete; | 
 | 30 |         Manager(Manager&&) = delete; | 
 | 31 |         Manager& operator=(Manager&&) = delete; | 
| Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 32 |  | 
 | 33 |         /** @brief Add a listener that will be called | 
 | 34 |           * when property is changed | 
 | 35 |          **/ | 
 | 36 |         void addListener(PropertyChangeListner* listener); | 
 | 37 |  | 
 | 38 |     private: | 
 | 39 |         /** @brief Persistent sdbusplus DBus connection */ | 
 | 40 |         sdbusplus::bus::bus& bus; | 
 | 41 |  | 
 | 42 |         /** @brief The match of settings property change */ | 
 | 43 |         sdbusplus::bus::match::match propertyChangeMatch; | 
 | 44 |  | 
| Lei YU | c6fe869 | 2017-02-23 15:12:07 +0800 | [diff] [blame^] | 45 |         /** @brief The match of pgood change */ | 
 | 46 |         sdbusplus::bus::match::match pgoodChangeMatch; | 
 | 47 |  | 
| Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 48 |         /** @brief The container to hold all the listeners */ | 
 | 49 |         std::set<PropertyChangeListner*> listeners; | 
 | 50 |  | 
| Lei YU | c6fe869 | 2017-02-23 15:12:07 +0800 | [diff] [blame^] | 51 |         /** @brief The value to indicate if host is on */ | 
 | 52 |         bool hostOn = false; | 
 | 53 |  | 
| Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 54 |         /** @brief The current time mode */ | 
 | 55 |         Mode timeMode; | 
 | 56 |  | 
 | 57 |         /** @brief The current time owner */ | 
 | 58 |         Owner timeOwner; | 
 | 59 |  | 
| Lei YU | c6fe869 | 2017-02-23 15:12:07 +0800 | [diff] [blame^] | 60 |         /** @brief Check if host is on and update hostOn variable */ | 
 | 61 |         void checkHostOn(); | 
 | 62 |  | 
| Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 63 |         /** @brief Get setting from settingsd service | 
 | 64 |          * | 
 | 65 |          * @param[in] setting - The string of the setting | 
 | 66 |          * | 
 | 67 |          * @return The setting value in string | 
 | 68 |          */ | 
 | 69 |         std::string getSettings(const char* setting) const; | 
 | 70 |  | 
 | 71 |         /** @brief Set current time mode | 
 | 72 |          * | 
 | 73 |          * @param[in] mode - The string of time mode | 
 | 74 |          */ | 
 | 75 |         void setCurrentTimeMode(const std::string& mode); | 
 | 76 |  | 
 | 77 |         /** @brief Set current time owner | 
 | 78 |          * | 
 | 79 |          * @param[in] owner - The string of time owner | 
 | 80 |          */ | 
 | 81 |         void setCurrentTimeOwner(const std::string& owner); | 
 | 82 |  | 
 | 83 |         /** @brief Notified on settings property changed | 
 | 84 |          * | 
 | 85 |          * @param[in] key - The name of property that is changed | 
 | 86 |          * @param[in] value - The value of the property | 
 | 87 |          */ | 
 | 88 |         void onPropertyChanged(const std::string& key, | 
 | 89 |                                const std::string& value); | 
 | 90 |  | 
| Lei YU | c6fe869 | 2017-02-23 15:12:07 +0800 | [diff] [blame^] | 91 |         /** @brief Notified on pgood has changed | 
 | 92 |          * | 
 | 93 |          * @param[in] pgood - The changed pgood value | 
 | 94 |          */ | 
 | 95 |         void onPgoodChanged(bool pgood); | 
 | 96 |  | 
| Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 97 |         /** @brief The static function called on settings property changed | 
 | 98 |          * | 
 | 99 |          * @param[in] msg - Data associated with subscribed signal | 
 | 100 |          * @param[in] userData - Pointer to this object instance | 
 | 101 |          * @param[out] retError  - Not used but required with signal API | 
 | 102 |          */ | 
 | 103 |         static int onPropertyChanged(sd_bus_message* msg, | 
 | 104 |                                      void* userData, | 
 | 105 |                                      sd_bus_error* retError); | 
 | 106 |  | 
| Lei YU | c6fe869 | 2017-02-23 15:12:07 +0800 | [diff] [blame^] | 107 |         /** @brief Notified on pgood has changed | 
 | 108 |          * | 
 | 109 |          * @param[in] msg - Data associated with subscribed signal | 
 | 110 |          * @param[in] userData - Pointer to this object instance | 
 | 111 |          * @param[out] retError  - Not used but required with signal API | 
 | 112 |          */ | 
 | 113 |         static int onPgoodChanged(sd_bus_message* msg, | 
 | 114 |                                   void* userData, | 
 | 115 |                                   sd_bus_error* retError); | 
 | 116 |  | 
| Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 117 |         /** @brief Convert a string to enum Mode | 
 | 118 |          * | 
 | 119 |          * Convert the time mode string to enum. | 
 | 120 |          * Valid strings are "NTP", "MANUAL" | 
 | 121 |          * If it's not a valid time mode string, return NTP. | 
 | 122 |          * | 
 | 123 |          * @param[in] mode - The string of time mode | 
 | 124 |          * | 
 | 125 |          * @return The Mode enum | 
 | 126 |          */ | 
 | 127 |         static Mode convertToMode(const std::string& mode); | 
 | 128 |  | 
 | 129 |         /** @brief Convert a string to enum Owner | 
 | 130 |          * | 
 | 131 |          * Convert the time owner string to enum. | 
 | 132 |          * Valid strings are "BMC", "HOST", "SPLIT", "BOTH" | 
 | 133 |          * If it's not a valid time owner string, return BMC. | 
 | 134 |          * | 
 | 135 |          * @param[in] owner - The string of time owner | 
 | 136 |          * | 
 | 137 |          * @return The Owner enum | 
 | 138 |          */ | 
 | 139 |         static Owner convertToOwner(const std::string& owner); | 
 | 140 |  | 
 | 141 |         using Updater = std::function<void(const std::string&)>; | 
 | 142 |  | 
 | 143 |         /** @brief Map the property string to functions that shall | 
 | 144 |          *  be called when the property is changed | 
 | 145 |          */ | 
 | 146 |         const std::map<std::string, Updater> propertyUpdaters = | 
 | 147 |         { | 
 | 148 |             {"time_mode", std::bind(&Manager::setCurrentTimeMode, | 
 | 149 |                                     this, std::placeholders::_1)}, | 
 | 150 |             {"time_owner", std::bind(&Manager::setCurrentTimeOwner, | 
 | 151 |                                      this, std::placeholders::_1)} | 
 | 152 |         }; | 
 | 153 |  | 
 | 154 |         /** @brief The properties that manager shall notify the | 
 | 155 |          *  listeners when changed | 
 | 156 |          */ | 
 | 157 |         static const std::set<std::string> managedProperties; | 
 | 158 |  | 
 | 159 |         /** @brief The map that maps the string to Owners */ | 
 | 160 |         static const std::map<std::string, Owner> ownerMap; | 
 | 161 | }; | 
 | 162 |  | 
 | 163 | } | 
 | 164 | } |