blob: 732d9fcd776b415a9e942de9b9b4392ab610410c [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 Venture8e2fdb32019-02-11 09:39:59 -080079 thisZoneConfig.minThermalRpm = zoneSettings.lookup("minThermalRpm");
80 thisZoneConfig.failsafePercent =
81 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
Patrick Venture5f59c0f2018-11-11 12:55:14 -080095 * is double. It won't work right.
Patrick Venture5c7cc542018-06-11 14:29:38 -070096 *
97 * If the configuration file value doesn't look explicitly like
Patrick Venture5f59c0f2018-11-11 12:55:14 -080098 * a double it won't let you assign it to one.
Patrick Venture5c7cc542018-06-11 14:29:38 -070099 */
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");
Patrick Venture7442c372019-02-11 10:21:05 -0800105 info.pidInfo.ts = pid.lookup("pid.samplePeriod");
106 info.pidInfo.proportionalCoeff =
107 pid.lookup("pid.proportionalCoeff");
108 info.pidInfo.integralCoeff = pid.lookup("pid.integralCoeff");
109 info.pidInfo.feedFwdOffset =
110 pid.lookup("pid.feedFwdOffOffsetCoeff");
111 info.pidInfo.feedFwdGain = pid.lookup("pid.feedFwdGainCoeff");
112 info.pidInfo.integralLimit.min =
113 pid.lookup("pid.integralCoeff.min");
114 info.pidInfo.integralLimit.max =
115 pid.lookup("pid.integralCoeff.max");
116 info.pidInfo.outLim.min = pid.lookup("pid.outLimit.min");
117 info.pidInfo.outLim.max = pid.lookup("pid.outLimit.max");
118 info.pidInfo.slewNeg = pid.lookup("pid.slewNeg");
119 info.pidInfo.slewPos = pid.lookup("pid.slewPos");
Patrick Venture5c7cc542018-06-11 14:29:38 -0700120
Patrick Venture7442c372019-02-11 10:21:05 -0800121 std::cerr << "outLim.min: " << info.pidInfo.outLim.min << "\n";
122 std::cerr << "outLim.max: " << info.pidInfo.outLim.max << "\n";
Patrick Venture5c7cc542018-06-11 14:29:38 -0700123
124 const Setting& inputs = pid["inputs"];
125 int icount = inputs.getLength();
126
127 for (int z = 0; z < icount; ++z)
128 {
129 std::string v;
130 v = pid["inputs"][z].c_str();
131 info.inputs.push_back(v);
132 }
133
134 thisZone[name] = info;
135 }
136
137 pidConfig[static_cast<int64_t>(id)] = thisZone;
138 zoneConfig[static_cast<int64_t>(id)] = thisZoneConfig;
139 }
140 }
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700141 catch (const SettingTypeException& setex)
Patrick Venture5c7cc542018-06-11 14:29:38 -0700142 {
143 std::cerr << "Setting '" << setex.getPath() << "' type exception!"
144 << std::endl;
145 throw;
146 }
147 catch (const SettingNotFoundException& snex)
148 {
149 std::cerr << "Setting '" << snex.getPath() << "' not found!"
150 << std::endl;
151 throw;
152 }
153
Patrick Venture7af157b2018-10-30 11:24:40 -0700154 return buildZones(pidConfig, zoneConfig, mgr, modeControlBus);
Patrick Venture5c7cc542018-06-11 14:29:38 -0700155}