comgt: add directip proto
[oweals/openwrt.git] / package / network / utils / comgt / files / directip.sh
1 #!/bin/sh
2
3 . /lib/functions.sh
4 . ../netifd-proto.sh
5 init_proto "$@"
6
7 proto_directip_init_config() {
8         available=1
9         proto_config_add_string "device:device"
10         proto_config_add_string "ifname"
11         proto_config_add_string "apn"
12         proto_config_add_string "pincode"
13         proto_config_add_string "auth"
14         proto_config_add_string "username"
15         proto_config_add_string "password"
16 }
17
18 proto_directip_setup() {
19         local interface="$1"
20         local chat
21
22         local device apn pincode ifname auth username password
23         json_get_vars device apn pincode ifname auth username password
24
25         [ -e "$device" ] || {
26                 proto_notify_error "$interface" NO_DEVICE
27                 proto_set_available "$interface" 0
28                 return 1
29         }
30
31         [ -n "$ifname" ] || {
32                 proto_notify_error "$interface" NO_IFNAME
33                 proto_set_available "$interface" 0
34                 return 1
35         }
36
37         cardinfo=$(gcom -d "$device" -s /etc/gcom/getcardinfo.gcom)
38         [ -n $(echo "$cardinfo" | grep -q "Sierra Wireless") ] || {
39                 proto_notify_error "$interface" BAD_DEVICE
40                 proto_block_restart "$interface"
41                 return 1
42         }
43
44         if [ -n "$pincode" ]; then
45                 PINCODE="$pincode" gcom -d "$device" -s /etc/gcom/setpin.gcom || {
46                         proto_notify_error "$interface" PIN_FAILED
47                         proto_block_restart "$interface"
48                         return 1
49                 }
50         fi
51         # wait for carrier to avoid firmware stability bugs
52         gcom -d "$device" -s /etc/gcom/getcarrier.gcom || return 1
53
54         local auth_type=0
55         [ -z "$auth" ] && case $auth in
56         pap) auth_type=1;;
57         chap) auth_type=1;;
58         esac
59
60         USE_APN="$apn" USE_USER="$username" USE_PASS="$password" USE_AUTH="$auth_type" \
61                         gcom -d "$device" -s /etc/gcom/directip.gcom || {
62                 proto_notify_error "$interface" CONNECT_FAILED
63                 proto_block_restart "$interface"
64                 return 1
65         }
66
67         logger -p daemon.info -t "directip[$$]" "Connected, starting DHCP"
68         proto_init_update "*" 1
69         proto_send_update "$interface"
70
71         json_init
72         json_add_string name "${interface}_dhcp"
73         json_add_string ifname "@$interface"
74         json_add_string proto "dhcp"
75         ubus call network add_dynamic "$(json_dump)"
76
77         return 0
78 }
79
80 proto_directip_teardown() {
81         local interface="$1"
82
83         local device
84         json_get_vars device
85
86         gcom -d "$device" -s /etc/gcom/directip-stop.gcom || proto_notify_error "$interface" CONNECT_FAILED
87
88         proto_init_update "*" 0
89         proto_send_update "$interface"
90 }
91
92 add_protocol directip