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.

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