GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 15-Feb-2005, 11:13
nkhambal nkhambal is offline
Regular Member
 
Join Date: Jul 2004
Location: CA USA
Posts: 313
nkhambal is a jewel in the roughnkhambal is a jewel in the rough

Help needed to understand the requirement


Hi all,

I am trying to develop an application protocol for testing. I have the requirement for the packet structure. But the data types in the requirement are little confusing to me. Can someone pls help me in identifying these data types.?

Format of the statments below is
=========================
Tyep Length Description
=========================
Quote:
1. CARD8 1 A single byte unsigned integer
2. CARD16 2 Two byte unsigned integer
3. CARD32 4 Four byte unsigned integer
4. ARRAY8 n+2 This is actually a CARD16 followed by a collection of CARD8.
The value of the CARD16 field (n) specifies the number of CARD8 values to
follow
5. ARRAY16 2*m+1 This is a CARD8 (m) which specifies the number of
CARD16 values to follow
6. ARRAY32 4*l+1 This is a CARD8 (l) which specifies the number of CARD32
values to follow
7. ARRAYofARRAY8 ? This is a CARD8 which specifies the number of ARRAY8
values to follow.
How should I define variables to hold these data types?

Thanks,
  #2  
Old 15-Feb-2005, 11:53
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,720
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by nkhambal
Hi all,

I am trying to develop an application protocol for testing. I have the requirement for the packet structure. But the data types in the requirement are little confusing to me. Can someone pls help me in identifying these data types.?

Format of the statments below is
=========================
Tyep Length Description
=========================

How should I define variables to hold these data types?

Thanks,

If your compiler has <stdint.h>, use the types there.

For example:
CPP / C++ / C Code:
/* 7.18.1.1  Exact-width integer types */
typedef signed char int8_t;
typedef unsigned char   uint8_t;
typedef short  int16_t;
typedef unsigned short  uint16_t;
typedef int  int32_t;
typedef unsigned   uint32_t;
typedef long long  int64_t;
typedef unsigned long long   uint64_t;

So you can do something like this for the basic data types.

CPP / C++ / C Code:
#include <stdio.h>
#include <stdint.h>

typedef uint8_t CARD8;
typedef uint16_t CARD16;
typedef uint32_t CARD32;


int main()
{
  CARD8 ubyte;
  CARD16 ushort;
  CARD32 uint;
  printf("sizeof(ubyte)  = %d\n", sizeof(ubyte));
  printf("sizeof(ushort) = %d\n", sizeof(ushort));
  printf("sizeof(uint)   = %d\n", sizeof(uint));

  return 0;
}

Now as for the ARRAY8 array. They are storing an integer value in the first two bytes of a byte array. This is particularly yukky, since the actual order of the first two bytes stored depends on whether the system is big-endian or little-endian. The issue of byte-ordering raises its ugly head for the other arrays as well. (So we need a little more information about the specification.)

I can show you a trick or two to handle this, but first: does your system have <stdint.h> so that the above code works?

(If not, you have to tell me what compiler you are using. I know ways of getting to the bottom of things with Borland and Microsoft compilers --- I'll tell you how I did it if you need to see it.)

Regards,

dave
Last edited by davekw7x : 15-Feb-2005 at 12:47.
  #3  
Old 15-Feb-2005, 13:02
nkhambal nkhambal is offline
Regular Member
 
Join Date: Jul 2004
Location: CA USA
Posts: 313
nkhambal is a jewel in the roughnkhambal is a jewel in the rough
Hi Dave,

I use gcc compiler on linux. I have <stdint.h>. The data types you showed in your example works on my complier.

Following is one more requirement of the protocol,

"Integer values are always stored most significant byte first in the packet (‘‘Big Endian’’ order")."

Thanks,
  #4  
Old 15-Feb-2005, 13:30
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,720
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by nkhambal
Hi Dave,

I use gcc compiler on linux. I have <stdint.h>. The data types you showed in your example works on my complier.

Following is one more requirement of the protocol,

"Integer values are always stored most significant byte first in the packet (‘‘Big Endian’’ order")."

Thanks,

So, to store quantities greater than a byte, get the bytes in order. One way is by simple bit manipulations (the other is by arithmetic operations)..

CPP / C++ / C Code:
           /* note the first two bytes are a placeholder for the byte count */
  CARD8  chary[100];
  int NumOfBytes;
.

.

.
  /* now suppose we want to have 10 bytes in the array */
  NumOfBytes = 10;
  chary[0] = NumOfBytes >> 8; /* most sig 8 bits */
  chary[1] = NumOfBytes & 0xff; /* least sig 8 bits */
  chary[2] = (First data byte here)
.
  chary[11] = (Tenth data byte here)
.
.

To get the bytecount back and print the array, you could have something like this:

CPP / C++ / C Code:
/* Note that the byte count is stored in the first two bytes
 * of the array in big-endian order.
 */
void PrintArray(CARD8 *array)
{
  int i;
  int NumOfBytes;

  NumOfElements = (array[0] << 8) | array[1]; 
  printf("NumOfBytes = %d\n", NumOfBytes);

  for (i = 2; i <  NumOfBytes + 2; i++) {
    printf("0x%02x ", array[i]);
  }
  printf("\n");
}

