umbim: add netifd proto handler for mbim style lte modems
[openwrt/svn-archive/archive.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 proto_config_add_string "device:device"
11 proto_config_add_string apn
12 proto_config_add_string pincode
13 proto_config_add_string delay
14 }
15
16 proto_mbim_setup() {
17 local interface="$1"
18 local tid=2
19 local ret
20
21 local device apn pincode delay
22 json_get_vars device apn pincode delay
23
24 [ -n "$device" ] || {
25 logger -p daemon.err -t "mbim[$$]" "No control device specified"
26 proto_notify_error "$interface" NO_DEVICE
27 proto_block_restart "$interface"
28 return 1
29 }
30 [ -c "$device" ] || {
31 logger -p daemon.err -t "mbim[$$]" "The specified control device does not exist"
32 proto_notify_error "$interface" NO_DEVICE
33 proto_block_restart "$interface"
34 return 1
35 }
36 [ -n "$apn" ] || {
37 logger -p daemon.err -t "mbim[$$]" "No APN specified"
38 proto_notify_error "$interface" NO_APN
39 proto_block_restart "$interface"
40 return 1
41 }
42
43
44 [ -n "$delay" ] && sleep "$delay"
45
46 logger -p daemon.info -t "mbim[$$]" "Reading capabilities"
47 umbim $DBG -n -d $device caps || {
48 logger -p daemon.err -t "mbim[$$]" "Failed to read modem caps"
49 proto_notify_error "$interface" PIN_FAILED
50 proto_block_restart "$interface"
51 return 1
52 }
53 tid=$((tid + 1))
54
55 [ "$pincode" ] && {
56 logger -p daemon.info -t "mbim[$$]" "Sending pin"
57 umbim $DBG -n -t $tid -d $device unlock "$pincode" || {
58 logger -p daemon.err -t "mbim[$$]" "Unable to verify PIN"
59 proto_notify_error "$interface" PIN_FAILED
60 proto_block_restart "$interface"
61 return 1
62 }
63 }
64 tid=$((tid + 1))
65
66 logger -p daemon.info -t "mbim[$$]" "Checking pin"
67 umbim $DBG -n -t $tid -d $device pinstate || {
68 logger -p daemon.err -t "mbim[$$]" "PIN required"
69 proto_notify_error "$interface" PIN_FAILED
70 proto_block_restart "$interface"
71 return 1
72 }
73 tid=$((tid + 1))
74
75 logger -p daemon.info -t "mbim[$$]" "Checking subscriber"
76 umbim $DBG -n -t $tid -d $device subscriber || {
77 logger -p daemon.err -t "mbim[$$]" "Subscriber init failed"
78 proto_notify_error "$interface" NO_SUBSCRIBER
79 proto_block_restart "$interface"
80 return 1
81 }
82 tid=$((tid + 1))
83
84 logger -p daemon.info -t "mbim[$$]" "Register with network"
85 umbim $DBG -n -t $tid -d $device registration || {
86 logger -p daemon.err -t "mbim[$$]" "Subscriber registration failed"
87 proto_notify_error "$interface" NO_REGISTRATION
88 proto_block_restart "$interface"
89 return 1
90 }
91 tid=$((tid + 1))
92
93 logger -p daemon.info -t "mbim[$$]" "Attach to network"
94 umbim $DBG -n -t $tid -d $device attach || {
95 logger -p daemon.err -t "mbim[$$]" "Failed to attach to network"
96 proto_notify_error "$interface" ATTACH_FAILED
97 proto_block_restart "$interface"
98 return 1
99 }
100 tid=$((tid + 1))
101
102 logger -p daemon.info -t "mbim[$$]" "Connect to network"
103 while ! umbim $DBG -n -t $tid -d $device connect "$apn"; do
104 tid=$((tid + 1))
105 sleep 1;
106 done
107 tid=$((tid + 1))
108
109 uci_set_state network $interface tid "$tid"
110
111 logger -p daemon.info -t "mbim[$$]" "Connected, starting DHCP"
112 proto_init_update "*" 1
113 proto_send_update "$interface"
114
115 json_init
116 json_add_string name "${interface}_dhcp"
117 json_add_string ifname "@$interface"
118 json_add_string proto "dhcp"
119 json_close_object
120 ubus call network add_dynamic "$(json_dump)"
121
122 json_init
123 json_add_string name "${interface}_dhcpv6"
124 json_add_string ifname "@$interface"
125 json_add_string proto "dhcpv6"
126 json_close_object
127 ubus call network add_dynamic "$(json_dump)"
128 }
129
130 proto_mbim_teardown() {
131 local interface="$1"
132
133 local device
134 json_get_vars device
135 local tid=$(uci_get_state network $interface tid)
136
137 logger -p daemon.info -t "mbim[$$]" "Stopping network"
138 [ -n "$tid" ] && {
139 umbim $DBG -t$tid -d "$device" disconnect
140 uci_revert_state network $interface tid
141 }
142
143 proto_init_update "*" 0
144 proto_send_update "$interface"
145 }
146
147 add_protocol mbim