KlvLib  1.58
klvtestappuds.cpp
// Copyright IMPLEOTV SYSTEMS LTD. ALL RIGHTS RESERVED
// KlvTestApp.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <stdio.h>
#include <memory.h>
#include <iostream>
#include "IKlvLib.h"
#include "Windows.h"
using namespace std;
#define BUFFER_SIZE 2048
int _tmain(int argc, _TCHAR* argv[])
{
char* pEncChunk;
size_t encodedLength = 0;
size_t encodedLength2 = 0;
char klvBuffer[BUFFER_SIZE]; // Create a buffer to hold the results
char klvBuffer2[BUFFER_SIZE]; // Create another buffer to hold the results
memset( klvBuffer, 0x0, BUFFER_SIZE );
/* Example 1.
Let's assume we have to KLV encode a hypothetical fast MMS data packet.
|------------------------------------------------------------------------------------------------------------------------|
|Tag| Name | Value | Interpretation | TLV Hex Bytes |
|------------------------------------------------------------------------------------------------------------------------|
| 2| UNIX Time Stamp | 1231798102000000 | Mon Jan 12 2009 22:08:22 (UTC) | 02 08 00 04 60 50 58 4E 01 80 |
| | | microseconds | | |
| 5| Platform Heading Angle | 0x71C2 | 159.9744 Degrees | 05 02 71 C2 |
| 6| Platform Pitch Angle | 0xFD3D | -0.4315251 Degrees | 06 02 FD 3D |
| 7| Platform Roll Angle | 0x08B8 | 3.405814 Degrees | 07 02 08 B8 |
| 13| Sensor Latitude | 0x5595B66D | 60.17682296 Degrees | 0D 04 55 95 B6 6D |
| 14| Sensor Longitude | 0x5B5360C4 | 128.42675904 Degrees | 0E 04 5B 53 60 C4 |
| 15| Sensor True Altitude | 0xC221 | 14190.72 Meters | 0F 02 C2 21 |
| 16| Sensor Horizontal FoV | 0xCD9C | 144.5713 Degrees | 10 02 CD 9C |
| 17| Sensor Vertical FoV | 0xD917 | 152.6436 Degrees | 11 02 D9 17 |
| 18| Sensor Rel. Azimuth Angle | 0x724A0A20 | 160.71921147 Degrees | 12 04 72 4A 0A 20 |
| 19| Sensor Rel. Elevation Angle | 0x87F84B86 | -168.79232483 Degrees | 13 04 87 F8 4B 86 |
| 20| Sensor Rel. Roll Angle | 0x00000000 | 0.0 Degrees | 14 04 00 00 00 00 |
| 21| Slant Range | 0x03830926 | 68590.98 Meters | 15 04 03 83 09 26 |
| 22| Target Width | 0x1281 | 722.8199 Meters | 16 02 12 81 |
| 23| Frame Center Latitude | 0xF101A229 | -10.54238863 Degrees | 17 04 F1 01 A2 29 |
| 24| Frame Center Longitude | 0x14BC082B | 29.15789012 Degrees | 18 04 14 BC 08 2B |
| 25| Frame Center Elevation | 0x34F3 | 3216.037 Meters | 19 02 34 F3 |
| 65| UAS LDS Version | 0x02 | MISB Standard 0601.2 | 41 01 02 |
| |
|------------------------------------------------------------------------------------------------------------------------|
*/
// All metadata shall be represented using big-endian encoding, i.e. the most significant byte (MSB) is first. Bytes shall be big-endian bit
// encoding – with the most significant bit (msb) first.
try
{
// 1. Create STANAG 4609 KLV encoder
IKlvEncoder* klvEncoder = CreateKlvEncoder(IKlvItem::SIXTEEN_BYTES, TRUE);
// 2. Set outer key. We use ASCII string for this example
klvEncoder->SetOuterKey("060E2B34020101010E01010201010000");
// 3. Set checksum key
klvEncoder->SetCheckSumKey("060E2B34040101010E01020301000000");
// 4. Add Items
// Key 02 | A timestamp. Unix Time Stamp - Microseconds L = 8. Mon Jan 12 2009 22:08:22 (UTC) 00 04 60 50 58 4E 01 80
unsigned __int64 val_64 = 1231798102000000; // After endian swap - { 0x00, 0x04, 0x60, 0x50, 0x58, 0x4E, 0x01, 0x80 };
// You can use either binary or ASCII form (Binary is more efficent, as it doesn't need ASCII to BIN conversion
char keyArr[16] = { 0x06, 0x0e, 0x2b, 0x34, 0x01, 0x01, 0x01, 0x03, 0x07, 0x02, 0x01, 0x01, 0x01, 0x05, 0x00, 0x00 };
klvEncoder->AddKlvItemBin(keyArr, val_64);
// ASCII form
//klvEncoder->AddKlvItem("060E2B34010101030702010101050000", val_64);
// Key 05 | Add Platform Heading Angle | 0x71C2 | 159.9744 Degrees
unsigned short val_16 = 0x71C2;
klvEncoder->AddKlvItem("060E2B34010101070701100106000000", val_16);
// Key 06 | Add Platform Pitch Angle | 0xFD3D | -0.4315251 Degrees
val_16 = 0xFD3D;
klvEncoder->AddKlvItem("060E2B34010101070701100105000000", val_16);
// Key 07 | Add Platform Roll Angle | 0x08B8 | 3.405814 Degrees
val_16 = 0x08B8;
klvEncoder->AddKlvItem("060E2B34010101070701100104000000", val_16);
// Another option to add it:
// EndianSwap( val_16 );
// klvEncoder->AddKlvItem("060E2B34010101070701100104000000", IKlvItem::IKlvItem::SIXTEEN_BYTES, (const char*)&val_16, sizeof val_16);
// Key 13 | Add Sensor Latitude | 0x5595B66D | 60.17682296 Degrees
// Here, lets demonstrate how to convert a latitude into 4 bytes LDS using supplied IStanag4609Converter class (optional)
// double lat = 60.17682296;
// unsigned int val_32 = IStanag4609Converter::LatUDS2LDS(lat); // NOT INCLUDED IN THIS DEMO
unsigned int val_32 = 0x5595B66D;
klvEncoder->AddKlvItem("060E2B34010101030701020102040200", val_32);
// Key 14 | Add Sensor Longitude | 0x5B5360C4 | 128.42675904 Degrees
val_32 = 0x5B5360C4;
klvEncoder->AddKlvItem("060E2B34010101030701020102060200", val_32);
// Key 15 | Add Sensor True Altitude | 0xC221 | 14190.72 Meters
val_16 = 0xC221;
klvEncoder->AddKlvItem("060E2B34010101010701020102020000", val_16);
// Key 16 | Add Sensor Horizontal FoV | 0xCD9C | 144.5713 Degrees
val_16 = 0xCD9C;
klvEncoder->AddKlvItem("060E2B34010101020420020101080000", val_16);
// Key 17 | Add Sensor Vertical FoV | 0xD917 | 152.6436 Degrees
val_16 = 0xD917;
klvEncoder->AddKlvItem("060E2B340101010704200201010A0100", val_16);
// Key 18 | Add Sensor Rel. Azimuth Angle | 0x724A0A20 | 160.71921147 Degrees
val_32 = 0x724A0A20;
klvEncoder->AddKlvItem("060E2B34010101010E01010204000000", val_32);
// Key 19 | Sensor Rel. Elevation Angle | 0x87F84B86 | -168.79232483 Degrees
val_32 = 0x87F84B86;
klvEncoder->AddKlvItem("060E2B34010101010E01010205000000", val_32);
// Key 20 | Sensor Rel. Roll Angle | 0x00000000 | 0.0 Degrees
val_32 = 0x00000000;
klvEncoder->AddKlvItem("060E2B34010101010E01010206000000", val_32);
// Key 21 | Slant Range | 0x03830926 | 68590.98 Meters
val_32 = 0x03830926;
klvEncoder->AddKlvItem("060E2B34010101010701080101000000", val_32);
// Key 22 | Target Width | 0x1281 | 722.8199 Meters
val_16 = 0x1281;
klvEncoder->AddKlvItem("060E2B34010101010701090201000000", val_16);
// Key 23 | Frame Center Latitude | 0xF101A229 | -10.54238863 Degrees
val_32 = 0xF101A229;
klvEncoder->AddKlvItem("060E2B34010101010701020103020000", val_32);
// Key 24 | Frame Center Longitude | 0x14BC082B | 29.15789012 Degrees
val_32 = 0x14BC082B;
klvEncoder->AddKlvItem("060E2B34010101010701020103040000", val_32);
// Key 25 | Frame Center Elevation | 0x34F3 | 3216.037 Meters
val_16 = 0x34F3;
klvEncoder->AddKlvItem("060E2B340101010A0701020103160000", val_16);
// Key 65 | UAS LDS Version | 0x02 | MISB Standard 0601.2
unsigned char val_8 = 0x02;
klvEncoder->AddKlvItem("060E2B34010101010E01020303000000", val_8);
pEncChunk = klvEncoder->Encode(encodedLength);
// 7. Use the results
if( encodedLength )
{
// Do something with the data
memcpy(klvBuffer, pEncChunk, encodedLength);
}
val_64 = 1231798102000000;
klvEncoder->AddKlvItemBin(keyArr, val_64);
// Key 13 | Add Sensor Latitude | 0x5595B66D | 60.17682296 Degrees
val_32 = 0x5595B66D;
klvEncoder->AddKlvItem("060E2B34010101030701020102040200", val_32);
// Key 14 | Add Sensor Longitude | 0x5B5360C4 | 128.42675904 Degrees
val_32 = 0x5B5360C4;
klvEncoder->AddKlvItem("060E2B34010101030701020102060200", val_32);
// Key 15 | Add Sensor True Altitude | 0xC221 | 14190.72 Meters
val_16 = 0xC221;
klvEncoder->AddKlvItem("060E2B34010101010701020102020000", val_16);
// Encode and return an encoded byte array
pEncChunk = klvEncoder->Encode(encodedLength2);
// Use the results
if( encodedLength2 )
{
// Do something with the data
memcpy(klvBuffer2, pEncChunk, encodedLength2);
}
// 9. Delete the KlvEncoder and deallocate memory
delete klvEncoder;
/* ----------------------------------------------------------------------------------------------- */
// Another example. Parse previously encoded buffer into KlvItems list
// 1. Create STANAG 4609 KLV decoder
IKlvDecoder* klvDecoder = CreateKlvDecoder();
//IStanag4609Converter* stanagConverter = CreateStanag4609Converter(); // NOT INCLUDED IN THIS DEMO
// 2. Parse the buffer that contains Klv encoded data
if( klvDecoder->Parse(IKlvItem::SIXTEEN_BYTES, IKlvItem::SIXTEEN_BYTES, klvBuffer, encodedLength ))
{
int itemCount;
IKlvItem** itemArray = klvDecoder->GetItemList(itemCount);
char valueStr[256];
for(int i = 0; i < itemCount; i++)
{
const char* keyStr = itemArray[i]->GetKeyString();
int length = itemArray[i]->GetLength();
unsigned char* value = (unsigned char* )itemArray[i]->GetValue();
for(int i=0; i<length; i++)
sprintf(valueStr + i*2, "%02X", *(value+i));
cout << keyStr << ": Ox" << valueStr << endl;
}
}
//3. Delete KLV Decoder
delete klvDecoder;
//delete stanagConverter; // NOT INCLUDED IN THIS DEMO
}
catch(char * str )
{
cout << "Exception raised: " << str << '\n';
}
getchar();
return 0;
}
Untitled 1




 Copyright 2010,    IMPLEOTV SYSTEMS LTD