|
Klv2CoT Converter Version 1.0
|
Example of KLV to CoT conversion using Klv2CoTConv library. For more info on CoT data see Sample CoT Message example
#include <IKlv2CoTConv.h> #include <stdio.h> #include <fstream> #include <iostream> #include <string> #include <list> using namespace std; #if !defined(WIN32) const char* CoTFile = "/home/alexc/workspace/cot2klvws/CoT2KlvTest/CoT.xml"; const char* KlvFile = "/home/alexc/workspace/cot2klvws/CoT2KlvTest/Klv.bin"; #else const char* CoTFile = "CoT.xml"; const char* KlvFile = "Klv.bin"; #endif void writeFile(const char* path, const char* cot_buf, size_t len); int main() { IKlv2CoTConv* pKlv2CoTConv; size_t len; string xmlStrPP, xmlStrSPI; string path; cout << "Klv2CoT Test Application" << endl; try { // Create Converter (LDS test) pKlv2CoTConv = CreateKlv2CoTConv(IKlv2CoTConv::LDS); // Set COT operational mode to PLATFORM_POSITION_AND_SPI in order to generate two types of Situational Awareness messages pKlv2CoTConv->SetCoTMode(IKlv2CoTConv::PLATFORM_POSITION_AND_SPI); // Set Affiliation info pKlv2CoTConv->SetAffiliationInfo(IKlv2CoTConv::AF_Friend); // Set BattleDimension info pKlv2CoTConv->SetBattleDimensionInfo(IKlv2CoTConv::BD_Air); //Set Function type pKlv2CoTConv->SetFunctionTypeInfo("M-F-O"); // Create Klv Items list std::list<IKlvItem*> klvItemsList; // Create Klv Item IKlvItem* item = CreateKlvItem(); // 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 }; char key = 2; item->Init((const char*)&key, IKlvItem::ONE_BYTE, (const char*)&val_64, sizeof(val_64), len ); klvItemsList.push_back(item); // Create Klv Item 3 Mission ID item = CreateKlvItem(); const char* str = "Mission 4"; key = 3; item->Init((const char*)&key, IKlvItem::ONE_BYTE, str, strlen(str)+1, len ); klvItemsList.push_back(item); // Create Klv Item 10 Platform Designation item = CreateKlvItem(); str = "Predator"; key = 10; item->Init((const char*)&key, IKlvItem::ONE_BYTE, str, strlen(str)+1, len ); klvItemsList.push_back(item); // Create Klv Item item = CreateKlvItem(); unsigned int val_32 = 0x5595B66D; key = 13; item->Init((const char*)&key, IKlvItem::ONE_BYTE, (const char*)&val_32, sizeof(val_32), len ); klvItemsList.push_back(item); // Create Klv Item item = CreateKlvItem(); // Key 14 | Add Sensor Longitude | 0x5B5360C4 | 128.42675904 Degrees val_32 = 0x5B5360C4; key = 14; item->Init((const char*)&key, IKlvItem::ONE_BYTE, (const char*)&val_32, sizeof(val_32), len ); klvItemsList.push_back(item); // Create Klv Item item = CreateKlvItem(); // Key 15 | Add Sensor True Altitude | 0xC221 | 14190.72 Meters unsigned short val_16 = 0xC221; key = 15; item->Init((const char*)&key, IKlvItem::ONE_BYTE, (const char*)&val_16, sizeof(val_16), len ); klvItemsList.push_back(item); // Parse Klv buffer and fill the CoT with the encoded data if(pKlv2CoTConv->Process(&klvItemsList, xmlStrPP, xmlStrSPI)) { // Write data to XML file. writeFile(path.c_str(), xmlStrPP.c_str(), xmlStrPP.length()) ; } else cout << "Error parsing Klv buffer" << endl; for(std::list<IKlvItem*>::iterator list_iter = klvItemsList.begin(); list_iter != klvItemsList.end(); list_iter++) { delete *list_iter; } } catch(Klv2CoTConvException& err) { cout << err.what() << endl; } // Delete converter if(pKlv2CoTConv) delete pKlv2CoTConv; return 0; } // write data to XML file void writeFile(const char* path, const char* cot_buf, size_t len) { fstream xml_file(path, ios::out|ios::app); xml_file.write(cot_buf, len); xml_file.close(); }