2 * Copyright 2013 Samsung Electronics Co., Ltd
4 * Licensed under the Flora License, Version 1.1 (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://floralicense.org/license/
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.
27 #include <vconf-keys.h>
31 #include <com-core_packet.h>
32 #include <livebox-errno.h>
37 #include "livebox_internal.h"
38 #include "desc_parser.h"
41 #include "master_rpc.h"
43 #include "critical_log.h"
55 static struct packet *master_fault_package(pid_t pid, int handle, const struct packet *packet)
61 if (packet_get(packet, "sss", &pkgname, &id, &function) != 3) {
62 ErrPrint("Invalid arguments\n");
66 master_rpc_clear_fault_package(pkgname);
67 lb_invoke_fault_handler(LB_FAULT_DEACTIVATED, pkgname, id, function);
71 static struct packet *master_hold_scroll(pid_t pid, int handle, const struct packet *packet)
73 struct livebox *handler;
79 ret = packet_get(packet, "ssi", &pkgname, &id, &seize);
81 ErrPrint("Invalid argument\n");
85 handler = lb_find_livebox(pkgname, id);
87 ErrPrint("Instance(%s) is not exists\n", id);
91 DbgPrint("HOLD: %s %d\n", id, seize);
92 lb_invoke_event_handler(handler, seize ? LB_EVENT_HOLD_SCROLL : LB_EVENT_RELEASE_SCROLL);
98 static struct packet *master_pinup(pid_t pid, int handle, const struct packet *packet)
103 struct livebox *handler;
109 ret = packet_get(packet, "iisss", &status, &pinup, &pkgname, &id, &content);
111 ErrPrint("Invalid argument\n");
115 handler = lb_find_livebox(pkgname, id);
117 ErrPrint("Instance (%s) is not exists\n", id);
122 new_content = strdup(content);
124 free(handler->content);
125 handler->content = new_content;
126 handler->is_pinned_up = pinup;
128 ErrPrint("Heap: %s\n", strerror(errno));
129 status = LB_STATUS_ERROR_MEMORY;
133 if (handler->pinup_cb) {
137 /* Make sure that user can call pinup API in its result callback */
138 cb = handler->pinup_cb;
139 cbdata = handler->pinup_cbdata;
141 handler->pinup_cb = NULL;
142 handler->pinup_cbdata = NULL;
144 cb(handler, status, cbdata);
145 } else if (status == 0) {
146 lb_invoke_event_handler(handler, LB_EVENT_PINUP_CHANGED);
153 static struct packet *master_deleted(pid_t pid, int handle, const struct packet *packet)
158 struct livebox *handler;
160 if (packet_get(packet, "ssd", &pkgname, &id, ×tamp) != 3) {
161 ErrPrint("Invalid arguemnt\n");
165 handler = lb_find_livebox_by_timestamp(timestamp);
169 * This can be happens only if the user delete a livebox
170 * right after create it before receive created event.
175 /*!< Check validity of this "handler" */
176 if (handler->state != CREATE) {
177 if (handler->state != DELETE) {
180 * This is not possible
182 CRITICAL_LOG("Already deleted handler (%s - %s)\n", pkgname, id);
187 if (handler->created_cb) {
193 * "if (handler->id == NULL)"
195 * The instance is not created yet.
196 * But the master forcely destroy it and send destroyed event to this
197 * without the created event.
199 * It could be destroyed when a slave has critical error(fault)
200 * before creating an instance successfully.
202 if (handler->created_cb == handler->deleted_cb) {
203 if (handler->created_cbdata != handler->deleted_cbdata)
204 DbgPrint("cb is same but cbdata is different (%s - %s)\n", pkgname, id);
206 handler->deleted_cb = NULL;
207 handler->deleted_cbdata = NULL;
210 cb = handler->created_cb;
211 cbdata = handler->created_cbdata;
213 handler->created_cb = NULL;
214 handler->created_cbdata = NULL;
216 cb(handler, LB_STATUS_ERROR_CANCEL, cbdata);
217 } else if (handler->id) {
218 if (handler->deleted_cb) {
222 cb = handler->deleted_cb;
223 cbdata = handler->deleted_cbdata;
225 handler->deleted_cb = NULL;
226 handler->deleted_cbdata = NULL;
228 cb(handler, LB_STATUS_SUCCESS, cbdata);
230 lb_invoke_event_handler(handler, LB_EVENT_DELETED);
234 /* Just try to delete it, if a user didn't remove it from the live box list */
241 static struct packet *master_lb_update_begin(pid_t pid, int handle, const struct packet *packet)
243 struct livebox *handler;
252 ret = packet_get(packet, "ssdsss", &pkgname, &id, &priority, &content, &title, &fbfile);
254 ErrPrint("Invalid argument\n");
258 handler = lb_find_livebox(pkgname, id);
260 ErrPrint("Instance[%s] is not exists\n", id);
264 if (handler->state != CREATE) {
265 ErrPrint("(%s) is not created\n", id);
269 lb_set_priority(handler, priority);
270 lb_set_content(handler, content);
271 lb_set_title(handler, title);
275 * Width & Height is not changed in this case.
276 * If the active update is began, the size should not be changed,
277 * And if the size is changed, the provider should finish the updating first.
278 * And then begin updating again after change its size.
280 if (lb_get_lb_fb(handler)) {
281 lb_set_lb_fb(handler, fbfile);
283 ret = fb_sync(lb_get_lb_fb(handler));
284 if (ret != LB_STATUS_SUCCESS)
285 ErrPrint("Failed to do sync FB (%s - %s) (%d)\n", pkgname, fbfile, ret);
287 lb_invoke_event_handler(handler, LB_EVENT_LB_UPDATE_BEGIN);
289 ErrPrint("Invalid request[%s], %s\n", id, fbfile);
296 static struct packet *master_pd_update_begin(pid_t pid, int handle, const struct packet *packet)
298 struct livebox *handler;
304 ret = packet_get(packet, "sss", &pkgname, &id, &fbfile);
306 ErrPrint("Invalid argument\n");
310 handler = lb_find_livebox(pkgname, id);
312 ErrPrint("Instance[%s] is not exists\n", id);
316 if (handler->state != CREATE) {
317 ErrPrint("[%s] is not created\n", id);
321 if (lb_get_pd_fb(handler)) {
322 lb_set_lb_fb(handler, fbfile);
324 ret = fb_sync(lb_get_lb_fb(handler));
325 if (ret != LB_STATUS_SUCCESS)
326 ErrPrint("Failed to do sync FB (%s - %s) (%d)\n", pkgname, fbfile, ret);
328 lb_invoke_event_handler(handler, LB_EVENT_PD_UPDATE_BEGIN);
330 ErrPrint("Invalid request[%s], %s\n", id, fbfile);
337 static struct packet *master_lb_update_end(pid_t pid, int handle, const struct packet *packet)
339 struct livebox *handler;
344 ret = packet_get(packet, "ss", &pkgname, &id);
346 ErrPrint("Invalid argument\n");
350 handler = lb_find_livebox(pkgname, id);
352 ErrPrint("Instance[%s] is not exists\n", id);
356 if (handler->state != CREATE) {
357 ErrPrint("[%s] is not created\n", id);
361 if (lb_get_lb_fb(handler)) {
362 lb_invoke_event_handler(handler, LB_EVENT_LB_UPDATE_END);
364 ErrPrint("Invalid request[%s]\n", id);
371 static struct packet *master_access_status(pid_t pid, int handle, const struct packet *packet)
373 struct livebox *handler;
379 ret = packet_get(packet, "ssi", &pkgname, &id, &status);
381 ErrPrint("Invalid argument\n");
385 handler = lb_find_livebox(pkgname, id);
387 ErrPrint("Instance[%s] is not exists\n", id);
391 if (handler->state != CREATE) {
392 ErrPrint("[%s] is not created\n", id);
396 if (handler->access_event_cb) {
400 cb = handler->access_event_cb;
401 cbdata = handler->access_event_cbdata;
403 handler->access_event_cb = NULL;
404 handler->access_event_cbdata = NULL;
406 cb(handler, status, cbdata);
408 ErrPrint("Invalid event[%s]\n", id);
414 static struct packet *master_pd_update_end(pid_t pid, int handle, const struct packet *packet)
416 struct livebox *handler;
421 ret = packet_get(packet, "ss", &pkgname, &id);
423 ErrPrint("Invalid argument\n");
427 handler = lb_find_livebox(pkgname, id);
429 ErrPrint("Instance[%s] is not exists\n", id);
433 if (handler->state != CREATE) {
434 ErrPrint("[%s] is not created\n", id);
438 if (lb_get_lb_fb(handler)) {
439 lb_invoke_event_handler(handler, LB_EVENT_PD_UPDATE_END);
441 ErrPrint("Invalid request[%s]", id);
448 static struct packet *master_lb_updated(pid_t pid, int handle, const struct packet *packet)
455 struct livebox *handler;
461 ret = packet_get(packet, "sssiidss",
463 &fbfile, &lb_w, &lb_h,
464 &priority, &content, &title);
466 ErrPrint("Invalid argument\n");
470 handler = lb_find_livebox(pkgname, id);
472 ErrPrint("instance(%s) is not exists\n", id);
476 if (handler->state != CREATE) {
479 * Already deleted by the user.
480 * Don't try to notice anything with this, Just ignore all events
481 * Beacuse the user doesn't wants know about this anymore
483 ErrPrint("(%s) is not exists, but updated\n", id);
487 lb_set_priority(handler, priority);
488 lb_set_content(handler, content);
489 lb_set_title(handler, title);
490 lb_set_size(handler, lb_w, lb_h);
492 if (lb_text_lb(handler)) {
493 (void)parse_desc(handler, livebox_filename(handler), 0);
496 * DESC parser will call the "text event callback".
497 * Don't need to call global event callback in this case.
500 } else if (lb_get_lb_fb(handler)) {
501 lb_set_lb_fb(handler, fbfile);
502 ret = fb_sync(lb_get_lb_fb(handler));
503 if (ret != LB_STATUS_SUCCESS)
504 ErrPrint("Failed to do sync FB (%s - %s) (%d)\n", pkgname, util_basename(util_uri_to_path(id)), ret);
506 ret = LB_STATUS_SUCCESS;
509 if (ret == LB_STATUS_SUCCESS)
510 lb_invoke_event_handler(handler, LB_EVENT_LB_UPDATED);
516 static struct packet *master_pd_created(pid_t pid, int handle, const struct packet *packet)
518 struct livebox *handler;
527 ret = packet_get(packet, "sssiii", &pkgname, &id, &buf_id, &width, &height, &status);
529 ErrPrint("Invalid argument\n");
533 handler = lb_find_livebox(pkgname, id);
535 ErrPrint("Instance(%s) is not exists\n", id);
539 if (handler->state != CREATE) {
540 ErrPrint("Instance(%s) is not created\n", id);
544 lb_set_pdsize(handler, width, height);
545 if (lb_text_pd(handler)) {
546 DbgPrint("Text TYPE does not need to handle this\n");
548 (void)lb_set_pd_fb(handler, buf_id);
549 ret = fb_sync(lb_get_pd_fb(handler));
551 ErrPrint("Failed to do sync FB (%s - %s)\n", pkgname, util_basename(util_uri_to_path(id)));
554 handler->is_pd_created = (status == 0);
556 if (handler->pd_created_cb) {
560 cb = handler->pd_created_cb;
561 cbdata = handler->pd_created_cbdata;
563 handler->pd_created_cb = NULL;
564 handler->pd_created_cbdata = NULL;
567 * Before call the Callback function,
568 * pd_create_cb must be reset.
569 * Because, in the create callback, user can call create_pd function again.
571 DbgPrint("PERF_DBOX\n");
572 cb(handler, status, cbdata);
573 } else if (handler->is_pd_created) {
574 lb_invoke_event_handler(handler, LB_EVENT_PD_CREATED);
581 static struct packet *master_pd_destroyed(pid_t pid, int handle, const struct packet *packet)
583 struct livebox *handler;
589 ret = packet_get(packet, "ssi", &pkgname, &id, &status);
591 ErrPrint("Invalid argument\n");
595 handler = lb_find_livebox(pkgname, id);
597 ErrPrint("Instance(%s) is not exists\n", id);
601 if (handler->state != CREATE) {
602 ErrPrint("Instance(%s) is not created\n", id);
606 handler->is_pd_created = 0;
608 if (handler->pd_destroyed_cb) {
612 cb = handler->pd_destroyed_cb;
613 cbdata = handler->pd_destroyed_cbdata;
615 handler->pd_destroyed_cb = NULL;
616 handler->pd_destroyed_cbdata = NULL;
619 * Before call the Callback function,
620 * pd_destroyed_cb must be reset.
621 * Because, in the create callback, user can call destroy_pd function again.
623 cb(handler, status, cbdata);
624 } else if (status == 0) {
625 lb_invoke_event_handler(handler, LB_EVENT_PD_DESTROYED);
632 static struct packet *master_pd_updated(pid_t pid, int handle, const struct packet *packet)
636 const char *descfile;
639 struct livebox *handler;
643 ret = packet_get(packet, "ssssii",
648 ErrPrint("Invalid argument\n");
652 handler = lb_find_livebox(pkgname, id);
654 ErrPrint("Instance(%s) is not exists\n", id);
658 if (handler->state != CREATE) {
661 * This handler is already deleted by the user.
662 * So don't try to notice anything about this anymore.
663 * Just ignore all events.
665 ErrPrint("Instance(%s) is not created\n", id);
669 lb_set_pdsize(handler, pd_w, pd_h);
671 if (lb_text_pd(handler)) {
672 (void)parse_desc(handler, descfile, 1);
674 (void)lb_set_pd_fb(handler, fbfile);
676 ret = fb_sync(lb_get_pd_fb(handler));
678 ErrPrint("Failed to do sync FB (%s - %s), %d\n", pkgname, util_basename(util_uri_to_path(id)), ret);
680 lb_invoke_event_handler(handler, LB_EVENT_PD_UPDATED);
687 static struct packet *master_update_mode(pid_t pid, int handle, const struct packet *packet)
689 struct livebox *handler;
697 ErrPrint("Invalid packet\n");
701 ret = packet_get(packet, "ssii", &pkgname, &id, &status, &active_mode);
703 ErrPrint("Invalid argument\n");
707 handler = lb_find_livebox(pkgname, id);
709 ErrPrint("Livebox(%s) is not found\n", id);
713 if (handler->state != CREATE) {
714 ErrPrint("Livebox(%s) is not created yet\n", id);
718 if (status == LB_STATUS_SUCCESS)
719 lb_set_update_mode(handler, active_mode);
721 if (handler->update_mode_cb) {
725 cb = handler->update_mode_cb;
726 cbdata = handler->update_mode_cbdata;
728 handler->update_mode_cb = NULL;
729 handler->update_mode_cbdata = NULL;
731 cb(handler, status, cbdata);
733 lb_invoke_event_handler(handler, LB_EVENT_UPDATE_MODE_CHANGED);
740 static struct packet *master_size_changed(pid_t pid, int handle, const struct packet *packet)
742 struct livebox *handler;
753 ErrPrint("Invalid packet\n");
757 ret = packet_get(packet, "sssiiii", &pkgname, &id, &fbfile, &is_pd, &w, &h, &status);
759 ErrPrint("Invalid argument\n");
763 handler = lb_find_livebox(pkgname, id);
765 ErrPrint("Livebox(%s) is not found\n", id);
769 if (handler->state != CREATE) {
770 ErrPrint("Livebox(%s) is not created yet\n", id);
777 * PD is not able to resized by the client.
778 * PD is only can be managed by the provider.
779 * So the PD has no private resized event handler.
780 * Notify it via global event handler only.
783 lb_set_pdsize(handler, w, h);
784 lb_invoke_event_handler(handler, LB_EVENT_PD_SIZE_CHANGED);
786 ErrPrint("This is not possible. PD Size is changed but the return value is not ZERO (%d)\n", status);
790 lb_set_size(handler, w, h);
794 * If there is a created LB FB,
797 if (lb_get_lb_fb(handler)) {
798 lb_set_lb_fb(handler, fbfile);
800 ret = fb_sync(lb_get_lb_fb(handler));
802 ErrPrint("Failed to do sync FB (%s - %s)\n", pkgname, util_basename(util_uri_to_path(id)));
804 /* Just update the size info only. */
809 * I cannot believe client.
810 * So I added some log before & after call the user callback.
812 if (handler->size_changed_cb) {
816 cb = handler->size_changed_cb;
817 cbdata = handler->size_cbdata;
819 handler->size_changed_cb = NULL;
820 handler->size_cbdata = NULL;
822 cb(handler, status, cbdata);
824 lb_invoke_event_handler(handler, LB_EVENT_LB_SIZE_CHANGED);
827 if (handler->size_changed_cb) {
831 cb = handler->size_changed_cb;
832 cbdata = handler->size_cbdata;
834 handler->size_changed_cb = NULL;
835 handler->size_cbdata = NULL;
837 cb(handler, status, cbdata);
846 static struct packet *master_period_changed(pid_t pid, int handle, const struct packet *packet)
848 struct livebox *handler;
855 ret = packet_get(packet, "idss", &status, &period, &pkgname, &id);
857 ErrPrint("Invalid argument\n");
861 handler = lb_find_livebox(pkgname, id);
863 ErrPrint("Livebox(%s) is not found\n", id);
867 if (handler->state != CREATE) {
868 ErrPrint("Livebox(%s) is not created\n", id);
873 lb_set_period(handler, period);
875 if (handler->period_changed_cb) {
879 cb = handler->period_changed_cb;
880 cbdata = handler->period_cbdata;
882 handler->period_changed_cb = NULL;
883 handler->period_cbdata = NULL;
885 cb(handler, status, cbdata);
886 } else if (status == 0) {
887 lb_invoke_event_handler(handler, LB_EVENT_PERIOD_CHANGED);
894 static struct packet *master_group_changed(pid_t pid, int handle, const struct packet *packet)
896 struct livebox *handler;
901 const char *category;
904 ret = packet_get(packet, "ssiss", &pkgname, &id, &status, &cluster, &category);
906 ErrPrint("Invalid argument\n");
910 handler = lb_find_livebox(pkgname, id);
912 ErrPrint("Livebox(%s) is not exists\n", id);
916 if (handler->state != CREATE) {
919 * Do no access this handler,
920 * You cannot believe this handler anymore.
922 ErrPrint("Livebox(%s) is not created\n", id);
927 (void)lb_set_group(handler, cluster, category);
929 if (handler->group_changed_cb) {
933 cb = handler->group_changed_cb;
934 cbdata = handler->group_cbdata;
936 handler->group_changed_cb = NULL;
937 handler->group_cbdata = NULL;
939 cb(handler, status, cbdata);
940 } else if (status == 0) {
941 lb_invoke_event_handler(handler, LB_EVENT_GROUP_CHANGED);
948 static struct packet *master_created(pid_t pid, int handle, const struct packet *packet)
950 struct livebox *handler;
961 const char *category;
962 const char *lb_fname;
963 const char *pd_fname;
967 const char *auto_launch;
972 enum lb_type lb_type;
973 enum pd_type pd_type;
977 int old_state = DESTROYED;
981 ret = packet_get(packet, "dsssiiiisssssdiiiiidsi",
983 &pkgname, &id, &content,
984 &lb_w, &lb_h, &pd_w, &pd_h,
985 &cluster, &category, &lb_fname, &pd_fname,
986 &auto_launch, &priority, &size_list, &user, &pinup_supported,
987 &lb_type, &pd_type, &period, &title, &is_pinned_up);
989 ErrPrint("Invalid argument\n");
990 ret = LB_STATUS_ERROR_INVALID;
994 ErrPrint("[%lf] pkgname: %s, id: %s, content: %s, "
995 "pd_w: %d, pd_h: %d, lb_w: %d, lb_h: %d, "
996 "cluster: %s, category: %s, lb_fname: \"%s\", pd_fname: \"%s\", "
997 "auto_launch: %s, priority: %lf, size_list: %d, user: %d, pinup: %d, "
998 "lb_type: %d, pd_type: %d, period: %lf, title: [%s], is_pinned_up: %d\n",
999 timestamp, pkgname, id, content,
1000 pd_w, pd_h, lb_w, lb_h,
1001 cluster, category, lb_fname, pd_fname,
1002 auto_launch, priority, size_list, user, pinup_supported,
1003 lb_type, pd_type, period, title, is_pinned_up);
1005 handler = lb_find_livebox_by_timestamp(timestamp);
1007 handler = lb_new_livebox(pkgname, id, timestamp);
1009 ErrPrint("Failed to create a new livebox\n");
1010 ret = LB_STATUS_ERROR_FAULT;
1014 old_state = handler->state;
1016 if (handler->state != CREATE) {
1017 if (handler->state != DELETE) {
1020 * This is not possible!!!
1022 ErrPrint("Invalid handler\n");
1023 ret = LB_STATUS_ERROR_INVALID;
1029 * After get the delete states,
1030 * call the create callback with deleted result.
1034 old_state = handler->state;
1037 ErrPrint("Already created: timestamp[%lf] "
1038 "pkgname[%s], id[%s] content[%s] "
1039 "cluster[%s] category[%s] lb_fname[%s] pd_fname[%s]\n",
1040 timestamp, pkgname, id,
1041 content, cluster, category,
1042 lb_fname, pd_fname);
1044 ret = LB_STATUS_ERROR_ALREADY;
1048 lb_set_id(handler, id);
1051 lb_set_size(handler, lb_w, lb_h);
1052 handler->lb.type = lb_type;
1053 handler->is_pinned_up = is_pinned_up;
1058 case _LB_TYPE_SCRIPT:
1059 case _LB_TYPE_BUFFER:
1060 if (!strlen(lb_fname))
1062 lb_set_lb_fb(handler, lb_fname);
1063 ret = fb_sync(lb_get_lb_fb(handler));
1065 ErrPrint("Failed to do sync FB (%s - %s)\n", pkgname, util_basename(util_uri_to_path(id)));
1068 lb_set_text_lb(handler);
1074 handler->pd.type = pd_type;
1075 lb_set_pdsize(handler, pd_w, pd_h);
1076 lb_set_default_pdsize(handler, pd_w, pd_h);
1078 case _PD_TYPE_SCRIPT:
1079 case _PD_TYPE_BUFFER:
1080 if (!strlen(pd_fname))
1083 lb_set_pd_fb(handler, pd_fname);
1084 ret = fb_sync(lb_get_pd_fb(handler));
1086 ErrPrint("Failed to do sync FB (%s - %s)\n", pkgname, util_basename(util_uri_to_path(id)));
1089 lb_set_text_pd(handler);
1095 lb_set_priority(handler, priority);
1097 lb_set_size_list(handler, size_list);
1098 lb_set_group(handler, cluster, category);
1100 lb_set_content(handler, content);
1101 lb_set_title(handler, title);
1103 lb_set_user(handler, user);
1105 lb_set_auto_launch(handler, auto_launch);
1106 lb_set_pinup(handler, pinup_supported);
1108 lb_set_period(handler, period);
1112 if (handler->state == CREATE) {
1115 * These callback can change the handler->state.
1116 * So we have to use the "old_state" which stored state before call these callbacks
1119 if (handler->created_cb) {
1123 cb = handler->created_cb;
1124 cbdata = handler->created_cbdata;
1126 handler->created_cb = NULL;
1127 handler->created_cbdata = NULL;
1129 cb(handler, ret, cbdata);
1131 lb_invoke_event_handler(handler, LB_EVENT_CREATED);
1136 if (ret == 0 && old_state == DELETE) {
1137 lb_send_delete(handler, handler->created_cb, handler->created_cbdata);
1141 * handler->created_cb = NULL;
1142 * handler->created_cbdata = NULL;
1144 * Do not clear this to use this from the deleted event callback.
1145 * if this value is not cleared when the deleted event callback check it,
1146 * it means that the created function is not called yet.
1147 * Then the call the deleted event callback with LB_STATUS_ERROR_CANCEL errno.
1154 static struct method s_table[] = {
1156 .cmd = "lb_updated", /* pkgname, id, lb_w, lb_h, priority, ret */
1157 .handler = master_lb_updated,
1160 .cmd = "pd_updated", /* pkgname, id, descfile, pd_w, pd_h, ret */
1161 .handler = master_pd_updated,
1164 .cmd = "pd_created",
1165 .handler = master_pd_created,
1168 .cmd = "pd_destroyed",
1169 .handler = master_pd_destroyed,
1172 .cmd = "fault_package", /* pkgname, id, function, ret */
1173 .handler = master_fault_package,
1176 .cmd = "deleted", /* pkgname, id, timestamp, ret */
1177 .handler = master_deleted,
1180 .cmd = "created", /* timestamp, pkgname, id, content, lb_w, lb_h, pd_w, pd_h, cluster, category, lb_file, pd_file, auto_launch, priority, size_list, is_user, pinup_supported, text_lb, text_pd, period, ret */
1181 .handler = master_created,
1184 .cmd = "group_changed",
1185 .handler = master_group_changed,
1188 .cmd = "period_changed",
1189 .handler = master_period_changed,
1192 .cmd = "size_changed",
1193 .handler = master_size_changed,
1197 .handler = master_pinup,
1201 .handler = master_hold_scroll,
1205 .cmd = "update_mode",
1206 .handler = master_update_mode,
1210 .cmd = "lb_update_begin",
1211 .handler = master_lb_update_begin,
1214 .cmd = "lb_update_end",
1215 .handler = master_lb_update_end,
1219 .cmd = "pd_update_begin",
1220 .handler = master_pd_update_begin,
1223 .cmd = "pd_update_end",
1224 .handler = master_pd_update_end,
1228 .cmd = "access_status",
1229 .handler = master_access_status,
1238 static void acquire_cb(struct livebox *handler, const struct packet *result, void *data)
1241 DbgPrint("Result packet is not valid\n");
1245 if (packet_get(result, "i", &ret) != 1)
1246 ErrPrint("Invalid argument\n");
1248 DbgPrint("Acquire returns: %d\n", ret);
1254 static inline int make_connection(void)
1256 struct packet *packet;
1259 DbgPrint("Let's making connection!\n");
1261 s_info.fd = com_core_packet_client_init(client_addr(), 0, s_table);
1262 if (s_info.fd < 0) {
1263 ErrPrint("Try this again later\n");
1264 return LB_STATUS_ERROR_IO;
1267 packet = packet_create("acquire", "d", util_timestamp());
1269 com_core_packet_client_fini(s_info.fd);
1271 return LB_STATUS_ERROR_FAULT;
1274 ret = master_rpc_async_request(NULL, packet, 1, acquire_cb, NULL);
1276 ErrPrint("Master RPC returns %d\n", ret);
1277 com_core_packet_client_fini(s_info.fd);
1279 return LB_STATUS_ERROR_IO;
1282 return LB_STATUS_SUCCESS;
1285 static int connected_cb(int handle, void *data)
1287 master_rpc_check_and_fire_consumer();
1291 static void master_started_cb(keynode_t *node, void *data)
1295 if (vconf_get_bool(VCONFKEY_MASTER_STARTED, &state) < 0)
1296 ErrPrint("Unable to get [%s]\n", VCONFKEY_MASTER_STARTED);
1298 DbgPrint("Master state: %d\n", state);
1299 if (state == 1 && make_connection() == LB_STATUS_SUCCESS) {
1301 ret = vconf_ignore_key_changed(VCONFKEY_MASTER_STARTED, master_started_cb);
1302 DbgPrint("master_started vconf key de-registered [%d]\n", ret);
1306 static gboolean timeout_cb(gpointer data)
1308 if (vconf_notify_key_changed(VCONFKEY_MASTER_STARTED, master_started_cb, NULL) < 0)
1309 ErrPrint("Failed to add vconf for monitoring service state\n");
1311 DbgPrint("vconf event callback is registered\n");
1313 master_started_cb(NULL, NULL);
1315 s_info.timer_id = 0;
1319 static int disconnected_cb(int handle, void *data)
1321 if (s_info.fd != handle) {
1322 /*!< This handle is not my favor */
1326 s_info.fd = -1; /*!< Disconnected */
1328 master_rpc_clear_all_request();
1329 lb_invoke_fault_handler(LB_FAULT_PROVIDER_DISCONNECTED, MASTER_PKGNAME, "default", "disconnected");
1333 /* Try to reconnect after 1 sec later */
1334 if (!s_info.timer_id) {
1335 DbgPrint("Reconnecting timer is added\n");
1336 s_info.timer_id = g_timeout_add(1000, timeout_cb, NULL);
1337 if (s_info.timer_id == 0) {
1338 ErrPrint("Unable to add reconnecting timer\n");
1342 ErrPrint("Reconnecting timer is already exists\n");
1348 int client_init(void)
1350 s_info.client_addr = vconf_get_str(VCONFKEY_MASTER_CLIENT_ADDR);
1351 if (!s_info.client_addr) {
1352 s_info.client_addr = strdup(CLIENT_SOCKET);
1353 if (!s_info.client_addr) {
1354 ErrPrint("Heap: %s\n", strerror(errno));
1359 DbgPrint("Server Address: %s\n", s_info.client_addr);
1361 com_core_add_event_callback(CONNECTOR_DISCONNECTED, disconnected_cb, NULL);
1362 com_core_add_event_callback(CONNECTOR_CONNECTED, connected_cb, NULL);
1363 if (vconf_notify_key_changed(VCONFKEY_MASTER_STARTED, master_started_cb, NULL) < 0)
1364 ErrPrint("Failed to add vconf for service state\n");
1366 DbgPrint("vconf event callback is registered\n");
1368 master_started_cb(NULL, NULL);
1377 const char *client_addr(void)
1379 return s_info.client_addr;
1382 int client_fini(void)
1385 ret = vconf_ignore_key_changed(VCONFKEY_MASTER_STARTED, master_started_cb);
1387 DbgPrint("Ignore vconf key: %d\n", ret);
1388 com_core_del_event_callback(CONNECTOR_DISCONNECTED, disconnected_cb, NULL);
1389 com_core_del_event_callback(CONNECTOR_CONNECTED, connected_cb, NULL);
1390 com_core_packet_client_fini(s_info.fd);
1392 return LB_STATUS_SUCCESS;