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);
212 handler->deleted_cb = NULL;
213 handler->deleted_cbdata = NULL;
216 cb = handler->created_cb;
217 cbdata = handler->created_cbdata;
219 handler->created_cb = NULL;
220 handler->created_cbdata = NULL;
222 cb(handler, LB_STATUS_ERROR_CANCEL, cbdata);
223 } else if (handler->id) {
224 if (handler->deleted_cb) {
228 cb = handler->deleted_cb;
229 cbdata = handler->deleted_cbdata;
231 handler->deleted_cb = NULL;
232 handler->deleted_cbdata = NULL;
234 cb(handler, LB_STATUS_SUCCESS, cbdata);
236 lb_invoke_event_handler(handler, LB_EVENT_DELETED);
240 /* Just try to delete it, if a user didn't remove it from the live box list */
247 static struct packet *master_lb_update_begin(pid_t pid, int handle, const struct packet *packet)
249 struct livebox *handler;
258 ret = packet_get(packet, "ssdsss", &pkgname, &id, &priority, &content, &title, &fbfile);
260 ErrPrint("Invalid argument\n");
264 handler = lb_find_livebox(pkgname, id);
266 ErrPrint("Instance[%s] is not exists\n", id);
270 if (handler->state != CREATE) {
271 ErrPrint("(%s) is not created\n", id);
275 lb_set_priority(handler, priority);
276 lb_set_content(handler, content);
277 lb_set_title(handler, title);
281 * Width & Height is not changed in this case.
282 * If the active update is began, the size should not be changed,
283 * And if the size is changed, the provider should finish the updating first.
284 * And then begin updating again after change its size.
286 if (lb_get_lb_fb(handler)) {
287 (void)lb_set_lb_fb(handler, fbfile);
289 ret = fb_sync(lb_get_lb_fb(handler));
290 if (ret != LB_STATUS_SUCCESS) {
291 ErrPrint("Failed to do sync FB (%s - %s) (%d)\n", pkgname, fbfile, ret);
293 lb_invoke_event_handler(handler, LB_EVENT_LB_UPDATE_BEGIN);
296 ErrPrint("Invalid request[%s], %s\n", id, fbfile);
303 static struct packet *master_pd_update_begin(pid_t pid, int handle, const struct packet *packet)
305 struct livebox *handler;
311 ret = packet_get(packet, "sss", &pkgname, &id, &fbfile);
313 ErrPrint("Invalid argument\n");
317 handler = lb_find_livebox(pkgname, id);
319 ErrPrint("Instance[%s] is not exists\n", id);
323 if (handler->state != CREATE) {
324 ErrPrint("[%s] is not created\n", id);
328 if (lb_get_pd_fb(handler)) {
329 (void)lb_set_lb_fb(handler, fbfile);
331 ret = fb_sync(lb_get_lb_fb(handler));
332 if (ret != LB_STATUS_SUCCESS) {
333 ErrPrint("Failed to do sync FB (%s - %s) (%d)\n", pkgname, fbfile, ret);
335 lb_invoke_event_handler(handler, LB_EVENT_PD_UPDATE_BEGIN);
338 ErrPrint("Invalid request[%s], %s\n", id, fbfile);
345 static struct packet *master_lb_update_end(pid_t pid, int handle, const struct packet *packet)
347 struct livebox *handler;
352 ret = packet_get(packet, "ss", &pkgname, &id);
354 ErrPrint("Invalid argument\n");
358 handler = lb_find_livebox(pkgname, id);
360 ErrPrint("Instance[%s] is not exists\n", id);
364 if (handler->state != CREATE) {
365 ErrPrint("[%s] is not created\n", id);
369 if (lb_get_lb_fb(handler)) {
370 lb_invoke_event_handler(handler, LB_EVENT_LB_UPDATE_END);
372 ErrPrint("Invalid request[%s]\n", id);
379 static struct packet *master_access_status(pid_t pid, int handle, const struct packet *packet)
381 struct livebox *handler;
387 ret = packet_get(packet, "ssi", &pkgname, &id, &status);
389 ErrPrint("Invalid argument\n");
393 handler = lb_find_livebox(pkgname, id);
395 ErrPrint("Instance[%s] is not exists\n", id);
399 if (handler->state != CREATE) {
400 ErrPrint("[%s] is not created\n", id);
404 if (handler->access_event_cb) {
408 cb = handler->access_event_cb;
409 cbdata = handler->access_event_cbdata;
411 handler->access_event_cb = NULL;
412 handler->access_event_cbdata = NULL;
414 cb(handler, status, cbdata);
416 ErrPrint("Invalid event[%s]\n", id);
422 static struct packet *master_pd_update_end(pid_t pid, int handle, const struct packet *packet)
424 struct livebox *handler;
429 ret = packet_get(packet, "ss", &pkgname, &id);
431 ErrPrint("Invalid argument\n");
435 handler = lb_find_livebox(pkgname, id);
437 ErrPrint("Instance[%s] is not exists\n", id);
441 if (handler->state != CREATE) {
442 ErrPrint("[%s] is not created\n", id);
446 if (lb_get_lb_fb(handler)) {
447 lb_invoke_event_handler(handler, LB_EVENT_PD_UPDATE_END);
449 ErrPrint("Invalid request[%s]", id);
456 static struct packet *master_lb_updated(pid_t pid, int handle, const struct packet *packet)
463 const char *safe_file;
464 struct livebox *handler;
470 ret = packet_get(packet, "sssiidsss",
472 &fbfile, &lb_w, &lb_h,
473 &priority, &content, &title,
476 ErrPrint("Invalid argument\n");
480 handler = lb_find_livebox(pkgname, id);
482 ErrPrint("instance(%s) is not exists\n", id);
486 if (handler->state != CREATE) {
489 * Already deleted by the user.
490 * Don't try to notice anything with this, Just ignore all events
491 * Beacuse the user doesn't wants know about this anymore
493 ErrPrint("(%s) is not exists, but updated\n", id);
497 lb_set_priority(handler, priority);
498 lb_set_content(handler, content);
499 lb_set_title(handler, title);
500 lb_set_size(handler, lb_w, lb_h);
501 lb_set_filename(handler, safe_file);
503 if (lb_text_lb(handler)) {
504 (void)parse_desc(handler, livebox_filename(handler), 0);
507 * DESC parser will call the "text event callback".
508 * Don't need to call global event callback in this case.
511 } else if (lb_get_lb_fb(handler)) {
512 if (conf_frame_drop_for_resizing() && handler->size_changed_cb) {
513 /* Just for skipping the update event callback call, After request to resize buffer, update event will be discarded */
514 DbgPrint("Discards obsoloted update event\n");
515 ret = LB_STATUS_ERROR_BUSY;
517 (void)lb_set_lb_fb(handler, fbfile);
519 if (!conf_manual_sync()) {
520 ret = fb_sync(lb_get_lb_fb(handler));
521 if (ret != LB_STATUS_SUCCESS) {
522 ErrPrint("Failed to do sync FB (%s - %s) (%d)\n", pkgname, util_basename(util_uri_to_path(id)), ret);
525 ret = LB_STATUS_SUCCESS;
529 ret = LB_STATUS_SUCCESS;
532 if (ret == LB_STATUS_SUCCESS) {
533 lb_invoke_event_handler(handler, LB_EVENT_LB_UPDATED);
540 static struct packet *master_pd_created(pid_t pid, int handle, const struct packet *packet)
542 struct livebox *handler;
551 ret = packet_get(packet, "sssiii", &pkgname, &id, &buf_id, &width, &height, &status);
553 ErrPrint("Invalid argument\n");
557 handler = lb_find_livebox(pkgname, id);
559 ErrPrint("Instance(%s) is not exists\n", id);
563 if (handler->state != CREATE) {
564 ErrPrint("Instance(%s) is not created\n", id);
568 lb_set_pdsize(handler, width, height);
569 if (lb_text_pd(handler)) {
570 DbgPrint("Text TYPE does not need to handle this\n");
572 (void)lb_set_pd_fb(handler, buf_id);
573 ret = fb_sync(lb_get_pd_fb(handler));
575 ErrPrint("Failed to do sync FB (%s - %s)\n", pkgname, util_basename(util_uri_to_path(id)));
579 handler->is_pd_created = (status == 0);
581 if (handler->pd_created_cb) {
585 cb = handler->pd_created_cb;
586 cbdata = handler->pd_created_cbdata;
588 handler->pd_created_cb = NULL;
589 handler->pd_created_cbdata = NULL;
592 * Before call the Callback function,
593 * pd_create_cb must be reset.
594 * Because, in the create callback, user can call create_pd function again.
596 DbgPrint("PERF_DBOX\n");
597 cb(handler, status, cbdata);
598 } else if (handler->is_pd_created) {
599 lb_invoke_event_handler(handler, LB_EVENT_PD_CREATED);
606 static struct packet *master_pd_destroyed(pid_t pid, int handle, const struct packet *packet)
608 struct livebox *handler;
614 ret = packet_get(packet, "ssi", &pkgname, &id, &status);
616 ErrPrint("Invalid argument\n");
620 handler = lb_find_livebox(pkgname, id);
622 ErrPrint("Instance(%s) is not exists\n", id);
626 if (handler->state != CREATE) {
627 ErrPrint("Instance(%s) is not created\n", id);
631 handler->is_pd_created = 0;
633 if (handler->pd_destroyed_cb) {
637 cb = handler->pd_destroyed_cb;
638 cbdata = handler->pd_destroyed_cbdata;
640 handler->pd_destroyed_cb = NULL;
641 handler->pd_destroyed_cbdata = NULL;
644 * Before call the Callback function,
645 * pd_destroyed_cb must be reset.
646 * Because, in the create callback, user can call destroy_pd function again.
648 cb(handler, status, cbdata);
649 } else if (status == 0) {
650 lb_invoke_event_handler(handler, LB_EVENT_PD_DESTROYED);
657 static struct packet *master_pd_updated(pid_t pid, int handle, const struct packet *packet)
661 const char *descfile;
664 struct livebox *handler;
668 ret = packet_get(packet, "ssssii",
673 ErrPrint("Invalid argument\n");
677 handler = lb_find_livebox(pkgname, id);
679 ErrPrint("Instance(%s) is not exists\n", id);
683 if (handler->state != CREATE) {
686 * This handler is already deleted by the user.
687 * So don't try to notice anything about this anymore.
688 * Just ignore all events.
690 ErrPrint("Instance(%s) is not created\n", id);
694 lb_set_pdsize(handler, pd_w, pd_h);
696 if (lb_text_pd(handler)) {
697 (void)parse_desc(handler, descfile, 1);
699 if (conf_frame_drop_for_resizing() && handler->size_changed_cb) {
700 /* Just for skipping the update event callback call, After request to resize buffer, update event will be discarded */
701 DbgPrint("Discards obsoloted update event\n");
703 (void)lb_set_pd_fb(handler, fbfile);
705 if (!conf_manual_sync()) {
706 ret = fb_sync(lb_get_pd_fb(handler));
708 ErrPrint("Failed to do sync FB (%s - %s), %d\n", pkgname, util_basename(util_uri_to_path(id)), ret);
710 lb_invoke_event_handler(handler, LB_EVENT_PD_UPDATED);
720 static struct packet *master_update_mode(pid_t pid, int handle, const struct packet *packet)
722 struct livebox *handler;
730 ErrPrint("Invalid packet\n");
734 ret = packet_get(packet, "ssii", &pkgname, &id, &status, &active_mode);
736 ErrPrint("Invalid argument\n");
740 handler = lb_find_livebox(pkgname, id);
742 ErrPrint("Livebox(%s) is not found\n", id);
746 if (handler->state != CREATE) {
747 ErrPrint("Livebox(%s) is not created yet\n", id);
751 if (status == LB_STATUS_SUCCESS) {
752 lb_set_update_mode(handler, active_mode);
755 if (handler->update_mode_cb) {
759 cb = handler->update_mode_cb;
760 cbdata = handler->update_mode_cbdata;
762 handler->update_mode_cb = NULL;
763 handler->update_mode_cbdata = NULL;
765 cb(handler, status, cbdata);
767 lb_invoke_event_handler(handler, LB_EVENT_UPDATE_MODE_CHANGED);
774 static struct packet *master_size_changed(pid_t pid, int handle, const struct packet *packet)
776 struct livebox *handler;
787 ErrPrint("Invalid packet\n");
791 ret = packet_get(packet, "sssiiii", &pkgname, &id, &fbfile, &is_pd, &w, &h, &status);
793 ErrPrint("Invalid argument\n");
797 handler = lb_find_livebox(pkgname, id);
799 ErrPrint("Livebox(%s) is not found\n", id);
803 if (handler->state != CREATE) {
804 ErrPrint("Livebox(%s) is not created yet\n", id);
811 * PD is not able to resized by the client.
812 * PD is only can be managed by the provider.
813 * So the PD has no private resized event handler.
814 * Notify it via global event handler only.
817 lb_set_pdsize(handler, w, h);
818 lb_invoke_event_handler(handler, LB_EVENT_PD_SIZE_CHANGED);
820 ErrPrint("This is not possible. PD Size is changed but the return value is not ZERO (%d)\n", status);
824 lb_set_size(handler, w, h);
828 * If there is a created LB FB,
831 if (lb_get_lb_fb(handler)) {
832 (void)lb_set_lb_fb(handler, fbfile);
834 ret = fb_sync(lb_get_lb_fb(handler));
836 ErrPrint("Failed to do sync FB (%s - %s)\n", pkgname, util_basename(util_uri_to_path(id)));
839 /* Just update the size info only. */
844 * I cannot believe client.
845 * So I added some log before & after call the user callback.
847 if (handler->size_changed_cb) {
851 cb = handler->size_changed_cb;
852 cbdata = handler->size_cbdata;
854 handler->size_changed_cb = NULL;
855 handler->size_cbdata = NULL;
857 cb(handler, status, cbdata);
859 lb_invoke_event_handler(handler, LB_EVENT_LB_SIZE_CHANGED);
862 if (handler->size_changed_cb) {
866 cb = handler->size_changed_cb;
867 cbdata = handler->size_cbdata;
869 handler->size_changed_cb = NULL;
870 handler->size_cbdata = NULL;
872 cb(handler, status, cbdata);
881 static struct packet *master_period_changed(pid_t pid, int handle, const struct packet *packet)
883 struct livebox *handler;
890 ret = packet_get(packet, "idss", &status, &period, &pkgname, &id);
892 ErrPrint("Invalid argument\n");
896 handler = lb_find_livebox(pkgname, id);
898 ErrPrint("Livebox(%s) is not found\n", id);
902 if (handler->state != CREATE) {
903 ErrPrint("Livebox(%s) is not created\n", id);
908 lb_set_period(handler, period);
911 if (handler->period_changed_cb) {
915 cb = handler->period_changed_cb;
916 cbdata = handler->period_cbdata;
918 handler->period_changed_cb = NULL;
919 handler->period_cbdata = NULL;
921 cb(handler, status, cbdata);
922 } else if (status == 0) {
923 lb_invoke_event_handler(handler, LB_EVENT_PERIOD_CHANGED);
930 static struct packet *master_group_changed(pid_t pid, int handle, const struct packet *packet)
932 struct livebox *handler;
937 const char *category;
940 ret = packet_get(packet, "ssiss", &pkgname, &id, &status, &cluster, &category);
942 ErrPrint("Invalid argument\n");
946 handler = lb_find_livebox(pkgname, id);
948 ErrPrint("Livebox(%s) is not exists\n", id);
952 if (handler->state != CREATE) {
955 * Do no access this handler,
956 * You cannot believe this handler anymore.
958 ErrPrint("Livebox(%s) is not created\n", id);
963 (void)lb_set_group(handler, cluster, category);
966 if (handler->group_changed_cb) {
970 cb = handler->group_changed_cb;
971 cbdata = handler->group_cbdata;
973 handler->group_changed_cb = NULL;
974 handler->group_cbdata = NULL;
976 cb(handler, status, cbdata);
977 } else if (status == 0) {
978 lb_invoke_event_handler(handler, LB_EVENT_GROUP_CHANGED);
985 static struct packet *master_created(pid_t pid, int handle, const struct packet *packet)
987 struct livebox *handler;
998 const char *category;
999 const char *lb_fname;
1000 const char *pd_fname;
1004 const char *auto_launch;
1008 int pinup_supported;
1009 enum lb_type lb_type;
1010 enum pd_type pd_type;
1014 int old_state = DESTROYED;
1018 ret = packet_get(packet, "dsssiiiisssssdiiiiidsi",
1020 &pkgname, &id, &content,
1021 &lb_w, &lb_h, &pd_w, &pd_h,
1022 &cluster, &category, &lb_fname, &pd_fname,
1023 &auto_launch, &priority, &size_list, &user, &pinup_supported,
1024 &lb_type, &pd_type, &period, &title, &is_pinned_up);
1026 ErrPrint("Invalid argument\n");
1027 ret = LB_STATUS_ERROR_INVALID;
1031 ErrPrint("[%lf] pkgname: %s, id: %s, content: %s, "
1032 "pd_w: %d, pd_h: %d, lb_w: %d, lb_h: %d, "
1033 "cluster: %s, category: %s, lb_fname: \"%s\", pd_fname: \"%s\", "
1034 "auto_launch: %s, priority: %lf, size_list: %d, user: %d, pinup: %d, "
1035 "lb_type: %d, pd_type: %d, period: %lf, title: [%s], is_pinned_up: %d\n",
1036 timestamp, pkgname, id, content,
1037 pd_w, pd_h, lb_w, lb_h,
1038 cluster, category, lb_fname, pd_fname,
1039 auto_launch, priority, size_list, user, pinup_supported,
1040 lb_type, pd_type, period, title, is_pinned_up);
1042 handler = lb_find_livebox_by_timestamp(timestamp);
1044 handler = lb_new_livebox(pkgname, id, timestamp);
1046 ErrPrint("Failed to create a new livebox\n");
1047 ret = LB_STATUS_ERROR_FAULT;
1051 old_state = handler->state;
1053 if (handler->state != CREATE) {
1054 if (handler->state != DELETE) {
1057 * This is not possible!!!
1059 ErrPrint("Invalid handler\n");
1060 ret = LB_STATUS_ERROR_INVALID;
1066 * After get the delete states,
1067 * call the create callback with deleted result.
1071 old_state = handler->state;
1074 ErrPrint("Already created: timestamp[%lf] "
1075 "pkgname[%s], id[%s] content[%s] "
1076 "cluster[%s] category[%s] lb_fname[%s] pd_fname[%s]\n",
1077 timestamp, pkgname, id,
1078 content, cluster, category,
1079 lb_fname, pd_fname);
1081 ret = LB_STATUS_ERROR_ALREADY;
1085 lb_set_id(handler, id);
1088 lb_set_size(handler, lb_w, lb_h);
1089 handler->lb.type = lb_type;
1090 handler->is_pinned_up = is_pinned_up;
1095 case _LB_TYPE_SCRIPT:
1096 case _LB_TYPE_BUFFER:
1097 if (!strlen(lb_fname)) {
1100 (void)lb_set_lb_fb(handler, lb_fname);
1102 ret = fb_sync(lb_get_lb_fb(handler));
1104 ErrPrint("Failed to do sync FB (%s - %s)\n", pkgname, util_basename(util_uri_to_path(id)));
1108 lb_set_text_lb(handler);
1114 handler->pd.type = pd_type;
1115 lb_set_pdsize(handler, pd_w, pd_h);
1116 lb_set_default_pdsize(handler, pd_w, pd_h);
1118 case _PD_TYPE_SCRIPT:
1119 case _PD_TYPE_BUFFER:
1120 if (!strlen(pd_fname)) {
1124 lb_set_pd_fb(handler, pd_fname);
1125 ret = fb_sync(lb_get_pd_fb(handler));
1127 ErrPrint("Failed to do sync FB (%s - %s)\n", pkgname, util_basename(util_uri_to_path(id)));
1131 lb_set_text_pd(handler);
1137 lb_set_priority(handler, priority);
1139 lb_set_size_list(handler, size_list);
1140 lb_set_group(handler, cluster, category);
1142 lb_set_content(handler, content);
1143 lb_set_title(handler, title);
1145 lb_set_user(handler, user);
1147 lb_set_auto_launch(handler, auto_launch);
1148 lb_set_pinup(handler, pinup_supported);
1150 lb_set_period(handler, period);
1154 if (handler->state == CREATE) {
1157 * These callback can change the handler->state.
1158 * So we have to use the "old_state" which stored state before call these callbacks
1161 if (handler->created_cb) {
1165 cb = handler->created_cb;
1166 cbdata = handler->created_cbdata;
1168 handler->created_cb = NULL;
1169 handler->created_cbdata = NULL;
1171 cb(handler, ret, cbdata);
1173 lb_invoke_event_handler(handler, LB_EVENT_CREATED);
1178 if (ret == 0 && old_state == DELETE) {
1179 lb_send_delete(handler, handler->created_cb, handler->created_cbdata);
1183 * handler->created_cb = NULL;
1184 * handler->created_cbdata = NULL;
1186 * Do not clear this to use this from the deleted event callback.
1187 * if this value is not cleared when the deleted event callback check it,
1188 * it means that the created function is not called yet.
1189 * Then the call the deleted event callback with LB_STATUS_ERROR_CANCEL errno.
1196 static struct method s_table[] = {
1198 .cmd = "lb_updated", /* pkgname, id, lb_w, lb_h, priority, ret */
1199 .handler = master_lb_updated,
1202 .cmd = "pd_updated", /* pkgname, id, descfile, pd_w, pd_h, ret */
1203 .handler = master_pd_updated,
1206 .cmd = "pd_created",
1207 .handler = master_pd_created,
1210 .cmd = "pd_destroyed",
1211 .handler = master_pd_destroyed,
1214 .cmd = "fault_package", /* pkgname, id, function, ret */
1215 .handler = master_fault_package,
1218 .cmd = "deleted", /* pkgname, id, timestamp, ret */
1219 .handler = master_deleted,
1222 .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 */
1223 .handler = master_created,
1226 .cmd = "group_changed",
1227 .handler = master_group_changed,
1230 .cmd = "period_changed",
1231 .handler = master_period_changed,
1234 .cmd = "size_changed",
1235 .handler = master_size_changed,
1239 .handler = master_pinup,
1243 .handler = master_hold_scroll,
1247 .cmd = "update_mode",
1248 .handler = master_update_mode,
1252 .cmd = "lb_update_begin",
1253 .handler = master_lb_update_begin,
1256 .cmd = "lb_update_end",
1257 .handler = master_lb_update_end,
1261 .cmd = "pd_update_begin",
1262 .handler = master_pd_update_begin,
1265 .cmd = "pd_update_end",
1266 .handler = master_pd_update_end,
1270 .cmd = "access_status",
1271 .handler = master_access_status,
1280 static void acquire_cb(struct livebox *handler, const struct packet *result, void *data)
1283 DbgPrint("Result packet is not valid\n");
1287 if (packet_get(result, "i", &ret) != 1) {
1288 ErrPrint("Invalid argument\n");
1290 DbgPrint("Acquire returns: %d\n", ret);
1297 static inline int make_connection(void)
1299 struct packet *packet;
1302 DbgPrint("Let's making connection!\n");
1304 s_info.fd = com_core_packet_client_init(client_addr(), 0, s_table);
1305 if (s_info.fd < 0) {
1306 ErrPrint("Try this again later\n");
1307 return LB_STATUS_ERROR_IO;
1310 packet = packet_create("acquire", "d", util_timestamp());
1312 com_core_packet_client_fini(s_info.fd);
1314 return LB_STATUS_ERROR_FAULT;
1317 ret = master_rpc_async_request(NULL, packet, 1, acquire_cb, NULL);
1319 ErrPrint("Master RPC returns %d\n", ret);
1320 com_core_packet_client_fini(s_info.fd);
1322 return LB_STATUS_ERROR_IO;
1325 return LB_STATUS_SUCCESS;
1328 static int connected_cb(int handle, void *data)
1330 master_rpc_check_and_fire_consumer();
1334 static void master_started_cb(keynode_t *node, void *data)
1338 if (vconf_get_bool(VCONFKEY_MASTER_STARTED, &state) < 0) {
1339 ErrPrint("Unable to get [%s]\n", VCONFKEY_MASTER_STARTED);
1342 DbgPrint("Master state: %d\n", state);
1343 if (state == 1 && make_connection() == LB_STATUS_SUCCESS) {
1345 ret = vconf_ignore_key_changed(VCONFKEY_MASTER_STARTED, master_started_cb);
1346 DbgPrint("master_started vconf key de-registered [%d]\n", ret);
1350 static gboolean timeout_cb(gpointer data)
1352 if (vconf_notify_key_changed(VCONFKEY_MASTER_STARTED, master_started_cb, NULL) < 0) {
1353 ErrPrint("Failed to add vconf for monitoring service state\n");
1355 DbgPrint("vconf event callback is registered\n");
1358 master_started_cb(NULL, NULL);
1360 s_info.timer_id = 0;
1364 static int disconnected_cb(int handle, void *data)
1366 if (s_info.fd != handle) {
1367 /*!< This handle is not my favor */
1371 s_info.fd = -1; /*!< Disconnected */
1373 master_rpc_clear_all_request();
1374 lb_invoke_fault_handler(LB_FAULT_PROVIDER_DISCONNECTED, MASTER_PKGNAME, "default", "disconnected");
1378 /* Try to reconnect after 1 sec later */
1379 if (!s_info.timer_id) {
1380 DbgPrint("Reconnecting timer is added\n");
1381 s_info.timer_id = g_timeout_add(1000, timeout_cb, NULL);
1382 if (s_info.timer_id == 0) {
1383 ErrPrint("Unable to add reconnecting timer\n");
1387 ErrPrint("Reconnecting timer is already exists\n");
1393 int client_init(void)
1395 s_info.client_addr = vconf_get_str(VCONFKEY_MASTER_CLIENT_ADDR);
1396 if (!s_info.client_addr) {
1397 s_info.client_addr = strdup(CLIENT_SOCKET);
1398 if (!s_info.client_addr) {
1399 ErrPrint("Heap: %s\n", strerror(errno));
1404 (void)file_service_init();
1406 DbgPrint("Server Address: %s\n", s_info.client_addr);
1408 com_core_add_event_callback(CONNECTOR_DISCONNECTED, disconnected_cb, NULL);
1409 com_core_add_event_callback(CONNECTOR_CONNECTED, connected_cb, NULL);
1410 if (vconf_notify_key_changed(VCONFKEY_MASTER_STARTED, master_started_cb, NULL) < 0) {
1411 ErrPrint("Failed to add vconf for service state\n");
1413 DbgPrint("vconf event callback is registered\n");
1416 master_started_cb(NULL, NULL);
1425 const char *client_addr(void)
1427 return s_info.client_addr;
1430 int client_fini(void)
1434 (void)file_service_fini();
1436 ret = vconf_ignore_key_changed(VCONFKEY_MASTER_STARTED, master_started_cb);
1438 DbgPrint("Ignore vconf key: %d\n", ret);
1441 com_core_del_event_callback(CONNECTOR_DISCONNECTED, disconnected_cb, NULL);
1442 com_core_del_event_callback(CONNECTOR_CONNECTED, connected_cb, NULL);
1443 com_core_packet_client_fini(s_info.fd);
1445 free(s_info.client_addr);
1446 s_info.client_addr = NULL;
1447 return LB_STATUS_SUCCESS;