Logo

Examples

In below sections the reader will find example code for the two most important aspects of the library. Namely de decoding and encoding of Asterix Messages. More examples can be found in the Technical Note document do be found in the document section.


Decoding of Asterix Messages

Decoding a received binary stream into the correct Asterix message is a pretty simple task. First of all we need to figure out which Asterix Category the received data might be encoded in. According to the Asterix standard we can see that the category of the message is encoded in the very first byte. Discarding most of the error handling and also to shorten the example to the bare minimum, this would look like.
		
  0   char msg[1000]; // Replace the 1000 with whatever the maximum expected size is
  1   // it is assumed the socked is set up properly
  2   socket.receive( msg, 1000 );
  3   // In reality this needs some sanity checks and error handlig
  4   int cat = static_cast<unsigned char>( msg[0] );
	
Determine the category
Now, that the category to be used has been determined, it is time to start the decoding. For this example the category received shall be 21, ADS-B target reports.
		
  0   #include <deque>
  1   #include <map>
  2   #include <memory>
  3   #include <iostream>
  4   #include "AsterixCategory021.h"
  5  
  6   int main( int argc, char* argv[] ){
  7      std::deque<char> message( msg[2], msg[msg.size()-1] );
  8      AsterixCategory021 astx = AsterixCategory021( message.size(), message );
  9  
 10      // No access some items of the message
 11      if( astx.isItemPresent( Cat021ItemNames::I021_010 ) ){
 12         std::cout<<"Item 010 SAC/SIC is present SAC: " << astx.getValue( Cat021ItemNames::I021_010_SAC )
 13            << " SIC: " << getValue( Cat021ItemNames::I021_010_SIC ) <<std::endl;
 14      }
 15      return 0;
 16   }
	
Decoding Cat 021
Although it is only a few lines, lets still go through the code in more detail as not every line might be entirely clear in its underlying purpose. The first thing that need to be done is to clean up the received message. By this we mean removing the first byte representing the Asterix Category and then remove the next two bytes consisting of the length of the message. For simplicity we assume there is only one Asterix message with in the received data and thus we can just pass remaining bytes tio the encoder without the need to worry about spillover or other length issues. So the only thing left to do is kick off the decoding by passing the bytes to the constructor of the Asterix category. After that we can access the elements. To do so we first have to check if the item in question was part of the message and if that was the case, we can retrieve it. Accessing items without checking their presence first, might result in out_of_bounds exceptions.


Encoding of Asterix Messages

Encoding might appear a bit more complicated then decoding, but the only difference is to set up the category first. In this example we will use Asterix category 62. So letz dive right into the code.
		
  0  #include  <memory>
  1  #include  <map>
  2  #include  "AsterixCategory062.h"
  3  #include  "TrackTypesIf.h"
  4  #include  "AsterixItemMaxAges.h"
  5  #include  "DetectionEntry.h"
  6  
  7  int main( int argc, char* argv[] ){
  8     AsterixCategory062 message = AsterixCategory062();
  9  
 10     // Define the input items and add them to the message
 11     std::shared_ptr<TrackTypesIf> track = std::make_shared<TrackTypesIf>();
 12     std::map<std::string, unsigned char> additional_items;
 13     additional_items[Cat062ItemNames::I062_010_SAC] = (unsigned char) 1;
 14     additional_items[Cat062ItemNames::I062_010_SIC] = (unsigned char) 2;
 15     message.setNonTrackRelatedValues( additional_items );
 16  
 17     // Determine which items shall be contained in the message
 18     std::map<std::string, bool> items_to_be_served;
 19     items_to_be_served[Cat062ItemNames::I062_010] = true;
 20  
 21     std::shared_ptr<AsterixItemMaxAges> max_ages;
 22     max_ages = std::make_shared<AsterixItemMaxAges>();
 23     max_ages->setMaxAgePerType( DetectionEntry::DET_TYPE::ADS_B, 10.0 );
 24     max_ages->setMaxAgePerType( DetectionEntry::DET_TYPE::PSR, 10.0 );
 25  
 26     // Encode the message
 27     std::vector<unsigned char> msg = message.getEncodedMessage( track, items_to_be_served, max_ages );
 28  
 29     return 0;
 30  }
	
Encoding Asterix Category 062
There are several ways to provide the Asterix Category object with the required information. In this example most of them are provided to the encode function directly,. But depending on the category it might also be possible to call a series of setter methods beforehand. The returned byte vector can now be send via the network or to a storage location. In the above code there are a few lines which might not seem necessary at first. One example of this is setting up the maximum ages for some detection items. This is required to decide which items still contribute to the message and of which detection type, like PSR, MDS, etc. the message shall be encoded. As this is configuration there is no default defined in the Asterix standard. In the same category falls the map of items to be served in the output. Each user can decide which items it is interested in and only those items will be part of the message. Most of the other items are directly populated by the provided track itself. Nearly every category will have such a main datatype.


Further Reading

More examples can be found in the Technical Note document to be found in the document section. If there are further questions, it is advised to contact us.


Asterix Lib availabe

The initial version of the Asterix Library is now available in the download section. In addition to that the git [...]


Two Projects Under One Roof

During the initial development of the FixMPTS software component, it became more and more clear, that the entire package should [...]


Launch of site

Although the project existed already for quite some time it is only presented to the public as of this moment. [...]