Fix the gcov build error
[platform/core/api/tethering.git] / src / tethering.c
index 017550b..55fc98a 100755 (executable)
@@ -78,6 +78,14 @@ static void __handle_wifi_tether_off(GDBusConnection *connection, const gchar *s
                const gchar *object_path, const gchar *interface_name, const gchar *signal_name,
                GVariant *parameters, gpointer user_data);
 
+static void __handle_p2p_tether_on(GDBusConnection *connection, const gchar *sender_name,
+               const gchar *object_path, const gchar *interface_name, const gchar *signal_name,
+               GVariant *parameters, gpointer user_data);
+
+static void __handle_p2p_tether_off(GDBusConnection *connection, const gchar *sender_name,
+               const gchar *object_path, const gchar *interface_name, const gchar *signal_name,
+               GVariant *parameters, gpointer user_data);
+
 static void __handle_usb_tether_on(GDBusConnection *connection, const gchar *sender_name,
                const gchar *object_path, const gchar *interface_name, const gchar *signal_name,
                GVariant *parameters, gpointer user_data);
@@ -130,6 +138,8 @@ static __tethering_sig_t sigs[] = {
        {0, SIGNAL_NAME_NET_CLOSED, __handle_net_closed},
        {0, SIGNAL_NAME_WIFI_TETHER_ON, __handle_wifi_tether_on},
        {0, SIGNAL_NAME_WIFI_TETHER_OFF, __handle_wifi_tether_off},
+       {0, SIGNAL_NAME_P2P_TETHER_ON, __handle_p2p_tether_on},
+       {0, SIGNAL_NAME_P2P_TETHER_OFF, __handle_p2p_tether_off},
        {0, SIGNAL_NAME_USB_TETHER_ON, __handle_usb_tether_on},
        {0, SIGNAL_NAME_USB_TETHER_OFF, __handle_usb_tether_off},
        {0, SIGNAL_NAME_BT_TETHER_ON, __handle_bt_tether_on},
@@ -360,6 +370,107 @@ static tethering_error_e __get_error(int agent_error)
        return err;
 }
 
