Solved

Experts Question

Posted on 2001-06-11
12
307 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
ID: 6177125
You may just have a problem with "signed" vs "unsigned" byte.
Look up the ranges.
0
 
LVL 1

Expert Comment

by:guyss
ID: 6180006
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
ID: 6180192
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
ID: 6180268
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
ID: 6180286
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
ID: 6180484
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
Make managing Office 365 email signatures a breeze

Are you using Office 365? Having trouble trying to set up email signatures for your users? Getting stressed out managing multiple signatures? Need an easier way to manage? We have a solution for you, try the most-user friendly and powerful signature management tool on the market.

 
LVL 2

Author Comment

by:Chandra V
ID: 6180527
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
ID: 6181143
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
ID: 6198040
Hey, what's happenin' I kind of like my suggestion. - No comments...?
0
 
LVL 4

Expert Comment

by:IainHere
ID: 6213714
Questions asked = 10
Grades given = 2
0
 
LVL 2

Author Comment

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

Anyway thanks alot.
0
 
LVL 2

Author Comment

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

Anyway thanks alot.
0

Featured Post

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

863 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

22 Experts available now in Live!

Get 1:1 Help Now