Add default Smack manifest for sysman.spec
[platform/core/system/libslp-sysman.git] / sysnoti.c
1 /*
2  * libslp-sysman
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: DongGi Jang <dg0402.jang@samsung.com>
7  * 
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20 */
21
22
23 #include <sys/types.h>
24 #include <sys/socket.h>
25 #include <sys/un.h>
26 #include <stdio.h>
27 #include <unistd.h>
28 #include <stdarg.h>
29 #include <errno.h>
30 #include <vconf.h>
31 #include <vconf-keys.h>
32
33 #include "sysman.h"
34 #include "sysman-priv.h"
35
36 #define PREDEF_PWROFF_POPUP                     "pwroff-popup"
37 #define PREDEF_ENTERSLEEP                       "entersleep"
38 #define PREDEF_LEAVESLEEP                       "leavesleep"
39 #define PREDEF_REBOOT                           "reboot"
40 #define PREDEF_BACKGRD                          "backgrd"
41 #define PREDEF_FOREGRD                          "foregrd"
42 #define PREDEF_ACTIVE                           "active"
43 #define PREDEF_INACTIVE                         "inactive"
44 #define PREDEF_SET_DATETIME                     "set_datetime"
45 #define PREDEF_SET_TIMEZONE                     "set_timezone"
46 #define PREDEF_MOUNT_MMC                        "mountmmc"
47 #define PREDEF_UNMOUNT_MMC                      "unmountmmc"
48 #define PREDEF_FORMAT_MMC                       "formatmmc"
49
50 #define PREDEF_SET_MAX_FREQUENCY                "set_max_frequency"
51 #define PREDEF_SET_MIN_FREQUENCY                "set_min_frequency"
52 #define PREDEF_RELEASE_MAX_FREQUENCY            "release_max_frequency"
53 #define PREDEF_RELEASE_MIN_FREQUENCY            "release_min_frequency"
54
55 enum sysnoti_cmd {
56         ADD_SYSMAN_ACTION,
57         CALL_SYSMAN_ACTION
58 };
59
60 #define SYSNOTI_SOCKET_PATH "/tmp/sn"
61
62 static inline int send_int(int fd, int val)
63 {
64         return write(fd, &val, sizeof(int));
65 }
66
67 static inline int send_str(int fd, char *str)
68 {
69         int len;
70         int ret;
71         if (str == NULL) {
72                 len = 0;
73                 ret = write(fd, &len, sizeof(int));
74         } else {
75                 len = strlen(str);
76                 if (len > SYSMAN_MAXSTR)
77                         len = SYSMAN_MAXSTR;
78                 write(fd, &len, sizeof(int));
79                 ret = write(fd, str, len);
80         }
81         return ret;
82 }
83
84 static int sysnoti_send(struct sysnoti *msg)
85 {
86         ERR("--- %s: start", __FUNCTION__);
87         int client_len;
88         int client_sockfd;
89         int result;
90         struct sockaddr_un clientaddr;
91         int i;
92
93         client_sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
94         if (client_sockfd == -1) {
95                 printf("%s: socket create failed\n", __FUNCTION__);
96                 return -1;
97         }
98         bzero(&clientaddr, sizeof(clientaddr));
99         clientaddr.sun_family = AF_UNIX;
100         strncpy(clientaddr.sun_path, SYSNOTI_SOCKET_PATH, sizeof(clientaddr.sun_path) - 1);
101         client_len = sizeof(clientaddr);
102
103         if (connect(client_sockfd, (struct sockaddr *)&clientaddr, client_len) <
104             0) {
105                 printf("%s: connect failed\n", __FUNCTION__);
106                 return -1;
107         }
108
109         send_int(client_sockfd, msg->pid);
110         send_int(client_sockfd, msg->cmd);
111         send_str(client_sockfd, msg->type);
112         send_str(client_sockfd, msg->path);
113         send_int(client_sockfd, msg->argc);
114         for (i = 0; i < msg->argc; i++)
115                 send_str(client_sockfd, msg->argv[i]);
116
117         ERR("--- %s: read", __FUNCTION__);
118         read(client_sockfd, &result, sizeof(int));
119
120         close(client_sockfd);
121         ERR("--- %s: end", __FUNCTION__);
122         return result;
123 }
124
125 API int sysman_call_predef_action(const char *type, int num, ...)
126 {
127         ERR("--- %s: start", __FUNCTION__);
128         struct sysnoti *msg;
129         int ret;
130         va_list argptr;
131
132         int i;
133         char *args = NULL;
134
135         if (type == NULL || num > SYSMAN_MAXARG) {
136                 errno = EINVAL;
137                 return -1;
138         }
139
140         msg = malloc(sizeof(struct sysnoti));
141
142         if (msg == NULL) {
143                 /* Do something for not enought memory error */
144                 return -1;
145         }
146
147         msg->pid = getpid();
148         msg->cmd = CALL_SYSMAN_ACTION;
149         msg->type = (char *)type;
150         msg->path = NULL;
151
152         msg->argc = num;
153         va_start(argptr, num);
154         for (i = 0; i < num; i++) {
155                 args = va_arg(argptr, char *);
156                 msg->argv[i] = args;
157         }
158         va_end(argptr);
159
160         ERR("--- %s: send msg", __FUNCTION__);
161         ret = sysnoti_send(msg);
162         free(msg);
163
164         ERR("--- %s: end", __FUNCTION__);
165         return ret;
166 }
167
168 API int sysman_inform_foregrd(void)
169 {
170         char buf[255];
171         snprintf(buf, sizeof(buf), "%d", getpid());
172         return sysman_call_predef_action(PREDEF_FOREGRD, 1, buf);
173 }
174
175 API int sysman_inform_backgrd(void)
176 {
177         char buf[255];
178         snprintf(buf, sizeof(buf), "%d", getpid());
179         return sysman_call_predef_action(PREDEF_BACKGRD, 1, buf);
180 }
181
182 API int sysman_inform_active(pid_t pid)
183 {
184         char buf[255];
185         snprintf(buf, sizeof(buf), "%d", pid);
186         return sysman_call_predef_action(PREDEF_ACTIVE, 1, buf);
187 }
188
189 API int sysman_inform_inactive(pid_t pid)
190 {
191         char buf[255];
192         snprintf(buf, sizeof(buf), "%d", pid);
193         return sysman_call_predef_action(PREDEF_INACTIVE, 1, buf);
194 }
195
196 API int sysman_request_poweroff(void)
197 {
198         return sysman_call_predef_action(PREDEF_PWROFF_POPUP, 0);
199 }
200
201 API int sysman_request_entersleep(void)
202 {
203         return sysman_call_predef_action(PREDEF_ENTERSLEEP, 0);
204 }
205
206 API int sysman_request_leavesleep(void)
207 {
208         return sysman_call_predef_action(PREDEF_LEAVESLEEP, 0);
209 }
210
211 API int sysman_request_reboot(void)
212 {
213         return sysman_call_predef_action(PREDEF_REBOOT, 0);
214 }
215
216 API int sysman_set_datetime(time_t timet)
217 {
218         if (timet < 0L)
219                 return -1;
220         char buf[255] = { 0 };
221         snprintf(buf, sizeof(buf), "%ld", timet);
222         return sysman_call_predef_action(PREDEF_SET_DATETIME, 1, buf);
223 }
224
225 API int sysman_set_timezone(char *tzpath_str)
226 {
227         if (tzpath_str == NULL)
228                 return -1;
229         char buf[255];
230         snprintf(buf, sizeof(buf), "%s", tzpath_str);
231         return sysman_call_predef_action(PREDEF_SET_TIMEZONE, 1, buf);
232 }
233
234 static int sysnoti_mount_mmc_cb(keynode_t *key_nodes, void *data)
235 {
236         struct mmc_contents *mmc_data;
237         mmc_data = (struct mmc_contents *)data;
238         DBG("mountmmc_cb called");
239         if (vconf_keynode_get_int(key_nodes) ==
240             VCONFKEY_SYSMAN_MMC_MOUNT_COMPLETED) {
241                 DBG("mount ok");
242                 (mmc_data->mmc_cb)(0, mmc_data->user_data); 
243         } else if (vconf_keynode_get_int(key_nodes) ==
244                    VCONFKEY_SYSMAN_MMC_MOUNT_ALREADY) {
245                 DBG("mount already");
246                 (mmc_data->mmc_cb)(-2, mmc_data->user_data); 
247         } else {
248                 DBG("mount fail");
249                 (mmc_data->mmc_cb)(-1, mmc_data->user_data); 
250         }
251         vconf_ignore_key_changed(VCONFKEY_SYSMAN_MMC_MOUNT,
252                                  (void *)sysnoti_mount_mmc_cb);
253         return 0;
254 }
255
256 API int sysman_request_mount_mmc(struct mmc_contents *mmc_data)
257 {
258         if (mmc_data != NULL && mmc_data->mmc_cb != NULL)
259                 vconf_notify_key_changed(VCONFKEY_SYSMAN_MMC_MOUNT,
260                                          (void *)sysnoti_mount_mmc_cb, (void *)mmc_data);
261         return sysman_call_predef_action(PREDEF_MOUNT_MMC, 0);
262 }
263
264 static int sysnoti_unmount_mmc_cb(keynode_t *key_nodes, void *data)
265 {
266         struct mmc_contents *mmc_data;
267         mmc_data = (struct mmc_contents *)data;
268         DBG("unmountmmc_cb called");
269         if (vconf_keynode_get_int(key_nodes) ==
270             VCONFKEY_SYSMAN_MMC_UNMOUNT_COMPLETED) {
271                 DBG("unmount ok");
272                 (mmc_data->mmc_cb)(0, mmc_data->user_data); 
273         } else {
274                 DBG("unmount fail");
275                 (mmc_data->mmc_cb)(-1, mmc_data->user_data); 
276         }
277         vconf_ignore_key_changed(VCONFKEY_SYSMAN_MMC_UNMOUNT,
278                                  (void *)sysnoti_unmount_mmc_cb);
279         return 0;
280 }
281
282 API int sysman_request_unmount_mmc(struct mmc_contents *mmc_data, int option)
283 {
284         char buf[255];
285         if (option != 1 && option != 2) {
286                 DBG("option is wrong. default option 1 will be used");
287                 option = 1;
288         }
289         snprintf(buf, sizeof(buf), "%d", option);
290
291         if (mmc_data != NULL && mmc_data->mmc_cb != NULL)
292                 vconf_notify_key_changed(VCONFKEY_SYSMAN_MMC_UNMOUNT,
293                                          (void *)sysnoti_unmount_mmc_cb,
294                                          (void *)mmc_data);
295         return sysman_call_predef_action(PREDEF_UNMOUNT_MMC, 1, buf);
296 }
297
298 static int sysnoti_format_mmc_cb(keynode_t *key_nodes, void *data)
299 {
300         struct mmc_contents *mmc_data;
301         mmc_data = (struct mmc_contents *)data;
302         DBG("format_cb called");
303         if (vconf_keynode_get_int(key_nodes) ==
304             VCONFKEY_SYSMAN_MMC_FORMAT_COMPLETED) {
305                 DBG("format ok");
306                 (mmc_data->mmc_cb)(0, mmc_data->user_data); 
307
308         } else {
309                 DBG("format fail");
310                 (mmc_data->mmc_cb)(-1, mmc_data->user_data); 
311         }
312         vconf_ignore_key_changed(VCONFKEY_SYSMAN_MMC_FORMAT,
313                                  (void *)sysnoti_format_mmc_cb);
314         return 0;
315 }
316
317 API int sysman_request_format_mmc(struct mmc_contents *mmc_data)
318 {
319         if (mmc_data != NULL && mmc_data->mmc_cb != NULL)
320                 vconf_notify_key_changed(VCONFKEY_SYSMAN_MMC_FORMAT,
321                                          (void *)sysnoti_format_mmc_cb,
322                                          (void *)mmc_data);
323         return sysman_call_predef_action(PREDEF_FORMAT_MMC, 0);
324 }
325
326 API int sysman_request_set_cpu_max_frequency(int val)
327 {
328         char buf_pid[8];
329         char buf_freq[256];
330         
331         // to do - need to check new frequncy is valid
332         snprintf(buf_pid, sizeof(buf_pid), "%d", getpid());
333         snprintf(buf_freq, sizeof(buf_freq), "%d", val * 1000);
334
335         return sysman_call_predef_action(PREDEF_SET_MAX_FREQUENCY, 2, buf_pid, buf_freq);
336 }
337
338 API int sysman_request_set_cpu_min_frequency(int val)
339 {
340         char buf_pid[8];
341         char buf_freq[256];
342         
343         // to do - need to check new frequncy is valid
344         snprintf(buf_pid, sizeof(buf_pid), "%d", getpid());
345         snprintf(buf_freq, sizeof(buf_freq), "%d", val * 1000);
346
347         return sysman_call_predef_action(PREDEF_SET_MIN_FREQUENCY, 2, buf_pid, buf_freq);
348 }
349
350 API int sysman_release_cpu_max_frequency()
351 {
352         char buf_pid[8];
353         
354         snprintf(buf_pid, sizeof(buf_pid), "%d", getpid());
355         
356         return sysman_call_predef_action(PREDEF_RELEASE_MAX_FREQUENCY, 1, buf_pid);
357 }
358
359 API int sysman_release_cpu_min_frequency()
360 {
361         char buf_pid[8];
362         
363         snprintf(buf_pid, sizeof(buf_pid), "%d", getpid());
364
365         return sysman_call_predef_action(PREDEF_RELEASE_MIN_FREQUENCY, 1, buf_pid);
366 }