release tizen_2.0 beta
[framework/telephony/tel-plugin-socket_communicator.git] / plugin / src / desc-sipc.c
1 /*
2  * tel-plugin-socket-communicator
3  *
4  * Copyright (c) 2012 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Ja-young Gu <jygu@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20
21 #include <stdio.h>
22 #include <string.h>
23 #include <stdlib.h>
24 #include <errno.h>
25 #include <glib-object.h>
26
27 #include <tcore.h>
28 #include <server.h>
29 #include <plugin.h>
30 #include <hal.h>
31 #include <communicator.h>
32 #include <queue.h>
33 #include <user_request.h>
34 #include <util.h>
35
36 #include "sipc.h"
37 #include "tapi_common.h"
38 #include "module_req.h"
39
40 #define SERVER_PATH "/tmp/telephony"
41
42
43 static int _dispatch_service_request(unsigned int ch_id, struct _tapi_header *hdr, gchar *data, void **outparam, void *cb_data)
44 {
45         TcorePlugin *plugin = NULL;
46         struct custom_data *d = (struct custom_data*)cb_data;
47
48         plugin = tcore_server_find_plugin(d->server, hdr->cp_name);
49
50         switch (hdr->cmd & 0x0FF00000) {
51                 case TCORE_TYPE_CALL:
52                         dbg("call request");
53                         scomm_service_reqeust_call(ch_id, d->comm, plugin, hdr->cmd, data, outparam);
54                         break;
55
56                 case TCORE_TYPE_SIM:
57                         dbg("sim request");
58                         scomm_service_reqeust_sim(ch_id, d->comm, plugin, hdr->cmd, data, outparam);
59                         break;
60
61                 case TCORE_TYPE_SAT:
62                         dbg("sat request");
63                         scomm_service_reqeust_sat(ch_id, d->comm, plugin, hdr->cmd, data, outparam);
64                         break;
65
66                 case TCORE_TYPE_SAP:
67                         dbg("sap request");
68                         scomm_service_reqeust_sap(ch_id, d->comm, plugin, hdr->cmd, data, outparam);
69                         break;
70
71                 case TCORE_TYPE_PHONEBOOK:
72                         dbg("phonebook request");
73                         scomm_service_reqeust_pb(ch_id, d->comm, plugin, hdr->cmd, data, outparam);
74                         break;
75
76                 case TCORE_TYPE_NETWORK:
77                         dbg("network request");
78                         scomm_service_request_network(ch_id, d->comm, plugin, hdr->cmd, data, outparam);
79                         break;
80
81                 case TCORE_TYPE_PS:
82                         dbg("packet service request");
83                         scomm_service_request_ps(ch_id, d->comm, plugin, hdr->cmd, data, outparam);
84                         break;
85
86                 case TCORE_TYPE_SMS:
87                         dbg("sms request");
88                         scomm_service_request_sms(ch_id, d->comm, plugin, hdr->cmd, data, outparam);
89                         break;
90
91                 case TCORE_TYPE_CUSTOM:
92                         dbg("custom request");
93                         scomm_service_request_custom(ch_id, d->comm, plugin, hdr->cmd, data, outparam);
94                         break;
95
96                 default:
97                         dbg("unknown request (0x%x)", hdr->cmd);
98                         break;
99         }
100
101         return 1;
102 }
103
104 static gboolean sipc_send_response(Communicator *comm, UserRequest *ur, enum tcore_response_command command, unsigned int data_len, const void *data)
105 {
106         switch (command & (TCORE_RESPONSE | 0x0FF00000)) {
107                 case TRESP_CALL:
108                         dbg("TRESP_CALL");
109                         scomm_service_response_call(comm, ur, command, data_len, data);
110                         break;
111
112                 case TRESP_PS:
113                         dbg("TRESP_PS");
114                         scomm_service_response_ps(comm, ur, command, data_len, data);
115                         break;
116
117                 case TRESP_SIM:
118                         dbg("TRESP_SIM");
119                         scomm_service_response_sim(comm, ur, command, data_len, data);
120                         break;
121
122                 case TRESP_SAP:
123                         dbg("TRESP_SAP");
124                         scomm_service_response_sap(comm, ur, command, data_len, data);
125                         break;
126
127                 case TRESP_PHONEBOOK:
128                         dbg("TRESP_PHONEBOOK");
129                         scomm_service_response_pb(comm, ur, command, data_len, data);
130                         break;
131
132                 case TRESP_NETWORK:
133                         dbg("TRESP_NETWORK");
134                         scomm_service_response_network(comm, ur, command, data_len, data);
135                         break;
136
137                 case TRESP_MODEM:
138                         dbg("TRESP_MODEM");
139                         break;
140
141                 case TRESP_SMS:
142                         dbg("TRESP_SMS");
143                         scomm_service_response_sms(comm, ur, command, data_len, data);
144                         break;
145
146                 case TRESP_CUSTOM:
147                         dbg("TRESP_CUSTOM");
148                         scomm_service_response_custom(comm, ur, command, data_len, data);
149                         break;
150
151                 default:
152                         warn("unknown command (0x%x)", command);
153                         break;
154         }
155
156         return TRUE;
157 }
158
159 static gboolean sipc_send_notification(Communicator *comm, CoreObject *source, enum tcore_notification_command command, unsigned int data_len, const void *data)
160 {
161         switch (command & (TCORE_NOTIFICATION | 0x0FF00000)) {
162                 case TNOTI_CALL:
163                         dbg("TNOTI_CALL");
164                         scomm_service_notification_call(comm, source, command, data_len, data);
165                         break;
166
167                 case TNOTI_PS:
168                         dbg("TNOTI_PS");
169                         scomm_service_notification_ps(comm, source, command, data_len, data);
170                         break;
171
172                 case TNOTI_SIM:
173                         dbg("TNOTI_SIM");
174                         scomm_service_notification_sim(comm, source, command, data_len, data);
175                         break;
176
177                 case TNOTI_SAP:
178                         dbg("TNOTI_SAP");
179                         scomm_service_notification_sap(comm, source, command, data_len, data);
180                         break;
181
182                 case TNOTI_PHONEBOOK:
183                         dbg("TNOTI_PHONEBOOK");
184                         scomm_service_notification_pb(comm, source, command, data_len, data);
185                         break;
186
187                 case TNOTI_NETWORK:
188                         dbg("TNOTI_NETWORK");
189                         scomm_service_notification_network(comm, source, command, data_len, data);
190                         break;
191
192                 case TNOTI_SERVER:
193                         dbg("TNOTI_SERVER");
194                         return TRUE;
195
196                 case TNOTI_MODEM:
197                         dbg("TNOTI_MODEM");
198                         break;
199
200                 case TNOTI_SMS:
201                         dbg("TNOTI_SMS");
202                         scomm_service_notification_sms(comm, source, command, data_len, data);
203                         break;
204
205                 case TNOTI_CUSTOM:
206                         dbg("TNOTI_CUSTOM");
207                         scomm_service_notification_custom(comm, source, command, data_len, data);
208                         break;
209
210                 default:
211                         warn("unknown command (0x%x)", command);
212                         break;
213         }
214
215         return TRUE;
216 }
217
218 struct tcore_communitor_operations ops = {
219         .send_response = sipc_send_response,
220         .send_notification = sipc_send_notification,
221 };
222
223 static int on_recv( unsigned int ch_id, void* inparam, unsigned int inparam_len, void **outparam, void *cb_data )
224 {
225         int rv = 1;
226         int o_data_len = 0;
227         gchar *data = NULL;
228         gchar *o_data = NULL;
229         struct _tapi_header hdr;
230
231         memcpy(&hdr, inparam, sizeof(struct _tapi_header));
232         data = g_new0(char , hdr.data_len+1);
233         memcpy(data, (inparam+sizeof(struct _tapi_header)), hdr.data_len );
234
235         if( !(hdr.cmd & TCORE_REQUEST) ){
236                 dbg("does not request command");
237                 return -1;
238         }
239
240         //check cookie, if cookie is wrong return -1 ( send fail? , or return another?)
241 /*      if( cookie does not matched)
242         {
243                 dbg("cookie is not matched");
244                 return -1;
245         }*/
246
247 #if 1
248         rv = _dispatch_service_request(ch_id, &hdr, data, (void **)&o_data, cb_data);
249         g_free(data);
250
251         o_data_len = strlen(o_data);
252         *outparam = g_new0(char, o_data_len);
253         memcpy(*outparam, o_data, o_data_len);
254         g_free(o_data);
255 #else
256         *outparam = g_new0(char, hdr.data_len+1);
257         memcpy(*outparam, data, hdr.data_len);
258 #endif
259
260         return rv;
261 }
262
263 static gboolean on_load(void)
264 {
265         dbg("socket communicator plug-in load!");
266         return TRUE;
267 }
268
269 static gboolean on_init(TcorePlugin *p)
270 {
271         gboolean rv = FALSE;
272
273         Communicator *comm;
274         struct custom_data *data = NULL;
275         sipc_server_t *sk_server = NULL;
276         sipc_callback_t *cbck = NULL;
277
278         if (!p)
279                 return FALSE;
280
281         g_type_init();
282
283         dbg("func entrance");
284
285         sk_server = sipc_server_open(SERVER_PATH);
286         if (!sk_server) {
287                 warn("error : fail to open server");
288                 return FALSE;
289         }
290
291         comm = tcore_communicator_new(p, "socket_communicator", &ops);
292
293         data = g_new0( struct custom_data, 1 );
294         data->server = tcore_plugin_ref_server(p);
295         data->plugin = p;
296         data->comm = comm;
297         data->sk_server = (void*) sk_server;
298         tcore_communicator_link_user_data(comm, data);
299
300         cbck = g_new0( sipc_callback_t, 1 );
301         cbck->func = on_recv;
302         cbck->data = data;
303         sipc_server_register_rx_callback(sk_server, cbck);
304
305         rv = sipc_server_run(sk_server);
306         if (!rv)
307                 return FALSE;
308
309         dbg("server is running");
310         return TRUE;
311 }
312
313 static void on_unload(TcorePlugin *p)
314 {
315         dbg("i'm unload!");
316         return;
317 }
318
319 struct tcore_plugin_define_desc plugin_define_desc =
320 {
321         .name     = "SOCKET_COMMUNICATOR",
322         .priority = TCORE_PLUGIN_PRIORITY_HIGH,
323         .version  = 1,
324         .load     = on_load,
325         .init     = on_init,
326         .unload   = on_unload,
327 };