Yahoo Groups archive

AVR-Chat

Index last updated: 2026-04-28 22:41 UTC

Thread

Bit fields

Bit fields

2010-06-07 by David VanHorn

I have an application that needs to receive a variable number of
packets in a block of data.
I store them into a block-sized array with a packet-sized offset
multiplied by the packet number..
I need to know when I've received all the packets, but out-of-order
and duplicate packets are possible.

I was thinking of doing this with a bit-field but that seems pretty messy.
Are there other good ways to know when I've received all the packets,
given that they may arrive out of order?

Re: [AVR-Chat] Bit fields

2010-06-07 by Russell Shaw

David VanHorn wrote:
> I have an application that needs to receive a variable number of
> packets in a block of data.
> I store them into a block-sized array with a packet-sized offset
> multiplied by the packet number..
> I need to know when I've received all the packets, but out-of-order
> and duplicate packets are possible.
> 
> I was thinking of doing this with a bit-field but that seems pretty messy.
> Are there other good ways to know when I've received all the packets,
> given that they may arrive out of order?

You could subtract the sequence number of the last packet of the last
batch from the highest sequence number packet of the latest batch of
received packets. The difference should agree with a counter for the
packets received in this batch so far.

Re: [AVR-Chat] Bit fields

2010-06-07 by David VanHorn

> You could subtract the sequence number of the last packet of the last
> batch from the highest sequence number packet of the latest batch of
> received packets. The difference should agree with a counter for the
> packets received in this batch so far.

I think I follow you, but I don't see how this tells me wether I've
received all of them.
Not that I expect this, but say the last packet arrives first...
Or the middle packet arrives three times. (very possible)

RE: [AVR-Chat] Bit fields

2010-06-07 by Cat C

If you know how many there are:

1. Initialize array with zeros, at least one in each packet.

2. Initialize counter with how many you expect.

3. When a packet comes, decrease counter IF it goes into empty (zero) position, otherwise not.

4. When counter is zero, you have them all.

 

Cat
 
> To: AVR-Chat@yahoogroups.com
> From: microbrix@gmail.com
> Date: Mon, 7 Jun 2010 17:08:25 -0600
> Subject: Re: [AVR-Chat] Bit fields
> 
> > You could subtract the sequence number of the last packet of the last
> > batch from the highest sequence number packet of the latest batch of
> > received packets. The difference should agree with a counter for the
> > packets received in this batch so far.
> 
> I think I follow you, but I don't see how this tells me wether I've
> received all of them.
> Not that I expect this, but say the last packet arrives first...
> Or the middle packet arrives three times. (very possible)
> 
> 
> ------------------------------------
> 
> Yahoo! Groups Links
> 
> 
> 
 		 	   		  

[Non-text portions of this message have been removed]

Re: [AVR-Chat] Bit fields

2010-06-07 by Russell Shaw

David VanHorn wrote:
>> You could subtract the sequence number of the last packet of the last
>> batch from the highest sequence number packet of the latest batch of
>> received packets. The difference should agree with a counter for the
>> packets received in this batch so far.
> 
> I think I follow you, but I don't see how this tells me wether I've
> received all of them.
> Not that I expect this, but say the last packet arrives first...
> Or the middle packet arrives three times. (very possible)

You receive the packets in batches, say 32 at a time. After each
batch, reset the counter to zero. The counter increments on each
packet received, so if the last packet of this batch is received
first, the counter is now 1. If the seq number of this packet is
63 and you subtract the last seq number of the last batch (31),
you get 32. So there's now 32 - 1 or 31 more packets to receive.

The batch size is 32 in this example (size of your "sliding window").

On alternative way to restate:

You receive the packets in batches, say 32 at a time. After each
batch, reset the counter to 32. The counter decrements on each
packet received. The counter shows how many more packets are
missing for this batch. When the counter reaches zero, you
advance to the next batch and repeat (window is slid to
next batch). As a check, make sure the seq no of each
received packet is in your current window range. Discard
those that are deemed to be duplicates by the protocol.

Depending on the protocol, packets for the next batch could
arrive prematurely. You could have a second storage area for
the next batch already set up if needed. Hopefully the protocol
would avoid the need for that.

Re: [AVR-Chat] Bit fields

2010-06-08 by David VanHorn

