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>
28 #include "service_common.h"
33 #define BADGE_ADDR "/tmp/.badge.service"
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)
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 service_common_unicast_packet(tcb, packet_reply);
95 packet_destroy(packet_reply);
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);
106 ErrPrint("Failed to get data from the packet");
110 static void _handler_delete_badge(struct tcb *tcb, struct packet *packet, void *data)
113 struct packet *packet_reply = NULL;
114 struct packet *packet_service = NULL;
115 char *pkgname = NULL;
118 if (packet_get(packet, "ss", &pkgname, &caller) == 2) {
119 pkgname = get_string(pkgname);
120 caller = get_string(caller);
122 if (pkgname != NULL && caller != NULL) {
123 ret = badge_db_delete(pkgname, caller);
126 ret = BADGE_ERROR_INVALID_DATA;
129 packet_reply = packet_create_reply(packet, "i", ret);
131 service_common_unicast_packet(tcb, packet_reply);
132 packet_destroy(packet_reply);
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);
143 ErrPrint("Failed to get data from the packet");
147 static void _handler_set_badge_count(struct tcb *tcb, struct packet *packet, void *data)
150 struct packet *packet_reply = NULL;
151 struct packet *packet_service = NULL;
152 char *pkgname = NULL;
156 if (packet_get(packet, "ssi", &pkgname, &caller, &count) == 3) {
157 pkgname = get_string(pkgname);
158 caller = get_string(caller);
160 if (pkgname != NULL && caller != NULL) {
161 ret = badge_db_set_count(pkgname, caller, count);
164 ret = BADGE_ERROR_INVALID_DATA;
167 packet_reply = packet_create_reply(packet, "i", ret);
169 service_common_unicast_packet(tcb, packet_reply);
170 packet_destroy(packet_reply);
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);
181 ErrPrint("Failed to get data from the packet");
185 static void _handler_set_display_option(struct tcb *tcb, struct packet *packet, void *data)
188 struct packet *packet_reply = NULL;
189 struct packet *packet_service = NULL;
190 char *pkgname = NULL;
194 if (packet_get(packet, "ssi", &pkgname, &caller, &is_display) == 3) {
195 pkgname = get_string(pkgname);
196 caller = get_string(caller);
198 if (pkgname != NULL && caller != NULL) {
199 ret = badge_db_set_display_option(pkgname, caller, is_display);
202 ret = BADGE_ERROR_INVALID_DATA;
205 packet_reply = packet_create_reply(packet, "i", ret);
207 service_common_unicast_packet(tcb, packet_reply);
208 packet_destroy(packet_reply);
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);
219 ErrPrint("Failed to get data from the packet");
223 static void _handler_service_register(struct tcb *tcb, struct packet *packet, void *data)
225 struct packet *packet_reply;
228 ret = tcb_client_type_set(tcb, TCB_CLIENT_TYPE_SERVICE);
230 packet_reply = packet_create_reply(packet, "i", ret);
232 service_common_unicast_packet(tcb, packet_reply);
233 packet_destroy(packet_reply);
240 static int service_thread_main(struct tcb *tcb, struct packet *packet, void *data)
244 static struct noti_service service_req_table[] = {
246 .cmd = "insert_badge",
247 .handler = _handler_insert_badge,
250 .cmd = "delete_badge",
251 .handler = _handler_delete_badge,
254 .cmd = "set_badge_count",
255 .handler = _handler_set_badge_count,
258 .cmd = "set_disp_option",
259 .handler = _handler_set_display_option,
262 .cmd = "service_register",
263 .handler = _handler_service_register,
271 DbgPrint("TCB: %p, Packet: %p\n", tcb, packet);
273 command = packet_command(packet);
275 ErrPrint("Invalid command\n");
278 DbgPrint("Command: %s, Packet type[%d]\n", command, packet_type(packet));
280 switch (packet_type(packet)) {
282 /* Need to send reply packet */
283 DbgPrint("REQ: Command: [%s]\n", command);
285 for (i = 0; service_req_table[i].cmd; i++) {
286 if (strcmp(service_req_table[i].cmd, command))
289 service_req_table[i].handler(tcb, packet, data);
294 case PACKET_REQ_NOACK:
299 ErrPrint("Packet type is not valid[%s]\n", command);
304 * return value has no meanning,
305 * it will be printed by dlogutil.
313 * Do not try to do anyother operation in these functions
315 HAPI int badge_service_init(void)
317 if (s_info.svc_ctx) {
318 ErrPrint("Already initialized\n");
319 return LB_STATUS_ERROR_ALREADY;
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;
328 DbgPrint("Successfully initiated\n");
329 return LB_STATUS_SUCCESS;
332 HAPI int badge_service_fini(void)
335 return LB_STATUS_ERROR_INVALID;
337 service_common_destroy(s_info.svc_ctx);
338 DbgPrint("Successfully Finalized\n");
339 return LB_STATUS_SUCCESS;