Add conversion utitlies, standardize printing
[platform/upstream/crda.git] / reglib.h
1 #ifndef REG_LIB_H
2 #define REG_LIB_H
3
4 #include <stdlib.h>
5 #include <linux/types.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         __u32 start_freq_khz;
14         __u32 end_freq_khz;
15         __u32 max_bandwidth_khz;
16 };
17
18 struct ieee80211_power_rule {
19         __u32 max_antenna_gain;
20         __u32 max_eirp;
21 };
22
23 struct ieee80211_reg_rule {
24         struct ieee80211_freq_range freq_range;
25         struct ieee80211_power_rule power_rule;
26         __u32 flags;
27 };
28
29 struct ieee80211_regdomain {
30         __u32 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 /* Avoid stdlib */
57 static inline int is_len_2(const char *alpha2)
58 {
59         if (alpha2[0] == '\0' || (alpha2[1] == '\0'))
60                 return 0;
61         if (alpha2[2] == '\0')
62                 return 1;
63         return 0;
64 }
65
66 static inline int is_valid_regdom(const char *alpha2)
67 {
68         if (!is_len_2(alpha2))
69                 return 0;
70
71         if (!is_alpha2(alpha2) && !is_world_regdom(alpha2))
72                 return 0;
73
74         return 1;
75 }
76
77 static inline __u32 max(__u32 a, __u32 b)
78 {
79         return (a > b) ? a : b;
80 }
81
82 static inline __u32 min(__u32 a, __u32 b)
83 {
84         return (a > b) ? b : a;
85 }
86
87 void *crda_get_file_ptr(__u8 *db, int dblen, int structlen, __be32 ptr);
88 int crda_verify_db_signature(__u8 *db, int dblen, int siglen);
89
90 /* File reg db entry -> rd converstion utilities */
91 void reg_rule2rd(__u8 *db, int dblen,
92         __be32 ruleptr, struct ieee80211_reg_rule *rd_reg_rule);
93 int country2rd(__u8 *db, int dblen,
94         struct regdb_file_reg_country *country,
95         struct ieee80211_regdomain **rdp);
96
97 /* reg helpers */
98 int is_valid_reg_rule(const struct ieee80211_reg_rule *rule);
99 void print_reg_rule(struct ieee80211_reg_rule *rule);
100 void print_regdom(struct ieee80211_regdomain *rd);
101
102 #endif