Skip to contentSkip to conversation

ieee802154: use helper function to get rid of redundancy

PATCH In review Jean Sacren 3 participants 8 messages May 31, 2014 – Jun 1, 2014 1 message not in the archive

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ieee802154: use helper function to get rid of redundancy

In some call sites of at86rf230_write_subreg(), they are rather redundant. Use a helper function to replace those call sites for efficiency.

With at86rf230_write_subreg_by_page(), now ->rssi_base_val is assigned before at86rf230_write_subreg() is evaluated. Looking at at86rf230_write_subreg(), that change could possibly affect

		__at86rf230_read_subreg()
and
		__at86rf230_write()

only or vice versa. Yet by the definition of

		struct at86rf230_local

changing ->rssi_base_val will in no way affect those two functions above and evaluation of those two has no impact on ->rssi_base_val. Thus, at86rf230_write_subreg_by_page() itself is safe.

Additionally at86rf230_write_subreg_by_channel() is simple and safe.

---
 drivers/net/ieee802154/at86rf230.c | 34 +++++++++++++++++++++++-----------
 1 file changed, 23 insertions(+), 11 deletions(-)
drivers/net/ieee802154/at86rf230.c +23 -11
index 4517b149ed07..5aecbd1c402c 100644
@@ -597,25 +597,37 @@ at86rf230_set_channel(struct at86rf230_local *lp, int page, int channel)
 	return at86rf230_write_subreg(lp, SR_CHANNEL, channel);
 }
 
