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