summaryrefslogtreecommitdiff
path: root/thermal/utils/config_parser.cpp
blob: d97513c8c1a64553e38a9529fd1ec49e703fdf87 (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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
/*
 * Copyright (C) 2018 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 <android-base/file.h>
#include <android-base/logging.h>
#include <android-base/strings.h>
#include <cmath>
#include <set>

#include <json/reader.h>
#include <json/value.h>

#include "config_parser.h"

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

using ::android::hardware::hidl_enum_range;
using ::android::hardware::thermal::V2_0::toString;
using TemperatureType_2_0 = ::android::hardware::thermal::V2_0::TemperatureType;

namespace {

template <typename T>
// Return false when failed parsing
bool getTypeFromString(std::string_view str, T *out) {
    auto types = hidl_enum_range<T>();
    for (const auto &type : types) {
        if (toString(type) == str) {
            *out = type;
            return true;
        }
    }
    return false;
}

float getFloatFromValue(const Json::Value &value) {
    if (value.isString()) {
        return std::stof(value.asString());
    } else {
        return value.asFloat();
    }
}

int getIntFromValue(const Json::Value &value) {
    if (value.isString()) {
        return std::stoi(value.asString());
    } else {
        return value.asInt();
    }
}

bool getFloatFromJsonValues(const Json::Value &values, ThrottlingArray *out, bool inc_check,
                            bool dec_check) {
    ThrottlingArray ret;

    if (inc_check && dec_check) {
        LOG(ERROR) << "Cannot enable inc_check and dec_check at the same time";
        return false;
    }

    if (values.size() != kThrottlingSeverityCount) {
        LOG(ERROR) << "Values size is invalid";
        return false;
    } else {
        float last = std::nanf("");
        for (Json::Value::ArrayIndex i = 0; i < kThrottlingSeverityCount; ++i) {
            ret[i] = getFloatFromValue(values[i]);
            if (inc_check && !std::isnan(last) && !std::isnan(ret[i]) && ret[i] < last) {
                LOG(FATAL) << "Invalid array[" << i << "]" << ret[i] << " min=" << last;
                return false;
            }
            if (dec_check && !std::isnan(last) && !std::isnan(ret[i]) && ret[i] > last) {
                LOG(FATAL) << "Invalid array[" << i << "]" << ret[i] << " max=" << last;
                return false;
            }
            last = std::isnan(ret[i]) ? last : ret[i];
            LOG(INFO) << "[" << i << "]: " << ret[i];
        }
    }

    *out = ret;
    return true;
}

}  // namespace

std::map<std::string, SensorInfo> ParseSensorInfo(std::string_view config_path) {
    std::string json_doc;
    std::map<std::string, SensorInfo> sensors_parsed;
    if (!android::base::ReadFileToString(config_path.data(), &json_doc)) {
        LOG(ERROR) << "Failed to read JSON config from " << config_path;
        return sensors_parsed;
    }

    Json::Value root;
    Json::Reader reader;

    if (!reader.parse(json_doc, root)) {
        LOG(ERROR) << "Failed to parse JSON config";
        return sensors_parsed;
    }

    Json::Value sensors = root["Sensors"];
    std::size_t total_parsed = 0;
    std::set<std::string> sensors_name_parsed;

    for (Json::Value::ArrayIndex i = 0; i < sensors.size(); ++i) {
        const std::string &name = sensors[i]["Name"].asString();
        LOG(INFO) << "Sensor[" << i << "]'s Name: " << name;
        if (name.empty()) {
            LOG(ERROR) << "Failed to read "
                       << "Sensor[" << i << "]'s Name";
            sensors_parsed.clear();
            return sensors_parsed;
        }

        auto result = sensors_name_parsed.insert(name);
        if (!result.second) {
            LOG(ERROR) << "Duplicate Sensor[" << i << "]'s Name";
            sensors_parsed.clear();
            return sensors_parsed;
        }

        std::string sensor_type_str = sensors[i]["Type"].asString();
        LOG(INFO) << "Sensor[" << name << "]'s Type: " << sensor_type_str;
        TemperatureType_2_0 sensor_type;

        if (!getTypeFromString(sensor_type_str, &sensor_type)) {
            LOG(ERROR) << "Invalid "
                       << "Sensor[" << name << "]'s Type: " << sensor_type_str;
            sensors_parsed.clear();
            return sensors_parsed;
        }

        std::array<ThrottleType, kThrottlingSeverityCount> throttle_type;
        throttle_type.fill(NONE);
        bool support_pid = false;
        bool support_hard_limit = false;
        Json::Value values = sensors[i]["ThrottleType"];
        if (values.size()) {
            for (Json::Value::ArrayIndex j = 0; j < values.size(); ++j) {
                if (values[j].asString() == "None") {
                    LOG(INFO) << "Sensor[" << name << "]'s throttle type[" << j << "]: None";
                    continue;
                } else if (values[j].asString() == "PID") {
                    throttle_type[j] = PID;
                    support_pid = true;
                    LOG(INFO) << "Sensor[" << name << "]'s throttle type[" << j << "]: PID";
                } else if (values[j].asString() == "LIMIT") {
                    throttle_type[j] = LIMIT;
                    support_hard_limit = true;
                    LOG(INFO) << "Sensor[" << name << "]'s throttle type[" << j << "]: LIMIT";
                } else {
                    LOG(ERROR) << "cannot identify the throttling type";
                    sensors_parsed.clear();
                    return sensors_parsed;
                }
            }
        }

        bool send_cb = false;
        if (sensors[i]["Monitor"].empty() || !sensors[i]["Monitor"].isBool()) {
            LOG(INFO) << "Failed to read Sensor[" << name << "]'s Monitor, set to 'false'";
        } else if (sensors[i]["Monitor"].asBool()) {
            send_cb = true;
        }
        LOG(INFO) << "Sensor[" << name << "]'s SendCallback: " << std::boolalpha << send_cb
                  << std::noboolalpha;

        bool send_powerhint = false;
        if (sensors[i]["SendPowerHint"].empty() || !sensors[i]["SendPowerHint"].isBool()) {
            LOG(INFO) << "Failed to read Sensor[" << name << "]'s SendPowerHint, set to 'false'";
        } else if (sensors[i]["SendPowerHint"].asBool()) {
            send_powerhint = true;
        }
        LOG(INFO) << "Sensor[" << name << "]'s SendPowerHint: " << std::boolalpha << send_powerhint
                  << std::noboolalpha;

        bool is_monitor = (send_cb | send_powerhint | support_pid | support_hard_limit);
        LOG(INFO) << "Sensor[" << name << "]'s Monitor: " << is_monitor;

        std::array<float, kThrottlingSeverityCount> hot_thresholds;
        hot_thresholds.fill(NAN);
        std::array<float, kThrottlingSeverityCount> cold_thresholds;
        cold_thresholds.fill(NAN);
        std::array<float, kThrottlingSeverityCount> hot_hysteresis;
        hot_hysteresis.fill(0.0);
        std::array<float, kThrottlingSeverityCount> cold_hysteresis;
        cold_hysteresis.fill(0.0);
        std::vector<std::string> linked_sensors;
        std::vector<float> coefficients;
        std::string trigger_sensor;

        FormulaOption formula = FormulaOption::COUNT_THRESHOLD;
        bool is_virtual_sensor = false;
        if (sensors[i]["VirtualSensor"].empty() || !sensors[i]["VirtualSensor"].isBool()) {
            LOG(INFO) << "Failed to read Sensor[" << name << "]'s VirtualSensor, set to 'false'";
        } else {
            is_virtual_sensor = sensors[i]["VirtualSensor"].asBool();
        }
        values = sensors[i]["HotThreshold"];
        if (values.size() != kThrottlingSeverityCount) {
            LOG(ERROR) << "Invalid "
                       << "Sensor[" << name << "]'s HotThreshold count" << values.size();
            sensors_parsed.clear();
            return sensors_parsed;
        } else {
            float min = std::numeric_limits<float>::min();
            for (Json::Value::ArrayIndex j = 0; j < kThrottlingSeverityCount; ++j) {
                hot_thresholds[j] = getFloatFromValue(values[j]);
                if (!std::isnan(hot_thresholds[j])) {
                    if (hot_thresholds[j] < min) {
                        LOG(ERROR) << "Invalid "
                                   << "Sensor[" << name << "]'s HotThreshold[j" << j
                                   << "]: " << hot_thresholds[j] << " < " << min;
                        sensors_parsed.clear();
                        return sensors_parsed;
                    }
                    min = hot_thresholds[j];
                }
                LOG(INFO) << "Sensor[" << name << "]'s HotThreshold[" << j
                          << "]: " << hot_thresholds[j];
            }
        }

        values = sensors[i]["HotHysteresis"];
        if (values.size() != kThrottlingSeverityCount) {
            LOG(INFO) << "Cannot find valid "
                      << "Sensor[" << name << "]'s HotHysteresis, default all to 0.0";
        } else {
            for (Json::Value::ArrayIndex j = 0; j < kThrottlingSeverityCount; ++j) {
                hot_hysteresis[j] = getFloatFromValue(values[j]);
                if (std::isnan(hot_hysteresis[j])) {
                    LOG(ERROR) << "Invalid "
                               << "Sensor[" << name << "]'s HotHysteresis: " << hot_hysteresis[j];
                    sensors_parsed.clear();
                    return sensors_parsed;
                }
                LOG(INFO) << "Sensor[" << name << "]'s HotHysteresis[" << j
                          << "]: " << hot_hysteresis[j];
            }
        }

        values = sensors[i]["ColdThreshold"];
        if (values.size() != kThrottlingSeverityCount) {
            LOG(INFO) << "Cannot find valid "
                      << "Sensor[" << name << "]'s ColdThreshold, default all to NAN";
        } else {
            float max = std::numeric_limits<float>::max();
            for (Json::Value::ArrayIndex j = 0; j < kThrottlingSeverityCount; ++j) {
                cold_thresholds[j] = getFloatFromValue(values[j]);
                if (!std::isnan(cold_thresholds[j])) {
                    if (cold_thresholds[j] > max) {
                        LOG(ERROR) << "Invalid "
                                   << "Sensor[" << name << "]'s ColdThreshold[j" << j
                                   << "]: " << cold_thresholds[j] << " > " << max;
                        sensors_parsed.clear();
                        return sensors_parsed;
                    }
                    max = cold_thresholds[j];
                }
                LOG(INFO) << "Sensor[" << name << "]'s ColdThreshold[" << j
                          << "]: " << cold_thresholds[j];
            }
        }

        values = sensors[i]["ColdHysteresis"];
        if (values.size() != kThrottlingSeverityCount) {
            LOG(INFO) << "Cannot find valid "
                      << "Sensor[" << name << "]'s ColdHysteresis, default all to 0.0";
        } else {
            for (Json::Value::ArrayIndex j = 0; j < kThrottlingSeverityCount; ++j) {
                cold_hysteresis[j] = getFloatFromValue(values[j]);
                if (std::isnan(cold_hysteresis[j])) {
                    LOG(ERROR) << "Invalid "
                               << "Sensor[" << name
                               << "]'s ColdHysteresis: " << cold_hysteresis[j];
                    sensors_parsed.clear();
                    return sensors_parsed;
                }
                LOG(INFO) << "Sensor[" << name << "]'s ColdHysteresis[" << j
                          << "]: " << cold_hysteresis[j];
            }
        }

        if (is_virtual_sensor) {
            values = sensors[i]["Combination"];
            if (values.size()) {
                linked_sensors.reserve(values.size());
                for (Json::Value::ArrayIndex j = 0; j < values.size(); ++j) {
                    linked_sensors.emplace_back(values[j].asString());
                    LOG(INFO) << "Sensor[" << name << "]'s combination[" << j
                              << "]: " << linked_sensors[j];
                }
            } else {
                sensors_parsed.clear();
                return sensors_parsed;
            }

            values = sensors[i]["Coefficient"];
            if (values.size()) {
                coefficients.reserve(values.size());
                for (Json::Value::ArrayIndex j = 0; j < values.size(); ++j) {
                    coefficients.emplace_back(getFloatFromValue(values[j]));
                    LOG(INFO) << "Sensor[" << name << "]'s coefficient[" << j
                              << "]: " << coefficients[j];
                }
            } else {
                sensors_parsed.clear();
                return sensors_parsed;
            }

            if (linked_sensors.size() != coefficients.size()) {
                sensors_parsed.clear();
                return sensors_parsed;
            }

            trigger_sensor = sensors[i]["TriggerSensor"].asString();
            if (sensors[i]["Formula"].asString().compare("COUNT_THRESHOLD") == 0) {
                formula = FormulaOption::COUNT_THRESHOLD;
            } else if (sensors[i]["Formula"].asString().compare("WEIGHTED_AVG") == 0) {
                formula = FormulaOption::WEIGHTED_AVG;
            } else if (sensors[i]["Formula"].asString().compare("MAXIMUM") == 0) {
                formula = FormulaOption::MAXIMUM;
            } else if (sensors[i]["Formula"].asString().compare("MINIMUM") == 0) {
                formula = FormulaOption::MINIMUM;
            } else {
                sensors_parsed.clear();
                return sensors_parsed;
            }
        }

        float vr_threshold = NAN;
        vr_threshold = getFloatFromValue(sensors[i]["VrThreshold"]);
        LOG(INFO) << "Sensor[" << name << "]'s VrThreshold: " << vr_threshold;

        float multiplier = sensors[i]["Multiplier"].asFloat();
        LOG(INFO) << "Sensor[" << name << "]'s Multiplier: " << multiplier;

        std::chrono::milliseconds polling_delay;
        if (sensors[i]["PollingDelay"].empty()) {
            polling_delay = kUeventPollTimeoutMs;
        } else {
            polling_delay = std::chrono::milliseconds(getIntFromValue(sensors[i]["PollingDelay"]));
        }
        LOG(INFO) << "Sensor[" << name << "]'s Polling delay: " << polling_delay.count();

        std::chrono::milliseconds passive_delay;
        if (sensors[i]["PassiveDelay"].empty()) {
            passive_delay = kMinPollIntervalMs;
        } else {
            passive_delay = std::chrono::milliseconds(getIntFromValue(sensors[i]["PassiveDelay"]));
        }
        LOG(INFO) << "Sensor[" << name << "]'s Passive delay: " << passive_delay.count();

        std::array<float, kThrottlingSeverityCount> k_po;
        k_po.fill(0.0);
        std::array<float, kThrottlingSeverityCount> k_pu;
        k_pu.fill(0.0);
        std::array<float, kThrottlingSeverityCount> k_i;
        k_i.fill(0.0);
        std::array<float, kThrottlingSeverityCount> k_d;
        k_d.fill(0.0);
        std::array<float, kThrottlingSeverityCount> i_max;
        i_max.fill(NAN);
        std::array<float, kThrottlingSeverityCount> max_alloc_power;
        max_alloc_power.fill(NAN);
        std::array<float, kThrottlingSeverityCount> min_alloc_power;
        min_alloc_power.fill(NAN);
        std::array<float, kThrottlingSeverityCount> s_power;
        s_power.fill(NAN);
        std::array<float, kThrottlingSeverityCount> i_cutoff;
        i_cutoff.fill(NAN);
        std::vector<std::string> cdev_request;
        std::vector<float> cdev_weight;

        if (support_pid) {
            LOG(INFO) << "Start to parse K_Po";
            values = sensors[i]["K_Po"];
            if (!getFloatFromJsonValues(values, &k_po, false, false)) {
                sensors_parsed.clear();
                return sensors_parsed;
            }

            LOG(INFO) << "Start to parse K_Pu";
            values = sensors[i]["K_Pu"];
            if (!getFloatFromJsonValues(values, &k_pu, false, false)) {
                sensors_parsed.clear();
                return sensors_parsed;
            }

            LOG(INFO) << "Start to parse K_I";
            values = sensors[i]["K_I"];
            if (!getFloatFromJsonValues(values, &k_i, false, false)) {
                sensors_parsed.clear();
                return sensors_parsed;
            }

            LOG(INFO) << "Start to parse K_D";
            values = sensors[i]["K_D"];
            if (!getFloatFromJsonValues(values, &k_d, false, false)) {
                sensors_parsed.clear();
                return sensors_parsed;
            }

            LOG(INFO) << "Start to parse I_Max";
            values = sensors[i]["I_Max"];
            if (!getFloatFromJsonValues(values, &i_max, false, false)) {
                sensors_parsed.clear();
                return sensors_parsed;
            }

            LOG(INFO) << "Start to parse MaxAllocPower";
            values = sensors[i]["MaxAllocPower"];
            if (!getFloatFromJsonValues(values, &max_alloc_power, false, true)) {
                sensors_parsed.clear();
                return sensors_parsed;
            }

            LOG(INFO) << "Start to parse MinAllocPower";
            values = sensors[i]["MinAllocPower"];
            if (!getFloatFromJsonValues(values, &min_alloc_power, false, true)) {
                sensors_parsed.clear();
                return sensors_parsed;
            }

            LOG(INFO) << "Start to parse S_Power";
            values = sensors[i]["S_Power"];
            if (!getFloatFromJsonValues(values, &s_power, false, true)) {
                sensors_parsed.clear();
                return sensors_parsed;
            }

            LOG(INFO) << "Start to parse I_Cutoff";
            values = sensors[i]["I_Cutoff"];
            if (!getFloatFromJsonValues(values, &i_cutoff, false, false)) {
                sensors_parsed.clear();
                return sensors_parsed;
            }

            values = sensors[i]["CdevRequest"];
            if (values.size()) {
                cdev_request.reserve(values.size());
                for (Json::Value::ArrayIndex j = 0; j < values.size(); ++j) {
                    cdev_request.emplace_back(values[j].asString());
                    LOG(INFO) << "Sensor[" << name << "]'s cdev_request[" << j
                              << "]: " << cdev_request[j];
                }
            } else {
                sensors_parsed.clear();
                return sensors_parsed;
            }

            values = sensors[i]["CdevWeight"];
            if (values.size()) {
                cdev_weight.reserve(values.size());
                for (Json::Value::ArrayIndex j = 0; j < values.size(); ++j) {
                    cdev_weight.emplace_back(getFloatFromValue(values[j]));
                    LOG(INFO) << "Sensor[" << name << "]'s cdev_weight[" << j
                              << "]: " << cdev_weight[j];
                }
            } else {
                sensors_parsed.clear();
                return sensors_parsed;
            }

            for (Json::Value::ArrayIndex j = 0; j < kThrottlingSeverityCount; ++j) {
                if (!std::isnan(s_power[j]) &&
                    (std::isnan(k_po[j]) || std::isnan(k_pu[j]) || std::isnan(k_i[j]) ||
                     std::isnan(k_d[j]) || std::isnan(i_max[j]) || std::isnan(max_alloc_power[j]) ||
                     std::isnan(min_alloc_power[j]) || std::isnan(i_cutoff[j]))) {
                    LOG(ERROR) << "Sensor[" << name << "]: Invalid PID parameters combinations";
                    sensors_parsed.clear();
                    return sensors_parsed;
                }
            }
        }

        std::map<std::string, ThrottlingArray> limit_info;
        if (support_hard_limit) {
            LOG(INFO) << "start to parse LimitInfo";
            values = sensors[i]["LimitInfo"];
            for (Json::Value::ArrayIndex j = 0; j < values.size(); ++j) {
                const std::string &cdev_name = values[j]["CdevRequest"].asString();
                Json::Value sub_values = values[j]["CdevInfo"];
                std::array<float, kThrottlingSeverityCount> state;
                if (!getFloatFromJsonValues(sub_values, &state, false, false)) {
                    sensors_parsed.clear();
                    return sensors_parsed;
                } else {
                    LOG(INFO) << "Sensor[" << name
                              << "]: Add cooling device request: " << cdev_name;
                    limit_info[cdev_name] = state;
                }
            }
        }

        std::unique_ptr<VirtualSensorInfo> virtual_sensor_info;
        if (is_virtual_sensor) {
            virtual_sensor_info.reset(
                    new VirtualSensorInfo{linked_sensors, coefficients, trigger_sensor, formula});
        }

        std::unique_ptr<ThrottlingInfo> throttling_info(new ThrottlingInfo{
                k_po, k_pu, k_i, k_d, i_max, max_alloc_power, min_alloc_power, s_power, i_cutoff,
                throttle_type, cdev_request, cdev_weight, limit_info});

        sensors_parsed[name] = {
                .type = sensor_type,
                .hot_thresholds = hot_thresholds,
                .cold_thresholds = cold_thresholds,
                .hot_hysteresis = hot_hysteresis,
                .cold_hysteresis = cold_hysteresis,
                .vr_threshold = vr_threshold,
                .multiplier = multiplier,
                .polling_delay = polling_delay,
                .passive_delay = passive_delay,
                .send_cb = send_cb,
                .send_powerhint = send_powerhint,
                .is_monitor = is_monitor,
                .virtual_sensor_info = std::move(virtual_sensor_info),
                .throttling_info = std::move(throttling_info),
        };

        ++total_parsed;
    }

    LOG(INFO) << total_parsed << " Sensors parsed successfully";
    return sensors_parsed;
}

std::map<std::string, CdevInfo> ParseCoolingDevice(std::string_view config_path) {
    std::string json_doc;
    std::map<std::string, CdevInfo> cooling_devices_parsed;
    if (!android::base::ReadFileToString(config_path.data(), &json_doc)) {
        LOG(ERROR) << "Failed to read JSON config from " << config_path;
        return cooling_devices_parsed;
    }

    Json::Value root;
    Json::Reader reader;

    if (!reader.parse(json_doc, root)) {
        LOG(ERROR) << "Failed to parse JSON config";
        return cooling_devices_parsed;
    }

    Json::Value cooling_devices = root["CoolingDevices"];
    std::size_t total_parsed = 0;
    std::set<std::string> cooling_devices_name_parsed;

    for (Json::Value::ArrayIndex i = 0; i < cooling_devices.size(); ++i) {
        const std::string &name = cooling_devices[i]["Name"].asString();
        LOG(INFO) << "CoolingDevice[" << i << "]'s Name: " << name;
        if (name.empty()) {
            LOG(ERROR) << "Failed to read "
                       << "CoolingDevice[" << i << "]'s Name";
            cooling_devices_parsed.clear();
            return cooling_devices_parsed;
        }

        auto result = cooling_devices_name_parsed.insert(name.data());
        if (!result.second) {
            LOG(ERROR) << "Duplicate CoolingDevice[" << i << "]'s Name";
            cooling_devices_parsed.clear();
            return cooling_devices_parsed;
        }

        std::string cooling_device_type_str = cooling_devices[i]["Type"].asString();
        LOG(INFO) << "CoolingDevice[" << name << "]'s Type: " << cooling_device_type_str;
        CoolingType cooling_device_type;

        if (!getTypeFromString(cooling_device_type_str, &cooling_device_type)) {
            LOG(ERROR) << "Invalid "
                       << "CoolingDevice[" << name << "]'s Type: " << cooling_device_type_str;
            cooling_devices_parsed.clear();
            return cooling_devices_parsed;
        }

        std::vector<float> power2state;
        Json::Value values = cooling_devices[i]["Power2State"];
        if (values.size()) {
            power2state.reserve(values.size());
            for (Json::Value::ArrayIndex j = 0; j < values.size(); ++j) {
                power2state.emplace_back(getFloatFromValue(values[j]));
                LOG(INFO) << "Cooling device[" << name << "]'s Power2State[" << j
                          << "]: " << power2state[j];
            }
        } else {
            cooling_devices_parsed.clear();
            return cooling_devices_parsed;
        }

        cooling_devices_parsed[name] = {
                .type = cooling_device_type,
                .power2state = power2state,
        };
        ++total_parsed;
    }

    LOG(INFO) << total_parsed << " CoolingDevices parsed successfully";
    return cooling_devices_parsed;
}

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