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.

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