2 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
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.
18 #include <sys/types.h>
19 #include <sys/socket.h>
26 #include <vconf-keys.h>
29 #include "sysman-priv.h"
31 #define PREDEF_PWROFF_POPUP "pwroff-popup"
32 #define PREDEF_ENTERSLEEP "entersleep"
33 #define PREDEF_LEAVESLEEP "leavesleep"
34 #define PREDEF_REBOOT "reboot"
35 #define PREDEF_BACKGRD "backgrd"
36 #define PREDEF_FOREGRD "foregrd"
37 #define PREDEF_ACTIVE "active"
38 #define PREDEF_INACTIVE "inactive"
39 #define PREDEF_SET_DATETIME "set_datetime"
40 #define PREDEF_SET_TIMEZONE "set_timezone"
41 #define PREDEF_MOUNT_MMC "mountmmc"
42 #define PREDEF_UNMOUNT_MMC "unmountmmc"
43 #define PREDEF_FORMAT_MMC "formatmmc"
45 #define PREDEF_SET_MAX_FREQUENCY "set_max_frequency"
46 #define PREDEF_SET_MIN_FREQUENCY "set_min_frequency"
47 #define PREDEF_RELEASE_MAX_FREQUENCY "release_max_frequency"
48 #define PREDEF_RELEASE_MIN_FREQUENCY "release_min_frequency"
55 #define SYSNOTI_SOCKET_PATH "/tmp/sn"
57 static inline int send_int(int fd, int val)
59 return write(fd, &val, sizeof(int));
62 static inline int send_str(int fd, char *str)
68 ret = write(fd, &len, sizeof(int));
71 if (len > SYSMAN_MAXSTR)
73 write(fd, &len, sizeof(int));
74 ret = write(fd, str, len);
79 static int sysnoti_send(struct sysnoti *msg)
81 ERR("--- %s: start", __FUNCTION__);
85 struct sockaddr_un clientaddr;
88 client_sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
89 if (client_sockfd == -1) {
90 ERR("%s: socket create failed\n", __FUNCTION__);
93 bzero(&clientaddr, sizeof(clientaddr));
94 clientaddr.sun_family = AF_UNIX;
95 strncpy(clientaddr.sun_path, SYSNOTI_SOCKET_PATH, sizeof(clientaddr.sun_path) - 1);
96 client_len = sizeof(clientaddr);
98 if (connect(client_sockfd, (struct sockaddr *)&clientaddr, client_len) <
100 ERR("%s: connect failed\n", __FUNCTION__);
101 close(client_sockfd);
105 send_int(client_sockfd, msg->pid);
106 send_int(client_sockfd, msg->cmd);
107 send_str(client_sockfd, msg->type);
108 send_str(client_sockfd, msg->path);
109 send_int(client_sockfd, msg->argc);
110 for (i = 0; i < msg->argc; i++)
111 send_str(client_sockfd, msg->argv[i]);
113 ERR("--- %s: read", __FUNCTION__);
114 read(client_sockfd, &result, sizeof(int));
116 close(client_sockfd);
117 ERR("--- %s: end", __FUNCTION__);
121 API int sysman_call_predef_action(const char *type, int num, ...)
123 ERR("--- %s: start", __FUNCTION__);
131 if (type == NULL || num > SYSMAN_MAXARG) {
136 msg = malloc(sizeof(struct sysnoti));
139 /* Do something for not enought memory error */
144 msg->cmd = CALL_SYSMAN_ACTION;
145 msg->type = (char *)type;
149 va_start(argptr, num);
150 for (i = 0; i < num; i++) {
151 args = va_arg(argptr, char *);
156 ERR("--- %s: send msg", __FUNCTION__);
157 ret = sysnoti_send(msg);
160 ERR("--- %s: end", __FUNCTION__);
164 API int sysman_inform_foregrd(void)
167 snprintf(buf, sizeof(buf), "%d", getpid());
168 return sysman_call_predef_action(PREDEF_FOREGRD, 1, buf);
171 API int sysman_inform_backgrd(void)
174 snprintf(buf, sizeof(buf), "%d", getpid());
175 return sysman_call_predef_action(PREDEF_BACKGRD, 1, buf);
178 API int sysman_inform_active(pid_t pid)
181 snprintf(buf, sizeof(buf), "%d", pid);
182 return sysman_call_predef_action(PREDEF_ACTIVE, 1, buf);
185 API int sysman_inform_inactive(pid_t pid)
188 snprintf(buf, sizeof(buf), "%d", pid);
189 return sysman_call_predef_action(PREDEF_INACTIVE, 1, buf);
192 API int sysman_request_poweroff(void)
194 return sysman_call_predef_action(PREDEF_PWROFF_POPUP, 0);
197 API int sysman_request_entersleep(void)
199 return sysman_call_predef_action(PREDEF_ENTERSLEEP, 0);
202 API int sysman_request_leavesleep(void)
204 return sysman_call_predef_action(PREDEF_LEAVESLEEP, 0);
207 API int sysman_request_reboot(void)
209 return sysman_call_predef_action(PREDEF_REBOOT, 0);
212 API int sysman_set_datetime(time_t timet)
216 char buf[255] = { 0 };
217 snprintf(buf, sizeof(buf), "%ld", timet);
218 return sysman_call_predef_action(PREDEF_SET_DATETIME, 1, buf);
221 API int sysman_set_timezone(char *tzpath_str)
223 if (tzpath_str == NULL)
226 snprintf(buf, sizeof(buf), "%s", tzpath_str);
227 return sysman_call_predef_action(PREDEF_SET_TIMEZONE, 1, buf);
230 static int sysnoti_mount_mmc_cb(keynode_t *key_nodes, void *data)
232 struct mmc_contents *mmc_data;
233 mmc_data = (struct mmc_contents *)data;
234 DBG("mountmmc_cb called");
235 if (vconf_keynode_get_int(key_nodes) ==
236 VCONFKEY_SYSMAN_MMC_MOUNT_COMPLETED) {
238 (mmc_data->mmc_cb)(0, mmc_data->user_data);
239 } else if (vconf_keynode_get_int(key_nodes) ==
240 VCONFKEY_SYSMAN_MMC_MOUNT_ALREADY) {
241 DBG("mount already");
242 (mmc_data->mmc_cb)(-2, mmc_data->user_data);
245 (mmc_data->mmc_cb)(-1, mmc_data->user_data);
247 vconf_ignore_key_changed(VCONFKEY_SYSMAN_MMC_MOUNT,
248 (void *)sysnoti_mount_mmc_cb);
252 API int sysman_request_mount_mmc(struct mmc_contents *mmc_data)
254 if (mmc_data != NULL && mmc_data->mmc_cb != NULL)
255 vconf_notify_key_changed(VCONFKEY_SYSMAN_MMC_MOUNT,
256 (void *)sysnoti_mount_mmc_cb, (void *)mmc_data);
257 return sysman_call_predef_action(PREDEF_MOUNT_MMC, 0);
260 static int sysnoti_unmount_mmc_cb(keynode_t *key_nodes, void *data)
262 struct mmc_contents *mmc_data;
263 mmc_data = (struct mmc_contents *)data;
264 DBG("unmountmmc_cb called");
265 if (vconf_keynode_get_int(key_nodes) ==
266 VCONFKEY_SYSMAN_MMC_UNMOUNT_COMPLETED) {
268 (mmc_data->mmc_cb)(0, mmc_data->user_data);
271 (mmc_data->mmc_cb)(-1, mmc_data->user_data);
273 vconf_ignore_key_changed(VCONFKEY_SYSMAN_MMC_UNMOUNT,
274 (void *)sysnoti_unmount_mmc_cb);
278 API int sysman_request_unmount_mmc(struct mmc_contents *mmc_data, int option)
281 if (option != 1 && option != 2) {
282 DBG("option is wrong. default option 1 will be used");
285 snprintf(buf, sizeof(buf), "%d", option);
287 if (mmc_data != NULL && mmc_data->mmc_cb != NULL)
288 vconf_notify_key_changed(VCONFKEY_SYSMAN_MMC_UNMOUNT,
289 (void *)sysnoti_unmount_mmc_cb,
291 return sysman_call_predef_action(PREDEF_UNMOUNT_MMC, 1, buf);
294 static int sysnoti_format_mmc_cb(keynode_t *key_nodes, void *data)
296 struct mmc_contents *mmc_data;
297 mmc_data = (struct mmc_contents *)data;
298 DBG("format_cb called");
299 if (vconf_keynode_get_int(key_nodes) ==
300 VCONFKEY_SYSMAN_MMC_FORMAT_COMPLETED) {
302 (mmc_data->mmc_cb)(0, mmc_data->user_data);
306 (mmc_data->mmc_cb)(-1, mmc_data->user_data);
308 vconf_ignore_key_changed(VCONFKEY_SYSMAN_MMC_FORMAT,
309 (void *)sysnoti_format_mmc_cb);
313 API int sysman_request_format_mmc(struct mmc_contents *mmc_data)
315 if (mmc_data != NULL && mmc_data->mmc_cb != NULL)
316 vconf_notify_key_changed(VCONFKEY_SYSMAN_MMC_FORMAT,
317 (void *)sysnoti_format_mmc_cb,
319 return sysman_call_predef_action(PREDEF_FORMAT_MMC, 0);
322 API int sysman_request_set_cpu_max_frequency(int val)
327 // to do - need to check new frequncy is valid
328 snprintf(buf_pid, sizeof(buf_pid), "%d", getpid());
329 snprintf(buf_freq, sizeof(buf_freq), "%d", val * 1000);
331 return sysman_call_predef_action(PREDEF_SET_MAX_FREQUENCY, 2, buf_pid, buf_freq);
334 API int sysman_request_set_cpu_min_frequency(int val)
339 // to do - need to check new frequncy is valid
340 snprintf(buf_pid, sizeof(buf_pid), "%d", getpid());
341 snprintf(buf_freq, sizeof(buf_freq), "%d", val * 1000);
343 return sysman_call_predef_action(PREDEF_SET_MIN_FREQUENCY, 2, buf_pid, buf_freq);
346 API int sysman_release_cpu_max_frequency()
350 snprintf(buf_pid, sizeof(buf_pid), "%d", getpid());
352 return sysman_call_predef_action(PREDEF_RELEASE_MAX_FREQUENCY, 1, buf_pid);
355 API int sysman_release_cpu_min_frequency()
359 snprintf(buf_pid, sizeof(buf_pid), "%d", getpid());
361 return sysman_call_predef_action(PREDEF_RELEASE_MIN_FREQUENCY, 1, buf_pid);