📄 voip.c
字号:
* Function - startVOIP
* Parameters - machineNumber - Set to 1 for machine that has server1IP
* and set to 2 for machine that has server2IP. This is just used
* to configure the UDP server and client so that it knows
* where it is sending messages to (i.e. the other machine).
* Returns - void
* Purpose - This is the main function to get everything going.
* Configures the system, starts tasks, starts UDP stuff,
* brings up audio card.
/**************************************************************************/
void startVOIP( int machineNumber )
{
/* To ensure that taskDelay(1) is 1ms delay */
sysClkRateSet(1000);
/* set 8 bit mono by default */
current_mode = 1;
/*generate the rc4 key once*/
rc4_init(&perm_key,&rc4_key_data,RC4_KEY_DATA_LEN);
/*create semaphores*/
SEM_RECEIVED = semBCreate(SEM_Q_FIFO,SEM_EMPTY);
SEM_OK_TRANSMIT = semBCreate(SEM_Q_FIFO,SEM_EMPTY);
SEM_ENCRYPT = semBCreate(SEM_Q_FIFO,SEM_EMPTY);
SEM_DECRYPTED = semBCreate(SEM_Q_FIFO,SEM_EMPTY);
/*start UDP server*/
taskSpawn("udpServer", 5, 0, 4000, udpServer, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
g_machine_number = machineNumber;
/*set transmit to ip so that we know who we are sending packets to*/
if( machineNumber == 1 )
{
transmitToIp = server2IP;
}
else
{
transmitToIp = server1IP;
}
taskDelay(5000);
/*setup our client*/
createUdpClient(transmitToIp);
/*start up sound card*/
startCS4281();
/*start encrypt task*/
taskSpawn("encryptionTask", 4, 0, 4000, encryptionTask, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
/*start decrypt task*/
taskSpawn("decryptionTask", 4, 0, 4000, decryptionTask, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
/*start record task*/
taskSpawn("getRecordBufferTask", 5, 0, 4000, getRecordBufferTask, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
/*start transmit task*/
taskSpawn("transmitTask", 5, 0, 4000, transmitTask, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
/*start play task*/
taskSpawn("setPlayBufferTask", 5, 0, 4000, setPlayBufferTask, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
}
/**************************************************************************
* Function - stopVOIP
* Parameters - none
* Returns - void
* Purpose - This function stops the DAC and ADC. It shuts down the
* tasks and closes the udp client connection. The rest of the audio
* card is still running and configured so that it start doing its
* job again without configuration changes needed. Call resumeVOIP
* to start VOIP up again after this has been called. Do not call
* startVOIP again because that would try to configure a card that
* has already been configured.
/**************************************************************************/
void stopVOIP()
{
/*stop DAC and ADC*/
stop_dac();
/*shutdown tasks*/
enable_transmit = 0;
enable_play = 0;
enable_record = 0;
enable_encryption_task=0;
enable_decryption_task=0;
/*close udp client connection*/
closeUdpClient();
}
/**************************************************************************
* Function - resumeVOIP
* Parameters - none
* Returns - void
* Purpose - This resumes VOIP after stopVOIP has been called.
* It starts the DAC and ADC, creates a upd client again and
* starts up the tasks.
/**************************************************************************/
void resumeVOIP()
{
/*set task loop variables back so the tasks will run*/
enable_transmit = 1;
enable_play = 1;
enable_record = 1;
enable_encryption_task=1;
enable_decryption_task=1;
/*start DAC and ADC*/
start_dac();
/*create udp client connection*/
createUdpClient(transmitToIp);
/*start encrypt task*/
taskSpawn("encryptionTask", 5, 0, 4000, encryptionTask, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
/*start decrypt task*/
taskSpawn("decryptionTask", 5, 0, 4000, decryptionTask, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
/*start record task*/
taskSpawn("getRecordBufferTask", 5, 0, 4000, getRecordBufferTask, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
/*start transmit task*/
taskSpawn("transmitTask", 5, 0, 4000, transmitTask, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
/*start play task*/
taskSpawn("setPlayBufferTask", 5, 0, 4000, setPlayBufferTask, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
}
/**************************************************************************
* Function - switchMode
* Parameters - int mode - The mode to switch this machine to.
* 1 = 8-bit mono, 2 = 8-bit stereo, 3 = 16-bit stereo
* Returns - void
* Purpose - Switch audio modes. This function can be called at anytime
* on either machine. It will change the mode of this machine and
* send a message to the other machine so it will also switch modes.
/**************************************************************************/
void switchMode(int mode)
{
/*send a request to other machine to switch modes*/
transmitRequest.request_type = 1;
transmitRequest.mode = mode;
udpSendRequest();
/*stop voip on this machine (to minimize noise when switch modes*/
stopVOIP();
/*set global to new mode*/
current_mode = mode;
/*re-configure DMAs, DAC and ADC to handle new mode*/
audio_switchMode();
/*send a request to other machine to start its ADC/DAC back up*/
createUdpClient(transmitToIp);
transmitRequest.request_type = 2;
udpSendRequest();
closeUdpClient();
/*start VOIP up again*/
resumeVOIP();
}
/**************************************************************************
* Function - localSwitchMode
* Parameters - int mode - The mode to switch this machine to.
* 1 = 8-bit mono, 2 = 8-bit stereo, 3 = 16-bit stereo
* Returns - void
* Purpose - This should never be called at a shell window. It should
* be considered a private function that is only called internally
* to the system. It switches the mode on the local machine only,
* so it is called by the udpServer when a message is received
* telling it to switch modes.
/**************************************************************************/
void localSwitchMode(int mode) /* only switches mode locally, not on other machine*/
{
stopVOIP();
current_mode = mode;
audio_switchMode();
}
/**************************************************************************
* Function - setVolume
* Parameters - int value - Number between 0 and 10.
* Returns - void
* Purpose - This is supposed to change the volume, but doesn't work.
/**************************************************************************/
void setVolume(int value)
{
UINT32 new_value = 0;
switch(value)
{
case 0:
new_value = 0x13F3F;
case 1:
new_value = 0x3A3A;
case 2:
new_value = 0x3636;
case 3:
new_value = 0x3232;
case 4:
new_value = 0x2E2E;
case 5:
new_value = 0x2A2A;
case 6:
new_value = 0x2626;
case 7:
new_value = 0x2222;
case 8:
new_value = 0x1E1E;
case 9:
new_value = 0x0D0D;
case 10:
new_value = 0x0;
default:
new_value = 0x0;
}
cs4281_write_ac97(BA0_AC97_MASTER_VOLUME, new_value);
}
/**************************************************************************
* Function - testrc4
* Parameters - none
* Returns - void
* Purpose - Function used to test the rc4 encryption.
/**************************************************************************/
int testrc4()
{
unsigned char key[8] = {23,82,107,6,35,78,88,7};
char buf[8];
rc4_key test;
rc4_key perm;
buf[0] = 'H';
buf[1] = 'e';
buf[2] = 'r';
buf[3] = 'e';
buf[4] = ' ';
buf[5] = 'w';
buf[6] = 'e';
buf[7] = '\0';
printf("before value: %s \n",buf);
rc4_init(&test,&key,8);
printf("x: %i, y: %i, state: %s\n",test.x,test.y,test.state);
perm.x = test.x;
perm.y = test.y;
memmove(perm.state,test.state, 256);
printf("perm x: %i, y: %i, state: %s\n",perm.x,perm.y,perm.state);
rc4_crypt(&test,buf,buf,8);
printf("test a x: %i, y: %i, state: %s\n",test.x,test.y,test.state);
printf("encrypted value: %s \n",buf);
test.x = perm.x;
test.y = perm.y;
memmove(test.state,perm.state, 256);
printf("test b x: %i, y: %i, state: %s\n",test.x,test.y,test.state);
rc4_crypt(&test,buf,buf,8);
printf("decrypted value: %s \n",buf);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -