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 "clock_hal.h"
22 #include <linux/rtc.h>
25 #include <sys/ioctl.h>
28 #include "clock_hal_clockhallog.h"
30 #define RTC_NODE_PATH_DEPTH 64
31 #define RTC_NODE_PERANT_PATH "/dev"
33 // set time to hardware
34 EFrameworkunifiedStatus SetHardwareClock(HANDLE h_app, const struct tm *l_tm) {
36 return eFrameworkunifiedStatusInvldHandle;
39 return eFrameworkunifiedStatusNullPointer;
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);
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);
66 if (strlen(rtc_path) == 0) {
67 FRAMEWORKUNIFIEDLOG(ZONE_WARN, __FUNCTION__, "/dev/rtcX not found.\n");
68 e_status = eFrameworkunifiedStatusFail;
72 // Set time to /dev/rtcX
73 int fd = ::open(rtc_path, O_WRONLY);
75 FRAMEWORKUNIFIEDLOG(ZONE_WARN, __FUNCTION__, "Error open(/dev/rtc)=[%d] errno=%d\n",
77 return eFrameworkunifiedStatusFail;
79 int ret = ::ioctl(fd, RTC_SET_TIME, &hal_rtc_time);
81 FRAMEWORKUNIFIEDLOG(ZONE_WARN, __FUNCTION__,
82 "Error ioctl(fd, RTC_SET_TIME, hal_rtc_time)=[%d] errno=%d\n",
84 e_status = eFrameworkunifiedStatusFail;
86 FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__,
87 "Success ioctl(fd, RTC_SET_TIME, hal_rtc_time)=[%d]\n", ret);