4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: DongGi Jang <dg0402.jang@samsung.com>
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
23 #include <sys/types.h>
24 #include <sys/socket.h>
31 #include <vconf-keys.h>
34 #include "sysman-priv.h"
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"
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"
60 #define SYSNOTI_SOCKET_PATH "/tmp/sn"
62 static inline int send_int(int fd, int val)
64 return write(fd, &val, sizeof(int));
67 static inline int send_str(int fd, char *str)
73 ret = write(fd, &len, sizeof(int));
76 if (len > SYSMAN_MAXSTR)
78 write(fd, &len, sizeof(int));
79 ret = write(fd, str, len);
84 static int sysnoti_send(struct sysnoti *msg)
86 ERR("--- %s: start", __FUNCTION__);
90 struct sockaddr_un clientaddr;
93 client_sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
94 if (client_sockfd == -1) {
95 printf("%s: socket create failed\n", __FUNCTION__);
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);
103 if (connect(client_sockfd, (struct sockaddr *)&clientaddr, client_len) <
105 printf("%s: connect failed\n", __FUNCTION__);
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]);
117 ERR("--- %s: read", __FUNCTION__);
118 read(client_sockfd, &result, sizeof(int));
120 close(client_sockfd);
121 ERR("--- %s: end", __FUNCTION__);
125 API int sysman_call_predef_action(const char *type, int num, ...)
127 ERR("--- %s: start", __FUNCTION__);
135 if (type == NULL || num > SYSMAN_MAXARG) {
140 msg = malloc(sizeof(struct sysnoti));
143 /* Do something for not enought memory error */
148 msg->cmd = CALL_SYSMAN_ACTION;
149 msg->type = (char *)type;
153 va_start(argptr, num);
154 for (i = 0; i < num; i++) {
155 args = va_arg(argptr, char *);
160 ERR("--- %s: send msg", __FUNCTION__);
161 ret = sysnoti_send(msg);
164 ERR("--- %s: end", __FUNCTION__);
168 API int sysman_inform_foregrd(void)
171 snprintf(buf, sizeof(buf), "%d", getpid());
172 return sysman_call_predef_action(PREDEF_FOREGRD, 1, buf);
175 API int sysman_inform_backgrd(void)
178 snprintf(buf, sizeof(buf), "%d", getpid());
179 return sysman_call_predef_action(PREDEF_BACKGRD, 1, buf);
182 API int sysman_inform_active(pid_t pid)
185 snprintf(buf, sizeof(buf), "%d", pid);
186 return sysman_call_predef_action(PREDEF_ACTIVE, 1, buf);
189 API int sysman_inform_inactive(pid_t pid)
192 snprintf(buf, sizeof(buf), "%d", pid);
193 return sysman_call_predef_action(PREDEF_INACTIVE, 1, buf);
196 API int sysman_request_poweroff(void)
198 return sysman_call_predef_action(PREDEF_PWROFF_POPUP, 0);
201 API int sysman_request_entersleep(void)
203 return sysman_call_predef_action(PREDEF_ENTERSLEEP, 0);
206 API int sysman_request_leavesleep(void)
208 return sysman_call_predef_action(PREDEF_LEAVESLEEP, 0);
211 API int sysman_request_reboot(void)
213 return sysman_call_predef_action(PREDEF_REBOOT, 0);
216 API int sysman_set_datetime(time_t timet)
220 char buf[255] = { 0 };
221 snprintf(buf, sizeof(buf), "%ld", timet);
222 return sysman_call_predef_action(PREDEF_SET_DATETIME, 1, buf);
225 API int sysman_set_timezone(char *tzpath_str)
227 if (tzpath_str == NULL)
230 snprintf(buf, sizeof(buf), "%s", tzpath_str);
231 return sysman_call_predef_action(PREDEF_SET_TIMEZONE, 1, buf);
234 static int sysnoti_mount_mmc_cb(keynode_t *key_nodes, void *data)
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) {
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);
249 (mmc_data->mmc_cb)(-1, mmc_data->user_data);
251 vconf_ignore_key_changed(VCONFKEY_SYSMAN_MMC_MOUNT,
252 (void *)sysnoti_mount_mmc_cb);
256 API int sysman_request_mount_mmc(struct mmc_contents *mmc_data)
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);
264 static int sysnoti_unmount_mmc_cb(keynode_t *key_nodes, void *data)
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) {
272 (mmc_data->mmc_cb)(0, mmc_data->user_data);
275 (mmc_data->mmc_cb)(-1, mmc_data->user_data);
277 vconf_ignore_key_changed(VCONFKEY_SYSMAN_MMC_UNMOUNT,
278 (void *)sysnoti_unmount_mmc_cb);
282 API int sysman_request_unmount_mmc(struct mmc_contents *mmc_data, int option)
285 if (option != 1 && option != 2) {
286 DBG("option is wrong. default option 1 will be used");
289 snprintf(buf, sizeof(buf), "%d", option);
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,
295 return sysman_call_predef_action(PREDEF_UNMOUNT_MMC, 1, buf);
298 static int sysnoti_format_mmc_cb(keynode_t *key_nodes, void *data)
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) {
306 (mmc_data->mmc_cb)(0, mmc_data->user_data);
310 (mmc_data->mmc_cb)(-1, mmc_data->user_data);
312 vconf_ignore_key_changed(VCONFKEY_SYSMAN_MMC_FORMAT,
313 (void *)sysnoti_format_mmc_cb);
317 API int sysman_request_format_mmc(struct mmc_contents *mmc_data)
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,
323 return sysman_call_predef_action(PREDEF_FORMAT_MMC, 0);
326 API int sysman_request_set_cpu_max_frequency(int val)
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);
335 return sysman_call_predef_action(PREDEF_SET_MAX_FREQUENCY, 2, buf_pid, buf_freq);
338 API int sysman_request_set_cpu_min_frequency(int val)
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);
347 return sysman_call_predef_action(PREDEF_SET_MIN_FREQUENCY, 2, buf_pid, buf_freq);
350 API int sysman_release_cpu_max_frequency()
354 snprintf(buf_pid, sizeof(buf_pid), "%d", getpid());
356 return sysman_call_predef_action(PREDEF_RELEASE_MAX_FREQUENCY, 1, buf_pid);
359 API int sysman_release_cpu_min_frequency()
363 snprintf(buf_pid, sizeof(buf_pid), "%d", getpid());
365 return sysman_call_predef_action(PREDEF_RELEASE_MIN_FREQUENCY, 1, buf_pid);