// File: /workspace/github_cpp_makefile_star2_O1/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 0x852eb(%rip), %rdi     # 0x12551f
callq 0x58e77
leaq 0x26d4b8(%rip), %r12    # 0x30d6f8
movq $0x0, (%r12)
movl $0x0, 0x2cc976(%rip)    # 0x36cbc8
leaq 0x29b5d7(%rip), %rdi    # 0x33b830
leaq 0x852c9(%rip), %rsi     # 0x125529
callq 0x9090
movq %rax, %rbx
leaq 0x19e121(%rip), %rax    # 0x23e390
movq (%rax), %r15
testq %r15, %r15
je 0xa02b4
leaq 0x17ad82(%rip), %rax    # 0x21b000
movl (%rax), %r12d
leaq 0x29b5a8(%rip), %r13    # 0x33b830
movq %r13, %rdi
callq 0x9120
leaq 0x85296(%rip), %rsi     # 0x12552d
movq %r15, %rdi
movl %r14d, %edx
movl %r12d, %ecx
leaq 0x26d451(%rip), %r12    # 0x30d6f8
movl %eax, %r8d
movq %r13, %r9
xorl %eax, %eax
callq 0x9560
testq %rbx, %rbx
je 0xa04ea
xorl %r14d, %r14d
movq %rbx, %rdi
callq 0x98c6
movl %eax, -0x100(%rbp,%r14,4)
incq %r14
addq $0x3, %rbx
cmpq $0x2c, %r14
jne 0xa02c0
leaq 0x26d450(%rip), %rax    # 0x30d734
cmpl $0x0, (%rax)
sete %cl
movl -0x80(%rbp), %eax
testl %eax, %eax
setne %dl
andb %cl, %dl
cmpb $0x1, %dl
jne 0xa0339
leaq 0x26d428(%rip), %rcx    # 0x30d728
movl %eax, (%rcx)
leaq 0x26d013(%rip), %rcx    # 0x30d31c
cmpl $0x0, (%rcx)
jne 0xa0339
leaq 0x26d1ff(%rip), %rdx    # 0x30d514
cmpl $0x7e1, (%rdx)          # imm = 0x7E1
setge %dl
cmpl $0x12, %eax
setl %sil
andb %dl, %sil
cmpl $0x11, %eax
setl %al
orb %sil, %al
jne 0xa0339
movl $0x6f, (%rcx)
leaq 0x26cf58(%rip), %rax    # 0x30d298
movl $0x8, (%rax)
movl -0x7c(%rbp), %eax
leaq 0x26ce88(%rip), %rcx    # 0x30d1d8
movl $0xffffff7f, %edx       # imm = 0xFFFFFF7F
andl (%rcx), %edx
andl $0x2, %eax
shll $0x6, %eax
orl %edx, %eax
movl %eax, (%rcx)
movl -0x94(%rbp), %eax
movl -0x90(%rbp), %ecx
shll $0x18, %eax
shll $0x10, %ecx
addl %eax, %ecx
movl -0x8c(%rbp), %eax
shll $0x8, %eax
addl %ecx, %eax
addl -0x88(%rbp), %eax
cvtsi2sd %eax, %xmm2
leaq 0x26d12c(%rip), %rax    # 0x30d4bd
cmpb $0x0, (%rax)
xorpd %xmm1, %xmm1
je 0xa03a8
leaq 0x26d387(%rip), %rax    # 0x30d728
xorps %xmm1, %xmm1
cvtsi2sdl (%rax), %xmm1
movapd %xmm2, %xmm0
subsd %xmm1, %xmm0
divsd 0x68c60(%rip), %xmm0    # 0x109018
addsd 0x6b4c8(%rip), %xmm0    # 0x10b888
movsd %xmm2, -0x30(%rbp)
callq 0x5c837
leaq 0x26d31f(%rip), %rax    # 0x30d6f0
xorl %ebx, %ebx
movl %ebx, (%rax)
callq 0xaea1
movsd -0x30(%rbp), %xmm1
addsd 0x68c51(%rip), %xmm0    # 0x109038
movsd %xmm0, (%r12)
movapd %xmm1, %xmm0
divsd 0x6b46f(%rip), %xmm0    # 0x10b868
cvttsd2si %xmm0, %ecx
movl %ebx, 0x2d1a55(%rip)    # 0x371e58
leaq 0x26d32e(%rip), %rax    # 0x30d738
movw %cx, (%rax)
leaq 0x26d334(%rip), %rax    # 0x30d748
movl $0x3, (%rax)
leaq 0x26cf0b(%rip), %rax    # 0x30d32c
cmpl %ebx, (%rax)
jne 0xa0432
leaq 0x178418(%rip), %rdx    # 0x218844
movl $0x806, (%rdx)          # imm = 0x806
movzwl %cx, %ecx
xorps %xmm0, %xmm0
cvtsi2sd %ecx, %xmm0
mulsd 0x7dd24(%rip), %xmm0    # 0x11e168
movl $0x9, %ecx
addsd %xmm0, %xmm1
cvttsd2si %xmm1, %edx
movl %ecx, (%rax)
movslq %edx, %rax
leaq 0x26d2f3(%rip), %rdx    # 0x30d750
movq %rax, (%rdx)
leaq 0x26d2d9(%rip), %rdx    # 0x30d740
movl %eax, (%rdx)
leaq 0x26d2d4(%rip), %rdx    # 0x30d744
movl %eax, (%rdx)
leaq 0x26d2c3(%rip), %rdx    # 0x30d73c
movl %eax, (%rdx)
leaq 0x26cea6(%rip), %rax    # 0x30d328
movl %ecx, (%rax)
leaq 0x26d03d(%rip), %rax    # 0x30d4c8
movl (%rax), %eax
cmpl 0xc9edd(%rip), %eax     # 0x16a370
je 0xa049a
callq 0x7327e
cmpl $0x0, 0x2cc727(%rip)    # 0x36cbc8
jne 0xa04c0
leaq 0x26d056(%rip), %rax    # 0x30d500
cmpl $0x20, (%rax)
jne 0xa04c0
leaq 0x83bf8(%rip), %rdi     # 0x1240ae
movl $0x65, %esi
callq 0x5ee52
leaq 0x296d89(%rip), %rax    # 0x337250
movb $0x0, (%rax)
leaq 0x297d81(%rip), %rax    # 0x338252
xorl %ecx, %ecx
movw %cx, (%rax)
leaq 0x297d77(%rip), %rax    # 0x338254
movw %cx, (%rax)
leaq 0x297d6f(%rip), %rax    # 0x338256
movw %cx, (%rax)
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq