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;
166 if (packet_get(packet, "ssdi", &pkgname, &id, ×tamp, &reason) != 4) {
167 ErrPrint("Invalid arguemnt\n");
171 handler = lb_find_livebox_by_timestamp(timestamp);
175 * This can be happens only if the user delete a livebox
176 * right after create it before receive created event.
181 /*!< Check validity of this "handler" */
182 if (handler->state != CREATE) {
183 if (handler->state != DELETE) {
186 * This is not possible
188 CRITICAL_LOG("Already deleted handler (%s - %s)\n", pkgname, id);
193 if (handler->created_cb) {
199 * "if (handler->id == NULL) {"
201 * The instance is not created yet.
202 * But the master forcely destroy it and send destroyed event to this
203 * without the created event.
205 * It could be destroyed when a slave has critical error(fault)
206 * before creating an instance successfully.
208 if (handler->created_cb == handler->deleted_cb) {
209 if (handler->created_cbdata != handler->deleted_cbdata) {
210 DbgPrint("cb is same but cbdata is different (%s - %s)\n", pkgname, id);
213 handler->deleted_cb = NULL;
214 handler->deleted_cbdata = NULL;
217 cb = handler->created_cb;
218 cbdata = handler->created_cbdata;
220 handler->created_cb = NULL;
221 handler->created_cbdata = NULL;
223 if (reason == LB_STATUS_SUCCESS) {
224 reason = LB_STATUS_ERROR_CANCEL;
226 cb(handler, reason, cbdata);
227 } else if (handler->id) {
228 if (handler->deleted_cb) {
232 cb = handler->deleted_cb;
233 cbdata = handler->deleted_cbdata;
235 handler->deleted_cb = NULL;
236 handler->deleted_cbdata = NULL;
238 cb(handler, reason, cbdata);
240 lb_invoke_event_handler(handler, LB_EVENT_DELETED);
244 /* Just try to delete it, if a user didn't remove it from the live box list */
251 static struct packet *master_lb_update_begin(pid_t pid, int handle, const struct packet *packet)
253 struct livebox *handler;
262 ret = packet_get(packet, "ssdsss", &pkgname, &id, &priority, &content, &title, &fbfile);
264 ErrPrint("Invalid argument\n");
268 handler = lb_find_livebox(pkgname, id);
270 ErrPrint("Instance[%s] is not exists\n", id);
274 if (handler->state != CREATE) {
275 ErrPrint("(%s) is not created\n", id);
279 lb_set_priority(handler, priority);
280 lb_set_content(handler, content);
281 lb_set_title(handler, title);
285 * Width & Height is not changed in this case.
286 * If the active update is began, the size should not be changed,
287 * And if the size is changed, the provider should finish the updating first.
288 * And then begin updating again after change its size.
290 if (lb_get_lb_fb(handler)) {
291 (void)lb_set_lb_fb(handler, fbfile);
293 ret = fb_sync(lb_get_lb_fb(handler));
294 if (ret != LB_STATUS_SUCCESS) {
295 ErrPrint("Failed to do sync FB (%s - %s) (%d)\n", pkgname, fbfile, ret);
297 lb_invoke_event_handler(handler, LB_EVENT_LB_UPDATE_BEGIN);
300 ErrPrint("Invalid request[%s], %s\n", id, fbfile);
307 static struct packet *master_pd_update_begin(pid_t pid, int handle, const struct packet *packet)
309 struct livebox *handler;
315 ret = packet_get(packet, "sss", &pkgname, &id, &fbfile);
317 ErrPrint("Invalid argument\n");
321 handler = lb_find_livebox(pkgname, id);
323 ErrPrint("Instance[%s] is not exists\n", id);
327 if (handler->state != CREATE) {
328 ErrPrint("[%s] is not created\n", id);
332 if (lb_get_pd_fb(handler)) {
333 (void)lb_set_lb_fb(handler, fbfile);
335 ret = fb_sync(lb_get_lb_fb(handler));
336 if (ret != LB_STATUS_SUCCESS) {
337 ErrPrint("Failed to do sync FB (%s - %s) (%d)\n", pkgname, fbfile, ret);
339 lb_invoke_event_handler(handler, LB_EVENT_PD_UPDATE_BEGIN);
342 ErrPrint("Invalid request[%s], %s\n", id, fbfile);
349 static struct packet *master_lb_update_end(pid_t pid, int handle, const struct packet *packet)
351 struct livebox *handler;
356 ret = packet_get(packet, "ss", &pkgname, &id);
358 ErrPrint("Invalid argument\n");
362 handler = lb_find_livebox(pkgname, id);
364 ErrPrint("Instance[%s] is not exists\n", id);
368 if (handler->state != CREATE) {
369 ErrPrint("[%s] is not created\n", id);
373 if (lb_get_lb_fb(handler)) {
374 lb_invoke_event_handler(handler, LB_EVENT_LB_UPDATE_END);
376 ErrPrint("Invalid request[%s]\n", id);
383 static struct packet *master_access_status(pid_t pid, int handle, const struct packet *packet)
385 struct livebox *handler;
391 ret = packet_get(packet, "ssi", &pkgname, &id, &status);
393 ErrPrint("Invalid argument\n");
397 handler = lb_find_livebox(pkgname, id);
399 ErrPrint("Instance[%s] is not exists\n", id);
403 if (handler->state != CREATE) {
404 ErrPrint("[%s] is not created\n", id);
408 if (handler->access_event_cb) {
412 cb = handler->access_event_cb;
413 cbdata = handler->access_event_cbdata;
415 handler->access_event_cb = NULL;
416 handler->access_event_cbdata = NULL;
418 cb(handler, status, cbdata);
420 ErrPrint("Invalid event[%s]\n", id);
426 static struct packet *master_pd_update_end(pid_t pid, int handle, const struct packet *packet)
428 struct livebox *handler;
433 ret = packet_get(packet, "ss", &pkgname, &id);
435 ErrPrint("Invalid argument\n");
439 handler = lb_find_livebox(pkgname, id);
441 ErrPrint("Instance[%s] is not exists\n", id);
445 if (handler->state != CREATE) {
446 ErrPrint("[%s] is not created\n", id);
450 if (lb_get_lb_fb(handler)) {
451 lb_invoke_event_handler(handler, LB_EVENT_PD_UPDATE_END);
453 ErrPrint("Invalid request[%s]", id);
460 static struct packet *master_lb_updated(pid_t pid, int handle, const struct packet *packet)
467 const char *safe_file;
468 struct livebox *handler;
474 ret = packet_get(packet, "sssiidsss",
476 &fbfile, &lb_w, &lb_h,
477 &priority, &content, &title,
480 ErrPrint("Invalid argument\n");
484 handler = lb_find_livebox(pkgname, id);
486 ErrPrint("instance(%s) is not exists\n", id);
490 if (handler->state != CREATE) {
493 * Already deleted by the user.
494 * Don't try to notice anything with this, Just ignore all events
495 * Beacuse the user doesn't wants know about this anymore
497 ErrPrint("(%s) is not exists, but updated\n", id);
501 lb_set_priority(handler, priority);
502 lb_set_content(handler, content);
503 lb_set_title(handler, title);
504 lb_set_size(handler, lb_w, lb_h);
505 lb_set_filename(handler, safe_file);
507 if (lb_text_lb(handler)) {
508 (void)parse_desc(handler, livebox_filename(handler), 0);
511 * DESC parser will call the "text event callback".
512 * Don't need to call global event callback in this case.
515 } else if (lb_get_lb_fb(handler)) {
516 if (conf_frame_drop_for_resizing() && handler->size_changed_cb) {
517 /* Just for skipping the update event callback call, After request to resize buffer, update event will be discarded */
518 DbgPrint("Discards obsoloted update event\n");
519 ret = LB_STATUS_ERROR_BUSY;
521 (void)lb_set_lb_fb(handler, fbfile);
523 if (!conf_manual_sync()) {
524 ret = fb_sync(lb_get_lb_fb(handler));
525 if (ret != LB_STATUS_SUCCESS) {
526 ErrPrint("Failed to do sync FB (%s - %s) (%d)\n", pkgname, util_basename(util_uri_to_path(id)), ret);
529 ret = LB_STATUS_SUCCESS;
533 ret = LB_STATUS_SUCCESS;
536 if (ret == LB_STATUS_SUCCESS) {
537 lb_invoke_event_handler(handler, LB_EVENT_LB_UPDATED);
544 static struct packet *master_pd_created(pid_t pid, int handle, const struct packet *packet)
546 struct livebox *handler;
555 ret = packet_get(packet, "sssiii", &pkgname, &id, &buf_id, &width, &height, &status);
557 ErrPrint("Invalid argument\n");
561 handler = lb_find_livebox(pkgname, id);
563 ErrPrint("Instance(%s) is not exists\n", id);
567 if (handler->state != CREATE) {
568 ErrPrint("Instance(%s) is not created\n", id);
572 lb_set_pdsize(handler, width, height);
573 if (lb_text_pd(handler)) {
574 DbgPrint("Text TYPE does not need to handle this\n");
576 (void)lb_set_pd_fb(handler, buf_id);
577 ret = fb_sync(lb_get_pd_fb(handler));
579 ErrPrint("Failed to do sync FB (%s - %s)\n", pkgname, util_basename(util_uri_to_path(id)));
583 handler->is_pd_created = (status == 0);
585 if (handler->pd_created_cb) {
589 cb = handler->pd_created_cb;
590 cbdata = handler->pd_created_cbdata;
592 handler->pd_created_cb = NULL;
593 handler->pd_created_cbdata = NULL;
596 * Before call the Callback function,
597 * pd_create_cb must be reset.
598 * Because, in the create callback, user can call create_pd function again.
600 DbgPrint("PERF_DBOX\n");
601 cb(handler, status, cbdata);
602 } else if (handler->is_pd_created) {
603 lb_invoke_event_handler(handler, LB_EVENT_PD_CREATED);
610 static struct packet *master_pd_destroyed(pid_t pid, int handle, const struct packet *packet)
612 struct livebox *handler;
618 ret = packet_get(packet, "ssi", &pkgname, &id, &status);
620 ErrPrint("Invalid argument\n");
624 handler = lb_find_livebox(pkgname, id);
626 ErrPrint("Instance(%s) is not exists\n", id);
630 if (handler->state != CREATE) {
631 ErrPrint("Instance(%s) is not created\n", id);
635 handler->is_pd_created = 0;
637 if (handler->pd_destroyed_cb) {
641 cb = handler->pd_destroyed_cb;
642 cbdata = handler->pd_destroyed_cbdata;
644 handler->pd_destroyed_cb = NULL;
645 handler->pd_destroyed_cbdata = NULL;
648 * Before call the Callback function,
649 * pd_destroyed_cb must be reset.
650 * Because, in the create callback, user can call destroy_pd function again.
652 cb(handler, status, cbdata);
653 } else if (status == 0) {
654 lb_invoke_event_handler(handler, LB_EVENT_PD_DESTROYED);
661 static struct packet *master_pd_updated(pid_t pid, int handle, const struct packet *packet)
665 const char *descfile;
668 struct livebox *handler;
672 ret = packet_get(packet, "ssssii",
677 ErrPrint("Invalid argument\n");
681 handler = lb_find_livebox(pkgname, id);
683 ErrPrint("Instance(%s) is not exists\n", id);
687 if (handler->state != CREATE) {
690 * This handler is already deleted by the user.
691 * So don't try to notice anything about this anymore.
692 * Just ignore all events.
694 ErrPrint("Instance(%s) is not created\n", id);
698 lb_set_pdsize(handler, pd_w, pd_h);
700 if (lb_text_pd(handler)) {
701 (void)parse_desc(handler, descfile, 1);
703 if (conf_frame_drop_for_resizing() && handler->size_changed_cb) {
704 /* Just for skipping the update event callback call, After request to resize buffer, update event will be discarded */
705 DbgPrint("Discards obsoloted update event\n");
707 (void)lb_set_pd_fb(handler, fbfile);
709 if (!conf_manual_sync()) {
710 ret = fb_sync(lb_get_pd_fb(handler));
712 ErrPrint("Failed to do sync FB (%s - %s), %d\n", pkgname, util_basename(util_uri_to_path(id)), ret);
714 lb_invoke_event_handler(handler, LB_EVENT_PD_UPDATED);
717 lb_invoke_event_handler(handler, LB_EVENT_PD_UPDATED);
726 static struct packet *master_update_mode(pid_t pid, int handle, const struct packet *packet)
728 struct livebox *handler;
736 ErrPrint("Invalid packet\n");
740 ret = packet_get(packet, "ssii", &pkgname, &id, &status, &active_mode);
742 ErrPrint("Invalid argument\n");
746 handler = lb_find_livebox(pkgname, id);
748 ErrPrint("Livebox(%s) is not found\n", id);
752 if (handler->state != CREATE) {
753 ErrPrint("Livebox(%s) is not created yet\n", id);
757 if (status == LB_STATUS_SUCCESS) {
758 lb_set_update_mode(handler, active_mode);
761 if (handler->update_mode_cb) {
765 cb = handler->update_mode_cb;
766 cbdata = handler->update_mode_cbdata;
768 handler->update_mode_cb = NULL;
769 handler->update_mode_cbdata = NULL;
771 cb(handler, status, cbdata);
773 lb_invoke_event_handler(handler, LB_EVENT_UPDATE_MODE_CHANGED);
780 static struct packet *master_size_changed(pid_t pid, int handle, const struct packet *packet)
782 struct livebox *handler;
793 ErrPrint("Invalid packet\n");
797 ret = packet_get(packet, "sssiiii", &pkgname, &id, &fbfile, &is_pd, &w, &h, &status);
799 ErrPrint("Invalid argument\n");
803 handler = lb_find_livebox(pkgname, id);
805 ErrPrint("Livebox(%s) is not found\n", id);
809 if (handler->state != CREATE) {
810 ErrPrint("Livebox(%s) is not created yet\n", id);
817 * PD is not able to resized by the client.
818 * PD is only can be managed by the provider.
819 * So the PD has no private resized event handler.
820 * Notify it via global event handler only.
823 lb_set_pdsize(handler, w, h);
824 lb_invoke_event_handler(handler, LB_EVENT_PD_SIZE_CHANGED);
826 ErrPrint("This is not possible. PD Size is changed but the return value is not ZERO (%d)\n", status);
830 lb_set_size(handler, w, h);
834 * If there is a created LB FB,
837 if (lb_get_lb_fb(handler)) {
838 (void)lb_set_lb_fb(handler, fbfile);
840 ret = fb_sync(lb_get_lb_fb(handler));
842 ErrPrint("Failed to do sync FB (%s - %s)\n", pkgname, util_basename(util_uri_to_path(id)));
845 /* Just update the size info only. */
850 * I cannot believe client.
851 * So I added some log before & after call the user callback.
853 if (handler->size_changed_cb) {
857 cb = handler->size_changed_cb;
858 cbdata = handler->size_cbdata;
860 handler->size_changed_cb = NULL;
861 handler->size_cbdata = NULL;
863 cb(handler, status, cbdata);
865 lb_invoke_event_handler(handler, LB_EVENT_LB_SIZE_CHANGED);
868 if (handler->size_changed_cb) {
872 cb = handler->size_changed_cb;
873 cbdata = handler->size_cbdata;
875 handler->size_changed_cb = NULL;
876 handler->size_cbdata = NULL;
878 cb(handler, status, cbdata);
887 static struct packet *master_period_changed(pid_t pid, int handle, const struct packet *packet)
889 struct livebox *handler;
896 ret = packet_get(packet, "idss", &status, &period, &pkgname, &id);
898 ErrPrint("Invalid argument\n");
902 handler = lb_find_livebox(pkgname, id);
904 ErrPrint("Livebox(%s) is not found\n", id);
908 if (handler->state != CREATE) {
909 ErrPrint("Livebox(%s) is not created\n", id);
914 lb_set_period(handler, period);
917 if (handler->period_changed_cb) {
921 cb = handler->period_changed_cb;
922 cbdata = handler->period_cbdata;
924 handler->period_changed_cb = NULL;
925 handler->period_cbdata = NULL;
927 cb(handler, status, cbdata);
928 } else if (status == 0) {
929 lb_invoke_event_handler(handler, LB_EVENT_PERIOD_CHANGED);
936 static struct packet *master_group_changed(pid_t pid, int handle, const struct packet *packet)
938 struct livebox *handler;
943 const char *category;
946 ret = packet_get(packet, "ssiss", &pkgname, &id, &status, &cluster, &category);
948 ErrPrint("Invalid argument\n");
952 handler = lb_find_livebox(pkgname, id);
954 ErrPrint("Livebox(%s) is not exists\n", id);
958 if (handler->state != CREATE) {
961 * Do no access this handler,
962 * You cannot believe this handler anymore.
964 ErrPrint("Livebox(%s) is not created\n", id);
969 (void)lb_set_group(handler, cluster, category);
972 if (handler->group_changed_cb) {
976 cb = handler->group_changed_cb;
977 cbdata = handler->group_cbdata;
979 handler->group_changed_cb = NULL;
980 handler->group_cbdata = NULL;
982 cb(handler, status, cbdata);
983 } else if (status == 0) {
984 lb_invoke_event_handler(handler, LB_EVENT_GROUP_CHANGED);
991 static struct packet *master_created(pid_t pid, int handle, const struct packet *packet)
993 struct livebox *handler;
1002 const char *content;
1003 const char *cluster;
1004 const char *category;
1005 const char *lb_fname;
1006 const char *pd_fname;
1010 const char *auto_launch;
1014 int pinup_supported;
1015 enum lb_type lb_type;
1016 enum pd_type pd_type;
1020 int old_state = DESTROYED;
1024 ret = packet_get(packet, "dsssiiiisssssdiiiiidsi",
1026 &pkgname, &id, &content,
1027 &lb_w, &lb_h, &pd_w, &pd_h,
1028 &cluster, &category, &lb_fname, &pd_fname,
1029 &auto_launch, &priority, &size_list, &user, &pinup_supported,
1030 &lb_type, &pd_type, &period, &title, &is_pinned_up);
1032 ErrPrint("Invalid argument\n");
1033 ret = LB_STATUS_ERROR_INVALID;
1037 ErrPrint("[%lf] pkgname: %s, id: %s, content: %s, "
1038 "pd_w: %d, pd_h: %d, lb_w: %d, lb_h: %d, "
1039 "cluster: %s, category: %s, lb_fname: \"%s\", pd_fname: \"%s\", "
1040 "auto_launch: %s, priority: %lf, size_list: %d, user: %d, pinup: %d, "
1041 "lb_type: %d, pd_type: %d, period: %lf, title: [%s], is_pinned_up: %d\n",
1042 timestamp, pkgname, id, content,
1043 pd_w, pd_h, lb_w, lb_h,
1044 cluster, category, lb_fname, pd_fname,
1045 auto_launch, priority, size_list, user, pinup_supported,
1046 lb_type, pd_type, period, title, is_pinned_up);
1048 handler = lb_find_livebox_by_timestamp(timestamp);
1050 handler = lb_new_livebox(pkgname, id, timestamp);
1052 ErrPrint("Failed to create a new livebox\n");
1053 ret = LB_STATUS_ERROR_FAULT;
1057 old_state = handler->state;
1059 if (handler->state != CREATE) {
1060 if (handler->state != DELETE) {
1063 * This is not possible!!!
1065 ErrPrint("Invalid handler\n");
1066 ret = LB_STATUS_ERROR_INVALID;
1072 * After get the delete states,
1073 * call the create callback with deleted result.
1077 old_state = handler->state;
1080 ErrPrint("Already created: timestamp[%lf] "
1081 "pkgname[%s], id[%s] content[%s] "
1082 "cluster[%s] category[%s] lb_fname[%s] pd_fname[%s]\n",
1083 timestamp, pkgname, id,
1084 content, cluster, category,
1085 lb_fname, pd_fname);
1087 ret = LB_STATUS_ERROR_ALREADY;
1091 lb_set_id(handler, id);
1094 lb_set_size(handler, lb_w, lb_h);
1095 handler->lb.type = lb_type;
1096 handler->is_pinned_up = is_pinned_up;
1101 case _LB_TYPE_SCRIPT:
1102 case _LB_TYPE_BUFFER:
1103 if (!strlen(lb_fname)) {
1106 (void)lb_set_lb_fb(handler, lb_fname);
1108 ret = fb_sync(lb_get_lb_fb(handler));
1110 ErrPrint("Failed to do sync FB (%s - %s)\n", pkgname, util_basename(util_uri_to_path(id)));
1114 lb_set_text_lb(handler);
1120 handler->pd.type = pd_type;
1121 lb_set_pdsize(handler, pd_w, pd_h);
1122 lb_set_default_pdsize(handler, pd_w, pd_h);
1124 case _PD_TYPE_SCRIPT:
1125 case _PD_TYPE_BUFFER:
1126 if (!strlen(pd_fname)) {
1130 lb_set_pd_fb(handler, pd_fname);
1131 ret = fb_sync(lb_get_pd_fb(handler));
1133 ErrPrint("Failed to do sync FB (%s - %s)\n", pkgname, util_basename(util_uri_to_path(id)));
1137 lb_set_text_pd(handler);
1143 lb_set_priority(handler, priority);
1145 lb_set_size_list(handler, size_list);
1146 lb_set_group(handler, cluster, category);
1148 lb_set_content(handler, content);
1149 lb_set_title(handler, title);
1151 lb_set_user(handler, user);
1153 lb_set_auto_launch(handler, auto_launch);
1154 lb_set_pinup(handler, pinup_supported);
1156 lb_set_period(handler, period);
1160 if (handler->state == CREATE) {
1163 * These callback can change the handler->state.
1164 * So we have to use the "old_state" which stored state before call these callbacks
1167 if (handler->created_cb) {
1171 cb = handler->created_cb;
1172 cbdata = handler->created_cbdata;
1174 handler->created_cb = NULL;
1175 handler->created_cbdata = NULL;
1177 cb(handler, ret, cbdata);
1179 lb_invoke_event_handler(handler, LB_EVENT_CREATED);
1184 if (ret == 0 && old_state == DELETE) {
1185 lb_send_delete(handler, handler->created_cb, handler->created_cbdata);
1189 * handler->created_cb = NULL;
1190 * handler->created_cbdata = NULL;
1192 * Do not clear this to use this from the deleted event callback.
1193 * if this value is not cleared when the deleted event callback check it,
1194 * it means that the created function is not called yet.
1195 * Then the call the deleted event callback with LB_STATUS_ERROR_CANCEL errno.
1202 static struct method s_table[] = {
1204 .cmd = "lb_updated", /* pkgname, id, lb_w, lb_h, priority, ret */
1205 .handler = master_lb_updated,
1208 .cmd = "pd_updated", /* pkgname, id, descfile, pd_w, pd_h, ret */
1209 .handler = master_pd_updated,
1212 .cmd = "pd_created",
1213 .handler = master_pd_created,
1216 .cmd = "pd_destroyed",
1217 .handler = master_pd_destroyed,
1220 .cmd = "fault_package", /* pkgname, id, function, ret */
1221 .handler = master_fault_package,
1224 .cmd = "deleted", /* pkgname, id, timestamp, ret */
1225 .handler = master_deleted,
1228 .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 */
1229 .handler = master_created,
1232 .cmd = "group_changed",
1233 .handler = master_group_changed,
1236 .cmd = "period_changed",
1237 .handler = master_period_changed,
1240 .cmd = "size_changed",
1241 .handler = master_size_changed,
1245 .handler = master_pinup,
1249 .handler = master_hold_scroll,
1253 .cmd = "update_mode",
1254 .handler = master_update_mode,
1258 .cmd = "lb_update_begin",
1259 .handler = master_lb_update_begin,
1262 .cmd = "lb_update_end",
1263 .handler = master_lb_update_end,
1267 .cmd = "pd_update_begin",
1268 .handler = master_pd_update_begin,
1271 .cmd = "pd_update_end",
1272 .handler = master_pd_update_end,
1276 .cmd = "access_status",
1277 .handler = master_access_status,
1286 static void acquire_cb(struct livebox *handler, const struct packet *result, void *data)
1289 DbgPrint("Result packet is not valid\n");
1293 if (packet_get(result, "i", &ret) != 1) {
1294 ErrPrint("Invalid argument\n");
1296 DbgPrint("Acquire returns: %d\n", ret);
1303 static inline int make_connection(void)
1305 struct packet *packet;
1308 DbgPrint("Let's making connection!\n");
1310 s_info.fd = com_core_packet_client_init(client_addr(), 0, s_table);
1311 if (s_info.fd < 0) {
1312 ErrPrint("Try this again later\n");
1313 return LB_STATUS_ERROR_IO;
1316 packet = packet_create("acquire", "d", util_timestamp());
1318 com_core_packet_client_fini(s_info.fd);
1320 return LB_STATUS_ERROR_FAULT;
1323 ret = master_rpc_async_request(NULL, packet, 1, acquire_cb, NULL);
1325 ErrPrint("Master RPC returns %d\n", ret);
1326 com_core_packet_client_fini(s_info.fd);
1328 return LB_STATUS_ERROR_IO;
1331 return LB_STATUS_SUCCESS;
1334 static int connected_cb(int handle, void *data)
1336 master_rpc_check_and_fire_consumer();
1340 static void master_started_cb(keynode_t *node, void *data)
1344 if (vconf_get_bool(VCONFKEY_MASTER_STARTED, &state) < 0) {
1345 ErrPrint("Unable to get [%s]\n", VCONFKEY_MASTER_STARTED);
1348 DbgPrint("Master state: %d\n", state);
1349 if (state == 1 && make_connection() == LB_STATUS_SUCCESS) {
1351 ret = vconf_ignore_key_changed(VCONFKEY_MASTER_STARTED, master_started_cb);
1352 DbgPrint("master_started vconf key de-registered [%d]\n", ret);
1356 static gboolean timeout_cb(gpointer data)
1358 if (vconf_notify_key_changed(VCONFKEY_MASTER_STARTED, master_started_cb, NULL) < 0) {
1359 ErrPrint("Failed to add vconf for monitoring service state\n");
1361 DbgPrint("vconf event callback is registered\n");
1364 master_started_cb(NULL, NULL);
1366 s_info.timer_id = 0;
1370 static int disconnected_cb(int handle, void *data)
1372 if (s_info.fd != handle) {
1373 /*!< This handle is not my favor */
1377 s_info.fd = -1; /*!< Disconnected */
1379 master_rpc_clear_all_request();
1380 lb_invoke_fault_handler(LB_FAULT_PROVIDER_DISCONNECTED, MASTER_PKGNAME, "default", "disconnected");
1384 /* Try to reconnect after 1 sec later */
1385 if (!s_info.timer_id) {
1386 DbgPrint("Reconnecting timer is added\n");
1387 s_info.timer_id = g_timeout_add(1000, timeout_cb, NULL);
1388 if (s_info.timer_id == 0) {
1389 ErrPrint("Unable to add reconnecting timer\n");
1393 ErrPrint("Reconnecting timer is already exists\n");
1399 int client_init(void)
1401 s_info.client_addr = vconf_get_str(VCONFKEY_MASTER_CLIENT_ADDR);
1402 if (!s_info.client_addr) {
1403 s_info.client_addr = strdup(CLIENT_SOCKET);
1404 if (!s_info.client_addr) {
1405 ErrPrint("Heap: %s\n", strerror(errno));
1410 (void)file_service_init();
1412 DbgPrint("Server Address: %s\n", s_info.client_addr);
1414 com_core_add_event_callback(CONNECTOR_DISCONNECTED, disconnected_cb, NULL);
1415 com_core_add_event_callback(CONNECTOR_CONNECTED, connected_cb, NULL);
1416 if (vconf_notify_key_changed(VCONFKEY_MASTER_STARTED, master_started_cb, NULL) < 0) {
1417 ErrPrint("Failed to add vconf for service state\n");
1419 DbgPrint("vconf event callback is registered\n");
1422 master_started_cb(NULL, NULL);
1431 const char *client_addr(void)
1433 return s_info.client_addr;
1436 int client_fini(void)
1440 (void)file_service_fini();
1442 ret = vconf_ignore_key_changed(VCONFKEY_MASTER_STARTED, master_started_cb);
1444 DbgPrint("Ignore vconf key: %d\n", ret);
1447 com_core_del_event_callback(CONNECTOR_DISCONNECTED, disconnected_cb, NULL);
1448 com_core_del_event_callback(CONNECTOR_CONNECTED, connected_cb, NULL);
1449 com_core_packet_client_fini(s_info.fd);
1451 free(s_info.client_addr);
1452 s_info.client_addr = NULL;
1453 return LB_STATUS_SUCCESS;