// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
andq $0x0, 0x8(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x50(%rsp)
movaps %xmm0, 0x40(%rsp)
movaps %xmm0, 0x30(%rsp)
movaps %xmm0, 0x20(%rsp)
movaps %xmm0, 0x10(%rsp)
cmpl $0x40, %edx
pushq $0x40
popq %r12
cmovbl %edx, %r12d
testq %rsi, %rsi
setne %al
testq %rdi, %rdi
setne %cl
testb %al, %cl
je 0x8b4e
movq %rsi, %rbx
movq %rdi, %r14
movb (%rsi), %r13b
leal 0x2(%r12), %r15d
movb $0x1, %bpl
movzwl 0x1e7(%r14), %eax
movw %ax, 0x6(%rsp)
cmpb $0x10, %r13b
jb 0x8aa2
testb $0x1, %bpl
pushq $0x30
popq %rax
pushq $0x3f
popq %rcx
cmovnel %ecx, %eax
movb %al, 0x8(%rsp)
movb $0x2, 0x9(%rsp)
movb %r13b, 0xa(%rsp)
pushq $0x5
jmp 0x8ab2
movl %r13d, %eax
orb $0x30, %al
movb %al, 0x8(%rsp)
movb $0x2, 0x9(%rsp)
pushq $0x4
popq %rax
movzwl 0x1e9(%r14), %ecx
movl %eax, %edx
movw %cx, 0x6(%rsp,%rdx)
movq %rdx, %rcx
orq $0x2, %rcx
movq 0x280(%r14), %rsi
testb $0x2, 0x9(%rsi)
je 0x8aec
xorl %esi, %esi
movb %sil, 0x6(%rsp,%rcx)
addl $0x4, %eax
leaq 0x6(%rsp), %rcx
movb %sil, 0x3(%rdx,%rcx)
movl %eax, %ecx
movzbl 0x1d6(%r14), %esi
movq %r14, %rdi
leaq 0x6(%rsp), %rdx
leaq 0x10(%rsp), %r8
movl %r15d, %r9d
callq 0x84cb
testl %eax, %eax
je 0x8b67
movzwl 0x10(%rsp), %edx
cmpl %edx, %r12d
cmovbl %r12d, %edx
movq %rbx, %rdi
leaq 0x12(%rsp), %rsi
callq 0x2150
cmpb %r13b, 0x12(%rsp)
sete %al
notb %bpl
orb %al, %bpl
testb $0x1, %bpl
jne 0x8b49
movb (%rbx), %r13b
xorl %ebp, %ebp
cmpb $0xf, %r13b
ja 0x8a70
pushq $0x1
popq %rbx
jmp 0x8b69
leaq 0x2e69(%rip), %rdi      # 0xb9be
leaq 0x2ecd(%rip), %rsi      # 0xba29
xorl %ebx, %ebx
xorl %eax, %eax
callq 0x24bf
jmp 0x8b69
xorl %ebx, %ebx
movl %ebx, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq