summaryrefslogtreecommitdiff
path: root/fpcsrc/packages/libndsfpc/examples/dswifi/ap_search/apSearch.pp
blob: de76752e1de6d38be92408d4a0437f8d6613902e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
program apSearch;

{$mode objfpc}

uses
  ctypes, nds9, dswifi9;

procedure findAP(ap: pWifi_AccessPoint); 
var
	selected, i, count: integer;
	ap2: Wifi_AccessPoint;
begin
  selected := 0;  
  count := 0;

  Wifi_ScanMode(); //this allows us to search for APs
  
  while ((keysDown() and KEY_A) = 0) do
  begin
    scanKeys();
    
    //find out how many APs there are in the area
    count := Wifi_GetNumAP();
    consoleClear();
    
    iprintf('Number of APs found: %d'#10, count);
    
    //display the APs to the user
    for i := 0 to count - 1 do
    begin
      Wifi_GetAPData(i, @ap2);
      // display the name of the AP
      if i = selected then
        iprintf('%s %s'#10, '*', pcchar(ap2.ssid))
      else
        iprintf('%s %s'#10, ' ', pcchar(ap2.ssid));
      
    end;

		//move the selection asterick
		if ((keysDown() and KEY_UP) <> 0) and (selected > 0) then 
      dec(selected);

		if ((keysDown() and KEY_DOWN) <> 0) and (selected < (count-1)) then 
      inc(selected);

		swiWaitForVBlank();
	end;

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

end;

//---------------------------------------------------------------------------------
procedure keyPressed(c: cint);
begin
  if (c > 0) then
    iprintf('%c', c);
end;

var
  ap3: pWifi_AccessPoint;
  status: integer;
  kb: pKeyboard;
  oldStatus: integer;
	url: array [0..255] of char;
  host: phostent;

begin
  status := integer(ASSOCSTATUS_DISCONNECTED);
  
  consoleDemoInit(); 

  new(kb);
  kb := keyboardDemoInit();
  kb^.OnKeyPressed := @keyPressed;
  
  Wifi_InitDefault(false);
  
  findAP(ap3);
  	
  iprintf('Connecting to %s'#10, pcchar(ap3^.ssid));
  
  //this tells the wifi lib to use dhcp for everything
  Wifi_SetIP(0,0,0,0,0);	
  
  Wifi_ConnectAP(ap3, integer(WEPMODE_NONE), 0, nil);

  while (status <> integer(ASSOCSTATUS_ASSOCIATED)) and (status <> integer(ASSOCSTATUS_CANNOTCONNECT)) do
  begin
    oldStatus := status;
    
    status := Wifi_AssocStatus();
    if oldStatus <> status then
      iprintf('%s', pchar(@ASSOCSTATUS_STRINGS[status]))
    else 
      iprintf('%s', '.');
    
    swiWaitForVBlank();
  end;

	consoleClear();
	consoleSetWindow(nil, 0,0,32,10);


  while true do
  begin
    iprintf('Url? ');
    
    scanf('%s', url);
    
    host := gethostbyname(url);
    
    if (host) <> nil then
    	iprintf('IP (%s) : %s'#10,  url, inet_ntoa(in_addr(host^.h_addr_list^)))
    else
    	iprintf('Could not resolve'#10);
    
    swiWaitForVBlank();
  end;

end.