release tizen_2.0 beta
[framework/telephony/tel-plugin-socket_communicator.git] / libclient / src / tapi_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 <security-server.h>
26
27 #include "sipc_log.h"
28 #include "tapi_common_sipc.h"
29 #include "tapi_sipc.h"
30
31 #define SERVER_PATH "/tmp/telephony"
32
33 #if 0
34 static void _free_event(gpointer data)
35 {
36         struct _tapi_event *e = 0;
37
38         e = (struct _tapi_event *) data;
39         if (e->cb_data)
40                 g_free(e->cb_data);
41 }
42 #endif
43
44 static struct _tapi_event* _find_event(GSList *list, unsigned int event)
45 {
46         struct _tapi_event *e = 0;
47
48         while (list) {
49
50                 e = (struct _tapi_event *) list->data;
51                 if (!e)
52                         return 0;
53
54                 if (e->event == event) {
55                         return e;
56                 }
57
58                 list = g_slist_next( list );
59         }
60
61         return 0;
62 }
63
64 static void _dump_cookie(char *data, int size)
65 {
66         char buf[255] = { 0, };
67         char tmp[3] = { 0, };
68         int i = 0;
69
70         for (i = 0; i < size; i++) {
71                 snprintf(tmp, 3, "%02X", data[i]);
72                 strncat(buf, tmp, 3);
73                 if ((i + 1) % 8 == 0)
74                         strncat(buf, "  ", 2);
75         }
76 }
77
78 static gboolean _get_cookie(char *out)
79 {
80         int ret;
81
82         memset(out, 0, 20);
83         ret = security_server_request_cookie(out, 20);
84         if (ret < 0) {
85                 return FALSE;
86         }
87
88         _dump_cookie(out, 20);
89
90         return TRUE;
91 }
92
93 static void _register_name(char* dest, char* src)
94 {
95         /*int len = strlen( src );
96          *dest = g_new0( char, len + 1 );*/
97         memset(dest, 0, 16);
98         memcpy(dest, src, 16);
99 }
100
101 static int _tapi_cb(unsigned int id, void *data, unsigned int data_len, void **_unused_, void *cb_data)
102 {
103         struct _tapi_handle* hdl = (struct _tapi_handle*) cb_data;
104         struct _tapi_header hdr = { 0, };
105         struct _tapi_service_object* o_data = NULL;
106
107         int hdr_len = sizeof(struct _tapi_header);
108         char *d;
109         struct _tapi_event* e = NULL;
110         GSList *l = NULL;
111
112         memcpy(&hdr, (char*) data, hdr_len);
113
114         d = g_new0( char, hdr.data_len );
115         memcpy(d, (char*) (data + hdr_len), hdr.data_len);
116
117         if ((hdr.cmd & 0xF0000000) == TCORE_NOTIFICATION) {
118                 l = hdl->notis;
119                 dbg("notification cmd = 0x%x", hdr.cmd);
120         }
121
122         if ((hdr.cmd & 0xF0000000) == TCORE_REQUEST) {
123                 l = hdl->cfrms;
124                 dbg("response cmd = 0x%x", hdr.cmd);
125         }
126
127         e = _find_event(l, hdr.cmd);
128
129         //extract data;
130         o_data = _tapi_service_object_extract_async_data(d);
131         o_data->cmd = hdr.cmd;
132
133         g_free(d);
134
135         if (e) {
136                 if (e->cb) {
137                         e->cb(o_data, e->cb_data);
138                 }
139                 else {
140                         dbg("e->cb is NULL");
141                 }
142         }
143         else {
144                 dbg("e is NULL");
145         }
146
147         return 1;
148 }
149
150 // API SET
151
152 struct _tapi_handle* tapi_init(char *app_name, char *cp_name)
153 {
154         struct _tapi_handle *h = 0;
155         sipc_callback_t *cb = NULL;
156
157         dbg("tapi init");
158         g_type_init();
159
160         h = g_new0( struct _tapi_handle, 1 );
161
162         // create channel key
163         h->key = sipc_channel_key_create();
164         if (!h->key) {
165                 dbg("key is NULL");
166                 return 0;
167         }
168
169         // create tx channel & init 
170         h->tx_ch = sipc_channel_open(h->key);
171         sipc_channel_set_type(h->tx_ch, SIPC_CHANNEL_TYPE_TX);
172         if (!sipc_channel_connect(h->tx_ch, SERVER_PATH))
173                 return 0;
174
175         // create rx channel & init
176         h->rx_ch = sipc_channel_open(h->key);
177         sipc_channel_set_type(h->rx_ch, SIPC_CHANNEL_TYPE_RX);
178         if (!sipc_channel_connect(h->rx_ch, SERVER_PATH))
179                 return 0;
180
181         cb = g_new0( sipc_callback_t, 1 );
182         cb->func = (sipc_cb) _tapi_cb;
183         cb->data = (void*) h;
184         sipc_channel_register_rx_callback(h->rx_ch, cb);
185
186         if (!_get_cookie(h->cookie))
187                 return 0;
188
189         // register app_name, cp_name
190         _register_name(h->app_name, app_name);
191         _register_name(h->cp_name, cp_name);
192
193         return h;
194 }
195
196 gboolean tapi_deinit(struct _tapi_handle *h)
197 {
198 //      g_free( h->app_name );
199 //      g_free( h->cp_name );
200
201         sipc_channel_close(h->tx_ch, h->key);
202         sipc_channel_close(h->rx_ch, h->key);
203         g_free(h->tx_ch);
204         g_free(h->rx_ch);
205
206         sipc_channel_key_destroy(h->key);
207         g_free(h->key);
208
209         g_slist_free(h->cfrms);
210         g_slist_free(h->notis);
211
212         g_free(h);
213
214         return TRUE;
215 }
216
217 struct _tapi_service_object* tapi_create_service_object(tapi_service_command_e cmd)
218 {
219         struct _tapi_service_object *obj = 0;
220
221         obj = g_new0( struct _tapi_service_object, 1 );
222         obj = _tapi_service_object_create(cmd);
223
224         return obj;
225 }
226
227 gboolean tapi_service_object_add_data(struct _tapi_service_object* obj, const gchar* key, void *in_data,
228                 tapi_object_data_type_e type)
229 {
230         gboolean rv = FALSE;
231
232         rv = _tapi_service_object_add_data(obj, key, in_data, type);
233         if (!rv)
234                 return FALSE;
235
236         return TRUE;
237 }
238
239 tapi_service_command_e tapi_service_object_get_command(const struct _tapi_service_object* obj)
240 {
241         if (!obj)
242                 return TAPI_SERVICE_UNKNOWN;
243
244         return obj->cmd;
245 }
246
247 gboolean tapi_service_object_get_data(const struct _tapi_service_object* obj, const gchar* key, void **out_data,
248                 tapi_object_data_type_e type)
249 {
250         gboolean rv = FALSE;
251
252         rv = _tapi_service_object_get_data(obj, key, out_data, type);
253         if (!rv)
254                 return FALSE;
255
256         return TRUE;
257 }
258
259 gchar tapi_service_object_get_char(const tapi_service_object_t* obj, const gchar* key)
260 {
261         return _tapi_service_object_get_char(obj, key);
262 }
263
264 gboolean tapi_service_object_get_boolean(const tapi_service_object_t* obj, const gchar* key)
265 {
266         return _tapi_service_object_get_boolean(obj, key);
267 }
268
269 gint tapi_service_object_get_int(const tapi_service_object_t* obj, const gchar* key)
270 {
271         return _tapi_service_object_get_int(obj, key);
272 }
273
274 gdouble tapi_service_object_get_double(const tapi_service_object_t* obj, const gchar* key)
275 {
276         return _tapi_service_object_get_double(obj, key);
277 }
278
279 gchar* tapi_service_object_get_string(const tapi_service_object_t* obj, const gchar* key)
280 {
281         return _tapi_service_object_get_string(obj, key);
282 }
283
284 struct _tapi_service_object* tapi_service_object_get_object(const tapi_service_object_t* obj, const gchar* key)
285 {
286         return _tapi_service_object_get_object(obj, key);
287 }
288
289 gboolean tapi_service_object_remove_data(struct _tapi_service_object* obj, const gchar* key)
290 {
291         gboolean rv = FALSE;
292
293         rv = _tapi_service_object_remove_data(obj, key);
294         if (!rv)
295                 return FALSE;
296
297         return TRUE;
298 }
299
300 gboolean tapi_service_object_remove_alldata(struct _tapi_service_object* obj)
301 {
302         gboolean rv = FALSE;
303
304         rv = _tapi_service_object_remove_alldata(obj);
305         if (!rv)
306                 return FALSE;
307
308         return TRUE;
309 }
310
311 gboolean tapi_service_object_destory(struct _tapi_service_object* obj)
312 {
313         gboolean rv = FALSE;
314
315         rv = _tapi_service_object_destory(obj);
316         if (!rv)
317                 return FALSE;
318
319         return TRUE;
320 }
321
322 gboolean tel_processing_command_sync(struct _tapi_handle *h, struct _tapi_service_object *in_obj,
323                 struct _tapi_service_object *out_obj)
324 {
325         gboolean rv = FALSE;
326
327         rv = _tapi_send_service_request_sync(h, in_obj, out_obj);
328         if (!rv)
329                 return FALSE;
330
331         return TRUE;
332 }
333
334 gboolean tel_processing_command_async(struct _tapi_handle *h, struct _tapi_service_object *in_obj, tapi_cb cb,
335                 void *user_data)
336 {
337         gboolean rv = FALSE;
338         struct _tapi_event *e = 0;
339
340         e = g_new0( struct _tapi_event, 1 );
341         e->event = in_obj->cmd;
342         e->cb = cb;
343         e->cb_data = user_data;
344
345         if( e->event & TCORE_REQUEST){
346                 h->cfrms = g_slist_append(h->cfrms, e);
347         }
348         else{
349                 return FALSE;
350         }
351
352         rv = _tapi_send_service_request_sync(h, in_obj, NULL);
353         if (!rv)
354                 return FALSE;
355
356         return TRUE;
357 }
358
359 gboolean tapi_register_notification(struct _tapi_handle *h, tapi_service_notification_e noti, tapi_cb cb, void *cb_data)
360 {
361         struct _tapi_event *e = 0;
362
363         e = g_new0( struct _tapi_event, 1 );
364         e->event = noti;
365         e->cb = cb;
366         e->cb_data = cb_data;
367
368         if ((e->event & 0xF0000000) == TCORE_NOTIFICATION) {
369                 h->notis = g_slist_append(h->notis, e);
370         }
371         else {
372                 return FALSE;
373         }
374
375         return TRUE;
376 }
377