1#!/bin/sh
2# $1: type.
3# echo "This is a script to get the modem status."
4
5
6act_node1="usb_modem_act_int"
7act_node2="usb_modem_act_bulk"
8modem_vid=`nvram get usb_modem_act_vid`
9modem_pid=`nvram get usb_modem_act_pid`
10modem_dev=`nvram get usb_modem_act_dev`
11sim_order=`nvram get modem_sim_order`
12
13usb_gobi2=`nvram get usb_gobi2`
14
15stop_lock=`nvram get stop_lock`
16if [ -n "$stop_lock" ] && [ "$stop_lock" -eq "1" ]; then
17	at_lock=""
18else
19	at_lock="flock -x /tmp/at_cmd_lock"
20fi
21
22jffs_dir="/jffs"
23
24
25# $1: ifname.
26_get_qcqmi_by_usbnet(){
27	rp1=`readlink -f /sys/class/net/$1/device 2>/dev/null`
28	if [ "$rp1" == "" ]; then
29		echo ""
30		return
31	fi
32
33	rp2=
34	i=0
35	while [ $i -lt 10 ]; do
36		rp2=`readlink -f /sys/class/GobiQMI/qcqmi$i/device 2>/dev/null`
37		if [ "$rp2" == "" ]; then
38			i=$((i+1))
39			continue
40		fi
41
42		if [ "$rp1" == "$rp2" ]; then
43			echo "qcqmi$i"
44			return
45		fi
46
47		i=$((i+1))
48	done
49
50	echo ""
51}
52
53# $1: VID, $2: PID.
54_get_gobi_device(){
55	if [ -z "$1" ] || [ -z "$2" ]; then
56		echo "0"
57		return
58	fi
59
60	if [ "$1" == "1478" ] && [ "$2" == "36902" -o "$2" == "36903" ]; then
61		echo "1"
62		return
63	fi
64
65	echo "0"
66}
67
68
69act_node=
70#modem_type=`nvram get usb_modem_act_type`
71#if [ "$modem_type" == "tty" -o "$modem_type" == "mbim" ]; then
72#	if [ "$modem_type" == "tty" -a "$modem_vid" == "6610" ]; then # e.q. ZTE MF637U
73#		act_node=$act_node1
74#	else
75#		act_node=$act_node2
76#	fi
77#else
78	act_node=$act_node1
79#fi
80
81modem_act_node=`nvram get $act_node`
82if [ "$modem_act_node" == "" ]; then
83	find_modem_node.sh
84
85	modem_act_node=`nvram get $act_node`
86	if [ "$modem_act_node" == "" ]; then
87		echo "Can't get $act_node!"
88		exit 1
89	fi
90fi
91
92is_gobi=`_get_gobi_device $modem_vid $modem_pid`
93
94if [ "$1" == "bytes" -o "$1" == "bytes-" ]; then
95	if [ "$modem_dev" == "" ]; then
96		echo "2:Can't get the active network device of USB."
97		exit 2
98	fi
99
100	if [ -z "$sim_order" ]; then
101		echo "12:Fail to get the SIM order."
102		exit 12
103	fi
104
105	if [ ! -d "$jffs_dir/sim/$sim_order" ]; then
106		mkdir -p "$jffs_dir/sim/$sim_order"
107	fi
108
109	rx_new=`cat "/sys/class/net/$modem_dev/statistics/rx_bytes" 2>/dev/null`
110	tx_new=`cat "/sys/class/net/$modem_dev/statistics/tx_bytes" 2>/dev/null`
111	echo "  rx_new=$rx_new."
112	echo "  tx_new=$tx_new."
113
114	if [ "$1" == "bytes" ]; then
115		rx_old=`nvram get modem_bytes_rx`
116		if [ -z "$rx_old" ]; then
117			rx_old=0
118		fi
119		tx_old=`nvram get modem_bytes_tx`
120		if [ -z "$tx_old" ]; then
121			tx_old=0
122		fi
123		echo "  rx_old=$rx_old."
124		echo "  tx_old=$tx_old."
125
126		rx_reset=`nvram get modem_bytes_rx_reset`
127		if [ -z "$rx_reset" ]; then
128			rx_reset=0
129		fi
130		tx_reset=`nvram get modem_bytes_tx_reset`
131		if [ -z "$tx_reset" ]; then
132			tx_reset=0
133		fi
134		echo "rx_reset=$rx_reset."
135		echo "tx_reset=$tx_reset."
136
137		rx_now=`lplus $rx_old $rx_new`
138		tx_now=`lplus $tx_old $tx_new`
139		rx_now=`lminus $rx_now $rx_reset`
140		tx_now=`lminus $tx_now $tx_reset`
141		echo "  rx_now=$rx_now."
142		echo "  tx_now=$tx_now."
143
144		nvram set modem_bytes_rx=$rx_now
145		nvram set modem_bytes_tx=$tx_now
146	else
147		rx_now=0
148		tx_now=0
149		nvram set modem_bytes_rx=$rx_now
150		nvram set modem_bytes_tx=$tx_now
151		data_start=`nvram get modem_bytes_data_start 2>/dev/null`
152		if [ -n "$data_start" ]; then
153			echo -n "$data_start" > "$jffs_dir/sim/$sim_order/modem_bytes_data_start"
154		fi
155	fi
156
157	nvram set modem_bytes_rx_reset=$rx_new
158	nvram set modem_bytes_tx_reset=$tx_new
159	echo "set rx_reset=$rx_new."
160	echo "set tx_reset=$tx_new."
161
162	echo "done."
163elif [ "$1" == "bytes+" ]; then
164	if [ -z "$sim_order" ]; then
165		echo "12:Fail to get the SIM order."
166		exit 12
167	fi
168
169	if [ ! -d "$jffs_dir/sim/$sim_order" ]; then
170		mkdir -p "$jffs_dir/sim/$sim_order"
171	fi
172
173	rx_now=`nvram get modem_bytes_rx`
174	tx_now=`nvram get modem_bytes_tx`
175	echo -n "$rx_now" > "$jffs_dir/sim/$sim_order/modem_bytes_rx"
176	echo -n "$tx_now" > "$jffs_dir/sim/$sim_order/modem_bytes_tx"
177
178	echo "done."
179elif [ "$1" == "get_dataset" ]; then
180	if [ -z "$sim_order" ]; then
181		echo "12:Fail to get the SIM order."
182		exit 12
183	fi
184
185	echo "Getting data setting..."
186
187	if [ ! -d "$jffs_dir/sim/$sim_order" ]; then
188		mkdir -p "$jffs_dir/sim/$sim_order"
189	fi
190
191	data_start=`cat "$jffs_dir/sim/$sim_order/modem_bytes_data_start" 2>/dev/null`
192	data_cycle=`cat "$jffs_dir/sim/$sim_order/modem_bytes_data_cycle" 2>/dev/null`
193	data_limit=`cat "$jffs_dir/sim/$sim_order/modem_bytes_data_limit" 2>/dev/null`
194	data_warning=`cat "$jffs_dir/sim/$sim_order/modem_bytes_data_warning" 2>/dev/null`
195
196	if [ -n "$data_start" ]; then
197		nvram set modem_bytes_data_start=$data_start
198	fi
199	if [ -z "$data_cycle" ] || [ "$data_cycle" -lt 1 -o "$data_cycle" -gt 31 ]; then
200		data_cycle=1
201		echo -n "$data_cycle" > "$jffs_dir/sim/$sim_order/modem_bytes_data_cycle"
202	fi
203	nvram set modem_bytes_data_cycle=$data_cycle
204	if [ -z "$data_limit" ]; then
205		data_limit=0
206		echo -n "$data_limit" > "$jffs_dir/sim/$sim_order/modem_bytes_data_limit"
207	fi
208	nvram set modem_bytes_data_limit=$data_limit
209	if [ -z "$data_warning" ]; then
210		data_warning=0
211		echo -n "$data_warning" > "$jffs_dir/sim/$sim_order/modem_bytes_data_warning"
212	fi
213	nvram set modem_bytes_data_warning=$data_warning
214
215	rx_now=`cat "$jffs_dir/sim/$sim_order/modem_bytes_rx" 2>/dev/null`
216	tx_now=`cat "$jffs_dir/sim/$sim_order/modem_bytes_tx" 2>/dev/null`
217	nvram set modem_bytes_rx=$rx_now
218	nvram set modem_bytes_tx=$tx_now
219
220	echo "done."
221elif [ "$1" == "set_dataset" ]; then
222	if [ -z "$sim_order" ]; then
223		echo "12:Fail to get the SIM order."
224		exit 12
225	fi
226
227	echo "Setting data setting..."
228
229	if [ ! -d "$jffs_dir/sim/$sim_order" ]; then
230		mkdir -p "$jffs_dir/sim/$sim_order"
231	fi
232
233	data_start=`nvram get modem_bytes_data_start 2>/dev/null`
234	data_cycle=`nvram get modem_bytes_data_cycle 2>/dev/null`
235	data_limit=`nvram get modem_bytes_data_limit 2>/dev/null`
236	data_warning=`nvram get modem_bytes_data_warning 2>/dev/null`
237
238	if [ -n "$data_start" ]; then
239		echo -n "$data_start" > "$jffs_dir/sim/$sim_order/modem_bytes_data_start"
240	fi
241	if [ -z "$data_cycle" ] || [ "$data_cycle" -lt 1 -o "$data_cycle" -gt 31 ]; then
242		data_cycle=1
243		nvram set modem_bytes_data_cycle=$data_cycle
244	fi
245	echo -n "$data_cycle" > "$jffs_dir/sim/$sim_order/modem_bytes_data_cycle"
246	if [ -z "$data_limit" ]; then
247		data_limit=0
248		nvram set modem_bytes_data_limit=$data_limit
249	fi
250	echo -n "$data_limit" > "$jffs_dir/sim/$sim_order/modem_bytes_data_limit"
251	if [ -z "$data_warning" ]; then
252		data_warning=0
253		nvram set modem_bytes_data_warning=$data_warning
254	fi
255	echo -n "$data_warning" > "$jffs_dir/sim/$sim_order/modem_bytes_data_warning"
256
257	echo "done."
258elif [ "$1" == "sim" ]; then
259	stop_sim=`nvram get stop_sim`
260	if [ -n "$stop_sim" ] && [ "$stop_sim" -eq "1" ]; then
261		echo "Skip to detect SIM..."
262		exit 0
263	fi
264
265	modem_enable=`nvram get modem_enable`
266	simdetect=`nvram get usb_modem_act_simdetect`
267	if [ -z "$simdetect" ]; then
268		modem_status.sh simdetect
269	fi
270
271	# check the SIM status.
272	at_ret=`$at_lock modem_at.sh '+CPIN?' 2>/dev/null`
273	sim_inserted1=`echo "$at_ret" |grep "READY" 2>/dev/null`
274	sim_inserted2=`echo "$at_ret" |grep "SIM" |awk '{FS=": "; print $2}' 2>/dev/null`
275	sim_inserted3=`echo "$at_ret" |grep "+CME ERROR: " |awk '{FS=": "; print $2}' 2>/dev/null`
276	sim_inserted4=`echo "$sim_inserted2" |cut -c 1-3`
277	if [ "$modem_enable" == "2" ]; then
278		echo "Detected CDMA2000's SIM"
279		act_sim=1
280	elif [ -n "$sim_inserted1" ]; then
281		echo "Got SIM."
282		act_sim=1
283	elif [ "$sim_inserted2" == "SIM PIN" ]; then
284		echo "Need PIN."
285		act_sim=2
286	elif [ "$sim_inserted2" == "SIM PUK" ]; then
287		echo "Need PUK."
288		act_sim=3
289	elif [ "$sim_inserted2" == "SIM PIN2" ]; then
290		echo "Need PIN2."
291		act_sim=4
292	elif [ "$sim_inserted2" == "SIM PUK2" ]; then
293		echo "Need PUK2."
294		act_sim=5
295	elif [ "$sim_inserted4" == "PH-" ]; then
296		echo "Waiting..."
297		act_sim=6
298	elif [ "$sim_inserted3" != "" ]; then
299		if [ "$sim_inserted3" == "SIM not inserted" ]; then
300			echo "SIM not inserted."
301			act_sim=-1
302		else
303			echo "CME ERROR: $sim_inserted3"
304			act_sim=-2
305		fi
306	else
307		echo "No or unknown response."
308		act_sim=-10
309	fi
310
311	act_sim_orig=`nvram get usb_modem_act_sim`
312	if [ "$act_sim_orig" != "$act_sim" ]; then
313		nvram set usb_modem_act_sim=$act_sim
314	fi
315
316	echo "done."
317elif [ "$1" == "signal" ]; then
318	stop_sig=`nvram get stop_sig`
319	if [ -n "$stop_sig" ] && [ "$stop_sig" -eq "1" ]; then
320		echo "Skip to detect signal..."
321		exit 0
322	fi
323
324	ret=`$at_lock modem_at.sh '+CSQ' |grep "+CSQ: " |awk '{FS=": "; print $2}' |awk '{FS=",99"; print $1}' 2>/dev/null`
325	if [ "$ret" == "" ]; then
326		echo "Fail to get the signal from $modem_act_node."
327		exit 3
328	fi
329
330	signal=
331	if [ $ret -eq 99 ]; then
332		# not known or not detectable.
333		signal=-1
334	elif [ $ret -le 1 ]; then
335		# almost no signal.
336		signal=0
337	elif [ $ret -le 9 ]; then
338		# Marginal.
339		signal=1
340	elif [ $ret -le 14 ]; then
341		# OK.
342		signal=2
343	elif [ $ret -le 19 ]; then
344		# Good.
345		signal=3
346	elif [ $ret -le 30 ]; then
347		# Excellent.
348		signal=4
349	elif [ $ret -eq 31 ]; then
350		# Full.
351		signal=5
352	else
353		echo "Can't identify the signal strength: $ret."
354		exit 4
355	fi
356
357	nvram set usb_modem_act_signal=$signal
358
359	echo "$signal"
360	echo "done."
361elif [ "$1" == "fullsignal" ]; then
362	if [ "$is_gobi" -eq "1" ]; then
363		fullstr=`$at_lock modem_at.sh '+CGCELLI' |grep "+CGCELLI:" |awk '{FS="CGCELLI:"; print $2}' 2>/dev/null`
364		if [ "$fullstr" == "" ]; then
365			echo "Fail to get the full signal information from $modem_act_node."
366			exit 3
367		fi
368
369		plmn_end=`echo -n "$fullstr" |awk '{FS="PLMN:"; print $2}' 2>/dev/null`
370		reg_type=`echo -n "$plmn_end" |awk '{FS=","; print $2}' 2>/dev/null`
371		reg_status=`echo -n "$plmn_end" |awk '{FS=","; print $3}' 2>/dev/null`
372
373		if [ "$reg_status" -eq "1" ]; then
374			plmn_head=`echo -n "$fullstr" |awk '{FS="PLMN:"; print $1}' 2>/dev/null`
375			cellid=`echo -n "$plmn_head" |awk '{FS="Cell_ID:"; print $2}' |awk '{FS=","; print $1}' 2>/dev/null`
376
377			if [ "$reg_type" -eq "8" ]; then
378				rsrq=`echo -n "$plmn_head" |awk '{FS="RSRQ:"; print $2}' |awk '{FS=","; print $1}' 2>/dev/null`
379				rsrp=`echo -n "$plmn_head" |awk '{FS="RSRP:"; print $2}' |awk '{FS=","; print $1}' 2>/dev/null`
380				lac=0
381				rssi=`echo -n "$plmn_head" |awk '{FS="RSSI:"; print $2}' |awk '{FS=","; print $1}' 2>/dev/null`
382			else
383				rsrq=0
384				rsrp=0
385				lac=`echo -n "$plmn_head" |awk '{FS="LAC:"; print $2}' |awk '{FS=","; print $1}' 2>/dev/null`
386				rssi=`echo -n "$plmn_end" |awk '{FS="RSSI:"; print $2}' |awk '{FS=","; print $1}' 2>/dev/null`
387			fi
388		fi
389
390		bearer_type=`echo -n "$plmn_end" |awk '{FS="BEARER:"; print $2}' |awk '{FS=","; print $1}' 2>/dev/null`
391
392		operation=
393		if [ "$bearer_type" == "0x01" ]; then
394			operation=GPRS
395		elif [ "$bearer_type" == "0x02" ]; then
396			operation=EDGE
397		elif [ "$bearer_type" == "0x03" ]; then
398			operation=HSDPA
399		elif [ "$bearer_type" == "0x04" ]; then
400			operation=HSUPA
401		elif [ "$bearer_type" == "0x05" ]; then
402			operation=WCDMA
403		elif [ "$bearer_type" == "0x06" ]; then
404			operation=CDMA
405		elif [ "$bearer_type" == "0x07" ]; then
406			operation="EV-DO REV 0"
407		elif [ "$bearer_type" == "0x08" ]; then
408			operation="EV-DO REV A"
409		elif [ "$bearer_type" == "0x09" ]; then
410			operation=GSM
411		elif [ "$bearer_type" == "0x0a" -o "$bearer_type" == "0x0A" ]; then
412			operation="EV-DO REV B"
413		elif [ "$bearer_type" == "0x0b" -o "$bearer_type" == "0x0B" ]; then
414			operation=LTE
415		elif [ "$bearer_type" == "0x0c" -o "$bearer_type" == "0x0C" ]; then
416			operation="HSDPA+"
417		elif [ "$bearer_type" == "0x0d" -o "$bearer_type" == "0x0D" ]; then
418			operation="DC-HSDPA+"
419		else
420			echo "Can't identify the operation type: $bearer_type."
421			exit 6
422		fi
423
424		echo "cellid=$cellid."
425		echo "lac=$lac."
426		echo "rsrq=$rsrq."
427		echo "rsrp=$rsrp."
428		echo "rssi=$rssi."
429		echo "reg_type=$reg_type."
430		echo "operation=$operation"
431
432		nvram set usb_modem_act_cellid=$cellid
433		nvram set usb_modem_act_lac=$lac
434		nvram set usb_modem_act_rsrq=$rsrq
435		nvram set usb_modem_act_rsrp=$rsrp
436		nvram set usb_modem_act_rssi=$rssi
437		nvram set usb_modem_act_operation="$operation"
438
439		echo "done."
440	fi
441elif [ "$1" == "operation" ]; then
442	if [ "$is_gobi" -eq "1" ]; then
443		stop_op=`nvram get stop_op`
444		if [ -n "$stop_op" ] && [ "$stop_op" -eq "1" ]; then
445			echo "Skip to detect operation..."
446			exit 0
447		fi
448
449		bearer_type=`$at_lock modem_at.sh '$CBEARER' |grep 'BEARER:' |awk '{FS=":"; print $2}' 2>/dev/null`
450		if [ "$bearer_type" == "" ]; then
451			echo "Fail to get the operation type from $modem_act_node."
452			exit 5
453		fi
454
455		operation=
456		if [ "$bearer_type" == "0x01" ]; then
457			operation=GPRS
458		elif [ "$bearer_type" == "0x02" ]; then
459			operation=EDGE
460		elif [ "$bearer_type" == "0x03" ]; then
461			operation=HSDPA
462		elif [ "$bearer_type" == "0x04" ]; then
463			operation=HSUPA
464		elif [ "$bearer_type" == "0x05" ]; then
465			operation=WCDMA
466		elif [ "$bearer_type" == "0x06" ]; then
467			operation=CDMA
468		elif [ "$bearer_type" == "0x07" ]; then
469			operation="EV-DO REV 0"
470		elif [ "$bearer_type" == "0x08" ]; then
471			operation="EV-DO REV A"
472		elif [ "$bearer_type" == "0x09" ]; then
473			operation=GSM
474		elif [ "$bearer_type" == "0x0a" -o "$bearer_type" == "0x0A" ]; then
475			operation="EV-DO REV B"
476		elif [ "$bearer_type" == "0x0b" -o "$bearer_type" == "0x0B" ]; then
477			operation=LTE
478		elif [ "$bearer_type" == "0x0c" -o "$bearer_type" == "0x0C" ]; then
479			operation="HSDPA+"
480		elif [ "$bearer_type" == "0x0d" -o "$bearer_type" == "0x0D" ]; then
481			operation="DC-HSDPA+"
482		else
483			echo "Can't identify the operation type: $bearer_type."
484			exit 6
485		fi
486
487		nvram set usb_modem_act_operation="$operation"
488
489		echo "$operation"
490		echo "done."
491	fi
492elif [ "$1" == "setmode" ]; then
493	if [ "$is_gobi" -eq "1" ]; then
494		mode=
495		if [ "$2" == "0" ]; then	# Auto
496			mode=10
497		elif [ "$2" == "43" ]; then	# 4G/3G
498			mode=17
499		elif [ "$2" == "4" ]; then	# 4G only
500			mode=11
501		elif [ "$2" == "3" ]; then	# 3G only
502			mode=2
503		elif [ "$2" == "2" ]; then	# 2G only
504			mode=1
505		else
506			echo "Can't identify the mode type: $2."
507			exit 7
508		fi
509
510		at_ret=`$at_lock modem_at.sh '+CSETPREFNET='$mode 2>/dev/null`
511		ret=`echo "$at_ret" |grep '+CSETPREFNET=' |awk '{FS="="; print $2}' 2>/dev/null`
512		if [ "$ret" == "" ]; then
513			echo "Fail to set the modem mode from $modem_act_node."
514			exit 8
515		fi
516
517		echo "Set the mode be $2($ret)."
518		echo "done."
519	fi
520elif [ "$1" == "getmode" ]; then
521	if [ "$is_gobi" -eq "1" ]; then
522		mode=
523
524		at_ret=`$at_lock modem_at.sh '+CGETPREFNET' 2>/dev/null`
525		ret=`echo "$at_ret" |grep '+CGETPREFNET:' |awk '{FS=":"; print $2}' 2>/dev/null`
526		if [ "$ret" == "" ]; then
527			echo "Fail to get the modem mode from $modem_act_node."
528			exit 9
529		elif [ "$ret" == "10" ]; then	# Auto
530			mode=0
531		elif [ "$ret" == "17" ]; then	# 4G/3G
532			mode=43
533		elif [ "$ret" == "11" ]; then	# 4G only
534			mode=4
535		elif [ "$ret" == "2" ]; then	# 3G only
536			mode=3
537		elif [ "$ret" == "1" ]; then	# 2G only
538			mode=2
539		else
540			echo "Can't identify the mode type: $ret."
541			exit 10
542		fi
543
544		echo "Get the mode be $mode."
545		echo "done."
546	fi
547elif [ "$1" == "imsi" ]; then
548	echo "Getting IMSI..."
549	at_ret=`$at_lock modem_at.sh '+CIMI' 2>/dev/null`
550	ret=`echo "$at_ret" |grep "^[0-9].*$" 2>/dev/null`
551	if [ "$ret" == "" ]; then
552		echo "Fail to get the IMSI from $modem_act_node."
553		exit 11
554	fi
555
556	nvram set usb_modem_act_imsi=$ret
557
558	sim_num=`nvram get modem_sim_num`
559	if [ -z "$sim_num" ]; then
560		sim_num=10
561	fi
562
563	nvram set modem_sim_order=-1
564	i=1
565	while [ $i -le $sim_num ]; do
566		echo -n "check SIM($i)..."
567		got_imsi=`nvram get modem_sim_imsi$i`
568
569		if [ "$got_imsi" == "" ]; then
570			echo "Set SIM($i)."
571			nvram set modem_sim_order=$i
572			nvram set modem_sim_imsi${i}=$ret
573			break
574		elif [ "$got_imsi" == "$ret" ]; then
575			echo "Get SIM($i)."
576			nvram set modem_sim_order=$i
577			break
578		fi
579
580		i=$((i+1))
581	done
582
583	echo "done."
584elif [ "$1" == "imsi_del" ]; then
585	if [ -z "$2" ]; then
586		echo "Usage: $0 $1 <SIM's order>"
587		exit 11;
588	fi
589
590	echo "Delete SIM..."
591
592	sim_num=`nvram get modem_sim_num`
593	if [ -z "$sim_num" ]; then
594		sim_num=10
595	fi
596
597	i=$2
598	while [ $i -le $sim_num ]; do
599		echo -n "check SIM($i)..."
600		got_imsi=`nvram get modem_sim_imsi$i`
601
602		if [ $i -eq $2 ]; then
603			echo -n "Delete SIM($i)."
604			got_imsi=""
605			nvram set modem_sim_imsi$i=$got_imsi
606			rm -rf "$jffs_dir/sim/$i"
607		fi
608
609		if [ -z "$got_imsi" ]; then
610			j=$((i+1))
611			next_imsi=`nvram get modem_sim_imsi$j`
612			if [ -n "$next_imsi" ]; then
613				echo -n "Move SIM($j) to SIM($i)."
614				nvram set modem_sim_imsi$i=$next_imsi
615				mv "$jffs_dir/sim/$j" "$jffs_dir/sim/$i"
616				nvram set modem_sim_imsi$j=
617			fi
618		fi
619
620		echo ""
621
622		i=$((i+1))
623	done
624
625	echo "done."
626elif [ "$1" == "imei" ]; then
627	echo -n "Getting IMEI..."
628	at_ret=`$at_lock modem_at.sh '+CGSN' 2>/dev/null`
629	ret=`echo "$at_ret" |grep "^[0-9].*$" 2>/dev/null`
630	if [ "$ret" == "" ]; then
631		echo "Fail to get the IMEI from $modem_act_node."
632		exit 12
633	fi
634
635	nvram set usb_modem_act_imei=$ret
636
637	echo "done."
638elif [ "$1" == "iccid" ]; then
639	if [ "$is_gobi" -eq "1" ]; then
640		echo -n "Getting ICCID..."
641		at_ret=`$at_lock modem_at.sh '+ICCID' 2>/dev/null`
642		ret=`echo "$at_ret" |grep "ICCID: " |awk '{FS="ICCID: "; print $2}' 2>/dev/null`
643		if [ "$ret" == "" ]; then
644			echo "Fail to get the ICCID from $modem_act_node."
645			exit 13
646		fi
647
648		nvram set usb_modem_act_iccid=$ret
649
650		echo "done."
651	fi
652elif [ "$1" == "rate" ]; then
653	if [ "$is_gobi" -eq "1" ]; then
654		echo -n "Getting Rate..."
655		qcqmi=`_get_qcqmi_by_usbnet $modem_dev 2>/dev/null`
656		at_ret=`gobi_api $qcqmi rate |grep "Max Tx" 2>/dev/null`
657		max_tx=`echo "$at_ret" |awk '{FS=","; print $1}' |awk '{FS=" "; print $3}' 2>/dev/null`
658		max_rx=`echo "$at_ret" |awk '{FS=","; print $2}' |awk '{FS=" "; print $2}' |awk '{FS="."; print $1}' 2>/dev/null`
659		if [ "$max_tx" == "" -o "$max_rx" == "" ]; then
660			echo "Fail to get the rate from $modem_act_node."
661			exit 14
662		fi
663
664		nvram set usb_modem_act_tx=$max_tx
665		nvram set usb_modem_act_rx=$max_rx
666
667		echo "done."
668	fi
669elif [ "$1" == "hwver" ]; then
670	if [ "$is_gobi" -eq "1" ]; then
671		echo -n "Getting HWVER..."
672		at_ret=`$at_lock modem_at.sh '$HWVER' 2>/dev/null`
673		ret=`echo "$at_ret" |grep "^[0-9].*$" 2>/dev/null`
674		if [ "$ret" == "" ]; then
675			nvram set usb_modem_act_hwver=
676			echo "Fail to get the hardware version from $modem_act_node."
677			exit 15
678		fi
679
680		nvram set usb_modem_act_hwver=$ret
681
682		echo "done."
683	fi
684elif [ "$1" == "swver" ]; then
685	if [ "$is_gobi" -eq "1" ]; then
686		echo -n "Getting SWVER..."
687		at_ret=`$at_lock modem_at.sh I 2>/dev/null`
688		ret=`echo -n "$at_ret" |grep "^WW" 2>/dev/null`
689		if [ "$ret" == "" ]; then
690			nvram set usb_modem_act_swver=
691			echo "Fail to get the software version from $modem_act_node."
692			exit 15
693		fi
694
695		nvram set usb_modem_act_swver=$ret
696
697		echo "done."
698	fi
699elif [ "$1" == "band" ]; then
700	if [ "$is_gobi" -eq "1" ]; then
701		echo -n "Getting Band..."
702		at_ret=`$at_lock modem_at.sh '$CRFI' 2>/dev/null`
703		ret=`echo "$at_ret" |grep '$CRFI:' |awk '{FS=":"; print $2}' 2>/dev/null`
704		if [ "$ret" == "" ]; then
705			echo "Fail to get the current band from $modem_act_node."
706			exit 16
707		fi
708
709		nvram set usb_modem_act_band="$ret"
710
711		echo "done."
712	fi
713elif [ "$1" == "setband" ]; then
714	if [ "$is_gobi" -eq "1" ]; then
715		echo -n "Setting Band..."
716		mode=11
717		if [ "$2" == "B3" ]; then
718			bandnum="0000000000000004"
719		elif [ "$2" == "B7" ]; then
720			bandnum="0000000000000040"
721		elif [ "$2" == "B20" ]; then
722			bandnum="0000000000080000"
723		elif [ "$2" == "B38" ]; then
724			bandnum="0000002000000000"
725		else # auto
726			mode=10
727			bandnum="0000002000080044"
728		fi
729
730		at_ret=`$at_lock modem_at.sh '$NV65633='$bandnum |grep "OK" 2>/dev/null`
731		if [ "$at_ret" == "" ]; then
732			echo "Fail to set the band from $modem_act_node."
733			exit 16
734		fi
735
736		at_ret=`$at_lock modem_at.sh '+CSETPREFNET='$mode |grep "OK" 2>/dev/null`
737		if [ "$at_ret" == "" ]; then
738			echo "Fail to set the band from $modem_act_node."
739			exit 16
740		fi
741
742		at_ret=`$at_lock modem_at.sh '+CFUN=1,1' |grep "OK" 2>/dev/null`
743		if [ "$at_ret" == "" ]; then
744			echo "Fail to set the band from $modem_act_node."
745			exit 16
746		fi
747
748		echo "done."
749	fi
750elif [ "$1" == "scan" ]; then
751	echo "Start to scan the stations:"
752	modem_roaming_scantime=`nvram get modem_roaming_scantime`
753	modem_roaming_scanlist=`nvram get modem_roaming_scanlist`
754	nvram set usb_modem_act_scanning=2
755	at_ret=`$at_lock modem_at.sh '+COPS=2' |grep "OK" 2>/dev/null`
756
757	echo "Scanning the stations."
758	nvram set freeze_duck=$modem_roaming_scantime
759	at_ret=`$at_lock modem_at.sh '+COPS=?' $modem_roaming_scantime 2>/dev/null`
760	ret=`echo "$at_ret" |grep '+COPS: ' |awk '{FS=": "; print $2}' |awk '{FS=",,"; print $1}' 2>/dev/null`
761	echo "Finish the scan."
762	nvram set usb_modem_act_scanning=1
763	if [ "$ret" == "" ]; then
764		echo "17:Fail to scan the stations."
765		exit 17
766	fi
767
768	echo "Count the stations."
769	num=`echo "$ret" |awk '{FS=")"; print NF}' 2>/dev/null`
770	if [ "$num" == "" ]; then
771		echo "18:Fail to count the stations."
772		exit 18
773	fi
774
775	echo "Work the list."
776	list="["
777	filter=""
778	i=1
779	while [ $i -lt $num ]; do
780		str=`echo "$ret" |awk '{FS=")"; print $'$i'}' |awk '{FS="("; print $2}' 2>/dev/null`
781
782		sta=`echo "$str" |awk '{FS=","; print $2}' 2>/dev/null`
783		sta_code=`echo "$str" |awk '{FS=","; print $4}' 2>/dev/null`
784		sta_type_number=`echo "$str" |awk '{FS=","; print $5}' 2>/dev/null`
785		if [ "$sta_type_number" == "0" -o "$sta_type_number" == "1" -o "$sta_type_number" == "3" ]; then
786			sta_type=2G
787		elif [ "$sta_type_number" == "2" ]; then
788			sta_type=3G
789		elif [ "$sta_type_number" == "4" ]; then
790			sta_type=HSDPA
791		elif [ "$sta_type_number" == "5" ]; then
792			sta_type=HSUPA
793		elif [ "$sta_type_number" == "6" ]; then
794			sta_type=H+
795		elif [ "$sta_type_number" == "7" ]; then
796			sta_type=4G
797		else
798			sta_type=unknown
799		fi
800
801		if [ "$list" != "[" ]; then
802			list=$list",[$sta, $sta_code, \"$sta_type\"]"
803		else
804			list=$list"[$sta, $sta_code, \"$sta_type\"]"
805		fi
806		filter=$filter","$sta","
807
808		i=$((i+1))
809	done
810	list=$list"]"
811	echo -n "$list" > $modem_roaming_scanlist
812	nvram set usb_modem_act_scanning=0
813
814	echo "done."
815elif [ "$1" == "station" ]; then
816	modem_reg_time=`nvram get modem_reg_time`
817	$at_lock modem_at.sh "+COPS=1,0,\"$2\"" "$modem_reg_time" 1,2>/dev/null
818	if [ $? -ne 0 ]; then
819		echo "19:Fail to set the station: $2."
820		exit 19
821	fi
822
823	echo "done."
824elif [ "$1" == "simauth" ]; then
825	if [ "$is_gobi" -eq "1" ]; then
826		nvram set usb_modem_act_auth=
827		nvram set usb_modem_act_auth_pin=
828		nvram set usb_modem_act_auth_puk=
829		at_ret=`$at_lock modem_at.sh '+CPINR' |grep "+CPINR:" |awk '{FS=":"; print $2}' 2>/dev/null`
830		if [ "$at_ret" == "" ]; then
831			echo "Fail to get the SIM status."
832			exit 20
833		fi
834
835		ret=`echo "$at_ret" |awk '{FS=","; print $3}' 2>/dev/null`
836		if [ "$ret" == "" ]; then
837			echo "Fail to get the SIM auth state."
838			exit 21
839		fi
840		nvram set usb_modem_act_auth=$ret
841		if [ "$ret" == "1" ]; then
842			echo "SIM auth state is ENABLED_NOT_VERIFIED."
843		elif [ "$ret" == "2" ]; then
844			echo "SIM auth state is ENABLED_VERIFIED."
845		elif [ "$ret" == "3" ]; then
846			echo "SIM auth state is DISABLED."
847		elif [ "$ret" == "4" ]; then
848			echo "SIM auth state is BLOCKED."
849		elif [ "$ret" == "5" ]; then
850			echo "SIM auth state is PERMANENTLY_BLOCKED."
851		else
852			echo "SIM auth state is UNKNOWN."
853		fi
854
855		ret=`echo "$at_ret" |awk '{FS=","; print $4}' 2>/dev/null`
856		if [ "$ret" == "" ]; then
857			echo "Fail to get the PIN retry."
858			exit 22
859		fi
860		nvram set usb_modem_act_auth_pin=$ret
861		echo "SIM PIN retry is $ret."
862
863		ret=`echo "$at_ret" |awk '{FS=","; print $5}' 2>/dev/null`
864		if [ "$ret" == "" ]; then
865			echo "Fail to get the PUK retry."
866			exit 23
867		fi
868		nvram set usb_modem_act_auth_puk=$ret
869
870		echo "SIM PUK retry is $ret."
871		echo "done."
872	fi
873elif [ "$1" == "simpin" ]; then
874	if [ "$2" == "" ]; then
875		nvram set g3state_pin=2
876
877		echo "24:Need to input the PIN code."
878		exit 24
879	fi
880
881	nvram set g3state_pin=1
882	at_ret=`$at_lock modem_at.sh '+CPIN='\"$2\" |grep "OK" 2>/dev/null`
883	if [ "$at_ret" == "" ]; then
884		nvram set g3err_pin=1
885
886		echo "25:Fail to unlock the SIM: $2."
887		exit 25
888	fi
889
890	nvram set g3err_pin=0
891	echo "done."
892elif [ "$1" == "simpuk" ]; then
893	# $2: the original PUK. $3: the new PIN.
894	if [ "$2" == "" ]; then
895		echo "26:Need to input the PUK code."
896		exit 26
897	elif [ "$3" == "" ]; then
898		echo "27:Need to input the new PIN code."
899		exit 27
900	fi
901
902	at_ret=`$at_lock modem_at.sh '+CPIN='\"$2\"','\"$3\" |grep "OK" 2>/dev/null`
903	if [ "$at_ret" == "" ]; then
904		echo "28:Fail to unlock the SIM PIN: $2."
905		exit 28
906	fi
907
908	echo "done."
909elif [ "$1" == "lockpin" ]; then
910	# $2: 1, lock; 0, unlock. $3: the original PIN.
911	simauth=`nvram get usb_modem_act_auth`
912	if [ "$simauth" == "1" ]; then
913		echo "29:SIM need to input the PIN code first."
914		exit 29
915	elif [ "$simauth" == "4" -o "$simauth" == "5" ]; then # lock
916		echo "30:SIM had been blocked."
917		exit 30
918	elif [ "$simauth" == "0" ]; then # lock
919		echo "31:Can't get the SIM auth state."
920		exit 31
921	fi
922
923	if [ "$2" == "" ]; then
924		echo "32:Decide to lock/unlock PIN."
925		exit 32
926	fi
927
928	if [ "$3" == "" ]; then
929		echo "33:Need the PIN code."
930		exit 33
931	fi
932
933	if [ "$2" == "1" -a "$simauth" == "1" ] || [ "$2" == "1" -a "$simauth" == "2" ] || [ "$2" == "0" -a "$simauth" == "3" ]; then # lock
934		if [ "$simauth" == "1" -o "$simauth" == "2" ]; then
935			echo "had locked."
936		elif [ "$simauth" == "3" ]; then
937			echo "had unlocked."
938		fi
939
940		echo "done."
941		exit 0
942	fi
943
944	at_ret=`$at_lock modem_at.sh '+CLCK="SC",'$2',"'$3'"' 2>/dev/null`
945	ok_ret=`echo -n $at_ret |grep "OK" 2>/dev/null`
946	if [ -z "$ok_ret" ]; then
947		if [ "$2" == "1" ]; then
948			echo "34:Fail to lock PIN."
949			exit 34
950		else
951			echo "35:Fail to unlock PIN."
952			exit 35
953		fi
954	fi
955
956	echo "done."
957elif [ "$1" == "pwdpin" ]; then
958	if [ "$2" == "" ]; then
959		echo "36:Need to input the original PIN code."
960		exit 36
961	elif [ "$3" == "" ]; then
962		echo "37:Need to input the new PIN code."
963		exit 37
964	fi
965
966	at_ret=`$at_lock modem_at.sh '+CPWD="SC",'$2','$3 |grep "OK" 2>/dev/null`
967	if [ "$at_ret" == "" ]; then
968		echo "38:Fail to change the PIN."
969		exit 38
970	fi
971
972	echo "done."
973elif [ "$1" == "gnws" ]; then
974	if [ "$is_gobi" -eq "1" ]; then
975		at_cgnws=`$at_lock modem_at.sh '+CGNWS' |grep "+CGNWS:" |awk '{FS=":"; print $2}' 2>/dev/null`
976		if [ "$at_cgnws" == "" ]; then
977			echo "Fail to get the CGNWS."
978			exit 39
979		fi
980
981		roaming=`echo "$at_cgnws" |awk '{FS=","; print $1}' 2>/dev/null`
982		signal=`echo "$at_cgnws" |awk '{FS=","; print $2}' 2>/dev/null`
983		reg_type=`echo "$at_cgnws" |awk '{FS=","; print $3}' 2>/dev/null`
984		reg_state=`echo "$at_cgnws" |awk '{FS=","; print $4}' 2>/dev/null`
985		mcc=`echo "$at_cgnws" |awk '{FS=","; print $5}' 2>/dev/null`
986		mnc=`echo "$at_cgnws" |awk '{FS=","; print $6}' 2>/dev/null`
987		spn=`echo "$at_cgnws" |awk '{FS=","; print $7}' 2>/dev/null`
988		isp_long=`echo "$at_cgnws" |awk '{FS=","; print $8}' 2>/dev/null`
989		isp_short=`echo "$at_cgnws" |awk '{FS=","; print $9}' 2>/dev/null`
990
991		echo "   Roaming=$roaming."
992		echo "    Signal=$signal."
993		echo " REG. Type=$reg_type."
994		echo "REG. State=$reg_state."
995		echo "       MCC=$mcc."
996		echo "       MNC=$mnc."
997		echo "       SPN=$spn."
998		echo "  ISP Long=$isp_long."
999		echo " ISP Short=$isp_short."
1000		echo "done."
1001	fi
1002elif [ "$1" == "send_sms" ]; then
1003	# $2: phone number, $3: sended message.
1004	at_ret=`$at_lock modem_at.sh +CMGF? 2>/dev/null`
1005	at_ret_ok=`echo -n "$at_ret" |grep "OK" 2>/dev/null`
1006	msg_format=`echo -n "$at_ret" |grep "+CMGF:" |awk '{FS=" "; print $2}' 2>/dev/null`
1007	if [ -z "$at_ret_ok" ] || [ "$msg_format" != "1" ]; then
1008		#echo "Changing the message format to the Text mode..."
1009		at_ret=`$at_lock modem_at.sh +CMGF=1 |grep "OK" 2>/dev/null`
1010		if [ "$at_ret" == "" ]; then
1011			echo "40:Fail to set the message format to the Text mode."
1012			exit 40
1013		fi
1014	fi
1015
1016	if [ -z "$2" -o -z "$3" ]; then
1017		echo "41:Usage: $0 $1 <phone number> <sended message>"
1018		exit 41
1019	fi
1020
1021	at_ret=`$at_lock modem_at.sh +CMGS=\"$2\" |grep ">" 2>/dev/null`
1022	at_ret1=`echo -n "$at_ret" |grep ">" 2>/dev/null`
1023	if [ -z "at_ret1" ]; then
1024		echo "42:Fail to execute +CMGS."
1025		exit 42
1026	fi
1027
1028	nvram set freeze_duck=10
1029	at_ret=`$at_lock chat -t 10 -e '' "$3^z" OK >> /dev/$modem_act_node < /dev/$modem_act_node 2>/tmp/at_ret`
1030	at_ret_ok=`echo -n "$at_ret" |grep "OK" 2>/dev/null`
1031	if [ -z "at_ret_ok" ]; then
1032		echo "43:Fail to send the message: $3."
1033		exit 43
1034	fi
1035
1036	echo "done."
1037elif [ "$1" == "simdetect" ]; then
1038	if [ "$is_gobi" -eq "1" ] && [ "$usb_gobi2" != "1" ]; then
1039		# $2: 0: disable, 1: enable.
1040		at_ret=`$at_lock modem_at.sh '$NV70210' 2>/dev/null`
1041		ret=`echo -n $at_ret |grep "OK" 2>/dev/null`
1042		if [ -z "$ret" ]; then
1043			echo "44:Fail to get the value of SIM detect."
1044			exit 44
1045		fi
1046
1047		current=`echo -n $at_ret |awk '{print $2}'`
1048
1049		if [ -z "$2" ]; then
1050			echo "$current"
1051			nvram set usb_modem_act_simdetect=$current
1052		elif [ "$2" == "1" -a "$current" == "0" ] || [ "$2" == "0" -a "$current" == "1" ]; then
1053			at_ret=`$at_lock modem_at.sh '$NV70210='$2 |grep "OK" 2>/dev/null`
1054			if [ -z "$at_ret" ]; then
1055				echo "45:Fail to set the SIM detect to be $2."
1056				exit 45
1057			fi
1058			nvram set usb_modem_act_simdetect=$2
1059
1060			# Use reboot to replace this.
1061			#at_ret=`$at_lock modem_at.sh '+CFUN=1,1' |grep "OK" 2>/dev/null`
1062			#if [ -z "$at_ret" ]; then
1063			#	echo "45:Fail to reset the Gobi."
1064			#	exit 46
1065			#fi
1066		fi
1067
1068		echo "done."
1069	fi
1070elif [ "$1" == "number" ]; then
1071	echo "Getting Phone number..."
1072	at_ret=`$at_lock modem_at.sh '+CNUM' 2>/dev/null`
1073	ret=`echo "$at_ret" |grep "^[0-9+].*$" 2>/dev/null`
1074	if [ "$ret" == "" ]; then
1075		echo "47:Fail to get the Phone number from $modem_act_node."
1076		exit 47
1077	fi
1078
1079	nvram set usb_modem_act_num=$ret
1080
1081	echo "done."
1082elif [ "$1" == "smsc" ]; then
1083	echo "Getting SMS centre..."
1084	at_ret=`$at_lock modem_at.sh '+CSCA?' |grep "+CSCA: " 2>/dev/null`
1085	if [ "$at_ret" == "" ]; then
1086		echo "48:Fail to get the SMSC from $modem_act_node."
1087		exit 48
1088	fi
1089
1090	smsc=`echo -n "$at_ret" |awk '{FS="\""; print $2}' 2>/dev/null`
1091
1092	nvram set usb_modem_act_smsc=$smsc
1093
1094	echo "smsc=$smsc."
1095	echo "done."
1096fi
1097
1098