release tizen_2.0 beta
[framework/telephony/tel-plugin-socket_communicator.git] / plugin / src / socket_pb.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 <core_object.h>
33 #include <queue.h>
34 #include <user_request.h>
35 #include <util.h>
36 #include <co_phonebook.h>
37 #include <co_sim.h>
38
39 #include "sipc.h"
40 #include "tapi_common.h"
41 #include "module_req.h"
42
43 gboolean scomm_service_reqeust_pb(unsigned int ch_id, Communicator *c, TcorePlugin *plugin, tapi_service_command_e cmd, gchar *data, void **outparam)
44 {
45         GSList *co_list = NULL;
46         CoreObject *co_pb = NULL;
47         UserRequest *ur = NULL;
48         struct tcore_user_info ui = { 0, 0, 0, NULL,0,0, NULL };
49         int ret = 0;
50
51         gchar *tmp = NULL;
52         int tmp_len = 0;
53         struct _tapi_service_object out_o = {0,};
54         struct _sipc_marshal_object *in_o;
55
56         co_list = tcore_plugin_get_core_objects_bytype(plugin, CORE_OBJECT_TYPE_PHONEBOOK);
57         if (!co_list) {
58                 dbg("err-no phonebook core object list in current plugin");
59                 return FALSE;
60         }
61
62         co_pb = (CoreObject *)co_list->data;
63         if (!co_pb) {
64                 dbg("err-no phonebook core object in current plugin");
65                 return FALSE;
66         }
67
68         g_slist_free(co_list);
69
70 #if 0
71         if (cmd == TAPI_SERVICE_PB_GET_INITINFO ) {
72                 /*synchronous data return case*/
73                 out_o.cmd = cmd;
74                 out_o.mo = sipc_util_marshal_object_create();
75         } else {
76 #endif
77                 /*asynchronous data return case*/
78                 ur = tcore_user_request_new(c, tcore_plugin_get_description(plugin)->name);
79                 if (!ur) {
80                         dbg("err - ur creation failed");
81                         return FALSE;
82                 }
83
84                 ui.channel_id = ch_id;
85                 ui.client_cmd = cmd;
86                 tcore_user_request_set_user_info(ur, &ui);
87
88                 in_o = sipc_util_marshal_object_deserializer(data);
89                 if(in_o){
90                         dbg("err - data deserializer failed");
91                         return FALSE;
92                 }
93 #if 0
94         }
95 #endif
96
97         switch (cmd) {
98 #if 0
99                 case TAPI_SERVICE_PB_GET_INITINFO : {
100                         gboolean g_init;
101                         struct tel_phonebook_support_list *g_pb_list;
102                         g_init = tcore_phonebook_get_status(co_pb);
103                         g_pb_list = (struct tel_phonebook_support_list*)tcore_phonebook_get_support_list(co_pb);
104                         sipc_util_marshal_object_add_data(out_o.mo, "status", (void*)&g_init,   SIPC_MARSHAL_DATA_BOOLEAN_TYPE);
105                         sipc_util_marshal_object_add_data(out_o.mo, "support_fdn", (void*)&g_pb_list->b_fdn, SIPC_MARSHAL_DATA_BOOLEAN_TYPE);
106                         sipc_util_marshal_object_add_data(out_o.mo, "support_adn", (void*)&g_pb_list->b_adn, SIPC_MARSHAL_DATA_BOOLEAN_TYPE);
107                         sipc_util_marshal_object_add_data(out_o.mo, "support_sdn", (void*)&g_pb_list->b_sdn, SIPC_MARSHAL_DATA_BOOLEAN_TYPE);
108                         sipc_util_marshal_object_add_data(out_o.mo, "support_usim", (void*)&g_pb_list->b_usim, SIPC_MARSHAL_DATA_BOOLEAN_TYPE);
109                         sipc_util_marshal_object_add_data(out_o.mo, "support_grp", (void*)&g_pb_list->b_gas, SIPC_MARSHAL_DATA_BOOLEAN_TYPE);
110
111                         tmp = sipc_util_marshal_object_serializer(out_o.mo);
112                         tmp_len = strlen(tmp);
113                         *outparam = g_new0(char, tmp_len+1);
114                         memcpy(*outparam, tmp, tmp_len);
115                         g_free(tmp);
116                 }
117                         break;
118 #endif
119                 case TAPI_SERVICE_PB_GET_COUNT : {
120                         struct treq_phonebook_get_count pb_count;
121                         enum tel_phonebook_type g_pb_type;
122                         g_pb_type = sipc_util_marshal_object_get_int(in_o, "type");
123                         pb_count.phonebook_type = g_pb_type;
124
125                         tcore_user_request_set_data(ur, sizeof(struct treq_phonebook_get_count), &pb_count);
126                         tcore_user_request_set_command(ur, TREQ_PHONEBOOK_GETCOUNT);
127                 }
128                         break;
129
130                 case TAPI_SERVICE_PB_GET_METAINFO : {
131                         struct treq_phonebook_get_info pb_info;
132                         struct treq_phonebook_get_usim_info pb_usim;
133                         enum tel_phonebook_type g_pb_type;
134                         g_pb_type = sipc_util_marshal_object_get_int(in_o, "type");
135                         pb_info.phonebook_type = g_pb_type;
136
137                         tcore_user_request_set_data(ur, sizeof(struct treq_phonebook_get_info), &pb_info);
138                         tcore_user_request_set_command(ur, TREQ_PHONEBOOK_GETMETAINFO);
139                 }
140                         break;
141
142                 case TAPI_SERVICE_PB_READ_RECORD : {
143                         struct treq_phonebook_read_record pb_read;
144                         enum tel_phonebook_type g_pb_type;
145                         gint g_index = 0;
146                         g_index = sipc_util_marshal_object_get_int(in_o, "index");
147                         g_pb_type = sipc_util_marshal_object_get_int(in_o, "type");
148                         pb_read.index = (unsigned short)g_index;
149                         pb_read.phonebook_type = g_pb_type;
150
151                         tcore_user_request_set_data(ur, sizeof(struct treq_phonebook_read_record), &pb_read);
152                         tcore_user_request_set_command(ur, TREQ_PHONEBOOK_READRECORD);
153                 }
154                         break;
155
156                 case TAPI_SERVICE_PB_UPDATE_RECORD : {
157                         struct treq_phonebook_update_record pb_update;
158                         enum tel_phonebook_type g_pb_type;
159                         gint g_index = 0;
160                         gchar *g_name = NULL;
161                         gint g_dcs = 0;
162                         gchar *g_number = NULL;
163                         gint g_ton = 0;
164                         gchar *g_number2 = NULL;
165                         gint g_number2_ton = 0;
166                         gchar *g_number3 = NULL;
167                         gint g_number3_ton = 0;
168                         gchar *g_number4 = NULL;
169                         gint g_number4_ton = 0;
170                         gchar *g_email1 = NULL;
171                         gchar *g_email2 = NULL;
172                         gchar *g_email3 = NULL;
173                         gchar *g_email4 = NULL;
174                         gint g_group_index =0;
175                         g_index = sipc_util_marshal_object_get_int(in_o, "index");
176                         g_pb_type = sipc_util_marshal_object_get_int(in_o, "type");
177                         g_name = sipc_util_marshal_object_get_string(in_o, "name");
178                         g_dcs = sipc_util_marshal_object_get_int(in_o, "dcs");
179                         g_number = sipc_util_marshal_object_get_string(in_o, "number");
180                         g_ton = sipc_util_marshal_object_get_int(in_o, "ton");
181                         g_number2 = sipc_util_marshal_object_get_string(in_o, "number2");
182                         g_number2_ton = sipc_util_marshal_object_get_int(in_o, "number2_ton");
183                         g_number3 = sipc_util_marshal_object_get_string(in_o, "number3");
184                         g_number3_ton = sipc_util_marshal_object_get_int(in_o, "number3_ton");
185                         g_number4 = sipc_util_marshal_object_get_string(in_o, "number4");
186                         g_number4_ton = sipc_util_marshal_object_get_int(in_o, "number4_ton");
187                         g_email1 = sipc_util_marshal_object_get_string(in_o, "email1");
188                         g_email2 = sipc_util_marshal_object_get_string(in_o, "email2");
189                         g_email3 = sipc_util_marshal_object_get_string(in_o, "email3");
190                         g_email4 = sipc_util_marshal_object_get_string(in_o, "email4");
191                         g_group_index = sipc_util_marshal_object_get_int(in_o, "group_index");
192
193                         pb_update.index = (unsigned short)g_index;
194                         pb_update.phonebook_type = g_pb_type;
195                         if(strlen(g_name)){
196                                 snprintf(pb_update.name, strlen(g_name)+1, g_name);
197                                 pb_update.dcs = g_dcs;
198                         }
199                         if(strlen(g_number)){
200                                 snprintf(pb_update.number, strlen(g_number)+1, g_number);
201                                 pb_update.ton = g_ton;
202                         }
203                         if(strlen(g_number2)){
204                                 snprintf(pb_update.anr1, strlen(g_number2)+1, g_number2);
205                                 pb_update.anr1_ton = g_number2_ton;
206                         }
207                         if(strlen(g_number3)){
208                                 snprintf(pb_update.anr2, strlen(g_number3)+1, g_number3);
209                                 pb_update.anr2_ton = g_number3_ton;
210                         }
211                         if(strlen(g_number4)){
212                                 snprintf(pb_update.anr3, strlen(g_number4)+1, g_number4);
213                                 pb_update.anr3_ton = g_number4_ton;
214                         }
215                         pb_update.group_index = (unsigned short)g_group_index;
216
217                         tcore_user_request_set_data(ur, sizeof(struct treq_phonebook_update_record), &pb_update);
218                         tcore_user_request_set_command(ur, TREQ_PHONEBOOK_UPDATERECORD);
219                 }
220                         break;
221
222                 case TAPI_SERVICE_PB_DELETE_RECORD : {
223                         struct treq_phonebook_delete_record pb_delete;
224                         enum tel_phonebook_type g_pb_type;
225                         gint g_index = 0;
226                         g_index = sipc_util_marshal_object_get_int(in_o, "index");
227                         g_pb_type = sipc_util_marshal_object_get_int(in_o, "type");
228                         pb_delete.index = (unsigned short)g_index;
229                         pb_delete.phonebook_type = g_pb_type;
230
231                         tcore_user_request_set_data(ur, sizeof(struct treq_phonebook_delete_record), &pb_delete);
232                         tcore_user_request_set_command(ur, TREQ_PHONEBOOK_DELETERECORD);
233                 }
234                         break;
235
236                 default :
237                         dbg("not handled command [%d] here",cmd);
238                         break;
239         }
240
241 #if 0
242         if (cmd == TAPI_SERVICE_PB_GET_INITINFO  ) {
243                 /*synchronous data return case*/
244         } else {
245 #endif
246                 /*asynchronous data return case*/
247                 ret = tcore_communicator_dispatch_request(c, ur);
248                 if (ret != TCORE_RETURN_SUCCESS) {
249                         return FALSE;
250                 }
251 #if 0
252         }
253 #endif
254
255         return TRUE;
256 }
257
258 gboolean scomm_service_response_pb(Communicator *comm, UserRequest *ur, enum tcore_response_command command,
259                 unsigned int data_len, const void *data)
260 {
261         const struct tresp_phonebook_get_count *resp_pbcnt = data;
262         const struct tresp_phonebook_get_info *resp_entry = data;
263         const struct tresp_phonebook_get_usim_info *resp_capa = data;
264         const struct tresp_phonebook_read_record *resp_pbread = data;
265         const struct tresp_phonebook_update_record *resp_pbupdate = data;
266         const struct tresp_phonebook_delete_record *resp_pbdelete = data;
267         int count =0;
268         int ret = 0, i;
269         gchar *out_d= NULL, *serial_d = NULL;
270         gchar *entry_key = NULL;
271         struct custom_data *ctx = NULL;
272         const struct tcore_user_info *ui;
273         sipc_server_t *s = NULL;
274         struct _tapi_header hdr;
275         struct _sipc_marshal_object* out_o = NULL;
276         struct _sipc_marshal_object* entry_o = NULL;
277
278         memset(&hdr, 0, sizeof(struct _tapi_header));
279
280         ctx = tcore_communicator_ref_user_data(comm);
281         if (!ctx) {
282                 dbg("user_data is NULL");
283                 return FALSE;
284         }
285
286         ui = tcore_user_request_ref_user_info(ur);
287         s = ctx->sk_server;
288
289         dbg("application channel id(%d), Command = [0x%x], data_len = %d", ui->channel_id, command, data_len);
290         hdr.cmd = (tapi_service_command_e)ui->client_cmd;
291
292         out_o = sipc_util_marshal_object_create();
293
294         switch (command) {
295                 case TRESP_PHONEBOOK_GETCOUNT:
296                         dbg("dbus comm - TRESP_PHONEBOOK_GETCOUNT");
297                         sipc_util_marshal_object_add_data(out_o, "result", (void*)&resp_pbcnt->result, SIPC_MARSHAL_DATA_INT_TYPE);
298                         sipc_util_marshal_object_add_data(out_o, "type", (void*)&resp_pbcnt->type, SIPC_MARSHAL_DATA_INT_TYPE);
299                         sipc_util_marshal_object_add_data(out_o, "used", (void*)&resp_pbcnt->used_count, SIPC_MARSHAL_DATA_INT_TYPE);
300                         sipc_util_marshal_object_add_data(out_o, "total", (void*)&resp_pbcnt->total_count, SIPC_MARSHAL_DATA_INT_TYPE);
301                         break;
302
303                 case TRESP_PHONEBOOK_GETMETAINFO:
304                 case TRESP_PHONEBOOK_GETUSIMINFO:
305                         dbg("dbus comm - TRESP_PHONEBOOK_GETMETAINFO");
306                         sipc_util_marshal_object_add_data(out_o, "result", (void*)&resp_entry->result, SIPC_MARSHAL_DATA_INT_TYPE);
307                         sipc_util_marshal_object_add_data(out_o, "type", (void*)&resp_entry->type, SIPC_MARSHAL_DATA_INT_TYPE);
308                         sipc_util_marshal_object_add_data(out_o, "index_min", (void*)&resp_entry->index_min, SIPC_MARSHAL_DATA_INT_TYPE);
309                         sipc_util_marshal_object_add_data(out_o, "index_max", (void*)&resp_entry->index_max, SIPC_MARSHAL_DATA_INT_TYPE);
310                         sipc_util_marshal_object_add_data(out_o, "number_length_max", (void*)&resp_entry->number_length_max, SIPC_MARSHAL_DATA_INT_TYPE);
311                         sipc_util_marshal_object_add_data(out_o, "text_length_max", (void*)&resp_entry->text_length_max, SIPC_MARSHAL_DATA_INT_TYPE);
312                         break;
313
314                 case TRESP_PHONEBOOK_READRECORD:
315                         dbg("dbus comm - TRESP_PHONEBOOK_READRECORD");
316                         sipc_util_marshal_object_add_data(out_o, "result", (void*)&resp_pbread->result, SIPC_MARSHAL_DATA_INT_TYPE);
317                         sipc_util_marshal_object_add_data(out_o, "index", (void*)&resp_pbread->index, SIPC_MARSHAL_DATA_INT_TYPE);
318                         sipc_util_marshal_object_add_data(out_o, "next_index", (void*)&resp_pbread->next_index, SIPC_MARSHAL_DATA_INT_TYPE);
319                         sipc_util_marshal_object_add_data(out_o, "name", (void*)&resp_pbread->name, SIPC_MARSHAL_DATA_STRING_TYPE);
320                         sipc_util_marshal_object_add_data(out_o, "dcs", (void*)&resp_pbread->dcs, SIPC_MARSHAL_DATA_INT_TYPE);
321                         sipc_util_marshal_object_add_data(out_o, "number", (void*)&resp_pbread->number, SIPC_MARSHAL_DATA_STRING_TYPE);
322                         sipc_util_marshal_object_add_data(out_o, "ton", (void*)&resp_pbread->ton, SIPC_MARSHAL_DATA_INT_TYPE);
323                         sipc_util_marshal_object_add_data(out_o, "number2", (void*)&resp_pbread->anr1, SIPC_MARSHAL_DATA_STRING_TYPE);
324                         sipc_util_marshal_object_add_data(out_o, "number2_ton", (void*)&resp_pbread->anr1_ton, SIPC_MARSHAL_DATA_INT_TYPE);
325                         sipc_util_marshal_object_add_data(out_o, "number3", (void*)&resp_pbread->anr2, SIPC_MARSHAL_DATA_STRING_TYPE);
326                         sipc_util_marshal_object_add_data(out_o, "number3_ton", (void*)&resp_pbread->anr2_ton, SIPC_MARSHAL_DATA_INT_TYPE);
327                         sipc_util_marshal_object_add_data(out_o, "number4", (void*)&resp_pbread->anr3, SIPC_MARSHAL_DATA_STRING_TYPE);
328                         sipc_util_marshal_object_add_data(out_o, "number4_ton", (void*)&resp_pbread->anr3_ton, SIPC_MARSHAL_DATA_INT_TYPE);
329                         sipc_util_marshal_object_add_data(out_o, "email1", (void*)&resp_pbread->email1, SIPC_MARSHAL_DATA_STRING_TYPE);
330                         sipc_util_marshal_object_add_data(out_o, "email2", (void*)&resp_pbread->email2, SIPC_MARSHAL_DATA_STRING_TYPE);
331                         sipc_util_marshal_object_add_data(out_o, "email3", (void*)&resp_pbread->email3, SIPC_MARSHAL_DATA_STRING_TYPE);
332                         sipc_util_marshal_object_add_data(out_o, "email4", (void*)&resp_pbread->email4, SIPC_MARSHAL_DATA_STRING_TYPE);
333                         sipc_util_marshal_object_add_data(out_o, "group_index", (void*)&resp_pbread->group_index, SIPC_MARSHAL_DATA_INT_TYPE);
334                         break;
335
336                 case TRESP_PHONEBOOK_UPDATERECORD:
337                         dbg("dbus comm - TRESP_PHONEBOOK_UPDATERECORD");
338                         sipc_util_marshal_object_add_data(out_o, "result", (void*)&resp_pbupdate->result, SIPC_MARSHAL_DATA_INT_TYPE);
339                         break;
340
341                 case TRESP_PHONEBOOK_DELETERECORD:
342                         dbg("dbus comm - TRESP_PHONEBOOK_DELETERECORD");
343                         sipc_util_marshal_object_add_data(out_o, "result", (void*)&resp_pbdelete->result, SIPC_MARSHAL_DATA_INT_TYPE);
344                         break;
345         }
346
347         if(out_o){
348                 serial_d = sipc_util_marshal_object_serializer(out_o);
349                 hdr.data_len = strlen(serial_d);
350                 sipc_util_marshal_object_destory(out_o);
351         }
352
353         out_d = g_new0(char, sizeof(struct _tapi_header)+hdr.data_len);
354         memcpy(out_d, &hdr, sizeof(struct _tapi_header));
355         if(serial_d){
356                 memcpy(out_d+sizeof(struct _tapi_header), serial_d, hdr.data_len);
357                 g_free(serial_d);
358         }
359
360         ret = sipc_server_send(s, ui->channel_id, out_d, sizeof(struct _tapi_header)+hdr.data_len, SIPC_SEND_DATA_ASYNC);
361         g_free(out_d);
362
363         if(ret < 0){
364                 return FALSE;
365         }
366
367         return TRUE;
368 }
369
370
371 gboolean scomm_service_notification_pb(Communicator *comm, CoreObject *source, enum tcore_notification_command command,
372                 unsigned int data_len, const void *data)
373 {
374         const struct tnoti_phonebook_status *n_pb_status = data;
375         int ret = 0;
376         gchar *out_d = NULL, *serial_d = NULL;
377
378         struct custom_data *ctx = NULL;
379         sipc_server_t *s = NULL;
380
381         struct _tapi_header hdr;
382         struct _sipc_marshal_object* out_o = NULL;
383
384         memset(&hdr, 0, sizeof(struct _tapi_header));
385
386         ctx = tcore_communicator_ref_user_data(comm);
387         if (!ctx) {
388                 dbg("user_data is NULL");
389                 return FALSE;
390         }
391
392         s = ctx->sk_server;
393         dbg("notification !!! (command = 0x%x, data_len = %d)", command, data_len);
394
395         out_o = sipc_util_marshal_object_create();
396
397         switch (command) {
398                 case TNOTI_PHONEBOOK_STATUS :
399                         dbg("notified sim_status[%d]", n_pb_status->b_init);
400                         sipc_util_marshal_object_add_data(out_o, "status", (void*)&n_pb_status->b_init, SIPC_MARSHAL_DATA_BOOLEAN_TYPE);
401                         sipc_util_marshal_object_add_data(out_o, "support_fdn", (void*)&n_pb_status->support_list.b_fdn, SIPC_MARSHAL_DATA_BOOLEAN_TYPE);
402                         sipc_util_marshal_object_add_data(out_o, "support_adn", (void*)&n_pb_status->support_list.b_adn, SIPC_MARSHAL_DATA_BOOLEAN_TYPE);
403                         sipc_util_marshal_object_add_data(out_o, "support_sdn", (void*)&n_pb_status->support_list.b_sdn, SIPC_MARSHAL_DATA_BOOLEAN_TYPE);
404                         sipc_util_marshal_object_add_data(out_o, "support_usim", (void*)&n_pb_status->support_list.b_usim, SIPC_MARSHAL_DATA_BOOLEAN_TYPE);
405                         sipc_util_marshal_object_add_data(out_o, "support_grp", (void*)&n_pb_status->support_list.b_gas, SIPC_MARSHAL_DATA_BOOLEAN_TYPE);
406                         break;
407
408                 default :
409                         dbg("not handled command [%d] here",command);
410                         break;
411         }
412
413         if(out_o){
414                 serial_d = sipc_util_marshal_object_serializer(out_o);
415                 hdr.data_len = strlen(serial_d);
416                 sipc_util_marshal_object_destory(out_o);
417         }
418
419         out_d = g_new0(char, sizeof(struct _tapi_header)+hdr.data_len);
420         memcpy(out_d, &hdr, sizeof(struct _tapi_header));
421         if(serial_d){
422                 memcpy(out_d+sizeof(struct _tapi_header), serial_d, hdr.data_len);
423                 g_free(serial_d);
424         }
425
426         ret = sipc_server_broadcast(s, out_d, sizeof(struct _tapi_header)+hdr.data_len);
427         g_free(out_d);
428
429         if(ret < 0){
430                 return FALSE;
431         }
432         return TRUE;
433 }