common_library: gettid is multiple declaration in cl_error
[staging/basesystem.git] / video_in_hal / clock_hal / src / clock_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 "clock_hal.h"
17
18 #include <assert.h>
19 #include <dirent.h>
20 #include <errno.h>
21 #include <fcntl.h>
22 #include <linux/rtc.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include <sys/ioctl.h>
26 #include <unistd.h>
27
28 #include "clock_hal_clockhallog.h"
29
30 #define RTC_NODE_PATH_DEPTH 64
31 #define RTC_NODE_PERANT_PATH "/dev"
32
33 // set time to hardware
34 EFrameworkunifiedStatus SetHardwareClock(HANDLE h_app, const struct tm *l_tm) {
35   if (NULL == h_app) {
36     return eFrameworkunifiedStatusInvldHandle;
37   }
38   if (NULL == l_tm) {
39     return eFrameworkunifiedStatusNullPointer;
40   }
41
42   EFrameworkunifiedStatus e_status = eFrameworkunifiedStatusOK;
43   struct rtc_time hal_rtc_time;
44   hal_rtc_time.tm_year = l_tm->tm_year;
45   hal_rtc_time.tm_mon = l_tm->tm_mon;
46   hal_rtc_time.tm_mday = l_tm->tm_mday;
47   hal_rtc_time.tm_hour = l_tm->tm_hour;
48   hal_rtc_time.tm_min = l_tm->tm_min;
49   hal_rtc_time.tm_sec = l_tm->tm_sec;
50   hal_rtc_time.tm_wday = l_tm->tm_wday;
51   FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__,
52          "setting time : %d-%02d/%02d %02d:%02d:%02d\n",
53          1900 + l_tm->tm_year, l_tm->tm_mon + 1, l_tm->tm_mday,
54          l_tm->tm_hour, l_tm->tm_min, l_tm->tm_sec);
55
56   // Search RTC node
57   DIR *dir = ::opendir(RTC_NODE_PERANT_PATH);
58   char rtc_path[RTC_NODE_PATH_DEPTH] = { '\0' };
59   for (struct dirent *dp = readdir(dir); dp != NULL; dp = readdir(dir)) {
60     if (strstr(dp->d_name, "rtc") != NULL) {
61       snprintf(rtc_path, sizeof(rtc_path), "/dev/%s", dp->d_name);
62       break;
63     }
64   }
65   closedir(dir);
66   if (strlen(rtc_path) == 0) {
67     FRAMEWORKUNIFIEDLOG(ZONE_WARN, __FUNCTION__, "/dev/rtcX not found.\n");
68     e_status = eFrameworkunifiedStatusFail;
69     return e_status;
70   }
71
72   // Set time to /dev/rtcX
73   int fd = ::open(rtc_path, O_WRONLY);
74   if (-1 == fd) {
75     FRAMEWORKUNIFIEDLOG(ZONE_WARN, __FUNCTION__, "Error open(/dev/rtc)=[%d] errno=%d\n",
76            fd, errno);
77     return eFrameworkunifiedStatusFail;
78   }
79   int ret = ::ioctl(fd, RTC_SET_TIME, &hal_rtc_time);
80   if (0 != ret) {
81     FRAMEWORKUNIFIEDLOG(ZONE_WARN, __FUNCTION__,
82            "Error ioctl(fd, RTC_SET_TIME, hal_rtc_time)=[%d] errno=%d\n",
83            ret, errno);
84     e_status = eFrameworkunifiedStatusFail;
85   } else {
86     FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__,
87            "Success ioctl(fd, RTC_SET_TIME, hal_rtc_time)=[%d]\n", ret);
88   }
89   ::close(fd);
90   return e_status;
91 }