// File: /workspace/github_cpp_makefile_star2_O3/jkingan[P]ladyheather/heathgps.cpp
// Function: echo_stream(unsigned int)
void echo_stream(unsigned c)
{
static char buf[16];
int i;
char fmt[SLEN];

   // This routine dumps the serial port data stream to a file and/or com 
   // device.  

   if(log_stream) ;
else if(zoom_screen == 'H') ;
   else if((rcvr_type == THERMO_RCVR) && (enviro_type == LFS_ENVIRO)) ;
   else if(ASCII_RCVR && (crlf_seen == 0)) return;  // !!!! dont start logging until a full message has been seen
   else if(0 && (saw_rcvr_msg == 0)) {  //lfs
      return;  // !!!! dont start logging until a full message has been seen 
   }

   if((log_stream & LOG_RAW_STREAM) && raw_file) {  // recording raw receiver data
      fprintf(raw_file, "%c", c);
      if(raw_flush_mode) fflush(raw_file);  // sync raw data stream to disk every byte (for crash analysis)
//else fflush(raw_file);
   }

   if((com[ECHO_PORT].com_running > 0) && (com[RCVR_PORT].com_running > 0)) { // echo receiver data to another com device
      // see if we have a byte that indicates a possible packet boundary
      // that can be used to send the buffered characters
      if(pkt_end2 >= 0) {  // packets end in a two byte sequence
         if(c == (unsigned) pkt_end2) sendout(ECHO_PORT, c, EOM);  // flush buffer on packet end
         else                         sendout(ECHO_PORT, c, ADD_CHAR);
      }
      else if(pkt_end1 >= 0) {  // packets end with a known byte
         if(c == (unsigned) pkt_end1) sendout(ECHO_PORT, c, EOM);  // flush buffer on packet end 
         else                         sendout(ECHO_PORT, c, ADD_CHAR);
      }
      else if(pkt_start1 >= 0) {  // packets start with a known byte
         if(c == (unsigned) pkt_start1) sendout(ECHO_PORT, c, EOM); // flush buffer on packet start... kludge
         else                           sendout(ECHO_PORT, c, ADD_CHAR);
      }
      else {  // packets have no known start/end markers
         sendout(ECHO_PORT, c, EOM);  // ship them out byte-by-byte
      }

      drain_port(ECHO_PORT);
   }                                                    

   if(zoom_screen != 'H') {
      if((log_stream & LOG_HEX_STREAM) == 0) return;
      if(log_file == 0) return;
   }

   // logging hex data to log_file or zoom screen
   if(0 && TSIP_FMT_RCVR) {  // we can pretty-print format the packets
      ++kol;
      if(last_was_dle) {  
         if(c == DLE) {
            if(kol >= 16) {
               echo_log("\n      ");
               kol = (-1);
            }
            sprintf(fmt, "%02X:%02X ", c,c);
            echo_log(fmt);
         }
         else if(c == ETX) {
            sprintf(fmt, "%02X:%02X ", DLE,(unsigned) (c&0xFF));
            echo_log(fmt);
         }
         else {
            sprintf(fmt, "\n%02X:%02X", DLE,(unsigned) (c&0xFF));
            echo_log(fmt);
            if(c == 0x8F) echo_log("-");
            else          echo_log(" ");
            kol = (-1);
         }
         if(log_flush_mode && log_file) fflush(log_file);
         last_was_dle = 0;
      }
      else if(c == DLE) last_was_dle = 1;
      else {
         if(kol >= 16) {
            echo_log("\n      ");
            kol = 0;
         }
         sprintf(fmt, "%02X ", (unsigned) (c&0xFF));
         echo_log(fmt);
         if(log_flush_mode && log_file) fflush(log_file);
      }
   }
   else {  // hex dump the packets
      if((last_was_dle == 2) || (kol >= 16) || packet_end) {
         for(i=kol; i<16; i++) {
            if((i % 4) == 0) echo_log(" ");
            echo_log("   ");
         }

         for(i=0; i<kol; i++) {
            if((i % 4) == 0) echo_log(" ");
            if((buf[i] >= ' ') && (buf[i] <= 0x7E)) {
               sprintf(fmt, "%c", buf[i]);
               echo_log(fmt);
            }
            else echo_log(".");
         }

         if(packet_end) {
            sprintf(fmt, "   {%s}\n", packet_id);
            echo_log(fmt);
            strcpy(packet_id, "");
            packet_end = 0;
         }
         else echo_log("\n");

         if(last_was_dle == 2) {
            echo_log("\n");
            last_was_dle = 0;
         }
         if(log_flush_mode && log_file) fflush(log_file);

         kol = 0;
      }
      if((kol % 4) == 0) echo_log(" ");
//    if((kol % 8) == 0) echo_log(" ");
      sprintf(fmt, "%02X ", (unsigned) (c&0xFF));
      echo_log(fmt);
      if(log_flush_mode && log_file) fflush(log_file);
      buf[kol] = c;
      ++kol;

      if(last_was_dle) {  // used to do pretty printing some packet boundaries
         if(c == (unsigned) pkt_end2) last_was_dle = 2;
         else last_was_dle = 0;
      }
      else if(c == (unsigned) pkt_end1) last_was_dle = 1;
   }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x108, %rsp            # imm = 0x108
movl %edi, %r13d
leaq 0x1e62dd(%rip), %r14    # 0x23e410
movl (%r14), %eax
leaq 0x2b466b(%rip), %r15    # 0x30c7a8
testl %eax, %eax
jne 0x58186
cmpb $0x48, (%r15)
je 0x58186
leaq 0x1c0782(%rip), %rax    # 0x2188d0
movq (%rax), %rax
cmpq $0x80000, %rax          # imm = 0x80000
jne 0x58165
leaq 0x1c07b4(%rip), %rcx    # 0x218914
cmpl $0x3, (%rcx)
je 0x581bc
movabsq $0x5fb3f521a, %rcx      # imm = 0x5FB3F521A
testq %rcx, %rax
je 0x581bc
leaq 0x1e6e19(%rip), %rax    # 0x23ef94
cmpl $0x0, (%rax)
je 0x58475
jmp 0x581bc
shrb $0x3, %al
leaq 0x1e61e0(%rip), %rbx    # 0x23e370
movq (%rbx), %rsi
testq %rsi, %rsi
setne %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x581bc
movl %r13d, %edi
callq 0x9480
leaq 0x1e626d(%rip), %rax    # 0x23e41c
cmpl $0x0, (%rax)
je 0x581bc
movq (%rbx), %rdi
callq 0x9420
leaq 0x2b611d(%rip), %rax    # 0x30e2e0
cmpl $0x0, 0x19c50(%rax)
jle 0x58236
cmpl $0x0, 0x179f0(%rax)
jle 0x58236
leaq 0x1c2e64(%rip), %rax    # 0x21b040
movl (%rax), %eax
testl %eax, %eax
js 0x581ed
cmpl %r13d, %eax
jne 0x5821c
movzbl %r13b, %esi
jmp 0x58210
leaq 0x1c2e48(%rip), %rax    # 0x21b03c
movl (%rax), %eax
testl %eax, %eax
jns 0x58207
leaq 0x1c2e43(%rip), %rax    # 0x21b044
movl (%rax), %eax
testl %eax, %eax
js 0x581e7
movzbl %r13b, %esi
cmpl %r13d, %eax
jne 0x58220
movl $0xb, %edi
movl $0x1, %edx
jmp 0x58227
movzbl %r13b, %esi
movl $0xb, %edi
xorl %edx, %edx
callq 0xbcaa
movl $0xb, %edi
callq 0x5802f
cmpb $0x48, (%r15)
je 0x58257
testb $0x1, (%r14)
je 0x58475
leaq 0x1e6113(%rip), %rax    # 0x23e360
cmpq $0x0, (%rax)
je 0x58475
leaq 0x1e6d02(%rip), %rbx    # 0x23ef60
cmpl $0x2, (%rbx)
leaq 0x1e6cfc(%rip), %rax    # 0x23ef64
movl (%rax), %r12d
je 0x5828b
cmpl $0xf, %r12d
jg 0x5828b
cmpl $0x0, 0x313a1a(%rip)    # 0x36bc94
jne 0x5828b
testb $0x3, %r12b
jne 0x583e4
jmp 0x583d8
movl %r13d, -0x2c(%rbp)
cmpl $0xf, %r12d
jg 0x582ce
leaq 0xca8b8(%rip), %r14     # 0x122b54
leaq 0xca8af(%rip), %r15     # 0x122b52
testb $0x3, %r12b
jne 0x582b1
movq %r14, %rdi
callq 0x5807d
movq %r15, %rdi
callq 0x5807d
incl %r12d
cmpl $0x10, %r12d
jne 0x582a3
leaq 0x1e6c9b(%rip), %rax    # 0x23ef64
cmpl $0x0, (%rax)
jle 0x58331
xorl %r15d, %r15d
leaq 0xca87c(%rip), %r14     # 0x122b54
leaq 0x317d21(%rip), %r12    # 0x370000
leaq 0xdaf7a(%rip), %rbx     # 0x133260
leaq -0x130(%rbp), %r13
testb $0x3, %r15b
jne 0x582fb
movq %r14, %rdi
callq 0x5807d
movb (%r15,%r12), %al
leal -0x7f(%rax), %ecx
movq %rbx, %rdi
cmpb $-0x5f, %cl
jb 0x5831a
movb %al, -0x130(%rbp)
movb $0x0, -0x12f(%rbp)
movq %r13, %rdi
callq 0x5807d
incq %r15
leaq 0x1e6c3b(%rip), %rax    # 0x23ef64
movslq (%rax), %rax
cmpq %rax, %r15
jl 0x582ed
cmpl $0x0, 0x31395c(%rip)    # 0x36bc94
je 0x58374
leaq 0xc7e67(%rip), %rsi     # 0x1201a8
leaq 0x313958(%rip), %rdx    # 0x36bca0
leaq -0x130(%rbp), %r14
movq %r14, %rdi
xorl %eax, %eax
callq 0x9060
movq %r14, %rdi
callq 0x5807d
movb $0x0, 0x313938(%rip)    # 0x36bca0
movl $0x0, 0x313922(%rip)    # 0x36bc94
jmp 0x58380
leaq 0xdd719(%rip), %rdi     # 0x135a94
callq 0x5807d
leaq 0x1e6bd9(%rip), %rbx    # 0x23ef60
cmpl $0x2, (%rbx)
movl -0x2c(%rbp), %r13d
jne 0x583a2
leaq 0xdd6fd(%rip), %rdi     # 0x135a94
callq 0x5807d
movl $0x0, (%rbx)
leaq 0x1e6077(%rip), %rax    # 0x23e420
cmpl $0x0, (%rax)
setne %al
leaq 0x1e5faa(%rip), %rcx    # 0x23e360
movq (%rcx), %rdi
testq %rdi, %rdi
setne %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x583cb
callq 0x9420
leaq 0x1e6b92(%rip), %rax    # 0x23ef64
movl $0x0, (%rax)
leaq 0xca775(%rip), %rdi     # 0x122b54
callq 0x5807d
movzbl %r13b, %edx
leaq 0xc7dc2(%rip), %rsi     # 0x1201b1
leaq -0x130(%rbp), %r14
movq %r14, %rdi
xorl %eax, %eax
callq 0x9060
movq %r14, %rdi
callq 0x5807d
leaq 0x1e6011(%rip), %rax    # 0x23e420
cmpl $0x0, (%rax)
setne %al
leaq 0x1e5f44(%rip), %rcx    # 0x23e360
movq (%rcx), %rdi
testq %rdi, %rdi
setne %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x58431
callq 0x9420
leaq 0x1e6b2c(%rip), %rdx    # 0x23ef64
movslq (%rdx), %rax
leaq 0x317bbe(%rip), %rcx    # 0x370000
movb %r13b, (%rax,%rcx)
incl %eax
movl %eax, (%rdx)
cmpl $0x0, (%rbx)
je 0x58462
leaq 0x1c2bea(%rip), %rcx    # 0x21b040
xorl %eax, %eax
cmpl %r13d, (%rcx)
sete %al
addl %eax, %eax
jmp 0x58473
leaq 0x1c2bd3(%rip), %rcx    # 0x21b03c
movl $0x1, %eax
cmpl %r13d, (%rcx)
jne 0x58475
movl %eax, (%rbx)
addq $0x108, %rsp            # imm = 0x108
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq