📄 testdrv.c
字号:
/*************************************************************************
* File name : testdrv.c
* Subsystem : modem卡驱动程序
* Target env : Linux
* Author : 谢红伟
* Last modified : 2002/04/30
* Description : This file contains the functions for test modem driver
* Copyright : zjkj
* Note :
**************************************************************************/
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <asm/io.h>
#include <sys/perm.h>
#include <pthread.h>
#include <sys/wait.h>
#include <time.h>
#include <sys/timeb.h>
#include <newt.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "pubfunc.h"
#include "zjmdmfuc.h"
#include "testdrv.h"
/*********************** Goble Variable *****************************/
BOOL runing_sigfunc;
/**********************************************
* 信号处理函数
**********************************************/
void HandleSignal(int sig_num)
{
int i,j;
char mdm_file[32];
int fd_mdm;
if(runing_sigfunc)
{
pthread_exit(NULL);
return;
}
runing_sigfunc = TRUE;
/*
for (i=0; i<thread_count; i++)
{
pthread_cancel(thread_id[i]);
}
*/
//close modem and chl
Log(NULL,"Recv system signal %d. Exiting system...",sig_num);
for (i=0; i<MDM_NUM; i++)
{
if (mdmarg[i].mdm_no < 0) continue;
for (j=0; j<CHL_NUM; j++)
{
if (mdmarg[i].chl_no[j]<0 || strlen(mdmarg[i].phone[j])<=0) continue;
sprintf(mdm_file,"%s%d",ZJMDM_NAME,mdmarg[i].mdm_no);
fd_mdm=open(mdm_file,O_RDWR|O_NONBLOCK);
if (fd_mdm > 1)
{
ioctl(fd_mdm,IOCTL_CMD_DISCONN |(j<<4));
usleep(100*1000);
//关闭通道
ioctl(fd_mdm,IOCTL_CMD_SHUTCH |(j<<4));
//close modem file
close(fd_mdm);
}
}
}
signal(sig_num, HandleSignal);
raise(SIGKILL);
//kill(-1,SIGKILL);
//abort();
//exit(0);
}
/******************************************************************
*打开modem文件
*******************************************************************/
int OpenMdm(U8 mdm_no)
{
int fd;
char mdmname[32];
sprintf(mdmname,"%s%d",ZJMDM_NAME,mdm_no);
fd = open(mdmname,O_RDWR|O_NONBLOCK);
// fd = open(mdmname,O_RDWR);
if(fd<0)
{
Log(NULL,"Fail : open file %s",mdmname);
return -1;
}
return fd;
}
/******************************************************************
* DESC : 产生测试数据块
******************************************************************/
void GenerateTestDataBlock(char *out_buf,int bytes)
{
int i,j,bytes_count = 0;
for (j=0;bytes_count<bytes;j++)
{
for (i=0; i<0xff; i++)
{
out_buf[bytes_count++] = i;
if (bytes_count>=bytes) break;
}
}
return;
}
/**************************************
* 保存测试结果
**************************************/
void SaveTestReult(t_testinfo *ptestinfo)
{
char res_filename[16];
char log_str[512];
time_t spend_time;
memset(res_str,0,sizeof(res_str));
memset(res_filename,0,sizeof(res_filename));
ptestinfo->end_time = time(NULL);
spend_time = ptestinfo->end_time - ptestinfo->start_time;
sprintf(res_filename,"res_%d_%d.txt",ptestinfo->mdm_no,ptestinfo->chl_no);
sprintf(log_str,"\r\n\t****** Modem %d channel %d communicate result ******\r\n",ptestinfo->mdm_no,ptestinfo->chl_no);
strcat(res_str,log_str);
sprintf(log_str,"\tData block\t:\t%d\tblock\r\n",2*ptestinfo->block_no);
strcat(res_str,log_str);
sprintf(log_str,"\tBytes number\t:\t%d\tbytes\r\n",2*ptestinfo->block_no*BLOCK_SIZE);
strcat(res_str,log_str);
sprintf(log_str,"\tWrong number\t:\t%d\tblock\r\n",ptestinfo->err_times);
strcat(res_str,log_str);
sprintf(log_str,"\tSpend time\t:\t%d\tseconds\r\n",spend_time);
strcat(res_str,log_str);
sprintf(log_str,"\tSuccess rate\t:\t%.2f\t%%\r\n",(1-(float)ptestinfo->err_times/(2*ptestinfo->block_no))*100);
strcat(res_str,log_str);
sprintf(log_str,"\tTransmit speed\t:\t%.2f\tbps\r\n",(float)2*ptestinfo->block_no*BLOCK_SIZE/spend_time);
strcat(res_str,log_str);
sprintf(log_str,"\t********************* END *********************\r\n");
strcat(res_str,log_str);
WriteToFile(res_filename,res_str,strlen(res_str));
}
/***********************************************
* 发送数据包
**********************************************/
BOOL SendPacket(char *Write_buf,long size,t_TdkMdmChl *p_tdk)
{
char wtc_buf[BLOCK_SIZE+LEADING_LEN+1];
#ifdef _DEBUG
Log(p_tdk,"Sending %d bytes data",size);
#endif
memset(wtc_buf,0xff,LEADING_LEN);
memcpy(wtc_buf+LEADING_LEN,Write_buf,size);
if(zj_write(p_tdk->fd,wtc_buf,size+LEADING_LEN,p_tdk->chl_no,
MAX_SEND_TIMEOUT)!= size+LEADING_LEN)
{
Log(p_tdk,"Fail : send %d bytes data error!",size);
return FALSE;
}
else
return TRUE;
}
/*************************************************************
* 接收数据包函数
**************************************************************/
int RecvPacket(char *Read_buf,int size, int is_new_pkt,t_TdkMdmChl *p_tdk)
{
int result,FindLeadingNum = 0;
time_t wait_start,recv_start;
unsigned char tmp_buf[1*KILO];
int valid_bytes = 0;
unsigned char findLeadingChar = FALSE;
unsigned char isValidData = FALSE;
int count,i = 0;
time(&recv_start);
time(&wait_start);
memset(tmp_buf,0,sizeof(tmp_buf));
#ifdef _DEBUG
Log(p_tdk,"Receiving %d bytes data...",size);
#endif
while (valid_bytes < size)
{
count = zj_read(p_tdk->fd,tmp_buf,size - valid_bytes,p_tdk->chl_no);
//Log(p_tdk,"Recv %d bytes. valid bytes = %d. size = %d bytes.",count,valid_bytes,size);
if (count <= 0)
{
if (JudgeTimeOut(wait_start,MAX_WAIT_PACKET_TIME) == IS_TIMEOUT)
{
Log(p_tdk,"Fail : wait data time out!Recv leading char number %d",FindLeadingNum);
return FALSE;
}
usleep(1000);
continue;
}
i = 0;
if (is_new_pkt == TRUE)
{
if (findLeadingChar == FALSE)
{
if (JudgeTimeOut(wait_start,MAX_WAIT_PACKET_TIME) == IS_TIMEOUT)
{
Log(p_tdk,"Fail : wait data time out!Recv leading char number %d",FindLeadingNum);
return FALSE;
}
for (i = 0; i<count; i++)
{
if (tmp_buf[i] == 0xff)
{
FindLeadingNum++;
if(FindLeadingNum>=LEADING_LEN/2) //要保证至少收到一半的前导字符
{
findLeadingChar = TRUE;
break;
}
}
else
{
FindLeadingNum = 0;
}
}
if (i >= count) continue;
}
if (isValidData == FALSE)
{
for (; i<count; i++)
{
if (tmp_buf[i] != 0xff)
{
isValidData = TRUE;
break;
}
}
if (i >= count) continue;
}
}
//执行到此已经能获得有效数据
memcpy(&Read_buf[valid_bytes], &tmp_buf[i], count - i);
valid_bytes += (count - i);
if (valid_bytes < size) msleep(1);
else break;
if (JudgeTimeOut(recv_start,MAX_RECV_PACKET_TIME) == IS_TIMEOUT)
{
err_times++;
Log(p_tdk,"Fail : received packet timeout!Recv leading char number %d",FindLeadingNum);
if (err_times > MAX_ERROR_ALLOWED)
{
Log(p_tdk,"Fail : error times is more than max error times!");
}
return FALSE;
}
}
#ifdef _DEBUG
Log(p_tdk,"Received %d bytes valid data.",valid_bytes);
#endif
return valid_bytes;
}
/******************************************************
* 多次通信测试函数
******************************************************/
BOOL MultTest(t_TdkMdmChl *p_tdk, int in_test_times)
{
char rdc_buf[BLOCK_SIZE];
int count ,i,times;
t_testinfo testinfo;
memset(&testinfo,0,sizeof(testinfo));
testinfo.mdm_no = p_tdk->mdm_no;
testinfo.chl_no = p_tdk->chl_no;
if (cs_type == SERVER)
{
for (times=0; times<in_test_times; times++)
{
Log(p_tdk,"*********Start %dth times test... *********",times+1);
Log(p_tdk,"Listening...");
for (i=0; i<READ_STATE_TIME; i++)
{
if (zj_GetCHState(p_tdk->fd,p_tdk->chl_no)!=CMD_CONN_SUCCESS)
sleep(1);
else break;
}
if (i>=READ_STATE_TIME)
{
zj_DisconnCH(p_tdk->fd,p_tdk->chl_no);
Log(p_tdk,"Fail : read call state timeout!");
continue;
}
Log(p_tdk,"Establish connect!");
zj_CorrectSysTime(p_tdk->fd);
testinfo.start_time = time(NULL);
for (i=0; i<BLOCK_NUM; i++)
{
//pthread_mutex_lock(&mutex_r);
testinfo.block_no = i+1;
if ((count = RecvPacket(rdc_buf,BLOCK_SIZE,TRUE,p_tdk))==FALSE)
{
testinfo.err_times++;
Log(p_tdk,"Sleep %d seconds",COMM_FAIL_WAIT_TIME);
sleep(COMM_FAIL_WAIT_TIME);
//pthread_mutex_unlock(&mutex_r);
break;
}
//pthread_mutex_unlock(&mutex_r);
if (CompBuf(p_tdk,rdc_buf,test_data_buf,count) == FALSE)
{
testinfo.err_times++;
Log(p_tdk,"Fail : NO.%d block of %d bytes is wrong!!!",testinfo.block_no,BLOCK_SIZE);
}
else
{
Log(p_tdk,"NO.%d block of %d bytes is OK!",testinfo.block_no,BLOCK_SIZE);
}
if(zj_clean_read_buffer(p_tdk->fd,p_tdk->chl_no))
{
testinfo.err_times++;
Log(p_tdk,"Sleep %d seconds",COMM_FAIL_WAIT_TIME);
sleep(COMM_FAIL_WAIT_TIME);
//pthread_mutex_unlock(&mutex_w);
break;
}
//CleanReadBuf(p_tdk->fd,p_tdk->chl_no);
//pthread_mutex_lock(&mutex_w);
if ((count = SendPacket(test_data_buf, BLOCK_SIZE,p_tdk)) == FALSE)
{
testinfo.err_times++;
Log(p_tdk,"Sleep %d seconds",COMM_FAIL_WAIT_TIME);
sleep(COMM_FAIL_WAIT_TIME);
//pthread_mutex_unlock(&mutex_w);
break;
}
//pthread_mutex_unlock(&mutex_w);
SaveTestReult(&testinfo);
}
Log(p_tdk, "Sleep %d seconds for wait client received all data",BLOCK_SIZE/80);
sleep(BLOCK_SIZE/80);
zj_ResetCH(p_tdk->fd,p_tdk->chl_no);
sleep(1);
zj_DisconnCH(p_tdk->fd,p_tdk->chl_no);
}
}
else
{
for (times=0; times<in_test_times; times++)
{
Log(p_tdk,"*********Start %dth times test... *********",times+1);
zj_CallOut(p_tdk->fd,p_tdk->chl_no,p_tdk->phone);
for (i=0; i<READ_STATE_TIME; i++)
{
if (zj_GetCHState(p_tdk->fd,p_tdk->chl_no)!=CMD_CONN_SUCCESS)
sleep(1);
else break;
}
if (i>=READ_STATE_TIME)
{
zj_DisconnCH(p_tdk->fd,p_tdk->chl_no);
Log(p_tdk,"Fail : read state timeout after call out!");
Log(p_tdk,"Sleep %d seconds",COMM_FAIL_WAIT_TIME);
sleep(COMM_FAIL_WAIT_TIME);
continue;
}
Log(p_tdk,"Establish connect!");
testinfo.start_time = time(NULL);
for (i=0; i<BLOCK_NUM; i++)
{
testinfo.block_no = i+1;
//pthread_mutex_lock(&mutex_w);
if ((count = SendPacket(test_data_buf, BLOCK_SIZE,p_tdk)) == FALSE)
{
Log(p_tdk,"Sleep %d seconds",COMM_FAIL_WAIT_TIME);
sleep(COMM_FAIL_WAIT_TIME);
testinfo.err_times++;
//pthread_mutex_lock(&mutex_w);
break;
}
//pthread_mutex_unlock(&mutex_w);
//pthread_mutex_lock(&mutex_r);
sleep(1);
if ((count = RecvPacket(rdc_buf,BLOCK_SIZE,TRUE,p_tdk))==FALSE)
{
Log(p_tdk,"Sleep %d seconds",COMM_FAIL_WAIT_TIME);
sleep(COMM_FAIL_WAIT_TIME);
testinfo.err_times++;
//pthread_mutex_unlock(&mutex_r);
break;
}
//pthread_mutex_unlock(&mutex_r);
if (CompBuf(p_tdk,rdc_buf,test_data_buf,count) == FALSE)
{
testinfo.err_times++;
Log(p_tdk,"Fail : NO.%d block of %d bytes is wrong!!!",testinfo.block_no,BLOCK_SIZE);
}
else
{
Log(p_tdk,"NO.%d block of %d bytes is OK!",testinfo.block_no,BLOCK_SIZE);
}
if(zj_clean_read_buffer(p_tdk->fd,p_tdk->chl_no))
{
testinfo.err_times++;
Log(p_tdk,"Sleep %d seconds",COMM_FAIL_WAIT_TIME);
sleep(COMM_FAIL_WAIT_TIME);
//pthread_mutex_unlock(&mutex_w);
break;
}
SaveTestReult(&testinfo);
}
zj_ResetCH(p_tdk->fd,p_tdk->chl_no);
sleep(1);
zj_DisconnCH(p_tdk->fd,p_tdk->chl_no);
Log(p_tdk,"Test again after sleep %d seconds",COMM_OK_WAIT_TIME);
sleep(COMM_OK_WAIT_TIME);
}
}
Log(p_tdk,"%s",res_str);
Log(p_tdk,"I should exit after sleep %d seconds...",COMM_OK_WAIT_TIME);
sleep(COMM_OK_WAIT_TIME);
return TRUE ;
}
/**********************************************
* 线程入口函数
**********************************************/
void *ThreadOptChl(void *ptr)
{
t_TdkMdmChl *p_tdk;
p_tdk=(t_TdkMdmChl*)ptr;
#ifdef _DEBUG
Log(p_tdk,"Thread for channel %d-%d start.",p_tdk->mdm_no,p_tdk->chl_no);
#endif
zj_OpenCH(p_tdk->fd,p_tdk->chl_no);
if (MultTest(p_tdk,comm_times))
Log(p_tdk,"Test successfully!");
else
Log(p_tdk,"Test fail!");
zj_ShutCH(p_tdk->fd,p_tdk->chl_no);
close(p_tdk->fd);
#ifdef _DEBUG
Log(p_tdk,"Thread for channel %d-%d exit...",p_tdk->mdm_no,p_tdk->chl_no);
#endif
return NULL;
}
/********** Main Function *****************/
int main()
{
int i,j,count;
int mdm_fd;
int result,err;
t_TdkMdmChl tdk[MDM_NUM][CHL_NUM];
/* 初始化 */
pthread_mutex_init(&mutex_r,NULL);
pthread_mutex_init(&mutex_w,NULL);
pthread_mutex_init(&mutex_c,NULL);
system("rm *.log *.res core *.send *.recv -f");
system("clear");
thread_count = 0;
count = 0;
err_times = 0;
runing_sigfunc = FALSE;
specifytty(1); //Set modem drive print console to 1
/* 设置 */
memset((char*)&mdmarg[0],-1,sizeof(t_MdmArg)*MDM_NUM);
mdm_num = MultChlSetup(mdmarg,&comm_times,&cs_type);
if(mdm_num <= 0) exit(0);
/* 捕获系统信号*/
for (i=0; i<20; i++)
signal(i, HandleSignal);
/* ignore the hangup signal */
signal(SIGHUP, SIG_IGN);
signal(SIGCHLD, SIG_IGN);
/*产生测试数据*/
GenerateTestDataBlock(test_data_buf,BLOCK_SIZE);
/* Create pthread of communicate */
for (i=0; i<MDM_NUM; i++)
{
if(mdmarg[i].mdm_no<0) continue;
mdm_fd = OpenMdm(i);
if(mdm_fd<0) continue;
for (j=0; j<CHL_NUM; j++)
{
if (mdmarg[i].chl_no[j]<0 || strlen(mdmarg[i].phone[j])<=0) continue;
tdk[i][j].mdm_no = mdmarg[i].mdm_no;
tdk[i][j].chl_no = mdmarg[i].chl_no[j];
tdk[i][j].fd = mdm_fd;
strcpy(tdk[i][j].phone,mdmarg[i].phone[j]);
result = pthread_create(&thread_id[thread_count],NULL,ThreadOptChl,(void*)&tdk[i][j]);
if(result!=0)
{
Log(&(tdk[i][j]),"Fail : create thread operated mdm.%d.chl.%d failed. ErrorNO : %d\r\n",
tdk[i][j].mdm_no,tdk[i][j].chl_no,result);
}
else
thread_count++;
}
}
for (i=0; i<thread_count; i++)
{
pthread_join(thread_id[i],NULL); //wait thread process exit
}
pthread_mutex_destroy(&mutex_r);
pthread_mutex_destroy(&mutex_w);
pthread_mutex_destroy(&mutex_c);
exit(0);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -