tizen 2.3.1 release tizen_2.3.1 submit/tizen_2.3.1/20150915.080634 tizen_2.3.1_release
authorjk7744.park <jk7744.park@samsung.com>
Tue, 8 Sep 2015 13:20:44 +0000 (22:20 +0900)
committerjk7744.park <jk7744.park@samsung.com>
Tue, 8 Sep 2015 13:20:44 +0000 (22:20 +0900)
29 files changed:
AUTHORS [new file with mode: 0644]
CMakeLists.txt [new file with mode: 0755]
LICENSE.APLv2.0 [new file with mode: 0644]
NOTICE [new file with mode: 0644]
include/mobileap.h [new file with mode: 0644]
include/mobileap_bluetooth.h [new file with mode: 0644]
include/mobileap_common.h [new file with mode: 0755]
include/mobileap_handler.h [new file with mode: 0644]
include/mobileap_iptables.h [new file with mode: 0644]
include/mobileap_network.h [new file with mode: 0644]
include/mobileap_notification.h [new file with mode: 0644]
include/mobileap_softap.h [new file with mode: 0755]
include/mobileap_usb.h [new file with mode: 0644]
include/mobileap_wifi.h [new file with mode: 0644]
include/tethering-dbus-interface.xml [new file with mode: 0644]
mobileap-agent.manifest [new file with mode: 0644]
packaging/mobileap-agent.spec [new file with mode: 0644]
packaging/org.tizen.tethering.service [new file with mode: 0644]
src/mobileap_bluetooth.c [new file with mode: 0755]
src/mobileap_common.c [new file with mode: 0755]
src/mobileap_handler.c [new file with mode: 0644]
src/mobileap_iptables.c [new file with mode: 0755]
src/mobileap_main.c [new file with mode: 0644]
src/mobileap_network.c [new file with mode: 0644]
src/mobileap_notification.c [new file with mode: 0644]
src/mobileap_softap.c [new file with mode: 0755]
src/mobileap_usb.c [new file with mode: 0755]
src/mobileap_wifi.c [new file with mode: 0755]
tethering_dump.sh [new file with mode: 0644]

