• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Chipsea Technologies (Shenzhen) Corp., Ltd. All rights reserved.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #ifndef _WIFI_MAC_H
16 #define _WIFI_MAC_H
17 
18 #include "wb_co_int.h"
19 #include "wb_co_math.h"
20 #include "wb_co_utils.h"
21 
22 
23 /* Sync with lmac_mac.h */
24 
25 /// Interface types
26 enum mac_vif_type
27 {
28     /// ESS STA interface
29     VIF_STA,
30     /// IBSS STA interface
31     VIF_IBSS,
32     /// AP interface
33     VIF_AP,
34     /// Mesh Point interface
35     VIF_MESH_POINT,
36     /// Monitor interface
37     VIF_MONITOR,
38     /// Unknown type
39     VIF_UNKNOWN
40 };
41 /// Macro defining an invalid VIF index
42 #define INVALID_VIF_IDX 0xFF
43 
44 /// MAC address length in bytes.
45 #define MAC_ADDR_LEN 6
46 
47 /// MAC address structure.
48 struct mac_addr
49 {
50     /// Array of 16-bit words that make up the MAC address.
51     uint16_t array[MAC_ADDR_LEN/2];
52 };
53 
54 /// SSID maximum length.
55 #define MAC_SSID_LEN 32
56 
57 /// SSID.
58 struct mac_ssid
59 {
60     /// Actual length of the SSID.
61     uint8_t length;
62     /// Array containing the SSID name.
63     uint8_t array[MAC_SSID_LEN];
64 };
65 
66 /// BSS type
67 enum mac_bss_type
68 {
69     INFRASTRUCTURE_MODE = 1,
70     INDEPENDENT_BSS_MODE,
71     ANY_BSS_MODE
72 };
73 
74 /// Channel Band
75 enum mac_chan_band
76 {
77     /// 2.4GHz Band
78     PHY_BAND_2G4,
79     /// 5GHz band
80     PHY_BAND_5G,
81     /// Number of bands
82     PHY_BAND_MAX,
83 };
84 
85 /// Operating Channel Bandwidth
86 enum mac_chan_bandwidth
87 {
88     /// 20MHz BW
89     PHY_CHNL_BW_20,
90     /// 40MHz BW
91     PHY_CHNL_BW_40,
92     /// 80MHz BW
93     PHY_CHNL_BW_80,
94     /// 160MHz BW
95     PHY_CHNL_BW_160,
96     /// 80+80MHz BW
97     PHY_CHNL_BW_80P80,
98     /// Reserved BW
99     PHY_CHNL_BW_OTHER,
100 };
101 
102 /// max number of channels in the 2.4 GHZ band
103 #define MAC_DOMAINCHANNEL_24G_MAX 14
104 
105 /// max number of channels in the 5 GHZ band
106 #define MAC_DOMAINCHANNEL_5G_MAX 28
107 
108 /// Channel Flag
109 enum mac_chan_flags
110 {
111     /// Cannot initiate radiation on this channel
112     CHAN_NO_IR = CO_BIT(0),
113     /// Channel is not allowed
114     CHAN_DISABLED = CO_BIT(1),
115     /// Radar detection required on this channel
116     CHAN_RADAR = CO_BIT(2),
117 };
118 
119 /// Primary Channel definition
120 struct mac_chan_def
121 {
122     /// Frequency of the channel (in MHz)
123     uint16_t freq;
124     /// RF band (@ref mac_chan_band)
125     uint8_t band;
126     /// Additional information (@ref mac_chan_flags)
127     uint8_t flags;
128     /// Max transmit power allowed on this channel (dBm)
129     int8_t tx_power;
130 };
131 
132 /// Operating Channel
133 struct mac_chan_op
134 {
135     /// Band (@ref mac_chan_band)
136     uint8_t band;
137     /// Channel type (@ref mac_chan_bandwidth)
138     uint8_t type;
139     /// Frequency for Primary 20MHz channel (in MHz)
140     uint16_t prim20_freq;
141     /// Frequency center of the contiguous channel or center of Primary 80+80 (in MHz)
142     uint16_t center1_freq;
143     /// Frequency center of the non-contiguous secondary 80+80 (in MHz)
144     uint16_t center2_freq;
145     /// Max transmit power allowed on this channel (dBm)
146     int8_t tx_power;
147     /// Additional information (@ref mac_chan_flags)
148     uint8_t flags;
149 };
150 
151 /// Cipher suites (order is important as it is used by MACHW)
152 enum mac_cipher_suite
153 {
154     /// 00-0F-AC 1
155     MAC_CIPHER_WEP40 = 0,
156     /// 00-0F-AC 2
157     MAC_CIPHER_TKIP = 1,
158     /// 00-0F-AC 4
159     MAC_CIPHER_CCMP = 2,
160     /// 00-0F-AC 5
161     MAC_CIPHER_WEP104 = 3,
162     /// 00-14-72 1
163     MAC_CIPHER_WPI_SMS4 = 4,
164     /// 00-0F-AC 6  (aka AES_CMAC)
165     MAC_CIPHER_BIP_CMAC_128 = 5,
166 
167     // following cipher are not supported by MACHW
168     /// 00-0F-AC 08
169     MAC_CIPHER_GCMP_128,
170     /// 00-0F-AC 09
171     MAC_CIPHER_GCMP_256,
172     /// 00-0F-AC 10
173     MAC_CIPHER_CCMP_256,
174     /// 00-0F-AC 11
175     MAC_CIPHER_BIP_GMAC_128,
176     /// 00-0F-AC 12
177     MAC_CIPHER_BIP_GMAC_256,
178     /// 00-0F-AC 13
179     MAC_CIPHER_BIP_CMAC_256,
180 
181     MAC_CIPHER_INVALID = 0xFF
182 };
183 
184 /// Authentication and Key Management suite
185 enum mac_akm_suite
186 {
187     /// No security
188     MAC_AKM_NONE,
189     /// Pre RSN (WEP or WPA)
190     MAC_AKM_PRE_RSN,
191     /// 00-0F-AC 1
192     MAC_AKM_8021X,
193     /// 00-0F-AC 2
194     MAC_AKM_PSK,
195     /// 00-0F-AC 3
196     MAC_AKM_FT_8021X,
197     /// 00-0F-AC 4
198     MAC_AKM_FT_PSK,
199     /// 00-0F-AC 5
200     MAC_AKM_8021X_SHA256,
201     /// 00-0F-AC 6
202     MAC_AKM_PSK_SHA256,
203     /// 00-0F-AC 7
204     MAC_AKM_TDLS,
205     /// 00-0F-AC 8
206     MAC_AKM_SAE,
207     /// 00-0F-AC 9
208     MAC_AKM_FT_OVER_SAE,
209     /// 00-0F-AC 11
210     MAC_AKM_8021X_SUITE_B,
211     /// 00-0F-AC 12
212     MAC_AKM_8021X_SUITE_B_192,
213     /// 00-0F-AC 14
214     MAC_AKM_FILS_SHA256,
215     /// 00-0F-AC 15
216     MAC_AKM_FILS_SHA384,
217     /// 00-0F-AC 16
218     MAC_AKM_FT_FILS_SHA256,
219     /// 00-0F-AC 17
220     MAC_AKM_FT_FILS_SHA384,
221     /// 00-0F-AC 18
222     MAC_AKM_OWE,
223 
224     /// 00-14-72 1
225     MAC_AKM_WAPI_CERT,
226     /// 00-14-72 2
227     MAC_AKM_WAPI_PSK,
228 };
229 
230 /// Scan result element, parsed from beacon or probe response frames.
231 struct mac_scan_result
232 {
233     /// Scan result is valid
234     bool valid_flag;
235     /// Network BSSID.
236     struct mac_addr bssid;
237     /// Network name.
238     struct mac_ssid ssid;
239     /// Network type (@ref mac_bss_type).
240     uint16_t bsstype;
241     /// Network channel.
242     struct mac_chan_def *chan;
243     /// Network beacon period (in TU).
244     uint16_t beacon_period;
245     /// Capability information
246     uint16_t cap_info;
247     /// Supported AKM (bit-field of @ref mac_akm_suite)
248     uint32_t akm;
249     /// Group cipher (bit-field of @ref mac_cipher_suite)
250     uint16_t group_cipher;
251     /// Group cipher (bit-field of @ref mac_cipher_suite)
252     uint16_t pairwise_cipher;
253     /// RSSI of the scanned BSS (in dBm)
254     uint8_t rssi;
255 };
256 
257 /// Legacy rate 802.11 definitions
258 enum mac_legacy_rates
259 {
260     /// DSSS/CCK 1Mbps
261     MAC_RATE_1MBPS   =   2,
262     /// DSSS/CCK 2Mbps
263     MAC_RATE_2MBPS   =   4,
264     /// DSSS/CCK 5.5Mbps
265     MAC_RATE_5_5MBPS =  11,
266     /// OFDM 6Mbps
267     MAC_RATE_6MBPS   =  12,
268     /// OFDM 9Mbps
269     MAC_RATE_9MBPS   =  18,
270     /// DSSS/CCK 11Mbps
271     MAC_RATE_11MBPS  =  22,
272     /// OFDM 12Mbps
273     MAC_RATE_12MBPS  =  24,
274     /// OFDM 18Mbps
275     MAC_RATE_18MBPS  =  36,
276     /// OFDM 24Mbps
277     MAC_RATE_24MBPS  =  48,
278     /// OFDM 36Mbps
279     MAC_RATE_36MBPS  =  72,
280     /// OFDM 48Mbps
281     MAC_RATE_48MBPS  =  96,
282     /// OFDM 54Mbps
283     MAC_RATE_54MBPS  = 108
284 };
285 
286 /// BSS Membership Selector definitions
287 enum mac_bss_membership
288 {
289     /// HT PHY
290     MAC_BSS_MEMBERSHIP_HT_PHY = 127,
291     /// VHT PHY
292     MAC_BSS_MEMBERSHIP_VHT_PHY = 126,
293 };
294 
295 /// MAC rateset maximum length
296 #define MAC_RATESET_LEN 12
297 
298 /// Structure containing the legacy rateset of a station
299 struct mac_rateset
300 {
301     /// Number of legacy rates supported
302     uint8_t     length;
303     /// Array of legacy rates
304     uint8_t     array[MAC_RATESET_LEN];
305 };
306 
307 /// MAC Security Key maximum length
308 #define MAC_SEC_KEY_LEN 32  // TKIP keys 256 bits (max length) with MIC keys
309 
310 /// Structure defining a security key
311 struct mac_sec_key
312 {
313     /// Key material length
314     uint8_t length;
315     /// Key material
316     uint32_t array[MAC_SEC_KEY_LEN/4];
317 };
318 
319 /// Access Category enumeration
320 enum mac_ac
321 {
322     /// Background
323     AC_BK = 0,
324     /// Best-effort
325     AC_BE,
326     /// Video
327     AC_VI,
328     /// Voice
329     AC_VO,
330     /// Number of access categories
331     AC_MAX
332 };
333 
334 /// Traffic ID enumeration
335 enum mac_tid
336 {
337     /// TID_0. Mapped to @ref AC_BE as per 802.11 standard.
338     TID_0,
339     /// TID_1. Mapped to @ref AC_BK as per 802.11 standard.
340     TID_1,
341     /// TID_2. Mapped to @ref AC_BK as per 802.11 standard.
342     TID_2,
343     /// TID_3. Mapped to @ref AC_BE as per 802.11 standard.
344     TID_3,
345     /// TID_4. Mapped to @ref AC_VI as per 802.11 standard.
346     TID_4,
347     /// TID_5. Mapped to @ref AC_VI as per 802.11 standard.
348     TID_5,
349     /// TID_6. Mapped to @ref AC_VO as per 802.11 standard.
350     TID_6,
351     /// TID_7. Mapped to @ref AC_VO as per 802.11 standard.
352     TID_7,
353     /// Non standard Management TID used internally
354     TID_MGT,
355     /// Number of TID supported
356     TID_MAX
357 };
358 
359 /// MCS bitfield maximum size (in bytes)
360 #define MAX_MCS_LEN 16 // 16 * 8 = 128
361 
362 /// MAC HT capability information element
363 struct mac_htcapability
364 {
365     /// HT capability information
366     uint16_t       ht_capa_info;
367     /// A-MPDU parameters
368     uint8_t        a_mpdu_param;
369     /// Supported MCS
370     uint8_t        mcs_rate[MAX_MCS_LEN];
371     /// HT extended capability information
372     uint16_t       ht_extended_capa;
373     /// Beamforming capability information
374     uint32_t       tx_beamforming_capa;
375     /// Antenna selection capability information
376     uint8_t        asel_capa;
377 };
378 
379 /// MAC VHT capability information element
380 struct mac_vhtcapability
381 {
382     /// VHT capability information
383     uint32_t       vht_capa_info;
384     /// RX MCS map
385     uint16_t       rx_mcs_map;
386     /// RX highest data rate
387     uint16_t       rx_highest;
388     /// TX MCS map
389     uint16_t       tx_mcs_map;
390     /// TX highest data rate
391     uint16_t       tx_highest;
392 };
393 
394 /// Length (in bytes) of the MAC HE capability field
395 #define MAC_HE_MAC_CAPA_LEN 6
396 /// Length (in bytes) of the PHY HE capability field
397 #define MAC_HE_PHY_CAPA_LEN 11
398 /// Maximum length (in bytes) of the PPE threshold data
399 #define MAC_HE_PPE_THRES_MAX_LEN 25
400 
401 /// Structure listing the per-NSS, per-BW supported MCS combinations
402 struct mac_he_mcs_nss_supp
403 {
404     /// per-NSS supported MCS in RX, for BW <= 80MHz
405     uint16_t rx_mcs_80;
406     /// per-NSS supported MCS in TX, for BW <= 80MHz
407     uint16_t tx_mcs_80;
408     /// per-NSS supported MCS in RX, for BW = 160MHz
409     uint16_t rx_mcs_160;
410     /// per-NSS supported MCS in TX, for BW = 160MHz
411     uint16_t tx_mcs_160;
412     /// per-NSS supported MCS in RX, for BW = 80+80MHz
413     uint16_t rx_mcs_80p80;
414     /// per-NSS supported MCS in TX, for BW = 80+80MHz
415     uint16_t tx_mcs_80p80;
416 };
417 
418 /// MAC HE capability information element
419 struct mac_hecapability
420 {
421     /// MAC HE capabilities
422     uint8_t mac_cap_info[MAC_HE_MAC_CAPA_LEN];
423     /// PHY HE capabilities
424     uint8_t phy_cap_info[MAC_HE_PHY_CAPA_LEN];
425     /// Supported MCS combinations
426     struct mac_he_mcs_nss_supp mcs_supp;
427     /// PPE Thresholds data
428     uint8_t ppe_thres[MAC_HE_PPE_THRES_MAX_LEN];
429 };
430 
431 /// Station flags
432 enum mac_sta_flags
433 {
434     /// Bit indicating that a STA has QoS (WMM) capability
435     STA_QOS_CAPA = 1 << 0,
436     /// Bit indicating that a STA has HT capability
437     STA_HT_CAPA = 1 << 1,
438     /// Bit indicating that a STA has VHT capability
439     STA_VHT_CAPA = 1 << 2,
440     /// Bit indicating that a STA has MFP capability
441     STA_MFP_CAPA = 1 << 3,
442     /// Bit indicating that the STA included the Operation Notification IE
443     STA_OPMOD_NOTIF = 1 << 4,
444     /// Bit indicating that a STA has HE capability
445     STA_HE_CAPA = 1 << 5,
446 };
447 
448 /// Connection flags
449 enum mac_connection_flags
450 {
451     /// Flag indicating whether the control port is controlled by host or not
452     CONTROL_PORT_HOST = 1 << 0,
453     /// Flag indicating whether the control port frame shall be sent unencrypted
454     CONTROL_PORT_NO_ENC = 1 << 1,
455     /// Flag indicating whether HT and VHT shall be disabled or not
456     DISABLE_HT = 1 << 2,
457     /// Flag indicating whether WPA or WPA2 authentication is in use
458     WPA_WPA2_IN_USE = 1 << 3,
459     /// Flag indicating whether MFP is in use
460     MFP_IN_USE = 1 << 4,
461 };
462 
463 
464 /* Sync with mac.h */
465 
466 /*
467  * DEFINES
468  ****************************************************************************************
469  */
470 /// duration of a Time Unit in microseconds
471 #define TU_DURATION                     1024
472 
473 /// Mask to test if it's a basic rate - BIT(7)
474 #define MAC_BASIC_RATE                  0x80
475 
476 /// Mask for extracting/checking word alignment
477 #define WORD_ALIGN                      3
478 
479 /**
480  ****************************************************************************************
481  * Compare two MAC addresses.
482  * The MAC addresses MUST be 16 bit aligned.
483  * @param[in] addr1_ptr Pointer to the first MAC address.
484  * @param[in] addr2_ptr Pointer to the second MAC address.
485  * @return True if equal, false if not.
486  ****************************************************************************************
487  */
488 #define MAC_ADDR_CMP(addr1_ptr, addr2_ptr)                                              \
489     ((*(((uint16_t*)(addr1_ptr)) + 0) == *(((uint16_t*)(addr2_ptr)) + 0)) &&            \
490      (*(((uint16_t*)(addr1_ptr)) + 1) == *(((uint16_t*)(addr2_ptr)) + 1)) &&            \
491      (*(((uint16_t*)(addr1_ptr)) + 2) == *(((uint16_t*)(addr2_ptr)) + 2)))
492 
493 /**
494  ****************************************************************************************
495  * Compare two MAC addresses whose alignment is not known.
496  * @param[in] __a1 Pointer to the first MAC address.
497  * @param[in] __a2 Pointer to the second MAC address.
498  * @return True if equal, false if not.
499  ****************************************************************************************
500  */
501 #define MAC_ADDR_CMP_PACKED(__a1, __a2)                                                 \
502     (memcmp(__a1, __a2, MAC_ADDR_LEN) == 0)
503 
504 /**
505  ****************************************************************************************
506  * Copy a MAC address.
507  * The MAC addresses MUST be 16 bit aligned.
508  * @param[in] addr1_ptr Pointer to the destination MAC address.
509  * @param[in] addr2_ptr Pointer to the source MAC address.
510  ****************************************************************************************
511  */
512 #define MAC_ADDR_CPY(addr1_ptr, addr2_ptr)                                              \
513     *(((uint16_t*)(addr1_ptr)) + 0) = *(((uint16_t*)(addr2_ptr)) + 0);                  \
514     *(((uint16_t*)(addr1_ptr)) + 1) = *(((uint16_t*)(addr2_ptr)) + 1);                  \
515     *(((uint16_t*)(addr1_ptr)) + 2) = *(((uint16_t*)(addr2_ptr)) + 2)
516 
517 /**
518  ****************************************************************************************
519  * Compare two SSID.
520  * @param[in] ssid1_ptr Pointer to the first SSID structure.
521  * @param[in] ssid2_ptr Pointer to the second SSID structure.
522  * @return True if equal, false if not.
523  ****************************************************************************************
524  */
525 #define MAC_SSID_CMP(ssid1_ptr,ssid2_ptr)                                               \
526     (((ssid1_ptr)->length == (ssid2_ptr)->length) &&                                    \
527      (memcmp((&(ssid1_ptr)->array[0]), (&(ssid2_ptr)->array[0]), (ssid1_ptr)->length) == 0))
528 
529 /**
530  ****************************************************************************************
531  * Check if MAC address is a group address: test the multicast bit.
532  * @param[in] mac_addr_ptr Pointer to the MAC address to be checked.
533  * @return 0 if unicast address, 1 otherwise
534  ****************************************************************************************
535  */
536 #define MAC_ADDR_GROUP(mac_addr_ptr) ((*((uint8_t *)(mac_addr_ptr))) & 1)
537 
538 #define MAC_ADDR_VALID(mac_addr_ptr) (mac_addr_ptr[0] | mac_addr_ptr[1] | mac_addr_ptr[2] | mac_addr_ptr[3] | mac_addr_ptr[4] | mac_addr_ptr[5])
539 /// MAC HT operation element
540 struct mac_htoprnelmt
541 {
542     /// Primary channel information
543     uint8_t     prim_channel;
544     /// HT operation information 1
545     uint8_t     ht_oper_1;
546     /// HT operation information 2
547     uint16_t    ht_oper_2;
548     /// HT operation information 3
549     uint16_t    ht_oper_3;
550     /// Basic MCS set
551     uint8_t     mcs_rate[MAX_MCS_LEN];
552 
553 };
554 
555 /// MU EDCA Parameter Set Element
556 struct mac_mu_edca_param_set
557 {
558     /// Per-AC MU EDCA parameters
559     uint32_t        ac_param[AC_MAX];
560     /// QoS information
561     uint8_t         qos_info;
562     /// Parameter set counter
563     uint8_t         param_set_cnt;
564 };
565 
566 /// EDCA Parameter Set Element
567 struct mac_edca_param_set
568 {
569     /// Per-AC EDCA parameters
570     uint32_t        ac_param[AC_MAX];
571     /// QoS information
572     uint8_t         qos_info;
573     /// Admission Control Mandatory bitfield
574     uint8_t         acm;
575     /// Parameter set counter
576     uint8_t         param_set_cnt;
577 };
578 
579 /*
580  * GLOBAL VARIABLES
581  ****************************************************************************************
582  */
583 /// Array converting a TID to its associated AC
584 extern const uint8_t mac_tid2ac[];
585 
586 /// Array converting an AC to its corresponding bit in the QoS Information field
587 extern const uint8_t mac_ac2uapsd[];
588 
589 /// Array converting an ACI to its corresponding AC. See Table 9-136 in 802.11-2016
590 extern const uint8_t mac_aci2ac[];
591 
592 /// Array converting an AC to its corresponding ACI. See Table 9-136 in 802.11-2016
593 extern const uint8_t mac_ac2aci[];
594 
595 /// Array converting a MAC HW rate id into its corresponding standard rate value
596 extern const uint8_t mac_id2rate[];
597 
598 /// Constant value corresponding to the Broadcast MAC address
599 extern const struct mac_addr mac_addr_bcst;
600 
601 
602 /**
603  ****************************************************************************************
604  * Get the value of a bit field in the HE MAC capability element.
605  * @param[in] he_cap Pointer to the HE capability structure
606  * @param[in] field  Bit field to be read
607  * @return The value of the field
608  ****************************************************************************************
609  */
610 #define HE_MAC_CAPA_VAL_GET(he_cap, field)    co_val_get((he_cap)->mac_cap_info,         \
611                                                          HE_MAC_CAPA_##field##_OFT,      \
612                                                          HE_MAC_CAPA_##field##_WIDTH)
613 
614 /**
615  ****************************************************************************************
616  * Set the value of a bit field in the HE MAC capability element.
617  * @param[in] he_cap Pointer to the HE capability structure
618  * @param[in] field Bit field to be written
619  * @param[in] val The value of the field
620  ****************************************************************************************
621  */
622 #define HE_MAC_CAPA_VAL_SET(he_cap, field, val) co_val_set((he_cap)->mac_cap_info,       \
623                                                            HE_MAC_CAPA_##field##_OFT,    \
624                                                            HE_MAC_CAPA_##field##_WIDTH,  \
625                                                            HE_MAC_CAPA_##field##_##val)
626 
627 /**
628  ****************************************************************************************
629  * Test if a bit in the HE MAC capability element is set.
630  * @param[in] he_cap Pointer to the HE capability structure
631  * @param[in] bit    Bit to be set
632  * @return true if set, false otherwise
633  ****************************************************************************************
634  */
635 #define HE_MAC_CAPA_BIT_IS_SET(he_cap, bit)   co_bit_is_set((he_cap)->mac_cap_info,      \
636                                                             HE_MAC_CAPA_##bit##_POS)
637 
638 /**
639  ****************************************************************************************
640  * Set a bit in the HE MAC capability element.
641  * @param[in] he_cap Pointer to the HE capability structure
642  * @param[in] bit    Bit to be set
643  ****************************************************************************************
644  */
645 #define HE_MAC_CAPA_BIT_SET(he_cap, bit)      co_bit_set((he_cap)->mac_cap_info,         \
646                                                           HE_MAC_CAPA_##bit##_POS)
647 
648 /**
649  ****************************************************************************************
650  * Clear a bit in the HE MAC capability element.
651  * @param[in] he_cap Pointer to the HE capability structure
652  * @param[in] bit    Bit to be cleared
653  ****************************************************************************************
654  */
655 #define HE_MAC_CAPA_BIT_CLR(he_cap, bit)      co_bit_clr((he_cap)->mac_cap_info,         \
656                                                          HE_MAC_CAPA_##bit##_POS)
657 
658 /**
659  ****************************************************************************************
660  * Get the value of a bit field in the HE PHY capability element.
661  * @param[in] he_cap Pointer to the HE capability structure
662  * @param[in] field  Bit field to be read
663  * @return The value of the field
664  ****************************************************************************************
665  */
666 #define HE_PHY_CAPA_VAL_GET(he_cap, field)    co_val_get((he_cap)->phy_cap_info,         \
667                                                          HE_PHY_CAPA_##field##_OFT,      \
668                                                          HE_PHY_CAPA_##field##_WIDTH)
669 
670 /**
671  ****************************************************************************************
672  * Set the value of a bit field in the HE PHY capability element.
673  * @param[in] he_cap Pointer to the HE capability structure
674  * @param[in] field Bit field to be written
675  * @param[in] val The value of the field
676  ****************************************************************************************
677  */
678 #define HE_PHY_CAPA_VAL_SET(he_cap, field, val) co_val_set((he_cap)->phy_cap_info,       \
679                                                            HE_PHY_CAPA_##field##_OFT,    \
680                                                            HE_PHY_CAPA_##field##_WIDTH,  \
681                                                            HE_PHY_CAPA_##field##_##val)
682 
683 /**
684  ****************************************************************************************
685  * Test if a bit in the HE PHY capability element is set.
686  * @param[in] he_cap Pointer to the HE capability structure
687  * @param[in] bit    Bit to be set
688  * @return true if set, false otherwise
689  ****************************************************************************************
690  */
691 #define HE_PHY_CAPA_BIT_IS_SET(he_cap, bit)   co_bit_is_set((he_cap)->phy_cap_info,      \
692                                                             HE_PHY_CAPA_##bit##_POS)
693 
694 /**
695  ****************************************************************************************
696  * Set a bit in the HE PHY capability element.
697  * @param[in] he_cap Pointer to the HE capability structure
698  * @param[in] bit    Bit to be set
699  ****************************************************************************************
700  */
701 #define HE_PHY_CAPA_BIT_SET(he_cap, bit)      co_bit_set((he_cap)->phy_cap_info,         \
702                                                          HE_PHY_CAPA_##bit##_POS)
703 
704 /**
705  ****************************************************************************************
706  * Clear a bit in the HE PHY capability element.
707  * @param[in] he_cap Pointer to the HE capability structure
708  * @param[in] bit    Bit to be cleared
709  ****************************************************************************************
710  */
711 #define HE_PHY_CAPA_BIT_CLR(he_cap, bit)      co_bit_clr((he_cap)->phy_cap_info,         \
712                                                          HE_PHY_CAPA_##bit##_POS)
713 
714 
715 /* Sync with llc.h */
716 
717 /** @name LLC field definitions
718  * LLC = DSAP + SSAP + CTRL
719  * @note: The 0xAA values indicate the presence of the SNAP
720  */
721 #define LLC_LLC_LEN                  3
722 #define LLC_DSAP                     0xAA
723 #define LLC_SSAP                     0xAA
724 #define LLC_CTRL                     0x03
725 
726 /** @name SNAP field definitions
727  * SNAP = OUI + ETHER_TYPE
728  */
729 #define LLC_SNAP_LEN                 5
730 #define LLC_OUI_LEN                  3
731 
732 /** @name 802.2 Header definitions
733  * The 802.2 Header is the LLC + the SNAP
734  */
735 #define LLC_802_2_HDR_LEN            (LLC_LLC_LEN + LLC_SNAP_LEN)
736 
737 
738 /** @name 802.2 Ethernet definitions */
739 /// Ethernet MTU
740 #define LLC_ETHER_MTU                1500
741 /// Size of the Ethernet header
742 #define LLC_ETHER_HDR_LEN            14
743 /// Size of the Ethernet type
744 #define LLC_ETHERTYPE_LEN            2
745 /// Offset of the Ethernet type within the header
746 #define LLC_ETHERTYPE_LEN_OFT        12
747 /// Offset of the EtherType in the 802.2 header
748 #define LLC_802_2_ETHERTYPE_OFT      6
749 /// Maximum value of the frame length
750 #define LLC_FRAMELENGTH_MAXVALUE     0x600
751 
752 /** @name Useful EtherType values */
753 #define LLC_ETHERTYPE_APPLETALK      0x809B
754 #define LLC_ETHERTYPE_IPX            0x8137
755 #define LLC_ETHERTYPE_AARP           0x80F3
756 #define LLC_ETHERTYPE_EAP_T          0x888E
757 #define LLC_ETHERTYPE_802_1Q         0x8100
758 #define LLC_ETHERTYPE_IP             0x0800
759 #define LLC_ETHERTYPE_ARP            0x0806
760 
761 /** @name 802_1Q VLAN header definitions */
762 #define LLC_802_1Q_TCI_VID_MASK       0x0FFF
763 #define LLC_802_1Q_TCI_PRI_MASK       0xE000
764 #define LLC_802_1Q_TCI_CFI_MASK       0x1000
765 #define LLC_802_1Q_HDR_LEN            4
766 #define LLC_802_1Q_VID_NULL           0
767 #define LLC_OFFSET_ETHERTYPE          12
768 #define LLC_OFFSET_IPV4_PRIO          15
769 #define LLC_OFFSET_802_1Q_TCI         14
770 /// @}
771 
772 
773 /*
774  * TYPES DEFINITION
775  ****************************************************************************************
776  */
777 
778 /// LLC/SNAP structure
779 struct llc_snap_short
780 {
781     /// DSAP + SSAP fieldsr
782     uint16_t dsap_ssap;
783     /// LLC control + OUI byte 0
784     uint16_t control_oui0;
785     /// OUI bytes 1 and 2
786     uint16_t oui1_2;
787 };
788 
789 /// LLC/SNAP structure
790 struct llc_snap
791 {
792     /// DSAP + SSAP fieldsr
793     uint16_t dsap_ssap;
794     /// LLC control + OUI byte 0
795     uint16_t control_oui0;
796     /// OUI bytes 1 and 2
797     uint16_t oui1_2;
798     /// Protocol
799     uint16_t proto_id;
800 };
801 
802 
803 /* Sync with phy.h */
804 
805 /// 5G lower bound freq
806 #define PHY_FREQ_5G 5000
807 
808 /// Macro retrieving the channel of the phy channel info
809 /// @param[in] __x phy channel info 1 value.
810 #define PHY_INFO_CHAN(__x) (((__x.info1) & 0xFFFF0000) >> 16)
811 /// Macro retrieving the HT length of the RX vector
812 /// @param[in] __x RX bytes 12 - 9 of Receive Vector 1.
813 #define RXVEC_HTLENGTH_0(__x)    (((__x) & 0x0000FFFF))
814 /// Macro retrieving the modulation format of the RX vector
815 /// @param[in] __x bytes 4 - 1 of Receive Vector 1.
816 #define RXVEC_FORMATMOD(__x)   (((__x) & 0x0000000F))
817 
818 /**
819  ****************************************************************************************
820  * @brief Compute the channel number from its center frequency and its band
821  * @param[in] band  RF band (must be @ref PHY_BAND_2G4 or @ref PHY_BAND_5G)
822  * @param[in] freq  Center frequency of the channel
823  *
824  * @return The channel number
825  ****************************************************************************************
826  */
phy_freq_to_channel(uint8_t band,uint16_t freq)827 __STATIC_INLINE int phy_freq_to_channel(uint8_t band, uint16_t freq)
828 {
829     int channel = 0;
830 
831     do
832     {
833         //2.4.GHz
834         if (band == PHY_BAND_2G4)
835         {
836             // Check if frequency is in the expected range
837             if ((freq < 2412) || (freq > 2484))
838                 break;
839 
840             // Compute the channel number
841             if (freq == 2484)
842                 channel = 14;
843             else
844                 channel = (freq - 2407)/5;
845         }
846         //5 GHz
847         else if (band == PHY_BAND_5G)
848         {
849             // Check if frequency is in the expected range
850             if ((freq < 5005) || (freq > 5825))
851                 break;
852 
853             // Compute the channel number
854             channel = (freq - PHY_FREQ_5G)/5;
855         }
856     } while(0);
857 
858     return (channel);
859 }
860 
phy_channel_to_freq(uint8_t band,int channel)861 __STATIC_INLINE uint16_t phy_channel_to_freq(uint8_t band, int channel)
862 {
863     if ((band == PHY_BAND_2G4) && (channel >= 1) && (channel <= 14))
864     {
865         if (channel == 14)
866             return 2484;
867         else
868             return 2407 + channel * 5;
869     }
870     else if ((band == PHY_BAND_5G) && (channel >= 1) && (channel <= 165))
871     {
872         return PHY_FREQ_5G + channel * 5;
873     }
874     return 0;
875 }
876 
877 #endif // _WIFI_MAC_H
878