// File: /workspace/github_cpp_makefile_star2_O2/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 $0x2f8, %rsp            # imm = 0x2F8
cmpl $0x1, %edi
jg 0x2408
movq 0x4be4(%rip), %rdi      # 0x6fd0
leaq 0x1c11(%rip), %rsi      # 0x4004
callq 0x2150
movq %rax, %rdi
callq 0x20a0
pushq $0x1
popq %rbx
jmp 0x2518
movq 0x8(%rsi), %rsi
leaq -0x58(%rbp), %rdi
leaq -0x318(%rbp), %rdx
callq 0x27ac
movq 0x4bad(%rip), %rdi      # 0x6fd0
leaq 0x1c05(%rip), %rsi      # 0x402f
callq 0x2150
leaq -0x58(%rbp), %rsi
movq %rax, %rdi
callq 0x2140
movq %rax, %rdi
callq 0x20a0
movq -0x58(%rbp), %rsi
leaq -0x318(%rbp), %rdi
pushq $0x4
popq %rdx
callq 0x2250
movq -0x318(%rbp), %rax
movq -0x18(%rax), %rax
testb $0x5, -0x2f8(%rbp,%rax)
jne 0x24d9
leaq -0x2a0(%rbp), %rdi
callq 0x2030
testb %al, %al
je 0x24d9
xorps %xmm0, %xmm0
movaps %xmm0, -0x70(%rbp)
andq $0x0, -0x60(%rbp)
xorl %ebx, %ebx
leaq -0x318(%rbp), %r15
leaq -0x29(%rbp), %r12
pushq $0x1
popq %r13
xorl %r14d, %r14d
movq -0x318(%rbp), %rax
movq -0x18(%rax), %rax
movl -0x2f8(%rbp,%rax), %eax
testb $0x2, %al
jne 0x2530
testb $0x5, %al
jne 0x252c
cmpb $0x55, -0x29(%rbp)
je 0x252c
movq %r15, %rdi
callq 0x2220
movq %rax, %rbx
movq %rdx, %r14
movq %r15, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x2190
jmp 0x249b
movq 0x4af0(%rip), %rdi      # 0x6fd0
leaq 0x1b54(%rip), %rsi      # 0x403b
callq 0x2150
leaq -0x58(%rbp), %rsi
movq %rax, %rdi
callq 0x2140
movq %rax, %rdi
callq 0x20a0
pushq $0x1
popq %rbx
leaq -0x318(%rbp), %rdi
callq 0x2040
leaq -0x58(%rbp), %rdi
callq 0x2090
movl %ebx, %eax
addq $0x2f8, %rsp            # imm = 0x2F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
testb $0x5, %al
je 0x2559
movq 0x4a99(%rip), %rdi      # 0x6fd0
leaq 0x1b0f(%rip), %rsi      # 0x404d
callq 0x2150
movq %rax, %rdi
callq 0x20a0
pushq $0x1
popq %rbx
leaq -0x70(%rbp), %rdi
callq 0x280a
jmp 0x2503
leaq -0x318(%rbp), %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x21a0
leaq -0xf0(%rbp), %rdi
callq 0x3bcc
movq 0x4a52(%rip), %rdi      # 0x6fd0
leaq -0xf0(%rbp), %rsi
callq 0x2140
leaq -0xf0(%rbp), %r14
movq %r14, %rdi
callq 0x2090
xorl %ebx, %ebx
leaq 0x47ce(%rip), %rax      # 0x6d70
addq $0x10, %rax
movq %rax, -0x38(%rbp)
leaq -0x110(%rbp), %r13
movq -0x318(%rbp), %rax
movq -0x18(%rax), %rax
testb $0x7, -0x2f8(%rbp,%rax)
jne 0x254e
andw $0x0, -0xce(%rbp)
andl $0x0, -0xcc(%rbp)
movb %bl, -0xc8(%rbp)
xorps %xmm0, %xmm0
movups %xmm0, 0x30(%r14)
andq $0x0, 0x40(%r14)
movups %xmm0, 0x11(%r14)
movups %xmm0, 0x8(%r14)
movq -0x38(%rbp), %rax
movq %rax, -0xf0(%rbp)
movb %bl, -0x94(%rbp)
leaq -0x318(%rbp), %rdi
leaq -0x29(%rbp), %rsi
pushq $0x1
popq %rdx
callq 0x2190
cmpb $0x55, -0x29(%rbp)
jne 0x272a
movq %r14, %rdi
pushq $0x55
popq %rsi
callq 0x27e8
leaq -0x318(%rbp), %rdi
leaq -0x29(%rbp), %rsi
pushq $0x1
popq %rdx
callq 0x2190
movb -0x29(%rbp), %r12b
movsbl %r12b, %r15d
cmpb $0x9, %r12b
ja 0x269b
movq 0x497d(%rip), %rdi      # 0x6fd0
leaq 0x1a20(%rip), %rsi      # 0x407a
callq 0x2150
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
movl 0x18(%rax,%rcx), %edx
andl $-0x4b, %edx
orl $0x2, %edx
movl %edx, 0x18(%rax,%rcx)
movq %rax, %rdi
movl %r15d, %esi
callq 0x2180
movq %rax, %rdi
leaq 0x1a0e(%rip), %rsi      # 0x4097
callq 0x2150
movq %rax, %rdi
callq 0x20a0
jmp 0x272a
movq %r14, %rdi
movl %r15d, %esi
callq 0x27e8
leaq -0x318(%rbp), %rdi
leaq -0x29(%rbp), %rsi
pushq $0x1
popq %rdx
callq 0x2190
cmpb $0x0, -0x29(%rbp)
jne 0x272a
movq %r14, %rdi
xorl %esi, %esi
callq 0x27e8
addb $-0x3, %r12b
subb $0x1, %r12b
jb 0x26f4
leaq -0x318(%rbp), %rdi
leaq -0x29(%rbp), %rsi
pushq $0x1
popq %rdx
callq 0x2190
movsbl -0x29(%rbp), %esi
movq %r14, %rdi
callq 0x27e8
jmp 0x26cd
movq %r14, %rdi
callq 0x3130
cmpb $0x1, -0x94(%rbp)
jne 0x272a
movq %r13, %r15
movq %r13, %rdi
movq %r14, %rsi
callq 0x3234
movq 0x48b6(%rip), %rdi      # 0x6fd0
movq %r15, %rsi
callq 0x2140
movq %r15, %rdi
callq 0x2090
movq %r14, %rdi
callq 0x2b1c
jmp 0x25b1
movq %rax, %rbx
leaq -0xf0(%rbp), %rdi
callq 0x2090
jmp 0x2785
jmp 0x276c
movq %rax, %rbx
leaq -0x110(%rbp), %rdi
callq 0x2090
jmp 0x2779
jmp 0x2776
jmp 0x276c
jmp 0x2771
jmp 0x2776
jmp 0x2776
movq %rax, %rbx
jmp 0x278e
jmp 0x276c
movq %rax, %rbx
jmp 0x2785
movq %rax, %rbx
jmp 0x279a
movq %rax, %rbx
leaq -0xf0(%rbp), %rdi
callq 0x2b1c
leaq -0x70(%rbp), %rdi
callq 0x280a
leaq -0x318(%rbp), %rdi
callq 0x2040
leaq -0x58(%rbp), %rdi
callq 0x2090
movq %rbx, %rdi
callq 0x2230
nop