+static tethering_type_e __convert_to_tethering_type(mobile_ap_type_e type)
+{
+       switch (type) {
+       case MOBILE_AP_TYPE_USB:
+               return TETHERING_TYPE_USB;
+       case MOBILE_AP_TYPE_WIFI:
+               return TETHERING_TYPE_WIFI;
+       case MOBILE_AP_TYPE_BT:
+               return TETHERING_TYPE_BT;
+       case MOBILE_AP_TYPE_P2P:
+               return TETHERING_TYPE_P2P;
+       default:
+               return TETHERING_TYPE_MAX;
+       }
+}
+
+static void __invoke_enable_cb(__tethering_h *th, tethering_type_e type, bool is_requested)
+{
+       if (th == NULL || th->enabled_cb[type] == NULL) {
+               ERR("th or enabled_cb is NULL");
+               return;
+       }
+
+       tethering_enabled_cb ecb = NULL;
+       void *data = NULL;
+
+       if (!_tethering_check_handle(th)) {
+               DBG("Tethering handle is not valid now, ignore it.");
+               return;
+       }
+
+       ecb = th->enabled_cb[type];
+       data = th->enabled_user_data[type];
+       ecb(TETHERING_ERROR_NONE, type, is_requested, data);
+}
+
+static int __get_disabled_cause_by_interface_error(tethering_type_e type)
+{
+       switch (type) {
+       case TETHERING_TYPE_USB:
+               return TETHERING_DISABLED_BY_USB_DISCONNECTION;
+       case TETHERING_TYPE_WIFI:
+               return TETHERING_DISABLED_BY_WIFI_ON;
+       case TETHERING_TYPE_BT:
+               return TETHERING_DISABLED_BY_BT_OFF;
+       default:
+               return TETHERING_DISABLED_BY_OTHERS;
+       }
+}
+
+static void __invoke_disabled_cb(__tethering_h *th, tethering_type_e type, GVariant *parameters)
+{
+       if (th == NULL || th->disabled_cb[type] == NULL) {
+               ERR("th or disabled_cb is NULL");
+               return;
+       }
+
+       tethering_disabled_cause_e code = TETHERING_DISABLED_BY_OTHERS;
+       tethering_disabled_cb dcb = NULL;
+       void *data = NULL;
+       char *buf = NULL;
+
+       if (!_tethering_check_handle(th)) {
+               DBG("Tethering handle is not valid now, ignore it.");
+               return;
+       }
+
+       dcb = th->disabled_cb[type];
+       data = th->disabled_user_data[type];
+
+       g_variant_get(parameters, "(s)", &buf);
+       if (!g_strcmp0(buf, SIGNAL_MSG_NOT_AVAIL_INTERFACE))
+               code = __get_disabled_cause_by_interface_error(type);
+       else if (!g_strcmp0(buf, SIGNAL_MSG_TIMEOUT))
+               code = TETHERING_DISABLED_BY_TIMEOUT;
+
+       dcb(TETHERING_ERROR_NONE, type, code, data);
+       g_free(buf);
+}
+
+static void __invoke_disabled_cbs(__tethering_h *th, tethering_disabled_cause_e code)
+{
+       if (th == NULL) {
+               ERR("th is NULL");
+               return;
+       }
+
+       if (!_tethering_check_handle(th)) {
+               DBG("Tethering handle is not valid now, ignore it.");
+               return;
+       }
+
+       for (tethering_type_e type = TETHERING_TYPE_USB; type <= TETHERING_TYPE_BT; type++) {
+               tethering_disabled_cb dcb = th->disabled_cb[type];
+               if (dcb == NULL)
+                       continue;
+               void *data = th->disabled_user_data[type];
+               dcb(TETHERING_ERROR_NONE, type, code, data);
+       }
+}
+
 static void __handle_dhcp(GDBusConnection *connection, const gchar *sender_name,
                const gchar *object_path, const gchar *interface_name, const gchar *signal_name,
                GVariant *parameters, gpointer user_data)
@@ -398,15 +509,8 @@ static void __handle_dhcp(GDBusConnection *connection, const gchar *sender_name,
                goto DONE;
        }
 
