// File: /workspace/github_cpp_makefile_star2_O3/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xd8, %rsp
movl %edi, %r14d
leaq 0x851ba(%rip), %rdi     # 0x1255ff
callq 0x58dc2
leaq 0x26d2a7(%rip), %r12    # 0x30d6f8
movq $0x0, (%r12)
movl $0x0, 0x2cc765(%rip)    # 0x36cbc8
leaq 0x29b3c6(%rip), %rdi    # 0x33b830
leaq 0x85198(%rip), %rsi     # 0x125609
callq 0x9090
movq %rax, %rbx
leaq 0x19df10(%rip), %rax    # 0x23e390
movq (%rax), %r15
testq %r15, %r15
je 0xa04c5
leaq 0x17ab71(%rip), %rax    # 0x21b000
movl (%rax), %r12d
leaq 0x29b397(%rip), %r13    # 0x33b830
movq %r13, %rdi
callq 0x9120
leaq 0x85165(%rip), %rsi     # 0x12560d
movq %r15, %rdi
movl %r14d, %edx
movl %r12d, %ecx
leaq 0x26d240(%rip), %r12    # 0x30d6f8
movl %eax, %r8d
movq %r13, %r9
xorl %eax, %eax
callq 0x9560
testq %rbx, %rbx
je 0xa06fc
xorl %r14d, %r14d
movq %rbx, %rdi
callq 0x98c6
movl %eax, -0x100(%rbp,%r14,4)
incq %r14
addq $0x3, %rbx
cmpq $0x2c, %r14
jne 0xa04d1
leaq 0x26d23f(%rip), %rax    # 0x30d734
cmpl $0x0, (%rax)
sete %cl
movl -0x80(%rbp), %eax
testl %eax, %eax
setne %dl
testb %dl, %cl
je 0xa0549
leaq 0x26d21a(%rip), %rcx    # 0x30d728
movl %eax, (%rcx)
leaq 0x26ce05(%rip), %rcx    # 0x30d31c
cmpl $0x0, (%rcx)
jne 0xa0552
leaq 0x26cff1(%rip), %rdx    # 0x30d514
cmpl $0x7e1, (%rdx)          # imm = 0x7E1
setge %dl
cmpl $0x12, %eax
setl %sil
andb %dl, %sil
cmpl $0x11, %eax
setl %dl
orb %sil, %dl
jne 0xa0552
movl $0x6f, (%rcx)
jmp 0xa0552
leaq 0x26d1d8(%rip), %rax    # 0x30d728
movl (%rax), %eax
leaq 0x26cd3f(%rip), %rcx    # 0x30d298
movl $0x8, (%rcx)
movl -0x7c(%rbp), %ecx
leaq 0x26cc6f(%rip), %rdx    # 0x30d1d8
movl $0xffffff7f, %esi       # imm = 0xFFFFFF7F
andl (%rdx), %esi
andl $0x2, %ecx
shll $0x6, %ecx
orl %esi, %ecx
movl %ecx, (%rdx)
movl -0x94(%rbp), %ecx
movl -0x90(%rbp), %edx
shll $0x18, %ecx
shll $0x10, %edx
addl %ecx, %edx
movl -0x8c(%rbp), %ecx
shll $0x8, %ecx
addl %edx, %ecx
addl -0x88(%rbp), %ecx
cvtsi2sd %ecx, %xmm2
leaq 0x26cf13(%rip), %rcx    # 0x30d4bd
cmpb $0x0, (%rcx)
xorpd %xmm1, %xmm1
je 0xa05ba
xorps %xmm1, %xmm1
cvtsi2sd %eax, %xmm1
movapd %xmm2, %xmm0
subsd %xmm1, %xmm0
divsd 0x68a4e(%rip), %xmm0    # 0x109018
addsd 0x6b2c6(%rip), %xmm0    # 0x10b898
movsd %xmm2, -0x30(%rbp)
callq 0x5c71d
leaq 0x26d10d(%rip), %rax    # 0x30d6f0
xorl %ebx, %ebx
movl %ebx, (%rax)
callq 0xae92
movsd -0x30(%rbp), %xmm1
addsd 0x68a3f(%rip), %xmm0    # 0x109038
movsd %xmm0, (%r12)
movapd %xmm1, %xmm0
divsd 0x6b26d(%rip), %xmm0    # 0x10b878
cvttsd2si %xmm0, %ecx
movl %ebx, 0x2d1843(%rip)    # 0x371e58
leaq 0x26d11c(%rip), %rax    # 0x30d738
movw %cx, (%rax)
leaq 0x26d122(%rip), %rax    # 0x30d748
movl $0x3, (%rax)
leaq 0x26ccf9(%rip), %rax    # 0x30d32c
cmpl %ebx, (%rax)
jne 0xa0644
leaq 0x178206(%rip), %rdx    # 0x218844
movl $0x806, (%rdx)          # imm = 0x806
movzwl %cx, %ecx
xorps %xmm0, %xmm0
cvtsi2sd %ecx, %xmm0
mulsd 0x7dbba(%rip), %xmm0    # 0x11e210
movl $0x9, %ecx
addsd %xmm0, %xmm1
cvttsd2si %xmm1, %edx
movl %ecx, (%rax)
movslq %edx, %rax
leaq 0x26d0e1(%rip), %rdx    # 0x30d750
movq %rax, (%rdx)
leaq 0x26d0c7(%rip), %rdx    # 0x30d740
movl %eax, (%rdx)
leaq 0x26d0c2(%rip), %rdx    # 0x30d744
movl %eax, (%rdx)
leaq 0x26d0b1(%rip), %rdx    # 0x30d73c
movl %eax, (%rdx)
leaq 0x26cc94(%rip), %rax    # 0x30d328
movl %ecx, (%rax)
leaq 0x26ce2b(%rip), %rax    # 0x30d4c8
movl (%rax), %eax
cmpl 0xc9ccb(%rip), %eax     # 0x16a370
je 0xa06ac
callq 0x730e8
cmpl $0x0, 0x2cc515(%rip)    # 0x36cbc8
jne 0xa06d2
leaq 0x26ce44(%rip), %rax    # 0x30d500
cmpl $0x20, (%rax)
jne 0xa06d2
leaq 0x83ac6(%rip), %rdi     # 0x12418e
movl $0x65, %esi
callq 0x5edda
leaq 0x296b77(%rip), %rax    # 0x337250
movb $0x0, (%rax)
leaq 0x297b6f(%rip), %rax    # 0x338252
xorl %ecx, %ecx
movw %cx, (%rax)
leaq 0x297b65(%rip), %rax    # 0x338254
movw %cx, (%rax)
leaq 0x297b5d(%rip), %rax    # 0x338256
movw %cx, (%rax)
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq