#include <unistd.h>
#include <glib.h>
#include <dbus/dbus.h>
-#include <dbus/dbus-glib.h>
-#include <dbus/dbus-glib-lowlevel.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "mobileap_notification.h"
#include "mobileap_iptables.h"
-GType tethering_object_get_type(void);
-#define TETHERING_TYPE_OBJECT (tethering_object_get_type())
-G_DEFINE_TYPE(TetheringObject, tethering_object, G_TYPE_OBJECT)
-
GMainLoop *mainloop = NULL;
int mobileap_state = MOBILE_AP_STATE_NONE;
-DBusConnection *tethering_conn = NULL;
-
-gboolean tethering_disable(TetheringObject *obj, DBusGMethodInvocation *context);
-gboolean tethering_get_station_info(TetheringObject *obj,
- DBusGMethodInvocation *context);
-gboolean tethering_get_data_packet_usage(TetheringObject *obj,
- DBusGMethodInvocation *context);
-
-#include "tethering-server-stub.h"
-
-static void tethering_object_init(TetheringObject *obj)
-{
- DBG("+\n");
- g_assert(obj != NULL);
-
- obj->init_count = 0;
- memset(&obj->softap_settings, 0x00, sizeof(obj->softap_settings));
-}
-
-static void tethering_object_finalize(GObject *obj)
-{
- DBG("+\n");
-
- G_OBJECT_CLASS(tethering_object_parent_class)->finalize(obj);
-}
-static void tethering_object_class_init(TetheringObjectClass *klass)
+GDBusObjectManagerServer *manager_server = NULL;
+guint owner_id = 0;
+GDBusConnection *teth_gdbus_conn = NULL;
+Tethering *tethering_obj = NULL;
+static int init_count = 0;
+guint conn_sig_id = 0;
+guint deleted_sig_id = 0;
+gboolean tethering_disable(Tethering *obj, GDBusMethodInvocation *context);
+gboolean tethering_get_station_info(Tethering *obj,
+ GDBusMethodInvocation *context);
+gboolean tethering_get_data_packet_usage(Tethering *obj,
+ GDBusMethodInvocation *context);
+
+Tethering *_get_tethering_obj(void)
{
- GObjectClass *object_class = (GObjectClass *)klass;
- const gchar *signalNames[E_SIGNAL_MAX] = {
- SIGNAL_NAME_NET_CLOSED,
- SIGNAL_NAME_STA_CONNECT,
- SIGNAL_NAME_STA_DISCONNECT,
- SIGNAL_NAME_WIFI_TETHER_ON,
- SIGNAL_NAME_WIFI_TETHER_OFF,
- SIGNAL_NAME_USB_TETHER_ON,
- SIGNAL_NAME_USB_TETHER_OFF,
- SIGNAL_NAME_BT_TETHER_ON,
- SIGNAL_NAME_BT_TETHER_OFF,
- SIGNAL_NAME_WIFI_AP_ON,
- SIGNAL_NAME_WIFI_AP_OFF,
- SIGNAL_NAME_NO_DATA_TIMEOUT,
- SIGNAL_NAME_LOW_BATTERY_MODE,
- SIGNAL_NAME_FLIGHT_MODE,
- SIGNAL_NAME_POWER_SAVE_MODE,
- SIGNAL_NAME_SECURITY_TYPE_CHANGED,
- SIGNAL_NAME_SSID_VISIBILITY_CHANGED,
- SIGNAL_NAME_PASSPHRASE_CHANGED
- };
-
- int i = 0;
-
- g_assert(klass != NULL);
-
- object_class->finalize = tethering_object_finalize;
-
- DBG("Creating signals\n");
-
- for (i = 0; i < E_SIGNAL_MAX; i++) {
- guint signalId;
-
- signalId = g_signal_new(signalNames[i],
- G_OBJECT_CLASS_TYPE(klass),
- G_SIGNAL_RUN_LAST,
- 0, NULL, NULL,
- g_cclosure_marshal_VOID__STRING,
- G_TYPE_NONE, 1, G_TYPE_STRING);
- klass->signals[i] = signalId;
- }
-
- DBG("Binding to GLib/D-Bus\n");
-
- dbus_g_object_type_install_info(TETHERING_TYPE_OBJECT,
- &dbus_glib_tethering_object_info);
-}
-
-static void __add_station_info_to_array(gpointer data, gpointer user_data)
-{
- mobile_ap_station_info_t *si = (mobile_ap_station_info_t *)data;
- GPtrArray *array = (GPtrArray *)user_data;
- GValue value = {0, {{0}}};
-
- g_value_init(&value, DBUS_STRUCT_STATIONS);
- g_value_take_boxed(&value,
- dbus_g_type_specialized_construct(DBUS_STRUCT_STATIONS));
- dbus_g_type_struct_set(&value, 0, si->interface, 1, si->ip,
- 2, si->mac, 3, si->hostname, 4, (guint)(si->tm), G_MAXUINT);
- g_ptr_array_add(array, g_value_get_boxed(&value));
+ return tethering_obj;
}
gboolean _mobileap_set_state(int state)
DBG("PM control [SUCCESS]\n");
}
-int _init_tethering(TetheringObject *obj)
+int _init_tethering(void)
{
int ret = MOBILE_AP_ERROR_NONE;
- DBG("obj->init_count: %d\n", obj->init_count);
+ DBG("init_count: %d\n", init_count);
- if (obj->init_count > 0) {
- obj->init_count++;
+ if (init_count > 0) {
+ init_count++;
return MOBILE_AP_ERROR_NONE;
}
}
_mh_core_execute_dhcp_server();
- obj->init_count++;
+ init_count++;
return ret;
}
-gboolean _deinit_tethering(TetheringObject *obj)
+gboolean _deinit_tethering(void)
{
- DBG("obj->init_count: %d\n", obj->init_count);
+ DBG("obj->init_count: %d\n", init_count);
guint idle_id;
- if (obj->init_count > 1) {
- obj->init_count--;
+ if (init_count > 1) {
+ init_count--;
return TRUE;
- } else if (obj->init_count <= 0) {
+ } else if (init_count <= 0) {
ERR("Already deinitialized\n");
- obj->init_count = 0;
+ init_count = 0;
return TRUE;
}
- obj->init_count = 0;
+ init_count = 0;
_mh_core_terminate_dhcp_server();
return TRUE;
}
-gboolean tethering_disable(TetheringObject *obj, DBusGMethodInvocation *context)
+gboolean tethering_disable(Tethering *obj, GDBusMethodInvocation *context)
{
int ret = MOBILE_AP_ERROR_NONE;
if (_mobileap_is_disabled()) {
ERR("Mobile hotspot has not been enabled\n");
ret = MOBILE_AP_ERROR_NOT_ENABLED;
- dbus_g_method_return(context, MOBILE_AP_DISABLE_CFM, ret);
+ tethering_complete_disable(obj, context, MOBILE_AP_DISABLE_CFM, ret);
return FALSE;
}
_disable_bt_tethering(obj);
_disable_usb_tethering(obj);
- dbus_g_method_return(context, MOBILE_AP_DISABLE_CFM, ret);
+ tethering_complete_disable(obj, context, MOBILE_AP_DISABLE_CFM, ret);
return TRUE;
}
-gboolean tethering_get_station_info(TetheringObject *obj,
- DBusGMethodInvocation *context)
+gboolean tethering_get_station_info(Tethering *obj,
+ GDBusMethodInvocation *context)
{
DBG("+\n");
- GPtrArray *array = g_ptr_array_new();
+ GVariant *var = NULL;
g_assert(obj != NULL);
g_assert(context != NULL);
- _station_info_foreach(__add_station_info_to_array, array);
+ var = _station_info_foreach();
- dbus_g_method_return(context, MOBILE_AP_GET_STATION_INFO_CFM, array);
- g_ptr_array_free(array, TRUE);
+ g_dbus_method_invocation_return_value(context, var);
+ g_variant_unref(var);
DBG("-\n");
-
return TRUE;
}
-gboolean tethering_get_data_packet_usage(TetheringObject *obj,
- DBusGMethodInvocation *context)
+gboolean tethering_get_data_packet_usage(Tethering *obj,
+ GDBusMethodInvocation *context)
{
char *if_name = NULL;
unsigned long long wifi_tx_bytes = 0;
unsigned long long rx_bytes = 0;
if (_get_network_interface_name(&if_name) == FALSE) {
- dbus_g_method_return(context, MOBILE_AP_GET_DATA_PACKET_USAGE_CFM,
+ tethering_complete_get_data_packet_usage(obj, context,
+ MOBILE_AP_GET_DATA_PACKET_USAGE_CFM,
0ULL, 0ULL);
return FALSE;
}
tx_bytes = wifi_tx_bytes + bt_tx_bytes + usb_tx_bytes;
rx_bytes = wifi_rx_bytes + bt_rx_bytes + usb_rx_bytes;
- dbus_g_method_return(context, MOBILE_AP_GET_DATA_PACKET_USAGE_CFM,
+ tethering_complete_get_data_packet_usage(obj, context, MOBILE_AP_GET_DATA_PACKET_USAGE_CFM,
tx_bytes, rx_bytes);
return TRUE;
}
-static DBusHandlerResult __dnsmasq_signal_filter(DBusConnection *conn,
- DBusMessage *msg, void *user_data)
+void static __handle_dnsmasq_dhcp_status_changed_cb(GDBusConnection *connection,
+ const gchar *sender_name, const gchar *object_path,
+ const gchar *interface_name, const gchar *signal_name,
+ GVariant *parameters, gpointer user_data)
{
- if (!user_data) {
- ERR("Invalid param\n");
- return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
- }
-
+ DBG("+\n");
char *ip_addr = NULL;
char *mac = NULL;
char *name = NULL;
char *bt_remote_device_name = NULL;
- DBusError error;
mobile_ap_type_e type = MOBILE_AP_TYPE_MAX;
- TetheringObject *obj = (TetheringObject *)user_data;
mobile_ap_station_info_t *info = NULL;
int n_station = 0;
time_t tm;
- dbus_error_init(&error);
- if (dbus_message_is_signal(msg, DNSMASQ_DBUS_INTERFACE,
- "DhcpConnected")) {
- if (!dbus_message_get_args(msg, &error,
- DBUS_TYPE_STRING, &ip_addr,
- DBUS_TYPE_STRING, &mac,
- DBUS_TYPE_STRING, &name,
- DBUS_TYPE_INVALID)) {
- ERR("Cannot read message, cause: %s\n", error.message);
- dbus_error_free(&error);
- return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
- }
+ if (signal_name == NULL) {
+ ERR("singal name is NULL\n");
+ return;
+ }
+ g_variant_get(parameters, "(sss)", &ip_addr, &mac, &name);
+ if (!g_strcmp0(signal_name, "DhcpConnected")) {
SDBG("DhcpConnected signal : %s %s %s\n", ip_addr, mac, name);
/*
* DHCP ACK received, destroy timeout if exists
*/
+ if (ip_addr == NULL || mac == NULL) {
+ goto EXIT;
+ }
_destroy_dhcp_ack_timer(mac);
if (_get_tethering_type_from_ip(ip_addr, &type) != MOBILE_AP_ERROR_NONE)
- return DBUS_HANDLER_RESULT_HANDLED;
+ goto EXIT;
if (_mobileap_is_enabled_by_type(type) == FALSE) {
- return DBUS_HANDLER_RESULT_HANDLED;
+ goto EXIT;
}
- info = (mobile_ap_station_info_t *)malloc(sizeof(mobile_ap_station_info_t));
+ info = (mobile_ap_station_info_t *)g_malloc(sizeof(mobile_ap_station_info_t));
if (info == NULL) {
ERR("malloc failed\n");
- return DBUS_HANDLER_RESULT_HANDLED;
+ goto EXIT;
}
info->interface = type;
else
info->hostname = g_strdup(name);
} else if (type == MOBILE_AP_TYPE_BT) {
- _bt_get_remote_device_name(obj, mac, &bt_remote_device_name);
+ _bt_get_remote_device_name(mac, &bt_remote_device_name);
if (bt_remote_device_name == NULL)
info->hostname = g_strdup(MOBILE_AP_NAME_UNKNOWN);
else
}
time(&tm);
info->tm = tm;
-
if (_add_station_info(info) != MOBILE_AP_ERROR_NONE) {
+ ERR("_add_station_info is failed\n");
g_free(info->hostname);
- free(info);
- return DBUS_HANDLER_RESULT_HANDLED;
+ g_free(info);
+ goto EXIT;
}
_get_station_count((gconstpointer)type,
_stop_timeout_cb(type);
_send_dbus_station_info("DhcpConnected", info);
-
- return DBUS_HANDLER_RESULT_HANDLED;
- } else if (dbus_message_is_signal(msg, DNSMASQ_DBUS_INTERFACE,
- "DhcpLeaseDeleted")) {
- if (!dbus_message_get_args(msg, &error,
- DBUS_TYPE_STRING, &ip_addr,
- DBUS_TYPE_STRING, &mac,
- DBUS_TYPE_STRING, &name,
- DBUS_TYPE_INVALID)) {
- ERR("Cannot read message, cause: %s\n", error.message);
- dbus_error_free(&error);
- return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
- }
+ } else if (!g_strcmp0(signal_name, "DhcpLeaseDeleted")) {
SDBG("DhcpLeaseDeleted signal : %s %s %s\n", ip_addr, mac, name);
-
_remove_station_info(ip_addr, _slist_find_station_by_ip_addr);
+ } else {
+ SDBG("UNKNOWN member signal\n");
+ }
+EXIT :
+ g_free(ip_addr);
+ g_free(mac);
+ g_free(name);
+ DBG("-\n");
+}
+
+static void on_bus_acquired_cb (GDBusConnection *connection, const gchar *name,
+ gpointer user_data)
+{
+ DBG("+\n");
+ GDBusInterfaceSkeleton *intf = NULL;
+ teth_gdbus_conn = connection;
- return DBUS_HANDLER_RESULT_HANDLED;
+ manager_server = g_dbus_object_manager_server_new(TETHERING_SERVICE_OBJECT_PATH);
+ if(manager_server == NULL) {
+ DBG("Manager server not created.");
+ return;
}
+ tethering_obj = tethering_skeleton_new();
+ intf = G_DBUS_INTERFACE_SKELETON(tethering_obj);
+ if (!g_dbus_interface_skeleton_export(intf, connection,
+ TETHERING_SERVICE_OBJECT_PATH, NULL)) {
+ ERR("Export with path failed");
+ } else {
+ DBG("Export sucessss");
+ }
+
+ g_signal_connect(tethering_obj, "handle-enable-wifi-tethering",
+ G_CALLBACK(tethering_enable_wifi_tethering), NULL);
+ g_signal_connect(tethering_obj, "handle-enable-bt-tethering",
+ G_CALLBACK(tethering_enable_bt_tethering), NULL);
+ g_signal_connect(tethering_obj, "handle-enable-usb-tethering",
+ G_CALLBACK(tethering_enable_usb_tethering), NULL);
+ g_signal_connect(tethering_obj, "handle-disable-wifi-tethering",
+ G_CALLBACK(tethering_disable_wifi_tethering), NULL);
+ g_signal_connect(tethering_obj, "handle-disable-bt-tethering",
+ G_CALLBACK(tethering_disable_bt_tethering), NULL);
+ g_signal_connect(tethering_obj, "handle-disable-usb-tethering",
+ G_CALLBACK(tethering_disable_usb_tethering), NULL);
+ g_signal_connect(tethering_obj, "handle-enable-wifi-ap",
+ G_CALLBACK(tethering_enable_wifi_ap), NULL);
+
+ g_signal_connect(tethering_obj, "handle-disable-wifi-ap",
+ G_CALLBACK(tethering_disable_wifi_ap), NULL);
+ g_signal_connect(tethering_obj, "handle-reload-wifi-settings",
+ G_CALLBACK(tethering_reload_wifi_settings), NULL);
+ g_signal_connect(tethering_obj, "handle-reload-wifi-ap-settings",
+ G_CALLBACK(tethering_reload_wifi_ap_settings), NULL);
+ g_signal_connect(tethering_obj, "handle-get-station-info",
+ G_CALLBACK(tethering_get_station_info), NULL);
+ g_signal_connect(tethering_obj, "handle-get-data-packet-usage",
+ G_CALLBACK(tethering_get_data_packet_usage), NULL);
- return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
+ _init_network((void *)tethering_obj);
+ _register_vconf_cb((void *)tethering_obj);
+
+ conn_sig_id = g_dbus_connection_signal_subscribe(connection, NULL, DNSMASQ_DBUS_INTERFACE,
+ "DhcpConnected", NULL, NULL, G_DBUS_SIGNAL_FLAGS_NONE,
+ __handle_dnsmasq_dhcp_status_changed_cb, NULL, NULL);
+ deleted_sig_id = g_dbus_connection_signal_subscribe(connection, NULL, DNSMASQ_DBUS_INTERFACE,
+ "DhcpLeaseDeleted", NULL, NULL, G_DBUS_SIGNAL_FLAGS_NONE,
+ __handle_dnsmasq_dhcp_status_changed_cb, NULL, NULL);
+
+ g_dbus_object_manager_server_set_connection(manager_server, connection);
+ DBG("-\n");
}
-int main(int argc, char **argv)
+static void on_name_acquired_cb(GDBusConnection *connection, const gchar *name,
+ gpointer user_data)
{
- const char *rule = "type='signal',interface='"DNSMASQ_DBUS_INTERFACE"'";
+ DBG("+\n");
+
+ DBG("-\n");
+}
+
+static void on_name_lost_db(GDBusConnection *conn, const gchar *name,
+ gpointer user_data)
+{
+ DBG("+\n");
+ /* May service name is already in use */
+ ERR("Service name is already in use");
- TetheringObject *tethering_obj = NULL;
- DBusGConnection *tethering_bus = NULL;
- DBusGProxy *tethering_bus_proxy = NULL;
- DBusError dbus_error;
- GError *error = NULL;
- guint result = 0;
- int ret;
+ /* The result of DBus name request is only permitted,
+ * such as DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER.
+ */
+ exit(2);
+
+ DBG("-\n");
+}
+
+static int __tethering_setup_gdbus(void)
+{
+ DBG("+\n");
+
+ owner_id = g_bus_own_name(DBUS_BUS_SYSTEM, TETHERING_SERVICE_NAME,
+ G_BUS_NAME_OWNER_FLAGS_NONE, on_bus_acquired_cb,
+ on_name_acquired_cb, on_name_lost_db, NULL, NULL);
+
+ if (!owner_id) {
+ ERR("g_bus_own_name is failed\n");
+ return -1;
+ }
+ return 0;
+}
+int main(int argc, char **argv)
+{
+ int ret = 0;
DBG("+\n");
return 0;
}
- /* D-Bus init */
- tethering_bus = dbus_g_bus_get(DBUS_BUS_SYSTEM, &error);
- if (error != NULL) {
- ERR("Couldn't connect to system bus[%s]\n", error->message);
- g_error_free(error);
- goto failure;
- }
-
- tethering_bus_proxy = dbus_g_proxy_new_for_name(tethering_bus,
- DBUS_SERVICE_DBUS, DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS);
- if (tethering_bus_proxy == NULL) {
- ERR("Failed to get a proxy for D-Bus\n");
- goto failure;
- }
- if (!dbus_g_proxy_call(tethering_bus_proxy, "RequestName", &error,
- G_TYPE_STRING, TETHERING_SERVICE_NAME,
- G_TYPE_UINT, 0, G_TYPE_INVALID,
- G_TYPE_UINT, &result, G_TYPE_INVALID)) {
- ERR("dbus_g_proxy_call is failed\n");
- if (error) {
- ERR("D-Bus.RequestName RPC failed[%s]\n",
- error->message);
- g_error_free(error);
- }
- goto failure;
- }
- if (result != 1) {
- ERR("Failed to get the primary well-known name.\n");
- goto failure;
- }
- g_object_unref(tethering_bus_proxy);
- tethering_bus_proxy = NULL;
-
- tethering_obj = g_object_new(TETHERING_TYPE_OBJECT, NULL);
- if (tethering_obj == NULL) {
- ERR("Failed to create one Tethering instance.\n");
- goto failure;
- }
- dbus_g_connection_register_g_object(tethering_bus,
- TETHERING_SERVICE_OBJECT_PATH, G_OBJECT(tethering_obj));
-
- tethering_conn = dbus_g_connection_get_connection(tethering_bus);
- dbus_error_init(&dbus_error);
- dbus_bus_add_match(tethering_conn, rule, &dbus_error);
- if (dbus_error_is_set(&dbus_error)) {
- ERR("Cannot add D-BUS match rule, cause: %s", dbus_error.message);
- dbus_error_free(&dbus_error);
- goto failure;
+ ret = __tethering_setup_gdbus();
+ if (ret < 0) {
+ ERR("tethering_setup_gdbus is failed\n");
+ return 0;
}
- dbus_connection_add_filter(tethering_conn, __dnsmasq_signal_filter, tethering_obj, NULL);
-
/* Platform modules */
if (appcore_set_i18n(MOBILEAP_LOCALE_COMMON_PKG, MOBILEAP_LOCALE_COMMON_RES) < 0) {
ERR("appcore_set_i18n is failed\n");
mobileap_state = MOBILE_AP_STATE_NONE;
}
- _init_network((void *)tethering_obj);
_register_wifi_station_handler();
- _register_vconf_cb((void *)tethering_obj);
ret = wifi_initialize();
if (ret != WIFI_ERROR_NONE) {
ERR("wifi_deinitialize() is failed : %d\n", ret);
}
- _unregister_vconf_cb((void *)tethering_obj);
+ _unregister_vconf_cb();
_unregister_wifi_station_handler();
_deinit_network();
- dbus_connection_remove_filter(tethering_conn, __dnsmasq_signal_filter, tethering_obj);
- dbus_bus_remove_match(tethering_conn, rule, NULL);
+ g_dbus_connection_signal_unsubscribe(teth_gdbus_conn, conn_sig_id);
+ g_dbus_connection_signal_unsubscribe(teth_gdbus_conn, deleted_sig_id);
g_object_unref(tethering_obj);
- dbus_g_connection_unref(tethering_bus);
-
- DBG("-\n");
- return 0;
-
- failure:
- if (tethering_obj)
- g_object_unref(tethering_obj);
- if (tethering_bus_proxy)
- g_object_unref(tethering_bus_proxy);
- if (tethering_bus)
- dbus_g_connection_unref(tethering_bus);
-
+ g_bus_unown_name(owner_id);
+ g_object_unref(manager_server);
DBG("-\n");
return 0;
}