b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame^] | 1 | #!/bin/sh |
| 2 | # Copyright (C) 2006 OpenWrt.org |
| 3 | |
| 4 | . /lib/functions.sh |
| 5 | . /usr/share/libubox/jshn.sh |
| 6 | |
| 7 | usage() { |
| 8 | cat <<EOF |
| 9 | Usage: $0 [config|up|down|reconf|reload|status|isup] |
| 10 | enables (default), disables or configures devices not yet configured. |
| 11 | EOF |
| 12 | exit 1 |
| 13 | } |
| 14 | |
| 15 | ubus_wifi_cmd() { |
| 16 | local cmd="$1" |
| 17 | local dev="$2" |
| 18 | |
| 19 | json_init |
| 20 | [ -n "$dev" ] && json_add_string device "$dev" |
| 21 | ubus call network.wireless "$cmd" "$(json_dump)" |
| 22 | } |
| 23 | |
| 24 | wifi_isup() { |
| 25 | local dev="$1" |
| 26 | |
| 27 | json_load "$(ubus_wifi_cmd "status" "$dev")" |
| 28 | json_get_keys devices |
| 29 | |
| 30 | for device in $devices; do |
| 31 | json_select "$device" |
| 32 | json_get_var up up |
| 33 | [ $up -eq 0 ] && return 1 |
| 34 | json_select .. |
| 35 | done |
| 36 | |
| 37 | return 0 |
| 38 | } |
| 39 | |
| 40 | wifi_updown() { |
| 41 | cmd=down |
| 42 | [ enable = "$1" ] && { |
| 43 | ubus_wifi_cmd "$cmd" "$2" |
| 44 | ubus call network reload |
| 45 | cmd=up |
| 46 | } |
| 47 | [ reconf = "$1" ] && { |
| 48 | ubus call network reload |
| 49 | cmd=reconf |
| 50 | } |
| 51 | ubus_wifi_cmd "$cmd" "$2" |
| 52 | } |
| 53 | |
| 54 | wifi_reload() { |
| 55 | ubus call network reload |
| 56 | } |
| 57 | |
| 58 | wifi_detect_notice() { |
| 59 | >&2 echo "WARNING: Wifi detect is deprecated. Use wifi config instead" |
| 60 | >&2 echo "For more information, see commit 5f8f8a366136a07df661e31decce2458357c167a" |
| 61 | exit 1 |
| 62 | } |
| 63 | |
| 64 | wifi_config() { |
| 65 | [ -e /tmp/.config_pending ] && return |
| 66 | ucode /usr/share/hostap/wifi-detect.uc |
| 67 | [ ! -f /etc/config/wireless ] && touch /etc/config/wireless |
| 68 | ucode /lib/wifi/mac80211.uc | uci -q batch |
| 69 | |
| 70 | for driver in $DRIVERS; do ( |
| 71 | if eval "type detect_$driver" 2>/dev/null >/dev/null; then |
| 72 | eval "detect_$driver" || echo "$driver: Detect failed" >&2 |
| 73 | else |
| 74 | echo "$driver: Hardware detection not supported" >&2 |
| 75 | fi |
| 76 | ); done |
| 77 | } |
| 78 | |
| 79 | DEVICES= |
| 80 | DRIVERS= |
| 81 | include /lib/wifi |
| 82 | |
| 83 | case "$1" in |
| 84 | down) wifi_updown "disable" "$2";; |
| 85 | detect) wifi_detect_notice ;; |
| 86 | config) wifi_config ;; |
| 87 | status) ubus_wifi_cmd "status" "$2";; |
| 88 | isup) wifi_isup "$2"; exit $?;; |
| 89 | reload) wifi_reload "$2";; |
| 90 | --help|help) usage;; |
| 91 | reconf) wifi_updown "reconf" "$2";; |
| 92 | ''|up) wifi_updown "enable" "$2";; |
| 93 | *) usage; exit 1;; |
| 94 | esac |