From baa55c2ab6e757b61b4982b5638dfc4cdfbb8324 Mon Sep 17 00:00:00 2001 From: Abdelali221 Date: Thu, 22 Jan 2026 23:13:11 +0100 Subject: [PATCH 1/3] Add comments to explain the code + Change the UI a bit. --- devices/wifi/source/main.c | 81 +++++++++++++++++++++++++++++--------- 1 file changed, 63 insertions(+), 18 deletions(-) diff --git a/devices/wifi/source/main.c b/devices/wifi/source/main.c index 7760f64..e6a194b 100644 --- a/devices/wifi/source/main.c +++ b/devices/wifi/source/main.c @@ -2,7 +2,6 @@ #include #include #include -#include static void *xfb = NULL; static GXRModeObj *rmode = NULL; @@ -11,7 +10,10 @@ void ParseScanBuff(u8* ScanBuff, u16 ScanBuffSize) { u16 APs = ScanBuff[0] << 8 | ScanBuff[1]; BSSDescriptor* ptr = (BSSDescriptor*)((u32)ScanBuff + 2); - if(APs) { + + // We check if the first two bytes of the buffer aren't 0. + // The bytes makes a 16 bit value that represents the number of APs detected. + if(APs) { printf("Found %d APs", APs); } else { printf("No APs were found."); @@ -21,26 +23,52 @@ void ParseScanBuff(u8* ScanBuff, u16 ScanBuffSize) { char ssid[32]; - WD_GetIE(ptr, IEID_SSID, (u8*)ssid, 32); + WD_GetIE(ptr, IEID_SSID, (u8*)ssid, 32); // Can be retrieved with ptr->SSID instead. printf("\n\n AP %d", i + 1); printf("\n\tBSSID : %02X:%02X:%02X:%02X:%02X:%02X", ptr->BSSID[0], ptr->BSSID[1], ptr->BSSID[2], ptr->BSSID[3], ptr->BSSID[4], ptr->BSSID[5]); printf("\n\tSSID : %s", ssid); - printf("\n\tRSSI : -%d", 65535 - ptr->RSSI); + printf("\n\tStrength : "); + switch(WD_GetRadioLevel(ptr)) { + case WD_SIGNAL_STRONG: + printf("Strong"); + break; + + case WD_SIGNAL_NORMAL: + printf("Normal"); + break; + + case WD_SIGNAL_FAIR: + printf("Fair"); + break; + + case WD_SIGNAL_WEAK: + printf("Weak"); + break; + } printf("\n\tChannel : %d", ptr->channel); - printf("\n\tNum of IEs : %d", WD_GetNumberOfIEs(ptr)); - u8 len = 0; - WD_GetIELength(ptr, 0x30, &len); - printf("\n\tIE 0x30 size : 0x%X", len); printf("\n\tIs Encrypted? %s\n", ptr->Capabilities & CAPAB_SECURED_FLAG ? "Yes" : "No"); + + // Sometimes length can be 0, which is wrong. + // And in that case we can use ptr->IEs_length to get the correct length. if (ptr->length == 0) { if((ptr->IEs_length + 0x3E) % 2 == 0) { ptr = (BSSDescriptor*)((u32)ptr + ptr->IEs_length + 0x3E); } else { ptr = (BSSDescriptor*)((u32)ptr + ptr->IEs_length + 0x3F); } - } else { + } else { // If it's not then we just use it as it is. + // Doubling ptr->length seems to match the BSSDescriptor length + IEs_length. ptr = (BSSDescriptor*)((u32)ptr + ptr->length*2); - } + } + + if(APs > 1) { + printf("\n\tPress A to get the next AP..."); + while(1) { + WPAD_ScanPads(); + u32 pressed = WPAD_ButtonsDown(0); + if (pressed & WPAD_BUTTON_A) break; + } + } } } @@ -51,7 +79,7 @@ int main(int argc, char *argv[]) { xfb = MEM_K0_TO_K1(SYS_AllocateFramebuffer(rmode)); - console_init(xfb,20,20,rmode->fbWidth,rmode->xfbHeight,rmode->fbWidth*VI_DISPLAY_PIX_SZ); + console_init(xfb,20,20,rmode->fbWidth - 20,rmode->xfbHeight - 20,rmode->fbWidth*VI_DISPLAY_PIX_SZ); VIDEO_Configure(rmode); @@ -73,29 +101,46 @@ int main(int argc, char *argv[]) { WDInfo inf; + // In order to perform a scan, we need to get the EnableChannels Mask from + // WD_GetInfo. WD_GetInfo(&inf); printf("\n\n\t\tWD Info :\n\n"); printf("\tIs Initialized? %s\n", inf.initialized ? "Yes" : "No"); printf("\tChannels Mask 0x%X\n", inf.EnableChannelsMask); - printf("\tCountry Code : %s\n", inf.CountryCode); - printf("Press A on the wiimote to scan for wifi SSID's'\n"); - printf("Press Home to return to loader\n"); ScanParameters Param; + printf("\tCountry Code : %s\n\n", inf.CountryCode); + + printf("\tPress A on the Wiimote to scan for WiFi Access Points\n"); + printf("\tPress Home to return to loader\n"); + + ScanParameters Param; + + // We set the default scan parameters : WD_SetDefaultScanParameters(&Param); - Param.ChannelBitmap = inf.EnableChannelsMask; - u8 buff[4096] __attribute__((aligned(32))); + + // We set the channel bitmap with the EnableChannel mask we got from + // WD_GetInfo. + Param.ChannelBitmap = inf.EnableChannelsMask; + + // The buffer that will hold the data comming from WD_ScanOnce(). + // It needs to be big enough to hold the APs data. + u8 buff[4096]; while (1) { WPAD_ScanPads(); u32 pressed = WPAD_ButtonsDown(0); if (pressed & WPAD_BUTTON_A) { - printf("\n\n Scanning...\n"); + printf("\x1b[2J\n\n Scanning...\n"); + // WD_ScanOnce starts by locking WD then it initializes it. WD_ScanOnce(&Param, buff, 4096); + // After that it performs a scan using WD_Scan which performs an IOCTLV call + // to the wd_fd, when it returns the buffer we close WD and unlock the driver + // and we pass it to ParseScanBuff to print the content : ParseScanBuff(buff, 4096); } if (pressed & WPAD_BUTTON_HOME) break; } return 0; -} \ No newline at end of file +} From 1e2515d4444661d9bf4659ec663efab7c53dcd5b Mon Sep 17 00:00:00 2001 From: Abdelali221 Date: Fri, 23 Jan 2026 12:28:53 +0100 Subject: [PATCH 2/3] Make the message only appear before the last AP. --- devices/wifi/source/main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/devices/wifi/source/main.c b/devices/wifi/source/main.c index e6a194b..0efe917 100644 --- a/devices/wifi/source/main.c +++ b/devices/wifi/source/main.c @@ -61,7 +61,7 @@ void ParseScanBuff(u8* ScanBuff, u16 ScanBuffSize) ptr = (BSSDescriptor*)((u32)ptr + ptr->length*2); } - if(APs > 1) { + if(APs > 1 && APs > i + 1) { printf("\n\tPress A to get the next AP..."); while(1) { WPAD_ScanPads(); From 51611e2b4ecc51e3c0ba0d2c1b846319fd0a6811 Mon Sep 17 00:00:00 2001 From: Abdelali221 Date: Sat, 24 Jan 2026 12:28:50 +0100 Subject: [PATCH 3/3] Remove the "wait for user input" loop in ParseScanBuff + Change console_init parameters. --- devices/wifi/source/main.c | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/devices/wifi/source/main.c b/devices/wifi/source/main.c index 0efe917..c7e9e22 100644 --- a/devices/wifi/source/main.c +++ b/devices/wifi/source/main.c @@ -60,15 +60,6 @@ void ParseScanBuff(u8* ScanBuff, u16 ScanBuffSize) // Doubling ptr->length seems to match the BSSDescriptor length + IEs_length. ptr = (BSSDescriptor*)((u32)ptr + ptr->length*2); } - - if(APs > 1 && APs > i + 1) { - printf("\n\tPress A to get the next AP..."); - while(1) { - WPAD_ScanPads(); - u32 pressed = WPAD_ButtonsDown(0); - if (pressed & WPAD_BUTTON_A) break; - } - } } } @@ -79,7 +70,7 @@ int main(int argc, char *argv[]) { xfb = MEM_K0_TO_K1(SYS_AllocateFramebuffer(rmode)); - console_init(xfb,20,20,rmode->fbWidth - 20,rmode->xfbHeight - 20,rmode->fbWidth*VI_DISPLAY_PIX_SZ); + console_init(xfb,20,20,rmode->fbWidth,rmode->xfbHeight,rmode->fbWidth*VI_DISPLAY_PIX_SZ); VIDEO_Configure(rmode);