[PATCH 4/4] P2P: Replace p2p_freq_to_channel with freq_to_channel

Jouni Malinen jouni
Wed Oct 5 12:35:32 PDT 2011


The country parameter was not really used for anything and it would
be better to share a single function for converting frequency
into an operating class (in global table) and channel.
---
 src/p2p/p2p.c            |   23 ++++++++++-------------
 src/p2p/p2p_go_neg.c     |    8 ++++----
 src/p2p/p2p_i.h          |    2 --
 src/p2p/p2p_invitation.c |    9 ++++-----
 src/p2p/p2p_utils.c      |   43 ++-----------------------------------------
 5 files changed, 20 insertions(+), 65 deletions(-)

diff --git a/src/p2p/p2p.c b/src/p2p/p2p.c
index 25d4d55..f25d621 100644
--- a/src/p2p/p2p.c
+++ b/src/p2p/p2p.c
@@ -955,8 +955,8 @@ static int p2p_prepare_channel(struct p2p_data *p2p, unsigned int force_freq)
 {
 	if (force_freq) {
 		u8 op_reg_class, op_channel;
-		if (p2p_freq_to_channel(p2p->cfg->country, force_freq,
-					&op_reg_class, &op_channel) < 0) {
+		if (freq_to_channel(force_freq,	&op_reg_class, &op_channel) ==
+		    WPAS_BAND_INVALID) {
 			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
 				"P2P: Unsupported frequency %u MHz",
 				force_freq);
@@ -981,9 +981,8 @@ static int p2p_prepare_channel(struct p2p_data *p2p, unsigned int force_freq)
 
 		if (!p2p->cfg->cfg_op_channel && p2p->best_freq_overall > 0 &&
 		    p2p_supported_freq(p2p, p2p->best_freq_overall) &&
-		    p2p_freq_to_channel(p2p->cfg->country,
-					p2p->best_freq_overall,
-					&op_reg_class, &op_channel) == 0) {
+		    freq_to_channel(p2p->best_freq_overall, &op_reg_class,
+				    &op_channel) != WPAS_BAND_INVALID) {
 			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
 				"P2P: Select best overall channel as "
 				"operating channel preference");
@@ -991,10 +990,9 @@ static int p2p_prepare_channel(struct p2p_data *p2p, unsigned int force_freq)
 			p2p->op_channel = op_channel;
 		} else if (!p2p->cfg->cfg_op_channel && p2p->best_freq_5 > 0 &&
 			   p2p_supported_freq(p2p, p2p->best_freq_5) &&
-			   p2p_freq_to_channel(p2p->cfg->country,
-					       p2p->best_freq_5,
-					       &op_reg_class, &op_channel) ==
-			   0) {
+			   freq_to_channel(p2p->best_freq_5,
+					   &op_reg_class, &op_channel) !=
+			   WPAS_BAND_INVALID) {
 			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
 				"P2P: Select best 5 GHz channel as "
 				"operating channel preference");
@@ -1003,10 +1001,9 @@ static int p2p_prepare_channel(struct p2p_data *p2p, unsigned int force_freq)
 		} else if (!p2p->cfg->cfg_op_channel &&
 			   p2p->best_freq_24 > 0 &&
 			   p2p_supported_freq(p2p, p2p->best_freq_24) &&
-			   p2p_freq_to_channel(p2p->cfg->country,
-					       p2p->best_freq_24,
-					       &op_reg_class, &op_channel) ==
-			   0) {
+			   freq_to_channel(p2p->best_freq_24,
+					   &op_reg_class, &op_channel) !=
+			   WPAS_BAND_INVALID) {
 			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
 				"P2P: Select best 2.4 GHz channel as "
 				"operating channel preference");
diff --git a/src/p2p/p2p_go_neg.c b/src/p2p/p2p_go_neg.c
index be04fad..003a5f1 100644
--- a/src/p2p/p2p_go_neg.c
+++ b/src/p2p/p2p_go_neg.c
@@ -304,8 +304,8 @@ static void p2p_reselect_channel(struct p2p_data *p2p,
 	freq = p2p_channel_to_freq(p2p->cfg->country, p2p->op_reg_class,
 				   p2p->op_channel);
 	if (freq >= 2400 && freq < 2500 && p2p->best_freq_5 > 0 &&
-	    p2p_freq_to_channel(p2p->cfg->country, p2p->best_freq_5,
-				&op_reg_class, &op_channel) == 0 &&
+	    freq_to_channel(p2p->best_freq_5, &op_reg_class, &op_channel) !=
+	    WPAS_BAND_INVALID &&
 	    p2p_channels_includes(intersection, op_reg_class, op_channel)) {
 		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Pick best 5 GHz "
 			"channel (reg_class %u channel %u) from intersection",
@@ -316,8 +316,8 @@ static void p2p_reselect_channel(struct p2p_data *p2p,
 	}
 
 	if (freq >= 4900 && freq < 6000 && p2p->best_freq_24 > 0 &&
-	    p2p_freq_to_channel(p2p->cfg->country, p2p->best_freq_24,
-				&op_reg_class, &op_channel) == 0 &&
+	    freq_to_channel(p2p->best_freq_24, &op_reg_class, &op_channel) !=
+	    WPAS_BAND_INVALID &&
 	    p2p_channels_includes(intersection, op_reg_class, op_channel)) {
 		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Pick best 2.4 GHz "
 			"channel (reg_class %u channel %u) from intersection",
diff --git a/src/p2p/p2p_i.h b/src/p2p/p2p_i.h
index 52d9311..45e71d5 100644
--- a/src/p2p/p2p_i.h
+++ b/src/p2p/p2p_i.h
@@ -504,8 +504,6 @@ struct p2p_group_info {
 /* p2p_utils.c */
 int p2p_random(char *buf, size_t len);
 int p2p_channel_to_freq(const char *country, int reg_class, int channel);
-int p2p_freq_to_channel(const char *country, unsigned int freq, u8 *reg_class,
-			u8 *channel);
 void p2p_channels_intersect(const struct p2p_channels *a,
 			    const struct p2p_channels *b,
 			    struct p2p_channels *res);
diff --git a/src/p2p/p2p_invitation.c b/src/p2p/p2p_invitation.c
index bb2767d..225596b 100644
--- a/src/p2p/p2p_invitation.c
+++ b/src/p2p/p2p_invitation.c
@@ -183,8 +183,8 @@ void p2p_process_invitation_req(struct p2p_data *p2p, const u8 *sa,
 	}
 
 	if (op_freq) {
-		if (p2p_freq_to_channel(p2p->cfg->country, op_freq,
-					&reg_class, &channel) < 0) {
+		if (freq_to_channel(op_freq, &reg_class, &channel) ==
+		    WPAS_BAND_INVALID) {
 			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
 				"P2P: Unknown forced freq %d MHz from "
 				"invitation_process()", op_freq);
@@ -456,9 +456,8 @@ int p2p_invite(struct p2p_data *p2p, const u8 *peer, enum p2p_invite_role role,
 	dev->invitation_reqs = 0;
 
 	if (force_freq) {
-		if (p2p_freq_to_channel(p2p->cfg->country, force_freq,
-					&p2p->op_reg_class, &p2p->op_channel) <
-		    0) {
+		if (freq_to_channel(force_freq, &p2p->op_reg_class,
+				    &p2p->op_channel) == WPAS_BAND_INVALID) {
 			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
 				"P2P: Unsupported frequency %u MHz",
 				force_freq);
diff --git a/src/p2p/p2p_utils.c b/src/p2p/p2p_utils.c
index da4b6ed..e82d04b 100644
--- a/src/p2p/p2p_utils.c
+++ b/src/p2p/p2p_utils.c
@@ -138,45 +138,6 @@ int p2p_channel_to_freq(const char *country, int reg_class, int channel)
 }
 
 
-/**
- * p2p_freq_to_channel - Convert frequency into channel info
- * @country: Country code
- * @reg_class: Buffer for returning regulatory class
- * @channel: Buffer for returning channel number
- * Returns: 0 on success, -1 if the specified frequency is unknown
- */
-int p2p_freq_to_channel(const char *country, unsigned int freq, u8 *reg_class,
-			u8 *channel)
-{
-	/* TODO: more operating classes */
-	if (freq >= 2412 && freq <= 2472) {
-		*reg_class = 81; /* 2.407 GHz, channels 1..13 */
-		*channel = (freq - 2407) / 5;
-		return 0;
-	}
-
-	if (freq == 2484) {
-		*reg_class = 82; /* channel 14 */
-		*channel = 14;
-		return 0;
-	}
-
-	if (freq >= 5180 && freq <= 5240) {
-		*reg_class = 115; /* 5 GHz, channels 36..48 */
-		*channel = (freq - 5000) / 5;
-		return 0;
-	}
-
-	if (freq >= 5745 && freq <= 5805) {
-		*reg_class = 124; /* 5 GHz, channels 149..161 */
-		*channel = (freq - 5000) / 5;
-		return 0;
-	}
-
-	return -1;
-}
-
-
 static void p2p_reg_class_intersect(const struct p2p_reg_class *a,
 				    const struct p2p_reg_class *b,
 				    struct p2p_reg_class *res)
@@ -263,8 +224,8 @@ int p2p_channels_includes(const struct p2p_channels *channels, u8 reg_class,
 int p2p_supported_freq(struct p2p_data *p2p, unsigned int freq)
 {
 	u8 op_reg_class, op_channel;
-	if (p2p_freq_to_channel(p2p->cfg->country, freq,
-				&op_reg_class, &op_channel) < 0)
+	if (freq_to_channel(freq, &op_reg_class, &op_channel) ==
+	    WPAS_BAND_INVALID)
 		return 0;
 	return p2p_channels_includes(&p2p->cfg->channels, op_reg_class,
 				     op_channel);
-- 
1.7.4.1


--FL5UXtIhxfXey3p5--



More information about the Hostap mailing list