summaryrefslogtreecommitdiff
path: root/mhi-qmi-connect.sh
blob: d9f24362a763a8f701261d16b3a0f221c52e2c05 (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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#!/bin/bash

trap interrupt_cb INT

qmidev=""
iface_id=4
mux_id=1
ep=pcie
wds_options=""
mtu=16384
apn=""
res=""
wwan_iface="mhi_hwip0"
rmnet_iface="rmnet_data${mux_id}"

usage()
{
	echo "Usage:"
	echo "  $0 <APN_NAME> [SIM_PIN]"
}

qmi_device_find()
{
	qmidev=$(ls /dev/mhi_*QMI | head -1)
	if (( $? != 0 )); then
		echo "** ERROR: QMI UCI device not found"
		exit -1
	fi
	echo "** QMI device found: ${qmidev}"
}

qmi_wda_set_data_format()
{
	res=$(qmicli -d ${qmidev} --wda-set-data-format ep-type=${ep},ep-iface-number=${iface_id},link-layer-protocol=raw-ip,ul-protocol=qmap,dl-protocol=qmap,dl-max-datagrams=32,dl-datagram-max-size=${mtu} 2>&1)
	if (( $? != 0 )); then
                echo "** ERROR: Unable to set data format"
		echo "------"
                echo $res
                echo "------"
                exit -1
        fi
}

qmi_wds_get_cid()
{
	res=$(qmicli -d ${qmidev} --wds-noop --client-no-release-cid 2>&1)
	if (( $? != 0 )); then
		echo "** ERROR: unable to get WDS CID"
		echo "------"
                echo $res
                echo "------"
		exit 1
	fi
	wds_cid=`echo "$res" | sed -n "s/.*CID.*'\(.*\)'.*/\1/p"`
	echo "** Allocated WDS CID $wds_cid"
	wds_options="--client-cid=${wds_cid} --client-no-release-cid"
}

qmi_wds_release_cid()
{
	res=$(qmicli -d ${qmidev} --wds-noop --client-cid=${wds_cid} 2>&1)
	echo "** Released WDS CID ${wds_cid}"
}

qmi_wds_reset()
{
        res=$(qmicli -d ${qmidev} --wds-reset ${wds_options} 2>&1)
	if (( $? != 0 )); then
		echo "** ERROR: unable to reset WDS"
		echo "------"
                echo $res
                echo "------"
		qmi_wds_release_cid
		exit 1
	fi
}

qmi_wds_bind_mux_data_port()
{
	res=$(qmicli -d ${qmidev} --wds-bind-mux-data-port ep-type=${ep},ep-iface-number=${iface_id},mux-id=${mux_id} ${wds_options} 2>&1)
	if (( $? != 0 )); then
		echo "** ERROR: unable to bind mux data port"
		echo "------"
                echo $res
                echo "------"
		qmi_wds_release_cid
		exit 1
	fi
}

qmi_wds_start_network()
{
	echo -n "** Connecting to APN ${apn}...  "
	res=$(qmicli -d ${qmidev} --wds-start-network="ip-type=4,apn=${apn}" ${wds_options} 2>&1)
	if (( $? != 0 )); then
		echo "ERROR Unable to connect to ${apn}"
		echo "------"
                echo $res
                echo "------"
		qmi_wds_release_cid
		exit 1
	fi
	echo "Connected!"
}

net_ipaddr="not-found"
net_dns="not-found"
net_mtu="not-found"

qmi_wds_get_settings()
{
	res=$(qmicli -d ${qmidev} --wds-get-current-settings ${wds_options} 2> /dev/null)
	if (( $? != 0 )); then
		echo "**ERROR retrieving settings"
		echo "------"
                echo $res
                echo "------"
		qmi_wds_release_cid
		exit 1
	fi
	net_ipaddr=$(echo $res | sed -n "s/.*IPv4 address: \(.*\) '.*/\1/p" | cut -d" " -f1)
	net_dns=$(echo $res | sed -n "s/.*DNS: \(.*\) '.*/\1/p" | cut -d" " -f1)
	net_mtu=$(echo $res | sed -n "s/.*MTU: \(.*\) '.*/\1/p" | cut -d" " -f1)
	echo "** IPv4 => $net_ipaddr"
	echo "** DNS1 => $net_dns"
	echo "** MTU => $net_mtu"
}

qmi_uim_sim_on()
{
	res=$(qmicli -d ${qmidev} --dms-set-operating-mode=online 2>&1)
	res=$(qmicli -d ${qmidev} --uim-sim-power-on=1 2>&1)
}

qmi_uim_verify_pin()
{
        echo -n "** Verifying UIM pin1...  "
        res=$(qmicli -d ${qmidev} --uim-verify-pin PIN1,1234 2>&1)
        if (( $? != 0 )); then
                echo "ERROR!"
		echo "------"
		echo $res
		echo "------"
                exit 1
        fi
	echo "OK!"
}

qmi_nas_wait_carrier()
{
	echo -n "** Waiting operator registration..."
	while (true); do
		sleep 1
		res=$(qmicli -d ${qmidev} --nas-get-serving-system 2>&1 | grep registered)
		if [[ "$res" == *"'registered'"* ]]; then
			echo " OK!"
			break
		fi
		echo -n "."
	done
}

setup_rmnet()
{
	# set MTU
	ip link set ${wwan_iface} mtu ${mtu}
	# Create bridge
	ip link add link ${wwan_iface} name ${rmnet_iface} type rmnet mux_id ${mux_id}
	ip link set ${rmnet_iface} mtu ${mtu}
	ip addr add ${net_ipaddr}/27 dev ${rmnet_iface}
	ip link set ${wwan_iface} up
	ip link set ${rmnet_iface} up
	ip route del default
	ip route add default via ${net_ipaddr}
	echo "nameserver ${net_dns}" > /etc/resolv.conf
}

release_rmnet()
{
	res=$(ip link set ${rmnet_iface} down 2>&1)
	res=$(ip link set ${wwan_iface} down 2>&1)
	res=$(ip link del ${rmnet_iface} 2>&1)
	res=$(ip route del ${net_ipaddr} 2>&1)
	echo "nameserver 8.8.8.8" > /etc/resolv.conf
}

interrupt_cb()
{
	echo " => Interrupted"
	qmi_wds_release_cid
	release_rmnet
	exit 0
}

if (( $# < 1 )); then
        usage
        exit 1
fi
apn=$1

if [ "$EUID" -ne 0 ]; then
	echo "Please run as root"
	exit
fi

qmi_device_find
if (( $# > 1 )); then
        pin=$2
        qmi_uim_verify_pin
fi
qmi_uim_sim_on
qmi_nas_wait_carrier
qmi_wda_set_data_format
qmi_wds_get_cid
qmi_wds_reset
qmi_wds_bind_mux_data_port
qmi_wds_start_network
qmi_wds_get_settings
setup_rmnet
echo "** Ready! (ctrl+c to stop wwan network)"
sleep infinity