Matthew Barth | d87f89f | 2020-07-30 10:41:32 -0500 | [diff] [blame] | 1 | /** |
| 2 | * Copyright © 2020 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 "json_parser.hpp" |
| 17 | |
Matthew Barth | 3174e72 | 2020-09-15 15:13:40 -0500 | [diff] [blame] | 18 | #include "json/event.hpp" |
Matthew Barth | fcfa052 | 2020-08-24 16:40:24 -0500 | [diff] [blame] | 19 | #include "json/fan.hpp" |
Matthew Barth | dfd4a05 | 2020-09-02 16:31:51 -0500 | [diff] [blame] | 20 | #include "json/group.hpp" |
Matthew Barth | a227a16 | 2020-08-05 10:51:45 -0500 | [diff] [blame] | 21 | #include "json/manager.hpp" |
Matthew Barth | 2331d18 | 2020-08-18 15:00:27 -0500 | [diff] [blame] | 22 | #include "json/profile.hpp" |
Matthew Barth | 4f0d3b7 | 2020-08-27 14:32:15 -0500 | [diff] [blame] | 23 | #include "json/zone.hpp" |
Matthew Barth | 23ac24c | 2020-08-04 13:55:43 -0500 | [diff] [blame] | 24 | #include "types.hpp" |
| 25 | |
| 26 | #include <sdbusplus/bus.hpp> |
| 27 | |
Matthew Barth | 413e4d0 | 2020-09-29 10:44:37 -0500 | [diff] [blame] | 28 | #include <algorithm> |
| 29 | #include <cstdlib> |
Matthew Barth | a3a8cc5 | 2021-01-15 12:40:25 -0600 | [diff] [blame] | 30 | #include <string> |
Matthew Barth | 413e4d0 | 2020-09-29 10:44:37 -0500 | [diff] [blame] | 31 | #include <tuple> |
| 32 | #include <vector> |
| 33 | |
Matthew Barth | d87f89f | 2020-07-30 10:41:32 -0500 | [diff] [blame] | 34 | namespace phosphor::fan::control |
Matthew Barth | 23ac24c | 2020-08-04 13:55:43 -0500 | [diff] [blame] | 35 | { |
| 36 | |
Matthew Barth | 413e4d0 | 2020-09-29 10:44:37 -0500 | [diff] [blame] | 37 | bool checkEntry(const std::vector<std::string>& activeProfiles, |
| 38 | const std::vector<std::string>& entryProfiles) |
| 39 | { |
| 40 | // Include entry if its list of profiles to be included in is empty |
| 41 | if (entryProfiles.empty()) |
| 42 | { |
| 43 | // Entry always to be included |
| 44 | return true; |
| 45 | } |
| 46 | else |
| 47 | { |
| 48 | for (const auto& profile : activeProfiles) |
| 49 | { |
| 50 | auto iter = |
| 51 | std::find(entryProfiles.begin(), entryProfiles.end(), profile); |
| 52 | if (iter != entryProfiles.end()) |
| 53 | { |
| 54 | // Entry configured to be included in active profile |
| 55 | return true; |
| 56 | } |
| 57 | } |
| 58 | } |
| 59 | return false; |
| 60 | } |
| 61 | |
Matthew Barth | 23ac24c | 2020-08-04 13:55:43 -0500 | [diff] [blame] | 62 | const std::vector<ZoneGroup> getZoneGroups(sdbusplus::bus::bus& bus) |
| 63 | { |
| 64 | std::vector<ZoneGroup> zoneGrps; |
| 65 | |
Matthew Barth | 2331d18 | 2020-08-18 15:00:27 -0500 | [diff] [blame] | 66 | // Profiles are optional |
Matthew Barth | 391ade0 | 2021-01-15 14:33:21 -0600 | [diff] [blame] | 67 | auto profiles = getConfig<json::Profile>(true); |
Matthew Barth | fcfa052 | 2020-08-24 16:40:24 -0500 | [diff] [blame] | 68 | // Fans to be controlled |
| 69 | auto fans = getConfig<json::Fan>(bus); |
Matthew Barth | 4f0d3b7 | 2020-08-27 14:32:15 -0500 | [diff] [blame] | 70 | // Zones within the system |
Matthew Barth | acd737c | 2021-03-04 11:04:01 -0600 | [diff] [blame] | 71 | auto zones = getConfig<json::Zone>(bus); |
Matthew Barth | 3174e72 | 2020-09-15 15:13:40 -0500 | [diff] [blame] | 72 | // Fan control events are optional |
| 73 | auto events = getConfig<json::Event>(bus, true); |
Matthew Barth | 2331d18 | 2020-08-18 15:00:27 -0500 | [diff] [blame] | 74 | |
Matthew Barth | 413e4d0 | 2020-09-29 10:44:37 -0500 | [diff] [blame] | 75 | // Ensure all configurations use the same set of active profiles |
| 76 | // (In case a profile's active state changes during configuration) |
| 77 | std::vector<std::string> activeProfiles; |
| 78 | for (const auto& profile : profiles) |
| 79 | { |
| 80 | if (profile.second->isActive()) |
| 81 | { |
| 82 | activeProfiles.emplace_back(profile.first.first); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | // Conditions list empty for JSON based configurations |
| 87 | // TODO Remove conditions after YAML based configuration removed |
| 88 | std::vector<Condition> conditions; |
| 89 | std::vector<ZoneDefinition> zoneDefs; |
| 90 | for (const auto& zone : zones) |
| 91 | { |
| 92 | // Check zone profiles against active profiles |
| 93 | if (checkEntry(activeProfiles, zone.second->getProfiles())) |
| 94 | { |
| 95 | auto zoneName = zone.second->getName(); |
| 96 | // Create FanDefinition list for zone |
| 97 | std::vector<FanDefinition> fanDefs; |
| 98 | for (const auto& fan : fans) |
| 99 | { |
| 100 | // Check fan profiles against active profiles and |
| 101 | // if the fan is included in the zone |
| 102 | if (checkEntry(activeProfiles, fan.second->getProfiles()) && |
| 103 | fan.second->getZone() == zoneName) |
| 104 | { |
Matthew Barth | a3a8cc5 | 2021-01-15 12:40:25 -0600 | [diff] [blame] | 105 | // Adjust fan object sensor list to be included in the |
| 106 | // YAML fan definitions structure |
| 107 | std::vector<std::string> fanSensorList; |
| 108 | for (const auto& sensorMap : fan.second->getSensors()) |
| 109 | { |
| 110 | auto sensor = sensorMap.first; |
| 111 | sensor.erase(0, 38); |
| 112 | fanSensorList.emplace_back(sensor); |
| 113 | } |
| 114 | fanDefs.emplace_back( |
| 115 | std::make_tuple(fan.second->getName(), fanSensorList, |
| 116 | fan.second->getInterface())); |
Matthew Barth | 413e4d0 | 2020-09-29 10:44:37 -0500 | [diff] [blame] | 117 | } |
| 118 | } |
| 119 | |
| 120 | // Create SetSpeedEvents list for zone |
| 121 | std::vector<SetSpeedEvent> speedEvents; |
| 122 | // TODO Populate SetSpeedEvents list with configured events |
| 123 | |
| 124 | // YAML zone name was an integer, JSON is a string |
| 125 | // Design direction is for it to be a string and this can be |
| 126 | // removed after YAML based configurations are removed. |
| 127 | char* zoneName_end; |
| 128 | auto zoneNum = std::strtol(zoneName.c_str(), &zoneName_end, 10); |
| 129 | if (*zoneName_end) |
| 130 | { |
| 131 | throw std::runtime_error( |
| 132 | "Zone names must be a string representation of a number"); |
| 133 | } |
| 134 | |
| 135 | zoneDefs.emplace_back(std::make_tuple( |
Matthew Barth | e47c958 | 2021-03-09 14:24:02 -0600 | [diff] [blame] | 136 | zoneNum, zone.second->getDefaultCeiling(), |
Matthew Barth | 413e4d0 | 2020-09-29 10:44:37 -0500 | [diff] [blame] | 137 | zone.second->getDefaultFloor(), zone.second->getIncDelay(), |
Matthew Barth | 7562772 | 2021-03-11 09:58:39 -0600 | [diff] [blame] | 138 | zone.second->getDecInterval(), std::vector<ZoneHandler>{}, |
Matthew Barth | 413e4d0 | 2020-09-29 10:44:37 -0500 | [diff] [blame] | 139 | std::move(fanDefs), std::move(speedEvents))); |
| 140 | } |
| 141 | } |
| 142 | // TODO Should only result in a single entry but YAML based configurations |
| 143 | // produce a list of zone groups. Change to single zone group after YAML |
| 144 | // based configuartions are removed. |
| 145 | zoneGrps.emplace_back(std::make_tuple(conditions, zoneDefs)); |
Matthew Barth | 2331d18 | 2020-08-18 15:00:27 -0500 | [diff] [blame] | 146 | |
Matthew Barth | 23ac24c | 2020-08-04 13:55:43 -0500 | [diff] [blame] | 147 | return zoneGrps; |
| 148 | } |
| 149 | |
Matthew Barth | 0676494 | 2021-03-04 09:28:40 -0600 | [diff] [blame] | 150 | const unsigned int getPowerOnDelay(sdbusplus::bus::bus& bus, |
| 151 | const sdeventplus::Event& event) |
Matthew Barth | 2dc5aba | 2020-08-04 14:23:34 -0500 | [diff] [blame] | 152 | { |
Matthew Barth | 0676494 | 2021-03-04 09:28:40 -0600 | [diff] [blame] | 153 | json::Manager mgr{bus, event}; |
Matthew Barth | a227a16 | 2020-08-05 10:51:45 -0500 | [diff] [blame] | 154 | return mgr.getPowerOnDelay(); |
Matthew Barth | 2dc5aba | 2020-08-04 14:23:34 -0500 | [diff] [blame] | 155 | } |
| 156 | |
Matthew Barth | 23ac24c | 2020-08-04 13:55:43 -0500 | [diff] [blame] | 157 | } // namespace phosphor::fan::control |