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.

186 lines
5.5 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.0"
  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=0 mode="${1}"
  75. trm_ifstatus="false"
  76. while [ ${cnt} -lt ${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=0
  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} -lt ${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.interface."${network}" up
  154. ubus call network reload
  155. f_log "info " "main ::: wwan interface connected to uplink ${ssid}"
  156. return 0
  157. fi
  158. done
  159. fi
  160. cnt=$((cnt+1))
  161. sleep 5
  162. done
  163. done
  164. f_log "info " "main ::: no wwan uplink found"
  165. fi
  166. }
  167. if [ "${trm_procd}" = "true" ]
  168. then
  169. f_envload
  170. f_main
  171. fi
  172. exit 0