Had a play with this demo and it wouldn't work. It kept reporting the selected SSID as Null and therefore failed to connect. I'm no C wizard but I think that the variable ap's scope was a bit confusing as it was passed into and out of Function findAP bearing in mind that Function also declared ap as a local variable.
There's probably a much simpler resolution than the one I implemented, that is to make a Global variable (Global_Ap) and not pass it between Functions.
Code: Select all
/*---------------------------------------------------------------------------------
Simple wifi demo to locate and connect to an ap
-- dovoto
Modified by Doogle 20/12/2008 - Made Wifi_AccessPoint Global rather than
pass pointers to and from Functions
---------------------------------------------------------------------------------*/
#include <nds.h>
#include <stdio.h>
#include <dswifi9.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <netdb.h>
Wifi_AccessPoint Global_Ap;
//---------------------------------------------------------------------------------
void findAP() {
//---------------------------------------------------------------------------------
int selected = 0;
int i;
int count = 0;
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_GetAPData(i, &Global_Ap);
// display the name of the AP
iprintf("%s %s\n", i == selected ? "*" : " ", Global_Ap.ssid);
}
//move the selection asterick
if(keysDown() & KEY_UP && selected >0 ) selected--;
if(keysDown() & KEY_DOWN && selected < (count-1)) selected++;
swiWaitForVBlank();
}
//user has made a choice so grab the ap and return it
Wifi_GetAPData(selected, &Global_Ap);
}
//---------------------------------------------------------------------------------
void keyPressed(int c){
//---------------------------------------------------------------------------------
if(c > 0) iprintf("%c",c);
}
//---------------------------------------------------------------------------------
int main(void) {
//---------------------------------------------------------------------------------
int status = ASSOCSTATUS_DISCONNECTED;
consoleDemoInit();
Keyboard* kb = keyboardGetDefault();
kb->OnKeyPressed = keyPressed;
keyboardInit(kb);
Wifi_InitDefault(false);
findAP();
iprintf("Connecting to %s\n", Global_Ap.ssid);
//this tells the wifi lib to use dhcp for everything
Wifi_SetIP(0,0,0,0,0);
Wifi_ConnectAP(&Global_Ap, WEPMODE_NONE, 0, 0);
while(status != ASSOCSTATUS_ASSOCIATED && status != ASSOCSTATUS_CANNOTCONNECT) {
int oldStatus = status;
status = Wifi_AssocStatus();
iprintf("%s", oldStatus != status ? ASSOCSTATUS_STRINGS[status] : ".");
swiWaitForVBlank();
}
consoleClear();
consoleSetWindow(NULL, 0,0,32,10);
char url[256];
while(1)
{
iprintf("Url? ");
scanf("%s", url);
struct hostent *host = gethostbyname(url);
if(host)
iprintf("IP (%s) : %s\n", url, inet_ntoa(*(struct in_addr *)host->h_addr_list[0]));
else
iprintf("Could not resolve\n");
swiWaitForVBlank();
}
return 0;
}