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