change usb control process name to usb-server
[framework/system/system-server.git] / ss_timemgr.c
1 /*
2  * Copyright 2012  Samsung Electronics Co., Ltd
3  *
4  * Licensed under the Flora License, Version 1.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.tizenopensource.org/license
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
17
18 #include <stdio.h>
19 #include <stdbool.h>
20 #include <unistd.h>
21 #include <sys/types.h>
22 #include <errno.h>
23 #include <sys/stat.h>
24 #include <pmapi.h>
25 #include <vconf.h>
26
27 #include <sysman.h>
28 #include "include/ss_data.h"
29 #include "ss_queue.h"
30 #include "ss_log.h"
31
32 #include <time.h>
33 #include <sys/ioctl.h>
34 #include <linux/rtc.h>
35 #include <fcntl.h>
36
37
38 static const char default_rtc0[] = "/dev/rtc0";
39 static const char default_rtc1[] = "/dev/rtc1";
40 static const char default_localtime[] = "/opt/etc/localtime";
41
42 char *substring(const char *str, size_t begin, size_t len)
43 {
44         if (str == 0 || strlen(str) == 0 || strlen(str) < begin
45             || strlen(str) < (begin + len))
46                 return 0;
47
48         return strndup(str + begin, len);
49 }
50
51 int handle_timezone(char *str)
52 {
53         int ret;
54         struct stat sts;
55         const char *sympath = default_localtime;
56
57         if (str == NULL)
58                 return -1;
59         const char *tzpath = str;
60
61         PRT_TRACE("TZPATH = %s\n", tzpath);
62
63         /* unlink current link
64          * eg. rm /opt/etc/localtime */
65         if (stat(sympath, &sts) == -1 && errno == ENOENT) {
66                 /* DO NOTHING */
67         } else {
68                 ret = unlink(sympath);
69                 if (ret < 0) {
70                         PRT_TRACE("unlink error : [%d]%s\n", ret,
71                                   strerror(errno));
72                         return -1;
73                 } else
74                         PRT_TRACE("unlink success\n");
75
76         }
77
78         /* symlink new link
79          * eg. ln -s /usr/share/zoneinfo/Asia/Seoul /opt/etc/localtime */
80         ret = symlink(tzpath, sympath);
81         if (ret < 0) {
82                 PRT_TRACE("symlink error : [%d]%s\n", ret, strerror(errno));
83                 return -1;
84         } else
85                 PRT_TRACE("symlink success\n");
86
87         tzset();
88         return 0;
89 }
90
91 /*
92  * TODO : error handling code should be added here.
93  */
94 int handle_date(char *str)
95 {
96         long int tmp = 0;
97         time_t timet = 0;
98         time_t before = 0;
99
100         if (str == NULL)
101                 return -1;
102
103         tmp = (long int)atoi(str);
104         timet = (time_t) tmp;
105
106         PRT_TRACE("ctime = %s", ctime(&timet));
107         vconf_set_int(VCONFKEY_SYSTEM_TIMECHANGE, timet);
108
109         return 0;
110 }
111
112 int set_datetime_action(int argc, char **argv)
113 {
114         int ret = 0;
115         unsigned int pm_state;
116         if (argc < 1)
117                 return -1;
118         if (vconf_get_int(VCONFKEY_PM_STATE, &ret) != 0)
119                 PRT_TRACE("Fail to get vconf value for pm state\n");
120         if (ret == 1)
121                 pm_state = 0x1;
122         else if (ret == 2)
123                 pm_state = 0x2;
124         else
125                 pm_state = 0x4;
126
127         pm_lock_state(pm_state, STAY_CUR_STATE, 0);
128         ret = handle_date(argv[0]);
129         pm_unlock_state(pm_state, STAY_CUR_STATE);
130         return ret;
131 }
132
133 int set_timezone_action(int argc, char **argv)
134 {
135         int ret;
136         unsigned int pm_state;
137         if (argc < 1)
138                 return -1;
139         if (vconf_get_int(VCONFKEY_PM_STATE, &ret) != 0)
140                 PRT_TRACE("Fail to get vconf value for pm state\n");
141         if (ret == 1)
142                 pm_state = 0x1;
143         else if (ret == 2)
144                 pm_state = 0x2;
145         else
146                 pm_state = 0x4;
147
148         pm_lock_state(pm_state, STAY_CUR_STATE, 0);
149         ret = handle_timezone(argv[0]);
150         pm_unlock_state(pm_state, STAY_CUR_STATE);
151         return ret;
152 }
153
154 int ss_time_manager_init(void)
155 {
156         ss_action_entry_add_internal(PREDEF_SET_DATETIME, set_datetime_action,
157                                      NULL, NULL);
158         ss_action_entry_add_internal(PREDEF_SET_TIMEZONE, set_timezone_action,
159                                      NULL, NULL);
160         return 0;
161 }