summaryrefslogtreecommitdiffstats
path: root/src/lib/connections/mobile_ppp
blob: 40750d8afbaaf902189b14868d07492a6ecf46af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# Contributed by Robbie Smith <zoqaeski@gmail.com>
# Based on Thomas Bächler’s <thomas@archlinux.org> pppoe script
# Also see <https://wiki.archlinux.org/index.php/3G_and_GPRS_modems_with_pppd> for more information.

: ${PPPD:=pppd}
: ${InterfaceRoot=dev/}

quote_word() {
    set -- "${@//\\/\\\\}"
    printf '"%s"\n' "${@//\"/\\\"}"
}

mobile_ppp_up() {
    local options chat
    options="$STATE_DIR/mobile_ppp.${Interface}.${Profile}/options"
    chat="$STATE_DIR/mobile_ppp.${Interface}.${Profile}/modem.chat"

    mkdir -p "$(dirname "$options")"
    cat >> "$options" << EOF
linkname $(quote_word "$Profile")
$(quote_word "$Interface")
921600
lock
crtscts
modem
passive
novj
holdoff 10

noauth
$(is_yes "${DefaultRoute:-yes}" || printf no)defaultroute
maxfail $(quote_word "${MaxFail:-5}")
$(is_yes "${UsePeerDNS:-yes}" && printf usepeerdns)
hide-password
${User:+user $(quote_word "$User")}
${Password:+password $(quote_word "$Password")}
connect $(quote_word "/usr/sbin/chat -v -t15 -f $(quote_word "$chat")")
${OptionsFile:+file $(quote_word "$OptionsFile")}
EOF

    cat >> "$chat" << EOF
ECHO ON
ABORT 'BUSY'
ABORT 'NO CARRIER'
ABORT 'VOICE'
ABORT 'NO DIALTONE'
ABORT 'NO DIAL TONE'
ABORT 'NO ANSWER'
ABORT 'DELAYED'
ABORT   '\nRINGING\r\n\r\nRINGING\r'
REPORT CONNECT
TIMEOUT 6
'' 'ATQ0'
'OK-AT-OK' 'ATZ'
TIMEOUT 3
'OK' 'AT+CFUN=1'
'OK' 'AT${Pin:++CPIN=$(quote_word "$Pin")}'
'OK\d-AT-OK' 'ATI'
'OK' 'ATZ'
'OK' 'ATQ0 V1 E1 S0=0 &C1 &D2 +FCLASS=0'
'OK' 'AT$(case $Mode in
  3Gonly) printf "\^SYSCFG=14,2,3fffffff,0,1";;
  3Gpref) printf "\^SYSCFG=2,2,3fffffff,0,1";;
  GPRSonly) printf "\^SYSCFG=13,1,3fffffff,0,0";;
  GPRSpref) printf "\^SYSCFG=2,1,3fffffff,0,0";;
esac)'
${AccessPointName:+'OK-AT-OK' 'AT+CGDCONT=1,"IP",$(quote_word "$AccessPointName")'}
'OK' 'ATDT${PhoneNumber:-*99#}'
TIMEOUT 30
CONNECT ''
EOF

    if ! $PPPD file "$options"; then
        rm "$options" "$chat"
        rmdir "$(dirname "$options")"
        report_error "Could not establish a ppp connection for profile '$Profile'."
        return 1
    fi
}

mobile_ppp_down() {
    local options chat pidfile pid
    options="$STATE_DIR/mobile_ppp.$Interface.$Profile/options"
    chat="$STATE_DIR/mobile_ppp.$Interface.$Profile/modem.chat"
    pidfile="/var/run/ppp-$Profile.pid"

    if [[ -r $pidfile ]]; then
        read pid < "$pidfile"
        (( pid )) && kill "$pid"
    fi

    rm "$options" "$chat"
    rmdir "$(dirname "$options")"
}


# vim: ft=sh ts=4 et sw=4: