xref: /openbmc/phosphor-fan-presence/presence/fan.cpp (revision 11fc0a7a596b2fd24d42e6b0ef3e4c956ef36e60)
1 /**
2  * Copyright © 2017 IBM Corporation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #include <map>
17 #include <sdbusplus/message.hpp>
18 #include <string>
19 #include "fan.hpp"
20 #include "sdbusplus.hpp"
21 
22 namespace phosphor
23 {
24 namespace fan
25 {
26 namespace presence
27 {
28 
29 using namespace std::literals::string_literals;
30 
31 static const auto invNamespace = "/xyz/openbmc_project/inventory"s;
32 static const auto itemIface = "xyz.openbmc_project.Inventory.Item"s;
33 static const auto invMgrIface = "xyz.openbmc_project.Inventory.Manager"s;
34 static const auto fanIface = "xyz.openbmc_project.Inventory.Item.Fan"s;
35 
36 void setPresence(const Fan& fan, bool newState)
37 {
38     using namespace sdbusplus::message;
39 
40     using Properties = std::map<std::string, variant<std::string, bool>>;
41     using Interfaces = std::map<std::string, Properties>;
42 
43     std::map<object_path, Interfaces> obj =
44     {{
45         std::get<1>(fan),
46         {{
47             itemIface,
48             {
49                 {"Present"s, newState},
50                 {"PrettyName"s, std::get<0>(fan)},
51             }
52         },
53         {
54             fanIface, {}
55         }},
56     }};
57 
58     util::SDBusPlus::lookupAndCallMethod(
59             invNamespace,
60             invMgrIface,
61             "Notify"s,
62             obj);
63 }
64 
65 bool getPresence(const Fan& fan)
66 {
67     return util::SDBusPlus::getProperty<bool>(
68             std::get<1>(fan),
69             itemIface,
70             "Present"s);
71 }
72 
73 } // namespace presence
74 } // namespace fan
75 } // namespace phosphor
76