2 * Copyright 2013 Samsung Electronics Co., Ltd
4 * Licensed under the Flora License, Version 1.0 (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://www.tizenopensource.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 <com-core_packet.h>
29 #include <livebox-errno.h>
34 #include "livebox_internal.h"
35 #include "desc_parser.h"
38 #include "master_rpc.h"
40 #include "critical_log.h"
42 static inline void make_connection(void);
52 static struct packet *master_fault_package(pid_t pid, int handle, const struct packet *packet)
58 if (packet_get(packet, "sss", &pkgname, &id, &function) != 3) {
59 ErrPrint("Invalid arguments\n");
63 master_rpc_clear_fault_package(pkgname);
64 lb_invoke_fault_handler(LB_FAULT_DEACTIVATED, pkgname, id, function);
65 DbgPrint("%s(%s) is deactivated\n", pkgname, id);
69 static struct packet *master_hold_scroll(pid_t pid, int handle, const struct packet *packet)
71 struct livebox *handler;
77 ret = packet_get(packet, "ssi", &pkgname, &id, &seize);
79 ErrPrint("Invalid argument\n");
83 handler = lb_find_livebox(pkgname, id);
85 ErrPrint("Instance(%s) is not exists\n", id);
89 lb_invoke_event_handler(handler, seize ? LB_EVENT_HOLD_SCROLL : LB_EVENT_RELEASE_SCROLL);
95 static struct packet *master_pinup(pid_t pid, int handle, const struct packet *packet)
100 struct livebox *handler;
105 ret = packet_get(packet, "iisss", &ret, &pinup, &pkgname, &id, &content);
107 ErrPrint("Invalid argument\n");
111 handler = lb_find_livebox(pkgname, id);
113 ErrPrint("Instance (%s) is not exists\n", id);
118 new_content = strdup(content);
120 free(handler->content);
121 handler->content = new_content;
122 handler->is_pinned_up = pinup;
124 ErrPrint("Heap: %s\n", strerror(errno));
125 ret = LB_STATUS_ERROR_MEMORY;
129 if (handler->pinup_cb) {
130 handler->pinup_cb(handler, ret, handler->pinup_cbdata);
132 handler->pinup_cb = NULL; /*!< Reset pinup cb */
133 handler->pinup_cbdata = NULL;
134 } else if (ret == 0) {
135 lb_invoke_event_handler(handler, LB_EVENT_PINUP_CHANGED);
142 static struct packet *master_deleted(pid_t pid, int handle, const struct packet *packet)
147 struct livebox *handler;
149 if (packet_get(packet, "ssd", &pkgname, &id, ×tamp) != 3) {
150 ErrPrint("Invalid arguemnt\n");
154 handler = lb_find_livebox_by_timestamp(timestamp);
158 * This can be happens only if the user delete a livebox
159 * right after create it before receive created event.
164 /*!< Check validity of this "handler" */
165 if (handler->state != CREATE) {
166 if (handler->state != DELETE) {
169 * This is not possible
171 CRITICAL_LOG("Already deleted handler (%s - %s)\n", pkgname, id);
176 if (handler->created_cb) {
180 * "if (handler->id == NULL)"
182 * The instance is not created yet.
183 * But the master forcely destroy it and send destroyed event to this
184 * without the created event.
186 * It could be destroyed when a slave has critical error(fault)
187 * before creating an instance successfully.
189 if (handler->created_cb == handler->deleted_cb) {
190 if (handler->created_cbdata != handler->deleted_cbdata)
191 DbgPrint("cb is same but cbdata is different (%s - %s)\n", pkgname, id);
193 handler->deleted_cb = NULL;
194 handler->deleted_cbdata = NULL;
197 DbgPrint("Call the created cb with LB_STATUS_ERROR_CANCEL\n");
198 handler->created_cb(handler, LB_STATUS_ERROR_CANCEL, handler->created_cbdata);
199 handler->created_cb = NULL;
200 handler->created_cbdata = NULL;
201 } else if (handler->id) {
202 if (handler->deleted_cb) {
203 DbgPrint("Call the deleted cb\n");
204 handler->deleted_cb(handler, LB_STATUS_SUCCESS, handler->deleted_cbdata);
206 handler->deleted_cb = NULL;
207 handler->deleted_cbdata = NULL;
209 DbgPrint("Call the lb,deleted\n");
210 lb_invoke_event_handler(handler, LB_EVENT_DELETED);
214 DbgPrint("[%p] %s(%s) is deleted\n", handler, pkgname, id);
216 /* Just try to delete it, if a user didn't remove it from the live box list */
223 static struct packet *master_lb_updated(pid_t pid, int handle, const struct packet *packet)
230 struct livebox *handler;
236 ret = packet_get(packet, "sssiidss",
238 &fbfile, &lb_w, &lb_h,
239 &priority, &content, &title);
241 ErrPrint("Invalid argument\n");
245 handler = lb_find_livebox(pkgname, id);
247 ErrPrint("instance(%s) is not exists\n", id);
251 if (handler->state != CREATE) {
254 * Already deleted by the user.
255 * Don't try to notice anything with this, Just ignore all events
256 * Beacuse the user doesn't wants know about this anymore
258 DbgPrint("(%s) is not exists, but updated\n", id);
262 lb_set_priority(handler, priority);
263 lb_set_content(handler, content);
264 lb_set_title(handler, title);
265 lb_set_size(handler, lb_w, lb_h);
267 if (lb_text_lb(handler)) {
268 (void)parse_desc(handler, livebox_filename(handler), 0);
271 * DESC parser will call the "text event callback".
272 * Don't need to call global event callback in this case.
275 } else if (lb_get_lb_fb(handler)) {
276 lb_set_lb_fb(handler, fbfile);
277 ret = fb_sync(lb_get_lb_fb(handler));
279 ErrPrint("Failed to do sync FB (%s - %s) (%d)\n", pkgname, util_basename(util_uri_to_path(id)), ret);
285 lb_invoke_event_handler(handler, LB_EVENT_LB_UPDATED);
291 static struct packet *master_pd_created(pid_t pid, int handle, const struct packet *packet)
293 struct livebox *handler;
302 ret = packet_get(packet, "sssiii", &pkgname, &id, &buf_id, &width, &height, &status);
304 ErrPrint("Invalid argument\n");
308 handler = lb_find_livebox(pkgname, id);
310 ErrPrint("Instance(%s) is not exists\n", id);
314 if (handler->state != CREATE) {
315 ErrPrint("Instance(%s) is not created\n", id);
319 lb_set_pdsize(handler, width, height);
320 if (lb_text_pd(handler)) {
321 DbgPrint("Text TYPE does not need to handle this\n");
323 (void)lb_set_pd_fb(handler, buf_id);
324 ret = fb_sync(lb_get_pd_fb(handler));
326 ErrPrint("Failed to do sync FB (%s - %s)\n", pkgname, util_basename(util_uri_to_path(id)));
329 handler->is_pd_created = (status == 0);
331 if (handler->pd_created_cb) {
332 DbgPrint("pd_created_cb (%s) - %d\n", buf_id, status);
333 handler->pd_created_cb(handler, status, handler->pd_created_cbdata);
335 handler->pd_created_cb = NULL;
336 handler->pd_created_cbdata = NULL;
337 } else if (status == 0) {
338 DbgPrint("LB_EVENT_PD_CREATED (%s) - %d\n", buf_id, status);
339 lb_invoke_event_handler(handler, LB_EVENT_PD_CREATED);
346 static struct packet *master_pd_destroyed(pid_t pid, int handle, const struct packet *packet)
348 struct livebox *handler;
354 ret = packet_get(packet, "ssi", &pkgname, &id, &status);
356 ErrPrint("Invalid argument\n");
360 handler = lb_find_livebox(pkgname, id);
362 ErrPrint("Instance(%s) is not exists\n", id);
366 if (handler->state != CREATE) {
367 ErrPrint("Instance(%s) is not created\n", id);
371 handler->is_pd_created = 0;
373 if (handler->pd_destroyed_cb) {
374 DbgPrint("Invoke the PD Destroyed CB\n");
375 handler->pd_destroyed_cb(handler, status, handler->pd_destroyed_cbdata);
377 handler->pd_destroyed_cb = NULL;
378 handler->pd_destroyed_cbdata = NULL;
379 } else if (status == 0) {
380 DbgPrint("Invoke the LB_EVENT_PD_DESTROYED event\n");
381 lb_invoke_event_handler(handler, LB_EVENT_PD_DESTROYED);
388 static struct packet *master_pd_updated(pid_t pid, int handle, const struct packet *packet)
392 const char *descfile;
395 struct livebox *handler;
399 ret = packet_get(packet, "ssssii",
404 ErrPrint("Invalid argument\n");
408 handler = lb_find_livebox(pkgname, id);
410 ErrPrint("Instance(%s) is not exists\n", id);
414 if (handler->state != CREATE) {
417 * This handler is already deleted by the user.
418 * So don't try to notice anything about this anymore.
419 * Just ignore all events.
421 ErrPrint("Instance(%s) is not created\n", id);
425 lb_set_pdsize(handler, pd_w, pd_h);
427 if (lb_text_pd(handler)) {
428 (void)parse_desc(handler, descfile, 1);
430 (void)lb_set_pd_fb(handler, fbfile);
432 ret = fb_sync(lb_get_pd_fb(handler));
434 ErrPrint("Failed to do sync FB (%s - %s)\n", pkgname, util_basename(util_uri_to_path(id)));
436 lb_invoke_event_handler(handler, LB_EVENT_PD_UPDATED);
443 static struct packet *master_size_changed(pid_t pid, int handle, const struct packet *packet)
445 struct livebox *handler;
456 ErrPrint("Invalid packet\n");
460 ret = packet_get(packet, "sssiiii", &pkgname, &id, &fbfile, &is_pd, &w, &h, &status);
462 ErrPrint("Invalid argument\n");
466 DbgPrint("Size is changed: %dx%d (%s), fb: [%s]\n", w, h, id, fbfile);
468 handler = lb_find_livebox(pkgname, id);
470 ErrPrint("Livebox(%s) is not found\n", id);
474 if (handler->state != CREATE) {
475 ErrPrint("Livebox(%s) is not created yet\n", id);
480 DbgPrint("PD is resized\n");
483 * PD is not able to resized by the client.
484 * PD is only can be managed by the provider.
485 * So the PD has no private resized event handler.
486 * Notify it via global event handler only.
489 lb_set_pdsize(handler, w, h);
490 lb_invoke_event_handler(handler, LB_EVENT_PD_SIZE_CHANGED);
492 ErrPrint("This is not possible. PD Size is changed but the return value is not ZERO (%d)\n", status);
496 DbgPrint("LB is successfully resized (%dx%d)\n", w, h);
497 lb_set_size(handler, w, h);
501 * If there is a created LB FB,
504 if (lb_get_lb_fb(handler)) {
505 lb_set_lb_fb(handler, fbfile);
507 ret = fb_sync(lb_get_lb_fb(handler));
509 ErrPrint("Failed to do sync FB (%s - %s)\n", pkgname, util_basename(util_uri_to_path(id)));
511 /* Just update the size info only. */
516 * I cannot believe client.
517 * So I added some log before & after call the user callback.
519 if (handler->size_changed_cb) {
520 handler->size_changed_cb(handler, status, handler->size_cbdata);
522 handler->size_changed_cb = NULL;
523 handler->size_cbdata = NULL;
525 lb_invoke_event_handler(handler, LB_EVENT_LB_SIZE_CHANGED);
528 DbgPrint("LB is not resized: %dx%d (%d)\n", w, h, status);
529 if (handler->size_changed_cb) {
530 handler->size_changed_cb(handler, status, handler->size_cbdata);
532 handler->size_changed_cb = NULL;
533 handler->size_cbdata = NULL;
542 static struct packet *master_period_changed(pid_t pid, int handle, const struct packet *packet)
544 struct livebox *handler;
551 ret = packet_get(packet, "idss", &status, &period, &pkgname, &id);
553 ErrPrint("Invalid argument\n");
557 handler = lb_find_livebox(pkgname, id);
559 ErrPrint("Livebox(%s) is not found\n", id);
563 if (handler->state != CREATE) {
564 ErrPrint("Livebox(%s) is not created\n", id);
568 DbgPrint("Update period is changed? %lf (%d)\n", period, status);
570 lb_set_period(handler, period);
572 if (handler->period_changed_cb) {
573 handler->period_changed_cb(handler, status, handler->group_cbdata);
575 handler->period_changed_cb = NULL;
576 handler->period_cbdata = NULL;
577 } else if (status == 0) {
578 lb_invoke_event_handler(handler, LB_EVENT_PERIOD_CHANGED);
585 static struct packet *master_group_changed(pid_t pid, int handle, const struct packet *packet)
587 struct livebox *handler;
592 const char *category;
595 ret = packet_get(packet, "ssiss", &pkgname, &id, &status, &cluster, &category);
597 ErrPrint("Invalid argument\n");
601 handler = lb_find_livebox(pkgname, id);
603 ErrPrint("Livebox(%s) is not exists\n", id);
607 if (handler->state != CREATE) {
610 * Do no access this handler,
611 * You cannot believe this handler anymore.
613 ErrPrint("Livebox(%s) is not created\n", id);
617 DbgPrint("Group is changed? [%s] / [%s] (%d)\n", cluster, category, status);
619 (void)lb_set_group(handler, cluster, category);
621 if (handler->group_changed_cb) {
622 handler->group_changed_cb(handler, status, handler->group_cbdata);
624 handler->group_changed_cb = NULL;
625 handler->group_cbdata = NULL;
626 } else if (status == 0) {
627 lb_invoke_event_handler(handler, LB_EVENT_GROUP_CHANGED);
634 static struct packet *master_created(pid_t pid, int handle, const struct packet *packet)
636 struct livebox *handler;
647 const char *category;
648 const char *lb_fname;
649 const char *pd_fname;
653 const char *auto_launch;
658 enum lb_type lb_type;
659 enum pd_type pd_type;
663 int old_state = DESTROYED;
667 ret = packet_get(packet, "dsssiiiisssssdiiiiidsi",
669 &pkgname, &id, &content,
670 &lb_w, &lb_h, &pd_w, &pd_h,
671 &cluster, &category, &lb_fname, &pd_fname,
672 &auto_launch, &priority, &size_list, &user, &pinup_supported,
673 &lb_type, &pd_type, &period, &title, &is_pinned_up);
675 ErrPrint("Invalid argument\n");
676 ret = LB_STATUS_ERROR_INVALID;
680 ErrPrint("[%lf] pkgname: %s, id: %s, content: %s, "
681 "pd_w: %d, pd_h: %d, lb_w: %d, lb_h: %d, "
682 "cluster: %s, category: %s, lb_fname: \"%s\", pd_fname: \"%s\", "
683 "auto_launch: %s, priority: %lf, size_list: %d, user: %d, pinup: %d, "
684 "lb_type: %d, pd_type: %d, period: %lf, title: [%s], is_pinned_up: %d\n",
685 timestamp, pkgname, id, content,
686 pd_w, pd_h, lb_w, lb_h,
687 cluster, category, lb_fname, pd_fname,
688 auto_launch, priority, size_list, user, pinup_supported,
689 lb_type, pd_type, period, title, is_pinned_up);
691 handler = lb_find_livebox_by_timestamp(timestamp);
693 handler = lb_new_livebox(pkgname, id, timestamp);
695 ErrPrint("Failed to create a new livebox\n");
696 ret = LB_STATUS_ERROR_FAULT;
700 old_state = handler->state;
702 if (handler->state != CREATE) {
703 if (handler->state != DELETE) {
706 * This is not possible!!!
708 ErrPrint("Invalid handler\n");
709 ret = LB_STATUS_ERROR_INVALID;
715 * After get the delete states,
716 * call the create callback with deleted result.
720 old_state = handler->state;
723 ErrPrint("Already created: timestamp[%lf] "
724 "pkgname[%s], id[%s] content[%s] "
725 "cluster[%s] category[%s] lb_fname[%s] pd_fname[%s]\n",
726 timestamp, pkgname, id,
727 content, cluster, category,
730 ret = LB_STATUS_ERROR_ALREADY;
734 lb_set_id(handler, id);
737 lb_set_size(handler, lb_w, lb_h);
738 handler->lb.type = lb_type;
739 handler->is_pinned_up = is_pinned_up;
744 case _LB_TYPE_SCRIPT:
745 case _LB_TYPE_BUFFER:
746 if (!strlen(lb_fname))
748 lb_set_lb_fb(handler, lb_fname);
749 ret = fb_sync(lb_get_lb_fb(handler));
751 ErrPrint("Failed to do sync FB (%s - %s)\n", pkgname, util_basename(util_uri_to_path(id)));
754 lb_set_text_lb(handler);
760 handler->pd.type = pd_type;
761 lb_set_pdsize(handler, pd_w, pd_h);
762 lb_set_default_pdsize(handler, pd_w, pd_h);
764 case _PD_TYPE_SCRIPT:
765 case _PD_TYPE_BUFFER:
766 if (!strlen(pd_fname))
769 lb_set_pd_fb(handler, pd_fname);
770 ret = fb_sync(lb_get_pd_fb(handler));
772 ErrPrint("Failed to do sync FB (%s - %s)\n", pkgname, util_basename(util_uri_to_path(id)));
775 lb_set_text_pd(handler);
781 lb_set_priority(handler, priority);
783 lb_set_size_list(handler, size_list);
784 lb_set_group(handler, cluster, category);
786 lb_set_content(handler, content);
787 lb_set_title(handler, title);
789 lb_set_user(handler, user);
791 lb_set_auto_launch(handler, auto_launch);
792 lb_set_pinup(handler, pinup_supported);
794 lb_set_period(handler, period);
798 if (handler->state == CREATE) {
801 * These callback can change the handler->state.
802 * So we have to use the "old_state" which stored state before call these callbacks
805 if (handler->created_cb) {
806 DbgPrint("Invoke the created_cb\n");
807 handler->created_cb(handler, ret, handler->created_cbdata);
809 handler->created_cb = NULL;
810 handler->created_cbdata = NULL;
812 DbgPrint("Invoke the lb,created\n");
813 lb_invoke_event_handler(handler, LB_EVENT_CREATED);
818 if (ret == 0 && old_state == DELETE) {
819 DbgPrint("Send the delete request\n");
820 lb_send_delete(handler, handler->created_cb, handler->created_cbdata);
824 * handler->created_cb = NULL;
825 * handler->created_cbdata = NULL;
827 * Do not clear this to use this from the deleted event callback.
828 * if this value is not cleared when the deleted event callback check it,
829 * it means that the created function is not called yet.
830 * Then the call the deleted event callback with LB_STATUS_ERROR_CANCEL errno.
837 static struct method s_table[] = {
839 .cmd = "lb_updated", /* pkgname, id, lb_w, lb_h, priority, ret */
840 .handler = master_lb_updated,
843 .cmd = "pd_updated", /* pkgname, id, descfile, pd_w, pd_h, ret */
844 .handler = master_pd_updated,
848 .handler = master_pd_created,
851 .cmd = "pd_destroyed",
852 .handler = master_pd_destroyed,
855 .cmd = "fault_package", /* pkgname, id, function, ret */
856 .handler = master_fault_package,
859 .cmd = "deleted", /* pkgname, id, timestamp, ret */
860 .handler = master_deleted,
863 .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 */
864 .handler = master_created,
867 .cmd = "group_changed",
868 .handler = master_group_changed,
871 .cmd = "period_changed",
872 .handler = master_period_changed,
875 .cmd = "size_changed",
876 .handler = master_size_changed,
880 .handler = master_pinup,
884 .handler = master_hold_scroll,
892 static void acquire_cb(struct livebox *handler, const struct packet *result, void *data)
895 DbgPrint("Result packet is not valid\n");
899 if (packet_get(result, "i", &ret) != 1)
900 ErrPrint("Invalid argument\n");
902 DbgPrint("Acquire returns: %d\n", ret);
908 static gboolean connector_cb(gpointer user_data)
910 s_info.reconnector = 0;
913 DbgPrint("Connection is already made\n");
921 static inline void make_connection(void)
923 struct packet *packet;
926 DbgPrint("Let's making connection!\n");
928 s_info.fd = com_core_packet_client_init(CLIENT_SOCKET, 0, s_table);
930 /*!< After 10 secs later, try to connect again */
931 s_info.reconnector = g_timeout_add(RECONNECT_PERIOD, connector_cb, NULL);
932 if (s_info.reconnector == 0)
933 ErrPrint("Failed to fire the reconnector\n");
935 ErrPrint("Try this again A sec later\n");
939 packet = packet_create("acquire", "d", util_timestamp());
941 com_core_packet_client_fini(s_info.fd);
946 ret = master_rpc_async_request(NULL, packet, 1, acquire_cb, NULL);
948 ErrPrint("Master RPC returns %d\n", ret);
949 com_core_packet_client_fini(s_info.fd);
955 static int connected_cb(int handle, void *data)
957 master_rpc_check_and_fire_consumer();
961 static int disconnected_cb(int handle, void *data)
963 if (s_info.fd != handle) {
964 /*!< This handle is not my favor */
968 s_info.fd = -1; /*!< Disconnected */
970 if (s_info.reconnector > 0) {
971 DbgPrint("Reconnector already fired\n");
975 /*!< After 10 secs later, try to connect again */
976 s_info.reconnector = g_timeout_add(RECONNECT_PERIOD, connector_cb, NULL);
977 if (s_info.reconnector == 0) {
978 ErrPrint("Failed to fire the reconnector\n");
982 master_rpc_clear_all_request();
983 lb_invoke_fault_handler(LB_FAULT_PROVIDER_DISCONNECTED, MASTER_PKGNAME, "default", "disconnected");
989 int client_init(void)
991 com_core_add_event_callback(CONNECTOR_DISCONNECTED, disconnected_cb, NULL);
992 com_core_add_event_callback(CONNECTOR_CONNECTED, connected_cb, NULL);
1002 const char *client_addr(void)
1004 return CLIENT_SOCKET;
1007 int client_fini(void)
1009 com_core_packet_client_fini(s_info.fd);
1010 com_core_del_event_callback(CONNECTOR_DISCONNECTED, disconnected_cb, NULL);
1011 com_core_del_event_callback(CONNECTOR_CONNECTED, connected_cb, NULL);