#include	<stdio.h>
#include	<string.h>
#include	<netinet/in.h>
#include	<sys/ipc.h>
#include	<sys/msg.h>
#include	<mqueue.h>
#include	"dsgwd_pi.h"
#include	"node.h"

char	headerRead;
char	header_gen;
char	SendHeader;
char	SendToInet;
char	PTT_On;

unsigned int	Packet_ID;
extern	struct usb_dev_handle *udev;	
extern	mqd_t	rf_qid;
extern	mqd_t	inet_qid;

char	ReSyncFrame[3] = {0x55, 0x2d, 0x16};
char	NullVoice0[12] = {0x9e,0x8d,0x32,0x88,0x26,0x1a,0x3f,0x61,0xe8,0x55,0x2d,0x16};
char	NullVoice1[12] = {0x9e,0x8d,0x32,0x88,0x26,0x1a,0x3f,0x61,0xe8,0xe7,0x84,0x76};
char	NullVoice_ID31[9] = {0xb2,0x4d,0x22,0x48,0xc0,0x16,0x28,0x26,0xc8};
char	LastFrame0[6] = {0x55,0x55,0x55,0x55,0xc8,0x7a};
char	LastFrame1[6] = {0x55,0x55,0x55,0x55,0x55,0x55};
char	SlowData[6];
char	voice[12];
char	header[41];
char 	LastFrameCheckRx[6];
char 	LastFrameCheckTx[6];
unsigned char	Packet_Seq;
unsigned char	Packet_Seq_Save;
char	COS_On;


char	JitterBuffer[JitterBufferSize][12];		
char	JitterBufferPnt;
char	CurJitterBufferPnt;
char	VoicePacketSW;
unsigned char	SequenceTemp;
int	voice_pnt = 0;


void	*node_adapter(void *arg)
{
        int     ret;
        int     wd;
        fd_set  read_USB, Mask_USB;
        struct  timeval timeout_usb;


	init_usb();
	headerRead = FALSE;
	header_gen = FALSE;
	SendHeader = FALSE;
	SendToInet = FALSE;
	COS_On = FALSE;
	PTT_On = FALSE;
	VoicePacketSW = FALSE;
	Packet_Seq = 0;
	voice_pnt = 0;

        FD_ZERO(&Mask_USB);
        FD_SET(inet_qid, &Mask_USB);
        wd = inet_qid + 1;

	while (1)
	{
		read_USB = Mask_USB;
		timeout_usb.tv_sec = 0;
		timeout_usb.tv_usec = 10000;
		switch (select(wd, (fd_set *)&read_USB, NULL, NULL, &timeout_usb))
		{
			case -1:
			case 0:
				break;
			default:
				if (FD_ISSET (inet_qid, &read_USB))
				{
					from_inet();
				}
				break;
		} 
		from_rig();
	}
}
