// File: /workspace/github_cpp_makefile_star2_O2/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 0x7e142(%rip), %rdi     # 0x10f547
callq 0x520bf
leaq 0x2662e7(%rip), %rax    # 0x2f76f8
andq $0x0, (%rax)
andl $0x0, 0x2c57ac(%rip)    # 0x356bc8
leaq 0x29440d(%rip), %rdi    # 0x325830
leaq 0x7e127(%rip), %rsi     # 0x10f551
callq 0x9090
movq %rax, %rbx
leaq 0x196f57(%rip), %rax    # 0x228390
movq (%rax), %r15
testq %r15, %r15
je 0x91477
leaq 0x173bb8(%rip), %rax    # 0x205000
movl (%rax), %r12d
leaq 0x2943de(%rip), %r13    # 0x325830
movq %r13, %rdi
callq 0x9120
leaq 0x7e0f4(%rip), %rsi     # 0x10f555
movq %r15, %rdi
movl %r14d, %edx
movl %r12d, %ecx
movl %eax, %r8d
movq %r13, %r9
xorl %eax, %eax
callq 0x9560
testq %rbx, %rbx
je 0x91663
xorl %r14d, %r14d
cmpq $0x2c, %r14
je 0x914a2
movq %rbx, %rdi
callq 0x98b2
movl %eax, -0x100(%rbp,%r14,4)
incq %r14
addq $0x3, %rbx
jmp 0x91483
leaq 0x26628b(%rip), %rax    # 0x2f7734
cmpl $0x0, (%rax)
sete %cl
movl -0x80(%rbp), %eax
testl %eax, %eax
setne %dl
andb %cl, %dl
cmpb $0x1, %dl
jne 0x914cf
leaq 0x266263(%rip), %rcx    # 0x2f7728
movl %eax, (%rcx)
pushq $-0x6f
popq %rdi
callq 0x51a91
leaq 0x265dc2(%rip), %rax    # 0x2f7298
movl $0x8, (%rax)
movl -0x7c(%rbp), %eax
leaq 0x265cf2(%rip), %rcx    # 0x2f71d8
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 0x265f96(%rip), %rax    # 0x2f74bd
cmpb $0x0, (%rax)
xorpd %xmm1, %xmm1
je 0x9153b
leaq 0x2661f1(%rip), %rax    # 0x2f7728
cvtsi2sdl (%rax), %xmm1
movapd %xmm2, %xmm0
subsd %xmm1, %xmm0
divsd 0x61acd(%rip), %xmm0    # 0xf3018
addsd 0x643bd(%rip), %xmm0    # 0xf5910
movsd %xmm2, -0x30(%rbp)
callq 0x55392
leaq 0x26618c(%rip), %rax    # 0x2f76f0
andl $0x0, (%rax)
callq 0x83b1e
movsd -0x30(%rbp), %xmm1
movapd %xmm1, %xmm0
divsd 0x64373(%rip), %xmm0    # 0xf58f0
cvttsd2si %xmm0, %ecx
leaq 0x2661b0(%rip), %rax    # 0x2f7738
movw %cx, (%rax)
leaq 0x2661b6(%rip), %rax    # 0x2f7748
movl $0x3, (%rax)
leaq 0x265d8d(%rip), %rax    # 0x2f732c
cmpl $0x0, (%rax)
jne 0x915b1
leaq 0x171299(%rip), %rdx    # 0x202844
movl $0x806, (%rdx)          # imm = 0x806
movzwl %cx, %ecx
cvtsi2sd %ecx, %xmm0
mulsd 0x76cc8(%rip), %xmm0    # 0x108288
pushq $0x9
popq %rcx
addsd %xmm0, %xmm1
cvttsd2si %xmm1, %edx
movl %ecx, (%rax)
movslq %edx, %rax
leaq 0x266179(%rip), %rdx    # 0x2f7750
movq %rax, (%rdx)
leaq 0x26615f(%rip), %rdx    # 0x2f7740
movl %eax, (%rdx)
leaq 0x26615a(%rip), %rdx    # 0x2f7744
movl %eax, (%rdx)
leaq 0x266149(%rip), %rdx    # 0x2f773c
movl %eax, (%rdx)
leaq 0x265d2c(%rip), %rax    # 0x2f7328
movl %ecx, (%rax)
leaq 0x265ec3(%rip), %rax    # 0x2f74c8
movl (%rax), %eax
cmpl 0xc2d63(%rip), %eax     # 0x154370
je 0x91614
callq 0x694f6
cmpl $0x0, 0x2c55ad(%rip)    # 0x356bc8
jne 0x91638
leaq 0x265edc(%rip), %rax    # 0x2f7500
cmpl $0x20, (%rax)
jne 0x91638
leaq 0x7caa6(%rip), %rdi     # 0x10e0d6
pushq $0x65
popq %rsi
callq 0x576d4
leaq 0x28fc11(%rip), %rax    # 0x321250
movb $0x0, (%rax)
leaq 0x290c09(%rip), %rax    # 0x322252
andw $0x0, (%rax)
leaq 0x290c00(%rip), %rax    # 0x322254
andw $0x0, (%rax)
leaq 0x290bf7(%rip), %rax    # 0x322256
andw $0x0, (%rax)
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq