// File: /workspace/github_cpp_makefile_star2_O2/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
pushq $0x40
popq %rsi
cmovbl %edx, %esi
testq %rbx, %rbx
setne %al
testq %rdi, %rdi
setne %cl
testb %al, %cl
je 0x8fda
movq %rdi, %r14
movb (%rbx), %r12b
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)
movzwl 0x1e7(%r14), %eax
movw %ax, 0x20(%rsp)
cmpb $0x10, %r12b
jb 0x8f19
testb $0x1, %r15b
pushq $0x30
popq %rax
pushq $0x3f
popq %rcx
cmovnel %ecx, %eax
movb %al, 0x22(%rsp)
movb $0x3, 0x23(%rsp)
movb %r12b, 0x24(%rsp)
pushq $0x5
jmp 0x8f29
movl %r12d, %eax
orb $0x30, %al
movb %al, 0x22(%rsp)
movb $0x3, 0x23(%rsp)
pushq $0x4
popq %rax
movl %eax, %ebp
movzwl 0x1e9(%r14), %ecx
movl %eax, %eax
movw %cx, 0x20(%rsp,%rax)
testb $0x20, 0x188(%r14)
je 0x8f56
cmpl $0x1000006, 0x1f8(%r14) # imm = 0x1000006
movl 0x4(%rsp), %ecx
ja 0x8f67
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 0x8d23
movl %eax, %ebp
cmpb %r12b, 0x2(%rbx)
sete %al
notb %r15b
orb %al, %r15b
testb $0x1, %r15b
jne 0x8ff1
movb (%rbx), %r12b
xorl %r15d, %r15d
cmpb $0xf, %r12b
ja 0x8ee7
jmp 0x8ff1
leaq 0x29dd(%rip), %rdi      # 0xb9be
leaq 0x2a2e(%rip), %rsi      # 0xba16
xorl %ebp, %ebp
xorl %eax, %eax
callq 0x24bf
movl %ebp, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq