Garmin Fleet Management Controller  2.19.0
GarminLinkLayer Class Reference

Link Layer in the OSI model. More...

#include <GarminLinkLayer.h>

+ Inheritance diagram for GarminLinkLayer:

Classes

struct  fifo_type
 A circular FIFO buffer for processing received bytes into packets. More...
 

Public Member Functions

void resetPhysicalLayer (PhysicalLayer *aPort)
 Reset the physical layer. More...
 
virtual void rx (uint8 const *const aData, uint32 const aSize)
 Copy Data to FIFO. More...
 
void setPhysicalLayer (PhysicalLayer *aPort)
 Set the physical layer. More...
 
void setTransportLayer (TransportLayer *aTransport)
 Set the transport layer. More...
 
virtual bool tx (Packet *aPacket)
 Transmit a packet. More...
 

Static Public Member Functions

static void destroyInstance ()
 Delete the one and only link layer object. More...
 
static GarminLinkLayergetInstance ()
 Get the one and only link layer object. More...
 

Protected Attributes

PhysicalLayermPhysicalLayer
 The serial port that this link layer communicates with. More...
 
TransportLayermTransportLayer
 The transport layer that this link layer communicates with. More...
 

Private Member Functions

 GarminLinkLayer ()
 Construct a new GarminLinkLayer. More...
 
void getDataFromFifo (uint8 *aData, uint32 aSize)
 Get Data From FIFO. More...
 
bool getPacketFromFifo ()
 Frame Packet. More...
 
void incrementHead (uint32 aIncrement)
 Increment FIFO head. More...
 
int numBytesInFifo ()
 Calculate the number of bytes in the FIFO buffer. More...
 

Private Attributes

fifo_type mFifo
 FIFO (First-In, First-Out) buffer used to store bytes received from the client until they are parsed into packets. More...
 

Static Private Attributes

static GarminLinkLayersInstance = NULL
 The one and only instance of this class. More...
 

Detailed Description

Link Layer in the OSI model.

In the OSI model, the Data Link layer provides encapsulation of data packets into frames, frame synchronization, and error control. For Fleet Management, encapsulation means adding the frame header, frame footer, and sequencing and DLE-stuffing the elements of the packet before transmitting a packet as a frame, and removing the DLE stuffing and verifying the checksum on incoming frames. Frame synchronization is performed by receiving bytes from the serial port into a FIFO buffer, then searching the data received to find valid frames (starting with DLE and ending with DLE-ETX), and sending any valid frames received up to the TransportLayer. Error control means responding to each received frame with either ACK or NAK, depending on whether the frame was received correctly.

Definition at line 39 of file GarminLinkLayer.h.

Constructor & Destructor Documentation

Member Function Documentation

void GarminLinkLayer::destroyInstance ( )
static

Delete the one and only link layer object.

Definition at line 40 of file GarminLinkLayer.cpp.

References sInstance.

Referenced by getInstance().

void GarminLinkLayer::getDataFromFifo ( uint8 aData,
uint32  aSize 
)
private

Get Data From FIFO.

This procedure copies data from the FIFO and places it into a persistent buffer. It does not advance the pointers (i.e., the data remains in the buffer).

Parameters
aDataThe data buffer to write to
aSizeThe number of bytes to retrieve.
Warning
Behavior is undefined if packet_size is greater than the number of bytes in the FIFO.

Definition at line 318 of file GarminLinkLayer.cpp.

References GarminLinkLayer::fifo_type::buffer, GarminLinkLayer::fifo_type::head, incrementHead(), mFifo, and GarminLinkLayer::fifo_type::size.

Referenced by getPacketFromFifo().

GarminLinkLayer * GarminLinkLayer::getInstance ( )
static

Get the one and only link layer object.

Returns
The link layer
Note
GarminLinkLayer is a singleton because this application is designed to communicate with a single client device. In a server-based application, there must be one link layer per connected client.

Definition at line 27 of file GarminLinkLayer.cpp.

References destroyInstance(), GarminLinkLayer(), and sInstance.

Referenced by SerialPort::initSerialPort(), and TcpIpPort::initTcpPort().

bool GarminLinkLayer::getPacketFromFifo ( )
private

Frame Packet.

This procedure processes raw data in the FIFO into Garmin packets, and adds the packets to the rx_queue for processing. While doing so, it also sends ACK/NAK link-level responses as appropriate, and logs the packets that are received.

Each invocation of getPacketFromFifo will add at most one packet to the rx_queue.

Returns
TRUE if it found a valid Garmin packet in the FIFO, FALSE otherwise.
Warning
This implementation is not thread-safe - this routine manipulates the fifo head pointer during the search for a complete packet, then resets it to retrieve the packet; therefore appending data to the fifo or even obtaining the size of the buffer during a get operation is unsafe. A multi-threaded implementation would need to either copy to a temporary buffer, or protect all fifo use with a mutex.

Definition at line 155 of file GarminLinkLayer.cpp.

References GarminLinkLayer::fifo_type::buffer, header_type::dle, footer_type::dle, footer_type::etx, FALSE, FIFO_SIZE, getDataFromFifo(), GarminLinkLayer::fifo_type::head, header_type::id, ID_ACK_BYTE, ID_DLE_BYTE, ID_ETX_BYTE, ID_NAK_BYTE, incrementHead(), Logger::logRawData(), GarminPacket::mChecksum, mFifo, GarminPacket::mFrame, GarminPacket::mFrameSize, MIN_PACKET_SIZE, GarminPacket::mPacketId, GarminPacket::mPayload, GarminPacket::mPayloadSize, LinkLayer::mTransportLayer, numBytesInFifo(), TransportLayer::rx(), GarminLinkLayer::fifo_type::size, header_type::size, SIZE_OF_FOOTER, SIZE_OF_HEADER, GarminLinkLayer::fifo_type::tail, TRUE, and TransportLayer::tx().

Referenced by rx().

void GarminLinkLayer::incrementHead ( uint32  aIncrement)
private

Increment FIFO head.

This procedure increments the receive FIFO's head by the amount requested, effectively removing the data from the FIFO. It will not increment the head beyond the tail, and it also accounts for wrapping.

Parameters
aIncrementThe number of bytes to remove from the FIFO.

Definition at line 349 of file GarminLinkLayer.cpp.

References GarminLinkLayer::fifo_type::head, mFifo, GarminLinkLayer::fifo_type::size, GarminLinkLayer::fifo_type::tail, and tx().

Referenced by getDataFromFifo(), and getPacketFromFifo().

int GarminLinkLayer::numBytesInFifo ( )
private

Calculate the number of bytes in the FIFO buffer.

This procedure calculates the number of bytes between the head and tail of the receive FIFO.

Returns
The number of bytes held in the FIFO buffer.

Definition at line 124 of file GarminLinkLayer.cpp.

References GarminLinkLayer::fifo_type::head, mFifo, GarminLinkLayer::fifo_type::size, and GarminLinkLayer::fifo_type::tail.

Referenced by getPacketFromFifo().

void GarminLinkLayer::resetPhysicalLayer ( PhysicalLayer aPort)

Reset the physical layer.

The existing physical layer is removed and replaced with a new physical layer.

Definition at line 65 of file GarminLinkLayer.cpp.

References FIFO_SIZE, GarminLinkLayer::fifo_type::head, mFifo, LinkLayer::mPhysicalLayer, rx(), PhysicalLayer::setLinkLayer(), LinkLayer::setPhysicalLayer(), GarminLinkLayer::fifo_type::size, and GarminLinkLayer::fifo_type::tail.

Referenced by GarminLinkLayer(), SerialPort::initSerialPort(), and TcpIpPort::initTcpPort().

void GarminLinkLayer::rx ( uint8 const *const  aData,
uint32 const  aSize 
)
virtual

Copy Data to FIFO.

This procedure appends data (from the PC's com port) to a local rx FIFO, then calls getPacketFromFifo.

Parameters
aDataBuffer containing the bytes to add to the FIFO
aSizeNumber of bytes to add

Implements LinkLayer.

Definition at line 94 of file GarminLinkLayer.cpp.

References GarminLinkLayer::fifo_type::buffer, getPacketFromFifo(), mFifo, GarminLinkLayer::fifo_type::size, and GarminLinkLayer::fifo_type::tail.

Referenced by resetPhysicalLayer().

void LinkLayer::setPhysicalLayer ( PhysicalLayer aPort)
inherited

Set the physical layer.

Set the physical layer that this LinkLayer sends bytes to and receives bytes from.

Parameters
aPortThe physical layer, or NULL if none.

Definition at line 41 of file LinkLayer.cpp.

References LinkLayer::mPhysicalLayer, and LinkLayer::setTransportLayer().

Referenced by GarminLinkLayer(), resetPhysicalLayer(), LinkLayer::~LinkLayer(), SerialPort::~SerialPort(), and TcpIpPort::~TcpIpPort().

void LinkLayer::setTransportLayer ( TransportLayer aTransport)
inherited

Set the transport layer.

Set the transport layer that this LinkLayer sends packets to and receives packets from.

Parameters
aTransportThe physical layer, or NULL if none.

Definition at line 55 of file LinkLayer.cpp.

References LinkLayer::mTransportLayer.

Referenced by GarminTransportLayer::GarminTransportLayer(), LinkLayer::setPhysicalLayer(), and GarminTransportLayer::~GarminTransportLayer().

bool GarminLinkLayer::tx ( Packet aPacket)
virtual

Transmit a packet.

Build a packet into a raw frame and call the physical layer to transmit it.

Parameters
aPacketThe packet to build into a frame
Returns
True if the packet was sent successfully, false otherwise

Implements LinkLayer.

Definition at line 400 of file GarminLinkLayer.cpp.

References ID_DLE_BYTE, ID_ETX_BYTE, Logger::logRawData(), GarminPacket::mChecksum, GarminPacket::mFrame, GarminPacket::mFrameSize, GarminPacket::mPacketId, GarminPacket::mPayload, GarminPacket::mPayloadSize, LinkLayer::mPhysicalLayer, and PhysicalLayer::tx().

Referenced by incrementHead(), and GarminTransportLayer::sendPacket().

Member Data Documentation

fifo_type GarminLinkLayer::mFifo
private

FIFO (First-In, First-Out) buffer used to store bytes received from the client until they are parsed into packets.

Definition at line 93 of file GarminLinkLayer.h.

Referenced by GarminLinkLayer(), getDataFromFifo(), getPacketFromFifo(), incrementHead(), numBytesInFifo(), resetPhysicalLayer(), and rx().

PhysicalLayer* LinkLayer::mPhysicalLayer
protectedinherited

The serial port that this link layer communicates with.

Definition at line 74 of file LinkLayer.h.

Referenced by GarminLinkLayer(), resetPhysicalLayer(), LinkLayer::setPhysicalLayer(), tx(), and LinkLayer::~LinkLayer().

TransportLayer* LinkLayer::mTransportLayer
protectedinherited

The transport layer that this link layer communicates with.

Definition at line 77 of file LinkLayer.h.

Referenced by getPacketFromFifo(), and LinkLayer::setTransportLayer().

GarminLinkLayer * GarminLinkLayer::sInstance = NULL
staticprivate

The one and only instance of this class.

Definition at line 89 of file GarminLinkLayer.h.

Referenced by destroyInstance(), and getInstance().


The documentation for this class was generated from the following files: