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.
28 #include <glib-object.h>
35 #include <communicator.h>
38 #include <user_request.h>
39 #include <co_network.h>
43 #ifndef PLUGIN_VERSION
44 #define PLUGIN_VERSION 1
47 #include "generated-code.h"
50 static void add_modem(struct custom_data *ctx, TcorePlugin *p)
52 TelephonyObjectSkeleton *object;
59 cp_name = tcore_server_get_cp_name_by_plugin(p);
60 if (cp_name == NULL) {
61 err("CP Name is NULL");
65 path = g_strdup_printf("%s/%s", MY_DBUS_PATH, cp_name);
66 dbg("PATH: [%s]", path);
68 object = g_hash_table_lookup(ctx->objects, path);
70 /* Create new DBUS Interface object */
71 object = telephony_object_skeleton_new(path);
73 /* Insert the Object to DBUS interfaces HASH Table */
74 dbg("New DBUS Interface object created!!! (object = %p)", object);
78 g_hash_table_insert(ctx->objects, g_strdup(path), object);
80 dbg("DBUS Interface object already created!!! (object = %p)", object);
83 /* Add DBUS Interfaces for all Modules supported */
86 dbus_plugin_setup_modem_interface(object, ctx);
89 if (tcore_plugin_ref_core_object(p, CORE_OBJECT_TYPE_CALL) != NULL)
90 dbus_plugin_setup_call_interface(object, ctx);
93 if (tcore_plugin_ref_core_object(p, CORE_OBJECT_TYPE_NETWORK) != NULL)
94 dbus_plugin_setup_network_interface(object, ctx);
97 if (tcore_plugin_ref_core_object(p, CORE_OBJECT_TYPE_SS) != NULL)
98 dbus_plugin_setup_ss_interface(object, ctx);
101 if (tcore_plugin_ref_core_object(p, CORE_OBJECT_TYPE_SMS) != NULL)
102 dbus_plugin_setup_sms_interface(object, ctx);
105 co_sim = tcore_plugin_ref_core_object(p, CORE_OBJECT_TYPE_SIM);
107 dbus_plugin_setup_sim_interface(object, ctx);
110 if ((co_sim != NULL) && (tcore_sim_get_status(co_sim) >= SIM_STATUS_INITIALIZING))
111 if (tcore_plugin_ref_core_object(p, CORE_OBJECT_TYPE_SAT) != NULL)
112 dbus_plugin_setup_sat_interface(object, ctx);
115 if (tcore_plugin_ref_core_object(p, CORE_OBJECT_TYPE_PHONEBOOK) != NULL)
116 dbus_plugin_setup_phonebook_interface(object, ctx);
119 if (tcore_plugin_ref_core_object(p, CORE_OBJECT_TYPE_SAP) != NULL)
120 dbus_plugin_setup_sap_interface(object, ctx);
123 if (tcore_plugin_ref_core_object(p, CORE_OBJECT_TYPE_GPS) != NULL)
124 dbus_plugin_setup_gps_interface(object, ctx);
126 /* Export the Object to Manager */
127 g_dbus_object_manager_server_export(ctx->manager, G_DBUS_OBJECT_SKELETON(object));
134 static void refresh_object(struct custom_data *ctx)
142 if (ctx->manager == NULL) {
147 plugins = tcore_server_ref_plugins(ctx->server);
152 for (cur = plugins; cur; cur = cur->next) {
154 /* AT Standard Plug-in is not considered */
156 || (strcmp(tcore_plugin_ref_plugin_name(p), "AT") == 0)) {
157 dbg("Plug-in Name: [%s]", tcore_plugin_ref_plugin_name(p));
161 co = tcore_plugin_ref_core_object(p, CORE_OBJECT_TYPE_MODEM);
170 static TReturn send_response(Communicator *comm, UserRequest *ur, enum tcore_response_command command, unsigned int data_len, const void *data)
172 struct custom_data *ctx = NULL;
173 const struct tcore_user_info *ui;
175 dbg("Response Command = [0x%x], data_len = %d", command, data_len);
177 ctx = tcore_communicator_ref_user_data(comm);
179 dbg("user_data is NULL");
183 ui = tcore_user_request_ref_user_info(ur);
185 switch (command & (TCORE_RESPONSE | 0x0FF00000)) {
187 dbus_plugin_call_response(ctx, ur, ui->user_data, command, data_len, data);
191 dbus_plugin_ss_response(ctx, ur, ui->user_data, command, data_len, data);
198 dbus_plugin_sim_response(ctx, ur, ui->user_data, command, data_len, data);
202 dbus_plugin_sap_response(ctx, ur, ui->user_data, command, data_len, data);
205 case TRESP_PHONEBOOK:
206 dbus_plugin_phonebook_response(ctx, ur, ui->user_data, command, data_len, data);
210 dbus_plugin_modem_response(ctx, ur, ui->user_data, command, data_len, data);
214 dbus_plugin_sms_response(ctx, ur, ui->user_data, command, data_len, data);
218 dbus_plugin_sat_response(ctx, ur, ui->user_data, command, data_len, data);
224 dbus_plugin_network_response(ctx, ur, ui->user_data, command, data_len, data);
228 dbus_plugin_gps_response(ctx, ur, ui->user_data, command, data_len, data);
232 warn("unknown command (0x%x)", command);
239 static TReturn send_notification(Communicator *comm, CoreObject *source, enum tcore_notification_command command, unsigned int data_len, const void *data)
241 struct custom_data *ctx = NULL;
242 TelephonyObjectSkeleton *object;
247 dbg("Notification!!! (command = 0x%x, data_len = %d)", command, data_len);
249 if (command == TNOTI_SERVER_ADDED_PLUGIN)
250 p = (TcorePlugin *)data;
252 p = tcore_object_ref_plugin(source);
254 cp_name = tcore_server_get_cp_name_by_plugin(p);
256 return TCORE_RETURN_FAILURE;
257 dbg("CP Name: [%s]", cp_name);
259 ctx = tcore_communicator_ref_user_data(comm);
261 dbg("user_data is NULL");
262 return TCORE_RETURN_FAILURE;
266 path = g_strdup_printf("%s/%s", MY_DBUS_PATH, cp_name);
269 path = g_strdup_printf("%s", MY_DBUS_PATH);
271 dbg("PATH: [%s]", path);
273 object = g_hash_table_lookup(ctx->objects, path);
274 dbg("DBUS interface Object = [0x%x]", object);
276 switch (command & (TCORE_NOTIFICATION | 0x0FF00000)) {
278 dbus_plugin_call_notification(ctx, cp_name, object, command, data_len, data);
282 dbus_plugin_ss_notification(ctx, cp_name, object, command, data_len, data);
289 dbus_plugin_sim_notification(ctx, cp_name, object, command, data_len, data);
293 dbus_plugin_sap_notification(ctx, cp_name, object, command, data_len, data);
296 case TNOTI_PHONEBOOK:
297 dbus_plugin_phonebook_notification(ctx, cp_name, object, command, data_len, data);
301 dbus_plugin_modem_notification(ctx, cp_name, object, command, data_len, data);
305 dbus_plugin_sms_notification(ctx, cp_name, object, command, data_len, data);
309 dbus_plugin_sat_notification(ctx, cp_name, object, command, data_len, data);
315 dbus_plugin_network_notification(ctx, cp_name, object, command, data_len, data);
319 dbus_plugin_gps_notification(ctx, cp_name, object, command, data_len, data);
323 dbg("Server Notification");
324 if (command == TNOTI_SERVER_ADDED_PLUGIN) {
325 dbg("Plug-in is added... Refresh the context");
331 warn("unknown command (0x%x)", command);
340 on_manager_getmodems (TelephonyManager *mgr,
341 GDBusMethodInvocation *invocation,
344 struct custom_data *ctx = user_data;
345 GSList *cp_name_list;
347 const char *name = NULL;
351 cp_name_list = tcore_server_get_cp_name_list(ctx->server);
352 if (cp_name_list == NULL) {
353 telephony_manager_complete_get_modems(mgr, invocation, &name);
357 count = g_slist_length(cp_name_list);
359 err("No Modems present");
360 telephony_manager_complete_get_modems(mgr, invocation, &name);
364 dbg("count: %d", count);
365 list = g_try_malloc0(sizeof(gchar *) * (count+1));
367 err("Failed to allocate memory");
368 g_slist_free_full(cp_name_list, g_free);
370 telephony_manager_complete_get_modems(mgr, invocation, &name);
375 for ( ; cp_name_list ; cp_name_list = cp_name_list->next) {
376 name = cp_name_list->data;
380 list[count] = g_strdup(name);
381 dbg("list[%d]: %s", count, list[count]);
385 telephony_manager_complete_get_modems(mgr, invocation, (const gchar **)list);
388 for (;count >= 0; count--)
393 /* Freeing the received list of CP names */
394 g_slist_free_full(cp_name_list, g_free);
399 static gboolean _set_telephony_ready(gpointer data)
401 struct custom_data *ctx = data;
402 static Storage *strg;
405 strg = tcore_server_find_storage(ctx->server, "vconf");
406 rv = tcore_storage_set_bool(strg, STORAGE_KEY_TELEPHONY_READY, TRUE);
408 err("Set Telephony Ready (TRUE) to registry - FAIL");
410 dbg("Set Telephony Ready (TRUE) to registry - SUCCESS");
415 static void on_bus_acquired(GDBusConnection *conn, const gchar *name, gpointer user_data)
417 struct custom_data *ctx = user_data;
418 TelephonyManager *mgr;
420 info("DBUS Registered");
422 /* Add interface to default object path */
423 mgr = telephony_manager_skeleton_new();
424 g_signal_connect (mgr,
426 G_CALLBACK (on_manager_getmodems),
427 ctx); /* user_data */
429 g_dbus_interface_skeleton_export(G_DBUS_INTERFACE_SKELETON(mgr), conn, MY_DBUS_PATH, NULL);
431 g_dbus_object_manager_server_set_connection (ctx->manager, conn);
433 dbg("Aquire DBUS - COMPLETE");
438 /* Telephony ready vconf key set in idle */
439 g_idle_add(_set_telephony_ready, ctx);
442 struct tcore_communitor_operations ops = {
443 .send_response = send_response,
444 .send_notification = send_notification,
447 static gboolean on_load()
454 static gboolean on_init(TcorePlugin *p)
457 struct custom_data *data;
465 data = calloc(sizeof(struct custom_data), 1);
472 comm = tcore_communicator_new(p, "dbus", &ops);
473 tcore_communicator_link_user_data(comm, data);
476 data->server = tcore_plugin_ref_server(p);
478 data->objects = g_hash_table_new(g_str_hash, g_str_equal);
479 data->cached_sat_main_menu = NULL;
481 dbg("data = %p", data);
483 id = g_bus_own_name (G_BUS_TYPE_SYSTEM,
485 G_BUS_NAME_OWNER_FLAGS_REPLACE,
491 data->manager = g_dbus_object_manager_server_new (MY_DBUS_PATH);
493 refresh_object(data);
498 static void on_unload(TcorePlugin *p)
500 struct custom_data *data;
508 comm = tcore_server_find_communicator(tcore_plugin_ref_server(p), "dbus");
512 data = tcore_communicator_ref_user_data(comm);
516 g_hash_table_destroy(data->objects);
521 struct tcore_plugin_define_desc plugin_define_desc =
523 .name = "NEW_DBUS_COMMUNICATOR",
524 .priority = TCORE_PLUGIN_PRIORITY_HIGH,
525 .version = PLUGIN_VERSION,