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.
28 #include <vconf-keys.h>
32 #include <com-core_packet.h>
33 #include <livebox-errno.h>
34 #include <secure_socket.h>
39 #include "livebox_internal.h"
40 #include "desc_parser.h"
43 #include "master_rpc.h"
45 #include "critical_log.h"
46 #include "file_service.h"
60 static struct packet *master_fault_package(pid_t pid, int handle, const struct packet *packet)
66 if (packet_get(packet, "sss", &pkgname, &id, &function) != 3) {
67 ErrPrint("Invalid arguments\n");
71 master_rpc_clear_fault_package(pkgname);
72 lb_invoke_fault_handler(LB_FAULT_DEACTIVATED, pkgname, id, function);
76 static struct packet *master_hold_scroll(pid_t pid, int handle, const struct packet *packet)
78 struct livebox *handler;
84 ret = packet_get(packet, "ssi", &pkgname, &id, &seize);
86 ErrPrint("Invalid argument\n");
90 handler = lb_find_livebox(pkgname, id);
92 ErrPrint("Instance(%s) is not exists\n", id);
96 DbgPrint("HOLD: %s %d\n", id, seize);
97 lb_invoke_event_handler(handler, seize ? LB_EVENT_HOLD_SCROLL : LB_EVENT_RELEASE_SCROLL);
103 static struct packet *master_pinup(pid_t pid, int handle, const struct packet *packet)
108 struct livebox *handler;
114 ret = packet_get(packet, "iisss", &status, &pinup, &pkgname, &id, &content);
116 ErrPrint("Invalid argument\n");
120 handler = lb_find_livebox(pkgname, id);
122 ErrPrint("Instance (%s) is not exists\n", id);
127 new_content = strdup(content);
129 free(handler->content);
130 handler->content = new_content;
131 handler->is_pinned_up = pinup;
133 ErrPrint("Heap: %s\n", strerror(errno));
134 status = LB_STATUS_ERROR_MEMORY;
138 if (handler->pinup_cb) {
142 /* Make sure that user can call pinup API in its result callback */
143 cb = handler->pinup_cb;
144 cbdata = handler->pinup_cbdata;
146 handler->pinup_cb = NULL;
147 handler->pinup_cbdata = NULL;
149 cb(handler, status, cbdata);
150 } else if (status == 0) {
151 lb_invoke_event_handler(handler, LB_EVENT_PINUP_CHANGED);
158 static struct packet *master_deleted(pid_t pid, int handle, const struct packet *packet)
163 struct livebox *handler;
165 if (packet_get(packet, "ssd", &pkgname, &id, ×tamp) != 3) {
166 ErrPrint("Invalid arguemnt\n");
170 handler = lb_find_livebox_by_timestamp(timestamp);
174 * This can be happens only if the user delete a livebox
175 * right after create it before receive created event.
180 /*!< Check validity of this "handler" */
181 if (handler->state != CREATE) {
182 if (handler->state != DELETE) {
185 * This is not possible
187 CRITICAL_LOG("Already deleted handler (%s - %s)\n", pkgname, id);
192 if (handler->created_cb) {
198 * "if (handler->id == NULL)"
200 * The instance is not created yet.
201 * But the master forcely destroy it and send destroyed event to this
202 * without the created event.
204 * It could be destroyed when a slave has critical error(fault)
205 * before creating an instance successfully.
207 if (handler->created_cb == handler->deleted_cb) {
208 if (handler->created_cbdata != handler->deleted_cbdata)
209 DbgPrint("cb is same but cbdata is different (%s - %s)\n", pkgname, id);
211 handler->deleted_cb = NULL;
212 handler->deleted_cbdata = NULL;
215 cb = handler->created_cb;
216 cbdata = handler->created_cbdata;
218 handler->created_cb = NULL;
219 handler->created_cbdata = NULL;
221 cb(handler, LB_STATUS_ERROR_CANCEL, cbdata);
222 } else if (handler->id) {
223 if (handler->deleted_cb) {
227 cb = handler->deleted_cb;
228 cbdata = handler->deleted_cbdata;
230 handler->deleted_cb = NULL;
231 handler->deleted_cbdata = NULL;
233 cb(handler, LB_STATUS_SUCCESS, cbdata);
235 lb_invoke_event_handler(handler, LB_EVENT_DELETED);
239 /* Just try to delete it, if a user didn't remove it from the live box list */
246 static struct packet *master_lb_update_begin(pid_t pid, int handle, const struct packet *packet)
248 struct livebox *handler;
257 ret = packet_get(packet, "ssdsss", &pkgname, &id, &priority, &content, &title, &fbfile);
259 ErrPrint("Invalid argument\n");
263 handler = lb_find_livebox(pkgname, id);
265 ErrPrint("Instance[%s] is not exists\n", id);
269 if (handler->state != CREATE) {
270 ErrPrint("(%s) is not created\n", id);
274 lb_set_priority(handler, priority);
275 lb_set_content(handler, content);
276 lb_set_title(handler, title);
280 * Width & Height is not changed in this case.
281 * If the active update is began, the size should not be changed,
282 * And if the size is changed, the provider should finish the updating first.
283 * And then begin updating again after change its size.
285 if (lb_get_lb_fb(handler)) {
286 lb_set_lb_fb(handler, fbfile);
288 ret = fb_sync(lb_get_lb_fb(handler));
289 if (ret != LB_STATUS_SUCCESS) {
290 ErrPrint("Failed to do sync FB (%s - %s) (%d)\n", pkgname, fbfile, ret);
292 lb_invoke_event_handler(handler, LB_EVENT_LB_UPDATE_BEGIN);
295 ErrPrint("Invalid request[%s], %s\n", id, fbfile);
302 static struct packet *master_pd_update_begin(pid_t pid, int handle, const struct packet *packet)
304 struct livebox *handler;
310 ret = packet_get(packet, "sss", &pkgname, &id, &fbfile);
312 ErrPrint("Invalid argument\n");
316 handler = lb_find_livebox(pkgname, id);
318 ErrPrint("Instance[%s] is not exists\n", id);
322 if (handler->state != CREATE) {
323 ErrPrint("[%s] is not created\n", id);
327 if (lb_get_pd_fb(handler)) {
328 lb_set_lb_fb(handler, fbfile);
330 ret = fb_sync(lb_get_lb_fb(handler));
331 if (ret != LB_STATUS_SUCCESS) {
332 ErrPrint("Failed to do sync FB (%s - %s) (%d)\n", pkgname, fbfile, ret);
334 lb_invoke_event_handler(handler, LB_EVENT_PD_UPDATE_BEGIN);
337 ErrPrint("Invalid request[%s], %s\n", id, fbfile);
344 static struct packet *master_lb_update_end(pid_t pid, int handle, const struct packet *packet)
346 struct livebox *handler;
351 ret = packet_get(packet, "ss", &pkgname, &id);
353 ErrPrint("Invalid argument\n");
357 handler = lb_find_livebox(pkgname, id);
359 ErrPrint("Instance[%s] is not exists\n", id);
363 if (handler->state != CREATE) {
364 ErrPrint("[%s] is not created\n", id);
368 if (lb_get_lb_fb(handler)) {
369 lb_invoke_event_handler(handler, LB_EVENT_LB_UPDATE_END);
371 ErrPrint("Invalid request[%s]\n", id);
378 static struct packet *master_access_status(pid_t pid, int handle, const struct packet *packet)
380 struct livebox *handler;
386 ret = packet_get(packet, "ssi", &pkgname, &id, &status);
388 ErrPrint("Invalid argument\n");
392 handler = lb_find_livebox(pkgname, id);
394 ErrPrint("Instance[%s] is not exists\n", id);
398 if (handler->state != CREATE) {
399 ErrPrint("[%s] is not created\n", id);
403 if (handler->access_event_cb) {
407 cb = handler->access_event_cb;
408 cbdata = handler->access_event_cbdata;
410 handler->access_event_cb = NULL;
411 handler->access_event_cbdata = NULL;
413 cb(handler, status, cbdata);
415 ErrPrint("Invalid event[%s]\n", id);
421 static struct packet *master_pd_update_end(pid_t pid, int handle, const struct packet *packet)
423 struct livebox *handler;
428 ret = packet_get(packet, "ss", &pkgname, &id);
430 ErrPrint("Invalid argument\n");
434 handler = lb_find_livebox(pkgname, id);
436 ErrPrint("Instance[%s] is not exists\n", id);
440 if (handler->state != CREATE) {
441 ErrPrint("[%s] is not created\n", id);
445 if (lb_get_lb_fb(handler)) {
446 lb_invoke_event_handler(handler, LB_EVENT_PD_UPDATE_END);
448 ErrPrint("Invalid request[%s]", id);
455 static struct packet *master_lb_updated(pid_t pid, int handle, const struct packet *packet)
462 struct livebox *handler;
468 ret = packet_get(packet, "sssiidss",
470 &fbfile, &lb_w, &lb_h,
471 &priority, &content, &title);
473 ErrPrint("Invalid argument\n");
477 handler = lb_find_livebox(pkgname, id);
479 ErrPrint("instance(%s) is not exists\n", id);
483 if (handler->state != CREATE) {
486 * Already deleted by the user.
487 * Don't try to notice anything with this, Just ignore all events
488 * Beacuse the user doesn't wants know about this anymore
490 ErrPrint("(%s) is not exists, but updated\n", id);
494 lb_set_priority(handler, priority);
495 lb_set_content(handler, content);
496 lb_set_title(handler, title);
497 lb_set_size(handler, lb_w, lb_h);
499 if (lb_text_lb(handler)) {
500 (void)parse_desc(handler, livebox_filename(handler), 0);
503 * DESC parser will call the "text event callback".
504 * Don't need to call global event callback in this case.
507 } else if (lb_get_lb_fb(handler)) {
508 lb_set_lb_fb(handler, fbfile);
509 ret = fb_sync(lb_get_lb_fb(handler));
510 if (ret != LB_STATUS_SUCCESS) {
511 ErrPrint("Failed to do sync FB (%s - %s) (%d)\n", pkgname, util_basename(util_uri_to_path(id)), ret);
514 ret = LB_STATUS_SUCCESS;
517 if (ret == LB_STATUS_SUCCESS) {
518 lb_invoke_event_handler(handler, LB_EVENT_LB_UPDATED);
525 static struct packet *master_pd_created(pid_t pid, int handle, const struct packet *packet)
527 struct livebox *handler;
536 ret = packet_get(packet, "sssiii", &pkgname, &id, &buf_id, &width, &height, &status);
538 ErrPrint("Invalid argument\n");
542 handler = lb_find_livebox(pkgname, id);
544 ErrPrint("Instance(%s) is not exists\n", id);
548 if (handler->state != CREATE) {
549 ErrPrint("Instance(%s) is not created\n", id);
553 lb_set_pdsize(handler, width, height);
554 if (lb_text_pd(handler)) {
555 DbgPrint("Text TYPE does not need to handle this\n");
557 (void)lb_set_pd_fb(handler, buf_id);
558 ret = fb_sync(lb_get_pd_fb(handler));
560 ErrPrint("Failed to do sync FB (%s - %s)\n", pkgname, util_basename(util_uri_to_path(id)));
564 handler->is_pd_created = (status == 0);
566 if (handler->pd_created_cb) {
570 cb = handler->pd_created_cb;
571 cbdata = handler->pd_created_cbdata;
573 handler->pd_created_cb = NULL;
574 handler->pd_created_cbdata = NULL;
577 * Before call the Callback function,
578 * pd_create_cb must be reset.
579 * Because, in the create callback, user can call create_pd function again.
581 DbgPrint("PERF_DBOX\n");
582 cb(handler, status, cbdata);
583 } else if (handler->is_pd_created) {
584 lb_invoke_event_handler(handler, LB_EVENT_PD_CREATED);
591 static struct packet *master_pd_destroyed(pid_t pid, int handle, const struct packet *packet)
593 struct livebox *handler;
599 ret = packet_get(packet, "ssi", &pkgname, &id, &status);
601 ErrPrint("Invalid argument\n");
605 handler = lb_find_livebox(pkgname, id);
607 ErrPrint("Instance(%s) is not exists\n", id);
611 if (handler->state != CREATE) {
612 ErrPrint("Instance(%s) is not created\n", id);
616 handler->is_pd_created = 0;
618 if (handler->pd_destroyed_cb) {
622 cb = handler->pd_destroyed_cb;
623 cbdata = handler->pd_destroyed_cbdata;
625 handler->pd_destroyed_cb = NULL;
626 handler->pd_destroyed_cbdata = NULL;
629 * Before call the Callback function,
630 * pd_destroyed_cb must be reset.
631 * Because, in the create callback, user can call destroy_pd function again.
633 cb(handler, status, cbdata);
634 } else if (status == 0) {
635 lb_invoke_event_handler(handler, LB_EVENT_PD_DESTROYED);
642 static struct packet *master_pd_updated(pid_t pid, int handle, const struct packet *packet)
646 const char *descfile;
649 struct livebox *handler;
653 ret = packet_get(packet, "ssssii",
658 ErrPrint("Invalid argument\n");
662 handler = lb_find_livebox(pkgname, id);
664 ErrPrint("Instance(%s) is not exists\n", id);
668 if (handler->state != CREATE) {
671 * This handler is already deleted by the user.
672 * So don't try to notice anything about this anymore.
673 * Just ignore all events.
675 ErrPrint("Instance(%s) is not created\n", id);
679 lb_set_pdsize(handler, pd_w, pd_h);
681 if (lb_text_pd(handler)) {
682 (void)parse_desc(handler, descfile, 1);
684 (void)lb_set_pd_fb(handler, fbfile);
686 ret = fb_sync(lb_get_pd_fb(handler));
688 ErrPrint("Failed to do sync FB (%s - %s), %d\n", pkgname, util_basename(util_uri_to_path(id)), ret);
690 lb_invoke_event_handler(handler, LB_EVENT_PD_UPDATED);
698 static struct packet *master_update_mode(pid_t pid, int handle, const struct packet *packet)
700 struct livebox *handler;
708 ErrPrint("Invalid packet\n");
712 ret = packet_get(packet, "ssii", &pkgname, &id, &status, &active_mode);
714 ErrPrint("Invalid argument\n");
718 handler = lb_find_livebox(pkgname, id);
720 ErrPrint("Livebox(%s) is not found\n", id);
724 if (handler->state != CREATE) {
725 ErrPrint("Livebox(%s) is not created yet\n", id);
729 if (status == LB_STATUS_SUCCESS) {
730 lb_set_update_mode(handler, active_mode);
733 if (handler->update_mode_cb) {
737 cb = handler->update_mode_cb;
738 cbdata = handler->update_mode_cbdata;
740 handler->update_mode_cb = NULL;
741 handler->update_mode_cbdata = NULL;
743 cb(handler, status, cbdata);
745 lb_invoke_event_handler(handler, LB_EVENT_UPDATE_MODE_CHANGED);
752 static struct packet *master_size_changed(pid_t pid, int handle, const struct packet *packet)
754 struct livebox *handler;
765 ErrPrint("Invalid packet\n");
769 ret = packet_get(packet, "sssiiii", &pkgname, &id, &fbfile, &is_pd, &w, &h, &status);
771 ErrPrint("Invalid argument\n");
775 handler = lb_find_livebox(pkgname, id);
777 ErrPrint("Livebox(%s) is not found\n", id);
781 if (handler->state != CREATE) {
782 ErrPrint("Livebox(%s) is not created yet\n", id);
789 * PD is not able to resized by the client.
790 * PD is only can be managed by the provider.
791 * So the PD has no private resized event handler.
792 * Notify it via global event handler only.
795 lb_set_pdsize(handler, w, h);
796 lb_invoke_event_handler(handler, LB_EVENT_PD_SIZE_CHANGED);
798 ErrPrint("This is not possible. PD Size is changed but the return value is not ZERO (%d)\n", status);
802 lb_set_size(handler, w, h);
806 * If there is a created LB FB,
809 if (lb_get_lb_fb(handler)) {
810 lb_set_lb_fb(handler, fbfile);
812 ret = fb_sync(lb_get_lb_fb(handler));
814 ErrPrint("Failed to do sync FB (%s - %s)\n", pkgname, util_basename(util_uri_to_path(id)));
817 /* Just update the size info only. */
822 * I cannot believe client.
823 * So I added some log before & after call the user callback.
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);
837 lb_invoke_event_handler(handler, LB_EVENT_LB_SIZE_CHANGED);
840 if (handler->size_changed_cb) {
844 cb = handler->size_changed_cb;
845 cbdata = handler->size_cbdata;
847 handler->size_changed_cb = NULL;
848 handler->size_cbdata = NULL;
850 cb(handler, status, cbdata);
859 static struct packet *master_period_changed(pid_t pid, int handle, const struct packet *packet)
861 struct livebox *handler;
868 ret = packet_get(packet, "idss", &status, &period, &pkgname, &id);
870 ErrPrint("Invalid argument\n");
874 handler = lb_find_livebox(pkgname, id);
876 ErrPrint("Livebox(%s) is not found\n", id);
880 if (handler->state != CREATE) {
881 ErrPrint("Livebox(%s) is not created\n", id);
886 lb_set_period(handler, period);
889 if (handler->period_changed_cb) {
893 cb = handler->period_changed_cb;
894 cbdata = handler->period_cbdata;
896 handler->period_changed_cb = NULL;
897 handler->period_cbdata = NULL;
899 cb(handler, status, cbdata);
900 } else if (status == 0) {
901 lb_invoke_event_handler(handler, LB_EVENT_PERIOD_CHANGED);
908 static struct packet *master_group_changed(pid_t pid, int handle, const struct packet *packet)
910 struct livebox *handler;
915 const char *category;
918 ret = packet_get(packet, "ssiss", &pkgname, &id, &status, &cluster, &category);
920 ErrPrint("Invalid argument\n");
924 handler = lb_find_livebox(pkgname, id);
926 ErrPrint("Livebox(%s) is not exists\n", id);
930 if (handler->state != CREATE) {
933 * Do no access this handler,
934 * You cannot believe this handler anymore.
936 ErrPrint("Livebox(%s) is not created\n", id);
941 (void)lb_set_group(handler, cluster, category);
944 if (handler->group_changed_cb) {
948 cb = handler->group_changed_cb;
949 cbdata = handler->group_cbdata;
951 handler->group_changed_cb = NULL;
952 handler->group_cbdata = NULL;
954 cb(handler, status, cbdata);
955 } else if (status == 0) {
956 lb_invoke_event_handler(handler, LB_EVENT_GROUP_CHANGED);
963 static struct packet *master_created(pid_t pid, int handle, const struct packet *packet)
965 struct livebox *handler;
976 const char *category;
977 const char *lb_fname;
978 const char *pd_fname;
982 const char *auto_launch;
987 enum lb_type lb_type;
988 enum pd_type pd_type;
992 int old_state = DESTROYED;
996 ret = packet_get(packet, "dsssiiiisssssdiiiiidsi",
998 &pkgname, &id, &content,
999 &lb_w, &lb_h, &pd_w, &pd_h,
1000 &cluster, &category, &lb_fname, &pd_fname,
1001 &auto_launch, &priority, &size_list, &user, &pinup_supported,
1002 &lb_type, &pd_type, &period, &title, &is_pinned_up);
1004 ErrPrint("Invalid argument\n");
1005 ret = LB_STATUS_ERROR_INVALID;
1009 ErrPrint("[%lf] pkgname: %s, id: %s, content: %s, "
1010 "pd_w: %d, pd_h: %d, lb_w: %d, lb_h: %d, "
1011 "cluster: %s, category: %s, lb_fname: \"%s\", pd_fname: \"%s\", "
1012 "auto_launch: %s, priority: %lf, size_list: %d, user: %d, pinup: %d, "
1013 "lb_type: %d, pd_type: %d, period: %lf, title: [%s], is_pinned_up: %d\n",
1014 timestamp, pkgname, id, content,
1015 pd_w, pd_h, lb_w, lb_h,
1016 cluster, category, lb_fname, pd_fname,
1017 auto_launch, priority, size_list, user, pinup_supported,
1018 lb_type, pd_type, period, title, is_pinned_up);
1020 handler = lb_find_livebox_by_timestamp(timestamp);
1022 handler = lb_new_livebox(pkgname, id, timestamp);
1024 ErrPrint("Failed to create a new livebox\n");
1025 ret = LB_STATUS_ERROR_FAULT;
1029 old_state = handler->state;
1031 if (handler->state != CREATE) {
1032 if (handler->state != DELETE) {
1035 * This is not possible!!!
1037 ErrPrint("Invalid handler\n");
1038 ret = LB_STATUS_ERROR_INVALID;
1044 * After get the delete states,
1045 * call the create callback with deleted result.
1049 old_state = handler->state;
1052 ErrPrint("Already created: timestamp[%lf] "
1053 "pkgname[%s], id[%s] content[%s] "
1054 "cluster[%s] category[%s] lb_fname[%s] pd_fname[%s]\n",
1055 timestamp, pkgname, id,
1056 content, cluster, category,
1057 lb_fname, pd_fname);
1059 ret = LB_STATUS_ERROR_ALREADY;
1063 lb_set_id(handler, id);
1066 lb_set_size(handler, lb_w, lb_h);
1067 handler->lb.type = lb_type;
1068 handler->is_pinned_up = is_pinned_up;
1073 case _LB_TYPE_SCRIPT:
1074 case _LB_TYPE_BUFFER:
1075 if (!strlen(lb_fname)) {
1078 lb_set_lb_fb(handler, lb_fname);
1079 ret = fb_sync(lb_get_lb_fb(handler));
1081 ErrPrint("Failed to do sync FB (%s - %s)\n", pkgname, util_basename(util_uri_to_path(id)));
1085 lb_set_text_lb(handler);
1091 handler->pd.type = pd_type;
1092 lb_set_pdsize(handler, pd_w, pd_h);
1093 lb_set_default_pdsize(handler, pd_w, pd_h);
1095 case _PD_TYPE_SCRIPT:
1096 case _PD_TYPE_BUFFER:
1097 if (!strlen(pd_fname)) {
1101 lb_set_pd_fb(handler, pd_fname);
1102 ret = fb_sync(lb_get_pd_fb(handler));
1104 ErrPrint("Failed to do sync FB (%s - %s)\n", pkgname, util_basename(util_uri_to_path(id)));
1108 lb_set_text_pd(handler);
1114 lb_set_priority(handler, priority);
1116 lb_set_size_list(handler, size_list);
1117 lb_set_group(handler, cluster, category);
1119 lb_set_content(handler, content);
1120 lb_set_title(handler, title);
1122 lb_set_user(handler, user);
1124 lb_set_auto_launch(handler, auto_launch);
1125 lb_set_pinup(handler, pinup_supported);
1127 lb_set_period(handler, period);
1131 if (handler->state == CREATE) {
1134 * These callback can change the handler->state.
1135 * So we have to use the "old_state" which stored state before call these callbacks
1138 if (handler->created_cb) {
1142 cb = handler->created_cb;
1143 cbdata = handler->created_cbdata;
1145 handler->created_cb = NULL;
1146 handler->created_cbdata = NULL;
1148 cb(handler, ret, cbdata);
1150 lb_invoke_event_handler(handler, LB_EVENT_CREATED);
1155 if (ret == 0 && old_state == DELETE) {
1156 lb_send_delete(handler, handler->created_cb, handler->created_cbdata);
1160 * handler->created_cb = NULL;
1161 * handler->created_cbdata = NULL;
1163 * Do not clear this to use this from the deleted event callback.
1164 * if this value is not cleared when the deleted event callback check it,
1165 * it means that the created function is not called yet.
1166 * Then the call the deleted event callback with LB_STATUS_ERROR_CANCEL errno.
1173 static struct method s_table[] = {
1175 .cmd = "lb_updated", /* pkgname, id, lb_w, lb_h, priority, ret */
1176 .handler = master_lb_updated,
1179 .cmd = "pd_updated", /* pkgname, id, descfile, pd_w, pd_h, ret */
1180 .handler = master_pd_updated,
1183 .cmd = "pd_created",
1184 .handler = master_pd_created,
1187 .cmd = "pd_destroyed",
1188 .handler = master_pd_destroyed,
1191 .cmd = "fault_package", /* pkgname, id, function, ret */
1192 .handler = master_fault_package,
1195 .cmd = "deleted", /* pkgname, id, timestamp, ret */
1196 .handler = master_deleted,
1199 .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 */
1200 .handler = master_created,
1203 .cmd = "group_changed",
1204 .handler = master_group_changed,
1207 .cmd = "period_changed",
1208 .handler = master_period_changed,
1211 .cmd = "size_changed",
1212 .handler = master_size_changed,
1216 .handler = master_pinup,
1220 .handler = master_hold_scroll,
1224 .cmd = "update_mode",
1225 .handler = master_update_mode,
1229 .cmd = "lb_update_begin",
1230 .handler = master_lb_update_begin,
1233 .cmd = "lb_update_end",
1234 .handler = master_lb_update_end,
1238 .cmd = "pd_update_begin",
1239 .handler = master_pd_update_begin,
1242 .cmd = "pd_update_end",
1243 .handler = master_pd_update_end,
1247 .cmd = "access_status",
1248 .handler = master_access_status,
1257 static void acquire_cb(struct livebox *handler, const struct packet *result, void *data)
1260 DbgPrint("Result packet is not valid\n");
1264 if (packet_get(result, "i", &ret) != 1) {
1265 ErrPrint("Invalid argument\n");
1267 DbgPrint("Acquire returns: %d\n", ret);
1274 static inline int make_connection(void)
1276 struct packet *packet;
1279 DbgPrint("Let's making connection!\n");
1281 s_info.fd = com_core_packet_client_init(client_addr(), 0, s_table);
1282 if (s_info.fd < 0) {
1283 ErrPrint("Try this again later\n");
1284 return LB_STATUS_ERROR_IO;
1287 packet = packet_create("acquire", "d", util_timestamp());
1289 com_core_packet_client_fini(s_info.fd);
1291 return LB_STATUS_ERROR_FAULT;
1294 ret = master_rpc_async_request(NULL, packet, 1, acquire_cb, NULL);
1296 ErrPrint("Master RPC returns %d\n", ret);
1297 com_core_packet_client_fini(s_info.fd);
1299 return LB_STATUS_ERROR_IO;
1302 return LB_STATUS_SUCCESS;
1305 static int connected_cb(int handle, void *data)
1307 master_rpc_check_and_fire_consumer();
1311 static void master_started_cb(keynode_t *node, void *data)
1315 if (vconf_get_bool(VCONFKEY_MASTER_STARTED, &state) < 0)
1316 ErrPrint("Unable to get [%s]\n", VCONFKEY_MASTER_STARTED);
1318 DbgPrint("Master state: %d\n", state);
1319 if (state == 1 && make_connection() == LB_STATUS_SUCCESS) {
1321 ret = vconf_ignore_key_changed(VCONFKEY_MASTER_STARTED, master_started_cb);
1322 DbgPrint("master_started vconf key de-registered [%d]\n", ret);
1326 static gboolean timeout_cb(gpointer data)
1328 if (vconf_notify_key_changed(VCONFKEY_MASTER_STARTED, master_started_cb, NULL) < 0)
1329 ErrPrint("Failed to add vconf for monitoring service state\n");
1331 DbgPrint("vconf event callback is registered\n");
1333 master_started_cb(NULL, NULL);
1335 s_info.timer_id = 0;
1339 static int disconnected_cb(int handle, void *data)
1341 if (s_info.fd != handle) {
1342 /*!< This handle is not my favor */
1346 s_info.fd = -1; /*!< Disconnected */
1348 master_rpc_clear_all_request();
1349 lb_invoke_fault_handler(LB_FAULT_PROVIDER_DISCONNECTED, MASTER_PKGNAME, "default", "disconnected");
1353 /* Try to reconnect after 1 sec later */
1354 if (!s_info.timer_id) {
1355 DbgPrint("Reconnecting timer is added\n");
1356 s_info.timer_id = g_timeout_add(1000, timeout_cb, NULL);
1357 if (s_info.timer_id == 0) {
1358 ErrPrint("Unable to add reconnecting timer\n");
1362 ErrPrint("Reconnecting timer is already exists\n");
1368 int client_init(void)
1370 s_info.client_addr = vconf_get_str(VCONFKEY_MASTER_CLIENT_ADDR);
1371 if (!s_info.client_addr) {
1372 s_info.client_addr = strdup(CLIENT_SOCKET);
1373 if (!s_info.client_addr) {
1374 ErrPrint("Heap: %s\n", strerror(errno));
1379 (void)file_service_init();
1381 DbgPrint("Server Address: %s\n", s_info.client_addr);
1383 com_core_add_event_callback(CONNECTOR_DISCONNECTED, disconnected_cb, NULL);
1384 com_core_add_event_callback(CONNECTOR_CONNECTED, connected_cb, NULL);
1385 if (vconf_notify_key_changed(VCONFKEY_MASTER_STARTED, master_started_cb, NULL) < 0) {
1386 ErrPrint("Failed to add vconf for service state\n");
1388 DbgPrint("vconf event callback is registered\n");
1391 master_started_cb(NULL, NULL);
1400 const char *client_addr(void)
1402 return s_info.client_addr;
1405 int client_fini(void)
1409 (void)file_service_fini();
1411 ret = vconf_ignore_key_changed(VCONFKEY_MASTER_STARTED, master_started_cb);
1413 DbgPrint("Ignore vconf key: %d\n", ret);
1416 com_core_del_event_callback(CONNECTOR_DISCONNECTED, disconnected_cb, NULL);
1417 com_core_del_event_callback(CONNECTOR_CONNECTED, connected_cb, NULL);
1418 com_core_packet_client_fini(s_info.fd);
1420 free(s_info.client_addr);
1421 s_info.client_addr = NULL;
1422 return LB_STATUS_SUCCESS;