// File: jkingan[P]ladyheather/heathui.cpp
// Function: set_serial_params(unsigned int, char*, int)
int set_serial_params(unsigned port, char *p, int open_port)
{
char *s;
int baud;
int data_bits;
int stop_bits;
int parity;

   // Set serial port parameters.
   // p is serial port parameter string
   // port is default port to config if no :P param seen in p string
   // if open_port is non-zero, com port is opened
   //
   // Returns port that was configed or negative number if error detected.
   //
   // If a serial port parameter is not specified it is not changed.

   if(p == 0) return (-1);  // null param string

   s = strstr(p, ":p");   // check for :P0 .. :P9 for forced port number
   if(s == 0) s = strstr(p, ":P");
   if(s == 0) s = strstr(p, ",p");
   if(s == 0) s = strstr(p, ",P");

   if(s && s[2]) {  // :Px value seen, override port arg
      if(isdigit(s[2])) port = (s[2]-'0');
      if     (toupper(s[2] == 'D')) port = DAC_PORT;       // :PD - external DAC port
      else if(toupper(s[2] == 'E')) port = ECHO_PORT;      // :PE - echo port
      else if(toupper(s[2] == 'F')) port = FAN_PORT;       // :PF - temperature control device port
      else if(toupper(s[2] == 'I')) port = TICC_PORT;      // :PI - TICC port
      else if(toupper(s[2] == 'K')) port = NMEA_PORT;      // :PK - echo in NMEA format port
      else if(toupper(s[2] == 'N')) port = THERMO_PORT;    // :PN - environmental sensor (thermometer) port
      else if(toupper(s[2] == 'R')) port = RCVR_PORT;      // :PR - receiver data port
      else if(toupper(s[2] == 'T')) port = TRACK_PORT;     // :PT - moon/sun position
   }

   if(port >= NUM_COM_PORTS) return (-2);  // port number out of range

   s = p;
   s = trim_whitespace(s);
   if(s == 0) return (-3);
   if(s[0] == 0) return (-3);  // null param string

   baud = com[port].baud_rate;
   data_bits = com[port].data_bits;
   stop_bits = com[port].stop_bits;
   parity = com[port].parity;

   if(isdigit(*s)) {
      baud = atoi(s);
//    if(baud >= 150) com[port].baud_rate = baud;
   }

   if     (strstr(s, ":8")) data_bits = 8;
   else if(strstr(s, ",8")) data_bits = 8;
   else if(strstr(s, ":7")) data_bits = 7;
   else if(strstr(s, ",7")) data_bits = 7;
   else if(strstr(s, ":6")) data_bits = 6;
   else if(strstr(s, ",6")) data_bits = 6;
   else if(strstr(s, ":5")) data_bits = 5;
   else if(strstr(s, ",5")) data_bits = 5;
// else                     data_bits = 8;

   if     (strstr(s, ":1")) stop_bits = 1;
   else if(strstr(s, ",1")) stop_bits = 1;
   else if(strstr(s, ":2")) stop_bits = 2;
   else if(strstr(s, ",2")) stop_bits = 2;
// else                     stop_bits = 1;

   if     (strstr(s, ":N")) parity = NO_PAR;
   else if(strstr(s, ":n")) parity = NO_PAR;
   else if(strstr(s, ":O")) parity = ODD_PAR;
   else if(strstr(s, ":0")) parity = ODD_PAR;
   else if(strstr(s, ":o")) parity = ODD_PAR;
   else if(strstr(s, ":E")) parity = EVEN_PAR;
   else if(strstr(s, ":e")) parity = EVEN_PAR;
   else if(strstr(s, ",N")) parity = NO_PAR;
   else if(strstr(s, ",n")) parity = NO_PAR;
   else if(strstr(s, ",O")) parity = ODD_PAR;
   else if(strstr(s, ",0")) parity = ODD_PAR;
   else if(strstr(s, ",o")) parity = ODD_PAR;
   else if(strstr(s, ",E")) parity = EVEN_PAR;
   else if(strstr(s, ",e")) parity = EVEN_PAR;
// else                     parity = NO_PAR;

   if(open_port == 2) {
      set_rcvr_baud(baud, data_bits, parity, stop_bits);
   }
   else {
      com[port].user_set_baud = 1;  //ppppp
      com[port].baud_rate = baud;
      com[port].data_bits = data_bits;
      com[port].parity = parity;
      com[port].stop_bits = stop_bits;
      if(open_port) init_com(port, 10);
   }
   return port;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
cmpq $0x0, -0x10(%rbp)
jne 0x35cc5
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x363e3
movq -0x10(%rbp), %rdi
leaq 0x13c730(%rip), %rsi    # 0x172400
callq 0x9090
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
jne 0x35cf4
movq -0x10(%rbp), %rdi
leaq 0x13c718(%rip), %rsi    # 0x172403
callq 0x9090
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
jne 0x35d0f
movq -0x10(%rbp), %rdi
leaq 0x13c700(%rip), %rsi    # 0x172406
callq 0x9090
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
jne 0x35d2a
movq -0x10(%rbp), %rdi
leaq 0x14ef86(%rip), %rsi    # 0x184ca7
callq 0x9090
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
je 0x35ead
movq -0x20(%rbp), %rax
cmpb $0x0, 0x2(%rax)
je 0x35ead
movq -0x20(%rbp), %rax
movsbl 0x2(%rax), %edi
callq 0x9330
cmpl $0x0, %eax
je 0x35d63
movq -0x20(%rbp), %rax
movsbl 0x2(%rax), %eax
subl $0x30, %eax
movl %eax, -0x8(%rbp)
movq -0x20(%rbp), %rax
movsbl 0x2(%rax), %eax
cmpl $0x44, %eax
sete %al
andb $0x1, %al
movzbl %al, %edi
callq 0x96e0
cmpl $0x0, %eax
je 0x35d8c
movl $0x10, -0x8(%rbp)
jmp 0x35eab
movq -0x20(%rbp), %rax
movsbl 0x2(%rax), %eax
cmpl $0x45, %eax
sete %al
andb $0x1, %al
movzbl %al, %edi
callq 0x96e0
cmpl $0x0, %eax
je 0x35db5
movl $0xb, -0x8(%rbp)
jmp 0x35ea9
movq -0x20(%rbp), %rax
movsbl 0x2(%rax), %eax
cmpl $0x46, %eax
sete %al
andb $0x1, %al
movzbl %al, %edi
callq 0x96e0
cmpl $0x0, %eax
je 0x35dde
movl $0x11, -0x8(%rbp)
jmp 0x35ea7
movq -0x20(%rbp), %rax
movsbl 0x2(%rax), %eax
cmpl $0x49, %eax
sete %al
andb $0x1, %al
movzbl %al, %edi
callq 0x96e0
cmpl $0x0, %eax
je 0x35e07
movl $0xd, -0x8(%rbp)
jmp 0x35ea5
movq -0x20(%rbp), %rax
movsbl 0x2(%rax), %eax
cmpl $0x4b, %eax
sete %al
andb $0x1, %al
movzbl %al, %edi
callq 0x96e0
cmpl $0x0, %eax
je 0x35e2d
movl $0xc, -0x8(%rbp)
jmp 0x35ea3
movq -0x20(%rbp), %rax
movsbl 0x2(%rax), %eax
cmpl $0x4e, %eax
sete %al
andb $0x1, %al
movzbl %al, %edi
callq 0x96e0
cmpl $0x0, %eax
je 0x35e53
movl $0xf, -0x8(%rbp)
jmp 0x35ea1
movq -0x20(%rbp), %rax
movsbl 0x2(%rax), %eax
cmpl $0x52, %eax
sete %al
andb $0x1, %al
movzbl %al, %edi
callq 0x96e0
cmpl $0x0, %eax
je 0x35e79
movl $0xa, -0x8(%rbp)
jmp 0x35e9f
movq -0x20(%rbp), %rax
movsbl 0x2(%rax), %eax
cmpl $0x54, %eax
sete %al
andb $0x1, %al
movzbl %al, %edi
callq 0x96e0
cmpl $0x0, %eax
je 0x35e9d
movl $0x12, -0x8(%rbp)
jmp 0x35e9f
jmp 0x35ea1
jmp 0x35ea3
jmp 0x35ea5
jmp 0x35ea7
jmp 0x35ea9
jmp 0x35eab
jmp 0x35ead
cmpl $0x13, -0x8(%rbp)
jb 0x35ebf
movl $0xfffffffe, -0x4(%rbp) # imm = 0xFFFFFFFE
jmp 0x363e3
movq -0x10(%rbp), %rax
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rdi
callq 0xa150
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
jne 0x35ee7
movl $0xfffffffd, -0x4(%rbp) # imm = 0xFFFFFFFD
jmp 0x363e3
movq -0x20(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x0, %eax
jne 0x35eff
movl $0xfffffffd, -0x4(%rbp) # imm = 0xFFFFFFFD
jmp 0x363e3
movl -0x8(%rbp), %eax
movl %eax, %ecx
leaq 0x3375b5(%rip), %rax    # 0x36d4c0
imulq $0x2260, %rcx, %rcx     # imm = 0x2260
addq %rcx, %rax
movl 0x210(%rax), %eax
movl %eax, -0x24(%rbp)
movl -0x8(%rbp), %eax
movl %eax, %ecx
leaq 0x337596(%rip), %rax    # 0x36d4c0
imulq $0x2260, %rcx, %rcx     # imm = 0x2260
addq %rcx, %rax
movl 0x214(%rax), %eax
movl %eax, -0x28(%rbp)
movl -0x8(%rbp), %eax
movl %eax, %ecx
leaq 0x337577(%rip), %rax    # 0x36d4c0
imulq $0x2260, %rcx, %rcx     # imm = 0x2260
addq %rcx, %rax
movl 0x218(%rax), %eax
movl %eax, -0x2c(%rbp)
movl -0x8(%rbp), %eax
movl %eax, %ecx
leaq 0x337558(%rip), %rax    # 0x36d4c0
imulq $0x2260, %rcx, %rcx     # imm = 0x2260
addq %rcx, %rax
movl 0x21c(%rax), %eax
movl %eax, -0x30(%rbp)
movq -0x20(%rbp), %rax
movsbl (%rax), %edi
callq 0x9330
cmpl $0x0, %eax
je 0x35f98
movq -0x20(%rbp), %rdi
callq 0x95c0
movl %eax, -0x24(%rbp)
movq -0x20(%rbp), %rdi
leaq 0x13c466(%rip), %rsi    # 0x172409
callq 0x9090
cmpq $0x0, %rax
je 0x35fba
movl $0x8, -0x28(%rbp)
jmp 0x360a8
movq -0x20(%rbp), %rdi
leaq 0x14e3f5(%rip), %rsi    # 0x1843ba
callq 0x9090
cmpq $0x0, %rax
je 0x35fdc
movl $0x8, -0x28(%rbp)
jmp 0x360a6
movq -0x20(%rbp), %rdi
leaq 0x13c425(%rip), %rsi    # 0x17240c
callq 0x9090
cmpq $0x0, %rax
je 0x35ffe
movl $0x7, -0x28(%rbp)
jmp 0x360a4
movq -0x20(%rbp), %rdi
leaq 0x14e3ab(%rip), %rsi    # 0x1843b4
callq 0x9090
cmpq $0x0, %rax
je 0x36020
movl $0x7, -0x28(%rbp)
jmp 0x360a2
movq -0x20(%rbp), %rdi
leaq 0x13c3e4(%rip), %rsi    # 0x17240f
callq 0x9090
cmpq $0x0, %rax
je 0x3603f
movl $0x6, -0x28(%rbp)
jmp 0x360a0
movq -0x20(%rbp), %rdi
leaq 0x13c3c8(%rip), %rsi    # 0x172412
callq 0x9090
cmpq $0x0, %rax
je 0x3605e
movl $0x6, -0x28(%rbp)
jmp 0x3609e
movq -0x20(%rbp), %rdi
leaq 0x13c3ac(%rip), %rsi    # 0x172415
callq 0x9090
cmpq $0x0, %rax
je 0x3607d
movl $0x5, -0x28(%rbp)
jmp 0x3609c
movq -0x20(%rbp), %rdi
leaq 0x13c390(%rip), %rsi    # 0x172418
callq 0x9090
cmpq $0x0, %rax
je 0x3609a
movl $0x5, -0x28(%rbp)
jmp 0x3609c
jmp 0x3609e
jmp 0x360a0
jmp 0x360a2
jmp 0x360a4
jmp 0x360a6
jmp 0x360a8
movq -0x20(%rbp), %rdi
leaq 0x13c368(%rip), %rsi    # 0x17241b
callq 0x9090
cmpq $0x0, %rax
je 0x360c7
movl $0x1, -0x2c(%rbp)
jmp 0x36128
movq -0x20(%rbp), %rdi
leaq 0x14e106(%rip), %rsi    # 0x1841d8
callq 0x9090
cmpq $0x0, %rax
je 0x360e6
movl $0x1, -0x2c(%rbp)
jmp 0x36126
movq -0x20(%rbp), %rdi
leaq 0x13c32d(%rip), %rsi    # 0x17241e
callq 0x9090
cmpq $0x0, %rax
je 0x36105
movl $0x2, -0x2c(%rbp)
jmp 0x36124
movq -0x20(%rbp), %rdi
leaq 0x160606(%rip), %rsi    # 0x196716
callq 0x9090
cmpq $0x0, %rax
je 0x36122
movl $0x2, -0x2c(%rbp)
jmp 0x36124
jmp 0x36126
jmp 0x36128
movq -0x20(%rbp), %rdi
leaq 0x13c2ee(%rip), %rsi    # 0x172421
callq 0x9090
cmpq $0x0, %rax
je 0x3614a
movl $0x0, -0x30(%rbp)
jmp 0x36310
movq -0x20(%rbp), %rdi
leaq 0x13c2cf(%rip), %rsi    # 0x172424
callq 0x9090
cmpq $0x0, %rax
je 0x3616c
movl $0x0, -0x30(%rbp)
jmp 0x3630e
movq -0x20(%rbp), %rdi
leaq 0x13c2b0(%rip), %rsi    # 0x172427
callq 0x9090
cmpq $0x0, %rax
je 0x3618e
movl $0x1, -0x30(%rbp)
jmp 0x3630c
movq -0x20(%rbp), %rdi
leaq 0x13c291(%rip), %rsi    # 0x17242a
callq 0x9090
cmpq $0x0, %rax
je 0x361b0
movl $0x1, -0x30(%rbp)
jmp 0x3630a
movq -0x20(%rbp), %rdi
leaq 0x13c272(%rip), %rsi    # 0x17242d
callq 0x9090
cmpq $0x0, %rax
je 0x361d2
movl $0x1, -0x30(%rbp)
jmp 0x36308
movq -0x20(%rbp), %rdi
leaq 0x13c253(%rip), %rsi    # 0x172430
callq 0x9090
cmpq $0x0, %rax
je 0x361f4
movl $0x2, -0x30(%rbp)
jmp 0x36306
movq -0x20(%rbp), %rdi
leaq 0x13c234(%rip), %rsi    # 0x172433
callq 0x9090
cmpq $0x0, %rax
je 0x36216
movl $0x2, -0x30(%rbp)
jmp 0x36304
movq -0x20(%rbp), %rdi
leaq 0x14da6a(%rip), %rsi    # 0x183c8b
callq 0x9090
cmpq $0x0, %rax
je 0x36238
movl $0x0, -0x30(%rbp)
jmp 0x36302
movq -0x20(%rbp), %rdi
leaq 0x13c1f3(%rip), %rsi    # 0x172436
callq 0x9090
cmpq $0x0, %rax
je 0x3625a
movl $0x0, -0x30(%rbp)
jmp 0x36300
movq -0x20(%rbp), %rdi
leaq 0x13c1d4(%rip), %rsi    # 0x172439
callq 0x9090
cmpq $0x0, %rax
je 0x3627c
movl $0x1, -0x30(%rbp)
jmp 0x362fe
movq -0x20(%rbp), %rdi
leaq 0x14de94(%rip), %rsi    # 0x18411b
callq 0x9090
cmpq $0x0, %rax
je 0x3629b
movl $0x1, -0x30(%rbp)
jmp 0x362fc
movq -0x20(%rbp), %rdi
leaq 0x13c196(%rip), %rsi    # 0x17243c
callq 0x9090
cmpq $0x0, %rax
je 0x362ba
movl $0x1, -0x30(%rbp)
jmp 0x362fa
movq -0x20(%rbp), %rdi
leaq 0x13c17a(%rip), %rsi    # 0x17243f
callq 0x9090
cmpq $0x0, %rax
je 0x362d9
movl $0x2, -0x30(%rbp)
jmp 0x362f8
movq -0x20(%rbp), %rdi
leaq 0x13c15e(%rip), %rsi    # 0x172442
callq 0x9090
cmpq $0x0, %rax
je 0x362f6
movl $0x2, -0x30(%rbp)
jmp 0x362f8
jmp 0x362fa
jmp 0x362fc
jmp 0x362fe
jmp 0x36300
jmp 0x36302
jmp 0x36304
jmp 0x36306
jmp 0x36308
jmp 0x3630a
jmp 0x3630c
jmp 0x3630e
jmp 0x36310
cmpl $0x2, -0x14(%rbp)
jne 0x3632c
movl -0x24(%rbp), %edi
movl -0x28(%rbp), %esi
movl -0x30(%rbp), %edx
movl -0x2c(%rbp), %ecx
callq 0x83b40
jmp 0x363dd
movl -0x8(%rbp), %eax
movl %eax, %ecx
leaq 0x337188(%rip), %rax    # 0x36d4c0
imulq $0x2260, %rcx, %rcx     # imm = 0x2260
addq %rcx, %rax
movl $0x1, 0x2258(%rax)
movl -0x24(%rbp), %ecx
movl -0x8(%rbp), %eax
movl %eax, %edx
leaq 0x337165(%rip), %rax    # 0x36d4c0
imulq $0x2260, %rdx, %rdx     # imm = 0x2260
addq %rdx, %rax
movl %ecx, 0x210(%rax)
movl -0x28(%rbp), %ecx
movl -0x8(%rbp), %eax
movl %eax, %edx
leaq 0x337146(%rip), %rax    # 0x36d4c0
imulq $0x2260, %rdx, %rdx     # imm = 0x2260
addq %rdx, %rax
movl %ecx, 0x214(%rax)
movl -0x30(%rbp), %ecx
movl -0x8(%rbp), %eax
movl %eax, %edx
leaq 0x337127(%rip), %rax    # 0x36d4c0
imulq $0x2260, %rdx, %rdx     # imm = 0x2260
addq %rdx, %rax
movl %ecx, 0x21c(%rax)
movl -0x2c(%rbp), %ecx
movl -0x8(%rbp), %eax
movl %eax, %edx
leaq 0x337108(%rip), %rax    # 0x36d4c0
imulq $0x2260, %rdx, %rdx     # imm = 0x2260
addq %rdx, %rax
movl %ecx, 0x218(%rax)
cmpl $0x0, -0x14(%rbp)
je 0x363db
movl -0x8(%rbp), %edi
movl $0xa, %esi
callq 0xc0e0
jmp 0x363dd
movl -0x8(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x30, %rsp
popq %rbp
retq
nopl (%rax)