add missing files for building
[profile/ivi/tel-plugin-dbus_tapi.git] / src / desc-dbus.c
1 /*
2  * tel-plugin-dbus-tapi
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 <pthread.h>
24 #include <unistd.h>
25 #include <stdlib.h>
26 #include <time.h>
27 #include <glib.h>
28 #include <glib-object.h>
29 #include <gio/gio.h>
30
31 #include <tcore.h>
32 #include <server.h>
33 #include <plugin.h>
34 #include <hal.h>
35 #include <communicator.h>
36 #include <storage.h>
37 #include <queue.h>
38 #include <user_request.h>
39 #include <co_network.h>
40 #include <co_sim.h>
41 #include <co_ps.h>
42
43 #ifndef PLUGIN_VERSION
44 #define PLUGIN_VERSION 1
45 #endif
46
47 #include "generated-code.h"
48 #include "common.h"
49
50 static void add_modem(struct custom_data *ctx, TcorePlugin *p)
51 {
52         TelephonyObjectSkeleton *object;
53         char *plugin_name = NULL;
54         char *path = NULL;
55         GSList *co_list = NULL;
56
57         plugin_name = tcore_plugin_ref_plugin_name(p);
58         if (!plugin_name)
59                 return;
60
61         path = g_strdup_printf("%s/%s", MY_DBUS_PATH, plugin_name);
62         dbg("path = [%s]", path);
63
64         object = g_hash_table_lookup(ctx->objects, path);
65         if (object) {
66                 dbg("dbus interface object already created. (object = %p)", object);
67                 goto OUT;
68         }
69
70         object = telephony_object_skeleton_new(path);
71         dbg("new dbus object created. (object = %p)", object);
72         g_hash_table_insert(ctx->objects, g_strdup(path), object);
73
74         /* Add interfaces */
75         dbus_plugin_setup_modem_interface(object, ctx);
76
77         co_list = tcore_plugin_get_core_objects_bytype(p, CORE_OBJECT_TYPE_CALL);
78         if (co_list) {
79                 g_slist_free(co_list);
80                 dbus_plugin_setup_call_interface(object, ctx);
81         }
82
83         co_list = tcore_plugin_get_core_objects_bytype(p, CORE_OBJECT_TYPE_NETWORK);
84         if (co_list) {
85                 g_slist_free(co_list);
86                 dbus_plugin_setup_network_interface(object, ctx);
87         }
88
89         co_list = tcore_plugin_get_core_objects_bytype(p, CORE_OBJECT_TYPE_SS);
90         if (co_list) {
91                 g_slist_free(co_list);
92                 dbus_plugin_setup_ss_interface(object, ctx);
93         }
94
95         co_list = tcore_plugin_get_core_objects_bytype(p, CORE_OBJECT_TYPE_SMS);
96         if (co_list) {
97                 g_slist_free(co_list);
98                 dbus_plugin_setup_sms_interface(object, ctx);
99         }
100
101         co_list = tcore_plugin_get_core_objects_bytype(p, CORE_OBJECT_TYPE_SAT);
102         if (co_list) {
103                 g_slist_free(co_list);
104                 dbus_plugin_setup_sat_interface(object, ctx);
105         }
106
107         co_list = tcore_plugin_get_core_objects_bytype(p, CORE_OBJECT_TYPE_PHONEBOOK);
108         if (co_list) {
109                 g_slist_free(co_list);
110                 dbus_plugin_setup_phonebook_interface(object, ctx);
111         }
112
113         co_list = tcore_plugin_get_core_objects_bytype(p, CORE_OBJECT_TYPE_SAP);
114         if (co_list) {
115                 g_slist_free(co_list);
116                 dbus_plugin_setup_sap_interface(object, ctx);
117         }
118
119         co_list = tcore_plugin_get_core_objects_bytype(p, CORE_OBJECT_TYPE_SIM);
120         if (co_list) {
121                 g_slist_free(co_list);
122                 dbus_plugin_setup_sim_interface(object, ctx);
123         }
124
125         g_dbus_object_manager_server_export (ctx->manager, G_DBUS_OBJECT_SKELETON (object));
126
127 OUT:
128         if (path)
129                 g_free(path);
130 }
131
132 static void refresh_object(struct custom_data *ctx)
133 {
134         GSList *plugins;
135         GSList *cur;
136         GSList *co_list;
137         TcorePlugin *p;
138
139         if (!ctx->manager) {
140                 dbg("not ready..");
141                 return;
142         }
143
144         plugins = tcore_server_ref_plugins(ctx->server);
145         if (!plugins)
146                 return;
147
148         cur = plugins;
149         for (cur = plugins; cur; cur = cur->next) {
150                 p = cur->data;
151                 if (!p)
152                         continue;
153
154                 co_list = tcore_plugin_get_core_objects_bytype(p, CORE_OBJECT_TYPE_MODEM);
155                 if (!co_list)
156                         continue;
157
158                 if (!tcore_object_get_hal(co_list->data)) {
159                         g_slist_free(co_list);
160                         continue;
161                 }
162
163                 g_slist_free(co_list);
164
165                 add_modem(ctx, p);
166         }
167 }
168
169 static TReturn send_response(Communicator *comm, UserRequest *ur, enum tcore_response_command command, unsigned int data_len, const void *data)
170 {
171         struct custom_data *ctx = NULL;
172         const struct tcore_user_info *ui;
173
174         dbg("Response Command = [0x%x], data_len = %d", command, data_len);
175
176         ctx = tcore_communicator_ref_user_data(comm);
177         if (!ctx) {
178                 dbg("user_data is NULL");
179                 return FALSE;
180         }
181
182         ui = tcore_user_request_ref_user_info(ur);
183
184         switch (command & (TCORE_RESPONSE | 0x0FF00000)) {
185                 case TRESP_CALL:
186                         dbus_plugin_call_response(ctx, ur, ui->user_data, command, data_len, data);
187                         break;
188
189                 case TRESP_SS:
190                         dbus_plugin_ss_response(ctx, ur, ui->user_data, command, data_len, data);
191                         break;
192
193                 case TRESP_PS:
194                         break;
195
196                 case TRESP_SIM:
197                         dbus_plugin_sim_response(ctx, ur, ui->user_data, command, data_len, data);
198                         break;
199
200                 case TRESP_SAP:
201                         dbus_plugin_sap_response(ctx, ur, ui->user_data, command, data_len, data);
202                         break;
203
204                 case TRESP_PHONEBOOK:
205                         dbus_plugin_phonebook_response(ctx, ur, ui->user_data, command, data_len, data);
206                         break;
207
208                 case TRESP_MODEM:
209                         dbus_plugin_modem_response(ctx, ur, ui->user_data, command, data_len, data);
210                         break;
211
212                 case TRESP_SMS:
213                         dbus_plugin_sms_response(ctx, ur, ui->user_data, command, data_len, data);
214                         break;
215
216                 case TRESP_SAT:
217                         dbus_plugin_sat_response(ctx, ur, ui->user_data, command, data_len, data);
218                         break;
219                 case TRESP_CUSTOM:
220                         break;
221
222                 case TRESP_NETWORK:
223                         dbus_plugin_network_response(ctx, ur, ui->user_data, command, data_len, data);
224                         break;
225
226                 default:
227                         warn("unknown command (0x%x)", command);
228                         break;
229         }
230
231         return FALSE;
232 }
233
234 static TReturn send_notification(Communicator *comm, CoreObject *source, enum tcore_notification_command command, unsigned int data_len, const void *data)
235 {
236         struct custom_data *ctx = NULL;
237         char *plugin_name;
238         char *path;
239         TelephonyObjectSkeleton *object;
240
241         dbg("notification !!! (command = 0x%x, data_len = %d)", command, data_len);
242
243         ctx = tcore_communicator_ref_user_data(comm);
244         if (!ctx) {
245                 dbg("user_data is NULL");
246                 return FALSE;
247         }
248
249         plugin_name = tcore_plugin_ref_plugin_name(tcore_object_ref_plugin(source));
250         if (plugin_name) {
251                 path = g_strdup_printf("%s/%s", MY_DBUS_PATH, plugin_name);
252         }
253         else {
254                 path = g_strdup_printf("%s", MY_DBUS_PATH);
255         }
256         dbg("path = [%s]", path);
257
258         object = g_hash_table_lookup(ctx->objects, path);
259         dbg("dbus inteface object = %p", object);
260
261         switch (command & (TCORE_NOTIFICATION | 0x0FF00000)) {
262                 case TNOTI_CALL:
263                         dbus_plugin_call_notification(ctx, plugin_name, object, command, data_len, data);
264                         break;
265
266                 case TNOTI_SS:
267                         dbus_plugin_ss_notification(ctx, plugin_name, object, command, data_len, data);
268                         break;
269
270                 case TNOTI_PS:
271                         break;
272
273                 case TNOTI_SIM:
274                         dbus_plugin_sim_notification(ctx, plugin_name, object, command, data_len, data);
275                         break;
276
277                 case TNOTI_SAP:
278                         dbus_plugin_sap_notification(ctx, plugin_name, object, command, data_len, data);
279                         break;
280
281                 case TNOTI_PHONEBOOK:
282                         dbus_plugin_phonebook_notification(ctx, plugin_name, object, command, data_len, data);
283                         break;
284
285                 case TNOTI_MODEM:
286                         dbus_plugin_modem_notification(ctx, plugin_name, object, command, data_len, data);
287                         break;
288
289                 case TNOTI_SMS:
290                         dbus_plugin_sms_notification(ctx, plugin_name, object, command, data_len, data);
291                         break;
292
293                 case TNOTI_SAT:
294                         dbus_plugin_sat_notification(ctx, plugin_name, object, command, data_len, data);
295                         break;
296                 case TNOTI_CUSTOM:
297                         break;
298
299                 case TNOTI_NETWORK:
300                         dbus_plugin_network_notification(ctx, plugin_name, object, command, data_len, data);
301                         break;
302
303                 case TNOTI_SERVER:
304                         if (command == TNOTI_SERVER_RUN) {
305                                 refresh_object(ctx);
306                         }
307                         break;
308
309                 default:
310                         warn("unknown command (0x%x)", command);
311                         break;
312         }
313
314         return FALSE;
315 }
316
317
318 static gboolean
319 on_manager_getmodems (TelephonyManager *mgr,
320                 GDBusMethodInvocation  *invocation,
321                 gpointer                user_data)
322 {
323         struct custom_data *ctx = user_data;
324         GSList *plugins;
325         GSList *cur;
326         GSList *co_list;
327         int max_count = 0;
328         int count;
329         TcorePlugin *p;
330         gchar **list;
331
332         plugins = tcore_server_ref_plugins(ctx->server);
333         if (!plugins) {
334                 telephony_manager_complete_get_modems(mgr, invocation, NULL);
335                 return TRUE;
336         }
337
338         max_count = g_slist_length(plugins);
339         list = calloc(sizeof(gchar *) * max_count, 1);
340         count = 0;
341
342         cur = plugins;
343         for (cur = plugins; cur; cur = cur->next) {
344                 p = cur->data;
345                 if (!p)
346                         continue;
347
348                 co_list = tcore_plugin_get_core_objects_bytype(p, CORE_OBJECT_TYPE_MODEM);
349                 if (!co_list)
350                         continue;
351
352                 if (!tcore_object_get_hal(co_list->data)) {
353                         g_slist_free(co_list);
354                         continue;
355                 }
356                 g_slist_free(co_list);
357
358                 list[count] = g_strdup(tcore_plugin_get_description(p)->name);
359                 count++;
360         }
361
362         telephony_manager_complete_get_modems(mgr, invocation, (const gchar **)list);
363
364         for (;count >= 0; count--) {
365                 if (list[count]) {
366                         g_free(list[count]);
367                 }
368         }
369         free(list);
370
371         return TRUE;
372 }
373
374 static void on_bus_acquired(GDBusConnection *conn, const gchar *name, gpointer user_data)
375 {
376         gboolean rv = FALSE;
377         gpointer handle = NULL;
378         static Storage *strg;
379         struct custom_data *ctx = user_data;
380         TelephonyManager *mgr;
381
382         dbg("dbus registered");
383
384         ctx->manager = g_dbus_object_manager_server_new (MY_DBUS_PATH);
385
386         refresh_object(ctx);
387
388         /* Add interface to default object path */
389         mgr = telephony_manager_skeleton_new();
390         g_signal_connect (mgr,
391                         "handle-get-modems",
392                         G_CALLBACK (on_manager_getmodems),
393                         ctx); /* user_data */
394
395         g_dbus_interface_skeleton_export(G_DBUS_INTERFACE_SKELETON(mgr), conn, MY_DBUS_PATH, NULL);
396
397         g_dbus_object_manager_server_set_connection (ctx->manager, conn);
398
399         //set telephony ready registry
400         strg = tcore_server_find_storage(ctx->server, "vconf");
401         handle = tcore_storage_create_handle(strg, "vconf");
402
403         rv = tcore_storage_set_bool(strg, STORAGE_KEY_TELEPHONY_READY, TRUE);
404         if(!rv){
405                 dbg("fail to set the telephony status to registry");
406         }
407
408         dbg("done to acquire the dbus");
409 }
410
411 struct tcore_communitor_operations ops = {
412         .send_response = send_response,
413         .send_notification = send_notification,
414 };
415
416 static gboolean on_load()
417 {
418         dbg("i'm load!");
419
420         return TRUE;
421 }
422
423 static gboolean on_init(TcorePlugin *p)
424 {
425         Communicator *comm;
426         struct custom_data *data;
427         guint id;
428
429         if (!p)
430                 return FALSE;
431
432         dbg("i'm init!");
433
434         data = calloc(sizeof(struct custom_data), 1);
435         if (!data) {
436                 return FALSE;
437         }
438
439         data->plugin = p;
440
441         comm = tcore_communicator_new(p, "dbus", &ops);
442         tcore_communicator_link_user_data(comm, data);
443
444         data->comm = comm;
445         data->server = tcore_plugin_ref_server(p);
446
447         data->objects = g_hash_table_new(g_str_hash, g_str_equal);
448         data->cached_sat_main_menu = NULL;
449
450         dbg("data = %p", data);
451
452         id = g_bus_own_name (G_BUS_TYPE_SYSTEM,
453                         MY_DBUS_SERVICE,
454                         G_BUS_NAME_OWNER_FLAGS_REPLACE,
455                         on_bus_acquired,
456                         NULL, NULL,
457                         data,
458                         NULL);
459
460         return TRUE;
461 }
462
463 static void on_unload(TcorePlugin *p)
464 {
465         struct custom_data *data;
466         Communicator *comm;
467
468         if (!p)
469                 return;
470
471         dbg("i'm unload");
472
473         comm = tcore_server_find_communicator(tcore_plugin_ref_server(p), "dbus");
474         if (!comm)
475                 return;
476
477         data = tcore_communicator_ref_user_data(comm);
478         if (!data)
479                 return;
480
481         g_hash_table_destroy(data->objects);
482
483         free(data);
484 }
485
486 struct tcore_plugin_define_desc plugin_define_desc =
487 {
488         .name = "NEW_DBUS_COMMUNICATOR",
489         .priority = TCORE_PLUGIN_PRIORITY_HIGH,
490         .version = PLUGIN_VERSION,
491         .load = on_load,
492         .init = on_init,
493         .unload = on_unload
494 };