2 * @copyright Copyright (c) 2017-2020 TOYOTA MOTOR CORPORATION.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #include "soc_temperature_hal.h"
23 #include "soc_temperature_hal_soctemperaturehallog.h"
25 #define MAX_TEMPERATURE_FROM_DRIVER_LENGTH (9) // 9bytes can content -999999\n\0 - 9999999\n\0 (unit:millicelsius)
27 #define THERMAL_ZONE0_TEMPERATURE_PATH "/sys/class/thermal/thermal_zone0/temp"
28 #define THERMAL_ZONE1_TEMPERATURE_PATH "/sys/class/thermal/thermal_zone1/temp"
29 #define THERMAL_ZONE2_TEMPERATURE_PATH "/sys/class/thermal/thermal_zone2/temp"
31 EFrameworkunifiedStatus GetCoreTemperature(int64_t* temperature, const char* node_path) {
32 if ((NULL == temperature) || (NULL == node_path)) {
33 return eFrameworkunifiedStatusNullPointer;
36 char sz_temperature[MAX_TEMPERATURE_FROM_DRIVER_LENGTH] = { 0 };
39 int fd = open(node_path, O_RDONLY);
41 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "open error(%d,%s)", errno, strerror(errno));
42 return eFrameworkunifiedStatusFail;
45 ssize_t ret = read(fd, sz_temperature, MAX_TEMPERATURE_FROM_DRIVER_LENGTH - 1);
46 int read_error = errno;
49 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "read error(%d,%s)", read_error, strerror(read_error));
50 return eFrameworkunifiedStatusFail;
54 int64_t temp = strtol(sz_temperature, NULL, DECIMAL);
56 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "strtol error(%d,%s)", errno, strerror(errno));
57 return eFrameworkunifiedStatusFail;
61 return eFrameworkunifiedStatusOK;
64 // Get SoC temperature value.
65 EFrameworkunifiedStatus GetSoCTemperature(float *temperature) {
66 if (NULL == temperature) {
67 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "parameter *temperature is null");
68 return eFrameworkunifiedStatusNullPointer;
71 // Get temperature from thermal_zone0
72 int64_t temperature0 = 0;
73 EFrameworkunifiedStatus e_status = GetCoreTemperature(&temperature0, THERMAL_ZONE0_TEMPERATURE_PATH);
74 if (eFrameworkunifiedStatusOK != e_status) {
75 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "get temperature0 failed");
76 return eFrameworkunifiedStatusFail;
79 // Get temperature from thermal_zone1
80 int64_t temperature1 = 0;
81 e_status = GetCoreTemperature(&temperature1, THERMAL_ZONE1_TEMPERATURE_PATH);
82 if (eFrameworkunifiedStatusOK != e_status) {
83 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "get temperature1 failed");
84 return eFrameworkunifiedStatusFail;
87 // Get temperature from thermal_zone2
88 int64_t temperature2 = 0;
89 e_status = GetCoreTemperature(&temperature2, THERMAL_ZONE2_TEMPERATURE_PATH);
90 if (eFrameworkunifiedStatusOK != e_status) {
91 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "get temperature2 failed");
92 return eFrameworkunifiedStatusFail;
95 // get max temperature
96 int64_t max_temperature = temperature0;
97 if (max_temperature < temperature1) {
98 max_temperature = temperature1;
100 if (max_temperature < temperature2) {
101 max_temperature = temperature2;
104 *temperature = max_temperature / 1000.0f;
105 return eFrameworkunifiedStatusOK;