// File: chenhn123[P]wdt_util_src/wdt_ct/func_i2c.cpp
// Function: wh_i2c_get_feature(WdtDevice*, unsigned char*, unsigned int)
int wh_i2c_get_feature(WDT_DEV *pdev, BYTE* buf, UINT32 buf_size)
{
	int retval;
	REQ_DATA* p_req_data = (REQ_DATA*) buf;
	int data_len = 0;
	BYTE cmd;
	BYTE tx_buffer[10] = {0}; 
	BYTE rx_buffer[80] = {0};
	bool retryflag = true;

	if (buf_size > 64)
		buf_size = 64;

	if (!buf || !pdev) {
		wh_printf("%s : pointer is null\n", __func__);
		return 0;
	}

retry:
	data_len = 0;
	cmd = buf[0];
	tx_buffer[data_len++] = pdev->board_info.dev_hid_desc.wCommandRegister; 
	tx_buffer[data_len++] = pdev->board_info.dev_hid_desc.wCommandRegister>>8; 


	if (p_req_data->DD.report_id > 0xF)
	{
		if(retryflag == true)
			tx_buffer[data_len++] = 0x3F;
		else 
			tx_buffer[data_len++] = 0x30;
		tx_buffer[data_len++] = I2C_HID_OPCODE_GET_REPORT;
		tx_buffer[data_len++] = cmd;
	}
	else
	{
		tx_buffer[data_len++] = 0x30 | cmd;
		tx_buffer[data_len++] = I2C_HID_OPCODE_GET_REPORT;
	}

        tx_buffer[data_len++] = pdev->board_info.dev_hid_desc.wDataRegister;
        tx_buffer[data_len++] = pdev->board_info.dev_hid_desc.wDataRegister>>8;


	if (pdev->pparam->options & EXEC_I2C_REDUNDANT) {
		tx_buffer[data_len++] = 0x00;
		tx_buffer[data_len++] = 0x00;
	}

	retval = wh_i2c_xfer(pdev, pdev->board_info.i2c_address, tx_buffer, data_len, rx_buffer, buf_size + 2);
	if (!retval)
		return 0;

	UINT32 	xfer_length = rx_buffer[1];
	xfer_length = (xfer_length << 8) | rx_buffer[0];

	if (buf_size < xfer_length)
		xfer_length = buf_size;

	memcpy(buf, &rx_buffer[2], xfer_length);

	if( (rx_buffer[2] != cmd) && (retryflag == true) &&
		(p_req_data->DD.report_id > 0xF) ) {
		retryflag = false;
		goto  retry;
	}	

	return 1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xa0, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x28(%rbp)
movl $0x0, -0x2c(%rbp)
leaq -0x37(%rbp), %rdi
xorl %esi, %esi
movl $0xa, %edx
callq 0x2110
leaq -0x90(%rbp), %rdi
xorl %esi, %esi
movl $0x50, %edx
callq 0x2110
movb $0x1, -0x91(%rbp)
cmpl $0x40, -0x1c(%rbp)
jbe 0xca6c
movl $0x40, -0x1c(%rbp)
cmpq $0x0, -0x18(%rbp)
je 0xca7a
cmpq $0x0, -0x10(%rbp)
jne 0xca9b
leaq 0x40ef(%rip), %rdi      # 0x10b70
leaq 0x4153(%rip), %rsi      # 0x10bdb
movb $0x0, %al
callq 0x24e0
movl $0x0, -0x4(%rbp)
jmp 0xccd1
jmp 0xca9d
movl $0x0, -0x2c(%rbp)
movq -0x18(%rbp), %rax
movb (%rax), %al
movb %al, -0x2d(%rbp)
movq -0x10(%rbp), %rax
movw 0x1e7(%rax), %ax
movb %al, %cl
movl -0x2c(%rbp), %eax
movl %eax, %edx
addl $0x1, %edx
movl %edx, -0x2c(%rbp)
cltq
movb %cl, -0x37(%rbp,%rax)
movq -0x10(%rbp), %rax
movzwl 0x1e7(%rax), %eax
sarl $0x8, %eax
movb %al, %cl
movl -0x2c(%rbp), %eax
movl %eax, %edx
addl $0x1, %edx
movl %edx, -0x2c(%rbp)
cltq
movb %cl, -0x37(%rbp,%rax)
movq -0x28(%rbp), %rax
movzbl (%rax), %eax
cmpl $0xf, %eax
jle 0xcb56
movb -0x91(%rbp), %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x1, %eax
jne 0xcb1c
movl -0x2c(%rbp), %eax
movl %eax, %ecx
addl $0x1, %ecx
movl %ecx, -0x2c(%rbp)
cltq
movb $0x3f, -0x37(%rbp,%rax)
jmp 0xcb2e
movl -0x2c(%rbp), %eax
movl %eax, %ecx
addl $0x1, %ecx
movl %ecx, -0x2c(%rbp)
cltq
movb $0x30, -0x37(%rbp,%rax)
movl -0x2c(%rbp), %eax
movl %eax, %ecx
addl $0x1, %ecx
movl %ecx, -0x2c(%rbp)
cltq
movb $0x2, -0x37(%rbp,%rax)
movb -0x2d(%rbp), %cl
movl -0x2c(%rbp), %eax
movl %eax, %edx
addl $0x1, %edx
movl %edx, -0x2c(%rbp)
cltq
movb %cl, -0x37(%rbp,%rax)
jmp 0xcb82
movzbl -0x2d(%rbp), %eax
orl $0x30, %eax
movb %al, %cl
movl -0x2c(%rbp), %eax
movl %eax, %edx
addl $0x1, %edx
movl %edx, -0x2c(%rbp)
cltq
movb %cl, -0x37(%rbp,%rax)
movl -0x2c(%rbp), %eax
movl %eax, %ecx
addl $0x1, %ecx
movl %ecx, -0x2c(%rbp)
cltq
movb $0x2, -0x37(%rbp,%rax)
movq -0x10(%rbp), %rax
movw 0x1e9(%rax), %ax
movb %al, %cl
movl -0x2c(%rbp), %eax
movl %eax, %edx
addl $0x1, %edx
movl %edx, -0x2c(%rbp)
cltq
movb %cl, -0x37(%rbp,%rax)
movq -0x10(%rbp), %rax
movzwl 0x1e9(%rax), %eax
sarl $0x8, %eax
movb %al, %cl
movl -0x2c(%rbp), %eax
movl %eax, %edx
addl $0x1, %edx
movl %edx, -0x2c(%rbp)
cltq
movb %cl, -0x37(%rbp,%rax)
movq -0x10(%rbp), %rax
movq 0x280(%rax), %rax
movl 0x8(%rax), %eax
andl $0x200, %eax            # imm = 0x200
cmpl $0x0, %eax
je 0xcbfd
movl -0x2c(%rbp), %eax
movl %eax, %ecx
addl $0x1, %ecx
movl %ecx, -0x2c(%rbp)
cltq
movb $0x0, -0x37(%rbp,%rax)
movl -0x2c(%rbp), %eax
movl %eax, %ecx
addl $0x1, %ecx
movl %ecx, -0x2c(%rbp)
cltq
movb $0x0, -0x37(%rbp,%rax)
movq -0x10(%rbp), %rdi
movq -0x10(%rbp), %rax
movb 0x1d6(%rax), %al
leaq -0x37(%rbp), %rdx
movl -0x2c(%rbp), %ecx
leaq -0x90(%rbp), %r8
movl -0x1c(%rbp), %r9d
addl $0x2, %r9d
movzbl %al, %esi
callq 0xc290
movl %eax, -0x20(%rbp)
cmpl $0x0, -0x20(%rbp)
jne 0xcc3e
movl $0x0, -0x4(%rbp)
jmp 0xccd1
movzbl -0x8f(%rbp), %eax
movl %eax, -0x98(%rbp)
movl -0x98(%rbp), %eax
shll $0x8, %eax
movzbl -0x90(%rbp), %ecx
orl %ecx, %eax
movl %eax, -0x98(%rbp)
movl -0x1c(%rbp), %eax
cmpl -0x98(%rbp), %eax
jae 0xcc77
movl -0x1c(%rbp), %eax
movl %eax, -0x98(%rbp)
movq -0x18(%rbp), %rdi
leaq -0x90(%rbp), %rsi
addq $0x2, %rsi
movl -0x98(%rbp), %eax
movl %eax, %edx
callq 0x2150
movzbl -0x8e(%rbp), %eax
movzbl -0x2d(%rbp), %ecx
cmpl %ecx, %eax
je 0xccca
movb -0x91(%rbp), %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x1, %eax
jne 0xccca
movq -0x28(%rbp), %rax
movzbl (%rax), %eax
cmpl $0xf, %eax
jle 0xccca
movb $0x0, -0x91(%rbp)
jmp 0xca9d
movl $0x1, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0xa0, %rsp
popq %rbp
retq
nopl (%rax)