Klv2CoT Converter Version 1.0
Sample Code

Reading binary file containing sample KLV packet and generating CoT messages.

#include <IKlv2CoTConv.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          

char* ReadBinData(const char* path, int& l);

void writeFile(const char* path, const char* cot_buf, size_t len);


int main()
{
                IKlv2CoTConv* pKlv2CoTConv;
                size_t len;
                string xmlStrPP, xmlStrSPI;
                string pathMessage1 = "c:\\SP.xml";
                string pathMessage2 = "c:\\SPI.xml";
                string path;

            cout << "Klv2CoT Test Application" << endl;
            try
            {
                        // Create Converter (LDS test)
                        pKlv2CoTConv = CreateKlv2CoTConv(IKlv2CoTConv::KLV_MODE::LDS);

                        // Create Converter (UDS test)
                        //pKlv2CoTConv = CreateKlv2CoTConv(IKlv2CoTConv::KLV_MODE::UDS);

                        // Read Klv bin data into the buffer
                        cout << "Please enter bin file path:" << endl;
                    getline (cin, path);
                    int l;
                    char* klv_buf =  ReadBinData(path.c_str(), l);
                        
                        // Parse Klv buffer and fill the CoT with the encoded data
                        if(pKlv2CoTConv->Process(klv_buf, len, xmlStrPP, xmlStrSPI))
                        {
                                // Write data to XML file.
                                 writeFile(pathMessage1.c_str(), xmlStrPP.c_str(),xmlStrPP.length());  
                                 writeFile(pathMessage2.c_str(), xmlStrSPI.c_str(),xmlStrSPI.length()) ;     
                        }
                        else
                                cout << "Error parsing Klv buffer" << endl;

            }
            catch(Klv2CoTConvException& err)
            {
                cout << err.what() << endl;
            }

                // Delete converter
            if(pKlv2CoTConv)
                delete pKlv2CoTConv;


        return 0;
}

char* ReadBinData(const char* path, int& l)
 {
  char * buffer;

  ifstream is;
  is.open (path, ios::binary );

  // get length of file:
  is.seekg (0, ios::end);
  l = is.tellg();
  is.seekg (0, ios::beg);

  buffer = new char [l];
 
  is.read (buffer,l);
  is.close(); 
  
  return buffer;
}

// 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();
}

Processing a list of KLV items and generating CoT messages.

#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();
}
Untitled 1




 Copyright 2011,    IMPLEOTV SYSTEMS LTD