You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

586 lines
18 KiB

  1. #!/bin/sh
  2. # travelmate, a wlan connection manager for travel router
  3. # written by Dirk Brenken (dev@brenken.org)
  4. # This is free software, licensed under the GNU General Public License v3.
  5. # You should have received a copy of the GNU General Public License
  6. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  7. # set initial defaults
  8. #
  9. LC_ALL=C
  10. PATH="/usr/sbin:/usr/bin:/sbin:/bin"
  11. trm_ver="1.4.7"
  12. trm_sysver="unknown"
  13. trm_enabled=0
  14. trm_debug=0
  15. trm_iface="trm_wwan"
  16. trm_captive=1
  17. trm_proactive=1
  18. trm_netcheck=0
  19. trm_captiveurl="http://captive.apple.com"
  20. trm_minquality=35
  21. trm_maxretry=5
  22. trm_maxwait=30
  23. trm_timeout=60
  24. trm_listexpiry=0
  25. trm_radio=""
  26. trm_connection=""
  27. trm_rtfile="/tmp/trm_runtime.json"
  28. trm_fetch="$(command -v uclient-fetch)"
  29. trm_iwinfo="$(command -v iwinfo)"
  30. trm_wpa="$(command -v wpa_supplicant)"
  31. trm_action="${1:-"start"}"
  32. trm_pidfile="/var/run/travelmate.pid"
  33. # trim leading and trailing whitespace characters
  34. #
  35. f_trim()
  36. {
  37. local IFS trim="${1}"
  38. trim="${trim#"${trim%%[![:space:]]*}"}"
  39. trim="${trim%"${trim##*[![:space:]]}"}"
  40. printf '%s' "${trim}"
  41. }
  42. # load travelmate environment
  43. #
  44. f_envload()
  45. {
  46. local IFS sys_call sys_desc sys_model
  47. # (re-)initialize global list variables
  48. #
  49. unset trm_devlist trm_stalist trm_radiolist trm_active_sta
  50. # get system information
  51. #
  52. sys_call="$(ubus -S call system board 2>/dev/null)"
  53. if [ -n "${sys_call}" ]
  54. then
  55. sys_desc="$(printf '%s' "${sys_call}" | jsonfilter -e '@.release.description')"
  56. sys_model="$(printf '%s' "${sys_call}" | jsonfilter -e '@.model')"
  57. trm_sysver="${sys_model}, ${sys_desc}"
  58. fi
  59. # get eap capabilities and rebind protection setting
  60. #
  61. trm_eap="$("${trm_wpa}" -veap >/dev/null 2>&1; printf "%u" ${?})"
  62. trm_rebind="$(uci_get dhcp "@dnsmasq[0]" rebind_protection)"
  63. # load config and check 'enabled' option
  64. #
  65. config_cb()
  66. {
  67. local name="${1}" type="${2}"
  68. if [ "${name}" = "travelmate" ] && [ "${type}" = "global" ]
  69. then
  70. option_cb()
  71. {
  72. local option="${1}" value="${2}"
  73. eval "${option}=\"${value}\""
  74. }
  75. else
  76. option_cb()
  77. {
  78. return 0
  79. }
  80. fi
  81. }
  82. config_load travelmate
  83. if [ "${trm_enabled}" -ne 1 ]
  84. then
  85. f_log "info" "travelmate is currently disabled, please set 'trm_enabled' to '1' to use this service"
  86. exit 0
  87. fi
  88. # validate input ranges
  89. #
  90. if [ "${trm_minquality}" -lt 20 ] || [ "${trm_minquality}" -gt 80 ]
  91. then
  92. trm_minquality=35
  93. fi
  94. if [ "${trm_listexpiry}" -lt 0 ] || [ "${trm_listexpiry}" -gt 300 ]
  95. then
  96. trm_listexpiry=0
  97. fi
  98. if [ "${trm_maxretry}" -lt 1 ] || [ "${trm_maxretry}" -gt 10 ]
  99. then
  100. trm_maxretry=5
  101. fi
  102. if [ "${trm_maxwait}" -lt 20 ] || [ "${trm_maxwait}" -gt 40 ] || [ "${trm_maxwait}" -ge "${trm_timeout}" ]
  103. then
  104. trm_maxwait=30
  105. fi
  106. if [ "${trm_timeout}" -lt 30 ] || [ "${trm_timeout}" -gt 300 ] || [ "${trm_timeout}" -le "${trm_maxwait}" ]
  107. then
  108. trm_timeout=60
  109. fi
  110. # load json runtime file
  111. #
  112. json_load_file "${trm_rtfile}" >/dev/null 2>&1
  113. json_select data >/dev/null 2>&1
  114. if [ "${?}" -ne 0 ]
  115. then
  116. > "${trm_rtfile}"
  117. json_init
  118. json_add_object "data"
  119. fi
  120. }
  121. # gather radio information & bring down all STA interfaces
  122. #
  123. f_prep()
  124. {
  125. local IFS mode network radio disabled eaptype config="${1}" proactive="${2}"
  126. mode="$(uci_get wireless "${config}" mode)"
  127. network="$(uci_get wireless "${config}" network)"
  128. radio="$(uci_get wireless "${config}" device)"
  129. disabled="$(uci_get wireless "${config}" disabled)"
  130. eaptype="$(uci_get wireless "${config}" eap_type)"
  131. if [ -n "${config}" ] && [ -n "${radio}" ] && [ -n "${mode}" ] && [ -n "${network}" ]
  132. then
  133. if [ -z "${trm_radio}" ] && [ -z "$(printf "%s" "${trm_radiolist}" | grep -Fo "${radio}")" ]
  134. then
  135. trm_radiolist="$(f_trim "${trm_radiolist} ${radio}")"
  136. elif [ -n "${trm_radio}" ] && [ -z "${trm_radiolist}" ]
  137. then
  138. trm_radiolist="$(f_trim "$(printf "%s" "${trm_radio}" | \
  139. awk '{while(match(tolower($0),/radio[0-9]/)){ORS=" ";print substr(tolower($0),RSTART,RLENGTH);$0=substr($0,RSTART+RLENGTH)}}')")"
  140. fi
  141. if [ "${mode}" = "sta" ] && [ "${network}" = "${trm_iface}" ]
  142. then
  143. if { [ -z "${disabled}" ] || [ "${disabled}" = "0" ]; } && { [ "${proactive}" -eq 0 ] || [ "${trm_ifstatus}" != "true" ]; }
  144. then
  145. uci_set wireless "${config}" disabled 1
  146. elif [ "${disabled}" = "0" ] && [ "${trm_ifstatus}" = "true" ] && [ -z "${trm_active_sta}" ] && [ "${proactive}" -eq 1 ]
  147. then
  148. trm_active_sta="${config}"
  149. fi
  150. if [ -z "${eaptype}" ] || { [ -n "${eaptype}" ] && [ "${trm_eap:-1}" -eq 0 ]; }
  151. then
  152. trm_stalist="$(f_trim "${trm_stalist} ${config}-${radio}")"
  153. fi
  154. fi
  155. fi
  156. f_log "debug" "f_prep ::: config: ${config}, mode: ${mode}, network: ${network}, radio: ${radio}, trm_radio: ${trm_radio:-"-"}, trm_active_sta: ${trm_active_sta:-"-"}, proactive: ${proactive}, trm_eap: ${trm_eap:-"-"}, trm_rebind: ${trm_rebind:-"-"}, disabled: ${disabled}"
  157. }
  158. # check interface status
  159. #
  160. f_check()
  161. {
  162. local IFS ifname radio dev_status last_status config sta_essid sta_bssid result cp_domain wait mode="${1}" status="${2:-"false"}"
  163. if [ "${mode}" != "initial" ] && [ "${status}" = "false" ]
  164. then
  165. ubus call network reload
  166. wait=$((trm_maxwait/6))
  167. sleep ${wait}
  168. fi
  169. wait=1
  170. while [ "${wait}" -le "${trm_maxwait}" ]
  171. do
  172. dev_status="$(ubus -S call network.wireless status 2>/dev/null)"
  173. if [ -n "${dev_status}" ]
  174. then
  175. if [ "${mode}" = "dev" ]
  176. then
  177. if [ "${trm_ifstatus}" != "${status}" ]
  178. then
  179. trm_ifstatus="${status}"
  180. f_jsnup
  181. fi
  182. for radio in ${trm_radiolist}
  183. do
  184. result="$(printf "%s" "${dev_status}" | jsonfilter -l1 -e "@.${radio}.up")"
  185. if [ "${result}" = "true" ] && [ -z "$(printf "%s" "${trm_devlist}" | grep -Fo "${radio}")" ]
  186. then
  187. trm_devlist="$(f_trim "${trm_devlist} ${radio}")"
  188. fi
  189. done
  190. if [ "${trm_devlist}" = "${trm_radiolist}" ] || [ "${wait}" -eq "${trm_maxwait}" ]
  191. then
  192. ifname="${trm_devlist}"
  193. break
  194. else
  195. unset trm_devlist
  196. fi
  197. elif [ "${mode}" = "rev" ]
  198. then
  199. break
  200. else
  201. ifname="$(printf "%s" "${dev_status}" | jsonfilter -l1 -e '@.*.interfaces[@.config.mode="sta"].ifname')"
  202. if [ -n "${ifname}" ]
  203. then
  204. trm_ifquality="$(${trm_iwinfo} "${ifname}" info 2>/dev/null | awk -F "[ ]" '/Link Quality:/{split($NF,var0,"/");printf "%i\n",(var0[1]*100/var0[2])}')"
  205. if [ "${mode}" = "initial" ] && [ "${trm_captive}" -eq 1 ]
  206. then
  207. result="$(${trm_fetch} --timeout=$((trm_maxwait/6)) "${trm_captiveurl}" -O /dev/null 2>&1 | \
  208. awk '/^Failed to redirect|^Redirected/{printf "%s" "net cp \047"$NF"\047";exit}/^Download completed/{printf "%s" "net ok";exit}/^Failed|^Connection error/{printf "%s" "net nok";exit}')"
  209. fi
  210. if [ "${trm_ifquality}" -ge "${trm_minquality}" ] && [ "${result%/*}" != "net nok" ]
  211. then
  212. trm_ifstatus="$(ubus -S call network.interface dump 2>/dev/null | jsonfilter -l1 -e "@.interface[@.device=\"${ifname}\"].up")"
  213. if [ "${trm_ifstatus}" = "true" ]
  214. then
  215. if [ "${mode}" = "sta" ] && [ "${trm_captive}" -eq 1 ] && [ "${trm_rebind:-0}" -eq 1 ] && [ -x "/etc/init.d/dnsmasq" ]
  216. then
  217. while true
  218. do
  219. result="$(${trm_fetch} --timeout=$((trm_maxwait/6)) "${trm_captiveurl}" -O /dev/null 2>&1 | \
  220. awk '/^Failed to redirect|^Redirected/{printf "%s" "net cp \047"$NF"\047";exit}/^Download completed/{printf "%s" "net ok";exit}/^Failed|^Connection error/{printf "%s" "net nok";exit}')"
  221. cp_domain="$(printf "%s" "${result}" | awk -F "[\\'| ]" '/^net cp/{printf "%s" $4}')"
  222. if [ "${trm_netcheck}" -eq 1 ] && [ "${result%/*}" = "net nok" ]
  223. then
  224. trm_ifstatus="${status}"
  225. f_jsnup
  226. break 2
  227. fi
  228. if [ -z "${cp_domain}" ] || [ -n "$(uci_get dhcp "@dnsmasq[0]" rebind_domain | grep -Fo "${cp_domain}")" ]
  229. then
  230. break
  231. fi
  232. uci -q add_list dhcp.@dnsmasq[0].rebind_domain="${cp_domain}"
  233. f_log "info" "captive portal domain '${cp_domain}' added to rebind whitelist"
  234. done
  235. if [ -n "$(uci -q changes dhcp)" ]
  236. then
  237. uci_commit dhcp
  238. /etc/init.d/dnsmasq reload
  239. fi
  240. fi
  241. trm_connection="${result:-"-"}/${trm_ifquality}"
  242. f_jsnup
  243. break
  244. fi
  245. elif [ -n "${trm_connection}" ]
  246. then
  247. sta_essid="$(printf "%s" "${dev_status}" | jsonfilter -l1 -e '@.*.interfaces[@.config.mode="sta"].*.ssid')"
  248. sta_bssid="$(printf "%s" "${dev_status}" | jsonfilter -l1 -e '@.*.interfaces[@.config.mode="sta"].*.bssid')"
  249. if [ "${trm_ifquality}" -lt "${trm_minquality}" ]
  250. then
  251. f_log "info" "uplink '${sta_essid:-"-"}/${sta_bssid:-"-"}' is out of range (${trm_ifquality}/${trm_minquality})"
  252. elif [ "${trm_netcheck}" -eq 1 ] && [ "${result%/*}" = "net nok" ]
  253. then
  254. f_log "info" "uplink '${sta_essid:-"-"}/${sta_bssid:-"-"}' has no internet (${result})"
  255. fi
  256. unset trm_connection
  257. trm_ifstatus="${status}"
  258. f_jsnup
  259. break
  260. elif [ "${mode}" = "initial" ]
  261. then
  262. f_jsnup
  263. break
  264. fi
  265. elif [ -n "${trm_connection}" ]
  266. then
  267. unset trm_connection
  268. trm_ifstatus="${status}"
  269. f_jsnup
  270. break
  271. elif [ "${mode}" = "initial" ]
  272. then
  273. f_jsnup
  274. break
  275. fi
  276. fi
  277. fi
  278. wait=$((wait+1))
  279. sleep 1
  280. done
  281. f_log "debug" "f_check::: mode: ${mode}, name: ${ifname:-"-"}, status: ${trm_ifstatus}, connection: ${trm_connection:-"-"}, wait: ${wait}, max_wait: ${trm_maxwait}, min_quality: ${trm_minquality}, captive: ${trm_captive}, netcheck: ${trm_netcheck}"
  282. }
  283. # update runtime information
  284. #
  285. f_jsnup()
  286. {
  287. local IFS config d1 d2 d3 last_date last_station sta_iface sta_radio sta_essid sta_bssid last_status dev_status status="${trm_ifstatus}" faulty_list faulty_station="${1}"
  288. dev_status="$(ubus -S call network.wireless status 2>/dev/null)"
  289. if [ -n "${dev_status}" ]
  290. then
  291. config="$(printf "%s" "${dev_status}" | jsonfilter -l1 -e '@.*.interfaces[@.config.mode="sta"].section')"
  292. if [ -n "${config}" ]
  293. then
  294. sta_iface="$(uci_get wireless "${config}" network)"
  295. sta_radio="$(uci_get wireless "${config}" device)"
  296. sta_essid="$(uci_get wireless "${config}" ssid)"
  297. sta_bssid="$(uci_get wireless "${config}" bssid)"
  298. fi
  299. fi
  300. json_get_var last_date "last_rundate"
  301. json_get_var last_station "station_id"
  302. if [ "${status}" = "true" ]
  303. then
  304. status="connected (${trm_connection:-"-"})"
  305. json_get_var last_status "travelmate_status"
  306. if [ "${last_status}" = "running / not connected" ] || [ "${last_station}" != "${sta_radio:-"-"}/${sta_essid:-"-"}/${sta_bssid:-"-"}" ]
  307. then
  308. last_date="$(/bin/date "+%Y.%m.%d-%H:%M:%S")"
  309. fi
  310. elif [ "${status}" = "error" ]
  311. then
  312. unset trm_connection
  313. status="program error"
  314. else
  315. unset trm_connection
  316. status="running / not connected"
  317. fi
  318. if [ -z "${last_date}" ]
  319. then
  320. last_date="$(/bin/date "+%Y.%m.%d-%H:%M:%S")"
  321. fi
  322. json_get_var faulty_list "faulty_stations"
  323. if [ -n "${faulty_list}" ] && [ "${trm_listexpiry}" -gt 0 ]
  324. then
  325. d1="$(/bin/date -d "${last_date}" "+%s")"
  326. d2="$(/bin/date "+%s")"
  327. d3=$(((d2 - d1)/60))
  328. if [ "${d3}" -ge "${trm_listexpiry}" ]
  329. then
  330. faulty_list=""
  331. fi
  332. fi
  333. if [ -n "${faulty_station}" ]
  334. then
  335. if [ -z "$(printf "%s" "${faulty_list}" | grep -Fo "${faulty_station}")" ]
  336. then
  337. faulty_list="$(f_trim "${faulty_list} ${faulty_station}")"
  338. fi
  339. fi
  340. json_add_string "travelmate_status" "${status}"
  341. json_add_string "travelmate_version" "${trm_ver}"
  342. json_add_string "station_id" "${sta_radio:-"-"}/${sta_essid:-"-"}/${sta_bssid:-"-"}"
  343. json_add_string "station_interface" "${sta_iface:-"-"}"
  344. json_add_string "faulty_stations" "${faulty_list}"
  345. json_add_string "last_rundate" "${last_date}"
  346. json_add_string "system" "${trm_sysver}"
  347. json_dump > "${trm_rtfile}"
  348. f_log "debug" "f_jsnup::: config: ${config:-"-"}, status: ${status:-"-"}, sta_iface: ${sta_iface:-"-"}, sta_radio: ${sta_radio:-"-"}, sta_essid: ${sta_essid:-"-"}, sta_bssid: ${sta_bssid:-"-"}, faulty_list: ${faulty_list:-"-"}, list_expiry: ${trm_listexpiry}"
  349. }
  350. # write to syslog
  351. #
  352. f_log()
  353. {
  354. local IFS class="${1}" log_msg="${2}"
  355. if [ -n "${log_msg}" ] && { [ "${class}" != "debug" ] || [ "${trm_debug}" -eq 1 ]; }
  356. then
  357. logger -p "${class}" -t "travelmate-${trm_ver}[${$}]" "${log_msg}"
  358. if [ "${class}" = "err" ]
  359. then
  360. trm_ifstatus="error"
  361. f_jsnup
  362. logger -p "${class}" -t "travelmate-${trm_ver}[${$}]" "Please check 'https://github.com/openwrt/packages/blob/master/net/travelmate/files/README.md' (${trm_sysver})"
  363. exit 1
  364. fi
  365. fi
  366. }
  367. # main function for connection handling
  368. #
  369. f_main()
  370. {
  371. local IFS cnt dev config scan scan_list scan_essid scan_bssid scan_quality faulty_list
  372. local station_id sta sta_essid sta_bssid sta_radio sta_iface active_essid active_bssid active_radio
  373. f_check "initial"
  374. f_log "debug" "f_main ::: status: ${trm_ifstatus}, proactive: ${trm_proactive}"
  375. if [ "${trm_ifstatus}" != "true" ] || [ "${trm_proactive}" -eq 1 ]
  376. then
  377. config_load wireless
  378. config_foreach f_prep wifi-iface ${trm_proactive}
  379. if [ "${trm_ifstatus}" = "true" ] && [ -n "${trm_active_sta}" ] && [ "${trm_proactive}" -eq 1 ]
  380. then
  381. json_get_var station_id "station_id"
  382. active_radio="${station_id%%/*}"
  383. active_essid="${station_id%/*}"
  384. active_essid="${active_essid#*/}"
  385. active_bssid="${station_id##*/}"
  386. f_check "dev" "true"
  387. f_log "debug" "f_main ::: active_radio: ${active_radio}, active_essid: \"${active_essid}\", active_bssid: ${active_bssid:-"-"}"
  388. else
  389. uci_commit wireless
  390. f_check "dev"
  391. fi
  392. json_get_var faulty_list "faulty_stations"
  393. f_log "debug" "f_main ::: iwinfo: ${trm_iwinfo:-"-"}, dev_list: ${trm_devlist:-"-"}, sta_list: ${trm_stalist:0:800}, faulty_list: ${faulty_list:-"-"}"
  394. # radio loop
  395. #
  396. for dev in ${trm_devlist}
  397. do
  398. if [ -z "$(printf "%s" "${trm_stalist}" | grep -o "\\-${dev}")" ]
  399. then
  400. f_log "debug" "f_main ::: no station on '${dev}' - continue"
  401. continue
  402. fi
  403. # station loop
  404. #
  405. for sta in ${trm_stalist}
  406. do
  407. config="${sta%%-*}"
  408. sta_radio="${sta##*-}"
  409. sta_essid="$(uci_get wireless "${config}" ssid)"
  410. sta_bssid="$(uci_get wireless "${config}" bssid)"
  411. sta_iface="$(uci_get wireless "${config}" network)"
  412. json_get_var faulty_list "faulty_stations"
  413. if [ -n "$(printf "%s" "${faulty_list}" | grep -Fo "${sta_radio}/${sta_essid}/${sta_bssid}")" ]
  414. then
  415. f_log "debug" "f_main ::: faulty station '${sta_radio}/${sta_essid}/${sta_bssid:-"-"}' - continue"
  416. continue
  417. fi
  418. if [ "${dev}" = "${active_radio}" ] && [ "${sta_essid}" = "${active_essid}" ] && [ "${sta_bssid:-"-"}" = "${active_bssid}" ]
  419. then
  420. f_log "debug" "f_main ::: active station prioritized '${active_radio}/${active_essid}/${active_bssid:-"-"}' - break"
  421. break 2
  422. fi
  423. f_log "debug" "f_main ::: sta_radio: ${sta_radio}, sta_essid: \"${sta_essid}\", sta_bssid: ${sta_bssid:-"-"}"
  424. if [ -z "${scan_list}" ]
  425. then
  426. scan_list="$(f_trim "$("${trm_iwinfo}" "${dev}" scan 2>/dev/null | \
  427. awk 'BEGIN{FS="[ ]"}/Address:/{var1=$NF}/ESSID:/{var2="";for(i=12;i<=NF;i++)if(var2==""){var2=$i}else{var2=var2" "$i};gsub(/,/,".",var2)}/Quality:/{split($NF,var0,"/");printf "%i,%s,%s\n",(var0[1]*100/var0[2]),var1,var2}' | \
  428. sort -rn | awk 'BEGIN{ORS=","}{print $0}' | awk '{print substr($0,1,4096)}')")"
  429. f_log "debug" "f_main ::: scan_list: ${scan_list:0:800}"
  430. if [ -z "${scan_list}" ]
  431. then
  432. f_log "debug" "f_main ::: no scan results on '${dev}' - continue"
  433. continue 2
  434. fi
  435. fi
  436. # scan loop
  437. #
  438. IFS=","
  439. for scan in ${scan_list}
  440. do
  441. if [ -z "${scan_quality}" ]
  442. then
  443. scan_quality="${scan}"
  444. elif [ -z "${scan_bssid}" ]
  445. then
  446. scan_bssid="${scan}"
  447. elif [ -z "${scan_essid}" ]
  448. then
  449. scan_essid="${scan}"
  450. fi
  451. if [ -n "${scan_quality}" ] && [ -n "${scan_bssid}" ] && [ -n "${scan_essid}" ]
  452. then
  453. if [ "${scan_quality}" -ge "${trm_minquality}" ]
  454. then
  455. if { { [ "${scan_essid}" = "\"${sta_essid//,/.}\"" ] && { [ -z "${sta_bssid}" ] || [ "${scan_bssid}" = "${sta_bssid}" ]; } } || \
  456. { [ "${scan_bssid}" = "${sta_bssid}" ] && [ "${scan_essid}" = "unknown" ]; } } && [ "${dev}" = "${sta_radio}" ]
  457. then
  458. f_log "debug" "f_main ::: scan_quality: ${scan_quality}, scan_essid: ${scan_essid}, scan_bssid: ${scan_bssid:-"-"}"
  459. if [ "${dev}" = "${active_radio}" ]
  460. then
  461. unset trm_connection active_radio active_essid active_bssid
  462. uci_set wireless "${trm_active_sta}" disabled 1
  463. uci_commit wireless
  464. fi
  465. # retry loop
  466. #
  467. cnt=1
  468. while [ "${cnt}" -le "${trm_maxretry}" ]
  469. do
  470. uci_set wireless "${config}" disabled 0
  471. f_check "sta"
  472. if [ "${trm_ifstatus}" = "true" ]
  473. then
  474. unset IFS scan_list
  475. uci_commit wireless
  476. f_log "info" "connected to uplink '${sta_radio}/${sta_essid}/${sta_bssid:-"-"}' (${cnt}/${trm_maxretry}, ${trm_sysver})"
  477. return 0
  478. else
  479. uci -q revert wireless
  480. f_check "rev"
  481. if [ "${cnt}" -eq "${trm_maxretry}" ]
  482. then
  483. faulty_station="${sta_radio}/${sta_essid}/${sta_bssid:-"-"}"
  484. f_jsnup "${faulty_station}"
  485. f_log "info" "uplink disabled '${sta_radio}/${sta_essid}/${sta_bssid:-"-"}' (${cnt}/${trm_maxretry}, ${trm_sysver})"
  486. break 2
  487. else
  488. f_jsnup
  489. f_log "info" "can't connect to uplink '${sta_radio}/${sta_essid}/${sta_bssid:-"-"}' (${cnt}/${trm_maxretry}, ${trm_sysver})"
  490. fi
  491. fi
  492. cnt=$((cnt+1))
  493. sleep $((trm_maxwait/6))
  494. done
  495. else
  496. unset scan_quality scan_bssid scan_essid
  497. continue
  498. fi
  499. else
  500. unset scan_quality scan_bssid scan_essid
  501. continue
  502. fi
  503. fi
  504. done
  505. unset IFS scan_quality scan_bssid scan_essid
  506. done
  507. unset scan_list
  508. done
  509. fi
  510. }
  511. # source required system libraries
  512. #
  513. if [ -r "/lib/functions.sh" ] && [ -r "/usr/share/libubox/jshn.sh" ]
  514. then
  515. . "/lib/functions.sh"
  516. . "/usr/share/libubox/jshn.sh"
  517. else
  518. f_log "err" "system libraries not found"
  519. fi
  520. # control travelmate actions
  521. #
  522. f_envload
  523. while true
  524. do
  525. if [ -z "${trm_action}" ]
  526. then
  527. rc=0
  528. while true
  529. do
  530. if [ "${rc}" -eq 0 ]
  531. then
  532. f_check "initial"
  533. fi
  534. sleep ${trm_timeout} 0
  535. rc=${?}
  536. if [ "${rc}" -ne 0 ]
  537. then
  538. f_check "initial"
  539. fi
  540. if [ "${rc}" -eq 0 ] || { [ "${rc}" -ne 0 ] && [ "${trm_ifstatus}" = "false" ]; }
  541. then
  542. break
  543. fi
  544. done
  545. elif [ "${trm_action}" = "stop" ]
  546. then
  547. > "${trm_rtfile}"
  548. f_log "info" "travelmate instance stopped ::: action: ${trm_action}, pid: $(cat ${trm_pidfile} 2>/dev/null)"
  549. exit 0
  550. else
  551. f_log "info" "travelmate instance started ::: action: ${trm_action}, pid: ${$}"
  552. unset trm_action
  553. fi
  554. json_cleanup
  555. f_envload
  556. f_main
  557. done