API Documentation

From Albatross

Jump to: navigation, search

Lib/Datagram.c

  • Functions
uint16_t datagram_get_destination(datagrizzle_t *d);
void datagram_set_destination(datagrizzle_t *d, uint16_t dest);
uint16_t datagram_get_sender(datagrizzle_t *d);
void datagram_set_sender(datagrizzle_t *d, uint16_t send);
uint16_t datagram_get_command(datagrizzle_t *d);
void datagram_set_command(datagrizzle_t *d, uint16_t cmd);
uint16_t datagram_get_chsum(datagrizzle_t *d);
void datagram_set_chsum(datagrizzle_t *d, uint16_t ch);
uint8_t datagram_check_header(datagrizzle_t *d);
uint8_t datagram_build_header_checksum(datagrizzle_t *d);
uint8_t datagram_check_fletcher(datagrizzle_t *d);
uint16_t datagram_build_fletcher_checksum(datagrizzle_t *d);
int16_t datagram_process(datagrizzle_t *d, uint16_t bitMask);
void datagram_process_payload(uint8_t *vars, datagrizzle_t *d, uint16_t startAddress, uint16_t endAddress);
void buffer_print(uint8_t * buffer);
void datagram_print(datagrizzle_t *d);
void datagram_init(datagrizzle_t *d, uint16_t maxSize);
uint8_t datagram_add_float(datagrizzle_t *d, uint16_t address, float f);
uint8_t datagram_add_byte(datagrizzle_t *d, uint16_t address, uint8_t b);
uint8_t datagram_add_short(datagrizzle_t *d, uint16_t address, uint16_t s);
uint8_t datagram_add_string(datagram_t *d, uint16_t address, char *s);
uint8_t datagram_pack(datagrizzle_t *d);
void datagram_send(datagrizzle_t *d);
void datagram_clear(datagrizzle_t *d);
void *datagram_threaded_reciever(void *tsd);
uint8_t datagram_recieved_and_begin_processing(datagrizzle_t *d);
void datagram_end_processing(datagrizzle_t *d);
void datagram_set_recieve_frequency(datagrizzle_t *d, uint8_t hz);
void datagram_pause_recieve(datagrizzle_t *d);
  • Example Recieve Implementation
int main(int argc, char *argv[])
{
	//Datagram for sending
	datagrizzle_t DgSend;
	//Datagram for recieving
	datagrizzle_t DgRecv;
	//Local variable buffer
	uint8_t vars[255];
	uint8_t running;
	pthread_t rcv;

	//Initiation
	datagram_clear(&DgRecv);
	datagram_init(&DgRecv,DATAGRAM_MAX_PAYLOAD_SIZE);
	datagram_set_recieve_frequency(&DgRecv,200);
	
	//Now attach the thread and start it
	pthread_create(&rcv, NULL,&datagram_threaded_reciever,(void *)&DgRecv);
	printf("Reciever Thread Started\n");
	running = true;
	while(running)
	{
		if(datagram_recieved_and_begin_processing(&DgRecv) == true)
		{
			//printf("Received bytes from %s\n",DgRecv.from);
			cmd = datagram_process(&DgRecv,THIS_DAEMON_BITMASK);
			if(cmd != -1)
			{
				printf("Valid Datagram\n");
				if(datagram_get_command = LWC_EXIT)
				{
					running = stopped;
				}
				//datagram_print(&DgRecv);
				datagram_process_payload(vars,&DgRecv,LOGGING_D_ADDRESS_START,LOGGING_D_ADDRESS_END);
			}
			else
			{
				printf("Invalid Datagram\n");
				//buffer_print(DgRecv.payload.buffer);
			}
			datagram_end_processing(&DgRecv);
		}
		datagram_pause_recieve(&DgRecv);
	}

	printf("--DONE--\n");
	return 0;

}
  • Example Send Implementation
datagrizzle_t DgSend;

datagram_clear(&DgSend);	//Sender
datagram_init(&DgSend,DATAGRAM_MAX_PAYLOAD_SIZE);

datagram_set_destination(&DgSend,41);
datagram_set_command(&DgSend,0x1234);
datagram_set_sender(THIS_DAEMON_BITMASK);
datagram_add_byte(&DgSend,HW_D_SERVO0,100);
datagram_pack(&DgSend);
datagram_print(&DgSend);
datagram_send(&DgSend);

