// File: 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:
pushq %rbp
movq %rsp, %rbp
subq $0x230, %rsp            # imm = 0x230
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
cmpq $0x0, -0x10(%rbp)
jne 0x6dae5
movl $0x1, -0x4(%rbp)
jmp 0x6e29a
cmpl $0x13, -0x8(%rbp)
jb 0x6daf7
movl $0x2, -0x4(%rbp)
jmp 0x6e29a
cmpl $0xd, -0x8(%rbp)
jne 0x6db0a
leaq 0x22e410(%rip), %rax    # 0x29bf14
movl $0x0, (%rax)
leaq -0x220(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x9350
movb $0x0, -0x12(%rbp)
movb $0x0, -0x11(%rbp)
movb -0x220(%rbp), %al
movb %al, -0x11(%rbp)
cmpb $0x0, -0x11(%rbp)
je 0x6db3a
movb -0x21f(%rbp), %al
movb %al, -0x12(%rbp)
leaq -0x220(%rbp), %rdi
movl $0x2c, %esi
callq 0x9100
movq %rax, -0x228(%rbp)
cmpq $0x0, -0x228(%rbp)
je 0x6db92
movq -0x228(%rbp), %rax
movb $0x0, (%rax)
movl -0x8(%rbp), %edi
movq -0x228(%rbp), %rsi
addq $0x1, %rsi
xorl %edx, %edx
callq 0x35ca0
movl %eax, -0x8(%rbp)
cmpl $0x13, -0x8(%rbp)
jb 0x6db90
movl $0x3, -0x4(%rbp)
jmp 0x6e29a
jmp 0x6db92
cmpl $0x11, -0x8(%rbp)
jne 0x6dbfe
leaq 0x2ff921(%rip), %rax    # 0x36d4c0
cmpl $0x0, 0x26ab8(%rax)
jne 0x6dbfe
leaq 0x2ff911(%rip), %rax    # 0x36d4c0
movl $0x1c200, 0x24a70(%rax) # imm = 0x1C200
leaq 0x2ff900(%rip), %rax    # 0x36d4c0
movl $0x8, 0x24a74(%rax)
leaq 0x2ff8ef(%rip), %rax    # 0x36d4c0
movl $0x0, 0x24a7c(%rax)
leaq 0x2ff8de(%rip), %rax    # 0x36d4c0
movl $0x1, 0x24a78(%rax)
leaq 0x2ff8b9(%rip), %rax    # 0x36d4ac
movl $0x11, (%rax)
jmp 0x6ddfd
cmpl $0x12, -0x8(%rbp)
jne 0x6dc6a
leaq 0x2ff8b5(%rip), %rax    # 0x36d4c0
cmpl $0x0, 0x28d18(%rax)
jne 0x6dc6a
leaq 0x2ff8a5(%rip), %rax    # 0x36d4c0
movl $0x2580, 0x205b0(%rax)  # imm = 0x2580
leaq 0x2ff894(%rip), %rax    # 0x36d4c0
movl $0x8, 0x205b4(%rax)
leaq 0x2ff883(%rip), %rax    # 0x36d4c0
movl $0x0, 0x205bc(%rax)
leaq 0x2ff872(%rip), %rax    # 0x36d4c0
movl $0x1, 0x205b8(%rax)
leaq 0x2ff84d(%rip), %rax    # 0x36d4ac
movl $0xf, (%rax)
jmp 0x6ddfb
cmpl $0xf, -0x8(%rbp)
jne 0x6dcd6
leaq 0x2ff849(%rip), %rax    # 0x36d4c0
cmpl $0x0, 0x225f8(%rax)
jne 0x6dcd6
leaq 0x2ff839(%rip), %rax    # 0x36d4c0
movl $0x1c200, 0x205b0(%rax) # imm = 0x1C200
leaq 0x2ff828(%rip), %rax    # 0x36d4c0
movl $0x8, 0x205b4(%rax)
leaq 0x2ff817(%rip), %rax    # 0x36d4c0
movl $0x0, 0x205bc(%rax)
leaq 0x2ff806(%rip), %rax    # 0x36d4c0
movl $0x1, 0x205b8(%rax)
leaq 0x2ff7e1(%rip), %rax    # 0x36d4ac
movl $0xf, (%rax)
jmp 0x6ddf9
cmpl $0xd, -0x8(%rbp)
jne 0x6dd9b
leaq 0x2ff7d9(%rip), %rax    # 0x36d4c0
cmpl $0x0, 0x1e138(%rax)
jne 0x6dd9b
leaq 0x209ded(%rip), %rax    # 0x277ae8
cmpl $0x50, (%rax)
jne 0x6dd13
leaq 0x2ff7b9(%rip), %rax    # 0x36d4c0
movl $0x4b00, 0x1c0f0(%rax)  # imm = 0x4B00
jmp 0x6dd66
leaq 0x209dce(%rip), %rax    # 0x277ae8
cmpl $0x54, (%rax)
jne 0x6dd32
leaq 0x2ff79a(%rip), %rax    # 0x36d4c0
movl $0x1c200, 0x1c0f0(%rax) # imm = 0x1C200
jmp 0x6dd64
leaq 0x209daf(%rip), %rax    # 0x277ae8
cmpl $0x4c, (%rax)
jne 0x6dd51
leaq 0x2ff77b(%rip), %rax    # 0x36d4c0
movl $0x2580, 0x1c0f0(%rax)  # imm = 0x2580
jmp 0x6dd62
leaq 0x2ff768(%rip), %rax    # 0x36d4c0
movl $0x1c200, 0x1c0f0(%rax) # imm = 0x1C200
jmp 0x6dd64
jmp 0x6dd66
leaq 0x2ff753(%rip), %rax    # 0x36d4c0
movl $0x8, 0x1c0f4(%rax)
leaq 0x2ff742(%rip), %rax    # 0x36d4c0
movl $0x0, 0x1c0fc(%rax)
leaq 0x2ff731(%rip), %rax    # 0x36d4c0
movl $0x1, 0x1c0f8(%rax)
jmp 0x6ddf7
cmpl $0xe, -0x8(%rbp)
jne 0x6ddf5
leaq 0x2ff718(%rip), %rax    # 0x36d4c0
cmpl $0x0, 0x20398(%rax)
jne 0x6ddf5
leaq 0x2ff708(%rip), %rax    # 0x36d4c0
movl $0x1c200, 0x1e350(%rax) # imm = 0x1C200
leaq 0x2ff6f7(%rip), %rax    # 0x36d4c0
movl $0x8, 0x1e354(%rax)
leaq 0x2ff6e6(%rip), %rax    # 0x36d4c0
movl $0x0, 0x1e35c(%rax)
leaq 0x2ff6d5(%rip), %rax    # 0x36d4c0
movl $0x1, 0x1e358(%rax)
jmp 0x6ddf7
jmp 0x6ddf9
jmp 0x6ddfb
jmp 0x6ddfd
cmpl $0xf, -0x8(%rbp)
jne 0x6de0d
movl $0x3, %edi
callq 0x50840
leaq -0x220(%rbp), %rdi
movl $0x3a, %esi
callq 0x9100
cmpq $0x0, %rax
je 0x6de3b
movl -0x8(%rbp), %edi
leaq -0x220(%rbp), %rsi
callq 0x6d660
movl %eax, -0x4(%rbp)
jmp 0x6e29a
movsbl -0x11(%rbp), %edi
callq 0x9330
cmpl $0x0, %eax
je 0x6de77
leaq -0x220(%rbp), %rdi
movl $0x2e, %esi
callq 0x9100
cmpq $0x0, %rax
je 0x6de77
movl -0x8(%rbp), %edi
leaq -0x220(%rbp), %rsi
callq 0x6d660
movl %eax, -0x4(%rbp)
jmp 0x6e29a
movsbl -0x11(%rbp), %eax
cmpl $0x30, %eax
jne 0x6dff0
movl -0x8(%rbp), %eax
movl %eax, %ecx
leaq 0x2ff630(%rip), %rax    # 0x36d4c0
imulq $0x2260, %rcx, %rcx     # imm = 0x2260
addq %rcx, %rax
movl 0x2234(%rax), %eax
movzbl %al, %edi
movzbl -0x12(%rbp), %esi
callq 0x638d0
movzbl %al, %ecx
movl -0x8(%rbp), %eax
movl %eax, %edx
leaq 0x2ff605(%rip), %rax    # 0x36d4c0
imulq $0x2260, %rdx, %rdx     # imm = 0x2260
addq %rdx, %rax
movl %ecx, 0x2234(%rax)
movl -0x8(%rbp), %eax
movl %eax, %ecx
leaq 0x2ff5e9(%rip), %rax    # 0x36d4c0
imulq $0x2260, %rcx, %rcx     # imm = 0x2260
addq %rcx, %rax
movl $0x0, 0x2234(%rax)
movl -0x8(%rbp), %eax
movl %eax, %ecx
leaq 0x2ff5c9(%rip), %rax    # 0x36d4c0
imulq $0x2260, %rcx, %rcx     # imm = 0x2260
addq %rcx, %rax
movl $0xffffffff, 0x222c(%rax) # imm = 0xFFFFFFFF
cmpl $0xf, -0x8(%rbp)
jne 0x6df25
movl $0xa, %edi
callq 0x50840
movl $0x13c, %edi            # imm = 0x13C
callq 0x9ec0
movl -0x8(%rbp), %eax
movl %eax, %ecx
leaq 0x2ff58f(%rip), %rax    # 0x36d4c0
imulq $0x2260, %rcx, %rcx     # imm = 0x2260
addq %rcx, %rax
cmpl $0x0, 0x2234(%rax)
jne 0x6df9d
movl -0x8(%rbp), %eax
movl %eax, %ecx
leaq 0x2ff570(%rip), %rax    # 0x36d4c0
imulq $0x2260, %rcx, %rcx     # imm = 0x2260
addq %rcx, %rax
movl $0x0, 0x4(%rax)
movl -0x8(%rbp), %eax
movl %eax, %ecx
leaq 0x2ff553(%rip), %rax    # 0x36d4c0
imulq $0x2260, %rcx, %rcx     # imm = 0x2260
addq %rcx, %rax
movl $0x0, (%rax)
movl -0x8(%rbp), %eax
movl %eax, %ecx
leaq 0x2ff537(%rip), %rax    # 0x36d4c0
imulq $0x2260, %rcx, %rcx     # imm = 0x2260
addq %rcx, %rax
movl $0x1, 0x2228(%rax)
leaq 0x2fe9a4(%rip), %rax    # 0x36c948
movsd (%rax), %xmm0
movl -0x8(%rbp), %eax
movl %eax, %ecx
leaq 0x2ff50c(%rip), %rax    # 0x36d4c0
imulq $0x2260, %rcx, %rcx     # imm = 0x2260
addq %rcx, %rax
subsd 0x2238(%rax), %xmm0
movl -0x8(%rbp), %eax
movl %eax, %ecx
leaq 0x2ff4ee(%rip), %rax    # 0x36d4c0
imulq $0x2260, %rcx, %rcx     # imm = 0x2260
addq %rcx, %rax
movsd %xmm0, 0x2248(%rax)
movl $0x0, -0x4(%rbp)
jmp 0x6e29a
movsbl -0x11(%rbp), %edi
callq 0x9330
cmpl $0x0, %eax
je 0x6e288
leaq -0x220(%rbp), %rdi
leaq 0x113bd8(%rip), %rsi    # 0x181be8
callq 0x9090
cmpq $0x0, %rax
je 0x6e03e
leaq -0x220(%rbp), %rdi
callq 0x95c0
movl %eax, %ecx
leaq 0x3281cc(%rip), %rax    # 0x3961fc
movl %ecx, (%rax)
movl $0x0, -0x4(%rbp)
jmp 0x6e29a
leaq -0x220(%rbp), %rdi
leaq 0x104002(%rip), %rsi    # 0x17204e
callq 0x9090
cmpq $0x0, %rax
je 0x6e07a
leaq -0x220(%rbp), %rdi
callq 0x95c0
movl %eax, %ecx
leaq 0x328190(%rip), %rax    # 0x3961fc
movl %ecx, (%rax)
movl $0x0, -0x4(%rbp)
jmp 0x6e29a
leaq -0x220(%rbp), %rdi
movl $0x55, %esi
callq 0x9100
cmpq $0x0, %rax
jne 0x6e0a8
leaq -0x220(%rbp), %rdi
movl $0x75, %esi
callq 0x9100
cmpq $0x0, %rax
je 0x6e110
leaq -0x220(%rbp), %rdi
callq 0x95c0
movl %eax, %ecx
movl -0x8(%rbp), %eax
movl %eax, %edx
leaq 0x2ff3fe(%rip), %rax    # 0x36d4c0
imulq $0x2260, %rdx, %rdx     # imm = 0x2260
addq %rdx, %rax
movl %ecx, 0x4(%rax)
movl -0x8(%rbp), %eax
movl %eax, %ecx
leaq 0x2ff3e5(%rip), %rax    # 0x36d4c0
imulq $0x2260, %rcx, %rcx     # imm = 0x2260
addq %rcx, %rax
movl $0x0, (%rax)
movl -0x8(%rbp), %eax
movl %eax, %ecx
leaq 0x2ff3c9(%rip), %rax    # 0x36d4c0
imulq $0x2260, %rcx, %rcx     # imm = 0x2260
addq %rcx, %rax
movl $0x0, 0x2228(%rax)
jmp 0x6e1e8
leaq -0x220(%rbp), %rdi
movl $0x41, %esi
callq 0x9100
cmpq $0x0, %rax
jne 0x6e13e
leaq -0x220(%rbp), %rdi
movl $0x61, %esi
callq 0x9100
cmpq $0x0, %rax
je 0x6e183
leaq -0x220(%rbp), %rax
movq %rax, -0x230(%rbp)
leaq -0x220(%rbp), %rdi
callq 0x95c0
movq -0x230(%rbp), %rdi
movl %eax, %edx
subl $0x1, %edx
leaq 0x11494f(%rip), %rsi    # 0x182aba
movb $0x0, %al
callq 0x9060
movl -0x8(%rbp), %edi
leaq -0x220(%rbp), %rsi
callq 0x6d250
jmp 0x6e1e6
leaq -0x220(%rbp), %rdi
callq 0x95c0
movl %eax, %ecx
movl -0x8(%rbp), %eax
movl %eax, %edx
leaq 0x2ff323(%rip), %rax    # 0x36d4c0
imulq $0x2260, %rdx, %rdx     # imm = 0x2260
addq %rdx, %rax
movl %ecx, (%rax)
movl -0x8(%rbp), %eax
movl %eax, %ecx
leaq 0x2ff30b(%rip), %rax    # 0x36d4c0
imulq $0x2260, %rcx, %rcx     # imm = 0x2260
addq %rcx, %rax
movl $0x0, 0x4(%rax)
movl -0x8(%rbp), %eax
movl %eax, %ecx
leaq 0x2ff2ee(%rip), %rax    # 0x36d4c0
imulq $0x2260, %rcx, %rcx     # imm = 0x2260
addq %rcx, %rax
movl $0x0, 0x2228(%rax)
jmp 0x6e1e8
jmp 0x6e1ea
jmp 0x6e1ec
movl -0x8(%rbp), %eax
movl %eax, %ecx
leaq 0x2ff2c8(%rip), %rax    # 0x36d4c0
imulq $0x2260, %rcx, %rcx     # imm = 0x2260
addq %rcx, %rax
cmpl $0x0, 0x2230(%rax)
je 0x6e238
movl -0x8(%rbp), %eax
movl %eax, %ecx
leaq 0x2ff2a9(%rip), %rax    # 0x36d4c0
imulq $0x2260, %rcx, %rcx     # imm = 0x2260
addq %rcx, %rax
movl $0x1, 0x2234(%rax)
movl -0x8(%rbp), %edi
movl $0x10, %esi
callq 0xc0e0
leaq 0x2fe709(%rip), %rax    # 0x36c948
movsd (%rax), %xmm0
movl -0x8(%rbp), %eax
movl %eax, %ecx
leaq 0x2ff271(%rip), %rax    # 0x36d4c0
imulq $0x2260, %rcx, %rcx     # imm = 0x2260
addq %rcx, %rax
subsd 0x2238(%rax), %xmm0
movl -0x8(%rbp), %eax
movl %eax, %ecx
leaq 0x2ff253(%rip), %rax    # 0x36d4c0
imulq $0x2260, %rcx, %rcx     # imm = 0x2260
addq %rcx, %rax
movsd %xmm0, 0x2248(%rax)
movl $0x0, -0x4(%rbp)
jmp 0x6e29a
movl -0x8(%rbp), %edi
leaq -0x220(%rbp), %rsi
callq 0x6d250
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x230, %rsp            # imm = 0x230
popq %rbp
retq
nopw %cs:(%rax,%rax)