// File: /workspace/github_cpp_makefile_star2_O3/jkingan[P]ladyheather/heathui.cpp
// Function: config_port(unsigned int, char*)
int config_port(unsigned port, char *arg)
{
char c, d;
char out[512+1];
char *comma;

   // setup to use a com device.  Returns 0 if successful,  non-zero if error.
   //
   // If arg contains a ':', it's a TCP/IP address.
   // If arg starts with a '0' disable port processing.
   // If arg starts with a digit, it's a serial/USB port.
   // Otherwise, it's a device name  __linux__  __MACH__   __FreeBSD__
   // Serial port baud rate info can follow the device number/name - separated by a comma

   if(arg == 0) return 1;
   if(port >= NUM_COM_PORTS) return 2;
   if(port == TICC_PORT) no_adev_flag = 0;

   strcpy(out, arg);

   c = d = 0;
   c = out[0];
   if(c) d = out[1];

   comma = strchr(out, ',');
   if(comma) {
      *comma = 0;
      port = (unsigned) set_serial_params(port, comma+1, 0);
////  port = (unsigned) set_serial_params(port, comma+1, 1);
      if(port >= NUM_COM_PORTS) return 3;
   }

   if((port == FAN_PORT) && (com[FAN_PORT].user_set_baud == 0)) {
      com[FAN_PORT].baud_rate = 115200;
      com[FAN_PORT].data_bits = 8;
      com[FAN_PORT].parity = NO_PAR;
      com[FAN_PORT].stop_bits = 1;
      fan_port = FAN_PORT;
   }
   else if((port == TRACK_PORT) && (com[TRACK_PORT].user_set_baud == 0)) {
      com[THERMO_PORT].baud_rate = 9600;
      com[THERMO_PORT].data_bits = 8;
      com[THERMO_PORT].parity = NO_PAR;
      com[THERMO_PORT].stop_bits = 1;
      fan_port = THERMO_PORT;
   }
   else if((port == THERMO_PORT) && (com[THERMO_PORT].user_set_baud == 0)) {
      com[THERMO_PORT].baud_rate = 115200;
      com[THERMO_PORT].data_bits = 8;
      com[THERMO_PORT].parity = NO_PAR;
      com[THERMO_PORT].stop_bits = 1;
      fan_port = THERMO_PORT;
   }
   else if((port == TICC_PORT) && (com[TICC_PORT].user_set_baud == 0)) {
      if(ticc_type == PICPET_TICC) com[TICC_PORT].baud_rate = 19200;
      else if(ticc_type == TAPR_TICC) com[TICC_PORT].baud_rate = 115200;
      else if(ticc_type == LARS_TICC) com[TICC_PORT].baud_rate = 9600;
      else com[TICC_PORT].baud_rate = 115200;
      com[TICC_PORT].data_bits = 8;
      com[TICC_PORT].parity = NO_PAR;
      com[TICC_PORT].stop_bits = 1;
   }
   else if((port == TICC_ECHO_PORT) && (com[TICC_ECHO_PORT].user_set_baud == 0)) {
      com[TICC_ECHO_PORT].baud_rate = 115200;
      com[TICC_ECHO_PORT].data_bits = 8;
      com[TICC_ECHO_PORT].parity = NO_PAR;
      com[TICC_ECHO_PORT].stop_bits = 1;
   }

   if(port == THERMO_PORT) {
      config_enviro_plots(3);
   }

   if(strchr(out, ':')) {  // a ':' in the string means ars is a TCP/IP addr
//sprintf(debug_text, "otcpp%s", out);
      return config_tcpip_device(port, out);
   }
   else if(isdigit(c) && strchr(out, '.')) { // a dotted IP address
//sprintf(debug_text, "otcpd%s", out);
      return config_tcpip_device(port, out);
   }
   else if(c == '0') {  // toggle com port processing flag
      com[port].process_com = toggle_option(com[port].process_com, d); //lfs
com[port].process_com = 0;//lfs
com[port].port_used = (-1);//lfs
if(port == THERMO_PORT) {
   config_enviro_plots(10);
   BEEP(316);
}

      if(com[port].process_com == 0) {
         com[port].com_port = com[port].usb_port = 0;
         com[port].user_disabled_com = 1;
      }
      com[port].last_com_time = this_msec - com[port].com_timeout;
      return 0;
   }
   else if(isdigit(c)) {  // arg is a com/usb port number
//sprintf(debug_text, "oport%s", out);
      if(strstr(out, "P")) {
         lpt_port = atoi(&out[0]);  // parallel port used for temp control
         return 0;
      }
      else if(strstr(out, "T")) {
         lpt_port = atoi(&out[0]);  // parallel port used for temp control
         return 0;
      }
#ifdef WINDOWS
#else // __linux__  __MACH__  __FreeBSD__
      else if(strchr(out, 'U') || strchr(out, 'u')) {  // linux ttyUSB#
         com[port].usb_port = atoi(&out[0]);
         com[port].com_port = 0;
         com[port].user_disabled_com = 0;
      }
      else if(strchr(out, 'A') || strchr(out, 'a')) {  // linux ttyACM#
         sprintf(out, "/dev/ttyACM%d", atoi(&out[0])-1);
         config_input_device(port, out);
      }
#endif
      else {  // hardware serial port or Windows com port
         com[port].com_port = atoi(&out[0]);
         com[port].usb_port = 0;
         com[port].user_disabled_com = 0;
      }

      if(com[port].com_running) {
         com[port].process_com = 1;
         init_com(port, 16);
      }

      com[port].last_com_time = this_msec - com[port].com_timeout;
      return 0;
   }

// Assembly:
testq %rsi, %rsi
je 0x4d709
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x210, %rsp            # imm = 0x210
movl %edi, %ebx
movl $0x2, %eax
cmpl $0x12, %edi
ja 0x4d76b
cmpl $0xd, %ebx
jne 0x4d68c
leaq 0x1ef6ae(%rip), %rax    # 0x23cd34
movl $0x0, (%rax)
leaq -0x230(%rbp), %r14
movq %r14, %rdi
callq 0x9340
movsbl (%r14), %r12d
movq %r14, %rdi
movl $0x2c, %esi
callq 0x9100
testq %rax, %rax
je 0x4d6d3
movb $0x0, (%rax)
incq %rax
movl %ebx, %edi
movq %rax, %rsi
xorl %edx, %edx
callq 0x262e7
movl %eax, %ebx
movl $0x3, %eax
cmpl $0x12, %ebx
ja 0x4d76b
leaq 0x2c0c06(%rip), %r15    # 0x30e2e0
cmpl $0x11, %ebx
jne 0x4d70f
cmpl $0x0, 0x26ab8(%r15)
jne 0x4d70f
movapd 0xbf8af(%rip), %xmm0    # 0x10cfa0
movapd %xmm0, 0x24a70(%r15)
leaq 0x2c0bcb(%rip), %rax    # 0x30e2cc
movl $0x11, (%rax)
jmp 0x4d744
movl $0x1, %eax
retq
cmpl $0x12, %ebx
jne 0x4d7f3
cmpl $0x0, 0x28d18(%r15)
jne 0x4d7f3
movapd 0xbba92(%rip), %xmm0    # 0x1091c0
movapd %xmm0, 0x205b0(%r15)
leaq 0x2c0b8e(%rip), %rax    # 0x30e2cc
movl $0xf, (%rax)
xorl %r14d, %r14d
leaq -0x230(%rbp), %rdi
movl $0x3a, %esi
callq 0x9100
testq %rax, %rax
je 0x4d77b
leaq -0x230(%rbp), %rsi
movl %ebx, %edi
callq 0x4d4ee
addq $0x210, %rsp            # imm = 0x210
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
leal -0x30(%r12), %eax
cmpl $0x9, %eax
ja 0x4d83a
leaq -0x230(%rbp), %rdi
movl $0x2e, %esi
callq 0x9100
testq %rax, %rax
jne 0x4d75d
cmpb $0x30, %r12b
je 0x4d844
leaq -0x230(%rbp), %rdi
movl $0x50, %esi
callq 0x9100
testq %rax, %rax
jne 0x4d7d9
leaq -0x230(%rbp), %rdi
movl $0x54, %esi
callq 0x9100
testq %rax, %rax
je 0x4d964
leaq -0x230(%rbp), %rdi
callq 0x95e0
leaq 0x2e9830(%rip), %rcx    # 0x33701c
movl %eax, (%rcx)
jmp 0x4d8b7
cmpl $0xf, %ebx
jne 0x4d8be
cmpl $0x0, 0x225f8(%r15)
jne 0x4d8be
movapd 0xbf78e(%rip), %xmm0    # 0x10cfa0
movapd %xmm0, 0x205b0(%r15)
leaq 0x2c0aaa(%rip), %rax    # 0x30e2cc
movl $0xf, (%rax)
movl $0x3, %edi
callq 0x393f6
movb $0x1, %r14b
jmp 0x4d747
cmpb $0x30, %r12b
jne 0x4d8ea
movl %ebx, %eax
imulq $0x2260, %rax, %rax     # imm = 0x2260
leaq (%r15,%rax), %rbx
movl $0x0, 0x2234(%r15,%rax)
movl $0xffffffff, 0x222c(%r15,%rax) # imm = 0xFFFFFFFF
testb %r14b, %r14b
je 0x4d88b
movl $0xa, %edi
callq 0x393f6
movl $0x13c, %edi            # imm = 0x13C
callq 0x9d78
cmpl $0x0, 0x2234(%rbx)
jne 0x4d89c
movq $0x0, (%rbx)
movl $0x1, 0x2228(%rbx)
leaq 0x2bfec5(%rip), %rax    # 0x30d768
movsd (%rax), %xmm0
subsd 0x2238(%rbx), %xmm0
movsd %xmm0, 0x2248(%rbx)
xorl %eax, %eax
jmp 0x4d76b
cmpl $0xd, %ebx
jne 0x4d8fd
cmpl $0x0, 0x1e138(%r15)
jne 0x4d8fd
leaq 0x1cb034(%rip), %rax    # 0x218908
movl (%rax), %eax
cmpl $0x4c, %eax
je 0x4d937
cmpl $0x50, %eax
je 0x4d930
cmpl $0x54, %eax
movl $0x1c200, %eax          # imm = 0x1C200
jmp 0x4d93c
leaq -0x230(%rbp), %rsi
movl %ebx, %edi
callq 0x4d35c
jmp 0x4d76b
cmpl $0xe, %ebx
jne 0x4d922
cmpl $0x0, 0x20398(%r15)
jne 0x4d922
movapd 0xbf68c(%rip), %xmm0    # 0x10cfa0
movapd %xmm0, 0x1e350(%r15)
jmp 0x4d744
cmpl $0xf, %ebx
jne 0x4d744
jmp 0x4d828
movl $0x4b00, %eax           # imm = 0x4B00
jmp 0x4d93c
movl $0x2580, %eax           # imm = 0x2580
movl %eax, 0x1c0f0(%r15)
movabsq $0x100000008, %rax      # imm = 0x100000008
movq %rax, 0x1c0f4(%r15)
movl $0x0, 0x1c0fc(%r15)
jmp 0x4d744
leaq -0x230(%rbp), %rdi
movl $0x55, %esi
callq 0x9100
testq %rax, %rax
jne 0x4d990
leaq -0x230(%rbp), %rdi
movl $0x75, %esi
callq 0x9100
testq %rax, %rax
je 0x4da07
leaq -0x230(%rbp), %rdi
callq 0x95e0
movl %ebx, %ecx
imulq $0x2260, %rcx, %rdx     # imm = 0x2260
movl %eax, 0x4(%r15,%rdx)
xorl %eax, %eax
movl %eax, (%r15,%rdx)
movl %eax, 0x2228(%r15,%rdx)
imulq $0x2260, %rcx, %rax     # imm = 0x2260
leaq (%r15,%rax), %r14
cmpl $0x0, 0x2230(%r15,%rax)
je 0x4d9e5
movl $0x1, 0x2234(%r14)
movl %ebx, %edi
movl $0x10, %esi
callq 0xb49e
leaq 0x2bfd7c(%rip), %rax    # 0x30d768
movsd (%rax), %xmm0
subsd 0x2238(%r14), %xmm0
movsd %xmm0, 0x2248(%r14)
jmp 0x4d8b7
leaq -0x230(%rbp), %rdi
movl $0x41, %esi
callq 0x9100
testq %rax, %rax
jne 0x4da33
leaq -0x230(%rbp), %rdi
movl $0x61, %esi
callq 0x9100
testq %rax, %rax
je 0x4da67
leaq -0x230(%rbp), %r14
movq %r14, %rdi
callq 0x95e0
leal -0x1(%rax), %edx
leaq 0xd0653(%rip), %rsi     # 0x11e09f
movq %r14, %rdi
xorl %eax, %eax
callq 0x9060
movl %ebx, %edi
movq %r14, %rsi
callq 0x4d35c
movl %ebx, %ecx
jmp 0x4d9b8
leaq -0x230(%rbp), %rdi
callq 0x95e0
movl %ebx, %ecx
imulq $0x2260, %rcx, %rdx     # imm = 0x2260
movl %eax, (%r15,%rdx)
xorl %eax, %eax
movl %eax, 0x4(%r15,%rdx)
jmp 0x4d9b0