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 cb(handler, reason, cbdata);
224 } else if (handler->id) {
225 if (handler->deleted_cb) {
229 cb = handler->deleted_cb;
230 cbdata = handler->deleted_cbdata;
232 handler->deleted_cb = NULL;
233 handler->deleted_cbdata = NULL;
235 cb(handler, reason, cbdata);
237 lb_invoke_event_handler(handler, LB_EVENT_DELETED);
241 /* Just try to delete it, if a user didn't remove it from the live box list */
248 static struct packet *master_lb_update_begin(pid_t pid, int handle, const struct packet *packet)
250 struct livebox *handler;
259 ret = packet_get(packet, "ssdsss", &pkgname, &id, &priority, &content, &title, &fbfile);
261 ErrPrint("Invalid argument\n");
265 handler = lb_find_livebox(pkgname, id);
267 ErrPrint("Instance[%s] is not exists\n", id);
271 if (handler->state != CREATE) {
272 ErrPrint("(%s) is not created\n", id);
276 lb_set_priority(handler, priority);
277 lb_set_content(handler, content);
278 lb_set_title(handler, title);
282 * Width & Height is not changed in this case.
283 * If the active update is began, the size should not be changed,
284 * And if the size is changed, the provider should finish the updating first.
285 * And then begin updating again after change its size.
287 if (lb_get_lb_fb(handler)) {
288 (void)lb_set_lb_fb(handler, fbfile);
290 ret = fb_sync(lb_get_lb_fb(handler));
291 if (ret != LB_STATUS_SUCCESS) {
292 ErrPrint("Failed to do sync FB (%s - %s) (%d)\n", pkgname, fbfile, ret);
294 lb_invoke_event_handler(handler, LB_EVENT_LB_UPDATE_BEGIN);
297 ErrPrint("Invalid request[%s], %s\n", id, fbfile);
304 static struct packet *master_pd_update_begin(pid_t pid, int handle, const struct packet *packet)
306 struct livebox *handler;
312 ret = packet_get(packet, "sss", &pkgname, &id, &fbfile);
314 ErrPrint("Invalid argument\n");
318 handler = lb_find_livebox(pkgname, id);
320 ErrPrint("Instance[%s] is not exists\n", id);
324 if (handler->state != CREATE) {
325 ErrPrint("[%s] is not created\n", id);
329 if (lb_get_pd_fb(handler)) {
330 (void)lb_set_lb_fb(handler, fbfile);
332 ret = fb_sync(lb_get_lb_fb(handler));
333 if (ret != LB_STATUS_SUCCESS) {
334 ErrPrint("Failed to do sync FB (%s - %s) (%d)\n", pkgname, fbfile, ret);
336 lb_invoke_event_handler(handler, LB_EVENT_PD_UPDATE_BEGIN);
339 ErrPrint("Invalid request[%s], %s\n", id, fbfile);
346 static struct packet *master_lb_update_end(pid_t pid, int handle, const struct packet *packet)
348 struct livebox *handler;
353 ret = packet_get(packet, "ss", &pkgname, &id);
355 ErrPrint("Invalid argument\n");
359 handler = lb_find_livebox(pkgname, id);
361 ErrPrint("Instance[%s] is not exists\n", id);
365 if (handler->state != CREATE) {
366 ErrPrint("[%s] is not created\n", id);
370 if (lb_get_lb_fb(handler)) {
371 lb_invoke_event_handler(handler, LB_EVENT_LB_UPDATE_END);
373 ErrPrint("Invalid request[%s]\n", id);
380 static struct packet *master_access_status(pid_t pid, int handle, const struct packet *packet)
382 struct livebox *handler;
388 ret = packet_get(packet, "ssi", &pkgname, &id, &status);
390 ErrPrint("Invalid argument\n");
394 handler = lb_find_livebox(pkgname, id);
396 ErrPrint("Instance[%s] is not exists\n", id);
400 if (handler->state != CREATE) {
401 ErrPrint("[%s] is not created\n", id);
405 if (handler->access_event_cb) {
409 cb = handler->access_event_cb;
410 cbdata = handler->access_event_cbdata;
412 handler->access_event_cb = NULL;
413 handler->access_event_cbdata = NULL;
415 cb(handler, status, cbdata);
417 ErrPrint("Invalid event[%s]\n", id);
423 static struct packet *master_pd_update_end(pid_t pid, int handle, const struct packet *packet)
425 struct livebox *handler;
430 ret = packet_get(packet, "ss", &pkgname, &id);
432 ErrPrint("Invalid argument\n");
436 handler = lb_find_livebox(pkgname, id);
438 ErrPrint("Instance[%s] is not exists\n", id);
442 if (handler->state != CREATE) {
443 ErrPrint("[%s] is not created\n", id);
447 if (lb_get_lb_fb(handler)) {
448 lb_invoke_event_handler(handler, LB_EVENT_PD_UPDATE_END);
450 ErrPrint("Invalid request[%s]", id);
457 static struct packet *master_lb_updated(pid_t pid, int handle, const struct packet *packet)
464 const char *safe_file;
465 struct livebox *handler;
471 ret = packet_get(packet, "sssiidsss",
473 &fbfile, &lb_w, &lb_h,
474 &priority, &content, &title,
477 ErrPrint("Invalid argument\n");
481 handler = lb_find_livebox(pkgname, id);
483 ErrPrint("instance(%s) is not exists\n", id);
487 if (handler->state != CREATE) {
490 * Already deleted by the user.
491 * Don't try to notice anything with this, Just ignore all events
492 * Beacuse the user doesn't wants know about this anymore
494 ErrPrint("(%s) is not exists, but updated\n", id);
498 lb_set_priority(handler, priority);
499 lb_set_content(handler, content);
500 lb_set_title(handler, title);
501 lb_set_size(handler, lb_w, lb_h);
502 lb_set_filename(handler, safe_file);
504 if (lb_text_lb(handler)) {
505 (void)parse_desc(handler, livebox_filename(handler), 0);
508 * DESC parser will call the "text event callback".
509 * Don't need to call global event callback in this case.
512 } else if (lb_get_lb_fb(handler)) {
513 if (conf_frame_drop_for_resizing() && handler->size_changed_cb) {
514 /* Just for skipping the update event callback call, After request to resize buffer, update event will be discarded */
515 DbgPrint("Discards obsoloted update event\n");
516 ret = LB_STATUS_ERROR_BUSY;
518 (void)lb_set_lb_fb(handler, fbfile);
520 if (!conf_manual_sync()) {
521 ret = fb_sync(lb_get_lb_fb(handler));
522 if (ret != LB_STATUS_SUCCESS) {
523 ErrPrint("Failed to do sync FB (%s - %s) (%d)\n", pkgname, util_basename(util_uri_to_path(id)), ret);
526 ret = LB_STATUS_SUCCESS;
530 ret = LB_STATUS_SUCCESS;
533 if (ret == LB_STATUS_SUCCESS) {
534 lb_invoke_event_handler(handler, LB_EVENT_LB_UPDATED);
541 static struct packet *master_pd_created(pid_t pid, int handle, const struct packet *packet)
543 struct livebox *handler;
552 ret = packet_get(packet, "sssiii", &pkgname, &id, &buf_id, &width, &height, &status);
554 ErrPrint("Invalid argument\n");
558 handler = lb_find_livebox(pkgname, id);
560 ErrPrint("Instance(%s) is not exists\n", id);
564 if (handler->state != CREATE) {
565 ErrPrint("Instance(%s) is not created\n", id);
569 lb_set_pdsize(handler, width, height);
570 if (lb_text_pd(handler)) {
571 DbgPrint("Text TYPE does not need to handle this\n");
573 (void)lb_set_pd_fb(handler, buf_id);
574 ret = fb_sync(lb_get_pd_fb(handler));
576 ErrPrint("Failed to do sync FB (%s - %s)\n", pkgname, util_basename(util_uri_to_path(id)));
580 handler->is_pd_created = (status == 0);
582 if (handler->pd_created_cb) {
586 cb = handler->pd_created_cb;
587 cbdata = handler->pd_created_cbdata;
589 handler->pd_created_cb = NULL;
590 handler->pd_created_cbdata = NULL;
593 * Before call the Callback function,
594 * pd_create_cb must be reset.
595 * Because, in the create callback, user can call create_pd function again.
597 DbgPrint("PERF_DBOX\n");
598 cb(handler, status, cbdata);
599 } else if (handler->is_pd_created) {
600 lb_invoke_event_handler(handler, LB_EVENT_PD_CREATED);
607 static struct packet *master_pd_destroyed(pid_t pid, int handle, const struct packet *packet)
609 struct livebox *handler;
615 ret = packet_get(packet, "ssi", &pkgname, &id, &status);
617 ErrPrint("Invalid argument\n");
621 handler = lb_find_livebox(pkgname, id);
623 ErrPrint("Instance(%s) is not exists\n", id);
627 if (handler->state != CREATE) {
628 ErrPrint("Instance(%s) is not created\n", id);
632 handler->is_pd_created = 0;
634 if (handler->pd_destroyed_cb) {
638 cb = handler->pd_destroyed_cb;
639 cbdata = handler->pd_destroyed_cbdata;
641 handler->pd_destroyed_cb = NULL;
642 handler->pd_destroyed_cbdata = NULL;
645 * Before call the Callback function,
646 * pd_destroyed_cb must be reset.
647 * Because, in the create callback, user can call destroy_pd function again.
649 cb(handler, status, cbdata);
650 } else if (status == 0) {
651 lb_invoke_event_handler(handler, LB_EVENT_PD_DESTROYED);
658 static struct packet *master_pd_updated(pid_t pid, int handle, const struct packet *packet)
662 const char *descfile;
665 struct livebox *handler;
669 ret = packet_get(packet, "ssssii",
674 ErrPrint("Invalid argument\n");
678 handler = lb_find_livebox(pkgname, id);
680 ErrPrint("Instance(%s) is not exists\n", id);
684 if (handler->state != CREATE) {
687 * This handler is already deleted by the user.
688 * So don't try to notice anything about this anymore.
689 * Just ignore all events.
691 ErrPrint("Instance(%s) is not created\n", id);
695 lb_set_pdsize(handler, pd_w, pd_h);
697 if (lb_text_pd(handler)) {
698 (void)parse_desc(handler, descfile, 1);
700 if (conf_frame_drop_for_resizing() && handler->size_changed_cb) {
701 /* Just for skipping the update event callback call, After request to resize buffer, update event will be discarded */
702 DbgPrint("Discards obsoloted update event\n");
704 (void)lb_set_pd_fb(handler, fbfile);
706 if (!conf_manual_sync()) {
707 ret = fb_sync(lb_get_pd_fb(handler));
709 ErrPrint("Failed to do sync FB (%s - %s), %d\n", pkgname, util_basename(util_uri_to_path(id)), ret);
711 lb_invoke_event_handler(handler, LB_EVENT_PD_UPDATED);
714 lb_invoke_event_handler(handler, LB_EVENT_PD_UPDATED);
723 static struct packet *master_update_mode(pid_t pid, int handle, const struct packet *packet)
725 struct livebox *handler;
733 ErrPrint("Invalid packet\n");
737 ret = packet_get(packet, "ssii", &pkgname, &id, &status, &active_mode);
739 ErrPrint("Invalid argument\n");
743 handler = lb_find_livebox(pkgname, id);
745 ErrPrint("Livebox(%s) is not found\n", id);
749 if (handler->state != CREATE) {
750 ErrPrint("Livebox(%s) is not created yet\n", id);
754 if (status == LB_STATUS_SUCCESS) {
755 lb_set_update_mode(handler, active_mode);
758 if (handler->update_mode_cb) {
762 cb = handler->update_mode_cb;
763 cbdata = handler->update_mode_cbdata;
765 handler->update_mode_cb = NULL;
766 handler->update_mode_cbdata = NULL;
768 cb(handler, status, cbdata);
770 lb_invoke_event_handler(handler, LB_EVENT_UPDATE_MODE_CHANGED);
777 static struct packet *master_size_changed(pid_t pid, int handle, const struct packet *packet)
779 struct livebox *handler;
790 ErrPrint("Invalid packet\n");
794 ret = packet_get(packet, "sssiiii", &pkgname, &id, &fbfile, &is_pd, &w, &h, &status);
796 ErrPrint("Invalid argument\n");
800 handler = lb_find_livebox(pkgname, id);
802 ErrPrint("Livebox(%s) is not found\n", id);
806 if (handler->state != CREATE) {
807 ErrPrint("Livebox(%s) is not created yet\n", id);
814 * PD is not able to resized by the client.
815 * PD is only can be managed by the provider.
816 * So the PD has no private resized event handler.
817 * Notify it via global event handler only.
820 lb_set_pdsize(handler, w, h);
821 lb_invoke_event_handler(handler, LB_EVENT_PD_SIZE_CHANGED);
823 ErrPrint("This is not possible. PD Size is changed but the return value is not ZERO (%d)\n", status);
827 lb_set_size(handler, w, h);
831 * If there is a created LB FB,
834 if (lb_get_lb_fb(handler)) {
835 (void)lb_set_lb_fb(handler, fbfile);
837 ret = fb_sync(lb_get_lb_fb(handler));
839 ErrPrint("Failed to do sync FB (%s - %s)\n", pkgname, util_basename(util_uri_to_path(id)));
842 /* Just update the size info only. */
847 * I cannot believe client.
848 * So I added some log before & after call the user callback.
850 if (handler->size_changed_cb) {
854 cb = handler->size_changed_cb;
855 cbdata = handler->size_cbdata;
857 handler->size_changed_cb = NULL;
858 handler->size_cbdata = NULL;
860 cb(handler, status, cbdata);
862 lb_invoke_event_handler(handler, LB_EVENT_LB_SIZE_CHANGED);
865 if (handler->size_changed_cb) {
869 cb = handler->size_changed_cb;
870 cbdata = handler->size_cbdata;
872 handler->size_changed_cb = NULL;
873 handler->size_cbdata = NULL;
875 cb(handler, status, cbdata);
884 static struct packet *master_period_changed(pid_t pid, int handle, const struct packet *packet)
886 struct livebox *handler;
893 ret = packet_get(packet, "idss", &status, &period, &pkgname, &id);
895 ErrPrint("Invalid argument\n");
899 handler = lb_find_livebox(pkgname, id);
901 ErrPrint("Livebox(%s) is not found\n", id);
905 if (handler->state != CREATE) {
906 ErrPrint("Livebox(%s) is not created\n", id);
911 lb_set_period(handler, period);
914 if (handler->period_changed_cb) {
918 cb = handler->period_changed_cb;
919 cbdata = handler->period_cbdata;
921 handler->period_changed_cb = NULL;
922 handler->period_cbdata = NULL;
924 cb(handler, status, cbdata);
925 } else if (status == 0) {
926 lb_invoke_event_handler(handler, LB_EVENT_PERIOD_CHANGED);
933 static struct packet *master_group_changed(pid_t pid, int handle, const struct packet *packet)
935 struct livebox *handler;
940 const char *category;
943 ret = packet_get(packet, "ssiss", &pkgname, &id, &status, &cluster, &category);
945 ErrPrint("Invalid argument\n");
949 handler = lb_find_livebox(pkgname, id);
951 ErrPrint("Livebox(%s) is not exists\n", id);
955 if (handler->state != CREATE) {
958 * Do no access this handler,
959 * You cannot believe this handler anymore.
961 ErrPrint("Livebox(%s) is not created\n", id);
966 (void)lb_set_group(handler, cluster, category);
969 if (handler->group_changed_cb) {
973 cb = handler->group_changed_cb;
974 cbdata = handler->group_cbdata;
976 handler->group_changed_cb = NULL;
977 handler->group_cbdata = NULL;
979 cb(handler, status, cbdata);
980 } else if (status == 0) {
981 lb_invoke_event_handler(handler, LB_EVENT_GROUP_CHANGED);
988 static struct packet *master_created(pid_t pid, int handle, const struct packet *packet)
990 struct livebox *handler;
1000 const char *cluster;
1001 const char *category;
1002 const char *lb_fname;
1003 const char *pd_fname;
1007 const char *auto_launch;
1011 int pinup_supported;
1012 enum lb_type lb_type;
1013 enum pd_type pd_type;
1017 int old_state = DESTROYED;
1021 ret = packet_get(packet, "dsssiiiisssssdiiiiidsi",
1023 &pkgname, &id, &content,
1024 &lb_w, &lb_h, &pd_w, &pd_h,
1025 &cluster, &category, &lb_fname, &pd_fname,
1026 &auto_launch, &priority, &size_list, &user, &pinup_supported,
1027 &lb_type, &pd_type, &period, &title, &is_pinned_up);
1029 ErrPrint("Invalid argument\n");
1030 ret = LB_STATUS_ERROR_INVALID;
1034 ErrPrint("[%lf] pkgname: %s, id: %s, content: %s, "
1035 "pd_w: %d, pd_h: %d, lb_w: %d, lb_h: %d, "
1036 "cluster: %s, category: %s, lb_fname: \"%s\", pd_fname: \"%s\", "
1037 "auto_launch: %s, priority: %lf, size_list: %d, user: %d, pinup: %d, "
1038 "lb_type: %d, pd_type: %d, period: %lf, title: [%s], is_pinned_up: %d\n",
1039 timestamp, pkgname, id, content,
1040 pd_w, pd_h, lb_w, lb_h,
1041 cluster, category, lb_fname, pd_fname,
1042 auto_launch, priority, size_list, user, pinup_supported,
1043 lb_type, pd_type, period, title, is_pinned_up);
1045 handler = lb_find_livebox_by_timestamp(timestamp);
1047 handler = lb_new_livebox(pkgname, id, timestamp);
1049 ErrPrint("Failed to create a new livebox\n");
1050 ret = LB_STATUS_ERROR_FAULT;
1054 old_state = handler->state;
1056 if (handler->state != CREATE) {
1057 if (handler->state != DELETE) {
1060 * This is not possible!!!
1062 ErrPrint("Invalid handler\n");
1063 ret = LB_STATUS_ERROR_INVALID;
1069 * After get the delete states,
1070 * call the create callback with deleted result.
1074 old_state = handler->state;
1077 ErrPrint("Already created: timestamp[%lf] "
1078 "pkgname[%s], id[%s] content[%s] "
1079 "cluster[%s] category[%s] lb_fname[%s] pd_fname[%s]\n",
1080 timestamp, pkgname, id,
1081 content, cluster, category,
1082 lb_fname, pd_fname);
1084 ret = LB_STATUS_ERROR_ALREADY;
1088 lb_set_id(handler, id);
1091 lb_set_size(handler, lb_w, lb_h);
1092 handler->lb.type = lb_type;
1093 handler->is_pinned_up = is_pinned_up;
1098 case _LB_TYPE_SCRIPT:
1099 case _LB_TYPE_BUFFER:
1100 if (!strlen(lb_fname)) {
1103 (void)lb_set_lb_fb(handler, lb_fname);
1105 ret = fb_sync(lb_get_lb_fb(handler));
1107 ErrPrint("Failed to do sync FB (%s - %s)\n", pkgname, util_basename(util_uri_to_path(id)));
1111 lb_set_text_lb(handler);
1117 handler->pd.type = pd_type;
1118 lb_set_pdsize(handler, pd_w, pd_h);
1119 lb_set_default_pdsize(handler, pd_w, pd_h);
1121 case _PD_TYPE_SCRIPT:
1122 case _PD_TYPE_BUFFER:
1123 if (!strlen(pd_fname)) {
1127 lb_set_pd_fb(handler, pd_fname);
1128 ret = fb_sync(lb_get_pd_fb(handler));
1130 ErrPrint("Failed to do sync FB (%s - %s)\n", pkgname, util_basename(util_uri_to_path(id)));
1134 lb_set_text_pd(handler);
1140 lb_set_priority(handler, priority);
1142 lb_set_size_list(handler, size_list);
1143 lb_set_group(handler, cluster, category);
1145 lb_set_content(handler, content);
1146 lb_set_title(handler, title);
1148 lb_set_user(handler, user);
1150 lb_set_auto_launch(handler, auto_launch);
1151 lb_set_pinup(handler, pinup_supported);
1153 lb_set_period(handler, period);
1157 if (handler->state == CREATE) {
1160 * These callback can change the handler->state.
1161 * So we have to use the "old_state" which stored state before call these callbacks
1164 if (handler->created_cb) {
1168 cb = handler->created_cb;
1169 cbdata = handler->created_cbdata;
1171 handler->created_cb = NULL;
1172 handler->created_cbdata = NULL;
1174 cb(handler, ret, cbdata);
1176 lb_invoke_event_handler(handler, LB_EVENT_CREATED);
1181 if (ret == 0 && old_state == DELETE) {
1182 lb_send_delete(handler, handler->created_cb, handler->created_cbdata);
1186 * handler->created_cb = NULL;
1187 * handler->created_cbdata = NULL;
1189 * Do not clear this to use this from the deleted event callback.
1190 * if this value is not cleared when the deleted event callback check it,
1191 * it means that the created function is not called yet.
1192 * Then the call the deleted event callback with LB_STATUS_ERROR_CANCEL errno.
1199 static struct method s_table[] = {
1201 .cmd = "lb_updated", /* pkgname, id, lb_w, lb_h, priority, ret */
1202 .handler = master_lb_updated,
1205 .cmd = "pd_updated", /* pkgname, id, descfile, pd_w, pd_h, ret */
1206 .handler = master_pd_updated,
1209 .cmd = "pd_created",
1210 .handler = master_pd_created,
1213 .cmd = "pd_destroyed",
1214 .handler = master_pd_destroyed,
1217 .cmd = "fault_package", /* pkgname, id, function, ret */
1218 .handler = master_fault_package,
1221 .cmd = "deleted", /* pkgname, id, timestamp, ret */
1222 .handler = master_deleted,
1225 .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 */
1226 .handler = master_created,
1229 .cmd = "group_changed",
1230 .handler = master_group_changed,
1233 .cmd = "period_changed",
1234 .handler = master_period_changed,
1237 .cmd = "size_changed",
1238 .handler = master_size_changed,
1242 .handler = master_pinup,
1246 .handler = master_hold_scroll,
1250 .cmd = "update_mode",
1251 .handler = master_update_mode,
1255 .cmd = "lb_update_begin",
1256 .handler = master_lb_update_begin,
1259 .cmd = "lb_update_end",
1260 .handler = master_lb_update_end,
1264 .cmd = "pd_update_begin",
1265 .handler = master_pd_update_begin,
1268 .cmd = "pd_update_end",
1269 .handler = master_pd_update_end,
1273 .cmd = "access_status",
1274 .handler = master_access_status,
1283 static void acquire_cb(struct livebox *handler, const struct packet *result, void *data)
1286 DbgPrint("Result packet is not valid\n");
1290 if (packet_get(result, "i", &ret) != 1) {
1291 ErrPrint("Invalid argument\n");
1293 DbgPrint("Acquire returns: %d\n", ret);
1300 static inline int make_connection(void)
1302 struct packet *packet;
1305 DbgPrint("Let's making connection!\n");
1307 s_info.fd = com_core_packet_client_init(client_addr(), 0, s_table);
1308 if (s_info.fd < 0) {
1309 ErrPrint("Try this again later\n");
1310 return LB_STATUS_ERROR_IO;
1313 packet = packet_create("acquire", "d", util_timestamp());
1315 com_core_packet_client_fini(s_info.fd);
1317 return LB_STATUS_ERROR_FAULT;
1320 ret = master_rpc_async_request(NULL, packet, 1, acquire_cb, NULL);
1322 ErrPrint("Master RPC returns %d\n", ret);
1323 com_core_packet_client_fini(s_info.fd);
1325 return LB_STATUS_ERROR_IO;
1328 return LB_STATUS_SUCCESS;
1331 static int connected_cb(int handle, void *data)
1333 master_rpc_check_and_fire_consumer();
1337 static void master_started_cb(keynode_t *node, void *data)
1341 if (vconf_get_bool(VCONFKEY_MASTER_STARTED, &state) < 0) {
1342 ErrPrint("Unable to get [%s]\n", VCONFKEY_MASTER_STARTED);
1345 DbgPrint("Master state: %d\n", state);
1346 if (state == 1 && make_connection() == LB_STATUS_SUCCESS) {
1348 ret = vconf_ignore_key_changed(VCONFKEY_MASTER_STARTED, master_started_cb);
1349 DbgPrint("master_started vconf key de-registered [%d]\n", ret);
1353 static gboolean timeout_cb(gpointer data)
1355 if (vconf_notify_key_changed(VCONFKEY_MASTER_STARTED, master_started_cb, NULL) < 0) {
1356 ErrPrint("Failed to add vconf for monitoring service state\n");
1358 DbgPrint("vconf event callback is registered\n");
1361 master_started_cb(NULL, NULL);
1363 s_info.timer_id = 0;
1367 static int disconnected_cb(int handle, void *data)
1369 if (s_info.fd != handle) {
1370 /*!< This handle is not my favor */
1374 s_info.fd = -1; /*!< Disconnected */
1376 master_rpc_clear_all_request();
1377 lb_invoke_fault_handler(LB_FAULT_PROVIDER_DISCONNECTED, MASTER_PKGNAME, "default", "disconnected");
1381 /* Try to reconnect after 1 sec later */
1382 if (!s_info.timer_id) {
1383 DbgPrint("Reconnecting timer is added\n");
1384 s_info.timer_id = g_timeout_add(1000, timeout_cb, NULL);
1385 if (s_info.timer_id == 0) {
1386 ErrPrint("Unable to add reconnecting timer\n");
1390 ErrPrint("Reconnecting timer is already exists\n");
1396 int client_init(void)
1398 s_info.client_addr = vconf_get_str(VCONFKEY_MASTER_CLIENT_ADDR);
1399 if (!s_info.client_addr) {
1400 s_info.client_addr = strdup(CLIENT_SOCKET);
1401 if (!s_info.client_addr) {
1402 ErrPrint("Heap: %s\n", strerror(errno));
1407 (void)file_service_init();
1409 DbgPrint("Server Address: %s\n", s_info.client_addr);
1411 com_core_add_event_callback(CONNECTOR_DISCONNECTED, disconnected_cb, NULL);
1412 com_core_add_event_callback(CONNECTOR_CONNECTED, connected_cb, NULL);
1413 if (vconf_notify_key_changed(VCONFKEY_MASTER_STARTED, master_started_cb, NULL) < 0) {
1414 ErrPrint("Failed to add vconf for service state\n");
1416 DbgPrint("vconf event callback is registered\n");
1419 master_started_cb(NULL, NULL);
1428 const char *client_addr(void)
1430 return s_info.client_addr;
1433 int client_fini(void)
1437 (void)file_service_fini();
1439 ret = vconf_ignore_key_changed(VCONFKEY_MASTER_STARTED, master_started_cb);
1441 DbgPrint("Ignore vconf key: %d\n", ret);
1444 com_core_del_event_callback(CONNECTOR_DISCONNECTED, disconnected_cb, NULL);
1445 com_core_del_event_callback(CONNECTOR_CONNECTED, connected_cb, NULL);
1446 com_core_packet_client_fini(s_info.fd);
1448 free(s_info.client_addr);
1449 s_info.client_addr = NULL;
1450 return LB_STATUS_SUCCESS;