// File: /workspace/github_cpp_makefile_star2_O3/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 0xb517
movl %edi, %ebx
callq 0xb025
movl %ebx, %eax
imulq $0x2260, %rax, %r13     # imm = 0x2260
leaq 0x302e15(%rip), %rdi    # 0x30e2e0
movb $0x0, 0x2251(%r13,%rdi)
cmpl $0xa, %ebx
sete %cl
movq 0x20d3ef(%rip), %rax    # 0x2188d0
cmpq $0x400, %rax            # imm = 0x400
sete %dl
andb %cl, %dl
movl 0x233a3e(%rip), %ecx    # 0x23ef30
testl %ecx, %ecx
sete %sil
testb %sil, %dl
jne 0xb517
testl %ecx, %ecx
sete %cl
cmpl $0xa, %ebx
sete %dl
cmpq $0x800000, %rax         # imm = 0x800000
sete %al
andb %dl, %al
testb %cl, %al
je 0xb526
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq (%rdi,%r13), %r12
movl $0x0, 0x233a60(%rip)    # 0x23ef94
movl (%r12), %eax
movl 0x4(%r12), %edx
movl %eax, %ecx
orl %edx, %ecx
jne 0xb5bf
cmpb $0x0, 0x10d(%r12)
je 0xb6fc
cmpl $0x0, 0x2234(%r12)
je 0xb517
movl %ebx, %edi
callq 0xb084
movl $0x2, 0x2230(%r12)
movb $0x0, 0x2250(%r12)
callq 0xa80b
mulsd 0xfdab2(%rip), %xmm0    # 0x109038
movsd %xmm0, 0x3021da(%rip)   # 0x30d768
movsd %xmm0, 0x2248(%r12)
cmpl $0xa, %ebx
jne 0xb5a7
movl $0x1, 0x301e79(%rip)    # 0x30d420
movl $0x30d40, %edi          # imm = 0x30D40
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x9540
testl %edx, %edx
je 0xb60b
cmpl $0x3e7, %edx            # imm = 0x3E7
je 0xb5eb
cmpl $0x270f, %edx           # imm = 0x270F
jne 0xb5db
cmpb $0x0, 0xc(%r12)
jne 0xb62a
leaq 0xc(%r12), %rdi
decl %edx
leaq 0xfe479(%rip), %rsi     # 0x109a62
jmp 0xb623
movabsq $0x72656874616568, %rax # imm = 0x72656874616568
movq %rax, 0x11(%r12)
movabsq $0x6165682f7665642f, %rax # imm = 0x6165682F7665642F
movq %rax, 0xc(%r12)
jmp 0xb62a
testl %eax, %eax
je 0xb6fc
leaq 0xc(%r12), %rdi
decl %eax
leaq 0xfe44f(%rip), %rsi     # 0x109a70
movl %eax, %edx
xorl %eax, %eax
callq 0x9060
leaq 0xc(%r12), %r14
leaq 0xfe446(%rip), %rdi     # 0x109a7c
movl %ebx, %esi
movq %r14, %rdx
xorl %eax, %eax
callq 0x9030
movq 0x144827(%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 0xb714
leaq 0x16588c(%rip), %rsi    # 0x170f00
movl %eax, %edi
callq 0x93e0
xorps %xmm0, %xmm0
movups %xmm0, 0x1658bf(%rip)   # 0x170f44
movups %xmm0, 0x1658c8(%rip)   # 0x170f54
movups %xmm0, 0x1658d1(%rip)   # 0x170f64
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
movl $0x0, 0x1658b6(%rip)    # 0x170f74
movl %eax, 0x165880(%rip)    # 0x170f44
cmpl $0x2, 0x218(%r12)
jne 0xb6d8
orl $0x40, %eax
movl %eax, 0x16586c(%rip)    # 0x170f44
movl 0x21c(%r12), %ecx
cmpl $0x1, %ecx
je 0xb782
cmpl $0x2, %ecx
jne 0xb78f
movl $0x100, %ecx            # imm = 0x100
jmp 0xb787
movl $0xfffffff4, %eax       # imm = 0xFFFFFFF4
movq %rax, 0x2230(%r12)
movb $0x4, 0x2251(%r12)
jmp 0xb76a
leaq 0xfe37c(%rip), %rdi     # 0x109a97
movl %ebx, %esi
movl %eax, %edx
xorl %eax, %eax
callq 0x9030
movl $0xa, %edi
callq 0x9510
leaq 0xfff2a(%rip), %rdi     # 0x10b661
callq 0x95d0
leaq 0xfff52(%rip), %rdi     # 0x10b695
callq 0x95d0
leaq 0xfff71(%rip), %rdi     # 0x10b6c0
callq 0x95d0
orb $0x4, 0x2251(%r12)
movl $0xfffffff3, %eax       # imm = 0xFFFFFFF3
movq %rax, 0x2230(%r12)
cmpl $0xa, %ebx
jne 0xb517
movl $0x1, 0x301ca3(%rip)    # 0x30d420
jmp 0xb517
movl $0x300, %ecx            # imm = 0x300
orl %ecx, %eax
movl %eax, 0x1657b5(%rip)    # 0x170f44
movq $0x4, 0x1657a2(%rip) # 0x170f3c
movl $0x0, 0x1657a4(%rip)    # 0x170f48
movw $0x0, 0x1657a5(%rip)    # 0x170f52
movl 0x210(%r12), %eax
cmpl $0x257f, %eax           # imm = 0x257F
jg 0xb7eb
cmpl $0x4af, %eax            # imm = 0x4AF
jg 0xb80f
cmpl $0x96, %eax
je 0xb851
cmpl $0x12c, %eax            # imm = 0x12C
je 0xb869
cmpl $0x258, %eax            # imm = 0x258
jne 0xb849
movl $0x8, %r14d
jmp 0xb887
cmpl $0xe0ff, %eax           # imm = 0xE0FF
jg 0xb82c
cmpl $0x2580, %eax           # imm = 0x2580
je 0xb849
cmpl $0x4b00, %eax           # imm = 0x4B00
je 0xb871
cmpl $0x9600, %eax           # imm = 0x9600
jne 0xb849
movl $0xf, %r14d
jmp 0xb887
cmpl $0x4b0, %eax            # imm = 0x4B0
je 0xb859
cmpl $0x960, %eax            # imm = 0x960
je 0xb879
cmpl $0x12c0, %eax           # imm = 0x12C0
jne 0xb849
movl $0xc, %r14d
jmp 0xb887
cmpl $0xe100, %eax           # imm = 0xE100
je 0xb861
cmpl $0x1c200, %eax          # imm = 0x1C200
je 0xb881
cmpl $0x38400, %eax          # imm = 0x38400
jne 0xb849
movl $0x1003, %r14d          # imm = 0x1003
jmp 0xb887
movl $0xd, %r14d
jmp 0xb887
movl $0x5, %r14d
jmp 0xb887
movl $0x9, %r14d
jmp 0xb887
movl $0x1001, %r14d          # imm = 0x1001
jmp 0xb887
movl $0x7, %r14d
jmp 0xb887
movl $0xe, %r14d
jmp 0xb887
movl $0xb, %r14d
jmp 0xb887
movl $0x1002, %r14d          # imm = 0x1002
leaq 0x1656ae(%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 0x302a04(%rip), %rax    # 0x30e2e0
movl 0x8(%r13,%rax), %edi
testl %edi, %edi
js 0xb8fb
leaq -0x2c(%rbp), %rdx
movl $0x2, (%rdx)
movl $0x5416, %esi           # imm = 0x5416
xorl %eax, %eax
callq 0x9530
cmpl $0xa, %ebx
jne 0xb96b
cmpq $0x4000, 0x20cfc5(%rip) # imm = 0x4000
jne 0xb96b
movl 0x318195(%rip), %edi    # 0x323aa8
testl %edi, %edi
js 0xb94d
leaq -0x30(%rbp), %rdx
movl $0x2, (%rdx)
movl $0x5416, %esi           # imm = 0x5416
xorl %eax, %eax
callq 0x9530
movl 0x318175(%rip), %edi    # 0x323aa8
testl %edi, %edi
js 0xb94d
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)
jmp 0xb982
movabsq $0x100000002, %rax      # imm = 0x100000002
movq %rax, 0x222c(%r12)
cmpl $0xa, %ebx
jne 0xb98c
movl $0x1, 0x301a94(%rip)    # 0x30d420
movl $0x30d40, %edi          # imm = 0x30D40
callq 0x9540
cmpl $0x0, 0x3025af(%rip)    # 0x30df4c
je 0xb517
callq 0xe3081
jmp 0xb517