UKHAS Wiki

UK High Altitude Society

User Tools

Site Tools


projects:high_altitude_slug:software:protocol

This is an old revision of the document!


Protocol documentation

This is for coordinating our development.

Basics

First two bytes contain the command. The next two bytes is the length of the data, then the rest is data.

Commands

We have a 16 unsigned integer for commands:

Human messages (0 - 9)

  • [0 - 9] CMD_MESSAGE_[0-9] string - Human messages. 0 is lowest priority, 9 is highest.

All processes (10 - 4999)

GPS reader (5000 - 5999)

Flight management (6000 - 6999)

Roll control (7000 - 7999)

  • 7000 CMD_DESIRED_HEADING double - The desired heading calculated by the flight management.

Pitch control (8000 - 8999)

Client Library Functions

client_init

The client_init function should be called before any other client related functions and connects to serverAddr (probably localhost) on port port. Both of these values should be #defined in config.h as SERVER and PORT respectively. The function is prototyped as:

int client_init (char *serverAddr, unsigned short port);

in client.h

On success client_init function returns the socket file descriptor of the server that it has connected to which should be passed to all client-related functions. On failure the function returns -1 and makes a note in the client error log.

client_recv_packet

The client_recv_packet function is used to read packets of data from the server. It reads a single packet of data from serverFD (the servers network file descriptor returned by client_init) and then places the packet into packet which should be a pointer to a client_packet structure. It is prototyped as:

int client_recv_packet (unsigned short serverFD, struct client_packet *packet);

in client.h

The client_recv_packet function will automatically perform byte-conversions on packet→command and packet→len using the ntohs function.

First of all the function will read 4 bytes of data from the serverFD, convert them to the host byte order and then use the packet→len variable to work out the length of the data segment of the packet. Next, malloc is used to allocate enough memory to store the data segment of the packet and the null byte (packet→len + 1), before going onto reading the data segment into the newly allocated string and then setting the last byte to \0.

Please note the the data segment of the packet (packet→data) is made up of memory allocated off of the heap, and so needs to be free()'ed when no longer needed to prevent a memory leak!

On success this function will return 0 while on failure this function will return -1 and will make a note in the error log about exactly what went wrong.

client_recv_packets

The client_recv_packets function is very similar to the client_recv_packet function, except that it will return all of the packets in the buffer and will allocate memory as required. Unlike client_recv_packet the client_recv_packets function is non-blocking and will return if there is nothing in the buffer. It is prototyped as:

struct client_packet *client_recv_packets(unsigned short serverFD, int *npackets);

in client.h

Since this function makes calls to the client_recv_packet function to actually read the data from the buffer it also performs byte-conversions on command and len member variables.

client_send_packet

The client_send_packet function should be used to send a packet of information to the server (aka the 'party line'). It sends packet to serverFD (which is the servers file descriptor) and is prototyped as:

int client_send_packet (unsigned short serverFD, struct client_packet *packet);

in client.h

Like the client_recv_packet function the client_send_packet function will automatically perform byte-conversions on both packet→command and packet→len to convert them to the network byte order using htons function.

On success the function will return 0 otherwise, on failure, will return -1 and make a note in the error log about what went wrong.

client_close

The client_close function simply closes serverFD by calling the close function (which it is no more than a wrapper over) and is prototyped as:

void client_close (unsigned short serverFD)

in client.h

client_close should be called when the connection to the server ('party line') is no longer needed.

Example Application

Here is a simple application that demonstrates the use of these functions. The program basically forks (creates a copy of) itself. Both of the processes then connect to the server, once a connection has been established the child process will then wait for 0.25 seconds, giving the parent process time to send two packets of data to the party line at which point the parent process then exits. After the child processes 0.25 second wait has elapsed it will call the client_recv_packets function to read all of the packets from the server (which should be the two that the parent sent) and then prints them out to the terminal before free()ing all of the memory allocated by the client_recv_packets function and exiting.

projects/high_altitude_slug/software/protocol.1152286024.txt.gz · Last modified: 2008/07/19 23:31 (external edit)

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki