Various patches are applied
[platform/framework/web/data-provider-master.git] / src / badge_service.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
19 #include <Eina.h>
20
21 #include <dlog.h>
22 #include <livebox-errno.h>
23 #include <packet.h>
24
25 #include <badge.h>
26 #include <badge_db.h>
27
28 #include "service_common.h"
29 #include "debug.h"
30 #include "util.h"
31 #include "conf.h"
32
33 #define BADGE_ADDR "/tmp/.badge.service"
34
35 static struct info {
36         Eina_List *context_list;
37         struct service_context *svc_ctx;
38 } s_info = {
39         .context_list = NULL, /*!< \WARN: This is only used for SERVICE THREAD */
40         .svc_ctx = NULL, /*!< \WARN: This is only used for MAIN THREAD */
41 };
42
43 struct context {
44         struct tcb *tcb;
45         double seq;
46 };
47
48 struct noti_service {
49         const char *cmd;
50         void (*handler)(struct tcb *tcb, struct packet *packet, void *data);
51 };
52
53 /*!
54  * FUNCTIONS to handle badge
55  */
56 static inline char *get_string(char *string)
57 {
58         if (string == NULL) {
59                 return NULL;
60         }
61         if (string[0] == '\0') {
62                 return NULL;
63         }
64
65         return string;
66 }
67
68 /*!
69  * SERVICE HANDLER
70  */
71 static void _handler_insert_badge(struct tcb *tcb, struct packet *packet, void *data)
72 {
73         int ret = 0;
74         struct packet *packet_reply = NULL;
75         struct packet *packet_service = NULL;
76         char *pkgname = NULL;
77         char *writable_pkg = NULL;
78         char *caller = NULL;
79
80         if (packet_get(packet, "sss", &pkgname, &writable_pkg, &caller) == 3) {
81                 pkgname = get_string(pkgname);
82                 writable_pkg = get_string(writable_pkg);
83                 caller = get_string(caller);
84
85                 if (pkgname != NULL && writable_pkg != NULL && caller != NULL) {
86                         ret = badge_db_insert(pkgname, writable_pkg, caller);
87
88                 } else {
89                         ret = BADGE_ERROR_INVALID_DATA;
90                 }
91
92                 packet_reply = packet_create_reply(packet, "i", ret);
93                 if (packet_reply) {
94                         service_common_unicast_packet(tcb, packet_reply);
95                         packet_destroy(packet_reply);
96                 }
97
98                 if (ret == BADGE_ERROR_NONE) {
99                         packet_service = packet_create("insert_badge", "is", ret, pkgname);
100                         if (packet_service != NULL) {
101                                 service_common_multicast_packet(tcb, packet_service, TCB_CLIENT_TYPE_SERVICE);
102                                 packet_destroy(packet_service);
103                         }
104                 }
105         } else {
106                 ErrPrint("Failed to get data from the packet");
107         }
108 }
109
110 static void _handler_delete_badge(struct tcb *tcb, struct packet *packet, void *data)
111 {
112         int ret = 0;
113         struct packet *packet_reply = NULL;
114         struct packet *packet_service = NULL;
115         char *pkgname = NULL;
116         char *caller = NULL;
117
118         if (packet_get(packet, "ss", &pkgname, &caller) == 2) {
119                 pkgname = get_string(pkgname);
120                 caller = get_string(caller);
121
122                 if (pkgname != NULL && caller != NULL) {
123                         ret = badge_db_delete(pkgname, caller);
124
125                 } else {
126                         ret = BADGE_ERROR_INVALID_DATA;
127                 }
128
129                 packet_reply = packet_create_reply(packet, "i", ret);
130                 if (packet_reply) {
131                         service_common_unicast_packet(tcb, packet_reply);
132                         packet_destroy(packet_reply);
133                 }
134
135                 if (ret == BADGE_ERROR_NONE) {
136                         packet_service = packet_create("delete_badge", "is", ret, pkgname);
137                         if (packet_service != NULL) {
138                                 service_common_multicast_packet(tcb, packet_service, TCB_CLIENT_TYPE_SERVICE);
139                                 packet_destroy(packet_service);
140                         }
141                 }
142         } else {
143                 ErrPrint("Failed to get data from the packet");
144         }
145 }
146
147 static void _handler_set_badge_count(struct tcb *tcb, struct packet *packet, void *data)
148 {
149         int ret = 0;
150         struct packet *packet_reply = NULL;
151         struct packet *packet_service = NULL;
152         char *pkgname = NULL;
153         char *caller = NULL;
154         int count = 0;
155
156         if (packet_get(packet, "ssi", &pkgname, &caller, &count) == 3) {
157                 pkgname = get_string(pkgname);
158                 caller = get_string(caller);
159
160                 if (pkgname != NULL && caller != NULL) {
161                         ret = badge_db_set_count(pkgname, caller, count);
162
163                 } else {
164                         ret = BADGE_ERROR_INVALID_DATA;
165                 }
166
167                 packet_reply = packet_create_reply(packet, "i", ret);
168                 if (packet_reply) {
169                         service_common_unicast_packet(tcb, packet_reply);
170                         packet_destroy(packet_reply);
171                 }
172
173                 if (ret == BADGE_ERROR_NONE) {
174                         packet_service = packet_create("set_badge_count", "isi", ret, pkgname, count);
175                         if (packet_service != NULL) {
176                                 service_common_multicast_packet(tcb, packet_service, TCB_CLIENT_TYPE_SERVICE);
177                                 packet_destroy(packet_service);
178                         }
179                 }
180         } else {
181                 ErrPrint("Failed to get data from the packet");
182         }
183 }
184
185 static void _handler_set_display_option(struct tcb *tcb, struct packet *packet, void *data)
186 {
187         int ret = 0;
188         struct packet *packet_reply = NULL;
189         struct packet *packet_service = NULL;
190         char *pkgname = NULL;
191         char *caller = NULL;
192         int is_display = 0;
193
194         if (packet_get(packet, "ssi", &pkgname, &caller, &is_display) == 3) {
195                 pkgname = get_string(pkgname);
196                 caller = get_string(caller);
197
198                 if (pkgname != NULL && caller != NULL) {
199                         ret = badge_db_set_display_option(pkgname, caller, is_display);
200
201                 } else {
202                         ret = BADGE_ERROR_INVALID_DATA;
203                 }
204
205                 packet_reply = packet_create_reply(packet, "i", ret);
206                 if (packet_reply) {
207                         service_common_unicast_packet(tcb, packet_reply);
208                         packet_destroy(packet_reply);
209                 }
210
211                 if (ret == BADGE_ERROR_NONE) {
212                         packet_service = packet_create("set_disp_option", "isi", ret, pkgname, is_display);
213                         if (packet_service != NULL) {
214                                 service_common_multicast_packet(tcb, packet_service, TCB_CLIENT_TYPE_SERVICE);
215                                 packet_destroy(packet_service);
216                         }
217                 }
218         } else {
219                 ErrPrint("Failed to get data from the packet");
220         }
221 }
222
223 static void _handler_service_register(struct tcb *tcb, struct packet *packet, void *data)
224 {
225         struct packet *packet_reply;
226         int ret;
227
228         ret = tcb_client_type_set(tcb, TCB_CLIENT_TYPE_SERVICE);
229
230         packet_reply = packet_create_reply(packet, "i", ret);
231         if (packet_reply) {
232                 service_common_unicast_packet(tcb, packet_reply);
233                 packet_destroy(packet_reply);
234         }
235 }
236
237 /*!
238  * SERVICE THREAD
239  */
240 static int service_thread_main(struct tcb *tcb, struct packet *packet, void *data)
241 {
242         int i = 0;
243         const char *command;
244         static struct noti_service service_req_table[] = {
245                 {
246                         .cmd = "insert_badge",
247                         .handler = _handler_insert_badge,
248                 },
249                 {
250                         .cmd = "delete_badge",
251                         .handler = _handler_delete_badge,
252                 },
253                 {
254                         .cmd = "set_badge_count",
255                         .handler = _handler_set_badge_count,
256                 },
257                 {
258                         .cmd = "set_disp_option",
259                         .handler = _handler_set_display_option,
260                 },
261                 {
262                         .cmd = "service_register",
263                         .handler = _handler_service_register,
264                 },
265                 {
266                         .cmd = NULL,
267                         .handler = NULL,
268                 },
269         };
270
271         DbgPrint("TCB: %p, Packet: %p\n", tcb, packet);
272
273         command = packet_command(packet);
274         if (!command) {
275                 ErrPrint("Invalid command\n");
276                 return -EINVAL;
277         }
278         DbgPrint("Command: %s, Packet type[%d]\n", command, packet_type(packet));
279
280         switch (packet_type(packet)) {
281         case PACKET_REQ:
282                 /* Need to send reply packet */
283                 DbgPrint("REQ: Command: [%s]\n", command);
284
285                 for (i = 0; service_req_table[i].cmd; i++) {
286                         if (strcmp(service_req_table[i].cmd, command))
287                                 continue;
288
289                         service_req_table[i].handler(tcb, packet, data);
290                         break;
291                 }
292
293                 break;
294         case PACKET_REQ_NOACK:
295                 break;
296         case PACKET_ACK:
297                 break;
298         default:
299                 ErrPrint("Packet type is not valid[%s]\n", command);
300                 return -EINVAL;
301         }
302
303         /*!
304          * return value has no meanning,
305          * it will be printed by dlogutil.
306          */
307         return 0;
308 }
309
310
311 /*!
312  * MAIN THREAD
313  * Do not try to do anyother operation in these functions
314  */
315 HAPI int badge_service_init(void)
316 {
317         if (s_info.svc_ctx) {
318                 ErrPrint("Already initialized\n");
319                 return LB_STATUS_ERROR_ALREADY;
320         }
321
322         s_info.svc_ctx = service_common_create(BADGE_ADDR, service_thread_main, NULL);
323         if (!s_info.svc_ctx) {
324                 ErrPrint("Unable to activate service thread\n");
325                 return LB_STATUS_ERROR_FAULT;
326         }
327
328         DbgPrint("Successfully initiated\n");
329         return LB_STATUS_SUCCESS;
330 }
331
332 HAPI int badge_service_fini(void)
333 {
334         if (!s_info.svc_ctx)
335                 return LB_STATUS_ERROR_INVALID;
336
337         service_common_destroy(s_info.svc_ctx);
338         DbgPrint("Successfully Finalized\n");
339         return LB_STATUS_SUCCESS;
340 }
341
342 /* End of a file */