// File: jkingan[P]ladyheather/heathgps.cpp
// Function: decode_rt17_msg(int, int)
void decode_rt17_msg(int msg, int status)
{
int page;
int pages;
int reply;
int i;
static int last_reply = (-2);
static int last_page = 0;

   // parse and decode the RT17 message

   start_msg_decode(1);

   have_critical_alarms = 17;
   if(status & 0x02) critical_alarms |= CRIT_PWR;
   else              critical_alarms &= (~CRIT_PWR);

   if(msg == 0x40) {  // GSOF data
      reply = rt17_buf[0];
      page = rt17_buf[1];
      pages = rt17_buf[2];
   }
   else if(msg == 0x57) {  // RAWDATA
      // rt17_buf[0] = message type subcode
      page = rt17_buf[1];
      reply = rt17_buf[2];

      pages = page & 0x0F;
      page  = (page >> 4) & 0x0F;
   }
   else {  // other messages
      for(i=0; i<tsip_wptr; i++) {  // copy RT17 buffer to tsip buffer
         if(i < MAX_TSIP) tsip_buf[i] = rt17_buf[i];
      }

      // !!!!parse message here
      if(msg == 0x55) parse_rt17_svdata(msg);
else if(debug_file) fprintf(debug_file, "Misc msg type %02X skipped.\n", msg);

      rt17_wptr = 0;
      last_reply = (-1);
      last_page = 0;
      return;
   }

if(0 && debug_file) {
   fprintf(debug_file, "copy page %d of %d   reply:%d  last_reply:%d  len:%d\n", page, pages, reply,last_reply, rt17_wptr);
}
   // merge multi-page packet data into one buffer (tsip_buf)
   if(page != (last_page + 1)) {  // page sequence error
if(debug_file) fprintf(debug_file, "page seq error: last:%d  this:%d\n", last_page, page);
      last_page = 0;
      return;
   }

   last_page = page;
   if(page == 1) {  // first page of data, copy all data
      last_reply = reply;
      for(i=0; i<tsip_wptr; i++) {
         if(rt17_wptr < MAX_TSIP) tsip_buf[rt17_wptr++] = rt17_buf[i];
      }
   }
   else if(reply != last_reply) {  // message sequence error
if(debug_file) fprintf(debug_file, "reply seq error: last:%d  this:%d\n", last_reply, reply);
   }
   else if(msg == 0x40) {  // copy only message data from following pages to tsip_buf
      for(i=3; i<tsip_wptr; i++) {  // skip subcode, reply, page, pages
         if(rt17_wptr < MAX_TSIP) tsip_buf[rt17_wptr++] = rt17_buf[i];
      }
   }
   else if(msg == 0x57) {  // copy only message data from following pages to tsip_buf
      for(i=4; i<tsip_wptr; i++) {  // skip subcode, page, reply, flags
         if(rt17_wptr < MAX_TSIP) tsip_buf[rt17_wptr++] = rt17_buf[i];
      }
   }
if(0 && debug_file) fprintf(debug_file, "Page:%02X  tsip_wptr:%d  rt17_wptr:%d\n", page, tsip_wptr, rt17_wptr);

   if(page != pages) {  // more pages follow for this message
      return;
   }
   last_page = 0;

   tsip_wptr = rt17_wptr;  // parse merged page buffer

   if(msg == 0x40) {  // GSOF
      parse_rt17_gsof(msg);
   }
   else if(msg == 0x57) {  // RAWDATA
      parse_rt17_rawdata(msg);
   }

   rt17_wptr = 0;
   last_reply = (-1);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x20, %rsp
movl %edi, -0x4(%rbp)
movl %esi, -0x8(%rbp)
movl $0x1, %edi
callq 0xae4a0
leaq 0x2bc189(%rip), %rax    # 0x36c488
movl $0x11, (%rax)
movl -0x8(%rbp), %eax
andl $0x2, %eax
cmpl $0x0, %eax
je 0xb0327
leaq 0x2bc099(%rip), %rax    # 0x36c3b0
movl (%rax), %ecx
orl $0x4, %ecx
leaq 0x2bc08d(%rip), %rax    # 0x36c3b0
movl %ecx, (%rax)
jmp 0xb033c
leaq 0x2bc082(%rip), %rax    # 0x36c3b0
movl (%rax), %ecx
andl $-0x5, %ecx
leaq 0x2bc076(%rip), %rax    # 0x36c3b0
movl %ecx, (%rax)
cmpl $0x40, -0x4(%rbp)
jne 0xb0365
movzbl 0x31fc87(%rip), %eax    # 0x3cffd0
movl %eax, -0x14(%rbp)
movzbl 0x31fc7e(%rip), %eax    # 0x3cffd1
movl %eax, -0xc(%rbp)
movzbl 0x31fc75(%rip), %eax    # 0x3cffd2
movl %eax, -0x10(%rbp)
jmp 0xb0442
cmpl $0x57, -0x4(%rbp)
jne 0xb0399
movzbl 0x31fc5f(%rip), %eax    # 0x3cffd1
movl %eax, -0xc(%rbp)
movzbl 0x31fc56(%rip), %eax    # 0x3cffd2
movl %eax, -0x14(%rbp)
movl -0xc(%rbp), %eax
andl $0xf, %eax
movl %eax, -0x10(%rbp)
movl -0xc(%rbp), %eax
sarl $0x4, %eax
andl $0xf, %eax
movl %eax, -0xc(%rbp)
jmp 0xb0440
movl $0x0, -0x18(%rbp)
movl -0x18(%rbp), %eax
leaq 0x2e7088(%rip), %rcx    # 0x397432
movzwl (%rcx), %ecx
cmpl %ecx, %eax
jge 0xb03e3
cmpl $0x1000, -0x18(%rbp)    # imm = 0x1000
jge 0xb03d6
movslq -0x18(%rbp), %rcx
leaq 0x31fc0b(%rip), %rax    # 0x3cffd0
movb (%rax,%rcx), %dl
movslq -0x18(%rbp), %rcx
leaq 0x2e605d(%rip), %rax    # 0x396430
movb %dl, (%rax,%rcx)
jmp 0xb03d8
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0xb03a0
cmpl $0x55, -0x4(%rbp)
jne 0xb03f3
movl -0x4(%rbp), %edi
callq 0xb0120
jmp 0xb041d
leaq 0x1ed176(%rip), %rax    # 0x29d570
cmpq $0x0, (%rax)
je 0xb041b
leaq 0x1ed169(%rip), %rax    # 0x29d570
movq (%rax), %rdi
movl -0x4(%rbp), %edx
leaq 0xd5c38(%rip), %rsi     # 0x18604c
movb $0x0, %al
callq 0x9550
jmp 0xb041d
movl $0x0, 0x320ba9(%rip)    # 0x3d0fd0
movl $0xffffffff, 0x1191e7(%rip) # imm = 0xFFFFFFFF
movl $0x0, 0x320ba5(%rip)    # 0x3d0fe0
jmp 0xb0672
jmp 0xb0442
movl -0xc(%rbp), %eax
movl 0x320b95(%rip), %ecx    # 0x3d0fe0
addl $0x1, %ecx
cmpl %ecx, %eax
je 0xb048f
leaq 0x1ed117(%rip), %rax    # 0x29d570
cmpq $0x0, (%rax)
je 0xb0480
leaq 0x1ed10a(%rip), %rax    # 0x29d570
movq (%rax), %rdi
movl 0x320b71(%rip), %edx    # 0x3d0fe0
movl -0xc(%rbp), %ecx
leaq 0xd5bf0(%rip), %rsi     # 0x186069
movb $0x0, %al
callq 0x9550
movl $0x0, 0x320b56(%rip)    # 0x3d0fe0
jmp 0xb0672
movl -0xc(%rbp), %eax
movl %eax, 0x320b48(%rip)    # 0x3d0fe0
cmpl $0x1, -0xc(%rbp)
jne 0xb0509
movl -0x14(%rbp), %eax
movl %eax, 0x119171(%rip)    # 0x1c9618
movl $0x0, -0x18(%rbp)
movl -0x18(%rbp), %eax
leaq 0x2e6f7a(%rip), %rcx    # 0x397432
movzwl (%rcx), %ecx
cmpl %ecx, %eax
jge 0xb0504
cmpl $0x1000, 0x320b07(%rip) # imm = 0x1000
jge 0xb04f7
movslq -0x18(%rbp), %rcx
leaq 0x31fafa(%rip), %rax    # 0x3cffd0
movb (%rax,%rcx), %dl
movl 0x320af1(%rip), %eax    # 0x3d0fd0
movl %eax, %ecx
addl $0x1, %ecx
movl %ecx, 0x320ae6(%rip)    # 0x3d0fd0
movslq %eax, %rcx
leaq 0x2e5f3c(%rip), %rax    # 0x396430
movb %dl, (%rax,%rcx)
jmp 0xb04f9
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0xb04ae
jmp 0xb0617
movl -0x14(%rbp), %eax
cmpl 0x119106(%rip), %eax    # 0x1c9618
je 0xb0547
leaq 0x1ed055(%rip), %rax    # 0x29d570
cmpq $0x0, (%rax)
je 0xb0542
leaq 0x1ed048(%rip), %rax    # 0x29d570
movq (%rax), %rdi
movl 0x1190e7(%rip), %edx    # 0x1c9618
movl -0x14(%rbp), %ecx
leaq 0xd5b50(%rip), %rsi     # 0x18608b
movb $0x0, %al
callq 0x9550
jmp 0xb0615
cmpl $0x40, -0x4(%rbp)
jne 0xb05ac
movl $0x3, -0x18(%rbp)
movl -0x18(%rbp), %eax
leaq 0x2e6ed4(%rip), %rcx    # 0x397432
movzwl (%rcx), %ecx
cmpl %ecx, %eax
jge 0xb05aa
cmpl $0x1000, 0x320a61(%rip) # imm = 0x1000
jge 0xb059d
movslq -0x18(%rbp), %rcx
leaq 0x31fa54(%rip), %rax    # 0x3cffd0
movb (%rax,%rcx), %dl
movl 0x320a4b(%rip), %eax    # 0x3d0fd0
movl %eax, %ecx
addl $0x1, %ecx
movl %ecx, 0x320a40(%rip)    # 0x3d0fd0
movslq %eax, %rcx
leaq 0x2e5e96(%rip), %rax    # 0x396430
movb %dl, (%rax,%rcx)
jmp 0xb059f
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0xb0554
jmp 0xb0613
cmpl $0x57, -0x4(%rbp)
jne 0xb0611
movl $0x4, -0x18(%rbp)
movl -0x18(%rbp), %eax
leaq 0x2e6e6f(%rip), %rcx    # 0x397432
movzwl (%rcx), %ecx
cmpl %ecx, %eax
jge 0xb060f
cmpl $0x1000, 0x3209fc(%rip) # imm = 0x1000
jge 0xb0602
movslq -0x18(%rbp), %rcx
leaq 0x31f9ef(%rip), %rax    # 0x3cffd0
movb (%rax,%rcx), %dl
movl 0x3209e6(%rip), %eax    # 0x3d0fd0
movl %eax, %ecx
addl $0x1, %ecx
movl %ecx, 0x3209db(%rip)    # 0x3d0fd0
movslq %eax, %rcx
leaq 0x2e5e31(%rip), %rax    # 0x396430
movb %dl, (%rax,%rcx)
jmp 0xb0604
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0xb05b9
jmp 0xb0611
jmp 0xb0613
jmp 0xb0615
jmp 0xb0617
movl -0xc(%rbp), %eax
cmpl -0x10(%rbp), %eax
je 0xb0621
jmp 0xb0672
movl $0x0, 0x3209b5(%rip)    # 0x3d0fe0
movl 0x32099f(%rip), %eax    # 0x3d0fd0
movw %ax, %cx
leaq 0x2e6df7(%rip), %rax    # 0x397432
movw %cx, (%rax)
cmpl $0x40, -0x4(%rbp)
jne 0xb064e
movl -0x4(%rbp), %edi
callq 0xb0240
jmp 0xb065e
cmpl $0x57, -0x4(%rbp)
jne 0xb065c
movl -0x4(%rbp), %edi
callq 0xb0170
jmp 0xb065e
movl $0x0, 0x320968(%rip)    # 0x3d0fd0
movl $0xffffffff, 0x118fa6(%rip) # imm = 0xFFFFFFFF
addq $0x20, %rsp
popq %rbp
retq
nopl (%rax,%rax)