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.
24 #include <com-core_packet.h>
26 #include <dynamicbox_errno.h>
27 #include <dynamicbox_conf.h>
29 #include "client_life.h"
31 #include "client_rpc.h"
40 * Static component information structure.
43 Eina_List *command_list; /*!< Packet Q: Before sending the request, all request commands will stay here */
44 Ecore_Timer *command_consumer; /*!< This timer will consuming the command Q. sending them to the specified client */
47 .command_consumer = NULL,
51 int handle; /*!< Handler for communication with client */
55 struct packet *packet;
56 struct client_node *client; /*!< Target client. who should receive this command */
61 * Creating or Destroying command object
63 static inline struct command *create_command(struct client_node *client, struct packet *packet)
65 struct command *command;
67 command = calloc(1, sizeof(*command));
69 ErrPrint("Heap: %s\n", strerror(errno));
73 command->packet = packet_ref(packet);
74 command->client = client_ref(client);
79 static inline void destroy_command(struct command *command)
81 client_unref(command->client);
82 packet_unref(command->packet);
86 static inline int count_command(void)
88 return eina_list_count(s_info.command_list);
91 static inline struct command *pop_command(void)
93 struct command *command;
95 command = eina_list_nth(s_info.command_list, 0);
100 s_info.command_list = eina_list_remove(s_info.command_list, command);
104 static Eina_Bool command_consumer_cb(void *data)
106 struct command *command;
107 struct client_rpc *rpc;
110 command = pop_command();
112 s_info.command_consumer = NULL;
113 return ECORE_CALLBACK_CANCEL;
116 if (!command->client) {
117 DbgPrint("Has no client\n");
121 if (client_is_faulted(command->client)) {
122 ErrPrint("Client[%p] is faulted, discard command\n", command->client);
126 rpc = client_data(command->client, RPC_TAG);
128 ErrPrint("Client is not activated\n");
132 if (rpc->handle < 0) {
133 DbgPrint("RPC is not initialized\n");
137 ret = com_core_packet_send_only(rpc->handle, command->packet);
139 ErrPrint("Failed to send packet %d\n", ret);
143 destroy_command(command);
144 return ECORE_CALLBACK_RENEW;
147 static inline void push_command(struct command *command)
149 s_info.command_list = eina_list_append(s_info.command_list, command);
151 if (s_info.command_consumer) {
155 s_info.command_consumer = ecore_timer_add(DYNAMICBOX_CONF_PACKET_TIME, command_consumer_cb, NULL);
156 if (!s_info.command_consumer) {
157 ErrPrint("Failed to add command consumer\n");
158 s_info.command_list = eina_list_remove(s_info.command_list, command);
159 destroy_command(command);
163 HAPI int client_rpc_async_request(struct client_node *client, struct packet *packet)
165 struct command *command;
166 struct client_rpc *rpc;
168 if (!client || !packet) {
169 return DBOX_STATUS_ERROR_INVALID_PARAMETER;
172 if (client_is_faulted(client)) {
173 ErrPrint("Client[%p] is faulted\n", client);
174 packet_unref(packet);
175 return DBOX_STATUS_ERROR_FAULT;
178 rpc = client_data(client, RPC_TAG);
180 ErrPrint("Client[%p] is not ready for communication (%s)\n", client, packet_command(packet));
183 command = create_command(client, packet);
185 packet_unref(packet);
186 return DBOX_STATUS_ERROR_OUT_OF_MEMORY;
189 push_command(command);
190 packet_unref(packet);
191 return DBOX_STATUS_ERROR_NONE;
194 static int deactivated_cb(struct client_node *client, void *data)
196 struct client_rpc *rpc;
197 struct command *command;
201 rpc = client_data(client, RPC_TAG);
203 ErrPrint("client is not valid\n");
204 return DBOX_STATUS_ERROR_NONE;
207 DbgPrint("Reset handle for %d\n", client_pid(client));
210 DbgPrint("Begin: Destroying command\n");
211 EINA_LIST_FOREACH_SAFE(s_info.command_list, l, n, command) {
212 if (command->client == client) {
213 s_info.command_list = eina_list_remove(s_info.command_list, command);
214 destroy_command(command);
217 DbgPrint("End: Destroying command\n");
219 return DBOX_STATUS_ERROR_NONE;
222 HAPI int client_rpc_init(struct client_node *client, int handle)
224 struct client_rpc *rpc;
227 rpc = calloc(1, sizeof(*rpc));
229 ErrPrint("Heap: %s\n", strerror(errno));
230 return DBOX_STATUS_ERROR_OUT_OF_MEMORY;
233 ret = client_set_data(client, RPC_TAG, rpc);
235 ErrPrint("Failed to set \"rpc\" for client\n");
240 DbgPrint("CLIENT: New handle assigned for %d, %d (old: %d)\n", client_pid(client), handle, rpc->handle);
241 rpc->handle = handle;
243 ret = client_event_callback_add(client, CLIENT_EVENT_DEACTIVATE, deactivated_cb, NULL);
245 struct client_rpc *weird;
247 weird = client_del_data(client, RPC_TAG);
249 ErrPrint("What happens? (%p <> %p)\n", weird, rpc);
257 HAPI int client_rpc_fini(struct client_node *client)
259 struct client_rpc *rpc;
261 rpc = client_del_data(client, RPC_TAG);
263 return DBOX_STATUS_ERROR_INVALID_PARAMETER;
266 client_event_callback_del(client, CLIENT_EVENT_DEACTIVATE, deactivated_cb, NULL);
268 return DBOX_STATUS_ERROR_NONE;
271 HAPI int client_rpc_handle(struct client_node *client)
273 struct client_rpc *rpc;
275 rpc = client_data(client, RPC_TAG);
277 DbgPrint("Client has no RPC\n");
278 return DBOX_STATUS_ERROR_INVALID_PARAMETER;