// File: rthoelen[P]NXCore_Kenwood/main.cc
// Function: timing_thread(void*)
void *timing_thread(void *t_id)
{
	int i;
	int minor_cycle=0;
	unsigned int seconds = 0;
	char h_buf[59];
	unsigned char nx_packet[28] = { 0x00, 0x99, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
				0x4e, 0x58, 0x44, 0x4e, 0x4e, 0x31, 0x58, 0x44,
				 0x4e, 0x43, 0x54, 0x00, 0x00, 0x00, 0x00, 0x01,
				0x00, 0x00, 0x00, 0x00 };
	char buf_64[80];

	
	
        memset((char *)&h_buf[0], 0, sizeof(h_buf));
	h_buf[21] = 0x99;
	for(;;)
	{

		// minor cycle ~ 80ms


		// send shutdown packets if needed

		for ( i = 0; i < repeater_count; i++)
		{
			if((repeater[i].keydown > 0)&&((minor_cycle % 2) == 1))
				shutdown_64001(i);
		}	

		// clear out 64001 packets

		recv(socket_01,buf_64, 80, MSG_DONTWAIT);

		// major loop ~ 1 second

		if(minor_cycle == 0)
		{

			for( i = 0; i < repeater_count; i++)
			{
				repeater[i].time_since_rx++;
				if(repeater[i].time_since_rx > repeater[i].hold_time)
				{
					repeater[i].time_since_rx = repeater[i].hold_time;
					repeater[i].rx_activity = 0;
				}

				repeater[i].time_since_tx++;
		
				while(tx_busy_sem == 1)
					usleep(1);
				if(repeater[i].time_since_tx > repeater[i].tx_hold_time)
				{
					repeater[i].time_since_tx = repeater[i].tx_hold_time;
					repeater[i].tx_busy = 0;
				}
			}	
	
			if((++seconds % 20) == 0 )
			{
				for( i = 0; i < repeater_count; i++)
				{
					if ( repeater[i].stealth && repeater[i].tx_busy == 0)
					{
						while(tx_sem==1)
							usleep(1);
						tx_sem=1;
						sendto(socket_00, h_buf, sizeof(h_buf), 0, (struct sockaddr *)&repeater[i].rpt_addr_00,
							sizeof(repeater[i].rpt_addr_00));

						sendto(socket_01, nx_packet, sizeof(nx_packet), 0, (struct sockaddr *)&repeater[i].rpt_addr_01,
							sizeof(repeater[i].rpt_addr_01));
						tx_sem=0;

					}	
				}

			}	

		}

		usleep(80000);
		if(++minor_cycle > 11)
			minor_cycle = 0;
	}
	return (void *)0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xc0, %rsp
movq %rdi, -0x8(%rbp)
movl $0x0, -0x10(%rbp)
movl $0x0, -0x14(%rbp)
movq 0x1633c(%rip), %rax     # 0x1dea0
movq %rax, -0x70(%rbp)
movq 0x16339(%rip), %rax     # 0x1dea8
movq %rax, -0x68(%rbp)
movq 0x16336(%rip), %rax     # 0x1deb0
movq %rax, -0x60(%rbp)
movl 0x16334(%rip), %eax     # 0x1deb8
movl %eax, -0x58(%rbp)
leaq -0x50(%rbp), %rdi
xorl %esi, %esi
movl $0x3b, %edx
callq 0x5250
movb $-0x67, -0x3b(%rbp)
movl $0x0, -0xc(%rbp)
movl -0xc(%rbp), %eax
cmpl 0x2297d(%rip), %eax     # 0x2a528
jge 0x7bed
movq 0x2296c(%rip), %rax     # 0x2a520
movslq -0xc(%rbp), %rcx
imulq $0x90, %rcx, %rcx
addq %rcx, %rax
cmpl $0x0, 0x74(%rax)
jle 0x7be0
movl -0x10(%rbp), %eax
movl $0x2, %ecx
cltd
idivl %ecx
cmpl $0x1, %edx
jne 0x7be0
movl -0xc(%rbp), %edi
callq 0x7ec0
jmp 0x7be2
movl -0xc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc(%rbp)
jmp 0x7ba2
movl 0x2295d(%rip), %edi     # 0x2a550
leaq -0xc0(%rbp), %rsi
movl $0x50, %edx
movl $0x40, %ecx
callq 0x5130
cmpl $0x0, -0x10(%rbp)
jne 0x7e9a
movl $0x0, -0xc(%rbp)
movl -0xc(%rbp), %eax
cmpl 0x22905(%rip), %eax     # 0x2a528
jge 0x7d86
movq 0x228f0(%rip), %rax     # 0x2a520
movslq -0xc(%rbp), %rcx
imulq $0x90, %rcx, %rcx
addq %rcx, %rax
movl 0x28(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x28(%rax)
movq 0x228d2(%rip), %rax     # 0x2a520
movslq -0xc(%rbp), %rcx
imulq $0x90, %rcx, %rcx
addq %rcx, %rax
movl 0x28(%rax), %eax
movq 0x228ba(%rip), %rcx     # 0x2a520
movslq -0xc(%rbp), %rdx
imulq $0x90, %rdx, %rdx
addq %rdx, %rcx
cmpl 0x30(%rcx), %eax
jle 0x7cc5
movq 0x228a0(%rip), %rax     # 0x2a520
movslq -0xc(%rbp), %rcx
imulq $0x90, %rcx, %rcx
addq %rcx, %rax
movl 0x30(%rax), %ecx
movq 0x22888(%rip), %rax     # 0x2a520
movslq -0xc(%rbp), %rdx
imulq $0x90, %rdx, %rdx
addq %rdx, %rax
movl %ecx, 0x28(%rax)
movq 0x22870(%rip), %rax     # 0x2a520
movslq -0xc(%rbp), %rcx
imulq $0x90, %rcx, %rcx
addq %rcx, %rax
movl $0x0, 0x38(%rax)
movq 0x22854(%rip), %rax     # 0x2a520
movslq -0xc(%rbp), %rcx
imulq $0x90, %rcx, %rcx
addq %rcx, %rax
movl 0x2c(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x2c(%rax)
cmpl $0x1, 0x2286e(%rip)     # 0x2a558
jne 0x7cf8
movl $0x1, %edi
callq 0x5660
jmp 0x7ce3
movq 0x22821(%rip), %rax     # 0x2a520
movslq -0xc(%rbp), %rcx
imulq $0x90, %rcx, %rcx
addq %rcx, %rax
movl 0x2c(%rax), %eax
movq 0x22809(%rip), %rcx     # 0x2a520
movslq -0xc(%rbp), %rdx
imulq $0x90, %rdx, %rdx
addq %rdx, %rcx
cmpl 0x34(%rcx), %eax
jle 0x7d76
movq 0x227ef(%rip), %rax     # 0x2a520
movslq -0xc(%rbp), %rcx
imulq $0x90, %rcx, %rcx
addq %rcx, %rax
movl 0x34(%rax), %ecx
movq 0x227d7(%rip), %rax     # 0x2a520
movslq -0xc(%rbp), %rdx
imulq $0x90, %rdx, %rdx
addq %rdx, %rax
movl %ecx, 0x2c(%rax)
movq 0x227bf(%rip), %rax     # 0x2a520
movslq -0xc(%rbp), %rcx
imulq $0x90, %rcx, %rcx
addq %rcx, %rax
movl $0x0, 0x3c(%rax)
jmp 0x7d78
movl -0xc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc(%rbp)
jmp 0x7c1a
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
movl $0x14, %ecx
xorl %edx, %edx
divl %ecx
cmpl $0x0, %edx
jne 0x7e98
movl $0x0, -0xc(%rbp)
movl -0xc(%rbp), %eax
cmpl 0x22777(%rip), %eax     # 0x2a528
jge 0x7e96
movq 0x22762(%rip), %rax     # 0x2a520
movslq -0xc(%rbp), %rcx
imulq $0x90, %rcx, %rcx
addq %rcx, %rax
cmpl $0x0, 0x44(%rax)
je 0x7e86
movq 0x22743(%rip), %rax     # 0x2a520
movslq -0xc(%rbp), %rcx
imulq $0x90, %rcx, %rcx
addq %rcx, %rax
cmpl $0x0, 0x3c(%rax)
jne 0x7e86
jmp 0x7df7
cmpl $0x1, 0x22756(%rip)     # 0x2a554
jne 0x7e0c
movl $0x1, %edi
callq 0x5660
jmp 0x7df7
movl $0x1, 0x2273e(%rip)     # 0x2a554
movl 0x22730(%rip), %edi     # 0x2a54c
leaq -0x50(%rbp), %rsi
movq 0x226f9(%rip), %r8      # 0x2a520
movslq -0xc(%rbp), %rax
imulq $0x90, %rax, %rax
addq %rax, %r8
movl $0x3b, %edx
xorl %ecx, %ecx
movl $0x10, %r9d
callq 0x5090
movl 0x22703(%rip), %edi     # 0x2a550
leaq -0x70(%rbp), %rsi
movq 0x226c8(%rip), %r8      # 0x2a520
movslq -0xc(%rbp), %rax
imulq $0x90, %rax, %rax
addq %rax, %r8
addq $0x10, %r8
movl $0x1c, %edx
xorl %ecx, %ecx
movl $0x10, %r9d
callq 0x5090
movl $0x0, 0x226ce(%rip)     # 0x2a554
jmp 0x7e88
movl -0xc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc(%rbp)
jmp 0x7da8
jmp 0x7e98
jmp 0x7e9a
movl $0x13880, %edi          # imm = 0x13880
callq 0x5660
movl -0x10(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x10(%rbp)
cmpl $0xb, %eax
jle 0x7eb9
movl $0x0, -0x10(%rbp)
jmp 0x7b9b
nop