blob: 665e2dd99af6aafb6c55e29fbd6b932fdbcbcd77 [file] [log] [blame]
Patrick Venture5c7cc542018-06-11 14:29:38 -07001/**
2 * Copyright 2017 Google Inc.
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
17#include "pid/builderconfig.hpp"
18
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070019#include "conf.hpp"
20#include "pid/builder.hpp"
21
Patrick Venture5c7cc542018-06-11 14:29:38 -070022#include <fstream>
23#include <iostream>
24#include <libconfig.h++>
25#include <memory>
26#include <sdbusplus/bus.hpp>
27#include <string>
28#include <unordered_map>
29
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070030std::unordered_map<int64_t, std::unique_ptr<PIDZone>>
Patrick Venture7af157b2018-10-30 11:24:40 -070031 buildZonesFromConfig(const std::string& path, SensorManager& mgr,
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070032 sdbusplus::bus::bus& modeControlBus)
Patrick Venture5c7cc542018-06-11 14:29:38 -070033{
34 using namespace libconfig;
35 // zone -> pids
36 std::map<int64_t, PIDConf> pidConfig;
37 // zone -> configs
Patrick Venturef3252312018-10-30 08:42:53 -070038 std::map<int64_t, struct ZoneConfig> zoneConfig;
Patrick Venture5c7cc542018-06-11 14:29:38 -070039
40 std::cerr << "entered BuildZonesFromConfig\n";
41
42 Config cfg;
43
44 /* The load was modeled after the example source provided. */
45 try
46 {
47 cfg.readFile(path.c_str());
48 }
49 catch (const FileIOException& fioex)
50 {
51 std::cerr << "I/O error while reading file: " << fioex.what()
52 << std::endl;
53 throw;
54 }
55 catch (const ParseException& pex)
56 {
57 std::cerr << "Parse error at " << pex.getFile() << ":" << pex.getLine()
58 << " - " << pex.getError() << std::endl;
59 throw;
60 }
61
62 try
63 {
64 const Setting& root = cfg.getRoot();
65 const Setting& zones = root["zones"];
66 int count = zones.getLength();
67
68 /* For each zone. */
69 for (int i = 0; i < count; ++i)
70 {
71 const Setting& zoneSettings = zones[i];
72
73 int id;
74 PIDConf thisZone;
Patrick Venturef3252312018-10-30 08:42:53 -070075 struct ZoneConfig thisZoneConfig;
Patrick Venture5c7cc542018-06-11 14:29:38 -070076
77 zoneSettings.lookupValue("id", id);
78
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070079 thisZoneConfig.minthermalrpm = zoneSettings.lookup("minthermalrpm");
Patrick Venture5c7cc542018-06-11 14:29:38 -070080 thisZoneConfig.failsafepercent =
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070081 zoneSettings.lookup("failsafepercent");
Patrick Venture5c7cc542018-06-11 14:29:38 -070082
83 const Setting& pids = zoneSettings["pids"];
84 int pidCount = pids.getLength();
85
86 for (int j = 0; j < pidCount; ++j)
87 {
88 const Setting& pid = pids[j];
89
90 std::string name;
Patrick Venturef3252312018-10-30 08:42:53 -070091 struct ControllerInfo info;
Patrick Venture5c7cc542018-06-11 14:29:38 -070092
93 /*
94 * Mysteriously if you use lookupValue on these, and the type
95 * is float. It won't work right.
96 *
97 * If the configuration file value doesn't look explicitly like
98 * a float it won't let you assign it to one.
99 */
100 name = pid.lookup("name").c_str();
101 info.type = pid.lookup("type").c_str();
102 /* set-point is only required to be set for thermal. */
103 /* TODO(venture): Verify this works optionally here. */
104 info.setpoint = pid.lookup("set-point");
James Feist22c257a2018-08-31 14:07:12 -0700105 info.pidInfo.ts = pid.lookup("pid.sampleperiod");
106 info.pidInfo.p_c = pid.lookup("pid.p_coefficient");
107 info.pidInfo.i_c = pid.lookup("pid.i_coefficient");
108 info.pidInfo.ff_off = pid.lookup("pid.ff_off_coefficient");
109 info.pidInfo.ff_gain = pid.lookup("pid.ff_gain_coefficient");
110 info.pidInfo.i_lim.min = pid.lookup("pid.i_limit.min");
111 info.pidInfo.i_lim.max = pid.lookup("pid.i_limit.max");
112 info.pidInfo.out_lim.min = pid.lookup("pid.out_limit.min");
113 info.pidInfo.out_lim.max = pid.lookup("pid.out_limit.max");
114 info.pidInfo.slew_neg = pid.lookup("pid.slew_neg");
115 info.pidInfo.slew_pos = pid.lookup("pid.slew_pos");
Patrick Venture5c7cc542018-06-11 14:29:38 -0700116
James Feist22c257a2018-08-31 14:07:12 -0700117 std::cerr << "out_lim.min: " << info.pidInfo.out_lim.min
118 << "\n";
119 std::cerr << "out_lim.max: " << info.pidInfo.out_lim.max
120 << "\n";
Patrick Venture5c7cc542018-06-11 14:29:38 -0700121
122 const Setting& inputs = pid["inputs"];
123 int icount = inputs.getLength();
124
125 for (int z = 0; z < icount; ++z)
126 {
127 std::string v;
128 v = pid["inputs"][z].c_str();
129 info.inputs.push_back(v);
130 }
131
132 thisZone[name] = info;
133 }
134
135 pidConfig[static_cast<int64_t>(id)] = thisZone;
136 zoneConfig[static_cast<int64_t>(id)] = thisZoneConfig;
137 }
138 }
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700139 catch (const SettingTypeException& setex)
Patrick Venture5c7cc542018-06-11 14:29:38 -0700140 {
141 std::cerr << "Setting '" << setex.getPath() << "' type exception!"
142 << std::endl;
143 throw;
144 }
145 catch (const SettingNotFoundException& snex)
146 {
147 std::cerr << "Setting '" << snex.getPath() << "' not found!"
148 << std::endl;
149 throw;
150 }
151
Patrick Venture7af157b2018-10-30 11:24:40 -0700152 return buildZones(pidConfig, zoneConfig, mgr, modeControlBus);
Patrick Venture5c7cc542018-06-11 14:29:38 -0700153}