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.

568 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.6"
  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 config="${1}" proactive="${2}"
  126. local mode="$(uci_get wireless "${config}" mode)"
  127. local network="$(uci_get wireless "${config}" network)"
  128. local radio="$(uci_get wireless "${config}" device)"
  129. local disabled="$(uci_get wireless "${config}" disabled)"
  130. local 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 [ ${trm_captive} -eq 1 ]
  206. then
  207. result="$(${trm_fetch} --timeout=$(( ${trm_maxwait} / 3 )) "${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} ] && ([ ${trm_captive} -eq 0 ] || [ ${trm_netcheck} -eq 0 ] || [ "${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 [ ${trm_captive} -eq 1 ]
  216. then
  217. cp_domain="$(printf "%s" "${result}" | awk -F "['| ]" '/^net cp/{printf "%s" $4}')"
  218. if [ -n "${cp_domain}" ] && [ ${trm_rebind:-0} -eq 1 ] && [ -x "/etc/init.d/dnsmasq" ]
  219. then
  220. while [ -n "${cp_domain}" ] && [ -z "$(uci_get dhcp "@dnsmasq[0]" rebind_domain | grep -Fo "${cp_domain}")" ]
  221. do
  222. uci -q add_list dhcp.@dnsmasq[0].rebind_domain="${cp_domain}"
  223. uci_commit dhcp
  224. /etc/init.d/dnsmasq reload
  225. f_log "info" "captive portal domain '${cp_domain}' added to rebind whitelist"
  226. result="$(${trm_fetch} --timeout=$(( ${trm_maxwait} / 3 )) "${trm_captiveurl}" -O /dev/null 2>&1 | \
  227. 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}')"
  228. cp_domain="$(printf "%s" "${result}" | awk -F "['| ]" '/^net cp/{printf "%s" $4}')"
  229. done
  230. fi
  231. fi
  232. trm_connection="${result}/${trm_ifquality}"
  233. f_jsnup
  234. break
  235. fi
  236. else
  237. if [ -n "${trm_connection}" ]
  238. then
  239. sta_essid="$(printf "%s" "${dev_status}" | jsonfilter -l1 -e '@.*.interfaces[@.config.mode="sta"].*.ssid')"
  240. sta_bssid="$(printf "%s" "${dev_status}" | jsonfilter -l1 -e '@.*.interfaces[@.config.mode="sta"].*.bssid')"
  241. if [ ${trm_ifquality} -lt ${trm_minquality} ]
  242. then
  243. f_log "info" "uplink '${sta_essid:-"-"}/${sta_bssid:-"-"}' is out of range (${trm_ifquality}/${trm_minquality})"
  244. elif [ ${trm_captive} -eq 1 ] && [ ${trm_netcheck} -eq 1 ] && [ "${result%/*}" = "net nok" ]
  245. then
  246. f_log "info" "uplink '${sta_essid:-"-"}/${sta_bssid:-"-"}' has no internet (${result})"
  247. fi
  248. unset trm_connection
  249. trm_ifstatus="${status}"
  250. f_jsnup
  251. break
  252. fi
  253. fi
  254. else
  255. if [ -n "${trm_connection}" ]
  256. then
  257. unset trm_connection
  258. trm_ifstatus="${status}"
  259. f_jsnup
  260. break
  261. fi
  262. fi
  263. fi
  264. fi
  265. wait=$(( wait + 1 ))
  266. sleep 1
  267. done
  268. f_log "debug" "f_check::: mode: ${mode}, name: ${ifname:-"-"}, status: ${trm_ifstatus}, quality: ${trm_ifquality}, result: ${result:-"-"}, connection: ${trm_connection:-"-"}, wait: ${wait}, max_wait: ${trm_maxwait}, min_quality: ${trm_minquality}, captive: ${trm_captive}, netcheck: ${trm_netcheck}"
  269. }
  270. # update runtime information
  271. #
  272. f_jsnup()
  273. {
  274. 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}"
  275. dev_status="$(ubus -S call network.wireless status 2>/dev/null)"
  276. if [ -n "${dev_status}" ]
  277. then
  278. config="$(printf "%s" "${dev_status}" | jsonfilter -l1 -e '@.*.interfaces[@.config.mode="sta"].section')"
  279. if [ -n "${config}" ]
  280. then
  281. sta_iface="$(uci_get wireless "${config}" network)"
  282. sta_radio="$(uci_get wireless "${config}" device)"
  283. sta_essid="$(uci_get wireless "${config}" ssid)"
  284. sta_bssid="$(uci_get wireless "${config}" bssid)"
  285. fi
  286. fi
  287. json_get_var last_date "last_rundate"
  288. json_get_var last_station "station_id"
  289. if [ "${status}" = "true" ]
  290. then
  291. status="connected (${trm_connection:-"-"})"
  292. json_get_var last_status "travelmate_status"
  293. if [ "${last_status}" = "running / not connected" ] || [ "${last_station}" != "${sta_radio:-"-"}/${sta_essid:-"-"}/${sta_bssid:-"-"}" ]
  294. then
  295. last_date="$(/bin/date "+%Y.%m.%d-%H:%M:%S")"
  296. fi
  297. else
  298. unset trm_connection
  299. status="running / not connected"
  300. fi
  301. if [ -z "${last_date}" ]
  302. then
  303. last_date="$(/bin/date "+%Y.%m.%d-%H:%M:%S")"
  304. fi
  305. json_get_var faulty_list "faulty_stations"
  306. if [ -n "${faulty_list}" ] && [ ${trm_listexpiry} -gt 0 ]
  307. then
  308. d1="$(/bin/date -d "${last_date}" "+%s")"
  309. d2="$(/bin/date "+%s")"
  310. d3=$(( (d2 - d1) / 60 ))
  311. if [ ${d3} -ge ${trm_listexpiry} ]
  312. then
  313. faulty_list=""
  314. fi
  315. fi
  316. if [ -n "${faulty_station}" ]
  317. then
  318. if [ -z "$(printf "%s" "${faulty_list}" | grep -Fo "${faulty_station}")" ]
  319. then
  320. faulty_list="$(f_trim "${faulty_list} ${faulty_station}")"
  321. fi
  322. fi
  323. json_add_string "travelmate_status" "${status}"
  324. json_add_string "travelmate_version" "${trm_ver}"
  325. json_add_string "station_id" "${sta_radio:-"-"}/${sta_essid:-"-"}/${sta_bssid:-"-"}"
  326. json_add_string "station_interface" "${sta_iface:-"-"}"
  327. json_add_string "faulty_stations" "${faulty_list}"
  328. json_add_string "last_rundate" "${last_date}"
  329. json_add_string "system" "${trm_sysver}"
  330. json_dump > "${trm_rtfile}"
  331. 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}"
  332. }
  333. # write to syslog
  334. #
  335. f_log()
  336. {
  337. local IFS class="${1}" log_msg="${2}"
  338. if [ -n "${log_msg}" ] && ([ "${class}" != "debug" ] || [ ${trm_debug} -eq 1 ])
  339. then
  340. logger -p "${class}" -t "travelmate-${trm_ver}[${$}]" "${log_msg}"
  341. if [ "${class}" = "err" ]
  342. then
  343. trm_ifstatus="error"
  344. f_jsnup
  345. logger -p "${class}" -t "travelmate-${trm_ver}[${$}]" "Please check 'https://github.com/openwrt/packages/blob/master/net/travelmate/files/README.md' (${trm_sysver})"
  346. exit 1
  347. fi
  348. fi
  349. }
  350. # main function for connection handling
  351. #
  352. f_main()
  353. {
  354. local IFS cnt dev config scan scan_list scan_essid scan_bssid scan_quality faulty_list
  355. local station_id sta sta_essid sta_bssid sta_radio sta_iface active_essid active_bssid active_radio
  356. f_check "initial"
  357. f_log "debug" "f_main ::: status: ${trm_ifstatus}, proactive: ${trm_proactive}"
  358. if [ "${trm_ifstatus}" != "true" ] || [ ${trm_proactive} -eq 1 ]
  359. then
  360. config_load wireless
  361. config_foreach f_prep wifi-iface ${trm_proactive}
  362. if [ "${trm_ifstatus}" = "true" ] && [ -n "${trm_active_sta}" ] && [ ${trm_proactive} -eq 1 ]
  363. then
  364. json_get_var station_id "station_id"
  365. active_radio="${station_id%%/*}"
  366. active_essid="${station_id%/*}"
  367. active_essid="${active_essid#*/}"
  368. active_bssid="${station_id##*/}"
  369. f_check "dev" "true"
  370. f_log "debug" "f_main ::: active_radio: ${active_radio}, active_essid: \"${active_essid}\", active_bssid: ${active_bssid:-"-"}"
  371. else
  372. uci_commit wireless
  373. f_check "dev"
  374. fi
  375. json_get_var faulty_list "faulty_stations"
  376. f_log "debug" "f_main ::: iwinfo: ${trm_iwinfo:-"-"}, dev_list: ${trm_devlist:-"-"}, sta_list: ${trm_stalist:0:800}, faulty_list: ${faulty_list:-"-"}"
  377. # radio loop
  378. #
  379. for dev in ${trm_devlist}
  380. do
  381. if [ -z "$(printf "%s" "${trm_stalist}" | grep -o "\-${dev}")" ]
  382. then
  383. f_log "debug" "f_main ::: no station on '${dev}' - continue"
  384. continue
  385. fi
  386. # station loop
  387. #
  388. for sta in ${trm_stalist}
  389. do
  390. config="${sta%%-*}"
  391. sta_radio="${sta##*-}"
  392. sta_essid="$(uci_get wireless "${config}" ssid)"
  393. sta_bssid="$(uci_get wireless "${config}" bssid)"
  394. sta_iface="$(uci_get wireless "${config}" network)"
  395. json_get_var faulty_list "faulty_stations"
  396. if [ -n "$(printf "%s" "${faulty_list}" | grep -Fo "${sta_radio}/${sta_essid}/${sta_bssid}")" ]
  397. then
  398. f_log "debug" "f_main ::: faulty station '${sta_radio}/${sta_essid}/${sta_bssid:-"-"}' - continue"
  399. continue
  400. fi
  401. if [ "${dev}" = "${active_radio}" ] && [ "${sta_essid}" = "${active_essid}" ] && [ "${sta_bssid:-"-"}" = "${active_bssid}" ]
  402. then
  403. f_log "debug" "f_main ::: active station prioritized '${active_radio}/${active_essid}/${active_bssid:-"-"}' - break"
  404. break 2
  405. fi
  406. f_log "debug" "f_main ::: sta_radio: ${sta_radio}, sta_essid: \"${sta_essid}\", sta_bssid: ${sta_bssid:-"-"}"
  407. if [ -z "${scan_list}" ]
  408. then
  409. scan_list="$(f_trim "$("${trm_iwinfo}" "${dev}" scan 2>/dev/null | \
  410. 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}' | \
  411. sort -rn | awk 'BEGIN{ORS=","}{print $0}' | awk '{print substr($0,1,4096)}')")"
  412. f_log "debug" "f_main ::: scan_list: ${scan_list:0:800}"
  413. if [ -z "${scan_list}" ]
  414. then
  415. f_log "debug" "f_main ::: no scan results on '${dev}' - continue"
  416. continue 2
  417. fi
  418. fi
  419. # scan loop
  420. #
  421. IFS=","
  422. for scan in ${scan_list}
  423. do
  424. if [ -z "${scan_quality}" ]
  425. then
  426. scan_quality="${scan}"
  427. elif [ -z "${scan_bssid}" ]
  428. then
  429. scan_bssid="${scan}"
  430. elif [ -z "${scan_essid}" ]
  431. then
  432. scan_essid="${scan}"
  433. fi
  434. if [ -n "${scan_quality}" ] && [ -n "${scan_bssid}" ] && [ -n "${scan_essid}" ]
  435. then
  436. if [ ${scan_quality} -ge ${trm_minquality} ]
  437. then
  438. if (([ "${scan_essid}" = "\"${sta_essid//,/.}\"" ] && ([ -z "${sta_bssid}" ] || [ "${scan_bssid}" = "${sta_bssid}" ])) || \
  439. ([ "${scan_bssid}" = "${sta_bssid}" ] && [ "${scan_essid}" = "unknown" ])) && [ "${dev}" = "${sta_radio}" ]
  440. then
  441. f_log "debug" "f_main ::: scan_quality: ${scan_quality}, scan_essid: ${scan_essid}, scan_bssid: ${scan_bssid:-"-"}"
  442. if [ "${dev}" = "${active_radio}" ]
  443. then
  444. unset trm_connection active_radio active_essid active_bssid
  445. uci_set wireless "${trm_active_sta}" disabled 1
  446. uci_commit wireless
  447. fi
  448. # retry loop
  449. #
  450. cnt=1
  451. while [ ${cnt} -le ${trm_maxretry} ]
  452. do
  453. uci_set wireless "${config}" disabled 0
  454. f_check "sta"
  455. if [ "${trm_ifstatus}" = "true" ]
  456. then
  457. unset IFS scan_list
  458. uci_commit wireless
  459. f_log "info" "connected to uplink '${sta_radio}/${sta_essid}/${sta_bssid:-"-"}' (${cnt}/${trm_maxretry}, ${trm_sysver})"
  460. return 0
  461. else
  462. uci -q revert wireless
  463. f_check "rev"
  464. if [ ${cnt} -eq ${trm_maxretry} ]
  465. then
  466. faulty_station="${sta_radio}/${sta_essid}/${sta_bssid:-"-"}"
  467. f_jsnup "${faulty_station}"
  468. f_log "info" "uplink disabled '${sta_radio}/${sta_essid}/${sta_bssid:-"-"}' (${cnt}/${trm_maxretry}, ${trm_sysver})"
  469. break 2
  470. else
  471. f_jsnup
  472. f_log "info" "can't connect to uplink '${sta_radio}/${sta_essid}/${sta_bssid:-"-"}' (${cnt}/${trm_maxretry}, ${trm_sysver})"
  473. fi
  474. fi
  475. cnt=$(( cnt + 1 ))
  476. sleep $(( ${trm_maxwait} / 6 ))
  477. done
  478. else
  479. unset scan_quality scan_bssid scan_essid
  480. continue
  481. fi
  482. else
  483. unset scan_quality scan_bssid scan_essid
  484. continue
  485. fi
  486. fi
  487. done
  488. unset IFS scan_quality scan_bssid scan_essid
  489. done
  490. unset scan_list
  491. done
  492. fi
  493. }
  494. # source required system libraries
  495. #
  496. if [ -r "/lib/functions.sh" ] && [ -r "/usr/share/libubox/jshn.sh" ]
  497. then
  498. . "/lib/functions.sh"
  499. . "/usr/share/libubox/jshn.sh"
  500. else
  501. f_log "err" "system libraries not found"
  502. fi
  503. # control travelmate actions
  504. #
  505. f_envload
  506. while true
  507. do
  508. if [ -z "${trm_action}" ]
  509. then
  510. rc=0
  511. while true
  512. do
  513. if [ ${rc} -eq 0 ]
  514. then
  515. f_check "initial"
  516. fi
  517. sleep ${trm_timeout} 0
  518. rc=${?}
  519. if [ ${rc} -ne 0 ]
  520. then
  521. f_check "initial"
  522. fi
  523. if [ ${rc} -eq 0 ] || ([ ${rc} -ne 0 ] && [ "${trm_ifstatus}" = "false" ])
  524. then
  525. break
  526. fi
  527. done
  528. elif [ "${trm_action}" = "stop" ]
  529. then
  530. > "${trm_rtfile}"
  531. f_log "info" "travelmate instance stopped ::: action: ${trm_action}, pid: $(cat ${trm_pidfile} 2>/dev/null)"
  532. exit 0
  533. else
  534. f_log "info" "travelmate instance started ::: action: ${trm_action}, pid: ${$}"
  535. unset trm_action
  536. fi
  537. json_cleanup
  538. f_envload
  539. f_main
  540. done