linux-bluetooth.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH BlueZ] mesh: Correctly generate NetKey list
@ 2020-01-09 17:57 Inga Stotland
  2020-01-09 21:00 ` Michał Lowas-Rzechonek
  2020-01-09 21:08 ` Michał Lowas-Rzechonek
  0 siblings, 2 replies; 8+ messages in thread
From: Inga Stotland @ 2020-01-09 17:57 UTC (permalink / raw)
  To: linux-bluetooth; +Cc: brian.gix, Inga Stotland

When responding with NetKey List Status, packed NetKey indices into
3 octets per pair. If number of NetKeys is odd, append the last key
index as a 2-octet value.
---
 mesh/net.c | 36 ++++++++++++++++++++++++++++--------
 1 file changed, 28 insertions(+), 8 deletions(-)

diff --git a/mesh/net.c b/mesh/net.c
index 0a4d2e72c..90ebdf496 100644
--- a/mesh/net.c
+++ b/mesh/net.c
@@ -1065,26 +1065,46 @@ bool mesh_net_get_key(struct mesh_net *net, bool new_key, uint16_t idx,
 bool mesh_net_key_list_get(struct mesh_net *net, uint8_t *buf, uint16_t *size)
 {
 	const struct l_queue_entry *entry;
-	uint16_t n, buf_size;
+	uint16_t num_keys, req_size, buf_size;
+	struct mesh_subnet *subnet;
 
 	if (!net || !buf || !size)
 		return false;
 
 	buf_size = *size;
-	if (buf_size < l_queue_length(net->subnets) * 2)
+
+	num_keys = l_queue_length(net->subnets);
+	req_size = (num_keys >> 1) * 3 + (num_keys % 2) * 2;
+
+	if (buf_size < req_size)
 		return false;
 
-	n = 0;
-	entry = l_queue_get_entries(net->subnets);
+	*size = req_size;
+
+	/* Pack NetKey indices in 3 octets */
+	for (entry = l_queue_get_entries(net->subnets); num_keys > 1;) {
+		uint32_t idx_pair;
 
-	for (; entry; entry = entry->next) {
-		struct mesh_subnet *subnet = entry->data;
+		subnet = entry->data;
+		idx_pair = subnet->idx;
+		idx_pair <<= 12;
+
+		subnet = entry->next->data;
+		idx_pair += subnet->idx;
+
+		l_put_le32(idx_pair, buf);
+		buf += 3;
+
+		num_keys -= 2;
+		entry = entry->next->next;
+	}
 
+	/* If odd number of NetKeys, fill in the end of the buffer */
+	if (num_keys % 2) {
+		subnet = entry->data;
 		l_put_le16(subnet->idx, buf);
-		n += 2;
 	}
 
-	*size = n;
 	return true;
 }
 
-- 
2.21.1


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* Re: [PATCH BlueZ] mesh: Correctly generate NetKey list
  2020-01-09 17:57 [PATCH BlueZ] mesh: Correctly generate NetKey list Inga Stotland
@ 2020-01-09 21:00 ` Michał Lowas-Rzechonek
  2020-01-09 21:23   ` Stotland, Inga
       [not found]   ` <31a576190ecbd1ba3f7e779d746baf35815fbed9.camel@intel.com>
  2020-01-09 21:08 ` Michał Lowas-Rzechonek
  1 sibling, 2 replies; 8+ messages in thread
From: Michał Lowas-Rzechonek @ 2020-01-09 21:00 UTC (permalink / raw)
  To: Inga Stotland; +Cc: linux-bluetooth, brian.gix

Hi Inga,

On 01/09, Inga Stotland wrote:
> When responding with NetKey List Status, packed NetKey indices into
> 3 octets per pair. If number of NetKeys is odd, append the last key
> index as a 2-octet value.
> ---
>  mesh/net.c | 36 ++++++++++++++++++++++++++++--------
>  1 file changed, 28 insertions(+), 8 deletions(-)
> 
> diff --git a/mesh/net.c b/mesh/net.c
> index 0a4d2e72c..90ebdf496 100644
> --- a/mesh/net.c
> +++ b/mesh/net.c
> @@ -1065,26 +1065,46 @@ bool mesh_net_get_key(struct mesh_net *net, bool new_key, uint16_t idx,
>  bool mesh_net_key_list_get(struct mesh_net *net, uint8_t *buf, uint16_t *size)
>  {
>  	const struct l_queue_entry *entry;
> -	uint16_t n, buf_size;
> +	uint16_t num_keys, req_size, buf_size;
> +	struct mesh_subnet *subnet;
>  
>  	if (!net || !buf || !size)
>  		return false;
>  
>  	buf_size = *size;
> -	if (buf_size < l_queue_length(net->subnets) * 2)
> +
> +	num_keys = l_queue_length(net->subnets);
> +	req_size = (num_keys >> 1) * 3 + (num_keys % 2) * 2;

Please don't use bit shifts for division.

Also, I think it's clearer to write this as:
    req_size = num_keys * 3 - num_keys % 2

> +
> +	if (buf_size < req_size)
>  		return false;
>  
> -	n = 0;
> -	entry = l_queue_get_entries(net->subnets);
> +	*size = req_size;
> +
> +	/* Pack NetKey indices in 3 octets */
> +	for (entry = l_queue_get_entries(net->subnets); num_keys > 1;) {
> +		uint32_t idx_pair;
>  
> -	for (; entry; entry = entry->next) {
> -		struct mesh_subnet *subnet = entry->data;
> +		subnet = entry->data;
> +		idx_pair = subnet->idx;
> +		idx_pair <<= 12;
> +
> +		subnet = entry->next->data;
> +		idx_pair += subnet->idx;
> +
> +		l_put_le32(idx_pair, buf);
> +		buf += 3;
> +
> +		num_keys -= 2;
> +		entry = entry->next->next;
> +	}
>  
> +	/* If odd number of NetKeys, fill in the end of the buffer */
> +	if (num_keys % 2) {
> +		subnet = entry->data;
>  		l_put_le16(subnet->idx, buf);
> -		n += 2;
>  	}
>  
> -	*size = n;
>  	return true;
>  }

Tested-by: Michał Lowas-Rzechonek <michal.lowas-rzechonek@silvair.com>

-- 
Michał Lowas-Rzechonek <michal.lowas-rzechonek@silvair.com>
Silvair http://silvair.com
Jasnogórska 44, 31-358 Krakow, POLAND

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH BlueZ] mesh: Correctly generate NetKey list
  2020-01-09 17:57 [PATCH BlueZ] mesh: Correctly generate NetKey list Inga Stotland
  2020-01-09 21:00 ` Michał Lowas-Rzechonek
@ 2020-01-09 21:08 ` Michał Lowas-Rzechonek
       [not found]   ` <d831f4046203d691b65bf145909cdc184bc788e9.camel@intel.com>
  1 sibling, 1 reply; 8+ messages in thread
From: Michał Lowas-Rzechonek @ 2020-01-09 21:08 UTC (permalink / raw)
  To: Inga Stotland; +Cc: linux-bluetooth, brian.gix

Inga,

On 01/09, Inga Stotland wrote:
> When responding with NetKey List Status, packed NetKey indices into
> 3 octets per pair. If number of NetKeys is odd, append the last key
> index as a 2-octet value.

Another thing: doesn't this duplicate the same logic implemented in
appkey.c function appkey_list?

OP_APPKEY_LIST is (and was) working fine.

-- 
Michał Lowas-Rzechonek <michal.lowas-rzechonek@silvair.com>
Silvair http://silvair.com
Jasnogórska 44, 31-358 Krakow, POLAND

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH BlueZ] mesh: Correctly generate NetKey list
       [not found]   ` <d831f4046203d691b65bf145909cdc184bc788e9.camel@intel.com>
@ 2020-01-09 21:23     ` michal.lowas-rzechonek
  0 siblings, 0 replies; 8+ messages in thread
From: michal.lowas-rzechonek @ 2020-01-09 21:23 UTC (permalink / raw)
  To: Stotland, Inga; +Cc: Gix, Brian, linux-bluetooth

Inga,

On 01/09, Stotland, Inga wrote:
>> Another thing: doesn't this duplicate the same logic implemented in
>> appkey.c function appkey_list?
> 
> No, it's a different list organization. So I cannot re-use the code.

I understand that the list we iterate over is different, but in the end
the index list serialization format is exactly the same.

-- 
Michał Lowas-Rzechonek <michal.lowas-rzechonek@silvair.com>
Silvair http://silvair.com
Jasnogórska 44, 31-358 Krakow, POLAND

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH BlueZ] mesh: Correctly generate NetKey list
  2020-01-09 21:00 ` Michał Lowas-Rzechonek
@ 2020-01-09 21:23   ` Stotland, Inga
       [not found]   ` <31a576190ecbd1ba3f7e779d746baf35815fbed9.camel@intel.com>
  1 sibling, 0 replies; 8+ messages in thread
From: Stotland, Inga @ 2020-01-09 21:23 UTC (permalink / raw)
  To: michal.lowas-rzechonek; +Cc: Gix, Brian, linux-bluetooth

Hi Michal,

On Thu, 2020-01-09 at 22:00 +0100, Michał Lowas-Rzechonek wrote:
> Hi Inga,
> 
> On 01/09, Inga Stotland wrote:
> > When responding with NetKey List Status, packed NetKey indices into
> > 3 octets per pair. If number of NetKeys is odd, append the last key
> > index as a 2-octet value.
> > ---
> >  mesh/net.c | 36 ++++++++++++++++++++++++++++--------
> >  1 file changed, 28 insertions(+), 8 deletions(-)
> > 
> > diff --git a/mesh/net.c b/mesh/net.c
> > index 0a4d2e72c..90ebdf496 100644
> > --- a/mesh/net.c
> > +++ b/mesh/net.c
> > @@ -1065,26 +1065,46 @@ bool mesh_net_get_key(struct mesh_net *net,
> > bool new_key, uint16_t idx,
> >  bool mesh_net_key_list_get(struct mesh_net *net, uint8_t *buf,
> > uint16_t *size)
> >  {
> >  	const struct l_queue_entry *entry;
> > -	uint16_t n, buf_size;
> > +	uint16_t num_keys, req_size, buf_size;
> > +	struct mesh_subnet *subnet;
> >  
> >  	if (!net || !buf || !size)
> >  		return false;
> >  
> >  	buf_size = *size;
> > -	if (buf_size < l_queue_length(net->subnets) * 2)
> > +
> > +	num_keys = l_queue_length(net->subnets);
> > +	req_size = (num_keys >> 1) * 3 + (num_keys % 2) * 2;
> 
> Please don't use bit shifts for division.

Why?

> 
> Also, I think it's clearer to write this as:
>     req_size = num_keys * 3 - num_keys % 2
> 

This is not how I read the spec. We need to pack 2 key indices in 3
octets.
For example, 4 keys are packed in 6 octets and 5 keys are packed in 8
octets. 

> > +
> > +	if (buf_size < req_size)
> >  		return false;
> >  
> > -	n = 0;
> > -	entry = l_queue_get_entries(net->subnets);
> > +	*size = req_size;
> > +
> > +	/* Pack NetKey indices in 3 octets */
> > +	for (entry = l_queue_get_entries(net->subnets); num_keys > 1;)
> > {
> > +		uint32_t idx_pair;
> >  
> > -	for (; entry; entry = entry->next) {
> > -		struct mesh_subnet *subnet = entry->data;
> > +		subnet = entry->data;
> > +		idx_pair = subnet->idx;
> > +		idx_pair <<= 12;
> > +
> > +		subnet = entry->next->data;
> > +		idx_pair += subnet->idx;
> > +
> > +		l_put_le32(idx_pair, buf);
> > +		buf += 3;
> > +
> > +		num_keys -= 2;
> > +		entry = entry->next->next;
> > +	}
> >  
> > +	/* If odd number of NetKeys, fill in the end of the buffer */
> > +	if (num_keys % 2) {
> > +		subnet = entry->data;
> >  		l_put_le16(subnet->idx, buf);
> > -		n += 2;
> >  	}
> >  
> > -	*size = n;
> >  	return true;
> >  }
> 
> Tested-by: Michał Lowas-Rzechonek <
> michal.lowas-rzechonek@silvair.com
> >
> 
> 

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH BlueZ] mesh: Correctly generate NetKey list
       [not found]   ` <31a576190ecbd1ba3f7e779d746baf35815fbed9.camel@intel.com>
@ 2020-01-09 21:29     ` michal.lowas-rzechonek
  2020-01-09 21:37       ` Stotland, Inga
  0 siblings, 1 reply; 8+ messages in thread
From: michal.lowas-rzechonek @ 2020-01-09 21:29 UTC (permalink / raw)
  To: Stotland, Inga; +Cc: Gix, Brian, linux-bluetooth

On 01/09, Stotland, Inga wrote:
>> Please don't use bit shifts for division.
> Why?

Because it's a different operation, and is less readable.  It's a
formula, so use math operators for numbers and bit operators for bits.

For example, idx_pair <<= 12 is fine, because the *context* is
bit-packing. Noone sane would write this as idx_pair *= 4096. This
reasoning applies the other way as well: don't write foo >> 2 where you
mean foo / 4.

>> Also, I think it's clearer to write this as:
>>     req_size = num_keys * 3 - num_keys % 2
>
> No, this is not how I read the spec. We need to pack 2 key indices in 3 octets.
> For example, 4 keys are packed in 6 octets and 5 keys are packed in 8 octets.

Yes, you're right. Sorry.

-- 
Michał Lowas-Rzechonek <michal.lowas-rzechonek@silvair.com>
Silvair http://silvair.com
Jasnogórska 44, 31-358 Krakow, POLAND

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH BlueZ] mesh: Correctly generate NetKey list
  2020-01-09 21:29     ` michal.lowas-rzechonek
@ 2020-01-09 21:37       ` Stotland, Inga
  2020-01-09 21:55         ` michal.lowas-rzechonek
  0 siblings, 1 reply; 8+ messages in thread
From: Stotland, Inga @ 2020-01-09 21:37 UTC (permalink / raw)
  To: michal.lowas-rzechonek; +Cc: Gix, Brian, linux-bluetooth

On Thu, 2020-01-09 at 22:29 +0100, michal.lowas-rzechonek@silvair.com
wrote:
> On 01/09, Stotland, Inga wrote:
> > > Please don't use bit shifts for division.
> > Why?
> 
> Because it's a different operation, and is less readable.  It's a
> formula, so use math operators for numbers and bit operators for bits.
> 
> For example, idx_pair <<= 12 is fine, because the *context* is
> bit-packing. Noone sane would write this as idx_pair *= 4096. This
> reasoning applies the other way as well: don't write foo >> 2 where you
> mean foo / 4.

Well, shift is a less expensive operation than a division (I believe,
some compilers may substitute integer division by 2 with a shift).

I don't see any reason not to use >> in this particular case, but I can
change if this is not readable...

> 
> > > Also, I think it's clearer to write this as:
> > >     req_size = num_keys * 3 - num_keys % 2
> > 
> > No, this is not how I read the spec. We need to pack 2 key indices in 3 octets.
> > For example, 4 keys are packed in 6 octets and 5 keys are packed in 8 octets.
> 
> Yes, you're right. Sorry.
> 

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH BlueZ] mesh: Correctly generate NetKey list
  2020-01-09 21:37       ` Stotland, Inga
@ 2020-01-09 21:55         ` michal.lowas-rzechonek
  0 siblings, 0 replies; 8+ messages in thread
From: michal.lowas-rzechonek @ 2020-01-09 21:55 UTC (permalink / raw)
  To: Stotland, Inga; +Cc: Gix, Brian, linux-bluetooth

On 01/09, Stotland, Inga wrote:
> Well, shift is a less expensive operation than a division (I believe,
> some compilers may substitute integer division by 2 with a shift).

So leave this to the compiler. In 99.999% cases it knows better.

http://www.linux-kongress.org/2009/slides/compiler_survey_felix_von_leitner.pdf slide 36

-- 
Michał Lowas-Rzechonek <michal.lowas-rzechonek@silvair.com>
Silvair http://silvair.com
Jasnogórska 44, 31-358 Krakow, POLAND

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2020-01-09 21:56 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-01-09 17:57 [PATCH BlueZ] mesh: Correctly generate NetKey list Inga Stotland
2020-01-09 21:00 ` Michał Lowas-Rzechonek
2020-01-09 21:23   ` Stotland, Inga
     [not found]   ` <31a576190ecbd1ba3f7e779d746baf35815fbed9.camel@intel.com>
2020-01-09 21:29     ` michal.lowas-rzechonek
2020-01-09 21:37       ` Stotland, Inga
2020-01-09 21:55         ` michal.lowas-rzechonek
2020-01-09 21:08 ` Michał Lowas-Rzechonek
     [not found]   ` <d831f4046203d691b65bf145909cdc184bc788e9.camel@intel.com>
2020-01-09 21:23     ` michal.lowas-rzechonek

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).