Interface to provider is changed
[platform/framework/web/livebox-viewer.git] / src / client.c
1 /*
2  * Copyright 2013  Samsung Electronics Co., Ltd
3  *
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
7  *
8  * http://floralicense.org/license/
9  *
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.
15  */
16
17 #include <stdio.h>
18 #include <unistd.h>
19 #include <errno.h>
20 #include <string.h>
21 #include <stdlib.h>
22 #include <pthread.h>
23
24 #include <dlog.h>
25 #include <glib.h>
26
27 #include <vconf.h>
28 #include <vconf-keys.h>
29
30 #include <packet.h>
31 #include <com-core.h>
32 #include <com-core_packet.h>
33 #include <livebox-errno.h>
34 #include <secure_socket.h>
35
36 #include "debug.h"
37 #include "client.h"
38 #include "livebox.h"
39 #include "livebox_internal.h"
40 #include "desc_parser.h"
41 #include "fb.h"
42 #include "util.h"
43 #include "master_rpc.h"
44 #include "conf.h"
45 #include "critical_log.h"
46 #include "file_service.h"
47
48 int errno;
49
50 static struct info {
51         int fd;
52         guint timer_id;
53         char *client_addr;
54 } s_info = {
55         .fd = -1,
56         .timer_id = 0,
57         .client_addr = NULL,
58 };
59
60 static struct packet *master_fault_package(pid_t pid, int handle, const struct packet *packet)
61 {
62         const char *pkgname;
63         const char *id;
64         const char *function;
65
66         if (packet_get(packet, "sss", &pkgname, &id, &function) != 3) {
67                 ErrPrint("Invalid arguments\n");
68                 return NULL;
69         }
70
71         master_rpc_clear_fault_package(pkgname);
72         lb_invoke_fault_handler(LB_FAULT_DEACTIVATED, pkgname, id, function);
73         return NULL;
74 }
75
76 static struct packet *master_hold_scroll(pid_t pid, int handle, const struct packet *packet)
77 {
78         struct livebox *handler;
79         const char *pkgname;
80         const char *id;
81         int seize;
82         int ret;
83
84         ret = packet_get(packet, "ssi", &pkgname, &id, &seize);
85         if (ret != 3) {
86                 ErrPrint("Invalid argument\n");
87                 goto out;
88         }
89
90         handler = lb_find_livebox(pkgname, id);
91         if (!handler) {
92                 ErrPrint("Instance(%s) is not exists\n", id);
93                 goto out;
94         }
95
96         DbgPrint("HOLD: %s %d\n", id, seize);
97         lb_invoke_event_handler(handler, seize ? LB_EVENT_HOLD_SCROLL : LB_EVENT_RELEASE_SCROLL);
98
99 out:
100         return NULL;
101 }
102
103 static struct packet *master_pinup(pid_t pid, int handle, const struct packet *packet)
104 {
105         const char *pkgname;
106         const char *id;
107         const char *content;
108         struct livebox *handler;
109         char *new_content;
110         int ret;
111         int status;
112         int pinup;
113
114         ret = packet_get(packet, "iisss", &status, &pinup, &pkgname, &id, &content);
115         if (ret != 5) {
116                 ErrPrint("Invalid argument\n");
117                 goto out;
118         }
119
120         handler = lb_find_livebox(pkgname, id);
121         if (!handler) {
122                 ErrPrint("Instance (%s) is not exists\n", id);
123                 goto out;
124         }
125
126         if (status == 0) {
127                 new_content = strdup(content);
128                 if (new_content) {
129                         free(handler->content);
130                         handler->content = new_content;
131                         handler->is_pinned_up = pinup;
132                 } else {
133                         ErrPrint("Heap: %s\n", strerror(errno));
134                         status = LB_STATUS_ERROR_MEMORY;
135                 }
136         }
137
138         if (handler->pinup_cb) {
139                 ret_cb_t cb;
140                 void *cbdata;
141
142                 /* Make sure that user can call pinup API in its result callback */
143                 cb = handler->pinup_cb;
144                 cbdata = handler->pinup_cbdata;
145
146                 handler->pinup_cb = NULL;
147                 handler->pinup_cbdata = NULL;
148
149                 cb(handler, status, cbdata);
150         } else if (status == 0) {
151                 lb_invoke_event_handler(handler, LB_EVENT_PINUP_CHANGED);
152         }
153
154 out:
155         return NULL;
156 }
157
158 static struct packet *master_deleted(pid_t pid, int handle, const struct packet *packet)
159 {
160         const char *pkgname;
161         const char *id;
162         double timestamp;
163         struct livebox *handler;
164
165         if (packet_get(packet, "ssd", &pkgname, &id, &timestamp) != 3) {
166                 ErrPrint("Invalid arguemnt\n");
167                 goto out;
168         }
169
170         handler = lb_find_livebox_by_timestamp(timestamp);
171         if (!handler) {
172                 /*!
173                  * \note
174                  * This can be happens only if the user delete a livebox
175                  * right after create it before receive created event.
176                  */
177                 goto out;
178         }
179
180         /*!< Check validity of this "handler" */
181         if (handler->state != CREATE) {
182                 if (handler->state != DELETE) {
183                         /*!
184                          * \note
185                          * This is not possible
186                          */
187                         CRITICAL_LOG("Already deleted handler (%s - %s)\n", pkgname, id);
188                         return NULL;
189                 }
190         }
191
192         if (handler->created_cb) {
193                 ret_cb_t cb;
194                 void *cbdata;
195                 /*!
196                  * \note
197                  *
198                  * "if (handler->id == NULL) {"
199                  *
200                  * The instance is not created yet.
201                  * But the master forcely destroy it and send destroyed event to this
202                  * without the created event.
203                  *
204                  * It could be destroyed when a slave has critical error(fault)
205                  * before creating an instance successfully.
206                  */
207                 if (handler->created_cb == handler->deleted_cb) {
208                         if (handler->created_cbdata != handler->deleted_cbdata) {
209                                 DbgPrint("cb is same but cbdata is different (%s - %s)\n", pkgname, id);
210                         }
211
212                         handler->deleted_cb = NULL;
213                         handler->deleted_cbdata = NULL;
214                 }
215
216                 cb = handler->created_cb;
217                 cbdata = handler->created_cbdata;
218
219                 handler->created_cb = NULL;
220                 handler->created_cbdata = NULL;
221
222                 cb(handler, LB_STATUS_ERROR_CANCEL, cbdata);
223         } else if (handler->id) {
224                 if (handler->deleted_cb) {
225                         ret_cb_t cb;
226                         void *cbdata;
227
228                         cb = handler->deleted_cb;
229                         cbdata = handler->deleted_cbdata;
230
231                         handler->deleted_cb = NULL;
232                         handler->deleted_cbdata = NULL;
233
234                         cb(handler, LB_STATUS_SUCCESS, cbdata);
235                 } else {
236                         lb_invoke_event_handler(handler, LB_EVENT_DELETED);
237                 }
238         }
239
240         /* Just try to delete it, if a user didn't remove it from the live box list */
241         lb_unref(handler);
242
243 out:
244         return NULL;
245 }
246
247 static struct packet *master_lb_update_begin(pid_t pid, int handle, const struct packet *packet)
248 {
249         struct livebox *handler;
250         const char *pkgname;
251         const char *id;
252         const char *content;
253         const char *title;
254         const char *fbfile;
255         double priority;
256         int ret;
257
258         ret = packet_get(packet, "ssdsss", &pkgname, &id, &priority, &content, &title, &fbfile);
259         if (ret != 6) {
260                 ErrPrint("Invalid argument\n");
261                 goto out;
262         }
263
264         handler = lb_find_livebox(pkgname, id);
265         if (!handler) {
266                 ErrPrint("Instance[%s] is not exists\n", id);
267                 goto out;
268         }
269
270         if (handler->state != CREATE) {
271                 ErrPrint("(%s) is not created\n", id);
272                 goto out;
273         }
274
275         lb_set_priority(handler, priority);
276         lb_set_content(handler, content);
277         lb_set_title(handler, title);
278
279         /*!
280          * \NOTE
281          * Width & Height is not changed in this case.
282          * If the active update is began, the size should not be changed,
283          * And if the size is changed, the provider should finish the updating first.
284          * And then begin updating again after change its size.
285          */
286         if (lb_get_lb_fb(handler)) {
287                 lb_set_lb_fb(handler, fbfile);
288
289                 ret = fb_sync(lb_get_lb_fb(handler));
290                 if (ret != LB_STATUS_SUCCESS) {
291                         ErrPrint("Failed to do sync FB (%s - %s) (%d)\n", pkgname, fbfile, ret);
292                 } else {
293                         lb_invoke_event_handler(handler, LB_EVENT_LB_UPDATE_BEGIN);
294                 }
295         } else {
296                 ErrPrint("Invalid request[%s], %s\n", id, fbfile);
297         }
298
299 out:
300         return NULL;
301 }
302
303 static struct packet *master_pd_update_begin(pid_t pid, int handle, const struct packet *packet)
304 {
305         struct livebox *handler;
306         const char *pkgname;
307         const char *id;
308         const char *fbfile;
309         int ret;
310
311         ret = packet_get(packet, "sss", &pkgname, &id, &fbfile);
312         if (ret != 2) {
313                 ErrPrint("Invalid argument\n");
314                 goto out;
315         }
316
317         handler = lb_find_livebox(pkgname, id);
318         if (!handler) {
319                 ErrPrint("Instance[%s] is not exists\n", id);
320                 goto out;
321         }
322
323         if (handler->state != CREATE) {
324                 ErrPrint("[%s] is not created\n", id);
325                 goto out;
326         }
327
328         if (lb_get_pd_fb(handler)) {
329                 lb_set_lb_fb(handler, fbfile);
330
331                 ret = fb_sync(lb_get_lb_fb(handler));
332                 if (ret != LB_STATUS_SUCCESS) {
333                         ErrPrint("Failed to do sync FB (%s - %s) (%d)\n", pkgname, fbfile, ret);
334                 } else {
335                         lb_invoke_event_handler(handler, LB_EVENT_PD_UPDATE_BEGIN);
336                 }
337         } else {
338                 ErrPrint("Invalid request[%s], %s\n", id, fbfile);
339         }
340
341 out:
342         return NULL;
343 }
344
345 static struct packet *master_lb_update_end(pid_t pid, int handle, const struct packet *packet)
346 {
347         struct livebox *handler;
348         const char *pkgname;
349         const char *id;
350         int ret;
351
352         ret = packet_get(packet, "ss", &pkgname, &id);
353         if (ret != 2) {
354                 ErrPrint("Invalid argument\n");
355                 goto out;
356         }
357
358         handler = lb_find_livebox(pkgname, id);
359         if (!handler) {
360                 ErrPrint("Instance[%s] is not exists\n", id);
361                 goto out;
362         }
363
364         if (handler->state != CREATE) {
365                 ErrPrint("[%s] is not created\n", id);
366                 goto out;
367         }
368
369         if (lb_get_lb_fb(handler)) {
370                 lb_invoke_event_handler(handler, LB_EVENT_LB_UPDATE_END);
371         } else {
372                 ErrPrint("Invalid request[%s]\n", id);
373         }
374
375 out:
376         return NULL;
377 }
378
379 static struct packet *master_access_status(pid_t pid, int handle, const struct packet *packet)
380 {
381         struct livebox *handler;
382         const char *pkgname;
383         const char *id;
384         int ret;
385         int status;
386
387         ret = packet_get(packet, "ssi", &pkgname, &id, &status);
388         if (ret != 3) {
389                 ErrPrint("Invalid argument\n");
390                 goto out;
391         }
392
393         handler = lb_find_livebox(pkgname, id);
394         if (!handler) {
395                 ErrPrint("Instance[%s] is not exists\n", id);
396                 goto out;
397         }
398
399         if (handler->state != CREATE) {
400                 ErrPrint("[%s] is not created\n", id);
401                 goto out;
402         }
403
404         if (handler->access_event_cb) {
405                 ret_cb_t cb;
406                 void *cbdata;
407
408                 cb = handler->access_event_cb;
409                 cbdata = handler->access_event_cbdata;
410
411                 handler->access_event_cb = NULL;
412                 handler->access_event_cbdata = NULL;
413
414                 cb(handler, status, cbdata);
415         } else {
416                 ErrPrint("Invalid event[%s]\n", id);
417         }
418 out:
419         return NULL;
420 }
421
422 static struct packet *master_pd_update_end(pid_t pid, int handle, const struct packet *packet)
423 {
424         struct livebox *handler;
425         const char *pkgname;
426         const char *id;
427         int ret;
428
429         ret = packet_get(packet, "ss", &pkgname, &id);
430         if (ret != 2) {
431                 ErrPrint("Invalid argument\n");
432                 goto out;
433         }
434
435         handler = lb_find_livebox(pkgname, id);
436         if (!handler) {
437                 ErrPrint("Instance[%s] is not exists\n", id);
438                 goto out;
439         }
440
441         if (handler->state != CREATE) {
442                 ErrPrint("[%s] is not created\n", id);
443                 goto out;
444         }
445
446         if (lb_get_lb_fb(handler)) {
447                 lb_invoke_event_handler(handler, LB_EVENT_PD_UPDATE_END);
448         } else {
449                 ErrPrint("Invalid request[%s]", id);
450         }
451
452 out:
453         return NULL;
454 }
455
456 static struct packet *master_lb_updated(pid_t pid, int handle, const struct packet *packet)
457 {
458         const char *pkgname;
459         const char *id;
460         const char *fbfile;
461         const char *content;
462         const char *title;
463         struct livebox *handler;
464         int lb_w;
465         int lb_h;
466         double priority;
467         int ret;
468
469         ret = packet_get(packet, "sssiidss",
470                                 &pkgname, &id,
471                                 &fbfile, &lb_w, &lb_h,
472                                 &priority, &content, &title);
473         if (ret != 8) {
474                 ErrPrint("Invalid argument\n");
475                 goto out;
476         }
477
478         handler = lb_find_livebox(pkgname, id);
479         if (!handler) {
480                 ErrPrint("instance(%s) is not exists\n", id);
481                 goto out;
482         }
483
484         if (handler->state != CREATE) {
485                 /*!
486                  * \note
487                  * Already deleted by the user.
488                  * Don't try to notice anything with this, Just ignore all events
489                  * Beacuse the user doesn't wants know about this anymore
490                  */
491                 ErrPrint("(%s) is not exists, but updated\n", id);
492                 goto out;
493         }
494
495         lb_set_priority(handler, priority);
496         lb_set_content(handler, content);
497         lb_set_title(handler, title);
498         lb_set_size(handler, lb_w, lb_h);
499
500         if (lb_text_lb(handler)) {
501                 (void)parse_desc(handler, livebox_filename(handler), 0);
502                 /*!
503                  * \note
504                  * DESC parser will call the "text event callback".
505                  * Don't need to call global event callback in this case.
506                  */
507                 goto out;
508         } else if (lb_get_lb_fb(handler)) {
509                 lb_set_lb_fb(handler, fbfile);
510                 ret = fb_sync(lb_get_lb_fb(handler));
511                 if (ret != LB_STATUS_SUCCESS) {
512                         ErrPrint("Failed to do sync FB (%s - %s) (%d)\n", pkgname, util_basename(util_uri_to_path(id)), ret);
513                 }
514         } else {
515                 ret = LB_STATUS_SUCCESS;
516         }
517
518         if (ret == LB_STATUS_SUCCESS) {
519                 lb_invoke_event_handler(handler, LB_EVENT_LB_UPDATED);
520         }
521
522 out:
523         return NULL;
524 }
525
526 static struct packet *master_pd_created(pid_t pid, int handle, const struct packet *packet)
527 {
528         struct livebox *handler;
529         const char *pkgname;
530         const char *id;
531         const char *buf_id;
532         int width;
533         int height;
534         int ret;
535         int status;
536
537         ret = packet_get(packet, "sssiii", &pkgname, &id, &buf_id, &width, &height, &status);
538         if (ret != 6) {
539                 ErrPrint("Invalid argument\n");
540                 goto out;
541         }
542
543         handler = lb_find_livebox(pkgname, id);
544         if (!handler) {
545                 ErrPrint("Instance(%s) is not exists\n", id);
546                 goto out;
547         }
548
549         if (handler->state != CREATE) {
550                 ErrPrint("Instance(%s) is not created\n", id);
551                 goto out;
552         }
553
554         lb_set_pdsize(handler, width, height);
555         if (lb_text_pd(handler)) {
556                 DbgPrint("Text TYPE does not need to handle this\n");
557         } else {
558                 (void)lb_set_pd_fb(handler, buf_id);
559                 ret = fb_sync(lb_get_pd_fb(handler));
560                 if (ret < 0) {
561                         ErrPrint("Failed to do sync FB (%s - %s)\n", pkgname, util_basename(util_uri_to_path(id)));
562                 }
563         }
564
565         handler->is_pd_created = (status == 0);
566
567         if (handler->pd_created_cb) {
568                 ret_cb_t cb;
569                 void *cbdata;
570
571                 cb = handler->pd_created_cb;
572                 cbdata = handler->pd_created_cbdata;
573
574                 handler->pd_created_cb = NULL;
575                 handler->pd_created_cbdata = NULL;
576
577                 /*!
578                  * Before call the Callback function,
579                  * pd_create_cb must be reset.
580                  * Because, in the create callback, user can call create_pd function again.
581                  */
582                 DbgPrint("PERF_DBOX\n");
583                 cb(handler, status, cbdata);
584         } else if (handler->is_pd_created) {
585                 lb_invoke_event_handler(handler, LB_EVENT_PD_CREATED);
586         }
587
588 out:
589         return NULL;
590 }
591
592 static struct packet *master_pd_destroyed(pid_t pid, int handle, const struct packet *packet)
593 {
594         struct livebox *handler;
595         const char *pkgname;
596         const char *id;
597         int ret;
598         int status;
599
600         ret = packet_get(packet, "ssi", &pkgname, &id, &status);
601         if (ret != 3) {
602                 ErrPrint("Invalid argument\n");
603                 goto out;
604         }
605
606         handler = lb_find_livebox(pkgname, id);
607         if (!handler) {
608                 ErrPrint("Instance(%s) is not exists\n", id);
609                 goto out;
610         }
611
612         if (handler->state != CREATE) {
613                 ErrPrint("Instance(%s) is not created\n", id);
614                 goto out;
615         }
616
617         handler->is_pd_created = 0;
618
619         if (handler->pd_destroyed_cb) {
620                 ret_cb_t cb;
621                 void *cbdata;
622
623                 cb = handler->pd_destroyed_cb;
624                 cbdata = handler->pd_destroyed_cbdata;
625
626                 handler->pd_destroyed_cb = NULL;
627                 handler->pd_destroyed_cbdata = NULL;
628
629                 /*!
630                  * Before call the Callback function,
631                  * pd_destroyed_cb must be reset.
632                  * Because, in the create callback, user can call destroy_pd function again.
633                  */
634                 cb(handler, status, cbdata);
635         } else if (status == 0) {
636                 lb_invoke_event_handler(handler, LB_EVENT_PD_DESTROYED);
637         }
638
639 out:
640         return NULL;
641 }
642
643 static struct packet *master_pd_updated(pid_t pid, int handle, const struct packet *packet)
644 {
645         const char *pkgname;
646         const char *id;
647         const char *descfile;
648         const char *fbfile;
649         int ret;
650         struct livebox *handler;
651         int pd_w;
652         int pd_h;
653
654         ret = packet_get(packet, "ssssii",
655                                 &pkgname, &id,
656                                 &descfile, &fbfile,
657                                 &pd_w, &pd_h);
658         if (ret != 6) {
659                 ErrPrint("Invalid argument\n");
660                 goto out;
661         }
662
663         handler = lb_find_livebox(pkgname, id);
664         if (!handler) {
665                 ErrPrint("Instance(%s) is not exists\n", id);
666                 goto out;
667         }
668
669         if (handler->state != CREATE) {
670                 /*!
671                  * \note
672                  * This handler is already deleted by the user.
673                  * So don't try to notice anything about this anymore.
674                  * Just ignore all events.
675                  */
676                 ErrPrint("Instance(%s) is not created\n", id);
677                 goto out;
678         }
679
680         lb_set_pdsize(handler, pd_w, pd_h);
681
682         if (lb_text_pd(handler)) {
683                 (void)parse_desc(handler, descfile, 1);
684         } else {
685                 (void)lb_set_pd_fb(handler, fbfile);
686
687                 ret = fb_sync(lb_get_pd_fb(handler));
688                 if (ret < 0) {
689                         ErrPrint("Failed to do sync FB (%s - %s), %d\n", pkgname, util_basename(util_uri_to_path(id)), ret);
690                 } else {
691                         lb_invoke_event_handler(handler, LB_EVENT_PD_UPDATED);
692                 }
693         }
694
695 out:
696         return NULL;
697 }
698
699 static struct packet *master_update_mode(pid_t pid, int handle, const struct packet *packet)
700 {
701         struct livebox *handler;
702         const char *pkgname;
703         const char *id;
704         int active_mode;
705         int status;
706         int ret;
707
708         if (!packet) {
709                 ErrPrint("Invalid packet\n");
710                 goto out;
711         }
712
713         ret = packet_get(packet, "ssii", &pkgname, &id, &status, &active_mode);
714         if (ret != 4) {
715                 ErrPrint("Invalid argument\n");
716                 goto out;
717         }
718
719         handler = lb_find_livebox(pkgname, id);
720         if (!handler) {
721                 ErrPrint("Livebox(%s) is not found\n", id);
722                 goto out;
723         }
724
725         if (handler->state != CREATE) {
726                 ErrPrint("Livebox(%s) is not created yet\n", id);
727                 goto out;
728         }
729
730         if (status == LB_STATUS_SUCCESS) {
731                 lb_set_update_mode(handler, active_mode);
732         }
733
734         if (handler->update_mode_cb) {
735                 ret_cb_t cb;
736                 void *cbdata;
737
738                 cb = handler->update_mode_cb;
739                 cbdata = handler->update_mode_cbdata;
740
741                 handler->update_mode_cb = NULL;
742                 handler->update_mode_cbdata = NULL;
743
744                 cb(handler, status, cbdata);
745         } else {
746                 lb_invoke_event_handler(handler, LB_EVENT_UPDATE_MODE_CHANGED);
747         }
748
749 out:
750         return NULL;
751 }
752
753 static struct packet *master_size_changed(pid_t pid, int handle, const struct packet *packet)
754 {
755         struct livebox *handler;
756         const char *pkgname;
757         const char *id;
758         const char *fbfile;
759         int status;
760         int ret;
761         int w;
762         int h;
763         int is_pd;
764
765         if (!packet) {
766                 ErrPrint("Invalid packet\n");
767                 goto out;
768         }
769
770         ret = packet_get(packet, "sssiiii", &pkgname, &id, &fbfile, &is_pd, &w, &h, &status);
771         if (ret != 7) {
772                 ErrPrint("Invalid argument\n");
773                 goto out;
774         }
775
776         handler = lb_find_livebox(pkgname, id);
777         if (!handler) {
778                 ErrPrint("Livebox(%s) is not found\n", id);
779                 goto out;
780         }
781
782         if (handler->state != CREATE) {
783                 ErrPrint("Livebox(%s) is not created yet\n", id);
784                 goto out;
785         }
786
787         if (is_pd) {
788                 /*!
789                  * \NOTE
790                  * PD is not able to resized by the client.
791                  * PD is only can be managed by the provider.
792                  * So the PD has no private resized event handler.
793                  * Notify it via global event handler only.
794                  */
795                 if (status == 0) {
796                         lb_set_pdsize(handler, w, h);
797                         lb_invoke_event_handler(handler, LB_EVENT_PD_SIZE_CHANGED);
798                 } else {
799                         ErrPrint("This is not possible. PD Size is changed but the return value is not ZERO (%d)\n", status);
800                 }
801         } else {
802                 if (status == 0) {
803                         lb_set_size(handler, w, h);
804
805                         /*!
806                          * \NOTE
807                          * If there is a created LB FB, 
808                          * Update it too.
809                          */
810                         if (lb_get_lb_fb(handler)) {
811                                 lb_set_lb_fb(handler, fbfile);
812
813                                 ret = fb_sync(lb_get_lb_fb(handler));
814                                 if (ret < 0) {
815                                         ErrPrint("Failed to do sync FB (%s - %s)\n", pkgname, util_basename(util_uri_to_path(id)));
816                                 }
817
818                                 /* Just update the size info only. */
819                         }
820
821                         /*!
822                          * \NOTE
823                          * I cannot believe client.
824                          * So I added some log before & after call the user callback.
825                          */
826                         if (handler->size_changed_cb) {
827                                 ret_cb_t cb;
828                                 void *cbdata;
829
830                                 cb = handler->size_changed_cb;
831                                 cbdata = handler->size_cbdata;
832
833                                 handler->size_changed_cb = NULL;
834                                 handler->size_cbdata = NULL;
835
836                                 cb(handler, status, cbdata);
837                         } else {
838                                 lb_invoke_event_handler(handler, LB_EVENT_LB_SIZE_CHANGED);
839                         }
840                 } else {
841                         if (handler->size_changed_cb) {
842                                 ret_cb_t cb;
843                                 void *cbdata;
844
845                                 cb = handler->size_changed_cb;
846                                 cbdata = handler->size_cbdata;
847
848                                 handler->size_changed_cb = NULL;
849                                 handler->size_cbdata = NULL;
850
851                                 cb(handler, status, cbdata);
852                         }
853                 }
854         }
855
856 out:
857         return NULL;
858 }
859
860 static struct packet *master_period_changed(pid_t pid, int handle, const struct packet *packet)
861 {
862         struct livebox *handler;
863         const char *pkgname;
864         const char *id;
865         int ret;
866         double period;
867         int status;
868
869         ret = packet_get(packet, "idss", &status, &period, &pkgname, &id);
870         if (ret != 4) {
871                 ErrPrint("Invalid argument\n");
872                 goto out;
873         }
874
875         handler = lb_find_livebox(pkgname, id);
876         if (!handler) {
877                 ErrPrint("Livebox(%s) is not found\n", id);
878                 goto out;
879         }
880
881         if (handler->state != CREATE) {
882                 ErrPrint("Livebox(%s) is not created\n", id);
883                 goto out;
884         }
885
886         if (status == 0) {
887                 lb_set_period(handler, period);
888         }
889
890         if (handler->period_changed_cb) {
891                 ret_cb_t cb;
892                 void *cbdata;
893
894                 cb = handler->period_changed_cb;
895                 cbdata = handler->period_cbdata;
896
897                 handler->period_changed_cb = NULL;
898                 handler->period_cbdata = NULL;
899
900                 cb(handler, status, cbdata);
901         } else if (status == 0) {
902                 lb_invoke_event_handler(handler, LB_EVENT_PERIOD_CHANGED);
903         }
904
905 out:
906         return NULL;
907 }
908
909 static struct packet *master_group_changed(pid_t pid, int handle, const struct packet *packet)
910 {
911         struct livebox *handler;
912         const char *pkgname;
913         const char *id;
914         int ret;
915         const char *cluster;
916         const char *category;
917         int status;
918
919         ret = packet_get(packet, "ssiss", &pkgname, &id, &status, &cluster, &category);
920         if (ret != 5) {
921                 ErrPrint("Invalid argument\n");
922                 goto out;
923         }
924
925         handler = lb_find_livebox(pkgname, id);
926         if (!handler) {
927                 ErrPrint("Livebox(%s) is not exists\n", id);
928                 goto out;
929         }
930
931         if (handler->state != CREATE) {
932                 /*!
933                  * \note
934                  * Do no access this handler,
935                  * You cannot believe this handler anymore.
936                  */
937                 ErrPrint("Livebox(%s) is not created\n", id);
938                 goto out;
939         }
940
941         if (status == 0) {
942                 (void)lb_set_group(handler, cluster, category);
943         }
944
945         if (handler->group_changed_cb) {
946                 ret_cb_t cb;
947                 void *cbdata;
948
949                 cb = handler->group_changed_cb;
950                 cbdata = handler->group_cbdata;
951
952                 handler->group_changed_cb = NULL;
953                 handler->group_cbdata = NULL;
954
955                 cb(handler, status, cbdata);
956         } else if (status == 0) {
957                 lb_invoke_event_handler(handler, LB_EVENT_GROUP_CHANGED);
958         }
959
960 out:
961         return NULL;
962 }
963
964 static struct packet *master_created(pid_t pid, int handle, const struct packet *packet)
965 {
966         struct livebox *handler;
967
968         int lb_w;
969         int lb_h;
970         int pd_w;
971         int pd_h;
972         const char *pkgname;
973         const char *id;
974
975         const char *content;
976         const char *cluster;
977         const char *category;
978         const char *lb_fname;
979         const char *pd_fname;
980         const char *title;
981
982         double timestamp;
983         const char *auto_launch;
984         double priority;
985         int size_list;
986         int user;
987         int pinup_supported;
988         enum lb_type lb_type;
989         enum pd_type pd_type;
990         double period;
991         int is_pinned_up;
992
993         int old_state = DESTROYED;
994
995         int ret;
996
997         ret = packet_get(packet, "dsssiiiisssssdiiiiidsi",
998                         &timestamp,
999                         &pkgname, &id, &content,
1000                         &lb_w, &lb_h, &pd_w, &pd_h,
1001                         &cluster, &category, &lb_fname, &pd_fname,
1002                         &auto_launch, &priority, &size_list, &user, &pinup_supported,
1003                         &lb_type, &pd_type, &period, &title, &is_pinned_up);
1004         if (ret != 22) {
1005                 ErrPrint("Invalid argument\n");
1006                 ret = LB_STATUS_ERROR_INVALID;
1007                 goto out;
1008         }
1009
1010         ErrPrint("[%lf] pkgname: %s, id: %s, content: %s, "
1011                 "pd_w: %d, pd_h: %d, lb_w: %d, lb_h: %d, "
1012                 "cluster: %s, category: %s, lb_fname: \"%s\", pd_fname: \"%s\", "
1013                 "auto_launch: %s, priority: %lf, size_list: %d, user: %d, pinup: %d, "
1014                 "lb_type: %d, pd_type: %d, period: %lf, title: [%s], is_pinned_up: %d\n",
1015                 timestamp, pkgname, id, content,
1016                 pd_w, pd_h, lb_w, lb_h,
1017                 cluster, category, lb_fname, pd_fname,
1018                 auto_launch, priority, size_list, user, pinup_supported,
1019                 lb_type, pd_type, period, title, is_pinned_up);
1020
1021         handler = lb_find_livebox_by_timestamp(timestamp);
1022         if (!handler) {
1023                 handler = lb_new_livebox(pkgname, id, timestamp);
1024                 if (!handler) {
1025                         ErrPrint("Failed to create a new livebox\n");
1026                         ret = LB_STATUS_ERROR_FAULT;
1027                         goto out;
1028                 }
1029
1030                 old_state = handler->state;
1031         } else {
1032                 if (handler->state != CREATE) {
1033                         if (handler->state != DELETE) {
1034                                 /*!
1035                                  * \note
1036                                  * This is not possible!!!
1037                                  */
1038                                 ErrPrint("Invalid handler\n");
1039                                 ret = LB_STATUS_ERROR_INVALID;
1040                                 goto out;
1041                         }
1042
1043                         /*!
1044                          * \note
1045                          * After get the delete states,
1046                          * call the create callback with deleted result.
1047                          */
1048                 }
1049
1050                 old_state = handler->state;
1051
1052                 if (handler->id) {
1053                         ErrPrint("Already created: timestamp[%lf] "
1054                                 "pkgname[%s], id[%s] content[%s] "
1055                                 "cluster[%s] category[%s] lb_fname[%s] pd_fname[%s]\n",
1056                                         timestamp, pkgname, id,
1057                                         content, cluster, category,
1058                                         lb_fname, pd_fname);
1059
1060                         ret = LB_STATUS_ERROR_ALREADY;
1061                         goto out;
1062                 }
1063
1064                 lb_set_id(handler, id);
1065         }
1066
1067         lb_set_size(handler, lb_w, lb_h);
1068         handler->lb.type = lb_type;
1069         handler->is_pinned_up = is_pinned_up;
1070
1071         switch (lb_type) {
1072         case _LB_TYPE_FILE:
1073                 break;
1074         case _LB_TYPE_SCRIPT:
1075         case _LB_TYPE_BUFFER:
1076                 if (!strlen(lb_fname)) {
1077                         break;
1078                 }
1079                 lb_set_lb_fb(handler, lb_fname);
1080                 ret = fb_sync(lb_get_lb_fb(handler));
1081                 if (ret < 0) {
1082                         ErrPrint("Failed to do sync FB (%s - %s)\n", pkgname, util_basename(util_uri_to_path(id)));
1083                 }
1084                 break;
1085         case _LB_TYPE_TEXT:
1086                 lb_set_text_lb(handler);
1087                 break;
1088         default:
1089                 break;
1090         }
1091
1092         handler->pd.type = pd_type;
1093         lb_set_pdsize(handler, pd_w, pd_h);
1094         lb_set_default_pdsize(handler, pd_w, pd_h);
1095         switch (pd_type) {
1096         case _PD_TYPE_SCRIPT:
1097         case _PD_TYPE_BUFFER:
1098                 if (!strlen(pd_fname)) {
1099                         break;
1100                 }
1101
1102                 lb_set_pd_fb(handler, pd_fname);
1103                 ret = fb_sync(lb_get_pd_fb(handler));
1104                 if (ret < 0) {
1105                         ErrPrint("Failed to do sync FB (%s - %s)\n", pkgname, util_basename(util_uri_to_path(id)));
1106                 }
1107                 break;
1108         case _PD_TYPE_TEXT:
1109                 lb_set_text_pd(handler);
1110                 break;
1111         default:
1112                 break;
1113         }
1114
1115         lb_set_priority(handler, priority);
1116
1117         lb_set_size_list(handler, size_list);
1118         lb_set_group(handler, cluster, category);
1119
1120         lb_set_content(handler, content);
1121         lb_set_title(handler, title);
1122
1123         lb_set_user(handler, user);
1124
1125         lb_set_auto_launch(handler, auto_launch);
1126         lb_set_pinup(handler, pinup_supported);
1127
1128         lb_set_period(handler, period);
1129
1130         ret = 0;
1131
1132         if (handler->state == CREATE) {
1133                 /*!
1134                  * \note
1135                  * These callback can change the handler->state.
1136                  * So we have to use the "old_state" which stored state before call these callbacks
1137                  */
1138
1139                 if (handler->created_cb) {
1140                         ret_cb_t cb;
1141                         void *cbdata;
1142
1143                         cb = handler->created_cb;
1144                         cbdata = handler->created_cbdata;
1145
1146                         handler->created_cb = NULL;
1147                         handler->created_cbdata = NULL;
1148
1149                         cb(handler, ret, cbdata);
1150                 } else {
1151                         lb_invoke_event_handler(handler, LB_EVENT_CREATED);
1152                 }
1153         }
1154
1155 out:
1156         if (ret == 0 && old_state == DELETE) {
1157                 lb_send_delete(handler, handler->created_cb, handler->created_cbdata);
1158
1159                 /*!
1160                  * \note
1161                  * handler->created_cb = NULL;
1162                  * handler->created_cbdata = NULL;
1163                  *
1164                  * Do not clear this to use this from the deleted event callback.
1165                  * if this value is not cleared when the deleted event callback check it,
1166                  * it means that the created function is not called yet.
1167                  * Then the call the deleted event callback with LB_STATUS_ERROR_CANCEL errno.
1168                  */
1169         }
1170
1171         return NULL;
1172 }
1173
1174 static struct method s_table[] = {
1175         {
1176                 .cmd = "lb_updated", /* pkgname, id, lb_w, lb_h, priority, ret */
1177                 .handler = master_lb_updated,
1178         },
1179         {
1180                 .cmd = "pd_updated", /* pkgname, id, descfile, pd_w, pd_h, ret */
1181                 .handler = master_pd_updated,
1182         },
1183         {
1184                 .cmd = "pd_created",
1185                 .handler = master_pd_created,
1186         },
1187         {
1188                 .cmd = "pd_destroyed",
1189                 .handler = master_pd_destroyed,
1190         },
1191         {
1192                 .cmd = "fault_package", /* pkgname, id, function, ret */
1193                 .handler = master_fault_package,
1194         },
1195         {
1196                 .cmd = "deleted", /* pkgname, id, timestamp, ret */
1197                 .handler = master_deleted,
1198         },
1199         {
1200                 .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 */
1201                 .handler = master_created,
1202         },
1203         {
1204                 .cmd = "group_changed",
1205                 .handler = master_group_changed,
1206         },
1207         {
1208                 .cmd = "period_changed",
1209                 .handler = master_period_changed,
1210         },
1211         {
1212                 .cmd = "size_changed",
1213                 .handler = master_size_changed,
1214         },
1215         {
1216                 .cmd = "pinup",
1217                 .handler = master_pinup,
1218         },
1219         {
1220                 .cmd = "scroll",
1221                 .handler = master_hold_scroll,
1222         },
1223
1224         {
1225                 .cmd = "update_mode",
1226                 .handler = master_update_mode,
1227         },
1228
1229         {
1230                 .cmd = "lb_update_begin",
1231                 .handler = master_lb_update_begin,
1232         },
1233         {
1234                 .cmd = "lb_update_end",
1235                 .handler = master_lb_update_end,
1236         },
1237
1238         {
1239                 .cmd = "pd_update_begin",
1240                 .handler = master_pd_update_begin,
1241         },
1242         {
1243                 .cmd = "pd_update_end",
1244                 .handler = master_pd_update_end,
1245         },
1246
1247         {
1248                 .cmd = "access_status",
1249                 .handler = master_access_status,
1250         },
1251
1252         {
1253                 .cmd = NULL,
1254                 .handler = NULL,
1255         },
1256 };
1257
1258 static void acquire_cb(struct livebox *handler, const struct packet *result, void *data)
1259 {
1260         if (!result) {
1261                 DbgPrint("Result packet is not valid\n");
1262         } else {
1263                 int ret;
1264
1265                 if (packet_get(result, "i", &ret) != 1) {
1266                         ErrPrint("Invalid argument\n");
1267                 } else {
1268                         DbgPrint("Acquire returns: %d\n", ret);
1269                 }
1270         }
1271
1272         return;
1273 }
1274
1275 static inline int make_connection(void)
1276 {
1277         struct packet *packet;
1278         int ret;
1279
1280         DbgPrint("Let's making connection!\n");
1281
1282         s_info.fd = com_core_packet_client_init(client_addr(), 0, s_table);
1283         if (s_info.fd < 0) {
1284                 ErrPrint("Try this again later\n");
1285                 return LB_STATUS_ERROR_IO;
1286         }
1287
1288         packet = packet_create("acquire", "d", util_timestamp());
1289         if (!packet) {
1290                 com_core_packet_client_fini(s_info.fd);
1291                 s_info.fd = -1;
1292                 return LB_STATUS_ERROR_FAULT;
1293         }
1294
1295         ret = master_rpc_async_request(NULL, packet, 1, acquire_cb, NULL);
1296         if (ret < 0) {
1297                 ErrPrint("Master RPC returns %d\n", ret);
1298                 com_core_packet_client_fini(s_info.fd);
1299                 s_info.fd = -1;
1300                 return LB_STATUS_ERROR_IO;
1301         }
1302
1303         return LB_STATUS_SUCCESS;
1304 }
1305
1306 static int connected_cb(int handle, void *data)
1307 {
1308         master_rpc_check_and_fire_consumer();
1309         return 0;
1310 }
1311
1312 static void master_started_cb(keynode_t *node, void *data)
1313 {
1314         int state = 0;
1315
1316         if (vconf_get_bool(VCONFKEY_MASTER_STARTED, &state) < 0) {
1317                 ErrPrint("Unable to get [%s]\n", VCONFKEY_MASTER_STARTED);
1318         }
1319
1320         DbgPrint("Master state: %d\n", state);
1321         if (state == 1 && make_connection() == LB_STATUS_SUCCESS) {
1322                 int ret;
1323                 ret = vconf_ignore_key_changed(VCONFKEY_MASTER_STARTED, master_started_cb);
1324                 DbgPrint("master_started vconf key de-registered [%d]\n", ret);
1325         }
1326 }
1327
1328 static gboolean timeout_cb(gpointer data)
1329 {
1330         if (vconf_notify_key_changed(VCONFKEY_MASTER_STARTED, master_started_cb, NULL) < 0) {
1331                 ErrPrint("Failed to add vconf for monitoring service state\n");
1332         } else {
1333                 DbgPrint("vconf event callback is registered\n");
1334         }
1335
1336         master_started_cb(NULL, NULL);
1337
1338         s_info.timer_id = 0;
1339         return FALSE;
1340 }
1341
1342 static int disconnected_cb(int handle, void *data)
1343 {
1344         if (s_info.fd != handle) {
1345                 /*!< This handle is not my favor */
1346                 return 0;
1347         }
1348
1349         s_info.fd = -1; /*!< Disconnected */
1350
1351         master_rpc_clear_all_request();
1352         lb_invoke_fault_handler(LB_FAULT_PROVIDER_DISCONNECTED, MASTER_PKGNAME, "default", "disconnected");
1353
1354         lb_delete_all();
1355
1356         /* Try to reconnect after 1 sec later */
1357         if (!s_info.timer_id) {
1358                 DbgPrint("Reconnecting timer is added\n");
1359                 s_info.timer_id = g_timeout_add(1000, timeout_cb, NULL);
1360                 if (s_info.timer_id == 0) {
1361                         ErrPrint("Unable to add reconnecting timer\n");
1362                         return 0;
1363                 }
1364         } else {
1365                 ErrPrint("Reconnecting timer is already exists\n");
1366         }
1367
1368         return 0;
1369 }
1370
1371 int client_init(void)
1372 {
1373         s_info.client_addr = vconf_get_str(VCONFKEY_MASTER_CLIENT_ADDR);
1374         if (!s_info.client_addr) {
1375                 s_info.client_addr = strdup(CLIENT_SOCKET);
1376                 if (!s_info.client_addr) {
1377                         ErrPrint("Heap: %s\n", strerror(errno));
1378                         return -ENOMEM;
1379                 }
1380         }
1381
1382         (void)file_service_init();
1383
1384         DbgPrint("Server Address: %s\n", s_info.client_addr);
1385
1386         com_core_add_event_callback(CONNECTOR_DISCONNECTED, disconnected_cb, NULL);
1387         com_core_add_event_callback(CONNECTOR_CONNECTED, connected_cb, NULL);
1388         if (vconf_notify_key_changed(VCONFKEY_MASTER_STARTED, master_started_cb, NULL) < 0) {
1389                 ErrPrint("Failed to add vconf for service state\n");
1390         } else {
1391                 DbgPrint("vconf event callback is registered\n");
1392         }
1393
1394         master_started_cb(NULL, NULL);
1395         return 0;
1396 }
1397
1398 int client_fd(void)
1399 {
1400         return s_info.fd;
1401 }
1402
1403 const char *client_addr(void)
1404 {
1405         return s_info.client_addr;
1406 }
1407
1408 int client_fini(void)
1409 {
1410         int ret;
1411
1412         (void)file_service_fini();
1413
1414         ret = vconf_ignore_key_changed(VCONFKEY_MASTER_STARTED, master_started_cb);
1415         if (ret < 0) {
1416                 DbgPrint("Ignore vconf key: %d\n", ret);
1417         }
1418
1419         com_core_del_event_callback(CONNECTOR_DISCONNECTED, disconnected_cb, NULL);
1420         com_core_del_event_callback(CONNECTOR_CONNECTED, connected_cb, NULL);
1421         com_core_packet_client_fini(s_info.fd);
1422         s_info.fd = -1;
1423         free(s_info.client_addr);
1424         s_info.client_addr = NULL;
1425         return LB_STATUS_SUCCESS;
1426 }
1427
1428 /* End of a file */