// File: /workspace/github_cpp_makefile_star2_O3/Marm0t[P]dji-phantom3-parser/parser.cpp
// Function: main
int main(int argc, char** argv)
{
    if (argc < 2 )
    {
        cout << "You should provide filename as an argument" << endl;
        return 1;
    }
    int num = 1;
    if(*(char *)&num == 1)
    {
       // cout << "Little-Endian"<<endl;
    }
    else
    {
        cout << "Big-Endian: exit because initial files are little endian and we're too lazy to do it by ourselves"<<endl;
        return 1;
    }
    string filename(argv[1]);
    cout << "File name: " << filename << endl;

    ifstream file(filename.c_str(), ios::binary);
    if (file.fail() || !file.is_open())
    {
        cout << "Cannot open file " << filename << endl;
        return 1;
    }

    char aBuf;
    int packetsNumber = 0;
    vector<Packet> packets01cf;
    streampos aPos;
    //ignore everything till beginning of first packet (0x55 marker)
    while(!file.eof() && !file.fail() && aBuf != 0x55)
    {
        aPos = file.tellg();
        file.read(&aBuf, 1); // read one byte
    }
    if (file.eof() || file.fail())
    {
        cout << "No dji-phantom-3 packets found in your file!" << endl;
        return 1;
    }
    // go back one byte in the file so we start reading packet from the beginning
    file.seekg(aPos);

    cout << GPSPacket::csvHeader();
    // now lets read all packets one by one
    while (!file.eof() && !file.fail()) 
    {
        // packet example:
        //# 0  1  2  3  4  5  6  7 ...
        // 55 28 00 6c 5c 00 70 f7 01 00 70 70 6e 70 71 70 10 70 78 70 62 88 70 70 70 70 70 70 70 70 70 70 70 70 4e 9d 71 70 60 a7
        //  ^ - we start from here and should read LEN bytes
        // LEN = byte#1 (in example above it is 0x28 (hex) = 40 (dec)
        //Packet packet;
        GPSPacket packet;
        file.read(&aBuf, 1);
        if (aBuf != 0x55)
        {
            // this happens very often especially at the beginning of file
            //cout << "Wrong packet start marker: " << hex << (int)aBuf << endl;
            continue;
        }
        packet.push_back(aBuf);

        // reading byte#1 (packet length)
        file.read(&aBuf, 1);
        uint8_t packetLen = aBuf;
        if (packetLen < PACKET_HEADER_LEN)
        {
            cout << "Packet length is too small (" << dec << packetLen << "), packet corrupted" << endl;
            continue;
        }
        packet.push_back(aBuf);
        
        // reading byte#2: must be always 0x00!
        file.read(&aBuf, 1);
        if (aBuf != 0x00)
        {
            //cout << "Corrupted packet header: byte#2 is not 0x00: " << hex << (int)aBuf << endl;
            continue;
        }
        packet.push_back(aBuf);

        // now lets read the rest of the packet accordint to packet len from byte#1
        // note thaat we have already read 3 first bytes (#0, #1 and #3)
        for (uint8_t i=3; i<packetLen; ++i)
        {
            file.read(&aBuf, 1);
            packet.push_back(aBuf);
        }

        // Packet is ready for parsing!
        packet.parseHex();
        if (packet.isValid())
        {
            cout << packet.toString();
            //cout << packet.toCsvString();
        }
        packetsNumber++;
    }

//    cout << "Number of packets found: " << dec << packetsNumber << endl;
    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x308, %rsp            # imm = 0x308
cmpl $0x1, %edi
jg 0x2411
movq 0x4c04(%rip), %rbx      # 0x6fd0
leaq 0x2c31(%rip), %rsi      # 0x5004
movl $0x2a, %edx
movq %rbx, %rdi
callq 0x2170
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x2140
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x20d0
movl $0x1, %ebx
jmp 0x25aa
movq 0x8(%rsi), %rsi
leaq -0x70(%rbp), %rdi
leaq -0x328(%rbp), %rdx
callq 0x298c
movq 0x4ba4(%rip), %rdi      # 0x6fd0
leaq 0x2bfc(%rip), %rsi      # 0x502f
movl $0xb, %edx
callq 0x2170
movq -0x70(%rbp), %rsi
movq -0x68(%rbp), %rdx
movq 0x4b84(%rip), %rdi      # 0x6fd0
callq 0x2170
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x2140
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x20d0
movq -0x70(%rbp), %rsi
leaq -0x328(%rbp), %rdi
movl $0x4, %edx
callq 0x2220
movq -0x328(%rbp), %rax
movq -0x18(%rax), %rax
testb $0x5, -0x308(%rbp,%rax)
jne 0x252a
leaq -0x2b0(%rbp), %rdi
callq 0x2040
testb %al, %al
je 0x252a
xorps %xmm0, %xmm0
movaps %xmm0, -0x120(%rbp)
movq $0x0, -0x110(%rbp)
xorl %ebx, %ebx
leaq -0x328(%rbp), %r15
leaq -0x29(%rbp), %r12
xorl %r14d, %r14d
movq -0x328(%rbp), %rax
movq -0x18(%rax), %rax
movl -0x308(%rbp,%rax), %eax
testb $0x2, %al
jne 0x25c2
testb $0x5, %al
jne 0x25be
cmpb $0x55, -0x29(%rbp)
je 0x25be
movq %r15, %rdi
callq 0x21f0
movq %rax, %rbx
movq %rdx, %r14
movl $0x1, %edx
movq %r15, %rdi
movq %r12, %rsi
callq 0x2180
jmp 0x24de
movq 0x4a9f(%rip), %rdi      # 0x6fd0
leaq 0x2b03(%rip), %rsi      # 0x503b
movl $0x11, %edx
callq 0x2170
movq -0x70(%rbp), %rsi
movq -0x68(%rbp), %rdx
movq 0x4a7f(%rip), %rdi      # 0x6fd0
callq 0x2170
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x2140
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movl $0x1, %ebx
movq %rax, %rdi
callq 0x20d0
leaq -0x328(%rbp), %rdi
callq 0x2050
leaq -0x60(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x25aa
movq -0x60(%rbp), %rsi
incq %rsi
callq 0x2160
movl %ebx, %eax
addq $0x308, %rsp            # imm = 0x308
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
testb $0x5, %al
je 0x261e
movq 0x4a07(%rip), %rbx      # 0x6fd0
leaq 0x2a7d(%rip), %rsi      # 0x504d
movl $0x2c, %edx
movq %rbx, %rdi
callq 0x2170
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x2140
movsbl %al, %esi
movq 0x49d5(%rip), %rdi      # 0x6fd0
callq 0x2030
movl $0x1, %ebx
movq %rax, %rdi
callq 0x20d0
leaq -0x120(%rbp), %rdi
callq 0x29ca
jmp 0x2585
leaq -0x328(%rbp), %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x2190
leaq -0x100(%rbp), %rdi
callq 0x43ee
movq -0x100(%rbp), %rsi
movq -0xf8(%rbp), %rdx
movq 0x497f(%rip), %rdi      # 0x6fd0
callq 0x2170
leaq -0xf0(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x2675
movq -0xf0(%rbp), %rsi
incq %rsi
callq 0x2160
movq -0x328(%rbp), %rax
movq -0x18(%rax), %rax
testb $0x7, -0x308(%rbp,%rax)
jne 0x28dd
leaq -0xf8(%rbp), %r14
leaq 0x46cc(%rip), %rax      # 0x6d68
addq $0x10, %rax
movq %rax, -0x80(%rbp)
leaq -0x328(%rbp), %r15
leaq -0x29(%rbp), %r12
leaq -0x50(%rbp), %r13
leaq 0x4676(%rip), %rax      # 0x6d30
addq $0x10, %rax
movq %rax, -0x78(%rbp)
movw $0x0, -0xde(%rbp)
movl $0x0, -0xdc(%rbp)
xorl %eax, %eax
movb %al, -0xd8(%rbp)
xorps %xmm0, %xmm0
movups %xmm0, 0x28(%r14)
movq $0x0, 0x38(%r14)
movups %xmm0, 0x9(%r14)
movups %xmm0, (%r14)
movq -0x80(%rbp), %rcx
movq %rcx, -0x100(%rbp)
movb %al, -0xa4(%rbp)
movl $0x1, %edx
movq %r15, %rdi
movq %r12, %rsi
callq 0x2180
cmpb $0x55, -0x29(%rbp)
jne 0x2883
movb $0x55, -0x50(%rbp)
movq %r14, %rdi
movq %r13, %rsi
callq 0x2a08
movl $0x1, %edx
movq %r15, %rdi
movq %r12, %rsi
callq 0x2180
movb -0x29(%rbp), %bl
cmpb $0x9, %bl
ja 0x27d3
movl $0x1c, %edx
movq 0x4878(%rip), %rdi      # 0x6fd0
leaq 0x291b(%rip), %rsi      # 0x507a
callq 0x2170
movq 0x4865(%rip), %rdi      # 0x6fd0
movq (%rdi), %rax
movq -0x18(%rax), %rax
movl 0x18(%rdi,%rax), %ecx
andl $-0x4b, %ecx
orl $0x2, %ecx
movl %ecx, 0x18(%rdi,%rax)
movb %bl, -0x50(%rbp)
movl $0x1, %edx
movq %r13, %rsi
callq 0x2170
movq %rax, %rbx
movl $0x13, %edx
movq %rax, %rdi
leaq 0x28f5(%rip), %rsi      # 0x5097
callq 0x2170
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x2140
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x20d0
jmp 0x2883
movb %bl, -0x50(%rbp)
movq %r14, %rdi
movq %r13, %rsi
callq 0x2a08
movl $0x1, %edx
movq %r15, %rdi
movq %r12, %rsi
callq 0x2180
cmpb $0x0, -0x29(%rbp)
jne 0x2883
movb $0x0, -0x50(%rbp)
movq %r14, %rdi
movq %r13, %rsi
callq 0x2a08
addb $-0x3, %bl
movl $0x1, %edx
movq %r15, %rdi
movq %r12, %rsi
callq 0x2180
movb -0x29(%rbp), %al
movb %al, -0x50(%rbp)
movq %r14, %rdi
movq %r13, %rsi
callq 0x2a08
decb %bl
jne 0x280d
leaq -0x100(%rbp), %rdi
callq 0x33f8
cmpb $0x1, -0xa4(%rbp)
jne 0x2883
movq %r13, %rdi
leaq -0x100(%rbp), %rsi
callq 0x34b6
movq -0x50(%rbp), %rsi
movq -0x48(%rbp), %rdx
movq 0x476b(%rip), %rdi      # 0x6fd0
callq 0x2170
movq -0x50(%rbp), %rdi
leaq -0x40(%rbp), %rax
cmpq %rax, %rdi
je 0x2883
movq -0x40(%rbp), %rsi
incq %rsi
callq 0x2160
movq -0x78(%rbp), %rax
movq %rax, -0x100(%rbp)
movq -0xd0(%rbp), %rdi
testq %rdi, %rdi
je 0x28a9
movq -0xc0(%rbp), %rsi
subq %rdi, %rsi
callq 0x2160
movq -0xf8(%rbp), %rdi
testq %rdi, %rdi
je 0x28c4
movq -0xe8(%rbp), %rsi
subq %rdi, %rsi
callq 0x2160
movq -0x328(%rbp), %rax
movq -0x18(%rax), %rax
testb $0x7, -0x308(%rbp,%rax)
je 0x26c2
xorl %ebx, %ebx
jmp 0x260d
movq %rax, %rbx
movq -0x50(%rbp), %rdi
leaq -0x40(%rbp), %rax
cmpq %rax, %rdi
je 0x2935
movq -0x40(%rbp), %rsi
incq %rsi
callq 0x2160
jmp 0x2935
jmp 0x2932
movq %rax, %rbx
leaq -0xf0(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x2948
movq -0xf0(%rbp), %rsi
incq %rsi
callq 0x2160
jmp 0x2948
jmp 0x2945
jmp 0x2945
jmp 0x2932
jmp 0x2932
jmp 0x2967
movq %rax, %rbx
leaq -0x100(%rbp), %rdi
callq 0x2c4e
jmp 0x2948
jmp 0x2945
movq %rax, %rbx
leaq -0x120(%rbp), %rdi
callq 0x29ca
jmp 0x2959
movq %rax, %rbx
leaq -0x328(%rbp), %rdi
callq 0x2050
jmp 0x296a
movq %rax, %rbx
leaq -0x60(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x2983
movq -0x60(%rbp), %rsi
incq %rsi
callq 0x2160
movq %rbx, %rdi
callq 0x2200
nop