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"
53 static struct packet *master_fault_package(pid_t pid, int handle, const struct packet *packet)
59 if (packet_get(packet, "sss", &pkgname, &id, &function) != 3) {
60 ErrPrint("Invalid arguments\n");
64 master_rpc_clear_fault_package(pkgname);
65 lb_invoke_fault_handler(LB_FAULT_DEACTIVATED, pkgname, id, function);
69 static struct packet *master_hold_scroll(pid_t pid, int handle, const struct packet *packet)
71 struct livebox *handler;
77 ret = packet_get(packet, "ssi", &pkgname, &id, &seize);
79 ErrPrint("Invalid argument\n");
83 handler = lb_find_livebox(pkgname, id);
85 ErrPrint("Instance(%s) is not exists\n", id);
89 DbgPrint("HOLD: %s %d\n", id, seize);
90 lb_invoke_event_handler(handler, seize ? LB_EVENT_HOLD_SCROLL : LB_EVENT_RELEASE_SCROLL);
96 static struct packet *master_pinup(pid_t pid, int handle, const struct packet *packet)
101 struct livebox *handler;
107 ret = packet_get(packet, "iisss", &status, &pinup, &pkgname, &id, &content);
109 ErrPrint("Invalid argument\n");
113 handler = lb_find_livebox(pkgname, id);
115 ErrPrint("Instance (%s) is not exists\n", id);
120 new_content = strdup(content);
122 free(handler->content);
123 handler->content = new_content;
124 handler->is_pinned_up = pinup;
126 ErrPrint("Heap: %s\n", strerror(errno));
127 status = LB_STATUS_ERROR_MEMORY;
131 if (handler->pinup_cb) {
135 /* Make sure that user can call pinup API in its result callback */
136 cb = handler->pinup_cb;
137 cbdata = handler->pinup_cbdata;
139 handler->pinup_cb = NULL;
140 handler->pinup_cbdata = NULL;
142 cb(handler, status, cbdata);
143 } else if (status == 0) {
144 lb_invoke_event_handler(handler, LB_EVENT_PINUP_CHANGED);
151 static struct packet *master_deleted(pid_t pid, int handle, const struct packet *packet)
156 struct livebox *handler;
158 if (packet_get(packet, "ssd", &pkgname, &id, ×tamp) != 3) {
159 ErrPrint("Invalid arguemnt\n");
163 handler = lb_find_livebox_by_timestamp(timestamp);
167 * This can be happens only if the user delete a livebox
168 * right after create it before receive created event.
173 /*!< Check validity of this "handler" */
174 if (handler->state != CREATE) {
175 if (handler->state != DELETE) {
178 * This is not possible
180 CRITICAL_LOG("Already deleted handler (%s - %s)\n", pkgname, id);
185 if (handler->created_cb) {
191 * "if (handler->id == NULL)"
193 * The instance is not created yet.
194 * But the master forcely destroy it and send destroyed event to this
195 * without the created event.
197 * It could be destroyed when a slave has critical error(fault)
198 * before creating an instance successfully.
200 if (handler->created_cb == handler->deleted_cb) {
201 if (handler->created_cbdata != handler->deleted_cbdata)
202 DbgPrint("cb is same but cbdata is different (%s - %s)\n", pkgname, id);
204 handler->deleted_cb = NULL;
205 handler->deleted_cbdata = NULL;
208 cb = handler->created_cb;
209 cbdata = handler->created_cbdata;
211 handler->created_cb = NULL;
212 handler->created_cbdata = NULL;
214 cb(handler, LB_STATUS_ERROR_CANCEL, cbdata);
215 } else if (handler->id) {
216 if (handler->deleted_cb) {
220 cb = handler->deleted_cb;
221 cbdata = handler->deleted_cbdata;
223 handler->deleted_cb = NULL;
224 handler->deleted_cbdata = NULL;
226 cb(handler, LB_STATUS_SUCCESS, cbdata);
228 lb_invoke_event_handler(handler, LB_EVENT_DELETED);
232 /* Just try to delete it, if a user didn't remove it from the live box list */
239 static struct packet *master_lb_update_begin(pid_t pid, int handle, const struct packet *packet)
241 struct livebox *handler;
250 ret = packet_get(packet, "ssdsss", &pkgname, &id, &priority, &content, &title, &fbfile);
252 ErrPrint("Invalid argument\n");
256 handler = lb_find_livebox(pkgname, id);
258 ErrPrint("Instance[%s] is not exists\n", id);
262 if (handler->state != CREATE) {
263 ErrPrint("(%s) is not created\n", id);
267 lb_set_priority(handler, priority);
268 lb_set_content(handler, content);
269 lb_set_title(handler, title);
273 * Width & Height is not changed in this case.
274 * If the active update is began, the size should not be changed,
275 * And if the size is changed, the provider should finish the updating first.
276 * And then begin updating again after change its size.
278 if (lb_get_lb_fb(handler)) {
279 lb_set_lb_fb(handler, fbfile);
281 ret = fb_sync(lb_get_lb_fb(handler));
282 if (ret != LB_STATUS_SUCCESS)
283 ErrPrint("Failed to do sync FB (%s - %s) (%d)\n", pkgname, fbfile, ret);
285 lb_invoke_event_handler(handler, LB_EVENT_LB_UPDATE_BEGIN);
287 ErrPrint("Invalid request[%s], %s\n", id, fbfile);
294 static struct packet *master_pd_update_begin(pid_t pid, int handle, const struct packet *packet)
296 struct livebox *handler;
302 ret = packet_get(packet, "sss", &pkgname, &id, &fbfile);
304 ErrPrint("Invalid argument\n");
308 handler = lb_find_livebox(pkgname, id);
310 ErrPrint("Instance[%s] is not exists\n", id);
314 if (handler->state != CREATE) {
315 ErrPrint("[%s] is not created\n", id);
319 if (lb_get_pd_fb(handler)) {
320 lb_set_lb_fb(handler, fbfile);
322 ret = fb_sync(lb_get_lb_fb(handler));
323 if (ret != LB_STATUS_SUCCESS)
324 ErrPrint("Failed to do sync FB (%s - %s) (%d)\n", pkgname, fbfile, ret);
326 lb_invoke_event_handler(handler, LB_EVENT_PD_UPDATE_BEGIN);
328 ErrPrint("Invalid request[%s], %s\n", id, fbfile);
335 static struct packet *master_lb_update_end(pid_t pid, int handle, const struct packet *packet)
337 struct livebox *handler;
342 ret = packet_get(packet, "ss", &pkgname, &id);
344 ErrPrint("Invalid argument\n");
348 handler = lb_find_livebox(pkgname, id);
350 ErrPrint("Instance[%s] is not exists\n", id);
354 if (handler->state != CREATE) {
355 ErrPrint("[%s] is not created\n", id);
359 if (lb_get_lb_fb(handler)) {
360 lb_invoke_event_handler(handler, LB_EVENT_LB_UPDATE_END);
362 ErrPrint("Invalid request[%s]\n", id);
369 static struct packet *master_access_status(pid_t pid, int handle, const struct packet *packet)
371 struct livebox *handler;
377 ret = packet_get(packet, "ssi", &pkgname, &id, &status);
379 ErrPrint("Invalid argument\n");
383 handler = lb_find_livebox(pkgname, id);
385 ErrPrint("Instance[%s] is not exists\n", id);
389 if (handler->state != CREATE) {
390 ErrPrint("[%s] is not created\n", id);
394 if (handler->access_event_cb) {
398 cb = handler->access_event_cb;
399 cbdata = handler->access_event_cbdata;
401 handler->access_event_cb = NULL;
402 handler->access_event_cbdata = NULL;
404 cb(handler, status, cbdata);
406 ErrPrint("Invalid event[%s]\n", id);
412 static struct packet *master_pd_update_end(pid_t pid, int handle, const struct packet *packet)
414 struct livebox *handler;
419 ret = packet_get(packet, "ss", &pkgname, &id);
421 ErrPrint("Invalid argument\n");
425 handler = lb_find_livebox(pkgname, id);
427 ErrPrint("Instance[%s] is not exists\n", id);
431 if (handler->state != CREATE) {
432 ErrPrint("[%s] is not created\n", id);
436 if (lb_get_lb_fb(handler)) {
437 lb_invoke_event_handler(handler, LB_EVENT_PD_UPDATE_END);
439 ErrPrint("Invalid request[%s]", id);
446 static struct packet *master_lb_updated(pid_t pid, int handle, const struct packet *packet)
453 struct livebox *handler;
459 ret = packet_get(packet, "sssiidss",
461 &fbfile, &lb_w, &lb_h,
462 &priority, &content, &title);
464 ErrPrint("Invalid argument\n");
468 handler = lb_find_livebox(pkgname, id);
470 ErrPrint("instance(%s) is not exists\n", id);
474 if (handler->state != CREATE) {
477 * Already deleted by the user.
478 * Don't try to notice anything with this, Just ignore all events
479 * Beacuse the user doesn't wants know about this anymore
481 ErrPrint("(%s) is not exists, but updated\n", id);
485 lb_set_priority(handler, priority);
486 lb_set_content(handler, content);
487 lb_set_title(handler, title);
488 lb_set_size(handler, lb_w, lb_h);
490 if (lb_text_lb(handler)) {
491 (void)parse_desc(handler, livebox_filename(handler), 0);
494 * DESC parser will call the "text event callback".
495 * Don't need to call global event callback in this case.
498 } else if (lb_get_lb_fb(handler)) {
499 lb_set_lb_fb(handler, fbfile);
500 ret = fb_sync(lb_get_lb_fb(handler));
501 if (ret != LB_STATUS_SUCCESS)
502 ErrPrint("Failed to do sync FB (%s - %s) (%d)\n", pkgname, util_basename(util_uri_to_path(id)), ret);
504 ret = LB_STATUS_SUCCESS;
507 if (ret == LB_STATUS_SUCCESS)
508 lb_invoke_event_handler(handler, LB_EVENT_LB_UPDATED);
514 static struct packet *master_pd_created(pid_t pid, int handle, const struct packet *packet)
516 struct livebox *handler;
525 ret = packet_get(packet, "sssiii", &pkgname, &id, &buf_id, &width, &height, &status);
527 ErrPrint("Invalid argument\n");
531 handler = lb_find_livebox(pkgname, id);
533 ErrPrint("Instance(%s) is not exists\n", id);
537 if (handler->state != CREATE) {
538 ErrPrint("Instance(%s) is not created\n", id);
542 lb_set_pdsize(handler, width, height);
543 if (lb_text_pd(handler)) {
544 DbgPrint("Text TYPE does not need to handle this\n");
546 (void)lb_set_pd_fb(handler, buf_id);
547 ret = fb_sync(lb_get_pd_fb(handler));
549 ErrPrint("Failed to do sync FB (%s - %s)\n", pkgname, util_basename(util_uri_to_path(id)));
552 handler->is_pd_created = (status == 0);
554 if (handler->pd_created_cb) {
558 cb = handler->pd_created_cb;
559 cbdata = handler->pd_created_cbdata;
561 handler->pd_created_cb = NULL;
562 handler->pd_created_cbdata = NULL;
565 * Before call the Callback function,
566 * pd_create_cb must be reset.
567 * Because, in the create callback, user can call create_pd function again.
569 DbgPrint("PERF_DBOX\n");
570 cb(handler, status, cbdata);
571 } else if (handler->is_pd_created) {
572 lb_invoke_event_handler(handler, LB_EVENT_PD_CREATED);
579 static struct packet *master_pd_destroyed(pid_t pid, int handle, const struct packet *packet)
581 struct livebox *handler;
587 ret = packet_get(packet, "ssi", &pkgname, &id, &status);
589 ErrPrint("Invalid argument\n");
593 handler = lb_find_livebox(pkgname, id);
595 ErrPrint("Instance(%s) is not exists\n", id);
599 if (handler->state != CREATE) {
600 ErrPrint("Instance(%s) is not created\n", id);
604 handler->is_pd_created = 0;
606 if (handler->pd_destroyed_cb) {
610 cb = handler->pd_destroyed_cb;
611 cbdata = handler->pd_destroyed_cbdata;
613 handler->pd_destroyed_cb = NULL;
614 handler->pd_destroyed_cbdata = NULL;
617 * Before call the Callback function,
618 * pd_destroyed_cb must be reset.
619 * Because, in the create callback, user can call destroy_pd function again.
621 cb(handler, status, cbdata);
622 } else if (status == 0) {
623 lb_invoke_event_handler(handler, LB_EVENT_PD_DESTROYED);
630 static struct packet *master_pd_updated(pid_t pid, int handle, const struct packet *packet)
634 const char *descfile;
637 struct livebox *handler;
641 ret = packet_get(packet, "ssssii",
646 ErrPrint("Invalid argument\n");
650 handler = lb_find_livebox(pkgname, id);
652 ErrPrint("Instance(%s) is not exists\n", id);
656 if (handler->state != CREATE) {
659 * This handler is already deleted by the user.
660 * So don't try to notice anything about this anymore.
661 * Just ignore all events.
663 ErrPrint("Instance(%s) is not created\n", id);
667 lb_set_pdsize(handler, pd_w, pd_h);
669 if (lb_text_pd(handler)) {
670 (void)parse_desc(handler, descfile, 1);
672 (void)lb_set_pd_fb(handler, fbfile);
674 ret = fb_sync(lb_get_pd_fb(handler));
676 ErrPrint("Failed to do sync FB (%s - %s), %d\n", pkgname, util_basename(util_uri_to_path(id)), ret);
678 lb_invoke_event_handler(handler, LB_EVENT_PD_UPDATED);
685 static struct packet *master_update_mode(pid_t pid, int handle, const struct packet *packet)
687 struct livebox *handler;
695 ErrPrint("Invalid packet\n");
699 ret = packet_get(packet, "ssii", &pkgname, &id, &status, &active_mode);
701 ErrPrint("Invalid argument\n");
705 handler = lb_find_livebox(pkgname, id);
707 ErrPrint("Livebox(%s) is not found\n", id);
711 if (handler->state != CREATE) {
712 ErrPrint("Livebox(%s) is not created yet\n", id);
716 if (status == LB_STATUS_SUCCESS)
717 lb_set_update_mode(handler, active_mode);
719 if (handler->update_mode_cb) {
723 cb = handler->update_mode_cb;
724 cbdata = handler->update_mode_cbdata;
726 handler->update_mode_cb = NULL;
727 handler->update_mode_cbdata = NULL;
729 cb(handler, status, cbdata);
731 lb_invoke_event_handler(handler, LB_EVENT_UPDATE_MODE_CHANGED);
738 static struct packet *master_size_changed(pid_t pid, int handle, const struct packet *packet)
740 struct livebox *handler;
751 ErrPrint("Invalid packet\n");
755 ret = packet_get(packet, "sssiiii", &pkgname, &id, &fbfile, &is_pd, &w, &h, &status);
757 ErrPrint("Invalid argument\n");
761 handler = lb_find_livebox(pkgname, id);
763 ErrPrint("Livebox(%s) is not found\n", id);
767 if (handler->state != CREATE) {
768 ErrPrint("Livebox(%s) is not created yet\n", id);
775 * PD is not able to resized by the client.
776 * PD is only can be managed by the provider.
777 * So the PD has no private resized event handler.
778 * Notify it via global event handler only.
781 lb_set_pdsize(handler, w, h);
782 lb_invoke_event_handler(handler, LB_EVENT_PD_SIZE_CHANGED);
784 ErrPrint("This is not possible. PD Size is changed but the return value is not ZERO (%d)\n", status);
788 lb_set_size(handler, w, h);
792 * If there is a created LB FB,
795 if (lb_get_lb_fb(handler)) {
796 lb_set_lb_fb(handler, fbfile);
798 ret = fb_sync(lb_get_lb_fb(handler));
800 ErrPrint("Failed to do sync FB (%s - %s)\n", pkgname, util_basename(util_uri_to_path(id)));
802 /* Just update the size info only. */
807 * I cannot believe client.
808 * So I added some log before & after call the user callback.
810 if (handler->size_changed_cb) {
814 cb = handler->size_changed_cb;
815 cbdata = handler->size_cbdata;
817 handler->size_changed_cb = NULL;
818 handler->size_cbdata = NULL;
820 cb(handler, status, cbdata);
822 lb_invoke_event_handler(handler, LB_EVENT_LB_SIZE_CHANGED);
825 if (handler->size_changed_cb) {
829 cb = handler->size_changed_cb;
830 cbdata = handler->size_cbdata;
832 handler->size_changed_cb = NULL;
833 handler->size_cbdata = NULL;
835 cb(handler, status, cbdata);
844 static struct packet *master_period_changed(pid_t pid, int handle, const struct packet *packet)
846 struct livebox *handler;
853 ret = packet_get(packet, "idss", &status, &period, &pkgname, &id);
855 ErrPrint("Invalid argument\n");
859 handler = lb_find_livebox(pkgname, id);
861 ErrPrint("Livebox(%s) is not found\n", id);
865 if (handler->state != CREATE) {
866 ErrPrint("Livebox(%s) is not created\n", id);
871 lb_set_period(handler, period);
873 if (handler->period_changed_cb) {
877 cb = handler->period_changed_cb;
878 cbdata = handler->period_cbdata;
880 handler->period_changed_cb = NULL;
881 handler->period_cbdata = NULL;
883 cb(handler, status, cbdata);
884 } else if (status == 0) {
885 lb_invoke_event_handler(handler, LB_EVENT_PERIOD_CHANGED);
892 static struct packet *master_group_changed(pid_t pid, int handle, const struct packet *packet)
894 struct livebox *handler;
899 const char *category;
902 ret = packet_get(packet, "ssiss", &pkgname, &id, &status, &cluster, &category);
904 ErrPrint("Invalid argument\n");
908 handler = lb_find_livebox(pkgname, id);
910 ErrPrint("Livebox(%s) is not exists\n", id);
914 if (handler->state != CREATE) {
917 * Do no access this handler,
918 * You cannot believe this handler anymore.
920 ErrPrint("Livebox(%s) is not created\n", id);
925 (void)lb_set_group(handler, cluster, category);
927 if (handler->group_changed_cb) {
931 cb = handler->group_changed_cb;
932 cbdata = handler->group_cbdata;
934 handler->group_changed_cb = NULL;
935 handler->group_cbdata = NULL;
937 cb(handler, status, cbdata);
938 } else if (status == 0) {
939 lb_invoke_event_handler(handler, LB_EVENT_GROUP_CHANGED);
946 static struct packet *master_created(pid_t pid, int handle, const struct packet *packet)
948 struct livebox *handler;
959 const char *category;
960 const char *lb_fname;
961 const char *pd_fname;
965 const char *auto_launch;
970 enum lb_type lb_type;
971 enum pd_type pd_type;
975 int old_state = DESTROYED;
979 ret = packet_get(packet, "dsssiiiisssssdiiiiidsi",
981 &pkgname, &id, &content,
982 &lb_w, &lb_h, &pd_w, &pd_h,
983 &cluster, &category, &lb_fname, &pd_fname,
984 &auto_launch, &priority, &size_list, &user, &pinup_supported,
985 &lb_type, &pd_type, &period, &title, &is_pinned_up);
987 ErrPrint("Invalid argument\n");
988 ret = LB_STATUS_ERROR_INVALID;
992 ErrPrint("[%lf] pkgname: %s, id: %s, content: %s, "
993 "pd_w: %d, pd_h: %d, lb_w: %d, lb_h: %d, "
994 "cluster: %s, category: %s, lb_fname: \"%s\", pd_fname: \"%s\", "
995 "auto_launch: %s, priority: %lf, size_list: %d, user: %d, pinup: %d, "
996 "lb_type: %d, pd_type: %d, period: %lf, title: [%s], is_pinned_up: %d\n",
997 timestamp, pkgname, id, content,
998 pd_w, pd_h, lb_w, lb_h,
999 cluster, category, lb_fname, pd_fname,
1000 auto_launch, priority, size_list, user, pinup_supported,
1001 lb_type, pd_type, period, title, is_pinned_up);
1003 handler = lb_find_livebox_by_timestamp(timestamp);
1005 handler = lb_new_livebox(pkgname, id, timestamp);
1007 ErrPrint("Failed to create a new livebox\n");
1008 ret = LB_STATUS_ERROR_FAULT;
1012 old_state = handler->state;
1014 if (handler->state != CREATE) {
1015 if (handler->state != DELETE) {
1018 * This is not possible!!!
1020 ErrPrint("Invalid handler\n");
1021 ret = LB_STATUS_ERROR_INVALID;
1027 * After get the delete states,
1028 * call the create callback with deleted result.
1032 old_state = handler->state;
1035 ErrPrint("Already created: timestamp[%lf] "
1036 "pkgname[%s], id[%s] content[%s] "
1037 "cluster[%s] category[%s] lb_fname[%s] pd_fname[%s]\n",
1038 timestamp, pkgname, id,
1039 content, cluster, category,
1040 lb_fname, pd_fname);
1042 ret = LB_STATUS_ERROR_ALREADY;
1046 lb_set_id(handler, id);
1049 lb_set_size(handler, lb_w, lb_h);
1050 handler->lb.type = lb_type;
1051 handler->is_pinned_up = is_pinned_up;
1056 case _LB_TYPE_SCRIPT:
1057 case _LB_TYPE_BUFFER:
1058 if (!strlen(lb_fname))
1060 lb_set_lb_fb(handler, lb_fname);
1061 ret = fb_sync(lb_get_lb_fb(handler));
1063 ErrPrint("Failed to do sync FB (%s - %s)\n", pkgname, util_basename(util_uri_to_path(id)));
1066 lb_set_text_lb(handler);
1072 handler->pd.type = pd_type;
1073 lb_set_pdsize(handler, pd_w, pd_h);
1074 lb_set_default_pdsize(handler, pd_w, pd_h);
1076 case _PD_TYPE_SCRIPT:
1077 case _PD_TYPE_BUFFER:
1078 if (!strlen(pd_fname))
1081 lb_set_pd_fb(handler, pd_fname);
1082 ret = fb_sync(lb_get_pd_fb(handler));
1084 ErrPrint("Failed to do sync FB (%s - %s)\n", pkgname, util_basename(util_uri_to_path(id)));
1087 lb_set_text_pd(handler);
1093 lb_set_priority(handler, priority);
1095 lb_set_size_list(handler, size_list);
1096 lb_set_group(handler, cluster, category);
1098 lb_set_content(handler, content);
1099 lb_set_title(handler, title);
1101 lb_set_user(handler, user);
1103 lb_set_auto_launch(handler, auto_launch);
1104 lb_set_pinup(handler, pinup_supported);
1106 lb_set_period(handler, period);
1110 if (handler->state == CREATE) {
1113 * These callback can change the handler->state.
1114 * So we have to use the "old_state" which stored state before call these callbacks
1117 if (handler->created_cb) {
1121 cb = handler->created_cb;
1122 cbdata = handler->created_cbdata;
1124 handler->created_cb = NULL;
1125 handler->created_cbdata = NULL;
1127 cb(handler, ret, cbdata);
1129 lb_invoke_event_handler(handler, LB_EVENT_CREATED);
1134 if (ret == 0 && old_state == DELETE) {
1135 lb_send_delete(handler, handler->created_cb, handler->created_cbdata);
1139 * handler->created_cb = NULL;
1140 * handler->created_cbdata = NULL;
1142 * Do not clear this to use this from the deleted event callback.
1143 * if this value is not cleared when the deleted event callback check it,
1144 * it means that the created function is not called yet.
1145 * Then the call the deleted event callback with LB_STATUS_ERROR_CANCEL errno.
1152 static struct method s_table[] = {
1154 .cmd = "lb_updated", /* pkgname, id, lb_w, lb_h, priority, ret */
1155 .handler = master_lb_updated,
1158 .cmd = "pd_updated", /* pkgname, id, descfile, pd_w, pd_h, ret */
1159 .handler = master_pd_updated,
1162 .cmd = "pd_created",
1163 .handler = master_pd_created,
1166 .cmd = "pd_destroyed",
1167 .handler = master_pd_destroyed,
1170 .cmd = "fault_package", /* pkgname, id, function, ret */
1171 .handler = master_fault_package,
1174 .cmd = "deleted", /* pkgname, id, timestamp, ret */
1175 .handler = master_deleted,
1178 .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 */
1179 .handler = master_created,
1182 .cmd = "group_changed",
1183 .handler = master_group_changed,
1186 .cmd = "period_changed",
1187 .handler = master_period_changed,
1190 .cmd = "size_changed",
1191 .handler = master_size_changed,
1195 .handler = master_pinup,
1199 .handler = master_hold_scroll,
1203 .cmd = "update_mode",
1204 .handler = master_update_mode,
1208 .cmd = "lb_update_begin",
1209 .handler = master_lb_update_begin,
1212 .cmd = "lb_update_end",
1213 .handler = master_lb_update_end,
1217 .cmd = "pd_update_begin",
1218 .handler = master_pd_update_begin,
1221 .cmd = "pd_update_end",
1222 .handler = master_pd_update_end,
1226 .cmd = "access_status",
1227 .handler = master_access_status,
1236 static void acquire_cb(struct livebox *handler, const struct packet *result, void *data)
1239 DbgPrint("Result packet is not valid\n");
1243 if (packet_get(result, "i", &ret) != 1)
1244 ErrPrint("Invalid argument\n");
1246 DbgPrint("Acquire returns: %d\n", ret);
1252 static inline int make_connection(void)
1254 struct packet *packet;
1257 DbgPrint("Let's making connection!\n");
1259 s_info.fd = com_core_packet_client_init(CLIENT_SOCKET, 0, s_table);
1260 if (s_info.fd < 0) {
1261 ErrPrint("Try this again later\n");
1262 return LB_STATUS_ERROR_IO;
1265 packet = packet_create("acquire", "d", util_timestamp());
1267 com_core_packet_client_fini(s_info.fd);
1269 return LB_STATUS_ERROR_FAULT;
1272 ret = master_rpc_async_request(NULL, packet, 1, acquire_cb, NULL);
1274 ErrPrint("Master RPC returns %d\n", ret);
1275 com_core_packet_client_fini(s_info.fd);
1277 return LB_STATUS_ERROR_IO;
1280 return LB_STATUS_SUCCESS;
1283 static int connected_cb(int handle, void *data)
1285 master_rpc_check_and_fire_consumer();
1289 static void master_started_cb(keynode_t *node, void *data)
1293 if (vconf_get_bool(VCONFKEY_MASTER_STARTED, &state) < 0)
1294 ErrPrint("Unable to get [%s]\n", VCONFKEY_MASTER_STARTED);
1296 DbgPrint("Master state: %d\n", state);
1297 if (state == 1 && make_connection() == LB_STATUS_SUCCESS) {
1299 ret = vconf_ignore_key_changed(VCONFKEY_MASTER_STARTED, master_started_cb);
1300 DbgPrint("master_started vconf key de-registered [%d]\n", ret);
1304 static gboolean timeout_cb(gpointer data)
1306 if (vconf_notify_key_changed(VCONFKEY_MASTER_STARTED, master_started_cb, NULL) < 0)
1307 ErrPrint("Failed to add vconf for monitoring service state\n");
1309 DbgPrint("vconf event callback is registered\n");
1311 master_started_cb(NULL, NULL);
1313 s_info.timer_id = 0;
1317 static int disconnected_cb(int handle, void *data)
1319 if (s_info.fd != handle) {
1320 /*!< This handle is not my favor */
1324 s_info.fd = -1; /*!< Disconnected */
1326 master_rpc_clear_all_request();
1327 lb_invoke_fault_handler(LB_FAULT_PROVIDER_DISCONNECTED, MASTER_PKGNAME, "default", "disconnected");
1331 /* Try to reconnect after 1 sec later */
1332 if (!s_info.timer_id) {
1333 DbgPrint("Reconnecting timer is added\n");
1334 s_info.timer_id = g_timeout_add(1000, timeout_cb, NULL);
1335 if (s_info.timer_id == 0) {
1336 ErrPrint("Unable to add reconnecting timer\n");
1340 ErrPrint("Reconnecting timer is already exists\n");
1346 int client_init(void)
1348 com_core_add_event_callback(CONNECTOR_DISCONNECTED, disconnected_cb, NULL);
1349 com_core_add_event_callback(CONNECTOR_CONNECTED, connected_cb, NULL);
1350 if (vconf_notify_key_changed(VCONFKEY_MASTER_STARTED, master_started_cb, NULL) < 0)
1351 ErrPrint("Failed to add vconf for service state\n");
1353 DbgPrint("vconf event callback is registered\n");
1355 master_started_cb(NULL, NULL);
1364 const char *client_addr(void)
1366 return CLIENT_SOCKET;
1369 int client_fini(void)
1372 ret = vconf_ignore_key_changed(VCONFKEY_MASTER_STARTED, master_started_cb);
1374 DbgPrint("Ignore vconf key: %d\n", ret);
1375 com_core_del_event_callback(CONNECTOR_DISCONNECTED, disconnected_cb, NULL);
1376 com_core_del_event_callback(CONNECTOR_CONNECTED, connected_cb, NULL);
1377 com_core_packet_client_fini(s_info.fd);
1379 return LB_STATUS_SUCCESS;