Re: [PATCH BlueZ] mesh: Correctly generate NetKey list

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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@xxxxxxxxxxx
> >
> 
> 




[Index of Archives]     [Bluez Devel]     [Linux Wireless Networking]     [Linux Wireless Personal Area Networking]     [Linux ATH6KL]     [Linux USB Devel]     [Linux Media Drivers]     [Linux Audio Users]     [Linux Kernel]     [Linux SCSI]     [Big List of Linux Books]

  Powered by Linux