// File: /workspace/github_cpp_makefile_star2_O1/chenhn123[P]wdt_util_src/wdt_ct/func_i2c.cpp
// Function: wh_i2c_set_feature(WdtDevice*, unsigned char*, unsigned int)
int wh_i2c_set_feature(WDT_DEV *pdev, BYTE* buf, UINT32 buf_size)
{

	REQ_DATA* p_req_data = (REQ_DATA*) buf;
	int data_len = 0;
	BYTE cmd;
	BYTE tx_buffer[80] = {0};
	bool retryflag = true;	
	int retval = 0;

	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_SET_REPORT;
		tx_buffer[data_len++] = cmd;
		
	} else {
		tx_buffer[data_len++] = 0x30 | cmd;
		tx_buffer[data_len++] = I2C_HID_OPCODE_SET_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->board_info.dev_type & FW_WDT8755 &&
		 pdev->board_info.dev_info.w8755_dev_info.protocol_version >= 0x01000007) ||
		 (pdev->dev_state != DS_PROGRAM) )
	{
		int xfer_size = buf_size + 2;
		tx_buffer[data_len++] = (xfer_size & 0xFF);
		tx_buffer[data_len++] = ((xfer_size & 0xFF00) >> 8);
	} else {
		tx_buffer[data_len++] = (buf_size & 0xFF);
		tx_buffer[data_len++] = ((buf_size & 0xFF00) >> 8);
	}

	memcpy(&tx_buffer[data_len], buf, buf_size);

	retval = wh_i2c_tx(pdev, pdev->board_info.i2c_address, tx_buffer,
			 data_len + buf_size);	

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

	return retval;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %rsi, %rbx
xorps %xmm0, %xmm0
movaps %xmm0, 0x60(%rsp)
movaps %xmm0, 0x50(%rsp)
movaps %xmm0, 0x40(%rsp)
movaps %xmm0, 0x30(%rsp)
movaps %xmm0, 0x20(%rsp)
cmpl $0x40, %edx
movl $0x40, %esi
cmovbl %edx, %esi
testq %rbx, %rbx
sete %al
testq %rdi, %rdi
sete %cl
orb %al, %cl
jne 0x9bde
movq %rdi, %r14
movl %esi, %eax
movq %rax, 0x18(%rsp)
movb $0x1, %r15b
movzbl %sil, %eax
movl %eax, 0xc(%rsp)
movq %rsi, 0x10(%rsp)
leal 0x2(%rsi), %eax
movl %eax, 0x4(%rsp)
movzbl %al, %eax
movl %eax, 0x8(%rsp)
movb (%rbx), %r12b
movzwl 0x1e7(%r14), %eax
movw %ax, 0x20(%rsp)
cmpb $0x10, %r12b
jb 0x9b0b
testb $0x1, %r15b
je 0x9b20
movb $0x3f, 0x22(%rsp)
jmp 0x9b25
movl %r12d, %eax
orb $0x30, %al
movb %al, 0x22(%rsp)
movb $0x3, 0x23(%rsp)
movl $0x4, %ebp
jmp 0x9b34
movb $0x30, 0x22(%rsp)
movb $0x3, 0x23(%rsp)
movb %r12b, 0x24(%rsp)
movl $0x5, %ebp
movzwl 0x1e9(%r14), %ecx
movl %ebp, %eax
movw %cx, 0x20(%rsp,%rax)
testb $0x20, 0x188(%r14)
je 0x9b5e
cmpl $0x1000006, 0x1f8(%r14) # imm = 0x1000006
movl 0x4(%rsp), %ecx
ja 0x9b6f
cmpl $0x3, 0x278(%r14)
movl 0x8(%rsp), %ecx
cmovel 0xc(%rsp), %ecx
movq %rax, %rdx
orq $0x2, %rdx
movb %cl, 0x20(%rsp,%rdx)
leaq 0x20(%rsp), %r13
movb $0x0, 0x3(%rax,%r13)
leal 0x4(%rbp), %eax
leaq (%rsp,%rax), %rdi
addq $0x20, %rdi
movq %rbx, %rsi
movq 0x18(%rsp), %rdx
callq 0x2150
movzbl 0x1d6(%r14), %esi
movq 0x10(%rsp), %rax
leal (%rax,%rbp), %ecx
addl $0x4, %ecx
movq %r14, %rdi
movq %r13, %rdx
callq 0x9924
movl %eax, %ebp
cmpb %r12b, 0x2(%rbx)
sete %al
notb %r15b
orb %al, %r15b
testb $0x1, %r15b
jne 0x9bf5
xorl %r15d, %r15d
cmpb $0xf, (%rbx)
ja 0x9ae8
jmp 0x9bf5
leaq 0x2ddd(%rip), %rdi      # 0xc9c2
leaq 0x2e2e(%rip), %rsi      # 0xca1a
xorl %ebp, %ebp
xorl %eax, %eax
callq 0x2506
movl %ebp, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq