// File: jkingan[P]ladyheather/heathgps.cpp
// Function: parse_uccm_time(int)
void parse_uccm_time(int why)
{
int vals[50];
int i,j;
double jd, jd0;
char *s;

   log_packet_id("uccm time");

   j = 0;
   fake_msec = 0.0;
   sent_uccm_status = 0;
   s = strstr(nmea_msg, "C5 ");

if(debug_file) fprintf(debug_file, "uccm time %d loop:%d  len:%d: [%s]\n", why, have_uccm_loop, (int) strlen(nmea_msg), nmea_msg);
   if(s == 0) return;


   for(i=0; i<131; i+=3) {  // get the values from the time line
      vals[j] = atohex(&s[i]);
      j++;
   }

   if(!user_set_utc_ofs && vals[32]) {  // leap second offset from UTC
      utc_offset = vals[32];
      check_utc_ofs(-111);
   }

   // vals[33]: 40=PPS validity?  41:phase settling  50:pps invalid?
   //           60:stable  62:stable, leap pending?
   // on power up: 41 -> 43 -> 63 -> 60/62 (62=leap pending?) Trimble
   // on power up: 41 -> 43 -> 60 -> 62 (62=leap pending?)    Trimble UCCM-P
   if(1) {
      have_leap_info = 8;
      if(vals[33] & 0x02) {  // leap pending flag?
         minor_alarms |= MINOR_LEAP_PEND;
      }
      else {
         minor_alarms &= (~MINOR_LEAP_PEND);
      }
   }


   // vals[34]: 04=normal 0C=antenna open/shorted  06=normal?
   // on power up: 00 -> 04
   // disconnect antenna: 04 -> 0C
   // reconnect antenna   0C -> 04


   // vals[35]: 8F=FFOM >0/settling/no antenna    85:FFOM 0,locked? - Symmetricom UCCMP
   // on power up: 8F -> 85           Symmetricom
   // disconnect antenna: 85 -> 8F    Symmetricom
   // reconnect antenna:  8F -> 85    Symmetricom
   //
   // vals[35]: 41=power up  4F=FFOM >0/settling/no antenna    45:FFOM 0,locked? - Trimble
   // on power up: 4F -> 45           Trimble UCCM-P
   // on power up: 41 -> 4F -> 45     Trimble UCCM
   // on antenna disconnect 45 -> 4F  Trimble
   // on antenna connect    4F -> 45 


   // vals[36]: 40=have date?/normal  50/60:date invalid?/no antenna
   // power up 50 -> 40                   Symmetricom 
   // disconnect antenna: 40 -> 50 -> 60  Symmetricom 
   // reconnect antenna:  60 -> 50 -> 40  Symmetricom 
   //
   // vals[36]: 80=have date?/normal  90:date invalid?/no antenna
   // power up 90 -> 80                   Trimble 
   // disconnect antenna: 80 -> 90        Trimble UCCM-P
   // reconnect antenna:  90 -> 80        Trimble UCCM-P

   jd = jd0 = (double) ((vals[27] * (256*256*256)) + (vals[28]*(256*256)) + (vals[29]*(256)) + vals[30]);

   if(timing_mode != TMODE_GPS) {  // we want UTC time - convert GPS to UTC
      jd -= (double) utc_offset;
   }

   jd /= (24.0*60.0*60.0);
   jd += GPS_EPOCH;

   set_gregorian_time(jd);

   fake_time_stamp = 0;
   start_fake_seconds(5);

   gps_week = (int) (jd0 / (7.0*24.0*60.0*60.0));
   faked_tow = 3;
   if(have_week == 0) need_redraw = 2054;
   have_week = 9;

//   tow = pri_tow = this_tow = survey_tow = (int) (jd - ((double) gps_week) * (7.0*24.0*60.0*60.0));
   tow = pri_tow = this_tow = survey_tow = (int) (jd0 - ((double) gps_week) * (7.0*24.0*60.0*60.0));
   have_tow = 9;

//if(log_file) fprintf(log_file, "uccm1 jd:%f  week:%d  tow:%d  %04d/%02d/%02d  %02d:%02d:%02d  %02X:%02X:%02X:%02X\n", 
//jd0,gps_week,tow,year,month,day,hours,minutes,seconds, vals[27],vals[28],vals[29],vals[30]);  //kkkkk
//if(raw_file) fprintf(raw_file, "uccm1 jd:%f  week:%d  tow:%d  %04d/%02d/%02d  %02d:%02d:%02d  %02X:%02X:%02X:%02X\n", 
//jd0,gps_week,tow,year,month,day,hours,minutes,seconds, vals[27],vals[28],vals[29],vals[30]);  //kkkkk

   if(seconds != last_misc_second) {
      update_gps_screen(555);
   }

   if(sent_uccm_status) ;
   else if(pri_seconds == SCPI_STATUS_SECOND) {
      queue_uccm_cmd("SYST:STAT?", UCCM_STATUS_MSG);
   }

   if(1) {  // samsung
      tsip_buf[0] = 0;
      tsip_wptr = 0;
      tsip_rptr = 0;
      tsip_sync = 0; 
   }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x100, %rsp            # imm = 0x100
movl %edi, -0x4(%rbp)
leaq 0xaee3e(%rip), %rdi     # 0x1888f3
callq 0x7add0
movl $0x0, -0xd8(%rbp)
leaq 0x292e0d(%rip), %rax    # 0x36c8d8
xorps %xmm0, %xmm0
movsd %xmm0, (%rax)
movl $0x0, 0x2f22bc(%rip)    # 0x3cbd98
leaq 0x2c0f2d(%rip), %rdi    # 0x39aa10
leaq 0xaee13(%rip), %rsi     # 0x1888fd
callq 0x9090
movq %rax, -0xf0(%rbp)
leaq 0x1c3a73(%rip), %rax    # 0x29d570
cmpq $0x0, (%rax)
je 0xd9b63
leaq 0x1c3a66(%rip), %rax    # 0x29d570
movq (%rax), %rax
movq %rax, -0x100(%rbp)
movl -0x4(%rbp), %eax
movl %eax, -0xf8(%rbp)
leaq 0x1a06bc(%rip), %rax    # 0x27a1e0
movl (%rax), %eax
movl %eax, -0xf4(%rbp)
leaq 0x2c0edd(%rip), %rdi    # 0x39aa10
callq 0x9120
movq -0x100(%rbp), %rdi
movl -0xf8(%rbp), %edx
movl -0xf4(%rbp), %ecx
movl %eax, %r8d
leaq 0xaedac(%rip), %rsi     # 0x188901
leaq 0x2c0eb4(%rip), %r9     # 0x39aa10
movb $0x0, %al
callq 0x9550
cmpq $0x0, -0xf0(%rbp)
jne 0xd9b72
jmp 0xd9e35
movl $0x0, -0xd4(%rbp)
cmpl $0x83, -0xd4(%rbp)
jge 0xd9bce
movq -0xf0(%rbp), %rdi
movslq -0xd4(%rbp), %rax
addq %rax, %rdi
callq 0x98d0
movl %eax, %ecx
movslq -0xd8(%rbp), %rax
movl %ecx, -0xd0(%rbp,%rax,4)
movl -0xd8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xd8(%rbp)
movl -0xd4(%rbp), %eax
addl $0x3, %eax
movl %eax, -0xd4(%rbp)
jmp 0xd9b7c
leaq 0x292d3f(%rip), %rax    # 0x36c914
cmpl $0x0, (%rax)
jne 0xd9bf6
cmpl $0x0, -0x50(%rbp)
je 0xd9bf6
movl -0x50(%rbp), %ecx
leaq 0x292d1e(%rip), %rax    # 0x36c908
movl %ecx, (%rax)
movl $0xffffff91, %edi       # imm = 0xFFFFFF91
callq 0x7a270
leaq 0x29287b(%rip), %rax    # 0x36c478
movl $0x8, (%rax)
movl -0x4c(%rbp), %eax
andl $0x2, %eax
cmpl $0x0, %eax
je 0xd9c28
leaq 0x2927a3(%rip), %rax    # 0x36c3b8
movl (%rax), %ecx
orl $0x80, %ecx
leaq 0x292794(%rip), %rax    # 0x36c3b8
movl %ecx, (%rax)
jmp 0xd9c40
leaq 0x292789(%rip), %rax    # 0x36c3b8
movl (%rax), %ecx
andl $0xffffff7f, %ecx       # imm = 0xFFFFFF7F
leaq 0x29277a(%rip), %rax    # 0x36c3b8
movl %ecx, (%rax)
movl -0x64(%rbp), %eax
shll $0x18, %eax
movl -0x60(%rbp), %ecx
shll $0x10, %ecx
addl %ecx, %eax
movl -0x5c(%rbp), %ecx
shll $0x8, %ecx
addl %ecx, %eax
addl -0x58(%rbp), %eax
cvtsi2sd %eax, %xmm0
movsd %xmm0, -0xe8(%rbp)
movsd %xmm0, -0xe0(%rbp)
leaq 0x292a29(%rip), %rax    # 0x36c69d
movzbl (%rax), %eax
cmpl $0x0, %eax
je 0xd9c9b
leaq 0x292c85(%rip), %rax    # 0x36c908
cvtsi2sdl (%rax), %xmm1
movsd -0xe0(%rbp), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, -0xe0(%rbp)
movsd -0xe0(%rbp), %xmm0
movsd 0x9535d(%rip), %xmm1    # 0x16f008
divsd %xmm1, %xmm0
movsd %xmm0, -0xe0(%rbp)
movsd 0x97959(%rip), %xmm0    # 0x171618
addsd -0xe0(%rbp), %xmm0
movsd %xmm0, -0xe0(%rbp)
movsd -0xe0(%rbp), %xmm0
callq 0x7fb00
leaq 0x292bed(%rip), %rax    # 0x36c8d0
movl $0x0, (%rax)
movl $0x5, %edi
callq 0xc5ec0
movsd -0xe8(%rbp), %xmm0
movsd 0x97905(%rip), %xmm1    # 0x171608
divsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movw %ax, %cx
leaq 0x292c03(%rip), %rax    # 0x36c918
movw %cx, (%rax)
leaq 0x292c09(%rip), %rax    # 0x36c928
movl $0x3, (%rax)
leaq 0x2927e0(%rip), %rax    # 0x36c50c
cmpl $0x0, (%rax)
jne 0xd9d3e
leaq 0x19dcec(%rip), %rax    # 0x277a24
movl $0x806, (%rax)          # imm = 0x806
leaq 0x2927c7(%rip), %rax    # 0x36c50c
movl $0x9, (%rax)
movsd -0xe8(%rbp), %xmm1
leaq 0x292bbe(%rip), %rax    # 0x36c918
movzwl (%rax), %eax
cvtsi2sd %eax, %xmm0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
movsd 0x97888(%rip), %xmm2    # 0x171608
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
cltq
leaq 0x292b9b(%rip), %rcx    # 0x36c930
movq %rax, (%rcx)
movl %eax, %ecx
leaq 0x292b7f(%rip), %rax    # 0x36c920
movl %ecx, (%rax)
leaq 0x292b7a(%rip), %rax    # 0x36c924
movl %ecx, (%rax)
leaq 0x292b69(%rip), %rax    # 0x36c91c
movl %ecx, (%rax)
leaq 0x29274c(%rip), %rax    # 0x36c508
movl $0x9, (%rax)
leaq 0x2928df(%rip), %rax    # 0x36c6a8
movl (%rax), %eax
cmpl 0xef58f(%rip), %eax     # 0x1c9360
je 0xd9ddd
movl $0x22b, %edi            # imm = 0x22B
callq 0x9d790
cmpl $0x0, 0x2f1fb4(%rip)    # 0x3cbd98
je 0xd9de8
jmp 0xd9e07
leaq 0x2928f1(%rip), %rax    # 0x36c6e0
cmpl $0x20, (%rax)
jne 0xd9e05
leaq 0xad680(%rip), %rdi     # 0x18747b
movl $0x65, %esi
callq 0x82fd0
jmp 0xd9e07
leaq 0x2bc622(%rip), %rax    # 0x396430
movb $0x0, (%rax)
leaq 0x2bd61a(%rip), %rax    # 0x397432
movw $0x0, (%rax)
leaq 0x2bd610(%rip), %rax    # 0x397434
movw $0x0, (%rax)
leaq 0x2bd606(%rip), %rax    # 0x397436
movw $0x0, (%rax)
addq $0x100, %rsp            # imm = 0x100
popq %rbp
retq
nop