Python Bindings

The C API has been bound to Python, in the datagram module. See the PyDocs below:

NAME
    datagram

FILE
    /home/hugo/Projects/albatross-dev/groundstation/bindings/datagram.so

DESCRIPTION
    Pyrex wrapper for datagram.h functions from onboard/lib
    Provides the Datagram class that encapsulates the C API.
    Uses the default IP, port and buffer size.

	Example: SENDING
		import datagram
		d = datagram.Datagram()
		d.set_destination(0xFFFF)
		d.set_command(datagram.LWC_DATA)
		d.add_float(datagram.STATE_D_VELOCITY_AIRSPEED, 1.234)
		d.pack_and_send()
		d.display()

	Example: RECIEVING (THREADED)
		r = datagram.Datagram()
		r.set_start_addr(datagram.STATE_D_ADDR_START)
		r.set_end_addr(datagram.STATE_D_ADDR_END)
		r.set_recieve_frequency(200)
		r.start_threaded_recv()
		while True:
			if r.recieved_and_begin_processing():
				cmd = r.process(0xFFFF)
				if cmd == 1:
					print r.buffer_get_float(datagram.STATE_D_VELOCITY_AIRSPEED)
			r.end_processing()
			r.pause_recieve()

    Hugo Vincent, 22 June 2005.

CLASSES
    __builtin__.object
        Datagram

    class Datagram(__builtin__.object)
     |  Methods defined here:
     |
     |  add_byte(...)
     |      Add a byte to the datagram at the specified address.
     |
     |  add_float(...)
     |      Add a float value to the datagram at specified address.
     |
     |  add_int(...)
     |      Add a (32-bit) integer to the datagram at the specified address.
     |      FIXME: not yet implemented in the C API.
     |
     |  add_short(...)
     |      Add a short to the datagram at the specified address.
     |
     |  add_string(...)
     |      Add a string to the datagram at the specified address.
     |
     |  buffer_get_float(...)
     |      Return the single-precision floating point value at the
     |      specified address.
     |
     |  clear(...)
     |      Clear the datagram so it can be reused.
     |
     |  display(...)
     |      Print the datagram out in a human-readable form.
     |
     |  end_processing(...)
     |      Signal to the underlying C API that we have finished processing
     |      this datagram.
     |
     |  get_command(...)
     |      Return the LWC field.
     |
     |  get_destination(...)
     |      Return the destination field bitmask.
     |
     |  get_local_vars(...)
     |      Returns the datagram address mapped local variable buffer.
     |
     |  get_sender(...)
     |      Return the sender bitmask.
     |
     |  pack_and_send(...)
     |      Pack the datagram (including adding the checksums etc.) and
     |      send the datagram by multicast.
     |
     |  pause_recieve(...)
     |      Pause/sleep for a time set with set_recieve_frequency().
     |
     |  process(...)
     |      Process a recieved datagram, extracting all the variables that
     |      are in this daemons address space (set with set_start_addr(),
     |      set_end_addr()). Data can subsequently be extracted from the
     |      datagram with buffer_get_float() et al.
     |
     |  recieved_and_begin_processing(...)
     |      Checks if the reciever thread has recieved a datagram;
     |      if so, returns True, else returns False.
     |
     |  send_log_message(...)
     |      Send a log message to the LogD.
     |
     |  set_command(...)
     |      Set the Light Weight Command field.
     |
     |  set_destination(...)
     |      Set the destination field bitmask.
     |
     |  set_end_addr(...)
     |      Set the end address for the local address space for this daemon.
     |
     |  set_recieve_frequency(...)
     |      Sets the polling frequency of the threaded reciever.
     |
     |  set_sender(...)
     |      Set the sender field bitmask.
     |
     |  set_start_addr(...)
     |      Set the start address for the local address space for this daemon.
     |
     |  start_threaded_recv(...)
     |      Start a separate thread that listens for incoming datagrams.
     |
     |  stop_threaded_recv(...)
     |      Stop a reciever thread started with start_threaded_recv().
     |
     |  ----------------------------------------------------------------------
     |  Data and other attributes defined here:
     |
     |  __new__ = <built-in method __new__ of type object>
     |      T.__new__(S, ...) -> a new object with type S, a subtype of T

