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