> 3. When a packet comes, decrease counter IF it goes into empty (zero) position, otherwise not.


All zeroes in the data part (the part i'd save) is legal.

RE: [AVR-Chat] Bit fields

2010-06-08 by Dave McLaughlin

Hi David,

 

I am assuming your packet count and packet number is known in each packet?
With this 1 of 3 etc you would know what is arriving. At startup, reset a
simple variable to all 1's (for instance, if the max packet count is 8 or
less, then you can use an unsigned char, for 16 use an unsigned int etc etc)

 

As each packet arrives, you go ahead and store it. You take the packet
number and clear the bit for that in the variable above. Once this variable
is ZERO you have all the packets. Because it simply clears the bits as a
packet arrives, it does not matter now if a packet is received more than
once.

 

This will work but you have to be careful because after the packets arrive
and you have a complete block, you may get another packet resent or does
this not matter and you simply start populating a new block?

 

This sound similar to your bit field idea and to be honest, this sounds like
the easiest solution, even if it sounds messy, there is no worrying about
packets arriving more than once with this method. You can either store or
discard if already received.

 

Good luck with it. Sounds like an interesting challenge!

 

Dave.
Show quoted textHide quoted text
From: AVR-Chat@yahoogroups.com [mailto:AVR-Chat@yahoogroups.com] On Behalf
Of David VanHorn
Sent: 08 June 2010 04:26
To: AVR-Chat@yahoogroups.com
Subject: [AVR-Chat] Bit fields

 

  

I have an application that needs to receive a variable number of
packets in a block of data.
I store them into a block-sized array with a packet-sized offset
multiplied by the packet number..
I need to know when I've received all the packets, but out-of-order
and duplicate packets are possible.

I was thinking of doing this with a bit-field but that seems pretty messy.
Are there other good ways to know when I've received all the packets,
given that they may arrive out of order?



[Non-text portions of this message have been removed]

Re: [AVR-Chat] Bit fields

2010-06-08 by David Kelly

On Jun 7, 2010, at 4:25 PM, David VanHorn wrote:

> I have an application that needs to receive a variable number of
> packets in a block of data.
> I store them into a block-sized array with a packet-sized offset
> multiplied by the packet number..
> I need to know when I've received all the packets, but out-of-order
> and duplicate packets are possible.
> 
> I was thinking of doing this with a bit-field but that seems pretty messy.
> Are there other good ways to know when I've received all the packets,
> given that they may arrive out of order?


I think it would be best to pay the 8x memory penalty and allocate an array of uint8_t "received" flags than to use a bit array. But perhaps you have more available CPU cycles and FLASH code space than SRAM, then the bit array would be attractive.

I don't think an array of bit fields will work because C can not take the address of a bit.

A bit of Googling found some macros to perform bit operations on an array of uint8_t's. This is pretty much the way I was thinking of doing it, use divide to index into the array, then use modulo to determine which bit:

> #include <limits.h> /* for CHAR_BIT */
> 
> #define BITMASK(b) (1 << ((b) % CHAR_BIT))
> #define BITSLOT(b) ((b) / CHAR_BIT)
> #define BITSET(a, b) ((a)[BITSLOT(b)] |= BITMASK(b))
> #define BITTEST(a, b) ((a)[BITSLOT(b)] & BITMASK(b))
> 
> (If you don't have <limits.h>, try using 8 for CHAR_BIT.)

--
David Kelly N4HHE, dkelly@HiWAAY.net
========================================================================
Whom computers would destroy, they must first drive mad.

RE: [AVR-Chat] Bit fields

2010-06-08 by Cat C

Like in a (linked) list you may have extra bytes with info.

> To: AVR-Chat@yahoogroups.com
> From: microbrix@gmail.com
> Date: Mon, 7 Jun 2010 19:03:37 -0600
> Subject: Re: [AVR-Chat] Bit fields
> 
> > 3. When a packet comes, decrease counter IF it goes into empty (zero) position, otherwise not.
> 
> 
> All zeroes in the data part (the part i'd save) is legal.
> 
> 
> ------------------------------------
> 
> Yahoo! Groups Links
> 
> 
> 
 		 	   		  

[Non-text portions of this message have been removed]

Move to quarantaine

This moves the raw source file on disk only. The archive index is not changed automatically, so you still need to run a manual refresh afterward.