1 /* 2 * Copyright (c) 2013 Adrian Chadd <adrian@FreeBSD.org> 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 16 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 23 * SUCH DAMAGE. 24 * 25 * $FreeBSD$ 26 */ 27 28 #include "diag.h" 29 30 #include "ah.h" 31 #include "ah_internal.h" 32 33 #include <getopt.h> 34 #include <errno.h> 35 #include <err.h> 36 #include <stdlib.h> 37 #include <string.h> 38 #include <ctype.h> 39 #include <unistd.h> 40 41 struct spectralhandler { 42 struct ath_diag atd; 43 int s; 44 struct ifreq ifr; 45 int ah_devid; 46 }; 47 48 int 49 spectral_opendev(struct spectralhandler *spectral, const char *devid) 50 { 51 HAL_REVS revs; 52 53 spectral->s = socket(AF_INET, SOCK_DGRAM, 0); 54 if (spectral->s < 0) { 55 warn("socket"); 56 return 0; 57 } 58 59 strncpy(spectral->atd.ad_name, devid, sizeof (spectral->atd.ad_name)); 60 61 /* Get the hardware revision, just to verify things are working */ 62 spectral->atd.ad_id = HAL_DIAG_REVS; 63 spectral->atd.ad_out_data = (caddr_t) &revs; 64 spectral->atd.ad_out_size = sizeof(revs); 65 if (ioctl(spectral->s, SIOCGATHDIAG, &spectral->atd) < 0) { 66 warn("%s", spectral->atd.ad_name); 67 return 0; 68 } 69 spectral->ah_devid = revs.ah_devid; 70 return 1; 71 } 72 73 void 74 spectral_closedev(struct spectralhandler *spectral) 75 { 76 close(spectral->s); 77 spectral->s = -1; 78 } 79 80 void 81 spectralset(struct spectralhandler *spectral, int op, u_int32_t param) 82 { 83 HAL_SPECTRAL_PARAM pe; 84 85 pe.ss_fft_period = HAL_SPECTRAL_PARAM_NOVAL; 86 pe.ss_period = HAL_SPECTRAL_PARAM_NOVAL; 87 pe.ss_count = HAL_SPECTRAL_PARAM_NOVAL; 88 pe.ss_short_report = HAL_SPECTRAL_PARAM_NOVAL; 89 pe.ss_spectral_pri = HAL_SPECTRAL_PARAM_NOVAL; 90 pe.ss_fft_period = HAL_SPECTRAL_PARAM_NOVAL; 91 pe.ss_enabled = HAL_SPECTRAL_PARAM_NOVAL; 92 pe.ss_active = HAL_SPECTRAL_PARAM_NOVAL; 93 94 switch (op) { 95 case SPECTRAL_PARAM_FFT_PERIOD: 96 pe.ss_fft_period = param; 97 break; 98 case SPECTRAL_PARAM_SS_PERIOD: 99 pe.ss_period = param; 100 break; 101 case SPECTRAL_PARAM_SS_COUNT: 102 pe.ss_count = param; 103 break; 104 case SPECTRAL_PARAM_SS_SHORT_RPT: 105 pe.ss_short_report = param; 106 break; 107 case SPECTRAL_PARAM_SS_SPECTRAL_PRI: 108 pe.ss_spectral_pri = param; 109 break; 110 } 111 112 spectral->atd.ad_id = SPECTRAL_CONTROL_SET_PARAMS | ATH_DIAG_IN; 113 spectral->atd.ad_out_data = NULL; 114 spectral->atd.ad_out_size = 0; 115 spectral->atd.ad_in_data = (caddr_t) &pe; 116 spectral->atd.ad_in_size = sizeof(HAL_SPECTRAL_PARAM); 117 if (ioctl(spectral->s, SIOCGATHSPECTRAL, &spectral->atd) < 0) 118 err(1, "%s", spectral->atd.ad_name); 119 } 120 121 static void 122 spectral_get(struct spectralhandler *spectral) 123 { 124 HAL_SPECTRAL_PARAM pe; 125 126 spectral->atd.ad_id = SPECTRAL_CONTROL_GET_PARAMS | ATH_DIAG_DYN; 127 memset(&pe, 0, sizeof(pe)); 128 129 spectral->atd.ad_in_data = NULL; 130 spectral->atd.ad_in_size = 0; 131 spectral->atd.ad_out_data = (caddr_t) &pe; 132 spectral->atd.ad_out_size = sizeof(pe); 133 134 if (ioctl(spectral->s, SIOCGATHSPECTRAL, &spectral->atd) < 0) 135 err(1, "%s", spectral->atd.ad_name); 136 137 printf("Spectral parameters (raw):\n"); 138 printf(" ss_enabled: %d\n", pe.ss_enabled); 139 printf(" ss_active: %d\n", pe.ss_active); 140 printf(" ss_count: %d\n", pe.ss_count); 141 printf(" ss_fft_period: %d\n", pe.ss_fft_period); 142 printf(" ss_period: %d\n", pe.ss_period); 143 printf(" ss_short_report: %d\n", pe.ss_short_report); 144 printf(" ss_spectral_pri: %d\n", pe.ss_spectral_pri); 145 printf(" radar_bin_thresh_sel: %d\n", pe.radar_bin_thresh_sel); 146 } 147 148 static void 149 spectral_start(struct spectralhandler *spectral) 150 { 151 HAL_SPECTRAL_PARAM pe; 152 153 spectral->atd.ad_id = SPECTRAL_CONTROL_START | ATH_DIAG_DYN; 154 memset(&pe, 0, sizeof(pe)); 155 156 /* 157 * XXX don't need these, but need to eliminate the ATH_DIAG_DYN flag 158 * and debug 159 */ 160 spectral->atd.ad_in_data = NULL; 161 spectral->atd.ad_in_size = 0; 162 spectral->atd.ad_out_data = (caddr_t) &pe; 163 spectral->atd.ad_out_size = sizeof(pe); 164 165 if (ioctl(spectral->s, SIOCGATHSPECTRAL, &spectral->atd) < 0) 166 err(1, "%s", spectral->atd.ad_name); 167 } 168 169 static void 170 spectral_stop(struct spectralhandler *spectral) 171 { 172 HAL_SPECTRAL_PARAM pe; 173 174 spectral->atd.ad_id = SPECTRAL_CONTROL_STOP | ATH_DIAG_DYN; 175 memset(&pe, 0, sizeof(pe)); 176 177 /* 178 * XXX don't need these, but need to eliminate the ATH_DIAG_DYN flag 179 * and debug 180 */ 181 spectral->atd.ad_in_data = NULL; 182 spectral->atd.ad_in_size = 0; 183 spectral->atd.ad_out_data = (caddr_t) &pe; 184 spectral->atd.ad_out_size = sizeof(pe); 185 186 if (ioctl(spectral->s, SIOCGATHSPECTRAL, &spectral->atd) < 0) 187 err(1, "%s", spectral->atd.ad_name); 188 } 189 190 static void 191 spectral_enable_at_reset(struct spectralhandler *spectral, int val) 192 { 193 int v = val; 194 195 spectral->atd.ad_id = SPECTRAL_CONTROL_ENABLE_AT_RESET 196 | ATH_DIAG_IN; 197 198 /* 199 * XXX don't need these, but need to eliminate the ATH_DIAG_DYN flag 200 * and debug 201 */ 202 spectral->atd.ad_out_data = NULL; 203 spectral->atd.ad_out_size = 0; 204 spectral->atd.ad_in_data = (caddr_t) &v; 205 spectral->atd.ad_in_size = sizeof(v); 206 207 printf("%s: val=%d\n", __func__, v); 208 209 if (ioctl(spectral->s, SIOCGATHSPECTRAL, &spectral->atd) < 0) 210 err(1, "%s", spectral->atd.ad_name); 211 } 212 213 static int 214 spectral_set_param(struct spectralhandler *spectral, const char *param, 215 const char *val) 216 { 217 int v; 218 219 v = atoi(val); 220 221 if (strcmp(param, "ss_short_report") == 0) { 222 spectralset(spectral, SPECTRAL_PARAM_SS_SHORT_RPT, v); 223 } else if (strcmp(param, "ss_fft_period") == 0) { 224 spectralset(spectral, SPECTRAL_PARAM_FFT_PERIOD, v); 225 } else if (strcmp(param, "ss_period") == 0) { 226 spectralset(spectral, SPECTRAL_PARAM_SS_PERIOD, v); 227 } else if (strcmp(param, "ss_count") == 0) { 228 spectralset(spectral, SPECTRAL_PARAM_SS_COUNT, v); 229 } else if (strcmp(param, "ss_spectral_pri") == 0) { 230 spectralset(spectral, SPECTRAL_PARAM_SS_SPECTRAL_PRI, v); 231 } else { 232 return (0); 233 } 234 235 #if 0 236 if (strcmp(param, "enabled") == 0) { 237 spectralset(spectral, DFS_PARAM_ENABLE, v); 238 } else if (strcmp(param, "firpwr") == 0) { 239 spectralset(spectral, DFS_PARAM_FIRPWR, v); 240 } else if (strcmp(param, "rrssi") == 0) { 241 spectralset(spectral, DFS_PARAM_RRSSI, v); 242 } else if (strcmp(param, "height") == 0) { 243 spectralset(spectral, DFS_PARAM_HEIGHT, v); 244 } else if (strcmp(param, "prssi") == 0) { 245 spectralset(spectral, DFS_PARAM_PRSSI, v); 246 } else if (strcmp(param, "inband") == 0) { 247 spectralset(spectral, DFS_PARAM_INBAND, v); 248 } else if (strcmp(param, "relpwr") == 0) { 249 spectralset(spectral, DFS_PARAM_RELPWR, v); 250 } else if (strcmp(param, "relstep") == 0) { 251 spectralset(spectral, DFS_PARAM_RELSTEP, v); 252 } else if (strcmp(param, "maxlen") == 0) { 253 spectralset(spectral, DFS_PARAM_MAXLEN, v); 254 } else if (strcmp(param, "usefir128") == 0) { 255 spectralset(spectral, DFS_PARAM_USEFIR128, v); 256 } else if (strcmp(param, "blockspectral") == 0) { 257 spectralset(spectral, DFS_PARAM_BLOCKRADAR, v); 258 } else if (strcmp(param, "enmaxrssi") == 0) { 259 spectralset(spectral, DFS_PARAM_MAXRSSI_EN, v); 260 } else if (strcmp(param, "extchannel") == 0) { 261 spectralset(spectral, DFS_PARAM_EN_EXTCH, v); 262 } else if (strcmp(param, "enrelpwr") == 0) { 263 spectralset(spectral, DFS_PARAM_RELPWR_EN, v); 264 } else if (strcmp(param, "en_relstep_check") == 0) { 265 spectralset(spectral, DFS_PARAM_RELSTEP_EN, v); 266 } else { 267 return 0; 268 } 269 #endif 270 271 return 1; 272 } 273 274 void 275 usage(const char *progname) 276 { 277 printf("Usage:\n"); 278 printf("\t%s: [-i <interface>] <cmd> (<arg>)\n", progname); 279 printf("\t%s: [-h]\n", progname); 280 printf("\n"); 281 printf("Valid commands:\n"); 282 printf("\tget:\t\tGet current spectral parameters\n"); 283 printf("\tset <param> <value>:\t\tSet spectral parameter\n"); 284 printf("\tstart: Start spectral scan\n"); 285 printf("\tstop: Stop spectral scan\n"); 286 printf("\tenable_at_reset <0|1>: enable reporting upon channel reset\n"); 287 } 288 289 int 290 main(int argc, char *argv[]) 291 { 292 struct spectralhandler spectral; 293 const char *devname = ATH_DEFAULT; 294 const char *progname = argv[0]; 295 296 memset(&spectral, 0, sizeof(spectral)); 297 298 /* Parse command line options */ 299 if (argc >= 2 && strcmp(argv[1], "-h") == 0) { 300 usage(progname); 301 exit(0); 302 } 303 if (argc >= 2 && strcmp(argv[1], "-?") == 0) { 304 usage(progname); 305 exit(0); 306 } 307 308 if (argc >= 2 && strcmp(argv[1], "-i") == 0) { 309 if (argc == 2) { 310 usage(progname); 311 exit(127); 312 } 313 devname = argv[2]; 314 argc -= 2; argv += 2; 315 } 316 317 /* At this point we require at least one command */ 318 if (argc == 1) { 319 usage(progname); 320 exit(127); 321 } 322 323 if (spectral_opendev(&spectral, devname) == 0) 324 exit(127); 325 326 if (strcasecmp(argv[1], "get") == 0) { 327 spectral_get(&spectral); 328 } else if (strcasecmp(argv[1], "set") == 0) { 329 if (argc < 4) { 330 usage(progname); 331 exit(127); 332 } 333 if (spectral_set_param(&spectral, argv[2], argv[3]) == 0) { 334 usage(progname); 335 exit(127); 336 } 337 } else if (strcasecmp(argv[1], "start") == 0) { 338 spectral_start(&spectral); 339 } else if (strcasecmp(argv[1], "stop") == 0) { 340 spectral_stop(&spectral); 341 } else if (strcasecmp(argv[1], "enable_at_reset") == 0) { 342 if (argc < 3) { 343 usage(progname); 344 exit(127); 345 } 346 spectral_enable_at_reset(&spectral, atoi(argv[2])); 347 } else { 348 usage(progname); 349 exit(127); 350 } 351 352 /* wrap up */ 353 spectral_closedev(&spectral); 354 exit(0); 355 } 356