⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 common.c

📁 基于h323协议的软phone
💻 C
📖 第 1 页 / 共 4 页
字号:
	static char msg[10];
	format[2] = length + '0';
	format[3] = '\0';
	if (val != verify)
	{
		sprintf(msg, format, val);
		WriteLcd(msg, 1, location);
	}
}
#endif
void getTime(int* ttt)
{
		struct timespec tpShow;
if (NULL == ttt)
	{
		return;
	}
	if (clock_gettime(CLOCK_REALTIME, &tpShow) == OK)
	{
		*ttt = tpShow.tv_sec;
	}
}

static void InitPhoneTime()
{
	struct tm sTime;
	struct timespec tpSetTime;
	time_t newTime;

	if (!phoneconfig.conf_sntp.ifsntp)
	{
		sTime.tm_year = 2004 - 1900;
		sTime.tm_mon  = 1 - 1;
		sTime.tm_mday = 1;
		sTime.tm_hour = 0;
		sTime.tm_min  = 0;
		sTime.tm_sec = 0;
		newTime = mktime(&sTime);

		tpSetTime.tv_sec = newTime;
		tpSetTime.tv_nsec = 0;
		if ( clock_settime(CLOCK_REALTIME, &tpSetTime) == OK )
		{            
			phoneconfig.conf_basic.time = newTime;
			/*printf("Init time ok\n");*/
		}
	}
}

extern int *state;		/* 在H323、SIP和MGCP中分别定义 */
int PreserveLcdMessage = FALSE;			/* LCD屏显的内容需要保护 */
struct timespec times = { 0, 100000000};

extern unsigned char gkEnabled;
extern int gatekeeperStatus;
extern unsigned char networkStatus;

/**OK!!!
 *one thread, show default message in LCD
*/
extern int lcdChange;

void ShowDefaultMessageInLCD()
{
	unsigned long start_voice_time = 0;
	unsigned long cur_voice_time = 0;
	int used_time = 0;
	int temp_used_time = 0;
	int hour = 0;
	int min = 0;
	int sec = 0;
	int showFlag = 0;
	int showVoiceTimeFlag = 0;
	struct timespec tpShow;
	struct tm timeBuffer;
	struct tm first;
	char msg1[LCD_WIDTH+1] = "       BP8805       ";
	char msg2[LCD_WIDTH+1];

	int i;
	static unsigned char count_timer = 0;

	InitPhoneTime();

	memset(&first, 0, sizeof(first));
	
	while (TRUE)
	{
		if ((!PreserveLcdMessage) && (!(*menu_inuse)))
		{
#ifdef HC
			if (ST_H323_ON_HOOK_STATE == *state)
#endif
#ifdef SIP
			if (ST_SIP_APP_ON_HOOK == *state)
#endif
#ifdef MGCP
			if (ST_MGCP_IDLE == *state)
#endif			
			{
				if (clock_gettime(CLOCK_REALTIME, &tpShow) == OK)
				{
					if(gmtime_r(&(tpShow.tv_sec), &timeBuffer) == OK)
					{
						if (!showFlag || lcdChange)
						{
							ClearLcd();
							showFlag = 1;
							lcdChange = 0;
						}

#ifdef HC
#ifdef GXM16032
						if ( !networkStatus )
						{
							if ( count_timer < 2 )
								msg1[0] = 0x7c, msg1[1] = 0x7c;
							else
								msg1[0] = ' ', msg1[1] = ' ';
						}
						else if ( gkEnabled && !gatekeeperStatus )
						{
							if ( count_timer < 2 )
								msg1[0] = 0xA1, msg1[1] = 0xC1;
							else
								msg1[0] = ' ', msg1[1] = ' ';
						}
						else
						{
							msg1[0] = ' ', msg1[1] = ' ';
						}
#else

#ifdef INCLUDE_PPPOE_APP
						if((phoneconfig.conf_network.ipType==PPPOEIP)&&!pppoeStatus)
						{
							if(count_timer<2)
								msg1[0]='\5';
							else
								msg1[0]=' ';
						}	
						else
#endif
						{
							if ( !networkStatus )
							{
								if ( count_timer < 2 )
									msg1[0] = '\7';
								else
									msg1[0] = ' ';
							}
							else if ( gkEnabled && !gatekeeperStatus )
							{
								if ( count_timer < 2 )
									msg1[0] = '\6';
								else
									msg1[0] = ' ';
							}
							else
							{
								msg1[0] = ' ';
							}
						}
#endif/*GXM16032*/
#endif/*HC*/						
						WriteLcd( msg1, 0, /*(LCD_WIDTH - strlen(msg1))/2*/0 );
#ifdef GXM16032
						sprintf(msg2, "%04u-%02u-%02u  %02u:%02u:%02u",
#else
						sprintf(msg2, "%04u\01%02u\02%02u\03 %02u:%02u:%02u", /* '\01' = '年', '\02' = '月', '\03' = '日' */
#endif
							timeBuffer.tm_year + 1900,
							timeBuffer.tm_mon + 1,
							timeBuffer.tm_mday,
							timeBuffer.tm_hour,
							timeBuffer.tm_min,
							timeBuffer.tm_sec);
						WriteLcd(msg2, 1, 0);
						
						memcpy(&first, &timeBuffer,sizeof(struct tm));

						start_voice_time = 0;
						showVoiceTimeFlag = 0;
					}
				}
			}
			else 
			{
				showFlag = 0;
#ifdef HC
				if (ST_H323_BIDIR_VOICE_STATE == *state)
#endif
#ifdef SIP
				if (ST_SIP_APP_VOICE == *state)
#endif
#ifdef MGCP
				if (ST_MGCP_ACTIVE == *state)
#endif		
				{
					if (!showVoiceTimeFlag && (!(*menu_inuse)))
					{
						ClearLcd();
					}
					if (start_voice_time)
					{
						cur_voice_time = tickGet();
						/*getTime((int *)&cur_voice_time);
						gmtime_r(&cur_voice_time, &timeBuffer) ;*/
					}
					else
					{
						start_voice_time = tickGet();
						/*getTime((int *)&start_voice_time);
						gmtime_r(&start_voice_time, &timeBuffer) ;*/
						hour = 0;
						min = 0;
						sec = 0;
						used_time = 0;
						continue;
					}
#if 0                                
					if (cur_voice_time < start_voice_time + min * 60 + sec)                                   
					{
						/* printf("time is changed perhaps!\n");*/
						start_voice_time = cur_voice_time - min * 60 - sec - 1;
                                   }
#endif
					temp_used_time = (cur_voice_time - start_voice_time) / 100;
					if (temp_used_time != used_time)
                                   {
						used_time = temp_used_time;
						hour = used_time / 3600;
						min = (used_time % 3600) / 60;
						sec = (used_time % 3600) % 60;
						if (!(*menu_inuse))
						{
							if (showVoiceTimeFlag && !lcdChange)
							{
								sprintf(msg2, " %02u:%02u:%02u", hour, min, sec);
								msg2[9] = '\0';								
								WriteLcd(msg2, 0, 11);
							}
							else
							{
#ifdef HC
								/*sprintf(msg2, "%s", remoteNumber);*/
								strncpy(msg2, remoteNumber, LCD_WIDTH);
								msg2[LCD_WIDTH] = '\0';
#endif
#ifdef SIP
								if (!p2pFlag)
									sprintf(msg2, "%s", recNum);
								else
								{
									ConvertInt2CharNum(recNum,  receive, 2);
									sprintf(msg2, "%s", recNum);
								}
#endif
#ifdef MGCP
								sprintf(msg2, "%s", remoteNum);
#endif
								if (remoteName[0] == '\0')
								{
									strcpy(remoteName, "Unknown");
								}
								LcdLowerShow(remoteName, msg2);
								sprintf(msg2, " %02u:%02u:%02u", hour, min, sec);
								msg2[9] = '\0';								
								WriteLcd(msg2, 0, 11);
								
								showVoiceTimeFlag = 1;
								lcdChange= 0;
							}
						}
					}
				}
			}
		}
		else
		{
			showVoiceTimeFlag = 0;
			showFlag = 0;
#ifdef HC
			if (ST_H323_BIDIR_VOICE_STATE != *state)
#endif
#ifdef SIP
			if (ST_SIP_APP_VOICE != *state)
#endif
#ifdef MGCP
			if (ST_MGCP_ACTIVE != *state)
#endif
			{
				start_voice_time = 0;
			}
		}		
		nanosleep(&times, NULL);
		++count_timer;
		count_timer %= 4;
	}
}

extern HAPP hApp;
void sendURQTask()
{
	AppSendWrapper(WP_SEND_UNREGISTER, -1);
}

#ifdef INCLUDE_PPPOE_APP
void PPPoeOff()
{
	if(*state==ST_H323_BIDIR_VOICE_STATE)
		AppTerminateAction(ST_H323_BIDIR_VOICE_STATE);

	if (gkEnabled)
	{
		if (SpawnTask( "tSendURQTask", (FUNCPTR) sendURQTask, FALSE, 100, 10*1024 ) == ERROR)
		{
			printf("H323Reboot: Unable to spawn URQ sending task\n");
		}
	}	
	taskDelay(50);
	pppoeDynamicOFF();
}
#endif

int SntpTimeGet()
{
	/* setting date/time from SNTP server */
	char stg[32];
	stg[0] = '\0';
	/* See if we have an SNTP Server entered */
	if (phoneconfig.conf_sntp.ifsntp)
	{
		ConvertInt2CharNum(stg, phoneconfig.conf_sntp.serverip, 2);
		if( strcmp( stg, "0.0.0.0" ) != 0 )
		{
			struct timespec ts;
			unsigned int sz;
			char tz[32];

			/* See if time zone correction factor is set (in hours) */
			long gmt = phoneconfig.conf_sntp.timezone;

			/* Get the time */
			if (sntpcTimeGet( stg, 1000, &ts) == OK)
			{
				/* correct for time zone, must convert GMT time in hours -> secs */
				ts.tv_sec = ts.tv_sec + (gmt * 60 * 60);
				sz = sizeof(stg) - 1;
				clock_settime( CLOCK_REALTIME, &ts );
				ctime_r( &ts.tv_sec, stg, &sz );
				fprintf( stderr, "SNTP Server time: %s\n", stg );
				return OK;
			}
			else
			{
				fprintf( stderr, "H323Init: Unable to retrieve time from SNTP Server @ %s\n", stg );
			}
		}
	}
	return ERROR;
}

void doSntp()
{
	NonWrapperMsg* msg;
	semTake(sntpSem, WAIT_FOREVER);
	if (SntpTimeGet() == OK)
		sntp_timer = CreateTimer(SNTP_RECFG_TIMER);
	else
		sntp_timer = CreateTimer(ONE_SEC * 30);
	semGive(sntpSem);
	if((msg=(NonWrapperMsg *)MQBufGet()) == NULL)
	{
		return;
	}
	msg->event = null_event;
	msg->digit = 0;
	if (MQSend((char*)msg, NO_WAIT) == -1)
	{
		MQBufFree((char*)msg);
		return;
	}	
}


int ConfigureSntpTime()
{
	if (sntp_timer != -1)
		DeleteTimer(&sntp_timer);
	if (SpawnTask( "tSntpTask", (FUNCPTR) doSntp, FALSE, 100, 1024*10 ) == ERROR)
	{
		printf("Unable to spawn sntp time get task\n");
	}	
}


#ifdef INCLUDE_NAT_APP

#include <vxWorks.h>
#include "wrn/nat/natExtCalls.h"
#include "wrn/nat/natMgmt.h"

void natON(IfType type)
{
	char tempIPAddr[INET_ADDR_LEN+1];

	if (phoneconfig.conf_network.natlocalip == 0x0
		|| phoneconfig.conf_network.natlocalmask == 0x0)
	{
		printf("Nat local ipaddr or submask == 0.0.0.0, do not turn nat on\n");
		return;
	}
	ConvertInt2CharNum(tempIPAddr, phoneconfig.conf_network.natlocalip, 2);
	switch (type)
	{
		case IF_TYPE_PPPOE:
			ifMaskSet(LOCAL_IF0, phoneconfig.conf_network.natlocalmask);
			ifAddrSet(LOCAL_IF0, tempIPAddr);
			break;
		case IF_TYPE_NAT:
			if (!muxDevExists("nat", 2))
			{
				natIfLoad();
			}
			ipAttach(2,"nat");
			ifMaskSet("nat2", phoneconfig.conf_network.natlocalmask);
			ifAddrSet("nat2", tempIPAddr);
			natIfAddrSet(phoneconfig.conf_network.natlocalip);
			natIfMaskSet(phoneconfig.conf_network.natlocalmask);
			natIfStart();
			break;
		default:
			return;
	}

	natcfg(type);
	if (natInit() == ERROR)
		printf("NAT initialization failed!\n");
	/* Initialize selected ALGs. */
	/* NAT FTP ALG: */
	if( natFtpInit(21) != OK ) 
		printf("NAT FTP ALG initialization failed!\n"); 

	/* NAT H323 ALG: */
#if 0
	if( natH323Init(1720) != OK ) 
		printf("NAT H323 ALG initialization failed!\n"); 
#endif
	/* NAT IPSec passthrough ALG */
	if( natIPSecPTInit(500) != OK ) 
		printf("NAT NAT IPSec passthrough ALG initialization failed!\n"); 

	/* NAT PPTP passthrough ALG */
	if( natPPTPPTInit() != OK ) 
		printf("NAT NAT PPTP passthrough ALG initialization failed!\n"); 

	/* NAT MIB: */
	natMgmtInit(); 
#if 0
	if (m2NatTreeAdd () == ERROR) 
		printf("Unable to add MIB fragment for NAT to tree!\n"); 
#endif
	printf("NAT configuration and initialization complete!\n");
}

void natOFF(IfType type)
{
	int id;
	id = taskNameToId("tNAT");
	if (taskIdVerify(id) == OK)
		nat_cleanup();		
	else
		printf("nat is off, need not clean\n");
	
	if (type == IF_TYPE_NAT)
	{		
		if (muxDevExists("nat", 2))
		{
			ifRouteDelete("nat", 2);
			ipDetach(2, "nat");
			natIfStop();
		}
	}
	printf("Turn nat off\n");	
}

#endif /* INCLUDE_NAT_APP */

#define TASK_DELAY_ONE_SEC	sysClkRateGet()

/* 模拟按键声音并在LCD上显示号码 */
void PlayRedialTone(char* key, int row)
{
	char tempKey[LCD_WIDTH+1];
	int i;
	dtmf_tone_type  tone;
	lcdCursorPos    pos;	
	pos.row = row;
	pos.col = 0;
	lcdSetCurPos( pos );

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -