// File: /workspace/github_cpp_makefile_star2_O2/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 %r12
pushq %rbx
cmpl $0x12, %edi
ja 0xb383
movl %edi, %ebx
callq 0xaeb6
movl %ebx, %eax
imulq $0x2260, %rax, %rax     # imm = 0x2260
leaq 0x2ecfaf(%rip), %r12    # 0x2f82e0
movb $0x0, 0x2251(%rax,%r12)
cmpl $0xa, %ebx
sete %dl
movq 0x1f7589(%rip), %rcx    # 0x2028d0
cmpq $0x400, %rcx            # imm = 0x400
sete %sil
andb %dl, %sil
movl 0x21dbd5(%rip), %edx    # 0x228f30
testl %edx, %edx
sete %dil
testb %dil, %sil
jne 0xb383
testl %edx, %edx
sete %dl
cmpl $0xa, %ebx
sete %sil
cmpq $0x800000, %rcx         # imm = 0x800000
sete %cl
andb %sil, %cl
testb %dl, %cl
je 0xb38c
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
addq %rax, %r12
andl $0x0, 0x21dbfe(%rip)    # 0x228f94
movl (%r12), %eax
movl 0x4(%r12), %edx
movl %eax, %ecx
orl %edx, %ecx
jne 0xb413
cmpb $0x0, 0x10d(%r12)
je 0xb54d
cmpl $0x0, 0x2234(%r12)
je 0xb383
movl %ebx, %edi
callq 0xaf12
movl $0x2, 0x2230(%r12)
movb $0x0, 0x2250(%r12)
callq 0xad3f
movsd %xmm0, 0x2ec380(%rip)   # 0x2f7768
movsd %xmm0, 0x2248(%r12)
cmpl $0xa, %ebx
jne 0xb401
movl $0x1, 0x2ec01f(%rip)    # 0x2f7420
movl $0xc8, %edi
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0xae83
testl %edx, %edx
je 0xb45f
cmpl $0x3e7, %edx            # imm = 0x3E7
je 0xb43f
cmpl $0x270f, %edx           # imm = 0x270F
jne 0xb42f
cmpb $0x0, 0xc(%r12)
jne 0xb47e
leaq 0xc(%r12), %rdi
decl %edx
leaq 0xe8675(%rip), %rsi     # 0xf3ab2
jmp 0xb477
movabsq $0x72656874616568, %rax # imm = 0x72656874616568
movq %rax, 0x11(%r12)
movabsq $0x6165682f7665642f, %rax # imm = 0x6165682F7665642F
movq %rax, 0xc(%r12)
jmp 0xb47e
testl %eax, %eax
je 0xb54d
leaq 0xc(%r12), %rdi
decl %eax
leaq 0xe864b(%rip), %rsi     # 0xf3ac0
movl %eax, %edx
xorl %eax, %eax
callq 0x9060
leaq 0xc(%r12), %r14
leaq 0xe8642(%rip), %rdi     # 0xf3acc
movl %ebx, %esi
movq %r14, %rdx
xorl %eax, %eax
callq 0x9030
movq 0x12e9db(%rip), %rax    # 0x139e78
movq (%rax), %rdi
callq 0x9420
movq %r14, %rdi
movl $0x902, %esi            # imm = 0x902
xorl %eax, %eax
callq 0x9230
movl %eax, 0x8(%r12)
testl %eax, %eax
js 0xb565
leaq 0x14fa38(%rip), %rsi    # 0x15af00
movl %eax, %edi
callq 0x93e0
xorps %xmm0, %xmm0
movups %xmm0, 0x14fa6b(%rip)   # 0x15af44
movups %xmm0, 0x14fa74(%rip)   # 0x15af54
andl $0x0, 0x14fa8d(%rip)    # 0x15af74
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, 0x14fa55(%rip)   # 0x15af64
movl %eax, 0x14fa2f(%rip)    # 0x15af44
cmpl $0x2, 0x218(%r12)
jne 0xb529
orl $0x40, %eax
movl %eax, 0x14fa1b(%rip)    # 0x15af44
movl 0x21c(%r12), %ecx
cmpl $0x1, %ecx
je 0xb5d1
cmpl $0x2, %ecx
jne 0xb5de
movl $0x100, %ecx            # imm = 0x100
jmp 0xb5d6
movl $0xfffffff4, %eax       # imm = 0xFFFFFFF4
movq %rax, 0x2230(%r12)
movb $0x4, 0x2251(%r12)
jmp 0xb5b9
leaq 0xe857b(%rip), %rdi     # 0xf3ae7
movl %ebx, %esi
movl %eax, %edx
xorl %eax, %eax
callq 0x9030
pushq $0xa
popq %rdi
callq 0x9510
leaq 0xea156(%rip), %rdi     # 0xf56dc
callq 0x95d0
leaq 0xea17e(%rip), %rdi     # 0xf5710
callq 0x95d0
leaq 0xea19d(%rip), %rdi     # 0xf573b
callq 0x95d0
orb $0x4, 0x2251(%r12)
movl $0xfffffff3, %eax       # imm = 0xFFFFFFF3
movq %rax, 0x2230(%r12)
cmpl $0xa, %ebx
jne 0xb383
movl $0x1, 0x2ebe54(%rip)    # 0x2f7420
jmp 0xb383
movl $0x300, %ecx            # imm = 0x300
orl %ecx, %eax
movl %eax, 0x14f966(%rip)    # 0x15af44
movq $0x4, 0x14f953(%rip) # 0x15af3c
andl $0x0, 0x14f958(%rip)    # 0x15af48
andw $0x0, 0x14f95a(%rip)    # 0x15af52
movl 0x210(%r12), %eax
cmpl $0x96, %eax
je 0xb67c
cmpl $0x38400, %eax          # imm = 0x38400
je 0xb664
cmpl $0x258, %eax            # imm = 0x258
je 0xb68c
cmpl $0x4b0, %eax            # imm = 0x4B0
je 0xb684
cmpl $0x960, %eax            # imm = 0x960
je 0xb65c
cmpl $0x12c0, %eax           # imm = 0x12C0
je 0xb688
cmpl $0x2580, %eax           # imm = 0x2580
je 0xb658
cmpl $0x4b00, %eax           # imm = 0x4B00
je 0xb680
cmpl $0x9600, %eax           # imm = 0x9600
je 0xb660
cmpl $0xe100, %eax           # imm = 0xE100
je 0xb66c
cmpl $0x1c200, %eax          # imm = 0x1C200
je 0xb674
cmpl $0x12c, %eax            # imm = 0x12C
jne 0xb658
pushq $0x7
jmp 0xb68e
pushq $0xd
jmp 0xb68e
pushq $0xb
jmp 0xb68e
pushq $0xf
jmp 0xb68e
movl $0x1003, %r14d          # imm = 0x1003
jmp 0xb690
movl $0x1001, %r14d          # imm = 0x1001
jmp 0xb690
movl $0x1002, %r14d          # imm = 0x1002
jmp 0xb690
pushq $0x5
jmp 0xb68e
pushq $0xe
jmp 0xb68e
pushq $0x9
jmp 0xb68e
pushq $0xc
jmp 0xb68e
pushq $0x8
popq %r14
leaq 0x14f8a5(%rip), %r15    # 0x15af3c
movq %r15, %rdi
movl %r14d, %esi
callq 0x91f0
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
pushq $0x4
popq %rsi
movl $0x800, %edx            # imm = 0x800
xorl %eax, %eax
callq 0x91d0
pushq $0x1
popq %rsi
movl %ebx, %edi
callq 0xad58
cmpl $0xa, %ebx
jne 0xb72c
cmpq $0x4000, 0x1f71da(%rip) # imm = 0x4000
jne 0xb72c
pushq $0xa
popq %rbx
pushq $0x1
popq %rsi
movl %ebx, %edi
callq 0xad58
movl %ebx, %edi
xorl %esi, %esi
callq 0xada4
movl $0x1f4, %edi            # imm = 0x1F4
callq 0xae83
movabsq $0x100000002, %rax      # imm = 0x100000002
movq %rax, 0x222c(%r12)
jmp 0xb743
movabsq $0x100000002, %rax      # imm = 0x100000002
movq %rax, 0x222c(%r12)
cmpl $0xa, %ebx
jne 0xb74d
movl $0x1, 0x2ebcd3(%rip)    # 0x2f7420
movl $0xc8, %edi
callq 0xae83
cmpl $0x0, 0x2ec7ee(%rip)    # 0x2f7f4c
je 0xb383
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0xcfae4