//If p2p device becomes GO, we still polling driver status
//to check whether some other p2p devices connected
void *polling_client(void *arg)
{
	struct p2p *p=(struct p2p*)arg;
	
	while( p->res_go == 0 ){

		if( p->no_sta_connected > 0 && ( p->wpsing == _FALSE ) )
		{
			if( read_all_sta(p) == _FALSE )
			{
				p->no_sta_connected--;
			}
		}

		p2p_status(p, 0);
		if( p->status == P2P_STATE_RX_PROVISION_DIS_REQ || p->status == P2P_STATE_GONEGO_FAIL || p->status == P2P_STATE_GONEGO_ING )
		{
			p->thread_trigger = THREAD_GO ;
			
			char peer_devaddr[18];
			char peer_req_cm[4];
			
			memset( peer_devaddr, 0x00, 18);
			memset( peer_req_cm, 0x00, 4);
			
			p2p_peer_devaddr(p, peer_devaddr);
			p2p_peer_req_cm(p, peer_req_cm);
			p2p_peer_info(p, p->peer_devaddr, peer_req_cm);

			ui_screen(p);

			//strncpy(p->peer_devaddr, peer_devaddr, 17);
			if( (strncmp( peer_req_cm, "dis", 3) == 0) || (strncmp( peer_req_cm, "lab", 3) == 0) )
			{
				printf("Here is your PIN, insert c to continue: %d\n", p->pin);
			}
			else if( (strncmp( peer_req_cm, "pbc", 3) == 0) )
			{
				printf("Please push b to accept:\n", p->status);
			}
			else if( (strncmp( peer_req_cm, "pad", 3) == 0) )
			{
				printf("Please insert peer PIN code:\n");
			}

			break;
  	}

		usleep( POLLING_INTERVAL );
	}

	return NULL;
}
Esempio n. 2
0
//Successively query local device status with interval "POLLING_INTERVAL"
//when status == P2P_STATE_RX_PROVISION_DIS_REQ, 
//it require user to insert corresponding WPS config method
void *polling_status(void *arg)
{
	struct p2p *p=(struct p2p*)arg;
	
	while( p->res == 0 ){

		p2p_status(p, 0);
		if( p->status == P2P_STATE_RX_PROVISION_DIS_REQ )
		{
			p->thread_trigger = THREAD_DEVICE ;
			
			char peer_devaddr[18];
			char peer_req_cm[4];
			
			memset( peer_devaddr, 0x00, 18);
			memset( peer_req_cm, 0x00, 4);
			
			p2p_peer_devaddr(p, peer_devaddr);
			p2p_peer_req_cm(p, peer_req_cm);
			p2p_peer_info(p, p->peer_devaddr, peer_req_cm);

			ui_screen(p);

			//strncpy(p->peer_devaddr, peer_devaddr, 17);
			if( (strncmp( peer_req_cm, "dis", 3) == 0) || (strncmp( peer_req_cm, "lab", 3) == 0) )
			{
				printf("Here is your PIN, insert c to continue: %d\n", p->pin);
			}
			else if( (strncmp( peer_req_cm, "pbc", 3) == 0) )
			{
				printf("Please push b to accept:\n");
			}
			else if( (strncmp( peer_req_cm, "pad", 3) == 0) )
			{
				printf("Please insert peer PIN code:\n");
			}

			break;
  	}

		usleep( POLLING_INTERVAL );
	}

	return NULL;
}
Esempio n. 3
0
void p2p_scan(struct p2p *p)
{
	p->p2p_get=0;
	if( p->enable >= P2P_ROLE_DEVICE )
	{
		p->have_p2p_dev=1;
		memset( p->cmd, 0x00, CMD_SZ );
		sprintf( p->cmd, "iwlist %s scan > scan.txt", p->ifname );
		system( p->cmd );

		p2p_status(p, 0);
		
	}
	else
	{
		p->p2p_get=1;
		memset( p->print_line, 0x00, CMD_SZ );
		sprintf( p->print_line, "%s", p->scan_msg );
	}
}
Esempio n. 4
0
void p2p_go_mode(struct p2p *p)
{
	FILE *pf = NULL;
	int count = 0, ret = 0, i = -1;
	char addr_lower[18];

	p2p_ifaddr(p);

	p->p2p_get=1;
	memset( p->print_line, 0x00, CMD_SZ );
	strcpy( p->print_line, "Start hostapd");
	ui_screen(p);

	if(p->ap_open != 1)
	{
		memset( p->cmd, 0x00, CMD_SZ );
		sprintf( p->cmd, "%s -B %s > temp.txt",p->ap_path, p->ap_conf);
		system( p->cmd );

		usleep( HOSTAPD_INIT_TIME );
	}

	if(p->wps_info==1 || p->wps_info==2)
	{
		do
		{
		memset( p->cmd, 0x00, CMD_SZ );
		sprintf( p->cmd, "%s wps_pin any %d > supp_status.txt", p->apcli_path, p->pin);
		system( p->cmd );
	
			pf = fopen( "./supp_status.txt", "r" );	
			if ( pf )
			{
				while( !feof( pf ) ){
					memset(p->parse, 0x00, CMD_SZ);
					fgets(p->parse, CMD_SZ, pf);
					if(strncmp(p->parse, "", 2) == 0)
					{
						ret = 1;
						p->wpsing = 1;
					}
				}
		
				fclose( pf );
			}
			
			if( ret == 0 )
				usleep( HOSTAPD_INIT_TIME );
		}
		while( ret == 0 );
	}
	else if(p->wps_info==3)
	{
		do
		{
		memset( p->cmd, 0x00, CMD_SZ );
		sprintf( p->cmd, "%s wps_pbc any > supp_status.txt", p->apcli_path);
		system( p->cmd );
						
			pf = fopen( "./supp_status.txt", "r" );	
			if ( pf )
			{
				while( !feof( pf ) ){
					memset(p->parse, 0x00, CMD_SZ);
					fgets(p->parse, CMD_SZ, pf);
					if(strncmp(p->parse, "OK", 2) == 0)
					{
						ret = 1;
						p->wpsing = 1;
					}
				}
		
				fclose( pf );
			}
			
			if( ret == 0 )
				usleep( HOSTAPD_INIT_TIME );
		}
		while( ret == 0 );
	}

	usleep( HOSTAPD_INTERVAL );
		
	while( count < WPS_RETRY )		//query status
	{
	
		memset( p->cmd, 0x00, CMD_SZ );
		sprintf( p->cmd, "%s all_sta > supp_status.txt", p->apcli_path);
		system( p->cmd );
		
		pf = fopen( "./supp_status.txt", "r" );
		if ( pf )
		{
			for(i=0;i<17;i++)
			{
				if( p->ap_open != 1 )
					addr_lower[i] = lower(p->peer_ifaddr[i]);
				else
					addr_lower[i] = lower(p->peer_devaddr[i]);
			}
			addr_lower[17]='\0';
     			
			while( !feof( pf ) ){
				memset( p->parse, 0x00, CMD_SZ );
				fgets( p->parse, CMD_SZ, pf );
				if( strncmp( p->parse, "dot11RSNAStatsSTAAddress=", 25) == 0 )
				{
					if( strncmp( p->parse+25, addr_lower, 17) == 0 )
					{
						count = WPS_RETRY;
						p->wpsing = 0;

						p->p2p_get=1;
						memset(p->print_line, 0x00, CMD_SZ);
						for(i=0; i<CMD_SZ; i++){
							if(p->parse[i] == '\n'){
								p->parse[i] = ' ';
							}
						}		
						sprintf(p->print_line, "%s", p->parse);
						ui_screen(p);
#ifdef DHCP
						memset( p->cmd, 0x00, CMD_SZ );
						sprintf( p->cmd, "ifconfig %s 192.168.1.254", p->ifname);
						system( p->cmd );
	
						usleep(50000);
						
						system( "/etc/rc.d/init.d/dhcpd start" );
						system( "clear" );
#endif //DHCP

						//After starting hostapd and doing WPS connection successful,
						//We create a thread to query driver if some other p2p devices connected.
						p2p_status(p, 0);
						usleep(50000);

#ifdef P2P_AUTO
						p->res_go = pthread_create(&p->pthread_go, NULL, &polling_client, (void *)p);

						if( p->res_go != 0 )
						{
							p->p2p_get=1;
							memset( p->print_line, 0x00, CMD_SZ );
							sprintf( p->print_line, "Thread creation failed" );
						}
#endif
						
						break;
					}
				}
			}

			fclose( pf );
		}
		
		if( count == WPS_RETRY)
			break;

		count++;
		usleep( HOSTAPD_INTERVAL );
		
		p->p2p_get=1;
		memset( p->print_line, 0x00, CMD_SZ );
		sprintf( p->print_line, "hostapd open, count:%d", count);
		ui_screen(p);

	}

	if(p->ap_open != 1)
		p->ap_open = 1;

}
Esempio n. 5
0
void p2p_set_nego(struct p2p *p)
{
	FILE *pf=NULL;
	int retry_count = 0, success = 0;

	p->p2p_get=1;
	memset( p->print_line, 0x00, CMD_SZ );
	strcpy( p->print_line, p->nego_msg);
	ui_screen(p);
	
	memset( p->cmd, 0x00, CMD_SZ );
	sprintf( p->cmd, "iwpriv %s p2p_set nego=%s ", p->ifname, p->peer_devaddr);
	system( p->cmd );

	usleep( PRE_NEGO_INTERVAL );

	do
	{

		p2p_status(p, 0);
		p2p_role(p, 0);
		
		if( p->status != P2P_STATE_GONEGO_OK ){
			
			if( p->role == P2P_ROLE_CLIENT || p->role == P2P_ROLE_GO )
				break;
			
			if( retry_count > 0)
			{
				int check_status = 0;
				
				while(check_status < (NEGO_RETRY_INTERVAL/500000) && p->status != P2P_STATE_GONEGO_OK )
				{
					p2p_status(p, 1);
					if( check_status == 0 )
						ui_screen(p);

					if( p->status == P2P_STATE_GONEGO_OK )
					{
						success = 1;
						break;
					}
					
					usleep(500000);
					check_status++;
				}
				
				//usleep( NEGO_RETRY_INTERVAL );
			}

			if( success ==0 ){	//need to re-fine
				memset( p->cmd, 0x00, CMD_SZ );
				sprintf( p->cmd, "iwpriv %s p2p_set nego=%s ", p->ifname, p->peer_devaddr);
				system( p->cmd );
	
				usleep(10000);
				retry_count++;
			}
		}
		else
		{
			p->p2p_get = 1;
			memset( p->print_line, 0x00, CMD_SZ );
			sprintf( p->print_line, "%s", p->ok_msg );
			p2p_ifaddr(p);
			ui_screen(p);
			break;
		}
			
	}
	while( p->status != P2P_STATE_GONEGO_OK && retry_count < MAX_NEGO_RETRY );

	if( p->status == P2P_STATE_GONEGO_OK || p->role == P2P_ROLE_CLIENT || p->role == P2P_ROLE_GO )
	{
		p2p_role(p ,0);
		
		if( p->role == P2P_ROLE_CLIENT )
		{
			p2p_client_mode(p);
		}
		else if( p->role == P2P_ROLE_GO )
		{
			p2p_go_mode(p);
		}
	}
	else
	{
		p->p2p_get = 1;
		p2p_status(p, 0);
		memset( p->print_line, 0x00, CMD_SZ );
		sprintf( p->print_line, "Status= %d, %s", p->status, p->fail_msg );
		ui_screen(p);

#ifdef P2P_ATUO		
		pthread_create(&p->pthread, NULL, &polling_status, (void *)p);
#endif
		
	}
}
Esempio n. 6
0
void p2p_prov_disc(struct p2p *p, char *msg, char *dis_msg, char *label_msg)
{
	int wps_cm, retry_count=0;
	char prov[100] = { 0x00 };
	
	p->p2p_get=0;
	ui_screen(p);
	printf("%s", msg);
	scanf("%d",&wps_cm);

	if(p->res == 0)
	{
		p->res = 1;
	}

	memset( p->cmd, 0x00, CMD_SZ );	
	if( wps_cm == 0 )
		sprintf( p->cmd, "iwpriv %s p2p_set prov_disc=%s_display", p->ifname, p->peer_devaddr);
	else if( wps_cm == 1 )
		sprintf( p->cmd, "iwpriv %s p2p_set prov_disc=%s_keypad", p->ifname, p->peer_devaddr);
	else if( wps_cm == 2 )
		sprintf( p->cmd, "iwpriv %s p2p_set prov_disc=%s_pbc", p->ifname, p->peer_devaddr);
	else if( wps_cm == 3 )
		sprintf( p->cmd, "iwpriv %s p2p_set prov_disc=%s_label", p->ifname, p->peer_devaddr);	
	system( p->cmd );
	strcpy( prov, p->cmd );

	usleep(500000);
	p2p_status( p, 0 );
	
	while(p->status != P2P_STATE_RX_PROVISION_DIS_RSP && retry_count < MAX_PROV_RETRY)
	{
		usleep( PROV_WAIT_TIME );
		retry_count++;
		p2p_status( p, 0 );
		if( (retry_count % PROV_RETRY_INTERVAL) == 0)
			system( prov );
	}
	
	if( p->status == P2P_STATE_RX_PROVISION_DIS_RSP )
	{
		switch(wps_cm)
		{
			case 0:	p->wps_info=1;	break;
			case 1: p->wps_info=2;	break;
			case 2: p->wps_info=3;	break;
			case 3: p->wps_info=1;	break;
		}
		
		if( wps_cm==1 || wps_cm==2 )
		{
			p2p_wpsinfo(p);
			
			if(p->connect_go == 1)
				p2p_client_mode(p);
			else
				p2p_set_nego(p);
		}
		else if( wps_cm==0 || wps_cm==3 )
		{
			ui_screen(p);
			if( wps_cm ==0 )
				printf("%s", dis_msg);	
			else if( wps_cm == 3 )
				printf("%s", label_msg);
			scanf("%d",&p->pin);

			p2p_wpsinfo(p);	

			if(p->connect_go == 1)
				p2p_client_mode(p);
			else
				p2p_set_nego(p);
				
		}
	}
	else
	{
		p->p2p_get = 1;
		memset( p->print_line, 0x00, CMD_SZ );
		sprintf( p->print_line, "Issue provision discovery fail" );
		ui_screen(p);	

#ifdef P2P_AUTO		
		pthread_create(&p->pthread, NULL, &polling_status, (void *)p);
#endif
		
	}

}
Esempio n. 7
0
void p2p_enable(struct p2p *p)
{
	if(p->enable == -1 )
	{
		if(p->wpa_open == 1){
			p->wpa_open = 0;
			system("killall wpa_supplicant");
#ifdef DHCP
			system("killall dhclient");
#endif			
		}
		if(p->ap_open ==1){
			p->ap_open = 0;
			system("killall hostapd");
#ifdef DHCP
			system("killall dhcpd");
#endif
		}

		memset( p->cmd, 0x00, CMD_SZ );
		sprintf( p->cmd, "iwpriv %s p2p_set enable=0", p->ifname);
		system( p->cmd );
		
		return;
	}
	
	p->p2p_get=0;
	memset( p->cmd, 0x00, CMD_SZ );
	sprintf( p->cmd, "iwpriv %s p2p_set enable=%d", p->ifname, p->enable);
	system( p->cmd );
	
	if( p->enable == P2P_ROLE_DISABLE )
	{
		p->have_p2p_dev = 0;
		p->wps_info = 0;
		p->pin = 12345670;

		if(p->res == 0)
		{
			p->res = 1;
		}

		if(p->res_go == 0)
		{
			p->res_go = 1;
		}
				
		p->wpa_open = 0;
		system("killall wpa_supplicant");
#ifdef DHCP
		system("killall dhclient");
#endif
		system("clear");
		p->ap_open = 0;
		system("killall hostapd");
#ifdef DHCP
		system("killall dhcpd");
#endif
		system("clear");
		
	}
	else if( p->enable == P2P_ROLE_DEVICE )
	{
		char msg[5] = "NULL";

#ifdef P2P_AUTO
		p->res = pthread_create(&p->pthread, NULL, &polling_status, (void *)p);
#endif
		
		if( p->res !=0 )
		{
			p->p2p_get=1;
			memset( p->print_line, 0x00, CMD_SZ );
			sprintf( p->print_line, "Thread creation failed" );
		}
		
		if(p->wpa_open == 1){
			p->wpa_open = 0;
			system("killall wpa_supplicant");
#ifdef DHCP
			system("killall dhclient");
#endif
		}
		if(p->ap_open ==1){
			p->ap_open = 0;
			system("killall hostapd");
#ifdef DHCP
			system("killall dhcpd");
#endif
		}
		
		p->intent = 1;
		p2p_intent(p);
		
		p2p_set_opch(p, NULL, 0);
		usleep(50000);
		p2p_softap_ssid(p, NULL, 0);

		p2p_setDN(p);

		p2p_scan( p );
		
	}
	else if( p->enable == P2P_ROLE_CLIENT )
	{
		if(p->ap_open ==1){
			p->ap_open = 0;
			system("killall hostapd");
#ifdef DHCP
			system("killall dhcpd");
#endif
		}
		
		p2p_status(p, 0);
		p2p_role(p, 0);
		p2p_intent(p);
	}
	else if( p->enable == P2P_ROLE_GO )
	{
		if(p->wpa_open == 1){
			p->wpa_open = 0;
			system("killall wpa_supplicant");
#ifdef DHCP
			system("killall dhclient");
#endif
		}

		p2p_status(p, 0);
		p2p_role(p, 0);
		p2p_intent(p);
		
		p2p_set_opch(p, NULL, 0);
		usleep(50000);
		p2p_softap_ssid(p, NULL, 0);

		if(p->ap_open != 1)
		{
			memset( p->cmd, 0x00, CMD_SZ );
			sprintf( p->cmd, "%s -B %s > temp.txt",p->ap_path, p->ap_conf);
			system( p->cmd );
	
			p->ap_open = 1;
		}

#ifdef P2P_AUTO
		p->res_go = pthread_create(&p->pthread_go, NULL, &polling_client, (void *)p);
#endif

		if( p->res_go != 0 )
		{
			p->p2p_get=1;
			memset( p->print_line, 0x00, CMD_SZ );
			sprintf( p->print_line, "Thread creation failed" );
		}
		
	}

}
void p2p_go_mode(struct p2p *p)
{
	int count = 0, i = -1;
	char addr_lower[18];

	p2p_ifaddr(p);
	p->no_sta_connected++;

	p->p2p_get=1;
	memset( p->print_line, 0x00, CMD_SZ );
	strcpy( p->print_line, "Start hostapd");
	ui_screen(p);

	if(p->ap_open != _TRUE)
	{
		memset( p->cmd, 0x00, CMD_SZ );
		sprintf( p->cmd, "%s -B %s > temp.txt",p->ap_path, p->ap_conf);
		system( p->cmd );

		usleep( HOSTAPD_INIT_TIME );
		p->ap_open = _TRUE;
	}

	do_wps(p);

	usleep( HOSTAPD_INTERVAL );


	while( count < WPS_RETRY )		//query status
	{
		if( read_all_sta(p) == _TRUE )
		{
			count = WPS_RETRY;
			p->wpsing = _FALSE;

			p->p2p_get=1;
			memset(p->print_line, 0x00, CMD_SZ);
			for(i=0; i<CMD_SZ; i++){
				if(p->parse[i] == '\n'){
					p->parse[i] = ' ';
				}
			}		
			sprintf(p->print_line, "%s", p->parse);
			ui_screen(p);
#ifdef DHCP
			memset( p->cmd, 0x00, CMD_SZ );
			sprintf( p->cmd, "ifconfig %s 192.168.1.254", p->ifname);
			system( p->cmd );

			usleep(50000);
			
			system( "/etc/rc.d/init.d/dhcpd start" );
			system( "clear" );
#endif //DHCP

			//After starting hostapd and doing WPS connection successful,
			//We create a thread to query driver if some other p2p devices connected.
			p2p_status(p, 0);
			usleep(50000);

#ifdef P2P_AUTO
			p->res_go = pthread_create(&p->pthread_go, NULL, &polling_client, (void *)p);

			if( p->res_go != 0 )
			{
				p->p2p_get=1;
				memset( p->print_line, 0x00, CMD_SZ );
				sprintf( p->print_line, "Thread creation failed" );
			}
#endif
			break;		
		}
		else
		{
			if( count == WPS_RETRY)
				break;

			count++;
			usleep( HOSTAPD_INTERVAL );
		
			p->p2p_get=1;
			memset( p->print_line, 0x00, CMD_SZ );
			sprintf( p->print_line, "hostapd open, count:%d", count);
			ui_screen(p);
		}
	}

	p->wpsing = _FALSE;
}
void p2p_set_nego(struct p2p *p)
{
	FILE *pf=NULL;
	int retry_count = 0, success = 0;
	int retry = NEGO_RETRY_INTERVAL, query = NEGO_QUERY_INTERVAL;

	p->p2p_get=1;
	memset( p->print_line, 0x00, CMD_SZ );
	strcpy( p->print_line, p->nego_msg);
	ui_screen(p);
	
	memset( p->cmd, 0x00, CMD_SZ );
	sprintf( p->cmd, "iwpriv %s p2p_set nego=%s ", p->ifname, p->peer_devaddr);
	system( p->cmd );

	usleep( PRE_NEGO_INTERVAL );

	p2p_status(p, 0);

	while( !p2p_check_success(p) && (retry_count < 120 / NEGO_QUERY_INTERVAL ))
	{
		retry_count++;

		if( (retry_count % ( retry / query ) )==0 )
		{
			memset( p->cmd, 0x00, CMD_SZ );
			sprintf( p->cmd, "iwpriv %s p2p_set nego=%s ", p->ifname, p->peer_devaddr);
			system( p->cmd );
			
			usleep( NEGO_QUERY_INTERVAL );
			p2p_status(p, 1);
		}
		else
		{
			ui_screen(p);
			usleep( NEGO_QUERY_INTERVAL );
			p2p_status(p, 1);
		}
	}

	if( p2p_check_success(p) )
	{
		p2p_role(p ,0);
		p->p2p_get = 1;
		memset( p->print_line, 0x00, CMD_SZ );
		sprintf( p->print_line, "%s", p->ok_msg );
		p2p_ifaddr(p);
		ui_screen(p);		
		
		if( p->role == P2P_ROLE_CLIENT )
		{
			p2p_client_mode(p);
		}
		else if( p->role == P2P_ROLE_GO )
		{
			p2p_get_opch(p);
			p2p_go_mode(p);
		}
	}
	else
	{
		p->p2p_get = 1;
		p2p_status(p, 0);
		memset( p->print_line, 0x00, CMD_SZ );
		sprintf( p->print_line, "Status= %d, %s", p->status, p->fail_msg );
		ui_screen(p);

#ifdef P2P_ATUO		
		pthread_create(&p->pthread, NULL, &polling_status, (void *)p);
#endif
		
	}
}
Esempio n. 10
0
void wifi_direct_connect()
{
	int ipfd,sockfd, n;
	struct ifreq ifr;
	struct sockaddr_in servaddr,cliaddr;
	
	char peerifa[40] = { 0x00 };
	char scan[CMD_SZ];	
	struct p2p p2pstruct;
	struct p2p *p=NULL;

	p = &p2pstruct;	
	if( p != NULL)
	{
		memset(p, 0x00, sizeof(struct p2p));
		init_p2p(p);
	}

	strcpy(p->ifname, "wlan0" );
	
	
	p->enable=P2P_ROLE_DISABLE;
	p2p_enable(p);
	p2p_get_hostapd_conf(p); //get hostapd.conf
	//usleep(50000);  
  	rename_intf(p);
	
	ui_screen(p);

	//p->show_scan_result = 1;
	//ui_screen(p);
	p->enable = 1;
	p2p_enable(p);
	//p->show_scan_result = 1;
	//ui_screen(p);
	printf("==> p2p_enable(p)\n");

	p->thread_trigger = THREAD_NONE ;
	

	p->wps_info=3;
	p2p_wpsinfo(p);
	printf("==> p2p_wpsinfor(p)\n");

	p2p_status(p, 0);
	//printf("==> p2p_status(p)\n");

	//usleep(10000);
	
	if(p->status != P2P_STATE_GONEGO_OK)
	{
		
		p2p_set_nego(p);
		printf("...Negotiated\n");
	}
	else
	{
		p2p_role(p,0);
						
		if( p->role == P2P_ROLE_CLIENT )
		{
			printf("==>P2P_ROLE_CLIENT\n");
			p2p_client_mode(p);
		
		}
		else if( p->role == P2P_ROLE_GO )
		{
			printf("==>P2P_ROLE_GO\n");
			p2p_go_mode(p);
		}
	}
	
	system("dhclient wlan0");
	
	/********* Want to get IP address, you should be wait!*/
   /* because it takes a little bit time ****/
	printf("WIFI DIRECT SUCCESS, Wait 2sec...\n");
	sleep(2);

	
	ipfd= socket(PF_INET, SOCK_DGRAM, 0);
	ifr.ifr_addr.sa_family = PF_INET;
	strncpy(ifr.ifr_name, "wlan0", IFNAMSIZ-1);
	ioctl(ipfd, SIOCGIFADDR, &ifr);
	close(ipfd);
	

	sockfd=socket(PF_INET, SOCK_DGRAM, 0);

	
	strcpy(ipaddr, inet_ntoa(((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr));

	printf(" ==> ip : %s\n",ipaddr);

}
//int main()
int main(int argc, char **argv)
{
	char	peerifa[40] = { 0x00 };
	char	scan[CMD_SZ];	
	struct p2p p2pstruct;
	struct p2p *p=NULL;

	p = &p2pstruct;	
	if( p != NULL)
	{
		memset(p, 0x00, sizeof(struct p2p));
		init_p2p(p);
	}

	strcpy(p->ifname, argv[1] );
	
	/* Disable P2P functionalities at first*/
	p->enable=P2P_ROLE_DISABLE;
	p2p_enable(p);
	p2p_get_hostapd_conf(p);
	usleep(50000);
  
  rename_intf(p);
  
	do
	{
		ui_screen(p);

		printf("*insert cmd:");
		memset( scan, 0x00, CMD_SZ );
		scanf("%s", scan);

		if( p->thread_trigger == THREAD_NONE )		//Active mode for user interface
		{
			
			if( strncmp(scan, "e", 1) == 0 )	//Enable
			{
				p->show_scan_result = 1;
				ui_screen(p);
				printf("Please insert enable mode;[0]Disable, [1]Device, [2]Client, [3]GO:");
				scanf("%d",&p->enable);
	
				p2p_enable(p);
				p->show_scan_result = 1;
			}
			else if( strncmp(scan, "a", 1) == 0 )	// Scan P2P device
			{
				p2p_scan(p);
				p->show_scan_result = 1;
			}
			else if( strncmp(scan, "d", 1) == 0 )	// Set device name
			{
				p->p2p_get = 0;
				printf("Please insert device name :");
				scanf("\n%[^\n]", p->dev_name);
				p2p_setDN(p);
				p->show_scan_result = 1;
			}
			else if( strncmp(scan, "i", 1) == 0 )	// Intent
			{
				p->show_scan_result = 1;
				ui_screen(p);
				printf("Please insert intent from [0~15(must be softap)] :");
				scanf("%d",&p->intent);
				p2p_intent(p);
				p->show_scan_result = 1;
			}
			else if( strncmp(scan, "w", 1) == 0 )	// WPS_info
			{
				p->show_scan_result = 1;
				ui_screen(p);
				printf("Please insert WPS method\n");
				printf("[0]None, [1]Peer Display PIN, [2]Self Display Pin, [3]PBC :");
				scanf("%d",&p->wps_info);
				p2p_wpsinfo(p);
				p->show_scan_result = 1;
			}
			else if( strncmp(scan, "c", 1) == 0 )	// PIN_code
			{
				char ins_no[CMD_SZ], ins_no_again[CMD_SZ];
				memset(ins_no, 0x00, CMD_SZ); 
				strcpy(ins_no, "Please insert 8-digit number, eg:12345670 :" );
				memset(ins_no_again, 0x00, CMD_SZ);
				strcpy(ins_no_again, "Invalid number, insert again, eg:12345670 :" );
	
				p2p_pincode(p, ins_no, ins_no_again);
				p->show_scan_result = 1;
			}
			else if( strncmp(scan, "m", 1) == 0 )	// Set peer device address
			{
				p->show_scan_result = 1;
				ui_screen(p);
				printf("Please insert number in scan list:");
				p2p_devaddr(p);
				p->show_scan_result = 1;
			}
			else if( strncmp(scan, "r", 1) == 0 )	// Get role
			{
				p2p_role(p,1);
				p->show_scan_result = 1;
			}
			else if( strncmp(scan, "s", 1) == 0 )	// Get status
			{
				p2p_status(p, 1);
				p->show_scan_result = 1;
			}
			else if( strncmp(scan, "p", 1) == 0 )	// Provision discovery
			{
				char msg[CMD_SZ];
				memset( msg, 0x00, CMD_SZ );
				char dis_msg[CMD_SZ];
				memset( dis_msg, 0x00, CMD_SZ );
				char label_msg[CMD_SZ];
				memset( label_msg, 0x00, CMD_SZ );
	
	
				if(strncmp(p->peer_devaddr, "00:00:00:00:00:00", 17) == 0)
				{
					strcpy( msg, "Please insert peer P2P device at first" );			
	
					p2p_prov_disc_no_addr(p, msg);
					p->show_scan_result = 1;
				}
				else
				{
					strcpy( msg, "Please insert WPS configuration method ;[0]display, [1]keypad, [2]pbc, [3]label:\n" );
					strcpy( dis_msg, "Please insert PIN code displays on peer device screen:" );
					strcpy( label_msg, "Please insert PIN code displays on peer label:" );

					p2p_prov_disc(p, msg, dis_msg, label_msg);
				}
			}
			else if( strncmp(scan, "n", 1) == 0 )	// Set negotiation
			{
				p2p_set_nego(p);
			}
			else if( strncmp(scan, "f", 1) == 0 ) // Reflash current state
			{
				p->show_scan_result = 1;
				p2p_status(p, 0);
				p2p_role(p, 0);
				p2p_ifaddr(p);

				if( p->status == P2P_STATE_RX_PROVISION_DIS_REQ )
				{
					char peer_devaddr[18];
					char peer_req_cm[4];
					
					memset( peer_devaddr, 0x00, 18);
					memset( peer_req_cm, 0x00, 4);
					
					p2p_peer_devaddr(p, peer_devaddr);
					p2p_peer_req_cm(p, peer_req_cm);
					p2p_peer_info(p, p->peer_devaddr, peer_req_cm);
				}
#ifndef P2P_AUTO
				else
				{
					if( p->role == P2P_ROLE_CLIENT )
					{
						p2p_client_mode(p);
					}
					else if( p->role == P2P_ROLE_GO )
					{
						p2p_go_mode(p);
					}
				}
#endif //P2P_AUTO
		
			}
			else if( strncmp(scan, "x", 1) == 0 )	// Start wpa_supplicant/hostapd
			{
				if( p->role == P2P_ROLE_CLIENT )
				{
					p2p_client_mode(p);
				}
				else if( p->role == P2P_ROLE_GO )
				{
					p2p_go_mode(p);
				}
			}
			else if( strncmp(scan, "h", 1) == 0 )	// Set operation channel
			{
				char msg[CMD_SZ];
				memset( msg, 0x00, CMD_SZ );
				strcpy( msg, "Please insert desired operation channel:" );			
	
				p2p_set_opch(p, msg, 1);
				p->show_scan_result = 1;
			}
			else if( strncmp(scan, "t", 1) == 0 )	// Set SoftAP ssid
			{
				char msg[CMD_SZ];
				memset( msg, 0x00, CMD_SZ );
				strcpy( msg, "Please insert desired SoftAP ssid:" );			
	
				p2p_softap_ssid(p, msg, 1);
				p->show_scan_result = 1;
			}
			else if( strncmp(scan, "l", 1) == 0 )	// Set Listen channel
			{
				char msg[CMD_SZ];
				memset( msg, 0x00, CMD_SZ );
				strcpy( msg, "Please insert desired Listen channel, only ch.1.6.11 are available:" );			

				p2p_listen_ch(p, msg);
				p->show_scan_result = 1;
			}
			else if( strncmp(scan, "q", 1) == 0 )	// Quit
			{
				if( p->res == 0 )
					p->res = 1;
				if( p->res_go == 0 )
					p->res_go = 1;
				break;
			}
			else	// Insert wrong commamd
			{
				p->p2p_get=1;
				p->show_scan_result = 1;
				memset( p->print_line, 0x00, CMD_SZ );
				sprintf( p->print_line, " BAD argument");
			}
			
		}
		else if( p->thread_trigger == THREAD_DEVICE )		//Passive mode for user interface
		{
			
			p->thread_trigger = THREAD_NONE ;
			
			if( strncmp(scan, "b", 1) == 0 )
			{
				p->wps_info=3;
				p2p_wpsinfo(p);
				
				p2p_status(p, 0);
				
				if(p->status != P2P_STATE_GONEGO_OK)
				{
					p2p_set_nego(p);
				}
				else
				{
					p2p_role(p,0);
										
					if( p->role == P2P_ROLE_CLIENT )
					{
						p2p_client_mode(p);
					}
					else if( p->role == P2P_ROLE_GO )
					{
						p2p_go_mode(p);
					}
				}
			}
			else if( strncmp(scan, "c", 1) == 0 )
			{
				p->wps_info=2;
				p2p_wpsinfo(p);
				
				p2p_status(p, 0);
							
				if(p->status != P2P_STATE_GONEGO_OK)
				{
					p2p_set_nego(p);
				}					
				else
				{
					p2p_role(p,0);
					p2p_ifaddr(p);
										
					if( p->role == P2P_ROLE_CLIENT )
					{
						p2p_client_mode(p);
					}
					else if( p->role == P2P_ROLE_GO )
					{
						p2p_go_mode(p);
					}
				}
			}
			else if( ('0' <= *scan ) && ( *scan <= '9') )
			{
				printf("In passive pin code\n");
				
				p->pin = atoi(scan);
				
				p->wps_info=1;
				p2p_wpsinfo(p);
				
				p2p_set_nego(p);
			}
		}
		else if( p->thread_trigger == THREAD_GO )		//Passive mode for user interface
		{
			
			p->thread_trigger = THREAD_NONE ;
			
			if( strncmp(scan, "b", 1) == 0 )
			{
				p->wps_info=3;
				p2p_wpsinfo(p);

			}
			else if( strncmp(scan, "c", 1) == 0 )
			{
				p->wps_info=2;
				p2p_wpsinfo(p);
			}
			else if( ('0' <= *scan ) && ( *scan <= '9') )
			{
				printf("In passive pin code\n");
				
				p->pin = atoi(scan);
				
				p->wps_info=1;
				p2p_wpsinfo(p);
			}
			
			p2p_go_mode(p);
			
		}
	}
	while( 1 );

	/* Disable P2P functionalities when exits*/
	p->enable= -1 ;
	p2p_enable(p);

	system( "rm -f ./supp_status.txt" );
	system( "rm -f ./temp.txt" );
	system( "rm -f ./scan.txt" );
	system( "rm -f ./peer.txt" );
	system( "rm -f ./status.txt" );
	system( "rm -f ./cm.txt" );
	
	return 0;

}
Esempio n. 12
0
int doWifiDirect(void) {
    char	peerifa[40] = { 0x00 };
	char	scan[CMD_SZ];	
	struct p2p p2pstruct;
	struct p2p *p=NULL;

	p = &p2pstruct;	
	if( p != NULL)
	{
		memset(p, 0x00, sizeof(struct p2p));
		init_p2p(p);
	}

	strcpy(p->ifname, "wlan0" );
	
	/* Disable P2P functionalities at first*/
	p->enable=P2P_ROLE_DISABLE;
	p2p_enable(p);
	p2p_get_hostapd_conf(p);
	usleep(50000);
  
  	rename_intf(p);
  
	do
	{
		system("clear");

		memset( scan, 0x00, CMD_SZ );

		if( p->thread_trigger == THREAD_NONE )		//Active mode for user interface
		{
		
			system("clear"); 

			p->enable = 1;

			p2p_enable(p);

			p2p_scan(p);
			usleep(500000);

			system("clear");
			p2p_devaddr(p);

			system("echo \"discovering device\""); //Connecting to device
			char msg[CMD_SZ];
			memset( msg, 0x00, CMD_SZ );
			char dis_msg[CMD_SZ];
			memset( dis_msg, 0x00, CMD_SZ );
			char label_msg[CMD_SZ];
			memset( label_msg, 0x00, CMD_SZ );


			if(strncmp(p->peer_devaddr, "00:00:00:00:00:00", 17) == 0)
			{
				strcpy( msg, "Please insert peer P2P device at first" );			

				p2p_prov_disc_no_addr(p, msg);
				p->show_scan_result = 1;
			}
			else
			{
				strcpy( msg, "Please insert WPS configuration method ;[0]display, [1]keypad, [2]pbc, [3]label:\n" );
				strcpy( dis_msg, "Please insert PIN code displays on peer device screen:" );
				strcpy( label_msg, "Please insert PIN code displays on peer label:" );

				p2p_prov_disc(p, msg, dis_msg, label_msg);
			}

			system("echo \"connection completed\""); //Exit Program
			if( p->res == 0 )
				p->res = 1;
			if( p->res_go == 0 )
				p->res_go = 1;
			break;
			
		}
		else if( p->thread_trigger == THREAD_DEVICE )		//Passive mode for user interface
		{
			
			p->thread_trigger = THREAD_NONE ;
			
			if( strncmp(scan, "b", 1) == 0 )
			{
				p->wps_info=3;
				p2p_wpsinfo(p);
				
				p2p_status(p, 0);
				
				if(p->status != P2P_STATE_GONEGO_OK)
				{
					p2p_set_nego(p);
				}
				else
				{
					p2p_role(p,0);
										
					if( p->role == P2P_ROLE_CLIENT )
					{
						p2p_client_mode(p);
					}
					else if( p->role == P2P_ROLE_GO )
					{
						p2p_go_mode(p);
					}
				}
			}
			else if( strncmp(scan, "c", 1) == 0 )
			{
				p->wps_info=2;
				p2p_wpsinfo(p);
				
				p2p_status(p, 0);
							
				if(p->status != P2P_STATE_GONEGO_OK)
				{
					p2p_set_nego(p);
				}					
				else
				{
					p2p_role(p,0);
					p2p_ifaddr(p);
										
					if( p->role == P2P_ROLE_CLIENT )
					{
						p2p_client_mode(p);
					}
					else if( p->role == P2P_ROLE_GO )
					{
						p2p_go_mode(p);
					}
				}
			}
			else if( ('0' <= *scan ) && ( *scan <= '9') )
			{
				printf("In passive pin code\n");
				
				p->pin = atoi(scan);
				
				p->wps_info=1;
				p2p_wpsinfo(p);
				
				p2p_set_nego(p);
			}
		}
		else if( p->thread_trigger == THREAD_GO )		//Passive mode for user interface
		{
			
			p->thread_trigger = THREAD_NONE ;
			
			if( strncmp(scan, "b", 1) == 0 )
			{
				p->wps_info=3;
				p2p_wpsinfo(p);

			}
			else if( strncmp(scan, "c", 1) == 0 )
			{
				p->wps_info=2;
				p2p_wpsinfo(p);
			}
			else if( ('0' <= *scan ) && ( *scan <= '9') )
			{
				printf("In passive pin code\n");
				
				p->pin = atoi(scan);
				
				p->wps_info=1;
				p2p_wpsinfo(p);
			}
			
			p2p_go_mode(p);
			
		}
	}
	while( 1 );

	/* Disable P2P functionalities when exits*/
	p->enable= -1 ;
	p2p_enable(p);

	system( "rm -f ./supp_status.txt" );
	system( "rm -f ./temp.txt" );
	system( "rm -f ./scan.txt" );
	system( "rm -f ./peer.txt" );
	system( "rm -f ./status.txt" );
	system( "rm -f ./cm.txt" );
	
	return 0;
}