8ece66211634a5b646a2ec753cc59196cd6d87fc
[openwrt.git] / package / network / utils / umbim / files / lib / netifd / proto / mbim.sh
1 #!/bin/sh
2
3 . /lib/functions.sh
4 . ../netifd-proto.sh
5 init_proto "$@"
6
7 #DBG=-v
8
9 proto_mbim_init_config() {
10         available=1
11         no_device=1
12         proto_config_add_string "device:device"
13         proto_config_add_string apn
14         proto_config_add_string pincode
15         proto_config_add_string delay
16         proto_config_add_string auth
17         proto_config_add_string username
18         proto_config_add_string password
19 }
20
21 proto_mbim_setup() {
22         local interface="$1"
23         local tid=2
24         local ret
25
26         local device apn pincode delay
27         json_get_vars device apn pincode delay auth username password
28
29         [ -n "$device" ] || {
30                 echo "mbim[$$]" "No control device specified"
31                 proto_notify_error "$interface" NO_DEVICE
32                 proto_set_available "$interface" 0
33                 return 1
34         }
35         [ -c "$device" ] || {
36                 echo "mbim[$$]" "The specified control device does not exist"
37                 proto_notify_error "$interface" NO_DEVICE
38                 proto_set_available "$interface" 0
39                 return 1
40         }
41
42         devname="$(basename "$device")"
43         devpath="$(readlink -f /sys/class/usbmisc/$devname/device/)"
44         ifname="$( ls "$devpath"/net )"
45
46         [ -n "$ifname" ] || {
47                 echo "mbim[$$]" "Failed to find matching interface"
48                 proto_notify_error "$interface" NO_IFNAME
49                 proto_set_available "$interface" 0
50                 return 1
51         }
52
53         [ -n "$apn" ] || {
54                 echo "mbim[$$]" "No APN specified"
55                 proto_notify_error "$interface" NO_APN
56                 return 1
57         }
58
59         [ -n "$delay" ] && sleep "$delay"
60
61         echo "mbim[$$]" "Reading capabilities"
62         umbim $DBG -n -d $device caps || {
63                 echo "mbim[$$]" "Failed to read modem caps"
64                 proto_notify_error "$interface" PIN_FAILED
65                 return 1
66         }
67         tid=$((tid + 1))
68
69         [ "$pincode" ] && {
70                 echo "mbim[$$]" "Sending pin"
71                 umbim $DBG -n -t $tid -d $device unlock "$pincode" || {
72                         echo "mbim[$$]" "Unable to verify PIN"
73                         proto_notify_error "$interface" PIN_FAILED
74                         proto_block_restart "$interface"
75                         return 1
76                 }
77         }
78         tid=$((tid + 1))
79
80         echo "mbim[$$]" "Checking pin"
81         umbim $DBG -n -t $tid -d $device pinstate || {
82                 echo "mbim[$$]" "PIN required"
83                 proto_notify_error "$interface" PIN_FAILED
84                 proto_block_restart "$interface"
85                 return 1
86         }
87         tid=$((tid + 1))
88
89         echo "mbim[$$]" "Checking subscriber"
90         umbim $DBG -n -t $tid -d $device subscriber || {
91                 echo "mbim[$$]" "Subscriber init failed"
92                 proto_notify_error "$interface" NO_SUBSCRIBER
93                 return 1
94         }
95         tid=$((tid + 1))
96
97         echo "mbim[$$]" "Register with network"
98         umbim $DBG -n -t $tid -d $device registration || {
99                 echo "mbim[$$]" "Subscriber registration failed"
100                 proto_notify_error "$interface" NO_REGISTRATION
101                 return 1
102         }
103         tid=$((tid + 1))
104
105         echo "mbim[$$]" "Attach to network"
106         umbim $DBG -n -t $tid -d $device attach || {
107                 echo "mbim[$$]" "Failed to attach to network"
108                 proto_notify_error "$interface" ATTACH_FAILED
109                 return 1
110         }
111         tid=$((tid + 1))
112  
113         echo "mbim[$$]" "Connect to network"
114         while ! umbim $DBG -n -t $tid -d $device connect "$apn" "$auth" "$username" "$password"; do
115                 tid=$((tid + 1))
116                 sleep 1;
117         done
118         tid=$((tid + 1))
119
120         uci_set_state network $interface tid "$tid"
121
122         echo "mbim[$$]" "Connected, starting DHCP"
123         proto_init_update "$ifname" 1
124         proto_send_update "$interface"
125
126         json_init
127         json_add_string name "${interface}_dhcp"
128         json_add_string ifname "@$interface"
129         json_add_string proto "dhcp"
130         json_close_object
131         ubus call network add_dynamic "$(json_dump)"
132
133         json_init
134         json_add_string name "${interface}_dhcpv6"
135         json_add_string ifname "@$interface"
136         json_add_string proto "dhcpv6"
137         ubus call network add_dynamic "$(json_dump)"
138 }
139
140 proto_mbim_teardown() {
141         local interface="$1"
142
143         local device
144         json_get_vars device
145         local tid=$(uci_get_state network $interface tid)
146
147         echo "mbim[$$]" "Stopping network"
148         [ -n "$tid" ] && {
149                 umbim $DBG -t$tid -d "$device" disconnect
150                 uci_revert_state network $interface tid
151         }
152
153         proto_init_update "*" 0
154         proto_send_update "$interface"
155 }
156
157 add_protocol mbim