예제 #1
0
int main( void )
{
	init();

	// main program
	while (1)
	{
		switch ( action() )
		{
			case CTRL_OK_ACTION:
				menu();
				break;

			case CTRL_DN_ACTION:
				COUNT_DN
				enterProgram( program.number, true );
				break;

			case CTRL_UP_ACTION:
				COUNT_UP
				enterProgram( program.number, true );
				break;

			case COM_ACTION:
				execCMD( receive_com( 10 ) );
				break;

			default:
				continue;
		}
	}

	return 0;
}
예제 #2
0
/*static void sig_chld_handler(int  SIGNO){
	if(waitpid(0, NULL,WNOHANG))
	printf("child process toally exit\n");
}
*/
int
main(int argc,char **argv){
		struct sockaddr_in servaddr;
		struct hostent *host;

		FILE *fp;
		char ipstr[20] = LOCAL_IP;
		char *p;

		//open spi device/
		spi_fd = open("/dev/spidev0.2", O_RDWR);
		if (spi_fd < 0) {
			  perror("open");
		  return 1;
		}
		/*if you execv this programme like this:a.out -l ,it means that you want to start
		 * a local transfer.In a local transfer ,you need not get the serv_ip from txt message
		 * (actually the serv_ip is included in default ffmpeg parameters(19.168.6.8)).
		 * otherwise,it means that you get serv_ip from txt message
		 */
		if((argc >=2) && (!strcmp(argv[1],"-l"))){
			 pFFMPEG = ffmpeg_av;
			 goto main_work;
		}

//start a child process to read serv_ip from 	txt message,and save it in a temp file--------
	  pc = vfork();
	  if(pc < 0){
	  	perror("Error Refork:");
			}
	  else if (pc == 0){  //in child first
			  if (execl("/bin/ccs","ccs",(char *)0) < 0)
			  perror("execv ccs error!");
			  exit(0);
	  	  }
	  else {
		   pid_ccs = pc;    //save the child pid
				printf("waiting txt command\n");
	   	   	   }
	  if(waitpid(pid_ccs, NULL,0)>0)
		  	  printf("child process toally exit\n");
		else{
					printf("unexpected result\n");
			}
//end of get serv_ip from txt message-------------------------------------

	/*read the tmp file to get serv_ip------------------------------------*/
		if((fp=fopen("/usr/nuf","r"))==NULL){
				perror("open file error");
				return -1;
			}
	 p = ipstr;
	 *p = fgetc(fp);
  //get the server ip/
	while(*p!= EOF && *p != 10){
				++p;
				*p=fgetc(fp);
		}
	*p='\0';
  fclose(fp);
	/*Remember to close your open files when you do not use it any more*/

	//Begin to FFMPEG and transfer avdieo at first !!//////////////////////
	   pFFMPEG = ffmpeg_av; 
	  // char rtp[] = "rtp:";
 	   char port_a[] = ":12386";
	   char port_v[] = ":12346";
	   char rtp_port_a[30] = "rtp:" ;  //[rtp:xxx.xxx.xxx.xxx:port]
	   char rtp_port_v[30] = "rtp:" ;  //[rtp:xxx.xxx.xxx.xxx:port]
     //ip should be modified at first time
     ffmpeg_av[17] = strcat(strcat(rtp_port_v,ipstr),port_v);
	  // strcpy(rtp_port,rtp);      //override rtp_port
     ffmpeg_av[32] = strcat(strcat(rtp_port_a,ipstr),port_a);
	   //strcpy(rtp_port,rtp);
	   ffmpeg_audio[15] = rtp_port_a;   //strcat(strcat(rtp_port_a,ipstr),port_a);
	   //strcpy(rtp_port,rtp);
	   ffmpeg_video[17] = rtp_port_v;   //strcat(strcat(rtp_port_v,ipstr),port_v);
	   //printf("ffmpeg_av:%s",ffmpeg_av[30]);

	   int num_net_test = 0;
	   while(get_if_stat("ppp0") < 0){
		   sleep(1);
		    //after ry tMAX_NET_TEST times,the net interface is still down,we give up,and give a report
		   if (num_net_test++ > MAX_NET_TEST){
						   printf("try to get PPP0  not success and exit now!\n");
						   return 1;
		   		}
	   	  }


/*child process for ffmpeg,and father process for socket connection----------------------------*/
main_work:
		pc = vfork();        
		if(pc < 0)
			perror("Error Fork:");
		//child process-----------------
		else if (pc == 0){
			if (execv("/usr/bin/ffmpeg",pFFMPEG) < 0)    //start ffmpeg with default parameters
					perror("execv FFMPG error!");
			exit(0);
		 }
		//END of child process ---------
		else{
    ///father process---------------------------
	     pid_ffmpeg = pc;  //save the child process id ,just for kill/wait the child
   while(1){
      if((host=gethostbyname(ipstr))==NULL){
           fprintf(stderr,"Gethostname error\n");
           exit(-1);
      	  	  	  }

      if( (sock_fd = socket( AF_INET,SOCK_STREAM,0))<0){
           printf("create socket error\n");
           exit(-1);
      	  	  	  }
      else printf("create socket success\n");
       
      servaddr.sin_family = AF_INET;
      servaddr.sin_port = htons(PORT);
      servaddr.sin_addr = *((struct in_addr *)host->h_addr);
      
      while(1){
       /*TCP three times handshakes-*/
    	  	  if(connect(sock_fd,(struct sockaddr *) &servaddr,sizeof(struct sockaddr))<0) {
						    //server is down or time out /;
								 sleep(1);      //wait for one minute to avoid asking for connection too frequently
						    continue;
    	  	  	}
					  else{
								printf("connect success\n");
								break;
						}
     }  //end of three handshakes
		  
		  char sendBuf[]="quit robot's control";

			while(1){
			  if(revcCMD(sock_fd,cmd) != 0){
				  	  printf("recv command error or server is down!\n");
					  close(sock_fd);
				  	  break;
			  	  }

		   if(strstr(cmd,"quit")!=NULL){
						 //sprintf(sendBuf,"quit robot's control");
						 send(sock_fd,sendBuf,strlen(sendBuf)+1,0);
						 close(sock_fd);
						 break;
		   	   	  }
			  			 	
			 if(execCMD(cmd) != 0) {
					   printf("unknow command\n");
			 	 }
		  }   //end of loop for(recv--exec)
   }  //end of loop for (creat socket--connect--recv--exec)
		close(spi_fd);
		return 0;
	 ///END of father process---------------------------
	}
}