blob: 35c7d2be86f8d84931b13a2e3f10778fbbdd516d [file] [log] [blame]
Matt Spinler7f88fe62017-04-10 14:39:02 -05001/**
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
18namespace phosphor
19{
20namespace fan
21{
22namespace control
23{
24
25
Matthew Barth14184132017-05-19 14:37:30 -050026Zone::Zone(Mode mode,
27 sdbusplus::bus::bus& bus,
Matt Spinler7f88fe62017-04-10 14:39:02 -050028 const ZoneDefinition& def) :
29 _bus(bus),
30 _fullSpeed(std::get<fullSpeedPos>(def)),
31 _zoneNum(std::get<zoneNumPos>(def))
32{
33 auto& fanDefs = std::get<fanListPos>(def);
34
35 for (auto& def : fanDefs)
36 {
37 _fans.emplace_back(std::make_unique<Fan>(bus, def));
38 }
Matthew Barth38a93a82017-05-11 14:12:27 -050039
Matthew Barth14184132017-05-19 14:37:30 -050040 // Do not enable set speed events when in init mode
41 if (mode != Mode::init)
Matthew Barth17d1fe22017-05-11 15:00:36 -050042 {
Matthew Barth14184132017-05-19 14:37:30 -050043 // Setup signal trigger for set speed events
44 for (auto& event : std::get<setSpeedEventsPos>(def))
Matthew Barth17d1fe22017-05-11 15:00:36 -050045 {
Matthew Barth14184132017-05-19 14:37:30 -050046 for (auto& prop : std::get<propChangeListPos>(event))
47 {
48 _signalEvents.emplace_back(
49 std::make_unique<SignalEvent>(
50 this,
51 EventData
52 {
53 std::get<groupPos>(event),
54 std::get<handlerObjPos>(prop),
55 std::get<actionPos>(event)
56 }));
57 _matches.emplace_back(
58 bus,
59 std::get<signaturePos>(prop).c_str(),
60 signalHandler,
61 _signalEvents.back().get());
62 }
Matthew Barth17d1fe22017-05-11 15:00:36 -050063 }
Matthew Barth38a93a82017-05-11 14:12:27 -050064 }
Matt Spinler7f88fe62017-04-10 14:39:02 -050065}
66
67
68void Zone::setSpeed(uint64_t speed)
69{
70 for (auto& fan : _fans)
71 {
72 fan->setSpeed(speed);
73 }
74}
75
Matthew Barth861d77c2017-05-22 14:18:25 -050076void Zone::setActiveAllow(const Group* group, bool isActiveAllow)
77{
78 _active[group] = isActiveAllow;
79 if (!isActiveAllow)
80 {
81 _isActive = false;
82 }
83 else
84 {
85 // Check all entries are set to allow control active
86 auto actPred = [](auto const& entry) {return entry.second;};
87 _isActive = std::all_of(_active.begin(),
88 _active.end(),
89 actPred);
90 }
91}
92
Matthew Barth38a93a82017-05-11 14:12:27 -050093int Zone::signalHandler(sd_bus_message* msg,
94 void* data,
95 sd_bus_error* err)
96{
97 auto sdbpMsg = sdbusplus::message::message(msg);
98 auto& signalEvent = *static_cast<SignalEvent*>(data);
99 std::get<zoneObjPos>(signalEvent)->handleEvent(
100 sdbpMsg,
Matthew Barth17d1fe22017-05-11 15:00:36 -0500101 std::get<eventDataPos>(signalEvent));
Matthew Barth38a93a82017-05-11 14:12:27 -0500102 return 0;
103}
104
105void Zone::handleEvent(sdbusplus::message::message& msg,
Matthew Barth17d1fe22017-05-11 15:00:36 -0500106 const EventData& eventData)
Matthew Barth38a93a82017-05-11 14:12:27 -0500107{
108 // Handle the callback
Matthew Barth17d1fe22017-05-11 15:00:36 -0500109 std::get<eventHandlerPos>(eventData)(_bus, msg, *this);
110 // Perform the action
111 std::get<eventActionPos>(eventData)(*this,
112 std::get<eventGroupPos>(eventData));
Matthew Barth38a93a82017-05-11 14:12:27 -0500113}
114
Matt Spinler7f88fe62017-04-10 14:39:02 -0500115}
116}
117}