wifi 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. #!/bin/sh
  2. # Copyright (C) 2006 OpenWrt.org
  3. . /etc/functions.sh
  4. find_net_config() {(
  5. local vif="$1"
  6. local cfg
  7. local ifname
  8. config_get cfg "$vif" network
  9. [ -z "$cfg" ] && {
  10. include /lib/network
  11. scan_interfaces
  12. config_get ifname "$vif" ifname
  13. cfg="$(find_config "$ifname")"
  14. }
  15. [ -z "$cfg" ] && return 0
  16. echo "$cfg"
  17. )}
  18. bridge_interface() {(
  19. local cfg="$1"
  20. [ -z "$cfg" ] && return 0
  21. include /lib/network
  22. scan_interfaces
  23. config_get iftype "$cfg" type
  24. [ "$iftype" = bridge ] && config_get "$cfg" ifname
  25. )}
  26. wifi_fixup_hwmode() {
  27. local device="$1"
  28. local default="$2"
  29. local hwmode hwmode_11n
  30. config_get channel "$device" channel
  31. config_get hwmode "$device" hwmode
  32. case "$hwmode" in
  33. 11bg) hwmode=bg;;
  34. 11a) hwmode=a;;
  35. 11b) hwmode=b;;
  36. 11g) hwmode=g;;
  37. 11n*)
  38. hwmode_11n="${hwmode##11n}"
  39. case "$hwmode" in
  40. a|g) ;;
  41. default) hwmode_11n="$default"
  42. esac
  43. config_set "$device" hwmode_11n "$hwmode_11n"
  44. ;;
  45. *)
  46. hwmode=
  47. if [ "${channel:-0}" -gt 0 ]; then
  48. if [ "${channel:-0}" -gt 14 ]; then
  49. hwmode=a
  50. else
  51. hwmode=g
  52. fi
  53. else
  54. hwmode="$default"
  55. fi
  56. ;;
  57. esac
  58. config_set "$device" hwmode "$hwmode"
  59. }
  60. wifi_updown() {
  61. [ enable = "$1" ] && wifi_updown disable "$2"
  62. for device in ${2:-$DEVICES}; do (
  63. config_get disabled "$device" disabled
  64. [ 1 == "$disabled" ] && {
  65. echo "'$device' is disabled"
  66. set disable
  67. }
  68. config_get iftype "$device" type
  69. if eval "type ${1}_$iftype" 2>/dev/null >/dev/null; then
  70. eval "scan_$iftype '$device'"
  71. eval "${1}_$iftype '$device'" || echo "$device($iftype): ${1} failed"
  72. else
  73. echo "$device($iftype): Interface type not supported"
  74. fi
  75. ); done
  76. }
  77. wifi_detect() {
  78. for driver in ${2:-$DRIVERS}; do (
  79. if eval "type detect_$driver" 2>/dev/null >/dev/null; then
  80. eval "detect_$driver" || echo "$driver: Detect failed" >&2
  81. else
  82. echo "$driver: Hardware detection not supported" >&2
  83. fi
  84. ); done
  85. }
  86. start_net() {(
  87. local iface="$1"
  88. local config="$2"
  89. local vifmac="$3"
  90. [ -f "/var/run/$iface.pid" ] && kill "$(cat /var/run/${iface}.pid)" 2>/dev/null
  91. include /lib/network
  92. scan_interfaces
  93. setup_interface "$iface" "$config" "" "$vifmac"
  94. )}
  95. set_wifi_up() {
  96. local cfg="$1"
  97. local ifname="$2"
  98. uci_set_state wireless "$cfg" up 1
  99. uci_set_state wireless "$cfg" ifname "$ifname"
  100. }
  101. set_wifi_down() {
  102. local cfg="$1"
  103. local vifs vif vifstr
  104. [ -f "/var/run/wifi-${cfg}.pid" ] &&
  105. kill "$(cat "/var/run/wifi-${cfg}.pid")"
  106. uci_revert_state wireless "$cfg"
  107. config_get vifs "$cfg" vifs
  108. for vif in $vifs; do
  109. uci_revert_state wireless "$vif"
  110. done
  111. }
  112. scan_wifi() {
  113. local cfgfile="$1"
  114. config_cb() {
  115. config_get TYPE "$CONFIG_SECTION" TYPE
  116. case "$TYPE" in
  117. wifi-device)
  118. append DEVICES "$CONFIG_SECTION"
  119. ;;
  120. wifi-iface)
  121. config_get device "$CONFIG_SECTION" device
  122. config_get vifs "$device" vifs
  123. append vifs "$CONFIG_SECTION"
  124. config_set "$device" vifs "$vifs"
  125. ;;
  126. esac
  127. }
  128. config_load "${cfgfile:-wireless}"
  129. }
  130. DEVICES=
  131. DRIVERS=
  132. include /lib/wifi
  133. scan_wifi
  134. case "$1" in
  135. down) wifi_updown "disable" "$2";;
  136. detect) wifi_detect "$2";;
  137. *) wifi_updown "enable" "$2";;
  138. esac