// File: /workspace/github_cpp_makefile_star2_O2/jkingan[P]ladyheather/heathgps.cpp
// Function: set_rcvr_baud(int, int, int, int)
int set_rcvr_baud(int baud, int data_bits, int parity, int stop_bits)
{
int i;

   // future command to set receiver serial port settings
   // !!!!! data_bits, parity, stop_bits not currently implemented
   //       We currentlu always set 8:N:1
   // returns: 0=baud set  1=improper rate   2=baud change not supported

   if(read_only) return 2;

   i = (-1);
   if     (baud == 150) return 1;   // don't allow non-standard values
   else if(baud == 300)    i = 0;   
   else if(baud == 600)    i = 1;   
   else if(baud == 1200)   i = 2;  
   else if(baud == 2400)   i = 3;  
   else if(baud == 4800)   i = 4;  
   else if(baud == 9600)   i = 5;  
   else if(baud == 19200)  i = 6; 
   else if(baud == 38400)  i = 7; 
   else if(baud == 57600)  i = 8; 
   else if(baud == 115200) i = 9; 
   else if(baud == 230400) {
      if(0 && (rcvr_type == VENUS_RCVR)) i = 10;
      else return 1;
   }
   else return 1;

   if(rcvr_type == ESIP_RCVR) {
      sprintf(out, "PERDCFG,UART1,%d", baud);
      send_esip_cmd(out);
      com[RCVR_PORT].baud_rate = baud;
   }
   else if(0 && (rcvr_type == NVS_RCVR)) {
      if(i < 4) return 1;
      send_nvs_start(0x0B);
      send_byte(0);    // current port
      send_dword(baud);
      send_byte(0);    // current protocol
      send_nvs_end();
      com[RCVR_PORT].baud_rate = baud;
   }
   else if(1 && (rcvr_type == SIRF_RCVR)) {
      if(i < 2) return 1;
      send_sirf_start(SET_SIRF_BINARY, 8+1);
      send_dword(baud);
      send_byte(8);
      send_byte(1);
      send_byte(0);
      send_byte(0);
      send_sirf_end();
      com[RCVR_PORT].baud_rate = baud;
   }
   else if(rcvr_type == TM4_RCVR) {  // time port baud rate
      i -= 2;
      if(i < 0) return 1;
      sprintf(out, "#10,%d", i);
      send_tm4_cmd(out);
      return 0;   // does not change control serial port baud rate !!!
   }
   else if(0 && (rcvr_type == TSIP_RCVR)) {
      if((baud >= 9600) && (baud <= 115200)) {
         set_trimble_protocol(baud, 8, NO_PAR, 1, 2,2);
         com[RCVR_PORT].baud_rate = baud;
      }
      else return 1;
   }
   else if(rcvr_type == UBX_RCVR) {
      rcvr_type = NMEA_RCVR;
      sprintf(out, "PUBX,41,1,0003,0001,%d,0", com[RCVR_PORT].baud_rate);  // 9600 bps ublox binary output mode (port 1)
      send_nmea_cmd(out);
      rcvr_type = UBX_RCVR;
      com[RCVR_PORT].baud_rate = baud;
   }
   else if(rcvr_type == VENUS_RCVR) {
      i -= 4;
      if(i < 0) return 1;
      send_venus_start(SET_VENUS_BAUD);
      send_byte(0);  // com1
      send_byte(i); 
      send_venus_save(1);
      com[RCVR_PORT].baud_rate = baud;
   }
   else if(rcvr_type == Z12_RCVR) {
      com[RCVR_PORT].baud_rate = baud;
      sprintf(out, "PASHS,SPD,A,%d", i);
      send_z12_cmd(out);
      com[RCVR_PORT].baud_rate = baud;
   }
   else if(0 && (rcvr_type == ZODIAC_RCVR)) {
      if(i == 9) i += 2;
      else       i += 1;
      if(i == 10) return 1;   // we don't allow 76800 baud

      send_zod_start(ZOD_SET_BAUD, 20, 0);
      send_zod_seq();

      send_word(0x0001);  // port 1

      send_word(1);       // 8 bits
      send_word(0);       // 1 stop
      send_word(0);       // no parity
      send_word(i);       // baud
      send_word(0);
      send_word(0);

      send_word(1);       // 8 bits
      send_word(0);       // 1 stop
      send_word(0);       // no parity
      send_word(i);       // baud
      send_word(0);
      send_word(0);

      send_zod_end();

      com[RCVR_PORT].baud_rate = baud;
   }
   else return 2;

   Sleep(COM_INIT_SLEEP);
   init_com(RCVR_PORT, 150);
   return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
leaq 0x2c90ea(%rip), %rax    # 0x32101a
pushq $0x2
popq %r14
cmpb $0x0, (%rax)
jne 0x5819c
movl %edi, %ebx
movb $0x1, %cl
pushq $0x1
popq %rax
cmpl $0x12c, %edi            # imm = 0x12C
je 0x57fc2
cmpl $0x1c200, %ebx          # imm = 0x1C200
je 0x57f9e
cmpl $0x4b0, %ebx            # imm = 0x4B0
je 0x57fc9
cmpl $0x960, %ebx            # imm = 0x960
je 0x57faa
cmpl $0x12c0, %ebx           # imm = 0x12C0
je 0x57fba
cmpl $0x2580, %ebx           # imm = 0x2580
je 0x57fb6
cmpl $0x4b00, %ebx           # imm = 0x4B00
je 0x57f9a
cmpl $0x9600, %ebx           # imm = 0x9600
je 0x57fa6
cmpl $0xe100, %ebx           # imm = 0xE100
je 0x57fa2
cmpl $0x258, %ebx            # imm = 0x258
jne 0x57fae
pushq $0x1
popq %r15
jmp 0x57fc5
pushq $0x6
jmp 0x57fbc
pushq $0x9
jmp 0x57fbc
pushq $0x8
jmp 0x57fbc
pushq $0x7
jmp 0x57fbc
pushq $0x3
jmp 0x57fcb
movl %eax, %r14d
jmp 0x5819c
pushq $0x5
jmp 0x57fbc
pushq $0x4
popq %r15
xorl %ecx, %ecx
jmp 0x57fcd
xorl %r15d, %r15d
movb $0x1, %dl
jmp 0x57fcf
pushq $0x2
popq %r15
xorl %edx, %edx
leaq 0x1aa8fa(%rip), %r12    # 0x2028d0
movq (%r12), %rsi
cmpq $0x4, %rsi
je 0x58138
cmpq $0x40, %rsi
je 0x580f4
cmpq $0x80, %rsi
je 0x5804e
movabsq $0x400000000, %rcx      # imm = 0x400000000
cmpq %rcx, %rsi
je 0x580ba
movabsq $0x100000000, %rax      # imm = 0x100000000
cmpq %rax, %rsi
je 0x58085
movl $0x80000000, %eax       # imm = 0x80000000
cmpq %rax, %rsi
jne 0x5819c
leaq 0x2ce1f2(%rip), %r14    # 0x326220
leaq 0xb2434(%rip), %rsi     # 0x10a469
movq %r14, %rdi
movl %ebx, %edx
xorl %eax, %eax
callq 0x9060
movq %r14, %rdi
callq 0x57791
jmp 0x58175
movl %eax, %r14d
testb %cl, %cl
jne 0x5819c
pushq $0x5
popq %rdi
pushq $0x4
popq %rsi
callq 0x582b6
xorl %edi, %edi
callq 0x523f2
addb $-0x4, %r15b
movzbl %r15b, %edi
callq 0x523f2
pushq $0x1
popq %rdi
callq 0x58354
jmp 0x58175
leaq 0x2a0254(%rip), %rax    # 0x2f82e0
movl %ebx, 0x159d0(%rax)
leaq 0x2ce187(%rip), %r14    # 0x326220
leaq 0xb23fa(%rip), %rsi     # 0x10a49a
movq %r14, %rdi
movl %r15d, %edx
xorl %eax, %eax
callq 0x9060
movq %r14, %rdi
callq 0x57b55
jmp 0x58175
movl %eax, %r14d
testb %dl, %dl
jne 0x5819c
addl $-0x2, %r15d
leaq 0x2ce150(%rip), %rbx    # 0x326220
leaq 0xb23a3(%rip), %rsi     # 0x10a47a
xorl %r14d, %r14d
movq %rbx, %rdi
movl %r15d, %edx
xorl %eax, %eax
callq 0x9060
movq %rbx, %rdi
callq 0x57a21
jmp 0x5819c
movl %eax, %r14d
testb %dl, %dl
jne 0x5819c
pushq $0x9
popq %rsi
movl $0x86, %edi
callq 0x581a8
movl %ebx, %edi
callq 0x525e2
pushq $0x8
popq %rdi
callq 0x523f2
pushq $0x1
popq %rdi
callq 0x523f2
xorl %edi, %edi
callq 0x523f2
xorl %edi, %edi
callq 0x523f2
callq 0x5824a
jmp 0x58175
movq $0x2, (%r12)
leaq 0x2a0199(%rip), %rax    # 0x2f82e0
movl 0x159d0(%rax), %edx
leaq 0x2ce0cc(%rip), %r14    # 0x326220
leaq 0xb2326(%rip), %rsi     # 0x10a481
movq %r14, %rdi
xorl %eax, %eax
callq 0x9060
movq %r14, %rdi
callq 0x579af
movq $0x4, (%r12)
leaq 0x2a0164(%rip), %rax    # 0x2f82e0
movl %ebx, 0x159d0(%rax)
movl $0xc8, %edi
callq 0xae83
pushq $0xa
popq %rdi
movl $0x96, %esi
callq 0xb30a
xorl %r14d, %r14d
movl %r14d, %eax
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq