// File: /workspace/github_cpp_makefile_star2_O1/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 0x4d74c
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 0x4d901
cmpl $0xd, %ebx
jne 0x4d6cf
leaq 0x1ef66b(%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 0x4d716
movb $0x0, (%rax)
incq %rax
movl %ebx, %edi
movq %rax, %rsi
xorl %edx, %edx
callq 0x265a5
movl %eax, %ebx
movl $0x3, %eax
cmpl $0x12, %ebx
ja 0x4d901
leaq 0x2c0bc3(%rip), %r15    # 0x30e2e0
cmpl $0x11, %ebx
jne 0x4d752
cmpl $0x0, 0x26ab8(%r15)
jne 0x4d752
movapd 0xbf81c(%rip), %xmm0    # 0x10cf50
movapd %xmm0, 0x24a70(%r15)
leaq 0x2c0b88(%rip), %rax    # 0x30e2cc
movl $0x11, (%rax)
jmp 0x4d7a0
movl $0x1, %eax
retq
cmpl $0x12, %ebx
jne 0x4d76b
cmpl $0x0, 0x28d18(%r15)
jne 0x4d76b
movapd 0xbba47(%rip), %xmm0    # 0x1091b0
jmp 0x4d78a
cmpl $0xf, %ebx
jne 0x4d8c7
cmpl $0x0, 0x225f8(%r15)
jne 0x4d8c7
movapd 0xbf7c6(%rip), %xmm0    # 0x10cf50
movapd %xmm0, 0x205b0(%r15)
leaq 0x2c0b32(%rip), %rax    # 0x30e2cc
movl $0xf, (%rax)
cmpl $0xf, %ebx
jne 0x4d7af
movl $0x3, %edi
callq 0x39326
leaq -0x230(%rbp), %rdi
movl $0x3a, %esi
callq 0x9100
testq %rax, %rax
je 0x4d7d8
leaq -0x230(%rbp), %rsi
movl %ebx, %edi
callq 0x4d531
jmp 0x4d901
leal -0x30(%r12), %r14d
cmpl $0x9, %r14d
ja 0x4d7f9
leaq -0x230(%rbp), %rdi
movl $0x2e, %esi
callq 0x9100
testq %rax, %rax
jne 0x4d7c5
cmpb $0x30, %r12b
jne 0x4d878
movl %ebx, %eax
imulq $0x2260, %rax, %rax     # imm = 0x2260
leaq (%r15,%rax), %r14
movl $0x0, 0x2234(%r15,%rax)
movl $0xffffffff, 0x222c(%r15,%rax) # imm = 0xFFFFFFFF
cmpl $0xf, %ebx
jne 0x4d83d
movl $0xa, %edi
callq 0x39326
movl $0x13c, %edi            # imm = 0x13C
callq 0x9d80
cmpl $0x0, 0x2234(%r14)
jne 0x4d859
movq $0x0, (%r14)
movl $0x1, 0x2228(%r14)
leaq 0x2bff08(%rip), %rax    # 0x30d768
movsd (%rax), %xmm0
subsd 0x2238(%r14), %xmm0
movsd %xmm0, 0x2248(%r14)
jmp 0x4d8c3
cmpl $0x9, %r14d
ja 0x4d8f3
leaq -0x230(%rbp), %rdi
movl $0x50, %esi
callq 0x9100
testq %rax, %rax
jne 0x4d8ae
leaq -0x230(%rbp), %rdi
movl $0x54, %esi
callq 0x9100
testq %rax, %rax
je 0x4d972
leaq -0x230(%rbp), %rdi
callq 0x95e0
leaq 0x2e975b(%rip), %rcx    # 0x33701c
movl %eax, (%rcx)
xorl %eax, %eax
jmp 0x4d901
cmpl $0xd, %ebx
jne 0x4d911
cmpl $0x0, 0x1e138(%r15)
jne 0x4d911
leaq 0x1cb02b(%rip), %rax    # 0x218908
movl (%rax), %eax
cmpl $0x4c, %eax
je 0x4d945
cmpl $0x50, %eax
je 0x4d93e
cmpl $0x54, %eax
movl $0x1c200, %eax          # imm = 0x1C200
jmp 0x4d94a
leaq -0x230(%rbp), %rsi
movl %ebx, %edi
callq 0x4d39f
addq $0x210, %rsp            # imm = 0x210
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
cmpl $0xe, %ebx
jne 0x4d7a0
cmpl $0x0, 0x20398(%r15)
jne 0x4d7a0
movapd 0xbf620(%rip), %xmm0    # 0x10cf50
movapd %xmm0, 0x1e350(%r15)
jmp 0x4d7a0
movl $0x4b00, %eax           # imm = 0x4B00
jmp 0x4d94a
movl $0x2580, %eax           # imm = 0x2580
movl %eax, 0x1c0f0(%r15)
movabsq $0x100000008, %rax      # imm = 0x100000008
movq %rax, 0x1c0f4(%r15)
movl $0x0, 0x1c0fc(%r15)
jmp 0x4d7a0
leaq -0x230(%rbp), %rdi
movl $0x55, %esi
callq 0x9100
movl %ebx, %r12d
testq %rax, %rax
jne 0x4d9a1
leaq -0x230(%rbp), %rdi
movl $0x75, %esi
callq 0x9100
testq %rax, %rax
je 0x4d9fd
leaq -0x230(%rbp), %rdi
callq 0x95e0
imulq $0x2260, %r12, %rcx     # imm = 0x2260
movl %eax, 0x4(%r15,%rcx)
xorl %eax, %eax
movl %eax, (%r15,%rcx)
movl %eax, 0x2228(%r15,%rcx)
imulq $0x2260, %r12, %rax     # imm = 0x2260
leaq (%r15,%rax), %r14
cmpl $0x0, 0x2230(%r15,%rax)
je 0x4d859
movl $0x1, 0x2234(%r14)
movl %ebx, %edi
movl $0x10, %esi
callq 0xb49f
jmp 0x4d859
leaq -0x230(%rbp), %rdi
movl $0x41, %esi
callq 0x9100
testq %rax, %rax
jne 0x4da29
leaq -0x230(%rbp), %rdi
movl $0x61, %esi
callq 0x9100
testq %rax, %rax
je 0x4da5b
leaq -0x230(%rbp), %r14
movq %r14, %rdi
callq 0x95e0
leal -0x1(%rax), %edx
leaq 0xd05d2(%rip), %rsi     # 0x11e014
movq %r14, %rdi
xorl %eax, %eax
callq 0x9060
movl %ebx, %edi
movq %r14, %rsi
callq 0x4d39f
jmp 0x4d9c7
leaq -0x230(%rbp), %rdi
callq 0x95e0
imulq $0x2260, %r12, %rcx     # imm = 0x2260
movl %eax, (%r15,%rcx)
xorl %eax, %eax
movl %eax, 0x4(%r15,%rcx)
jmp 0x4d9bf