// File: chenhn123[P]wdt_util_src/wdt_ct/wdt_dev_api.cpp
// Function: find_device_name(char*, char*, char*)
int find_device_name(char *hid_dev_name, char *driver_name, char *driver_path)
{
	char dev_path[] = "/sys/bus/i2c/devices/";

	struct dirent * devs_dir_entry;
	DIR * devs_dir;
	struct dirent * dev_dir_entry;
	DIR * dev_dir;
	int 	device_found = 0;
	ssize_t sz;
	char	tmp_buf[256];
	char	tmp_path[277];

	devs_dir = opendir(dev_path);
	if (!devs_dir) {
		printf("can not open device path: %s\n", dev_path);
		return 0;
	}


	while((devs_dir_entry = readdir(devs_dir)) != NULL) {
		if (devs_dir_entry->d_type != DT_LNK)
			continue;

		sz = readlinkat(dirfd(devs_dir), devs_dir_entry->d_name, tmp_buf, 256);
		if (sz < 0)
			continue;

		tmp_buf[sz] = 0;

		sprintf(tmp_path, "%s%s", dev_path, tmp_buf);

		dev_dir = opendir(tmp_path);
		if (!dev_dir) 
			continue;

		while ((dev_dir_entry = readdir(dev_dir)) != NULL) {
			if (!strcmp(dev_dir_entry->d_name, hid_dev_name)) {
				strcpy(driver_name, devs_dir_entry->d_name);
				device_found = 1;
				break;
			}
		}
		closedir(dev_dir);

		if (device_found){
			strcat(tmp_path, "/uevent");
			FILE *stream;
                        char line[64];

                	stream = fopen(tmp_path, "r");
                	if (stream == NULL) {
            	        	printf("can not open driver path: %s\n", tmp_path);
		        	return 0;
               		}

                	if(fgets (line, 64, stream)!=NULL) {
            			line[strcspn(line, "\n")] = 0;
                        	char *modulename;
                		modulename = strchr(line, '=') +1;
                        	strcat(driver_path, modulename);
                        	strcat(driver_path, "/");
                	}
                	fclose(stream);
                	break;
		}
	}
	closedir(devs_dir);
	return device_found;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x2f0, %rsp            # imm = 0x2F0
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq 0x5852(%rip), %rax      # 0x10800
movq %rax, -0x40(%rbp)
movq 0x584f(%rip), %rax      # 0x10808
movq %rax, -0x38(%rbp)
movl 0x584d(%rip), %eax      # 0x10810
movl %eax, -0x30(%rbp)
movw 0x5847(%rip), %ax       # 0x10814
movw %ax, -0x2c(%rbp)
movl $0x0, -0x64(%rbp)
leaq -0x40(%rbp), %rdi
callq 0x2240
movq %rax, -0x50(%rbp)
cmpq $0x0, -0x50(%rbp)
jne 0xb00a
leaq -0x40(%rbp), %rsi
leaq 0x561e(%rip), %rdi      # 0x10615
movb $0x0, %al
callq 0x2050
movl $0x0, -0x4(%rbp)
jmp 0xb1f4
jmp 0xb00c
movq -0x50(%rbp), %rdi
callq 0x2250
movq %rax, -0x48(%rbp)
cmpq $0x0, %rax
je 0xb1e5
movq -0x48(%rbp), %rax
movzbl 0x12(%rax), %eax
cmpl $0xa, %eax
je 0xb032
jmp 0xb00c
movq -0x50(%rbp), %rdi
callq 0x2100
movl %eax, %edi
movq -0x48(%rbp), %rsi
addq $0x13, %rsi
leaq -0x170(%rbp), %rdx
movl $0x100, %ecx            # imm = 0x100
callq 0x2090
movq %rax, -0x70(%rbp)
cmpq $0x0, -0x70(%rbp)
jge 0xb063
jmp 0xb00c
movq -0x70(%rbp), %rax
movb $0x0, -0x170(%rbp,%rax)
leaq -0x290(%rbp), %rdi
leaq -0x40(%rbp), %rdx
leaq -0x170(%rbp), %rcx
leaq 0x55ab(%rip), %rsi      # 0x10633
movb $0x0, %al
callq 0x2080
leaq -0x290(%rbp), %rdi
callq 0x2240
movq %rax, -0x60(%rbp)
cmpq $0x0, -0x60(%rbp)
jne 0xb0ab
jmp 0xb00c
jmp 0xb0ad
movq -0x60(%rbp), %rdi
callq 0x2250
movq %rax, -0x58(%rbp)
cmpq $0x0, %rax
je 0xb0f2
movq -0x58(%rbp), %rdi
addq $0x13, %rdi
movq -0x10(%rbp), %rsi
callq 0x2200
cmpl $0x0, %eax
jne 0xb0f0
movq -0x18(%rbp), %rdi
movq -0x48(%rbp), %rsi
addq $0x13, %rsi
callq 0x2180
movl $0x1, -0x64(%rbp)
jmp 0xb0f2
jmp 0xb0ad
movq -0x60(%rbp), %rdi
callq 0x2280
cmpl $0x0, -0x64(%rbp)
je 0xb1e0
leaq -0x290(%rbp), %rdi
leaq 0x5525(%rip), %rsi      # 0x10638
callq 0x22a0
leaq -0x290(%rbp), %rdi
leaq 0x5aa0(%rip), %rsi      # 0x10bc6
callq 0x21d0
movq %rax, -0x298(%rbp)
cmpq $0x0, -0x298(%rbp)
jne 0xb15d
leaq -0x290(%rbp), %rsi
leaq 0x54f6(%rip), %rdi      # 0x10640
movb $0x0, %al
callq 0x2050
movl $0x0, -0x4(%rbp)
jmp 0xb1f4
leaq -0x2e0(%rbp), %rdi
movq -0x298(%rbp), %rdx
movl $0x40, %esi
callq 0x2260
cmpq $0x0, %rax
je 0xb1d2
leaq -0x2e0(%rbp), %rdi
leaq 0x4b3e(%rip), %rsi      # 0xfcc7
callq 0x2060
movb $0x0, -0x2e0(%rbp,%rax)
leaq -0x2e0(%rbp), %rdi
movl $0x3d, %esi
callq 0x20c0
addq $0x1, %rax
movq %rax, -0x2e8(%rbp)
movq -0x20(%rbp), %rdi
movq -0x2e8(%rbp), %rsi
callq 0x22a0
movq -0x20(%rbp), %rdi
leaq 0x5515(%rip), %rsi      # 0x106e2
callq 0x22a0
movq -0x298(%rbp), %rdi
callq 0x21a0
jmp 0xb1e5
jmp 0xb00c
movq -0x50(%rbp), %rdi
callq 0x2280
movl -0x64(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x2f0, %rsp            # imm = 0x2F0
popq %rbp
retq