2 * Copyright 2013 Samsung Electronics Co., Ltd
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
8 * http://floralicense.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.
22 #include <livebox-errno.h>
25 #include <sys/smack.h>
30 #include "service_common.h"
36 Eina_List *context_list;
37 struct service_context *svc_ctx;
39 .context_list = NULL, /*!< \WARN: This is only used for SERVICE THREAD */
40 .svc_ctx = NULL, /*!< \WARN: This is only used for MAIN THREAD */
50 void (*handler)(struct tcb *tcb, struct packet *packet, void *data);
54 * FUNCTIONS to handle badge
56 static inline char *get_string(char *string)
61 if (string[0] == '\0') {
71 static void _handler_insert_badge(struct tcb *tcb, struct packet *packet, void *data)
73 int ret = 0, ret_p = 0;
74 struct packet *packet_reply = NULL;
75 struct packet *packet_service = NULL;
77 char *writable_pkg = NULL;
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);
85 if (pkgname != NULL && writable_pkg != NULL && caller != NULL) {
86 ret = badge_db_insert(pkgname, writable_pkg, caller);
89 ret = BADGE_ERROR_INVALID_DATA;
92 packet_reply = packet_create_reply(packet, "i", ret);
94 if ((ret_p = service_common_unicast_packet(tcb, packet_reply)) < 0) {
95 ErrPrint("Failed to send a reply packet:%d", ret_p);
97 packet_destroy(packet_reply);
99 ErrPrint("Failed to create a reply packet");
102 if (ret == BADGE_ERROR_NONE) {
103 packet_service = packet_create("insert_badge", "is", ret, pkgname);
104 if (packet_service != NULL) {
105 if ((ret_p = service_common_multicast_packet(tcb, packet_service, TCB_CLIENT_TYPE_SERVICE)) < 0) {
106 ErrPrint("Failed to send a muticast packet:%d", ret_p);
108 packet_destroy(packet_service);
110 ErrPrint("Failed to create a multicast packet");
113 ErrPrint("Failed to insert a badge:%d", ret);
116 ErrPrint("Failed to get data from the packet");
120 static void _handler_delete_badge(struct tcb *tcb, struct packet *packet, void *data)
122 int ret = 0, ret_p = 0;
123 struct packet *packet_reply = NULL;
124 struct packet *packet_service = NULL;
125 char *pkgname = NULL;
128 if (packet_get(packet, "ss", &pkgname, &caller) == 2) {
129 pkgname = get_string(pkgname);
130 caller = get_string(caller);
132 if (pkgname != NULL && caller != NULL) {
133 ret = badge_db_delete(pkgname, caller);
136 ret = BADGE_ERROR_INVALID_DATA;
139 packet_reply = packet_create_reply(packet, "i", ret);
141 if ((ret_p = service_common_unicast_packet(tcb, packet_reply)) < 0) {
142 ErrPrint("Failed to send a reply packet:%d", ret_p);
144 packet_destroy(packet_reply);
146 ErrPrint("Failed to create a reply packet");
149 if (ret == BADGE_ERROR_NONE) {
150 packet_service = packet_create("delete_badge", "is", ret, pkgname);
151 if (packet_service != NULL) {
152 if ((ret_p = service_common_multicast_packet(tcb, packet_service, TCB_CLIENT_TYPE_SERVICE)) < 0) {
153 ErrPrint("Failed to send a muticast packet:%d", ret_p);
155 packet_destroy(packet_service);
157 ErrPrint("Failed to create a multicast packet");
160 ErrPrint("Failed to delete a badge:%d", ret);
163 ErrPrint("Failed to get data from the packet");
167 static void _handler_set_badge_count(struct tcb *tcb, struct packet *packet, void *data)
169 int ret = 0, ret_p = 0;
170 struct packet *packet_reply = NULL;
171 struct packet *packet_service = NULL;
172 char *pkgname = NULL;
176 if (packet_get(packet, "ssi", &pkgname, &caller, &count) == 3) {
177 pkgname = get_string(pkgname);
178 caller = get_string(caller);
180 if (pkgname != NULL && caller != NULL) {
181 ret = badge_db_set_count(pkgname, caller, count);
184 ret = BADGE_ERROR_INVALID_DATA;
187 packet_reply = packet_create_reply(packet, "i", ret);
189 if ((ret_p = service_common_unicast_packet(tcb, packet_reply)) < 0) {
190 ErrPrint("Failed to send a reply packet:%d", ret_p);
192 packet_destroy(packet_reply);
194 ErrPrint("Failed to create a reply packet");
197 if (ret == BADGE_ERROR_NONE) {
198 packet_service = packet_create("set_badge_count", "isi", ret, pkgname, count);
199 if (packet_service != NULL) {
200 if ((ret_p = service_common_multicast_packet(tcb, packet_service, TCB_CLIENT_TYPE_SERVICE)) < 0) {
201 ErrPrint("Failed to send a muticast packet:%d", ret_p);
203 packet_destroy(packet_service);
205 ErrPrint("Failed to create a multicast packet");
208 ErrPrint("Failed to set count of badge:%d", ret);
211 ErrPrint("Failed to get data from the packet");
215 static void _handler_set_display_option(struct tcb *tcb, struct packet *packet, void *data)
217 int ret = 0, ret_p = 0;
218 struct packet *packet_reply = NULL;
219 struct packet *packet_service = NULL;
220 char *pkgname = NULL;
224 if (packet_get(packet, "ssi", &pkgname, &caller, &is_display) == 3) {
225 pkgname = get_string(pkgname);
226 caller = get_string(caller);
228 if (pkgname != NULL && caller != NULL) {
229 ret = badge_db_set_display_option(pkgname, caller, is_display);
232 ret = BADGE_ERROR_INVALID_DATA;
235 packet_reply = packet_create_reply(packet, "i", ret);
237 if ((ret_p = service_common_unicast_packet(tcb, packet_reply)) < 0) {
238 ErrPrint("Failed to send a reply packet:%d", ret_p);
240 packet_destroy(packet_reply);
242 ErrPrint("Failed to create a reply packet");
245 if (ret == BADGE_ERROR_NONE) {
246 packet_service = packet_create("set_disp_option", "isi", ret, pkgname, is_display);
247 if (packet_service != NULL) {
248 if ((ret_p = service_common_multicast_packet(tcb, packet_service, TCB_CLIENT_TYPE_SERVICE)) < 0) {
249 ErrPrint("Failed to send a muticast packet:%d", ret_p);
251 packet_destroy(packet_service);
253 ErrPrint("Failed to create a multicast packet");
256 ErrPrint("Failed to set display option of badge:%d", ret);
259 ErrPrint("Failed to get data from the packet");
263 static void _handler_service_register(struct tcb *tcb, struct packet *packet, void *data)
266 struct packet *packet_reply;
268 ret = tcb_client_type_set(tcb, TCB_CLIENT_TYPE_SERVICE);
270 ErrPrint("Failed to set the type of client:%d", ret);
273 packet_reply = packet_create_reply(packet, "i", ret);
275 if ((ret = service_common_unicast_packet(tcb, packet_reply)) < 0) {
276 ErrPrint("Failed to send a reply packet:%d", ret);
278 packet_destroy(packet_reply);
280 ErrPrint("Failed to create a reply packet");
287 static int service_thread_main(struct tcb *tcb, struct packet *packet, void *data)
291 static struct noti_service service_req_table[] = {
293 .cmd = "insert_badge",
294 .handler = _handler_insert_badge,
297 .cmd = "delete_badge",
298 .handler = _handler_delete_badge,
301 .cmd = "set_badge_count",
302 .handler = _handler_set_badge_count,
305 .cmd = "set_disp_option",
306 .handler = _handler_set_display_option,
309 .cmd = "service_register",
310 .handler = _handler_service_register,
319 DbgPrint("TCB: %p is terminated (NIL packet)\n", tcb);
323 command = packet_command(packet);
325 ErrPrint("Invalid command\n");
328 DbgPrint("Command: [%s], Packet type[%d]\n", command, packet_type(packet));
330 switch (packet_type(packet)) {
332 /* Need to send reply packet */
333 for (i = 0; service_req_table[i].cmd; i++) {
334 if (strcmp(service_req_table[i].cmd, command))
337 service_req_table[i].handler(tcb, packet, data);
342 case PACKET_REQ_NOACK:
347 ErrPrint("Packet type is not valid[%s]\n", command);
352 * return value has no meanning,
353 * it will be printed by dlogutil.
361 * Do not try to do anyother operation in these functions
363 HAPI int badge_service_init(void)
365 if (s_info.svc_ctx) {
366 ErrPrint("Already initialized\n");
367 return LB_STATUS_ERROR_ALREADY;
370 s_info.svc_ctx = service_common_create(BADGE_SOCKET, service_thread_main, NULL);
371 if (!s_info.svc_ctx) {
372 ErrPrint("Unable to activate service thread\n");
373 return LB_STATUS_ERROR_FAULT;
376 if (smack_fsetlabel(service_common_fd(s_info.svc_ctx), BADGE_SMACK_LABEL, SMACK_LABEL_IPOUT) != 0) {
377 if (errno != EOPNOTSUPP) {
378 ErrPrint("Unable to set SMACK label(%d)\n", errno);
379 service_common_destroy(s_info.svc_ctx);
380 s_info.svc_ctx = NULL;
381 return LB_STATUS_ERROR_FAULT;
385 if (smack_fsetlabel(service_common_fd(s_info.svc_ctx), BADGE_SMACK_LABEL, SMACK_LABEL_IPIN) != 0) {
386 if (errno != EOPNOTSUPP) {
387 ErrPrint("Unable to set SMACK label(%d)\n", errno);
388 service_common_destroy(s_info.svc_ctx);
389 s_info.svc_ctx = NULL;
390 return LB_STATUS_ERROR_FAULT;
394 DbgPrint("Successfully initiated\n");
395 return LB_STATUS_SUCCESS;
398 HAPI int badge_service_fini(void)
401 return LB_STATUS_ERROR_INVALID;
403 service_common_destroy(s_info.svc_ctx);
404 s_info.svc_ctx = NULL;
405 DbgPrint("Successfully finalized\n");
406 return LB_STATUS_SUCCESS;