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.

212 lines
6.9 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. # prepare environment
  8. #
  9. LC_ALL=C
  10. PATH="/usr/sbin:/usr/bin:/sbin:/bin"
  11. trm_ver="0.3.5"
  12. trm_enabled=1
  13. trm_debug=0
  14. trm_maxwait=20
  15. trm_maxretry=3
  16. trm_radio="*"
  17. trm_iw=1
  18. f_envload()
  19. {
  20. # source required system libraries
  21. #
  22. if [ -r "/lib/functions.sh" ]
  23. then
  24. . "/lib/functions.sh"
  25. else
  26. f_log "error" "required system library not found"
  27. fi
  28. # load uci config and check 'enabled' option
  29. #
  30. option_cb()
  31. {
  32. local option="${1}"
  33. local value="${2}"
  34. eval "${option}=\"${value}\""
  35. }
  36. config_load travelmate
  37. if [ ${trm_enabled} -ne 1 ]
  38. then
  39. f_log "info " "travelmate is currently disabled, please set 'trm_enabled' to '1' to use this service"
  40. exit 0
  41. fi
  42. # check for preferred wireless tool
  43. #
  44. if [ ${trm_iw} -eq 1 ]
  45. then
  46. trm_scanner="$(which iw)"
  47. else
  48. trm_scanner="$(which iwinfo)"
  49. fi
  50. if [ -z "${trm_scanner}" ]
  51. then
  52. f_log "error" "no wireless tool for wlan scanning found, please install 'iw' or 'iwinfo'"
  53. fi
  54. }
  55. # function to bring down all STA interfaces
  56. #
  57. f_prepare()
  58. {
  59. local config="${1}"
  60. local mode="$(uci -q get wireless."${config}".mode)"
  61. local network="$(uci -q get wireless."${config}".network)"
  62. local disabled="$(uci -q get wireless."${config}".disabled)"
  63. if [ "${mode}" = "sta" ] && [ -n "${network}" ]
  64. then
  65. trm_stalist="${trm_stalist} ${config}_${network}"
  66. if [ -z "${disabled}" ] || [ "${disabled}" = "0" ]
  67. then
  68. uci -q set wireless."${config}".disabled=1
  69. f_log "debug" "config: ${config}, interface: ${network}"
  70. fi
  71. fi
  72. }
  73. f_check()
  74. {
  75. local ifname cnt=1 mode="${1}"
  76. trm_ifstatus="false"
  77. while [ ${cnt} -le ${trm_maxwait} ]
  78. do
  79. if [ "${mode}" = "ap" ]
  80. then
  81. ifname="$(ubus -S call network.wireless status | jsonfilter -l1 -e "@.${trm_radio}.interfaces[@.config.mode=\"ap\"].ifname")"
  82. if [ -n "${ifname}" ]
  83. then
  84. trm_ifstatus="$(ubus -S call network.wireless status | jsonfilter -l1 -e "@.${trm_radio}.up")"
  85. fi
  86. else
  87. ifname="$(ubus -S call network.wireless status | jsonfilter -l1 -e '@.*.interfaces[@.config.mode="sta"].ifname')"
  88. if [ -n "${ifname}" ]
  89. then
  90. trm_ifstatus="$(ubus -S call network.interface dump | jsonfilter -e "@.interface[@.device=\"${ifname}\"].up")"
  91. fi
  92. fi
  93. if [ "${mode}" = "initial" ] || [ "${trm_ifstatus}" = "true" ]
  94. then
  95. break
  96. fi
  97. cnt=$((cnt+1))
  98. sleep 1
  99. done
  100. f_log "debug" "mode: ${mode}, radio: ${trm_radio}, name: ${ifname}, status: ${trm_ifstatus}, count: ${cnt}, max-wait: ${trm_maxwait}"
  101. }
  102. # function to write to syslog
  103. #
  104. f_log()
  105. {
  106. local class="${1}"
  107. local log_msg="${2}"
  108. if [ -n "${log_msg}" ] && ([ "${class}" != "debug" ] || [ ${trm_debug} -eq 1 ])
  109. then
  110. logger -t "travelmate-[${trm_ver}] ${class}" "${log_msg}"
  111. if [ "${class}" = "error" ]
  112. then
  113. logger -t "travelmate-[${trm_ver}] ${class}" "Please check the online documentation 'https://github.com/openwrt/packages/blob/master/net/travelmate/files/README.md'"
  114. exit 255
  115. fi
  116. fi
  117. }
  118. f_main()
  119. {
  120. local ap_list ssid_list config network ssid cnt=1
  121. local sysver="$(ubus -S call system board | jsonfilter -e '@.release.description')"
  122. f_log "info " "start travelmate scanning ..."
  123. f_check "initial"
  124. if [ "${trm_ifstatus}" != "true" ]
  125. then
  126. config_load wireless
  127. config_foreach f_prepare wifi-iface
  128. if [ -n "$(uci -q changes wireless)" ]
  129. then
  130. uci -q commit wireless
  131. ubus call network reload
  132. fi
  133. f_check "ap"
  134. ap_list="$(ubus -S call network.wireless status | jsonfilter -e "@.${trm_radio}.interfaces[@.config.mode=\"ap\"].ifname" | awk '{ORS=" "; print $0}')"
  135. f_log "debug" "ap-list: ${ap_list}, sta-list: ${trm_stalist}"
  136. if [ -z "${ap_list}" ] || [ -z "${trm_stalist}" ]
  137. then
  138. f_log "error" "no usable AP/STA configuration found"
  139. fi
  140. for ap in ${ap_list}
  141. do
  142. cnt=1
  143. while [ ${cnt} -le ${trm_maxretry} ]
  144. do
  145. if [ ${trm_iw} -eq 1 ]
  146. then
  147. ssid_list="$(${trm_scanner} dev "${ap}" scan 2>/dev/null | \
  148. awk '/SSID: /{if(!seen[$0]++){printf "\"";for(i=2; i<=NF; i++)if(i==2)printf $i;else printf " "$i;printf "\" "}}')"
  149. else
  150. ssid_list="$(${trm_scanner} "${ap}" scan | \
  151. awk '/ESSID: ".*"/{ORS=" ";if (!seen[$0]++) for(i=2; i<=NF; i++) print $i}')"
  152. fi
  153. f_log "debug" "scanner: ${trm_scanner}, ap: ${ap}, ssids: ${ssid_list}"
  154. if [ -n "${ssid_list}" ]
  155. then
  156. for sta in ${trm_stalist}
  157. do
  158. config="${sta%%_*}"
  159. network="${sta##*_}"
  160. ssid="\"$(uci -q get wireless."${config}".ssid)\""
  161. if [ -n "$(printf "${ssid_list}" | grep -Fo "${ssid}")" ]
  162. then
  163. uci -q set wireless."${config}".disabled=0
  164. uci -q commit wireless
  165. ubus call network reload
  166. f_check "sta"
  167. if [ "${trm_ifstatus}" = "true" ]
  168. then
  169. f_log "info " "wwan interface connected to uplink ${ssid} (${cnt}/${trm_maxretry}, ${sysver})"
  170. sleep 5
  171. return 0
  172. else
  173. uci -q set wireless."${config}".disabled=1
  174. uci -q commit wireless
  175. ubus call network reload
  176. f_log "info " "wwan interface can't connect to uplink ${ssid} (${cnt}/${trm_maxretry}, ${sysver})"
  177. fi
  178. fi
  179. done
  180. else
  181. f_log "info " "empty uplink list (${cnt}/${trm_maxretry}, ${sysver})"
  182. fi
  183. cnt=$((cnt+1))
  184. sleep 5
  185. done
  186. done
  187. f_log "info " "no wwan uplink found (${sysver})"
  188. else
  189. f_log "info " "wwan uplink still connected (${sysver})"
  190. fi
  191. }
  192. if [ "${trm_procd}" = "true" ]
  193. then
  194. f_envload
  195. f_main
  196. fi
  197. exit 0