diff --git a/AUTHORS b/AUTHORS
new file mode 100644 (file)
index 0000000..4d93362
--- /dev/null
+++ b/AUTHORS
@@ -0,0 +1,3 @@
+Hocheol Seo <hocheol.seo at samsung dot com>
+Injun Yang <injun.yang at samsung dot com>
+Seungyoun Ju <sy39.ju at samsung dot com>
diff --git a/CMakeLists.txt b/CMakeLists.txt
new file mode 100755 (executable)
index 0000000..7243cd2
--- /dev/null
@@ -0,0 +1,65 @@
+CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
+PROJECT(mobileap-agent C)
+
+SET(SRCS src/mobileap_softap.c
+       src/mobileap_main.c
+       src/mobileap_wifi.c
+       src/mobileap_usb.c
+       src/mobileap_bluetooth.c
+       src/mobileap_handler.c
+       src/mobileap_common.c
+       src/mobileap_notification.c
+       src/mobileap_network.c
+       src/mobileap_iptables.c
+)
+
+SET(CMAKE_INSTALL_PREFIX /usr)
+
+SET(APP_VENDOR "samsung")
+SET(APP_NAME mobileap-agent)
+SET(APP_DIR ${CMAKE_INSTALL_PREFIX}/bin)
+SET(INCLUDE_DIR ${CMAKE_SOURCE_DIR}/include)
+
+IF("${CMAKE_BUILD_TYPE}" STREQUAL "")
+       SET(CMAKE_BUILD_TYPE "Release")
+ENDIF("${CMAKE_BUILD_TYPE}" STREQUAL "")
+
+MESSAGE("Build type: ${CMAKE_BUILD_TYPE}")
+
+INCLUDE_DIRECTORIES(${INCLUDE_DIR})
+
+INCLUDE(FindPkgConfig)
+pkg_check_modules(pkgs REQUIRED gio-2.0 dlog dbus-glib-1 deviced vconf notification capi-network-connection capi-network-bluetooth appcore-common capi-network-wifi capi-network-wifi-direct alarm-service appsvc libssl)
+FOREACH(flag ${pkgs_CFLAGS})
+       SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}")
+ENDFOREACH(flag)
+
+SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS} -fPIE -Wall")
+SET(CMAKE_C_FLAGS_DEBUG "-O0 -g -fPIE")
+SET(CMAKE_C_FLAGS_RELEASE "-O2 -fPIE")
+
+FIND_PROGRAM(UNAME NAMES uname)
+EXEC_PROGRAM("${UNAME}" ARGS "-m" OUTPUT_VARIABLE "ARCH")
+IF("${ARCH}" STREQUAL "arm")
+       ADD_DEFINITIONS("-DTARGET")
+       MESSAGE("add -DTARGET")
+ENDIF("${ARCH}" STREQUAL "arm")
+
+FIND_PROGRAM(DBUS_BINDING_TOOL NAMES dbus-binding-tool)
+EXEC_PROGRAM("${DBUS_BINDING_TOOL}" ARGS "--prefix=tethering ${INCLUDE_DIR}/tethering-dbus-interface.xml --mode=glib-server --output=${INCLUDE_DIR}/tethering-server-stub.h")
+
+ADD_DEFINITIONS("-DPREFIX=\"${CMAKE_INSTALL_PREFIX}\"")
+ADD_DEFINITIONS("-DVENDOR=\"${APP_VENDOR}\"")
+ADD_DEFINITIONS("-DAPPNAME=\"${APP_NAME}\"")
+ADD_DEFINITIONS("-DAPP_DIR=\"${APP_DIR}\"")
+
+SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--as-needed -pie")
+
+ADD_EXECUTABLE(${PROJECT_NAME} ${SRCS})
+SET(CMAKE_EXE_LINKER_FLAGS "-Wl,--as-needed -pie")
+TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${pkgs_LDFLAGS})
+
+INSTALL(TARGETS ${PROJECT_NAME} DESTINATION bin)
+INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/packaging/org.tizen.tethering.service DESTINATION share/dbus-1/services)
+INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/tethering_dump.sh DESTINATION /opt/etc/dump.d/module.d/)
+
diff --git a/LICENSE.APLv2.0 b/LICENSE.APLv2.0
new file mode 100644 (file)
index 0000000..d645695
--- /dev/null
@@ -0,0 +1,202 @@
+
+                                 Apache License
+                           Version 2.0, January 2004
+                        http://www.apache.org/licenses/
+
+   TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
+
+   1. Definitions.
+
+      "License" shall mean the terms and conditions for use, reproduction,
+      and distribution as defined by Sections 1 through 9 of this document.
+
+      "Licensor" shall mean the copyright owner or entity authorized by
+      the copyright owner that is granting the License.
+
+      "Legal Entity" shall mean the union of the acting entity and all
+      other entities that control, are controlled by, or are under common
+      control with that entity. For the purposes of this definition,
+      "control" means (i) the power, direct or indirect, to cause the
+      direction or management of such entity, whether by contract or
+      otherwise, or (ii) ownership of fifty percent (50%) or more of the
+      outstanding shares, or (iii) beneficial ownership of such entity.
+
+      "You" (or "Your") shall mean an individual or Legal Entity
+      exercising permissions granted by this License.
+
+      "Source" form shall mean the preferred form for making modifications,
+      including but not limited to software source code, documentation
+      source, and configuration files.
+
+      "Object" form shall mean any form resulting from mechanical
+      transformation or translation of a Source form, including but
+      not limited to compiled object code, generated documentation,
+      and conversions to other media types.
+
+      "Work" shall mean the work of authorship, whether in Source or
+      Object form, made available under the License, as indicated by a
+      copyright notice that is included in or attached to the work
+      (an example is provided in the Appendix below).
+
+      "Derivative Works" shall mean any work, whether in Source or Object
+      form, that is based on (or derived from) the Work and for which the
+      editorial revisions, annotations, elaborations, or other modifications
+      represent, as a whole, an original work of authorship. For the purposes
+      of this License, Derivative Works shall not include works that remain
+      separable from, or merely link (or bind by name) to the interfaces of,
+      the Work and Derivative Works thereof.
+
+      "Contribution" shall mean any work of authorship, including
+      the original version of the Work and any modifications or additions
+      to that Work or Derivative Works thereof, that is intentionally
+      submitted to Licensor for inclusion in the Work by the copyright owner
+      or by an individual or Legal Entity authorized to submit on behalf of
+      the copyright owner. For the purposes of this definition, "submitted"
+      means any form of electronic, verbal, or written communication sent
+      to the Licensor or its representatives, including but not limited to
+      communication on electronic mailing lists, source code control systems,
+      and issue tracking systems that are managed by, or on behalf of, the
+      Licensor for the purpose of discussing and improving the Work, but
+      excluding communication that is conspicuously marked or otherwise
+      designated in writing by the copyright owner as "Not a Contribution."
+
+      "Contributor" shall mean Licensor and any individual or Legal Entity
+      on behalf of whom a Contribution has been received by Licensor and
+      subsequently incorporated within the Work.
+
+   2. Grant of Copyright License. Subject to the terms and conditions of
+      this License, each Contributor hereby grants to You a perpetual,
+      worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+      copyright license to reproduce, prepare Derivative Works of,
+      publicly display, publicly perform, sublicense, and distribute the
+      Work and such Derivative Works in Source or Object form.
+
+   3. Grant of Patent License. Subject to the terms and conditions of
+      this License, each Contributor hereby grants to You a perpetual,
+      worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+      (except as stated in this section) patent license to make, have made,
+      use, offer to sell, sell, import, and otherwise transfer the Work,
+      where such license applies only to those patent claims licensable
+      by such Contributor that are necessarily infringed by their
+      Contribution(s) alone or by combination of their Contribution(s)
+      with the Work to which such Contribution(s) was submitted. If You
+      institute patent litigation against any entity (including a
+      cross-claim or counterclaim in a lawsuit) alleging that the Work
+      or a Contribution incorporated within the Work constitutes direct
+      or contributory patent infringement, then any patent licenses
+      granted to You under this License for that Work shall terminate
+      as of the date such litigation is filed.
+
+   4. Redistribution. You may reproduce and distribute copies of the
+      Work or Derivative Works thereof in any medium, with or without
+      modifications, and in Source or Object form, provided that You
+      meet the following conditions:
+
+      (a) You must give any other recipients of the Work or
+          Derivative Works a copy of this License; and
+
+      (b) You must cause any modified files to carry prominent notices
+          stating that You changed the files; and
+
+      (c) You must retain, in the Source form of any Derivative Works
+          that You distribute, all copyright, patent, trademark, and
+          attribution notices from the Source form of the Work,
+          excluding those notices that do not pertain to any part of
+          the Derivative Works; and
+
+      (d) If the Work includes a "NOTICE" text file as part of its
+          distribution, then any Derivative Works that You distribute must
+          include a readable copy of the attribution notices contained
+          within such NOTICE file, excluding those notices that do not
+          pertain to any part of the Derivative Works, in at least one
+          of the following places: within a NOTICE text file distributed
+          as part of the Derivative Works; within the Source form or
+          documentation, if provided along with the Derivative Works; or,
+          within a display generated by the Derivative Works, if and
+          wherever such third-party notices normally appear. The contents
+          of the NOTICE file are for informational purposes only and
+          do not modify the License. You may add Your own attribution
+          notices within Derivative Works that You distribute, alongside
+          or as an addendum to the NOTICE text from the Work, provided
+          that such additional attribution notices cannot be construed
+          as modifying the License.
+
+      You may add Your own copyright statement to Your modifications and
+      may provide additional or different license terms and conditions
+      for use, reproduction, or distribution of Your modifications, or
+      for any such Derivative Works as a whole, provided Your use,
+      reproduction, and distribution of the Work otherwise complies with
+      the conditions stated in this License.
+
+   5. Submission of Contributions. Unless You explicitly state otherwise,
+      any Contribution intentionally submitted for inclusion in the Work
+      by You to the Licensor shall be under the terms and conditions of
+      this License, without any additional terms or conditions.
+      Notwithstanding the above, nothing herein shall supersede or modify
+      the terms of any separate license agreement you may have executed
+      with Licensor regarding such Contributions.
+
+   6. Trademarks. This License does not grant permission to use the trade
+      names, trademarks, service marks, or product names of the Licensor,
+      except as required for reasonable and customary use in describing the
+      origin of the Work and reproducing the content of the NOTICE file.
+
+   7. Disclaimer of Warranty. Unless required by applicable law or
+      agreed to in writing, Licensor provides the Work (and each
+      Contributor provides its Contributions) on an "AS IS" BASIS,
+      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+      implied, including, without limitation, any warranties or conditions
+      of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
+      PARTICULAR PURPOSE. You are solely responsible for determining the
+      appropriateness of using or redistributing the Work and assume any
+      risks associated with Your exercise of permissions under this License.
+
+   8. Limitation of Liability. In no event and under no legal theory,
+      whether in tort (including negligence), contract, or otherwise,
+      unless required by applicable law (such as deliberate and grossly
+      negligent acts) or agreed to in writing, shall any Contributor be
+      liable to You for damages, including any direct, indirect, special,
+      incidental, or consequential damages of any character arising as a
+      result of this License or out of the use or inability to use the
+      Work (including but not limited to damages for loss of goodwill,
+      work stoppage, computer failure or malfunction, or any and all
+      other commercial damages or losses), even if such Contributor
+      has been advised of the possibility of such damages.
+
+   9. Accepting Warranty or Additional Liability. While redistributing
+      the Work or Derivative Works thereof, You may choose to offer,
+      and charge a fee for, acceptance of support, warranty, indemnity,
+      or other liability obligations and/or rights consistent with this
+      License. However, in accepting such obligations, You may act only
+      on Your own behalf and on Your sole responsibility, not on behalf
+      of any other Contributor, and only if You agree to indemnify,
+      defend, and hold each Contributor harmless for any liability
+      incurred by, or claims asserted against, such Contributor by reason
+      of your accepting any such warranty or additional liability.
+
+   END OF TERMS AND CONDITIONS
+
+   APPENDIX: How to apply the Apache License to your work.
+
+      To apply the Apache License to your work, attach the following
+      boilerplate notice, with the fields enclosed by brackets "[]"
+      replaced with your own identifying information. (Don't include
+      the brackets!)  The text should be enclosed in the appropriate
+      comment syntax for the file format. We also recommend that a
+      file or class name and description of purpose be included on the
+      same "printed page" as the copyright notice for easier
+      identification within third-party archives.
+
+   Copyright [yyyy] [name of copyright owner]
+
+   Licensed under the Apache License, Version 2.0 (the "License");
+   you may not use this file except in compliance with the License.
+   You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
diff --git a/NOTICE b/NOTICE
new file mode 100644 (file)
index 0000000..ccdad52
--- /dev/null
+++ b/NOTICE
@@ -0,0 +1,3 @@
+Copyright (c) Samsung Electronics Co., Ltd. All rights reserved.
+Except as noted, this software is licensed under Apache License, Version 2.
+Please, see the LICENSE file for Apache License terms and conditions.
diff --git a/include/mobileap.h b/include/mobileap.h
new file mode 100644 (file)
index 0000000..a5ad66f
--- /dev/null
@@ -0,0 +1,220 @@
+/*
+ * mobileap-agent
+ * Copyright (c) 2012 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the License);
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef __MOBILEAP_INTERNAL_H__
+#define __MOBILEAP_INTERNAL_H__
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/* Client / Agent common */
+#define DBUS_STRUCT_UINT_STRING (dbus_g_type_get_struct ("GValueArray", \
+                       G_TYPE_UINT, G_TYPE_STRING, G_TYPE_INVALID))
+
+#define DBUS_STRUCT_STATIONS (dbus_g_type_get_struct ("GValueArray", \
+                       G_TYPE_UINT, G_TYPE_STRING, G_TYPE_STRING, \
+                       G_TYPE_STRING, G_TYPE_UINT, G_TYPE_INVALID))
+
+#define DBUS_STRUCT_STATION (dbus_g_type_get_struct ("GValueArray", \
+                       G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, \
+                       G_TYPE_INVALID))
+
+#define DBUS_STRUCT_INTERFACE (dbus_g_type_get_struct ("GValueArray", \
+                       G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, \
+                       G_TYPE_STRING, G_TYPE_INVALID))
+
+
+#define TETHERING_SERVICE_OBJECT_PATH  "/Tethering"
+#define TETHERING_SERVICE_NAME         "org.tizen.tethering"
+#define TETHERING_SERVICE_INTERFACE    "org.tizen.tethering"
+
+#define SIGNAL_NAME_NET_CLOSED         "net_closed"
+#define SIGNAL_NAME_STA_CONNECT                "sta_connected"
+#define SIGNAL_NAME_STA_DISCONNECT     "sta_disconnected"
+#define SIGNAL_NAME_WIFI_TETHER_ON     "wifi_on"
+#define SIGNAL_NAME_WIFI_TETHER_OFF    "wifi_off"
+#define SIGNAL_NAME_USB_TETHER_ON      "usb_on"
+#define SIGNAL_NAME_USB_TETHER_OFF     "usb_off"
+#define SIGNAL_NAME_BT_TETHER_ON       "bluetooth_on"
+#define SIGNAL_NAME_BT_TETHER_OFF      "bluetooth_off"
+#define SIGNAL_NAME_WIFI_AP_ON         "wifi_ap_on"
+#define SIGNAL_NAME_WIFI_AP_OFF                "wifi_ap_off"
+#define SIGNAL_NAME_NO_DATA_TIMEOUT    "no_data_timeout"
+#define SIGNAL_NAME_LOW_BATTERY_MODE   "low_batt_mode"
+#define SIGNAL_NAME_FLIGHT_MODE                "flight_mode"
+#define SIGNAL_NAME_POWER_SAVE_MODE            "power_save_mode"
+#define SIGNAL_NAME_DHCP_STATUS                "dhcp_status"
+#define SIGNAL_NAME_SECURITY_TYPE_CHANGED      "security_type_changed"
+#define SIGNAL_NAME_SSID_VISIBILITY_CHANGED    "ssid_visibility_changed"
+#define SIGNAL_NAME_PASSPHRASE_CHANGED         "passphrase_changed"
+
+#define SIGNAL_MSG_NOT_AVAIL_INTERFACE "Interface is not available"
+#define SIGNAL_MSG_TIMEOUT             "There is no connection for a while"
+#define SIGNAL_MSG_SSID_VISIBLE                "ssid_visible"
+#define SIGNAL_MSG_SSID_HIDE           "ssid_hide"
+
+#define DNSMASQ_LEASES_FILE            "/var/lib/misc/dnsmasq.leases"
+#define IP_USB_SUBNET                  "192.168.129"
+
+typedef enum {
+       E_SIGNAL_NET_CLOSED,
+       E_SIGNAL_STA_CONNECT,
+       E_SIGNAL_STA_DISCONNECT,
+       E_SIGNAL_WIFI_TETHER_ON,
+       E_SIGNAL_WIFI_TETHER_OFF,
+       E_SIGNAL_USB_TETHER_ON,
+       E_SIGNAL_USB_TETHER_OFF,
+       E_SIGNAL_BT_TETHER_ON,
+       E_SIGNAL_BT_TETHER_OFF,
+       E_SIGNAL_WIFI_AP_ON,
+       E_SIGNAL_WIFI_AP_OFF,
+       E_SIGNAL_NO_DATA_TIMEOUT,
+       E_SIGNAL_LOW_BATTERY_MODE,
+       E_SIGNAL_FLIGHT_MODE,
+       E_SIGNAL_POWER_SAVE_MODE,
+       E_SIGNAL_SECURITY_TYPE_CHANGED,
+       E_SIGNAL_SSID_VISIBILITY_CHANGED,
+       E_SIGNAL_PASSPHRASE_CHANGED,
+       E_SIGNAL_MAX
+} mobile_ap_sig_e;
+
+/**
+* WiFi tethering configuration
+*/
+#define MOBILE_AP_WIFI_CHANNEL         6       /**< Channel number */
+#define MOBILE_AP_WIFI_BSSID_LEN       6       /**< BSSID Length */
+#define MOBILE_AP_WIFI_SSID_MAX_LEN    32      /**< Maximum length of ssid */
+#define MOBILE_AP_WIFI_KEY_MIN_LEN     8       /**< Minimum length of wifi key */
+#define MOBILE_AP_WIFI_PLAIN_TEXT_KEY_MAX_LEN  63      /**< Maximum length of wifi plain text key */
+#define MOBILE_AP_WIFI_KEY_MAX_LEN     64      /**< Maximum length of wifi hash key */
+
+/**
+* Common configuration
+*/
+#define MOBILE_AP_MAX_WIFI_STA         10      /**< Firmware limitation (BCM4339) */
+#define MOBILE_AP_MAX_BT_STA           4       /**< Bluetooth specification (1 Master and 4 Slaves) */
+#define MOBILE_AP_MAX_USB_STA          1       /**< Only one usb connection is possible */
+#define MOBILE_AP_MAX_CONNECTED_STA    15      /**< Maximum connected station. 10(Wi-Fi) + 4(BT) + 1(USB) */
+
+#define MOBILE_AP_STR_INFO_LEN         20      /**< length of the ip or mac address*/
+#define MOBILE_AP_STR_HOSTNAME_LEN     33      /**< length of the hostname */
+#define MOBILE_AP_NAME_UNKNOWN         "UNKNOWN"
+
+/**
+* Mobile AP error code
+*/
+typedef enum {
+       MOBILE_AP_ERROR_NONE,                   /**< No error */
+       MOBILE_AP_ERROR_RESOURCE,               /**< Socket creation error, file open error */
+       MOBILE_AP_ERROR_INTERNAL,               /**< Driver related error */
+       MOBILE_AP_ERROR_INVALID_PARAM,          /**< Invalid parameter */
+       MOBILE_AP_ERROR_ALREADY_ENABLED,        /**< Mobile AP is already ON */
+       MOBILE_AP_ERROR_NOT_ENABLED,            /**< Mobile AP is not ON, so cannot be disabled */
+       MOBILE_AP_ERROR_NET_OPEN,               /**< PDP network open error */
+       MOBILE_AP_ERROR_NET_CLOSE,              /**< PDP network close error */
+       MOBILE_AP_ERROR_DHCP,                   /**< DHCP error */
+       MOBILE_AP_ERROR_IN_PROGRESS,            /**< Request is in progress */
+       MOBILE_AP_ERROR_NOT_PERMITTED,          /**< Operation is not permitted */
+       MOBILE_AP_ERROR_PERMISSION_DENIED,      /**< Permission Denied */
+
+       MOBILE_AP_ERROR_MAX
+} mobile_ap_error_code_e;
+
+/**
+* Event type on callback
+*/
+typedef enum {
+       MOBILE_AP_ENABLE_CFM,                   /* mobile_ap_enable() */
+       MOBILE_AP_DISABLE_CFM,                  /* mobile_ap_disable() */
+
+       MOBILE_AP_ENABLE_WIFI_TETHERING_CFM,    /* mobile_ap_enable_wifi_tethering() */
+       MOBILE_AP_DISABLE_WIFI_TETHERING_CFM,   /* mobile_ap_disable_wifi_tethering() */
+       MOBILE_AP_CHANGE_WIFI_CONFIG_CFM,       /* mobile_ap_change_wifi_config() */
+
+       MOBILE_AP_ENABLE_USB_TETHERING_CFM,     /* mobile_ap_enable_usb_tethering() */
+       MOBILE_AP_DISABLE_USB_TETHERING_CFM,    /* mobile_ap_disable_usb_tethering() */
+
+       MOBILE_AP_ENABLE_BT_TETHERING_CFM,      /* mobile_ap_enable_bt_tethering() */
+       MOBILE_AP_DISABLE_BT_TETHERING_CFM,     /* mobile_ap_disable_bt_tethering() */
+
+       MOBILE_AP_ENABLE_WIFI_AP_CFM,           /* mobile_ap_enable_wifi_ap() */
+       MOBILE_AP_DISABLE_WIFI_AP_CFM,          /* mobile_ap_disable_wifi_ap() */
+
+       MOBILE_AP_GET_STATION_INFO_CFM,         /* mobile_ap_get_station_info() */
+       MOBILE_AP_GET_DATA_PACKET_USAGE_CFM,    /* mobile_ap_get_data_packet_usage() */
+
+       MOBILE_AP_DISABLED_IND,                 /* Turning off tethering service indication */
+
+       MOBILE_AP_ENABLED_WIFI_TETHERING_IND,   /* Turning on WiFi tethering indication */
+       MOBILE_AP_DISABLED_WIFI_TETHERING_IND,  /* Turning off WiFi tethering indication */
+
+       MOBILE_AP_ENABLED_USB_TETHERING_IND,    /* Turning on USB tethering indication */
+       MOBILE_AP_DISABLED_USB_TETHERING_IND,   /* Turning off USB tethering indication */
+
+       MOBILE_AP_ENABLED_BT_TETHERING_IND,     /* Turning on BT tethering indication */
+       MOBILE_AP_DISABLED_BT_TETHERING_IND,    /* Turning off BT tethering indication */
+
+       MOBILE_AP_ENABLED_WIFI_AP_IND,          /* Turning on WiFi AP indication */
+       MOBILE_AP_DISABLED_WIFI_AP_IND,         /* Turning off WiFi AP indication */
+
+       MOBILE_AP_STATION_CONNECT_IND,          /* Station connection indication */
+       MOBILE_AP_STATION_DISCONNECT_IND,       /* Station disconnection indication */
+       MOBILE_AP_USB_STATION_CONNECT_IND,
+
+       MOBILE_AP_MAX_EVENT,
+} mobile_ap_event_e;
+
+typedef enum {
+       MOBILE_AP_TYPE_WIFI,
+       MOBILE_AP_TYPE_USB,
+       MOBILE_AP_TYPE_BT,
+       MOBILE_AP_TYPE_WIFI_AP,
+       MOBILE_AP_TYPE_MAX,
+} mobile_ap_type_e;
+
+typedef struct {
+       unsigned long long pdp_tx_bytes;        /**< packet data transmitted */
+       unsigned long long pdp_rx_bytes;        /**< packet data received */
+} mobile_ap_data_packet_usage_t;
+
+typedef struct {
+       mobile_ap_type_e interface;                     /**< interface type */
+       char ip[MOBILE_AP_STR_INFO_LEN];                /**< assigned IP address */
+       char mac[MOBILE_AP_STR_INFO_LEN];               /**< MAC Address */
+       char *hostname;
+       time_t tm;      /**< connection time*/
+} mobile_ap_station_info_t;
+
+typedef struct {
+       mobile_ap_type_e interface;                     /**< interface type */
+       char interface_name[MOBILE_AP_STR_INFO_LEN];            /**< interface alphanumeric name */
+       char ip_address[MOBILE_AP_STR_INFO_LEN];                /**< assigned ip addresss to interface */
+       char gateway_address[MOBILE_AP_STR_INFO_LEN];   /**< gateway address of interface */
+       char subnet_mask[MOBILE_AP_STR_INFO_LEN];       /**< subnet mask of interface */
+} mobile_ap_interface_info_t;
+
+typedef struct {
+       unsigned short number;                  /**< Number of connected device */
+       mobile_ap_station_info_t sta_info[MOBILE_AP_MAX_CONNECTED_STA];
+} mobile_ap_device_info_t;
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __MOBILEAP_INTERNAL_H__ */
diff --git a/include/mobileap_bluetooth.h b/include/mobileap_bluetooth.h
new file mode 100644 (file)
index 0000000..f37f8e2
--- /dev/null
@@ -0,0 +1,35 @@
+/*
+ * mobileap-agent
+ * Copyright (c) 2012 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the License);
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef __MOBILEAP_BLUETOOTH_H__
+#define __MOBILEAP_BLUETOOTH_H__
+
+#include "mobileap_softap.h"
+
+#define PS_RECHECK_INTERVAL 500
+#define PS_RECHECK_COUNT_MAX 5
+
+void _bt_get_remote_device_name(TetheringObject *obj, const char *mac, char **name);
+mobile_ap_error_code_e _disable_bt_tethering(TetheringObject *obj);
+gboolean _is_trying_bt_operation(void);
+
+gboolean tethering_enable_bt_tethering(TetheringObject *obj,
+               DBusGMethodInvocation *context);
+gboolean tethering_disable_bt_tethering(TetheringObject *obj,
+               DBusGMethodInvocation *context);
+
+#endif /* __MOBILEAP_BLUETOOTH_H__ */
diff --git a/include/mobileap_common.h b/include/mobileap_common.h
new file mode 100755 (executable)
index 0000000..c5c1678
--- /dev/null
@@ -0,0 +1,49 @@
+/*
+ * mobileap-agent
+ * Copyright (c) 2012 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the License);
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef __MOBILEAP_COMMON_H__
+#define __MOBILEAP_COMMON_H__
+
+#include <glib.h>
+
+#include "mobileap_softap.h"
+
+gint _slist_find_station_by_interface(gconstpointer a, gconstpointer b);
+gint _slist_find_station_by_mac(gconstpointer a, gconstpointer b);
+gint _slist_find_station_by_ip_addr(gconstpointer a, gconstpointer b);
+
+void _emit_mobileap_dbus_signal(TetheringObject *obj,
+               mobile_ap_sig_e num, const gchar *message);
+void _send_dbus_station_info(const char *member,
+               mobile_ap_station_info_t *info);
+void _update_station_count(int count);
+int _add_station_info(mobile_ap_station_info_t *info);
+int _remove_station_info(gconstpointer data, GCompareFunc func);
+int _remove_station_info_all(mobile_ap_type_e type);
+int _get_station_info(gconstpointer data, GCompareFunc func,
+               mobile_ap_station_info_t **si);
+int _get_station_count(gconstpointer data, GCompareFunc func, int *count);
+int _station_info_foreach(GFunc func, void *user_data);
+int _add_interface_routing(const char *interface, const in_addr_t gateway);
+int _del_interface_routing(const char *interface, const in_addr_t gateway);
+int _add_routing_rule(const char *interface);
+int _del_routing_rule(const char *interface);
+int _flush_ip_address(const char *interface);
+int _execute_command(const char *cmd);
+int _get_tethering_type_from_ip(const char *ip, mobile_ap_type_e *type);
+
+#endif /* __MOBILEAP_COMMON_H__ */
diff --git a/include/mobileap_handler.h b/include/mobileap_handler.h
new file mode 100644 (file)
index 0000000..8315858
--- /dev/null
@@ -0,0 +1,39 @@
+/*
+ * mobileap-agent
+ * Copyright (c) 2012 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the License);
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef __MOBILEAP_HANDLER_H__
+#define __MOBILEAP_HANDLER_H__
+
+#include <time.h>
+#include <alarm.h>
+
+typedef struct {
+       void *obj;
+       unsigned int state;
+} changed_state_t;
+
+void _register_vconf_cb(void *user_data);
+void _unregister_vconf_cb(void *user_data);
+
+gboolean _is_power_save_survival_mode(void);
+int _sp_timeout_handler(alarm_id_t alarm_id, void *user_param);
+void _init_timeout_cb(mobile_ap_type_e type, void *user_data);
+void _start_timeout_cb(mobile_ap_type_e type, time_t end_time);
+void _stop_timeout_cb(mobile_ap_type_e type);
+void _deinit_timeout_cb(mobile_ap_type_e type);
+
+#endif
diff --git a/include/mobileap_iptables.h b/include/mobileap_iptables.h
new file mode 100644 (file)
index 0000000..520764b
--- /dev/null
@@ -0,0 +1,55 @@
+/*
+ * mobileap-agent
+ * Copyright (c) 2012 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the License);
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef __MOBILEAP_IPTABLES_H__
+#define __MOBILEAP_IPTABLES_H__
+
+#define IPTABLES               "/usr/sbin/iptables"
+#define TABLE_FILTER           "filter"
+#define TABLE_NAT              "nat"
+#define TABLE_MANGLE           "mangle"
+#define CHAIN_FW               "FORWARD"
+#define CHAIN_POST             "POSTROUTING"
+#define CHAIN_PRE              "PREROUTING"
+#define TETH_FILTER_FW         "teth_filter_fw"
+#define TETH_NAT_POST          "teth_nat_post"
+#define TETH_NAT_PRE           "teth_nat_pre"
+#define STATE_RELATED_ESTAB    "RELATED,ESTABLISHED"
+#define STATE_INVALID          "INVALID"
+#define ACTION_DROP            "DROP"
+#define ACTION_RETURN          "RETURN"
+
+typedef enum {
+       PKT_REDIRECTION_RULE,
+       FORWARD_RULE_WITH_ACTION,
+       FORWARD_RULE_WITH_ACTION_AND_STATE,
+       DEFAULT_RULE,
+       PORT_FW_RULE,
+       MASQ_RULE,
+       CLAMP_MSS_RULE,
+} iptables_rule_e;
+
+int _iptables_create_chain(const char *table_name, const char *chain_name);
+int _iptables_flush_rules(const char *table_name, const char *chain_name);
+int _iptables_delete_chain(const char *table_name, const char *chain_name);
+int _iptables_add_rule(iptables_rule_e rule_type, const char *table,
+       const char *chain, ...);
+int _iptables_delete_rule(iptables_rule_e rule_type, const char *table,
+       const char *chain, ...);
+int _get_data_usage(const char *src, const char *dest, unsigned long long *tx,
+       unsigned long long *rx);
+#endif
diff --git a/include/mobileap_network.h b/include/mobileap_network.h
new file mode 100644 (file)
index 0000000..18f1dbf
--- /dev/null
@@ -0,0 +1,39 @@
+/*
+ * mobileap-agent
+ * Copyright (c) 2012 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the License);
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef __MOBILEAP_NETWORK_H__
+#define __MOBILEAP_NETWORK_H__
+
+#include <glib.h>
+
+#define TETHERING_NET_OPEN_RETRY_INTERVAL      2000    /* 2 secs */
+
+gboolean _get_network_interface_name(char **if_name);
+gboolean _get_network_gateway_address(char **ip);
+gboolean _is_trying_network_operation(void);
+gboolean _set_masquerade(void);
+gboolean _unset_masquerade(void);
+gboolean _add_default_router(void);
+gboolean _del_default_router(void);
+void _add_port_forward_rule(void);
+void _del_port_forward_rule(void);
+int _open_network(void);
+void _close_network(void);
+gboolean _init_network(void *user_data);
+gboolean _deinit_network(void);
+
+#endif /* __MOBILEAP_NETWORK_H__ */
diff --git a/include/mobileap_notification.h b/include/mobileap_notification.h
new file mode 100644 (file)
index 0000000..7117359
--- /dev/null
@@ -0,0 +1,57 @@
+/*
+ * mobileap-agent
+ * Copyright (c) 2012 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the License);
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef __MOBILEAP_NOTIFICATION_H__
+#define __MOBILEAP_NOTIFICATION_H__
+
+#include <appcore-common.h>
+#include <notification.h>
+
+#define MH_NOTI_STR_MAX                        50
+#define MH_NOTI_PATH_MAX               256
+
+#define MH_NOTI_ICON_PATH              "/usr/ug/res/images/ug-setting-mobileap-efl"
+#define MH_NOTI_ICON_BT                        MH_NOTI_ICON_PATH"/noti_tethering_bluetooth.png"
+#define MH_NOTI_ICON_GENERAL           MH_NOTI_ICON_PATH"/noti_tethering_general.png"
+#define MH_NOTI_ICON_USB               MH_NOTI_ICON_PATH"/noti_tethering_usb.png"
+#define MH_NOTI_ICON_WIFI              MH_NOTI_ICON_PATH"/noti_tethering_wifi_num.png"
+#define MH_NOTI_ICON_WIFI_PD           MH_NOTI_ICON_PATH"/noti_tethering_wifi_num_%02d.png"
+
+#define MOBILEAP_LOCALE_COMMON_PKG     "ug-setting-mobileap-efl"
+#define MOBILEAP_LOCALE_COMMON_RES     "/usr/ug/res/locale"
+
+#ifdef _
+#undef _
+#endif
+#define _(str)                         dgettext(MOBILEAP_LOCALE_COMMON_PKG, str)
+
+#define MH_STR_TETHERING                       "IDS_MOBILEAP_BODY_TETHERING"
+#define MH_STR_CONNECTED_DEV           "IDS_MOBILEAP_POP_CONNECTED_DEVICES_C_PD"
+#define MH_STR_CONNECTION_TIMEOUT              "IDS_ST_BODY_CONNECTION_TIMEOUT"
+#define MH_STR_CONFIGURE_TETHERING     "IDS_MOBILEAP_BODY_TAP_TO_CONFIGURE_TETHERING"
+#define MH_STR_TETHERING_ACTIVE        _("IDS_MOBILEAP_BODY_TETHERING_ACTIVE_ABB")
+#define MH_STR_BT_VISIBILITY   _("IDS_ST_BODY_BLUETOOTH_VISIBILITY_HAS_TIMED_OUT_YOUR_DEVICE_MIGHT_NOT_BE_FOUND")
+#define MH_STR_TETHERING_ENABLED       _("IDS_COM_BODY_TETHERING_ENABLED")
+
+int _create_timeout_noti(const char *icon_path);
+int _delete_timeout_noti(void);
+int _create_connected_noti(int count, const char *icon_path);
+int _update_connected_noti(int count, const char *icon_path);
+int _delete_connected_noti(void);
+void _create_tethering_active_noti(void);
+void _create_bt_tethering_active_noti(void);
+#endif
diff --git a/include/mobileap_softap.h b/include/mobileap_softap.h
new file mode 100755 (executable)
index 0000000..14ce713
--- /dev/null
@@ -0,0 +1,237 @@
+/*
+ * mobileap-agent
+ * Copyright (c) 2012 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the License);
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef __MOBILEAP_SOFTAP_H__
+#define __MOBILEAP_SOFTAP_H__
+
+#include <glib.h>
+#include <dbus/dbus.h>
+#include <dbus/dbus-glib.h>
+#include <dbus/dbus-glib-lowlevel.h>
+#include <dlog.h>
+#include <vconf.h>
+#include <netinet/in.h>
+
+#include "mobileap.h"
+
+#ifdef LOG_TAG
+#undef LOG_TAG
+#endif
+#define LOG_TAG                "MOBILEAP_AGENT"
+
+#define DBG(fmt, args...)      LOGD(fmt, ##args)
+#define ERR(fmt, args...)      LOGE(fmt, ##args)
+#define SDBG(fmt, args...)     SECURE_LOGD(fmt, ##args)
+#define SERR(fmt, args...)     SECURE_LOGE(fmt, ##args)
+
+#define DRIVER_DELAY           250000  /* micro seconds */
+
+#define IF_BUF_LEN             32
+#define NET_BUF_LEN            12
+#define INTERFACE_NAME_LEN     12
+#define SECURITY_TYPE_LEN      32
+
+/* Network Interface */
+#define IP_SUBNET_MASK         "255.255.255.0"
+
+#define WIFI_IF                        "wlan0"
+#define IP_ADDRESS_WIFI                0xC0A82B02      /* 192.168.43.2 */
+
+#define SOFTAP_IF              "wl0.1"
+#define IP_ADDRESS_SOFTAP      0xC0A82B01      /* 192.168.43.1 */
+
+#define USB_IF                 "usb0"
+#define IP_ADDRESS_USB         0xC0A88103      /* 192.168.129.3 */
+
+#define BT_IF_PREFIX           "bnep"
+#define BT_IF_ALL              BT_IF_PREFIX"+"
+#define IP_ADDRESS_BT_1                0xC0A88201      /* 192.168.130.1 */
+#define IP_ADDRESS_BT_2                0xC0A88301      /* 192.168.131.1 */
+#define IP_ADDRESS_BT_3                0xC0A88401      /* 192.168.132.1 */
+#define IP_ADDRESS_BT_4                0xC0A88501      /* 192.168.133.1 */
+#define IP_ADDRESS_BT_5                0xC0A88601      /* 192.168.134.1 */
+#define IP_ADDRESS_BT_6                0xC0A88701      /* 192.168.135.1 */
+#define IP_ADDRESS_BT_7                0xC0A88801      /* 192.168.136.1 */
+
+#define RET_FAILURE            (-1)
+#define RET_SUCCESS            (0)
+#define MAX_BUF_SIZE           (256u)
+
+#define DNSMASQ_CONF_LEN       1024
+#define DNSMASQ_CONF   \
+                       "dhcp-range=192.168.43.3,192.168.43.254,255.255.255.0\n" \
+                       "dhcp-range=192.168.130.2,192.168.130.150,255.255.255.0\n" \
+                       "dhcp-range=192.168.131.2,192.168.131.150,255.255.255.0\n" \
+                       "dhcp-range=192.168.132.2,192.168.132.150,255.255.255.0\n" \
+                       "dhcp-range=192.168.133.2,192.168.133.150,255.255.255.0\n" \
+                       "dhcp-range=192.168.134.2,192.168.134.150,255.255.255.0\n" \
+                       "dhcp-range=192.168.135.2,192.168.135.150,255.255.255.0\n" \
+                       "dhcp-range=192.168.136.2,192.168.136.150,255.255.255.0\n" \
+                       "dhcp-range=192.168.137.2,192.168.137.150,255.255.255.0\n" \
+                       "dhcp-range=set:blue,192.168.129.4,192.168.129.150,255.255.255.0\n"\
+                       "enable-dbus\n" \
+                       "group=app\n" \
+                       "user=app\n" \
+                       "dhcp-option=tag:blue,option:router,192.168.129.3\n"
+#define DNSMASQ_CONF_FILE      "/tmp/dnsmasq.conf"
+
+/* Start of hostapd configuration */
+#define MH_CTRL_INTF           "/tmp/mh_wpa_ctrl"
+#define MH_MONITOR_INTF                "/tmp/mh_wpa_monitor"
+
+#define HOSTAPD_BIN            "/usr/sbin/hostapd"
+#define HOSTAPD_ENTROPY_FILE   "/opt/var/lib/misc/hostapd.bin"
+#define HOSTAPD_CONF_FILE      "/opt/var/lib/misc/hostapd.conf"
+#define HOSTAPD_CTRL_INTF_DIR  "/opt/var/lib/misc/hostapd"
+#define HOSTAPD_CONF_LEN       1024
+#define HOSTAPD_CONF           "interface=%s\n" \
+                               "driver=nl80211\n" \
+                               "ctrl_interface=%s\n" \
+                               "ssid=%s\n" \
+                               "channel=%d\n" \
+                               "ignore_broadcast_ssid=%d\n" \
+                               "hw_mode=g\n" \
+                               "max_num_sta=%d\n" \
+                               "ieee80211n=1\n"
+#define HOSTAPD_DEBUG_FILE     "/var/log/hostapd.log"
+#define HOSTAPD_REQ_MAX_LEN    128
+#define HOSTAPD_RETRY_MAX      5
+#define HOSTAPD_RETRY_DELAY    500000  /* us */
+#define HOSTAPD_STA_DISCONN    "AP-STA-DISCONNECTED "  /* from wpa_ctrl.h */
+#define HOSTAPD_STA_CONN       "AP-STA-CONNECTED "
+#define HOSTAPD_STA_DISCONN_LEN 20
+#define HOSTAPD_STA_CONN_LEN   17
+#define HOSTAPD_MONITOR_ATTACH "ATTACH"
+#define HOSTAPD_MONITOR_DETACH "DETACH"
+#define HOSTAPD_DHCP_MAX_INTERVAL 30000 /* 30 seconds */
+
+/* Samsung VSIE value in beacon / probe response.
+ * Wi-Fi station can identify AP whether it is tethering or AP only using this value.
+ */
+#define HOSTAPD_VENDOR_ELEMENTS_TETH   "DD050016328000"        /* Samsung tethering device */
+#define HOSTAPD_VENDOR_ELEMENTS_WIFI_AP        "DD050016321000"        /* Specific application mode AP (e.g. GroupPlay) */
+/* End of hostapd configuration */
+
+#define IP_FORWARD     "/proc/sys/net/ipv4/ip_forward"
+#define IP_CMD         "/usr/sbin/ip"
+#define GREP           "/bin/grep"
+#define AWK            "/usr/bin/awk"
+#define DATA_USAGE_FILE        "/tmp/tethering_data_usage.txt"
+
+#define TETHERING_ROUTING_TABLE        252
+#define SRC_ROUTING_RULE       "iif %s lookup %d"
+#define DEFAULT_ROUTER         "default via %s dev %s scope global table %d"
+#define INTERFACE_ROUTING      "%s/24 table %d dev %s"
+#define DNS_ORDER              1
+#define TCP_DNS_FORWARD_RULE   "-i %s -p tcp --dport 53 -j DNAT --to %s:53"
+#define UDP_DNS_FORWARD_RULE   "-i %s -p udp --dport 53 -j DNAT --to %s:53"
+
+#define MOBILE_AP_STATE_NONE   0
+#define MOBILE_AP_STATE_WIFI   1
+#define MOBILE_AP_STATE_USB    2
+#define MOBILE_AP_STATE_BT     4
+#define MOBILE_AP_STATE_WIFI_AP        8
+#define MOBILE_AP_STATE_ALL    15
+
+#define DNSMASQ_DBUS_INTERFACE "uk.org.thekelleys.dnsmasq"
+
+#define PROC_NET_DEV                   "/proc/net/dev"
+#define TETHERING_CONN_TIMEOUT         (1200)  /* 20 Mins */
+#define WIFI_AP_CONN_TIMEOUT           (300)   /* 5 Mins */
+#define CHECK_NET_STATE_RETRY_COUNT    5
+#define PSK_ITERATION_COUNT            4096
+
+typedef struct {
+       int hide_mode;
+
+       char ssid[MOBILE_AP_WIFI_SSID_MAX_LEN + 1];
+       /* in AP case, hex key will be passed from library, so one extra byte is needed */
+       char key[MOBILE_AP_WIFI_KEY_MAX_LEN + 1];
+       char security_type[SECURITY_TYPE_LEN];
+} softap_settings_t;
+
+typedef struct {
+       /* The parent class object state. */
+       GObject parent;
+
+       int init_count;
+
+       softap_settings_t softap_settings;
+} TetheringObject;
+
+typedef struct {
+       /* The parent class state. */
+       GObjectClass parent;
+
+       /* class member */
+       guint signals[E_SIGNAL_MAX];
+} TetheringObjectClass;
+
+typedef struct {
+       unsigned int number;    /* Number of connected device */
+                               /* BSSID list of connected device */
+       char bssid[MOBILE_AP_MAX_WIFI_STA][MOBILE_AP_STR_INFO_LEN];
+} softap_device_info_t;
+
+typedef struct {
+       const char *key;
+       vconf_callback_fn cb;
+       int *value;
+} vconf_reg_t;
+
+typedef enum {
+       MOBILE_AP_DRV_INTERFACE_NONE,
+       MOBILE_AP_WEXT,
+       MOBILE_AP_NL80211,
+} mobile_ap_drv_interface_e;
+
+typedef struct {
+       guint tid;
+       char *mac_addr;
+} sta_timer_t;
+
+/* ssid : 32  key : 64 */
+int _mh_core_enable_softap(const mobile_ap_type_e type, const char *ssid,
+               const char *security, const char *key, int hide_mode);
+int _mh_core_disable_softap(void);
+int _mh_core_get_device_info(softap_device_info_t *di);
+int _mh_core_execute_dhcp_server(void);
+int _mh_core_terminate_dhcp_server(void);
+int _mh_core_enable_masquerade(const char *ext_if);
+int _mh_core_disable_masquerade(const char *ext_if);
+void _mh_core_add_data_to_array(GPtrArray *array, guint type, gchar *dev_name);
+int _mh_core_set_ip_address(const char *if_name, const in_addr_t ip);
+void _register_wifi_station_handler(void);
+void _unregister_wifi_station_handler(void);
+
+
+void _block_device_sleep(void);
+void _unblock_device_sleep(void);
+int _init_tethering(TetheringObject *obj);
+gboolean _deinit_tethering(TetheringObject *obj);
+gboolean _mobileap_clear_state(int state);
+gboolean _terminate_mobileap_agent(gpointer user_data);
+
+
+gboolean _mobileap_is_disabled(void);
+gboolean _mobileap_is_enabled(int state);
+gboolean _mobileap_is_enabled_by_type(mobile_ap_type_e type);
+gboolean _mobileap_set_state(int state);
+void _flush_dhcp_ack_timer(void);
+void _destroy_dhcp_ack_timer(char *mac_addr);
+
+#endif
diff --git a/include/mobileap_usb.h b/include/mobileap_usb.h
new file mode 100644 (file)
index 0000000..74f9a30
--- /dev/null
@@ -0,0 +1,30 @@
+/*
+ * mobileap-agent
+ * Copyright (c) 2012 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the License);
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef __MOBILEAP_USB_H__
+#define __MOBILEAP_USB_H__
+
+#include "mobileap_softap.h"
+
+mobile_ap_error_code_e _disable_usb_tethering(TetheringObject *obj);
+gboolean _is_trying_usb_operation(void);
+
+gboolean tethering_enable_usb_tethering(TetheringObject *obj,
+                                               DBusGMethodInvocation *context);
+gboolean tethering_disable_usb_tethering(TetheringObject *obj,
+                                               DBusGMethodInvocation *context);
+#endif /* __MOBILEAP_USB_H__ */
diff --git a/include/mobileap_wifi.h b/include/mobileap_wifi.h
new file mode 100644 (file)
index 0000000..61e3f71
--- /dev/null
@@ -0,0 +1,78 @@
+/*
+ * mobileap-agent
+ * Copyright (c) 2012 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the License);
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef __MOBILEAP_WIFI_H__
+#define __MOBILEAP_WIFI_H__
+
+#include "mobileap_softap.h"
+
+#define VCONFKEY_MOBILE_HOTSPOT_SSID           "memory/private/mobileap-agent/ssid"
+#define SOFTAP_SECURITY_TYPE_OPEN_STR          "open"
+#define SOFTAP_SECURITY_TYPE_WPA2_PSK_STR      "wpa2-psk"
+#define SOFTAP_PASSPHRASE_PATH                 "wifi_tethering.txt"
+#define SOFTAP_PASSPHRASE_GROUP_ID             "secure-storage::tethering"
+
+typedef enum {
+       SOFTAP_SECURITY_TYPE_OPEN,
+       SOFTAP_SECURITY_TYPE_WPA2_PSK,
+} softap_security_type_e;
+
+typedef struct {
+       int hide_mode;
+       char *ssid;
+       char *key;
+       softap_security_type_e security_type;
+} wifi_saved_settings;
+
+int _get_wifi_name_from_lease_info(const char *mac, char **name_buf);
+void _add_wifi_device_to_array(softap_device_info_t *di, GPtrArray *array);
+mobile_ap_error_code_e _enable_wifi_tethering(TetheringObject *obj, gchar *ssid,
+       gchar *passphrase, int hide_mode, softap_security_type_e security_type);
+mobile_ap_error_code_e _disable_wifi_tethering(TetheringObject *obj);
+gboolean _is_trying_wifi_operation(void);
+mobile_ap_error_code_e _reload_softap_settings(TetheringObject *obj,
+               gchar *ssid, gchar *key, gint hide_mode, gint security_type);
+mobile_ap_error_code_e _reload_softap_settings_for_ap(TetheringObject *obj,
+       gchar *ssid, gchar *key, gint hide_mode, gint security_type);
+
+/* Dbus method */
+mobile_ap_error_code_e _enable_wifi_ap(TetheringObject *obj, gchar *ssid,
+               gchar *passphrase, int hide_mode,
+                softap_security_type_e security_type);
+mobile_ap_error_code_e _disable_wifi_ap(TetheringObject *obj);
+gboolean tethering_enable_wifi_tethering(TetheringObject *obj, gchar *ssid,
+               gchar *key, gint hide_mode, gint security_type,
+               DBusGMethodInvocation *context);
+
+gboolean tethering_disable_wifi_tethering(TetheringObject *obj,
+               DBusGMethodInvocation *context);
+
+gboolean tethering_reload_wifi_settings(TetheringObject *obj, gchar *ssid,
+               gchar *key, gint visibility, gint security_type,
+               DBusGMethodInvocation *context);
+
+gboolean tethering_reload_wifi_ap_settings(TetheringObject *obj, gchar *ssid,
+                gchar *key, gint hide_mode,
+                gint security, DBusGMethodInvocation *context);
+
+gboolean tethering_enable_wifi_ap(TetheringObject *obj, gchar *ssid,
+               gchar *key, gint hide_mode, gint security_type,
+               DBusGMethodInvocation *context);
+
+gboolean tethering_disable_wifi_ap(TetheringObject *obj,
+               DBusGMethodInvocation *context);
+#endif /* __MOBILEAP_WIFI_H__ */
diff --git a/include/tethering-dbus-interface.xml b/include/tethering-dbus-interface.xml
new file mode 100644 (file)
index 0000000..ad6127b
--- /dev/null
@@ -0,0 +1,165 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+
+<!DOCTYPE node PUBLIC
+       "-//freedesktop//DTD D-Bus Object Introspection 1.0//EN"
+       "http://standards.freedesktop.org/dbus/1.0/introspect.dtd">
+
+<node>
+       <interface name="org.tizen.tethering">
+
+               <!-- Method definitions -->
+
+               <method name="disable">
+                       <annotation name="org.freedesktop.DBus.GLib.Async" value=""/>
+                       <arg type="u" name="type" direction="out"/>
+                       <arg type="u" name="result" direction="out"/>
+               </method>
+
+               <method name="enable_wifi_tethering">
+                       <annotation name="org.freedesktop.DBus.GLib.Async" value=""/>
+                       <arg type="s" name="ssid" direction="in"/>
+                       <arg type="s" name="key" direction="in"/>
+                       <arg type="i" name="visibility" direction="in"/>
+                       <arg type="i" name="security" direction="in"/>
+                       <arg type="u" name="type" direction="out"/>
+                       <arg type="u" name="result" direction="out"/>
+               </method>
+
+               <method name="disable_wifi_tethering">
+                       <annotation name="org.freedesktop.DBus.GLib.Async" value=""/>
+                       <arg type="u" name="type" direction="out"/>
+                       <arg type="u" name="result" direction="out"/>
+               </method>
+
+               <method name="enable_bt_tethering">
+                       <annotation name="org.freedesktop.DBus.GLib.Async" value=""/>
+                       <arg type="u" name="type" direction="out"/>
+                       <arg type="u" name="result" direction="out"/>
+               </method>
+
+               <method name="disable_bt_tethering">
+                       <annotation name="org.freedesktop.DBus.GLib.Async" value=""/>
+                       <arg type="u" name="type" direction="out"/>
+                       <arg type="u" name="result" direction="out"/>
+               </method>
+
+               <method name="enable_usb_tethering">
+                       <annotation name="org.freedesktop.DBus.GLib.Async" value=""/>
+                       <arg type="u" name="type" direction="out"/>
+                       <arg type="u" name="result" direction="out"/>
+               </method>
+
+               <method name="disable_usb_tethering">
+                       <annotation name="org.freedesktop.DBus.GLib.Async" value=""/>
+                       <arg type="u" name="type" direction="out"/>
+                       <arg type="u" name="result" direction="out"/>
+               </method>
+
+               <method name="enable_wifi_ap">
+                       <annotation name="org.freedesktop.DBus.GLib.Async" value=""/>
+                       <arg type="s" name="ssid" direction="in"/>
+                       <arg type="s" name="key" direction="in"/>
+                       <arg type="i" name="visibility" direction="in"/>
+                       <arg type="i" name="security" direction="in"/>
+                       <arg type="u" name="type" direction="out"/>
+                       <arg type="u" name="result" direction="out"/>
+               </method>
+
+               <method name="disable_wifi_ap">
+                       <annotation name="org.freedesktop.DBus.GLib.Async" value=""/>
+                       <arg type="u" name="type" direction="out"/>
+                       <arg type="u" name="result" direction="out"/>
+               </method>
+
+               <method name="reload_wifi_settings">
+                       <annotation name="org.freedesktop.DBus.GLib.Async" value=""/>
+                       <arg type="s" name="ssid" direction="in"/>
+                       <arg type="s" name="key" direction="in"/>
+                       <arg type="i" name="visibility" direction="in"/>
+                       <arg type="i" name="security" direction="in"/>
+                       <arg type="u" name="result" direction="out"/>
+               </method>
+
+               <method name="reload_wifi_ap_settings">
+                       <annotation name="org.freedesktop.DBus.GLib.Async" value=""/>
+                       <arg type="s" name="ssid" direction="in"/>
+                       <arg type="s" name="key" direction="in"/>
+                       <arg type="i" name="visibility" direction="in"/>
+                       <arg type="i" name="security" direction="in"/>
+                       <arg type="u" name="result" direction="out"/>
+               </method>
+
+               <method name="get_station_info">
+                       <annotation name="org.freedesktop.DBus.GLib.Async" value=""/>
+                       <arg type="u" name="type" direction="out"/>
+                       <arg type="a(usssu)" name="station" direction="out"/>
+               </method>
+
+               <method name="get_data_packet_usage">
+                       <annotation name="org.freedesktop.DBus.GLib.Async" value=""/>
+                       <arg type="u" name="type" direction="out"/>
+                       <arg type="t" name="rx_data" direction="out"/>
+                       <arg type="t" name="tx_data" direction="out"/>
+               </method>
+
+               <!-- Signal (D-Bus) definitions -->
+               <signal name="net_closed">
+                       <arg type="s" name="arg1" direction="out"/>
+               </signal>
+
+               <signal name="sta_connected">
+                       <arg type="s" name="arg1" direction="out"/>
+               </signal>
+
+               <signal name="sta_disconnected">
+                       <arg type="s" name="arg1" direction="out"/>
+               </signal>
+
+               <signal name="wifi_on">
+                       <arg type="s" name="arg1" direction="out"/>
+               </signal>
+
+               <signal name="wifi_off">
+                       <arg type="s" name="arg1" direction="out"/>
+               </signal>
+
+               <signal name="usb_on">
+                       <arg type="s" name="arg1" direction="out"/>
+               </signal>
+
+               <signal name="usb_off">
+                       <arg type="s" name="arg1" direction="out"/>
+               </signal>
+
+               <signal name="bluetooth_on">
+                       <arg type="s" name="arg1" direction="out"/>
+               </signal>
+
+               <signal name="bluetooth_off">
+                       <arg type="s" name="arg1" direction="out"/>
+               </signal>
+
+               <signal name="wifi_ap_on">
+                       <arg type="s" name="arg1" direction="out"/>
+               </signal>
+
+               <signal name="wifi_ap_off">
+                       <arg type="s" name="arg1" direction="out"/>
+               </signal>
+
+               <signal name="no_data_timeout">
+                       <arg type="s" name="arg1" direction="out"/>
+               </signal>
+
+               <signal name="low_batt_mode">
+                       <arg type="s" name="arg1" direction="out"/>
+               </signal>
+
+               <signal name="flight_mode">
+                       <arg type="s" name="arg1" direction="out"/>
+               </signal>
+               <signal name="power_save_mode">
+                       <arg type="s" name="arg1" direction="out"/>
+               </signal>
+       </interface>
+</node>
\ No newline at end of file
diff --git a/mobileap-agent.manifest b/mobileap-agent.manifest
new file mode 100644 (file)
index 0000000..9e1197e
--- /dev/null
@@ -0,0 +1,38 @@
+<manifest>
+       <define>
+               <domain name="mobileap-agent"/>
+               <request>
+                       <smack request="bt-service" type="rw"/>
+                       <smack request="bt-service::platform" type="w"/>
+                       <smack request="bt-service::public" type="w"/>
+                       <smack request="secure-storage::tethering" type="rw"/>
+                       <smack request="mdm-server::admin" type="r"/>
+                       <smack request="mdm-server::security" type="r"/>
+                       <smack request="mdm-server::restriction" type="r"/>
+                       <smack request="deviced::display" type="rw"/>
+                       <smack request="connman" type="rw"/>
+                       <smack request="connman::get" type="rw"/>
+                       <smack request="net-config" type="rw"/>
+                       <smack request="alarm-server::alarm" type="w"/>
+                       <smack request="system::use_internet" type="w"/>
+                       <smack request="telephony_framework::api_ps_public" type="rw"/>
+               </request>
+               <permit>
+                       <smack permit="system::use_internet" type="w"/>
+               </permit>
+       </define>
+       <request>
+               <domain name="mobileap-agent"/>
+       </request>
+       <assign>
+               <filesystem path="/usr/share/dbus-1/services/org.tizen.tethering.service" label="_"/>
+               <dbus name="org.tizen.tethering" own="mobileap-agent" bus="system">
+                       <node name="/Tethering">
+                               <interface name="org.tizen.tethering">
+                                       <annotation name="org.tizen.smack" value="tethering::dbus-access"/>
+                               </interface>
+                       </node>
+               </dbus>
+               <filesystem path="/opt/etc/dump.d/module.d/tethering_dump.sh" label="_" exec_label="none" />
+       </assign>
+</manifest>
diff --git a/packaging/mobileap-agent.spec b/packaging/mobileap-agent.spec
new file mode 100644 (file)
index 0000000..077d62b
--- /dev/null
@@ -0,0 +1,69 @@
+Name:          mobileap-agent
+Summary:       Mobile AP daemon for setting tethering environments
+Version:       1.0.17
+Release:       1
+Group:         System/Network
+License:       Apache-2.0
+Source0:       %{name}-%{version}.tar.gz
+BuildRequires: pkgconfig(dlog)
+BuildRequires: pkgconfig(dbus-glib-1)
+BuildRequires: pkgconfig(glib-2.0)
+BuildRequires: pkgconfig(gthread-2.0)
+BuildRequires: pkgconfig(deviced)
+BuildRequires: pkgconfig(vconf)
+BuildRequires: pkgconfig(notification)
+BuildRequires: pkgconfig(capi-network-connection)
+BuildRequires: pkgconfig(capi-network-bluetooth)
+BuildRequires: pkgconfig(syspopup-caller)
+BuildRequires: pkgconfig(bundle)
+BuildRequires: pkgconfig(appcore-common)
+BuildRequires: pkgconfig(capi-network-wifi-direct)
+BuildRequires: pkgconfig(capi-network-wifi)
+BuildRequires: pkgconfig(alarm-service)
+BuildRequires: pkgconfig(appsvc)
+BuildRequires: pkgconfig(libssl)
+BuildRequires: cmake
+Requires(post):        /usr/bin/vconftool
+Requires(post):        bluetooth-agent
+Requires(post):        ss-server
+Requires:      iproute2
+Requires:      iptables
+Requires:      dnsmasq
+
+%description
+Mobile AP daemon for setting tethering environments
+
+%prep
+%setup -q
+
+
+%build
+export CFLAGS="$CFLAGS -DTIZEN_DEBUG_ENABLE"
+export CXXFLAGS="$CXXFLAGS -DTIZEN_DEBUG_ENABLE"
+export FFLAGS="$FFLAGS -DTIZEN_DEBUG_ENABLE"
+
+%cmake -DCMAKE_BUILD_TYPE="" \
+       .
+
+make %{?jobs:-j%jobs}
+
+
+%install
+%make_install
+
+%post
+/usr/bin/vconftool set -t string memory/private/mobileap-agent/ssid "" -u 0 -i -f -s system::vconf_network
+/usr/bin/vconftool set -t int memory/mobile_hotspot/connected_device "0" -u 0 -i -f -s system::vconf_network
+/usr/bin/vconftool set -t int memory/mobile_hotspot/mode "0" -u 0 -i -f -s system::vconf_network
+/usr/bin/vconftool set -t int db/mobile_hotspot/security "1" -u 5000 -f -s system::vconf_network
+/usr/bin/vconftool set -t int db/mobile_hotspot/hide "0" -u 5000 -f -s system::vconf_network
+
+/bin/chmod +x /opt/etc/dump.d/module.d/tethering_dump.sh
+
+%files
+%manifest mobileap-agent.manifest
+%defattr(-,root,root,-)
+/usr/share/dbus-1/services/org.tizen.tethering.service
+%{_bindir}/mobileap-agent
+/opt/etc/dump.d/module.d/tethering_dump.sh
+
diff --git a/packaging/org.tizen.tethering.service b/packaging/org.tizen.tethering.service
new file mode 100644 (file)
index 0000000..577633b
--- /dev/null
@@ -0,0 +1,4 @@
+[D-BUS Service]
+Name=org.tizen.tethering
+Exec=/usr/bin/mobileap-agent
+User=root
diff --git a/src/mobileap_bluetooth.c b/src/mobileap_bluetooth.c
new file mode 100755 (executable)
index 0000000..43cd1c2
--- /dev/null
@@ -0,0 +1,618 @@
+/*
+ * mobileap-agent
+ * Copyright (c) 2012 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the License);
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <stdio.h>
+#include <glib.h>
+#include <dbus/dbus.h>
+#include <dbus/dbus-glib.h>
+#include <dbus/dbus-glib-lowlevel.h>
+#include <bluetooth.h>
+
+#include "mobileap_softap.h"
+#include "mobileap_common.h"
+#include "mobileap_bluetooth.h"
+#include "mobileap_handler.h"
+#include "mobileap_notification.h"
+
+typedef struct {
+       bt_device_info_s *info;
+       char *intf_name;
+       const in_addr_t intf_ip;
+} __bt_remote_device_s;
+
+static __bt_remote_device_s __bt_remote_devices[MOBILE_AP_MAX_BT_STA] = {
+       {NULL, NULL, IP_ADDRESS_BT_1},
+       {NULL, NULL, IP_ADDRESS_BT_2},
+       {NULL, NULL, IP_ADDRESS_BT_3},
+       {NULL, NULL, IP_ADDRESS_BT_4}};
+
+static DBusGMethodInvocation *g_context = NULL;
+
+static void __bt_nap_connection_changed(bool connected, const char *remote_address,
+                               const char *interface_name, void *user_data);
+static void __bt_adapter_state_changed(int result, bt_adapter_state_e adapter_state, void *user_data);
+static void __handle_bt_adapter_visibility();
+
+int __recheck_bt_adapter_timer = 0;
+
+static __bt_remote_device_s *__find_bt_remote(const char *mac)
+{
+       int i;
+
+       for (i = 0; i < MOBILE_AP_MAX_BT_STA; i++) {
+               if (__bt_remote_devices[i].info == NULL)
+                       continue;
+
+               if (!g_ascii_strcasecmp(__bt_remote_devices[i].info->remote_address, mac))
+                       break;
+       }
+
+       if (i == MOBILE_AP_MAX_BT_STA) {
+               SERR("Not found : %s\n", mac);
+               return NULL;
+       }
+
+       return &__bt_remote_devices[i];
+}
+
+static __bt_remote_device_s *__add_bt_remote(bt_device_info_s *info, const char *intf_name)
+{
+       int i;
+
+       for (i = 0; i < MOBILE_AP_MAX_BT_STA; i++) {
+               if (__bt_remote_devices[i].info == NULL)
+                       break;
+       }
+
+       if (i == MOBILE_AP_MAX_BT_STA) {
+               ERR("Too many BT devices are connected\n");
+               return NULL;
+       }
+
+       __bt_remote_devices[i].intf_name = g_strdup(intf_name);
+       if (__bt_remote_devices[i].intf_name == NULL) {
+               ERR("Memory allocation failed\n");
+               return NULL;
+       }
+
+       _add_interface_routing(__bt_remote_devices[i].intf_name,
+                       __bt_remote_devices[i].intf_ip);
+       _add_routing_rule(__bt_remote_devices[i].intf_name);
+       __bt_remote_devices[i].info = info;
+
+       return &__bt_remote_devices[i];
+}
+
+static gboolean __del_bt_remote(const char *mac)
+{
+       int i;
+
+       for (i = 0; i < MOBILE_AP_MAX_BT_STA; i++) {
+               if (__bt_remote_devices[i].info == NULL)
+                       continue;
+
+               if (!g_ascii_strcasecmp(__bt_remote_devices[i].info->remote_address, mac))
+                       break;
+       }
+
+       if (i == MOBILE_AP_MAX_BT_STA) {
+               SERR("Not found : %s\n", mac);
+               return FALSE;
+       }
+
+       _del_routing_rule(__bt_remote_devices[i].intf_name);
+       _del_interface_routing(__bt_remote_devices[i].intf_name,
+                       __bt_remote_devices[i].intf_ip);
+        bt_adapter_free_device_info(__bt_remote_devices[i].info);
+        g_free(__bt_remote_devices[i].intf_name);
+
+        __bt_remote_devices[i].info = NULL;
+        __bt_remote_devices[i].intf_name = NULL;
+
+       return TRUE;
+}
+
+static void __del_bt_remote_all(void)
+{
+       int i;
+
+       for (i = 0; i < MOBILE_AP_MAX_BT_STA; i++) {
+               if (__bt_remote_devices[i].info) {
+                       bt_adapter_free_device_info(__bt_remote_devices[i].info);
+                       __bt_remote_devices[i].info = NULL;
+               }
+
+               if (__bt_remote_devices[i].intf_name) {
+                       _del_routing_rule(__bt_remote_devices[i].intf_name);
+                       _del_interface_routing(__bt_remote_devices[i].intf_name,
+                               __bt_remote_devices[i].intf_ip);
+                       g_free(__bt_remote_devices[i].intf_name);
+                       __bt_remote_devices[i].intf_name = NULL;
+               }
+       }
+
+       return;
+}
+
+static mobile_ap_error_code_e __init_bt(TetheringObject *obj)
+{
+       int ret;
+
+       ret = bt_initialize();
+       if (ret != BT_ERROR_NONE) {
+               ERR("bt_initialize is failed : %d\n", ret);
+               return MOBILE_AP_ERROR_RESOURCE;
+       }
+
+       ret = bt_adapter_set_state_changed_cb(__bt_adapter_state_changed, (void *)obj);
+       if (ret != BT_ERROR_NONE) {
+               ERR("bt_adapter_set_state_changed_cb is failed : %d\n", ret);
+               bt_deinitialize();
+               return MOBILE_AP_ERROR_RESOURCE;
+       }
+
+       return MOBILE_AP_ERROR_NONE;
+}
+
+static void __deinit_bt(void)
+{
+       int ret;
+
+       ret = bt_adapter_unset_state_changed_cb();
+       if (ret != BT_ERROR_NONE)
+               ERR("bt_adapter_unset_state_changed_cb is failed : %d\n", ret);
+
+       ret = bt_deinitialize();
+       if (ret != BT_ERROR_NONE)
+               ERR("bt_deinitialize is failed : %d\n", ret);
+
+       return;
+}
+
+static gboolean __is_bt_adapter_on(void)
+{
+       int ret;
+       bt_adapter_state_e adapter_state = BT_ADAPTER_DISABLED;
+
+       ret = bt_adapter_get_state(&adapter_state);
+       if (ret != BT_ERROR_NONE) {
+               ERR("bt_adapter_get_state is failed : %d\n", ret);
+               return FALSE;
+       }
+
+       if (adapter_state == BT_ADAPTER_ENABLED)
+               return TRUE;
+       else
+               return FALSE;
+}
+
+gboolean __bt_adapter_timeout_cb(gpointer data)
+{
+       DBG("+\n");
+
+       static int retry_count = 0;
+       if (__is_bt_adapter_on() == TRUE) {
+               DBG("BT Adapter is enabled by other process \n");
+               retry_count = 0;
+               DBG("-\n");
+               return FALSE;
+       } else {
+               if (++retry_count >= PS_RECHECK_COUNT_MAX) {
+                       retry_count = 0;
+                       ERR("_enable_bt_tethering() is failed because of bt_adapter_eanbled() failed:n");
+                       _mobileap_clear_state(MOBILE_AP_STATE_BT);
+                       __deinit_bt();
+                       dbus_g_method_return(g_context,
+                                       MOBILE_AP_ENABLE_BT_TETHERING_CFM, MOBILE_AP_ERROR_INTERNAL);
+                       g_context = NULL;
+                       _unblock_device_sleep();
+                       DBG("-\n");
+                       return FALSE;
+               } else {
+                       DBG("-\n");
+                       return TRUE;
+               }
+       }
+}
+
+static mobile_ap_error_code_e __turn_on_bt_adapter(TetheringObject *obj)
+{
+       int ret;
+
+       ret = bt_adapter_enable();
+       if (ret == BT_ERROR_NOW_IN_PROGRESS) {
+               if (__recheck_bt_adapter_timer) {
+                       g_source_remove(__recheck_bt_adapter_timer);
+               }
+               __recheck_bt_adapter_timer = g_timeout_add(PS_RECHECK_INTERVAL,
+                               __bt_adapter_timeout_cb, NULL);
+               return MOBILE_AP_ERROR_NONE;
+       }
+
+       if (ret != BT_ERROR_NONE && ret != BT_ERROR_ALREADY_DONE) {
+               ERR("bt_adapter_enable is failed : %d\n", ret);
+               if (ret == BT_ERROR_PERMISSION_DENIED)
+                       return MOBILE_AP_ERROR_PERMISSION_DENIED;
+               else
+                       return MOBILE_AP_ERROR_RESOURCE;
+       }
+
+       return MOBILE_AP_ERROR_NONE;
+}
+
+static mobile_ap_error_code_e __turn_on_bt_nap(TetheringObject *obj)
+{
+       int bt_ret = BT_ERROR_NONE;
+
+       bt_ret = bt_nap_set_connection_state_changed_cb(__bt_nap_connection_changed, (void *)obj);
+       if (bt_ret != BT_ERROR_NONE) {
+               ERR("bt_nap_set_connection_state_changed_cb is failed : %d\n", bt_ret);
+               return MOBILE_AP_ERROR_RESOURCE;
+       }
+
+       bt_ret = bt_nap_activate();
+       if (bt_ret != BT_ERROR_NONE && bt_ret != BT_ERROR_ALREADY_DONE) {
+               bt_nap_unset_connection_state_changed_cb();
+               ERR("bt_nap_activate is failed : %d\n", bt_ret);
+               if (bt_ret == BT_ERROR_PERMISSION_DENIED)
+                       return MOBILE_AP_ERROR_PERMISSION_DENIED;
+               else
+                       return MOBILE_AP_ERROR_RESOURCE;
+       }
+
+       return MOBILE_AP_ERROR_NONE;
+}
+
+static void __turn_off_bt_nap(void)
+{
+       int bt_ret;
+
+       bt_ret = bt_nap_disconnect_all();
+       if (bt_ret != BT_ERROR_NONE)
+               ERR("bt_nap_disconnect_all is failed : %d\n", bt_ret);
+       else
+               DBG("bt_nap_disconnect_all is called\n");
+
+       bt_ret = bt_nap_unset_connection_state_changed_cb();
+       if (bt_ret != BT_ERROR_NONE)
+               ERR("bt_nap_unset_connection_state_changed_cb is failed : %d\n", bt_ret);
+
+       return;
+}
+
+mobile_ap_error_code_e _enable_bt_tethering(TetheringObject *obj)
+{
+       mobile_ap_error_code_e ret = MOBILE_AP_ERROR_NONE;
+
+       DBG("+\n");
+       if (__recheck_bt_adapter_timer) {
+               g_source_remove(__recheck_bt_adapter_timer);
+               __recheck_bt_adapter_timer = 0;
+       }
+
+       if (_mobileap_is_enabled(MOBILE_AP_STATE_WIFI_AP)) {
+               ERR("Wi-Fi AP is enabled\n");
+               return MOBILE_AP_ERROR_RESOURCE;
+       }
+
+       ret = _init_tethering(obj);
+       if (ret != MOBILE_AP_ERROR_NONE) {
+               return ret;
+       }
+
+       ret = __turn_on_bt_nap(obj);
+       if (ret != MOBILE_AP_ERROR_NONE) {
+               _deinit_tethering(obj);
+               return ret;
+       }
+       _delete_timeout_noti();
+       _init_timeout_cb(MOBILE_AP_TYPE_BT, (void *)obj);
+       _start_timeout_cb(MOBILE_AP_TYPE_BT, time(NULL) + TETHERING_CONN_TIMEOUT);
+
+       return ret;
+}
+
+mobile_ap_error_code_e _disable_bt_tethering(TetheringObject *obj)
+{
+       int ret = BT_ERROR_NONE;
+       if (!_mobileap_is_enabled(MOBILE_AP_STATE_BT)) {
+               ERR("BT tethering has not been enabled\n");
+               return MOBILE_AP_ERROR_NOT_ENABLED;
+       }
+       ret = bt_adapter_unset_visibility_mode_changed_cb();
+       if (ret != BT_ERROR_NONE)
+               ERR("bt_adapter_unset_visibility_mode_changed_cb is failed : %d\n", ret);
+
+       _block_device_sleep();
+       if (__is_bt_adapter_on()) {
+               __turn_off_bt_nap();
+               __deinit_bt();
+       }
+
+       _remove_station_info_all(MOBILE_AP_TYPE_BT);
+       __del_bt_remote_all();
+       _deinit_timeout_cb(MOBILE_AP_TYPE_BT);
+
+       _deinit_tethering(obj);
+       _mobileap_clear_state(MOBILE_AP_STATE_BT);
+       _unblock_device_sleep();
+
+       return MOBILE_AP_ERROR_NONE;
+}
+
+
+static void __bt_nap_connection_changed(bool connected, const char *remote_address, const char *interface_name, void *user_data)
+{
+       if (remote_address == NULL || interface_name == NULL || user_data == NULL) {
+               ERR("Invalid param\n");
+               return;
+       }
+
+       __bt_remote_device_s *remote;
+       bt_device_info_s *info;
+       int ret;
+       int n_station = 0;
+
+       SDBG("Remote address : %s, Interface : %s, %s\n",
+                       remote_address, interface_name,
+                       connected ? "Connected" : "Disconnected");
+
+       if (connected) {
+               ret = bt_adapter_get_bonded_device_info(remote_address, &info);
+               if (ret != BT_ERROR_NONE) {
+                       ERR("bt_adapter_get_bonded_device_info is failed : %d\n", ret);
+                       return;
+               }
+
+               remote = __add_bt_remote(info, interface_name);
+               if (remote == NULL) {
+                       ERR("__add_bt_remote is failed\n");
+                        bt_adapter_free_device_info(info);
+                       return;
+               }
+
+               ret = _mh_core_set_ip_address(interface_name, remote->intf_ip);
+               if (ret != MOBILE_AP_ERROR_NONE) {
+                       ERR("Setting ip address error : %d\n", ret);
+               }
+       } else {
+               _remove_station_info(remote_address, _slist_find_station_by_mac);
+               if (__del_bt_remote(remote_address) == FALSE) {
+                       ERR("__del_bt_remote is failed\n");
+               }
+
+               _get_station_count((gconstpointer)MOBILE_AP_TYPE_BT,
+                               _slist_find_station_by_interface, &n_station);
+               if (n_station == 0)
+                       _start_timeout_cb(MOBILE_AP_TYPE_BT, time(NULL) + TETHERING_CONN_TIMEOUT);
+       }
+
+       return;
+}
+
+static void __bt_adapter_state_changed(int result, bt_adapter_state_e adapter_state, void *user_data)
+{
+       if (user_data == NULL) {
+               ERR("Invalid param\n");
+               return;
+       }
+
+       DBG("+\n");
+
+       if (!_mobileap_is_enabled(MOBILE_AP_STATE_BT))
+               return;
+
+       int ret = MOBILE_AP_ERROR_RESOURCE;
+       TetheringObject *obj = (TetheringObject *)user_data;
+
+       if (result != BT_ERROR_NONE) {
+               ERR("BT Adapter operation is failed : %d\n", result);
+               goto FAIL;
+       }
+
+       SDBG("BT Adapter is %s\n", adapter_state == BT_ADAPTER_ENABLED ?
+                       "enabled" : "disabled");
+       if (adapter_state == BT_ADAPTER_DISABLED) {
+               _disable_bt_tethering(obj);
+               _emit_mobileap_dbus_signal(obj, E_SIGNAL_BT_TETHER_OFF,
+                               SIGNAL_MSG_NOT_AVAIL_INTERFACE);
+               return;
+       } else {
+               ret = _enable_bt_tethering(obj);
+               if (ret != MOBILE_AP_ERROR_NONE) {
+                       ERR("_enable_bt_tethering() is failed : %d\n", ret);
+                       __deinit_bt();
+                       goto FAIL;
+               }
+
+               _emit_mobileap_dbus_signal(obj, E_SIGNAL_BT_TETHER_ON, NULL);
+               dbus_g_method_return(g_context,
+                               MOBILE_AP_ENABLE_BT_TETHERING_CFM, ret);
+               __handle_bt_adapter_visibility();
+               g_context = NULL;
+               _unblock_device_sleep();
+
+               return;
+       }
+
+FAIL:
+       dbus_g_method_return(g_context,
+                       MOBILE_AP_ENABLE_BT_TETHERING_CFM, ret);
+       g_context = NULL;
+       _mobileap_clear_state(MOBILE_AP_STATE_BT);
+       _unblock_device_sleep();
+
+       return;
+}
+
+void _bt_get_remote_device_name(TetheringObject *obj, const char *mac, char **name)
+{
+       if (obj == NULL || mac == NULL || name == NULL) {
+               ERR("Invalid param\n");
+               return;
+       }
+
+       __bt_remote_device_s *remote = NULL;
+
+       remote = __find_bt_remote(mac);
+       if (remote == NULL)
+               return;
+
+       *name = g_strdup(remote->info->remote_name);
+       if (*name == NULL) {
+               ERR("Memory allocation failed\n");
+               return;
+       }
+
+       return;
+}
+
+static void __bt_adapter_visibility_changed_cb(int result,
+                               bt_adapter_visibility_mode_e visibility_mode, void *user_data)
+{
+       DBG("+\n");
+
+       int ret;
+       int duration;
+       bt_adapter_visibility_mode_e mode = BT_ADAPTER_VISIBILITY_MODE_NON_DISCOVERABLE;
+
+       ret = bt_adapter_get_visibility(&mode, &duration);
+       if (ret != BT_ERROR_NONE) {
+               ERR("bt_adapter_get_visibility is failed 0x[%X]\n", ret);
+       }
+
+       if (mode == BT_ADAPTER_VISIBILITY_MODE_NON_DISCOVERABLE) {
+                       ERR("_launch_toast_popup() is failed\n");
+       }
+
+       DBG("-\n");
+}
+
+static void __handle_bt_adapter_visibility()
+{
+       DBG("+\n");
+
+       int ret;
+       int duration;
+       bt_adapter_visibility_mode_e mode = BT_ADAPTER_VISIBILITY_MODE_NON_DISCOVERABLE;
+
+       ret = bt_adapter_get_visibility(&mode, &duration);
+       if (ret != BT_ERROR_NONE) {
+               ERR("bt_adapter_get_visibility is failed 0x[%X]\n", ret);
+       }
+
+       if (mode == BT_ADAPTER_VISIBILITY_MODE_NON_DISCOVERABLE) {
+               ret = bt_adapter_set_visibility(BT_ADAPTER_VISIBILITY_MODE_LIMITED_DISCOVERABLE, 120);
+               if (ret != BT_ERROR_NONE) {
+                       ERR("bt_adapter_set_visibility is failed 0x[%X]\n", ret);
+               }
+       }
+       bt_adapter_set_visibility_mode_changed_cb(__bt_adapter_visibility_changed_cb, NULL);
+       DBG("-\n");
+}
+gboolean tethering_enable_bt_tethering(TetheringObject *obj,
+               DBusGMethodInvocation *context)
+{
+       mobile_ap_error_code_e ret;
+       gboolean ret_val = FALSE;
+
+       DBG("+\n");
+
+       g_assert(obj != NULL);
+       g_assert(context != NULL);
+
+       if (g_context) {
+               DBG("It is turnning on\n");
+               dbus_g_method_return(context,
+                               MOBILE_AP_ENABLE_BT_TETHERING_CFM,
+                               MOBILE_AP_ERROR_IN_PROGRESS);
+               return FALSE;
+       }
+
+       g_context = context;
+
+       _block_device_sleep();
+
+       ret = __init_bt(obj);
+       if (ret != MOBILE_AP_ERROR_NONE)
+               goto DONE;
+
+       if (!_mobileap_set_state(MOBILE_AP_STATE_BT)) {
+               ret = MOBILE_AP_ERROR_RESOURCE;
+               __deinit_bt();
+               goto DONE;
+       }
+
+       if (__is_bt_adapter_on() == FALSE) {
+               DBG("Bluetooth is deactivated\n");
+               if (__turn_on_bt_adapter(obj) != MOBILE_AP_ERROR_NONE) {
+                       ERR("__turn_on_bt_adapter is failed\n");
+                       ret = MOBILE_AP_ERROR_INTERNAL;
+                       _mobileap_clear_state(MOBILE_AP_STATE_BT);
+                       __deinit_bt();
+                       goto DONE;
+               }
+
+               return TRUE;
+       }
+
+       ret = _enable_bt_tethering(obj);
+       if (ret != MOBILE_AP_ERROR_NONE) {
+               ERR("_enable_bt_tethering() is failed : %d\n", ret);
+               _mobileap_clear_state(MOBILE_AP_STATE_BT);
+               __deinit_bt();
+       } else {
+               _emit_mobileap_dbus_signal(obj, E_SIGNAL_BT_TETHER_ON, NULL);
+               __handle_bt_adapter_visibility();
+               ret_val = TRUE;
+       }
+
+DONE:
+       dbus_g_method_return(g_context,
+                       MOBILE_AP_ENABLE_BT_TETHERING_CFM, ret);
+       g_context = NULL;
+
+       _unblock_device_sleep();
+
+       return ret_val;
+}
+
+gboolean tethering_disable_bt_tethering(TetheringObject *obj,
+               DBusGMethodInvocation *context)
+{
+       mobile_ap_error_code_e ret;
+
+       DBG("+\n");
+
+       g_assert(obj != NULL);
+       g_assert(context != NULL);
+
+       ret = _disable_bt_tethering(obj);
+       if (ret != MOBILE_AP_ERROR_NONE) {
+               dbus_g_method_return(context, MOBILE_AP_DISABLE_BT_TETHERING_CFM, ret);
+               return FALSE;
+       }
+
+       _emit_mobileap_dbus_signal(obj, E_SIGNAL_BT_TETHER_OFF, NULL);
+       dbus_g_method_return(context, MOBILE_AP_DISABLE_BT_TETHERING_CFM, ret);
+       return TRUE;
+}
+
+gboolean _is_trying_bt_operation(void)
+{
+       return (g_context ? TRUE : FALSE);
+}
diff --git a/src/mobileap_common.c b/src/mobileap_common.c
new file mode 100755 (executable)
index 0000000..39a1989
--- /dev/null
@@ -0,0 +1,573 @@
+/*
+ * mobileap-agent
+ * Copyright (c) 2012 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the License);
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <stdio.h>
+#include <glib.h>
+#include <string.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+
+#include <dbus/dbus.h>
+#include <dbus/dbus-glib.h>
+#include <dbus/dbus-glib-lowlevel.h>
+
+#include "mobileap_notification.h"
+#include "mobileap_common.h"
+
+#define TETHERING_OBJECT_GET_CLASS(obj) \
+       (G_TYPE_INSTANCE_GET_CLASS ((obj), \
+       TETHERING_TYPE_OBJECT , TetheringObjectClass))
+
+extern DBusConnection *tethering_conn;
+
+static GSList *station_list = NULL;
+
+gint _slist_find_station_by_interface(gconstpointer a, gconstpointer b)
+{
+       mobile_ap_station_info_t *si = (mobile_ap_station_info_t *)a;
+       mobile_ap_type_e interface = (mobile_ap_type_e)b;
+
+       return si->interface - interface;
+}
+
+gint _slist_find_station_by_mac(gconstpointer a, gconstpointer b)
+{
+       mobile_ap_station_info_t *si = (mobile_ap_station_info_t *)a;
+       const char *mac = (const char *)b;
+
+       return g_ascii_strcasecmp(si->mac, mac);
+}
+
+gint _slist_find_station_by_ip_addr(gconstpointer a, gconstpointer b)
+{
+       mobile_ap_station_info_t *si = (mobile_ap_station_info_t *)a;
+       const char *ip_addr = (const char *)b;
+
+       return g_ascii_strcasecmp(si->ip, ip_addr);
+}
+
+void _emit_mobileap_dbus_signal(TetheringObject *obj,
+                               mobile_ap_sig_e num, const gchar *message)
+{
+       TetheringObjectClass *klass = TETHERING_OBJECT_GET_CLASS(obj);
+
+       SDBG("Emitting signal id [%d], with message [%s]\n", num, message);
+
+       if (num == E_SIGNAL_WIFI_TETHER_ON ||
+                       num == E_SIGNAL_USB_TETHER_ON || num == E_SIGNAL_BT_TETHER_ON) {
+               _create_tethering_active_noti();
+       }
+
+       g_signal_emit(obj, klass->signals[num], 0, message);
+}
+
+void _send_dbus_station_info(const char *member, mobile_ap_station_info_t *info)
+{
+       if (tethering_conn == NULL)
+               return;
+
+       if (member == NULL || info == NULL) {
+               ERR("Invalid param\n");
+               return;
+       }
+
+       DBusMessage *msg = NULL;
+       char *ip = info->ip;
+       char *mac = info->mac;
+       char *hostname = info->hostname;
+
+       msg = dbus_message_new_signal(TETHERING_SERVICE_OBJECT_PATH,
+                       TETHERING_SERVICE_INTERFACE,
+                       SIGNAL_NAME_DHCP_STATUS);
+       if (!msg) {
+               ERR("Unable to allocate D-Bus signal\n");
+               return;
+       }
+
+       if (!dbus_message_append_args(msg,
+                               DBUS_TYPE_STRING, &member,
+                               DBUS_TYPE_UINT32, &info->interface,
+                               DBUS_TYPE_STRING, &ip,
+                               DBUS_TYPE_STRING, &mac,
+                               DBUS_TYPE_STRING, &hostname,
+                               DBUS_TYPE_UINT32, &info->tm,
+                               DBUS_TYPE_INVALID)) {
+               ERR("Event sending failed\n");
+               dbus_message_unref(msg);
+               return;
+       }
+
+       dbus_connection_send(tethering_conn, msg, NULL);
+       dbus_message_unref(msg);
+
+       return;
+}
+
+void _update_station_count(int count)
+{
+       static int prev_cnt = 0;
+       char icon_path[MH_NOTI_PATH_MAX] = {0, };
+       int wifi_count = 0;
+       int bt_count = 0;
+       int usb_count = 0;
+
+       if (_mobileap_is_enabled(MOBILE_AP_STATE_WIFI_AP)) {
+               return;
+       }
+
+       if (prev_cnt == count) {
+               return;
+       }
+
+       if (vconf_set_int(VCONFKEY_MOBILE_HOTSPOT_CONNECTED_DEVICE,
+                               count) < 0) {
+               ERR("Error setting up vconf\n");
+               return;
+       }
+
+       if (count == 0) {
+               prev_cnt = 0;
+               _delete_connected_noti();
+               return;
+       }
+
+       _get_station_count((gconstpointer)MOBILE_AP_TYPE_WIFI, _slist_find_station_by_interface, &wifi_count);
+       _get_station_count((gconstpointer)MOBILE_AP_TYPE_BT, _slist_find_station_by_interface, &bt_count);
+       _get_station_count((gconstpointer)MOBILE_AP_TYPE_USB, _slist_find_station_by_interface, &usb_count);
+
+       if (wifi_count > 0 && bt_count == 0 && usb_count == 0) {
+               g_strlcpy(icon_path, MH_NOTI_ICON_WIFI, sizeof(icon_path));
+       } else if (wifi_count == 0 && bt_count > 0 && usb_count == 0) {
+               g_strlcpy(icon_path, MH_NOTI_ICON_BT, sizeof(icon_path));
+       } else if (wifi_count == 0 && bt_count == 0 && usb_count > 0) {
+               g_strlcpy(icon_path, MH_NOTI_ICON_USB, sizeof(icon_path));
+       } else if (wifi_count == 0 && bt_count == 0 && usb_count == 0) {
+               return;
+       } else {
+               g_strlcpy(icon_path, MH_NOTI_ICON_GENERAL, sizeof(icon_path));
+       }
+
+       if (prev_cnt == 0) {
+               _create_connected_noti(count, icon_path);
+       } else {
+               _update_connected_noti(count, icon_path);
+       }
+
+       prev_cnt = count;
+       return;
+}
+
+int _add_station_info(mobile_ap_station_info_t *info)
+{
+       if (info == NULL) {
+               ERR("Invalid param\n");
+               return MOBILE_AP_ERROR_INVALID_PARAM;
+       }
+
+       guint count;
+       GSList *l = NULL;
+       mobile_ap_station_info_t *si = NULL;
+       int i = 0;
+
+       if (_get_station_info(info->mac, _slist_find_station_by_mac, &si) ==
+                       MOBILE_AP_ERROR_NONE) {
+               if (!si) {
+                       return MOBILE_AP_ERROR_INTERNAL;
+               }
+
+               if (g_strcmp0(si->hostname, info->hostname) == 0 &&
+                               g_strcmp0(si->ip, info->ip) == 0) {
+                       return MOBILE_AP_ERROR_INTERNAL;
+               }
+
+               _remove_station_info(si->mac, _slist_find_station_by_mac);
+       }
+
+       station_list = g_slist_append(station_list, info);
+       for (l = station_list; l != NULL; l = g_slist_next(l)) {
+               si = (mobile_ap_station_info_t *)l->data;
+               SDBG("[%d] interface : %d\n", i, si->interface);
+               SDBG("[%d] station MAC : %s\n", i, si->mac);
+               SDBG("[%d] station Hostname : %s\n", i, si->hostname);
+               SDBG("[%d] station IP : %s\n", i, si->ip);
+               SDBG("[%d] station connected time : %d\n", i, si->tm);
+               i++;
+       }
+
+       count = g_slist_length(station_list);
+       _update_station_count(count);
+
+       return MOBILE_AP_ERROR_NONE;
+}
+
+int _remove_station_info(gconstpointer data, GCompareFunc func)
+{
+       if (func == NULL) {
+               ERR("Invalid param\n");
+               return MOBILE_AP_ERROR_INVALID_PARAM;
+       }
+
+       if (station_list == NULL) {
+               ERR("There is no station\n");
+               return MOBILE_AP_ERROR_INTERNAL;
+       }
+
+       GSList *l = NULL;
+       mobile_ap_station_info_t *si = NULL;
+       int count;
+
+       l = g_slist_find_custom(station_list, data, func);
+       if (!l) {
+               ERR("Not found\n");
+               return MOBILE_AP_ERROR_INTERNAL;
+       }
+
+       si = (mobile_ap_station_info_t *)l->data;
+       SDBG("Remove station MAC : %s\n", si->mac);
+       station_list = g_slist_delete_link(station_list, l);
+       _send_dbus_station_info("DhcpLeaseDeleted", si);
+       g_free(si->hostname);
+       g_free(si);
+
+       count = g_slist_length(station_list);
+       _update_station_count(count);
+
+       return MOBILE_AP_ERROR_NONE;
+}
+
+int _remove_station_info_all(mobile_ap_type_e type)
+{
+       if (station_list == NULL) {
+               return MOBILE_AP_ERROR_NONE;
+       }
+
+       GSList *l = station_list;
+       GSList *temp_l = NULL;
+       mobile_ap_station_info_t *si = NULL;
+       int count;
+
+       _flush_dhcp_ack_timer();
+
+       while (l) {
+               si = (mobile_ap_station_info_t *)l->data;
+               if (si->interface != type) {
+                       l = g_slist_next(l);
+                       continue;
+               }
+
+               SDBG("Remove station MAC : %s\n", si->mac);
+               _send_dbus_station_info("DhcpLeaseDeleted", si);
+               g_free(si->hostname);
+               g_free(si);
+
+               temp_l = l;
+               l = g_slist_next(l);
+               station_list = g_slist_delete_link(station_list, temp_l);
+       }
+
+       count = g_slist_length(station_list);
+       _update_station_count(count);
+
+       return MOBILE_AP_ERROR_NONE;
+}
+
+int _get_station_info(gconstpointer data, GCompareFunc func,
+               mobile_ap_station_info_t **si)
+{
+       if (func == NULL || si == NULL) {
+               ERR("Invalid param\n");
+               return MOBILE_AP_ERROR_INVALID_PARAM;
+       }
+
+       if (station_list == NULL) {
+               ERR("There is no station\n");
+               return MOBILE_AP_ERROR_INTERNAL;
+       }
+
+       GSList *l = NULL;
+       mobile_ap_station_info_t *node = NULL;
+
+       l = g_slist_find_custom(station_list, data, func);
+       if (!l) {
+               ERR("Not found\n");
+               return MOBILE_AP_ERROR_INTERNAL;
+       }
+
+       node = l->data;
+       SDBG("Found station : %s\n", node->mac);
+       *si = node;
+
+       return MOBILE_AP_ERROR_NONE;
+}
+
+int _get_station_count(gconstpointer data, GCompareFunc func, int *count)
+{
+       if (count == NULL) {
+               ERR("Invalid param\n");
+               return MOBILE_AP_ERROR_INVALID_PARAM;
+       }
+
+       GSList *l = station_list;
+       int _count = 0;
+
+       for (_count = 0; l != NULL; _count++, l = g_slist_next(l)) {
+               l = g_slist_find_custom(l, data, func);
+               if (l == NULL)
+                       break;
+       }
+
+       *count = _count;
+
+       return MOBILE_AP_ERROR_NONE;
+}
+
+int _station_info_foreach(GFunc func, void *user_data)
+{
+       if (func == NULL) {
+               ERR("Invalid param\n");
+               return MOBILE_AP_ERROR_INVALID_PARAM;
+       }
+
+       g_slist_foreach(station_list, func, user_data);
+
+       return MOBILE_AP_ERROR_NONE;
+}
+
+int _add_interface_routing(const char *interface, const in_addr_t gateway)
+{
+       if (interface == NULL || interface[0] == '\0') {
+               ERR("Invalid parameter\n");
+               return MOBILE_AP_ERROR_INVALID_PARAM;
+       }
+
+       char cmd[MAX_BUF_SIZE] = {0, };
+       struct in_addr addr;
+       char *interface_gw;
+       in_addr_t subnet;
+
+       addr.s_addr = htonl(gateway);
+
+       subnet = inet_netof(addr);
+       addr = inet_makeaddr(subnet, 0);
+       interface_gw = inet_ntoa(addr);
+
+       snprintf(cmd, sizeof(cmd), "%s route add "INTERFACE_ROUTING,
+                       IP_CMD, interface_gw, TETHERING_ROUTING_TABLE, interface);
+       if (_execute_command(cmd)) {
+               ERR("cmd failed : %s\n", cmd);
+               return MOBILE_AP_ERROR_INTERNAL;
+       }
+
+       return MOBILE_AP_ERROR_NONE;
+}
+
+int _del_interface_routing(const char *interface, const in_addr_t gateway)
+{
+       if (interface == NULL || interface[0] == '\0') {
+               ERR("Invalid parameter\n");
+               return MOBILE_AP_ERROR_INVALID_PARAM;
+       }
+
+       char cmd[MAX_BUF_SIZE] = {0, };
+       struct in_addr addr;
+       char *interface_gw;
+       in_addr_t subnet;
+
+       addr.s_addr = htonl(gateway);
+
+       subnet = inet_netof(addr);
+       addr = inet_makeaddr(subnet, 0);
+       interface_gw = inet_ntoa(addr);
+
+       snprintf(cmd, sizeof(cmd), "%s route del "INTERFACE_ROUTING,
+                       IP_CMD, interface_gw, TETHERING_ROUTING_TABLE, interface);
+       if (_execute_command(cmd)) {
+               ERR("cmd failed : %s\n", cmd);
+               return MOBILE_AP_ERROR_INTERNAL;
+       }
+
+       return MOBILE_AP_ERROR_NONE;
+}
+
+int _add_routing_rule(const char *interface)
+{
+       if (interface == NULL || interface[0] == '\0') {
+               ERR("Invalid parameter\n");
+               return MOBILE_AP_ERROR_INVALID_PARAM;
+       }
+
+       char cmd[MAX_BUF_SIZE] = {0, };
+
+       snprintf(cmd, sizeof(cmd), "%s rule add "SRC_ROUTING_RULE,
+                       IP_CMD, interface, TETHERING_ROUTING_TABLE);
+       if (_execute_command(cmd)) {
+               ERR("cmd failed : %s\n", cmd);
+               return MOBILE_AP_ERROR_INTERNAL;
+       }
+
+       return MOBILE_AP_ERROR_NONE;
+}
+
+int _del_routing_rule(const char *interface)
+{
+       if (interface == NULL || interface[0] == '\0') {
+               ERR("Invalid parameter\n");
+               return MOBILE_AP_ERROR_INVALID_PARAM;
+       }
+
+       char cmd[MAX_BUF_SIZE] = {0, };
+
+       snprintf(cmd, sizeof(cmd), "%s rule del "SRC_ROUTING_RULE,
+                       IP_CMD, interface, TETHERING_ROUTING_TABLE);
+       if (_execute_command(cmd)) {
+               ERR("cmd failed : %s\n", cmd);
+               return MOBILE_AP_ERROR_INTERNAL;
+       }
+
+       return MOBILE_AP_ERROR_NONE;
+}
+
+int _flush_ip_address(const char *interface)
+{
+       if (interface == NULL || interface[0] == '\0') {
+               ERR("Invalid parameter\n");
+               return MOBILE_AP_ERROR_INVALID_PARAM;
+       }
+
+       char cmd[MAX_BUF_SIZE] = {0, };
+
+       snprintf(cmd, sizeof(cmd), "%s addr flush dev %s",
+                       IP_CMD, interface);
+       if (_execute_command(cmd)) {
+               ERR("cmd failed : %s\n", cmd);
+               return MOBILE_AP_ERROR_INTERNAL;
+       }
+
+       return MOBILE_AP_ERROR_NONE;
+}
+
+int _execute_command(const char *cmd)
+{
+       if (cmd == NULL) {
+               ERR("Invalid param\n");
+               return EXIT_FAILURE;
+       }
+
+       int status = 0;
+       int exit_status = 0;
+       pid_t pid = 0;
+       gchar **args = NULL;
+
+       SDBG("CMD : %s\n", cmd);
+
+       args = g_strsplit_set(cmd, " ", -1);
+       if (!args) {
+               ERR("g_strsplit_set failed\n");
+               return EXIT_FAILURE;
+       }
+
+       if ((pid = fork()) < 0) {
+               ERR("fork failed\n");
+               return EXIT_FAILURE;
+       }
+
+       if (!pid) {
+               if (execv(args[0], args)) {
+                       ERR("execl failed\n");
+               }
+
+               ERR("Should never get here!\n");
+               return EXIT_FAILURE;
+       } else {
+               /* Need to add timeout */
+               waitpid(pid, &status, 0);
+               g_strfreev(args);
+
+               if (WIFEXITED(status)) {
+                       exit_status = WEXITSTATUS(status);
+                       if (exit_status) {
+                               ERR("child return : %d\n", exit_status);
+                               return EXIT_FAILURE;
+                       }
+                       return EXIT_SUCCESS;
+               } else {
+                       ERR("child is terminated without exit\n");
+                       return EXIT_FAILURE;
+               }
+       }
+}
+
+int _get_tethering_type_from_ip(const char *ip, mobile_ap_type_e *type)
+{
+       if (ip == NULL || type == NULL) {
+               ERR("Invalid param\n");
+               return MOBILE_AP_ERROR_INVALID_PARAM;
+       }
+
+       static gboolean is_init = FALSE;
+       static in_addr_t subnet_wifi;
+       static in_addr_t subnet_bt_min;
+       static in_addr_t subnet_bt_max;
+       static in_addr_t subnet_usb;
+
+       struct in_addr addr;
+       in_addr_t subnet;
+
+       if (inet_aton(ip, &addr) == 0) {
+               SERR("Address : %s is invalid\n", ip);
+               return MOBILE_AP_ERROR_INVALID_PARAM;
+       }
+       subnet = inet_netof(addr);
+
+       if (is_init == FALSE) {
+               addr.s_addr = htonl(IP_ADDRESS_WIFI);
+               subnet_wifi = inet_netof(addr);
+
+               addr.s_addr = htonl(IP_ADDRESS_BT_1);
+               subnet_bt_min = inet_netof(addr);
+
+               addr.s_addr = htonl(IP_ADDRESS_BT_7);
+               subnet_bt_max = inet_netof(addr);
+
+               addr.s_addr = htonl(IP_ADDRESS_USB);
+               subnet_usb = inet_netof(addr);
+               is_init = TRUE;
+       }
+
+       if (subnet == subnet_wifi) {
+               if (_mobileap_is_enabled(MOBILE_AP_STATE_WIFI))
+                       *type = MOBILE_AP_TYPE_WIFI;
+               else
+                       *type = MOBILE_AP_TYPE_WIFI_AP;
+               return MOBILE_AP_ERROR_NONE;
+       } else if (subnet >= subnet_bt_min && subnet <= subnet_bt_max) {
+               *type = MOBILE_AP_TYPE_BT;
+               return MOBILE_AP_ERROR_NONE;
+       } else if (subnet == subnet_usb) {
+               *type = MOBILE_AP_TYPE_USB;
+               return MOBILE_AP_ERROR_NONE;
+       }
+
+       SERR("Tethering type cannot be decided from %s\n", ip);
+
+       return MOBILE_AP_ERROR_INVALID_PARAM;
+}
diff --git a/src/mobileap_handler.c b/src/mobileap_handler.c
new file mode 100644 (file)
index 0000000..89915c1
--- /dev/null
@@ -0,0 +1,509 @@
+/*
+ * mobileap-agent
+ * Copyright (c) 2012 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the License);
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <glib.h>
+#include <dbus/dbus.h>
+#include <dbus/dbus-glib.h>
+#include <dbus/dbus-glib-lowlevel.h>
+#include <stdlib.h>
+
+#include "mobileap_softap.h"
+#include "mobileap_common.h"
+#include "mobileap_bluetooth.h"
+#include "mobileap_wifi.h"
+#include "mobileap_usb.h"
+#include "mobileap_notification.h"
+#include "mobileap_handler.h"
+
+#define VCONF_IS_DEVICE_RENAMED_IN_UG "file/private/libug-setting-mobileap-efl/is_device_rename_local"
+
+typedef struct {
+       alarm_id_t alarm_id;
+       time_t end_time;
+       GSourceFunc func;
+       void *user_data;
+} sp_timeout_handler_t;
+
+static gboolean __wifi_timeout_cb(gpointer user_data);
+static gboolean __bt_timeout_cb(gpointer user_data);
+
+static sp_timeout_handler_t sp_timeout_handler[MOBILE_AP_TYPE_MAX] = {
+       {0, 0, __wifi_timeout_cb, NULL},
+       {0, 0, NULL, NULL},
+       {0, 0, __bt_timeout_cb, NULL},
+       {0, 0, NULL, NULL}};
+
+static void __handle_network_cellular_state_changed_cb(keynode_t *key, void *data)
+{
+       if (key == NULL) {
+               ERR("Parameter is NULL\n");
+               return;
+       }
+
+       TetheringObject *obj = (TetheringObject *)data;
+       int vconf_key = 0;
+
+       if (!_mobileap_is_enabled(MOBILE_AP_STATE_WIFI | MOBILE_AP_STATE_WIFI_AP)) {
+               return;
+       }
+
+       if (vconf_keynode_get_type(key) != VCONF_TYPE_INT) {
+               ERR("Invalid vconf key type\n");
+               return;
+       }
+
+       vconf_key = vconf_keynode_get_int(key);
+       SDBG("key = %s, value = %d(int)\n",
+                       vconf_keynode_get_name(key), vconf_key);
+
+       if (vconf_key != VCONFKEY_NETWORK_CELLULAR_FLIGHT_MODE)
+               return;
+
+       if (_mobileap_is_enabled(MOBILE_AP_STATE_WIFI))
+               _disable_wifi_tethering(obj);
+       else if (_mobileap_is_enabled(MOBILE_AP_STATE_WIFI_AP))
+               _disable_wifi_ap(obj);
+       if (_mobileap_is_enabled(MOBILE_AP_STATE_USB))
+               _disable_usb_tethering(obj);
+
+       _emit_mobileap_dbus_signal(obj, E_SIGNAL_FLIGHT_MODE, NULL);
+
+       return;
+}
+
+static void __handle_device_name_changed_cb(keynode_t *key, void *data)
+{
+       if (key == NULL || data == NULL) {
+               ERR("Parameter is NULL\n");
+               return;
+       }
+
+       TetheringObject *obj = (TetheringObject *)data;
+       char *vconf_key = NULL;
+       softap_settings_t new_settings;
+       softap_security_type_e sec_type;
+
+       if (!_mobileap_is_enabled(MOBILE_AP_STATE_WIFI | MOBILE_AP_STATE_WIFI_AP)) {
+               return;
+       }
+
+       if (vconf_keynode_get_type(key) != VCONF_TYPE_STRING) {
+               ERR("Invalid vconf key type\n");
+               return;
+       }
+       vconf_key = vconf_keynode_get_str(key);
+
+       if (g_strcmp0(vconf_key, obj->softap_settings.ssid) != 0) {
+               DBG("Device name is changed\n");
+               new_settings = obj->softap_settings;
+               if (!g_strcmp0(new_settings.security_type, SOFTAP_SECURITY_TYPE_WPA2_PSK_STR)) {
+                       sec_type = SOFTAP_SECURITY_TYPE_WPA2_PSK;
+               } else {
+                       sec_type = SOFTAP_SECURITY_TYPE_OPEN;
+               }
+               g_strlcpy(new_settings.ssid, vconf_key, sizeof(new_settings.ssid));
+               if (_mobileap_is_enabled(MOBILE_AP_STATE_WIFI)) {
+                       _reload_softap_settings(obj, new_settings.ssid, new_settings.key,
+                                       new_settings.hide_mode, sec_type);
+               } else if (_mobileap_is_enabled(MOBILE_AP_STATE_WIFI_AP)) {
+                       _reload_softap_settings_for_ap(obj, new_settings.ssid, new_settings.key,
+                                       new_settings.hide_mode, sec_type);
+               }
+       }
+       return;
+}
+
+static void __handle_language_changed_cb(keynode_t *key, void *data)
+{
+       if (key == NULL || data == NULL) {
+               ERR("Parameter is NULL\n");
+               return;
+       }
+
+       char *language = NULL;
+
+       if (vconf_keynode_get_type(key) != VCONF_TYPE_STRING) {
+               ERR("Invalid vconf key type\n");
+               return;
+       }
+
+       language = vconf_get_str(VCONFKEY_LANGSET);
+       if (language) {
+               setenv("LANG", language, 1);
+               setenv("LC_MESSAGES",  language, 1);
+               setlocale(LC_ALL, language);
+               free(language);
+       }
+
+       return;
+}
+
+gboolean _is_power_save_survival_mode(void)
+{
+       int ret;
+       int status;
+
+       ret = vconf_get_int(VCONFKEY_SETAPPL_PSMODE, &status);
+       if (ret != 0) {
+               ERR("vconf_get_int is failed : %d\n", ret);
+               return FALSE;
+       }
+
+       DBG(" PS mode status : %d", status);
+       if (status == SETTING_PSMODE_SURVIVAL)
+               return TRUE;
+       else
+               return FALSE;
+}
+
+void _register_vconf_cb(void *user_data)
+{
+       if (user_data == NULL) {
+               ERR("Invalid param\n");
+               return;
+       }
+
+       vconf_reg_t vconf_reg[] = {
+               {VCONFKEY_NETWORK_CELLULAR_STATE,
+                       __handle_network_cellular_state_changed_cb, NULL},
+               {VCONFKEY_SETAPPL_DEVICE_NAME_STR,
+                       __handle_device_name_changed_cb, NULL},
+               {VCONFKEY_LANGSET,
+                       __handle_language_changed_cb, NULL},
+               {NULL, NULL, NULL}
+       };
+
+       int i = 0;
+       int ret = 0;
+
+       while (vconf_reg[i].key != NULL && vconf_reg[i].cb != NULL) {
+               ret = vconf_notify_key_changed(vconf_reg[i].key,
+                                       vconf_reg[i].cb, user_data);
+               if (ret != 0) {
+                       ERR("vconf_notify_key_changed is failed : %d\n", ret);
+               }
+
+               if (vconf_reg[i].value) {
+                       ret = vconf_get_int(vconf_reg[i].key,
+                                       vconf_reg[i].value);
+                       if (ret != 0) {
+                               ERR("vconf_get_int is failed : %d\n", ret);
+                       }
+               }
+
+               i++;
+       }
+
+       return;
+}
+
+void _unregister_vconf_cb(void *user_data)
+{
+       if (user_data == NULL) {
+               ERR("Invalid param\n");
+               return;
+       }
+
+       vconf_reg_t vconf_reg[] = {
+               {VCONFKEY_NETWORK_CELLULAR_STATE,
+                       __handle_network_cellular_state_changed_cb, NULL},
+               {VCONFKEY_SETAPPL_DEVICE_NAME_STR,
+                       __handle_device_name_changed_cb, NULL},
+               {VCONFKEY_LANGSET,
+                       __handle_language_changed_cb, NULL},
+               {NULL, NULL, NULL}
+       };
+
+       int i = 0;
+       int ret = 0;
+
+       while (vconf_reg[i].key != NULL && vconf_reg[i].cb != NULL) {
+               ret = vconf_ignore_key_changed(vconf_reg[i].key,
+                               vconf_reg[i].cb);
+               if (ret != 0) {
+                       ERR("vconf_notify_key_changed is failed : %d\n", ret);
+               }
+
+               i++;
+       }
+
+       return;
+}
+
+static gboolean __wifi_timeout_cb(gpointer data)
+{
+       DBG("+\n");
+       if (data == NULL) {
+               ERR("data is NULL\n");
+               return FALSE;
+       }
+
+       TetheringObject *obj = (TetheringObject *)data;
+
+       if (_mobileap_is_enabled(MOBILE_AP_STATE_WIFI) == FALSE) {
+               ERR("There is no conn. via Wi-Fi tethernig. But nothing to do\n");
+               return FALSE;
+       }
+
+       _disable_wifi_tethering(obj);
+       _emit_mobileap_dbus_signal(obj,
+                       E_SIGNAL_WIFI_TETHER_OFF, SIGNAL_MSG_TIMEOUT);
+
+       DBG("-\n");
+       return FALSE;
+}
+
+static gboolean __bt_timeout_cb(gpointer data)
+{
+       DBG("+\n");
+       if (data == NULL) {
+               ERR("data is NULL\n");
+               return FALSE;
+       }
+
+       TetheringObject *obj = (TetheringObject *)data;
+
+       if (_mobileap_is_enabled(MOBILE_AP_STATE_BT) == FALSE) {
+               ERR("There is no conn. via BT tethering. But nothing to do\n");
+               return FALSE;
+       }
+
+       _disable_bt_tethering(obj);
+       _emit_mobileap_dbus_signal(obj,
+                       E_SIGNAL_BT_TETHER_OFF, SIGNAL_MSG_TIMEOUT);
+
+       DBG("-\n");
+       return FALSE;
+}
+
+static sp_timeout_handler_t *__find_next_timeout(void)
+{
+       sp_timeout_handler_t *next_timeout = &sp_timeout_handler[MOBILE_AP_TYPE_WIFI];
+       mobile_ap_type_e i;
+
+       for (i = MOBILE_AP_TYPE_USB; i < MOBILE_AP_TYPE_MAX; i++) {
+               if (sp_timeout_handler[i].end_time == 0)
+                       continue;
+
+               if (sp_timeout_handler[i].end_time < next_timeout->end_time ||
+                               next_timeout->end_time == 0)
+                       next_timeout = &sp_timeout_handler[i];
+       }
+
+       return next_timeout;
+}
+
+static void __expire_timeout(sp_timeout_handler_t *sp)
+{
+       if (sp->alarm_id > 0) {
+               alarmmgr_remove_alarm(sp->alarm_id);
+               sp->alarm_id = 0;
+       }
+
+       sp->end_time = 0;
+
+       if (sp->func)
+               sp->func(sp->user_data);
+}
+
+static void __reset_timeout(sp_timeout_handler_t *sp)
+{
+       if (sp->alarm_id > 0) {
+               alarmmgr_remove_alarm(sp->alarm_id);
+               sp->alarm_id = 0;
+       }
+
+       sp->end_time = 0;
+}
+
+int _sp_timeout_handler(alarm_id_t alarm_id, void *user_param)
+{
+       DBG("+\n");
+
+       int ret;
+       time_t now;
+       time_t interval;
+       mobile_ap_type_e i;
+       sp_timeout_handler_t *next_timeout;
+
+       now = time(NULL);
+       for (i = MOBILE_AP_TYPE_WIFI; i < MOBILE_AP_TYPE_MAX; i++) {
+               if (sp_timeout_handler[i].end_time == 0)
+                       continue;
+
+               if (sp_timeout_handler[i].alarm_id == alarm_id) {
+                       sp_timeout_handler[i].alarm_id = 0;
+                       __expire_timeout(&sp_timeout_handler[i]);
+                       continue;
+               }
+
+               interval = (time_t)difftime(sp_timeout_handler[i].end_time, now);
+               if (interval > 0)
+                       continue;
+
+               __expire_timeout(&sp_timeout_handler[i]);
+       }
+
+       next_timeout = __find_next_timeout();
+       if (next_timeout->end_time == 0)
+               return 0;
+
+       interval = (time_t)difftime(next_timeout->end_time, now);
+       ret = alarmmgr_add_alarm(ALARM_TYPE_VOLATILE, interval, 0, NULL,
+                       &next_timeout->alarm_id);
+       if (ret != ALARMMGR_RESULT_SUCCESS) {
+               ERR("alarmmgr_add_alarm is failed. end_time : %d\n",
+                               next_timeout->end_time);
+               return 0;
+       }
+
+       DBG("-\n");
+       return 0;
+}
+
+void _init_timeout_cb(mobile_ap_type_e type, void *user_data)
+{
+       DBG("+\n");
+
+       if (sp_timeout_handler[type].func == NULL) {
+               return;
+       }
+
+       if (user_data == NULL) {
+               ERR("Invalid param\n");
+               return;
+       }
+
+       sp_timeout_handler[type].alarm_id = 0;
+       sp_timeout_handler[type].end_time = 0;
+       sp_timeout_handler[type].user_data = user_data;
+
+       DBG("-\n");
+       return;
+}
+
+void _start_timeout_cb(mobile_ap_type_e type, time_t end_time)
+{
+       int ret;
+       time_t interval;
+       mobile_ap_type_e i;
+       sp_timeout_handler_t *next_timeout;
+
+       if (sp_timeout_handler[type].func == NULL) {
+               return;
+       }
+
+       __reset_timeout(&sp_timeout_handler[type]);
+       sp_timeout_handler[type].end_time = end_time;
+
+       next_timeout = __find_next_timeout();
+       if (next_timeout->alarm_id > 0) {
+               return;
+       }
+
+       for (i = MOBILE_AP_TYPE_WIFI; i < MOBILE_AP_TYPE_MAX; i++) {
+               if (sp_timeout_handler[i].alarm_id == 0)
+                       continue;
+
+               __reset_timeout(&sp_timeout_handler[i]);
+       }
+
+       interval = (time_t)difftime(next_timeout->end_time, time(NULL));
+       if (interval <= 0) {
+               __expire_timeout(next_timeout);
+               return;
+       }
+
+       ret = alarmmgr_add_alarm(ALARM_TYPE_VOLATILE, interval, 0, NULL,
+                       &next_timeout->alarm_id);
+       if (ret != ALARMMGR_RESULT_SUCCESS) {
+               ERR("alarmmgr_add_alarm is failed. type : %d, end_time : %d\n",
+                               type, end_time);
+               return;
+       }
+
+       return;
+}
+
+void _stop_timeout_cb(mobile_ap_type_e type)
+{
+       DBG("+\n");
+
+       int ret;
+       time_t interval;
+       mobile_ap_type_e i;
+       sp_timeout_handler_t *next_timeout;
+
+       if (sp_timeout_handler[type].func == NULL) {
+               return;
+       }
+
+       if (sp_timeout_handler[type].alarm_id == 0) {
+               sp_timeout_handler[type].end_time = 0;
+               return;
+       }
+
+       for (i = MOBILE_AP_TYPE_WIFI; i < MOBILE_AP_TYPE_MAX; i++) {
+               if (sp_timeout_handler[i].end_time != sp_timeout_handler[type].end_time ||
+                               type == i)
+                       continue;
+
+               sp_timeout_handler[i].alarm_id = sp_timeout_handler[type].alarm_id;
+               sp_timeout_handler[type].alarm_id = 0;
+               sp_timeout_handler[type].end_time = 0;
+               return;
+       }
+       __reset_timeout(&sp_timeout_handler[type]);
+
+       next_timeout = __find_next_timeout();
+       if (next_timeout->end_time == 0)
+               return;
+
+       interval = (time_t)difftime(next_timeout->end_time, time(NULL));
+       if (interval <= 0) {
+               __expire_timeout(next_timeout);
+               return;
+       }
+
+       ret = alarmmgr_add_alarm(ALARM_TYPE_VOLATILE, interval, 0, NULL,
+                       &next_timeout->alarm_id);
+       if (ret != ALARMMGR_RESULT_SUCCESS) {
+               ERR("alarmmgr_add_alarm is failed. type : %d, end_time : %d\n",
+                               type, next_timeout->end_time);
+       }
+
+       DBG("-\n");
+       return;
+}
+
+void _deinit_timeout_cb(mobile_ap_type_e type) {
+       DBG("+\n");
+
+       if (sp_timeout_handler[type].func == NULL) {
+               return;
+       }
+
+       if (sp_timeout_handler[type].alarm_id > 0) {
+               _stop_timeout_cb(type);
+       }
+
+       sp_timeout_handler[type].user_data = NULL;
+       sp_timeout_handler[type].end_time = 0;
+
+       DBG("-\n");
+       return;
+}
diff --git a/src/mobileap_iptables.c b/src/mobileap_iptables.c
new file mode 100755 (executable)
index 0000000..429cbf2
--- /dev/null
@@ -0,0 +1,329 @@
+/*
+ * mobileap-agent
+ * Copyright (c) 2012 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the License);
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <stdio.h>
+#include <stdarg.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <unistd.h>
+#include "mobileap_iptables.h"
+#include "mobileap_softap.h"
+#include "mobileap.h"
+#include "mobileap_common.h"
+
+#define CREATE_CHAIN_STR               "-t %s -N %s"  /* table_name, chain_name */
+#define REDIRECTION_ADD_RULE_STR       "-t %s -A %s -j %s"
+#define REDIRECTION_DEL_RULE_STR       "-t %s -D %s -j %s"
+#define FLUSH_CMD_STR          "-t %s -F %s"
+#define DELETE_CHAIN_STR       "-t %s -X %s"
+#define FORWARD_RULE_WITH_ACTION_STR           "-t %s -A %s -i %s -o %s -j %s"
+#define FORWARD_RULE_WITH_ACTION_AND_STATE_STR "-t %s -A %s -i %s -o %s -m state --state %s -j %s"
+#define MASQUERADE_RULE_STR            "-t %s -A %s -o %s -j MASQUERADE"
+#define PORT_FORWARD_RULE_STR  "-t %s -A %s -i %s -p %s -d %s --dport %d -j DNAT --to %s:%d"
+#define CLAMP_MSS_RULE_STR     "-t %s -A %s -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --clamp-mss-to-pmtu"
+#define DEFAULT_RULE_STR       "-t %s -A %s -j %s"
+
+
+int _iptables_create_chain(const char *table_name, const char *chain_name)
+{
+       char cmd[MAX_BUF_SIZE] = { 0, };
+
+       snprintf(cmd, sizeof(cmd), "%s "CREATE_CHAIN_STR, IPTABLES, table_name,
+               chain_name);
+       SDBG("command [%s]\n", cmd);
+       if (_execute_command(cmd)) {
+               SERR("command [%s] failed\n", cmd);
+               return MOBILE_AP_ERROR_INTERNAL;
+       }
+
+       return MOBILE_AP_ERROR_NONE;
+}
+
+int _iptables_flush_rules(const char *table_name, const char *chain_name)
+{
+       char cmd[MAX_BUF_SIZE] = { 0, };
+
+       snprintf(cmd, sizeof(cmd), "%s "FLUSH_CMD_STR, IPTABLES, table_name,
+               chain_name);
+       SDBG("command [%s]\n", cmd);
+       if (_execute_command(cmd)) {
+               SERR("command [%s] failed\n", cmd);
+               return MOBILE_AP_ERROR_INTERNAL;
+       }
+
+       return MOBILE_AP_ERROR_NONE;
+}
+
+int _iptables_delete_chain(const char *table_name, const char *chain_name)
+{
+       char cmd[MAX_BUF_SIZE] = { 0, };
+
+       snprintf(cmd, sizeof(cmd), "%s "DELETE_CHAIN_STR, IPTABLES, table_name,
+               chain_name);
+       SDBG("command [%s]\n", cmd);
+       if (_execute_command(cmd)) {
+               SERR("command [%s] failed\n", cmd);
+               return MOBILE_AP_ERROR_INTERNAL;
+       }
+
+       return MOBILE_AP_ERROR_NONE;
+}
+
+int _iptables_add_rule(iptables_rule_e rule_type, const char *table, const char *chain, ...)
+{
+       if (table == NULL || chain == NULL) {
+               ERR("invalid parameters\n");
+               return MOBILE_AP_ERROR_INVALID_PARAM;
+       }
+
+       va_list ap;
+       char cmd[MAX_BUF_SIZE] = { 0, };
+
+       va_start(ap, chain);
+       switch (rule_type) {
+       case PKT_REDIRECTION_RULE: {
+               char *dst_chain;
+
+               dst_chain = va_arg(ap, char *);
+               if (dst_chain == NULL) {
+                       ERR("invalid parameters\n");
+                       goto ERROR_EXIT;
+               }
+
+               snprintf(cmd, sizeof(cmd), "%s "REDIRECTION_ADD_RULE_STR, IPTABLES,
+                               table, chain, dst_chain);
+               break;
+       }
+
+       case FORWARD_RULE_WITH_ACTION: {
+               char *in_iface = NULL;
+               char *out_iface = NULL;
+               char *action = NULL;
+
+               in_iface = va_arg(ap, char *);
+               out_iface = va_arg(ap, char *);
+               action = va_arg(ap, char *);
+
+               if (in_iface == NULL || out_iface == NULL || action == NULL) {
+                       ERR("invalid parameters\n");
+                       goto ERROR_EXIT;
+               }
+
+               snprintf(cmd, sizeof(cmd), "%s "FORWARD_RULE_WITH_ACTION_STR, IPTABLES,
+                       table, chain, in_iface, out_iface, action);
+               break;
+       }
+
+       case FORWARD_RULE_WITH_ACTION_AND_STATE: {
+               char *in_iface = NULL;
+               char *out_iface = NULL;
+               char *action = NULL;
+               char *state = NULL;
+
+               in_iface = va_arg(ap, char *);
+               out_iface = va_arg(ap, char *);
+               action = va_arg(ap, char *);
+               state = va_arg(ap, char *);
+
+               if (in_iface == NULL || out_iface == NULL || action == NULL ||
+                               state == NULL) {
+                       ERR("invalid parameters\n");
+                       goto ERROR_EXIT;
+               }
+
+               snprintf(cmd, sizeof(cmd), "%s "FORWARD_RULE_WITH_ACTION_AND_STATE_STR,
+                       IPTABLES, table, chain, in_iface, out_iface, state, action);
+
+               break;
+       }
+
+       case PORT_FW_RULE: {
+               char *ip_iface = NULL;
+               char *proto = NULL;
+               char *org_ip = NULL;
+               char *final_ip = NULL;
+               unsigned short org_port = 0;
+               unsigned short final_port = 0;
+
+               ip_iface = va_arg(ap, char *);
+               proto = va_arg(ap, char *);
+               org_ip = va_arg(ap, char *);
+               final_ip = va_arg(ap, char *);
+               org_port = va_arg(ap, int);
+               final_port = va_arg(ap, int);
+
+               if (ip_iface == NULL || proto == NULL || org_ip == NULL ||
+                               final_ip == NULL) {
+                       ERR("invalid parameters\n");
+                       goto ERROR_EXIT;
+               }
+
+               snprintf(cmd, sizeof(cmd), "%s "PORT_FORWARD_RULE_STR,
+                       IPTABLES, table, chain, ip_iface, proto,
+                       org_ip, org_port, final_ip, final_port);
+               break;
+       }
+
+       case MASQ_RULE: {
+               char *ext_iface = NULL;
+
+               ext_iface = va_arg(ap, char *);
+
+               if (ext_iface == NULL) {
+                       ERR("invalid parameters\n");
+                       goto ERROR_EXIT;
+               }
+
+               snprintf(cmd, sizeof(cmd), "%s "MASQUERADE_RULE_STR, IPTABLES,
+                               table, chain, ext_iface);
+               break;
+       }
+
+       case CLAMP_MSS_RULE: {
+               snprintf(cmd, sizeof(cmd), "%s "CLAMP_MSS_RULE_STR, IPTABLES,
+                       table, chain);
+               break;
+       }
+
+       case DEFAULT_RULE: {
+               char *action;
+
+               action = va_arg(ap, char *);
+
+               if (action == NULL) {
+                       ERR("invalid parameters\n");
+                       goto ERROR_EXIT;
+               }
+
+               snprintf(cmd, sizeof(cmd), "%s "DEFAULT_RULE_STR, IPTABLES,
+                       table, chain, action);
+               break;
+       }
+
+       default:
+               ERR("case not supported\n");
+               goto ERROR_EXIT;
+       }
+
+       if (_execute_command(cmd)) {
+               SERR("command [%s] failed\n", cmd);
+               goto ERROR_EXIT;
+       }
+
+       va_end(ap);
+       return MOBILE_AP_ERROR_NONE;
+
+ERROR_EXIT:
+       va_end(ap);
+       return MOBILE_AP_ERROR_INVALID_PARAM;
+}
+
+int _iptables_delete_rule(iptables_rule_e rule_type, const char *table, const char *chain, ...)
+{
+       va_list ap;
+       char cmd[MAX_BUF_SIZE] = { 0, };
+
+       va_start(ap, chain);
+       switch (rule_type) {
+       case PKT_REDIRECTION_RULE: {
+               char *dst_chain = NULL;
+
+               dst_chain = va_arg(ap, char *);
+               snprintf(cmd, sizeof(cmd), "%s "REDIRECTION_DEL_RULE_STR, IPTABLES,
+                       table, chain, dst_chain);
+               break;
+       }
+       default:
+               ERR("case not supported\n");
+               goto ERROR_EXIT;
+       }
+
+       if (_execute_command(cmd)) {
+               SERR("command [%s] failed\n", cmd);
+               return MOBILE_AP_ERROR_INTERNAL;
+       }
+       va_end(ap);
+       return MOBILE_AP_ERROR_NONE;
+
+ERROR_EXIT:
+       va_end(ap);
+       return MOBILE_AP_ERROR_INTERNAL;
+}
+
+int _get_data_usage(const char *src, const char *dest, unsigned long long *tx,
+               unsigned long long *rx)
+{
+       if (src == NULL || src[0] == '\0' || dest == NULL || dest[0] == '\0' ||
+                       tx == NULL || rx == NULL) {
+               ERR("Invalid parameter\n");
+               return MOBILE_AP_ERROR_INVALID_PARAM;
+       }
+
+       char cmd[MAX_BUF_SIZE] = {0, };
+       char buf[MAX_BUF_SIZE] = {0, };
+       FILE *fp = NULL;
+
+       /* Tx : Src. -> Dest. */
+       snprintf(cmd, sizeof(cmd),
+               "%s -t %s -L %s -vx | %s -v DROP | %s \"%s[ ]*%s\" | %s '{ print $2 }' > %s",
+               IPTABLES, TABLE_FILTER, TETH_FILTER_FW, GREP, GREP, src, dest, AWK, DATA_USAGE_FILE);
+       if (system(cmd) < 0) {
+               ERR("cmd %s is failed\n", cmd);
+       }
+
+       *tx = 0;
+
+       fp = fopen(DATA_USAGE_FILE, "r");
+       if (fp == NULL) {
+               ERR("%s open failed\n", DATA_USAGE_FILE);
+               ERR("%s\n", strerror(errno));
+               return MOBILE_AP_ERROR_INTERNAL;
+       }
+
+       while (fgets(buf, sizeof(buf), fp) != NULL) {
+               *tx += atoll(buf);
+       }
+
+       fclose(fp);
+       unlink(DATA_USAGE_FILE);
+
+       /* Rx : Dest. -> Src. */
+       snprintf(cmd, sizeof(cmd),
+               "%s -t %s -L %s -vx | %s -v DROP | %s \"%s[ ]*%s\" | %s '{ print $2 }' > %s",
+               IPTABLES, TABLE_FILTER, TETH_FILTER_FW, GREP, GREP, dest, src, AWK, DATA_USAGE_FILE);
+       if (system(cmd) < 0) {
+               ERR("cmd %s is failed\n", cmd);
+       }
+
+       *rx = 0;
+
+       fp = fopen(DATA_USAGE_FILE, "r");
+       if (fp == NULL) {
+               ERR("%s open failed\n", DATA_USAGE_FILE);
+               ERR("%s\n", strerror(errno));
+               return MOBILE_AP_ERROR_INTERNAL;
+       }
+
+       while (fgets(buf, sizeof(buf), fp) != NULL) {
+               *rx += atoll(buf);
+       }
+
+       fclose(fp);
+       unlink(DATA_USAGE_FILE);
+
+       return MOBILE_AP_ERROR_NONE;
+}
diff --git a/src/mobileap_main.c b/src/mobileap_main.c
new file mode 100644 (file)
index 0000000..c906bb4
--- /dev/null
@@ -0,0 +1,649 @@
+/*
+ * mobileap-agent
+ * Copyright (c) 2012 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the License);
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <fcntl.h>
+#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 <time.h>
+#include <dd-display.h>
+#include <vconf.h>
+#include <net_connection.h>
+#include <appcore-common.h>
+#include <wifi.h>
+#include <alarm.h>
+
+#include "mobileap_softap.h"
+#include "mobileap_handler.h"
+#include "mobileap_common.h"
+#include "mobileap_bluetooth.h"
+#include "mobileap_wifi.h"
+#include "mobileap_usb.h"
+#include "mobileap_network.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)
+{
+       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));
+}
+
+gboolean _mobileap_set_state(int state)
+{
+       int vconf_ret = 0;
+
+       mobileap_state |= state;
+
+       vconf_ret = vconf_set_int(VCONFKEY_MOBILE_HOTSPOT_MODE, mobileap_state);
+       if (vconf_ret != 0) {
+               ERR("vconf_set_int is failed : %d\n", vconf_ret);
+               return FALSE;
+       }
+
+       return TRUE;
+}
+
+gboolean _mobileap_is_disabled(void)
+{
+       return mobileap_state ? FALSE : TRUE;
+}
+
+gboolean _mobileap_is_enabled(int state)
+{
+       return (mobileap_state & state) ? TRUE : FALSE;
+}
+
+gboolean _mobileap_is_enabled_by_type(mobile_ap_type_e type)
+{
+       switch (type) {
+       case MOBILE_AP_TYPE_WIFI:
+               if (_mobileap_is_enabled(MOBILE_AP_STATE_WIFI))
+                       return TRUE;
+               break;
+
+       case MOBILE_AP_TYPE_BT:
+               if (_mobileap_is_enabled(MOBILE_AP_STATE_BT))
+                       return TRUE;
+               break;
+
+       case MOBILE_AP_TYPE_USB:
+               if (_mobileap_is_enabled(MOBILE_AP_STATE_USB))
+                       return TRUE;
+               break;
+
+       case MOBILE_AP_TYPE_WIFI_AP:
+               if (_mobileap_is_enabled(MOBILE_AP_STATE_WIFI_AP))
+                       return TRUE;
+               break;
+
+       default:
+               ERR("Unknow type : %d\n", type);
+               break;
+       }
+
+       return FALSE;
+}
+
+gboolean _mobileap_clear_state(int state)
+{
+       int vconf_ret = 0;
+
+       mobileap_state &= (~state);
+
+       vconf_ret = vconf_set_int(VCONFKEY_MOBILE_HOTSPOT_MODE, mobileap_state);
+       if (vconf_ret != 0) {
+               ERR("vconf_set_int is failed : %d\n", vconf_ret);
+               return FALSE;
+       }
+
+       return TRUE;
+}
+
+gboolean _terminate_mobileap_agent(gpointer user_data)
+{
+       if (mainloop == NULL) {
+               return FALSE;
+       }
+
+       if (!_mobileap_is_disabled()) {
+               DBG("Tethering is enabled\n");
+               return FALSE;
+       }
+
+       if (_is_trying_network_operation()) {
+               DBG("Network operation is going on\n");
+               return FALSE;
+       }
+
+       if (_is_trying_wifi_operation()) {
+               DBG("Wi-Fi operation is going on\n");
+               return FALSE;
+       }
+
+       if (_is_trying_bt_operation()) {
+               DBG("BT operation is going on\n");
+               return FALSE;
+       }
+
+       if (_is_trying_usb_operation()) {
+               DBG("USB operation is going on\n");
+               return FALSE;
+       }
+
+       DBG("All tethering / AP's are turned off\n");
+       g_main_loop_quit(mainloop);
+       mainloop = NULL;
+
+       return FALSE;
+}
+
+void _block_device_sleep(void)
+{
+       int ret = 0;
+
+       ret = display_lock_state(LCD_OFF, STAY_CUR_STATE, 0);
+       if (ret < 0)
+               ERR("PM control [ERROR] result = %d\n", ret);
+       else
+               DBG("PM control [SUCCESS]\n");
+}
+
+void _unblock_device_sleep(void)
+{
+       int ret = 0;
+
+       ret = display_unlock_state(LCD_OFF, PM_SLEEP_MARGIN);
+       if (ret < 0)
+               ERR("PM control [ERROR] result = %d\n", ret);
+       else
+               DBG("PM control [SUCCESS]\n");
+}
+
+int _init_tethering(TetheringObject *obj)
+{
+       int ret = MOBILE_AP_ERROR_NONE;
+
+       DBG("obj->init_count: %d\n", obj->init_count);
+
+       if (obj->init_count > 0) {
+               obj->init_count++;
+               return MOBILE_AP_ERROR_NONE;
+       }
+
+       if (!_mobileap_is_enabled(MOBILE_AP_STATE_WIFI_AP)) {
+               ret = _open_network();
+       }
+       _mh_core_execute_dhcp_server();
+
+       obj->init_count++;
+
+       return ret;
+}
+
+gboolean _deinit_tethering(TetheringObject *obj)
+{
+       DBG("obj->init_count: %d\n", obj->init_count);
+
+       guint idle_id;
+
+       if (obj->init_count > 1) {
+               obj->init_count--;
+               return TRUE;
+       } else if (obj->init_count <= 0) {
+               ERR("Already deinitialized\n");
+               obj->init_count = 0;
+               return TRUE;
+       }
+
+       obj->init_count = 0;
+
+       _mh_core_terminate_dhcp_server();
+
+       if (!_mobileap_is_enabled(MOBILE_AP_STATE_WIFI_AP)) {
+               _close_network();
+       }
+
+       idle_id = g_idle_add(_terminate_mobileap_agent, NULL);
+       if (idle_id == 0) {
+               ERR("g_idle_add is failed\n");
+       }
+
+       return TRUE;
+}
+
+gboolean tethering_disable(TetheringObject *obj, DBusGMethodInvocation *context)
+{
+       int ret = MOBILE_AP_ERROR_NONE;
+
+       DBG("+\n");
+       g_assert(obj != NULL);
+       g_assert(context != NULL);
+
+       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);
+               return FALSE;
+       }
+
+       _disable_wifi_tethering(obj);
+       _disable_bt_tethering(obj);
+       _disable_usb_tethering(obj);
+
+       dbus_g_method_return(context, MOBILE_AP_DISABLE_CFM, ret);
+
+       return TRUE;
+}
+
+gboolean tethering_get_station_info(TetheringObject *obj,
+                                               DBusGMethodInvocation *context)
+{
+       DBG("+\n");
+
+       GPtrArray *array = g_ptr_array_new();
+
+       g_assert(obj != NULL);
+       g_assert(context != NULL);
+
+       _station_info_foreach(__add_station_info_to_array, array);
+
+       dbus_g_method_return(context, MOBILE_AP_GET_STATION_INFO_CFM, array);
+       g_ptr_array_free(array, TRUE);
+
+       DBG("-\n");
+
+       return TRUE;
+}
+
+gboolean tethering_get_data_packet_usage(TetheringObject *obj,
+                                               DBusGMethodInvocation *context)
+{
+       char *if_name = NULL;
+       unsigned long long wifi_tx_bytes = 0;
+       unsigned long long wifi_rx_bytes = 0;
+       unsigned long long bt_tx_bytes = 0;
+       unsigned long long bt_rx_bytes = 0;
+       unsigned long long usb_tx_bytes = 0;
+       unsigned long long usb_rx_bytes = 0;
+       unsigned long long 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,
+                               0ULL, 0ULL);
+               return FALSE;
+       }
+
+       if (_mobileap_is_enabled(MOBILE_AP_STATE_WIFI))
+               _get_data_usage(WIFI_IF, if_name,
+                               &wifi_tx_bytes, &wifi_rx_bytes);
+
+       if (_mobileap_is_enabled(MOBILE_AP_STATE_BT))
+               _get_data_usage(BT_IF_ALL, if_name,
+                               &bt_tx_bytes, &bt_rx_bytes);
+
+       if (_mobileap_is_enabled(MOBILE_AP_STATE_USB))
+               _get_data_usage(USB_IF, if_name,
+                               &usb_tx_bytes, &usb_rx_bytes);
+       free(if_name);
+
+       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,
+                       tx_bytes, rx_bytes);
+
+       return TRUE;
+}
+
+static DBusHandlerResult __dnsmasq_signal_filter(DBusConnection *conn,
+               DBusMessage *msg, void *user_data)
+{
+       if (!user_data) {
+               ERR("Invalid param\n");
+               return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
+       }
+
+       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;
+               }
+               SDBG("DhcpConnected signal : %s  %s %s\n", ip_addr, mac, name);
+               /*
+                * DHCP ACK received, destroy timeout if exists
+                */
+               _destroy_dhcp_ack_timer(mac);
+
+               if (_get_tethering_type_from_ip(ip_addr, &type) != MOBILE_AP_ERROR_NONE)
+                       return DBUS_HANDLER_RESULT_HANDLED;
+
+               if (_mobileap_is_enabled_by_type(type) == FALSE) {
+                       return DBUS_HANDLER_RESULT_HANDLED;
+               }
+
+               info = (mobile_ap_station_info_t *)malloc(sizeof(mobile_ap_station_info_t));
+               if (info == NULL) {
+                       ERR("malloc failed\n");
+                       return DBUS_HANDLER_RESULT_HANDLED;
+               }
+
+               info->interface = type;
+               g_strlcpy(info->ip, ip_addr, sizeof(info->ip));
+               g_strlcpy(info->mac, mac, sizeof(info->mac));
+               if (type == MOBILE_AP_TYPE_WIFI || type == MOBILE_AP_TYPE_USB ||
+                               type == MOBILE_AP_TYPE_WIFI_AP) {
+                       if (name[0] == '\0')
+                               info->hostname = g_strdup(MOBILE_AP_NAME_UNKNOWN);
+                       else
+                               info->hostname = g_strdup(name);
+               } else if (type == MOBILE_AP_TYPE_BT) {
+                       _bt_get_remote_device_name(obj, mac, &bt_remote_device_name);
+                       if (bt_remote_device_name == NULL)
+                               info->hostname = g_strdup(MOBILE_AP_NAME_UNKNOWN);
+                       else
+                               info->hostname = bt_remote_device_name;
+               }
+               time(&tm);
+               info->tm = tm;
+
+               if (_add_station_info(info) != MOBILE_AP_ERROR_NONE) {
+                       g_free(info->hostname);
+                       free(info);
+                       return DBUS_HANDLER_RESULT_HANDLED;
+               }
+
+               _get_station_count((gconstpointer)type,
+                               _slist_find_station_by_interface, &n_station);
+               if (n_station == 1)
+                       _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;
+               }
+               SDBG("DhcpLeaseDeleted signal : %s %s %s\n", ip_addr, mac, name);
+
+               _remove_station_info(ip_addr, _slist_find_station_by_ip_addr);
+
+               return DBUS_HANDLER_RESULT_HANDLED;
+       }
+
+       return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
+}
+
+int main(int argc, char **argv)
+{
+       const char *rule = "type='signal',interface='"DNSMASQ_DBUS_INTERFACE"'";
+
+       TetheringObject *tethering_obj = NULL;
+       DBusGConnection *tethering_bus = NULL;
+       DBusGProxy *tethering_bus_proxy = NULL;
+       DBusError dbus_error;
+       GError *error = NULL;
+       guint result = 0;
+       int ret;
+
+       DBG("+\n");
+
+#if !GLIB_CHECK_VERSION(2,36,0)
+       g_type_init();
+#endif
+
+       mainloop = g_main_loop_new(NULL, FALSE);
+       if (mainloop == NULL) {
+               ERR("Couldn't create GMainLoop\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;
+       }
+       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");
+       }
+
+       if (vconf_get_int(VCONFKEY_MOBILE_HOTSPOT_MODE, &mobileap_state) < 0) {
+               ERR("vconf_get_int 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_initialize() is failed : %d\n", ret);
+       }
+
+       ret = alarmmgr_init(APPNAME);
+       if (ret != ALARMMGR_RESULT_SUCCESS) {
+               ERR("alarmmgr_init(%s) is failed : %d\n", APPNAME, ret);
+       } else {
+               ret = alarmmgr_set_cb(_sp_timeout_handler, NULL);
+               if (ret != ALARMMGR_RESULT_SUCCESS) {
+                       ERR("alarmmgr_set_cb is failed : %d\n", ret);
+               }
+       }
+
+       g_main_loop_run(mainloop);
+
+       alarmmgr_fini();
+
+       ret = wifi_deinitialize();
+       if (ret != WIFI_ERROR_NONE) {
+               ERR("wifi_deinitialize() is failed : %d\n", ret);
+       }
+
+       _unregister_vconf_cb((void *)tethering_obj);
+       _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_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);
+
+       DBG("-\n");
+       return 0;
+}
diff --git a/src/mobileap_network.c b/src/mobileap_network.c
new file mode 100644 (file)
index 0000000..337faab
--- /dev/null
@@ -0,0 +1,1195 @@
+/*
+ * mobileap-agent
+ * Copyright (c) 2012 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the License);
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <stdio.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <glib.h>
+#include <string.h>
+#include <net_connection.h>
+
+#include "mobileap_softap.h"
+#include "mobileap_common.h"
+#include "mobileap_network.h"
+#include "mobileap_wifi.h"
+#include "mobileap_bluetooth.h"
+#include "mobileap_usb.h"
+#include "mobileap_iptables.h"
+
+typedef enum {
+       __NO_SERVICE,
+       __INTERNET,
+       __TETHERING_ONLY
+} tethering_cellular_service_type_e;
+
+typedef struct {
+       connection_profile_h handle;
+       tethering_cellular_service_type_e svc_type;
+} tethering_cellular_profile_s;
+
+#define        MH_PORT_FORWARD_CONF_FILEPATH   "/tmp/mobileap_agent_port_forward_info"
+#define        MH_MAX_PORT_FORWARD_RULE_LEN    64      /* interface(10) protocol(10) ip(15):port(5) ip(15):port(5) */
+#define        MH_MAX_NO_OF_PORT_FORWARD_RULE  64
+
+typedef struct {
+       char *input_interface;
+       char *proto;
+       char *org_dest_ip;
+       unsigned short org_dest_port;
+       char *new_dest_ip;
+       unsigned short new_dest_port;
+} port_forward_info_s;
+
+static TetheringObject *obj = NULL;
+static connection_h connection = NULL;
+static tethering_cellular_profile_s c_prof = {NULL, __NO_SERVICE};
+static guint net_timeout_id;
+static connection_profile_h tethered_prof = NULL;
+static GSList *port_forward_info = NULL;
+
+
+static gboolean __try_to_open_tethering_profile(gpointer user_data);
+
+static mobile_ap_error_code_e __get_conn_error(int conn_error)
+{
+       mobile_ap_error_code_e err = MOBILE_AP_ERROR_NONE;
+
+       switch (conn_error) {
+       case CONNECTION_ERROR_NONE:
+               err = MOBILE_AP_ERROR_NONE;
+               break;
+
+       case CONNECTION_ERROR_OUT_OF_MEMORY:
+               err = MOBILE_AP_ERROR_RESOURCE;
+               break;
+
+       case CONNECTION_ERROR_INVALID_OPERATION:
+               err = MOBILE_AP_ERROR_INTERNAL;
+               break;
+
+       case CONNECTION_ERROR_INVALID_PARAMETER:
+               err = MOBILE_AP_ERROR_INVALID_PARAM;
+               break;
+
+       case CONNECTION_ERROR_ALREADY_EXISTS:
+               err = MOBILE_AP_ERROR_ALREADY_ENABLED;
+               break;
+
+       case CONNECTION_ERROR_PERMISSION_DENIED:
+               err = MOBILE_AP_ERROR_PERMISSION_DENIED;
+               break;
+
+       case CONNECTION_ERROR_DHCP_FAILED:
+               err = MOBILE_AP_ERROR_DHCP;
+               break;
+
+       case CONNECTION_ERROR_NOW_IN_PROGRESS:
+               err = MOBILE_AP_ERROR_IN_PROGRESS;
+               break;
+
+       default:
+               ERR("Not defined error : %d\n", conn_error);
+               err = MOBILE_AP_ERROR_INTERNAL;
+               break;
+       }
+
+       return err;
+}
+
+static gboolean __is_valid_ipv4_addr(const char *ip)
+{
+       int i;
+       int len;
+       int dot_count = 0;
+       int addr;
+       char tmp_ip[16] = {0, };
+       char *p = tmp_ip;
+
+       if (ip == NULL)
+               return FALSE;
+
+       len = strlen(ip);
+       if (len > 15 /* 255.255.255.255 */ || len < 7 /* 0.0.0.0 */)
+               return FALSE;
+       g_strlcpy(tmp_ip, ip, sizeof(tmp_ip));
+
+       for (i = 0; i <= len; i++) {
+               if (tmp_ip[i] == '.') {
+                       if (++dot_count > 3)
+                               return FALSE;
+                       if (&tmp_ip[i] == p)
+                               return FALSE;
+                       tmp_ip[i] = '\0';
+                       addr = atoi(p);
+                       if (addr < 0 || addr > 255)
+                               return FALSE;
+                       p = &tmp_ip[i + 1];
+               } else if (tmp_ip[i] == '\0') {
+                       if (&tmp_ip[i] == p)
+                               return FALSE;
+                       addr = atoi(p);
+                       if (addr < 0 || addr > 255)
+                               return FALSE;
+                       break;
+               } else if (tmp_ip[i] < '0' || tmp_ip[i] > '9')
+                       return FALSE;
+       }
+
+       if (dot_count != 3)
+               return FALSE;
+
+       return TRUE;
+}
+
+static void __clear_port_forward_info(void)
+{
+       GSList *l;
+       GSList *temp_l;
+       port_forward_info_s *pf;
+
+       for (l = port_forward_info; l; ) {
+               pf = (port_forward_info_s *)l->data;
+               if (pf) {
+                       g_free(pf->new_dest_ip);
+                       g_free(pf->org_dest_ip);
+                       g_free(pf->proto);
+                       g_free(pf->input_interface);
+                       g_free(pf);
+               }
+
+               temp_l = l;
+               l = g_slist_next(l);
+               port_forward_info = g_slist_delete_link(port_forward_info, temp_l);
+       }
+
+       return;
+}
+
+static gboolean __read_port_forward_info(const char *conf_file)
+{
+       if (conf_file == NULL) {
+               ERR("Invalid parameter\n");
+               return FALSE;
+       }
+
+       DBG("+\n");
+
+       FILE *fp;
+       char buf[MH_MAX_PORT_FORWARD_RULE_LEN];
+       port_forward_info_s *pf;
+       int no_of_rule = 0;
+
+       __clear_port_forward_info();
+
+       fp = fopen(conf_file, "r");
+       if (fp == NULL) {
+               ERR("fopen is failed : %s\n", strerror(errno));
+               return FALSE;
+       }
+
+       while (fgets(buf, sizeof(buf), fp)) {
+               int i;
+               char *token;
+               char *saveptr1 = NULL;
+               char *saveptr2 = NULL;
+
+               char *input_interface;
+               char *proto;
+               char *dest_ip[2];
+               char *dest_port[2];
+
+               if (no_of_rule++ >= MH_MAX_NO_OF_PORT_FORWARD_RULE) {
+                       DBG("There are too many rules\n");
+                       break;
+               }
+
+               /* "Input interface" "Protocol" "Original destination IP:Port" "New destination IP:Port" */
+               /* pdp0 udp 10.90.50.38:23 192.168.43.10:23 */
+
+               input_interface = strtok_r(buf, " ", &saveptr1);
+               if (input_interface == NULL) {
+                       SERR("Invalid rule : %s\n", buf);
+                       continue;
+               }
+
+               proto = strtok_r(NULL, " ", &saveptr1);
+               if (proto == NULL) {
+                       SERR("Invalid rule : %s\n", buf);
+                       continue;
+               }
+
+               for (i = 0; i < sizeof(dest_ip) / sizeof(char *); i++) {
+                       token = strtok_r(NULL, " ", &saveptr1);
+                       if (token == NULL) {
+                               SERR("Invalid rule : %s\n", buf);
+                               break;
+                       }
+
+                       dest_ip[i] = strtok_r(token, ":", &saveptr2);
+                       if (dest_ip[i] == NULL ||
+                                       !__is_valid_ipv4_addr(dest_ip[i])) {
+                               SERR("Invalid rule : %s\n", buf);
+                               break;
+                       }
+
+                       dest_port[i] = strtok_r(NULL, ":", &saveptr2);
+                       if (dest_port[i] == NULL) {
+                               SERR("Invalid rule : %s\n", buf);
+                               break;
+                       }
+               }
+
+               if (i < sizeof(dest_ip) / sizeof(char *))
+                       continue;
+
+               pf = (port_forward_info_s *)malloc(sizeof(port_forward_info_s));
+               if (pf == NULL)
+                       break;
+
+               pf->input_interface = g_strdup(input_interface);
+               pf->proto = g_strdup(proto);
+               pf->org_dest_ip = g_strdup(dest_ip[0]);
+               pf->org_dest_port = (unsigned short)atoi(dest_port[0]);
+               pf->new_dest_ip = g_strdup(dest_ip[1]);
+               pf->new_dest_port = (unsigned short)atoi(dest_port[1]);
+               port_forward_info = g_slist_append(port_forward_info, pf);
+
+               SDBG("Port forward rule #%d : %s %s %s:%d %s:%d\n", no_of_rule,
+                               pf->input_interface, pf->proto,
+                               pf->org_dest_ip, pf->org_dest_port,
+                               pf->new_dest_ip, pf->new_dest_port);
+       }
+
+       fclose(fp);
+
+       return TRUE;
+}
+
+static gboolean __is_valid_port_forward_info(port_forward_info_s *pf)
+{
+       if (pf == NULL)
+               return FALSE;
+
+       if (!pf->input_interface || !pf->proto ||
+                       !pf->org_dest_ip || !pf->new_dest_ip)
+               return FALSE;
+
+       if (!strlen(pf->input_interface) || !strlen(pf->proto) ||
+                       !strlen(pf->org_dest_ip) || !strlen(pf->new_dest_ip))
+               return FALSE;
+
+       return TRUE;
+}
+
+static void __print_cellular_profile(void)
+{
+       int ret = 0;
+       char *apn = NULL;
+       char *home_url = NULL;
+       bool roaming = false;
+       connection_cellular_service_type_e service_type;
+
+       if (c_prof.handle == NULL)
+               return;
+
+       ret = connection_profile_get_cellular_service_type(c_prof.handle, &service_type);
+       if (ret != CONNECTION_ERROR_NONE)
+               ERR("connection API fail: 0x%X\n", ret);
+       else
+               SDBG("Service type: %d\n", service_type);
+
+       ret = connection_profile_get_cellular_apn(c_prof.handle, &apn);
+       if (ret != CONNECTION_ERROR_NONE)
+               ERR("connection API fail: 0x%X\n", ret);
+       else {
+               SDBG("APN: %s\n", apn);
+               g_free(apn);
+       }
+
+       ret = connection_profile_get_cellular_home_url(c_prof.handle, &home_url);
+       if (ret != CONNECTION_ERROR_NONE)
+               ERR("connection API fail: 0x%X\n", ret);
+       else {
+               SDBG("Home url: %s\n", home_url);
+               g_free(home_url);
+       }
+
+       ret = connection_profile_is_cellular_roaming(c_prof.handle, &roaming);
+       if (ret != CONNECTION_ERROR_NONE)
+               ERR("connection API fail: 0x%X\n", ret);
+       else
+               SDBG("Roaming: %d\n", roaming);
+}
+
+static void __handle_open_network_error(void)
+{
+       int ret = MOBILE_AP_ERROR_NONE;
+
+       if (_mobileap_is_disabled()) {
+               return;
+       }
+
+       ret = _disable_wifi_tethering(obj);
+       DBG("_disable_wifi_tethering returns %d\n", ret);
+
+       ret = _disable_bt_tethering(obj);
+       DBG("_disable_bt_tethering returns %d\n", ret);
+
+       ret = _disable_usb_tethering(obj);
+       DBG("_disable_usb_tethering returns %d\n", ret);
+
+       _emit_mobileap_dbus_signal(obj, E_SIGNAL_NET_CLOSED, NULL);
+
+       return;
+}
+
+static gboolean __is_equal_profile(connection_profile_h a, connection_profile_h b)
+{
+       char *a_id = NULL;
+       char *b_id = NULL;
+       int ret;
+
+       ret = connection_profile_get_id(a, &a_id);
+       if (ret != CONNECTION_ERROR_NONE || a_id == NULL) {
+               ERR("connection_profile_get_id is failed [0x%X]\n", ret);
+               return FALSE;
+       }
+
+       ret = connection_profile_get_id(b, &b_id);
+       if (ret != CONNECTION_ERROR_NONE || b_id == NULL) {
+               ERR("connection_profile_get_id is failed [0x%X]\n", ret);
+               g_free(a_id);
+               return FALSE;
+       }
+
+       ret = g_strcmp0(a_id, b_id);
+       g_free(a_id);
+       g_free(b_id);
+
+       return (ret == 0) ? TRUE : FALSE;
+}
+
+static gboolean __is_connected_profile(connection_profile_h profile)
+{
+       if (profile == NULL) {
+               ERR("profile is NULL\n");
+               return FALSE;
+       }
+
+       int ret;
+       connection_profile_state_e pstat = CONNECTION_PROFILE_STATE_DISCONNECTED;
+
+       ret = connection_profile_get_state(profile, &pstat);
+       if (ret != CONNECTION_ERROR_NONE) {
+               ERR("connection_profile_get_state is failed: 0x%X\n", ret);
+               return FALSE;
+       }
+
+       if (pstat != CONNECTION_PROFILE_STATE_CONNECTED) {
+               return FALSE;
+       }
+
+       DBG("Profile is connected\n");
+       return TRUE;
+}
+
+static void __connection_type_changed_cb(connection_type_e type, void *user_data)
+{
+       DBG("Changed connection type is [%s]\n",
+                       type == CONNECTION_TYPE_DISCONNECTED ? "DISCONNECTED" :
+                       type == CONNECTION_TYPE_WIFI ? "Wi-Fi" :
+                       type == CONNECTION_TYPE_CELLULAR ? "Cellular" :
+                       type == CONNECTION_TYPE_ETHERNET ? "Ethernet" :
+                       "Unknown");
+
+       if (_mobileap_is_disabled()) {
+               DBG("Tethering is disabled\n");
+               return;
+       }
+
+       if (_open_network() != MOBILE_AP_ERROR_NONE) {
+               ERR("_open_network() is failed\n");
+               __handle_open_network_error();
+       }
+
+       return;
+}
+
+void __cellular_state_changed_cb(keynode_t *node, void *user_data)
+{
+       if (node == NULL) {
+               ERR("Invalid parameter\n");
+               return;
+       }
+
+       if (vconf_keynode_get_type(node) != VCONF_TYPE_INT) {
+               ERR("Invalid vconf key type\n");
+               return;
+       }
+
+       int ret;
+       int cellular_state;
+       connection_type_e net_type;
+
+       cellular_state = vconf_keynode_get_int(node);
+       SDBG("key = %s, value = %d(int)\n",
+                       vconf_keynode_get_name(node), cellular_state);
+
+       if (_mobileap_is_disabled())
+               return;
+
+       if (cellular_state != VCONFKEY_NETWORK_CELLULAR_ON)
+               return;
+
+       ret = connection_get_type(connection, &net_type);
+       if (ret != CONNECTION_ERROR_NONE) {
+               ERR("connection_get_type is failed [0x%X]\n", ret);
+               return;
+       }
+
+       if (net_type != CONNECTION_TYPE_DISCONNECTED &&
+                       net_type != CONNECTION_TYPE_CELLULAR)
+               return;
+
+       if (tethered_prof)
+               return;
+
+       DBG("VCONFKEY_NETWORK_CELLULAR_ON\n");
+       if (_open_network() != MOBILE_AP_ERROR_NONE) {
+               ERR("_open_network() is failed\n");
+               __handle_open_network_error();
+       }
+
+       return;
+}
+
+static void __profile_state_changed_cb(connection_profile_state_e state, void *user_data)
+{
+       if (c_prof.handle == NULL || c_prof.svc_type == __NO_SERVICE) {
+               ERR("There is no proper profile\n");
+               return;
+       }
+
+       DBG("Tethering cellular profile is %s\n",
+                       state == CONNECTION_PROFILE_STATE_DISCONNECTED ? "Disconnected" :
+                       state == CONNECTION_PROFILE_STATE_ASSOCIATION ? "Associated" :
+                       state == CONNECTION_PROFILE_STATE_CONFIGURATION ? "Configured" :
+                       state == CONNECTION_PROFILE_STATE_CONNECTED ? "Connected" :
+                       "Unknown");
+
+       int ret;
+       int cellular_state;
+
+       connection_profile_refresh(c_prof.handle);
+
+       if (_mobileap_is_disabled())
+               return;
+
+       if (c_prof.svc_type != __TETHERING_ONLY)
+               return;
+
+       if (tethered_prof) {
+               if (!__is_equal_profile(tethered_prof, c_prof.handle))
+                       return;
+               connection_profile_refresh(tethered_prof);
+       }
+
+       if (state != CONNECTION_PROFILE_STATE_DISCONNECTED)
+               return;
+
+       DBG("Cellular profile is disconnected\n");
+       _close_network();
+
+       ret = vconf_get_int(VCONFKEY_NETWORK_CELLULAR_STATE, &cellular_state);
+       if (ret < 0) {
+               ERR("vconf_get_int is failed : %d\n", ret);
+               if (vconf_ignore_key_changed(VCONFKEY_NETWORK_CELLULAR_STATE,
+                                       __cellular_state_changed_cb) < 0) {
+                       ERR("vconf_ignore_key_changed is failed\n");
+               }
+               return;
+       }
+
+       if (cellular_state != VCONFKEY_NETWORK_CELLULAR_ON)
+               return;
+
+       if (_open_network() != MOBILE_AP_ERROR_NONE) {
+               ERR("_open_network() is failed\n");
+               __handle_open_network_error();
+       }
+
+       DBG("-\n");
+       return;
+}
+
+static void __update_tethering_cellular_profile(void)
+{
+       int ret;
+       connection_profile_h profile;
+       tethering_cellular_service_type_e svc_type;
+
+       ret = connection_get_default_cellular_service_profile(connection,
+                       CONNECTION_CELLULAR_SERVICE_TYPE_TETHERING, &profile);
+       if (ret == CONNECTION_ERROR_NONE) {
+               svc_type = __TETHERING_ONLY;
+               goto DONE;
+       }
+       DBG("There is no tethering profile\n");
+
+       ret = connection_get_default_cellular_service_profile(connection,
+                       CONNECTION_CELLULAR_SERVICE_TYPE_INTERNET, &profile);
+       if (ret == CONNECTION_ERROR_NONE) {
+               svc_type = __INTERNET;
+               goto DONE;
+       }
+       ERR("Getting default connection for internet is failed\n");
+       /* To-Do : Need to consider prepaid internet profile */
+
+       if (c_prof.handle) {
+               connection_profile_unset_state_changed_cb(c_prof.handle);
+               connection_profile_destroy(c_prof.handle);
+               c_prof.handle = NULL;
+               c_prof.svc_type = __NO_SERVICE;
+       }
+       return;
+
+DONE:
+       if (c_prof.handle == NULL ||
+                       !__is_equal_profile(c_prof.handle, profile)) {
+               if (c_prof.handle) {
+                       DBG("Tethering cellular profile is updated\n");
+                       connection_profile_unset_state_changed_cb(c_prof.handle);
+                       connection_profile_destroy(c_prof.handle);
+               }
+
+               c_prof.handle = profile;
+               c_prof.svc_type = svc_type;
+               connection_profile_set_state_changed_cb(c_prof.handle,
+                               __profile_state_changed_cb, NULL);
+       } else {
+               connection_profile_destroy(profile);
+               connection_profile_refresh(c_prof.handle);
+       }
+
+       return;
+}
+
+static void __profile_closed_cb(connection_error_e result, void *user_data)
+{
+       connection_profile_refresh(c_prof.handle);
+
+       if (result != CONNECTION_ERROR_NONE) {
+               ERR("Unable to close profile [0x%X]", result);
+       } else {
+               DBG("Tethering profile is closed");
+       }
+
+       return;
+}
+
+static gboolean __close_tethering_profile(void)
+{
+       if (c_prof.handle == NULL || c_prof.svc_type == __NO_SERVICE) {
+               ERR("There is no proper cellular profile\n");
+               return FALSE;
+       }
+
+       int ret;
+       connection_profile_state_e state;
+
+       DBG("+\n");
+
+       if (net_timeout_id) {
+               g_source_remove(net_timeout_id);
+               net_timeout_id = 0;
+       }
+
+       if (c_prof.svc_type == __INTERNET) {
+               __profile_closed_cb(CONNECTION_ERROR_NONE, NULL);
+               return TRUE;
+       }
+
+       ret = connection_profile_get_state(c_prof.handle, &state);
+       if (ret != CONNECTION_ERROR_NONE) {
+               ERR("connection_profile_get_state is failed [0x%X]\n", ret);
+               return FALSE;
+       }
+
+       if (state == CONNECTION_PROFILE_STATE_DISCONNECTED) {
+               DBG("Already disconnected profile\n");
+               return TRUE;
+       }
+
+       ret = connection_close_profile(connection, c_prof.handle,
+                       __profile_closed_cb, NULL);
+       if (ret != CONNECTION_ERROR_NONE) {
+               ERR("Connection close Failed!!\n");
+               return FALSE;
+       }
+
+       DBG("-\n");
+       return TRUE;
+}
+
+static void __profile_opened_cb(connection_error_e result, void *user_data)
+{
+       if (c_prof.handle == NULL || c_prof.svc_type == __NO_SERVICE) {
+               ERR("There is no proper profile\n");
+               return;
+       }
+
+       int ret;
+       connection_type_e net_type;
+
+       DBG("+\n");
+
+       connection_profile_refresh(c_prof.handle);
+
+       if (_mobileap_is_disabled()) {
+               __close_tethering_profile();
+               return;
+       }
+
+       if (result == CONNECTION_ERROR_OPERATION_ABORTED) {
+               DBG("connection_open_profile is cancelled\n");
+               return;
+       }
+
+       /* Check opened and retry context */
+       ret = connection_get_type(connection, &net_type);
+       if (ret != CONNECTION_ERROR_NONE) {
+               ERR("connection_get_type is failed\n");
+               __close_tethering_profile();
+               return;
+       }
+
+       if (net_type != CONNECTION_TYPE_DISCONNECTED &&
+                       net_type != CONNECTION_TYPE_CELLULAR) {
+               DBG("Connection type is changed\n");
+               __close_tethering_profile();
+               return;
+       }
+
+       if (tethered_prof) {
+               connection_profile_refresh(tethered_prof);
+               return;
+       }
+       /* End of check */
+
+       if (result != CONNECTION_ERROR_ALREADY_EXISTS &&
+                       result != CONNECTION_ERROR_NONE) {
+               DBG("Retry to open profile [0x%X]\n", result);
+               if (net_timeout_id) {
+                       g_source_remove(net_timeout_id);
+                       net_timeout_id = 0;
+               }
+               net_timeout_id = g_timeout_add(TETHERING_NET_OPEN_RETRY_INTERVAL,
+                               __try_to_open_tethering_profile,
+                               NULL);
+               return;
+       }
+
+       DBG("Tethering profile is opened");
+
+       __print_cellular_profile();
+
+       connection_profile_clone(&tethered_prof, c_prof.handle);
+       _set_masquerade();
+       _add_default_router();
+       _add_port_forward_rule();
+
+       DBG("-\n");
+
+       return;
+}
+
+static gboolean __open_tethering_profile(void)
+{
+       if (c_prof.handle == NULL || c_prof.svc_type == __NO_SERVICE) {
+               ERR("There is no proper cellular profile\n");
+               return FALSE;
+       }
+
+       int ret;
+
+       DBG("+\n");
+
+       if (c_prof.svc_type == __INTERNET) {
+               return TRUE;
+       }
+
+       if (__is_connected_profile(c_prof.handle)) {
+               DBG("Already connected profile\n");
+               return TRUE;
+       }
+
+       ret = connection_open_profile(connection, c_prof.handle,
+                       __profile_opened_cb, NULL);
+       if (ret != CONNECTION_ERROR_NONE) {
+               ERR("Unable to open profile [0x%X]", ret);
+               return FALSE;
+       }
+
+       DBG("-\n");
+       return TRUE;
+}
+
+static gboolean __try_to_open_tethering_profile(gpointer user_data)
+{
+       DBG("+\n");
+
+       if (_mobileap_is_disabled()) {
+               DBG("Tethering is disabled\n");
+               net_timeout_id = 0;
+               return FALSE;
+       }
+
+       if (__open_tethering_profile() == FALSE)
+               return TRUE;
+
+       net_timeout_id = 0;
+       return FALSE;
+}
+
+gboolean _is_trying_network_operation(void)
+{
+       if (net_timeout_id)
+               return TRUE;
+
+       return FALSE;
+}
+
+gboolean _get_network_interface_name(char **if_name)
+{
+       if (if_name == NULL) {
+               ERR("if_name is NULL\n");
+               return FALSE;
+       }
+
+       if (tethered_prof == NULL) {
+               return FALSE;
+       }
+
+       int ret = 0;
+
+       connection_profile_refresh(tethered_prof);
+
+       ret = connection_profile_get_network_interface_name(tethered_prof, if_name);
+       if (ret != CONNECTION_ERROR_NONE) {
+               ERR("connection_profile_get_network_interface_name is failed : 0x%X\n", ret);
+               return FALSE;
+       }
+
+       if (strlen(*if_name) == 0) {
+               ERR("if_name is zero length\n");
+               free(*if_name);
+               return FALSE;
+       }
+
+       return TRUE;
+}
+
+gboolean _get_network_gateway_address(char **ip)
+{
+       if (ip == NULL) {
+               ERR("ip is NULL\n");
+               return FALSE;
+       }
+
+       if (tethered_prof == NULL) {
+               return FALSE;
+       }
+
+       int ret = 0;
+
+       connection_profile_refresh(tethered_prof);
+
+       ret = connection_profile_get_gateway_address(tethered_prof,
+                       CONNECTION_ADDRESS_FAMILY_IPV4, ip);
+       if (ret != CONNECTION_ERROR_NONE) {
+               ERR("connection_profile_get_ip_address is failed : 0x%X\n", ret);
+               return FALSE;
+       }
+
+       return TRUE;
+}
+
+gboolean _set_masquerade(void)
+{
+       char *if_name = NULL;
+
+       if (_get_network_interface_name(&if_name) == FALSE) {
+               ERR("_get_network_interface_name is failed\n");
+               return FALSE;
+       }
+       SDBG("Network interface : %s\n", if_name);
+
+       _mh_core_enable_masquerade(if_name);
+       free(if_name);
+
+       return TRUE;
+}
+
+gboolean _unset_masquerade(void)
+{
+       if (tethered_prof == NULL) {
+               DBG("There is nothing to unset masquerading\n");
+               return TRUE;
+       }
+
+       char *if_name = NULL;
+
+       if (_get_network_interface_name(&if_name) == FALSE) {
+               ERR("_get_network_interface_name is failed\n");
+               return FALSE;
+       }
+       SDBG("Network interface : %s\n", if_name);
+
+       _mh_core_disable_masquerade(if_name);
+       free(if_name);
+
+       return TRUE;
+}
+
+gboolean _add_default_router(void)
+{
+       if (tethered_prof == NULL) {
+               DBG("There is no network\n");
+               return TRUE;
+       }
+
+       char cmd[MAX_BUF_SIZE] = {0, };
+       char *ip = NULL;
+       char *interface = NULL;
+
+       if (_get_network_gateway_address(&ip) == FALSE) {
+               return FALSE;
+       }
+
+       if (_get_network_interface_name(&interface) == FALSE) {
+               free(ip);
+               return FALSE;
+       }
+
+       snprintf(cmd, sizeof(cmd), "%s route replace "DEFAULT_ROUTER,
+                       IP_CMD, ip, interface, TETHERING_ROUTING_TABLE);
+       free(interface);
+       free(ip);
+
+       if (_execute_command(cmd)) {
+               ERR("%s is failed\n", cmd);
+               return FALSE;
+       }
+
+       return TRUE;
+}
+
+gboolean _del_default_router(void)
+{
+       if (tethered_prof == NULL) {
+               DBG("There is no network\n");
+               return TRUE;
+       }
+
+       char cmd[MAX_BUF_SIZE] = {0, };
+       char *ip = NULL;
+       char *interface = NULL;
+
+       if (_get_network_gateway_address(&ip) == FALSE) {
+               return FALSE;
+       }
+
+       if (_get_network_interface_name(&interface) == FALSE) {
+               free(ip);
+               return FALSE;
+       }
+
+       snprintf(cmd, sizeof(cmd), "%s route del "DEFAULT_ROUTER,
+                       IP_CMD, ip, interface, TETHERING_ROUTING_TABLE);
+       free(interface);
+       free(ip);
+
+       if (_execute_command(cmd)) {
+               ERR("%s is failed\n", cmd);
+               return FALSE;
+       }
+
+       return TRUE;
+}
+
+void _add_port_forward_rule(void)
+{
+       DBG("+\n");
+
+       GSList *l;
+       port_forward_info_s *pf;
+
+       if (access(MH_PORT_FORWARD_CONF_FILEPATH, F_OK) < 0) {
+               return;
+       }
+
+       if (__read_port_forward_info(MH_PORT_FORWARD_CONF_FILEPATH) == FALSE) {
+               ERR("__read_port_forward_info() is failed\n");
+               return;
+       }
+
+       _iptables_create_chain(TABLE_NAT, TETH_NAT_PRE);
+       _iptables_add_rule(PKT_REDIRECTION_RULE, TABLE_NAT, CHAIN_PRE,
+               TETH_NAT_PRE);
+
+       for (l = port_forward_info; l; l = g_slist_next(l)) {
+               pf = (port_forward_info_s *)l->data;
+
+               if (__is_valid_port_forward_info(pf) == FALSE)
+                       continue;
+
+               _iptables_add_rule(PORT_FW_RULE, TABLE_NAT, TETH_NAT_PRE,
+                       pf->input_interface, pf->proto, pf->org_dest_ip,
+                       pf->new_dest_ip, (int)pf->org_dest_port, (int)pf->new_dest_port);
+       }
+
+       return;
+}
+
+void _del_port_forward_rule(void)
+{
+       GSList *l;
+       GSList *temp_l;
+       port_forward_info_s *pf;
+
+       DBG("+\n");
+
+       if (port_forward_info == NULL) {
+               DBG("port forwarding rules were not applied, no need to deleted\n");
+               return;
+       }
+
+       for(l = port_forward_info; l;) {
+               pf = (port_forward_info_s *)l->data;
+               if (pf) {
+                       g_free(pf->new_dest_ip);
+                       g_free(pf->org_dest_ip);
+                       g_free(pf->proto);
+                       g_free(pf->input_interface);
+                       g_free(pf);
+               }
+
+               temp_l = l;
+               l = g_slist_next(l);
+               port_forward_info = g_slist_delete_link(port_forward_info,
+                                       temp_l);
+       }
+
+       _iptables_delete_rule(PKT_REDIRECTION_RULE, TABLE_NAT, CHAIN_PRE,
+               TETH_NAT_PRE);
+       _iptables_flush_rules(TABLE_NAT, TETH_NAT_PRE);
+       _iptables_delete_chain(TABLE_NAT, TETH_NAT_PRE);
+
+       return;
+}
+
+int _open_network(void)
+{
+       DBG("+\n");
+
+       int ret;
+       int con_ret;
+       int cellular_state;
+       connection_type_e net_type;
+
+       ret = connection_get_type(connection, &net_type);
+       if (ret != CONNECTION_ERROR_NONE) {
+               ERR("connection_get_type is failed\n");
+               con_ret = __get_conn_error(ret);
+               return con_ret;
+       }
+
+       if (vconf_get_int(VCONFKEY_NETWORK_CELLULAR_STATE, &cellular_state) < 0) {
+               ERR("vconf_get_int is failed\n");
+               return MOBILE_AP_ERROR_INTERNAL;
+       }
+
+       DBG("Connection type : %d, Cellular State : %d\n",
+                       net_type, cellular_state);
+
+       if (tethered_prof) {
+               if (net_type == CONNECTION_TYPE_CELLULAR) {
+                       __update_tethering_cellular_profile();
+                       if (__is_equal_profile(tethered_prof, c_prof.handle)) {
+                               DBG("Cellular profile is already configured\n");
+                               return MOBILE_AP_ERROR_NONE;
+                       }
+               }
+
+               DBG("There is already tethered profile\n");
+               _close_network();
+       }
+
+       if (net_type == CONNECTION_TYPE_DISCONNECTED &&
+                       cellular_state != VCONFKEY_NETWORK_CELLULAR_ON) {
+               DBG("There is no network\n");
+               /* Callback will handle this once Network type is changed */
+               return MOBILE_AP_ERROR_NONE;
+       }
+
+       switch (net_type) {
+       case CONNECTION_TYPE_DISCONNECTED:
+       case CONNECTION_TYPE_CELLULAR:
+               __update_tethering_cellular_profile();
+               if (c_prof.handle == NULL || c_prof.svc_type == __NO_SERVICE) {
+                       DBG("There is no proper cellular profile for tethering\n");
+                       return MOBILE_AP_ERROR_NONE;
+               }
+               __print_cellular_profile();
+
+               if (!__is_connected_profile(c_prof.handle)) {
+                       if (c_prof.svc_type != __TETHERING_ONLY) {
+                               return MOBILE_AP_ERROR_NONE;
+                       }
+
+                       if (net_timeout_id) {
+                               g_source_remove(net_timeout_id);
+                               net_timeout_id = 0;
+                       }
+                       net_timeout_id = g_timeout_add(TETHERING_NET_OPEN_RETRY_INTERVAL,
+                                       __try_to_open_tethering_profile, NULL);
+
+                       return MOBILE_AP_ERROR_NONE;
+               }
+               connection_profile_clone(&tethered_prof, c_prof.handle);
+               break;
+
+       case CONNECTION_TYPE_WIFI:
+       case CONNECTION_TYPE_ETHERNET:
+       case CONNECTION_TYPE_BT:
+               ret = connection_get_current_profile(connection, &tethered_prof);
+               if (ret != CONNECTION_ERROR_NONE) {
+                       ERR("connection_get_current_profile is failed [0x%X]\n", ret);
+                       con_ret = __get_conn_error(ret);
+                       return con_ret;
+               }
+               break;
+
+       default:
+               ERR("Unknown connection type : %d\n", net_type);
+               return MOBILE_AP_ERROR_INTERNAL;
+       }
+
+       _set_masquerade();
+       _add_default_router();
+       _add_port_forward_rule();
+
+       DBG("-\n");
+
+       return MOBILE_AP_ERROR_NONE;
+}
+
+void _close_network(void)
+{
+       if (tethered_prof == NULL) {
+               DBG("There is no tethered profile\n");
+               return;
+       }
+
+       DBG("+\n");
+
+       _del_port_forward_rule();
+       _del_default_router();
+       _unset_masquerade();
+
+       connection_profile_destroy(tethered_prof);
+       tethered_prof = NULL;
+       __close_tethering_profile();
+
+       DBG("-\n");
+       return;
+}
+
+gboolean _init_network(void *user_data)
+{
+       if (user_data == NULL) {
+               ERR("Invalid parameter\n");
+               return FALSE;
+       }
+
+       int ret;
+
+       obj = (TetheringObject *)user_data;
+
+       ret = connection_create(&connection);
+       if (ret != CONNECTION_ERROR_NONE) {
+               ERR("connection_create is failed : 0x%X\n", ret);
+               goto FAIL;
+       }
+
+       ret = connection_set_type_changed_cb(connection,
+                       __connection_type_changed_cb, user_data);
+       if (ret != CONNECTION_ERROR_NONE) {
+               ERR("connection_set_type_changed cb is failed : 0x%X\n", ret);
+               goto FAIL;
+       }
+
+       ret = vconf_notify_key_changed(VCONFKEY_NETWORK_CELLULAR_STATE,
+                       __cellular_state_changed_cb, NULL);
+       if (ret < 0) {
+               ERR("vconf_notify_key_changed is failed : %d\n", ret);
+               connection_unset_type_changed_cb(connection);
+               goto FAIL;
+       }
+
+       __update_tethering_cellular_profile();
+
+       return TRUE;
+
+FAIL:
+       if (connection) {
+               connection_destroy(connection);
+               connection = NULL;
+       }
+
+       return FALSE;
+}
+
+gboolean _deinit_network(void)
+{
+       int ret;
+
+       if (connection == NULL) {
+               ERR("Connection handle is not initialized\n");
+               return TRUE;
+       }
+
+       if (c_prof.handle) {
+               vconf_ignore_key_changed(VCONFKEY_NETWORK_CELLULAR_STATE,
+                               __cellular_state_changed_cb);
+               connection_profile_unset_state_changed_cb(c_prof.handle);
+               connection_profile_destroy(c_prof.handle);
+               c_prof.handle = NULL;
+               c_prof.svc_type = __NO_SERVICE;
+       }
+
+       ret = connection_unset_type_changed_cb(connection);
+       if (ret != CONNECTION_ERROR_NONE) {
+               ERR("connection_unset_type_changed_cb is failed : %d\n", ret);
+       }
+
+       connection_destroy(connection);
+       connection = NULL;
+       obj = NULL;
+
+       return TRUE;
+}
diff --git a/src/mobileap_notification.c b/src/mobileap_notification.c
new file mode 100644 (file)
index 0000000..b338570
--- /dev/null
@@ -0,0 +1,474 @@
+/*
+ * mobileap-agent
+ * Copyright (c) 2012 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the License);
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#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 <notification.h>
+#include <bluetooth.h>
+
+#include "mobileap_softap.h"
+#include "mobileap_notification.h"
+
+#define MH_NOTI_LAUNCH_PKGNAME "setting-mobileap-efl"
+#define MH_NOTI_CALLER_PKGNAME "mobileap-agent"
+#define MH_LOCALE_DOMAIN       "ug-setting-mobileap-efl"
+#define MH_LOCALE_DIR "/usr/ug/res/locale"
+
+
+static int connected_noti_id = 0;
+static int timeout_noti_id = 0;
+
+static int __create_status_noti(const char *content)
+{
+       if (content == NULL)
+               return MOBILE_AP_ERROR_INVALID_PARAM;
+
+       notification_error_e ret;
+
+       ret = notification_status_message_post(content);
+       if (ret != NOTIFICATION_ERROR_NONE) {
+               ERR("notification_status_message_post() is failed : %d\n", ret);
+               return MOBILE_AP_ERROR_INTERNAL;
+       }
+
+       return MOBILE_AP_ERROR_NONE;
+}
+
+int _create_timeout_noti(const char *icon_path)
+{
+       DBG("+\n");
+
+       notification_h noti = NULL;
+       notification_error_e ret = NOTIFICATION_ERROR_NONE;
+       char *old_icon_path = NULL;
+       char *general_icon_path = NULL;
+
+       if (timeout_noti_id) {
+               noti = notification_load(MH_NOTI_CALLER_PKGNAME, timeout_noti_id);
+               if (noti == NULL) {
+                       DBG("Notification can be deleted already\n");
+               } else {
+                       ret = notification_get_image(noti,
+                                       NOTIFICATION_IMAGE_TYPE_ICON, &old_icon_path);
+                       if (ret == NOTIFICATION_ERROR_NONE) {
+                               if (g_strcmp0(icon_path, old_icon_path))
+                                       general_icon_path = MH_NOTI_ICON_GENERAL;
+                       }
+
+                       ret = notification_delete(noti);
+                       if (ret != NOTIFICATION_ERROR_NONE) {
+                               ERR("Fail to notification_delete [%d]\n", ret);
+
+                               ret = notification_free(noti);
+                               if (ret != NOTIFICATION_ERROR_NONE)
+                                       ERR("Fail to notification_free [%d]\n", ret);
+                               return MOBILE_AP_ERROR_INTERNAL;
+                       }
+
+                       ret = notification_free(noti);
+                       if (ret != NOTIFICATION_ERROR_NONE) {
+                               ERR("Fail to notification_free [%d]\n", ret);
+                       }
+               }
+
+               timeout_noti_id = 0;
+       }
+
+       noti = notification_create(NOTIFICATION_TYPE_NOTI);
+       if (!noti) {
+               ERR("Fail to notification_create\n");
+               return MOBILE_AP_ERROR_INTERNAL;
+       }
+
+       ret = notification_set_pkgname(noti, MH_NOTI_CALLER_PKGNAME);
+       if (ret != NOTIFICATION_ERROR_NONE) {
+               ERR("Fail to notification_set_pkgname [%d]\n", ret);
+               goto FAIL;
+       }
+
+       ret = notification_set_property(noti,
+                       NOTIFICATION_PROP_VOLATILE_DISPLAY);
+       if (ret != NOTIFICATION_ERROR_NONE) {
+               ERR("Fail to notification_set_property [%d]\n", ret);
+               goto FAIL;
+       }
+
+       ret = notification_set_layout(noti, NOTIFICATION_LY_NOTI_EVENT_SINGLE);
+       if (ret != NOTIFICATION_ERROR_NONE) {
+               ERR("Fail to notification_set_layout [%d]\n", ret);
+               goto FAIL;
+       }
+
+       ret = notification_set_image(noti,
+                       NOTIFICATION_IMAGE_TYPE_ICON, general_icon_path ?
+                       general_icon_path : icon_path);
+       if (ret != NOTIFICATION_ERROR_NONE) {
+               ERR("Fail to notification_set_image [%d]\n", ret);
+               goto FAIL;
+       }
+
+       ret = notification_set_text(noti,
+                       NOTIFICATION_TEXT_TYPE_TITLE, NULL,
+                       MH_STR_CONNECTION_TIMEOUT,
+                       NOTIFICATION_VARIABLE_TYPE_NONE);
+       if (ret != NOTIFICATION_ERROR_NONE) {
+               ERR("Fail to notification_set_text [%d]\n", ret);
+               goto FAIL;
+       }
+
+       ret = notification_set_text(noti,
+                       NOTIFICATION_TEXT_TYPE_CONTENT, NULL,
+                       MH_STR_CONFIGURE_TETHERING,
+                       NOTIFICATION_VARIABLE_TYPE_NONE);
+       if (ret != NOTIFICATION_ERROR_NONE) {
+               ERR("Fail to notification_set_text [%d]\n", ret);
+               goto FAIL;
+       }
+
+       ret = notification_set_text_domain(noti, MH_LOCALE_DOMAIN, MH_LOCALE_DIR);
+       if (ret != NOTIFICATION_ERROR_NONE) {
+               ERR("Fail to notification_set_text_domain [%d]\n", ret);
+               goto FAIL;
+       }
+
+       ret = notification_set_application(noti, MH_NOTI_LAUNCH_PKGNAME);
+       if (ret != NOTIFICATION_ERROR_NONE) {
+               ERR("Fail to notification_set_application [%d]\n", ret);
+               goto FAIL;
+       }
+
+       ret = notification_insert(noti, &timeout_noti_id);
+       if (ret != NOTIFICATION_ERROR_NONE) {
+               ERR("Fail to notification_insert [%d]\n", ret);
+               goto FAIL;
+       }
+
+       ret = notification_free(noti);
+       if (ret != NOTIFICATION_ERROR_NONE) {
+               ERR("Fail to notification_free [%d]\n", ret);
+               goto FAIL;
+       }
+
+       DBG("-\n");
+       return MOBILE_AP_ERROR_NONE;
+
+FAIL:
+       ret = notification_free(noti);
+       if (ret != NOTIFICATION_ERROR_NONE)
+               ERR("Fail to notification_free [%d]\n", ret);
+
+       return MOBILE_AP_ERROR_INTERNAL;
+}
+
+int _delete_timeout_noti(void)
+{
+       notification_error_e ret = NOTIFICATION_ERROR_NONE;
+       notification_list_h noti_list = NULL;
+       notification_list_h l = NULL;
+       notification_h noti = NULL;
+       notification_ly_type_e layout;
+
+       DBG("+\n");
+
+       ret = notification_get_detail_list(MH_NOTI_CALLER_PKGNAME,
+                       NOTIFICATION_GROUP_ID_NONE,
+                       NOTIFICATION_PRIV_ID_NONE,
+                       -1,
+                       &noti_list);
+       if (ret != NOTIFICATION_ERROR_NONE) {
+               ERR("Fail to notification_get_detail_list\n");
+               return MOBILE_AP_ERROR_INTERNAL;
+       }
+
+       if (noti_list == NULL) {
+               return MOBILE_AP_ERROR_NONE;
+       }
+
+       for (l = noti_list; l; l = notification_list_get_next(l)) {
+               noti = notification_list_get_data(l);
+               if (noti == NULL)
+                       break;
+
+               ret = notification_get_layout(noti, &layout);
+               if (ret == NOTIFICATION_ERROR_NONE &&
+                               layout == NOTIFICATION_LY_NOTI_EVENT_SINGLE) {
+                       DBG("Found timeout noti\n");
+                       notification_delete(noti);
+               }
+       }
+
+       notification_free_list(noti_list);
+
+       DBG("-\n");
+
+       return MOBILE_AP_ERROR_NONE;
+}
+
+int _create_connected_noti(int count, const char *icon_path)
+{
+       DBG("+\n");
+       notification_h noti = NULL;
+       notification_error_e ret = NOTIFICATION_ERROR_NONE;
+       bundle *b = NULL;
+
+       noti = notification_create(NOTIFICATION_TYPE_ONGOING);
+       if (!noti) {
+               ERR("Fail to notification_create\n");
+               return MOBILE_AP_ERROR_INTERNAL;
+       }
+
+       ret = notification_set_pkgname(noti, MH_NOTI_CALLER_PKGNAME);
+       if (ret != NOTIFICATION_ERROR_NONE) {
+               ERR("Fail to notification_set_pkgname [%d]\n", ret);
+               goto FAIL;
+       }
+
+       ret = notification_set_property(noti,
+                       NOTIFICATION_PROP_DISABLE_AUTO_DELETE |
+                       NOTIFICATION_PROP_VOLATILE_DISPLAY);
+       if (ret != NOTIFICATION_ERROR_NONE) {
+               ERR("Fail to notification_set_property [%d]\n", ret);
+               goto FAIL;
+       }
+
+       b = bundle_create();
+       bundle_add(b, "caller", "notification");
+
+       appsvc_set_pkgname(b, "setting-mobileap-efl");
+
+       ret = notification_set_execute_option(noti,
+                       NOTIFICATION_EXECUTE_TYPE_SINGLE_LAUNCH, "Launch", NULL, b);
+       if (ret != NOTIFICATION_ERROR_NONE) {
+               ERR("Failed to notification_set_execute_option");
+               goto FAIL;
+       }
+
+       ERR("Successfully added notification");
+
+       ret = notification_set_layout(noti, NOTIFICATION_LY_ONGOING_EVENT);
+       if (ret != NOTIFICATION_ERROR_NONE) {
+               ERR("Fail to notification_set_image [%d]\n", ret);
+               goto FAIL;
+       }
+
+       ret = notification_set_image(noti, NOTIFICATION_IMAGE_TYPE_ICON, icon_path);
+       if (ret != NOTIFICATION_ERROR_NONE) {
+               ERR("Fail to notification_set_image [%d]\n", ret);
+               goto FAIL;
+       }
+
+       ret = notification_set_text(noti,
+                       NOTIFICATION_TEXT_TYPE_TITLE, NULL,
+                       MH_STR_TETHERING,
+                       NOTIFICATION_VARIABLE_TYPE_NONE);
+       if (ret != NOTIFICATION_ERROR_NONE) {
+               ERR("Fail to notification_set_text [%d]\n", ret);
+               goto FAIL;
+       }
+
+       ret = notification_set_text(noti,
+                       NOTIFICATION_TEXT_TYPE_CONTENT, NULL,
+                       MH_STR_CONNECTED_DEV,
+                       NOTIFICATION_VARIABLE_TYPE_INT, count,
+                       NOTIFICATION_VARIABLE_TYPE_NONE);
+       if (ret != NOTIFICATION_ERROR_NONE) {
+               ERR("Fail to notification_set_text [%d]\n", ret);
+               goto FAIL;
+       }
+
+       ret = notification_set_text_domain(noti, MH_LOCALE_DOMAIN, MH_LOCALE_DIR);
+       if (ret != NOTIFICATION_ERROR_NONE) {
+               ERR("Fail to notification_set_text_domain [%d]\n", ret);
+               goto FAIL;
+       }
+
+       ret = notification_set_application(noti, MH_NOTI_LAUNCH_PKGNAME);
+       if (ret != NOTIFICATION_ERROR_NONE) {
+               ERR("Fail to notification_set_application [%d]\n", ret);
+               goto FAIL;
+       }
+
+       ret = notification_set_display_applist(noti,
+                       NOTIFICATION_DISPLAY_APP_ALL ^ NOTIFICATION_DISPLAY_APP_INDICATOR);
+       if (ret != NOTIFICATION_ERROR_NONE) {
+               ERR("Fail to notification_set_display_applist [%d]\n", ret);
+               goto FAIL;
+       }
+
+       ret = notification_insert(noti, &connected_noti_id);
+       if (ret != NOTIFICATION_ERROR_NONE) {
+               ERR("Fail to notification_insert [%d]\n", ret);
+               goto FAIL;
+       }
+
+       ret = notification_free(noti);
+       if (ret != NOTIFICATION_ERROR_NONE) {
+               ERR("Fail to notification_free [%d]\n", ret);
+               goto FAIL;
+       }
+
+       DBG("-\n");
+       return MOBILE_AP_ERROR_NONE;
+
+FAIL:
+       if (b != NULL)
+               bundle_free(b);
+       ret = notification_free(noti);
+       if (ret != NOTIFICATION_ERROR_NONE)
+               ERR("Fail to notification_free [%d]\n", ret);
+       return MOBILE_AP_ERROR_INTERNAL;
+}
+
+int _update_connected_noti(int count, const char *icon_path)
+{
+       DBG("+\n");
+
+       notification_h noti = NULL;
+       notification_error_e ret = NOTIFICATION_ERROR_NONE;
+
+       noti = notification_load(MH_NOTI_CALLER_PKGNAME, connected_noti_id);
+       if (noti == NULL) {
+               ERR("notification_load is failed\n");
+               return MOBILE_AP_ERROR_INTERNAL;
+       }
+
+       ret = notification_set_image(noti,
+                       NOTIFICATION_IMAGE_TYPE_ICON, icon_path);
+       if (ret != NOTIFICATION_ERROR_NONE) {
+               ERR("Fail to notification_set_image [%d]\n", ret);
+               goto FAIL;
+       }
+
+       ret = notification_set_text(noti,
+                       NOTIFICATION_TEXT_TYPE_CONTENT, NULL,
+                       MH_STR_CONNECTED_DEV,
+                       NOTIFICATION_VARIABLE_TYPE_INT, count,
+                       NOTIFICATION_VARIABLE_TYPE_NONE);
+       if (ret != NOTIFICATION_ERROR_NONE) {
+               ERR("Fail to notification_set_text [%d]\n", ret);
+               goto FAIL;
+       }
+
+       ret = notification_update(noti);
+       if (ret != NOTIFICATION_ERROR_NONE) {
+               ERR("Fail to notification_update [%d]\n", ret);
+               goto FAIL;
+       }
+
+       ret = notification_free(noti);
+       if (ret != NOTIFICATION_ERROR_NONE) {
+               ERR("Fail to notification_free [%d]\n", ret);
+               return MOBILE_AP_ERROR_INTERNAL;
+       }
+
+       DBG("-\n");
+       return MOBILE_AP_ERROR_NONE;
+
+FAIL:
+       ret = notification_free(noti);
+       if (ret != NOTIFICATION_ERROR_NONE) {
+               ERR("Fail to notification_free [%d]\n", ret);
+       }
+
+       return MOBILE_AP_ERROR_INTERNAL;
+}
+
+int _delete_connected_noti(void)
+{
+       DBG("+\n");
+       notification_h noti = NULL;
+       notification_error_e ret;
+
+       noti = notification_load(MH_NOTI_CALLER_PKGNAME, connected_noti_id);
+       if (noti == NULL) {
+               ERR("notification_load is failed\n");
+               connected_noti_id = 0;
+               return MOBILE_AP_ERROR_INTERNAL;
+       }
+       connected_noti_id = 0;
+
+       ret = notification_delete(noti);
+       if (ret != NOTIFICATION_ERROR_NONE) {
+               ERR("Fail to notification_delete [%d]\n", ret);
+
+               ret = notification_free(noti);
+               if (ret != NOTIFICATION_ERROR_NONE)
+                       ERR("Fail to notification_free [%d]\n", ret);
+               return MOBILE_AP_ERROR_INTERNAL;
+       }
+
+       ret = notification_free(noti);
+       if (ret != NOTIFICATION_ERROR_NONE) {
+               ERR("Fail to notification_free [%d]\n", ret);
+               return MOBILE_AP_ERROR_INTERNAL;
+       }
+
+       DBG("-\n");
+       return MOBILE_AP_ERROR_NONE;
+}
+
+void _create_tethering_active_noti(void)
+{
+       int active_count = 0;
+
+       if (_mobileap_is_enabled(MOBILE_AP_STATE_WIFI))
+               active_count++;
+
+       if (_mobileap_is_enabled(MOBILE_AP_STATE_BT))
+               active_count++;
+
+       if (_mobileap_is_enabled(MOBILE_AP_STATE_USB))
+               active_count++;
+
+       if (active_count == 1)
+               __create_status_noti(_("IDS_COM_BODY_TETHERING_ENABLED"));
+
+       return;
+}
+
+void _create_bt_tethering_active_noti(void)
+{
+       int ret;
+       bt_adapter_visibility_mode_e mode = BT_ADAPTER_VISIBILITY_MODE_NON_DISCOVERABLE;
+       int duration;
+       int str_len;
+       char *str1 = NULL;
+       char *str2 = NULL;
+       char *str = NULL;
+
+       if (!_mobileap_is_enabled(MOBILE_AP_STATE_WIFI) &&
+                       !_mobileap_is_enabled(MOBILE_AP_STATE_USB)) {
+               str1 = MH_STR_TETHERING_ACTIVE;
+               __create_status_noti(str1);
+       }
+
+       ret = bt_adapter_get_visibility(&mode, &duration);
+       if (ret != BT_ERROR_NONE) {
+               ERR("bt_adapter_get_visibility is failed 0x[%X]\n", ret);
+       }
+
+       if (mode == BT_ADAPTER_VISIBILITY_MODE_NON_DISCOVERABLE) {
+               str2 = MH_STR_BT_VISIBILITY;
+               __create_status_noti(str2);
+       }
+
+       return;
+}
diff --git a/src/mobileap_softap.c b/src/mobileap_softap.c
new file mode 100755 (executable)
index 0000000..42d65cb
--- /dev/null
@@ -0,0 +1,1523 @@
+/*
+ * mobileap-agent
+ * Copyright (c) 2012 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the License);
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <sys/stat.h>
+#include <sys/un.h>
+#include <errno.h>
+#include <sys/ioctl.h>
+#include <glib.h>
+#include <glib-object.h>
+#include <dbus/dbus-glib.h>
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <ctype.h>
+#include <sys/wait.h>
+#include <sys/types.h>
+#include <fcntl.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <linux/wireless.h>
+#include <openssl/evp.h>
+#include <openssl/sha.h>
+
+#include "mobileap_common.h"
+#include "mobileap_softap.h"
+#include "mobileap_handler.h"
+#include "mobileap_wifi.h"
+#include "mobileap_iptables.h"
+
+#define NETCONFIG_SERVICE                              "net.netconfig"
+#define NETCONFIG_WIFI_INTERFACE               "net.netconfig.wifi"
+#define NETCONFIG_WIFI_PATH                            "/net/netconfig/wifi"
+
+#define NETCONFIG_DBUS_REPLY_TIMEOUT   (10 * 1000)
+
+static pid_t dnsmasq_pid = 0;
+static pid_t hostapd_pid = 0;
+static int hostapd_ctrl_fd = 0;
+static int hostapd_monitor_fd = 0;
+static GIOChannel *hostapd_io_channel = NULL;
+static guint hostapd_io_source = 0;
+GSList *sta_timer_list = NULL;
+
+static gboolean __hostapd_connect_timer_cb(gpointer user_data);
+
+static char *__find_first_caps_char(char *str)
+{
+       if (str == NULL) {
+               ERR("NULL string passes\n");
+               return NULL;
+       }
+
+       while(*str) {
+               if (isupper(*str)) {
+                       return str;
+               }
+               str++;
+       }
+       return NULL;
+}
+
+static int __issue_ioctl(int sock_fd, char *if_name, char *cmd, char *buf)
+{
+       int ret_val = MOBILE_AP_ERROR_NONE;
+       struct iwreq iwr;
+
+       memset(buf, 0, MAX_BUF_SIZE);
+       memset(&iwr, 0, sizeof(iwr));
+
+       /* Configure ioctl parameters */
+       g_strlcpy(iwr.ifr_name, if_name, IFNAMSIZ);
+       g_strlcpy(buf, cmd, MAX_BUF_SIZE);
+       iwr.u.data.pointer = buf;
+       iwr.u.data.length = MAX_BUF_SIZE;
+
+       usleep(DRIVER_DELAY);
+
+       /* Issue ioctl */
+       if ((ioctl(sock_fd, SIOCSIWPRIV, &iwr)) < 0) {
+               ERR("ioctl failed...!!!\n");
+               ret_val = MOBILE_AP_ERROR_INTERNAL;
+       }
+
+       return ret_val;
+}
+
+static int __get_psk_hexascii(const char *pass, const unsigned char *salt,
+               char *psk, unsigned int psk_len)
+{
+       if (pass == NULL || salt == NULL || psk == NULL || psk_len <
+                       (SHA256_DIGEST_LENGTH * 2 + 1)) {
+               ERR("Invalid parameter\n");
+               return MOBILE_AP_ERROR_INVALID_PARAM;
+       }
+
+       int i = 0;
+       int d_16 = 0;
+       int r_16 = 0;
+       unsigned char buf[SHA256_DIGEST_LENGTH] = {0, };
+
+       if (!PKCS5_PBKDF2_HMAC_SHA1(pass, strlen(pass),
+                               salt, strlen((const char *)salt),
+                               PSK_ITERATION_COUNT, sizeof(buf), buf)) {
+               ERR("Getting psk is failed\n");
+               return MOBILE_AP_ERROR_RESOURCE;
+       }
+
+       for (i = 0; i < SHA256_DIGEST_LENGTH; i++) {
+               d_16 = buf[i] >> 4;
+               r_16 = buf[i] & 0xf;
+
+               psk[i << 1] = d_16 < 10 ? d_16 + '0' : d_16 - 10 + 'a';
+               psk[(i << 1) + 1] = r_16 < 10 ? r_16 + '0' : r_16 - 10 + 'a';
+       }
+       psk[i << 1] = '\0';
+
+       return MOBILE_AP_ERROR_NONE;
+}
+
+static int __execute_hostapd(const mobile_ap_type_e type, const char *ssid,
+               const char *security, const char *passphrase, int hide_mode)
+{
+       DBG("+\n");
+
+       char *conf = NULL;
+       char *old_conf;
+       char buf[HOSTAPD_CONF_LEN] = "";
+       FILE *fp = NULL;
+       pid_t pid;
+       int ret;
+       char key[MOBILE_AP_WIFI_KEY_MAX_LEN + 1];
+       /* Default conf. */
+       snprintf(buf, sizeof(buf), HOSTAPD_CONF,
+                       WIFI_IF,
+                       HOSTAPD_CTRL_INTF_DIR,
+                       ssid,
+                       MOBILE_AP_WIFI_CHANNEL,
+                       hide_mode ? 2 : 0,
+                       MOBILE_AP_MAX_WIFI_STA);
+       conf = g_strdup(buf);
+
+       /* Vendor elements conf. */
+       if (type == MOBILE_AP_TYPE_WIFI) {
+               snprintf(buf, sizeof(buf),
+                               "vendor_elements=%s\n", HOSTAPD_VENDOR_ELEMENTS_TETH);
+       } else if (type == MOBILE_AP_TYPE_WIFI_AP) {
+               snprintf(buf, sizeof(buf),
+                               "vendor_elements=%s\n", HOSTAPD_VENDOR_ELEMENTS_WIFI_AP);
+       } else {
+               ERR("Unknown type: %d\n", type);
+               g_free(conf);
+               return MOBILE_AP_ERROR_INVALID_PARAM;
+       }
+       old_conf = conf;
+       conf = g_strconcat(old_conf, buf, NULL);
+       g_free(old_conf);
+
+       /* Security conf. */
+       if (security != NULL && !strcmp(security, "wpa2-psk")) {
+               ret = __get_psk_hexascii(passphrase, (const unsigned char *)ssid, key, sizeof(key));
+               if (ret != MOBILE_AP_ERROR_NONE) {
+                       g_free(conf);
+                       ERR("hex conversion failed\n");
+                       return MOBILE_AP_ERROR_RESOURCE;
+               }
+               snprintf(buf, sizeof(buf),
+                               "wpa=2\nrsn_pairwise=CCMP\nwpa_psk=%s\n", key);
+
+               old_conf = conf;
+               conf = g_strconcat(old_conf, buf, NULL);
+               g_free(old_conf);
+       }
+
+       fp = fopen(HOSTAPD_CONF_FILE, "w");
+       if (NULL == fp) {
+               ERR("Could not create the file.\n");
+               g_free(conf);
+               return MOBILE_AP_ERROR_RESOURCE;
+       }
+       fputs(conf, fp);
+       g_free(conf);
+       fclose(fp);
+
+       pid = fork();
+       if (pid < 0) {
+               ERR("fork failed\n");
+               return MOBILE_AP_ERROR_RESOURCE;
+       }
+
+       if (pid == 0) {
+               if (execl(HOSTAPD_BIN, HOSTAPD_BIN, "-e", HOSTAPD_ENTROPY_FILE,
+                                       HOSTAPD_CONF_FILE,
+                                       "-f", HOSTAPD_DEBUG_FILE, "-ddd",
+                                       (char *)NULL)) {
+                       ERR("execl failed\n");
+               }
+
+               ERR("Should not get here!");
+               return MOBILE_AP_ERROR_RESOURCE;
+       }
+
+       hostapd_pid = pid;
+
+       return MOBILE_AP_ERROR_NONE;
+}
+
+static int __terminate_hostapd()
+{
+       DBG("+\n");
+
+       int ret;
+
+       if (hostapd_pid == 0) {
+               ERR("There is no hostapd\n");
+               return MOBILE_AP_ERROR_NONE;
+       }
+
+       kill(hostapd_pid, SIGTERM);
+       waitpid(hostapd_pid, NULL, 0);
+       hostapd_pid = 0;
+
+       ret = unlink(HOSTAPD_CONF_FILE);
+       if (ret < 0) {
+               ERR("unlink is failed : %s\n", strerror(errno));
+       }
+
+       return MOBILE_AP_ERROR_NONE;
+}
+
+/*
+ * number NUM_STA(void)
+ * addr STA-FIRST(void)
+ * addr STA-NEXT(addr)
+ * void DISASSOCIATE(addr)
+ * void READ_WHITELIST(filename)
+ * void SET_MAXCLIENT(number)
+ */
+static int __send_hostapd_req(int fd, const char *req, const int req_len,
+               char *buf, int *buf_len)
+{
+       if (fd < 0 || req == NULL || req_len <= 0 ||
+                       buf == NULL || buf_len == NULL || *buf_len <= 0) {
+               ERR("Invalid param\n");
+               return MOBILE_AP_ERROR_INVALID_PARAM;
+       }
+
+       struct timeval tv = {10, 0};
+       fd_set fds;
+       int ret = 0;
+
+       ret = send(fd, req, req_len, 0);
+       if (ret < 0) {
+               ERR("send is failed : %s\n", strerror(errno));
+               return MOBILE_AP_ERROR_INTERNAL;
+       }
+
+       while (TRUE) {
+               FD_ZERO(&fds);
+               FD_SET(fd, &fds);
+               ret = select(fd + 1, &fds, NULL, NULL, &tv);
+               if (ret < 0) {
+                       return MOBILE_AP_ERROR_INTERNAL;
+               } else if (ret == 0) {
+                       ERR("There is no response from hostapd\n");
+                       return MOBILE_AP_ERROR_INTERNAL;
+               } else if (!FD_ISSET(fd, &fds)) {
+                       ERR("Unknown case\n");
+                       return MOBILE_AP_ERROR_INTERNAL;
+               }
+
+               ret = recv(fd, buf, (*buf_len) - 1, 0);
+               if (ret < 0) {
+                       ERR("recv is failed\n");
+                       return MOBILE_AP_ERROR_INTERNAL;
+               }
+
+               if (buf[0] == '<') {
+                       ERR("Unsolicited message\n");
+                       continue;
+               }
+
+               *buf_len = ret;
+               buf[ret] = '\0';
+               if (ret == 0) {
+                       ERR("socket is closed\n");
+               }
+
+               break;
+       }
+
+       return MOBILE_AP_ERROR_NONE;
+}
+
+static int __open_hostapd_intf(int *fd, const char *intf)
+{
+       if (fd == NULL || intf == NULL) {
+               ERR("fd is NULL\n");
+               return MOBILE_AP_ERROR_INVALID_PARAM;
+       }
+
+       DBG("+\n");
+
+       int retry = 0;
+       char ctrl_intf[255] = {0, };
+       struct sockaddr_un src;
+       struct sockaddr_un dest;
+       struct stat stat_buf;
+
+       *fd = socket(PF_UNIX, SOCK_DGRAM, 0);
+       if (*fd < 0) {
+               ERR("socket is failed\n");
+               return MOBILE_AP_ERROR_INTERNAL;
+       }
+
+       src.sun_family = AF_UNIX;
+       g_strlcpy(src.sun_path, intf, sizeof(src.sun_path));
+
+       if (stat(src.sun_path, &stat_buf) == 0) {
+               unlink(src.sun_path);
+       }
+
+       if (bind(*fd, (struct sockaddr *)&src, sizeof(src)) < 0) {
+               ERR("bind is failed\n");
+               close(*fd);
+               *fd = -1;
+               unlink(src.sun_path);
+               return MOBILE_AP_ERROR_INTERNAL;
+       }
+
+       snprintf(ctrl_intf, sizeof(ctrl_intf), "%s/%s",
+                       HOSTAPD_CTRL_INTF_DIR, WIFI_IF);
+       dest.sun_family = AF_UNIX;
+       g_strlcpy(dest.sun_path, ctrl_intf, sizeof(dest.sun_path));
+
+       while (connect(*fd, (struct sockaddr *)&dest, sizeof(dest)) < 0) {
+               if (++retry >= HOSTAPD_RETRY_MAX)
+                       goto FAIL;
+               usleep(HOSTAPD_RETRY_DELAY);
+       }
+
+       return MOBILE_AP_ERROR_NONE;
+
+FAIL:
+       ERR("Cannot make connection to hostapd\n");
+       close(*fd);
+       *fd = -1;
+       unlink(src.sun_path);
+
+       return MOBILE_AP_ERROR_INTERNAL;
+}
+
+static int __close_hostapd_intf(int *fd)
+{
+       DBG("+\n");
+
+       if (fd == NULL) {
+               ERR("fd is NULL\n");
+               return MOBILE_AP_ERROR_INVALID_PARAM;
+       }
+
+       if (*fd > 0)
+               close(*fd);
+       *fd = -1;
+
+       return MOBILE_AP_ERROR_NONE;
+}
+
+static gboolean __hostapd_monitor_cb(GIOChannel *source)
+{
+       DBG("+\n");
+
+       GSList *l;
+       char buf[HOSTAPD_REQ_MAX_LEN + 1] = {0, };
+       char *pbuf = NULL;
+       gsize read = 0;
+       int n_station = 0;
+       int type;
+       sta_timer_t *ptr = NULL;
+       char *mac = NULL;
+       char *end = NULL;
+       gboolean discon_event = FALSE;
+
+
+#if !GLIB_CHECK_VERSION(2, 31, 0)
+       int ret = 0;
+
+       ret = g_io_channel_read(hostapd_io_channel, buf,
+                       HOSTAPD_REQ_MAX_LEN, &read);
+       if (ret != G_IO_ERROR_NONE) {
+               ERR("g_io_channel_read is failed\n");
+               return FALSE;
+       }
+#else
+       GError *err = NULL;
+       GIOStatus ios;
+
+       ios = g_io_channel_read_chars(hostapd_io_channel, buf,
+                       HOSTAPD_REQ_MAX_LEN, &read, &err);
+       if (err != NULL) {
+               ERR("g_io_channel_read_chars is failed : %s\n", err->message);
+               g_error_free(err);
+               return FALSE;
+       } else if (ios != G_IO_STATUS_NORMAL) {
+               ERR("g_io_channel_read_chars is failed : %d\n", ios);
+               return FALSE;
+       }
+#endif
+
+       buf[read] = '\0';
+       pbuf = strrchr(buf, '\n');
+       if (pbuf != NULL)
+               *pbuf = '\0';
+
+       SDBG("Read string from hostapd = [%s]\n", buf);
+       pbuf = buf;
+       /* concatenated string, containing multiple events can arrive */
+       while (pbuf && *pbuf) {
+               pbuf = __find_first_caps_char(pbuf);
+               if (!pbuf || !*pbuf) {
+                       break;
+               }
+
+               if (!strncmp(pbuf, HOSTAPD_STA_CONN, HOSTAPD_STA_CONN_LEN)) {
+                       pbuf = pbuf + HOSTAPD_STA_CONN_LEN;
+                       if (!pbuf || !*pbuf) {
+                               ERR("No mac address\n");
+                               return TRUE;
+                       }
+
+                       end = strchr(pbuf, '<');
+                       if (end && *end) {
+                               mac = g_strndup(pbuf, (long)(end - pbuf));
+                               pbuf = end + 1;
+                       } else {
+                               mac = g_strdup(pbuf);
+                               pbuf += strlen(mac);
+                       }
+
+                       if (mac == NULL) {
+                               ERR("strdup failed\n");
+                               return TRUE;
+                       }
+
+                       for (l = sta_timer_list; l != NULL; l = g_slist_next(l)) {
+                               ptr = (sta_timer_t *)l->data;
+                               if (ptr == NULL) {
+                                       continue;
+                               }
+
+                               if (g_strcmp0(ptr->mac_addr, mac) == 0) {
+                                       g_free(mac);
+                                       mac = NULL;
+                                       break;
+                               }
+                       }
+
+                       /* Matched station found, so skip */
+                       if (l != NULL) {
+                               continue;
+                       }
+
+                       SDBG("%s%s\n", HOSTAPD_STA_CONN, mac);
+                       ptr = (sta_timer_t *)g_malloc(sizeof(sta_timer_t));
+                       if (ptr == NULL) {
+                               ERR("g_malloc failed\n");
+                               g_free(mac);
+                               mac = NULL;
+                               return TRUE;
+                       }
+                       ptr->mac_addr = mac;
+                       ptr->tid = g_timeout_add(HOSTAPD_DHCP_MAX_INTERVAL,
+                                       __hostapd_connect_timer_cb, mac);
+                       sta_timer_list = g_slist_append(sta_timer_list, ptr);
+
+               } else if (!strncmp(pbuf, HOSTAPD_STA_DISCONN, HOSTAPD_STA_DISCONN_LEN)) {
+                       pbuf = pbuf + HOSTAPD_STA_DISCONN_LEN;
+                       if (!pbuf || !*pbuf) {
+                               break;
+                       }
+
+                       end = strchr(pbuf, '<');
+                       if (end && *end) {
+                               mac = g_strndup(pbuf, (long)(end - pbuf));
+                               pbuf = end + 1;
+                       } else {
+                               mac = g_strdup(pbuf);
+                               pbuf += strlen(mac);
+                       }
+
+                       if (mac == NULL) {
+                               ERR("strdup failed\n");
+                               return TRUE;
+                       }
+
+                       SDBG("%s%s\n", HOSTAPD_STA_DISCONN, mac);
+                       _remove_station_info(mac, _slist_find_station_by_mac);
+
+                       /*
+                        * Destroy the timer if its not expired before disconnection
+                        */
+                       _destroy_dhcp_ack_timer(mac);
+                       g_free(mac);
+                       mac = NULL;
+                       discon_event = TRUE;
+
+               } else {
+                       pbuf = strchr(pbuf, '>');
+                       if (pbuf == NULL)
+                               break;
+                       pbuf++;
+               }
+       }
+
+       if (discon_event == FALSE)
+               goto DONE;
+
+       if (_mobileap_is_enabled(MOBILE_AP_STATE_WIFI)) {
+               type = MOBILE_AP_TYPE_WIFI;
+       } else if (_mobileap_is_enabled(MOBILE_AP_STATE_WIFI_AP)) {
+               type = MOBILE_AP_TYPE_WIFI_AP;
+       } else {
+               goto DONE;
+       }
+
+       _get_station_count((gconstpointer)type,
+                       _slist_find_station_by_interface, &n_station);
+
+       if (n_station == 0) {
+               if (type == MOBILE_AP_TYPE_WIFI)
+                       _start_timeout_cb(type, time(NULL) + TETHERING_CONN_TIMEOUT);
+               else if (type == MOBILE_AP_TYPE_WIFI_AP)
+                       _start_timeout_cb(type, time(NULL) + WIFI_AP_CONN_TIMEOUT);
+       }
+DONE:
+       return TRUE;
+}
+
+static int __open_hostapd_monitor(int *fd)
+{
+       if (fd == NULL) {
+               ERR("fd is NULL\n");
+               return MOBILE_AP_ERROR_INVALID_PARAM;
+       }
+
+       DBG("+\n");
+
+       char buf[HOSTAPD_REQ_MAX_LEN] = {0, };
+       int buf_len = 0;
+
+       if (__open_hostapd_intf(fd, MH_MONITOR_INTF) != MOBILE_AP_ERROR_NONE) {
+               ERR("__open_hostapd_intf() is failed\n");
+               return MOBILE_AP_ERROR_INTERNAL;
+       }
+
+       hostapd_io_channel = g_io_channel_unix_new(*fd);
+       if (hostapd_io_channel == NULL) {
+               ERR("g_io_channel_unix_new is failed\n");
+               return MOBILE_AP_ERROR_INTERNAL;
+       }
+
+       g_io_channel_set_encoding(hostapd_io_channel, NULL, NULL);
+       g_io_channel_set_flags(hostapd_io_channel,
+                       G_IO_FLAG_APPEND | G_IO_FLAG_NONBLOCK, NULL);
+
+       hostapd_io_source = g_io_add_watch(hostapd_io_channel, G_IO_IN,
+                       (GIOFunc)__hostapd_monitor_cb, NULL);
+
+       buf_len = sizeof(buf);
+       __send_hostapd_req(*fd, HOSTAPD_MONITOR_ATTACH,
+                       strlen(HOSTAPD_MONITOR_ATTACH), buf, &buf_len);
+
+       return MOBILE_AP_ERROR_NONE;
+}
+
+static int __close_hostapd_monitor(int *fd)
+{
+       GError *err = NULL;
+       char buf[HOSTAPD_REQ_MAX_LEN] = {0, };
+       int buf_len = 0;
+
+       buf_len = sizeof(buf);
+       __send_hostapd_req(*fd, HOSTAPD_MONITOR_DETACH,
+                       strlen(HOSTAPD_MONITOR_DETACH), buf, &buf_len);
+
+       if (hostapd_io_source != 0) {
+               g_source_remove(hostapd_io_source);
+               hostapd_io_source = 0;
+       }
+
+       if (hostapd_io_channel != NULL) {
+               g_io_channel_shutdown(hostapd_io_channel, TRUE, &err);
+               g_io_channel_unref(hostapd_io_channel);
+               hostapd_io_channel = NULL;
+       }
+
+       __close_hostapd_intf(fd);
+
+       return MOBILE_AP_ERROR_NONE;
+}
+
+static mobile_ap_drv_interface_e __get_drv_interface(void)
+{
+       static mobile_ap_drv_interface_e drv_interface = MOBILE_AP_DRV_INTERFACE_NONE;
+
+       if (drv_interface != MOBILE_AP_DRV_INTERFACE_NONE) {
+               return drv_interface;
+       }
+
+       const char *drv_rfkill_path = "/sys/devices/platform";
+       const char *wext_drv[] = {
+               "bcm4329-b1", "bcm4330-b0",
+               "bcm4330-b1", "bcm4330-b2",
+               NULL};
+
+       char path[MAX_BUF_SIZE] = { 0 };
+       struct stat stat_buf = { 0 };
+       int fd = 0;
+       int i = 0;
+
+       drv_interface = MOBILE_AP_NL80211;
+
+       for (i = 0; wext_drv[i] != NULL; i++) {
+               snprintf(path, sizeof(path), "%s/%s",
+                               drv_rfkill_path, wext_drv[i]);
+               fd = open(path, O_RDONLY);
+               if (fd < 0)
+                       continue;
+
+               if (fstat(fd, &stat_buf) == 0 && S_ISDIR(stat_buf.st_mode)) {
+                       drv_interface = MOBILE_AP_WEXT;
+                       close(fd);
+                       break;
+               }
+
+               close(fd);
+       }
+
+       return drv_interface;
+}
+
+static int __mh_core_softap_firmware_start(void)
+{
+       int err = 0;
+       DBusError error;
+       DBusMessageIter iter;
+       DBusMessage *reply = NULL;
+       DBusMessage *message = NULL;
+       DBusConnection *connection = NULL;
+       const char *device = "softap";
+
+       connection = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
+       if (connection == NULL) {
+               ERR("Failed to get system bus");
+               return -EIO;
+       }
+
+       message = dbus_message_new_method_call(NETCONFIG_SERVICE,
+                       NETCONFIG_WIFI_PATH, NETCONFIG_WIFI_INTERFACE ".Firmware", "Start");
+       if (message == NULL) {
+               ERR("Failed DBus method call");
+               dbus_connection_unref(connection);
+               return -EIO;
+       }
+
+       dbus_message_iter_init_append(message, &iter);
+       dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &device);
+
+       dbus_error_init(&error);
+
+       reply = dbus_connection_send_with_reply_and_block(connection, message,
+                       NETCONFIG_DBUS_REPLY_TIMEOUT, &error);
+       if (dbus_error_is_set(&error) == TRUE) {
+               if (NULL != strstr(error.message, ".AlreadyExists")) {
+                       // softap already enabled
+               } else {
+                       ERR("dbus_connection_send_with_reply_and_block() failed. "
+                                       "DBus error [%s: %s]", error.name, error.message);
+
+                       err = -EIO;
+
+                       dbus_error_free(&error);
+               }
+
+               dbus_error_free(&error);
+       }
+
+       if (reply != NULL)
+               dbus_message_unref(reply);
+
+       dbus_message_unref(message);
+       dbus_connection_unref(connection);
+
+       return err;
+}
+
+static int __mh_core_softap_firmware_stop(void)
+{
+       int err = 0;
+       DBusError error;
+       DBusMessageIter iter;
+       DBusMessage *reply = NULL;
+       DBusMessage *message = NULL;
+       DBusConnection *connection = NULL;
+       const char *device = "softap";
+
+       connection = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
+       if (connection == NULL) {
+               ERR("Failed to get system bus");
+               return -EIO;
+       }
+
+       message = dbus_message_new_method_call(NETCONFIG_SERVICE,
+                       NETCONFIG_WIFI_PATH, NETCONFIG_WIFI_INTERFACE ".Firmware", "Stop");
+       if (message == NULL) {
+               ERR("Failed DBus method call");
+               dbus_connection_unref(connection);
+               return -EIO;
+       }
+
+       dbus_message_iter_init_append(message, &iter);
+       dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &device);
+
+       dbus_error_init(&error);
+
+       reply = dbus_connection_send_with_reply_and_block(connection, message,
+                       NETCONFIG_DBUS_REPLY_TIMEOUT, &error);
+       if (dbus_error_is_set(&error) == TRUE) {
+               if (NULL != strstr(error.message, ".AlreadyExists")) {
+                       // softap already disabled
+               } else {
+                       ERR("dbus_connection_send_with_reply_and_block() failed. "
+                                       "DBus error [%s: %s]", error.name, error.message);
+
+                       err = -EIO;
+
+                       dbus_error_free(&error);
+               }
+
+               dbus_error_free(&error);
+       }
+
+       if (reply != NULL)
+               dbus_message_unref(reply);
+
+       dbus_message_unref(message);
+       dbus_connection_unref(connection);
+
+       return err;
+}
+
+int _mh_core_enable_softap(const mobile_ap_type_e type, const char *ssid,
+               const char *security, const char *key, int hide_mode)
+{
+       if (ssid == NULL || security == NULL || key == NULL) {
+               ERR("Invalid param\n");
+               return MOBILE_AP_ERROR_INTERNAL;
+       }
+
+       char cmd[MAX_BUF_SIZE];
+       int ret_status = MOBILE_AP_ERROR_NONE;
+       mobile_ap_drv_interface_e drv_interface = MOBILE_AP_DRV_INTERFACE_NONE;
+
+       int sock_fd;
+       char *if_name = WIFI_IF;
+       char buf[MAX_BUF_SIZE] = { 0 };
+
+       char wext_ssid[MOBILE_AP_WIFI_SSID_MAX_LEN] = { 0 };
+       char *ptr = NULL;
+
+       if (__mh_core_softap_firmware_start() < 0)
+               return MOBILE_AP_ERROR_INTERNAL;
+
+       drv_interface = __get_drv_interface();
+
+       switch (drv_interface) {
+       case MOBILE_AP_WEXT:
+               if ((sock_fd = socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
+                       ERR("Failed to open socket...!!!\n");
+                       ret_status = MOBILE_AP_ERROR_RESOURCE;
+                       break;
+               }
+
+               /*
+                * In case of Wireless extension interface,
+                * 32 byte SSID including null character can be accepted.
+                */
+               g_strlcpy(wext_ssid, ssid, sizeof(wext_ssid));
+               if (!g_utf8_validate(wext_ssid, -1, (const char **)&ptr))
+                       *ptr = '\0';
+
+               snprintf(cmd, MAX_BUF_SIZE, "ASCII_CMD=AP_CFG,"
+                                       "SSID_LEN=%d,SSID=%s,"
+                                       "SEC=%s,KEY_LEN=%d,KEY=%s,CHANNEL=%d,"
+                                       "PREAMBLE=0,MAX_SCB=%d,HIDE=%d,END",
+                                       strlen(wext_ssid), wext_ssid,
+                                       security, strlen(key), key,
+                                       MOBILE_AP_WIFI_CHANNEL,
+                                       MOBILE_AP_MAX_WIFI_STA, hide_mode);
+               ret_status = __issue_ioctl(sock_fd, if_name, cmd, buf);
+               if (ret_status != MOBILE_AP_ERROR_NONE) {
+                       ERR("__issue_ioctl failed...!!!\n");
+                       close(sock_fd);
+                       break;
+               }
+
+               /* Start broadcasting of BSS. */
+               snprintf(cmd, MAX_BUF_SIZE, "ASCII_CMD=AP_BSS_START");
+               ret_status = __issue_ioctl(sock_fd, if_name, cmd, buf);
+               if (ret_status != MOBILE_AP_ERROR_NONE) {
+                       ERR("__issue_ioctl failed...!!!\n");
+                       close(sock_fd);
+                       break;
+               }
+
+               close(sock_fd);
+
+               ret_status = _mh_core_set_ip_address(SOFTAP_IF,
+                               IP_ADDRESS_SOFTAP);
+               if (ret_status != MOBILE_AP_ERROR_NONE) {
+                       ERR("_mh_core_set_ip_address of SOFTAP_IF is failed\n");
+                       break;
+               }
+
+               ret_status = _mh_core_set_ip_address(WIFI_IF,
+                               IP_ADDRESS_WIFI);
+               if (ret_status != MOBILE_AP_ERROR_NONE) {
+                       ERR("_mh_core_set_ip_address of WIFI_IF is failed\n");
+                       break;
+               }
+               break;
+
+       case MOBILE_AP_NL80211:
+
+               ret_status = _mh_core_set_ip_address(WIFI_IF,
+                               IP_ADDRESS_SOFTAP);
+               if (ret_status != MOBILE_AP_ERROR_NONE) {
+                       ERR("_mh_core_set_ip_address is failed\n");
+                       break;
+               }
+
+               ret_status = __execute_hostapd(type, ssid, security, key, hide_mode);
+               if (ret_status != MOBILE_AP_ERROR_NONE) {
+                       ERR("__execute_hostapd is failed\n");
+                       break;
+               }
+
+               ret_status = __open_hostapd_intf(&hostapd_ctrl_fd, MH_CTRL_INTF);
+               if (ret_status != MOBILE_AP_ERROR_NONE) {
+                       ERR("__open_hostapd_intf is failed\n");
+                       __terminate_hostapd();
+                       break;
+               }
+
+               ret_status = __open_hostapd_monitor(&hostapd_monitor_fd);
+               if (ret_status != MOBILE_AP_ERROR_NONE) {
+                       ERR("__open_hostapd_monitor is failed\n");
+                       __close_hostapd_intf(&hostapd_ctrl_fd);
+                       __terminate_hostapd();
+                       break;
+               }
+
+               break;
+
+       default:
+               ERR("Unknown driver interface : %d\n", drv_interface);
+               break;
+       }
+
+       if (ret_status != MOBILE_AP_ERROR_NONE)
+               __mh_core_softap_firmware_stop();
+
+       return ret_status;
+}
+
+int _mh_core_disable_softap(void)
+{
+       char cmd[MAX_BUF_SIZE] = { 0 };
+       int ret_status = MOBILE_AP_ERROR_NONE;
+       mobile_ap_drv_interface_e drv_interface = MOBILE_AP_DRV_INTERFACE_NONE;
+
+       int sock_fd = 0;
+       char buf[MAX_BUF_SIZE] = { 0 };
+       char *if_name = WIFI_IF;
+
+       drv_interface = __get_drv_interface();
+
+       switch (drv_interface) {
+       case MOBILE_AP_WEXT:
+               if ((sock_fd = socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
+                       ERR("Failed to open socket...!!!\n");
+                       ret_status = MOBILE_AP_ERROR_RESOURCE;
+                       break;
+               }
+
+               /* Stop broadcasting of BSS. */
+               snprintf(cmd, MAX_BUF_SIZE, "ASCII_CMD=AP_BSS_STOP");
+               ret_status = __issue_ioctl(sock_fd, if_name, cmd, buf);
+               if (ret_status != MOBILE_AP_ERROR_NONE) {
+                       ERR("__issue_ioctl failed...!!!\n");
+                       close(sock_fd);
+                       break;
+               }
+
+               close(sock_fd);
+               break;
+
+       case MOBILE_AP_NL80211:
+               ret_status = __close_hostapd_intf(&hostapd_ctrl_fd);
+               if (ret_status != MOBILE_AP_ERROR_NONE)
+                       ERR("hostapd termination is failed\n");
+
+               ret_status = __close_hostapd_monitor(&hostapd_monitor_fd);
+               if (ret_status != MOBILE_AP_ERROR_NONE)
+                       ERR("hostapd termination is failed\n");
+
+               ret_status = __terminate_hostapd();
+               if (ret_status != MOBILE_AP_ERROR_NONE) {
+                       ERR("hostapd termination is failed\n");
+               }
+               break;
+
+       default:
+               ERR("Unknown driver interface : %d\n", drv_interface);
+               break;
+       }
+
+       if (__mh_core_softap_firmware_stop() < 0)
+               ret_status = MOBILE_AP_ERROR_INTERNAL;
+
+       return ret_status;
+}
+
+static int __get_device_info_by_wext(softap_device_info_t *di)
+{
+       int sock_fd = 0;
+       char *if_name = SOFTAP_IF;
+       char cmd[MAX_BUF_SIZE];
+       char buf[MAX_BUF_SIZE] = { 0 };
+       int ret = MOBILE_AP_ERROR_NONE;
+
+       char *buf_ptr = NULL;
+       int i;
+
+       if ((sock_fd = socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
+               ERR("Failed to open socket...!!!\n");
+               di->number = 0;
+               return MOBILE_AP_ERROR_RESOURCE;
+       }
+
+       snprintf(cmd, MAX_BUF_SIZE, "AP_GET_STA_LIST");
+       ret = __issue_ioctl(sock_fd, if_name, cmd, buf);
+       if (ret != MOBILE_AP_ERROR_NONE) {
+               ERR("__issue_ioctl failed...!!!\n");
+               di->number = 0;
+               close(sock_fd);
+               return ret;
+       }
+
+       buf_ptr = buf;
+
+       sscanf(buf_ptr, "%02x", &di->number);
+
+       buf_ptr += 2;
+       for (i = 0; i < di->number; i++) {
+               unsigned int l_bssid[MOBILE_AP_WIFI_BSSID_LEN];
+               sscanf(buf_ptr, "%02X%02X%02X%02X%02X%02X", &l_bssid[0],
+                                       &l_bssid[1], &l_bssid[2], &l_bssid[3],
+                                       &l_bssid[4], &l_bssid[5]);
+               snprintf(di->bssid[i], MOBILE_AP_STR_INFO_LEN,
+                                       "%02X:%02X:%02X:%02X:%02X:%02X",
+                                       l_bssid[0], l_bssid[1], l_bssid[2],
+                                       l_bssid[3], l_bssid[4], l_bssid[5]);
+
+               SDBG("STA[%d] address[%s]\n", i, di->bssid[i]);
+
+               buf_ptr += 12;
+       }
+
+       close(sock_fd);
+
+       return ret;
+}
+
+static int __get_device_info_by_nl80211(softap_device_info_t *di)
+{
+       int ret = 0;
+       int no_of_sta = 0;
+       int buf_len = 0;
+       char req[HOSTAPD_REQ_MAX_LEN] = {0, };
+       char buf[MOBILE_AP_STR_INFO_LEN] = {0, };
+
+       buf_len = sizeof(buf);
+       g_strlcpy(req, "NUM_STA", sizeof(req));
+       ret = __send_hostapd_req(hostapd_ctrl_fd,
+                       req, strlen(req), buf, &buf_len);
+       if (ret != MOBILE_AP_ERROR_NONE) {
+               ERR("__send_hostapd_req is failed : %d\n", ret);
+               return ret;
+       }
+
+       DBG("The number of station : %s\n", buf);
+       if (atoi(buf) == 0) {
+               DBG("There is no station\n");
+               return MOBILE_AP_ERROR_NONE;
+       }
+
+       buf_len = sizeof(buf);
+       g_strlcpy(req, "STA-FIRST", sizeof(req));
+       ret = __send_hostapd_req(hostapd_ctrl_fd,
+                       req, strlen(req), buf, &buf_len);
+       if (ret != MOBILE_AP_ERROR_NONE) {
+               ERR("__send_hostapd_req is failed : %d\n", ret);
+               return ret;
+       }
+
+       do {
+               if (!strncmp(buf, "FAIL", 4)) {
+                       ERR("FAIL is returned\n");
+                       break;
+               }
+
+               if (buf[0] == '\0') {
+                       ERR("NULL string\n");
+                       break;
+               }
+
+               SDBG("Station : %s\n", buf);
+               g_strlcpy(di->bssid[no_of_sta++], buf, MOBILE_AP_STR_INFO_LEN);
+
+               buf_len = sizeof(buf);
+               snprintf(req, sizeof(req), "STA-NEXT %s", buf);
+               ret = __send_hostapd_req(hostapd_ctrl_fd,
+                               req, strlen(req), buf, &buf_len);
+       } while (ret == MOBILE_AP_ERROR_NONE);
+
+       di->number = no_of_sta;
+
+       return ret;
+}
+
+int _mh_core_get_device_info(softap_device_info_t *di)
+{
+       if (di == NULL) {
+               ERR("Invalid param\n");
+               return MOBILE_AP_ERROR_INVALID_PARAM;
+       }
+
+       int ret = MOBILE_AP_ERROR_NONE;
+
+       switch (__get_drv_interface()) {
+       case MOBILE_AP_WEXT:
+               ret = __get_device_info_by_wext(di);
+               break;
+
+       case MOBILE_AP_NL80211:
+               ret = __get_device_info_by_nl80211(di);
+               break;
+
+       default:
+               ERR("Unknown interface\n");
+               break;
+       }
+
+       return ret;
+}
+
+int _mh_core_execute_dhcp_server(void)
+{
+       char buf[DNSMASQ_CONF_LEN] = "";
+       FILE *fp = NULL;
+       pid_t pid;
+
+       fp = fopen(DNSMASQ_CONF_FILE, "w");
+       if (NULL == fp) {
+               ERR("Could not create the file.\n");
+               return MOBILE_AP_ERROR_RESOURCE;
+       }
+       snprintf(buf, DNSMASQ_CONF_LEN, DNSMASQ_CONF);
+       fputs(buf, fp);
+       fclose(fp);
+
+       pid = fork();
+       if (pid < 0) {
+               ERR("fork failed\n");
+               return MOBILE_AP_ERROR_RESOURCE;
+       }
+
+       if (pid == 0) {
+               /* -d : Debug mode
+                * -p 0 : DNS off
+                * -C file : Configuration file path
+                */
+               if (execl("/usr/bin/dnsmasq", "/usr/bin/dnsmasq", "-d",
+                                       "-p", "0", "-C", DNSMASQ_CONF_FILE,
+                                       (char *)NULL)) {
+                       ERR("execl failed\n");
+               }
+
+               ERR("Should not get here!");
+               return MOBILE_AP_ERROR_RESOURCE;
+       }
+
+       dnsmasq_pid = pid;
+
+       return MOBILE_AP_ERROR_NONE;
+}
+
+int _mh_core_terminate_dhcp_server(void)
+{
+       int ret;
+
+       if (dnsmasq_pid == 0) {
+               ERR("There is no dnsmasq\n");
+               return MOBILE_AP_ERROR_NONE;
+       }
+
+       kill(dnsmasq_pid, SIGTERM);
+       waitpid(dnsmasq_pid, NULL, 0);
+       dnsmasq_pid = 0;
+
+       ret = unlink(DNSMASQ_CONF_FILE);
+       if (ret < 0) {
+               ERR("unlink is failed : %s\n", strerror(errno));
+       }
+
+       return MOBILE_AP_ERROR_NONE;
+}
+
+int _mh_core_enable_masquerade(const char *ext_if)
+{
+       if (ext_if == NULL || strlen(ext_if) == 0) {
+               ERR("ext_if[%s] is invalid\n", ext_if);
+               return MOBILE_AP_ERROR_INVALID_PARAM;
+       }
+
+       int fd = -1;
+
+       fd = open(IP_FORWARD, O_WRONLY);
+       if (fd < 0) {
+               ERR("open failed\n");
+               return MOBILE_AP_ERROR_RESOURCE;
+       }
+
+       if (write(fd, "1", 1) != 1) {
+               ERR("write failed\n");
+               close(fd);
+               return MOBILE_AP_ERROR_INTERNAL;
+       }
+       close(fd);
+
+       _iptables_create_chain(TABLE_NAT, TETH_NAT_POST);
+       _iptables_add_rule(PKT_REDIRECTION_RULE, TABLE_NAT, CHAIN_POST,
+                       TETH_NAT_POST);
+       _iptables_add_rule(MASQ_RULE, TABLE_NAT, TETH_NAT_POST, ext_if);
+
+       _iptables_create_chain(TABLE_FILTER, TETH_FILTER_FW);
+
+       _iptables_add_rule(PKT_REDIRECTION_RULE, TABLE_FILTER, CHAIN_FW,
+                       TETH_FILTER_FW);
+
+       _iptables_add_rule(CLAMP_MSS_RULE, TABLE_FILTER, TETH_FILTER_FW);
+
+       _iptables_add_rule(FORWARD_RULE_WITH_ACTION_AND_STATE, TABLE_FILTER, TETH_FILTER_FW,
+               BT_IF_ALL, ext_if, ACTION_RETURN, STATE_RELATED_ESTAB);
+       _iptables_add_rule(FORWARD_RULE_WITH_ACTION_AND_STATE, TABLE_FILTER, TETH_FILTER_FW,
+               WIFI_IF, ext_if, ACTION_RETURN, STATE_RELATED_ESTAB);
+       _iptables_add_rule(FORWARD_RULE_WITH_ACTION_AND_STATE, TABLE_FILTER, TETH_FILTER_FW,
+               USB_IF, ext_if, ACTION_RETURN, STATE_RELATED_ESTAB);
+
+       _iptables_add_rule(FORWARD_RULE_WITH_ACTION_AND_STATE, TABLE_FILTER, TETH_FILTER_FW,
+               ext_if, BT_IF_ALL, ACTION_DROP, STATE_INVALID);
+       _iptables_add_rule(FORWARD_RULE_WITH_ACTION_AND_STATE, TABLE_FILTER, TETH_FILTER_FW,
+               ext_if, WIFI_IF, ACTION_DROP, STATE_INVALID);
+       _iptables_add_rule(FORWARD_RULE_WITH_ACTION_AND_STATE, TABLE_FILTER, TETH_FILTER_FW,
+               ext_if, USB_IF, ACTION_DROP, STATE_INVALID);
+
+       _iptables_add_rule(FORWARD_RULE_WITH_ACTION, TABLE_FILTER, TETH_FILTER_FW,
+               ext_if, BT_IF_ALL, ACTION_RETURN);
+       _iptables_add_rule(FORWARD_RULE_WITH_ACTION, TABLE_FILTER, TETH_FILTER_FW,
+               ext_if, WIFI_IF, ACTION_RETURN);
+       _iptables_add_rule(FORWARD_RULE_WITH_ACTION, TABLE_FILTER, TETH_FILTER_FW,
+               ext_if, USB_IF, ACTION_RETURN);
+
+       _iptables_add_rule(FORWARD_RULE_WITH_ACTION, TABLE_FILTER, TETH_FILTER_FW,
+               BT_IF_ALL, ext_if, ACTION_RETURN);
+       _iptables_add_rule(FORWARD_RULE_WITH_ACTION, TABLE_FILTER, TETH_FILTER_FW,
+               WIFI_IF, ext_if, ACTION_RETURN);
+       _iptables_add_rule(FORWARD_RULE_WITH_ACTION, TABLE_FILTER, TETH_FILTER_FW,
+               USB_IF, ext_if, ACTION_RETURN);
+
+       _iptables_add_rule(DEFAULT_RULE, TABLE_FILTER, TETH_FILTER_FW,
+               ACTION_DROP);
+
+       return MOBILE_AP_ERROR_NONE;
+}
+
+int _mh_core_disable_masquerade(const char *ext_if)
+{
+       if (ext_if == NULL || strlen(ext_if) == 0) {
+               ERR("ext_if[%s] is invalid\n", ext_if);
+               return MOBILE_AP_ERROR_INVALID_PARAM;
+       }
+
+       int fd = -1;
+
+       fd = open(IP_FORWARD, O_WRONLY);
+       if (fd < 0) {
+               ERR("open failed\n");
+               return MOBILE_AP_ERROR_RESOURCE;
+       }
+
+       if (write(fd, "0", 1) != 1) {
+               ERR("write failed\n");
+               close(fd);
+               return MOBILE_AP_ERROR_INTERNAL;
+       }
+       close(fd);
+
+       _iptables_delete_rule(PKT_REDIRECTION_RULE, TABLE_NAT, CHAIN_POST,
+                       TETH_NAT_POST);
+       _iptables_flush_rules(TABLE_NAT, TETH_NAT_POST);
+       _iptables_delete_chain(TABLE_NAT, TETH_NAT_POST);
+
+       _iptables_delete_rule(PKT_REDIRECTION_RULE, TABLE_FILTER, CHAIN_FW,
+                       TETH_FILTER_FW);
+       _iptables_flush_rules(TABLE_FILTER, TETH_FILTER_FW);
+       _iptables_delete_chain(TABLE_FILTER, TETH_FILTER_FW);
+
+       return MOBILE_AP_ERROR_NONE;
+}
+
+void _mh_core_add_data_to_array(GPtrArray *array, guint type, gchar *dev_name)
+{
+       GValue value = {0, {{0}}};
+
+       g_value_init(&value, DBUS_STRUCT_UINT_STRING);
+       g_value_take_boxed(&value,
+                       dbus_g_type_specialized_construct(DBUS_STRUCT_UINT_STRING));
+       dbus_g_type_struct_set(&value, 0, type, 1, dev_name, G_MAXUINT);
+       g_ptr_array_add(array, g_value_get_boxed(&value));
+}
+
+int _mh_core_set_ip_address(const char *if_name, const in_addr_t ip)
+{
+       struct ifreq ifr;
+       struct sockaddr_in addr;
+       int sock_fd;
+
+       SDBG("if_name : %s ip address : 0x%X\n", if_name, ip);
+
+       if ((sock_fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
+               ERR("socket open failed!!!\n");
+               return MOBILE_AP_ERROR_RESOURCE;
+       }
+
+       g_strlcpy(ifr.ifr_name, if_name, IFNAMSIZ);
+
+       memset(&addr, 0, sizeof(struct sockaddr));
+       addr.sin_family = AF_INET;
+       addr.sin_port = 0;
+       addr.sin_addr.s_addr = htonl(ip);
+
+       memcpy(&ifr.ifr_addr, &addr, sizeof(struct sockaddr));
+       if (ioctl(sock_fd, SIOCSIFADDR, &ifr) < 0) {
+               ERR("ioctl failed...!!!\n");
+               close(sock_fd);
+               return MOBILE_AP_ERROR_INTERNAL;
+       }
+
+       if (ioctl(sock_fd, SIOCGIFFLAGS, &ifr) < 0) {
+               ERR("ioctl failed...!!!\n");
+               close(sock_fd);
+               return MOBILE_AP_ERROR_INTERNAL;
+       }
+
+       ifr.ifr_flags |= IFF_UP | IFF_RUNNING;
+       if (ioctl(sock_fd, SIOCSIFFLAGS, &ifr) < 0) {
+               ERR("ioctl failed...!!!\n");
+               close(sock_fd);
+               return MOBILE_AP_ERROR_INTERNAL;
+       }
+
+       close(sock_fd);
+
+       return MOBILE_AP_ERROR_NONE;
+}
+
+static gboolean __send_station_event_cb(gpointer data)
+{
+       int sig = GPOINTER_TO_INT(data);
+       int n_station = 0;
+       int type;
+       mobile_ap_station_info_t *si = NULL;
+
+       if (_mobileap_is_enabled(MOBILE_AP_STATE_WIFI))
+               type = MOBILE_AP_TYPE_WIFI;
+       else if (_mobileap_is_enabled(MOBILE_AP_STATE_WIFI_AP))
+               type = MOBILE_AP_TYPE_WIFI_AP;
+       else
+               return FALSE;
+
+       if (sig == SIGUSR1) {
+               DBG("STA connected(%d)\n", sig);
+               /* STA connection is handled in the dnsmasq signal handler */
+       } else if (sig == SIGUSR2) {
+               DBG("STA disconnected(%d)\n", sig);
+
+               /* Temporarily care only one station.
+                * Driver team should be support detail information */
+               if (_get_station_info((gconstpointer)type,
+                               _slist_find_station_by_interface,
+                               &si) != MOBILE_AP_ERROR_NONE) {
+                       return FALSE;
+               }
+               _remove_station_info(si->mac, _slist_find_station_by_mac);
+
+               _get_station_count((gconstpointer)type,
+                               _slist_find_station_by_interface, &n_station);
+               if (n_station == 0) {
+                       if (type == MOBILE_AP_TYPE_WIFI)
+                               _start_timeout_cb(type, time(NULL) + TETHERING_CONN_TIMEOUT);
+                       else if (type == MOBILE_AP_TYPE_WIFI_AP)
+                               _start_timeout_cb(type, time(NULL) + WIFI_AP_CONN_TIMEOUT);
+               }
+       }
+
+       return FALSE;
+}
+
+static void __handle_station_signal(int sig)
+{
+       g_idle_add(__send_station_event_cb, GINT_TO_POINTER(sig));
+       return;
+}
+
+void _register_wifi_station_handler(void)
+{
+       struct sigaction sa;
+
+       if (__get_drv_interface() != MOBILE_AP_WEXT)
+               return;
+
+       memset(&sa, 0, sizeof(sa));
+       sa.sa_handler = __handle_station_signal;
+       sigaction(SIGUSR1, &sa, NULL);
+       sigaction(SIGUSR2, &sa, NULL);
+
+       return;
+}
+
+void _unregister_wifi_station_handler(void)
+{
+       struct sigaction sa;
+
+       if (__get_drv_interface() != MOBILE_AP_WEXT)
+               return;
+
+       memset(&sa, 0, sizeof(sa));
+       sa.sa_handler = SIG_DFL;
+       sigaction(SIGUSR1, &sa, NULL);
+       sigaction(SIGUSR2, &sa, NULL);
+
+       return;
+}
+
+static gboolean __hostapd_connect_timer_cb(gpointer user_data)
+{
+       char *mac = (char *)user_data;
+       GSList *l = NULL;
+       GSList *temp = NULL;
+       sta_timer_t *ptr = NULL;
+       mobile_ap_station_info_t *info = NULL;
+       time_t tm;
+       int n_station = 0;
+       int type;
+       int ret;
+
+       if (_mobileap_is_enabled(MOBILE_AP_STATE_WIFI))
+               type = MOBILE_AP_TYPE_WIFI;
+       else if (_mobileap_is_enabled(MOBILE_AP_STATE_WIFI_AP))
+               type = MOBILE_AP_TYPE_WIFI_AP;
+       else
+               return FALSE;
+
+       for (l = sta_timer_list; l != NULL; l = g_slist_next(l)) {
+               ptr = (sta_timer_t *)l->data;
+               if (ptr == NULL)
+                       continue;
+
+               if (!g_strcmp0(ptr->mac_addr, mac)) {
+                       DBG("client with Static IP, Add station\n");
+
+                       info = (mobile_ap_station_info_t *)g_malloc(sizeof(mobile_ap_station_info_t));
+                       if (info == NULL) {
+                               ERR("g_malloc failed\n");
+                               g_free(ptr->mac_addr);
+                               g_free(ptr);
+                               temp = l;
+                               l = g_slist_next(l);
+                               sta_timer_list = g_slist_delete_link(sta_timer_list, temp);
+                               break;
+                       }
+
+                       time(&tm);
+                       info->tm = tm;
+                       info->interface = type;
+                       g_strlcpy(info->ip, "", sizeof(info->ip));
+                       g_strlcpy(info->mac, mac, sizeof(info->mac));
+
+                       ret = _get_wifi_name_from_lease_info(mac, &info->hostname);
+                       if (ret != MOBILE_AP_ERROR_NONE)
+                               info->hostname = g_strdup(MOBILE_AP_NAME_UNKNOWN);
+
+                       g_free(ptr->mac_addr);
+                       g_free(ptr);
+                       temp = l;
+                       l = g_slist_next(l);
+                       sta_timer_list = g_slist_delete_link(sta_timer_list, temp);
+
+                       goto SUCCESS;
+               }
+       }
+
+       return FALSE;
+
+SUCCESS :
+       if (_add_station_info(info) != MOBILE_AP_ERROR_NONE) {
+               g_free(info->hostname);
+               g_free(info);
+               return FALSE;
+       }
+
+       _get_station_count((gconstpointer)type,
+                       _slist_find_station_by_interface, &n_station);
+       if (n_station == 1)
+               _stop_timeout_cb(type);
+
+       _send_dbus_station_info("DhcpConnected", info);
+
+       return FALSE;
+}
+
+void _flush_dhcp_ack_timer(void)
+{
+       DBG("+\n");
+
+       GSList *l = NULL;
+       GSList *temp = NULL;
+       sta_timer_t *ptr = NULL;
+
+       for (l = sta_timer_list; l != NULL; l = g_slist_next(l)) {
+               ptr = (sta_timer_t *)l->data;
+               if (ptr) {
+                       if (ptr->tid != 0) {
+                               g_source_remove(ptr->tid);
+                               ptr->tid = 0;
+                       }
+                       g_free(ptr->mac_addr);
+                       g_free(ptr);
+               }
+
+               temp = l;
+               l = g_slist_next(l);
+               sta_timer_list = g_slist_delete_link(sta_timer_list, temp);
+       }
+
+       DBG("-\n");
+       return;
+}
+
+void _destroy_dhcp_ack_timer(char *mac_addr)
+{
+       DBG("+\n");
+       if (mac_addr == NULL) {
+               ERR("mac address passed NULL\n");
+               return;
+       }
+
+       GSList *l = NULL;
+       GSList *temp = NULL;
+       sta_timer_t *ptr = NULL;
+
+       for (l = sta_timer_list; l != NULL; l = g_slist_next(l)) {
+
+               ptr = (sta_timer_t *)l->data;
+               if (ptr == NULL)
+                       continue;
+
+               if (!g_strcmp0(ptr->mac_addr, mac_addr)) {
+
+                       if (ptr->tid != 0) {
+                               g_source_remove(ptr->tid);
+                               ptr->tid = 0;
+                       }
+                       g_free(ptr->mac_addr);
+                       g_free(ptr);
+                       temp = l;
+                       l = g_slist_next(l);
+                       sta_timer_list = g_slist_delete_link(sta_timer_list,
+                                               temp);
+                       break;
+               }
+       }
+
+       DBG("-\n");
+       return;
+}
diff --git a/src/mobileap_usb.c b/src/mobileap_usb.c
new file mode 100755 (executable)
index 0000000..063325c
--- /dev/null
@@ -0,0 +1,483 @@
+/*
+ * mobileap-agent
+ * Copyright (c) 2012 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the License);
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#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 <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <gio/gio.h>
+
+#include "mobileap_softap.h"
+#include "mobileap_common.h"
+#include "mobileap_usb.h"
+
+static DBusGMethodInvocation *g_context = NULL;
+static gboolean in_progress = FALSE;
+static GDBusConnection *conn = NULL;
+static guint subscription_id = 0;
+static int usb_client_state = 0;
+
+#define USB_SYSTEM_DEVICE_PATH "/Org/Tizen/System/DeviceD/Usb"
+#define USB_SYSTEM_DEVICE_IFACE        "org.tizen.system.deviced.Usb"
+#define USB_STATE_CHANGE_SIGNAL        "StateChanged"
+
+enum usbclient_state {
+       USBCLIENT_STATE_DISCONNECTED = 0x00, /* usb cable is detached */
+       USBCLIENT_STATE_CONNECTED    = 0x01, /* usb cable is attached */
+       /* usb cable is attached and available (ready to use) */
+       USBCLIENT_STATE_AVAILABLE    = 0x02,
+};
+
+/* GDbus Signal Handler for USB Device State Changes */
+static void __usb_device_state_change_cb(GDBusConnection *connection,
+       const gchar *sender_name, const gchar *object_path,
+       const gchar *interface_name, const gchar *signal_name,
+       GVariant *parameters, gpointer user_data)
+{
+       unsigned int value = 0;
+       TetheringObject *obj = (TetheringObject *)user_data;
+
+       if (NULL == parameters || NULL == obj) {
+               ERR("Paramters Invalid \n");
+               return;
+       }
+
+       if (strcmp(object_path, USB_SYSTEM_DEVICE_PATH) ||
+               strcmp(interface_name, USB_SYSTEM_DEVICE_IFACE) ||
+               strcmp(signal_name, USB_STATE_CHANGE_SIGNAL)) {
+               ERR("Unknown DBUS Signal\n");
+               return;
+       }
+       g_variant_get(parameters, "(u)", &value);
+       DBG("Received signal(%s), value: (%u)", signal_name, value);
+       DBG("USB connected ? (%s)", value & USBCLIENT_STATE_CONNECTED? "Yes":"No");
+       DBG("USB available ? (%s)", value & USBCLIENT_STATE_AVAILABLE? "Yes":"No");
+
+       if (USBCLIENT_STATE_DISCONNECTED == value) {
+               _disable_usb_tethering(obj);
+
+               if (g_context) {
+                       dbus_g_method_return(g_context, MOBILE_AP_ENABLE_USB_TETHERING_CFM,
+                               MOBILE_AP_ERROR_RESOURCE);
+                       g_context = NULL;
+               } else {
+                       _emit_mobileap_dbus_signal(obj, E_SIGNAL_USB_TETHER_OFF,
+                                       SIGNAL_MSG_NOT_AVAIL_INTERFACE);
+               }
+       }
+
+       usb_client_state = value;
+}
+
+int _dbus_register_usb_state_change_signal(void *data)
+{
+       mobile_ap_error_code_e ret = MOBILE_AP_ERROR_NONE;
+       GError *error = NULL;
+
+#if !GLIB_CHECK_VERSION(2,36,0)
+       g_type_init();
+#endif
+
+       conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
+       if (error) {
+               ERR("Error occurred (%s)", error->message);
+               g_error_free(error);
+               ret = MOBILE_AP_ERROR_RESOURCE;
+               goto FAIL;
+       }
+       if (!conn) {
+               ERR("Failed to get gdbus connection");
+               ret = MOBILE_AP_ERROR_RESOURCE;
+               goto FAIL;
+       }
+
+       subscription_id = g_dbus_connection_signal_subscribe(
+                                               conn, NULL, USB_SYSTEM_DEVICE_IFACE,
+                                               USB_STATE_CHANGE_SIGNAL, USB_SYSTEM_DEVICE_PATH, NULL,
+                                               G_DBUS_SIGNAL_FLAGS_NONE, __usb_device_state_change_cb,
+                                               data, NULL);
+
+       if (subscription_id == 0) {
+               ERR("Failed to subscribe signal (%d)", USB_STATE_CHANGE_SIGNAL);
+               ret = MOBILE_AP_ERROR_RESOURCE;
+               goto FAIL;
+       }
+
+       usb_client_state = -1;
+       DBG("Successfully Subscribed USB State Signal Handler");
+       return ret;
+
+FAIL:
+       if (conn)
+               g_object_unref(conn);
+       return ret;
+}
+
+static void __handle_usb_disconnect_cb(keynode_t *key, void *data)
+{
+       if (key == NULL || data == NULL) {
+               ERR("Parameter is NULL\n");
+               return;
+       }
+
+       char *vconf_name;
+       int vconf_key;
+       TetheringObject *obj = (TetheringObject *)data;
+
+       if (!_mobileap_is_enabled(MOBILE_AP_STATE_USB)) {
+               ERR("USB tethering is not enabled\n");
+               return;
+       }
+
+       if (vconf_keynode_get_type(key) != VCONF_TYPE_INT) {
+               ERR("Invalid vconf key type\n");
+               return;
+       }
+
+       vconf_name = vconf_keynode_get_name(key);
+       vconf_key = vconf_keynode_get_int(key);
+       SDBG("key = %s, value = %d(int)\n", vconf_name, vconf_key);
+
+       /*
+        * P140305-02551: Disconnected State is implemented from DBUS instead of
+        * VCONF key.
+        */
+       if (usb_client_state == USBCLIENT_STATE_DISCONNECTED)
+               DBG("USB is disconnected\n");
+       else if (vconf_name && !strcmp(vconf_name, VCONFKEY_SETAPPL_USB_MODE_INT) &&
+                       vconf_key != SETTING_USB_TETHERING_MODE)
+               SDBG("USB Mode is changed [%d]\n", vconf_key);
+       else
+               return;
+
+       _disable_usb_tethering(obj);
+
+       if (g_context) {
+               dbus_g_method_return(g_context,
+                               MOBILE_AP_ENABLE_USB_TETHERING_CFM,
+                               MOBILE_AP_ERROR_RESOURCE);
+               g_context = NULL;
+       } else {
+               _emit_mobileap_dbus_signal(obj, E_SIGNAL_USB_TETHER_OFF,
+                               SIGNAL_MSG_NOT_AVAIL_INTERFACE);
+       }
+
+       return;
+}
+
+static void __handle_usb_mode_change(keynode_t *key, void *data)
+{
+       if (key == NULL || data == NULL) {
+               ERR("Parameter is NULL\n");
+               return;
+       }
+
+       TetheringObject *obj = (TetheringObject *)data;
+       int ret;
+       int vconf_key;
+       guint idle_id;
+
+       if (vconf_keynode_get_type(key) != VCONF_TYPE_INT) {
+               ERR("Invalid vconf key\n");
+               return;
+       }
+
+       vconf_key = vconf_keynode_get_int(key);
+       SDBG("key = %s, value = %d(int)\n",
+                       vconf_keynode_get_name(key), vconf_key);
+
+       if (_mobileap_is_enabled(MOBILE_AP_STATE_USB)) {
+               if (vconf_key != SETTING_USB_TETHERING_MODE) {
+                       DBG("Is progressing for usb mode change\n");
+                       return;
+               }
+
+               DBG("USB tethering is enabled\n");
+               vconf_ignore_key_changed(VCONFKEY_SETAPPL_USB_MODE_INT,
+                               __handle_usb_mode_change);
+
+               /* USB Mode change is handled while USB tethering is enabled */
+               vconf_notify_key_changed(VCONFKEY_SETAPPL_USB_MODE_INT,
+                               __handle_usb_disconnect_cb, (void *)obj);
+               ret = vconf_get_int(VCONFKEY_SETAPPL_USB_MODE_INT, &vconf_key);
+               if (ret != 0) {
+                       ERR("vconf_get_int is failed. but ignored [%d]\n", ret);
+               }
+
+               if (vconf_key != SETTING_USB_TETHERING_MODE) {
+                       ERR("USB Mode is changed suddenly\n");
+                       _disable_usb_tethering(obj);
+                       if (g_context) {
+                               dbus_g_method_return(g_context,
+                                               MOBILE_AP_ENABLE_USB_TETHERING_CFM,
+                                               MOBILE_AP_ERROR_RESOURCE);
+                               g_context = NULL;
+                       }
+                       return;
+               }
+               _add_interface_routing(USB_IF, IP_ADDRESS_USB);
+               _add_routing_rule(USB_IF);
+
+               _emit_mobileap_dbus_signal(obj, E_SIGNAL_USB_TETHER_ON, NULL);
+               if (g_context) {
+                       dbus_g_method_return(g_context,
+                                       MOBILE_AP_ENABLE_USB_TETHERING_CFM,
+                                       MOBILE_AP_ERROR_NONE);
+                       g_context = NULL;
+               }
+       } else {
+               if (vconf_key == SETTING_USB_TETHERING_MODE) {
+                       DBG("Is progressing for usb mode change\n");
+                       return;
+               }
+
+               DBG("USB tethering is disabled\n");
+               vconf_ignore_key_changed(VCONFKEY_SETAPPL_USB_MODE_INT,
+                               __handle_usb_mode_change);
+               _emit_mobileap_dbus_signal(obj, E_SIGNAL_USB_TETHER_OFF, NULL);
+               if (g_context) {
+                       dbus_g_method_return(g_context,
+                                       MOBILE_AP_DISABLE_USB_TETHERING_CFM,
+                                       MOBILE_AP_ERROR_NONE);
+                       g_context = NULL;
+               }
+
+               in_progress = FALSE;
+               idle_id = g_idle_add(_terminate_mobileap_agent, NULL);
+               if (idle_id == 0)
+                       ERR("g_idle_add is failed\n");
+       }
+}
+
+mobile_ap_error_code_e _enable_usb_tethering(TetheringObject *obj)
+{
+       mobile_ap_error_code_e ret = MOBILE_AP_ERROR_NONE;
+       int vconf_ret;
+       int usb_mode = SETTING_USB_NONE_MODE;
+
+       if (_mobileap_is_enabled(MOBILE_AP_STATE_USB)) {
+               ERR("USB tethering is already enabled\n");
+               ret = MOBILE_AP_ERROR_ALREADY_ENABLED;
+               return ret;
+       }
+
+       if (_mobileap_is_enabled(MOBILE_AP_STATE_WIFI_AP)) {
+               ERR("Wi-Fi AP is enabled\n");
+               ret = MOBILE_AP_ERROR_RESOURCE;
+               return ret;
+       }
+
+       /* Register DBus Signal Handler for USB Client State */
+       if (_dbus_register_usb_state_change_signal(obj) != MOBILE_AP_ERROR_NONE) {
+               ERR("Failed to register dbus signal(%d)", ret);
+               ret = MOBILE_AP_ERROR_RESOURCE;
+               goto FAIL;
+       }
+
+       if (!_mobileap_set_state(MOBILE_AP_STATE_USB)) {
+               ret = MOBILE_AP_ERROR_RESOURCE;
+               goto FAIL;
+       }
+
+       ret = _init_tethering(obj);
+       if (ret != MOBILE_AP_ERROR_NONE) {
+               goto FAIL;
+       }
+
+       vconf_notify_key_changed(VCONFKEY_SETAPPL_USB_MODE_INT,
+                       __handle_usb_mode_change, (void *)obj);
+
+       vconf_ret = vconf_get_int(VCONFKEY_SETAPPL_USB_MODE_INT, &usb_mode);
+       if (vconf_ret != 0) {
+               ERR("Error getting vconf\n");
+               vconf_ignore_key_changed(VCONFKEY_SETAPPL_USB_MODE_INT,
+                               __handle_usb_mode_change);
+               _deinit_tethering(obj);
+               ret = MOBILE_AP_ERROR_RESOURCE;
+               goto FAIL;
+       }
+
+       if (usb_mode == SETTING_USB_TETHERING_MODE) {
+               vconf_ignore_key_changed(VCONFKEY_SETAPPL_USB_MODE_INT,
+                               __handle_usb_mode_change);
+               _add_interface_routing(USB_IF, IP_ADDRESS_USB);
+               _add_routing_rule(USB_IF);
+       }
+
+       DBG("-\n");
+       return MOBILE_AP_ERROR_NONE;
+
+FAIL:
+       /* Clear DBus Signal Handler for USB Client State */
+       if (conn) {
+               if (subscription_id > 0) {
+                       g_dbus_connection_signal_unsubscribe(conn, subscription_id);
+                       subscription_id = 0;
+               }
+               g_object_unref(conn);
+               conn = NULL;
+       }
+       _mobileap_clear_state(MOBILE_AP_STATE_USB);
+
+       return ret;
+}
+
+mobile_ap_error_code_e _disable_usb_tethering(TetheringObject *obj)
+{
+       mobile_ap_error_code_e ret = MOBILE_AP_ERROR_NONE;
+
+       if (!_mobileap_is_enabled(MOBILE_AP_STATE_USB)) {
+               ERR("USB tethering has not been enabled\n");
+               ret = MOBILE_AP_ERROR_NOT_ENABLED;
+               return ret;
+       }
+
+       _deinit_tethering(obj);
+
+       if (_remove_station_info_all(MOBILE_AP_TYPE_USB) != MOBILE_AP_ERROR_NONE) {
+               ERR("_remove_station_info_all is failed. Ignore it\n");
+       }
+
+       /* Clear DBus Signal Handler for USB Client State */
+       if (conn) {
+               if (subscription_id > 0) {
+                       g_dbus_connection_signal_unsubscribe(conn, subscription_id);
+                       subscription_id = 0;
+               }
+               g_object_unref(conn);
+               conn = NULL;
+       }
+
+       vconf_ignore_key_changed(VCONFKEY_SETAPPL_USB_MODE_INT,
+                       __handle_usb_disconnect_cb);
+
+       _mobileap_clear_state(MOBILE_AP_STATE_USB);
+       _del_routing_rule(USB_IF);
+       _del_interface_routing(USB_IF, IP_ADDRESS_USB);
+
+       DBG("_disable_usb_tethering is done\n");
+
+       return ret;
+}
+
+gboolean tethering_enable_usb_tethering(TetheringObject *obj,
+               DBusGMethodInvocation *context)
+{
+       mobile_ap_error_code_e ret = MOBILE_AP_ERROR_NONE;
+       gboolean ret_val = FALSE;
+
+       DBG("+\n");
+
+       g_assert(obj != NULL);
+       g_assert(context != NULL);
+
+       if (g_context) {
+               DBG("It is turnning on\n");
+               dbus_g_method_return(context,
+                               MOBILE_AP_ENABLE_USB_TETHERING_CFM,
+                               MOBILE_AP_ERROR_IN_PROGRESS);
+               return FALSE;
+       }
+
+       g_context = context;
+
+       ret = _enable_usb_tethering(obj);
+       if (ret != MOBILE_AP_ERROR_NONE) {
+               ERR("_enable_usb_tethering() is failed : %d\n", ret);
+               goto DONE;
+       } else {
+               DBG("Don't need to wait for usb-setting\n");
+               _emit_mobileap_dbus_signal(obj, E_SIGNAL_USB_TETHER_ON, NULL);
+               ret_val = TRUE;
+       }
+
+DONE:
+       dbus_g_method_return(g_context,
+                       MOBILE_AP_ENABLE_USB_TETHERING_CFM, ret);
+       g_context = NULL;
+
+       return ret_val;
+}
+
+gboolean tethering_disable_usb_tethering(TetheringObject *obj,
+               DBusGMethodInvocation *context)
+{
+       mobile_ap_error_code_e ret = MOBILE_AP_ERROR_NONE;
+       int usb_mode = SETTING_USB_NONE_MODE;
+       int vconf_ret = 0;
+
+       DBG("+\n");
+
+       g_assert(obj != NULL);
+       g_assert(context != NULL);
+
+       if (g_context) {
+               DBG("It is turnning on\n");
+               dbus_g_method_return(context,
+                               MOBILE_AP_DISABLE_USB_TETHERING_CFM,
+                               MOBILE_AP_ERROR_IN_PROGRESS);
+               return FALSE;
+       }
+
+       g_context = context;
+
+       ret = _disable_usb_tethering(obj);
+       if (ret != MOBILE_AP_ERROR_NONE) {
+               dbus_g_method_return(g_context,
+                               MOBILE_AP_DISABLE_USB_TETHERING_CFM, ret);
+               g_context = NULL;
+               return FALSE;
+       }
+
+       vconf_notify_key_changed(VCONFKEY_SETAPPL_USB_MODE_INT,
+                       __handle_usb_mode_change, (void *)obj);
+       vconf_ret = vconf_get_int(VCONFKEY_SETAPPL_USB_MODE_INT, &usb_mode);
+       if (vconf_ret != 0) {
+               ERR("Error getting vconf : %d. This error is ignored\n", vconf_ret);
+               goto DONE;
+       }
+
+       if (usb_mode != SETTING_USB_TETHERING_MODE) {
+               DBG("Don't need to wait for usb-setting\n");
+               goto DONE;
+       }
+
+       in_progress = TRUE;
+
+       DBG("-\n");
+       return TRUE;
+
+DONE:
+       vconf_ignore_key_changed(VCONFKEY_SETAPPL_USB_MODE_INT,
+                       __handle_usb_mode_change);
+       _emit_mobileap_dbus_signal(obj, E_SIGNAL_USB_TETHER_OFF, NULL);
+       dbus_g_method_return(g_context,
+                       MOBILE_AP_DISABLE_USB_TETHERING_CFM, ret);
+       g_context = NULL;
+
+       return TRUE;
+}
+
+gboolean _is_trying_usb_operation(void)
+{
+       return (g_context ? TRUE : FALSE || in_progress);
+}
diff --git a/src/mobileap_wifi.c b/src/mobileap_wifi.c
new file mode 100755 (executable)
index 0000000..8f6d505
--- /dev/null
@@ -0,0 +1,919 @@
+/*
+ * mobileap-agent
+ * Copyright (c) 2012 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the License);
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#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 <wifi.h>
+#include <wifi-direct.h>
+
+#include "mobileap_softap.h"
+#include "mobileap_common.h"
+#include "mobileap_wifi.h"
+#include "mobileap_handler.h"
+#include "mobileap_notification.h"
+
+#define WIFI_RECOVERY_GUARD_TIME       1000    /* ms */
+
+static mobile_ap_error_code_e __update_softap_settings(softap_settings_t *st,
+       gchar *ssid, gchar *passphrase, int hide_mode, softap_security_type_e security_type);
+static int __turn_off_wifi(TetheringObject *obj);
+
+static DBusGMethodInvocation *g_context = NULL;
+static guint wifi_recovery_timeout_id = 0;
+static gboolean prev_wifi_on = FALSE;
+static wifi_saved_settings wifi_settings = {0, NULL, NULL, 0};
+
+static void _wifi_direct_state_cb(int error_code, wifi_direct_device_state_e state, void *user_data)
+{
+       bool wifi_state = false;
+
+       DBG("+\n");
+
+       if (user_data == NULL) {
+               ERR("The param is NULL\n");
+               return;
+       }
+
+       TetheringObject *obj = (TetheringObject *)user_data;
+       int ret = 0;
+
+       if (state != WIFI_DIRECT_DEVICE_STATE_DEACTIVATED) {
+               ERR("Unknown state : %d\n", state);
+               return;
+       }
+
+       wifi_direct_unset_device_state_changed_cb();
+       wifi_direct_deinitialize();
+
+       if (error_code != 0) {
+               ERR("wifi_direct_deactivate fail in cb : %d\n", error_code);
+               ret = MOBILE_AP_ERROR_INTERNAL;
+               goto DONE;
+       }
+       DBG("Wi-Fi direct is turned off\n");
+
+       wifi_is_activated(&wifi_state);
+       if (wifi_state) {
+               DBG("Wi-Fi is turned on. Turn off Wi-Fi");
+               if (__turn_off_wifi(obj) != MOBILE_AP_ERROR_NONE) {
+                       ERR("_turn_off_wifi is failed\n");
+                       ret = MOBILE_AP_ERROR_INTERNAL;
+                       goto DONE;
+               }
+               return;
+       }
+
+       ret = _enable_wifi_tethering(obj, wifi_settings.ssid, wifi_settings.key,
+                       wifi_settings.hide_mode, wifi_settings.security_type);
+       if (ret != MOBILE_AP_ERROR_NONE) {
+               ERR("_enable_wifi_tethering is failed\n");
+       } else {
+               _emit_mobileap_dbus_signal(obj, E_SIGNAL_WIFI_TETHER_ON, NULL);
+       }
+
+DONE:
+       dbus_g_method_return(g_context,
+                       MOBILE_AP_ENABLE_WIFI_TETHERING_CFM, ret);
+       g_context = NULL;
+
+       g_free(wifi_settings.ssid);
+       g_free(wifi_settings.key);
+       memset(&wifi_settings, 0, sizeof(wifi_settings));
+
+       DBG("-\n");
+       return;
+}
+
+static void __wifi_activated_cb(wifi_error_e result, void *user_data)
+{
+       DBG("Wi-Fi on is done\n");
+
+       return;
+}
+
+static void __wifi_deactivated_cb(wifi_error_e result, void *user_data)
+{
+       DBG("+\n");
+
+       if (user_data == NULL) {
+               ERR("The param is NULL\n");
+               return;
+       }
+
+       TetheringObject *obj = (TetheringObject *)user_data;
+       int ret;
+
+       if (result != WIFI_ERROR_NONE) {
+               ERR("__wifi_deactivated_cb error : %d\n", result);
+               ret = MOBILE_AP_ERROR_INTERNAL;
+               goto DONE;
+       }
+
+       DBG("Wi-Fi is turned off\n");
+
+       ret = _enable_wifi_tethering(obj, wifi_settings.ssid, wifi_settings.key,
+                       wifi_settings.hide_mode, wifi_settings.security_type);
+       if (ret != MOBILE_AP_ERROR_NONE) {
+               ERR("_enable_wifi_tethering is failed\n");
+       } else {
+               prev_wifi_on = TRUE;
+               _emit_mobileap_dbus_signal(obj, E_SIGNAL_WIFI_TETHER_ON, NULL);
+       }
+
+DONE:
+       dbus_g_method_return(g_context,
+                       MOBILE_AP_ENABLE_WIFI_TETHERING_CFM, ret);
+       g_context = NULL;
+
+       g_free(wifi_settings.ssid);
+       g_free(wifi_settings.key);
+       memset(&wifi_settings, 0, sizeof(wifi_settings));
+
+       DBG("-\n");
+       return;
+}
+
+static int __turn_off_wifi(TetheringObject *obj)
+{
+       int ret;
+
+       ret = wifi_deactivate(__wifi_deactivated_cb, (void *)obj);
+       if (ret != WIFI_ERROR_NONE) {
+               ERR("wifi_deactivate() is failed : %d\n", ret);
+               return MOBILE_AP_ERROR_INTERNAL;
+       }
+
+       return MOBILE_AP_ERROR_NONE;
+}
+
+static gboolean __turn_on_wifi_timeout_cb(gpointer user_data)
+{
+       int ret;
+       guint idle_id;
+
+       wifi_recovery_timeout_id = 0;
+
+       ret = wifi_activate(__wifi_activated_cb, NULL);
+       if (ret != WIFI_ERROR_NONE) {
+               ERR("wifi_activate() is failed : %d\n", ret);
+       }
+
+       idle_id = g_idle_add(_terminate_mobileap_agent, NULL);
+       if (idle_id == 0) {
+               ERR("g_idle_add is failed\n");
+       }
+
+       return FALSE;
+}
+
+static int __turn_on_wifi(void)
+{
+       if (wifi_recovery_timeout_id > 0) {
+               g_source_remove(wifi_recovery_timeout_id);
+               wifi_recovery_timeout_id = 0;
+       }
+
+       wifi_recovery_timeout_id = g_timeout_add(WIFI_RECOVERY_GUARD_TIME,
+                       __turn_on_wifi_timeout_cb, NULL);
+       if (wifi_recovery_timeout_id == 0) {
+               ERR("g_timeout_add is failed\n");
+               return MOBILE_AP_ERROR_INTERNAL;
+       }
+
+       return MOBILE_AP_ERROR_NONE;
+}
+
+static gboolean __is_wifi_direct_on(void)
+{
+       int wifi_direct_state = 0;
+       int ret;
+
+       ret = vconf_get_int(VCONFKEY_WIFI_DIRECT_STATE, &wifi_direct_state);
+       if (ret < 0) {
+               ERR("vconf_get_int() is failed : %d\n", ret);
+               return FALSE;
+       }
+
+       return wifi_direct_state != 0 ? TRUE : FALSE;
+}
+
+static int __turn_off_wifi_direct(TetheringObject *obj)
+{
+       int ret;
+
+       ret = wifi_direct_initialize();
+       if (ret < 0) {
+               ERR("wifi_direct_initialize() is failed : %d\n", ret);
+               return MOBILE_AP_ERROR_INTERNAL;
+       }
+
+       ret = wifi_direct_set_device_state_changed_cb(_wifi_direct_state_cb, (void *)obj);
+       if (ret < 0) {
+               ERR("wifi_direct_set_device_state_changed_cb() is failed : %d\n", ret);
+               ret = wifi_direct_deinitialize();
+               DBG("wifi_direct_deinitialize() ret : %d\n", ret);
+               return MOBILE_AP_ERROR_INTERNAL;
+       }
+
+       ret = wifi_direct_deactivate();
+       if (ret < 0) {
+               ERR("wifi_direct_deactivate() is failed : %d\n", ret);
+               ret = wifi_direct_unset_device_state_changed_cb();
+               DBG("wifi_direct_unset_device_state_changed_cb() ret : %d\n", ret);
+               ret = wifi_direct_deinitialize();
+               DBG("wifi_direct_deinitialize() ret : %d\n", ret);
+               return MOBILE_AP_ERROR_INTERNAL;
+       }
+
+       return MOBILE_AP_ERROR_NONE;
+}
+
+static mobile_ap_error_code_e __update_softap_settings(softap_settings_t *st,
+       gchar *ssid, gchar *passphrase, int hide_mode, softap_security_type_e security_type)
+{
+       if (st == NULL) {
+               ERR("Invalid param\n");
+               return MOBILE_AP_ERROR_INVALID_PARAM;
+       }
+
+       g_strlcpy(st->ssid, ssid, sizeof(st->ssid));
+
+       if (security_type == SOFTAP_SECURITY_TYPE_WPA2_PSK) {
+               g_strlcpy(st->security_type, SOFTAP_SECURITY_TYPE_WPA2_PSK_STR,
+                       sizeof(st->security_type));
+               g_strlcpy(st->key, passphrase, sizeof(st->key));
+       } else if (security_type == SOFTAP_SECURITY_TYPE_OPEN) {
+               g_strlcpy(st->security_type, SOFTAP_SECURITY_TYPE_OPEN_STR,
+                       sizeof(st->security_type));
+               g_strlcpy(st->key, "00000000", sizeof(st->key));
+       } else {
+               ERR("Unknown security type\n");
+               return MOBILE_AP_ERROR_INTERNAL;
+       }
+
+       st->hide_mode = hide_mode;
+
+       SDBG("ssid : %s security type : %s hide mode : %d\n",
+                       st->ssid, st->security_type, st->hide_mode);
+
+       return MOBILE_AP_ERROR_NONE;
+}
+
+static gboolean __is_equal_softap_settings(softap_settings_t *a, softap_settings_t *b)
+{
+       if (a->hide_mode != b->hide_mode)
+               return FALSE;
+
+       if (strcmp(a->ssid, b->ssid) != 0)
+               return FALSE;
+
+       if (strcmp(a->key, b->key) != 0)
+               return FALSE;
+
+       if (strcmp(a->security_type, b->security_type) != 0)
+               return FALSE;
+
+       return TRUE;
+}
+
+mobile_ap_error_code_e _reload_softap_settings(TetheringObject *obj,
+               gchar *ssid, gchar *key, gint hide_mode, gint security_type)
+{
+       gboolean backup_prev_wifi_on = prev_wifi_on;
+       mobile_ap_error_code_e ret;
+       softap_settings_t *old_settings = &obj->softap_settings;
+       softap_settings_t new_settings;
+
+       if (obj == NULL || ssid == NULL || !strlen(ssid)) {
+               ERR("invalid parameters\n");
+               return MOBILE_AP_ERROR_INVALID_PARAM;
+       }
+
+       if (!_mobileap_is_enabled(MOBILE_AP_STATE_WIFI))
+               return MOBILE_AP_ERROR_NONE;
+
+       ret = __update_softap_settings(&new_settings, ssid, key, hide_mode,
+                       (softap_security_type_e)security_type);
+       if (ret != MOBILE_AP_ERROR_NONE) {
+               ERR("__update_softap_settings is failed\n");
+               return ret;
+       }
+
+       if (__is_equal_softap_settings(&new_settings, old_settings) == TRUE) {
+               DBG("No need to reload settings\n");
+               return MOBILE_AP_ERROR_NONE;
+       }
+
+       prev_wifi_on = FALSE;
+       ret = _disable_wifi_tethering(obj);
+
+       prev_wifi_on = backup_prev_wifi_on;
+       if (ret != MOBILE_AP_ERROR_NONE) {
+               ERR("_disable_wifi_tethering is failed : %d\n", ret);
+               return ret;
+       }
+
+       ret = _enable_wifi_tethering(obj, ssid, key, hide_mode,
+                       (softap_security_type_e)security_type);
+       if (ret != MOBILE_AP_ERROR_NONE) {
+               ERR("_enable_wifi_tethering is failed : %d\n", ret);
+               return ret;
+       }
+       _emit_mobileap_dbus_signal(obj, E_SIGNAL_WIFI_TETHER_ON, NULL);
+
+       return MOBILE_AP_ERROR_NONE;
+}
+
+mobile_ap_error_code_e _reload_softap_settings_for_ap(TetheringObject *obj,
+       gchar *ssid, gchar *key, gint hide_mode, gint security_type)
+{
+       gboolean backup_prev_wifi_on = prev_wifi_on;
+       mobile_ap_error_code_e ret;
+       softap_settings_t *old_settings = &obj->softap_settings;
+       softap_settings_t new_settings;
+
+       if (obj == NULL || ssid == NULL || !strlen(ssid)) {
+               ERR("invalid parameters\n");
+               return MOBILE_AP_ERROR_INVALID_PARAM;
+       }
+
+       ret = __update_softap_settings(&new_settings, ssid, key, hide_mode,
+                       (softap_security_type_e)security_type);
+       if (ret != MOBILE_AP_ERROR_NONE) {
+               ERR("__update_softap_settings is failed\n");
+               return ret;
+       }
+
+       if (__is_equal_softap_settings(&new_settings, old_settings) == TRUE) {
+               DBG("No need to reload settings\n");
+               return MOBILE_AP_ERROR_NONE;
+       }
+
+       prev_wifi_on = FALSE;
+
+       ret = _disable_wifi_ap(obj);
+       prev_wifi_on = backup_prev_wifi_on;
+       if (ret != MOBILE_AP_ERROR_NONE) {
+               ERR("_disable_wifi_ap is failed : %d\n", ret);
+               return ret;
+       }
+
+       ret = _enable_wifi_ap(obj, ssid, key, hide_mode,
+                       (softap_security_type_e)security_type);
+       if (ret != MOBILE_AP_ERROR_NONE) {
+               ERR("_enable_wifi_ap is failed : %d\n", ret);
+               return ret;
+       }
+
+       return MOBILE_AP_ERROR_NONE;
+}
+
+int _get_wifi_name_from_lease_info(const char *mac, char **name_buf)
+{
+       if (mac == NULL || name_buf == NULL) {
+               return MOBILE_AP_ERROR_INVALID_PARAM;
+       }
+
+       GIOChannel *io = NULL;
+       char *line = NULL;
+       char *device_name = MOBILE_AP_NAME_UNKNOWN;
+       char ip_addr[MOBILE_AP_STR_INFO_LEN] = {0, };
+       char mac_addr[MOBILE_AP_STR_INFO_LEN] = {0, };
+       char name[MOBILE_AP_STR_HOSTNAME_LEN] = {0, };
+       char expire[MOBILE_AP_STR_INFO_LEN] = {0, };
+       char extra[MOBILE_AP_STR_INFO_LEN] = {0, };
+
+       io = g_io_channel_new_file(DNSMASQ_LEASES_FILE, "r", NULL);
+       if (io == NULL) {
+               return MOBILE_AP_ERROR_RESOURCE;
+       }
+
+       while (g_io_channel_read_line(io, &line, NULL, NULL, NULL) ==
+                       G_IO_STATUS_NORMAL) {
+               sscanf(line, "%19s %19s %19s %19s %19s",
+                               expire, mac_addr, ip_addr, name, extra);
+               g_free(line);
+
+               if (g_ascii_strcasecmp(mac_addr, mac) == 0) {
+                       if (g_strcmp0(name, "*") != 0)
+                               device_name = name;
+                       break;
+               }
+       }
+       g_io_channel_unref(io);
+
+       *name_buf = g_strdup(device_name);
+
+       return MOBILE_AP_ERROR_NONE;
+}
+
+void _add_wifi_device_to_array(softap_device_info_t *di, GPtrArray *array)
+{
+       int i = 0;
+       GIOChannel *io = NULL;
+       gchar *line = NULL;
+       gchar *device_name = NULL;
+       gchar ip_addr[MOBILE_AP_STR_INFO_LEN] = {0, };
+       gchar mac_addr[MOBILE_AP_STR_INFO_LEN] = {0, };
+       gchar name[MOBILE_AP_STR_HOSTNAME_LEN] = {0, };
+       gchar expire[MOBILE_AP_STR_INFO_LEN] = {0, };
+       gchar extra[MOBILE_AP_STR_INFO_LEN] = {0, };
+
+       int found = 0;
+
+       for (i = 0; i < di->number; i++)
+               SDBG("bssid[%d]:%s\n", i, di->bssid[i]);
+
+       DBG("Number of connected device:%d\n", di->number);
+
+       io = g_io_channel_new_file(DNSMASQ_LEASES_FILE, "r", NULL);
+
+       while (g_io_channel_read_line(io, &line, NULL, NULL, NULL) ==
+                                                       G_IO_STATUS_NORMAL) {
+               sscanf(line, "%19s %19s %19s %19s %19s", expire, mac_addr,
+                                                       ip_addr, name, extra);
+               SDBG("mac_addr:%s ip_addr:%s name:%s expire:%s\n", mac_addr,
+                                                       ip_addr, name, expire);
+
+               for (i = 0; i < di->number; i++) {
+                       if (g_ascii_strcasecmp(di->bssid[i], mac_addr) == 0) {
+                               if (!strcmp(name, "*"))
+                                       device_name = MOBILE_AP_NAME_UNKNOWN;
+                               else
+                                       device_name = name;
+
+                               _mh_core_add_data_to_array(array, MOBILE_AP_TYPE_WIFI,
+                                                               device_name);
+
+                               found++;
+
+                               break;
+                       }
+               }
+
+               g_free(line);
+       }
+       g_io_channel_unref(io);
+
+       /* Set the name UNKNOWN unless we got the name. */
+       for (i = found; i < di->number; i++) {
+               _mh_core_add_data_to_array(array, MOBILE_AP_TYPE_WIFI,
+                                                       MOBILE_AP_NAME_UNKNOWN);
+       }
+}
+
+mobile_ap_error_code_e _enable_wifi_tethering(TetheringObject *obj, gchar *ssid,
+       gchar *passphrase, int hide_mode, softap_security_type_e security_type)
+{
+       mobile_ap_error_code_e ret;
+
+       if (obj == NULL || ssid == NULL || !strlen(ssid)) {
+               ERR("invalid parameters\n");
+               return MOBILE_AP_ERROR_INVALID_PARAM;
+       }
+
+       if (security_type == SOFTAP_SECURITY_TYPE_WPA2_PSK && passphrase == NULL) {
+               ERR("passphrase is null\n");
+               return MOBILE_AP_ERROR_INVALID_PARAM;
+       }
+
+       if (_mobileap_is_enabled(MOBILE_AP_STATE_WIFI_AP)) {
+               ERR("Wi-Fi AP is already enabled\n");
+               ret = MOBILE_AP_ERROR_RESOURCE;
+               return ret;
+       }
+
+       if (_mobileap_is_enabled(MOBILE_AP_STATE_WIFI)) {
+               ERR("Wi-Fi tethering is already enabled\n");
+               ret = MOBILE_AP_ERROR_ALREADY_ENABLED;
+               return ret;
+       }
+
+       /* Update global state */
+       if (!_mobileap_set_state(MOBILE_AP_STATE_WIFI)) {
+               ret = MOBILE_AP_ERROR_RESOURCE;
+               return ret;
+       }
+
+       /* Update Wi-Fi hotspot data to common object */
+       ret = __update_softap_settings(&obj->softap_settings, ssid, passphrase,
+                       hide_mode, security_type);
+       if (ret != MOBILE_AP_ERROR_NONE) {
+               _mobileap_clear_state(MOBILE_AP_STATE_WIFI);
+               return ret;
+       }
+
+       if (vconf_set_str(VCONFKEY_MOBILE_HOTSPOT_SSID,
+                               obj->softap_settings.ssid) < 0) {
+               ERR("vconf_set_str is failed\n");
+       }
+
+       /* Initialize tethering */
+       _block_device_sleep();
+       ret = _init_tethering(obj);
+       if (ret != MOBILE_AP_ERROR_NONE) {
+               _mobileap_clear_state(MOBILE_AP_STATE_WIFI);
+               goto DONE;
+       }
+
+       /* Upload driver */
+       ret = _mh_core_enable_softap(MOBILE_AP_TYPE_WIFI,
+                       obj->softap_settings.ssid,
+                       obj->softap_settings.security_type,
+                       obj->softap_settings.key,
+                       obj->softap_settings.hide_mode);
+       if (ret != MOBILE_AP_ERROR_NONE) {
+               _deinit_tethering(obj);
+               _mobileap_clear_state(MOBILE_AP_STATE_WIFI);
+               goto DONE;
+       }
+       _delete_timeout_noti();
+
+       _init_timeout_cb(MOBILE_AP_TYPE_WIFI, (void *)obj);
+       _start_timeout_cb(MOBILE_AP_TYPE_WIFI, time(NULL) + TETHERING_CONN_TIMEOUT);
+
+       _add_interface_routing(WIFI_IF, IP_ADDRESS_SOFTAP);
+       _add_routing_rule(WIFI_IF);
+
+DONE:
+       _unblock_device_sleep();
+       return ret;
+}
+
+mobile_ap_error_code_e _enable_wifi_ap(TetheringObject *obj,
+                gchar *ssid, gchar *passphrase, int hide_mode,
+                softap_security_type_e security_type)
+{
+       mobile_ap_error_code_e ret = MOBILE_AP_ERROR_NONE;
+
+       if (obj == NULL || ssid == NULL || !strlen(ssid)) {
+               ERR("invalid parameters\n");
+               return MOBILE_AP_ERROR_INVALID_PARAM;
+       }
+
+       if (security_type == SOFTAP_SECURITY_TYPE_WPA2_PSK &&
+               (passphrase == NULL || strlen(passphrase) >= MOBILE_AP_WIFI_KEY_MAX_LEN)) {
+               ERR("hex key length is not correct\n");
+               return MOBILE_AP_ERROR_INVALID_PARAM;
+       }
+
+       if (_mobileap_is_enabled(MOBILE_AP_STATE_WIFI | MOBILE_AP_STATE_BT
+                       | MOBILE_AP_STATE_USB)) {
+               ERR("Tethering is already enabled\n");
+               return MOBILE_AP_ERROR_RESOURCE;
+       }
+
+       if (_mobileap_is_enabled(MOBILE_AP_STATE_WIFI_AP)) {
+               ERR("Wi-Fi AP is already enabled\n");
+               return MOBILE_AP_ERROR_ALREADY_ENABLED;
+       }
+
+       if (!_mobileap_set_state(MOBILE_AP_STATE_WIFI_AP)) {
+               return MOBILE_AP_ERROR_RESOURCE;
+       }
+
+       ret = __update_softap_settings(&obj->softap_settings, ssid, passphrase,
+                       hide_mode, security_type);
+       if (ret != MOBILE_AP_ERROR_NONE) {
+               _mobileap_clear_state(MOBILE_AP_STATE_WIFI_AP);
+               return ret;
+       }
+
+       _block_device_sleep();
+
+       if (_init_tethering(obj) != MOBILE_AP_ERROR_NONE) {
+               _mobileap_clear_state(MOBILE_AP_STATE_WIFI_AP);
+               ret = MOBILE_AP_ERROR_RESOURCE;
+               goto DONE;
+       }
+
+       /* Upload driver */
+       ret = _mh_core_enable_softap(MOBILE_AP_TYPE_WIFI_AP,
+                       obj->softap_settings.ssid,
+                       obj->softap_settings.security_type,
+                       obj->softap_settings.key,
+                       obj->softap_settings.hide_mode);
+       if (ret != MOBILE_AP_ERROR_NONE) {
+               _deinit_tethering(obj);
+               _mobileap_clear_state(MOBILE_AP_STATE_WIFI_AP);
+               goto DONE;
+       }
+
+       _delete_timeout_noti();
+       _init_timeout_cb(MOBILE_AP_TYPE_WIFI_AP, (void *)obj);
+       _start_timeout_cb(MOBILE_AP_TYPE_WIFI_AP, time(NULL) + WIFI_AP_CONN_TIMEOUT);
+       _add_interface_routing(WIFI_IF, IP_ADDRESS_SOFTAP);
+       _add_routing_rule(WIFI_IF);
+
+DONE:
+       _unblock_device_sleep();
+       return ret;
+}
+
+mobile_ap_error_code_e _disable_wifi_tethering(TetheringObject *obj)
+{
+       int ret;
+       int state;
+       mobile_ap_type_e type;
+
+       type = MOBILE_AP_TYPE_WIFI;
+       state = MOBILE_AP_STATE_WIFI;
+
+       if (!_mobileap_is_enabled(state)) {
+               ERR("Wi-Fi tethering ap has not been activated\n");
+               ret = MOBILE_AP_ERROR_NOT_ENABLED;
+               return ret;
+       }
+
+       _block_device_sleep();
+       _del_routing_rule(WIFI_IF);
+       _del_interface_routing(WIFI_IF, IP_ADDRESS_SOFTAP);
+       _flush_ip_address(WIFI_IF);
+       _deinit_timeout_cb(type);
+
+       if (_remove_station_info_all(type) != MOBILE_AP_ERROR_NONE) {
+               ERR("_remove_station_info_all is failed. Ignore it.\n");
+       }
+
+       ret = _mh_core_disable_softap();
+       if (ret != MOBILE_AP_ERROR_NONE) {
+               ERR("_mh_core_disable_softap is failed : %d\n", ret);
+               goto DONE;
+       }
+
+       _deinit_tethering(obj);
+       _mobileap_clear_state(state);
+
+       if (prev_wifi_on == TRUE) {
+               DBG("Previous Wi-Fi was turned on. Recover it\n");
+               if (__turn_on_wifi() != MOBILE_AP_ERROR_NONE) {
+                       ERR("__turn_on_wifi() is failed\n");
+               }
+               prev_wifi_on = FALSE;
+       }
+       DBG("_disable_wifi_tethering is done\n");
+
+DONE:
+       _unblock_device_sleep();
+       return ret;
+}
+
+mobile_ap_error_code_e _disable_wifi_ap(TetheringObject *obj)
+{
+       int ret;
+       int state;
+       mobile_ap_type_e type;
+
+       type = MOBILE_AP_TYPE_WIFI_AP;
+       state = MOBILE_AP_STATE_WIFI_AP;
+
+       if (!_mobileap_is_enabled(state)) {
+               ERR("Wi-Fi ap tethering has not been activated\n");
+               ret = MOBILE_AP_ERROR_NOT_ENABLED;
+               return ret;
+       }
+
+       _block_device_sleep();
+       _del_routing_rule(WIFI_IF);
+       _del_interface_routing(WIFI_IF, IP_ADDRESS_SOFTAP);
+       _flush_ip_address(WIFI_IF);
+       _deinit_timeout_cb(type);
+
+       if (_remove_station_info_all(type) != MOBILE_AP_ERROR_NONE) {
+               ERR("_remove_station_info_all is failed. Ignore it.\n");
+       }
+
+       ret = _mh_core_disable_softap();
+       if (ret != MOBILE_AP_ERROR_NONE) {
+               ERR("_mh_core_disable_softap is failed : %d\n", ret);
+               goto DONE;
+       }
+
+       _deinit_tethering(obj);
+       _mobileap_clear_state(state);
+
+       DBG("_disable_wifi_ap is done\n");
+
+DONE:
+       _unblock_device_sleep();
+       return ret;
+}
+
+gboolean tethering_enable_wifi_tethering(TetheringObject *obj, gchar *ssid,
+               gchar *key, gint visibility, gint security_type,
+               DBusGMethodInvocation *context)
+{
+       mobile_ap_error_code_e ret = MOBILE_AP_ERROR_NONE;
+       gboolean ret_val = FALSE;
+       bool wifi_state = false;
+
+       DBG("+\n");
+       g_assert(obj != NULL);
+       g_assert(context != NULL);
+
+       if (g_context) {
+               DBG("It is turnning on\n");
+               dbus_g_method_return(context,
+                               MOBILE_AP_ENABLE_WIFI_TETHERING_CFM,
+                               MOBILE_AP_ERROR_IN_PROGRESS);
+               return FALSE;
+       }
+
+       g_context = context;
+
+       wifi_settings.ssid = g_strdup(ssid);
+       if (security_type == SOFTAP_SECURITY_TYPE_WPA2_PSK) {
+               wifi_settings.key = g_strdup(key);
+               wifi_settings.security_type = SOFTAP_SECURITY_TYPE_WPA2_PSK;
+       } else {
+               wifi_settings.security_type = SOFTAP_SECURITY_TYPE_OPEN;
+       }
+       wifi_settings.hide_mode = (!visibility);
+
+       if (wifi_recovery_timeout_id) {
+               DBG("Wi-Fi recovery is cancelled\n");
+               g_source_remove(wifi_recovery_timeout_id);
+               wifi_recovery_timeout_id = 0;
+               prev_wifi_on = TRUE;
+       }
+
+       if (__is_wifi_direct_on() == TRUE) {
+               DBG("Wi-Fi and Wi-Fi direct are turned on\n");
+               if (__turn_off_wifi_direct(obj) != MOBILE_AP_ERROR_NONE) {
+                       ERR("_turn_off_wifi_direct is failed\n");
+                       ret = MOBILE_AP_ERROR_INTERNAL;
+                       goto DONE;
+               }
+
+               return TRUE;
+       }
+
+       wifi_is_activated(&wifi_state);
+       if (wifi_state == true) {
+               DBG("Wi-Fi is turned on\n");
+               if (__turn_off_wifi(obj) != MOBILE_AP_ERROR_NONE) {
+                       ERR("_turn_off_wifi is failed\n");
+                       ret = MOBILE_AP_ERROR_INTERNAL;
+                       goto DONE;
+               }
+
+               return TRUE;
+       }
+
+       ret = _enable_wifi_tethering(obj, ssid, key, !visibility,
+                       (softap_security_type_e)security_type);
+       if (ret != MOBILE_AP_ERROR_NONE) {
+               ERR("_enable_wifi_tethering is failed\n");
+       } else {
+               _emit_mobileap_dbus_signal(obj, E_SIGNAL_WIFI_TETHER_ON, NULL);
+               ret_val = TRUE;
+       }
+
+DONE:
+       dbus_g_method_return(g_context,
+                       MOBILE_AP_ENABLE_WIFI_TETHERING_CFM, ret);
+       g_context = NULL;
+
+       g_free(wifi_settings.ssid);
+       g_free(wifi_settings.key);
+       memset(&wifi_settings, 0, sizeof(wifi_settings));
+
+       return ret_val;
+}
+
+gboolean tethering_disable_wifi_tethering(TetheringObject *obj,
+               DBusGMethodInvocation *context)
+{
+       int ret = MOBILE_AP_ERROR_NONE;
+
+       DBG("+\n");
+       g_assert(obj != NULL);
+       g_assert(context != NULL);
+
+       ret = _disable_wifi_tethering(obj);
+
+       _emit_mobileap_dbus_signal(obj, E_SIGNAL_WIFI_TETHER_OFF, NULL);
+       dbus_g_method_return(context, MOBILE_AP_DISABLE_WIFI_TETHERING_CFM, ret);
+
+       if (ret != MOBILE_AP_ERROR_NONE)
+               return FALSE;
+
+       return TRUE;
+}
+
+gboolean tethering_enable_wifi_ap(TetheringObject *obj, gchar *ssid, gchar *key,
+       gint visibility, gint security_type, DBusGMethodInvocation *context)
+{
+       mobile_ap_error_code_e ret = MOBILE_AP_ERROR_NONE;
+       gboolean ret_val = FALSE;
+
+       DBG("+\n");
+       g_assert(obj != NULL);
+       g_assert(context != NULL);
+
+       if (wifi_recovery_timeout_id) {
+               DBG("Wi-Fi recovery is cancelled\n");
+               g_source_remove(wifi_recovery_timeout_id);
+               wifi_recovery_timeout_id = 0;
+       }
+
+       ret = _enable_wifi_ap(obj, ssid, key, !visibility,
+                       (softap_security_type_e)security_type);
+       if (ret != MOBILE_AP_ERROR_NONE) {
+               ERR("_enable_wifi_tethering is failed\n");
+       } else {
+               _emit_mobileap_dbus_signal(obj, E_SIGNAL_WIFI_AP_ON, NULL);
+               ret_val = TRUE;
+       }
+
+       dbus_g_method_return(context, MOBILE_AP_ENABLE_WIFI_AP_CFM, ret);
+
+       return ret_val;
+}
+
+gboolean tethering_disable_wifi_ap(TetheringObject *obj,
+               DBusGMethodInvocation *context)
+{
+       int ret = MOBILE_AP_ERROR_NONE;
+
+       DBG("+\n");
+       g_assert(obj != NULL);
+       g_assert(context != NULL);
+
+       ret = _disable_wifi_ap(obj);
+
+       _emit_mobileap_dbus_signal(obj, E_SIGNAL_WIFI_AP_OFF, NULL);
+       dbus_g_method_return(context, MOBILE_AP_DISABLE_WIFI_AP_CFM, ret);
+
+       if (ret != MOBILE_AP_ERROR_NONE)
+               return FALSE;
+
+       return TRUE;
+}
+
+gboolean tethering_reload_wifi_settings(TetheringObject *obj, gchar *ssid,
+               gchar *key, gint visibility, gint security_type,
+               DBusGMethodInvocation *context)
+{
+       mobile_ap_error_code_e ret = MOBILE_AP_ERROR_NONE;
+       gboolean ret_val = TRUE;
+
+       DBG("+\n");
+       g_assert(obj != NULL);
+       g_assert(context != NULL);
+
+       ret = _reload_softap_settings(obj, ssid, key, !visibility, security_type);
+       if (ret != MOBILE_AP_ERROR_NONE) {
+               ERR("_reload_softap_settings is failed\n");
+               ret_val = FALSE;
+       }
+
+       dbus_g_method_return(context, ret);
+
+       return ret_val;
+}
+
+gboolean tethering_reload_wifi_ap_settings(TetheringObject *obj, gchar *ssid,
+       gchar *key, gint visibility, gint security, DBusGMethodInvocation *context)
+{
+
+       mobile_ap_error_code_e ret = MOBILE_AP_ERROR_NONE;
+       gboolean ret_val = TRUE;
+
+       DBG("+\n");
+       g_assert(obj != NULL);
+       g_assert(context != NULL);
+
+       ret = _reload_softap_settings_for_ap(obj, ssid, key, !visibility, security);
+       if (ret != MOBILE_AP_ERROR_NONE) {
+               ERR("_reload_softap_settings_for_ap is failed\n");
+               ret_val = FALSE;
+       }
+
+       dbus_g_method_return(context, ret);
+       return ret_val;
+}
+
+gboolean _is_trying_wifi_operation(void)
+{
+       return (g_context || wifi_recovery_timeout_id ? TRUE : FALSE);
+}
diff --git a/tethering_dump.sh b/tethering_dump.sh
new file mode 100644 (file)
index 0000000..887c55f
--- /dev/null
@@ -0,0 +1,17 @@
+#!/bin/sh
+#
+# Tethering module log dump (tethering_dump.sh)
+#
+
+# Variables
+TETH_DEBUG=$1/tethering
+mkdir -p ${TETH_DEBUG}
+
+# copy files
+#/bin/cp -a /tmp/hostapd.log ${TETH_DEBUG}
+/bin/cp -rf /tmp/dnsmasq.conf /opt/var/lib/misc/* ${TETH_DEBUG}
+
+#vconftool get db/mobile_hotspot > ${TETH_DEBUG}/vconf_db.log
+#vconftool get memory/mobile_hotspot > ${TETH_DEBUG}/vconf_memory.log
+/usr/sbin/iptables -t nat -L -vv > ${TETH_DEBUG}/iptables_nat.log
+/usr/sbin/iptables -L -vv > ${TETH_DEBUG}/iptables_filter.log