|
| 1 | +#!/bin/sh /etc/rc.common |
| 2 | + |
| 3 | +. "${IPKG_INSTROOT}/lib/netifd/netifd-wireless.sh" |
| 4 | + |
| 5 | +STOP=10 |
| 6 | + |
| 7 | +USE_PROCD=1 |
| 8 | +PROG=/sbin/smart_manager |
| 9 | +SERVICE=smart_manager |
| 10 | + |
| 11 | +setup_dcs_conf() { |
| 12 | + local cfg="$1" |
| 13 | + local conf_path="$2" |
| 14 | + local ifname="$3" |
| 15 | + |
| 16 | + config_get_bool enable_datalog "$cfg" enable_datalog 0 |
| 17 | + config_get_bool enable_test "$cfg" enable_test 0 |
| 18 | + config_get log_level "$cfg" log_level 2 |
| 19 | + config_get algorithm "$cfg" algorithm ewma |
| 20 | + config_get rounds "$cfg" rounds 10 |
| 21 | + config_get threshold "$cfg" threshold 5 |
| 22 | + config_get scan_int "$cfg" scan_int 2 |
| 23 | + config_get round_int "$cfg" round_int 10 |
| 24 | + config_get ewma_alpha "$cfg" ewma_alpha 30 |
| 25 | + config_get root_dir "$cfg" root_dir "/var/log" |
| 26 | + config_get dtims_for_csa "$cfg" dtims_for_csa 10 |
| 27 | + |
| 28 | + [ $enable_datalog -eq 0 ] && enable_datalog=false || enable_datalog=true |
| 29 | + datalog=$(cat <<EOF |
| 30 | +datalog: { |
| 31 | + root_dir = "$root_dir" |
| 32 | + dcs: { |
| 33 | + enabled = $enable_datalog |
| 34 | + } |
| 35 | +} |
| 36 | +EOF |
| 37 | +) |
| 38 | + |
| 39 | + algorithm_sec= |
| 40 | + case "$algorithm" in |
| 41 | + "sample_and_hold") |
| 42 | + algorithm_sec=$(cat <<EOF |
| 43 | + algo_type = "sample_and_hold" |
| 44 | + sample_and_hold: { |
| 45 | + rounds_for_eval = $rounds |
| 46 | + threshold_percentage = $threshold |
| 47 | + sec_per_scan = $scan_int |
| 48 | + sec_per_round = $round_int |
| 49 | + } |
| 50 | +EOF |
| 51 | +) |
| 52 | + ;; |
| 53 | + |
| 54 | + *) |
| 55 | + algorithm_sec=$(cat <<EOF |
| 56 | + algo_type = "ewma" |
| 57 | + ewma: { |
| 58 | + ewma_alpha = $ewma_alpha |
| 59 | + rounds_for_csa = $rounds |
| 60 | + threshold_percentage = $threshold |
| 61 | + sec_per_scan = $scan_int |
| 62 | + sec_per_round = $round_int |
| 63 | + } |
| 64 | +EOF |
| 65 | +) |
| 66 | + ;; |
| 67 | + esac |
| 68 | + |
| 69 | + |
| 70 | + cat > "$conf_path" <<EOF |
| 71 | +module_dirs: ["/usr/share/smart_manager/"] |
| 72 | +modules: ["dcs"] |
| 73 | +interface_name = "$ifname" |
| 74 | +logging: { |
| 75 | + level = $log_level |
| 76 | +} |
| 77 | +backends: { |
| 78 | + hostapd: { |
| 79 | + control_path: "/var/run/hostapd_s1g" |
| 80 | + } |
| 81 | +} |
| 82 | +$datalog |
| 83 | +dcs: { |
| 84 | +$algorithm_sec |
| 85 | + trigger_csa = True |
| 86 | + dtims_for_csa = $dtims_for_csa |
| 87 | +} |
| 88 | +EOF |
| 89 | +} |
| 90 | + |
| 91 | +get_first_ifname() { |
| 92 | + [ -n "$ifname" ] && return |
| 93 | + json_get_vars ifname |
| 94 | +} |
| 95 | + |
| 96 | +get_first_ap_ifname() { |
| 97 | + # This relies on running after netifd morse.sh has set up the AP. |
| 98 | + # Hostapd does not need to have started. |
| 99 | + # At boot, this will fail due to the above, but netifd morse.sh will restart |
| 100 | + # us after setting up the AP. |
| 101 | + json_load "$(wifi status "$radio")" |
| 102 | + json_select "$radio" |
| 103 | + for_each_interface "ap" get_first_ifname |
| 104 | + [ -n "$ifname" ] && return 0; |
| 105 | +} |
| 106 | + |
| 107 | +start_dcs() { |
| 108 | + local cfg="$1" |
| 109 | + # trim radio0_dcs to radio0 |
| 110 | + local radio="${cfg%_dcs}" |
| 111 | + |
| 112 | + eval "HAS_DCS_CONFIG_$radio=1" |
| 113 | + |
| 114 | + config_get_bool enabled "$cfg" enabled 0 |
| 115 | + [ $enabled -ne 1 ] && return |
| 116 | + |
| 117 | + if [ "$(uci get "wireless.${radio}.type")" != "morse" ]; then |
| 118 | + logger -t $SERVICE -p daemon.crit "wireless.${cfg} not a morse device!" |
| 119 | + return 1 |
| 120 | + fi |
| 121 | + |
| 122 | + radiojs=$(wifi status $radio 2>/dev/null) |
| 123 | + if [ $? -ne 0 ]; then |
| 124 | + logger -t $SERVICE -p daemon.crit "Selected radio $radio does not exist!" |
| 125 | + return 1 |
| 126 | + fi |
| 127 | + |
| 128 | + json_load "$radiojs" |
| 129 | + json_select $radio |
| 130 | + json_get_vars disabled |
| 131 | + set_default disabled 1 |
| 132 | + if [ "$disabled" -ne 0 ]; then |
| 133 | + logger -t $SERVICE -p daemon.crit "$radio disabled" |
| 134 | + return 1 |
| 135 | + fi |
| 136 | + |
| 137 | + ifname= |
| 138 | + if ! get_first_ap_ifname "$radio"; then |
| 139 | + logger -t $SERVICE -p daemon.crit "Couldn't get ifname for ap on $radio" |
| 140 | + return 1 |
| 141 | + fi |
| 142 | + |
| 143 | + dcs_conf="/var/run/smart_manager.${radio}.conf" |
| 144 | + |
| 145 | + setup_dcs_conf $cfg $dcs_conf $ifname |
| 146 | + |
| 147 | + procd_open_instance $cfg.$ifname |
| 148 | + procd_set_param command $PROG $dcs_conf |
| 149 | + procd_set_param file $dcs_conf |
| 150 | + procd_set_param stdout 1 |
| 151 | + procd_set_param stderr 1 |
| 152 | + procd_set_param respawn |
| 153 | + procd_close_instance |
| 154 | +} |
| 155 | + |
| 156 | +start_default_dcs() { |
| 157 | + local wifi_device="$1" |
| 158 | + local dcs_section="${wifi_device}_dcs" |
| 159 | + |
| 160 | + # Abort if there's already a config section (i.e. we did it earlier). |
| 161 | + # This val was saved above. |
| 162 | + if [ "$(eval echo \$HAS_DCS_CONFIG_${wifi_device})" = 1 ]; then |
| 163 | + return |
| 164 | + fi |
| 165 | + |
| 166 | + if [ "$(config_get "$wifi_device" type)" != morse ]; then |
| 167 | + return |
| 168 | + fi |
| 169 | + |
| 170 | + case "$(config_get "$wifi_device" channel)" in |
| 171 | + ''|0|auto) |
| 172 | + config_set "$dcs_section" enabled 1 |
| 173 | + start_dcs "$dcs_section" |
| 174 | + ;; |
| 175 | + esac |
| 176 | +} |
| 177 | + |
| 178 | +start_service() { |
| 179 | + config_load smart_manager |
| 180 | + config_foreach start_dcs dcs |
| 181 | + |
| 182 | + # Default to loading smart_manager if no config |
| 183 | + # and channel is set to auto (i.e. ACS is used). |
| 184 | + config_load wireless |
| 185 | + config_foreach start_default_dcs wifi-device |
| 186 | +} |
| 187 | + |
| 188 | +stop_service() { |
| 189 | + rm -f /var/run/smart_manager* |
| 190 | +} |
| 191 | + |
| 192 | +service_triggers() { |
| 193 | + procd_add_reload_trigger smart_manager |
| 194 | +} |
0 commit comments