Project

General

Profile

USBNetworking » replicant_usb_networking_device.sh

Paul Kocialkowski, 02/28/2014 05:17 PM

 
1
#!/system/bin/sh
2

    
3
# Replicant USB Networking
4
# ========================
5
#
6
# Copyright (C) 2011,2014 Paul Kocialkowski and Riku Saikkonen, GPLv3+
7
#
8
# This program is free software: you can redistribute it and/or modify
9
# it under the terms of the GNU General Public License as published by
10
# the Free Software Foundation, either version 3 of the License, or
11
# (at your option) any later version.
12
#
13
# You should have received a copy of the GNU General Public License
14
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
15

    
16
set -e
17

    
18
CFG_TYPE="static"               # default to "dhcp" or "static"
19
USB_IFACE="rndis0"
20
STATIC_IP="192.168.4.202"
21
STATIC_IFCONFIG_OPTIONS=""      # e.g. "netmask 255.255.255.0"
22
STATIC_GATEWAY="192.168.4.200"
23
STATIC_DNS1="8.8.8.8"
24
STATIC_DNS2=""
25

    
26
start_rndis () {
27
    #echo 1 >/sys/class/android_usb/android0/enable
28
    #setprop sys.usb.config rndis,adb
29
    svc usb setFunction rndis
30
}
31

    
32
stop_rndis () {
33
    #echo 0 >/sys/class/android_usb/android0/enable
34
    #setprop sys.usb.config mtp,adb
35
    svc usb setFunction
36
}
37

    
38
if_up () {
39
    #ifconfig $USB_IFACE up
40
    netcfg $USB_IFACE up
41
}
42

    
43
if_down () {
44
    #ifconfig $USB_IFACE down
45
    netcfg $USB_IFACE down
46
}
47

    
48
dns_changed () {
49
    # Signal possible change of DNS server to libc
50
    setprop net.dnschange $((`getprop net.dnschange`+1))
51
}
52

    
53
configure_static () {
54
    if_up
55
    ifconfig $USB_IFACE $STATIC_IP $STATIC_IFCONFIG_OPTIONS
56
    route add default gw $STATIC_GATEWAY dev $USB_IFACE
57
    setprop net.dns1 "$STATIC_DNS1"
58
    setprop net.dns2 "$STATIC_DNS2"
59
    dns_changed
60
    echo "Static IP $STATIC_IP configured"
61
}
62

    
63
configure_dhcp () {
64
    echo "Configuring DHCP, please wait"
65
    #if_up
66
    #dhcpcd -d $USB_IFACE
67
    netcfg $USB_IFACE dhcp
68
    setprop net.dns1 "`getprop net.$USB_IFACE.dns1`"
69
    setprop net.dns2 "`getprop net.$USB_IFACE.dns2`"
70
    dns_changed
71
    echo "DHCP configured"
72
}
73

    
74
configure () {
75
    case "$CFG_TYPE" in
76
        dhcp)
77
            configure_dhcp
78
            ;;
79
        static)
80
            configure_static
81
            ;;
82
        *)
83
            echo "$0: invalid type: $CFG_TYPE" 1>&2
84
            exit 1
85
            ;;
86
    esac
87
}
88

    
89
[ "" != "$2" ] && CFG_TYPE="$2"
90

    
91
case "$1" in
92
    start1)
93
        echo "Starting Replicant USB networking, phase 1"
94
        start_rndis
95
        ;;
96
    start2)
97
        echo "Starting Replicant USB networking, phase 2"
98
        configure
99
        ;;
100
    start)
101
        echo "Starting Replicant USB networking"
102
        start_rndis
103
        echo "Please start connection sharing on host now!"
104
        # work around short DHCP timeout by sleeping here
105
        if [ "dhcp" = "$CFG_TYPE" ]; then
106
            echo "Waiting 15 s"
107
            sleep 15
108
        fi
109
        configure
110
        ;;
111
    stop)
112
        echo "Stopping Replicant USB networking"
113
        if_down
114
        stop_rndis
115
        ;;
116
    *)
117
        echo "Usage: sh $0 {start|start1|start2|stop} [{dhcp|static}]"
118
        ;;
119
esac
120

    
121
exit 0
(1-1/2)