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.

294 lines
9.4 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="0.9.3"
  12. trm_sysver="$(ubus -S call system board | jsonfilter -e '@.release.description')"
  13. trm_enabled=0
  14. trm_debug=0
  15. trm_automatic=1
  16. trm_maxretry=3
  17. trm_maxwait=30
  18. trm_timeout=60
  19. trm_iwinfo="$(command -v iwinfo)"
  20. trm_radio=""
  21. trm_rtfile="/tmp/trm_runtime.json"
  22. # f_envload: load travelmate environment
  23. #
  24. f_envload()
  25. {
  26. # initialize lists
  27. #
  28. trm_devlist=""
  29. trm_stalist=""
  30. trm_radiolist=""
  31. # load uci config and check 'enabled' option
  32. #
  33. option_cb()
  34. {
  35. local option="${1}"
  36. local value="${2}"
  37. eval "${option}=\"${value}\""
  38. }
  39. config_load travelmate
  40. if [ ${trm_enabled} -ne 1 ]
  41. then
  42. f_log "info " "travelmate is currently disabled, please set 'trm_enabled' to '1' to use this service"
  43. exit 0
  44. fi
  45. }
  46. # f_prepare: gather radio information & bring down all STA interfaces
  47. #
  48. f_prepare()
  49. {
  50. local config="${1}"
  51. local mode="$(uci -q get wireless."${config}".mode)"
  52. local radio="$(uci -q get wireless."${config}".device)"
  53. local disabled="$(uci -q get wireless."${config}".disabled)"
  54. if ([ -z "${trm_radio}" ] || [ "${trm_radio}" = "${radio}" ]) && \
  55. [ -z "$(printf "%s" "${trm_radiolist}" | grep -Fo " ${radio}")" ]
  56. then
  57. trm_radiolist="${trm_radiolist} ${radio}"
  58. fi
  59. if [ "${mode}" = "sta" ]
  60. then
  61. trm_stalist="${trm_stalist} ${config}_${radio}"
  62. if [ -z "${disabled}" ] || [ "${disabled}" = "0" ]
  63. then
  64. uci -q set wireless."${config}".disabled=1
  65. fi
  66. fi
  67. f_log "debug" "prepare: ${mode}, radio: ${radio}, config: ${config}, disabled: ${disabled}"
  68. }
  69. # f_check: check interface status
  70. #
  71. f_check()
  72. {
  73. local ifname radio status cnt=1 mode="${1}"
  74. trm_ifstatus="false"
  75. ubus call network reload
  76. while [ ${cnt} -le ${trm_maxwait} ]
  77. do
  78. status="$(ubus -S call network.wireless status 2>/dev/null)"
  79. if [ -n "${status}" ]
  80. then
  81. if [ "${mode}" = "dev" ]
  82. then
  83. for radio in ${trm_radiolist}
  84. do
  85. trm_ifstatus="$(printf "%s" "${status}" | jsonfilter -l1 -e "@.${radio}.up")"
  86. if [ "${trm_ifstatus}" = "true" ]
  87. then
  88. trm_devlist="${trm_devlist} ${radio}"
  89. else
  90. trm_devlist=""
  91. break
  92. fi
  93. done
  94. ifname="${trm_devlist}"
  95. else
  96. ifname="$(printf "%s" "${status}" | jsonfilter -l1 -e '@.*.interfaces[@.config.mode="sta"].ifname')"
  97. if [ -n "${ifname}" ]
  98. then
  99. trm_ifstatus="$(ubus -S call network.interface dump 2>/dev/null | jsonfilter -l1 -e "@.interface[@.device=\"${ifname}\"].up")"
  100. fi
  101. fi
  102. if [ "${mode}" = "initial" ] || [ "${trm_ifstatus}" = "true" ]
  103. then
  104. break
  105. fi
  106. fi
  107. cnt=$((cnt+1))
  108. sleep 1
  109. done
  110. f_log "debug" "check: ${mode}, name: ${ifname}, status: ${trm_ifstatus}, count: ${cnt}, max-wait: ${trm_maxwait}, automatic: ${trm_automatic}"
  111. }
  112. # f_jsnupdate: update runtime information
  113. #
  114. f_jsnupdate()
  115. {
  116. local iface="${1}" radio="${2}" ssid="${3}"
  117. json_init
  118. json_add_object "data"
  119. json_add_string "travelmate_version" "${trm_ver}"
  120. json_add_string "station_connection" "${trm_ifstatus}"
  121. json_add_string "station_ssid" "${ssid}"
  122. json_add_string "station_interface" "${iface}"
  123. json_add_string "station_radio" "${radio}"
  124. json_add_string "last_rundate" "$(/bin/date "+%d.%m.%Y %H:%M:%S")"
  125. json_add_string "system" "${trm_sysver}"
  126. json_close_object
  127. json_dump > "${trm_rtfile}"
  128. }
  129. # f_status: output runtime information
  130. #
  131. f_status()
  132. {
  133. local key keylist value
  134. if [ -s "${trm_rtfile}" ]
  135. then
  136. printf "%s\n" "::: travelmate runtime information"
  137. json_load "$(cat "${trm_rtfile}" 2>/dev/null)"
  138. json_select data
  139. json_get_keys keylist
  140. for key in ${keylist}
  141. do
  142. json_get_var value "${key}"
  143. printf " %-18s : %s\n" "${key}" "${value}"
  144. done
  145. fi
  146. }
  147. # f_log: write to syslog, exit on error
  148. #
  149. f_log()
  150. {
  151. local class="${1}"
  152. local log_msg="${2}"
  153. if [ -n "${log_msg}" ] && ([ "${class}" != "debug" ] || [ ${trm_debug} -eq 1 ])
  154. then
  155. logger -t "travelmate-[${trm_ver}] ${class}" "${log_msg}"
  156. if [ "${class}" = "error" ]
  157. then
  158. logger -t "travelmate-[${trm_ver}] ${class}" "Please check 'https://github.com/openwrt/packages/blob/master/net/travelmate/files/README.md' (${trm_sysver})"
  159. exit 255
  160. fi
  161. fi
  162. }
  163. # f_main: main function for connection handling
  164. #
  165. f_main()
  166. {
  167. local dev config ssid_list sta_ssid sta_radio sta_iface cnt=1
  168. f_check "initial"
  169. if [ "${trm_ifstatus}" != "true" ]
  170. then
  171. > "${trm_rtfile}"
  172. config_load wireless
  173. config_foreach f_prepare wifi-iface
  174. if [ -n "$(uci -q changes wireless)" ]
  175. then
  176. uci -q commit wireless
  177. fi
  178. f_check "dev"
  179. f_log "debug" "main: ${trm_devlist}, sta-list: ${trm_stalist}"
  180. for dev in ${trm_devlist}
  181. do
  182. cnt=1
  183. if [ -z "$(printf "%s" "${trm_stalist}" | grep -Fo "_${dev}")" ]
  184. then
  185. continue
  186. fi
  187. while [ ${trm_maxretry} -eq 0 ] || [ ${cnt} -le ${trm_maxretry} ]
  188. do
  189. ssid_list="$(${trm_iwinfo} "${dev}" scan | awk '/ESSID: "/{ORS=" ";if (!seen[$0]++) for(i=2; i<=NF; i++) print $i}')"
  190. f_log "debug" "main: ${trm_iwinfo}, dev: ${dev}, ssids: ${ssid_list}"
  191. if [ -n "${ssid_list}" ]
  192. then
  193. for sta in ${trm_stalist}
  194. do
  195. config="${sta%%_*}"
  196. sta_radio="${sta##*_}"
  197. sta_ssid="$(uci -q get wireless."${config}".ssid)"
  198. sta_iface="$(uci -q get wireless."${config}".network)"
  199. if [ -n "$(printf "%s" "${ssid_list}" | grep -Fo "\"${sta_ssid}\"")" ] && [ "${dev}" = "${sta_radio}" ]
  200. then
  201. uci -q set wireless."${config}".disabled=0
  202. f_check "sta"
  203. if [ "${trm_ifstatus}" = "true" ]
  204. then
  205. uci -q commit wireless
  206. f_log "info " "interface '${sta_iface}' on '${sta_radio}' connected to uplink '${sta_ssid}' (${trm_sysver})"
  207. f_jsnupdate "${sta_iface}" "${sta_radio}" "${sta_ssid}"
  208. return 0
  209. elif [ ${trm_maxretry} -ne 0 ] && [ ${cnt} -eq ${trm_maxretry} ]
  210. then
  211. uci -q set wireless."${config}".disabled=1
  212. uci -q set wireless."${config}".ssid="${sta_ssid}_err"
  213. uci -q commit wireless
  214. f_check "dev"
  215. f_log "info " "can't connect to uplink '${sta_ssid}' (${cnt}/${trm_maxretry}), uplink disabled (${trm_sysver})"
  216. else
  217. if [ ${trm_maxretry} -eq 0 ]
  218. then
  219. cnt=0
  220. fi
  221. uci -q revert wireless
  222. f_check "dev"
  223. f_log "info " "can't connect to uplink '${sta_ssid}' (${cnt}/${trm_maxretry}) (${trm_sysver})"
  224. fi
  225. f_jsnupdate "${sta_iface}" "${sta_radio}" "${sta_ssid}"
  226. fi
  227. done
  228. fi
  229. cnt=$((cnt+1))
  230. sleep 5
  231. done
  232. done
  233. if [ ! -s "${trm_rtfile}" ]
  234. then
  235. trm_ifstatus="false"
  236. f_jsnupdate "n/a" "n/a" "n/a"
  237. fi
  238. else
  239. if [ ! -s "${trm_rtfile}" ]
  240. then
  241. config="$(ubus -S call network.wireless status | jsonfilter -l1 -e '@.*.interfaces[@.config.mode="sta"].section')"
  242. sta_radio="$(uci -q get wireless."${config}".device)"
  243. sta_ssid="$(uci -q get wireless."${config}".ssid)"
  244. sta_iface="$(uci -q get wireless."${config}".network)"
  245. f_jsnupdate "${sta_iface}" "${sta_radio}" "${sta_ssid}"
  246. fi
  247. fi
  248. }
  249. # source required system libraries
  250. #
  251. if [ -r "/lib/functions.sh" ] && [ -r "/usr/share/libubox/jshn.sh" ]
  252. then
  253. . "/lib/functions.sh"
  254. . "/usr/share/libubox/jshn.sh"
  255. else
  256. f_log "error" "system libraries not found"
  257. fi
  258. # handle different travelmate actions
  259. #
  260. f_envload
  261. case "${1}" in
  262. status)
  263. f_status
  264. ;;
  265. *)
  266. f_main
  267. while [ ${trm_automatic} -eq 1 ]
  268. do
  269. sleep ${trm_timeout}
  270. f_envload
  271. f_main
  272. done
  273. ;;
  274. esac
  275. exit 0