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

📄 gsm0710.c

📁 GSM 协议 0710多路复用的源码
💻 C
📖 第 1 页 / 共 3 页
字号:
		{			// not an information frame			if(_debug)				syslog(LOG_DEBUG,"not an information frame\n");#ifdef DEBUG			print_frame(frame);#endif                        switch((frame->control & ~PF))			{				case UA:					if(_debug)						syslog(LOG_DEBUG,"is FRAME_IS(UA, frame)\n");					if (cstatus[frame->channel].opened == 1)					{						syslog(LOG_INFO,"Logical channel %d closed.\n", frame->channel);						cstatus[frame->channel].opened = 0;					}					else					{						cstatus[frame->channel].opened = 1;						if (frame->channel == 0)						{							syslog(LOG_INFO,"Control channel opened.\n");							// send version Siemens version test						//	write_frame(0, version_test, 18, UIH);						}						else						{char at[] = {'a','t','\r', '\n'};write_frame(frame->channel, at, 4, UIH);							syslog(LOG_INFO,"Logical channel %d opened.\n", frame->channel);						}					}					break;				case DM:					if (cstatus[frame->channel].opened)					{						syslog(LOG_INFO,"DM received, so the channel %d was already closed.\n", frame->channel);						cstatus[frame->channel].opened = 0;					}					else					{						if (frame->channel == 0)						{							syslog(LOG_INFO,"Couldn't open control channel.\n->Terminating.\n");							terminate = 1;							terminateCount = -1;    // don't need to close channels						}						else						{							syslog(LOG_INFO,"Logical channel %d couldn't be opened.\n", frame->channel);						}					}					break;				case DISC:					if (cstatus[frame->channel].opened)					{						cstatus[frame->channel].opened = 0;						write_frame(frame->channel, NULL, 0, UA | PF);						if (frame->channel == 0)						{							syslog(LOG_INFO,"Control channel closed.\n");							if (faultTolerant) {								restart = 1;							} else {								terminate = 1;								terminateCount = -1;    // don't need to close channels							}						}						else						{							syslog(LOG_INFO,"Logical channel %d closed.\n", frame->channel);						}					}					else					{						// channel already closed						syslog(LOG_INFO,"Received DISC even though channel %d was already closed.\n", frame->channel);						write_frame(frame->channel, NULL, 0, DM | PF);					}					break;				case SABM:					// channel open request					if (cstatus[frame->channel].opened == 0)					{						if (frame->channel == 0)						{							syslog(LOG_INFO,"Control channel opened.\n");						}						else						{							syslog(LOG_INFO,"Logical channel %d opened.\n", frame->channel);						}					}					else					{						// channel already opened						syslog(LOG_INFO,"Received SABM even though channel %d was already closed.\n", frame->channel);					}					cstatus[frame->channel].opened = 1;					write_frame(frame->channel, NULL, 0, UA | PF);					break;			}		}		destroy_frame(frame);	}	if(_debug)		syslog(LOG_DEBUG,"out of %s\n", __FUNCTION__);	return framesExtracted;}/** Wait for child process to kill the parent. */void parent_signal_treatment(int param) {  fprintf(stderr, "MUX started\n");  exit(0);}/** * Daemonize process, this process  create teh daemon */int daemonize(int _debug){	if(!_debug)	{	        signal(SIGHUP, parent_signal_treatment);                if((the_pid=fork()) < 0) {                        wait_for_daemon_status = 0;			return(-1);		} else                    if(the_pid!=0) {                        if (wait_for_daemon_status) {                            wait(NULL);			    fprintf(stderr, "MUX startup failed. See syslog for details.\n");                            exit(1);                        }                         exit(0);//parent goes bye-bye                    }		//child continues		setsid();   //become session leader		//signal(SIGHUP, SIG_IGN);		if(wait_for_daemon_status == 0 && (the_pid = fork()) != 0)			exit(0);		chdir("/"); //change working directory		umask(0);// clear our file mode creation mask        // Close out the standard file descriptors        close(STDIN_FILENO);        close(STDOUT_FILENO);        close(STDERR_FILENO);	}	//daemonize process stop here	return 0;}/** * Function responsible by all signal handlers treatment * any new signal must be added here */void signal_treatment(int param){	switch(param)	{		case SIGPIPE:			exit(0);			break;		case SIGHUP:			//reread the configuration files			break;		case SIGINT:			//exit(0);			terminate = 1;			break;		case SIGKILL:			//kill immediatly			//i'm not sure if i put exit or sustain the terminate attribution			terminate = 1;			//exit(0);			break;		case SIGUSR1:			terminate  = 1;			//sig_term(param);		case SIGTERM:			terminate = 1;			break;		default:			exit(0);			break;	}}/** * Fuunction to init Modemd Siemes MC35 families * Siemens need and special step-by for after get-in MUX state */int initSiemensMC35(){	char mux_command[] = "AT+CMUX=0\r\n";    char speed_command[20] = "AT+IPR=57600\r\n";	unsigned char close_mux[2] = { C_CLD | CR, 1 };    int baud = indexOfBaud(baudrate);	//Modem Init for Siemens MC35i	if (!at_command(serial_fd,"AT\r\n", 10000))	{		if(_debug)			syslog(LOG_DEBUG, "ERROR AT %d\r\n", __LINE__);        syslog(LOG_INFO, "Modem does not respond to AT commands, trying close MUX mode");		write_frame(0, close_mux, 2, UIH);        at_command(serial_fd,"AT\r\n", 10000);	}    if (baud != 0) {        sprintf(speed_command, "AT+IPR=%d\r\n", baudrate);    }	if (!at_command(serial_fd, speed_command, 10000))	{		if(_debug)			syslog(LOG_DEBUG, "ERROR %s %d \r\n", speed_command, __LINE__);	}	if (!at_command(serial_fd,"AT\r\n", 10000))	{		if(_debug)			syslog(LOG_DEBUG, "ERROR AT %d \r\n", __LINE__);	}	if (!at_command(serial_fd,"AT&S0\r\n", 10000))	{		if(_debug)			syslog(LOG_DEBUG, "ERRO AT&S0 %d\r\n", __LINE__);	}	if (!at_command(serial_fd,"AT\\Q3\r\n", 10000))	{		if(_debug)			syslog(LOG_DEBUG, "ERRO AT\\Q3 %d\r\n", __LINE__);	}        if (pin_code > 0 && pin_code < 10000)         {            // Some modems, such as webbox, will sometimes hang if SIM code            // is given in virtual channel            char pin_command[20];            sprintf(pin_command, "AT+CPIN=\"%d\"\r\n", pin_code);            if (!at_command(serial_fd,pin_command, 20000))            {		if(_debug)			syslog(LOG_DEBUG, "ERROR AT+CPIN %d\r\n", __LINE__);            }        }	if (!at_command(serial_fd, mux_command, 10000))	{		syslog(LOG_ERR, "MUX mode doesn't function.\n");		return -1;	}	return 0;}/** * Function to start modems that only needs at+cmux=X to get-in mux state */int initGeneric(){	char mux_command[20] = "AT+CMUX=0\r\n";	char mux_active[5] = {0xf9,0xf9,0xf9,0xf9,'\0'};    unsigned char close_mux[2] = { C_CLD | CR, 1 };    int baud = indexOfBaud(baudrate);    if (baud != 0) {        // Setup the speed explicitly, if given        //sprintf(mux_command, "AT+CMUX=0,0,%d\r\n", baud);    }		/**	 * Modem Init for Siemens Generic like Sony	 * that don't need initialization sequence like Siemens MC35	 */	if (!at_command(serial_fd,"AT\r\n", 10000))	{		if(_debug)			syslog(LOG_DEBUG, "ERROR AT %d\r\n", __LINE__);        syslog(LOG_INFO, "Modem does not respond to AT commands, trying close MUX mode");		write_frame(0, close_mux, 2, UIH);        at_command(serial_fd,"AT\r\n", 10000);	}        if (pin_code > 0 && pin_code < 10000)         {            // Some modems, such as webbox, will sometimes hang if SIM code            // is given in virtual channel            char pin_command[20];            sprintf(pin_command, "AT+CPIN=%d\r\n", pin_code);            if (!at_command(serial_fd,pin_command, 20000))            {		if(_debug)			syslog(LOG_DEBUG, "ERROR AT+CPIN %d\r\n", __LINE__);            }        }	if (!at_command(serial_fd, mux_command, 10000))	{		syslog(LOG_ERR, "MUX mode doesn't function.\n");		return -1;	}	if(!at_command(serial_fd, mux_active, 10000))	{		syslog(LOG_ERR, "MUX cannot be activated.\n");	//	return -1;	}	return 0;}int openDevicesAndMuxMode() {	int i;	int ret = -1;	syslog(LOG_INFO,"Open devices...\n");	// open ussp devices	maxfd = 0;	for (i = 0; i < numOfPorts; i++)	{		remaining[i] = 0;		if ((ussp_fd[i] = open_pty(ptydev[i], i)) < 0)		{			syslog(LOG_ERR,"Can't open %s. %s (%d).\n", ptydev[i], strerror(errno), errno);			return -1;		} 		else if (ussp_fd[i] > maxfd)			maxfd = ussp_fd[i];		cstatus[i].opened = 0;		cstatus[i].v24_signals = S_DV | S_RTR | S_RTC | EA;	}	cstatus[i].opened = 0;	syslog(LOG_INFO,"Open serial port...\n");		// open the serial port	if ((serial_fd = open_serialport(serportdev)) < 0)	{		syslog(LOG_ALERT,"Can't open %s. %s (%d).\n", serportdev, strerror(errno), errno);		return -1;	}	else if (serial_fd > maxfd)		maxfd = serial_fd;	syslog(LOG_INFO,"Opened serial port. Switching to mux-mode.\n");	switch(_modem_type)	{	case MC35:		//we coould have other models like XP48 TC45/35		ret = initSiemensMC35();		break;	case GENERIC:		ret = initGeneric();		break;		// case default:		// syslog(LOG_ERR, "OOPS Strange modem\n");	}	if (ret != 0) {		return ret;	}	//End Modem Init	terminateCount = numOfPorts;	syslog(LOG_INFO, "Waiting for mux-mode.\n");	sleep(1);	syslog(LOG_INFO, "Opening control channel.\n");	write_frame(0, NULL, 0, SABM | PF);	syslog(LOG_INFO, "Opening logical channels.\n");	for (i = 1; i <= numOfPorts; i++)	{		sleep(1);		write_frame(i, NULL, 0, SABM | PF);		syslog(LOG_INFO, "Connecting %s to virtual channel %d on %s\n", ptsname(ussp_fd[i-1]), i, serportdev);	}	return ret;}void closeDevices() {	int i;	close(serial_fd);	for (i = 0; i < numOfPorts; i++) {		char *symlinkName = createSymlinkName(i);		close(ussp_fd[i]);		if (symlinkName) {			// Remove the symbolic link to the slave device			unlink(symlinkName);			free(symlinkName);		}	}}/** * The main program */int main(int argc, char *argv[], char *env[]){#define PING_TEST_LEN 6	static char ping_test[] = "\x23\x09PING";	//struct sigaction sa;	int sel, len;	fd_set rfds;	struct timeval timeout;	unsigned char buf[4096], **tmp;	char *programName;	int i, size,t;	unsigned char close_mux[2] = { C_CLD | CR, 1 };	int opt;	pid_t parent_pid;    // for fault tolerance	int pingNumber = 1;	time_t frameReceiveTime;	time_t currentTime;	programName = argv[0];	/*************************************/	if(argc<2)	{		usage(programName);		exit(-1);	}	_modem_type = GENERIC;	serportdev="/dev/modem";	while((opt=getopt(argc,argv,"p:f:h?dwrm:b:P:s:"))>0)	{		switch(opt)		{			case 'p' :				serportdev = optarg;				break;			case 'f' :				max_frame_size = atoi(optarg);				break;			//Vitorio			case 'd' :				_debug = 1;				break;			case 'm':				if(!strcmp(optarg,"mc35"))					_modem_type = MC35;				else if(!strcmp(optarg,"mc75"))					_modem_type = MC35;				else if(!strcmp(optarg,"generic"))					_modem_type = GENERIC;				else _modem_type = UNKNOW_MODEM;				break;			case 'b':				baudrate = atoi(optarg);				break;			case 's':  		    	devSymlinkPrefix = optarg;				break;			case 'w':				wait_for_daemon_status = 1;				break;			case 'P':				pin_code = atoi(optarg);				break;			case 'r':				faultTolerant = 1;				break;			case '?' :			case 'h' :				usage(programName);				exit(0);				break;			default:				break;		}	}	//DAEMONIZE	//SHOW TIME	parent_pid = getpid();	daemonize(_debug);	//The Hell is from now-one    /* SIGNALS treatment*/	signal(SIGHUP, signal_treatment);	signal(SIGPIPE, signal_treatment);	signal(SIGKILL, signal_treatment);	signal(SIGINT, signal_treatment);    signal(SIGUSR1, signal_treatment);	signal(SIGTERM, signal_treatment);	programName = argv[0];	if(_debug)	{		openlog(programName, LOG_NDELAY | LOG_PID | LOG_PERROR  , LOG_LOCAL0);//pode ir at

⌨️ 快捷键说明

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