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.
21 #include <livebox-errno.h>
26 #include "service_common.h"
31 #define SHORTCUT_ADDR "/tmp/.shortcut.service"
34 Eina_List *context_list;
35 struct service_context *svc_ctx;
37 .context_list = NULL, /*!< \WARN: This is only used for SERVICE THREAD */
38 .svc_ctx = NULL, /*!< \WARN: This is only used for MAIN THREAD */
49 static inline int put_reply_context(struct tcb *tcb, double seq)
53 ctx = malloc(sizeof(*ctx));
55 ErrPrint("Heap: %s\n", strerror(errno));
60 ctx->seq = seq; /* Could we this sequence value is uniq? */
62 s_info.context_list = eina_list_append(s_info.context_list, ctx);
69 static inline struct tcb *get_reply_context(double seq)
77 EINA_LIST_FOREACH_SAFE(s_info.context_list, l, n, ctx) {
81 s_info.context_list = eina_list_remove(s_info.context_list, ctx);
93 static int service_thread_main(struct tcb *tcb, struct packet *packet, void *data)
97 DbgPrint("TCB: %p, Packet: %p\n", tcb, packet);
99 command = packet_command(packet);
101 ErrPrint("Invalid command\n");
104 DbgPrint("Command: %s, Packet type[%d]\n", command, packet_type(packet));
106 switch (packet_type(packet)) {
108 /* Need to send reply packet */
109 DbgPrint("REQ: Command: [%s]\n", command);
110 if (service_common_multicast_packet(tcb, packet, TCB_CLIENT_TYPE_SERVICE) < 0)
111 ErrPrint("Unable to send service request packet\n");
113 (void)put_reply_context(tcb, packet_seq(packet));
115 case PACKET_REQ_NOACK:
116 /* Doesn't need to send reply packet */
117 DbgPrint("REQ_NOACK: Command: [%s]\n", command);
118 if (!strcmp(command, "service_register")) {
119 tcb_client_type_set(tcb, TCB_CLIENT_TYPE_SERVICE);
123 if (service_common_multicast_packet(tcb, packet, TCB_CLIENT_TYPE_SERVICE) < 0)
124 ErrPrint("Unable to send service reuqest packet\n");
127 /* Okay, client(or app) send a reply packet to us. */
128 DbgPrint("ACK: Command: [%s]\n", command);
129 tcb = get_reply_context(packet_seq(packet));
131 ErrPrint("There is no proper context\n");
135 if (service_common_unicast_packet(tcb, packet) < 0)
136 ErrPrint("Unable to send reply packet\n");
139 ErrPrint("Packet type is not valid[%s]\n", command);
144 * return value has no meanning,
145 * it will be printed by dlogutil.
153 * Do not try to do anyother operation in these functions
156 HAPI int shortcut_service_init(void)
158 if (s_info.svc_ctx) {
159 ErrPrint("Already initialized\n");
160 return LB_STATUS_ERROR_ALREADY;
163 s_info.svc_ctx = service_common_create(SHORTCUT_ADDR, service_thread_main, NULL);
164 if (!s_info.svc_ctx) {
165 ErrPrint("Unable to activate service thread\n");
166 return LB_STATUS_ERROR_FAULT;
169 DbgPrint("Successfully initiated\n");
170 return LB_STATUS_SUCCESS;
173 HAPI int shortcut_service_fini(void)
176 return LB_STATUS_ERROR_INVALID;
178 service_common_destroy(s_info.svc_ctx);
179 DbgPrint("Successfully Finalized\n");
180 return LB_STATUS_SUCCESS;