// File: /workspace/github_cpp_makefile_star2_O3/goodix[P]gdix_hid_firmware_update/gtx2/gtx2_update.cpp
// Function: GTx2Update::Run(void*)
int GTx2Update::Run(void *para)
{
	int ret = 0;
	int retry = 0;
	updateFlag flag = NO_NEED_UPDATE;
	if (!m_Initialized) {
		gdix_err("Can't Process Update Before Initialized.\n");
		return -1;
	}
	// get all parameter
	pGTUpdatePara parameter = (pGTUpdatePara)para;

	// check if the image has config
	if (image->HasConfig()) {
		flag = image->GetUpdateFlag();
	} else {
		// default for firmware
		flag = NEED_UPDATE_FW;
	}

	gdix_dbg("Flag = %d\n", flag);

	if (!parameter->force) {
		ret = check_update();
		if (ret) {
			gdix_err("Doesn't meet the update conditions\n");
			return -3;
		}
	}

	if (flag & NEED_UPDATE_CONFIG) {
		retry = 0;
		do {
			ret = cfg_update();
			if (ret) {
				gdix_dbg("Update cfg failed\n");
				usleep(200000);
			} else {
				usleep(300000);
				gdix_dbg("Update cfg success\n");
				break;
			}
		} while (retry++ < 3);
		if (ret) {
			gdix_err("config update err:ret=%d\n", ret);
			return ret;
		}
	}

	if (flag & NEED_UPDATE_FW) {
		retry = 0;
		do {
			ret = fw_update(parameter->firmwareFlag);
			if (ret) {
				gdix_dbg("Update failed\n");
				usleep(200000);
			} else {
				usleep(300000);
				gdix_dbg("Update success\n");
				break;
			}
		} while (retry++ < 3);
		if (ret) {
			gdix_err("Firmware update err:ret=%d\n", ret);
			return ret;
		}
	}

	// before go forward,Set Basic Properties to refresh SensorID
	// dev->SetBasicProperties();

	// if (flag & NEED_UPDATE_CONFIG) {
	// 	retry = 0;
	// 	do {
	// 		ret = cfg_update();
	// 		if (ret) {
	// 			gdix_dbg("Update cfg failed\n");
	// 			usleep(200000);
	// 		} else {
	// 			usleep(300000);
	// 			gdix_dbg("Update cfg success\n");
	// 			break;
	// 		}
	// 	} while (retry++ < 3);
	// 	if (ret) {
	// 		gdix_err("config update err:ret=%d\n", ret);
	// 		return ret;
	// 	}
	// }
	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
cmpb $0x0, 0x18(%rdi)
je 0xab87
movq %rsi, %rbx
movq %rdi, %r14
movq 0x10(%rdi), %rdi
movq (%rdi), %rax
callq *0x98(%rax)
movl $0x1, %ebp
testb %al, %al
je 0xab28
movq 0x10(%r14), %rdi
movq (%rdi), %rax
callq *0xa8(%rax)
movl %eax, %ebp
leaq 0x16762(%rip), %r15     # 0x21291
cmpb $0x1, (%r15)
jne 0xab5c
movq 0x1647c(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
leaq 0xd105(%rip), %rsi      # 0x17c4b
leaq 0xc507(%rip), %rdx      # 0x17054
movl $0x43, %ecx
movl %ebp, %r8d
xorl %eax, %eax
callq 0x5180
cmpb $0x0, (%rbx)
jne 0xaba6
movq (%r14), %rax
movq %r14, %rdi
callq *0x20(%rax)
testl %eax, %eax
je 0xaba6
movl $0xfffffffd, %ebp       # imm = 0xFFFFFFFD
cmpb $0x1, (%r15)
jne 0xad30
callq 0x571d
jmp 0xad30
leaq 0x16703(%rip), %rax     # 0x21291
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
cmpb $0x1, (%rax)
jne 0xad30
callq 0x56fb
jmp 0xad30
testb $0x2, %bpl
je 0xac69
movl %ebp, 0x4(%rsp)
movl $0x4, %r12d
leaq 0xc493(%rip), %r13      # 0x17054
movq (%r14), %rax
movq %r14, %rdi
callq *0x38(%rax)
testl %eax, %eax
je 0xac31
movl %eax, %ebp
cmpb $0x1, (%r15)
jne 0xabf6
movq 0x163db(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
leaq 0xc53e(%rip), %rsi      # 0x17125
movq %r13, %rdx
movl $0x52, %ecx
xorl %eax, %eax
callq 0x5180
movl $0x30d40, %edi          # imm = 0x30D40
callq 0x5170
decl %r12d
jne 0xabc1
cmpb $0x1, (%r15)
jne 0xad30
movq 0x163c2(%rip), %rax     # 0x20fd8
movq (%rax), %rdi
leaq 0xc54e(%rip), %rsi      # 0x1716e
leaq 0xc42d(%rip), %rdx      # 0x17054
movl $0x5b, %ecx
jmp 0xacea
movl $0x493e0, %edi          # imm = 0x493E0
callq 0x5170
cmpb $0x1, (%r15)
movl 0x4(%rsp), %ebp
jne 0xac69
movq 0x1636c(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
leaq 0xc4f3(%rip), %rsi      # 0x17149
leaq 0xc3f7(%rip), %rdx      # 0x17054
movl $0x56, %ecx
xorl %eax, %eax
callq 0x5180
testb $0x1, %bpl
je 0xad2e
movl $0x4, %r13d
leaq 0xc3d4(%rip), %r12      # 0x17054
movl 0x4(%rbx), %esi
movq (%r14), %rax
movq %r14, %rdi
callq *0x30(%rax)
testl %eax, %eax
je 0xacf6
movl %eax, %ebp
cmpb $0x1, (%r15)
jne 0xacb8
movq 0x16319(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
leaq 0xc40d(%rip), %rsi      # 0x170b6
movq %r12, %rdx
movl $0x65, %ecx
xorl %eax, %eax
callq 0x5180
movl $0x30d40, %edi          # imm = 0x30D40
callq 0x5170
decl %r13d
jne 0xac80
cmpb $0x1, (%r15)
jne 0xad30
movq 0x16304(%rip), %rax     # 0x20fd8
movq (%rax), %rdi
leaq 0xbc34(%rip), %rsi      # 0x16912
leaq 0xc36f(%rip), %rdx      # 0x17054
movl $0x6e, %ecx
movl %ebp, %r8d
xorl %eax, %eax
callq 0x5180
jmp 0xad30
movl $0x493e0, %edi          # imm = 0x493E0
callq 0x5170
cmpb $0x1, (%r15)
jne 0xad2e
movq 0x162ab(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
leaq 0xc3bf(%rip), %rsi      # 0x170d6
leaq 0xc336(%rip), %rdx      # 0x17054
xorl %ebp, %ebp
movl $0x69, %ecx
xorl %eax, %eax
callq 0x5180
jmp 0xad30
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop