common_library: gettid is multiple declaration in cl_error
[staging/basesystem.git] / video_in_hal / soc_temperature_hal / src / soc_temperature_hal.cpp
1 /*
2  * @copyright Copyright (c) 2017-2020 TOYOTA MOTOR CORPORATION.
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 #include "soc_temperature_hal.h"
17
18 #include <errno.h>
19 #include <fcntl.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <unistd.h>
23 #include "soc_temperature_hal_soctemperaturehallog.h"
24
25 #define MAX_TEMPERATURE_FROM_DRIVER_LENGTH (9)  // 9bytes can content -999999\n\0 - 9999999\n\0 (unit:millicelsius)
26 #define DECIMAL  (10)
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"
30
31 EFrameworkunifiedStatus GetCoreTemperature(int64_t* temperature, const char* node_path) {
32   if ((NULL == temperature) || (NULL == node_path)) {
33     return eFrameworkunifiedStatusNullPointer;
34   }
35
36   char sz_temperature[MAX_TEMPERATURE_FROM_DRIVER_LENGTH] = { 0 };
37
38   // read temperature
39   int fd = open(node_path, O_RDONLY);
40   if (0 > fd) {
41     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "open error(%d,%s)", errno, strerror(errno));
42     return eFrameworkunifiedStatusFail;
43   }
44
45   ssize_t ret = read(fd, sz_temperature, MAX_TEMPERATURE_FROM_DRIVER_LENGTH - 1);
46   int read_error = errno;
47   close(fd);
48   if (-1 == ret) {
49     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "read error(%d,%s)", read_error, strerror(read_error));
50     return eFrameworkunifiedStatusFail;
51   }
52
53   errno = 0;
54   int64_t temp = strtol(sz_temperature, NULL, DECIMAL);
55   if (errno) {
56     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "strtol error(%d,%s)", errno, strerror(errno));
57     return eFrameworkunifiedStatusFail;
58   }
59
60   *temperature = temp;
61   return eFrameworkunifiedStatusOK;
62 }
63
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;
69   }
70
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;
77   }
78
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;
85   }
86
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;
93   }
94
95   // get max temperature
96   int64_t max_temperature = temperature0;
97   if (max_temperature < temperature1) {
98     max_temperature = temperature1;
99   }
100   if (max_temperature < temperature2) {
101     max_temperature = temperature2;
102   }
103
104   *temperature = max_temperature / 1000.0f;
105   return eFrameworkunifiedStatusOK;
106 }