示例#1
0
void wardriving_loop()
{
	int num_aps, i, index, flags, pressed;
	touchPosition touchXY;
	Wifi_AccessPoint cur_ap;
	u32 lasttick;
	char state, display_state;
	/* Vars for AP_DISPLAY */
	int entry_n;
	struct AP_HT_Entry *entry = NULL;

	print_to_debug("Setting scan mode...");

	Wifi_ScanMode();
	state = STATE_SCANNING;
	display_state = STATE_CONNECTING;

	for (i = 0; i < 3; i++) {
		sizes[i] = DEFAULT_ALLOC_SIZE;
		num[i] = num_null[i] = 0;
		first_null[i] = -1;
		ap[i] = (struct AP_HT_Entry **)
		    malloc(sizes[i] * sizeof(struct AP_HT_Entry *));
		if (ap[i] == NULL)
			abort_msg("alloc failed");
	}

	flags = DISP_WPA | DISP_OPN | DISP_WEP;
	memset(modes, 0, sizeof(modes));
	strcpy(modes, "OPN+WEP+WPA");

	index = 0;

	TIMER0_CR = TIMER_ENABLE | TIMER_DIV_1024;
	TIMER1_CR = TIMER_ENABLE | TIMER_CASCADE;
	lasttick = tick();

	while (1) {
		switch (state) {
		case STATE_SCANNING:
			curtick = tick();

			/* Wait for VBL just before key handling and redraw */
			swiWaitForVBlank();
			scanKeys();
			pressed = keysDown();

			/* Handle stylus press to display more detailed infos 
			 * handle this before AP insertion, to avoid race
			 * conditions */
			if (pressed & KEY_TOUCH) {
				touchRead(&touchXY);
				/* Entry number : 8 pixels for text, 3 lines */
				entry_n = touchXY.py / 8 / 3;
				entry = cur_entries[entry_n];
#ifdef DEBUG
				printf_to_debug("Entry : Y : %d\n", entry_n);
				printf_to_debug("SSID : %s\n", entry->ap->ssid);
#endif
				if (entry) {
					state = STATE_AP_DISPLAY;
					//display_state = STATE_PACKET_INIT;
					display_state = STATE_CONNECTING;
					print_to_debug("Packet scan mode");
					print_to_debug(" A : try to connect");
					print_to_debug(" B : back to scan");
					break;
				}
			}

			num_aps = Wifi_GetNumAP();
			for (i = 0; i < num_aps; i++) {
				if (Wifi_GetAPData(i, &cur_ap) !=
				    WIFI_RETURN_OK)
					continue;
				insert_ap(&cur_ap);
			}

			/* Check timeouts every second */
			if (timeout && (curtick - lasttick > 1000)) {
				lasttick = tick();
				clean_timeouts(lasttick);
			}

			if (pressed & KEY_RIGHT)
				timeout += 5000;
			if (pressed & KEY_LEFT && timeout > 0)
				timeout -= 5000;

			if (pressed & KEY_DOWN)
				index++;
			if (pressed & KEY_UP && index > 0)
				index--;
			if (pressed & KEY_R
			    && (index + (DISPLAY_LINES - 1)) <= numap)
				index += DISPLAY_LINES - 1;
			if (pressed & KEY_L && index >= DISPLAY_LINES - 1)
				index -= DISPLAY_LINES - 1;

			if (pressed & KEY_B)
				flags ^= DISP_OPN;
			if (pressed & KEY_A)
				flags ^= DISP_WEP;
			if (pressed & KEY_X)
				flags ^= DISP_WPA;

			/* Update modes string */
			if (pressed & KEY_B || pressed & KEY_A
			    || pressed & KEY_X) {
				modes[0] = 0;
				if (flags & DISP_OPN)
					strcat(modes, "OPN+");
				if (flags & DISP_WEP)
					strcat(modes, "WEP+");
				if (flags & DISP_WPA)
					strcat(modes, "WPA+");
				modes[strlen(modes) - 1] = 0;	/* remove the + */
			}

			display_list(index, flags);
			break;

		case STATE_AP_DISPLAY:
			switch (display_state) {
			case STATE_CONNECTING:
				/* TODO:
				 * 1) default to packet display
				 * 2) try DHCP [DONE]
				 * 3) try default IPs
				 * 4) handle WEP ?
				 */
				/* Try to connect */
				if (!(entry->ap->flags & WFLAG_APDATA_WPA) &&
				    !(entry->ap->flags & WFLAG_APDATA_WEP)) {
					print_to_debug
					    ("Trying to connect to :");
					print_to_debug(entry->ap->ssid);
					if (entry->ap->rssi <= 40)
						print_to_debug
						    ("Warning : weak signal");
					print_to_debug("Press B to cancel");
					switch (connect_ap(entry->ap)) {
					case ASSOCSTATUS_ASSOCIATED:
						display_state =
						    STATE_CONNECTED_FIRST;
						break;

					default:
						print_to_debug("Cnx failed");
						state = STATE_SCANNING;
						Wifi_ScanMode();
					}
				} else {
					print_to_debug
					    ("WEP/WPA AP not supported");
					state = STATE_SCANNING;
					break;
				}
				break;

			case STATE_CONNECTED_FIRST:
				display_ap(entry->ap, 1);
				display_state = STATE_CONNECTED;
				break;

			case STATE_CONNECTED:
				display_ap(entry->ap, 0);
				break;

			case STATE_PACKET_INIT:
				memcpy(mac_filter, entry->ap->macaddr, 6);
				Wifi_SetChannel(entry->ap->channel);
				Wifi_RawSetPacketHandler(cap_handler);
				Wifi_SetPromiscuousMode(1);
				display_state = STATE_PACKET;
				break;

			case STATE_PACKET:
				Wifi_Update();
				if (valid_packet)
					print_to_debug("Un paquet !\n");
				else
					print_to_debug("No paquet !\n");
				break;
			}

			scanKeys();
			if (keysDown() & KEY_A && state == STATE_PACKET) {
				state = STATE_CONNECTING;
			}
			if (keysDown() & KEY_B) {
				print_to_debug("Back to scan mode");
				state = STATE_SCANNING;
				Wifi_RawSetPacketHandler(NULL);
				Wifi_SetPromiscuousMode(0);
				Wifi_ScanMode();
			}
			swiWaitForVBlank();
			break;
		}
	}
}
示例#2
0
int main(void)
{
	aplist *head = NULL;
	aplist *cur;
	
	bool attached = true;
	// change this to false if you are only testing servos
	bool wifi_scan = true;
	// change this for emulator 
	bool emulate = false;

	float pain = 0.9f;
	time_t update = 0;
	uint8 num;
	uint8 servo_pos = 0;
	uint8 current_servo = 1;
	unsigned char SERVO_PINS[3] = { SERVO_PIN1 ,SERVO_PIN2 ,SERVO_PIN3 } ;

	uint16 val[3] = { 0 };
	uint8 i;

	touchPosition touch;
	
	videoSetMode(MODE_4_2D);
	vramSetBankA(VRAM_A_MAIN_BG);

	// set up our bitmap background
	bgInit(3, BgType_Bmp16, BgSize_B16_256x256, 0,0);
	decompress(cclogoBitmap, BG_GFX,  LZ77Vram);

	// initialise lower screen for textoutput
	consoleDemoInit();

	if (emulate == false) {	

	  iprintf("Initializing WiFi.. ");
	  Wifi_InitDefault(false);
	  while (Wifi_CheckInit() != 1) {
	  }
	  Wifi_ScanMode();
	  iprintf("done\n");
	  
	  iprintf("Initializing DS brut.. ");
	  uart_init();
	  uart_set_spi_rate(1000);
	  iprintf("done\n\n\n");

	  iprintf("Using servo pins: %u %u %u\n\n", SERVO_PIN1, SERVO_PIN2, SERVO_PIN3);
	  iprintf("Default pain multiplier: %.2f\n\n", pain);
	  swiDelay(60000000);
	  while (1) {
		  
		  scanKeys();
		  touchRead(&touch);	
		  if (keysDown() & KEY_X) {
			  if (attached) {
				  servo_detach(SERVO_PIN1);
				  servo_detach(SERVO_PIN2);
				  servo_detach(SERVO_PIN3);
				  attached = false;
			  } else {
				  attached = true;
			  }
		  }
		  if (keysDown() & KEY_A) {
			  if (attached) {
				uint8 i = 0;
				for (i=0;i<3;i++) {
					servo_set(SERVO_PINS[i],0);
				}
			  }
			  //servo_set(SERVO_PIN1, 180-((rand() % 100)+(rand() % 50)+(rand() % 25)));
			  //servo_set(SERVO_PIN2, 180-((rand() % 100)+(rand() % 50)+(rand() % 25)));
			  //servo_set(SERVO_PIN3, 180-((rand() % 100)+(rand() % 50)+(rand() % 25)));
		  }
		  if (keysDown() & KEY_B) {
			  if (wifi_scan == true)	{
				  wifi_scan = false;
			  }
			  else {
				  wifi_scan = true;
			  }
		  }
		  if (keysDown() & KEY_DOWN) {
			  pain -= 0.1f;
			  if (pain < 0.0f) {
				  pain = 0.0f;
			  }
			  update = time(NULL);
		  }
		  if (keysDown() & KEY_UP) {
			  pain += 0.1f;
			  if (2.0f < pain) {
				  pain = 2.0f;
			  }
			  update = time(NULL);
		  }
		  if (keysDown() & KEY_L) {
			  current_servo += 1;
			  if (current_servo > 3) {
				  current_servo = 1;
			  }
		  }
		  consoleClear();
		  if (wifi_scan == true) {
			num = 0;
			cur = head;
			iprintf("\n");
			while (cur && num < 15) {
				// display
				if (!(cur->flags & 0x2)) {
					cur = cur->next;
					continue;
				}
				iprintf("%2u ", num);
				if (cur->ssid[0] == '\0') {
					iprintf("%02x%02x%02x%02x%02x%02x", cur->mac[0], cur->mac[1], cur->mac[2], cur->mac[3], cur->mac[4], cur->mac[5]);
				} else {
					iprintf("%s", cur->ssid);
				}
				iprintf(" @ %u", cur->rssi);
				if ((cur->flags & 0x06) == 0x06) {
					iprintf(" WPA");
				} else if (cur->flags & 0x02) {
					iprintf(" WEP");
				}
				
				// calculate servo commands
				if (attached && num < 3) {
						val[num] = (uint16)(cur->rssi*pain);
						if (180 < val[num]) {
							val[num] = 180;
						}
						iprintf(" %u", val[num]);
				}
				
				iprintf("\n");
				if (num == 2) {
					iprintf ("\n");
				}
				
				num++;
				cur = cur->next;
			}
			iprintf("\n");
			if (time(NULL) < update+3) {
				printf("\npain multiplier: %.2f\n", pain);
			}
			
			// set the servo to zero if we don't have enough wifi nodes
			for (i=num; i<3; i++) {
				val[i] = 0;
			}
			
			if (attached) {
				servo_set(SERVO_PIN1, (uint8)val[0]);
				servo_set(SERVO_PIN2, 180-(uint8)val[1]);
				servo_set(SERVO_PIN3, (uint8)val[2]);
			}
			updateApList(&head, 0x00, NULL, NULL);
			sortApList(&head, false);

		  }

		  if (KEY_TOUCH && attached && !wifi_scan) {
			  servo_pos = (touch.rawx / 3850.) * 180;	
			  iprintf ("servo pos x: %d;", servo_pos);
			  iprintf ("\n");
			  servo_set(SERVO_PINS[current_servo-1],servo_pos);
			  iprintf ("current servo: %d", current_servo);
			  iprintf ("\n\n");
		  }
		  
		  
		  swiWaitForVBlank();
	  }
 	}	
while (1) {
		  swiWaitForVBlank();
}
	return 0;
}
示例#3
0
//---------------------------------------------------------------------------------
Wifi_AccessPoint* findAP(void){
//---------------------------------------------------------------------------------

	int selected = 0;  
	int i;
	int count = 0, displaytop = 0;

	static Wifi_AccessPoint ap;

	Wifi_ScanMode(); //this allows us to search for APs

	int pressed = 0;
	do {
		scanKeys();

		//find out how many APs there are in the area
		count = Wifi_GetNumAP();


		consoleClear();

		iprintf("%d APs detected\n\n", count);

		int displayend = displaytop + 10;
		if (displayend > count) displayend = count;

		//display the APs to the user
		for(i = displaytop; i < displayend; i++) {
			Wifi_AccessPoint ap;

			Wifi_GetAPData(i, &ap);

			// display the name of the AP
			iprintf("%s %.29s\n  Wep:%s Sig:%i\n", 
				i == selected ? "*" : " ", 
				ap.ssid, 
				ap.flags & WFLAG_APDATA_WEP ? "Yes " : "No ",
				ap.rssi * 100 / 0xD0);

		}

		pressed = keysDown();
		//move the selection asterick
		if(pressed & KEY_UP) {
			selected--;
			if(selected < 0) {
				selected = 0;
			}
			if(selected<displaytop) displaytop = selected;
		}

		if(pressed & KEY_DOWN) {
			selected++;
			if(selected >= count) {
				selected = count - 1;
			}
			displaytop = selected - 9;
			if (displaytop<0) displaytop = 0;
		}
		swiWaitForVBlank();
	} while(!(pressed & KEY_A));

	//user has made a choice so grab the ap and return it
	Wifi_GetAPData(selected, &ap);

	return &ap;
}
示例#4
0
//---------------------------------------------------------------------------------
Wifi_AccessPoint* findAP(void){
	//---------------------------------------------------------------------------------

	int selected = 0;  
	int i;
	int count = 0;

	static Wifi_AccessPoint ap;

	Wifi_ScanMode(); //this allows us to search for APs

	while(!(keysDown() & KEY_A))
	{
		scanKeys();

		//find out how many APs there are in the area
		count = Wifi_GetNumAP();

		consoleClear();

		iprintf("Number of APs found: %d\n", count);

		//display the APs to the user
		for(i = 0; i < count; i++)
		{
			Wifi_AccessPoint ap;

			Wifi_GetAPData(i, &ap);

			// display the name of the AP
			iprintf("%s %s Wep:%s Sig:%i\n", 
				i == selected ? "*" : " ", 
				ap.ssid, 
				ap.flags & WFLAG_APDATA_WEP ? "Yes " : "No ",
				ap.rssi * 100 / 0xD0);

		}

		//move the selection asterick
		if(keysDown() & KEY_UP)
		{
			selected--;
			if(selected < 0)
			{
				selected = 0;
			}
		}

		if(keysDown()&KEY_DOWN)
		{
			selected++;
			if(selected >= count) 
			{
				selected = count - 1;
			}
		}

		swiWaitForVBlank();
	}

	//user has made a choice so grab the ap and return it
	Wifi_GetAPData(selected, &ap);

	return &ap;
}