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_pinup(pid_t pid, int handle, const struct packet *packet)
74 struct livebox *handler;
79 ret = packet_get(packet, "iisss", &ret, &pinup, &pkgname, &id, &content);
81 ErrPrint("Invalid argument\n");
85 handler = lb_find_livebox(pkgname, id);
87 ErrPrint("Instance (%s) is not exists\n", id);
92 new_content = strdup(content);
94 free(handler->content);
95 handler->content = new_content;
96 handler->is_pinned_up = pinup;
98 ErrPrint("Heap: %s\n", strerror(errno));
99 ret = LB_STATUS_ERROR_MEMORY;
103 if (handler->pinup_cb) {
104 handler->pinup_cb(handler, ret, handler->pinup_cbdata);
106 handler->pinup_cb = NULL; /*!< Reset pinup cb */
107 handler->pinup_cbdata = NULL;
108 } else if (ret == 0) {
109 lb_invoke_event_handler(handler, LB_EVENT_PINUP_CHANGED);
116 static struct packet *master_deleted(pid_t pid, int handle, const struct packet *packet)
121 struct livebox *handler;
123 if (packet_get(packet, "ssd", &pkgname, &id, ×tamp) != 3) {
124 ErrPrint("Invalid arguemnt\n");
128 handler = lb_find_livebox_by_timestamp(timestamp);
132 * This can be happens only if the user delete a livebox
133 * right after create it before receive created event.
138 /*!< Check validity of this "handler" */
139 if (handler->state != CREATE) {
140 if (handler->state != DELETE) {
143 * This is not possible
145 CRITICAL_LOG("Already deleted handler (%s - %s)\n", pkgname, id);
150 if (handler->created_cb) {
154 * "if (handler->id == NULL)"
156 * The instance is not created yet.
157 * But the master forcely destroy it and send destroyed event to this
158 * without the created event.
160 * It could be destroyed when a slave has critical error(fault)
161 * before creating an instance successfully.
163 if (handler->created_cb == handler->deleted_cb) {
164 if (handler->created_cbdata != handler->deleted_cbdata)
165 DbgPrint("cb is same but cbdata is different (%s - %s)\n", pkgname, id);
167 handler->deleted_cb = NULL;
168 handler->deleted_cbdata = NULL;
171 DbgPrint("Call the created cb with LB_STATUS_ERROR_CANCEL\n");
172 handler->created_cb(handler, LB_STATUS_ERROR_CANCEL, handler->created_cbdata);
173 handler->created_cb = NULL;
174 handler->created_cbdata = NULL;
175 } else if (handler->id) {
176 if (handler->deleted_cb) {
177 DbgPrint("Call the deleted cb\n");
178 handler->deleted_cb(handler, LB_STATUS_SUCCESS, handler->deleted_cbdata);
180 handler->deleted_cb = NULL;
181 handler->deleted_cbdata = NULL;
183 DbgPrint("Call the lb,deleted\n");
184 lb_invoke_event_handler(handler, LB_EVENT_DELETED);
188 DbgPrint("[%p] %s(%s) is deleted\n", handler, pkgname, id);
190 /* Just try to delete it, if a user didn't remove it from the live box list */
197 static struct packet *master_lb_updated(pid_t pid, int handle, const struct packet *packet)
204 struct livebox *handler;
210 ret = packet_get(packet, "sssiidss",
212 &fbfile, &lb_w, &lb_h,
213 &priority, &content, &title);
215 ErrPrint("Invalid argument\n");
219 handler = lb_find_livebox(pkgname, id);
221 ErrPrint("instance(%s) is not exists\n", id);
225 if (handler->state != CREATE) {
228 * Already deleted by the user.
229 * Don't try to notice anything with this, Just ignore all events
230 * Beacuse the user doesn't wants know about this anymore
232 DbgPrint("(%s) is not exists, but updated\n", id);
236 lb_set_priority(handler, priority);
237 lb_set_content(handler, content);
238 lb_set_title(handler, title);
240 if (lb_text_lb(handler)) {
241 lb_set_size(handler, lb_w, lb_h);
242 (void)parse_desc(handler, livebox_filename(handler), 0);
245 * DESC parser will call the "text event callback".
246 * Don't need to call global event callback in this case.
249 } else if (lb_get_lb_fb(handler)) {
250 lb_set_size(handler, lb_w, lb_h);
251 lb_set_lb_fb(handler, fbfile);
252 ret = fb_sync(lb_get_lb_fb(handler));
254 ErrPrint("Failed to do sync FB (%s - %s) (%d)\n", pkgname, util_basename(util_uri_to_path(id)), ret);
256 lb_set_size(handler, lb_w, lb_h);
261 lb_invoke_event_handler(handler, LB_EVENT_LB_UPDATED);
267 static struct packet *master_pd_created(pid_t pid, int handle, const struct packet *packet)
269 struct livebox *handler;
278 ret = packet_get(packet, "sssiii", &pkgname, &id, &buf_id, &width, &height, &status);
280 ErrPrint("Invalid argument\n");
284 handler = lb_find_livebox(pkgname, id);
286 ErrPrint("Instance(%s) is not exists\n", id);
290 if (handler->state != CREATE) {
291 ErrPrint("Instance(%s) is not created\n", id);
295 lb_set_pdsize(handler, width, height);
296 if (lb_text_pd(handler)) {
297 DbgPrint("Text TYPE does not need to handle this\n");
299 (void)lb_set_pd_fb(handler, buf_id);
300 ret = fb_sync(lb_get_pd_fb(handler));
302 ErrPrint("Failed to do sync FB (%s - %s)\n", pkgname, util_basename(util_uri_to_path(id)));
305 handler->is_pd_created = (status == 0);
307 if (handler->pd_created_cb) {
308 DbgPrint("pd_created_cb (%s) - %d\n", buf_id, status);
309 handler->pd_created_cb(handler, status, handler->pd_created_cbdata);
311 handler->pd_created_cb = NULL;
312 handler->pd_created_cbdata = NULL;
313 } else if (status == 0) {
314 DbgPrint("LB_EVENT_PD_CREATED (%s) - %d\n", buf_id, status);
315 lb_invoke_event_handler(handler, LB_EVENT_PD_CREATED);
322 static struct packet *master_pd_destroyed(pid_t pid, int handle, const struct packet *packet)
324 struct livebox *handler;
330 ret = packet_get(packet, "ssi", &pkgname, &id, &status);
332 ErrPrint("Invalid argument\n");
336 handler = lb_find_livebox(pkgname, id);
338 ErrPrint("Instance(%s) is not exists\n", id);
342 if (handler->state != CREATE) {
343 ErrPrint("Instance(%s) is not created\n", id);
347 handler->is_pd_created = 0;
349 if (handler->pd_destroyed_cb) {
350 DbgPrint("Invoke the PD Destroyed CB\n");
351 handler->pd_destroyed_cb(handler, status, handler->pd_destroyed_cbdata);
353 handler->pd_destroyed_cb = NULL;
354 handler->pd_destroyed_cbdata = NULL;
355 } else if (status == 0) {
356 DbgPrint("Invoke the LB_EVENT_PD_DESTROYED event\n");
357 lb_invoke_event_handler(handler, LB_EVENT_PD_DESTROYED);
364 static struct packet *master_pd_updated(pid_t pid, int handle, const struct packet *packet)
368 const char *descfile;
371 struct livebox *handler;
375 ret = packet_get(packet, "ssssii",
380 ErrPrint("Invalid argument\n");
384 handler = lb_find_livebox(pkgname, id);
386 ErrPrint("Instance(%s) is not exists\n", id);
390 if (handler->state != CREATE) {
393 * This handler is already deleted by the user.
394 * So don't try to notice anything about this anymore.
395 * Just ignore all events.
397 ErrPrint("Instance(%s) is not created\n", id);
401 lb_set_pdsize(handler, pd_w, pd_h);
403 if (lb_text_pd(handler)) {
404 (void)parse_desc(handler, descfile, 1);
406 (void)lb_set_pd_fb(handler, fbfile);
408 ret = fb_sync(lb_get_pd_fb(handler));
410 ErrPrint("Failed to do sync FB (%s - %s)\n", pkgname, util_basename(util_uri_to_path(id)));
412 lb_invoke_event_handler(handler, LB_EVENT_PD_UPDATED);
419 static struct packet *master_size_changed(pid_t pid, int handle, const struct packet *packet)
421 struct livebox *handler;
431 ErrPrint("Invalid packet\n");
435 ret = packet_get(packet, "ssiiii", &pkgname, &id, &is_pd, &w, &h, &status);
437 ErrPrint("Invalid argument\n");
441 DbgPrint("Size is changed: %dx%d (%s)\n", w, h, id);
443 handler = lb_find_livebox(pkgname, id);
445 ErrPrint("Livebox(%s) is not found\n", id);
449 if (handler->state != CREATE) {
450 ErrPrint("Livebox(%s) is not created yet\n", id);
455 DbgPrint("PD is resized\n");
458 * PD is not able to resized by the client.
459 * PD is only can be managed by the provider.
460 * So the PD has no private resized event handler.
461 * Notify it via global event handler only.
464 lb_set_pdsize(handler, w, h);
465 lb_invoke_event_handler(handler, LB_EVENT_PD_SIZE_CHANGED);
467 ErrPrint("This is not possible. PD Size is changed but the return value is not ZERO (%d)\n", status);
471 DbgPrint("LB is successfully resized (%dx%d)\n", w, h);
472 lb_set_size(handler, w, h);
476 * If there is a created LB FB,
479 if (lb_get_lb_fb(handler))
480 (void)lb_set_lb_fb(handler, id);
484 * I cannot believe client.
485 * So I added some log before & after call the user callback.
487 if (handler->size_changed_cb) {
488 handler->size_changed_cb(handler, status, handler->size_cbdata);
490 handler->size_changed_cb = NULL;
491 handler->size_cbdata = NULL;
493 lb_invoke_event_handler(handler, LB_EVENT_LB_SIZE_CHANGED);
496 DbgPrint("LB is not resized: %dx%d (%d)\n", w, h, status);
497 if (handler->size_changed_cb) {
498 handler->size_changed_cb(handler, status, handler->size_cbdata);
500 handler->size_changed_cb = NULL;
501 handler->size_cbdata = NULL;
510 static struct packet *master_period_changed(pid_t pid, int handle, const struct packet *packet)
512 struct livebox *handler;
519 ret = packet_get(packet, "idss", &status, &period, &pkgname, &id);
521 ErrPrint("Invalid argument\n");
525 handler = lb_find_livebox(pkgname, id);
527 ErrPrint("Livebox(%s) is not found\n", id);
531 if (handler->state != CREATE) {
532 ErrPrint("Livebox(%s) is not created\n", id);
536 DbgPrint("Update period is changed? %lf (%d)\n", period, status);
538 lb_set_period(handler, period);
540 if (handler->period_changed_cb) {
541 handler->period_changed_cb(handler, status, handler->group_cbdata);
543 handler->period_changed_cb = NULL;
544 handler->period_cbdata = NULL;
545 } else if (status == 0) {
546 lb_invoke_event_handler(handler, LB_EVENT_PERIOD_CHANGED);
553 static struct packet *master_group_changed(pid_t pid, int handle, const struct packet *packet)
555 struct livebox *handler;
560 const char *category;
563 ret = packet_get(packet, "ssiss", &pkgname, &id, &status, &cluster, &category);
565 ErrPrint("Invalid argument\n");
569 handler = lb_find_livebox(pkgname, id);
571 ErrPrint("Livebox(%s) is not exists\n", id);
575 if (handler->state != CREATE) {
578 * Do no access this handler,
579 * You cannot believe this handler anymore.
581 ErrPrint("Livebox(%s) is not created\n", id);
585 DbgPrint("Group is changed? [%s] / [%s] (%d)\n", cluster, category, status);
587 (void)lb_set_group(handler, cluster, category);
589 if (handler->group_changed_cb) {
590 handler->group_changed_cb(handler, status, handler->group_cbdata);
592 handler->group_changed_cb = NULL;
593 handler->group_cbdata = NULL;
594 } else if (status == 0) {
595 lb_invoke_event_handler(handler, LB_EVENT_GROUP_CHANGED);
602 static struct packet *master_created(pid_t pid, int handle, const struct packet *packet)
604 struct livebox *handler;
615 const char *category;
616 const char *lb_fname;
617 const char *pd_fname;
621 const char *auto_launch;
626 enum lb_type lb_type;
627 enum pd_type pd_type;
631 int old_state = DESTROYED;
635 ret = packet_get(packet, "dsssiiiisssssdiiiiidsi",
637 &pkgname, &id, &content,
638 &lb_w, &lb_h, &pd_w, &pd_h,
639 &cluster, &category, &lb_fname, &pd_fname,
640 &auto_launch, &priority, &size_list, &user, &pinup_supported,
641 &lb_type, &pd_type, &period, &title, &is_pinned_up);
643 ErrPrint("Invalid argument\n");
644 ret = LB_STATUS_ERROR_INVALID;
648 ErrPrint("[%lf] pkgname: %s, id: %s, content: %s, "
649 "pd_w: %d, pd_h: %d, lb_w: %d, lb_h: %d, "
650 "cluster: %s, category: %s, lb_fname: \"%s\", pd_fname: \"%s\", "
651 "auto_launch: %s, priority: %lf, size_list: %d, user: %d, pinup: %d, "
652 "lb_type: %d, pd_type: %d, period: %lf, title: [%s], is_pinned_up: %d\n",
653 timestamp, pkgname, id, content,
654 pd_w, pd_h, lb_w, lb_h,
655 cluster, category, lb_fname, pd_fname,
656 auto_launch, priority, size_list, user, pinup_supported,
657 lb_type, pd_type, period, title, is_pinned_up);
659 handler = lb_find_livebox_by_timestamp(timestamp);
661 handler = lb_new_livebox(pkgname, id, timestamp);
663 ErrPrint("Failed to create a new livebox\n");
664 ret = LB_STATUS_ERROR_FAULT;
668 old_state = handler->state;
670 if (handler->state != CREATE) {
671 if (handler->state != DELETE) {
674 * This is not possible!!!
676 ErrPrint("Invalid handler\n");
677 ret = LB_STATUS_ERROR_INVALID;
683 * After get the delete states,
684 * call the create callback with deleted result.
688 old_state = handler->state;
691 ErrPrint("Already created: timestamp[%lf] "
692 "pkgname[%s], id[%s] content[%s] "
693 "cluster[%s] category[%s] lb_fname[%s] pd_fname[%s]\n",
694 timestamp, pkgname, id,
695 content, cluster, category,
698 ret = LB_STATUS_ERROR_ALREADY;
702 lb_set_id(handler, id);
705 lb_set_size(handler, lb_w, lb_h);
706 handler->lb.type = lb_type;
707 handler->is_pinned_up = is_pinned_up;
712 case _LB_TYPE_SCRIPT:
713 case _LB_TYPE_BUFFER:
714 if (!strlen(lb_fname))
716 lb_set_lb_fb(handler, lb_fname);
717 ret = fb_sync(lb_get_lb_fb(handler));
719 ErrPrint("Failed to do sync FB (%s - %s)\n", pkgname, util_basename(util_uri_to_path(id)));
722 lb_set_text_lb(handler);
728 handler->pd.type = pd_type;
729 lb_set_pdsize(handler, pd_w, pd_h);
730 lb_set_default_pdsize(handler, pd_w, pd_h);
732 case _PD_TYPE_SCRIPT:
733 case _PD_TYPE_BUFFER:
734 if (!strlen(pd_fname))
737 lb_set_pd_fb(handler, pd_fname);
738 ret = fb_sync(lb_get_pd_fb(handler));
740 ErrPrint("Failed to do sync FB (%s - %s)\n", pkgname, util_basename(util_uri_to_path(id)));
743 lb_set_text_pd(handler);
749 lb_set_priority(handler, priority);
751 lb_set_size_list(handler, size_list);
752 lb_set_group(handler, cluster, category);
754 lb_set_content(handler, content);
755 lb_set_title(handler, title);
757 lb_set_user(handler, user);
759 lb_set_auto_launch(handler, auto_launch);
760 lb_set_pinup(handler, pinup_supported);
762 lb_set_period(handler, period);
766 if (handler->state == CREATE) {
769 * These callback can change the handler->state.
770 * So we have to use the "old_state" which stored state before call these callbacks
773 if (handler->created_cb) {
774 DbgPrint("Invoke the created_cb\n");
775 handler->created_cb(handler, ret, handler->created_cbdata);
777 handler->created_cb = NULL;
778 handler->created_cbdata = NULL;
780 DbgPrint("Invoke the lb,created\n");
781 lb_invoke_event_handler(handler, LB_EVENT_CREATED);
786 if (ret == 0 && old_state == DELETE) {
787 DbgPrint("Send the delete request\n");
788 lb_send_delete(handler, handler->created_cb, handler->created_cbdata);
792 * handler->created_cb = NULL;
793 * handler->created_cbdata = NULL;
795 * Do not clear this to use this from the deleted event callback.
796 * if this value is not cleared when the deleted event callback check it,
797 * it means that the created function is not called yet.
798 * Then the call the deleted event callback with LB_STATUS_ERROR_CANCEL errno.
805 static struct method s_table[] = {
807 .cmd = "lb_updated", /* pkgname, id, lb_w, lb_h, priority, ret */
808 .handler = master_lb_updated,
811 .cmd = "pd_updated", /* pkgname, id, descfile, pd_w, pd_h, ret */
812 .handler = master_pd_updated,
816 .handler = master_pd_created,
819 .cmd = "pd_destroyed",
820 .handler = master_pd_destroyed,
823 .cmd = "fault_package", /* pkgname, id, function, ret */
824 .handler = master_fault_package,
827 .cmd = "deleted", /* pkgname, id, timestamp, ret */
828 .handler = master_deleted,
831 .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 */
832 .handler = master_created,
835 .cmd = "group_changed",
836 .handler = master_group_changed,
839 .cmd = "period_changed",
840 .handler = master_period_changed,
843 .cmd = "size_changed",
844 .handler = master_size_changed,
848 .handler = master_pinup,
856 static void acquire_cb(struct livebox *handler, const struct packet *result, void *data)
859 DbgPrint("Result packet is not valid\n");
863 if (packet_get(result, "i", &ret) != 1)
864 ErrPrint("Invalid argument\n");
866 DbgPrint("Acquire returns: %d\n", ret);
872 static gboolean connector_cb(gpointer user_data)
874 s_info.reconnector = 0;
877 DbgPrint("Connection is already made\n");
885 static inline void make_connection(void)
887 struct packet *packet;
890 DbgPrint("Let's making connection!\n");
892 s_info.fd = com_core_packet_client_init(CLIENT_SOCKET, 0, s_table);
894 /*!< After 10 secs later, try to connect again */
895 s_info.reconnector = g_timeout_add(RECONNECT_PERIOD, connector_cb, NULL);
896 if (s_info.reconnector == 0)
897 ErrPrint("Failed to fire the reconnector\n");
899 ErrPrint("Try this again A sec later\n");
903 packet = packet_create("acquire", "d", util_timestamp());
905 com_core_packet_client_fini(s_info.fd);
910 ret = master_rpc_async_request(NULL, packet, 1, acquire_cb, NULL);
912 ErrPrint("Master RPC returns %d\n", ret);
913 com_core_packet_client_fini(s_info.fd);
919 static int connected_cb(int handle, void *data)
921 master_rpc_check_and_fire_consumer();
925 static int disconnected_cb(int handle, void *data)
927 if (s_info.fd != handle) {
928 /*!< This handle is not my favor */
932 s_info.fd = -1; /*!< Disconnected */
934 if (s_info.reconnector > 0) {
935 DbgPrint("Reconnector already fired\n");
939 /*!< After 10 secs later, try to connect again */
940 s_info.reconnector = g_timeout_add(RECONNECT_PERIOD, connector_cb, NULL);
941 if (s_info.reconnector == 0) {
942 ErrPrint("Failed to fire the reconnector\n");
946 master_rpc_clear_all_request();
947 lb_invoke_fault_handler(LB_FAULT_PROVIDER_DISCONNECTED, MASTER_PKGNAME, "default", "disconnected");
953 int client_init(void)
955 com_core_add_event_callback(CONNECTOR_DISCONNECTED, disconnected_cb, NULL);
956 com_core_add_event_callback(CONNECTOR_CONNECTED, connected_cb, NULL);
966 const char *client_addr(void)
968 return CLIENT_SOCKET;
971 int client_fini(void)
973 com_core_packet_client_fini(s_info.fd);
974 com_core_del_event_callback(CONNECTOR_DISCONNECTED, disconnected_cb, NULL);
975 com_core_del_event_callback(CONNECTOR_CONNECTED, connected_cb, NULL);