Solved

Experts Question

Posted on 2001-06-11
12
294 Views
Last Modified: 2010-04-15
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
Comment
Question by:Chandra V
  • 5
  • 2
  • 2
  • +3
12 Comments
 
LVL 3

Expert Comment

by:3rsrichard
Comment Utility
You may just have a problem with "signed" vs "unsigned" byte.
Look up the ranges.
0
 
LVL 1

Expert Comment

by:guyss
Comment Utility
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
 
LVL 6

Expert Comment

by:graham_k
Comment Utility
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
 
LVL 2

Author Comment

by:Chandra V
Comment Utility
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
 
LVL 2

Author Comment

by:Chandra V
Comment Utility
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
 
LVL 6

Expert Comment

by:graham_k
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 2

Author Comment

by:Chandra V
Comment Utility
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
 
LVL 2

Accepted Solution

by:
obg earned 50 total points
Comment Utility
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
 
LVL 2

Expert Comment

by:obg
Comment Utility
Hey, what's happenin' I kind of like my suggestion. - No comments...?
0
 
LVL 4

Expert Comment

by:IainHere
Comment Utility
Questions asked = 10
Grades given = 2
0
 
LVL 2

Author Comment

by:Chandra V
Comment Utility
Good suggession. But I haven't got the actual solution to my problem.

Anyway thanks alot.
0
 
LVL 2

Author Comment

by:Chandra V
Comment Utility
Good suggession. But I haven't got the actual solution to my problem.

Anyway thanks alot.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now