// File: /workspace/github_cpp_makefile_star2_O2/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, %r12d
leaq 0x1d6ef5(%rip), %r14    # 0x228410
movl (%r14), %eax
leaq 0x2a5283(%rip), %r15    # 0x2f67a8
testl %eax, %eax
jne 0x51552
cmpb $0x48, (%r15)
je 0x51552
leaq 0x1b139a(%rip), %rcx    # 0x2028d0
movq (%rcx), %rcx
cmpq $0x80000, %rcx          # imm = 0x80000
jne 0x515d0
leaq 0x1b13c7(%rip), %rdx    # 0x202914
cmpl $0x3, (%rdx)
jne 0x515d0
shrb $0x3, %al
leaq 0x1d6e14(%rip), %rbx    # 0x228370
movq (%rbx), %rsi
testq %rsi, %rsi
setne %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x51588
movl %r12d, %edi
callq 0x9480
leaq 0x1d6ea1(%rip), %rax    # 0x22841c
cmpl $0x0, (%rax)
je 0x51588
movq (%rbx), %rdi
callq 0x9420
leaq 0x2a6d51(%rip), %rax    # 0x2f82e0
cmpl $0x0, 0x19c50(%rax)
jle 0x5160a
cmpl $0x0, 0x179f0(%rax)
jle 0x5160a
leaq 0x1b3a98(%rip), %rax    # 0x205040
movl (%rax), %eax
testl %eax, %eax
jns 0x515bf
leaq 0x1b3a87(%rip), %rax    # 0x20503c
movl (%rax), %eax
testl %eax, %eax
js 0x51841
movzbl %r12b, %esi
cmpl %r12d, %eax
jne 0x515f8
pushq $0xb
popq %rdi
pushq $0x1
popq %rdx
jmp 0x515fd
movabsq $0x5fb3f521a, %rdx      # imm = 0x5FB3F521A
testq %rdx, %rcx
je 0x51552
leaq 0x1d79aa(%rip), %rcx    # 0x228f94
cmpl $0x0, (%rcx)
je 0x5182f
jmp 0x51552
pushq $0xb
popq %rdi
xorl %edx, %edx
callq 0xba3b
pushq $0xb
popq %rdi
callq 0x5142f
cmpb $0x48, (%r15)
je 0x5162b
testb $0x1, (%r14)
je 0x5182f
leaq 0x1d6d3f(%rip), %rax    # 0x228360
cmpq $0x0, (%rax)
je 0x5182f
leaq 0x1d792e(%rip), %r15    # 0x228f60
cmpl $0x2, (%r15)
leaq 0x1d7927(%rip), %rax    # 0x228f64
movl (%rax), %ebx
je 0x5165d
cmpl $0xf, %ebx
jg 0x5165d
cmpl $0x0, 0x304647(%rip)    # 0x355c94
jne 0x5165d
testb $0x3, %bl
jne 0x5179e
jmp 0x51792
leaq 0xbb438(%rip), %r14     # 0x10ca9c
leaq 0xbb42f(%rip), %r15     # 0x10ca9a
cmpl $0x10, %ebx
jge 0x51689
testb $0x3, %bl
jne 0x5167d
movq %r14, %rdi
callq 0x5147d
movq %r15, %rdi
callq 0x5147d
incl %ebx
jmp 0x5166b
movl %r12d, -0x2c(%rbp)
xorl %r15d, %r15d
leaq 0xbb405(%rip), %r14     # 0x10ca9c
leaq 0x308962(%rip), %r12    # 0x35a000
leaq 0xcbadb(%rip), %rbx     # 0x11d180
leaq -0x130(%rbp), %r13
leaq 0x1d78b1(%rip), %rax    # 0x228f64
movslq (%rax), %rax
cmpq %rax, %r15
jge 0x516f2
testb $0x3, %r15b
jne 0x516c9
movq %r14, %rdi
callq 0x5147d
movb (%r15,%r12), %al
leal -0x7f(%rax), %ecx
movq %rbx, %rdi
cmpb $-0x5f, %cl
jb 0x516e8
movb %al, -0x130(%rbp)
movb $0x0, -0x12f(%rbp)
movq %r13, %rdi
callq 0x5147d
incq %r15
jmp 0x516ac
cmpl $0x0, 0x30459b(%rip)    # 0x355c94
je 0x51732
leaq 0xb89ee(%rip), %rsi     # 0x10a0f0
leaq 0x304597(%rip), %rdx    # 0x355ca0
leaq -0x130(%rbp), %r14
movq %r14, %rdi
xorl %eax, %eax
callq 0x9060
movq %r14, %rdi
callq 0x5147d
movb $0x0, 0x304577(%rip)    # 0x355ca0
andl $0x0, 0x304564(%rip)    # 0x355c94
jmp 0x5173e
leaq 0xce27b(%rip), %rdi     # 0x11f9b4
callq 0x5147d
movl -0x2c(%rbp), %r12d
leaq 0x1d7817(%rip), %r15    # 0x228f60
cmpl $0x2, (%r15)
jne 0x5175f
leaq 0xce25e(%rip), %rdi     # 0x11f9b4
callq 0x5147d
andl $0x0, (%r15)
leaq 0x1d6cba(%rip), %rax    # 0x228420
cmpl $0x0, (%rax)
setne %al
leaq 0x1d6bed(%rip), %rcx    # 0x228360
movq (%rcx), %rdi
testq %rdi, %rdi
setne %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x51788
callq 0x9420
leaq 0x1d77d5(%rip), %rax    # 0x228f64
andl $0x0, (%rax)
leaq 0xbb303(%rip), %rdi     # 0x10ca9c
callq 0x5147d
movzbl %r12b, %edx
leaq 0xb8950(%rip), %rsi     # 0x10a0f9
leaq -0x130(%rbp), %r14
movq %r14, %rdi
xorl %eax, %eax
callq 0x9060
movq %r14, %rdi
callq 0x5147d
leaq 0x1d6c57(%rip), %rax    # 0x228420
cmpl $0x0, (%rax)
setne %al
leaq 0x1d6b8a(%rip), %rcx    # 0x228360
movq (%rcx), %rdi
testq %rdi, %rdi
setne %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x517eb
callq 0x9420
leaq 0x1d7772(%rip), %rdx    # 0x228f64
movslq (%rdx), %rax
leaq 0x308804(%rip), %rcx    # 0x35a000
movb %r12b, (%rax,%rcx)
incl %eax
movl %eax, (%rdx)
cmpl $0x0, (%r15)
je 0x5181d
leaq 0x1b382f(%rip), %rcx    # 0x205040
xorl %eax, %eax
cmpl %r12d, (%rcx)
sete %al
addl %eax, %eax
jmp 0x5182c
leaq 0x1b3818(%rip), %rax    # 0x20503c
cmpl %r12d, (%rax)
jne 0x5182f
pushq $0x1
popq %rax
movl %eax, (%r15)
addq $0x108, %rsp            # imm = 0x108
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1b37fc(%rip), %rax    # 0x205044
movl (%rax), %eax
movzbl %r12b, %esi
testl %eax, %eax
jns 0x515c3
jmp 0x515c8