// File: /workspace/github_cpp_makefile_star2_O3/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb8, %rsp
movups 0x9974(%rip), %xmm0     # 0xfc0c
leaq -0x50(%rbp), %rax
movups %xmm0, 0xc(%rax)
movaps 0x9959(%rip), %xmm0     # 0xfc00
movaps %xmm0, (%rax)
xorps %xmm0, %xmm0
leaq -0x90(%rbp), %rax
movaps %xmm0, 0x10(%rax)
movaps %xmm0, (%rax)
movaps %xmm0, 0x20(%rax)
movups %xmm0, 0x2b(%rax)
movb $-0x67, 0x15(%rax)
xorl %r14d, %r14d
xorl %ebx, %ebx
movl 0xf106(%rip), %eax      # 0x153d8
testl %eax, %eax
jle 0x6330
movl %r14d, %r13d
andl $0x80000001, %r13d      # imm = 0x80000001
movl $0x74, %r15d
movq 0xf0e3(%rip), %rcx      # 0x153d0
xorl %r12d, %r12d
cmpl $0x1, %r13d
sete %dl
cmpl $0x0, (%rcx,%r15)
setg %sil
andb %dl, %sil
cmpb $0x1, %sil
jne 0x631e
movl %r12d, %edi
callq 0x6512
movq 0xf0b8(%rip), %rcx      # 0x153d0
movl 0xf0ba(%rip), %eax      # 0x153d8
incq %r12
movslq %eax, %rdx
addq $0x90, %r15
cmpq %rdx, %r12
jl 0x62f0
movl 0xf0ca(%rip), %edi      # 0x15400
movl $0x50, %edx
leaq -0xe0(%rbp), %rsi
movl $0x40, %ecx
callq 0x40d0
testl %r14d, %r14d
je 0x6373
movl $0x13880, %edi          # imm = 0x13880
callq 0x4450
cmpl $0xb, %r14d
leal 0x1(%r14), %eax
movl $0x0, %ecx
cmovgel %ecx, %eax
movl %eax, %r14d
jmp 0x62cc
cmpl $0x0, 0xf05e(%rip)      # 0x153d8
jle 0x650b
movq 0xf049(%rip), %rcx      # 0x153d0
xorl %r12d, %r12d
leaq (%r12,%r12,8), %r15
shlq $0x4, %r15
movl 0x28(%rcx,%r15), %edx
movl 0x30(%rcx,%r15), %eax
leal 0x1(%rdx), %esi
movl %esi, 0x28(%rcx,%r15)
cmpl %eax, %edx
jl 0x63b8
leaq (%rcx,%r15), %rdx
movl %eax, 0x28(%rdx)
movl $0x0, 0x38(%rcx,%r15)
incl 0x2c(%rcx,%r15)
cmpl $0x1, 0xf044(%rip)      # 0x15408
jne 0x63e0
movl $0x1, %edi
callq 0x4450
cmpl $0x1, 0xf031(%rip)      # 0x15408
je 0x63c6
movq 0xeff0(%rip), %rcx      # 0x153d0
movl 0x34(%rcx,%r15), %eax
cmpl %eax, 0x2c(%rcx,%r15)
jle 0x63fc
leaq (%rcx,%r15), %rdx
movl %eax, 0x2c(%rdx)
movl $0x0, 0x3c(%rcx,%r15)
incq %r12
movslq 0xefd2(%rip), %rax      # 0x153d8
cmpq %rax, %r12
jl 0x638a
incl %ebx
imull $0xcccccccd, %ebx, %ecx # imm = 0xCCCCCCCD
rorl $0x2, %ecx
cmpl $0xccccccd, %ecx        # imm = 0xCCCCCCD
setae %cl
testl %eax, %eax
setle %dl
orb %cl, %dl
jne 0x6351
movq 0xef99(%rip), %r8       # 0x153d0
xorl %r12d, %r12d
leaq (%r12,%r12,8), %rcx
shlq $0x4, %rcx
cmpl $0x0, 0x44(%r8,%rcx)
je 0x64f7
addq %r8, %rcx
cmpl $0x0, 0x3c(%rcx)
jne 0x64f7
cmpl $0x1, 0xefa2(%rip)      # 0x15404
jne 0x647e
movl $0x1, %edi
callq 0x4450
cmpl $0x1, 0xef8f(%rip)      # 0x15404
je 0x6464
movq 0xef52(%rip), %r8       # 0x153d0
movl $0x1, 0xef7c(%rip)      # 0x15404
movl 0xef6e(%rip), %edi      # 0x153fc
movq %r12, %rax
shlq $0x4, %rax
leaq (%rax,%rax,8), %r15
addq %r15, %r8
movl $0x3b, %edx
leaq -0x90(%rbp), %rsi
xorl %ecx, %ecx
movl $0x10, %r9d
callq 0x4060
movl 0xef45(%rip), %edi      # 0x15400
movq 0xef0e(%rip), %rax      # 0x153d0
leaq (%rax,%r15), %r8
addq $0x10, %r8
movl $0x1c, %edx
leaq -0x50(%rbp), %rsi
xorl %ecx, %ecx
movl $0x10, %r9d
callq 0x4060
movl $0x0, 0xef1a(%rip)      # 0x15404
movq 0xeedf(%rip), %r8       # 0x153d0
movl 0xeee1(%rip), %eax      # 0x153d8
incq %r12
movslq %eax, %rcx
cmpq %rcx, %r12
jl 0x643a
jmp 0x6351
incl %ebx
jmp 0x6351