ef85fac016f93954fb833717e4352ed9fa65d005
[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         struct ieee80211_reg_rule reg_rules[];
33 };
34
35 static inline int is_world_regdom(const char *alpha2)
36 {
37         if (alpha2[0] == '0' && alpha2[1] == '0')
38                 return 1;
39         return 0;
40 }
41
42 static inline int isalpha_upper(char letter)
43 {
44         if (letter >= 'A' && letter <= 'Z')
45                 return 1;
46         return 0;
47 }
48
49 static inline int is_alpha2(const char *alpha2)
50 {
51         if (isalpha_upper(alpha2[0]) && isalpha_upper(alpha2[1]))
52                 return 1;
53         return 0;
54 }
55
56 static inline int is_valid_regdom(const char *alpha2)
57 {
58         if (!is_alpha2(alpha2) && !is_world_regdom(alpha2))
59                 return 0;
60
61         return 1;
62 }
63
64 static inline uint32_t max(uint32_t a, uint32_t b)
65 {
66         return (a > b) ? a : b;
67 }
68
69 static inline uint32_t min(uint32_t a, uint32_t b)
70 {
71         return (a > b) ? b : a;
72 }
73
74 void *crda_get_file_ptr(uint8_t *db, int dblen, int structlen, uint32_t ptr);
75 int crda_verify_db_signature(uint8_t *db, int dblen, int siglen);
76
77 /* File reg db entry -> rd converstion utilities */
78 struct ieee80211_regdomain *country2rd(uint8_t *db, int dblen,
79         struct regdb_file_reg_country *country);
80
81 struct ieee80211_regdomain *
82 reglib_get_country_idx(unsigned int idx, const char *file);
83
84 #define reglib_for_each_country(__rd, __idx, __file)                    \
85         for (__rd = reglib_get_country_idx(__idx, __file);              \
86              __rd != NULL;                                              \
87              __rd = reglib_get_country_idx(__idx, __file),              \
88              __idx++)
89
90 /* reg helpers */
91 void print_regdom(struct ieee80211_regdomain *rd);
92
93 #endif