summaryrefslogtreecommitdiff
path: root/Config.cpp
blob: 31f87b76a413b3a2590241fd3bd3ffa684969320 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/*
 * Copyright (C) 2021 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include "Config.h"

#include <cmath>
#include <iostream>
#include <fstream>

#include <android-base/properties.h>
#include <android-base/logging.h>

namespace android {
namespace hardware {
namespace thermal {
namespace V2_0 {
namespace implementation {

template<class T> bool Config::typeToEnum(std::string &type, T &t)
{
	if (type.empty())
		return false;;

	for (const auto cdt : hidl_enum_range<T>()) {
		
		const std::string upperType = toString(cdt);

		/*
		 * Let's compare on uppercase only so we can be
		 * comfortable with the syntax used in the
		 * configuration file.
		 */
		if (toUpper(type) != toUpper(upperType))
			continue;

		t = cdt;

		return true;
	}
	
	return false;
}

std::string Config::toUpper(const std::string &str)
{
	std::string upper_str(str);

	for (std::string::size_type i = 0; i < upper_str.length(); i++)
		upper_str[i] = std::toupper(str[i], std::locale());

	return upper_str;
}

bool Config::readHotColdThrottling(Json::Value &throttling,
				   float *tempThreshold)
{
	if (throttling.empty())
		return false;

	for (const std::string mn : throttling.getMemberNames()) {

		for (const auto ts : hidl_enum_range<V2_0::ThrottlingSeverity>()) {

			const std::string severity = toString(ts);

			if (toUpper(severity) != toUpper(mn))
				continue;

			tempThreshold[(int)ts] = throttling[mn].asFloat();
			
			LOG(INFO) << "Throttling[\"" << severity << "\"] = " <<
				tempThreshold[(int)ts] << "°C";

		}
	}

	return true;
}

bool Config::readColdThrottling(Json::Value &throttling,
				TemperatureThreshold &tempThreshold)
{
	return readHotColdThrottling(throttling,
				     tempThreshold.coldThrottlingThresholds.data());
}

bool Config::readHotThrottling(Json::Value &throttling,
			       TemperatureThreshold &tempThreshold)
{
	return readHotColdThrottling(throttling,
				     tempThreshold.hotThrottlingThresholds.data());
}

bool Config::readVrThrottling(Json::Value &throttling,
			      TemperatureThreshold &tempThreshold)
{
	LOG(INFO) << "Reading Virtual Reality configuration";
	
	if (throttling["None"].empty()) {
		LOG(ERROR) << "Invalid temperature threshold";
		return false;
	}
	
	tempThreshold.vrThrottlingThreshold = throttling["None"].asFloat();
	
	LOG(INFO) << "Virtual Reality Throtlling: " <<
		tempThreshold.vrThrottlingThreshold << "°C";
	
	return true;
}
	
void Config::initThreshold(TemperatureThreshold &tempThreshold)
{
	for (int i = 0; i < tempThreshold.hotThrottlingThresholds.size(); i++)
		tempThreshold.hotThrottlingThresholds[i] = NAN;

	for (int i = 0; i < tempThreshold.coldThrottlingThresholds.size(); i++)
		tempThreshold.coldThrottlingThresholds[i] = NAN;

	tempThreshold.vrThrottlingThreshold = NAN;
}

bool Config::readThrottling(const std::string &name, Json::Value &throttling)
{
	TemperatureThreshold tempThreshold = {
		.name = name,
	};

	initThreshold(tempThreshold);
	
	if (throttling.empty()) {
		LOG(INFO) << "No threshold specified for '" << name << "'";
		return true;
	}

	for (Json::Value::ArrayIndex i = 0; i < throttling.size(); ++i) {

		std::string type = throttling[i]["Type"].asString();

		LOG(INFO) << "Getting '" << type << "' throttling configuration";

		if (type == "Hot") {
			if (!readHotThrottling(throttling[i], tempThreshold)) {
				LOG(ERROR) << "Failed to read hot throttling entry";
				return false;
			}
		} else if (type == "Cold") {
			if (!readColdThrottling(throttling[i], tempThreshold)) {
				LOG(ERROR) << "Failed to read cold throttling entry";
				return false;
			}
		} else if (type == "Vr") {
			if (!readVrThrottling(throttling[i], tempThreshold)) {
				LOG(ERROR) << "Failed to read Virtual Reality throttling entry";
				return false;
			}
		} else {
			LOG(ERROR) << "Invalid Throttling type: " << type;
			return false;
		}

		tempThreshold.name = name;
	}

	this->m_threshold.insert(std::pair<std::string,
				 TemperatureThreshold>(name, tempThreshold));

	return true;
}

