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