4 * Copyright (c) 2012 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Ja-young Gu <jygu@samsung.com>
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
34 #include <communicator.h>
37 #include <user_request.h>
38 #include <co_network.h>
42 #include "sat_ui_support.h"
44 #ifndef PLUGIN_VERSION
45 #define PLUGIN_VERSION 1
48 #include "generated-code.h"
51 static void add_modem(struct custom_data *ctx, TcorePlugin *p)
53 TelephonyObjectSkeleton *object;
61 cp_name = tcore_server_get_cp_name_by_plugin(p);
62 if (cp_name == NULL) {
63 err("CP Name is NULL");
67 path = g_strdup_printf("%s/%s", MY_DBUS_PATH, cp_name);
68 dbg("PATH: [%s]", path);
70 object = g_hash_table_lookup(ctx->objects, path);
72 dbg("DBUS interface object already created (object: %p)", object);
76 object = telephony_object_skeleton_new(path);
77 dbg("New DBUS object created (object: [%p])", object);
78 g_hash_table_insert(ctx->objects, g_strdup(path), object);
81 co_list = tcore_plugin_get_core_objects_bytype(p, CORE_OBJECT_TYPE_MODEM);
83 g_slist_free(co_list);
84 dbus_plugin_setup_modem_interface(object, ctx);
87 co_list = tcore_plugin_get_core_objects_bytype(p, CORE_OBJECT_TYPE_CALL);
89 g_slist_free(co_list);
90 dbus_plugin_setup_call_interface(object, ctx);
93 co_list = tcore_plugin_get_core_objects_bytype(p, CORE_OBJECT_TYPE_NETWORK);
95 g_slist_free(co_list);
96 dbus_plugin_setup_network_interface(object, ctx);
99 co_list = tcore_plugin_get_core_objects_bytype(p, CORE_OBJECT_TYPE_SS);
101 g_slist_free(co_list);
102 dbus_plugin_setup_ss_interface(object, ctx);
105 co_list = tcore_plugin_get_core_objects_bytype(p, CORE_OBJECT_TYPE_SMS);
107 g_slist_free(co_list);
108 dbus_plugin_setup_sms_interface(object, ctx);
111 co_list = tcore_plugin_get_core_objects_bytype(p, CORE_OBJECT_TYPE_SAT);
113 g_slist_free(co_list);
114 dbus_plugin_setup_sat_interface(object, ctx);
117 co_list = tcore_plugin_get_core_objects_bytype(p, CORE_OBJECT_TYPE_PHONEBOOK);
119 g_slist_free(co_list);
120 dbus_plugin_setup_phonebook_interface(object, ctx);
123 co_list = tcore_plugin_get_core_objects_bytype(p, CORE_OBJECT_TYPE_SAP);
125 g_slist_free(co_list);
126 dbus_plugin_setup_sap_interface(object, ctx);
129 co_list = tcore_plugin_get_core_objects_bytype(p, CORE_OBJECT_TYPE_SIM);
131 g_slist_free(co_list);
132 dbus_plugin_setup_sim_interface(object, ctx);
135 co_list = tcore_plugin_get_core_objects_bytype(p, CORE_OBJECT_TYPE_GPS);
137 g_slist_free(co_list);
138 dbus_plugin_setup_gps_interface(object, ctx);
140 dbus_plugin_setup_oem_interface(object, ctx);
141 /* Export the Object to Manager */
142 g_dbus_object_manager_server_export (ctx->manager, G_DBUS_OBJECT_SKELETON (object));
150 static void refresh_object(struct custom_data *ctx)
152 GSList *modem_plg_list;
153 TcorePlugin *modem_plg;
157 dbg("Telephony not ready...");
161 modem_plg_list = tcore_server_get_modem_plugin_list(ctx->server);
162 for (cur = modem_plg_list; cur; cur = cur->next) {
163 modem_plg = cur->data;
164 if (modem_plg == NULL) {
165 dbg("No Modem Plug-in");
170 add_modem(ctx, modem_plg);
172 g_slist_free(modem_plg_list);
175 static TReturn send_response(Communicator *comm, UserRequest *ur, enum tcore_response_command command, unsigned int data_len, const void *data)
177 struct custom_data *ctx = NULL;
178 struct dbus_request_info *dbus_info;
179 gboolean ret = FALSE;
181 dbg("Response Command = [0x%x], data_len = %d", command, data_len);
184 ctx = tcore_communicator_ref_user_data(comm);
186 dbg("user_data is NULL");
187 return TCORE_RETURN_EINVAL;
190 dbus_info = (struct dbus_request_info *)tcore_user_request_ref_user_info(ur);
192 dbg("dbus_info is NULL");
193 return TCORE_RETURN_EINVAL;
197 g_dbus_method_invocation_return_error (dbus_info->invocation,
201 return TCORE_RETURN_SUCCESS;
204 switch (command & (TCORE_RESPONSE | 0x0FF00000)) {
206 ret = dbus_plugin_call_response(ctx, ur, dbus_info, command, data_len, data);
210 ret = dbus_plugin_ss_response(ctx, ur, dbus_info, command, data_len, data);
217 ret = dbus_plugin_sim_response(ctx, ur, dbus_info, command, data_len, data);
221 ret = dbus_plugin_sap_response(ctx, ur, dbus_info, command, data_len, data);
224 case TRESP_PHONEBOOK:
225 ret = dbus_plugin_phonebook_response(ctx, ur, dbus_info, command, data_len, data);
229 ret = dbus_plugin_modem_response(ctx, ur, dbus_info, command, data_len, data);
233 ret = dbus_plugin_sms_response(ctx, ur, dbus_info, command, data_len, data);
237 ret = dbus_plugin_sat_response(ctx, ur, dbus_info, command, data_len, data);
241 ret = dbus_plugin_oem_response(ctx, ur, dbus_info, command, data_len, data);
245 ret = dbus_plugin_network_response(ctx, ur, dbus_info, command, data_len, data);
249 ret = dbus_plugin_gps_response(ctx, ur, dbus_info, command, data_len, data);
253 warn("unknown command (0x%x)", command);
258 return TCORE_RETURN_SUCCESS;
260 return TCORE_RETURN_FAILURE;
263 static TReturn send_notification(Communicator *comm, CoreObject *source, enum tcore_notification_command command, unsigned int data_len, const void *data)
265 struct custom_data *ctx = NULL;
266 unsigned int noti = 0;
267 gboolean ret = FALSE;
269 ctx = tcore_communicator_ref_user_data(comm);
271 err("user_data is NULL");
272 return TCORE_RETURN_EINVAL;
275 noti = (command & (TCORE_NOTIFICATION | 0x0FF00000));
278 * Notifications are classified into -
279 * Server (System) notifications
280 * Module notifications
282 if (noti == TNOTI_SERVER) {
283 dbg("Server (System) Notification");
286 case TNOTI_SERVER_ADDED_MODEM_PLUGIN: {
289 cp_name = tcore_server_get_cp_name_by_plugin((TcorePlugin*)data);
290 dbg("Modem Plug-in (%s) is added... Exporting interfaces for the modem", cp_name);
291 add_modem(ctx, (TcorePlugin*)data);
297 case TNOTI_SERVER_ADDED_MODEM_PLUGIN_COMPLETED: {
308 dbg("[%d] Modem plug-ins are added...", *count);
310 strg = tcore_server_find_storage(ctx->server, "vconf");
312 b_set = tcore_storage_set_int(strg, STORAGE_KEY_TELEPHONY_SIM_SLOT_COUNT, *count);
313 if (b_set == FALSE) {
314 err("Fail to set the sim slot count vconf");
316 /* Reset STORAGE_KEY_TELEPHONY_READY */
317 b_set = tcore_storage_set_bool(strg, STORAGE_KEY_TELEPHONY_READY, FALSE);
318 warn("Reset TELEPHONY_READY!!!");
320 if (ctx->name_acquired == TRUE
321 && tcore_storage_get_bool(strg, STORAGE_KEY_TELEPHONY_READY) == FALSE) {
322 b_set = tcore_storage_set_bool(strg, STORAGE_KEY_TELEPHONY_READY, TRUE);
323 if (b_set == FALSE) {
324 err("Fail to set telephony ready");
326 /* Reset STORAGE_KEY_TELEPHONY_SIM_SLOT_COUNT */
327 b_set = tcore_storage_set_int(strg, STORAGE_KEY_TELEPHONY_SIM_SLOT_COUNT, -1);
328 warn("Reset STORAGE_KEY_TELEPHONY_SIM_SLOT_COUNT!!!");
330 #ifdef ENABLE_KPI_LOGS
331 TIME_CHECK("Setting VCONFKEY_TELEPHONY_READY to TRUE");
333 msg("Setting VCONFKEY_TELEPHONY_READY to TRUE");
337 dbg("Bus acquired...[%s]", (ctx->name_acquired)?"YES":"NO" );
344 warn("Unsupported System notification: (0x%x)", command);
349 TelephonyObjectSkeleton *object;
353 cp_name = tcore_server_get_cp_name_by_plugin(tcore_object_ref_plugin(source));
354 if (cp_name == NULL) {
355 err("CP name is NULL");
356 return TCORE_RETURN_FAILURE;
359 path = g_strdup_printf("%s/%s", MY_DBUS_PATH, cp_name);
361 /* Look-up Hash table for Object */
362 object = g_hash_table_lookup(ctx->objects, path);
364 dbg("[%s]:(cmd[0x%x] data[%p] len[%d] obj[%p])",
365 cp_name, command, data, data_len, object);
368 if (object == NULL) {
369 err("Object is NOT defined!!!");
370 return TCORE_RETURN_FAILURE;
375 ret = dbus_plugin_call_notification(ctx, source, object, command, data_len, data);
379 ret = dbus_plugin_ss_notification(ctx, source, object, command, data_len, data);
383 warn("PS Notification (0x%x)... Not handled!!!", noti);
387 ret = dbus_plugin_sim_notification(ctx, source, object, command, data_len, data);
391 ret = dbus_plugin_sap_notification(ctx, source, object, command, data_len, data);
394 case TNOTI_PHONEBOOK:
395 ret = dbus_plugin_phonebook_notification(ctx, source, object, command, data_len, data);
399 ret = dbus_plugin_modem_notification(ctx, source, object, command, data_len, data);
403 ret = dbus_plugin_sms_notification(ctx, source, object, command, data_len, data);
407 ret = dbus_plugin_sat_notification(ctx, source, object, command, data_len, data);
411 ret = dbus_plugin_network_notification(ctx, source, object, command, data_len, data);
415 ret = dbus_plugin_gps_notification(ctx, source, object, command, data_len, data);
419 if ((command & (TCORE_NOTIFICATION | 0x0F000000)) == (TNOTI_CUSTOM)) {
420 dbg("Custom Notification: [0x%x]", command);
421 ret = dbus_plugin_oem_notification(ctx, source, object, command, data_len, data);
423 warn("Unknown/Unhandled Notification: [0x%x]", command);
431 return TCORE_RETURN_SUCCESS;
433 return TCORE_RETURN_FAILURE;
438 on_manager_getmodems (TelephonyManager *mgr,
439 GDBusMethodInvocation *invocation,
442 struct custom_data *ctx = user_data;
443 GSList *cp_name_list;
445 const char *name = NULL;
449 cp_name_list = tcore_server_get_cp_name_list(ctx->server);
450 if (cp_name_list == NULL) {
451 telephony_manager_complete_get_modems(mgr, invocation, &name);
455 count = g_slist_length(cp_name_list);
456 list = g_try_malloc0(sizeof(gchar *) * (count+1));
458 err("Memory allocation failed!!");
459 telephony_manager_complete_get_modems(mgr, invocation, &name);
460 g_slist_free_full(cp_name_list, g_free);
465 for ( ; cp_name_list ; cp_name_list = cp_name_list->next) {
466 name = cp_name_list->data;
467 list[count] = g_strdup(name);
468 dbg("list[%d]: %s", count, list[count]);
472 telephony_manager_complete_get_modems(mgr, invocation, (const gchar **)list);
475 for (;count >= 0; count--)
480 /* Freeing the received list of CP names */
481 g_slist_free_full(cp_name_list, g_free);
486 static void on_name_lost(GDBusConnection *conn, const gchar *name, gpointer user_data)
488 info("Lost the name %s on the session bus\n", name);
491 static void on_name_acquired(GDBusConnection *conn, const gchar *name, gpointer user_data)
493 struct custom_data *ctx = user_data;
498 ctx->name_acquired = TRUE; /* Setting Bus acquried flag after Bus name is acquired */
500 info("Acquired the name %s on the session bus", name);
501 strg = tcore_server_find_storage(ctx->server, "vconf");
503 count = tcore_storage_get_int(strg, STORAGE_KEY_TELEPHONY_SIM_SLOT_COUNT);
505 err("SIM slot count not yet set");
507 if (tcore_storage_get_bool(strg, STORAGE_KEY_TELEPHONY_READY) == FALSE) {
508 b_set = tcore_storage_set_bool(strg, STORAGE_KEY_TELEPHONY_READY, TRUE);
509 if (b_set == FALSE) {
510 err("Fail to set telephony ready");
512 /* Reset STORAGE_KEY_TELEPHONY_SIM_SLOT_COUNT */
513 b_set = tcore_storage_set_int(strg, STORAGE_KEY_TELEPHONY_SIM_SLOT_COUNT, -1);
514 warn("Reset STORAGE_KEY_TELEPHONY_SIM_SLOT_COUNT!!!");
516 #ifdef ENABLE_KPI_LOGS
517 TIME_CHECK("Setting VCONFKEY_TELEPHONY_READY to TRUE");
519 msg("Setting VCONFKEY_TELEPHONY_READY to TRUE");
526 static void on_bus_acquired(GDBusConnection *conn, const gchar *name, gpointer user_data)
528 struct custom_data *ctx = user_data;
530 info("dbus registered");
534 /* Add interface to default object path */
535 ctx->mgr = telephony_manager_skeleton_new();
536 g_signal_connect (ctx->mgr,
538 G_CALLBACK (on_manager_getmodems),
539 ctx); /* user_data */
541 g_dbus_interface_skeleton_export(G_DBUS_INTERFACE_SKELETON(ctx->mgr), conn, MY_DBUS_PATH, NULL);
543 g_dbus_object_manager_server_set_connection (ctx->manager, conn);
545 dbg("Aquire DBUS - COMPLETE");
550 struct tcore_communitor_operations ops = {
551 .send_response = send_response,
552 .send_notification = send_notification,
555 static gboolean on_load()
562 static gboolean on_init(TcorePlugin *p)
565 struct custom_data *data;
573 data = calloc(1, sizeof(struct custom_data));
580 comm = tcore_communicator_new(p, "dbus", &ops);
581 tcore_communicator_link_user_data(comm, data);
584 data->server = tcore_plugin_ref_server(p);
586 data->objects = g_hash_table_new(g_str_hash, g_str_equal);
587 data->cached_data = NULL;
589 dbg("data = %p", data);
591 id = g_bus_own_name (G_BUS_TYPE_SYSTEM,
593 G_BUS_NAME_OWNER_FLAGS_REPLACE,
595 on_name_acquired, on_name_lost,
600 dbg("owner id=[%d]", data->owner_id);
602 data->manager = g_dbus_object_manager_server_new (MY_DBUS_PATH);
603 refresh_object(data);
608 static void on_unload(TcorePlugin *p)
610 struct custom_data *data = 0;
611 Communicator *comm = 0;
612 Server *s = tcore_plugin_ref_server(p);
614 struct cached_data *object = NULL;
621 comm = tcore_server_find_communicator(s, "dbus");
625 data = tcore_communicator_ref_user_data(comm);
629 if(data->owner_id > 0) {
630 g_bus_unown_name(data->owner_id);
633 g_hash_table_destroy(data->objects);
635 for (list = data->cached_data; list; list = list->next) {
636 object = (struct cached_data *)list->data;
640 g_variant_unref(object->cached_sat_main_menu);
641 g_free(object->cp_name);
644 g_slist_free(data->cached_data);
648 tcore_server_remove_communicator(s, comm);
651 EXPORT_API struct tcore_plugin_define_desc plugin_define_desc =
653 .name = "NEW_DBUS_COMMUNICATOR",
654 .priority = TCORE_PLUGIN_PRIORITY_HIGH,
655 .version = PLUGIN_VERSION,