apSearch.pp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. program apSearch;
  2. {$mode objfpc}
  3. uses
  4. ctypes, nds9, dswifi9;
  5. var
  6. ap: pWifi_AccessPoint;
  7. function findAP(): pWifi_AccessPoint;
  8. var
  9. selected, i, count, displaytop: integer;
  10. ap2: Wifi_AccessPoint;
  11. pressed: cint;
  12. displayend: integer;
  13. s1, s2: string;
  14. begin
  15. selected := 0;
  16. count := 0;
  17. displaytop := 0;
  18. Wifi_ScanMode(); //this allows us to search for APs
  19. pressed := 0;
  20. while ((pressed and KEY_A) = 0) do
  21. begin
  22. scanKeys();
  23. pressed := keysDown();
  24. if (pressed and KEY_START) <> 0 then exit;
  25. //find out how many APs there are in the area
  26. count := Wifi_GetNumAP();
  27. consoleClear();
  28. iprintf('%d APs detected'#10, count);
  29. displayend := displaytop + 10;
  30. if (displayend > count) then displayend := count;
  31. //display the APs to the user
  32. for i := displaytop to displayend - 1 do
  33. begin
  34. Wifi_GetAPData(i, ap);
  35. // display the name of the AP
  36. if i = selected then
  37. s1 := '*'
  38. else
  39. s1 := ' ';
  40. if (ap.flags and WFLAG_APDATA_WEP) <> 0 then
  41. s2 := 'Yes '
  42. else
  43. s2 := 'No ';
  44. iprintf('%s %.29s'#10' Wep:%s Sig:%i'#10, s1, pcchar(ap^.ssid), s2, ap.rssi * 100 div $D0);
  45. end;
  46. //move the selection asterick
  47. if ((pressed and KEY_UP) <> 0) and (selected > 0) then
  48. dec(selected);
  49. if ((pressed and KEY_DOWN) <> 0) and (selected < (count-1)) then
  50. inc(selected);
  51. swiWaitForVBlank();
  52. end;
  53. //user has made a choice so grab the ap and return it
  54. Wifi_GetAPData(selected, ap);
  55. result := ap;
  56. end;
  57. //---------------------------------------------------------------------------------
  58. procedure keyPressed(c: cint);
  59. begin
  60. if (c > 0) then
  61. iprintf('%c', c);
  62. end;
  63. var
  64. ap3: pWifi_AccessPoint;
  65. status: integer;
  66. kb: pKeyboard;
  67. oldStatus: integer;
  68. url: array [0..255] of char;
  69. host: phostent;
  70. wepkey = array [0..63] of char;
  71. wepmode: cint;
  72. len: integer;
  73. ip: cuint32;
  74. quit: integer;
  75. pressed: cint;
  76. begin
  77. Wifi_InitDefault(false);
  78. consoleDemoInit();
  79. new(kb);
  80. kb := keyboardDemoInit();
  81. kb^.OnKeyPressed := @keyPressed;
  82. while true do
  83. begin
  84. status := integer(ASSOCSTATUS_DISCONNECTED);
  85. consoleClear();
  86. consoleSetWindow(nil, 0,0,32,24);
  87. ap3 := findAp();
  88. consoleClear();
  89. consoleSetWindow(nil, 0,0,32,10);
  90. iprintf('Connecting to %s'#10, pcchar(ap3^.ssid));
  91. //this tells the wifi lib to use dhcp for everything
  92. Wifi_SetIP(0,0,0,0,0);
  93. wepmode := WEPMODE_NONE;
  94. if (ap3^.flags and WFLAG_APDATA_WEP) <> 0 then
  95. begin
  96. iprintf('Enter Wep Key'#10);
  97. while (wepmode = WEPMODE_NONE) do
  98. begin
  99. scanf('%s', wepkey);
  100. if (strlen(wepkey) = 13) then
  101. wepmode := WEPMODE_128BIT;
  102. else if (strlen(wepkey) = 5) then
  103. wepmode := WEPMODE_40BIT;
  104. else
  105. iprintf('Invalid key!'#10);
  106. end;
  107. Wifi_ConnectAP(ap3, wepmode, 0, pcuint8(wepkey));
  108. end else
  109. Wifi_ConnectAP(ap3, integer(WEPMODE_NONE), 0, nil);
  110. consoleClear();
  111. while (status <> ASSOCSTATUS_ASSOCIATED) and (status <> ASSOCSTATUS_CANNOTCONNECT) do
  112. begin
  113. status := Wifi_AssocStatus();
  114. len := strlen(ASSOCSTATUS_STRINGS[status]);
  115. iprintf(#27'[0;0H\x1b[K');
  116. iprintf(#27'[0;%dH%s', (32-len) div 2, ASSOCSTATUS_STRINGS[status]);
  117. scanKeys();
  118. if (keysDown() and KEY_B) <> 0 then break;
  119. swiWaitForVBlank();
  120. end;
  121. if (status = ASSOCSTATUS_ASSOCIATED) then
  122. begin
  123. ip := Wifi_GetIP();
  124. iprintf(#10'ip: [%li.%li.%li.%li]'#10, (ip ) and $FF, (ip shr 8) and $FF, (ip shr 16) and $FF, (ip shr 24) and $FF);
  125. while true do
  126. begin
  127. scanf('%s', url);
  128. if (strcmp(url, 'quit') = 0) then break;
  129. host := gethostbyname(url);
  130. if(host <> nil) then
  131. iprintf('IP (%s) : %s'#10, url, inet_ntoa(pin_addr(host)^.h_addr_list[0]))
  132. else
  133. iprintf('Could not resolve'#10);
  134. swiWaitForVBlank();
  135. end;
  136. end else
  137. iprintf(#10'Connection failed!'#10);
  138. quit := 0;
  139. iprintf('Press A to try again, B to quit.');
  140. while true do
  141. begin
  142. swiWaitForVBlank();
  143. scanKeys();
  144. pressed := keysDown();
  145. if(pressed and KEY_B) <> 0 then quit := 1;
  146. if(pressed and (KEY_A or KEY_B)) <> 0 then break;
  147. end;
  148. if (quit) <> 0 then break;
  149. end;
  150. end.