// File: /workspace/github_cpp_makefile_star2_O2/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 0x493ef
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x210, %rsp            # imm = 0x210
movl %edi, %ebx
cmpl $0x12, %edi
jbe 0x493f3
pushq $0x2
jmp 0x49440
pushq $0x1
popq %rax
retq
cmpl $0xd, %ebx
jne 0x49402
leaq 0x1dd935(%rip), %rax    # 0x226d34
andl $0x0, (%rax)
leaq -0x230(%rbp), %r14
movq %r14, %rdi
callq 0x9330
movsbl (%r14), %r12d
pushq $0x2c
popq %rsi
movq %r14, %rdi
callq 0x9100
testq %rax, %rax
je 0x49446
movb $0x0, (%rax)
incq %rax
movl %ebx, %edi
movq %rax, %rsi
xorl %edx, %edx
callq 0x246b8
movl %eax, %ebx
cmpl $0x12, %eax
jbe 0x49446
pushq $0x3
popq %rax
jmp 0x494d6
leaq 0x2aee93(%rip), %r15    # 0x2f82e0
cmpl $0x11, %ebx
jne 0x4947c
cmpl $0x0, 0x26ab8(%r15)
jne 0x4947c
movapd 0xadbbc(%rip), %xmm0    # 0xf7020
movapd %xmm0, 0x24a70(%r15)
leaq 0x2aee58(%rip), %rax    # 0x2f82cc
movl $0x11, (%rax)
jmp 0x494b1
cmpl $0x12, %ebx
jne 0x49558
cmpl $0x0, 0x28d18(%r15)
jne 0x49558
movapd 0xa9d25(%rip), %xmm0    # 0xf31c0
movapd %xmm0, 0x205b0(%r15)
leaq 0x2aee21(%rip), %rax    # 0x2f82cc
movl $0xf, (%rax)
xorl %r14d, %r14d
leaq -0x230(%rbp), %rdi
pushq $0x3a
popq %rsi
callq 0x9100
testq %rax, %rax
je 0x494e6
leaq -0x230(%rbp), %rsi
movl %ebx, %edi
callq 0x4927d
addq $0x210, %rsp            # imm = 0x210
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
leal -0x30(%r12), %eax
cmpl $0x9, %eax
ja 0x4959d
leaq -0x230(%rbp), %rdi
pushq $0x2e
popq %rsi
callq 0x9100
testq %rax, %rax
jne 0x494c8
cmpb $0x30, %r12b
je 0x495a7
leaq -0x230(%rbp), %rdi
pushq $0x50
popq %rsi
callq 0x9100
testq %rax, %rax
jne 0x4953e
leaq -0x230(%rbp), %rdi
pushq $0x54
popq %rsi
callq 0x9100
testq %rax, %rax
je 0x496b9
leaq -0x230(%rbp), %rdi
callq 0x95e0
leaq 0x2d7acb(%rip), %rcx    # 0x32101c
movl %eax, (%rcx)
jmp 0x4960f
cmpl $0xf, %ebx
jne 0x49616
cmpl $0x0, 0x225f8(%r15)
jne 0x49616
movapd 0xadaa9(%rip), %xmm0    # 0xf7020
movapd %xmm0, 0x205b0(%r15)
leaq 0x2aed45(%rip), %rax    # 0x2f82cc
movl $0xf, (%rax)
pushq $0x3
popq %rdi
callq 0x362c5
movb $0x1, %r14b
jmp 0x494b4
cmpb $0x30, %r12b
jne 0x49642
movl %ebx, %eax
imulq $0x2260, %rax, %rax     # imm = 0x2260
leaq (%r15,%rax), %rbx
andl $0x0, 0x2234(%r15,%rax)
orl $-0x1, 0x222c(%r15,%rax)
testb %r14b, %r14b
je 0x495e6
pushq $0xa
popq %rdi
callq 0x362c5
movl $0x13c, %edi            # imm = 0x13C
callq 0x9d23
cmpl $0x0, 0x2234(%rbx)
jne 0x495f4
andq $0x0, (%rbx)
movl $0x1, 0x2228(%rbx)
leaq 0x2ae16d(%rip), %rax    # 0x2f7768
movsd (%rax), %xmm0
subsd 0x2238(%rbx), %xmm0
movsd %xmm0, 0x2248(%rbx)
xorl %eax, %eax
jmp 0x494d6
cmpl $0xd, %ebx
jne 0x49655
cmpl $0x0, 0x1e138(%r15)
jne 0x49655
leaq 0x1b92dc(%rip), %rax    # 0x202908
movl (%rax), %eax
cmpl $0x4c, %eax
je 0x4968f
cmpl $0x50, %eax
je 0x49688
cmpl $0x54, %eax
movl $0x1c200, %eax          # imm = 0x1C200
jmp 0x49694
leaq -0x230(%rbp), %rsi
movl %ebx, %edi
callq 0x49101
jmp 0x494d6
cmpl $0xe, %ebx
jne 0x4967a
cmpl $0x0, 0x20398(%r15)
jne 0x4967a
movapd 0xad9b4(%rip), %xmm0    # 0xf7020
movapd %xmm0, 0x1e350(%r15)
jmp 0x494b1
cmpl $0xf, %ebx
jne 0x494b1
jmp 0x4958d
movl $0x4b00, %eax           # imm = 0x4B00
jmp 0x49694
movl $0x2580, %eax           # imm = 0x2580
movl %eax, 0x1c0f0(%r15)
movabsq $0x100000008, %rax      # imm = 0x100000008
movq %rax, 0x1c0f4(%r15)
andl $0x0, 0x1c0fc(%r15)
jmp 0x494b1
leaq -0x230(%rbp), %rdi
pushq $0x55
popq %rsi
callq 0x9100
testq %rax, %rax
jne 0x496e1
leaq -0x230(%rbp), %rdi
pushq $0x75
popq %rsi
callq 0x9100
testq %rax, %rax
je 0x49756
leaq -0x230(%rbp), %rdi
callq 0x95e0
movl %ebx, %ecx
imulq $0x2260, %rcx, %rdx     # imm = 0x2260
movl %eax, 0x4(%r15,%rdx)
andl $0x0, (%r15,%rdx)
andl $0x0, 0x2228(%r15,%rdx)
imulq $0x2260, %rcx, %rax     # imm = 0x2260
leaq (%r15,%rax), %r14
cmpl $0x0, 0x2230(%r15,%rax)
je 0x49734
movl $0x1, 0x2234(%r14)
pushq $0x10
popq %rsi
movl %ebx, %edi
callq 0xb30a
leaq 0x2ae02d(%rip), %rax    # 0x2f7768
movsd (%rax), %xmm0
subsd 0x2238(%r14), %xmm0
movsd %xmm0, 0x2248(%r14)
jmp 0x4960f
leaq -0x230(%rbp), %rdi
pushq $0x41
popq %rsi
callq 0x9100
testq %rax, %rax
jne 0x4977e
leaq -0x230(%rbp), %rdi
pushq $0x61
popq %rsi
callq 0x9100
testq %rax, %rax
je 0x497b2
leaq -0x230(%rbp), %r14
movq %r14, %rdi
callq 0x95e0
leal -0x1(%rax), %edx
leaq 0xbe983(%rip), %rsi     # 0x10811a
movq %r14, %rdi
xorl %eax, %eax
callq 0x9060
movl %ebx, %edi
movq %r14, %rsi
callq 0x49101
movl %ebx, %ecx
jmp 0x49709
leaq -0x230(%rbp), %rdi
callq 0x95e0
movl %ebx, %ecx
imulq $0x2260, %rcx, %rdx     # imm = 0x2260
movl %eax, (%r15,%rdx)
andl $0x0, 0x4(%r15,%rdx)
jmp 0x49700