42c79998308d1e93ecadce6a8ff2f204548aa2fb
[platform/upstream/crda.git] / reglib.h
1 #ifndef REG_LIB_H
2 #define REG_LIB_H
3
4 #include <stdlib.h>
5 #include <stdint.h>
6
7 #include "regdb.h"
8
9 /* Common regulatory structures, functions and helpers */
10
11 /* This matches the kernel's data structures */
12 struct ieee80211_freq_range {
13         uint32_t start_freq_khz;
14         uint32_t end_freq_khz;
15         uint32_t max_bandwidth_khz;
16 };
17
18 struct ieee80211_power_rule {
19         uint32_t max_antenna_gain;
20         uint32_t max_eirp;
21 };
22
23 struct ieee80211_reg_rule {
24         struct ieee80211_freq_range freq_range;
25         struct ieee80211_power_rule power_rule;
26         uint32_t flags;
27 };
28
29 struct ieee80211_regdomain {
30         uint32_t n_reg_rules;
31         char alpha2[2];
32         uint8_t dfs_region;
33         struct ieee80211_reg_rule reg_rules[];
34 };
35
36 static inline int is_world_regdom(const char *alpha2)
37 {
38         if (alpha2[0] == '0' && alpha2[1] == '0')
39                 return 1;
40         return 0;
41 }
42
43 static inline int isalpha_upper(char letter)
44 {
45         if (letter >= 'A' && letter <= 'Z')
46                 return 1;
47         return 0;
48 }
49
50 static inline int is_alpha2(const char *alpha2)
51 {
52         if (isalpha_upper(alpha2[0]) && isalpha_upper(alpha2[1]))
53                 return 1;
54         return 0;
55 }
56
57 static inline int is_valid_regdom(const char *alpha2)
58 {
59         if (!is_alpha2(alpha2) && !is_world_regdom(alpha2))
60                 return 0;
61
62         return 1;
63 }
64
65 static inline uint32_t max(uint32_t a, uint32_t b)
66 {
67         return (a > b) ? a : b;
68 }
69
70 static inline uint32_t min(uint32_t a, uint32_t b)
71 {
72         return (a > b) ? b : a;
73 }
74
75 void *crda_get_file_ptr(uint8_t *db, int dblen, int structlen, uint32_t ptr);
76 int crda_verify_db_signature(uint8_t *db, int dblen, int siglen);
77
78 struct ieee80211_regdomain *
79 reglib_get_country_idx(unsigned int idx, const char *file);
80
81 #define reglib_for_each_country(__rd, __idx, __file)                    \
82         for (__rd = reglib_get_country_idx(__idx, __file);              \
83              __rd != NULL;                                              \
84              __rd = reglib_get_country_idx(__idx, __file),              \
85              __idx++)
86
87 struct ieee80211_regdomain *
88 reglib_get_country_alpha2(const char *alpha2, const char *file);
89
90 /* reg helpers */
91 void print_regdom(struct ieee80211_regdomain *rd);
92
93 #endif