KlvLib  1.58
Using KlvLib sample
// 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"
#include <fstream>
#include <fstream>
using namespace std;
#define BUFFER_SIZE 2048
void WriteBinData(const char* path, char* data, size_t len);
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
unsigned __int64 val_64;
unsigned int val_32;
unsigned short val_16;
unsigned char val_8;
char key;
memset( klvBuffer, 0x0, BUFFER_SIZE );
memset( klvBuffer2, 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 |
| 1| Checksum | 0xC84C | 0xC84C | 01 02 C8 4C |
|------------------------------------------------------------------------------------------------------------------------|
*/
// 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();
//Create EG0601 Converter instance
// IEG0601Converter* conv = CreateEG0601Converter(); // NOT INCLUDED IN THIS DEMO
// 2. Set outer key. We use ASCII string for this example. It is also possible to set it in bynary form.
klvEncoder->SetOuterKey("060E2B34020B01010E01030101000000");
// In case of 16 bytes UDS the following key should be used:
//klvEncoder->SetOuterKey("060E2B34020101010E01010201010000");
// 3. Set checksum key
klvEncoder->SetCheckSumKey("01");
// Or in binary form:
//char chkSumkey = 1;
//klvEncoder->SetCheckSumKey(&chkSumkey, IKlvItem::ONE_BYTE);
// In case of 16 bytes UDS the following key should be used:
//klvklvEncoderEncoderUDS->SetCheckSumKey("060E2B34040101010E01020301000000");
// This is a demonstration of how to add 16 bytes value
//unsigned __int64 val_64 = 1231798102000000;
//EndianSwap(val_64); // Now we have { 0x00, 0x04, 0x60, 0x50, 0x58, 0x4E, 0x01, 0x80 };
//char arr[16] = { 0x06, 0x0e, 0x2b, 0x34, 0x01, 0x01, 0x01, 0x07, 0x07, 0x01, 0x10, 0x01, 0x04, 0x00, 0x00, 0x00 };
//klvEncoder->AddKlvItem(arr, IKlvItem::SIXTEEN_BYTES, (const char*)&val_64, sizeof val_64);
//klvEncoder->AddKlvItem("060e2b34010101070701100104000000", IKlvItem::SIXTEEN_BYTES, (const char*)&val_64, sizeof val_64);
// 4. Add Items
//for(int j = 0; j <1000000; j++)
//{
// 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
val_64 = 1231798102000000; // After endian swap - { 0x00, 0x04, 0x60, 0x50, 0x58, 0x4E, 0x01, 0x80 };
key = 2;
klvEncoder->AddKlvItem(key, val_64);
// Key 05 | Add Platform Heading Angle | 0x71C2 | 159.9744 Degrees
val_16 = 0x71C2;
key = 5;
klvEncoder->AddKlvItem(key, val_16);
// Key 06 | Add Platform Pitch Angle | 0xFD3D | -0.4315251 Degrees
val_16 = 0xFD3D;
key = 6;
klvEncoder->AddKlvItem(key, val_16);
// Key 07 | Add Platform Roll Angle | 0x08B8 | 3.405814 Degrees
val_16 = 0x08B8;
key = 7;
klvEncoder->AddKlvItem(key, val_16);
// Another option to add it:
// EndianSwap( val_16 );
// klvEncoder->AddKlvItem(&key, IKlvItem::ONE_BYTE, (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 EG0601Converter (optional)
// Convert Latitude to 4 bytes (int32)
// char* p = conv->EncodeValue( 13, 60.17682296, length); // returns pointer to the 4 bytes buffer containing converted value.
val_32 = 0x5595B66D;
key = 13;
klvEncoder->AddKlvItem(key, val_32);
// Key 14 | Add Sensor Longitude | 0x5B5360C4 | 128.42675904 Degrees
val_32 = 0x5B5360C4;
key = 14;
klvEncoder->AddKlvItem(key, val_32);
// Key 15 | Add Sensor True Altitude | 0xC221 | 14190.72 Meters
val_16 = 0xC221;
key = 15;
klvEncoder->AddKlvItem(key, val_16);
// Key 16 | Add Sensor Horizontal FoV | 0xCD9C | 144.5713 Degrees
val_16 = 0xCD9C;
key = 16;
klvEncoder->AddKlvItem(key, val_16);
// Key 17 | Add Sensor Vertical FoV | 0xD917 | 152.6436 Degrees
val_16 = 0xD917;
key = 17;
klvEncoder->AddKlvItem(key, val_16);
// Key 18 | Add Sensor Rel. Azimuth Angle | 0x724A0A20 | 160.71921147 Degrees
val_32 = 0x724A0A20;
key = 18;
klvEncoder->AddKlvItem(key, val_32);
// Key 19 | Sensor Rel. Elevation Angle | 0x87F84B86 | -168.79232483 Degrees
val_32 = 0x87F84B86;
key = 19;
klvEncoder->AddKlvItem(key, val_32);
// Key 20 | Sensor Rel. Roll Angle | 0x00000000 | 0.0 Degrees
val_32 = 0x00000000;
key = 20;
klvEncoder->AddKlvItem(key, val_32);
// Key 21 | Slant Range | 0x03830926 | 68590.98 Meters
val_32 = 0x03830926;
key = 21;
klvEncoder->AddKlvItem(key, val_32);
// Key 22 | Target Width | 0x1281 | 722.8199 Meters
val_16 = 0x1281;
key = 22;
klvEncoder->AddKlvItem(key, val_16);
// Key 23 | Frame Center Latitude | 0xF101A229 | -10.54238863 Degrees
val_32 = 0xF101A229;
key = 23;
klvEncoder->AddKlvItem(key, val_32);
// Key 24 | Frame Center Longitude | 0x14BC082B | 29.15789012 Degrees
val_32 = 0x14BC082B;
key = 24;
klvEncoder->AddKlvItem(key, val_32);
// Key 25 | Frame Center Elevation | 0x34F3 | 3216.037 Meters
val_16 = 0x34F3;
key = 25;
klvEncoder->AddKlvItem(key, val_16);
// Key 65 | UAS LDS Version | 0x02 | MISB Standard 0601.2
val_8 = 0x02;
key = 65;
klvEncoder->AddKlvItem(key, val_8);
// If you added items in different order and want to sort them by tag, you can use a sort method.
//klvEncoder->Sort();
// 6. Encode and return an encoded byte array
pEncChunk = klvEncoder->Encode(encodedLength);
// }
// 7. Use the results
if( encodedLength )
{
// Do something with the data. Copy it to the buffer
memcpy(klvBuffer, pEncChunk, encodedLength);
// Or Write it to file
WriteBinData("data.bin", klvBuffer, encodedLength);
}
// Create another KLV packet. Note, when you start adding items after Encode function is called, all previously added items will
// be deleted and a memory alocated for the encoded data will be released.
// 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
val_64 = 1231798102000000; // After endian swap - { 0x00, 0x04, 0x60, 0x50, 0x58, 0x4E, 0x01, 0x80 };
key = 2;
klvEncoder->AddKlvItem(key, val_64);
// Key 13 | Add Sensor Latitude | 0x5595B66D | 60.17682296 Degrees
val_32 = 0x5595B66D;
key = 13;
klvEncoder->AddKlvItem(key, val_32);
// Key 14 | Add Sensor Longitude | 0x5B5360C4 | 128.42675904 Degrees
val_32 = 0x5B5360C4;
key = 14;
klvEncoder->AddKlvItem(key, val_32);
// Key 15 | Add Sensor True Altitude | 0xC221 | 14190.72 Meters
val_16 = 0xC221;
key = 15;
klvEncoder->AddKlvItem(key, val_16);
// Encode and return an encoded byte array
pEncChunk = klvEncoder->Encode(encodedLength2);
// Use the results
if( encodedLength2 )
{
// Do something with the data. Copy it to the buffer
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::ONE_BYTE, klvBuffer, encodedLength ))
{
int itemCount;
IKlvItem** itemArray = klvDecoder->GetItemList(itemCount);
char valueStr[1024];
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 conv; // NOT INCLUDED IN THIS DEMO
}
catch(char * str )
{
cout << "Exception raised: " << str << '\n';
}
getchar();
return 0;
}
void WriteBinData(const char* path, char* data, size_t len)
{
ofstream myFile (path, ios::out | ios::binary);
myFile.write (data, len);
}
Untitled 1




 Copyright 2010,    IMPLEOTV SYSTEMS LTD