2 * Copyright (c) 2000 - 2015 Samsung Electronics Co., Ltd All Rights Reserved
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.
23 #include <sys/types.h>
28 #include <bundle_internal.h>
34 #include "aul_error.h"
36 #include "aul_widget.h"
38 struct aul_widget_info_s {
48 struct widget_cb_info {
49 aul_widget_info_cb callback;
53 #define WIDGET_LOG_BUFFER_SIZE 10000
54 #define WIDGET_LOG_BUFFER_STRING_SIZE 256
56 static int __log_index;
58 static bool __log_init = false;
60 static int __init_log(void)
63 char buffer[256] = {0, };
64 char caller[255] = {0, };
67 ret = aul_app_get_appid_bypid(getpid(), caller, sizeof(caller));
68 if (ret != AUL_R_OK) {
69 _E("Failed to get appid by pid(%d)", getpid());
73 snprintf(buffer, sizeof(buffer),
74 "/run/aul/log/widget/%d/widget_%s.log", getuid(), caller);
75 __log_fd = open(buffer, O_CREAT | O_WRONLY, 0644);
77 _E("Failed to open %s - %d", buffer, errno);
81 offset = lseek(__log_fd, 0, SEEK_END);
83 __log_index = (int)(offset / WIDGET_LOG_BUFFER_STRING_SIZE);
84 if (__log_index >= WIDGET_LOG_BUFFER_SIZE) {
86 lseek(__log_fd, 0, SEEK_SET);
94 API int aul_widget_write_log(const char *tag, const char *format, ...)
99 char time_buf[32] = {0,};
100 char format_buffer[WIDGET_LOG_BUFFER_STRING_SIZE];
101 char buffer[WIDGET_LOG_BUFFER_STRING_SIZE];
108 _E("Invalid file descriptor");
113 ctime_r(&now, time_buf);
114 if (__log_index != 0)
115 offset = lseek(__log_fd, 0, SEEK_CUR);
117 offset = lseek(__log_fd, 0, SEEK_SET);
120 _E("error in lseek: %d", errno);
123 va_start(ap, format);
124 vsnprintf(format_buffer, sizeof(format_buffer), format, ap);
127 snprintf(buffer, sizeof(buffer), "[%-6d][%-5d] %-15s %-50s %s",
128 getpid(), __log_index, tag, format_buffer, time_buf);
130 ret = write(__log_fd, buffer, strlen(buffer));
132 _E("Cannot write the amd log: %d", ret);
136 if (++__log_index >= WIDGET_LOG_BUFFER_SIZE)
142 static const char *__to_appid(const char *widget_id)
145 appid = g_strstr_len(widget_id, strlen(widget_id), "@") + 1;
146 if (appid != (const char *)1) {
147 if (appid > widget_id + (sizeof(char) * strlen(widget_id)))
148 appid = (char *)widget_id;
150 appid = (char *)widget_id;
156 API int aul_widget_instance_add(const char *widget_id, const char *instance_id)
161 if (widget_id == NULL || instance_id == NULL)
164 kb = bundle_create();
170 bundle_add_str(kb, AUL_K_WIDGET_ID, widget_id);
171 bundle_add_str(kb, AUL_K_WIDGET_INSTANCE_ID, instance_id);
173 ret = aul_sock_send_bundle(AUL_UTIL_PID, getuid(), WIDGET_ADD, kb,
178 return aul_error_convert(ret);
183 API int aul_widget_instance_del(const char *widget_id, const char *instance_id)
188 if (widget_id == NULL || instance_id == NULL)
191 kb = bundle_create();
197 bundle_add_str(kb, AUL_K_WIDGET_ID, widget_id);
198 bundle_add_str(kb, AUL_K_WIDGET_INSTANCE_ID, instance_id);
200 ret = aul_sock_send_bundle(AUL_UTIL_PID, getuid(), WIDGET_DEL, kb,
205 return aul_error_convert(ret);
211 aul_widget_instance_foreach_cb cb;
215 static void __foreach_cb(const char *key, const int type,
216 const bundle_keyval_t *kv, void *user_data)
218 struct __cb_data *cb_data = (struct __cb_data *)user_data;
220 cb_data->cb(key, cb_data->data);
223 API int aul_widget_instance_foreach(const char *widget_id,
224 aul_widget_instance_foreach_cb cb, void *data)
229 app_pkt_t *pkt = NULL;
230 bundle *list_kb = NULL;
231 struct __cb_data cb_data;
233 if (widget_id == NULL)
236 kb = bundle_create();
242 bundle_add_str(kb, AUL_K_APPID, __to_appid(widget_id));
243 bundle_add_str(kb, AUL_K_WIDGET_ID, widget_id);
245 fd = aul_sock_send_bundle(AUL_UTIL_PID, getuid(), WIDGET_LIST, kb,
249 ret = aul_sock_recv_reply_pkt(fd, &pkt);
250 if (ret < 0 || pkt == NULL) {
251 _E("failed to get instance list of %s", widget_id);
253 list_kb = bundle_decode(pkt->data, pkt->len);
257 bundle_foreach(list_kb, __foreach_cb, &cb_data);
258 bundle_free(list_kb);
271 return aul_error_convert(ret);
276 API int aul_widget_instance_update(const char *widget_id,
277 const char *instance_id, bundle *param)
283 if (widget_id == NULL)
287 kb = bundle_create();
294 appid = __to_appid(widget_id);
296 bundle_add_str(kb, AUL_K_WIDGET_ID, widget_id);
299 bundle_add_str(kb, AUL_K_WIDGET_INSTANCE_ID, instance_id);
301 ret = app_request_to_launchpad_for_uid(WIDGET_UPDATE, appid, kb,
310 API int aul_widget_instance_get_content(const char *widget_id,
311 const char *instance_id, char **content)
316 app_pkt_t *pkt = NULL;
318 if (widget_id == NULL || instance_id == NULL || content == NULL)
321 kb = bundle_create();
327 bundle_add_str(kb, AUL_K_APPID, __to_appid(widget_id));
328 bundle_add_str(kb, AUL_K_WIDGET_ID, widget_id);
329 bundle_add_str(kb, AUL_K_WIDGET_INSTANCE_ID, instance_id);
331 ret = aul_sock_send_bundle(AUL_UTIL_PID, getuid(), WIDGET_GET_CONTENT,
334 ret = aul_sock_recv_reply_sock_fd(ret, &fd, 1);
336 ret = aul_sock_recv_reply_pkt(fd[0], &pkt);
337 if (ret == 0 && pkt && pkt->cmd == 0) {
338 *content = strdup((const char *)pkt->data);
339 _D("recieved content: %s", *content);
344 _E("failed to get content");
347 _E("failed to get socket fd:%d", ret);
356 ret = aul_error_convert(ret);
361 API int aul_widget_instance_count(const char *widget_id)
366 if (widget_id == NULL)
369 kb = bundle_create();
375 bundle_add_str(kb, AUL_K_WIDGET_ID, widget_id);
376 ret = app_send_cmd(AUL_UTIL_PID, WIDGET_COUNT, kb);
382 static void __foreach_widget_info(app_pkt_t *pkt, void *user_data)
384 struct widget_cb_info *cb_info = (struct widget_cb_info *)user_data;
385 struct aul_widget_info_s info = { 0, };
389 if (pkt == NULL || cb_info == NULL) {
390 _E("Invalid parameter");
394 if (pkt->cmd == APP_GET_INFO_ERROR) {
395 _E("Failed to get widget info");
399 if (pkt->opt & AUL_SOCK_BUNDLE)
400 b = bundle_decode(pkt->data, pkt->len);
405 bundle_get_str(b, AUL_K_WIDGET_ID, &info.widget_id);
406 if (info.widget_id == NULL) {
411 bundle_get_str(b, AUL_K_WIDGET_INSTANCE_ID, &info.instance_id);
412 if (info.instance_id == NULL) {
417 bundle_get_str(b, AUL_K_APPID, &info.app_id);
418 if (info.app_id == NULL) {
423 bundle_get_str(b, AUL_K_PKGID, &info.package_id);
424 if (info.package_id == NULL) {
429 bundle_get_str(b, AUL_K_EXEC, &info.app_path);
430 if (info.app_path == NULL) {
435 val = bundle_get_val(b, AUL_K_WID);
436 if (val && isdigit(*val))
437 info.surf = strtoul(val, NULL, 10);
439 val = bundle_get_val(b, AUL_K_PID);
440 if (val && isdigit(*val))
441 info.pid = atoi(val);
443 cb_info->callback(&info, cb_info->user_data);
448 API int aul_widget_info_foreach_for_uid(aul_widget_info_cb callback,
449 void *user_data, uid_t uid)
451 struct widget_cb_info cb_info = {callback, user_data};
452 char buf[MAX_PID_STR_BUFSZ];
457 if (callback == NULL) {
458 _E("Invalid parameter");
468 snprintf(buf, sizeof(buf), "%u", uid);
469 r = bundle_add_str(b, AUL_K_TARGET_UID, buf);
470 if (r != BUNDLE_ERROR_NONE) {
471 _E("Failed to add target uid(%u)", uid);
476 fd = aul_sock_send_bundle(AUL_UTIL_PID, uid, WIDGET_RUNNING_INFO,
480 return aul_error_convert(fd);
484 r = aul_sock_recv_pkt_with_cb(fd, __foreach_widget_info, &cb_info);
486 return aul_error_convert(r);
491 API int aul_widget_info_foreach(aul_widget_info_cb callback, void *user_data)
493 return aul_widget_info_foreach_for_uid(callback, user_data, getuid());
496 API int aul_widget_info_get_pid(aul_widget_info_h info, pid_t *pid)
498 if (info == NULL || pid == NULL) {
499 _E("Invalid parameter");
508 API int aul_widget_info_get_surface_id(aul_widget_info_h info,
511 if (info == NULL || surf == NULL) {
512 _E("Invalid parameter");
521 API int aul_widget_info_get_widget_id(aul_widget_info_h info, char **widget_id)
523 if (info == NULL || widget_id == NULL) {
524 _E("Invalid parameter");
528 *widget_id = strdup(info->widget_id);
529 if (*widget_id == NULL) {
537 API int aul_widget_info_get_instance_id(aul_widget_info_h info,
540 if (info == NULL || instance_id == NULL) {
541 _E("Invalid parameter");
545 *instance_id = strdup(info->instance_id);
546 if (*instance_id == NULL) {
554 API int aul_widget_info_get_app_id(aul_widget_info_h info, char **app_id)
556 if (info == NULL || app_id == NULL) {
557 _E("Invalid parameter");
561 *app_id = strdup(info->app_id);
562 if (*app_id == NULL) {
570 API int aul_widget_info_get_package_id(aul_widget_info_h info,
573 if (info == NULL || package_id == NULL) {
574 _E("Invalid parameter");
578 *package_id = strdup(info->package_id);
579 if (*package_id == NULL) {
587 API int aul_widget_info_get_app_path(aul_widget_info_h info, char **app_path)
589 if (info == NULL || app_path == NULL) {
590 _E("Invalid parameter");
594 *app_path = strdup(info->app_path);
595 if (*app_path == NULL) {
603 API int aul_widget_instance_change_status(const char *widget_id,
609 kb = bundle_create();
615 bundle_add_str(kb, AUL_K_STATUS, status);
616 bundle_add_str(kb, AUL_K_WIDGET_ID, widget_id);
617 ret = aul_sock_send_bundle(AUL_UTIL_PID, getuid(),
618 WIDGET_CHANGE_STATUS, kb, AUL_SOCK_NOREPLY);
622 _E("send error %d, %s", ret, status);
623 return aul_error_convert(ret);