2 * P2P - generic helper functions
3 * Copyright (c) 2009, Atheros Communications
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
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
21 * This function generates a random string using the following character set:
22 * 'A'-'Z', 'a'-'z', '0'-'9'.
24 int p2p_random(char *buf, size_t len)
28 u8 letters = 'Z' - 'A' + 1;
31 if (os_get_random((unsigned char *) buf, len))
33 /* Character set: 'A'-'Z', 'a'-'z', '0'-'9' */
34 for (i = 0; i < len; i++) {
36 val %= 2 * letters + numbers;
39 else if (val < 2 * letters)
40 buf[i] = 'a' + (val - letters);
42 buf[i] = '0' + (val - 2 * letters);
49 static int p2p_channel_to_freq_j4(int reg_class, int channel)
51 /* Table J-4 in P802.11REVmb/D4.0 - Global operating classes */
52 /* TODO: more regulatory classes */
56 if (channel < 1 || channel > 13)
58 return 2407 + 5 * channel;
63 return 2414 + 5 * channel;
64 case 83: /* channels 1..9; 40 MHz */
65 case 84: /* channels 5..13; 40 MHz */
66 if (channel < 1 || channel > 13)
68 return 2407 + 5 * channel;
69 case 115: /* channels 36,40,44,48; indoor only */
70 case 118: /* channels 52,56,60,64; dfs */
71 if (channel < 36 || channel > 64)
73 return 5000 + 5 * channel;
74 case 124: /* channels 149,153,157,161 */
75 case 125: /* channels 149,153,157,161,165,169 */
76 if (channel < 149 || channel > 161)
78 return 5000 + 5 * channel;
79 case 116: /* channels 36,44; 40 MHz; indoor only */
80 case 117: /* channels 40,48; 40 MHz; indoor only */
81 case 119: /* channels 52,60; 40 MHz; dfs */
82 case 120: /* channels 56,64; 40 MHz; dfs */
83 if (channel < 36 || channel > 64)
85 return 5000 + 5 * channel;
86 case 126: /* channels 149,157; 40 MHz */
87 case 127: /* channels 153,161; 40 MHz */
88 if (channel < 149 || channel > 161)
90 return 5000 + 5 * channel;
97 * p2p_channel_to_freq - Convert channel info to frequency
98 * @country: Country code
99 * @reg_class: Regulatory class
100 * @channel: Channel number
101 * Returns: Frequency in MHz or -1 if the specified channel is unknown
103 int p2p_channel_to_freq(const char *country, int reg_class, int channel)
105 if (country[2] == 0x04)
106 return p2p_channel_to_freq_j4(reg_class, channel);
108 /* These are mainly for backwards compatibility; to be removed */
110 case 1: /* US/1, EU/1, JP/1 = 5 GHz, channels 36,40,44,48 */
111 if (channel < 36 || channel > 48)
113 return 5000 + 5 * channel;
114 case 3: /* US/3 = 5 GHz, channels 149,153,157,161 */
115 case 5: /* US/5 = 5 GHz, channels 149,153,157,161 */
116 if (channel < 149 || channel > 161)
118 return 5000 + 5 * channel;
119 case 4: /* EU/4 = 2.407 GHz, channels 1..13 */
120 case 12: /* US/12 = 2.407 GHz, channels 1..11 */
121 case 30: /* JP/30 = 2.407 GHz, channels 1..13 */
122 if (channel < 1 || channel > 13)
124 return 2407 + 5 * channel;
125 case 31: /* JP/31 = 2.414 GHz, channel 14 */
128 return 2414 + 5 * channel;
136 * p2p_freq_to_channel - Convert frequency into channel info
137 * @country: Country code
138 * @reg_class: Buffer for returning regulatory class
139 * @channel: Buffer for returning channel number
140 * Returns: 0 on success, -1 if the specified frequency is unknown
142 int p2p_freq_to_channel(const char *country, unsigned int freq, u8 *reg_class,
145 /* TODO: more operating classes */
146 if (freq >= 2412 && freq <= 2472) {
147 *reg_class = 81; /* 2.407 GHz, channels 1..13 */
148 *channel = (freq - 2407) / 5;
153 *reg_class = 82; /* channel 14 */
158 if (freq >= 5180 && freq <= 5240) {
159 *reg_class = 115; /* 5 GHz, channels 36..48 */
160 *channel = (freq - 5000) / 5;
164 if (freq >= 5745 && freq <= 5805) {
165 *reg_class = 124; /* 5 GHz, channels 149..161 */
166 *channel = (freq - 5000) / 5;
174 static void p2p_reg_class_intersect(const struct p2p_reg_class *a,
175 const struct p2p_reg_class *b,
176 struct p2p_reg_class *res)
180 res->reg_class = a->reg_class;
182 for (i = 0; i < a->channels; i++) {
183 for (j = 0; j < b->channels; j++) {
184 if (a->channel[i] != b->channel[j])
186 res->channel[res->channels] = a->channel[i];
188 if (res->channels == P2P_MAX_REG_CLASS_CHANNELS)
196 * p2p_channels_intersect - Intersection of supported channel lists
197 * @a: First set of supported channels
198 * @b: Second set of supported channels
199 * @res: Data structure for returning the intersection of support channels
201 * This function can be used to find a common set of supported channels. Both
202 * input channels sets are assumed to use the same country code. If different
203 * country codes are used, the regulatory class numbers may not be matched
204 * correctly and results are undefined.
206 void p2p_channels_intersect(const struct p2p_channels *a,
207 const struct p2p_channels *b,
208 struct p2p_channels *res)
212 os_memset(res, 0, sizeof(*res));
214 for (i = 0; i < a->reg_classes; i++) {
215 const struct p2p_reg_class *a_reg = &a->reg_class[i];
216 for (j = 0; j < b->reg_classes; j++) {
217 const struct p2p_reg_class *b_reg = &b->reg_class[j];
218 if (a_reg->reg_class != b_reg->reg_class)
220 p2p_reg_class_intersect(
222 &res->reg_class[res->reg_classes]);
223 if (res->reg_class[res->reg_classes].channels) {
225 if (res->reg_classes == P2P_MAX_REG_CLASSES)
234 * p2p_channels_includes - Check whether a channel is included in the list
235 * @channels: List of supported channels
236 * @reg_class: Regulatory class of the channel to search
237 * @channel: Channel number of the channel to search
238 * Returns: 1 if channel was found or 0 if not
240 int p2p_channels_includes(const struct p2p_channels *channels, u8 reg_class,
244 for (i = 0; i < channels->reg_classes; i++) {
245 const struct p2p_reg_class *reg = &channels->reg_class[i];
246 if (reg->reg_class != reg_class)
248 for (j = 0; j < reg->channels; j++) {
249 if (reg->channel[j] == channel)
257 int p2p_supported_freq(struct p2p_data *p2p, unsigned int freq)
259 u8 op_reg_class, op_channel;
260 if (p2p_freq_to_channel(p2p->cfg->country, freq,
261 &op_reg_class, &op_channel) < 0)
263 return p2p_channels_includes(&p2p->cfg->channels, op_reg_class,