linux-wireless.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Luis R. Rodriguez" <mcgrof@do-not-panic.com>
To: wireless-regdb@lists.infradead.org
Cc: linux-wireless@vger.kernel.org,
	"Luis R. Rodriguez" <mcgrof@do-not-panic.com>
Subject: [PATCH 06/40] crda: move regdom_intersect() to reglib
Date: Thu, 30 May 2013 19:08:55 -0700	[thread overview]
Message-ID: <1369966169-23640-7-git-send-email-mcgrof@do-not-panic.com> (raw)
In-Reply-To: <1369966169-23640-1-git-send-email-mcgrof@do-not-panic.com>

From: "Luis R. Rodriguez" <mcgrof@do-not-panic.com>

This will be used later by other code so just share it.

Signed-off-by: Luis R. Rodriguez <mcgrof@do-not-panic.com>
---
 intersect.c |  157 ----------------------------------------------------------
 reglib.c    |  159 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 reglib.h    |    3 ++
 3 files changed, 162 insertions(+), 157 deletions(-)

diff --git a/intersect.c b/intersect.c
index 7352e73..1c00a67 100644
--- a/intersect.c
+++ b/intersect.c
@@ -9,163 +9,6 @@
 /* Intersects regulatory domains, this will skip any regulatory marked with
  * an alpha2 of '00', which is used to indicate a regulatory domain */
 
