2 * Copyright 2012 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>
33 #include "livebox_internal.h"
34 #include "desc_parser.h"
37 #include "master_rpc.h"
39 #include "critical_log.h"
41 static inline void make_connection(void);
51 static struct packet *master_fault_package(pid_t pid, int handle, const struct packet *packet)
57 if (packet_get(packet, "sss", &pkgname, &id, &function) != 3) {
58 ErrPrint("Invalid arguments\n");
62 master_rpc_clear_fault_package(pkgname);
63 lb_invoke_fault_handler(LB_FAULT_DEACTIVATED, pkgname, id, function);
64 DbgPrint("%s(%s) is deactivated\n", pkgname, id);
68 static struct packet *master_pinup(pid_t pid, int handle, const struct packet *packet)
73 struct livebox *handler;
78 ret = packet_get(packet, "iisss", &ret, &pinup, &pkgname, &id, &content);
80 ErrPrint("Invalid argument\n");
85 handler = lb_find_livebox(pkgname, 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));
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;
109 lb_invoke_event_handler(handler, LB_EVENT_PINUP_CHANGED);
117 static struct packet *master_deleted(pid_t pid, int handle, const struct packet *packet)
122 struct livebox *handler;
124 if (packet_get(packet, "ssd", &pkgname, &id, ×tamp) != 3) {
125 ErrPrint("Invalid arguemnt\n");
129 handler = lb_find_livebox_by_timestamp(timestamp);
133 * This can be happens only if the user delete a livebox
134 * right after create it before receive created event.
139 /*!< Check validity of this "handler" */
140 if (handler->state != CREATE) {
141 if (handler->state != DELETE) {
144 * This is not possible
146 CRITICAL_LOG("Already deleted handler (%s - %s)\n", pkgname, id);
151 if (handler->created_cb) {
155 * "if (handler->id == NULL)"
157 * The instance is not created yet.
158 * But the master forcely destroy it and send destroyed event to this
159 * without the created event.
161 * It could be destroyed when a slave has critical error(fault)
162 * before creating an instance successfully.
164 if (handler->created_cb == handler->deleted_cb) {
165 if (handler->created_cbdata != handler->deleted_cbdata)
166 DbgPrint("cb is same but cbdata is different (%s - %s)\n", pkgname, id);
168 handler->deleted_cb = NULL;
169 handler->deleted_cbdata = NULL;
172 DbgPrint("Call the created cb with -ECANCELED\n");
173 handler->created_cb(handler, -ECANCELED, handler->created_cbdata);
174 handler->created_cb = NULL;
175 handler->created_cbdata = NULL;
176 } else if (handler->id) {
177 if (handler->deleted_cb) {
178 DbgPrint("Call the deleted cb\n");
179 handler->deleted_cb(handler, 0, handler->deleted_cbdata);
181 handler->deleted_cb = NULL;
182 handler->deleted_cbdata = NULL;
184 DbgPrint("Call the lb,deleted\n");
185 lb_invoke_event_handler(handler, LB_EVENT_DELETED);
189 DbgPrint("[%p] %s(%s) is deleted\n", handler, pkgname, id);
191 /* Just try to delete it, if a user didn't remove it from the live box list */
198 static struct packet *master_lb_updated(pid_t pid, int handle, const struct packet *packet)
205 struct livebox *handler;
211 ret = packet_get(packet, "sssiidss",
213 &fbfile, &lb_w, &lb_h,
214 &priority, &content, &title);
216 ErrPrint("Invalid argument\n");
221 handler = lb_find_livebox(pkgname, id);
227 if (handler->state != CREATE) {
230 * Already deleted by the user.
231 * Don't try to notice anything with this, Just ignore all events
232 * Beacuse the user doesn't wants know about this anymore
238 lb_set_priority(handler, priority);
239 lb_set_content(handler, content);
240 lb_set_title(handler, title);
242 if (lb_text_lb(handler)) {
243 lb_set_size(handler, lb_w, lb_h);
244 ret = parse_desc(handler, livebox_filename(handler), 0);
247 * DESC parser will call the "text event callback".
250 } else if (lb_get_lb_fb(handler)) {
251 lb_set_size(handler, lb_w, lb_h);
252 lb_set_lb_fb(handler, fbfile);
253 ret = fb_sync(lb_get_lb_fb(handler));
255 ErrPrint("Failed to do sync FB (%s - %s)\n", pkgname, util_basename(util_uri_to_path(id)));
257 lb_set_size(handler, lb_w, lb_h);
262 lb_invoke_event_handler(handler, LB_EVENT_LB_UPDATED);
268 static struct packet *master_pd_created(pid_t pid, int handle, const struct packet *packet)
270 struct livebox *handler;
279 ret = packet_get(packet, "sssiii", &pkgname, &id, &buf_id, &width, &height, &status);
281 ErrPrint("Invalid argument\n");
285 handler = lb_find_livebox(pkgname, id);
291 if (handler->state != CREATE) {
296 lb_set_pdsize(handler, width, height);
297 if (lb_text_pd(handler)) {
298 DbgPrint("Text TYPE does not need to handle this\n");
300 (void)lb_set_pd_fb(handler, buf_id);
301 ret = fb_sync(lb_get_pd_fb(handler));
303 ErrPrint("Failed to do sync FB (%s - %s)\n",
305 util_basename(util_uri_to_path(id)));
309 handler->is_pd_created = (status == 0);
311 if (handler->pd_created_cb) {
312 DbgPrint("pd_created_cb (%s) - %d\n", buf_id, status);
313 handler->pd_created_cb(handler, status, handler->pd_created_cbdata);
315 handler->pd_created_cb = NULL;
316 handler->pd_created_cbdata = NULL;
317 } else if (status == 0) {
318 DbgPrint("LB_EVENT_PD_CREATED (%s) - %d\n", buf_id, status);
319 lb_invoke_event_handler(handler, LB_EVENT_PD_CREATED);
327 static struct packet *master_pd_destroyed(pid_t pid, int handle, const struct packet *packet)
329 struct livebox *handler;
335 ret = packet_get(packet, "ssi", &pkgname, &id, &status);
337 ErrPrint("Invalid argument\n");
341 handler = lb_find_livebox(pkgname, id);
347 if (handler->state != CREATE) {
352 handler->is_pd_created = 0;
354 if (handler->pd_destroyed_cb) {
355 DbgPrint("Invoke the PD Destroyed CB\n");
356 handler->pd_destroyed_cb(handler, status, handler->pd_destroyed_cbdata);
358 handler->pd_destroyed_cb = NULL;
359 handler->pd_destroyed_cbdata = NULL;
360 } else if (status == 0) {
361 DbgPrint("Invoke the LB_EVENT_PD_DESTROYED event\n");
362 lb_invoke_event_handler(handler, LB_EVENT_PD_DESTROYED);
370 static struct packet *master_pd_updated(pid_t pid, int handle, const struct packet *packet)
374 const char *descfile;
377 struct livebox *handler;
381 ret = packet_get(packet, "ssssii",
386 ErrPrint("Invalid argument\n");
391 handler = lb_find_livebox(pkgname, id);
397 if (handler->state != CREATE) {
400 * This handler is already deleted by the user.
401 * So don't try to notice anything about this anymore.
402 * Just ignore all events.
408 lb_set_pdsize(handler, pd_w, pd_h);
410 if (lb_text_pd(handler)) {
411 ret = parse_desc(handler, descfile, 1);
413 (void)lb_set_pd_fb(handler, fbfile);
415 ret = fb_sync(lb_get_pd_fb(handler));
417 ErrPrint("Failed to do sync FB (%s - %s)\n",
419 util_basename(util_uri_to_path(id)));
423 lb_invoke_event_handler(handler, LB_EVENT_PD_UPDATED);
431 static struct packet *master_size_changed(pid_t pid, int handle, const struct packet *packet)
433 struct livebox *handler;
443 ErrPrint("Invalid packet\n");
448 ret = packet_get(packet, "ssiiii", &pkgname, &id, &is_pd, &w, &h, &status);
450 ErrPrint("Invalid argument\n");
455 DbgPrint("Size is changed: %dx%d (%s)\n", w, h, id);
457 handler = lb_find_livebox(pkgname, id);
459 ErrPrint("Livebox(%s - %s) is not found\n", pkgname, id);
464 if (handler->state != CREATE) {
465 ErrPrint("Hander is not created yet\n");
471 DbgPrint("PD is resized\n");
474 * PD is not able to resized by the client.
475 * PD is only can be managed by the provider.
476 * So the PD has no private resized event handler.
477 * Notify it via global event handler only.
480 lb_set_pdsize(handler, w, h);
481 lb_invoke_event_handler(handler, LB_EVENT_PD_SIZE_CHANGED);
483 ErrPrint("This is not possible. PD Size is changed but the return value is not ZERO\n");
487 DbgPrint("LB is successfully resized (%dx%d)\n", w, h);
488 lb_set_size(handler, w, h);
492 * If there is a created LB FB,
495 if (lb_get_lb_fb(handler))
496 lb_set_lb_fb(handler, id);
500 * I cannot believe client.
501 * So I added some log before & after call the user callback.
503 if (handler->size_changed_cb) {
504 handler->size_changed_cb(handler, status, handler->size_cbdata);
506 handler->size_changed_cb = NULL;
507 handler->size_cbdata = NULL;
509 lb_invoke_event_handler(handler, LB_EVENT_LB_SIZE_CHANGED);
512 DbgPrint("LB is not resized: %dx%d (%d)\n", w, h, status);
513 if (handler->size_changed_cb) {
514 handler->size_changed_cb(handler, status, handler->size_cbdata);
516 handler->size_changed_cb = NULL;
517 handler->size_cbdata = NULL;
526 static struct packet *master_period_changed(pid_t pid, int handle, const struct packet *packet)
528 struct livebox *handler;
535 ret = packet_get(packet, "idss", &status, &period, &pkgname, &id);
537 ErrPrint("Invalid argument\n");
542 handler = lb_find_livebox(pkgname, id);
544 ErrPrint("Livebox(%s - %s) is not found\n", pkgname, id);
549 if (handler->state != CREATE) {
554 DbgPrint("Update period is changed? %lf (%d)\n", period, status);
556 lb_set_period(handler, period);
558 if (handler->period_changed_cb) {
559 handler->period_changed_cb(handler, status, handler->group_cbdata);
561 handler->period_changed_cb = NULL;
562 handler->period_cbdata = NULL;
564 lb_invoke_event_handler(handler, LB_EVENT_PERIOD_CHANGED);
573 static struct packet *master_group_changed(pid_t pid, int handle, const struct packet *packet)
575 struct livebox *handler;
580 const char *category;
583 ret = packet_get(packet, "ssiss", &pkgname, &id, &status, &cluster, &category);
585 ErrPrint("Invalid argument\n");
590 handler = lb_find_livebox(pkgname, id);
596 if (handler->state != CREATE) {
599 * Do no access this handler,
600 * You cannot believe this handler anymore.
606 DbgPrint("Group is changed? [%s] / [%s] (%d)\n", cluster, category, status);
608 lb_set_group(handler, cluster, category);
610 if (handler->group_changed_cb) {
611 handler->group_changed_cb(handler, status, handler->group_cbdata);
613 handler->group_changed_cb = NULL;
614 handler->group_cbdata = NULL;
616 lb_invoke_event_handler(handler, LB_EVENT_GROUP_CHANGED);
625 static struct packet *master_created(pid_t pid, int handle, const struct packet *packet)
627 struct livebox *handler;
638 const char *category;
639 const char *lb_fname;
640 const char *pd_fname;
644 const char *auto_launch;
649 enum lb_type lb_type;
650 enum pd_type pd_type;
654 int old_state = DESTROYED;
658 ret = packet_get(packet, "dsssiiiisssssdiiiiidsi",
660 &pkgname, &id, &content,
661 &lb_w, &lb_h, &pd_w, &pd_h,
662 &cluster, &category, &lb_fname, &pd_fname,
663 &auto_launch, &priority, &size_list, &user, &pinup_supported,
664 &lb_type, &pd_type, &period, &title, &is_pinned_up);
666 ErrPrint("Invalid argument\n");
671 ErrPrint("[%lf] pkgname: %s, id: %s, content: %s, "
672 "pd_w: %d, pd_h: %d, lb_w: %d, lb_h: %d, "
673 "cluster: %s, category: %s, lb_fname: \"%s\", pd_fname: \"%s\", "
674 "auto_launch: %s, priority: %lf, size_list: %d, user: %d, pinup: %d, "
675 "lb_type: %d, pd_type: %d, period: %lf, title: [%s], is_pinned_up: %d\n",
676 timestamp, pkgname, id, content,
677 pd_w, pd_h, lb_w, lb_h,
678 cluster, category, lb_fname, pd_fname,
679 auto_launch, priority, size_list, user, pinup_supported,
680 lb_type, pd_type, period, title, is_pinned_up);
682 handler = lb_find_livebox_by_timestamp(timestamp);
684 handler = lb_new_livebox(pkgname, id, timestamp);
686 ErrPrint("Failed to create a new livebox\n");
691 old_state = handler->state;
693 if (handler->state != CREATE) {
694 if (handler->state != DELETE) {
697 * This is not possible!!!
699 ErrPrint("Invalid handler\n");
706 * After get the delete states,
707 * call the create callback with deleted result.
711 old_state = handler->state;
714 ErrPrint("Already created: timestamp[%lf] "
715 "pkgname[%s], id[%s] content[%s] "
716 "cluster[%s] category[%s] lb_fname[%s] pd_fname[%s]\n",
717 timestamp, pkgname, id,
718 content, cluster, category,
725 lb_set_id(handler, id);
728 lb_set_size(handler, lb_w, lb_h);
729 handler->lb.type = lb_type;
730 handler->is_pinned_up = is_pinned_up;
735 case _LB_TYPE_SCRIPT:
736 case _LB_TYPE_BUFFER:
737 if (!strlen(lb_fname))
739 lb_set_lb_fb(handler, lb_fname);
740 ret = fb_sync(lb_get_lb_fb(handler));
742 ErrPrint("Failed to do sync FB (%s - %s)\n", pkgname, util_basename(util_uri_to_path(id)));
745 lb_set_text_lb(handler);
751 handler->pd.type = pd_type;
752 lb_set_pdsize(handler, pd_w, pd_h);
753 lb_set_default_pdsize(handler, pd_w, pd_h);
755 case _PD_TYPE_SCRIPT:
756 case _PD_TYPE_BUFFER:
757 if (!strlen(pd_fname))
760 lb_set_pd_fb(handler, pd_fname);
761 ret = fb_sync(lb_get_pd_fb(handler));
763 ErrPrint("Failed to do sync FB (%s - %s)\n", pkgname, util_basename(util_uri_to_path(id)));
766 lb_set_text_pd(handler);
772 lb_set_priority(handler, priority);
774 lb_set_size_list(handler, size_list);
775 lb_set_group(handler, cluster, category);
777 lb_set_content(handler, content);
778 lb_set_title(handler, title);
780 lb_set_user(handler, user);
782 lb_set_auto_launch(handler, auto_launch);
783 lb_set_pinup(handler, pinup_supported);
785 lb_set_period(handler, period);
789 if (handler->state == CREATE) {
792 * These callback can change the handler->state.
793 * So we have to use the "old_state" which stored state before call these callbacks
796 if (handler->created_cb) {
797 DbgPrint("Invoke the created_cb\n");
798 handler->created_cb(handler, ret, handler->created_cbdata);
800 handler->created_cb = NULL;
801 handler->created_cbdata = NULL;
803 DbgPrint("Invoke the lb,created\n");
804 lb_invoke_event_handler(handler, LB_EVENT_CREATED);
809 if (ret == 0 && old_state == DELETE) {
810 DbgPrint("Send the delete request\n");
811 lb_send_delete(handler, handler->created_cb, handler->created_cbdata);
815 * handler->created_cb = NULL;
816 * handler->created_cbdata = NULL;
818 * Do not clear this to use this from the deleted event callback.
819 * if this value is not cleared when the deleted event callback check it,
820 * it means that the created function is not called yet.
821 * Then the call the deleted event callback with -ECANCELED errno.
828 static struct method s_table[] = {
830 .cmd = "lb_updated", /* pkgname, id, lb_w, lb_h, priority, ret */
831 .handler = master_lb_updated,
834 .cmd = "pd_updated", /* pkgname, id, descfile, pd_w, pd_h, ret */
835 .handler = master_pd_updated,
839 .handler = master_pd_created,
842 .cmd = "pd_destroyed",
843 .handler = master_pd_destroyed,
846 .cmd = "fault_package", /* pkgname, id, function, ret */
847 .handler = master_fault_package,
850 .cmd = "deleted", /* pkgname, id, timestamp, ret */
851 .handler = master_deleted,
854 .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 */
855 .handler = master_created,
858 .cmd = "group_changed",
859 .handler = master_group_changed,
862 .cmd = "period_changed",
863 .handler = master_period_changed,
866 .cmd = "size_changed",
867 .handler = master_size_changed,
871 .handler = master_pinup,
879 static void acquire_cb(struct livebox *handler, const struct packet *result, void *data)
882 DbgPrint("Result packet is not valid\n");
886 if (packet_get(result, "i", &ret) != 1)
887 ErrPrint("Invalid argument\n");
889 DbgPrint("Acquire returns: %d\n", ret);
895 static gboolean connector_cb(gpointer user_data)
897 s_info.reconnector = 0;
900 DbgPrint("Connection is already made\n");
908 static inline void make_connection(void)
910 struct packet *packet;
913 DbgPrint("Let's making connection!\n");
915 s_info.fd = com_core_packet_client_init(CLIENT_SOCKET, 0, s_table);
917 /*!< After 10 secs later, try to connect again */
918 s_info.reconnector = g_timeout_add(RECONNECT_PERIOD, connector_cb, NULL);
919 if (s_info.reconnector == 0)
920 ErrPrint("Failed to fire the reconnector\n");
922 ErrPrint("Try this again A sec later\n");
926 packet = packet_create("acquire", "d", util_timestamp());
928 com_core_packet_client_fini(s_info.fd);
933 ret = master_rpc_async_request(NULL, packet, 1, acquire_cb, NULL);
935 ErrPrint("Master RPC returns %d\n", ret);
936 com_core_packet_client_fini(s_info.fd);
942 static int connected_cb(int handle, void *data)
944 master_rpc_check_and_fire_consumer();
948 static int disconnected_cb(int handle, void *data)
950 if (s_info.fd != handle) {
951 /*!< This handle is not my favor */
955 s_info.fd = -1; /*!< Disconnected */
957 if (s_info.reconnector > 0) {
958 DbgPrint("Reconnector already fired\n");
962 /*!< After 10 secs later, try to connect again */
963 s_info.reconnector = g_timeout_add(RECONNECT_PERIOD, connector_cb, NULL);
964 if (s_info.reconnector == 0) {
965 ErrPrint("Failed to fire the reconnector\n");
969 master_rpc_clear_all_request();
970 lb_invoke_fault_handler(LB_FAULT_PROVIDER_DISCONNECTED, MASTER_PKGNAME, "default", "disconnected");
976 int client_init(void)
978 com_core_add_event_callback(CONNECTOR_DISCONNECTED, disconnected_cb, NULL);
979 com_core_add_event_callback(CONNECTOR_CONNECTED, connected_cb, NULL);
989 const char *client_addr(void)
991 return CLIENT_SOCKET;
994 int client_fini(void)
996 com_core_packet_client_fini(s_info.fd);
997 com_core_del_event_callback(CONNECTOR_DISCONNECTED, disconnected_cb, NULL);
998 com_core_del_event_callback(CONNECTOR_CONNECTED, connected_cb, NULL);