// File: 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
subq $0x110, %rsp            # imm = 0x110
movl %edi, -0x4(%rbp)
leaq 0x223b9b(%rip), %rax    # 0x29d5f0
cmpl $0x0, (%rax)
je 0x79a5c
jmp 0x79abe
leaq 0x2f1f25(%rip), %rax    # 0x36b988
movzbl (%rax), %eax
cmpl $0x48, %eax
jne 0x79a6d
jmp 0x79abc
leaq 0x1fe03c(%rip), %rax    # 0x277ab0
cmpq $0x80000, (%rax)        # imm = 0x80000
jne 0x79a8b
leaq 0x1fe070(%rip), %rax    # 0x277af4
cmpl $0x3, (%rax)
jne 0x79a8b
jmp 0x79aba
leaq 0x1fe01e(%rip), %rcx    # 0x277ab0
movabsq $0x5fb3f521a, %rax      # imm = 0x5FB3F521A
andq (%rcx), %rax
cmpq $0x0, %rax
je 0x79ab6
leaq 0x2246c8(%rip), %rax    # 0x29e174
cmpl $0x0, (%rax)
jne 0x79ab6
jmp 0x79f2c
jmp 0x79ab8
jmp 0x79aba
jmp 0x79abc
jmp 0x79abe
leaq 0x223b2b(%rip), %rax    # 0x29d5f0
movl (%rax), %eax
andl $0x8, %eax
cmpl $0x0, %eax
je 0x79b14
leaq 0x223a7a(%rip), %rax    # 0x29d550
cmpq $0x0, (%rax)
je 0x79b14
leaq 0x223a6d(%rip), %rax    # 0x29d550
movq (%rax), %rdi
movl -0x4(%rbp), %edx
leaq 0x116b12(%rip), %rsi    # 0x190602
movb $0x0, %al
callq 0x9550
leaq 0x223afe(%rip), %rax    # 0x29d5fc
cmpl $0x0, (%rax)
je 0x79b12
leaq 0x223a46(%rip), %rax    # 0x29d550
movq (%rax), %rdi
callq 0x9430
jmp 0x79b14
leaq 0x2f39a5(%rip), %rax    # 0x36d4c0
cmpl $0x0, 0x19c50(%rax)
jle 0x79c31
leaq 0x2f3991(%rip), %rax    # 0x36d4c0
cmpl $0x0, 0x179f0(%rax)
jle 0x79c31
leaq 0x2006dd(%rip), %rax    # 0x27a220
cmpl $0x0, (%rax)
jl 0x79b84
movl -0x4(%rbp), %eax
leaq 0x2006ce(%rip), %rcx    # 0x27a220
cmpl (%rcx), %eax
jne 0x79b6d
movl -0x4(%rbp), %eax
movl $0xb, %edi
movl $0x1, %edx
movzbl %al, %esi
callq 0xd160
jmp 0x79b7f
movl -0x4(%rbp), %eax
movl $0xb, %edi
xorl %edx, %edx
movzbl %al, %esi
callq 0xd160
jmp 0x79c27
leaq 0x200691(%rip), %rax    # 0x27a21c
cmpl $0x0, (%rax)
jl 0x79bc9
movl -0x4(%rbp), %eax
leaq 0x200682(%rip), %rcx    # 0x27a21c
cmpl (%rcx), %eax
jne 0x79bb5
movl -0x4(%rbp), %eax
movl $0xb, %edi
movl $0x1, %edx
movzbl %al, %esi
callq 0xd160
jmp 0x79bc7
movl -0x4(%rbp), %eax
movl $0xb, %edi
xorl %edx, %edx
movzbl %al, %esi
callq 0xd160
jmp 0x79c25
leaq 0x200654(%rip), %rax    # 0x27a224
cmpl $0x0, (%rax)
jl 0x79c0e
movl -0x4(%rbp), %eax
leaq 0x200645(%rip), %rcx    # 0x27a224
cmpl (%rcx), %eax
jne 0x79bfa
movl -0x4(%rbp), %eax
movl $0xb, %edi
movl $0x1, %edx
movzbl %al, %esi
callq 0xd160
jmp 0x79c0c
movl -0x4(%rbp), %eax
movl $0xb, %edi
xorl %edx, %edx
movzbl %al, %esi
callq 0xd160
jmp 0x79c23
movl -0x4(%rbp), %eax
movl $0xb, %edi
movl $0x1, %edx
movzbl %al, %esi
callq 0xd160
jmp 0x79c25
jmp 0x79c27
movl $0xb, %edi
callq 0x79900
leaq 0x2f1d50(%rip), %rax    # 0x36b988
movzbl (%rax), %eax
cmpl $0x48, %eax
je 0x79c6a
leaq 0x2239a9(%rip), %rax    # 0x29d5f0
movl (%rax), %eax
andl $0x1, %eax
cmpl $0x0, %eax
jne 0x79c56
jmp 0x79f2c
leaq 0x2238e3(%rip), %rax    # 0x29d540
cmpq $0x0, (%rax)
jne 0x79c68
jmp 0x79f2c
jmp 0x79c6a
leaq 0x2244cf(%rip), %rax    # 0x29e140
cmpl $0x2, (%rax)
je 0x79c8f
leaq 0x2244c7(%rip), %rax    # 0x29e144
cmpl $0x10, (%rax)
jge 0x79c8f
cmpl $0x0, 0x3511eb(%rip)    # 0x3cae74
je 0x79e35
leaq 0x2244ae(%rip), %rax    # 0x29e144
movl (%rax), %eax
movl %eax, -0x8(%rbp)
cmpl $0x10, -0x8(%rbp)
jge 0x79cd4
movl -0x8(%rbp), %eax
movl $0x4, %ecx
cltd
idivl %ecx
cmpl $0x0, %edx
jne 0x79cbd
leaq 0x10c12c(%rip), %rdi    # 0x185de4
callq 0x79980
leaq 0x10c11e(%rip), %rdi    # 0x185de2
callq 0x79980
movl -0x8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x8(%rbp)
jmp 0x79c9b
movl $0x0, -0x8(%rbp)
movl -0x8(%rbp), %eax
leaq 0x22445f(%rip), %rcx    # 0x29e144
cmpl (%rcx), %eax
jge 0x79d7f
movl -0x8(%rbp), %eax
movl $0x4, %ecx
cltd
idivl %ecx
cmpl $0x0, %edx
jne 0x79d09
leaq 0x10c0e0(%rip), %rdi    # 0x185de4
callq 0x79980
movslq -0x8(%rbp), %rcx
leaq 0x3554bc(%rip), %rax    # 0x3cf1d0
movsbl (%rax,%rcx), %eax
cmpl $0x20, %eax
jl 0x79d63
movslq -0x8(%rbp), %rcx
leaq 0x3554a8(%rip), %rax    # 0x3cf1d0
movsbl (%rax,%rcx), %eax
cmpl $0x7e, %eax
jg 0x79d63
leaq -0x110(%rbp), %rdi
movslq -0x8(%rbp), %rcx
leaq 0x35548d(%rip), %rax    # 0x3cf1d0
movsbl (%rax,%rcx), %edx
leaq 0x1168b4(%rip), %rsi    # 0x190602
movb $0x0, %al
callq 0x9060
leaq -0x110(%rbp), %rdi
callq 0x79980
jmp 0x79d6f
leaq 0x11c05e(%rip), %rdi    # 0x195dc8
callq 0x79980
jmp 0x79d71
movl -0x8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x8(%rbp)
jmp 0x79cdb
cmpl $0x0, 0x3510ee(%rip)    # 0x3cae74
je 0x79dcf
leaq -0x110(%rbp), %rdi
leaq 0x109672(%rip), %rsi    # 0x183408
leaq 0x3510e3(%rip), %rdx    # 0x3cae80
movb $0x0, %al
callq 0x9060
leaq -0x110(%rbp), %rdi
callq 0x79980
leaq 0x3510c9(%rip), %rdi    # 0x3cae80
leaq 0x11a7d4(%rip), %rsi    # 0x194592
callq 0x9350
movl $0x0, 0x3510a7(%rip)    # 0x3cae74
jmp 0x79ddb
leaq 0x11e850(%rip), %rdi    # 0x198626
callq 0x79980
leaq 0x22435e(%rip), %rax    # 0x29e140
cmpl $0x2, (%rax)
jne 0x79e00
leaq 0x11e838(%rip), %rdi    # 0x198626
callq 0x79980
leaq 0x224346(%rip), %rax    # 0x29e140
movl $0x0, (%rax)
leaq 0x2237f9(%rip), %rax    # 0x29d600
cmpl $0x0, (%rax)
je 0x79e28
leaq 0x22372d(%rip), %rax    # 0x29d540
cmpq $0x0, (%rax)
je 0x79e28
leaq 0x223720(%rip), %rax    # 0x29d540
movq (%rax), %rdi
callq 0x9430
leaq 0x224315(%rip), %rax    # 0x29e144
movl $0x0, (%rax)
leaq 0x224308(%rip), %rax    # 0x29e144
movl (%rax), %eax
movl $0x4, %ecx
cltd
idivl %ecx
cmpl $0x0, %edx
jne 0x79e57
leaq 0x10bf92(%rip), %rdi    # 0x185de4
callq 0x79980
leaq -0x110(%rbp), %rdi
movl -0x4(%rbp), %edx
andl $0xff, %edx
leaq 0x1095a3(%rip), %rsi    # 0x183411
movb $0x0, %al
callq 0x9060
leaq -0x110(%rbp), %rdi
callq 0x79980
leaq 0x223778(%rip), %rax    # 0x29d600
cmpl $0x0, (%rax)
je 0x79ea9
leaq 0x2236ac(%rip), %rax    # 0x29d540
cmpq $0x0, (%rax)
je 0x79ea9
leaq 0x22369f(%rip), %rax    # 0x29d540
movq (%rax), %rdi
callq 0x9430
movl -0x4(%rbp), %eax
movb %al, %dl
leaq 0x22428f(%rip), %rax    # 0x29e144
movslq (%rax), %rcx
leaq 0x355311(%rip), %rax    # 0x3cf1d0
movb %dl, (%rax,%rcx)
leaq 0x22427b(%rip), %rax    # 0x29e144
movl (%rax), %ecx
addl $0x1, %ecx
leaq 0x22426f(%rip), %rax    # 0x29e144
movl %ecx, (%rax)
leaq 0x224262(%rip), %rax    # 0x29e140
cmpl $0x0, (%rax)
je 0x79f0f
movl -0x4(%rbp), %eax
leaq 0x200333(%rip), %rcx    # 0x27a220
cmpl (%rcx), %eax
jne 0x79f00
leaq 0x224248(%rip), %rax    # 0x29e140
movl $0x2, (%rax)
jmp 0x79f0d
leaq 0x224239(%rip), %rax    # 0x29e140
movl $0x0, (%rax)
jmp 0x79f2c
movl -0x4(%rbp), %eax
leaq 0x200303(%rip), %rcx    # 0x27a21c
cmpl (%rcx), %eax
jne 0x79f2a
leaq 0x22421c(%rip), %rax    # 0x29e140
movl $0x1, (%rax)
jmp 0x79f2c
addq $0x110, %rsp            # imm = 0x110
popq %rbp
retq
nopw %cs:(%rax,%rax)