-/* Sanity check on a regulatory rule */
-static int is_valid_reg_rule(const struct ieee80211_reg_rule *rule)
-{
-	const struct ieee80211_freq_range *freq_range = &rule->freq_range;
-	uint32_t freq_diff;
-
-	if (freq_range->start_freq_khz == 0 || freq_range->end_freq_khz == 0)
-		return 0;
-
-	if (freq_range->start_freq_khz > freq_range->end_freq_khz)
-		return 0;
-
-	freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
-
-	if (freq_range->end_freq_khz <= freq_range->start_freq_khz ||
-	    freq_range->max_bandwidth_khz > freq_diff)
-		return 0;
-
-	return 1;
-}
-
-/* Helper for regdom_intersect(), this does the real
- * mathematical intersection fun */
-static int reg_rules_intersect(const struct ieee80211_reg_rule *rule1,
-			       const struct ieee80211_reg_rule *rule2,
-			       struct ieee80211_reg_rule *intersected_rule)
-{
-	const struct ieee80211_freq_range *freq_range1, *freq_range2;
-	struct ieee80211_freq_range *freq_range;
-	const struct ieee80211_power_rule *power_rule1, *power_rule2;
-	struct ieee80211_power_rule *power_rule;
-	uint32_t freq_diff;
-
-	freq_range1 = &rule1->freq_range;
-	freq_range2 = &rule2->freq_range;
-	freq_range = &intersected_rule->freq_range;
-
-	power_rule1 = &rule1->power_rule;
-	power_rule2 = &rule2->power_rule;
-	power_rule = &intersected_rule->power_rule;
-
-	freq_range->start_freq_khz = max(freq_range1->start_freq_khz,
-					 freq_range2->start_freq_khz);
-	freq_range->end_freq_khz = min(freq_range1->end_freq_khz,
-				       freq_range2->end_freq_khz);
-	freq_range->max_bandwidth_khz = min(freq_range1->max_bandwidth_khz,
-					    freq_range2->max_bandwidth_khz);
-
-	freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
-	if (freq_range->max_bandwidth_khz > freq_diff)
-		freq_range->max_bandwidth_khz = freq_diff;
-
-	power_rule->max_eirp = min(power_rule1->max_eirp,
-		power_rule2->max_eirp);
-	power_rule->max_antenna_gain = min(power_rule1->max_antenna_gain,
-		power_rule2->max_antenna_gain);
-
-	intersected_rule->flags = rule1->flags | rule2->flags;
-
-	if (!is_valid_reg_rule(intersected_rule))
-		return -EINVAL;
-
-	return 0;
-}
-
-/**
- * regdom_intersect - do the intersection between two regulatory domains
- * @rd1: first regulatory domain
- * @rd2: second regulatory domain
- *
- * Use this function to get the intersection between two regulatory domains.
- * Once completed we will mark the alpha2 for the rd as intersected, "98",
- * as no one single alpha2 can represent this regulatory domain.
- *
- * Returns a pointer to the regulatory domain structure which will hold the
- * resulting intersection of rules between rd1 and rd2. We will
- * malloc() this structure for you.
- */
-static struct ieee80211_regdomain *
-regdom_intersect(const struct ieee80211_regdomain *rd1,
-		 const struct ieee80211_regdomain *rd2)
-{
-	int r, size_of_regd;
-	unsigned int x, y;
-	unsigned int num_rules = 0, rule_idx = 0;
-	const struct ieee80211_reg_rule *rule1, *rule2;
-	struct ieee80211_reg_rule *intersected_rule;
-	struct ieee80211_regdomain *rd;
-	/* This is just a dummy holder to help us count */
-	struct ieee80211_reg_rule irule;
-
-	/* Uses the stack temporarily for counter arithmetic */
-	intersected_rule = &irule;
-
-	memset(intersected_rule, 0, sizeof(struct ieee80211_reg_rule));
-
-	if (!rd1 || !rd2)
-		return NULL;
-
-	/* First we get a count of the rules we'll need, then we actually
-	 * build them. This is to so we can malloc() and free() a
-	 * regdomain once. The reason we use reg_rules_intersect() here
-	 * is it will return -EINVAL if the rule computed makes no sense.
-	 * All rules that do check out OK are valid. */
-
-	for (x = 0; x < rd1->n_reg_rules; x++) {
-		rule1 = &rd1->reg_rules[x];
-		for (y = 0; y < rd2->n_reg_rules; y++) {
-			rule2 = &rd2->reg_rules[y];
-			if (!reg_rules_intersect(rule1, rule2,
-					intersected_rule))
-				num_rules++;
-			memset(intersected_rule, 0,
-					sizeof(struct ieee80211_reg_rule));
-		}
-	}
-
-	if (!num_rules)
-		return NULL;
-
-	size_of_regd = sizeof(struct ieee80211_regdomain) +
-		((num_rules + 1) * sizeof(struct ieee80211_reg_rule));
-
-	rd = malloc(size_of_regd);
-	if (!rd)
-		return NULL;
-
-	memset(rd, 0, size_of_regd);
-
-	for (x = 0; x < rd1->n_reg_rules; x++) {
-		rule1 = &rd1->reg_rules[x];
-		for (y = 0; y < rd2->n_reg_rules; y++) {
-			rule2 = &rd2->reg_rules[y];
-			/* This time around instead of using the stack lets
-			 * write to the target rule directly saving ourselves
-			 * a memcpy() */
-			intersected_rule = &rd->reg_rules[rule_idx];
-			r = reg_rules_intersect(rule1, rule2,
-				intersected_rule);
-			if (r)
-				continue;
-			rule_idx++;
-		}
-	}
-
-	if (rule_idx != num_rules) {
-		free(rd);
-		return NULL;
-	}
-
-	rd->n_reg_rules = num_rules;
-	rd->alpha2[0] = '9';
-	rd->alpha2[1] = '9';
-
-	return rd;
-}
-
 int main(int argc, char **argv)
 {
 	int r = 0;
diff --git a/reglib.c b/reglib.c
index 0b1599b..867e8cf 100644
--- a/reglib.c
+++ b/reglib.c
@@ -359,3 +359,162 @@ out:
 	close(fd);
 	return rd;
 }
