Skip to content

Commit 6e8102e

Browse files
committed
crda: make reglib_for_each_country() use the reglib context
This allows users of reglib to iterate over the regdb with just one open() and mmap() to be kept sharing as much code as possible. This makes the regdbdump and intersection code use the context therefore sharing all that boiler plate code. Signed-off-by: Luis R. Rodriguez <mcgrof@do-not-panic.com>
1 parent 1c2fb47 commit 6e8102e

File tree

4 files changed

+42
-35
lines changed

4 files changed

+42
-35
lines changed

intersect.c

+13-2
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,32 @@
88

99
int main(int argc, char **argv)
1010
{
11+
const struct reglib_regdb_ctx *ctx;
1112
const struct ieee80211_regdomain *rd;
1213

1314
if (argc != 2) {
14-
fprintf(stderr, "You must specify a file\n");
15+
fprintf(stderr, "Usage: %s <regulatory-binary-file>\n", argv[0]);
1516
return -EINVAL;
1617
}
1718

18-
rd = reglib_intersect_regdb(argv[1]);
19+
ctx = reglib_malloc_regdb_ctx(argv[1]);
20+
if (!ctx) {
21+
fprintf(stderr, "Invalid or empty regulatory file, note: "
22+
"a binary regulatory file should be used.\n");
23+
return -EINVAL;
24+
}
25+
26+
rd = reglib_intersect_regdb(ctx);
1927
if (!rd) {
2028
fprintf(stderr, "Intersection not possible\n");
29+
reglib_free_regdb_ctx(ctx);
2130
return -ENOENT;
2231
}
2332

2433
reglib_print_regdom(rd);
34+
2535
free((struct ieee80211_regdomain *) rd);
2636

37+
reglib_free_regdb_ctx(ctx);
2738
return 0;
2839
}

regdbdump.c

+14-14
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,35 @@
22
#include <errno.h>
33
#include "reglib.h"
44

5-
static int reglib_regdbdump(char *regdb_file)
5+
static void reglib_regdbdump(const struct reglib_regdb_ctx *ctx)
66
{
77
const struct ieee80211_regdomain *rd = NULL;
88
unsigned int idx = 0;
99

10-
reglib_for_each_country(rd, idx, regdb_file) {
10+
reglib_for_each_country(rd, idx, ctx) {
1111
reglib_print_regdom(rd);
1212
free((struct ieee80211_regdomain *) rd);
1313
}
14-
15-
if (!idx) {
16-
fprintf(stderr, "Invalid or empty regulatory file, note: "
17-
"a binary regulatory file should be used.\n");
18-
return -EINVAL;
19-
}
20-
21-
return 0;
2214
}
2315

2416
int main(int argc, char **argv)
2517
{
26-
int r;
18+
const struct reglib_regdb_ctx *ctx;
2719

2820
if (argc != 2) {
2921
fprintf(stderr, "Usage: %s <regulatory-binary-file>\n", argv[0]);
30-
return 2;
22+
return -EINVAL;
23+
}
24+
25+
ctx = reglib_malloc_regdb_ctx(argv[1]);
26+
if (!ctx) {
27+
fprintf(stderr, "Invalid or empty regulatory file, note: "
28+
"a binary regulatory file should be used.\n");
29+
return -EINVAL;
3130
}
3231

33-
r = reglib_regdbdump(argv[1]);
32+
reglib_regdbdump(ctx);
33+
reglib_free_regdb_ctx(ctx);
3434

35-
return r;
35+
return 0;
3636
}

reglib.c

+9-14
Original file line numberDiff line numberDiff line change
@@ -336,28 +336,19 @@ country2rd(const struct reglib_regdb_ctx *ctx,
336336
}
337337

338338
const struct ieee80211_regdomain *
339-
reglib_get_rd_idx(unsigned int idx, const char *file)
339+
reglib_get_rd_idx(unsigned int idx, const struct reglib_regdb_ctx *ctx)
340340
{
341-
const struct reglib_regdb_ctx *ctx;
342341
struct regdb_file_reg_country *country;
343-
const struct ieee80211_regdomain *rd = NULL;
344342

345-
ctx = reglib_malloc_regdb_ctx(file);
346343
if (!ctx)
347344
return NULL;
348345

349346
if (idx >= ctx->num_countries)
350-
goto out;
347+
return NULL;
351348

352349
country = ctx->countries + idx;
353350

354-
rd = country2rd(ctx, country);
355-
if (!rd)
356-
goto out;
357-
358-
out:
359-
reglib_free_regdb_ctx(ctx);
360-
return rd;
351+
return country2rd(ctx, country);
361352
}
362353

363354
const struct ieee80211_regdomain *
@@ -552,14 +543,18 @@ reglib_intersect_rds(const struct ieee80211_regdomain *rd1,
552543
return rd;
553544
}
554545

555-
const struct ieee80211_regdomain *reglib_intersect_regdb(char *regdb_file)
546+
const struct ieee80211_regdomain *
547+
reglib_intersect_regdb(const struct reglib_regdb_ctx *ctx)
556548
{
557549
const struct ieee80211_regdomain *rd;
558550
struct ieee80211_regdomain *prev_rd_intsct = NULL, *rd_intsct = NULL;
559551
int intersected = 0;
560552
unsigned int idx = 0;
561553

562-
reglib_for_each_country(rd, idx, regdb_file) {
554+
if (!ctx)
555+
return NULL;
556+
557+
reglib_for_each_country(rd, idx, ctx) {
563558
if (reglib_is_world_regdom((const char *) rd->alpha2)) {
564559
free((struct ieee80211_regdomain *) rd);
565560
continue;

reglib.h

+6-5
Original file line numberDiff line numberDiff line change
@@ -139,12 +139,12 @@ const struct reglib_regdb_ctx *reglib_malloc_regdb_ctx(const char *regdb_file);
139139
void reglib_free_regdb_ctx(const struct reglib_regdb_ctx *regdb_ctx);
140140

141141
const struct ieee80211_regdomain *
142-
reglib_get_rd_idx(unsigned int idx, const char *file);
142+
reglib_get_rd_idx(unsigned int idx, const struct reglib_regdb_ctx *ctx);
143143

144-
#define reglib_for_each_country(__rd, __idx, __file) \
145-
for (__rd = reglib_get_rd_idx(__idx, __file); \
144+
#define reglib_for_each_country(__rd, __idx, __ctx) \
145+
for (__rd = reglib_get_rd_idx(__idx, __ctx); \
146146
__rd != NULL; \
147-
__rd = reglib_get_rd_idx(++__idx, __file)) \
147+
__rd = reglib_get_rd_idx(++__idx, __ctx)) \
148148

149149
const struct ieee80211_regdomain *
150150
reglib_get_rd_alpha2(const char *alpha2, const char *file);
@@ -166,6 +166,7 @@ reglib_intersect_rds(const struct ieee80211_regdomain *rd1,
166166
* to find rules that fit all regulatory domains it return a regulatory
167167
* domain with such rules otherwise it returns NULL.
168168
*/
169-
const struct ieee80211_regdomain *reglib_intersect_regdb(char *regdb_file);
169+
const struct ieee80211_regdomain *
170+
reglib_intersect_regdb(const struct reglib_regdb_ctx *ctx);
170171

171172
#endif

0 commit comments

Comments
 (0)