Commit 6e8102e3 authored by Luis R. Rodriguez's avatar Luis R. Rodriguez

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: 's avatarLuis R. Rodriguez <mcgrof@do-not-panic.com>
parent 1c2fb479
......@@ -8,21 +8,32 @@
int main(int argc, char **argv)
{
const struct reglib_regdb_ctx *ctx;
const struct ieee80211_regdomain *rd;
if (argc != 2) {
fprintf(stderr, "You must specify a file\n");
fprintf(stderr, "Usage: %s <regulatory-binary-file>\n", argv[0]);
return -EINVAL;
}
rd = reglib_intersect_regdb(argv[1]);
ctx = reglib_malloc_regdb_ctx(argv[1]);
if (!ctx) {
fprintf(stderr, "Invalid or empty regulatory file, note: "
"a binary regulatory file should be used.\n");
return -EINVAL;
}
rd = reglib_intersect_regdb(ctx);
if (!rd) {
fprintf(stderr, "Intersection not possible\n");
reglib_free_regdb_ctx(ctx);
return -ENOENT;
}
reglib_print_regdom(rd);
free((struct ieee80211_regdomain *) rd);
reglib_free_regdb_ctx(ctx);
return 0;
}
......@@ -2,35 +2,35 @@
#include <errno.h>
#include "reglib.h"
static int reglib_regdbdump(char *regdb_file)
static void reglib_regdbdump(const struct reglib_regdb_ctx *ctx)
{
const struct ieee80211_regdomain *rd = NULL;
unsigned int idx = 0;
reglib_for_each_country(rd, idx, regdb_file) {
reglib_for_each_country(rd, idx, ctx) {
reglib_print_regdom(rd);
free((struct ieee80211_regdomain *) rd);
}
if (!idx) {
fprintf(stderr, "Invalid or empty regulatory file, note: "
"a binary regulatory file should be used.\n");
return -EINVAL;
}
return 0;
}
int main(int argc, char **argv)
{
int r;
const struct reglib_regdb_ctx *ctx;
if (argc != 2) {
fprintf(stderr, "Usage: %s <regulatory-binary-file>\n", argv[0]);
return 2;
return -EINVAL;
}
ctx = reglib_malloc_regdb_ctx(argv[1]);
if (!ctx) {
fprintf(stderr, "Invalid or empty regulatory file, note: "
"a binary regulatory file should be used.\n");
return -EINVAL;
}
r = reglib_regdbdump(argv[1]);
reglib_regdbdump(ctx);
reglib_free_regdb_ctx(ctx);
return r;
return 0;
}
......@@ -336,28 +336,19 @@ country2rd(const struct reglib_regdb_ctx *ctx,
}
const struct ieee80211_regdomain *
reglib_get_rd_idx(unsigned int idx, const char *file)
reglib_get_rd_idx(unsigned int idx, const struct reglib_regdb_ctx *ctx)
{
const struct reglib_regdb_ctx *ctx;
struct regdb_file_reg_country *country;
const struct ieee80211_regdomain *rd = NULL;
ctx = reglib_malloc_regdb_ctx(file);
if (!ctx)
return NULL;
if (idx >= ctx->num_countries)
goto out;
return NULL;
country = ctx->countries + idx;
rd = country2rd(ctx, country);
if (!rd)
goto out;
out:
reglib_free_regdb_ctx(ctx);
return rd;
return country2rd(ctx, country);
}
const struct ieee80211_regdomain *
......@@ -552,14 +543,18 @@ reglib_intersect_rds(const struct ieee80211_regdomain *rd1,
return rd;
}
const struct ieee80211_regdomain *reglib_intersect_regdb(char *regdb_file)
const struct ieee80211_regdomain *
reglib_intersect_regdb(const struct reglib_regdb_ctx *ctx)
{
const struct ieee80211_regdomain *rd;
struct ieee80211_regdomain *prev_rd_intsct = NULL, *rd_intsct = NULL;
int intersected = 0;
unsigned int idx = 0;
reglib_for_each_country(rd, idx, regdb_file) {
if (!ctx)
return NULL;
reglib_for_each_country(rd, idx, ctx) {
if (reglib_is_world_regdom((const char *) rd->alpha2)) {
free((struct ieee80211_regdomain *) rd);
continue;
......
......@@ -139,12 +139,12 @@ const struct reglib_regdb_ctx *reglib_malloc_regdb_ctx(const char *regdb_file);
void reglib_free_regdb_ctx(const struct reglib_regdb_ctx *regdb_ctx);
const struct ieee80211_regdomain *
reglib_get_rd_idx(unsigned int idx, const char *file);
reglib_get_rd_idx(unsigned int idx, const struct reglib_regdb_ctx *ctx);
#define reglib_for_each_country(__rd, __idx, __file) \
for (__rd = reglib_get_rd_idx(__idx, __file); \
#define reglib_for_each_country(__rd, __idx, __ctx) \
for (__rd = reglib_get_rd_idx(__idx, __ctx); \
__rd != NULL; \
__rd = reglib_get_rd_idx(++__idx, __file)) \
__rd = reglib_get_rd_idx(++__idx, __ctx)) \
const struct ieee80211_regdomain *
reglib_get_rd_alpha2(const char *alpha2, const char *file);
......@@ -166,6 +166,7 @@ reglib_intersect_rds(const struct ieee80211_regdomain *rd1,
* to find rules that fit all regulatory domains it return a regulatory
* domain with such rules otherwise it returns NULL.
*/
const struct ieee80211_regdomain *reglib_intersect_regdb(char *regdb_file);
const struct ieee80211_regdomain *
reglib_intersect_regdb(const struct reglib_regdb_ctx *ctx);
#endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment