// File: /workspace/github_c_makefile_star2_O0/seuscq[P]rtk_hciattach/hciattach_rtk.c
// Function: rtk_get_bt_config
int rtk_get_bt_config(struct btrtl_info *btrtl, uint8_t **config_buf,
		RT_U32 *config_baud_rate)
{
	char bt_config_file_name[PATH_MAX] = { 0 };
	RT_U8 *bt_addr_temp = NULL;
	uint8_t bt_addr[6] = { 0x00, 0xe0, 0x4c, 0x88, 0x88, 0x88 };
	struct stat st;
	size_t filelen;
	size_t tlength;
	int fd;
	int ret = 0;
	int i = 0;

#ifdef USE_CUSTOMER_ADDRESS
#define BDADDR_STRING_LEN	17
	size_t size;
	size_t result;
	uint8_t tbuf[BDADDR_STRING_LEN + 1];
	char *str;

	if (stat(BT_ADDR_FILE, &st) < 0) {
		RS_INFO("Couldnt access customer BT MAC file %s",
		        BT_ADDR_FILE);

		/* for (i = 0; i < 6; i++)
		 * 	rtk_get_ram_addr(&bt_addr[i]);
		 * rtk_write_btmac2file(bt_addr);
		 */
		goto GET_CONFIG;
	}

	size = st.st_size;
	/* Only read the first 17-byte if the file length is larger */
	if (size > BDADDR_STRING_LEN)
		size = BDADDR_STRING_LEN;

	fd = open(BT_ADDR_FILE, O_RDONLY);
	if (fd == -1) {
		RS_ERR("Couldnt open BT MAC file %s, %s", BT_ADDR_FILE,
		       strerror(errno));
	} else {
		memset(tbuf, 0, sizeof(tbuf));
		result = read(fd, tbuf, size);
		close(fd);
		if (result == -1) {
			RS_ERR("Couldnt read BT MAC file %s, err %s",
			       BT_ADDR_FILE, strerror(errno));
			goto GET_CONFIG;
		}

		if (bachk(tbuf) < 0) {
			goto GET_CONFIG;
		}

		str = tbuf;
		for (i = 5; i >= 0; i--) {
			bt_addr[i] = (uint8_t)strtoul(str, NULL, 16);
			str += 3;
		}

		/*reserve LAP addr from 0x9e8b00 to 0x9e8b3f, change to 0x008b** */
		if (0x9e == bt_addr[3] && 0x8b == bt_addr[4]
		    && (bt_addr[5] <= 0x3f)) {
			bt_addr[3] = 0x00;
		}

		RS_DBG("BT MAC is %02x:%02x:%02x:%02x:%02x:%02x",
		       bt_addr[5], bt_addr[4],
		       bt_addr[3], bt_addr[2],
		       bt_addr[1], bt_addr[0]);
		customer_bdaddr = 1;
	}
#endif

GET_CONFIG:
	//ret = sprintf(bt_config_file_name, BT_CONFIG_DIRECTORY "rtlbt_config");
	ret = sprintf(bt_config_file_name, "%s", BT_CONFIG_DIRECTORY);
	strcat(bt_config_file_name, btrtl->patch_ent->config_file);
	if (stat(bt_config_file_name, &st) < 0) {
		RS_ERR("can't access bt config file:%s, errno:%d\n",
		       bt_config_file_name, errno);
		return -1;
	}

	filelen = st.st_size;

	if ((fd = open(bt_config_file_name, O_RDONLY)) < 0) {
		perror("Can't open bt config file");
		return -1;
	}

	tlength = filelen;
#ifdef USE_CUSTOMER_ADDRESS
	tlength += 9;
#endif

#ifdef EXTRA_CONFIG_OPTION
	config_file_proc(EXTRA_CONFIG_FILE);
	if (!xtalset_supported())
		config_flags &= ~CONFIG_XTAL;
	if (config_flags & CONFIG_TXPOWER)
		tlength += 7;
	if (config_flags & CONFIG_XTAL)
		tlength += 4;
#endif

	if ((*config_buf = malloc(tlength)) == NULL) {
		RS_DBG("Couldnt malloc buffer for config (%zd)\n", tlength);
		close(fd);
		return -1;
	}
	//we may need to parse this config file.
	//for easy debug, only get need data.

	if (read(fd, *config_buf, filelen) < (ssize_t) filelen) {
		perror("Can't load bt config file");
		free(*config_buf);
		close(fd);
		return -1;
	}

	*config_buf = rtk_parse_config_file(*config_buf, &filelen, bt_addr,
					    config_baud_rate);
	util_hexdump((const uint8_t *)*config_buf, filelen);
	RS_INFO("Cfg length %u", (uint16_t)filelen);
	RS_DBG("Get config baud rate from config file:%x",
	       (unsigned int)*config_baud_rate);

	close(fd);
	return filelen;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x1150, %rsp           # imm = 0x1150
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
leaq -0x1020(%rbp), %rdi
xorl %esi, %esi
movl $0x1000, %edx           # imm = 0x1000
callq 0x20d0
movq $0x0, -0x1028(%rbp)
movl 0x6915(%rip), %eax      # 0xb540
movl %eax, -0x102e(%rbp)
movw 0x690c(%rip), %ax       # 0xb544
movw %ax, -0x102a(%rbp)
movl $0x0, -0x10d8(%rbp)
movl $0x0, -0x10dc(%rbp)
leaq 0x6d07(%rip), %rdi      # 0xb961
leaq -0x10c0(%rbp), %rsi
callq 0x2160
cmpl $0x0, %eax
jge 0x4c98
jmp 0x4c6d
movq 0xb384(%rip), %rax      # 0xfff8
movq (%rax), %rdi
leaq 0x6cef(%rip), %rsi      # 0xb96d
leaq 0x6904(%rip), %rdx      # 0xb589
leaq 0x6cd5(%rip), %rcx      # 0xb961
movb $0x0, %al
callq 0x2150
jmp 0x4ed8
movq -0x1090(%rbp), %rax
movq %rax, -0x10e8(%rbp)
cmpq $0x11, -0x10e8(%rbp)
jbe 0x4cbb
movq $0x11, -0x10e8(%rbp)
leaq 0x6c9f(%rip), %rdi      # 0xb961
xorl %esi, %esi
movb $0x0, %al
callq 0x21f0
movl %eax, -0x10d4(%rbp)
cmpl $-0x1, -0x10d4(%rbp)
jne 0x4d24
jmp 0x4cdc
movq 0xb315(%rip), %rax      # 0xfff8
movq (%rax), %rax
movq %rax, -0x1120(%rbp)
callq 0x2040
movl (%rax), %edi
callq 0x2290
movq -0x1120(%rbp), %rdi
movq %rax, %r8
leaq 0x6c8f(%rip), %rsi      # 0xb999
leaq 0x6878(%rip), %rdx      # 0xb589
leaq 0x6c49(%rip), %rcx      # 0xb961
movb $0x0, %al
callq 0x2150
jmp 0x4ed6
leaq -0x1110(%rbp), %rdi
xorl %esi, %esi
movl $0x12, %edx
callq 0x20d0
movl -0x10d4(%rbp), %edi
leaq -0x1110(%rbp), %rsi
movq -0x10e8(%rbp), %rdx
callq 0x2110
movq %rax, -0x10f0(%rbp)
movl -0x10d4(%rbp), %edi
callq 0x2100
cmpq $-0x1, -0x10f0(%rbp)
jne 0x4db6
jmp 0x4d6e
movq 0xb283(%rip), %rax      # 0xfff8
movq (%rax), %rax
movq %rax, -0x1128(%rbp)
callq 0x2040
movl (%rax), %edi
callq 0x2290
movq -0x1128(%rbp), %rdi
movq %rax, %r8
leaq 0x6c28(%rip), %rsi      # 0xb9c4
leaq 0x67e6(%rip), %rdx      # 0xb589
leaq 0x6bb7(%rip), %rcx      # 0xb961
movb $0x0, %al
callq 0x2150
jmp 0x4ed8
leaq -0x1110(%rbp), %rdi
callq 0x4af0
cmpl $0x0, %eax
jge 0x4dcc
jmp 0x4ed8
leaq -0x1110(%rbp), %rax
movq %rax, -0x1118(%rbp)
movl $0x5, -0x10dc(%rbp)
cmpl $0x0, -0x10dc(%rbp)
jl 0x4e35
movq -0x1118(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0x10, %edx
callq 0x2240
movb %al, %cl
movslq -0x10dc(%rbp), %rax
movb %cl, -0x102e(%rbp,%rax)
movq -0x1118(%rbp), %rax
addq $0x3, %rax
movq %rax, -0x1118(%rbp)
movl -0x10dc(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x10dc(%rbp)
jmp 0x4de4
movzbl -0x102b(%rbp), %ecx
movl $0x9e, %eax
cmpl %ecx, %eax
jne 0x4e68
movzbl -0x102a(%rbp), %ecx
movl $0x8b, %eax
cmpl %ecx, %eax
jne 0x4e68
movzbl -0x1029(%rbp), %eax
cmpl $0x3f, %eax
jg 0x4e68
movb $0x0, -0x102b(%rbp)
jmp 0x4e6a
cmpb $0x0, 0xb45f(%rip)      # 0x102d0
je 0x4ecd
movq 0xb17e(%rip), %rax      # 0xfff8
movq (%rax), %rdi
movzbl -0x1029(%rbp), %ecx
movzbl -0x102a(%rbp), %r8d
movzbl -0x102b(%rbp), %r9d
movzbl -0x102c(%rbp), %r11d
movzbl -0x102d(%rbp), %r10d
movzbl -0x102e(%rbp), %eax
leaq 0x6b41(%rip), %rsi      # 0xb9f3
leaq 0x66d0(%rip), %rdx      # 0xb589
movl %r11d, (%rsp)
movl %r10d, 0x8(%rsp)
movl %eax, 0x10(%rsp)
movb $0x0, %al
callq 0x2150
jmp 0x4ecf
movb $0x1, 0xb89e(%rip)      # 0x10774
jmp 0x4ed8
leaq -0x1020(%rbp), %rdi
leaq 0x6b3a(%rip), %rsi      # 0xba20
leaq 0x6b36(%rip), %rdx      # 0xba23
movb $0x0, %al
callq 0x2270
movl %eax, -0x10d8(%rbp)
leaq -0x1020(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x90(%rax), %rax
movq 0x18(%rax), %rsi
callq 0x2260
leaq -0x1020(%rbp), %rdi
leaq -0x10c0(%rbp), %rsi
callq 0x2160
cmpl $0x0, %eax
jge 0x4f85
jmp 0x4f2f
movq 0xb0c2(%rip), %rax      # 0xfff8
movq (%rax), %rax
movq %rax, -0x1138(%rbp)
leaq -0x1020(%rbp), %rax
movq %rax, -0x1130(%rbp)
callq 0x2040
movq -0x1138(%rbp), %rdi
movq -0x1130(%rbp), %rcx
movl (%rax), %r8d
leaq 0x6acd(%rip), %rsi      # 0xba38
leaq 0x6617(%rip), %rdx      # 0xb589
movb $0x0, %al
callq 0x2150
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x51aa
movq -0x1090(%rbp), %rax
movq %rax, -0x10c8(%rbp)
leaq -0x1020(%rbp), %rdi
xorl %esi, %esi
movb $0x0, %al
callq 0x21f0
movl %eax, -0x10d4(%rbp)
cmpl $0x0, %eax
jge 0x4fc6
leaq 0x6ab8(%rip), %rdi      # 0xba6d
callq 0x2220
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x51aa
movq -0x10c8(%rbp), %rax
movq %rax, -0x10d0(%rbp)
movq -0x10d0(%rbp), %rax
addq $0x9, %rax
movq %rax, -0x10d0(%rbp)
leaq 0x6a9a(%rip), %rdi      # 0xba87
callq 0x51c0
callq 0x5310
cmpl $0x0, %eax
jne 0x500b
movl 0xb76e(%rip), %eax      # 0x10770
andl $-0x3, %eax
movl %eax, 0xb765(%rip)      # 0x10770
movl 0xb75f(%rip), %eax      # 0x10770
andl $0x1, %eax
cmpl $0x0, %eax
je 0x502b
movq -0x10d0(%rbp), %rax
addq $0x7, %rax
movq %rax, -0x10d0(%rbp)
movl 0xb73f(%rip), %eax      # 0x10770
andl $0x2, %eax
cmpl $0x0, %eax
je 0x504b
movq -0x10d0(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x10d0(%rbp)
movq -0x10d0(%rbp), %rdi
callq 0x2190
movq -0x18(%rbp), %rcx
movq %rax, (%rcx)
cmpq $0x0, %rax
jne 0x50ae
jmp 0x5066
cmpb $0x0, 0xb263(%rip)      # 0x102d0
je 0x5095
movq 0xaf82(%rip), %rax      # 0xfff8
movq (%rax), %rdi
movq -0x10d0(%rbp), %rcx
leaq 0x6a16(%rip), %rsi      # 0xba9d
leaq 0x64fb(%rip), %rdx      # 0xb589
movb $0x0, %al
callq 0x2150
jmp 0x5097
movl -0x10d4(%rbp), %edi
callq 0x2100
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x51aa
movl -0x10d4(%rbp), %edi
movq -0x18(%rbp), %rax
movq (%rax), %rsi
movq -0x10c8(%rbp), %rdx
callq 0x2110
cmpq -0x10c8(%rbp), %rax
jge 0x50ff
leaq 0x69f3(%rip), %rdi      # 0xbaca
callq 0x2220
movq -0x18(%rbp), %rax
movq (%rax), %rdi
callq 0x2030
movl -0x10d4(%rbp), %edi
callq 0x2100
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x51aa
movq -0x18(%rbp), %rax
movq (%rax), %rdi
leaq -0x102e(%rbp), %rdx
movq -0x20(%rbp), %rcx
leaq -0x10c8(%rbp), %rsi
callq 0x41d0
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, (%rax)
movq -0x18(%rbp), %rax
movq (%rax), %rdi
movq -0x10c8(%rbp), %rsi
callq 0x3ac0
movq 0xaeb7(%rip), %rax      # 0xfff8
movq (%rax), %rdi
movq -0x10c8(%rbp), %rax
movzwl %ax, %ecx
leaq 0x698f(%rip), %rsi      # 0xbae4
leaq 0x642d(%rip), %rdx      # 0xb589
movb $0x0, %al
callq 0x2150
jmp 0x5165
cmpb $0x0, 0xb164(%rip)      # 0x102d0
je 0x5193
movq 0xae83(%rip), %rax      # 0xfff8
movq (%rax), %rdi
movq -0x20(%rbp), %rax
movl (%rax), %ecx
leaq 0x6972(%rip), %rsi      # 0xbaf7
leaq 0x63fd(%rip), %rdx      # 0xb589
movb $0x0, %al
callq 0x2150
jmp 0x5195
movl -0x10d4(%rbp), %edi
callq 0x2100
movq -0x10c8(%rbp), %rax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x1150, %rsp           # imm = 0x1150
popq %rbp
retq
nopw %cs:(%rax,%rax)