#include "serial_communications.h"
/** @name packetise.c
This file contains functions pertaining to the construction of packets.
@author Daniel Storey, UWA, 1998
@version 1.0 */
/*@{*/
/*************************************************************************/
/** Encode the length of data in a regular packet.
Encode an integer representing packet data length as an ASCII string.
@param len int, representing the length which is to be encoded.
@param a string, in which the encoded value is to be written.
@return void. */
/*************************************************************************/
void int_2_packet_length(char a[3], int len)
{
int base = 80, offset = 48;
char i = 0, j = 0;
if(lenmy_address[0], ADDRESS_LEN);
packet.source_address[ADDRESS_LEN] = '\0';
/* Number */
packet.number[0] = increment_packet_number(channel);
packet.number[1] = '\0';
/* Frame Type */
strncpy(&packet.type[0], type, PACKET_TYPE_BYTES);
packet.type[PACKET_TYPE_BYTES] = '\0';
/* Data */
length = packet_length(packet.type);
if((data[0]!='\0')&&(data!=NULL)) {
strncpy(&packet.data[0], data, length);
packet.data[length] = '\0';
}
else {
packet.data[0] = '\0';
}
/* Define and Add CRC Code */
crc(&packet.crc[0], packet.data);
/* End of packet symbol */
packet.end[0]= PACKET_END;
packet.end[1]='\0';
/* Packet has not yet been sent */
packet.sent = 0;
return packet;
}
/*************************************************************************/
/** Generate the preamble.
Write the preamble to the channel variable.
@param channel Channel, the variable holding the channel data.
@return void. */
/*************************************************************************/
void write_preamble(Channel *channel)
{
int i;
/* preamble */
for (i=0; i<(PREAMBLE_BYTES-LOCK_BYTES-AFTER_BYTES); i++)
channel->preamble[i]=0x55;
/* lock UART */
for (i=0; ipreamble[PREAMBLE_BYTES-LOCK_BYTES-AFTER_BYTES+i]= (unsigned char) 0xff;
for(i=0; ipreamble[PREAMBLE_BYTES-AFTER_BYTES+i]= (unsigned char) 0x55;
/* Add end of string Character */
channel->preamble[PREAMBLE_BYTES]='\0';
return;
}
/*************************************************************************/
/** Break a string into a packet.
Break the string that is passed to the function and place it in a packet.
@param packet Packet, the packet to which data is to be written.
@param address string, the string from which the data is to be taken.
@return Packet, the completed packet. */
/*************************************************************************/
Packet receive_packet(Packet packet, const char* string)
{
int i = 0, j = 0, length = 0;
/* Start */
packet.start[0] = PACKET_START;
packet.start[1] = '\0';
i++;
/* Destination Address */
for(j=0; jnext_address[0], &type[0], &data[0], channel);
/* Remove CRC from packet */
packet.crc[0] = '\0';
return packet;
}
/*************************************************************************/
/** Increment the channel to indicate a packet has been sent.
Carefully change the value of last_packet_number to indicate that a channel has been sent.
@param channel Channel, the variable holding the channel data.
@return char, the value of last_packet_number in the channel variable. */
/*************************************************************************/
char increment_packet_number(Channel *channel)
{
if(channel->last_packet_number == 126)
channel->last_packet_number = 46;
else
channel->last_packet_number++;
return channel->last_packet_number;
}
/*************************************************************************/
/** Increment the channel to indicate a packet has been sent.
Carefully decrement the value of last_packet_number. This is used when a special packet is generated to ensure packet numbers are not wasted.
@param channel Channel, the variable holding the channel data.
@return char, the value of last_packet_number in the channel variable. */
/*************************************************************************/
void decrement_packet_number(Channel *channel)
{
if(channel->last_packet_number == 46)
channel->last_packet_number = 126;
else
channel->last_packet_number--;
}