bool Config::readSensor(Json::Value &sensor)
{
	std::string name = sensor["Name"].asString();
	std::string type = sensor["Type"].asString();

	Temperature_1_0 temperature_1_0;
	Temperature_2_0 temperature_2_0;

	if (name.empty()) {
		LOG(ERROR) << "Missing sensor name section";
		return false;
	}

	temperature_1_0.name = name;
	temperature_2_0.name = name;

	if (typeToEnum(type, temperature_1_0.type))
		m_temperature_1_0.push_back(temperature_1_0);

	if (typeToEnum(type, temperature_2_0.type))
		m_temperature_2_0.push_back(temperature_2_0);

	/*
	 * The skin temperature sensors are special ones and are
	 * stored in a second list for quick access for monitoring
	 */
	if (temperature_1_0.type == V1_0::TemperatureType::SKIN)
		m_skin_sensors.push_back(name);
	
	LOG(INFO) << "Sensor: '" << name << "' / type: " << type;
	
	if (!readThrottling(name, sensor["Throttling"]))
		return false;
	
	return true;
}

bool Config::readCoolingDevice(Json::Value &coolingDevice)
{
	std::string name = coolingDevice["Name"].asString();
	std::string type = coolingDevice["Type"].asString();

	CoolingDevice_1_0 coolingDevice_1_0;
	CoolingDevice_2_0 coolingDevice_2_0;

	if (name.empty() || type.empty()) {
		LOG(ERROR) << "Missing Cooling device name/type";
		return false;
	}

	coolingDevice_1_0.name = name;
	coolingDevice_2_0.name = name;

	if (typeToEnum(type, coolingDevice_1_0.type))
		m_cooling_device_1_0.push_back(coolingDevice_1_0);
	
	if (typeToEnum(type, coolingDevice_2_0.type))
		m_cooling_device_2_0.push_back(coolingDevice_2_0);

	return true;
}

bool Config::readFile(const std::string path, std::stringstream &config)
{
	std::ifstream file;

	LOG(INFO) << "Reading configuration file: " << path;

	file.open(path);
	if (!file) {
		LOG(ERROR) << "Failed to open: " << path;
		return false;
	}

	config << file.rdbuf();

	LOG(INFO) << config.str();
	
	file.close();

	return true;
}
	
bool Config::parseFile(std::string path, Json::Value &root)
{
	std::stringstream config;
	std::string strerr;

	Json::CharReaderBuilder builder;
	std::unique_ptr<Json::CharReader> reader(builder.newCharReader());

	if (!readFile(path, config)) {
		LOG(ERROR) << "Failed to read configuration file";
		return false;
	}

	if (!reader->parse(&*config.str().begin(), &*config.str().end(), &root, &strerr)) {
		LOG(ERROR) << "Failed to parse JSON config: " << strerr;
		return false;
	}

	LOG(INFO) << "Configuration file parsed successfully";
	
	return true;
}

bool Config::read(std::string path)
{
	Json::Value root;
	Json::Value sensors;
	Json::Value coolingDevices;

	if (!parseFile(path, root))
		return false;

	sensors = root["Sensors"];

	for (Json::Value::ArrayIndex i = 0; i < sensors.size(); ++i) {

		if (!readSensor(sensors[i]))
			return false;
        }

	coolingDevices = root["CoolingDevices"];

	for (Json::Value::ArrayIndex i = 0; i < coolingDevices.size(); ++i) {

		if (!readCoolingDevice(coolingDevices[i]))
			return false;
        }

	return true;
}

bool Config::init(void)
{
	std::string property("vendor.thermal.config");
	std::string default_conf("thermal.json");
	std::string path = "/vendor/etc/" + android::base::GetProperty(property, default_conf);

	return read(path);
}

}  // namespace implementation
}  // namespace V2_0
}  // namespace thermal
}  // namespace hardware
}  // namespace android