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>
29 #include <tzplatform_config.h>
33 #include "aul_app_com.h"
35 #include "aul_error.h"
38 #include "aul_widget.h"
41 struct aul_widget_info_s {
51 struct widget_cb_info {
52 aul_widget_info_cb callback;
56 struct widget_event_s {
57 aul_app_com_connection_h conn;
58 aul_widget_event_cb callback;
62 #define WIDGET_LOG_BUFFER_SIZE 10000
63 #define WIDGET_LOG_BUFFER_STRING_SIZE 256
65 static int __log_index;
67 static bool __log_init = false;
68 static struct widget_event_s __event;
70 static int __init_log(void)
73 char buffer[512] = {0, };
74 char caller[255] = {0, };
77 ret = aul_app_get_appid_bypid(getpid(), caller, sizeof(caller));
78 if (ret != AUL_R_OK) {
79 _E("Failed to get appid by pid(%d)", getpid());
83 snprintf(buffer, sizeof(buffer),
84 "/run/aul/log/widget/%d/widget_%s.log", getuid(), caller);
85 __log_fd = open(buffer, O_CREAT | O_WRONLY, 0644);
87 _E("Failed to open %s - %d", buffer, errno);
91 offset = lseek(__log_fd, 0, SEEK_END);
93 __log_index = (int)(offset / WIDGET_LOG_BUFFER_STRING_SIZE);
94 if (__log_index >= WIDGET_LOG_BUFFER_SIZE) {
96 lseek(__log_fd, 0, SEEK_SET);
104 API int aul_widget_write_log(const char *tag, const char *format, ...)
109 char time_buf[32] = {0,};
110 char format_buffer[WIDGET_LOG_BUFFER_STRING_SIZE];
111 char buffer[WIDGET_LOG_BUFFER_SIZE];
118 _E("Invalid file descriptor");
123 ctime_r(&now, time_buf);
124 if (__log_index != 0)
125 offset = lseek(__log_fd, 0, SEEK_CUR);
127 offset = lseek(__log_fd, 0, SEEK_SET);
130 _E("error in lseek: %d", errno);
133 va_start(ap, format);
134 vsnprintf(format_buffer, sizeof(format_buffer), format, ap);
137 snprintf(buffer, sizeof(buffer), "[%-6d][%-5d] %-15s %-50s %s",
138 getpid(), __log_index, tag, format_buffer, time_buf);
140 ret = write(__log_fd, buffer, strlen(buffer));
142 _E("Cannot write the amd log: %d", ret);
146 if (++__log_index >= WIDGET_LOG_BUFFER_SIZE)
152 static const char *__to_appid(const char *widget_id)
155 appid = g_strstr_len(widget_id, strlen(widget_id), "@") + 1;
156 if (appid != (const char *)1) {
157 if (appid > widget_id + (sizeof(char) * strlen(widget_id)))
158 appid = (char *)widget_id;
160 appid = (char *)widget_id;
166 API int aul_widget_instance_add(const char *widget_id, const char *instance_id)
171 if (widget_id == NULL || instance_id == NULL)
174 kb = bundle_create();
180 bundle_add_str(kb, AUL_K_WIDGET_ID, widget_id);
181 bundle_add_str(kb, AUL_K_WIDGET_INSTANCE_ID, instance_id);
183 ret = aul_sock_send_bundle(AUL_UTIL_PID, getuid(), WIDGET_ADD, kb,
188 return aul_error_convert(ret);
193 API int aul_widget_instance_del(const char *widget_id, const char *instance_id)
198 if (widget_id == NULL || instance_id == NULL)
201 kb = bundle_create();
207 bundle_add_str(kb, AUL_K_WIDGET_ID, widget_id);
208 bundle_add_str(kb, AUL_K_WIDGET_INSTANCE_ID, instance_id);
210 ret = aul_sock_send_bundle(AUL_UTIL_PID, getuid(), WIDGET_DEL, kb,
215 return aul_error_convert(ret);
221 aul_widget_instance_foreach_cb cb;
225 static void __foreach_cb(const char *key, const int type,
226 const bundle_keyval_t *kv, void *user_data)
228 struct __cb_data *cb_data = (struct __cb_data *)user_data;
230 cb_data->cb(key, cb_data->data);
233 API int aul_widget_instance_foreach(const char *widget_id,
234 aul_widget_instance_foreach_cb cb, void *data)
239 app_pkt_t *pkt = NULL;
240 bundle *list_kb = NULL;
241 struct __cb_data cb_data;
243 if (widget_id == NULL)
246 kb = bundle_create();
252 bundle_add_str(kb, AUL_K_APPID, __to_appid(widget_id));
253 bundle_add_str(kb, AUL_K_WIDGET_ID, widget_id);
255 fd = aul_sock_send_bundle(AUL_UTIL_PID, getuid(), WIDGET_LIST, kb,
259 ret = aul_sock_recv_reply_pkt(fd, &pkt);
260 if (ret < 0 || pkt == NULL) {
261 _E("failed to get instance list of %s", widget_id);
263 list_kb = bundle_decode(pkt->data, pkt->len);
267 bundle_foreach(list_kb, __foreach_cb, &cb_data);
268 bundle_free(list_kb);
281 return aul_error_convert(ret);
286 API int aul_widget_instance_update(const char *widget_id,
287 const char *instance_id, bundle *param)
293 if (widget_id == NULL)
297 kb = bundle_create();
304 appid = __to_appid(widget_id);
306 bundle_add_str(kb, AUL_K_WIDGET_ID, widget_id);
309 bundle_add_str(kb, AUL_K_WIDGET_INSTANCE_ID, instance_id);
311 ret = app_request_to_launchpad_for_uid(WIDGET_UPDATE, appid, kb,
320 API int aul_widget_instance_get_content(const char *widget_id,
321 const char *instance_id, char **content)
326 app_pkt_t *pkt = NULL;
328 if (widget_id == NULL || instance_id == NULL || content == NULL)
331 kb = bundle_create();
337 bundle_add_str(kb, AUL_K_APPID, __to_appid(widget_id));
338 bundle_add_str(kb, AUL_K_WIDGET_ID, widget_id);
339 bundle_add_str(kb, AUL_K_WIDGET_INSTANCE_ID, instance_id);
341 ret = aul_sock_send_bundle(AUL_UTIL_PID, getuid(), WIDGET_GET_CONTENT,
344 ret = aul_sock_recv_reply_sock_fd(ret, &fd, 1);
346 ret = aul_sock_recv_reply_pkt(fd[0], &pkt);
347 if (ret == 0 && pkt && pkt->cmd == 0) {
348 *content = strdup((const char *)pkt->data);
349 _D("recieved content: %s", *content);
354 _E("failed to get content");
357 _E("failed to get socket fd:%d", ret);
366 ret = aul_error_convert(ret);
371 API int aul_widget_instance_count(const char *widget_id)
376 if (widget_id == NULL)
379 kb = bundle_create();
385 bundle_add_str(kb, AUL_K_WIDGET_ID, widget_id);
386 ret = app_send_cmd(AUL_UTIL_PID, WIDGET_COUNT, kb);
392 static void __foreach_widget_info(app_pkt_t *pkt, void *user_data)
394 struct widget_cb_info *cb_info = (struct widget_cb_info *)user_data;
395 struct aul_widget_info_s info = { 0, };
399 if (pkt == NULL || cb_info == NULL) {
400 _E("Invalid parameter");
404 if (pkt->cmd == APP_GET_INFO_ERROR) {
405 _E("Failed to get widget info");
409 if (pkt->opt & AUL_SOCK_BUNDLE)
410 b = bundle_decode(pkt->data, pkt->len);
415 bundle_get_str(b, AUL_K_WIDGET_ID, &info.widget_id);
416 if (info.widget_id == NULL) {
421 bundle_get_str(b, AUL_K_WIDGET_INSTANCE_ID, &info.instance_id);
422 if (info.instance_id == NULL) {
427 bundle_get_str(b, AUL_K_APPID, &info.app_id);
428 if (info.app_id == NULL) {
433 bundle_get_str(b, AUL_K_PKGID, &info.package_id);
434 if (info.package_id == NULL) {
439 bundle_get_str(b, AUL_K_EXEC, &info.app_path);
440 if (info.app_path == NULL) {
445 val = bundle_get_val(b, AUL_K_WID);
446 if (val && isdigit(*val))
447 info.surf = strtoul(val, NULL, 10);
449 val = bundle_get_val(b, AUL_K_PID);
450 if (val && isdigit(*val))
451 info.pid = atoi(val);
453 cb_info->callback(&info, cb_info->user_data);
458 API int aul_widget_info_foreach_for_uid(aul_widget_info_cb callback,
459 void *user_data, uid_t uid)
461 struct widget_cb_info cb_info = {callback, user_data};
462 char buf[MAX_PID_STR_BUFSZ];
467 if (callback == NULL) {
468 _E("Invalid parameter");
478 snprintf(buf, sizeof(buf), "%u", uid);
479 r = bundle_add_str(b, AUL_K_TARGET_UID, buf);
480 if (r != BUNDLE_ERROR_NONE) {
481 _E("Failed to add target uid(%u)", uid);
486 fd = aul_sock_send_bundle(AUL_UTIL_PID, uid, WIDGET_RUNNING_INFO,
490 return aul_error_convert(fd);
494 r = aul_sock_recv_pkt_with_cb(fd, __foreach_widget_info, &cb_info);
496 return aul_error_convert(r);
501 API int aul_widget_info_foreach(aul_widget_info_cb callback, void *user_data)
503 return aul_widget_info_foreach_for_uid(callback, user_data, getuid());
506 API int aul_widget_info_get_pid(aul_widget_info_h info, pid_t *pid)
508 if (info == NULL || pid == NULL) {
509 _E("Invalid parameter");
518 API int aul_widget_info_get_surface_id(aul_widget_info_h info,
521 if (info == NULL || surf == NULL) {
522 _E("Invalid parameter");
531 API int aul_widget_info_get_widget_id(aul_widget_info_h info, char **widget_id)
533 if (info == NULL || widget_id == NULL) {
534 _E("Invalid parameter");
538 *widget_id = strdup(info->widget_id);
539 if (*widget_id == NULL) {
547 API int aul_widget_info_get_instance_id(aul_widget_info_h info,
550 if (info == NULL || instance_id == NULL) {
551 _E("Invalid parameter");
555 *instance_id = strdup(info->instance_id);
556 if (*instance_id == NULL) {
564 API int aul_widget_info_get_app_id(aul_widget_info_h info, char **app_id)
566 if (info == NULL || app_id == NULL) {
567 _E("Invalid parameter");
571 *app_id = strdup(info->app_id);
572 if (*app_id == NULL) {
580 API int aul_widget_info_get_package_id(aul_widget_info_h info,
583 if (info == NULL || package_id == NULL) {
584 _E("Invalid parameter");
588 *package_id = strdup(info->package_id);
589 if (*package_id == NULL) {
597 API int aul_widget_info_get_app_path(aul_widget_info_h info, char **app_path)
599 if (info == NULL || app_path == NULL) {
600 _E("Invalid parameter");
604 *app_path = strdup(info->app_path);
605 if (*app_path == NULL) {
613 API int aul_widget_instance_change_status(const char *widget_id,
619 kb = bundle_create();
625 bundle_add_str(kb, AUL_K_STATUS, status);
626 bundle_add_str(kb, AUL_K_WIDGET_ID, widget_id);
627 ret = aul_sock_send_bundle(AUL_UTIL_PID, getuid(),
628 WIDGET_CHANGE_STATUS, kb, AUL_SOCK_NOREPLY);
632 _E("send error %d, %s", ret, status);
633 return aul_error_convert(ret);
639 API int aul_widget_service_set_disable(const char *widget_id, bool is_disable)
643 char ambient_mode[32] = {0, };
645 if (widget_id == NULL)
648 kb = bundle_create();
654 snprintf(ambient_mode, sizeof(ambient_mode), "%d", (int)is_disable);
656 bundle_add_str(kb, AUL_K_APPID, __to_appid(widget_id));
657 bundle_add_str(kb, AUL_K_WIDGET_ID, widget_id);
658 bundle_add_str(kb, AUL_K_WIDGET_DISABLE, ambient_mode);
660 ret = app_send_cmd(AUL_UTIL_PID, WIDGET_DISABLE, kb);
662 _E("widget disable send cmd error");
669 static int __aul_widget_event_cb(const char *endpoint,
670 aul_app_com_result_e result,
674 const char *event_name;
675 const char *event_data_raw;
678 event_name = bundle_get_val(envelope, AUL_K_EVENT_NAME);
680 _E("Failed to get event name");
684 event_data_raw = bundle_get_val(envelope, AUL_K_EVENT_DATA);
685 if (!event_data_raw) {
686 _E("Failed to get event data");
690 event_data = bundle_decode((const bundle_raw *)event_data_raw,
691 strlen(event_data_raw));
693 _E("Failed to decode event data");
697 if (__event.callback)
698 __event.callback(event_name, event_data, __event.user_data);
700 bundle_free(event_data);
705 static int __register_widget_event(void)
710 ret = aul_app_com_create("widget.event", NULL,
711 __aul_widget_event_cb, NULL,
713 if (ret != AUL_R_OK) {
714 _E("Failed to join aul app com");
722 static int __unregister_widget_event(void)
725 aul_app_com_leave(__event.conn);
732 API int aul_widget_set_event_cb(aul_widget_event_cb callback, void *user_data)
735 _E("Invalid parameter");
739 __event.callback = callback;
740 __event.user_data = user_data;
742 return __register_widget_event();
745 API int aul_widget_unset_event_cb(void)
747 __event.callback = NULL;
748 __event.user_data = NULL;
750 return __unregister_widget_event();
753 API int aul_widget_send_event(const char *event_name, bundle *event_data)
756 bundle_raw *raw = NULL;
760 if (!event_name || !event_data) {
761 _E("Invalid parameter");
771 bundle_add(b, AUL_K_EVENT_NAME, event_name);
773 bundle_encode(event_data, &raw, &len);
775 _E("Failed to encode event data");
780 bundle_add(b, AUL_K_EVENT_DATA, (const char *)raw);
783 ret = aul_sock_send_bundle(AUL_UTIL_PID, getuid(), WIDGET_EVENT, b,
787 _E("Failed to send widget event. error(%d)", ret);
788 return aul_error_convert(ret);
794 API int aul_widget_send_status_to_viewer(const char *class_id,
795 const char *instance_id, const char *viewer_endpoint,
796 int status, int err, bundle *extra)
799 bundle_raw *raw = NULL;
804 data = bundle_create();
811 snprintf(err_str, sizeof(err_str), "%d", err);
812 bundle_add_str(data, AUL_K_WIDGET_ERROR_CODE, err_str);
815 bundle_add_str(data, AUL_K_WIDGET_ID, class_id);
816 bundle_add_str(data, AUL_K_WIDGET_INSTANCE_ID, instance_id);
817 bundle_add_byte(data, AUL_K_WIDGET_STATUS, &status, sizeof(int));
820 bundle_encode(extra, &raw, &len);
822 "__WIDGET_CONTENT_INFO__", (const char *)raw);
824 ret = aul_widget_instance_add(class_id, instance_id);
826 _E("Failed to add instance. error(%d)", ret);
828 return aul_error_convert(ret);
832 LOGD("send update %s(%d) to %s", instance_id, status, viewer_endpoint);
833 ret = aul_app_com_send(viewer_endpoint, data);
836 _E("Failed to add instance. error(%d)", ret);
837 return aul_error_convert(ret);
843 API int aul_widget_send_status_to_service(const char *class_id,
844 const char *instance_id, const char *sender_pkgid, int status)
846 bundle *data = bundle_create();
854 bundle_add_str(data, AUL_K_WIDGET_ID, class_id);
855 bundle_add_str(data, AUL_K_WIDGET_INSTANCE_ID, instance_id);
856 bundle_add_byte(data, AUL_K_WIDGET_STATUS, &status, sizeof(int));
857 bundle_add_str(data, AUL_K_PKGID, sender_pkgid);
859 LOGD("send lifecycle %s(%d)", instance_id, status);
860 ret = aul_app_com_send("widget.status", data);
862 _E("send lifecycle error:%d", ret);