+
+/* Sanity check on a regulatory rule */
+static int is_valid_reg_rule(const struct ieee80211_reg_rule *rule)
+{
+	const struct ieee80211_freq_range *freq_range = &rule->freq_range;
+	uint32_t freq_diff;
+
+	if (freq_range->start_freq_khz == 0 || freq_range->end_freq_khz == 0)
+		return 0;
+
+	if (freq_range->start_freq_khz > freq_range->end_freq_khz)
+		return 0;
+
+	freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
+
+	if (freq_range->end_freq_khz <= freq_range->start_freq_khz ||
+	    freq_range->max_bandwidth_khz > freq_diff)
+		return 0;
+
+	return 1;
+}
+
+/*
+ * Helper for regdom_intersect(), this does the real
+ * mathematical intersection fun
+ */
+static int reg_rules_intersect(const struct ieee80211_reg_rule *rule1,
+			       const struct ieee80211_reg_rule *rule2,
+			       struct ieee80211_reg_rule *intersected_rule)
+{
+	const struct ieee80211_freq_range *freq_range1, *freq_range2;
+	struct ieee80211_freq_range *freq_range;
+	const struct ieee80211_power_rule *power_rule1, *power_rule2;
+	struct ieee80211_power_rule *power_rule;
+	uint32_t freq_diff;
+
+	freq_range1 = &rule1->freq_range;
+	freq_range2 = &rule2->freq_range;
+	freq_range = &intersected_rule->freq_range;
+
+	power_rule1 = &rule1->power_rule;
+	power_rule2 = &rule2->power_rule;
+	power_rule = &intersected_rule->power_rule;
+
+	freq_range->start_freq_khz = max(freq_range1->start_freq_khz,
+					 freq_range2->start_freq_khz);
+	freq_range->end_freq_khz = min(freq_range1->end_freq_khz,
+				       freq_range2->end_freq_khz);
+	freq_range->max_bandwidth_khz = min(freq_range1->max_bandwidth_khz,
+					    freq_range2->max_bandwidth_khz);
+
+	freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
+	if (freq_range->max_bandwidth_khz > freq_diff)
+		freq_range->max_bandwidth_khz = freq_diff;
+
+	power_rule->max_eirp = min(power_rule1->max_eirp,
+		power_rule2->max_eirp);
+	power_rule->max_antenna_gain = min(power_rule1->max_antenna_gain,
+		power_rule2->max_antenna_gain);
+
+	intersected_rule->flags = rule1->flags | rule2->flags;
+
+	if (!is_valid_reg_rule(intersected_rule))
+		return -EINVAL;
+
+	return 0;
+}
+
+/**
+ * regdom_intersect - do the intersection between two regulatory domains
+ * @rd1: first regulatory domain
+ * @rd2: second regulatory domain
+ *
+ * Use this function to get the intersection between two regulatory domains.
+ * Once completed we will mark the alpha2 for the rd as intersected, "98",
+ * as no one single alpha2 can represent this regulatory domain.
+ *
+ * Returns a pointer to the regulatory domain structure which will hold the
+ * resulting intersection of rules between rd1 and rd2. We will
+ * malloc() this structure for you.
+ */
+struct ieee80211_regdomain *
+regdom_intersect(const struct ieee80211_regdomain *rd1,
+		 const struct ieee80211_regdomain *rd2)
+{
+	int r, size_of_regd;
+	unsigned int x, y;
+	unsigned int num_rules = 0, rule_idx = 0;
+	const struct ieee80211_reg_rule *rule1, *rule2;
+	struct ieee80211_reg_rule *intersected_rule;
+	struct ieee80211_regdomain *rd;
+	/* This is just a dummy holder to help us count */
+	struct ieee80211_reg_rule irule;
+
+	/* Uses the stack temporarily for counter arithmetic */
+	intersected_rule = &irule;
+
+	memset(intersected_rule, 0, sizeof(struct ieee80211_reg_rule));
+
+	if (!rd1 || !rd2)
+		return NULL;
+
+	/* First we get a count of the rules we'll need, then we actually
+	 * build them. This is to so we can malloc() and free() a
+	 * regdomain once. The reason we use reg_rules_intersect() here
+	 * is it will return -EINVAL if the rule computed makes no sense.
+	 * All rules that do check out OK are valid. */
+
+	for (x = 0; x < rd1->n_reg_rules; x++) {
+		rule1 = &rd1->reg_rules[x];
+		for (y = 0; y < rd2->n_reg_rules; y++) {
+			rule2 = &rd2->reg_rules[y];
+			if (!reg_rules_intersect(rule1, rule2,
+					intersected_rule))
+				num_rules++;
+			memset(intersected_rule, 0,
+					sizeof(struct ieee80211_reg_rule));
+		}
+	}
+
+	if (!num_rules)
+		return NULL;
+
+	size_of_regd = sizeof(struct ieee80211_regdomain) +
+		((num_rules + 1) * sizeof(struct ieee80211_reg_rule));
+
+	rd = malloc(size_of_regd);
+	if (!rd)
+		return NULL;
+
+	memset(rd, 0, size_of_regd);
+
+	for (x = 0; x < rd1->n_reg_rules; x++) {
+		rule1 = &rd1->reg_rules[x];
+		for (y = 0; y < rd2->n_reg_rules; y++) {
+			rule2 = &rd2->reg_rules[y];
+			/* This time around instead of using the stack lets
+			 * write to the target rule directly saving ourselves
+			 * a memcpy() */
+			intersected_rule = &rd->reg_rules[rule_idx];
+			r = reg_rules_intersect(rule1, rule2,
+				intersected_rule);
+			if (r)
+				continue;
+			rule_idx++;
+		}
+	}
+
+	if (rule_idx != num_rules) {
+		free(rd);
+		return NULL;
+	}
+
+	rd->n_reg_rules = num_rules;
+	rd->alpha2[0] = '9';
+	rd->alpha2[1] = '9';
+
+	return rd;
+}
diff --git a/reglib.h b/reglib.h
index e5da38b..b52d717 100644
--- a/reglib.h
+++ b/reglib.h
@@ -87,5 +87,8 @@ reglib_get_rd_alpha2(const char *alpha2, const char *file);
 
 /* reg helpers */
 void print_regdom(const struct ieee80211_regdomain *rd);