-       if (ap_type == MOBILE_AP_TYPE_USB)
-               type = TETHERING_TYPE_USB;
-       else if (ap_type == MOBILE_AP_TYPE_WIFI)
-               type = TETHERING_TYPE_WIFI;
-       else if (ap_type == MOBILE_AP_TYPE_BT)
-               type = TETHERING_TYPE_BT;
-       else if (ap_type == MOBILE_AP_TYPE_P2P)
-               type = TETHERING_TYPE_P2P;
-       else {
+       type = __convert_to_tethering_type(ap_type);
+       if (type == TETHERING_TYPE_MAX) {
                ERR("Not supported tethering type [%d]\n", ap_type);
                goto DONE;
        }
@@ -444,30 +548,8 @@ static void __handle_net_closed(GDBusConnection *connection, const gchar *sender
 {
        DBG("+\n");
        TETHERING_LOCK;
-
-       if (user_data == NULL) {
-               ERR("parameter(user_data) is NULL");
-               TETHERING_UNLOCK;
-               return;
-       }
-
-       __tethering_h *th = (__tethering_h *)user_data;
-       tethering_type_e type = 0;
-       tethering_disabled_cb dcb = NULL;
-       void *data = NULL;
-       tethering_disabled_cause_e code = TETHERING_DISABLED_BY_NETWORK_CLOSE;
-
        SINFO("Tethering Disabled by network close !");
-
-       for (type = TETHERING_TYPE_USB; type <= TETHERING_TYPE_BT; type++) {
-               dcb = th->disabled_cb[type];
-               if (dcb == NULL)
-                       continue;
-               data = th->disabled_user_data[type];
-
-               dcb(TETHERING_ERROR_NONE, type, code, data);
-       }
-
+       __invoke_disabled_cbs((__tethering_h *)user_data, TETHERING_DISABLED_BY_NETWORK_CLOSE);
        TETHERING_UNLOCK;
        DBG("-\n");
 }
@@ -478,35 +560,7 @@ static void __handle_wifi_tether_on(GDBusConnection *connection, const gchar *se
 {
        DBG("+\n");
        TETHERING_LOCK;
-
-       if (user_data == NULL) {
-               ERR("parameter(user_data) is NULL");
-               TETHERING_UNLOCK;
-               return;
-       }
-
-       __tethering_h *th = (__tethering_h *)user_data;
-       tethering_type_e type = TETHERING_TYPE_WIFI;
-       bool is_requested = false;
-       tethering_enabled_cb ecb = NULL;
-       void *data = NULL;
-
-       if (!_tethering_check_handle(th)) {
-               DBG("Tethering handle is not valid now, ignore it.");
-               TETHERING_UNLOCK;
-               return;
-       }
-
-       ecb = th->enabled_cb[type];
-       if (ecb == NULL) {
-               TETHERING_UNLOCK;
-               return;
-       }
-
-       data = th->enabled_user_data[type];
-
-       ecb(TETHERING_ERROR_NONE, type, is_requested, data);
-
+       __invoke_enable_cb((__tethering_h *)user_data, TETHERING_TYPE_WIFI, false);
        TETHERING_UNLOCK;
        DBG("-\n");
 }
@@ -517,42 +571,29 @@ static void __handle_wifi_tether_off(GDBusConnection *connection, const gchar *s
 {
        DBG("+\n");
        TETHERING_LOCK;
+       __invoke_disabled_cb((__tethering_h *)user_data, TETHERING_TYPE_WIFI, parameters);
+       TETHERING_UNLOCK;
+       DBG("-\n");
+}
 
-       if (user_data == NULL) {
-               ERR("parameter(user_data) is NULL");
-               TETHERING_UNLOCK;
-               return;
-       }
-
-       __tethering_h *th = (__tethering_h *)user_data;
-       tethering_type_e type = TETHERING_TYPE_WIFI;
-       tethering_disabled_cause_e code = TETHERING_DISABLED_BY_OTHERS;
-       tethering_disabled_cb dcb = NULL;
-       void *data = NULL;
-       char *buf = NULL;
-
-       if (!_tethering_check_handle(th)) {
-               DBG("Tethering handle is not valid now, ignore it.");
-               TETHERING_UNLOCK;
-               return;
-       }
-
-       dcb = th->disabled_cb[type];
-       if (dcb == NULL) {
-               TETHERING_UNLOCK;
-               return;
-       }
-
-       data = th->disabled_user_data[type];
-       g_variant_get(parameters, "(s)", &buf);
-       if (!g_strcmp0(buf, SIGNAL_MSG_NOT_AVAIL_INTERFACE))
-               code = TETHERING_DISABLED_BY_WIFI_ON;
-       else if (!g_strcmp0(buf, SIGNAL_MSG_TIMEOUT))
-               code = TETHERING_DISABLED_BY_TIMEOUT;
-
-       g_free(buf);
-       dcb(TETHERING_ERROR_NONE, type, code, data);
+static void __handle_p2p_tether_on(GDBusConnection *connection, const gchar *sender_name,
+                       const gchar *object_path, const gchar *interface_name, const gchar *signal_name,
+                       GVariant *parameters, gpointer user_data)
+{
+       DBG("+\n");
+       TETHERING_LOCK;
+       __invoke_enable_cb((__tethering_h *)user_data, TETHERING_TYPE_P2P, false);
+       TETHERING_UNLOCK;
+       DBG("-\n");
+}
 
+static void __handle_p2p_tether_off(GDBusConnection *connection, const gchar *sender_name,
+                       const gchar *object_path, const gchar *interface_name, const gchar *signal_name,
+                       GVariant *parameters, gpointer user_data)
+{
+       DBG("+\n");
+       TETHERING_LOCK;
+       __invoke_disabled_cb((__tethering_h *)user_data, TETHERING_TYPE_P2P, parameters);
        TETHERING_UNLOCK;
        DBG("-\n");
 }
@@ -563,35 +604,7 @@ static void __handle_usb_tether_on(GDBusConnection *connection, const gchar *sen
 {
        DBG("+\n");
        TETHERING_LOCK;
-
-       if (user_data == NULL) {
-               ERR("parameter(user_data) is NULL");
-               TETHERING_UNLOCK;
-               return;
-       }
-
-       __tethering_h *th = (__tethering_h *)user_data;
-       tethering_type_e type = TETHERING_TYPE_USB;
-       bool is_requested = false;
-       tethering_enabled_cb ecb = NULL;
-       void *data = NULL;
-
-       if (!_tethering_check_handle(th)) {
-               DBG("Tethering handle is not valid now, ignore it.");
-               TETHERING_UNLOCK;
-               return;
-       }
-
-       ecb = th->enabled_cb[type];
-       if (ecb == NULL) {
-               TETHERING_UNLOCK;
-               return;
-       }
-
-       data = th->enabled_user_data[type];
-
-       ecb(TETHERING_ERROR_NONE, type, is_requested, data);
-
+       __invoke_enable_cb((__tethering_h *)user_data, TETHERING_TYPE_USB, false);
        TETHERING_UNLOCK;
        DBG("-\n");
 }
@@ -602,41 +615,7 @@ static void __handle_usb_tether_off(GDBusConnection *connection, const gchar *se
 {
        DBG("+\n");
        TETHERING_LOCK;
-
-       if (user_data == NULL) {
-               ERR("parameter(user_data) is NULL");
-               TETHERING_UNLOCK;
-               return;
-       }
-
-       __tethering_h *th = (__tethering_h *)user_data;
-       tethering_type_e type = TETHERING_TYPE_USB;
-       tethering_disabled_cause_e code = TETHERING_DISABLED_BY_OTHERS;
-       tethering_disabled_cb dcb = NULL;
-       void *data = NULL;
-       char *buf = NULL;
-
-       if (!_tethering_check_handle(th)) {
-               DBG("Tethering handle is not valid now, ignore it.");
-               TETHERING_UNLOCK;
-               return;
-       }
-
-       dcb = th->disabled_cb[type];
-       if (dcb == NULL) {
-               TETHERING_UNLOCK;
-               return;
-       }
-
-       data = th->disabled_user_data[type];
-
-       g_variant_get(parameters, "(s)", &buf);
-       if (!g_strcmp0(buf, SIGNAL_MSG_NOT_AVAIL_INTERFACE))
-               code = TETHERING_DISABLED_BY_USB_DISCONNECTION;
-
-       dcb(TETHERING_ERROR_NONE, type, code, data);
-       g_free(buf);
-
+       __invoke_disabled_cb((__tethering_h *)user_data, TETHERING_TYPE_USB, parameters);
        TETHERING_UNLOCK;
        DBG("-\n");
 }
@@ -647,35 +626,7 @@ static void __handle_bt_tether_on(GDBusConnection *connection, const gchar *send
 {
        DBG("+\n");
        TETHERING_LOCK;
-
-       if (user_data == NULL) {
-               ERR("parameter(user_data) is NULL");
-               TETHERING_UNLOCK;
-               return;
-       }
-
-       __tethering_h *th = (__tethering_h *)user_data;
-       tethering_type_e type = TETHERING_TYPE_BT;
-       bool is_requested = false;
-       tethering_enabled_cb ecb = NULL;
-       void *data = NULL;
-
-       if (!_tethering_check_handle(th)) {
-               DBG("Tethering handle is not valid now, ignore it.");
-               TETHERING_UNLOCK;
-               return;
-       }
-
-       ecb = th->enabled_cb[type];
-       if (ecb == NULL) {
-               TETHERING_UNLOCK;
-               return;
-       }
-
-       data = th->enabled_user_data[type];
-
-       ecb(TETHERING_ERROR_NONE, type, is_requested, data);
-
+       __invoke_enable_cb((__tethering_h *)user_data, TETHERING_TYPE_BT, false);
        TETHERING_UNLOCK;
        DBG("-\n");
 }
@@ -686,43 +637,7 @@ static void __handle_bt_tether_off(GDBusConnection *connection, const gchar *sen
 {
        DBG("+\n");
        TETHERING_LOCK;
-
-       if (user_data == NULL) {
-               ERR("parameter(user_data) is NULL");
-               TETHERING_UNLOCK;
-               return;
-       }
-
-       __tethering_h *th = (__tethering_h *)user_data;
-       tethering_type_e type = TETHERING_TYPE_BT;
-       tethering_disabled_cause_e code = TETHERING_DISABLED_BY_OTHERS;
-       tethering_disabled_cb dcb = NULL;
-       void *data = NULL;
-       char *buf = NULL;
-
-       if (!_tethering_check_handle(th)) {
-               DBG("Tethering handle is not valid now, ignore it.");
-               TETHERING_UNLOCK;
-               return;
-       }
-
-       dcb = th->disabled_cb[type];
-       if (dcb == NULL) {
-               TETHERING_UNLOCK;
-               return;
-       }
-
-       data = th->disabled_user_data[type];
-       g_variant_get(parameters, "(s)", &buf);
-       if (!g_strcmp0(buf, SIGNAL_MSG_NOT_AVAIL_INTERFACE))
-               code = TETHERING_DISABLED_BY_BT_OFF;
-       else if (!g_strcmp0(buf, SIGNAL_MSG_TIMEOUT))
-               code = TETHERING_DISABLED_BY_TIMEOUT;
-
-       dcb(TETHERING_ERROR_NONE, type, code, data);
-
-       g_free(buf);
-
+       __invoke_disabled_cb((__tethering_h *)user_data, TETHERING_TYPE_BT, parameters);
        TETHERING_UNLOCK;
        DBG("-\n");
 }
@@ -733,34 +648,7 @@ static void __handle_no_data_timeout(GDBusConnection *connection, const gchar *s
 {
        DBG("+\n");
        TETHERING_LOCK;
-
-       if (user_data == NULL) {
-               ERR("parameter(user_data) is NULL");
-               TETHERING_UNLOCK;
-               return;
-       }
-
-       __tethering_h *th = (__tethering_h *)user_data;
-       tethering_type_e type = 0;
-       tethering_disabled_cb dcb = NULL;
-       void *data = NULL;
-       tethering_disabled_cause_e code = TETHERING_DISABLED_BY_TIMEOUT;
-
-       if (!_tethering_check_handle(th)) {
-               DBG("Tethering handle is not valid now, ignore it.");
-               TETHERING_UNLOCK;
-               return;
-       }
-
-       for (type = TETHERING_TYPE_USB; type <= TETHERING_TYPE_BT; type++) {
-               dcb = th->disabled_cb[type];
-               if (dcb == NULL)
-                       continue;
-               data = th->disabled_user_data[type];
-
-               dcb(TETHERING_ERROR_NONE, type, code, data);
-       }
-
+       __invoke_disabled_cbs((__tethering_h *)user_data, TETHERING_DISABLED_BY_TIMEOUT);
        TETHERING_UNLOCK;
        DBG("-\n");
 }
@@ -771,34 +659,7 @@ static void __handle_low_battery_mode(GDBusConnection *connection, const gchar *
 {
        DBG("+\n");
        TETHERING_LOCK;
-
-       if (user_data == NULL) {
-               ERR("parameter(user_data) is NULL");
-               TETHERING_UNLOCK;
-               return;
-       }
-
-       __tethering_h *th = (__tethering_h *)user_data;
-       tethering_type_e type = 0;
-       tethering_disabled_cb dcb = NULL;
-       void *data = NULL;
-       tethering_disabled_cause_e code = TETHERING_DISABLED_BY_LOW_BATTERY;
-
-       if (!_tethering_check_handle(th)) {
-               DBG("Tethering handle is not valid now, ignore it.");
-               TETHERING_UNLOCK;
-               return;
-       }
-
-       for (type = TETHERING_TYPE_USB; type <= TETHERING_TYPE_BT; type++) {
-               dcb = th->disabled_cb[type];
-               if (dcb == NULL)
-                       continue;
-               data = th->disabled_user_data[type];
-
-               dcb(TETHERING_ERROR_NONE, type, code, data);
-       }
-
+       __invoke_disabled_cbs((__tethering_h *)user_data, TETHERING_DISABLED_BY_LOW_BATTERY);
        TETHERING_UNLOCK;
        DBG("-\n");
 }
@@ -809,34 +670,7 @@ static void __handle_flight_mode(GDBusConnection *connection, const gchar *sende
 {
        DBG("+\n");
        TETHERING_LOCK;
-
-       if (user_data == NULL) {
-               ERR("parameter(user_data) is NULL");
-               TETHERING_UNLOCK;
-               return;
-       }
-
-       __tethering_h *th = (__tethering_h *)user_data;
-       tethering_type_e type = 0;
-       tethering_disabled_cb dcb = NULL;
-       void *data = NULL;
-       tethering_disabled_cause_e code = TETHERING_DISABLED_BY_FLIGHT_MODE;
-
-       if (!_tethering_check_handle(th)) {
-               DBG("Tethering handle is not valid now, ignore it.");
-               TETHERING_UNLOCK;
-               return;
-       }
-
-       for (type = TETHERING_TYPE_USB; type <= TETHERING_TYPE_BT; type++) {
-               dcb = th->disabled_cb[type];
-               if (dcb == NULL)
-                       continue;
-               data = th->disabled_user_data[type];
-
-               dcb(TETHERING_ERROR_NONE, type, code, data);
-       }
-
+       __invoke_disabled_cbs((__tethering_h *)user_data, TETHERING_DISABLED_BY_FLIGHT_MODE);
        TETHERING_UNLOCK;
        DBG("-\n");
 }
@@ -1023,6 +857,7 @@ static void __wifi_enabled_cfm_cb(GObject *source_object, GAsyncResult *res,
                g_error_free(g_error);
        } else {
                g_variant_get(g_var, "(u)", &info);
+               g_variant_unref(g_var);
                error = __get_error(info);
        }
        retry = 0;
@@ -1042,7 +877,6 @@ static void __wifi_enabled_cfm_cb(GObject *source_object, GAsyncResult *res,
                return;
        }
        ecb(error, type, true, data);
-       g_variant_unref(g_var);
 
        TETHERING_UNLOCK;
        INFO("-\n");
@@ -2946,20 +2780,16 @@ API int tethering_foreach_connected_clients(tethering_h tethering, tethering_typ
                while (g_variant_iter_loop(inner_iter, "{sv}", &key, &value)) {
                        if (g_strcmp0(key, "Type") == 0) {
                                interface = g_variant_get_int32(value);
-                               if (interface == MOBILE_AP_TYPE_USB)
-                                       client.interface = TETHERING_TYPE_USB;
-                               else if (interface == MOBILE_AP_TYPE_WIFI)
-                                       client.interface = TETHERING_TYPE_WIFI;
-                               else if (interface == MOBILE_AP_TYPE_BT)
-                                       client.interface = TETHERING_TYPE_BT;
-                               else if (interface == MOBILE_AP_TYPE_P2P)
-                                       client.interface = TETHERING_TYPE_P2P;
-                               else {
+                               tethering_type_e converted_type = __convert_to_tethering_type(interface);
+                               if (converted_type == TETHERING_TYPE_MAX) {
                                        ERR("Invalid interface\n");
                                        g_free(key);
                                        g_variant_unref(value);
                                        break;
+                               } else {
+                                       client.interface = converted_type;
                                }
+
                                DBG("interface is %d\n", client.interface);
                                if (client.interface != type && (TETHERING_TYPE_ALL != type)) {
                                        g_free(key);