// File: /workspace/github_cpp_makefile_star2_O1/jkingan[P]ladyheather/heather.cpp
// Function: init_com(unsigned int, int)
void init_com(unsigned port, int why)
{
int col, i;
int sync;

   // Initialize the serial device for asynchronous non-blocking communication
   // The device can be a serial port or ethernet connection
   if(port >= NUM_COM_PORTS) return;

   kill_com(port, why);   // in case COM port already open
   com[port].com_error = 0;
   if((port == RCVR_PORT) && (rcvr_type == NO_RCVR) && (enable_terminal == 0)) { // alternate ready/not ready each check
      return;
   }
if((port == RCVR_PORT) && (rcvr_type == TIDE_RCVR) && (enable_terminal == 0)) {
   return;
}

   crlf_seen = 0;
   if((com[port].com_port == 0) && (com[port].usb_port == 0) && com[port].IP_addr[0]) {
      //
      // TCP: In Windows, COM0 with process_com=TRUE means we're using TCP/IP
      //
      if(com[port].process_com) {
         init_tcpip(port);
         com[port].com_running = TCPIP_RUNNING;
         com[port].com_data_lost = 0;
         com[port].last_com_time = this_msec = GetMsecs();
         if(port == RCVR_PORT) first_request = 1;
         Sleep(COM_INIT_SLEEP);
      }
      return;
   }

   if((com[port].usb_port == USE_IDEV_NAME) && com[port].com_dev[0]) {  // user set com device name with -id=xxx command line option
   }
   else if(com[port].usb_port == USE_DEV_NAME) {  // use symlink to /dev/heather
      strcpy(com[port].com_dev, HEATHER_DEV);
   }
   else if(com[port].usb_port) {    // USB serial port
      #ifdef __MACH__
         if(com[port].usb_port <= 1) sprintf(com[port].com_dev, "/dev/tty.usbserial");
         else                        sprintf(com[port].com_dev, "/dev/tty.usbserial%d", com[port].usb_port-1);
      #elif defined(__linux__)
         sprintf(com[port].com_dev, "/dev/ttyUSB%d", com[port].usb_port-1);
      #elif defined(__FreeBSD__)
         sprintf(com[port].com_dev, "/dev/cuaU%d", com[port].usb_port-1);
      #else
         strcpy(com[port].com_dev, HEATHER_DEV);
      #endif
   }
   else if(com[port].com_port) {    // hardware serial port
      #ifdef __MACH__
         sprintf(com[port].com_dev, "/dev/ttys%d", com[port].com_port-1);
      #elif defined(__linux__)
         sprintf(com[port].com_dev, "/dev/ttyS%d", com[port].com_port-1);
      #elif defined(__FreeBSD__)
        sprintf(com[port].com_dev, "/dev/cuau%d", com[port].com_port-1);
      #else
        strcpy(com[port].com_dev, HEATHER_DEV);
      #endif
   }
   else {  // should never happen
      com[port].com_running = (-12); //NOT_RUNNING;
      com[port].process_com = 0;
      com[port].com_error |= INIT_ERR;
      if(port == RCVR_PORT) first_request = 1;
      return;
   }
printf("opening com[%d] device:%s\n", port, com[port].com_dev); // zork - show_debug_info
fflush(stdout);

//
// we init the com device here
//

/*
   #ifndef O_RSYNC
      sync = O_SYNC;
   #else
      sync = O_RSYNC;
   #endif
   sync = O_DSYNC | sync;
*/
sync = 0;

   com[port].com_fd = open(com[port].com_dev, O_RDWR | O_NOCTTY | O_NONBLOCK | sync);
   if(com[port].com_fd < 0) {
      printf("Com[%d] open failed:%d\n", port, com[port].com_fd);
      printf("\n");
      printf("Do you have permission to access the serial device?\n");
      printf("try:  sudo usermod -a -G dialout user_name\n");
      printf("      and then re-boot the system\n");
      com[port].process_com = 0;
      com[port].com_running = (-13); //NOT_RUNNING;
      com[port].com_error |= INIT_ERR;
      if(port == RCVR_PORT) first_request = 1;
      return;
   }

   tcgetattr(com[port].com_fd, &oldtio); /* save current port settings */

   /* set new port settings for non-canonical input processing */
   bzero(&newtio, sizeof(newtio));
   if     (com[port].data_bits == 7) newtio.c_cflag = CS7 | CLOCAL | CREAD;
   else if(com[port].data_bits == 6) newtio.c_cflag = CS6 | CLOCAL | CREAD;
   else if(com[port].data_bits == 5) newtio.c_cflag = CS5 | CLOCAL | CREAD;
   else                              newtio.c_cflag = CS8 | CLOCAL | CREAD;

   if(com[port].stop_bits == 2) newtio.c_cflag |= (CSTOPB);

   if(com[port].parity == ODD_PAR) {   // odd parity
      newtio.c_cflag |= (PARENB | PARODD);
   }
   else if(com[port].parity == EVEN_PAR) {  // even parity
      newtio.c_cflag |= (PARENB);
   }
   else {  // no parity
   }

   newtio.c_iflag = IGNPAR;   // ignore parity errors on reads
   newtio.c_oflag = 0;
   newtio.c_lflag = 0;
   newtio.c_cc[VMIN] = 0;
   newtio.c_cc[VTIME] = 0;

   if     (com[port].baud_rate == 150)    i = B150;
   else if(com[port].baud_rate == 300)    i = B300;
   else if(com[port].baud_rate == 600)    i = B600;
   else if(com[port].baud_rate == 1200)   i = B1200;
   else if(com[port].baud_rate == 2400)   i = B2400;
   else if(com[port].baud_rate == 4800)   i = B4800;
   else if(com[port].baud_rate == 9600)   i = B9600;
   else if(com[port].baud_rate == 19200)  i = B19200;
   else if(com[port].baud_rate == 38400)  i = B38400;
   else if(com[port].baud_rate == 57600)  i = B57600;
   else if(com[port].baud_rate == 115200) i = B115200;
   else if(com[port].baud_rate == 230400) i = B230400;
   else i = B9600;
   cfsetispeed(&newtio, i);
   cfsetospeed(&newtio, i);

   tcflush(com[port].com_fd, TCIFLUSH);
   tcsetattr(com[port].com_fd, TCSANOW, &newtio);
   fcntl(com[port].com_fd, F_SETFL, FNDELAY); // make sure port is in non-blocking mode

   SetDtrLine(port, 1);
   if((rcvr_type == ACRON_RCVR) && (port == RCVR_PORT)) {  // power up the RS-232 interface
      SetDtrLine(port, 1);  // drive +12V
      SetRtsLine(port, 0);  // drive -12V
      Sleep(500);
   }
   com[port].com_running = SERIAL_RUNNING;
   com[port].port_used = 2;  // serial port open
   if(port == RCVR_PORT) first_request = 1;
   Sleep(COM_INIT_SLEEP);

   if(modem_alarms) reset_alarm();
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
cmpl $0x12, %edi
ja 0xb9b4
movl %edi, %ebx
callq 0xb034
movl %ebx, %eax
imulq $0x2260, %rax, %r13     # imm = 0x2260
leaq 0x302e10(%rip), %rdi    # 0x30e2e0
movb $0x0, 0x2251(%r13,%rdi)
cmpl $0xa, %ebx
sete %cl
movq 0x20d3ea(%rip), %rax    # 0x2188d0
cmpq $0x400, %rax            # imm = 0x400
sete %dl
andb %cl, %dl
movl 0x233a39(%rip), %ecx    # 0x23ef30
testl %ecx, %ecx
sete %sil
testb %sil, %dl
jne 0xb9b4
testl %ecx, %ecx
sete %cl
cmpl $0xa, %ebx
sete %dl
cmpq $0x800000, %rax         # imm = 0x800000
sete %al
andb %dl, %al
testb %cl, %al
jne 0xb9b4
leaq (%rdi,%r13), %r12
movl $0x0, 0x233a62(%rip)    # 0x23ef94
movl (%r12), %edx
testl %edx, %edx
jne 0xb546
cmpl $0x0, 0x4(%r12)
je 0xb77e
movl 0x4(%r12), %eax
testl %eax, %eax
je 0xb590
cmpl $0x3e7, %eax            # imm = 0x3E7
je 0xb570
cmpl $0x270f, %eax           # imm = 0x270F
jne 0xb5a8
cmpb $0x0, 0xc(%r12)
jne 0xb5bf
testl %eax, %eax
je 0xb590
cmpl $0x3e7, %eax            # imm = 0x3E7
jne 0xb5a8
movabsq $0x72656874616568, %rax # imm = 0x72656874616568
movq %rax, 0x11(%r12)
movabsq $0x6165682f7665642f, %rax # imm = 0x6165682F7665642F
movq %rax, 0xc(%r12)
jmp 0xb5bf
testl %edx, %edx
je 0xb83f
leaq 0xc(%r12), %rdi
decl %edx
leaq 0xfe4ba(%rip), %rsi     # 0x109a60
jmp 0xb5b8
leaq 0xc(%r12), %rdi
decl %eax
leaq 0xfe49c(%rip), %rsi     # 0x109a52
movl %eax, %edx
xorl %eax, %eax
callq 0x9060
leaq 0xc(%r12), %r14
leaq 0xfe4a1(%rip), %rdi     # 0x109a6c
movl %ebx, %esi
movq %r14, %rdx
xorl %eax, %eax
callq 0x9030
movq 0x144892(%rip), %rax    # 0x14fe70
movq (%rax), %rdi
callq 0x9420
movq %r14, %rdi
movl $0x902, %esi            # imm = 0x902
xorl %eax, %eax
callq 0x9240
movl %eax, 0x8(%r12)
testl %eax, %eax
js 0xb67d
leaq 0x1658fb(%rip), %rsi    # 0x170f00
movl %eax, %edi
callq 0x93e0
xorps %xmm0, %xmm0
movups %xmm0, 0x165926(%rip)   # 0x170f3c
movups %xmm0, 0x16592f(%rip)   # 0x170f4c
movups %xmm0, 0x165938(%rip)   # 0x170f5c
movl 0x214(%r12), %eax
addl $-0x5, %eax
movl %eax, %ecx
shll $0x4, %ecx
addl $0x880, %ecx            # imm = 0x880
cmpl $0x3, %eax
movl $0x8b0, %eax            # imm = 0x8B0
cmovbl %ecx, %eax
movups %xmm0, 0x16591c(%rip)   # 0x170f68
movl %eax, 0x1658f2(%rip)    # 0x170f44
cmpl $0x2, 0x218(%r12)
jne 0xb664
orb $0x40, 0x1658e0(%rip)   # 0x170f44
movl 0x21c(%r12), %eax
cmpl $0x1, %eax
je 0xb6d8
cmpl $0x2, %eax
jne 0xb6e3
movl $0x100, %eax            # imm = 0x100
jmp 0xb6dd
leaq 0xfe403(%rip), %rdi     # 0x109a87
movl %ebx, %esi
movl %eax, %edx
xorl %eax, %eax
callq 0x9030
movl $0xa, %edi
callq 0x9510
leaq 0xfffb4(%rip), %rdi     # 0x10b654
callq 0x95d0
leaq 0xfffdc(%rip), %rdi     # 0x10b688
callq 0x95d0
leaq 0xffffb(%rip), %rdi     # 0x10b6b3
callq 0x95d0
orb $0x4, 0x2251(%r12)
movl $0xfffffff3, %eax       # imm = 0xFFFFFFF3
movq %rax, 0x2230(%r12)
jmp 0xb855
movl $0x300, %eax            # imm = 0x300
orl %eax, 0x165861(%rip)    # 0x170f44
movq $0x4, 0x16584e(%rip) # 0x170f3c
movl $0x0, 0x165850(%rip)    # 0x170f48
movw $0x0, 0x165851(%rip)    # 0x170f52
movl 0x210(%r12), %eax
cmpl $0x257f, %eax           # imm = 0x257F
jg 0xb747
cmpl $0x4af, %eax            # imm = 0x4AF
jg 0xb7fe
cmpl $0x96, %eax
je 0xb875
cmpl $0x12c, %eax            # imm = 0x12C
je 0xb88d
cmpl $0x258, %eax            # imm = 0x258
jne 0xb86d
movl $0x8, %r14d
jmp 0xb8ab
cmpl $0xe0ff, %eax           # imm = 0xE0FF
jg 0xb822
cmpl $0x2580, %eax           # imm = 0x2580
je 0xb86d
cmpl $0x4b00, %eax           # imm = 0x4B00
je 0xb895
cmpl $0x9600, %eax           # imm = 0x9600
jne 0xb86d
movl $0xf, %r14d
jmp 0xb8ab
cmpb $0x0, 0x10d(%r12)
je 0xb546
cmpl $0x0, 0x2234(%r12)
je 0xb9b4
movl %ebx, %edi
callq 0xb093
movl $0x2, 0x2230(%r12)
movb $0x0, 0x2250(%r12)
callq 0xa826
mulsd 0xfd873(%rip), %xmm0    # 0x109038
movsd %xmm0, 0x301f9b(%rip)   # 0x30d768
movsd %xmm0, 0x2248(%r12)
cmpl $0xa, %ebx
jne 0xb7e6
movl $0x1, 0x301c3a(%rip)    # 0x30d420
movl $0x30d40, %edi          # imm = 0x30D40
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x9540
cmpl $0x4b0, %eax            # imm = 0x4B0
je 0xb87d
cmpl $0x960, %eax            # imm = 0x960
je 0xb89d
cmpl $0x12c0, %eax           # imm = 0x12C0
jne 0xb86d
movl $0xc, %r14d
jmp 0xb8ab
cmpl $0xe100, %eax           # imm = 0xE100
je 0xb885
cmpl $0x1c200, %eax          # imm = 0x1C200
je 0xb8a5
cmpl $0x38400, %eax          # imm = 0x38400
jne 0xb86d
movl $0x1003, %r14d          # imm = 0x1003
jmp 0xb8ab
movl $0xfffffff4, %eax       # imm = 0xFFFFFFF4
movq %rax, 0x2230(%r12)
movb $0x4, 0x2251(%r12)
cmpl $0xa, %ebx
jne 0xb9b4
movl $0x1, 0x301bb8(%rip)    # 0x30d420
jmp 0xb9b4
movl $0xd, %r14d
jmp 0xb8ab
movl $0x5, %r14d
jmp 0xb8ab
movl $0x9, %r14d
jmp 0xb8ab
movl $0x1001, %r14d          # imm = 0x1001
jmp 0xb8ab
movl $0x7, %r14d
jmp 0xb8ab
movl $0xe, %r14d
jmp 0xb8ab
movl $0xb, %r14d
jmp 0xb8ab
movl $0x1002, %r14d          # imm = 0x1002
leaq 0x16568a(%rip), %r15    # 0x170f3c
movq %r15, %rdi
movl %r14d, %esi
callq 0x9200
movq %r15, %rdi
movl %r14d, %esi
callq 0x93b0
movl 0x8(%r12), %edi
xorl %esi, %esi
callq 0x95b0
movl 0x8(%r12), %edi
xorl %esi, %esi
movq %r15, %rdx
callq 0x94c0
movl 0x8(%r12), %edi
movl $0x4, %esi
movl $0x800, %edx            # imm = 0x800
xorl %eax, %eax
callq 0x91e0
leaq 0x3029e0(%rip), %rax    # 0x30e2e0
movl 0x8(%r13,%rax), %edi
testl %edi, %edi
js 0xb91f
leaq -0x2c(%rbp), %rdx
movl $0x2, (%rdx)
movl $0x5416, %esi           # imm = 0x5416
xorl %eax, %eax
callq 0x9530
cmpl $0xa, %ebx
jne 0xb97b
cmpq $0x4000, 0x20cfa1(%rip) # imm = 0x4000
jne 0xb97b
movl 0x318171(%rip), %edi    # 0x323aa8
testl %edi, %edi
js 0xb951
leaq -0x30(%rbp), %rdx
movl $0x2, (%rdx)
movl $0x5416, %esi           # imm = 0x5416
xorl %eax, %eax
callq 0x9530
movl 0x318151(%rip), %edi    # 0x323aa8
testl %edi, %edi
js 0xb971
leaq -0x34(%rbp), %rdx
movl $0x4, (%rdx)
movl $0x5417, %esi           # imm = 0x5417
xorl %eax, %eax
callq 0x9530
movl $0x7a120, %edi          # imm = 0x7A120
callq 0x9540
movabsq $0x100000002, %rax      # imm = 0x100000002
movq %rax, 0x222c(%r12)
cmpl $0xa, %ebx
jne 0xb99c
movl $0x1, 0x301a84(%rip)    # 0x30d420
movl $0x30d40, %edi          # imm = 0x30D40
callq 0x9540
cmpl $0x0, 0x30259f(%rip)    # 0x30df4c
je 0xb9b4
callq 0xe2532
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq