Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 352
  • Last Modified:

Experts Question

In my communication protocl application a want to use a modulus 128 operation which is as follows

I have a variable 'sequence_no' which comes from communication packet.

I have to verify in my function whether the 'sequence_no' is previously received or not.

For this operation here is an algorithm

unsigned char current_no; // This my applications global variable

if(sequence_no<=(current_no-1))
 // packet is already received
else if( sequence_no >= current_no)
{
      // receive packet
     current_no ++;
     current_no %= 128; // I have to round off to '0' when number equals 128
}
else
 // invalid packet(misalignment)

The above works fine for 0-127

My problem is, when the 'sequence_no = 127' if i receive the same packet twice for the first time it validates TRUE,
but for the second time the current_no rounds off to '0' and the condition "sequence_no<=(current_no-1)" fails.

Can any one suggest a solution to above?
If there is any difficuly in understanding question please comment..


0
Chandra V
Asked:
Chandra V
  • 5
  • 2
  • 2
  • +3
1 Solution
 
3rsrichardCommented:
You may just have a problem with "signed" vs "unsigned" byte.
Look up the ranges.
0
 
guyssCommented:
use a long variable, use %128 when sending but keep the entire variable for your use.

if you can't do this you might also check the absolute value of the difference between 'sequence_no' and 'current_no' if it's bigger the 128/2 then you know you have to add 128 and then test your condition...
0
 
graham_kCommented:
is it that simple - are you sure that your packets always arrive in sequence (although possibly duplicated)?

If they can also arrive out of sequence then you need a map of those already received (in which case, this problem goes away & another replaces it).

Most telecommunications protocols allow for out of sequence reception.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Chandra VConsultantAuthor Commented:
Hai graham_k,

You have guessed correctly. I have to support out of sequence_number reception also for the current windows size (i.e, 4).

Can you suggest some solution?
0
 
Chandra VConsultantAuthor Commented:
Hai graham_k,

You have guessed correctly. I have to support out of sequence_number reception also for the current windows size (i.e, 4).

Can you suggest some solution?
0
 
graham_kCommented:
well, for a start, that means that you need to have an array of [window size], with the receive status of each frame.

I obviously can't post commercial source code here. But consider something like ...

- at start of transmission, set all to not received
- when a frame is received ...

  if (abs(received frame # - expect frame #) > window size))
  {
    frame is wildly out of sequence.  E.g windows size is 64 and are expecting frame # 12, but receivef # 101 or are expecting # 99 and received # 3
    ignore frame & return;
  }

  if (received number < expected)
     simple out of sequence frame, just set array entry to received and store the frame data

  else
    if (received number == expected)
       expected = (expected + 1) mod window size
       shuffle all window status up one
       set frame receive status
    else
      // we have received a higher frame # than expected
     set frame receive status
     don't change value of next expected frame

Now - this is all off the top of my head & I haven't looked at the code in some time, but it might give you a rough idea and I can't give you my emploer's code (or even algorithm) anyway.

Also, what do you do if you receive frame #4 saying that it is the final frame & you have previously received # 0,1,2, but not #3? Do you discard all received frames, or do you pad the missing bit with zeros (if frames are of a fixed length)?

You probably also need a similar mechanism for sent frames, where you note which are acknowledged, so that when you don't have anything more to send, you send the oldest unacknowledged frame again.  Also, some protocols have times on each frame & if they don't get a timely acknowledgement the conection is released.


Maybe if you say which protocol it is, someone will know more than me & can point you to some source.

I think this one will take some time, though.

best wishes,

Graham

0
 
Chandra VConsultantAuthor Commented:
Dear graham_k,

Thankyou for your analysation.

I am looking for someone who can give some code.

I need it in developing Protection protocol of my V5.2 Protocol Stack software.
0
 
obgCommented:
Slightly irrelevant because your compiler probably optimizes this anyway, but your modulus operation is the same as doing current_no &= 0x7F; (which even looks better from my point of view).

Here is my suggestion for the relevant part:

#define MARGIN 4
#define OK 0
#define OUT_OF_BOUNDS -1
#define RECVD -2
#define LOST -3

int check(int seq)
{
  static char rec[MARGIN + 1] = {0};
  static int current = 0, pos = 0;
  int i, j, rc = OK;

  i = seq - current + pos & 0x7F; // Array index
  if (i > pos + MARGIN)
    return OUT_OF_BOUNDS;
  if (i > pos) {
    current = seq;
    pos = i > MARGIN ? MARGIN : i;
  }
  if (i > MARGIN) { // Shift out
    for (j = 0; j < i - MARGIN; j++)
      if (!rec[j]) // Any non received shifted out?
     rc = LOST;
    memmove(rec, rec + j, MARGIN - j + 1);
    for (j = MARGIN - j + 1; j < MARGIN; j++)
      rec[j] = 0; // Clear the gap (if any)
    rec[MARGIN] = 1; // This one we just received
    pos = MARGIN;
  } else {
    if (rec[i]) // If we got it already
      return RECVD;
    else
      rec[i] = 1; // Now we do...
  }
  return rc;
}
0
 
obgCommented:
Hey, what's happenin' I kind of like my suggestion. - No comments...?
0
 
IainHereCommented:
Questions asked = 10
Grades given = 2
0
 
Chandra VConsultantAuthor Commented:
Good suggession. But I haven't got the actual solution to my problem.

Anyway thanks alot.
0
 
Chandra VConsultantAuthor Commented:
Good suggession. But I haven't got the actual solution to my problem.

Anyway thanks alot.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 5
  • 2
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now