+static int at86rf230_write_subreg_by_channel(struct at86rf230_local *lp,
+					     int channel)
+{
+	if (channel)
+		channel = 1;
+
+	return at86rf230_write_subreg(lp, SR_SUB_MODE, channel);
+}
+
+static int at86rf230_write_subreg_by_page(struct at86rf230_local *lp, int page)
+{
+	if (page) {
+		lp->rssi_base_val = -98;
+		page = 1;
+	} else {
+		lp->rssi_base_val = -100;
+	}
+
+	return at86rf230_write_subreg(lp, SR_BPSK_QPSK, page);
+}
+
 static int
 at86rf212_set_channel(struct at86rf230_local *lp, int page, int channel)
 {
 	int rc;
 
-	if (channel == 0)
-		rc = at86rf230_write_subreg(lp, SR_SUB_MODE, 0);
-	else
-		rc = at86rf230_write_subreg(lp, SR_SUB_MODE, 1);
+	rc = at86rf230_write_subreg_by_channel(lp, channel);
 	if (rc < 0)
 		return rc;
 
-	if (page == 0) {
-		rc = at86rf230_write_subreg(lp, SR_BPSK_QPSK, 0);
-		lp->rssi_base_val = -100;
-	} else {
-		rc = at86rf230_write_subreg(lp, SR_BPSK_QPSK, 1);
-		lp->rssi_base_val = -98;
-	}
+	rc = at86rf230_write_subreg_by_page(lp, page);
 	if (rc < 0)
 		return rc;
 
maintainer
#1 lore raw

Hi Jean,

the at86rf230 driver supports several at86rf2xx chips. You split the at86rf212_set_channel which is at86rf212 specific in two function which are named at86rf230_foo.

Sorry, but I think we should not do this. One reason is that the code is much easier to read when we have one channel_set callback for at86rf23x and at86rf212 chips.

Btw. I preparing a big cleanup for this driver which supports regmap and asychronous handling for rx/tx paths.

- Alex

#2 lore raw

From: Alexander Aring <[email protected]>
Date: Sun, 01 Jun 2014 09:26:57 +0200

Hi Alex,

Thank you very much for the feedback.

Quoted 3 lines

the at86rf230 driver supports several at86rf2xx chips. You split the at86rf212_set_channel which is at86rf212 specific in two function which are named at86rf230_foo.

I didn't "split" at86rf212_set_channel() in two functions. I spliced those two sections of code and made at86rf212_set_channel() far succinct and easy to read.

Quoted 3 lines

Sorry, but I think we should not do this. One reason is that the code is much easier to read when we have one channel_set callback for at86rf23x and at86rf212 chips.

If you use one channel_set callback as before the change, how would you overcome the redundancy?

Quoted 2 lines

Btw. I preparing a big cleanup for this driver which supports regmap and asychronous handling for rx/tx paths.

Thank you for letting me know. I'm sure I will learn a lot.

A message that is not in the archive (sent off-list or not yet received): [email protected]

#4 lore raw

Hi,

Quoted 7 lines · Jean Sacren

From: Alexander Aring <[email protected]>
Date: Sun, 01 Jun 2014 09:26:57 +0200

Hi Alex,

Thank you very much for the feedback.

Quoted 8 lines
Quoted 3 lines

the at86rf230 driver supports several at86rf2xx chips. You split the at86rf212_set_channel which is at86rf212 specific in two function which are named at86rf230_foo.

I didn't "split" at86rf212_set_channel() in two functions. I spliced those two sections of code and made at86rf212_set_channel() far succinct and easy to read.

yes, but this driver supports more than one chip and it's easier to read if we have one channel_set function for each chip type. Note you also named the specific channel_set function to a another at86rf230_foo function which is at86rf212 specific only. Sorry that will confuse all the people who will ever read this code.

There is a at86rf230_ops and at86rf212_ops struct. The channel_set function it's much easier to have only one callback for each struct, otherwise you have 4 different channel_set functions and nobody knows for which at86rf2xx type that function is for.

Quoted 7 lines
Quoted 3 lines

Sorry, but I think we should not do this. One reason is that the code is much easier to read when we have one channel_set callback for at86rf23x and at86rf212 chips.

If you use one channel_set callback as before the change, how would you overcome the redundancy?

There is no redundancy, sorry. There would be a redundancy if two chiptypes like at86rf231 and at86rf212 needs some code of this callback and you can do some codesharing, but you can't do that there.

- Alex

------------------------------------------------------------------------------
Time is money. Stop wasting it! Get your web API in 5 minutes. www.restlet.com/download
http://p.sf.net/sfu/restlet

#5 lore raw

From: Alexander Aring <[email protected]>
Date: Sun, 01 Jun 2014 16:35:53 +0200

Quoted 26 lines

Hi,

Quoted 7 lines · Jean Sacren

From: Alexander Aring <[email protected]>
Date: Sun, 01 Jun 2014 09:26:57 +0200

Hi Alex,

Thank you very much for the feedback.

Quoted 8 lines
Quoted 3 lines

the at86rf230 driver supports several at86rf2xx chips. You split the at86rf212_set_channel which is at86rf212 specific in two function which are named at86rf230_foo.

I didn't "split" at86rf212_set_channel() in two functions. I spliced those two sections of code and made at86rf212_set_channel() far succinct and easy to read.

yes, but this driver supports more than one chip and it's easier to read if we have one channel_set function for each chip type. Note you also named the specific channel_set function to a another at86rf230_foo function which is at86rf212 specific only. Sorry that will confuse all the people who will ever read this code.

Quoted 4 lines

There is a at86rf230_ops and at86rf212_ops struct. The channel_set function it's much easier to have only one callback for each struct, otherwise you have 4 different channel_set functions and nobody knows for which at86rf2xx type that function is for.

You mean something like the following will be less confusing?

drivers/net/ieee802154/at86rf230.c +10 -9
index 4517b149ed07..06b494bacc44 100644
@@ -602,20 +602,21 @@ at86rf212_set_channel(struct at86rf230_local *lp, int page, int channel)
 {
 	int rc;
 
-	if (channel == 0)
-		rc = at86rf230_write_subreg(lp, SR_SUB_MODE, 0);
-	else
-		rc = at86rf230_write_subreg(lp, SR_SUB_MODE, 1);
+	if (channel)
+		channel = 1;
+
+	rc = at86rf230_write_subreg(lp, SR_SUB_MODE, channel);
 	if (rc < 0)
 		return rc;
 
-	if (page == 0) {
-		rc = at86rf230_write_subreg(lp, SR_BPSK_QPSK, 0);
-		lp->rssi_base_val = -100;
-	} else {
-		rc = at86rf230_write_subreg(lp, SR_BPSK_QPSK, 1);
+	if (page) {
 		lp->rssi_base_val = -98;
+		page = 1;
+	} else {
+		lp->rssi_base_val = -100;
 	}
+
+	rc = at86rf230_write_subreg(lp, SR_BPSK_QPSK, page);
 	if (rc < 0)
 		return rc;
maintainer
#6 lore raw
Quoted 28 lines · Jean Sacren

From: Alexander Aring <[email protected]>
Date: Sun, 01 Jun 2014 16:35:53 +0200

Quoted 26 lines

Hi,

Quoted 7 lines · Jean Sacren

From: Alexander Aring <[email protected]>
Date: Sun, 01 Jun 2014 09:26:57 +0200

Hi Alex,

Thank you very much for the feedback.

Quoted 8 lines
Quoted 3 lines

the at86rf230 driver supports several at86rf2xx chips. You split the at86rf212_set_channel which is at86rf212 specific in two function which are named at86rf230_foo.

I didn't "split" at86rf212_set_channel() in two functions. I spliced those two sections of code and made at86rf212_set_channel() far succinct and easy to read.

yes, but this driver supports more than one chip and it's easier to read if we have one channel_set function for each chip type. Note you also named the specific channel_set function to a another at86rf230_foo function which is at86rf212 specific only. Sorry that will confuse all the people who will ever read this code.

Quoted 6 lines
Quoted 4 lines

There is a at86rf230_ops and at86rf212_ops struct. The channel_set function it's much easier to have only one callback for each struct, otherwise you have 4 different channel_set functions and nobody knows for which at86rf2xx type that function is for.

You mean something like the following will be less confusing?

Yes that's less but there are issues and I don't see any reason why we should do that.

Quoted 10 lines
drivers/net/ieee802154/at86rf230.c +0 -1
index 4517b149ed07..06b494bacc44 100644
@@ -602,20 +602,21 @@ at86rf212_set_channel(struct at86rf230_local *lp, int page, int channel)
 {
 	int rc;
 
-	if (channel == 0)

drivers/net/ieee802154/at86rf230.c at86rf212_set_channel(struct at86rf230_local *lp, int page, int channel) · line 611

Quoted 10 lines
drivers/net/ieee802154/at86rf230.c +4 -3
-		rc = at86rf230_write_subreg(lp, SR_SUB_MODE, 0);
-	else
-		rc = at86rf230_write_subreg(lp, SR_SUB_MODE, 1);
+	if (channel)
+		channel = 1;
+
+	rc = at86rf230_write_subreg(lp, SR_SUB_MODE, channel);
 	if (rc < 0)
 		return rc;
 

First:

This will break the at86rf212_set_channel function. At the end of this function we need the channel parameter and you overwrite it here. At the end of this function stands:

"return at86rf230_write_subreg(lp, SR_CHANNEL, channel);"

Second:

The variable channel should be a new variable named sub_mode and initialized to 0, this fixes the first issue.

But again, I don't see any reasons why we should change that. It's the same thing like before.

- Alex

#7 lore raw

From: Alexander Aring <[email protected]>
Date: Sun, 01 Jun 2014 18:39:28 +0200

Quoted 29 lines
Quoted 2 lines

You mean something like the following will be less confusing?

Yes that's less but there are issues and I don't see any reason why we should do that.

Quoted 10 lines
drivers/net/ieee802154/at86rf230.c +0 -1
index 4517b149ed07..06b494bacc44 100644
@@ -602,20 +602,21 @@ at86rf212_set_channel(struct at86rf230_local *lp, int page, int channel)
 {
 	int rc;
 
-	if (channel == 0)

drivers/net/ieee802154/at86rf230.c at86rf212_set_channel(struct at86rf230_local *lp, int page, int channel) · line 611

Quoted 10 lines
drivers/net/ieee802154/at86rf230.c +4 -3
-		rc = at86rf230_write_subreg(lp, SR_SUB_MODE, 0);
-	else
-		rc = at86rf230_write_subreg(lp, SR_SUB_MODE, 1);
+	if (channel)
+		channel = 1;
+
+	rc = at86rf230_write_subreg(lp, SR_SUB_MODE, channel);
 	if (rc < 0)
 		return rc;
 

First:

Quoted 5 lines

This will break the at86rf212_set_channel function. At the end of this function we need the channel parameter and you overwrite it here. At the end of this function stands:

"return at86rf230_write_subreg(lp, SR_CHANNEL, channel);"

You are right, but we can:

	if (channel)
		rc = 1;

	rc = at86rf230_write_subreg(lp, SR_SUB_MODE, rc);
#8 lore raw

From: Jean Sacren <[email protected]>
Date: Sun, 01 Jun 2014 11:49:29 -0600

Quoted 36 lines

From: Alexander Aring <[email protected]>
Date: Sun, 01 Jun 2014 18:39:28 +0200

Quoted 33 lines
Quoted 2 lines

You mean something like the following will be less confusing?

Yes that's less but there are issues and I don't see any reason why we should do that.

Quoted 10 lines
drivers/net/ieee802154/at86rf230.c +0 -1
index 4517b149ed07..06b494bacc44 100644
@@ -602,20 +602,21 @@ at86rf212_set_channel(struct at86rf230_local *lp, int page, int channel)
 {
 	int rc;
 
-	if (channel == 0)

drivers/net/ieee802154/at86rf230.c at86rf212_set_channel(struct at86rf230_local *lp, int page, int channel) · line 611

Quoted 10 lines
drivers/net/ieee802154/at86rf230.c +4 -3
-		rc = at86rf230_write_subreg(lp, SR_SUB_MODE, 0);
-	else
-		rc = at86rf230_write_subreg(lp, SR_SUB_MODE, 1);
+	if (channel)
+		channel = 1;
+
+	rc = at86rf230_write_subreg(lp, SR_SUB_MODE, channel);
 	if (rc < 0)
 		return rc;
 

First:

This will break the at86rf212_set_channel function. At the end of this function we need the channel parameter and you overwrite it here. At the end of this function stands:

Quoted 8 lines
Quoted 1 line

"return at86rf230_write_subreg(lp, SR_CHANNEL, channel);"

You are right, but we can:

	if (channel)
		rc = 1;

	rc = at86rf230_write_subreg(lp, SR_SUB_MODE, rc);

Obviously:

	int rc = 0;