Get the idea?

(Of course you could define functions or macros to put the bytes into the larger quantities and get them back again.)

Regards,

Dave
  #5  
Old 15-Feb-2005, 16:33
machinated machinated is offline
Regular Member
 
Join Date: Mar 2004
Location: victoria, canada
Posts: 324
machinated has a spectacular aura aboutmachinated has a spectacular aura about
just wondering, is it "high and low endian" or "big and little endian"?
__________________
spasms!!!
  #6  
Old 15-Feb-2005, 16:51
nkhambal nkhambal is offline
Regular Member
 
Join Date: Jul 2004
Location: CA USA
Posts: 313
nkhambal is a jewel in the roughnkhambal is a jewel in the rough
Hi machinated,

Its the later one...

Dave,

Just saw your reply. Tryin to understand it. Will get back to you.

Thanks,
  #7  
Old 15-Feb-2005, 17:03
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,720
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by machinated
just wondering, is it "high and low endian" or "big and little endian"?


Is it style or is it substance? (Potato or potahto?)

Little-endian is a play on the old nursery rhyme "One little-, two little-, three little- Native Americans." That was OK when we said it 1973 (to distinguish between Intel and Motorola microprocessors). The Political Correction commandos may have coerced some into calling it high-end and low-end, but it just doesn't have that certain ring to it (at least to me).

Regards,

Dave
  #8  
Old 15-Feb-2005, 17:52
nkhambal nkhambal is offline
Regular Member
 
Join Date: Jul 2004
Location: CA USA
Posts: 313
nkhambal is a jewel in the roughnkhambal is a jewel in the rough
Hi Dave,

I got the presentation of CARD8 variable.

But how will I represent ARRAYOFARRAY8 ?

My packet looks like the one below.

CPP / C++ / C Code:

typedef struct xquerypacket_
{
	XPacket genX;
	XQueryPacket queryX;
}XQuery;

/* Generic XDMCP Packet*/

typedef struct xpakcet_
{
	CARD16 version;
	CARD16 opcode;
	CARD16 msglength;	
}XPacket;

/* END */

/*Variable Payload in XDMCP Packet*/

typedef struct _xquerypacket_
{
	CARD8 authname; /*This should be type ARRAYOFARRAY8*/	
}XQueryPacket;


I am filling the packet as below.

CPP / C++ / C Code:
query->genX.version = htons(1);
query->genX.opcode = htons(QUERY);
query->genX.msglength = htons(1);
query->queryX.authname = htons(0);
  #9  
Old 15-Feb-2005, 20:02
nkhambal nkhambal is offline
Regular Member
 
Join Date: Jul 2004
Location: CA USA
Posts: 313
nkhambal is a jewel in the roughnkhambal is a jewel in the rough
Hi Dave,

I have figured out that the problem was in my "sendto" statement (UDP datagram socket send operation).

I was calculating the wrong packet length.

Thanks,
  #10  
Old 15-Feb-2005, 20:02
machinated machinated is offline
Regular Member
 
Join Date: Mar 2004
Location: victoria, canada
Posts: 324
machinated has a spectacular aura aboutmachinated has a spectacular aura about
how are you representing any of your arrays? array8 specifies a card16 followed by card8s - which is fine if you have an array of card8s, u can join 2 card8s to form card 16. but array16 is a card8 followed by card16 elements. and same for array32 - i know they say 2*m+1 and 4*l+1 respectively, but does that imply that you are to have 2 times the number of card8 elements for array16 and 4 times the number of card8 elements for array32? how would you go about manipulating values in each element? you'd have to do a lot of bit manipulating! maybe you should have structures, that would certainly make things easier, and since contents of a structure are stored contigously in memory then manipulating the values with pointers wouldn't be a problem either if need be.
e.g.

CPP / C++ / C Code:
struct array16
{
   CARD8 numelements;
   CARD16 card16[100];
};

what do you think? unless they've specified that you are to create arrays and not structures. or maybe you already have structures? then forget about this whole message!

so then arrayofarray8 would be a struct that would have a card8 and an array of array8 structures.
CPP / C++ / C Code:

struct arrayofarray8
{
 CARD8 numarrays
 array8[100];
};


for completeness's sake:
CPP / C++ / C Code:

struct array8
{
 CARD16 numelem;
 CARD8 card8[100];
};

I'm not saying that my ideas are the best ways to implement your problem. I am just making guesses here. but it's worth a try. good luck
__________________
spasms!!!
 
 

Recent GIDBlogDeveloping GUIs with wxPython (Part 2) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
how to use resources, general info needed. drewdaman MS Visual C++ / MFC Forum 1 14-Dec-2004 11:57
Knight tour (arrays help needed) dilmv C++ Forum 7 18-Oct-2004 14:31
urgent help needed :c + mysql insert jack C Programming Language 1 13-Apr-2004 21:16
Free 1st month / Free setup / No credit card needed...Plans start at 4.95 LarryIsaac Web Hosting Advertisements & Offers 0 11-Oct-2003 14:03

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 20:24.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.