|
CoT2Klv Converter Version 1.0
|
Example of COT to KLV conversion using CoT2KlvConv library. For more info on CoT data see Sample CoT Message example
#include <ICoT2KlvConv.h> #include <stdio.h> #include <fstream> #include <iostream> #include <string> 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 const char secSampleData[] = {0x01, 0x01, 0x01, 0x02, 0x01, 0x01, 0x03, 0x06, 0x00, 0x00, 0x00, 0x00, 0x55, 0x53, 0x16, 0x02, 0x04, 0x00}; string readFile( const char* filePath); void writeFile(const char* path, const unsigned char* klv_buf, size_t len); int main() { ICoT2KlvConv* pCoT2KlvConv; unsigned char klv_buf[2048]; size_t len; cout << "CoT2Klv Test Application" << endl; try { // Create Converter pCoT2KlvConv = CreateCoT2KlvConv(ICoT2KlvConv::LDS); pCoT2KlvConv->SetSecurityMetadata((const unsigned char*)secSampleData, sizeof( secSampleData)); // Read CoT xml data into the buffer string CoTBuf = readFile(CoTFile); // Print it cout << CoTBuf << endl; // Parse CoT buffer and fill the klv_buf with the encoded data if(pCoT2KlvConv->Process(CoTBuf.c_str(), klv_buf, len)) { // Do something with the data, for example write it to file. writeFile(KlvFile, klv_buf, len); } else cout << "Error parsing CoT buffer" << endl; } catch(CoT2KlvConvException& err) { cout << err.what() << endl; } // Delete converter if(pCoT2KlvConv) delete pCoT2KlvConv; return 0; } // Read the XML file containing simulated CoT packet string readFile( const char* filePath) { ifstream ifs(filePath); if (ifs) { string s((std::istreambuf_iterator<char>(ifs)), istreambuf_iterator<char>()); return s; } return ""; } // write buffer to bin file void writeFile(const char* path, const unsigned char* klv_buf, size_t len) { fstream binary_file(path, ios::out|ios::binary|ios::app); binary_file.write((const char*)klv_buf, len); binary_file.close(); }