Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1 | /* |
| 2 | * P2P - generic helper functions |
| 3 | * Copyright (c) 2009, Atheros Communications |
| 4 | * |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 5 | * This software may be distributed under the terms of the BSD license. |
| 6 | * See README for more details. |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include "includes.h" |
| 10 | |
| 11 | #include "common.h" |
| 12 | #include "p2p_i.h" |
| 13 | |
| 14 | |
| 15 | /** |
| 16 | * p2p_random - Generate random string for SSID and passphrase |
| 17 | * @buf: Buffer for returning the result |
| 18 | * @len: Number of octets to write to the buffer |
| 19 | * Returns: 0 on success, -1 on failure |
| 20 | * |
| 21 | * This function generates a random string using the following character set: |
| 22 | * 'A'-'Z', 'a'-'z', '0'-'9'. |
| 23 | */ |
| 24 | int p2p_random(char *buf, size_t len) |
| 25 | { |
| 26 | u8 val; |
| 27 | size_t i; |
| 28 | u8 letters = 'Z' - 'A' + 1; |
| 29 | u8 numbers = 10; |
| 30 | |
| 31 | if (os_get_random((unsigned char *) buf, len)) |
| 32 | return -1; |
| 33 | /* Character set: 'A'-'Z', 'a'-'z', '0'-'9' */ |
| 34 | for (i = 0; i < len; i++) { |
| 35 | val = buf[i]; |
| 36 | val %= 2 * letters + numbers; |
| 37 | if (val < letters) |
| 38 | buf[i] = 'A' + val; |
| 39 | else if (val < 2 * letters) |
| 40 | buf[i] = 'a' + (val - letters); |
| 41 | else |
| 42 | buf[i] = '0' + (val - 2 * letters); |
| 43 | } |
| 44 | |
| 45 | return 0; |
| 46 | } |
| 47 | |
| 48 | |
Dmitry Shmidt | 4b06059 | 2013-04-29 16:42:49 -0700 | [diff] [blame] | 49 | /** |
| 50 | * p2p_channel_to_freq - Convert channel info to frequency |
| 51 | * @op_class: Operating class |
| 52 | * @channel: Channel number |
| 53 | * Returns: Frequency in MHz or -1 if the specified channel is unknown |
| 54 | */ |
| 55 | int p2p_channel_to_freq(int op_class, int channel) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 56 | { |
Dmitry Shmidt | 4b06059 | 2013-04-29 16:42:49 -0700 | [diff] [blame] | 57 | /* Table E-4 in IEEE Std 802.11-2012 - Global operating classes */ |
| 58 | /* TODO: more operating classes */ |
| 59 | switch (op_class) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 60 | case 81: |
| 61 | /* channels 1..13 */ |
| 62 | if (channel < 1 || channel > 13) |
| 63 | return -1; |
| 64 | return 2407 + 5 * channel; |
| 65 | case 82: |
| 66 | /* channel 14 */ |
| 67 | if (channel != 14) |
| 68 | return -1; |
| 69 | return 2414 + 5 * channel; |
| 70 | case 83: /* channels 1..9; 40 MHz */ |
| 71 | case 84: /* channels 5..13; 40 MHz */ |
| 72 | if (channel < 1 || channel > 13) |
| 73 | return -1; |
| 74 | return 2407 + 5 * channel; |
| 75 | case 115: /* channels 36,40,44,48; indoor only */ |
| 76 | case 118: /* channels 52,56,60,64; dfs */ |
| 77 | if (channel < 36 || channel > 64) |
| 78 | return -1; |
| 79 | return 5000 + 5 * channel; |
| 80 | case 124: /* channels 149,153,157,161 */ |
| 81 | case 125: /* channels 149,153,157,161,165,169 */ |
| 82 | if (channel < 149 || channel > 161) |
| 83 | return -1; |
| 84 | return 5000 + 5 * channel; |
| 85 | case 116: /* channels 36,44; 40 MHz; indoor only */ |
| 86 | case 117: /* channels 40,48; 40 MHz; indoor only */ |
| 87 | case 119: /* channels 52,60; 40 MHz; dfs */ |
| 88 | case 120: /* channels 56,64; 40 MHz; dfs */ |
| 89 | if (channel < 36 || channel > 64) |
| 90 | return -1; |
| 91 | return 5000 + 5 * channel; |
| 92 | case 126: /* channels 149,157; 40 MHz */ |
| 93 | case 127: /* channels 153,161; 40 MHz */ |
| 94 | if (channel < 149 || channel > 161) |
| 95 | return -1; |
| 96 | return 5000 + 5 * channel; |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 97 | case 128: /* center freqs 42, 58, 106, 122, 138, 155; 80 MHz */ |
| 98 | if (channel < 36 || channel > 161) |
| 99 | return -1; |
| 100 | return 5000 + 5 * channel; |
Dmitry Shmidt | 76cd2cc | 2014-05-27 12:56:04 -0700 | [diff] [blame] | 101 | case 180: /* 60 GHz band, channels 1..4 */ |
| 102 | if (channel < 1 || channel > 4) |
| 103 | return -1; |
| 104 | return 56160 + 2160 * channel; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 105 | } |
| 106 | return -1; |
| 107 | } |
| 108 | |
| 109 | |
| 110 | /** |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 111 | * p2p_freq_to_channel - Convert frequency into channel info |
Dmitry Shmidt | 4b06059 | 2013-04-29 16:42:49 -0700 | [diff] [blame] | 112 | * @op_class: Buffer for returning operating class |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 113 | * @channel: Buffer for returning channel number |
| 114 | * Returns: 0 on success, -1 if the specified frequency is unknown |
| 115 | */ |
Dmitry Shmidt | 4b06059 | 2013-04-29 16:42:49 -0700 | [diff] [blame] | 116 | int p2p_freq_to_channel(unsigned int freq, u8 *op_class, u8 *channel) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 117 | { |
| 118 | /* TODO: more operating classes */ |
| 119 | if (freq >= 2412 && freq <= 2472) { |
Dmitry Shmidt | b7b4d0e | 2013-08-26 12:09:05 -0700 | [diff] [blame] | 120 | if ((freq - 2407) % 5) |
| 121 | return -1; |
| 122 | |
Dmitry Shmidt | 4b06059 | 2013-04-29 16:42:49 -0700 | [diff] [blame] | 123 | *op_class = 81; /* 2.407 GHz, channels 1..13 */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 124 | *channel = (freq - 2407) / 5; |
| 125 | return 0; |
| 126 | } |
| 127 | |
| 128 | if (freq == 2484) { |
Dmitry Shmidt | 4b06059 | 2013-04-29 16:42:49 -0700 | [diff] [blame] | 129 | *op_class = 82; /* channel 14 */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 130 | *channel = 14; |
| 131 | return 0; |
| 132 | } |
| 133 | |
| 134 | if (freq >= 5180 && freq <= 5240) { |
Dmitry Shmidt | b7b4d0e | 2013-08-26 12:09:05 -0700 | [diff] [blame] | 135 | if ((freq - 5000) % 5) |
| 136 | return -1; |
| 137 | |
Dmitry Shmidt | 4b06059 | 2013-04-29 16:42:49 -0700 | [diff] [blame] | 138 | *op_class = 115; /* 5 GHz, channels 36..48 */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 139 | *channel = (freq - 5000) / 5; |
| 140 | return 0; |
| 141 | } |
| 142 | |
| 143 | if (freq >= 5745 && freq <= 5805) { |
Dmitry Shmidt | b7b4d0e | 2013-08-26 12:09:05 -0700 | [diff] [blame] | 144 | if ((freq - 5000) % 5) |
| 145 | return -1; |
| 146 | |
Dmitry Shmidt | 4b06059 | 2013-04-29 16:42:49 -0700 | [diff] [blame] | 147 | *op_class = 124; /* 5 GHz, channels 149..161 */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 148 | *channel = (freq - 5000) / 5; |
| 149 | return 0; |
| 150 | } |
| 151 | |
Dmitry Shmidt | 661b4f7 | 2014-09-29 14:58:27 -0700 | [diff] [blame^] | 152 | if (freq >= 58320 && freq <= 64800) { |
| 153 | if ((freq - 58320) % 2160) |
| 154 | return -1; |
| 155 | |
| 156 | *op_class = 180; /* 60 GHz, channels 1..4 */ |
| 157 | *channel = (freq - 56160) / 2160; |
| 158 | return 0; |
| 159 | } |
| 160 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 161 | return -1; |
| 162 | } |
| 163 | |
| 164 | |
| 165 | static void p2p_reg_class_intersect(const struct p2p_reg_class *a, |
| 166 | const struct p2p_reg_class *b, |
| 167 | struct p2p_reg_class *res) |
| 168 | { |
| 169 | size_t i, j; |
| 170 | |
| 171 | res->reg_class = a->reg_class; |
| 172 | |
| 173 | for (i = 0; i < a->channels; i++) { |
| 174 | for (j = 0; j < b->channels; j++) { |
| 175 | if (a->channel[i] != b->channel[j]) |
| 176 | continue; |
| 177 | res->channel[res->channels] = a->channel[i]; |
| 178 | res->channels++; |
| 179 | if (res->channels == P2P_MAX_REG_CLASS_CHANNELS) |
| 180 | return; |
| 181 | } |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | |
| 186 | /** |
| 187 | * p2p_channels_intersect - Intersection of supported channel lists |
| 188 | * @a: First set of supported channels |
| 189 | * @b: Second set of supported channels |
| 190 | * @res: Data structure for returning the intersection of support channels |
| 191 | * |
| 192 | * This function can be used to find a common set of supported channels. Both |
| 193 | * input channels sets are assumed to use the same country code. If different |
| 194 | * country codes are used, the regulatory class numbers may not be matched |
| 195 | * correctly and results are undefined. |
| 196 | */ |
| 197 | void p2p_channels_intersect(const struct p2p_channels *a, |
| 198 | const struct p2p_channels *b, |
| 199 | struct p2p_channels *res) |
| 200 | { |
| 201 | size_t i, j; |
| 202 | |
| 203 | os_memset(res, 0, sizeof(*res)); |
| 204 | |
| 205 | for (i = 0; i < a->reg_classes; i++) { |
| 206 | const struct p2p_reg_class *a_reg = &a->reg_class[i]; |
| 207 | for (j = 0; j < b->reg_classes; j++) { |
| 208 | const struct p2p_reg_class *b_reg = &b->reg_class[j]; |
| 209 | if (a_reg->reg_class != b_reg->reg_class) |
| 210 | continue; |
| 211 | p2p_reg_class_intersect( |
| 212 | a_reg, b_reg, |
| 213 | &res->reg_class[res->reg_classes]); |
| 214 | if (res->reg_class[res->reg_classes].channels) { |
| 215 | res->reg_classes++; |
| 216 | if (res->reg_classes == P2P_MAX_REG_CLASSES) |
| 217 | return; |
| 218 | } |
| 219 | } |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 224 | static void p2p_op_class_union(struct p2p_reg_class *cl, |
| 225 | const struct p2p_reg_class *b_cl) |
| 226 | { |
| 227 | size_t i, j; |
| 228 | |
| 229 | for (i = 0; i < b_cl->channels; i++) { |
| 230 | for (j = 0; j < cl->channels; j++) { |
| 231 | if (b_cl->channel[i] == cl->channel[j]) |
| 232 | break; |
| 233 | } |
| 234 | if (j == cl->channels) { |
| 235 | if (cl->channels == P2P_MAX_REG_CLASS_CHANNELS) |
| 236 | return; |
| 237 | cl->channel[cl->channels++] = b_cl->channel[i]; |
| 238 | } |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | |
| 243 | /** |
| 244 | * p2p_channels_union - Union of channel lists |
| 245 | * @a: First set of channels |
| 246 | * @b: Second set of channels |
| 247 | * @res: Data structure for returning the union of channels |
| 248 | */ |
| 249 | void p2p_channels_union(const struct p2p_channels *a, |
| 250 | const struct p2p_channels *b, |
| 251 | struct p2p_channels *res) |
| 252 | { |
| 253 | size_t i, j; |
| 254 | |
| 255 | if (a != res) |
| 256 | os_memcpy(res, a, sizeof(*res)); |
| 257 | |
| 258 | for (i = 0; i < res->reg_classes; i++) { |
| 259 | struct p2p_reg_class *cl = &res->reg_class[i]; |
| 260 | for (j = 0; j < b->reg_classes; j++) { |
| 261 | const struct p2p_reg_class *b_cl = &b->reg_class[j]; |
| 262 | if (cl->reg_class != b_cl->reg_class) |
| 263 | continue; |
| 264 | p2p_op_class_union(cl, b_cl); |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | for (j = 0; j < b->reg_classes; j++) { |
| 269 | const struct p2p_reg_class *b_cl = &b->reg_class[j]; |
| 270 | |
| 271 | for (i = 0; i < res->reg_classes; i++) { |
| 272 | struct p2p_reg_class *cl = &res->reg_class[i]; |
| 273 | if (cl->reg_class == b_cl->reg_class) |
| 274 | break; |
| 275 | } |
| 276 | |
| 277 | if (i == res->reg_classes) { |
| 278 | if (res->reg_classes == P2P_MAX_REG_CLASSES) |
| 279 | return; |
| 280 | os_memcpy(&res->reg_class[res->reg_classes++], |
| 281 | b_cl, sizeof(struct p2p_reg_class)); |
| 282 | } |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | |
| 287 | void p2p_channels_remove_freqs(struct p2p_channels *chan, |
| 288 | const struct wpa_freq_range_list *list) |
| 289 | { |
| 290 | size_t o, c; |
| 291 | |
| 292 | if (list == NULL) |
| 293 | return; |
| 294 | |
| 295 | o = 0; |
| 296 | while (o < chan->reg_classes) { |
| 297 | struct p2p_reg_class *op = &chan->reg_class[o]; |
| 298 | |
| 299 | c = 0; |
| 300 | while (c < op->channels) { |
| 301 | int freq = p2p_channel_to_freq(op->reg_class, |
| 302 | op->channel[c]); |
| 303 | if (freq > 0 && freq_range_list_includes(list, freq)) { |
| 304 | op->channels--; |
| 305 | os_memmove(&op->channel[c], |
| 306 | &op->channel[c + 1], |
| 307 | op->channels - c); |
| 308 | } else |
| 309 | c++; |
| 310 | } |
| 311 | |
| 312 | if (op->channels == 0) { |
| 313 | chan->reg_classes--; |
| 314 | os_memmove(&chan->reg_class[o], &chan->reg_class[o + 1], |
| 315 | (chan->reg_classes - o) * |
| 316 | sizeof(struct p2p_reg_class)); |
| 317 | } else |
| 318 | o++; |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 323 | /** |
| 324 | * p2p_channels_includes - Check whether a channel is included in the list |
| 325 | * @channels: List of supported channels |
| 326 | * @reg_class: Regulatory class of the channel to search |
| 327 | * @channel: Channel number of the channel to search |
| 328 | * Returns: 1 if channel was found or 0 if not |
| 329 | */ |
| 330 | int p2p_channels_includes(const struct p2p_channels *channels, u8 reg_class, |
| 331 | u8 channel) |
| 332 | { |
| 333 | size_t i, j; |
| 334 | for (i = 0; i < channels->reg_classes; i++) { |
| 335 | const struct p2p_reg_class *reg = &channels->reg_class[i]; |
| 336 | if (reg->reg_class != reg_class) |
| 337 | continue; |
| 338 | for (j = 0; j < reg->channels; j++) { |
| 339 | if (reg->channel[j] == channel) |
| 340 | return 1; |
| 341 | } |
| 342 | } |
| 343 | return 0; |
| 344 | } |
| 345 | |
| 346 | |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 347 | int p2p_channels_includes_freq(const struct p2p_channels *channels, |
| 348 | unsigned int freq) |
| 349 | { |
| 350 | size_t i, j; |
| 351 | for (i = 0; i < channels->reg_classes; i++) { |
| 352 | const struct p2p_reg_class *reg = &channels->reg_class[i]; |
| 353 | for (j = 0; j < reg->channels; j++) { |
Dmitry Shmidt | 4b06059 | 2013-04-29 16:42:49 -0700 | [diff] [blame] | 354 | if (p2p_channel_to_freq(reg->reg_class, |
| 355 | reg->channel[j]) == (int) freq) |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 356 | return 1; |
| 357 | } |
| 358 | } |
| 359 | return 0; |
| 360 | } |
| 361 | |
| 362 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 363 | int p2p_supported_freq(struct p2p_data *p2p, unsigned int freq) |
| 364 | { |
| 365 | u8 op_reg_class, op_channel; |
Dmitry Shmidt | 4b06059 | 2013-04-29 16:42:49 -0700 | [diff] [blame] | 366 | if (p2p_freq_to_channel(freq, &op_reg_class, &op_channel) < 0) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 367 | return 0; |
| 368 | return p2p_channels_includes(&p2p->cfg->channels, op_reg_class, |
| 369 | op_channel); |
| 370 | } |
Dmitry Shmidt | 44c9578 | 2013-05-17 09:51:35 -0700 | [diff] [blame] | 371 | |
| 372 | |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 373 | int p2p_supported_freq_go(struct p2p_data *p2p, unsigned int freq) |
| 374 | { |
| 375 | u8 op_reg_class, op_channel; |
| 376 | if (p2p_freq_to_channel(freq, &op_reg_class, &op_channel) < 0) |
| 377 | return 0; |
| 378 | return p2p_channels_includes(&p2p->cfg->channels, op_reg_class, |
| 379 | op_channel) && |
| 380 | !freq_range_list_includes(&p2p->no_go_freq, freq); |
| 381 | } |
| 382 | |
| 383 | |
| 384 | int p2p_supported_freq_cli(struct p2p_data *p2p, unsigned int freq) |
| 385 | { |
| 386 | u8 op_reg_class, op_channel; |
| 387 | if (p2p_freq_to_channel(freq, &op_reg_class, &op_channel) < 0) |
| 388 | return 0; |
| 389 | return p2p_channels_includes(&p2p->cfg->channels, op_reg_class, |
| 390 | op_channel) || |
| 391 | p2p_channels_includes(&p2p->cfg->cli_channels, op_reg_class, |
| 392 | op_channel); |
| 393 | } |
| 394 | |
| 395 | |
Dmitry Shmidt | 44c9578 | 2013-05-17 09:51:35 -0700 | [diff] [blame] | 396 | unsigned int p2p_get_pref_freq(struct p2p_data *p2p, |
| 397 | const struct p2p_channels *channels) |
| 398 | { |
| 399 | unsigned int i; |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 400 | int freq = 0; |
| 401 | const struct p2p_channels *tmpc = channels ? |
| 402 | channels : &p2p->cfg->channels; |
| 403 | |
| 404 | if (tmpc == NULL) |
| 405 | return 0; |
Dmitry Shmidt | 44c9578 | 2013-05-17 09:51:35 -0700 | [diff] [blame] | 406 | |
| 407 | for (i = 0; p2p->cfg->pref_chan && i < p2p->cfg->num_pref_chan; i++) { |
| 408 | freq = p2p_channel_to_freq(p2p->cfg->pref_chan[i].op_class, |
| 409 | p2p->cfg->pref_chan[i].chan); |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 410 | if (p2p_channels_includes_freq(tmpc, freq)) |
Dmitry Shmidt | 44c9578 | 2013-05-17 09:51:35 -0700 | [diff] [blame] | 411 | return freq; |
| 412 | } |
Dmitry Shmidt | 44c9578 | 2013-05-17 09:51:35 -0700 | [diff] [blame] | 413 | return 0; |
| 414 | } |
Dmitry Shmidt | 4ce9c87 | 2013-10-24 11:08:13 -0700 | [diff] [blame] | 415 | |
| 416 | |
| 417 | void p2p_channels_dump(struct p2p_data *p2p, const char *title, |
| 418 | const struct p2p_channels *chan) |
| 419 | { |
| 420 | char buf[500], *pos, *end; |
| 421 | size_t i, j; |
| 422 | int ret; |
| 423 | |
| 424 | pos = buf; |
| 425 | end = pos + sizeof(buf); |
| 426 | |
| 427 | for (i = 0; i < chan->reg_classes; i++) { |
| 428 | const struct p2p_reg_class *c; |
| 429 | c = &chan->reg_class[i]; |
| 430 | ret = os_snprintf(pos, end - pos, " %u:", c->reg_class); |
| 431 | if (ret < 0 || ret >= end - pos) |
| 432 | break; |
| 433 | pos += ret; |
| 434 | |
| 435 | for (j = 0; j < c->channels; j++) { |
| 436 | ret = os_snprintf(pos, end - pos, "%s%u", |
| 437 | j == 0 ? "" : ",", |
| 438 | c->channel[j]); |
| 439 | if (ret < 0 || ret >= end - pos) |
| 440 | break; |
| 441 | pos += ret; |
| 442 | } |
| 443 | } |
| 444 | *pos = '\0'; |
| 445 | |
| 446 | p2p_dbg(p2p, "%s:%s", title, buf); |
| 447 | } |
Dmitry Shmidt | e0e48dc | 2013-11-18 12:00:06 -0800 | [diff] [blame] | 448 | |
| 449 | |
Dmitry Shmidt | d11f019 | 2014-03-24 12:09:47 -0700 | [diff] [blame] | 450 | static u8 p2p_channel_pick_random(const u8 *channels, unsigned int num_channels) |
| 451 | { |
| 452 | unsigned int r; |
Dmitry Shmidt | 661b4f7 | 2014-09-29 14:58:27 -0700 | [diff] [blame^] | 453 | if (os_get_random((u8 *) &r, sizeof(r)) < 0) |
| 454 | r = 0; |
Dmitry Shmidt | d11f019 | 2014-03-24 12:09:47 -0700 | [diff] [blame] | 455 | r %= num_channels; |
| 456 | return channels[r]; |
| 457 | } |
| 458 | |
| 459 | |
Dmitry Shmidt | e0e48dc | 2013-11-18 12:00:06 -0800 | [diff] [blame] | 460 | int p2p_channel_select(struct p2p_channels *chans, const int *classes, |
| 461 | u8 *op_class, u8 *op_channel) |
| 462 | { |
Dmitry Shmidt | d11f019 | 2014-03-24 12:09:47 -0700 | [diff] [blame] | 463 | unsigned int i, j; |
Dmitry Shmidt | e0e48dc | 2013-11-18 12:00:06 -0800 | [diff] [blame] | 464 | |
Dmitry Shmidt | d11f019 | 2014-03-24 12:09:47 -0700 | [diff] [blame] | 465 | for (j = 0; classes == NULL || classes[j]; j++) { |
Dmitry Shmidt | a0d265f | 2013-11-19 13:13:41 -0800 | [diff] [blame] | 466 | for (i = 0; i < chans->reg_classes; i++) { |
| 467 | struct p2p_reg_class *c = &chans->reg_class[i]; |
Dmitry Shmidt | e0e48dc | 2013-11-18 12:00:06 -0800 | [diff] [blame] | 468 | |
Dmitry Shmidt | a0d265f | 2013-11-19 13:13:41 -0800 | [diff] [blame] | 469 | if (c->channels == 0) |
| 470 | continue; |
Dmitry Shmidt | e0e48dc | 2013-11-18 12:00:06 -0800 | [diff] [blame] | 471 | |
Dmitry Shmidt | d11f019 | 2014-03-24 12:09:47 -0700 | [diff] [blame] | 472 | if (classes == NULL || c->reg_class == classes[j]) { |
Dmitry Shmidt | a0d265f | 2013-11-19 13:13:41 -0800 | [diff] [blame] | 473 | /* |
| 474 | * Pick one of the available channels in the |
| 475 | * operating class at random. |
| 476 | */ |
Dmitry Shmidt | a0d265f | 2013-11-19 13:13:41 -0800 | [diff] [blame] | 477 | *op_class = c->reg_class; |
Dmitry Shmidt | d11f019 | 2014-03-24 12:09:47 -0700 | [diff] [blame] | 478 | *op_channel = p2p_channel_pick_random( |
| 479 | c->channel, c->channels); |
Dmitry Shmidt | a0d265f | 2013-11-19 13:13:41 -0800 | [diff] [blame] | 480 | return 0; |
| 481 | } |
Dmitry Shmidt | e0e48dc | 2013-11-18 12:00:06 -0800 | [diff] [blame] | 482 | } |
Dmitry Shmidt | d11f019 | 2014-03-24 12:09:47 -0700 | [diff] [blame] | 483 | if (classes == NULL) |
| 484 | break; |
Dmitry Shmidt | e0e48dc | 2013-11-18 12:00:06 -0800 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | return -1; |
| 488 | } |
Dmitry Shmidt | d11f019 | 2014-03-24 12:09:47 -0700 | [diff] [blame] | 489 | |
| 490 | |
| 491 | int p2p_channel_random_social(struct p2p_channels *chans, u8 *op_class, |
| 492 | u8 *op_channel) |
| 493 | { |
Dmitry Shmidt | 661b4f7 | 2014-09-29 14:58:27 -0700 | [diff] [blame^] | 494 | u8 chan[4]; |
Dmitry Shmidt | d11f019 | 2014-03-24 12:09:47 -0700 | [diff] [blame] | 495 | unsigned int num_channels = 0; |
| 496 | |
| 497 | /* Try to find available social channels from 2.4 GHz */ |
| 498 | if (p2p_channels_includes(chans, 81, 1)) |
| 499 | chan[num_channels++] = 1; |
| 500 | if (p2p_channels_includes(chans, 81, 6)) |
| 501 | chan[num_channels++] = 6; |
| 502 | if (p2p_channels_includes(chans, 81, 11)) |
| 503 | chan[num_channels++] = 11; |
| 504 | |
Dmitry Shmidt | 661b4f7 | 2014-09-29 14:58:27 -0700 | [diff] [blame^] | 505 | /* Try to find available social channels from 60 GHz */ |
| 506 | if (p2p_channels_includes(chans, 180, 2)) |
| 507 | chan[num_channels++] = 2; |
| 508 | |
Dmitry Shmidt | d11f019 | 2014-03-24 12:09:47 -0700 | [diff] [blame] | 509 | if (num_channels == 0) |
| 510 | return -1; |
| 511 | |
Dmitry Shmidt | d11f019 | 2014-03-24 12:09:47 -0700 | [diff] [blame] | 512 | *op_channel = p2p_channel_pick_random(chan, num_channels); |
Dmitry Shmidt | 661b4f7 | 2014-09-29 14:58:27 -0700 | [diff] [blame^] | 513 | if (*op_channel == 2) |
| 514 | *op_class = 180; |
| 515 | else |
| 516 | *op_class = 81; |
Dmitry Shmidt | d11f019 | 2014-03-24 12:09:47 -0700 | [diff] [blame] | 517 | |
| 518 | return 0; |
| 519 | } |