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.

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