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 "zone.hpp"
17 
18 namespace phosphor
19 {
20 namespace fan
21 {
22 namespace control
23 {
24 
25 
26 Zone::Zone(sdbusplus::bus::bus& bus,
27            const ZoneDefinition& def) :
28     _bus(bus),
29     _fullSpeed(std::get<fullSpeedPos>(def)),
30     _zoneNum(std::get<zoneNumPos>(def))
31 {
32     auto& fanDefs = std::get<fanListPos>(def);
33 
34     for (auto& def : fanDefs)
35     {
36         _fans.emplace_back(std::make_unique<Fan>(bus, def));
37     }
38 
39     // Setup signal trigger for set speed events
40     for (auto& event : std::get<setSpeedEventsPos>(def))
41     {
42         for (auto& prop : std::get<propChangeListPos>(event))
43         {
44             _signalEvents.emplace_back(
45                 std::make_unique<SignalEvent>(this,
46                                               EventData
47                                               {
48                                                   std::get<groupPos>(event),
49                                                   std::get<handlerObjPos>(prop),
50                                                   std::get<actionPos>(event)
51                                               }));
52             _matches.emplace_back(bus,
53                                   std::get<signaturePos>(prop).c_str(),
54                                   signalHandler,
55                                   _signalEvents.back().get());
56         }
57     }
58 }
59 
60 
61 void Zone::setSpeed(uint64_t speed)
62 {
63     for (auto& fan : _fans)
64     {
65         fan->setSpeed(speed);
66     }
67 }
68 
69 int Zone::signalHandler(sd_bus_message* msg,
70                         void* data,
71                         sd_bus_error* err)
72 {
73     auto sdbpMsg = sdbusplus::message::message(msg);
74     auto& signalEvent = *static_cast<SignalEvent*>(data);
75     std::get<zoneObjPos>(signalEvent)->handleEvent(
76         sdbpMsg,
77         std::get<eventDataPos>(signalEvent));
78     return 0;
79 }
80 
81 void Zone::handleEvent(sdbusplus::message::message& msg,
82                        const EventData& eventData)
83 {
84     // Handle the callback
85     std::get<eventHandlerPos>(eventData)(_bus, msg, *this);
86     // Perform the action
87     std::get<eventActionPos>(eventData)(*this,
88                                         std::get<eventGroupPos>(eventData));
89 }
90 
91 }
92 }
93 }
94