-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcell_mgmt
executable file
·2467 lines (2133 loc) · 52.9 KB
/
cell_mgmt
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
#
# Copyright (C) MOXA Inc. All rights reserved.
# Copyright (C) 2014-2015 Lock Lin <[email protected]>
# Copyright (C) 2015 Harry YJ Jhou <[email protected]>
#
# This software is distributed under the terms of the
# MOXA License. See the file COPYING-MOXA for details.
#
# cell_mgmt
# cellular management. It supports sierra MC9090 MC7304 MC7354.
#
#--------- Initialize Global variables ---------
PKG="moxa-cellular-utils"
VERSION="2.10.13"
PROG=$0
PID=$$
STATUS_DIR=/var/run/${PKG}
PRODUCT_NAME_FILE=${STATUS_DIR}/product_name
# profile
CONF_DIR=/etc/${PKG}
PROFILE_FILE=${CONF_DIR}/${PKG}.conf
PRODUCT_DIR=${CONF_DIR}/product.d
MODULE_DIR=${CONF_DIR}/module.d
MODULE_ON_DIR=${CONF_DIR}/module-on.d
MODULE_OFF_DIR=${CONF_DIR}/module-off.d
# Get product name and store
if [ ! -f "${PRODUCT_NAME_FILE}" ]; then
KVERSION="$(which kversion)"
[ -z "${KVERSION}" ] && >&2 echo "Error: \`kversion\` not found." && exit 1
PRODUCT="$(${KVERSION} 2> /dev/null | awk '{print $1}')"
mkdir -p ${STATUS_DIR}
echo "${PRODUCT}" > ${PRODUCT_NAME_FILE}
else
PRODUCT=$(cat ${PRODUCT_NAME_FILE})
fi
# QMI
# new libqmi includes a tool called "qmi-proxy"
# use qmicli -p means interaction with the QMI interface of modem by "qmi-proxy"
QMICLI=$(which qmicli)
QMICLI_END="-p"
[ -z "${QMICLI}" ] && >&2 echo "Error: \`qmicli\` not found." && exit 1
# variables
FORCE=0
NEED_LOCK=0
NEED_INITIALIZE=0
_logger () {
logger -t "cell_mgmt" "${1}"
}
_exit() {
local ret=${1:-0}
if [ $ret -eq 60 ]; then
_module_crash_exit
fi
exit $ret
}
_error() {
local code=${1:-0}
if [ ${code} -eq 0 ]; then
>&2 echo "$2"
else
case ${code} in
1)
>&2 echo "Error: Unknown error"
;;
2)
>&2 echo "Error: \"${PROFILE_FILE}\" does not exist."
;;
3)
>&2 echo "Error: Operation not supported."
;;
4)
>&2 echo "Error: Invalid input."
;;
5)
>&2 echo "Error: Module not supported."
;;
6)
>&2 echo "Error: Module not found."
;;
60)
>&2 echo "Error: ${PROG} is busy, please " \
"wait a moment and try again."
;;
*)
;;
esac
fi
}
_func_exist() {
if type ${1} 2> /dev/null | grep -q 'function' 2>/dev/null; then
return 0
fi
return 1
}
# nested is not recommended, recursive calling may occurred
__get_op_func() {
local op="$1"
if _func_exist _module_${MODULE_ID}_${op}; then
echo _module_${MODULE_ID}_${op}
elif [ x"${QMI_PORT}" != x"NotSupport" ] && _func_exist qmi_${op}; then
echo qmi_${op}
elif [ x"${AT_PORT}" != x"NotSupport" ] && _func_exist at_${op}; then
echo at_${op}
else
echo ""
fi
}
_get_op_func() {
local op="$1"
# cell_${op} or _xxx_cell_${op}: top most functions, nested is allowed
if _func_exist _module_${MODULE_ID}_cell_${op}; then
echo _module_${MODULE_ID}_cell_${op}
elif _func_exist cell_${op}; then
echo cell_${op}
else
__get_op_func "${op}"
fi
}
_exec_op_rtn() {
local func="$(__get_op_func $1)"
if [ x"${func}" != x"" ]; then
#echo "eval ${func} ${@:2}"
eval ${func} ${@:2}
else
_error 3
return 3
fi
}
_exec_op() {
local func="$(__get_op_func $1)"
if [ x"${func}" == x"" ]; then
_error 3
_exit 3
fi
#echo "eval ${func} ${@:2}"
eval ${func} ${@:2}
}
_save_state() {
local KEY=$1
local VAL=$2
echo "Saving state... ($KEY: $VAL)"
if [ -f $STATE_FILE ]; then
PREVIOUS=`cat $STATE_FILE 2>/dev/null`
PREVIOUS=`echo "$PREVIOUS" | grep -v $KEY`
if [ "x$PREVIOUS" != "x" ]; then
echo $PREVIOUS > $STATE_FILE
else
rm $STATE_FILE
fi
fi
if [ "x$VAL" != "x" ]; then
echo "$KEY=\"$VAL\"" >> $STATE_FILE
fi
}
_load_state() {
if [ -f $STATE_FILE ]; then
# Loading previous state
. $STATE_FILE
fi
}
_clear_state() {
echo "Clearing state..."
rm -f $STATE_FILE
rm -f $STATUS_FILE
}
_load_profile() {
. $PROFILE_FILE
for conf in $(ls ${PRODUCT_DIR}); do
if [ "${conf: -5}" == ".conf" ]; then
. ${PRODUCT_DIR}/$conf
fi
done
for conf in $(ls ${MODULE_DIR}); do
if [ "${conf: -5}" == ".conf" ]; then
. ${MODULE_DIR}/$conf
fi
done
eval "_product_${PRODUCT}_profile" 2>/dev/null
if [ x"$?" != x"0" ]; then
_error 0 "Error: \`kversion\` or profile not found."
_exit 1
fi
}
_search_wwan_node() {
local module_name
local vendor_id
local product_id
local slot_num=0
local id=-1
local unsupport_num=0
for i in ${MODULE_PATH[@]}; do
let slot_num++
if [ ! -e $i ]; then
continue
fi
vendor_id=$(cat $i/idVendor 2> /dev/null)
product_id=$(cat $i/idProduct 2> /dev/null)
module_name=$(cat $i/product 2> /dev/null)
if ! _func_exist _module_${vendor_id}_${product_id}; then
_error 0 "Error: Module \"${module_name}\" not supported."
let unsupport_num++
continue
fi
let id++
ALL_MODULE_ID[${id}]=${vendor_id}_${product_id}
ALL_MODULE_NAME[${id}]=${module_name}
ALL_SLOT_NUM[${id}]=$slot_num
ALL_INTERFACE[${id}]=""
ALL_QMI_PROTOCOL[${id}]="802-3"
ALL_QMI_PORT[${id}]="NotSupport"
ALL_AT_PORT[${id}]="NotSupport"
ALL_GPS_PORT[${id}]="NotSupport"
ALL_MODEM_PORT[${id}]="NotSupport"
ALL_AT_PORT_RESV[${id}]="NotSupport"
ALL_AT_TIMEOUT[${id}]="0.3"
eval "_module_${vendor_id}_${product_id} \
$i ${id} \"${module_name}\""
if [ $? -ne 0 ]; then
_error 0 "Error: Initialize module"\
"\"${ALL_MODULE_NAME[$id]}\" failed."
continue
fi
done
if [ $unsupport_num -gt 0 ] && [ $id -lt 0 ]; then
_error 5
_exit 5
fi
}
_set_state_file() {
local slot=$1
mkdir -p "${STATUS_DIR}"
LOCK_FD=$((slot+119))
LOCK_FD_GLOBAL=$((slot+129))
LOCK_FILE="${STATUS_DIR}/${slot}.lock"
LOCK_FILE_GLOBAL="${STATUS_DIR}/${slot}.global.lock"
INIT_FILE="${STATUS_DIR}/${slot}.init"
STATE_FILE="${STATUS_DIR}/${slot}.state"
STATUS_FILE="${STATUS_DIR}/${slot}.status"
AT_LOCKED_FILE="${STATUS_DIR}/${slot}.at.locked"
}
_set_variables() {
export SLOT="${ALL_SLOT_NUM[$INTERFACE]}"
[ -z "${SLOT}" ] && SLOT=1
MODULE_ID="${ALL_MODULE_ID[$INTERFACE]}"
MODULE_NAME="${ALL_MODULE_NAME[$INTERFACE]}"
QMI_PROTOCOL=${ALL_QMI_PROTOCOL[${INTERFACE}]}
QMI_NODE="${ALL_INTERFACE[$INTERFACE]}"
export WWAN_NODE="${QMI_NODE}"
if [ x"${ALL_QMI_PORT[$INTERFACE]}" == x"NotSupport" ]; then
QMI_PORT="NotSupport"
else
export QMI_PORT="/dev/${ALL_QMI_PORT[$INTERFACE]}"
fi
if [ x"${ALL_AT_PORT[$INTERFACE]}" == x"NotSupport" ]; then
AT_PORT="NotSupport"
else
export AT_PORT="/dev/${ALL_AT_PORT[$INTERFACE]}"
fi
if [ x"${ALL_AT_PORT_RESV[$INTERFACE]}" == x"NotSupport" ]; then
AT_PORT_RESV="NotSupport"
else
export AT_PORT_RESV=$(echo ${ALL_AT_PORT_RESV[$INTERFACE]} | \
awk '{for(i=1;i<=NF;++i) {sub(/^/,"/dev/",$i)};print}')
fi
if [ x"${ALL_GPS_PORT[$INTERFACE]}" == x"NotSupport" ]; then
GPS_PORT="NotSupport"
else
export GPS_PORT="/dev/${ALL_GPS_PORT[$INTERFACE]}"
fi
if [ x"${ALL_MODEM_PORT[$INTERFACE]}" == x"NotSupport" ]; then
MODEM_PORT="NotSupport"
else
export MODEM_PORT="/dev/${ALL_MODEM_PORT[$INTERFACE]}"
fi
AT_TIMEOUT="${ALL_AT_TIMEOUT[$INTERFACE]}"
# state file for different interface
if [ x"$INTERFACE" != x"" ]; then
_set_state_file ${SLOT}
fi
# Initialize if never initialized before
if [ ! -f "${INIT_FILE}" ]; then
NEED_INITIALIZE=1
fi
# Load previous state, if any
CID=""
PDH=""
_load_state
# Update WWAN interface
if [ -z "${QMI_NODE}" ]; then
QMI_NODE="$(_exec_op status | grep "^PPPIFName: " | \
awk '{print $2}')"
fi
}
_init() {
unset ALL_MODULE_ID
unset ALL_MODULE_NAME
unset ALL_SLOT_NUM
unset ALL_INTERFACE
unset ALL_QMI_PROTOCOL
unset ALL_QMI_PORT
unset ALL_AT_PORT
unset ALL_GPS_PORT
unset ALL_MODEM_PORT
unset ALL_AT_TIMEOUT
_search_wwan_node
if [ ${#ALL_INTERFACE[@]} -eq 0 ]; then
INTERFACE=
return
fi
if [ x"${INTERFACE}" == x"" ] || \
[ ${INTERFACE} -ge ${#ALL_INTERFACE[@]} ]; then
INTERFACE=0
fi
_set_variables
}
init() {
if [ -f $PROFILE_FILE ]; then
_load_profile
_init
else
_error 2
_exit 2
fi
}
_signal_csq2dbm() {
local signal="$1"
signal=${signal##*AT}
signal=${signal%%,*}
if [ ! -z "${signal}" ]; then
if [ "${signal}" -le "31" 2>/dev/null ] && [ "${signal}" -ge "2" 2>/dev/null ] ; then
let signal=signal-2
let signal=signal*2-109
echo "${signal} dBm"
else
echo "-999"
fi
else
echo "-999"
fi
}
_signal_dbm2csq() {
local signal="$1"
# csq = (dbm + 113) / 2
if [ -z "${signal}" ] || [ $signal -le -113 ]; then
echo "99"
return 0
fi
signal=$(echo ${signal} | awk '{printf "%d\n",int((($1+113)/2+0.5))}')
echo ${signal}
}
_connect_error_note() {
>&2 echo "Please check the following items before start LTE connection:"
>&2 echo " 1. Antenna setting"
>&2 echo " 2. SIM card inserted and PIN code unlocked, \`sim_status\`"
>&2 echo " 3. Signal strength \`signal\`"
>&2 echo " 4. Remember to set_apn before start LTE connection"
}
_get_profile() {
local key="$1"
local _id="$INTERFACE"
[ x"${_id}" == x"0" ] && _id=""
key=${key}${_id}
echo ${!key}
}
_update_profile() {
local key="$1"
local value="$2"
if grep -q "${key}=" ${PROFILE_FILE}; then
eval "sed -i s/${key}=.*/${key}=${value}/ $PROFILE_FILE"
else
echo "${key}=${value}" >> ${PROFILE_FILE}
fi
}
_contains() {
[[ $1 =~ (^|[[:space:]])$2($|[[:space:]]) ]] && return 0 || return 1
}
_start_network_init() {
ignore_dns_gw=0
local card_status=""
local VAL_OPTS="APN Auth Username Password PIN Phone \
OpCode OpRadioType OpPreferredRadioType"
local BOOL_OPTS="ignore-dns-gw"
local _id="$INTERFACE"
[ x"${_id}" == x"0" ] && _id=""
# get value from configuration
for opt in ${VAL_OPTS}; do
eval "${opt}=\$${opt}${_id}"
done
# modify APN Username Password PIN
for i in $(seq 1 $#); do
eval "arg=\$$i"
param=$(echo "$arg" | cut -d'=' -f1)
if _contains "${VAL_OPTS}" ${param}; then
eval "${param}=\"$(echo "$arg" | cut -d'=' -f2-)\""
_update_profile ${param}${_id} ${!param}
elif _contains "${BOOL_OPTS}" ${param}; then
param=$(echo "${param}" | tr "-" "_")
eval "${param}=1"
fi
done
# check APN
if [ x"$APN" == x"" ]; then
_error 0 "Error: Please assign a valid APN."
_cell_usage_cmd _cell_usage_start
_exit 4
fi
card_status="$(_exec_op sim_status | grep "^+CPIN")"
if [ "$(echo "${card_status}" | grep "SIM PIN")" ] && [ x"$PIN" == x"" ]; then
_error 0 "Error: Please assign the PIN code to unlock SIM card."
_cell_usage_cmd _cell_usage_start
_exit 4
fi
# Check PIN status
_cell_unlock_pin "${PIN}" "_cell_usage_start"
}
#--------- qmi base functions -----------
_qmicli() {
local i
local qmi_out=""
local qmi_ret=0
for i in {1..5}; do
qmi_out="$(${QMICLI} -d ${QMI_PORT} $@ ${QMICLI_END} 2>&1)"
qmi_ret=$?
_logger "Type: [QMI], Port: [${QMI_PORT}], CMD: [${@}], \
Response: [${qmi_out}]"
# error 3 (internal) and 31 (invalid service type)
if ! (echo ${qmi_out} | grep "QMI protocol error (3)") &&
! (echo ${qmi_out} | grep "QMI protocol error (31)"); then
echo "${qmi_out}"
return 0
fi
done
# reset qmi service
_error 0 "Error: Internal error, please reset module and try again."
_exit 1
}
_qmicli_value() {
local output="$1"
local key="$2"
local num="$3"
local num_opt=""
local cmd=""
if [ -z "${3}" ]; then
num=1
fi
num_opt="| sed -n ${num}p"
cmd="echo \"${output}\" | awk '/${key}:/{ print}' ${num_opt} | \
sed \"s/^[^:]*: [']*//;s/[' \t]*$//\""
eval "${cmd}"
}
_qmi_start_network() {
local is_raw_ip="N"
local raw_ip_file="/sys/class/net/${QMI_NODE}/qmi/raw_ip"
local auth_arg=""
if [ x"$CID" != x"" ]; then
USE_PREVIOUS_CID="--client-cid=$CID"
fi
if [ x"$PDH" != x"" ]; then
_error 0 "Error: Cannot re-start network, PDH already exists."
_exit 1
fi
# initialize the interface with protocol
if [ x"${QMI_PROTOCOL}" == x"802-3" ]; then
is_raw_ip=N
elif [ x"${QMI_PROTOCOL}" == x"raw-ip" ]; then
is_raw_ip=Y
else
QMI_PROTOCOL="802-3"
is_raw_ip=N
fi
# initialize the authorization arg.
if [ x"$Auth" != x"" ]; then
auth_arg=",auth=$Auth"
fi
if [ x"$Username" != x"" ]; then
auth_arg="$auth_arg,username=$Username"
fi
if [ x"$Password" != x"" ]; then
auth_arg="$auth_arg,password=$Password"
fi
if [ -f "${raw_ip_file}" ]; then
echo ${is_raw_ip} > ${raw_ip_file} 2> /dev/null
_qmicli --set-expected-data-format=${QMI_PROTOCOL} || \
{ _error 0 "Error: Cannot update data format."; \
_exit 1; }
elif [ x"${QMI_PROTOCOL}" == x"raw-ip" ]; then
_error 0 "Error: \"raw-ip\" not supported."
_exit 1
fi
#START_NETWORK_CMD="$QMICLI -d $QMI_PORT --wds-start-network=$APN --client-no-release-cid --device-open-net=net-802-3|net-no-qos-header $QMICLI_END"
START_NETWORK_CMD="_qmicli --wds-start-network=apn=$APN,ip-type=4$auth_arg --client-no-release-cid --device-open-net=net-${QMI_PROTOCOL}|net-no-qos-header"
echo "Starting network with '$START_NETWORK_CMD'..."
if [ x"$QMIDEBUG" != x"" ]; then
if [ x"$INTERFACE" == x"" -o x"$INTERFACE" == x"1" ]; then
START_NETWORK_OUT="\
[/dev/cdc-wdm0] Network started
Packet data handle: '3634026241'
[/dev/cdc-wdm0] Client ID not released:
Service: 'wds'
CID: '80'"
else
START_NETWORK_OUT="\
[/dev/cdc-wdm1] Network started
Packet data handle: '363402624$3'
[/dev/cdc-wdm1] Client ID not released:
Service: 'wds'
CID: '80'"
fi
else
START_NETWORK_OUT=$($START_NETWORK_CMD)
fi
# Save the new CID if we didn't use any before
if [ x"$CID" == x"" ]; then
CID=`echo "$START_NETWORK_OUT" | sed -n "s/.*CID.*'\(.*\)'.*/\1/p"`
if [ x"$CID" == x"" ]; then
_error 0 "Error: Network start failed, client not allocated."
_connect_error_note
_exit 1
else
_save_state "CID" $CID
fi
fi
PDH=$(echo "$START_NETWORK_OUT" | sed -n "s/.*handle.*'\(.*\)'.*/\1/p")
if [ x"$PDH" == x"" ]; then
_error 0 "Error: Network start failed, no packet data handle."
_connect_error_note
# Cleanup the client
#$QMICLI -d "$QMI_PORT" --wds-noop --client-cid="$CID" $QMICLI_END
_qmicli --wds-noop --client-cid="$CID"
_clear_state
_exit 1
else
_save_state "PDH" $PDH
fi
echo "Network started successfully"
}
_qmi_stop_network() {
if [ x"$CID" == x"" ]; then
echo "Network already stopped"
elif [ x"$PDH" == x"" ]; then
echo "Network already stopped; need to cleanup CID $CID"
# Cleanup the client
#$QMICLI -d "$QMI_PORT" --wds-noop --client-cid="$CID" $QMICLI_END
_qmicli --wds-noop --client-cid="$CID"
PDH=""
else
#STOP_NETWORK_CMD="$QMICLI -d $QMI_PORT --wds-stop-network=$PDH --client-cid=$CID $QMICLI_END"
STOP_NETWORK_CMD="_qmicli --wds-stop-network=$PDH --client-cid=$CID"
echo "Stopping network with '$STOP_NETWORK_CMD'..."
if [ x"$QMIDEBUG" != x"" ]; then
STOP_NETWORK_OUT="\
[/dev/cdc-wdm$INTERFACE] Network stopped
"
else
STOP_NETWORK_OUT=`$STOP_NETWORK_CMD`
fi
PDH=""
CID=""
echo "Network stopped successfully"
fi
_clear_state
}
_qmi_packet_service_status() {
if [ x"$CID" != x"" ]; then
USE_PREVIOUS_CID="--client-cid=$CID --client-no-release-cid"
fi
#STATUS_CMD="$QMICLI -d $QMI_PORT --wds-get-packet-service-status $USE_PREVIOUS_CID $QMICLI_END"
STATUS_CMD="_qmicli --wds-get-packet-service-status $USE_PREVIOUS_CID"
echo "Getting status with '$STATUS_CMD'..."
if [ x"$QMIDEBUG" != x"" ]; then
CONN="disconnected"
else
local info
info=$(${STATUS_CMD}) || return $?
CONN="$(_qmicli_value "${info}" "Connection status")"
fi
if [ x"$CONN" == x"" ]; then
_error 0 "Error: Couldn't get packet service status."
_exit 1
else
echo "Status: $CONN"
if [ x"$CONN" != x"connected" ]; then
_exit 1
fi
fi
}
_qmi_kill_proxy() {
local proxy_pid=$(ps wwax | grep -v "grep" | grep "qmi-proxy" | awk '{print $1}')
if [ ! -z "${proxy_pid}" ]; then
kill ${proxy_pid}
fi
}
_qmi_card_app_state() {
# Only check the USIM infomation
local usim_info="$(echo "${1}" | grep -A1 -E "Application type:.*'usim.*'")"
local app_state="$(_qmicli_value "${usim_info}" "Application state")"
case "${app_state}" in
"pin1-or-upin-pin-required")
echo "+CPIN: SIM PIN"
;;
"puk1-or-upin-puk-required")
echo "+CPIN: SIM PUK"
;;
"ready")
echo "+CPIN: READY"
;;
*)
echo "+CME ERROR: SIM ${app_state}"
return 1
;;
esac
}
_qmi_card_status() {
local card_info=
local card_state=
card_info="$(_qmicli --uim-get-card-status)" || \
{ _error 1; _exit 1; }
card_state="$(_qmicli_value "${card_info}" "Card state")"
case "${card_state}" in
"absent")
echo "+CME ERROR: SIM not inserted"
;;
"present")
_qmi_card_app_state "${card_info}"
;;
*)
return 1
;;
esac
}
#--------- qmi functions -----------
qmi_fw_update() {
# arg1 is fw path.
local cmd="-w $QMI_PORT --update $1"
qmi-firmware-update $cmd
}
qmi_attach_status() {
local info=
local info_fix_char=
local oldIFS=$IFS
info="$(_qmicli --nas-get-serving-system)" || \
{ _error 1; _exit 1; }
IFS=
info_fix_char=`echo ${info} | sed "/CS:\|PS:/!d" `
IFS=$oldIFS
echo "CS: $(_qmicli_value "${info_fix_char}" "CS")"
echo "PS: $(_qmicli_value "${info_fix_char}" "PS")"
}
#check cellular status
qmi_status() {
local ps_status
ps_status=$(qmi_attach_status | grep PS | awk -F":" '{print $2}' | sed 's/ //g')
local con_status=$(_qmi_packet_service_status | grep "Status")
if [ x"$(echo ${con_status}| awk '{print $2}')" == x"connected" ]; then
if [ "${ps_status}" == "detached" ]; then
echo "Status: disconnected"
_exit 1
elif [ -f "${STATUS_FILE}" ]; then
cat "${STATUS_FILE}"
_exit 0
fi
fi
echo "Status: disconnected"
_exit 1
}
#cellular signal strength in level
qmi_signal() {
local sig_info=""
sig_info=$(qmi_signal_adv | grep "^RAT:\|^Signal Level:" | sed 's/RAT://g;s/Signal Level:/Level/g;' | sed 's/gsm/2G/g;s/umts/3G/g;s/lte/4G/g')
echo $sig_info
}
# cell_signal_adv
qmi_signal_adv() {
local rat=""
local csq=""
local current=""
local rssi=""
local ecio=""
local rsrp=""
local rsrq=""
local signal=""
signal=$(_qmicli --nas-get-signal-strength | \
sed "s/'\|'://g;s/^[ \t]*Network //g") || \
{ _error 1; exit 1; }
current=$(echo "${signal}" | awk '/Current:/{getline; print}')
rssi=$(echo "${signal}" | awk '/RSSI:/{getline; print}' | cut -d ' ' -f 2)
ecio=$(echo "${signal}" | awk '/ECIO:/{getline; print}' | cut -d ' ' -f 2)
rsrp=$(echo "${signal}" | awk '/RSRP:/{getline; print}' | cut -d ' ' -f 2)
rsrq=$(echo "${signal}" | awk '/RSRQ:/{getline; print}' | cut -d ' ' -f 2)
rat=$(echo ${current} | cut -d ' ' -f 1)
signal=$(echo "${rssi}" | awk '{print $(NF-1)}')
csq=$(_signal_dbm2csq ${signal})
[ -z "${csq}" ] && csq=99
if [ "${rat}" = "umts" ] || [ "${rat}" = "cmda" ] \
|| [ "${rat}" = "wcdma" ]; then
sig_indicator="RSSI"
sig_strength="${rssi}"
elif [ "${rat}" = "lte" ]; then
sig_indicator="RSRP"
sig_strength="${rsrp}"
else
rat="none"
sig_indicator="none"
sig_strength="-999"
fi
_format_signal_adv "${rat} ${csq} ${rssi:-"-"} - ${rscp:-"-"} ${ecio:-"-"} ${rsrp:-"-"} ${rsrq:-"-"} ${sig_strength} ${sig_indicator}"
return 0
}
# cell_start
qmi_start_network() {
local card_status=""
local ignore_dns_gw=0
local pin_status
_start_network_init $@
_qmi_start_network
# query IP from DHCP Server
ip link set ${QMI_NODE} up
if [ x"${QMI_PROTOCOL}" == x"raw-ip" ]; then
cell_udhcpc ${QMI_NODE} $ignore_dns_gw
else
cell_dhclient ${QMI_NODE} $ignore_dns_gw
fi
_exit 0
}
# cell_stop
qmi_stop_network() {
# release IP by dhclient -r
if [ x"${QMI_NODE}" != x"" ]; then
if [ x"${QMI_PROTOCOL}" == x"raw-ip" ]; then
cell_udhcpc_kill "${QMI_NODE}"
else
cell_dhclient_kill "${QMI_NODE}"
fi
fi
# cellular stop by qmi-network
_qmi_stop_network
if [ x"${QMI_NODE}" != x"" ]; then
ip link set ${QMI_NODE} down
fi
}
qmi_sim_status() {
_qmi_card_status
}
# unlock pin code
qmi_unlock_pin() {
local ret=
ret=$(_qmicli --uim-verify-pin=PIN1,$1) || \
{ _error 1; exit 1; }
if ! echo "${ret}" | grep -q 'PIN verified successfully'; then
echo $ret
_exit 1
fi
}
qmi_pin_retries() {
# [/dev/cdc-wdm0] PIN status retrieved successfully
# [/dev/cdc-wdm0] PIN1:
# Status: enabled-verified
# Verify: 3
# Unblock: 10
# [/dev/cdc-wdm0] PIN2:
# Status: blocked
# Verify: 0
# Unblock: 10
local pin_info
local pin_status
local pin_retries
pin_info="$(_qmicli --dms-uim-get-pin-status)" || \
{ _error 1; exit 1; }
pin_status="$(_qmicli_value "${pin_info}" "Status" 1)"
pin_retries="$(_qmicli_value "${pin_info}" "Verify" 1)"
case "${pin_status}" in
"enabled-verified"|"enabled-not-verified")
echo ${pin_retries}
;;
*)
echo 0
;;
esac
}
# sim card pin code protection
qmi_pin_protection(){
local pin_enable="$1"
local pin_current="$2"
local par_fail="0"
# user does not give current pin
[ $# -ne 2 ] && par_fail="1"
[ x"$pin_enable" != x"enable" ] && [ x"$pin_enable" != x"disable" ] && par_fail="1"
if [ x"$par_fail" == x"1" ]; then
_cell_usage_cmd _cell_usage_pin_protection
_exit 4
else
_qmicli --uim-set-pin-protection=PIN1,$pin_enable,$pin_current
_exit 0
fi
}
qmi_set_flight_mode() {
local flight=${1:-1}
local mode=""
# flight mode (persistent-low-power)
# online mode (online)
if [ ${flight} -eq 1 ]; then
mode="low-power"
else
mode="online"
fi
if _qmicli --dms-set-operating-mode="${mode}" &> /dev/null; then
return 0
fi
return 1
}
qmi_get_profiles() {
_qmicli --wds-get-profile-list=3gpp | \
awk -f ${CONF_DIR}/qmicli-profile-scan.awk
}
qmi_module_ids() {
local info=
info="$(_qmicli --dms-get-ids)" || \
{ _error 1; exit 1; }
echo IMEI: $(_qmicli_value "${info}" "IMEI")
echo ESN: $(_qmicli_value "${info}" "ESN")
}
qmi_iccid() {
local info=
info="$(_qmicli --dms-uim-get-iccid)" || \
{ _error 1; exit 1; }
echo ICC-ID: $(_qmicli_value "${info}" "ICCID")
}
qmi_imsi() {
local info=
info=$(_qmicli --dms-uim-get-imsi) || \
{ _error 1; exit 1; }
echo IMSI: $(_qmicli_value "${info}" "IMSI")
}
qmi_location_info() {
local info=""
local service=""
local lac=""
local tac=""
local cellid=""
local bid=""
local nid=""
local lcid=""
info="$(_qmicli --nas-get-cell-location-info)" || \
{ _error 1; exit 1; }
service="$(echo "${info}" | sed -n 2p | sed 's/[\t ]Info//g')"
if [[ ${service} == CDMA* ]]; then
bid=$(_qmicli_value "${info}" "Base Station ID")
nid=$(_qmicli_value "${info}" "Network ID")
elif [ x"${service}" == x"Intrafrequency LTE" ]; then
cellid=$(_qmicli_value "${info}" "Global Cell ID")
tac=$(_qmicli_value "${info}" "Tracking Area Code")
else
cellid=$(_qmicli_value "${info}" "Cell ID")
lac=$(_qmicli_value "${info}" "Location Area Code")
lcid=$(_qmicli_value "${info}" "${service} Cell ID")
fi
# for UMTS
if [ x"$lac" != x"" ]; then
echo LAC: $lac
fi