network.sh 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. #!/bin/sh
  2. # Lock PCI, If PCILock is empty, will lock first PCI.
  3. PCIStatic=1
  4. # PCILock="<PCI>,<RFCN>,<BAND>,<SCS>"
  5. PCILock="530,627264,78,1"
  6. # BAND="", BAND="78", BAND="1:78"
  7. BAND=""
  8. # Use SIM Card Index
  9. SIMCardIndex=1
  10. # Empty NVRAM
  11. EmptyNVRAM=0
  12. [ -e /bin/sendat ] || exit 1
  13. PORT=2
  14. MaxNum=120
  15. LOG="/tmp/network.log"
  16. function Now() {
  17. echo -ne `date '+[%Y/%m/%d %H:%M:%S']`
  18. }
  19. function WaitAT() {
  20. for i in $(seq 1 $MaxNum); do
  21. if [ -e "/dev/ttyUSB${PORT}" ]; then
  22. /bin/sendat "$PORT" 'AT' |grep -q 'OK'
  23. [ $? -eq 0 ] && return 0
  24. fi
  25. sleep 1
  26. done
  27. return 1
  28. }
  29. function WaitSIM(){
  30. WaitAT || return 1
  31. echo "$(Now) Wait SIM ..." |tee -a "$LOG"
  32. for i in $(seq 1 $MaxNum); do
  33. stat=`/bin/sendat "$PORT" 'AT+QINISTAT' |grep '+QINISTAT:' |grep -o '[0-9]*'`
  34. echo "$(Now) SIM Satus: $stat" |tee -a "$LOG"
  35. [ "$stat" -ne 7 ] && sleep 1 && continue || break
  36. done
  37. }
  38. function Driver(){
  39. echo "$(Now) Set Driver ..." |tee -a "$LOG"
  40. [ $1 -gt 0 ] && /bin/sendat "$PORT" 'AT+QPRTPARA=3'
  41. /bin/sendat "$PORT" 'AT+QCFG="pcie/mode",1'
  42. /bin/sendat "$PORT" 'AT+QCFG="data_interface",1,0'
  43. /bin/sendat "$PORT" 'AT+QETH="eth_driver","r8168",0'
  44. /bin/sendat "$PORT" 'AT+QETH="eth_driver","r8125",1'
  45. /bin/sendat "$PORT" 'AT+QCFG="volte_disable",0'
  46. /bin/sendat "$PORT" 'AT+QCFG="sms_control",1,1'
  47. /bin/sendat "$PORT" 'AT+QCFG="call_control",0,0'
  48. /bin/sendat "$PORT" 'AT+CPMS="ME","ME","ME"'
  49. /bin/sendat "$PORT" 'AT+CMGF=0'
  50. }
  51. function NR5G(){
  52. rfBand=`/bin/sendat "$PORT" 'AT+QNWPREFCFG="rf_band"' |grep '+QNWPREFCFG:\s*"nr5g_band"' |cut -d',' -f2 |grep -o '[0-9A-Za-z:]*'`
  53. band="${1:-$rfBand}"
  54. echo "$(Now) Set NR5G ${band} ..." |tee -a "$LOG"
  55. /bin/sendat "$PORT" 'AT+QNWPREFCFG="roam_pref",255'
  56. /bin/sendat "$PORT" 'AT+QNWPREFCFG="rat_acq_order",NR5G:LTE:WCDMA'
  57. /bin/sendat "$PORT" 'AT+QNWPREFCFG="mode_pref",NR5G'
  58. /bin/sendat "$PORT" 'AT+QNWPREFCFG="nr5g_disable_mode",2'
  59. /bin/sendat "$PORT" "AT+QNWPREFCFG=\"nr5g_band\",${band}"
  60. }
  61. function COPS(){
  62. echo "$(Now) Search COPS ..." |tee -a "$LOG"
  63. /bin/sendat "$PORT" 'AT+COPS=2'
  64. /bin/sendat "$PORT" 'AT+COPS=0'
  65. for i in $(seq 1 $MaxNum); do
  66. cops=`/bin/sendat "$PORT" 'AT+COPS?' |grep '+COPS: 0,' |cut -d'"' -f2 |sed 's/[[:space:]]//g'`
  67. [ -n "$cops" ] && echo "$(Now) COPS: $cops" |tee -a "$LOG" && break || sleep 1
  68. done
  69. }
  70. function Modem(){
  71. echo "$(Now) Reset Modem ..." |tee -a "$LOG"
  72. SIMCard="${1:-1}"
  73. ResetModem="${2:-0}"
  74. /bin/sendat "$PORT" 'AT+QNWCFG="data_roaming",1'
  75. /bin/sendat "$PORT" 'AT+QCFG="ims",1'
  76. /bin/sendat "$PORT" 'AT+QSCLK=0,0'
  77. /bin/sendat "$PORT" 'AT+QMAPWAC=1'
  78. /bin/sendat "$PORT" "AT+QUIMSLOT=${SIMCard}"
  79. [ "$ResetModem" -gt 0 ] && /bin/sendat "$PORT" 'AT+CFUN=1,1'
  80. sleep 5
  81. }
  82. function MPDN() {
  83. WaitAT || return 1
  84. echo "$(Now) Empty MPDN ..." |tee -a "$LOG"
  85. /bin/sendat "$PORT" 'AT+QMAP="MPDN_rule",0'
  86. sleep 5
  87. WaitAT || return 1
  88. for i in $(seq 1 $MaxNum); do
  89. result=`/bin/sendat "$PORT" 'AT+QMAP="MPDN_rule"'`
  90. echo "$result" |grep -q '0,0,0,0,0'
  91. [ $? -eq 0 ] && break || sleep 1
  92. echo "$result" |grep -q '0,1,0,1,1'
  93. [ $? -eq 0 ] && return 1
  94. done
  95. WaitAT || return 1
  96. echo "$(Now) Reset MPDN ..." |tee -a "$LOG"
  97. /bin/sendat "$PORT" 'AT+QMAP="MPDN_rule",0,1,0,1,1,"FF:FF:FF:FF:FF:FF"'
  98. sleep 5
  99. WaitAT || return 1
  100. for i in $(seq 1 $MaxNum); do
  101. result=`/bin/sendat "$PORT" 'AT+QMAP="MPDN_rule"'`
  102. echo "$result" |grep -q '0,1,0,1,1'
  103. [ $? -eq 0 ] && break || sleep 1
  104. echo "$result" |grep -q '0,0,0,0,0'
  105. [ $? -eq 0 ] && return 1
  106. done
  107. }
  108. function WaitIPv4() {
  109. WaitAT || return 1
  110. echo "$(Now) Wait IPv4 ..." |tee -a "$LOG"
  111. maxNum=$(($MaxNum/4))
  112. for i in $(seq 1 $maxNum); do
  113. ipv4=`/bin/sendat "$PORT" 'AT+CGPADDR=1' |grep '+CGPADDR:' |cut -d',' -f2 |grep -o '[0-9\.]*'`
  114. [ -n "$ipv4" ] && [ "$ipv4" != "0.0.0.0" ] && echo "$(Now) IPv4: $ipv4" |tee -a "$LOG" && return 0
  115. sleep 1
  116. done
  117. return 1
  118. }
  119. function ReloadIf() {
  120. [ -e /sbin/ifup ] || return 1
  121. echo "$(Now) Check Interface ..." |tee -a "$LOG"
  122. mode="${1:-0}"
  123. if [ "$mode" -eq 0 ]; then
  124. ipv4=`/bin/sendat "$PORT" 'AT+CGPADDR=1' |grep '+CGPADDR:' |cut -d',' -f2 |grep -o '[0-9\.]*'`
  125. [ -n "$ipv4" ] || ipv4="0.0.0.0"
  126. ipv4If=`ubus call network.interface.wan status |grep '"address":' |cut -d'"' -f4 |grep -o '[0-9\.]*'`
  127. [ -n "$ipv4If" ] || ipv4If="0.0.0.0"
  128. [ "$ipv4" == "$ipv4If" ] && return 0
  129. fi
  130. echo "$(Now) Reload Interface ..." |tee -a "$LOG"
  131. /sbin/ifup wan
  132. /sbin/ifup wan6
  133. return 0
  134. }
  135. # /bin/sendat 3 'AT+QSCAN=2,1'
  136. # /bin/sendat 3 'AT'
  137. function LockNR5G() {
  138. # pci:rfcn:band:scs
  139. echo "$(Now) NR5G Lock ..." |tee -a "$LOG"
  140. cell=`/bin/sendat "$PORT" 'AT+QENG="servingcell"'|grep '+QENG:' |cut -d',' -f8,10,16,11`
  141. echo "$(Now) NR5G Cell: $cell" |tee -a "$LOG"
  142. [ -n "$cell" ] || return 1
  143. lock="${1:-$cell}"
  144. echo "$(Now) NR5G Lock: $lock" |tee -a "$LOG"
  145. pci=`echo "$lock" |cut -d',' -f1`
  146. rfcn=`echo "$lock" |cut -d',' -f2`
  147. band=`echo "$lock" |cut -d',' -f3`
  148. scs=`echo "$lock" |cut -d',' -f4`
  149. [ "$lock" != "$cell" ] && {
  150. meas=`/bin/sendat "$PORT" 'AT+QNWCFG="nr5g_meas_info"' |grep '+QNWCFG:' |cut -d',' -f4,3`
  151. echo "$meas" |grep -q "^${rfcn},${pci}$"
  152. [ $? -eq 0 ] || {
  153. echo "$(Now) NR5G Lock: NotFoundPCI" |tee -a "$LOG"
  154. return 1
  155. }
  156. }
  157. scsHz="$(($((2**${scs}))*15))"
  158. /bin/sendat "$PORT" "AT+QNWLOCK=\"common/5g\",${pci},${rfcn},${scsHz},${band}" |grep -q "OK"
  159. [ $? -eq 0 ] && {
  160. echo "$(Now) NR5G: N${band}:${rfcn}:${pci}" |tee -a "$LOG"
  161. return 0
  162. } || {
  163. echo "$(Now) NR5G Lock: Fail" |tee -a "$LOG"
  164. return 1
  165. }
  166. }
  167. echo "$(Now) START" |tee -a "$LOG"
  168. for i in $(seq 1 $MaxNum); do
  169. n=$(($i/2))
  170. m=$(($i%2))
  171. [ $m -eq 1 ] && {
  172. [ $n -eq 0 ] && {
  173. Driver "$EmptyNVRAM"
  174. NR5G "$BAND"
  175. COPS
  176. }
  177. Modem "$SIMCardIndex" "$n"
  178. WaitSIM
  179. }
  180. [ "$PCIStatic" -gt 0 ] && LockNR5G "$PCILock"
  181. MPDN || continue
  182. WaitIPv4 || continue
  183. ReloadIf && break
  184. done
  185. echo "$(Now) FINISH" |tee -a "$LOG"
  186. # /bin/sendat "$PORT" 'AT+CMGL=4'
  187. # sms_tool -s "ME" -f "%Y/%d/%m %H:%M:%S" -d /dev/ttyUSB2 -j recv