[Support Legacy Connection] Update peer disconnection logic.
[platform/core/connectivity/wifi-direct-manager.git] / files / wifi-direct-dhcp.sh.in
1 #!/bin/sh
2 if [ $2 ]; then
3 INTERFACE_NAME="$2"
4 else
5 INTERFACE_NAME="p2p0"
6 fi
7 INTERFACE_PREFIX="p2p"
8 TARGET="TM1"
9 DEFAULT_IP="192.168.49.1"
10 DEFAULT_NET="192.168.49.1/24"
11 DEFAULT_BRD="192.168.49.255"
12
13 if [ ! -f @SBIN_DIR@/ip ]; then
14         interface=`@SBIN_DIR@/ifconfig -a|/bin/grep ${INTERFACE_NAME}|@BIN_DIR@/cut -d":" -f1`
15 else
16         interface=`@SBIN_DIR@/ip link|/bin/grep ${INTERFACE_NAME}|@BIN_DIR@/cut -d":" -f2`
17 fi
18 echo "Target is ${TARGET} and interface ${INTERFACE_PREFIX}: ${interface}."
19
20 start_dhcp_server()
21 {
22         if [ "X${interface}" == "X" ]; then
23                 echo "interface(${INTERFACE_PREFIX}) is not up"
24                 return 0
25         fi
26
27         /bin/rm /@TZ_SYS_VAR@/lib/misc/dhcpd.leases
28         /bin/touch @TZ_SYS_VAR@/lib/misc/dhcpd.leases
29         if [ ! -f @SBIN_DIR@/ip ]; then
30                 @SBIN_DIR@/ifconfig ${interface} ${DEFAULT_IP} up
31         else
32                 @SBIN_DIR@/ip addr add ${DEFAULT_NET} brd ${DEFAULT_BRD} dev ${interface}
33         fi
34         @SBIN_DIR@/dhcpd -S -i ${interface} @TZ_SYS_RO_ETC@/wifi-direct/dhcpd.conf -f &
35
36         route=`/bin/cat @TZ_SYS_RO_ETC@/wifi-direct/dhcpd.conf | /bin/grep router | /bin/awk '{print $3}'`
37         if [ -z $route ]; then
38                 route="192.168.49.1"
39         fi
40         subnet=`/bin/cat @TZ_SYS_RO_ETC@/wifi-direct/dhcpd.conf | /bin/grep subnet | /bin/awk '{print $3}'`
41
42         if [ -z $subnet ]; then
43                 subnet="255.255.255.0"
44         fi
45
46         @BIN_DIR@/vconftool set -t string memory/private/wifi_direct_manager/p2p_ifname ${interface} -f
47         @BIN_DIR@/vconftool set -t string memory/private/wifi_direct_manager/p2p_subnet_mask ${subnet} -f
48         @BIN_DIR@/vconftool set -t string memory/private/wifi_direct_manager/p2p_gateway ${route} -f
49         @BIN_DIR@/vconftool set -t string memory/private/wifi_direct_manager/p2p_local_ip ${DEFAULT_IP} -f
50 }
51
52 start_dhcp_client()
53 {
54         if [ "X${interface}" == "X" ]; then
55                 echo "interface(${INTERFACE_PREFIX}) is not up"
56                 return 0
57         fi
58
59         @BIN_DIR@/vconftool set -t string memory/private/wifi_direct_manager/dhcpc_server_ip "0.0.0.0" -f
60         @BIN_DIR@/dhcp -S -i $interface -s @TZ_SYS_RO_ETC@/wifi-direct/udhcp_script.non-autoip &
61 }
62
63
64 clean_vconfs()
65 {
66         @BIN_DIR@/vconftool set -t string memory/private/wifi_direct_manager/p2p_ifname "" -f
67         @BIN_DIR@/vconftool set -t string memory/private/wifi_direct_manager/p2p_subnet_mask "" -f
68         @BIN_DIR@/vconftool set -t string memory/private/wifi_direct_manager/p2p_gateway "" -f
69         @BIN_DIR@/vconftool set -t string memory/private/wifi_direct_manager/p2p_local_ip "" -f
70         @BIN_DIR@/vconftool set -t string memory/private/wifi_direct_manager/dhcpc_server_ip "0.0.0.0" -f
71 }
72
73 stop_dhcp()
74 {
75         @BIN_DIR@/pkill -x dhcp
76         @BIN_DIR@/pkill -x dhcpd
77         if [ "X${interface}" == "X" ]; then
78                 echo "interface(${INTERFACE_PREFIX}) is not up"
79                 return 0
80         fi
81
82         if [ ! -f @SBIN_DIR@/ip ]; then
83                 @SBIN_DIR@/ifconfig ${interface} "0.0.0.0"
84         else
85                 @SBIN_DIR@/ip addr del ${local_ip_net} dev ${interface}
86         fi
87 }
88
89 is_running()
90 {
91         program=$1
92         run=`/bin/ps -eo comm|/bin/grep ${program}`
93         if [ "X${run}" == "X" ]; then
94                 echo "${program} is not running"
95         else
96                 echo "${program} is already running"
97         fi
98 }
99
100 status_dhcp()
101 {
102         is_running @BIN_DIR@/dhcp
103         is_running @SBIN_DIR@/dhcpd
104 }
105
106
107 case $1 in
108 "server")
109 stop_dhcp
110 start_dhcp_server
111 ;;
112 "client")
113 clean_vconfs
114 stop_dhcp
115 start_dhcp_client
116 ;;
117 "stop")
118 clean_vconfs
119 stop_dhcp
120 ;;
121 "status")
122 status_dhcp
123 ;;
124 *)
125 /bin/echo wifi-direct-dhcp.sh [server] [client] [stop] [status]
126 exit 1
127 ;;
128 esac
129