// File: /workspace/github_cpp_makefile_star2_O2/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 0x8213(%rip), %xmm0     # 0xde7c
leaq -0x50(%rbp), %rax
movups %xmm0, 0xc(%rax)
movaps 0x81f8(%rip), %xmm0     # 0xde70
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)
pushq $0x1
popq %rbx
xorl %r12d, %r12d
movl $0x0, -0x2c(%rbp)
movl %r12d, %r15d
andl $0x80000001, %r15d      # imm = 0x80000001
movl 0xe743(%rip), %eax      # 0x143f8
pushq $0x74
popq %r13
xorl %r14d, %r14d
movslq %eax, %rcx
cmpq %rcx, %r14
jge 0x5cfb
cmpl $0x1, %r15d
sete %cl
movq 0xe71e(%rip), %rdx      # 0x143f0
cmpl $0x0, (%rdx,%r13)
setg %dl
andb %cl, %dl
cmpb $0x1, %dl
jne 0x5cef
movl %r14d, %edi
callq 0x5e7b
movl 0xe709(%rip), %eax      # 0x143f8
incq %r14
addq $0x90, %r13
jmp 0x5cbc
movl 0xe71f(%rip), %edi      # 0x14420
leaq -0xe0(%rbp), %rsi
pushq $0x50
popq %rdx
pushq $0x40
popq %rcx
callq 0x40c0
testl %r12d, %r12d
jne 0x5dab
xorl %r14d, %r14d
movslq 0xe6d2(%rip), %rcx      # 0x143f8
cmpq %rcx, %r14
jge 0x5d98
movq 0xe6be(%rip), %rax      # 0x143f0
imulq $0x90, %r14, %r15
movl 0x28(%rax,%r15), %edx
movl 0x30(%rax,%r15), %ecx
leal 0x1(%rdx), %esi
movl %esi, 0x28(%rax,%r15)
cmpl %ecx, %edx
jl 0x5d5c
leaq (%rax,%r15), %rdx
movl %ecx, 0x28(%rdx)
andl $0x0, 0x38(%rax,%r15)
incl 0x2c(%rax,%r15)
cmpl $0x1, 0xe6c0(%rip)      # 0x14428
jne 0x5d73
movl %ebx, %edi
callq 0x4490
jmp 0x5d61
movq 0xe676(%rip), %rax      # 0x143f0
movl 0x34(%rax,%r15), %ecx
cmpl %ecx, 0x2c(%rax,%r15)
jle 0x5d93
leaq (%rax,%r15), %rdx
movl %ecx, 0x2c(%rdx)
andl $0x0, 0x3c(%rax,%r15)
incq %r14
jmp 0x5d1f
movl -0x2c(%rbp), %eax
incl %eax
movl %eax, -0x2c(%rbp)
xorl %edx, %edx
pushq $0x14
popq %rsi
divl %esi
testl %edx, %edx
je 0x5dce
movl $0x13880, %edi          # imm = 0x13880
callq 0x4490
cmpl $0xb, %r12d
leal 0x1(%r12), %eax
movl $0x0, %ecx
cmovgel %ecx, %eax
movl %eax, %r12d
jmp 0x5ca5
xorl %r14d, %r14d
movslq %ecx, %rax
cmpq %rax, %r14
jge 0x5dab
movq 0xe610(%rip), %rax      # 0x143f0
imulq $0x90, %r14, %r15
cmpl $0x0, 0x44(%rax,%r15)
je 0x5e73
addq %r15, %rax
cmpl $0x0, 0x3c(%rax)
jne 0x5e73
jmp 0x5e05
movl %ebx, %edi
callq 0x4490
cmpl $0x1, 0xe618(%rip)      # 0x14424
je 0x5dfe
movl $0x1, 0xe60c(%rip)      # 0x14424
movl 0xe5fe(%rip), %edi      # 0x1441c
movq 0xe5cb(%rip), %r8       # 0x143f0
addq %r15, %r8
leaq -0x90(%rbp), %rsi
pushq $0x3b
popq %rdx
xorl %ecx, %ecx
pushq $0x10
popq %r13
movl %r13d, %r9d
callq 0x4050
movl 0xe5da(%rip), %edi      # 0x14420
movq 0xe5a3(%rip), %rax      # 0x143f0
leaq (%rax,%r15), %r8
addq $0x10, %r8
leaq -0x50(%rbp), %rsi
pushq $0x1c
popq %rdx
xorl %ecx, %ecx
movl %r13d, %r9d
callq 0x4050
andl $0x0, 0xe5b7(%rip)      # 0x14424
movl 0xe585(%rip), %ecx      # 0x143f8
incq %r14
jmp 0x5dd1