1d0d75335d875dbdf09d7793c17acb2166fbd668
[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_SAP:
62                         dbg("sap request");
63                         scomm_service_reqeust_sap(ch_id, d->comm, plugin, hdr->cmd, data, outparam);
64                         break;
65
66                 case TCORE_TYPE_PHONEBOOK:
67                         dbg("phonebook request");
68                         scomm_service_reqeust_pb(ch_id, d->comm, plugin, hdr->cmd, data, outparam);
69                         break;
70
71                 case TCORE_TYPE_NETWORK:
72                         dbg("network request");
73                         scomm_service_request_network(ch_id, d->comm, plugin, hdr->cmd, data, outparam);
74                         break;
75
76                 case TCORE_TYPE_PS:
77                         dbg("packet service request");
78                         scomm_service_request_ps(ch_id, d->comm, plugin, hdr->cmd, data, outparam);
79                         break;
80
81                 case TCORE_TYPE_SMS:
82                         dbg("sms request");
83                         scomm_service_request_sms(ch_id, d->comm, plugin, hdr->cmd, data, outparam);
84                         break;
85
86                 case TCORE_TYPE_CUSTOM:
87                         dbg("custom request");
88                         scomm_service_request_custom(ch_id, d->comm, plugin, hdr->cmd, data, outparam);
89                         break;
90
91                 default:
92                         dbg("unknown request (0x%x)", hdr->cmd);
93                         break;
94         }
95
96         return 1;
97 }
98
99 static gboolean sipc_send_response(Communicator *comm, UserRequest *ur, enum tcore_response_command command, unsigned int data_len, const void *data)
100 {
101         switch (command & (TCORE_RESPONSE | 0x0FF00000)) {
102                 case TRESP_CALL:
103                         dbg("TRESP_CALL");
104                         scomm_service_response_call(comm, ur, command, data_len, data);
105                         break;
106
107                 case TRESP_PS:
108                         dbg("TRESP_PS");
109                         scomm_service_response_ps(comm, ur, command, data_len, data);
110                         break;
111
112                 case TRESP_SIM:
113                         dbg("TRESP_SIM");
114                         scomm_service_response_sim(comm, ur, command, data_len, data);
115                         break;
116
117                 case TRESP_SAP:
118                         dbg("TRESP_SAP");
119                         scomm_service_response_sap(comm, ur, command, data_len, data);
120                         break;
121
122                 case TRESP_PHONEBOOK:
123                         dbg("TRESP_PHONEBOOK");
124                         scomm_service_response_pb(comm, ur, command, data_len, data);
125                         break;
126
127                 case TRESP_NETWORK:
128                         dbg("TRESP_NETWORK");
129                         scomm_service_response_network(comm, ur, command, data_len, data);
130                         break;
131
132                 case TRESP_MODEM:
133                         dbg("TRESP_MODEM");
134                         break;
135
136                 case TRESP_SMS:
137                         dbg("TRESP_SMS");
138                         scomm_service_response_sms(comm, ur, command, data_len, data);
139                         break;
140
141                 case TRESP_CUSTOM:
142                         dbg("TRESP_CUSTOM");
143                         scomm_service_response_custom(comm, ur, command, data_len, data);
144                         break;
145
146                 default:
147                         warn("unknown command (0x%x)", command);
148                         break;
149         }
150
151         return TRUE;
152 }
153
154 static gboolean sipc_send_notification(Communicator *comm, CoreObject *source, enum tcore_notification_command command, unsigned int data_len, const void *data)
155 {
156         switch (command & (TCORE_NOTIFICATION | 0x0FF00000)) {
157                 case TNOTI_CALL:
158                         dbg("TNOTI_CALL");
159                         scomm_service_notification_call(comm, source, command, data_len, data);
160                         break;
161
162                 case TNOTI_PS:
163                         dbg("TNOTI_PS");
164                         scomm_service_notification_ps(comm, source, command, data_len, data);
165                         break;
166
167                 case TNOTI_SIM:
168                         dbg("TNOTI_SIM");
169                         scomm_service_notification_sim(comm, source, command, data_len, data);
170                         break;
171
172                 case TNOTI_SAP:
173                         dbg("TNOTI_SAP");
174                         scomm_service_notification_sap(comm, source, command, data_len, data);
175                         break;
176
177                 case TNOTI_PHONEBOOK:
178                         dbg("TNOTI_PHONEBOOK");
179                         scomm_service_notification_pb(comm, source, command, data_len, data);
180                         break;
181
182                 case TNOTI_NETWORK:
183                         dbg("TNOTI_NETWORK");
184                         scomm_service_notification_network(comm, source, command, data_len, data);
185                         break;
186
187                 case TNOTI_SERVER:
188                         dbg("TNOTI_SERVER");
189                         return TRUE;
190
191                 case TNOTI_MODEM:
192                         dbg("TNOTI_MODEM");
193                         break;
194
195                 case TNOTI_SMS:
196                         dbg("TNOTI_SMS");
197                         scomm_service_notification_sms(comm, source, command, data_len, data);
198                         break;
199
200                 case TNOTI_CUSTOM:
201                         dbg("TNOTI_CUSTOM");
202                         scomm_service_notification_custom(comm, source, command, data_len, data);
203                         break;
204
205                 default:
206                         warn("unknown command (0x%x)", command);
207                         break;
208         }
209
210         return TRUE;
211 }
212
213 struct tcore_communitor_operations ops = {
214         .send_response = sipc_send_response,
215         .send_notification = sipc_send_notification,
216 };
217
218 static int on_recv( unsigned int ch_id, void* inparam, unsigned int inparam_len, void **outparam, void *cb_data )
219 {
220         int rv = 1;
221         int o_data_len = 0;
222         gchar *data = NULL;
223         gchar *o_data = NULL;
224         struct _tapi_header hdr;
225
226         memcpy(&hdr, inparam, sizeof(struct _tapi_header));
227         data = g_new0(char , hdr.data_len+1);
228         memcpy(data, (inparam+sizeof(struct _tapi_header)), hdr.data_len );
229
230         if( !(hdr.cmd & TCORE_REQUEST) ){
231                 dbg("does not request command");
232                 return -1;
233         }
234
235         //check cookie, if cookie is wrong return -1 ( send fail? , or return another?)
236 /*      if( cookie does not matched)
237         {
238                 dbg("cookie is not matched");
239                 return -1;
240         }*/
241
242 #if 1
243         rv = _dispatch_service_request(ch_id, &hdr, data, (void **)&o_data, cb_data);
244         g_free(data);
245
246         o_data_len = strlen(o_data);
247         *outparam = g_new0(char, o_data_len);
248         memcpy(*outparam, o_data, o_data_len);
249         g_free(o_data);
250 #else
251         *outparam = g_new0(char, hdr.data_len+1);
252         memcpy(*outparam, data, hdr.data_len);
253 #endif
254
255         return rv;
256 }
257
258 static gboolean on_load(void)
259 {
260         dbg("socket communicator plug-in load!");
261         return TRUE;
262 }
263
264 static gboolean on_init(TcorePlugin *p)
265 {
266         gboolean rv = FALSE;
267
268         Communicator *comm;
269         struct custom_data *data = NULL;
270         sipc_server_t *sk_server = NULL;
271         sipc_callback_t *cbck = NULL;
272
273         if (!p)
274                 return FALSE;
275
276         g_type_init();
277
278         dbg("func entrance");
279
280         sk_server = sipc_server_open(SERVER_PATH);
281         if (!sk_server) {
282                 warn("error : fail to open server");
283                 return FALSE;
284         }
285
286         comm = tcore_communicator_new(p, "socket_communicator", &ops);
287
288         data = g_new0( struct custom_data, 1 );
289         data->server = tcore_plugin_ref_server(p);
290         data->plugin = p;
291         data->comm = comm;
292         data->sk_server = (void*) sk_server;
293         tcore_communicator_link_user_data(comm, data);
294
295         cbck = g_new0( sipc_callback_t, 1 );
296         cbck->func = on_recv;
297         cbck->data = data;
298         sipc_server_register_rx_callback(sk_server, cbck);
299
300         rv = sipc_server_run(sk_server);
301         if (!rv)
302                 return FALSE;
303
304         dbg("server is running");
305         return TRUE;
306 }
307
308 static void on_unload(TcorePlugin *p)
309 {
310         dbg("i'm unload!");
311         return;
312 }
313
314 struct tcore_plugin_define_desc plugin_define_desc =
315 {
316         .name     = "SOCKET_COMMUNICATOR",
317         .priority = TCORE_PLUGIN_PRIORITY_HIGH,
318         .version  = 1,
319         .load     = on_load,
320         .init     = on_init,
321         .unload   = on_unload,
322 };