OpenWRT automatic channel selection

Now the channel auto mode Openwrt is automatically locked in 1, in fact this is a problem, here is the modification is added to their channel selection algorithm can automatically select the channel the startup script
  1. #!/bin/sh
  2. # Copyright (C) 2006 OpenWrt.org

  3. DEBUG=off

  4. . /lib/functions.sh

  5. usage() {
  6. cat <<EOF
  7. Usage: $0 [down|detect]
  8. enables (default), disables or detects a wifi configuration.
  9. EOF
  10. exit 1
  11. }
  12. find_net_config() {(

  13. best=$(autochannel)
  14. uci_channel=$(uci get wireless.radio0.channel)
  15. if [ "$uci_channel" = "auto" ];then
  16. channel=$best;
  17. orig=$(cat /var/run/hostapd-phy0.conf | grep "channel=")
  18. if [ -z $orig ];then
  19. $orig='channel=1'
  20. fi
  21. sed -i "s/$orig/channel=$channel/g" /var/run/hostapd-phy0.conf
  22. fi

  23. local vif="$1"
  24. local cfg
  25. local ifname
  26. config_get cfg "$vif" network
  27. [ -z "$cfg" ] && {
  28. include /lib/network
  29. scan_interfaces
  30. config_get ifname "$vif" ifname
  31. cfg="$(find_config "$ifname")"
  32. }
  33. [ -z "$cfg" ] && return 0
  34. echo "$cfg"
  35. )}
  36. bridge_interface() {(
  37. local cfg="$1"
  38. [ -z "$cfg" ] && return 0
  39. include /lib/network
  40. scan_interfaces
  41. for cfg in $cfg; do
  42. config_get iftype "$cfg" type
  43. [ "$iftype" = bridge ] && config_get "$cfg" ifname
  44. prepare_interface_bridge "$cfg"
  45. return $?
  46. done
  47. )}
  48. prepare_key_wep() {
  49. local key="$1"
  50. local hex=1
  51. echo -n "$key" | grep -qE "[^a-fA-F0-9]" && hex=0
  52. [ "${#key}" -eq 10 -a $hex -eq 1 ] || \
  53. [ "${#key}" -eq 26 -a $hex -eq 1 ] || {
  54. [ "${key:0:2}" = "s:" ] && key="${key#s:}"
  55. key="$(echo -n "$key" | hexdump -ve '1/1 "%02x" ""')"
  56. }
  57. echo "$key"
  58. }
  59. wifi_fixup_hwmode() {
  60. local device="$1"
  61. local default="$2"
  62. local hwmode hwmode_11n
  63. config_get channel "$device" channel

  64. config_get hwmode "$device" hwmode
  65. case "$hwmode" in
  66. 11bg) hwmode=bg;;
  67. 11a) hwmode=a;;
  68. 11b) hwmode=b;;
  69. 11g) hwmode=g;;
  70. 11n*)
  71. hwmode_11n="${hwmode##11n}"
  72. case "$hwmode_11n" in
  73. a|g) ;;
  74. default) hwmode_11n="$default"
  75. esac
  76. config_set "$device" hwmode_11n "$hwmode_11n"
  77. ;;
  78. *)
  79. hwmode=
  80. if [ "${channel:-0}" -gt 0 ]; then
  81. if [ "${channel:-0}" -gt 14 ]; then
  82. hwmode=a
  83. else
  84. hwmode=g
  85. fi
  86. else
  87. hwmode="$default"
  88. fi
  89. ;;
  90. esac
  91. config_set "$device" hwmode "$hwmode"
  92. }
  93. wifi_updown() {
  94. [ enable = "$1" ] && {
  95. wifi_updown disable "$2"
  96. scan_wifi
  97. }
  98. for device in ${2:-$DEVICES}; do (
  99. config_get disabled "$device" disabled
  100. [ 1 == "$disabled" ] && {
  101. echo "'$device' is disabled"
  102. set disable
  103. }
  104. config_get iftype "$device" type
  105. if eval "type ${1}_$iftype" 2>/dev/null >/dev/null; then
  106. eval "scan_$iftype '$device'"
  107. eval "${1}_$iftype '$device'" || echo "$device($iftype): ${1} failed"
  108. else
  109. echo "$device($iftype): Interface type not supported"
  110. fi
  111. ); done
  112. }
  113. wifi_detect() {
  114. for driver in ${2:-$DRIVERS}; do (
  115. if eval "type detect_$driver" 2>/dev/null >/dev/null; then
  116. eval "detect_$driver" || echo "$driver: Detect failed" >&2
  117. else
  118. echo "$driver: Hardware detection not supported" >&2
  119. fi
  120. ); done
  121. }
  122. start_net() {(
  123. local iface="$1"
  124. local config="$2"
  125. local vifmac="$3"
  126. [ -f "/var/run/$iface.pid" ] && kill "$(cat /var/run/${iface}.pid)" 2>/dev/null
  127. [ -z "$config" ] || {
  128. include /lib/network
  129. scan_interfaces
  130. for config in $config; do
  131. setup_interface "$iface" "$config" "" "$vifmac"
  132. done
  133. }
  134. )}
  135. set_wifi_up() {
  136. local cfg="$1"
  137. local ifname="$2"
  138. uci_set_state wireless "$cfg" up 1
  139. uci_set_state wireless "$cfg" ifname "$ifname"
  140. }
  141. set_wifi_down() {
  142. local cfg="$1"
  143. local vifs vif vifstr
  144. [ -f "/var/run/wifi-${cfg}.pid" ] &&
  145. kill "$(cat "/var/run/wifi-${cfg}.pid")" 2>/dev/null
  146. uci_revert_state wireless "$cfg"
  147. config_get vifs "$cfg" vifs
  148. for vif in $vifs; do
  149. uci_revert_state wireless "$vif"
  150. done
  151. }
  152. scan_wifi() {
  153. local cfgfile="$1"
  154. DEVICES=
  155. config_cb() {
  156. local type="$1"
  157. local section="$2"
  158. # section start
  159. case "$type" in
  160. wifi-device)
  161. append DEVICES "$section"
  162. config_set "$section" vifs ""
  163. config_set "$section" ht_capab ""
  164. ;;
  165. esac
  166. # section end
  167. config_get TYPE "$CONFIG_SECTION" TYPE
  168. case "$TYPE" in
  169. wifi-iface)
  170. config_get device "$CONFIG_SECTION" device
  171. config_get vifs "$device" vifs
  172. append vifs "$CONFIG_SECTION"
  173. config_set "$device" vifs "$vifs"
  174. ;;
  175. esac
  176. }
  177. config_load "${cfgfile:-wireless}"
  178. }
  179. DEVICES=
  180. DRIVERS=
  181. include /lib/wifi
  182. scan_wifi
  183. case "$1" in
  184. down) wifi_updown "disable" "$2";;
  185. detect) wifi_detect "$2";;
  186. --help|help) usage;;
  187. *) wifi_updown "enable" "$2";;
  188. esac

Started by Judith at December 14, 2016 - 2:06 PM

A praise

Posted by Pete at December 23, 2016 - 2:32 PM

Hello, I want to ask, this script you is to modify that file? ? ?

Posted by Kathie at December 29, 2016 - 2:34 PM

There is usually a driven scripts in the /lib/wifi/ directory, you can write a wlan.sh script yourself, read the /etc/config/wireless configuration file
The wireless card information, such as the driving information, and then mapped to drive the script operation function under /lib/wifi/ on it.

Posted by John at January 08, 2017 - 3:24 PM