DATA
    BUFFER_SIZE = 300
    DEST_COMMD = 22
    DEST_GPSD = 8
    DEST_HWD = 4
    DEST_LOGD = 1
    DEST_SUPERVISORD = 2
    HW_D_ADDR_END = 15
    HW_D_ADDR_START = 0
    HW_D_BUFSIZE = 15
    HW_D_SERVO0 = 0
    HW_D_SERVO1 = 2
    HW_D_SERVO2 = 4
    HW_D_SERVO3 = 6
    HW_D_SERVO4 = 8
    HW_D_SERVO5 = 10
    HW_D_SERVO6 = 12
    HW_D_SERVO7 = 14
    LOCAL_SIZE = 256
    LOGGING_D_ADDR_END = 165
    LOGGING_D_ADDR_START = 145
    LOGGING_D_BUFSIZE = 20
    LOGGING_D_MESSAGE = 145
    LOGGING_D_MESSAGE10 = 154
    LOGGING_D_MESSAGE11 = 155
    LOGGING_D_MESSAGE12 = 156
    LOGGING_D_MESSAGE13 = 157
    LOGGING_D_MESSAGE14 = 158
    LOGGING_D_MESSAGE15 = 159
    LOGGING_D_MESSAGE16 = 160
    LOGGING_D_MESSAGE17 = 161
    LOGGING_D_MESSAGE18 = 162
    LOGGING_D_MESSAGE19 = 163
    LOGGING_D_MESSAGE2 = 146
    LOGGING_D_MESSAGE20 = 164
    LOGGING_D_MESSAGE21 = 165
    LOGGING_D_MESSAGE3 = 147
    LOGGING_D_MESSAGE4 = 148
    LOGGING_D_MESSAGE5 = 149
    LOGGING_D_MESSAGE6 = 150
    LOGGING_D_MESSAGE7 = 151
    LOGGING_D_MESSAGE8 = 152
    LOGGING_D_MESSAGE9 = 153
    LWC_ACK = 2
    LWC_DATA = 1
    LWC_HWD_EXIT = 3
    LWC_LOGD_EXIT = 6
    LWC_STATED_EXIT = 4
    LWC_SUPERVISORD_EXIT = 5
    LWC_TESTD_EXIT = 7
    MAX_MESSAGE_LENGTH = 21
    MAX_PAYLOAD_SIZE = 200
    STATE_D_ADDR_END = 143
    STATE_D_ADDR_START = 16
    STATE_D_ALTITUDE = 24
    STATE_D_BUFSIZE = 127
    STATE_D_LATITUDE = 16
    STATE_D_LONGITUDE = 20
    STATE_D_P = 96
    STATE_D_PHI = 120
    STATE_D_PHI_DOT = 132
    STATE_D_PSI = 128
    STATE_D_PSI_DOT = 140
    STATE_D_P_DOT = 108
    STATE_D_Q = 100
    STATE_D_Q_DOT = 112
    STATE_D_R = 104
    STATE_D_R_DOT = 116
    STATE_D_THETA = 124
    STATE_D_THETA_DOT = 136
    STATE_D_U = 64
    STATE_D_U_DOT = 76
    STATE_D_V = 68
    STATE_D_VELOCITY_AIRSPEED = 88
    STATE_D_VELOCITY_GROUND = 92
    STATE_D_V_DOT = 80
    STATE_D_W = 72
    STATE_D_W_DOT = 84
    STATE_D_XW = 28
    STATE_D_XW_DOT = 52
    STATE_D_XW_LAUNCH = 40
    STATE_D_YW = 32
    STATE_D_YW_DOT = 56
    STATE_D_YW_LAUNCH = 44
    STATE_D_ZW = 36
    STATE_D_ZW_DOT = 60
    STATE_D_ZW_LAUNCH = 48
    SUPERVISOR_D_ADDR_END = 144
    SUPERVISOR_D_ADDR_START = 144
    SUPERVISOR_D_BUFSIZE = 0
    SUPERVISOR_D_CHECK_INTERCAL = 144
    TEST_D_ADDR_END = 166
    TEST_D_ADDR_START = 166
    TEST_D_BUFSIZE = 0
    TEST_D_TEST = 166
    UDP_FROM_STRING = 30
Personal tools