+struct ieee80211_regdomain *
+regdom_intersect(const struct ieee80211_regdomain *rd1,
+		 const struct ieee80211_regdomain *rd2);
 
 #endif
-- 
1.7.10.4


  parent reply	other threads:[~2013-05-31  2:10 UTC|newest]

Thread overview: 42+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-05-31  2:08 [PATCH 00/40] crda: reglib enhancements Luis R. Rodriguez
2013-05-31  2:08 ` [PATCH 01/40] crda: make reg_rules_intersect() style match Linux Luis R. Rodriguez
2013-05-31  2:08 ` [PATCH 02/40] crda: port over Linux is_valid_reg_rule() change bd05f28e Luis R. Rodriguez
2013-05-31  2:08 ` [PATCH 03/40] crda: remove verbose errors out of regdom_intersect() Luis R. Rodriguez
2013-05-31  2:08 ` [PATCH 04/40] crda: constify usage of struct ieee80211_regdomain Luis R. Rodriguez
2013-05-31  2:08 ` [PATCH 05/40] crda: remove unused BUG_ON() from intersect.c Luis R. Rodriguez
2013-05-31  2:08 ` Luis R. Rodriguez [this message]
2013-05-31  2:08 ` [PATCH 07/40] crda: fix regression when using reglib_for_each_country() Luis R. Rodriguez
2013-05-31  2:08 ` [PATCH 08/40] crda: move intersection if first attempt failed Luis R. Rodriguez
2013-05-31  2:08 ` [PATCH 09/40] crda: do not double count on reglib_for_each_country() Luis R. Rodriguez
2013-05-31  2:08 ` [PATCH 10/40] crda: annotate intersection worst case scenerio Luis R. Rodriguez
2013-05-31  2:09 ` [PATCH 11/40] crda: fix intersect.c memory management Luis R. Rodriguez
2013-05-31  2:09 ` [PATCH 12/40] crda: explicitly munmap() on reglib_get_rd_alpha2() Luis R. Rodriguez
2013-05-31  2:09 ` [PATCH 13/40] crda: explicitly close file descriptor and munmap() on failures Luis R. Rodriguez
2013-05-31  2:09 ` [PATCH 14/40] crda: separate crda_verify_db_signature() implementations Luis R. Rodriguez
2013-05-31  2:09 ` [PATCH 15/40] crda: use gcry_sexp_release() on crda_verify_db_signature() Luis R. Rodriguez
2013-05-31  2:09 ` [PATCH 16/40] crda: explicitly use close() and munmap() on reglib_get_rd_alpha2() Luis R. Rodriguez
2013-05-31  2:09 ` [PATCH 17/40] crda: use gcry_mpi_release() when using gcry_mpi_scan() Luis R. Rodriguez
2013-05-31  2:09 ` [PATCH 18/40] crda: rename world and prev_world on intersect.c Luis R. Rodriguez
2013-05-31  2:09 ` [PATCH 19/40] crda: remove verbosity out of intersect.c Luis R. Rodriguez
2013-05-31  2:09 ` [PATCH 20/40] crda: rename regdom_intersect() to reglib_intersect_rds() Luis R. Rodriguez
2013-05-31  2:09 ` [PATCH 21/40] crda: rename crda_get_file_ptr() to reglib_get_file_ptr() Luis R. Rodriguez
2013-05-31  2:09 ` [PATCH 22/40] crda: rename crda_verify_db_signature() to reglib_verify_db_signature() Luis R. Rodriguez
2013-05-31  2:09 ` [PATCH 23/40] crda: rename print_regdom() to reglib_print_regdom() Luis R. Rodriguez
2013-05-31  2:09 ` [PATCH 24/40] crda: add regdb_dfs_regions Luis R. Rodriguez
2013-05-31  2:09 ` [PATCH 25/40] crda: make print-regdom use internal flags Luis R. Rodriguez
2013-05-31  2:09 ` [PATCH 26/40] crda: move reg print helpers to reglib Luis R. Rodriguez
2013-05-31  2:09 ` [PATCH 27/40] crda: rename is_world_regdom() to reglib_is_world_regdom() Luis R. Rodriguez
2013-05-31  2:09 ` [PATCH 28/40] crda: rename isalpha_upper() to reglib_isalpha_upper() Luis R. Rodriguez
2013-05-31  2:09 ` [PATCH 29/40] crda: rename is_alpha2() to reglib_is_alpha2() Luis R. Rodriguez
2013-05-31  2:09 ` [PATCH 30/40] crda: rename is_valid_regdom() to reglib_is_valid_regdom() Luis R. Rodriguez
2013-05-31  2:09 ` [PATCH 31/40] crda: rename max() to reglib_max() Luis R. Rodriguez
2013-05-31  2:09 ` [PATCH 32/40] crda: rename min() to reglib_min() Luis R. Rodriguez
2013-05-31  2:09 ` [PATCH 33/40] crda: fix spacing on reglib_for_each_country() Luis R. Rodriguez
2013-05-31  2:09 ` [PATCH 34/40] crda: clarify intersect.c only computes an intersection Luis R. Rodriguez
2013-05-31  2:09 ` [PATCH 35/40] crda: separate intersecting a full db into a helper Luis R. Rodriguez
2013-05-31  2:09 ` [PATCH 36/40] crda: move reglib_intersect_regdb() to reglib Luis R. Rodriguez
2013-05-31  2:09 ` [PATCH 37/40] crda: move regdbprint to its own helper Luis R. Rodriguez
2013-05-31  2:09 ` [PATCH 38/40] crda: add reglib regdb context helpers: reglib_(malloc|free)_regdb_ctx() Luis R. Rodriguez
2013-05-31  2:09 ` [PATCH 39/40] crda: pass struct reglib_regdb_ctx to country2rd() Luis R. Rodriguez
2013-05-31  2:09 ` [PATCH 40/40] crda: make reglib_for_each_country() use the reglib context Luis R. Rodriguez
2013-06-30 23:08 ` [PATCH 00/40] crda: reglib enhancements Luis R. Rodriguez

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1369966169-23640-7-git-send-email-mcgrof@do-not-panic.com \
    --to=mcgrof@do-not-panic.com \
    --cc=linux-wireless@vger.kernel.org \
    --cc=wireless-regdb@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).