// File: /workspace/github_cpp_makefile_star2_O1/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 0xac01
movq %rsi, %rbx
movq %rdi, %r14
movq 0x10(%rdi), %rdi
movq (%rdi), %rax
callq *0x98(%rax)
movl $0x1, %r12d
testb %al, %al
je 0xaba2
movq 0x10(%r14), %rdi
movq (%rdi), %rax
callq *0xa8(%rax)
movl %eax, %r12d
leaq 0x166e8(%rip), %r15     # 0x21291
cmpb $0x1, (%r15)
jne 0xabd6
movq 0x16402(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
leaq 0xd0ec(%rip), %rsi      # 0x17cac
leaq 0xc4ee(%rip), %rdx      # 0x170b5
movl $0x43, %ecx
movl %r12d, %r8d
xorl %eax, %eax
callq 0x5180
cmpb $0x0, (%rbx)
jne 0xac20
movq (%r14), %rax
movq %r14, %rdi
callq *0x20(%rax)
testl %eax, %eax
je 0xac20
movl $0xfffffffd, %ebp       # imm = 0xFFFFFFFD
cmpb $0x1, (%r15)
jne 0xadb4
callq 0x5771
jmp 0xadb4
leaq 0x16689(%rip), %rax     # 0x21291
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
cmpb $0x1, (%rax)
jne 0xadb4
callq 0x574f
jmp 0xadb4
testb $0x2, %r12b
je 0xaceb
movl %r12d, 0x4(%rsp)
movl $0x4, %r12d
leaq 0xc479(%rip), %r13      # 0x170b5
movq (%r14), %rax
movq %r14, %rdi
callq *0x38(%rax)
movl %eax, %ebp
testl %eax, %eax
je 0xac82
cmpb $0x1, (%r15)
jne 0xac71
movq 0x16360(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
leaq 0xc524(%rip), %rsi      # 0x17186
movq %r13, %rdx
movl $0x52, %ecx
xorl %eax, %eax
callq 0x5180
movl $0x30d40, %edi          # imm = 0x30D40
callq 0x5170
decl %r12d
jne 0xac3c
jmp 0xacb6
movl $0x493e0, %edi          # imm = 0x493E0
callq 0x5170
cmpb $0x1, (%r15)
jne 0xacb6
movq 0x1631f(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
leaq 0xc507(%rip), %rsi      # 0x171aa
leaq 0xc40b(%rip), %rdx      # 0x170b5
movl $0x56, %ecx
xorl %eax, %eax
callq 0x5180
testl %ebp, %ebp
movl 0x4(%rsp), %r12d
je 0xaceb
cmpb $0x1, (%r15)
jne 0xadb4
movq 0x16308(%rip), %rax     # 0x20fd8
movq (%rax), %rdi
leaq 0xc4f5(%rip), %rsi      # 0x171cf
leaq 0xc3d4(%rip), %rdx      # 0x170b5
movl $0x5b, %ecx
jmp 0xada6
testb $0x1, %r12b
je 0xadb2
movl $0x4, %r13d
leaq 0xc3b3(%rip), %r12      # 0x170b5
movl 0x4(%rbx), %esi
movq (%r14), %rax
movq %r14, %rdi
callq *0x30(%rax)
movl %eax, %ebp
testl %eax, %eax
je 0xad4b
cmpb $0x1, (%r15)
jne 0xad3a
movq 0x16297(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
leaq 0xc3ec(%rip), %rsi      # 0x17117
movq %r12, %rdx
movl $0x65, %ecx
xorl %eax, %eax
callq 0x5180
movl $0x30d40, %edi          # imm = 0x30D40
callq 0x5170
decl %r13d
jne 0xad02
jmp 0xad7f
movl $0x493e0, %edi          # imm = 0x493E0
callq 0x5170
cmpb $0x1, (%r15)
jne 0xad7f
movq 0x16256(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
leaq 0xc3cb(%rip), %rsi      # 0x17137
leaq 0xc342(%rip), %rdx      # 0x170b5
movl $0x69, %ecx
xorl %eax, %eax
callq 0x5180
testl %ebp, %ebp
je 0xadb2
cmpb $0x1, (%r15)
jne 0xadb4
movq 0x16248(%rip), %rax     # 0x20fd8
movq (%rax), %rdi
leaq 0xbbd9(%rip), %rsi      # 0x16973
leaq 0xc314(%rip), %rdx      # 0x170b5
movl $0x6e, %ecx
movl %ebp, %r8d
xorl %eax, %eax
callq 0x5180
jmp 0xadb4
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop