17f88fe61SMatt Spinler /**
27f88fe61SMatt Spinler  * Copyright © 2017 IBM Corporation
37f88fe61SMatt Spinler  *
47f88fe61SMatt Spinler  * Licensed under the Apache License, Version 2.0 (the "License");
57f88fe61SMatt Spinler  * you may not use this file except in compliance with the License.
67f88fe61SMatt Spinler  * You may obtain a copy of the License at
77f88fe61SMatt Spinler  *
87f88fe61SMatt Spinler  *     http://www.apache.org/licenses/LICENSE-2.0
97f88fe61SMatt Spinler  *
107f88fe61SMatt Spinler  * Unless required by applicable law or agreed to in writing, software
117f88fe61SMatt Spinler  * distributed under the License is distributed on an "AS IS" BASIS,
127f88fe61SMatt Spinler  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
137f88fe61SMatt Spinler  * See the License for the specific language governing permissions and
147f88fe61SMatt Spinler  * limitations under the License.
157f88fe61SMatt Spinler  */
167f88fe61SMatt Spinler #include "zone.hpp"
177f88fe61SMatt Spinler 
187f88fe61SMatt Spinler namespace phosphor
197f88fe61SMatt Spinler {
207f88fe61SMatt Spinler namespace fan
217f88fe61SMatt Spinler {
227f88fe61SMatt Spinler namespace control
237f88fe61SMatt Spinler {
247f88fe61SMatt Spinler 
257f88fe61SMatt Spinler 
26*14184131SMatthew Barth Zone::Zone(Mode mode,
27*14184131SMatthew Barth            sdbusplus::bus::bus& bus,
287f88fe61SMatt Spinler            const ZoneDefinition& def) :
297f88fe61SMatt Spinler     _bus(bus),
307f88fe61SMatt Spinler     _fullSpeed(std::get<fullSpeedPos>(def)),
317f88fe61SMatt Spinler     _zoneNum(std::get<zoneNumPos>(def))
327f88fe61SMatt Spinler {
337f88fe61SMatt Spinler     auto& fanDefs = std::get<fanListPos>(def);
347f88fe61SMatt Spinler 
357f88fe61SMatt Spinler     for (auto& def : fanDefs)
367f88fe61SMatt Spinler     {
377f88fe61SMatt Spinler         _fans.emplace_back(std::make_unique<Fan>(bus, def));
387f88fe61SMatt Spinler     }
3938a93a8aSMatthew Barth 
40*14184131SMatthew Barth     // Do not enable set speed events when in init mode
41*14184131SMatthew Barth     if (mode != Mode::init)
42*14184131SMatthew Barth     {
4317d1fe23SMatthew Barth         // Setup signal trigger for set speed events
4438a93a8aSMatthew Barth         for (auto& event : std::get<setSpeedEventsPos>(def))
4538a93a8aSMatthew Barth         {
4617d1fe23SMatthew Barth             for (auto& prop : std::get<propChangeListPos>(event))
4738a93a8aSMatthew Barth             {
4838a93a8aSMatthew Barth                 _signalEvents.emplace_back(
49*14184131SMatthew Barth                         std::make_unique<SignalEvent>(
50*14184131SMatthew Barth                                 this,
5117d1fe23SMatthew Barth                                 EventData
5217d1fe23SMatthew Barth                                 {
5317d1fe23SMatthew Barth                                     std::get<groupPos>(event),
5417d1fe23SMatthew Barth                                     std::get<handlerObjPos>(prop),
5517d1fe23SMatthew Barth                                     std::get<actionPos>(event)
5617d1fe23SMatthew Barth                                 }));
57*14184131SMatthew Barth                 _matches.emplace_back(
58*14184131SMatthew Barth                         bus,
5938a93a8aSMatthew Barth                         std::get<signaturePos>(prop).c_str(),
6038a93a8aSMatthew Barth                         signalHandler,
6138a93a8aSMatthew Barth                         _signalEvents.back().get());
6238a93a8aSMatthew Barth             }
6338a93a8aSMatthew Barth         }
647f88fe61SMatt Spinler     }
65*14184131SMatthew Barth }
667f88fe61SMatt Spinler 
677f88fe61SMatt Spinler 
687f88fe61SMatt Spinler void Zone::setSpeed(uint64_t speed)
697f88fe61SMatt Spinler {
707f88fe61SMatt Spinler     for (auto& fan : _fans)
717f88fe61SMatt Spinler     {
727f88fe61SMatt Spinler         fan->setSpeed(speed);
737f88fe61SMatt Spinler     }
747f88fe61SMatt Spinler }
757f88fe61SMatt Spinler 
7638a93a8aSMatthew Barth int Zone::signalHandler(sd_bus_message* msg,
7738a93a8aSMatthew Barth                         void* data,
7838a93a8aSMatthew Barth                         sd_bus_error* err)
7938a93a8aSMatthew Barth {
8038a93a8aSMatthew Barth     auto sdbpMsg = sdbusplus::message::message(msg);
8138a93a8aSMatthew Barth     auto& signalEvent = *static_cast<SignalEvent*>(data);
8238a93a8aSMatthew Barth     std::get<zoneObjPos>(signalEvent)->handleEvent(
8338a93a8aSMatthew Barth         sdbpMsg,
8417d1fe23SMatthew Barth         std::get<eventDataPos>(signalEvent));
8538a93a8aSMatthew Barth     return 0;
8638a93a8aSMatthew Barth }
8738a93a8aSMatthew Barth 
8838a93a8aSMatthew Barth void Zone::handleEvent(sdbusplus::message::message& msg,
8917d1fe23SMatthew Barth                        const EventData& eventData)
9038a93a8aSMatthew Barth {
9138a93a8aSMatthew Barth     // Handle the callback
9217d1fe23SMatthew Barth     std::get<eventHandlerPos>(eventData)(_bus, msg, *this);
9317d1fe23SMatthew Barth     // Perform the action
9417d1fe23SMatthew Barth     std::get<eventActionPos>(eventData)(*this,
9517d1fe23SMatthew Barth                                         std::get<eventGroupPos>(eventData));
9638a93a8aSMatthew Barth }
9738a93a8aSMatthew Barth 
987f88fe61SMatt Spinler }
997f88fe61SMatt Spinler }
1007f88fe61SMatt Spinler }
101