Skip to content

Commit f3c0490

Browse files
committed
Add WD implementation.
1 parent 2c1b6df commit f3c0490

3 files changed

Lines changed: 318 additions & 1 deletion

File tree

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ OGCOBJ := \
152152
console_font_8x16.o timesupp.o lock_supp.o usbgecko.o usbmouse.o \
153153
sbrk.o malloc_lock.o kprintf.o stm.o aes.o sha.o ios.o es.o isfs.o usb.o network_common.o \
154154
sdgecko_io.o sdgecko_buf.o gcsd.o argv.o network_wii.o wiisd.o conf.o usbstorage.o \
155-
texconv.o wiilaunch.o sys_report.o
155+
texconv.o wiilaunch.o wd.o sys_report.o
156156

157157
#---------------------------------------------------------------------------------
158158
MODOBJ := freqtab.o mixer.o modplay.o semitonetab.o gcmodplay.o

gc/ogc/wd.h

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*-----------------------------------------------------------------------
2+
3+
wd.h -- Wireless Driver Implementation.
4+
5+
Copyright:
6+
7+
- (C) 2025-2026 B. Abdelali (Abdelali221) (Author)
8+
- (C) 2008 Dolphin Emulator Project (For providing data structures)
9+
10+
This software is provided 'as-is', without any express or implied
11+
warranty. In no event will the authors be held liable for any
12+
damages arising from the use of this software.
13+
14+
Permission is granted to anyone to use this software for any
15+
purpose, including commercial applications, and to alter it and
16+
redistribute it freely, subject to the following restrictions:
17+
18+
1. The origin of this software must not be misrepresented; you
19+
must not claim that you wrote the original software. If you use
20+
this software in a product, an acknowledgment in the product
21+
documentation would be appreciated but is not required.
22+
23+
2. Altered source versions must be plainly marked as such, and
24+
must not be misrepresented as being the original software.
25+
26+
3. This notice may not be removed or altered from any source
27+
distribution.
28+
29+
-----------------------------------------------------------------------*/
30+
31+
#ifndef __WD_H_
32+
#define __WD_H_
33+
#include <gccore.h>
34+
35+
#define SSID_LENGTH 32
36+
#define BSSID_LENGTH 6
37+
38+
enum MODES
39+
{
40+
Unintialized = 0,
41+
DSCommunications = 1,
42+
NA0 = 2,
43+
AOSSAPScan = 3,
44+
NA1 = 4,
45+
NA2 = 5,
46+
NA3 = 6
47+
};
48+
49+
typedef struct WDInfo
50+
{
51+
u8 MAC[6];
52+
u16 EnableChannelsMask;
53+
u16 NTRallowedChannelsMask;
54+
u8 CountryCode[4];
55+
u8 channel;
56+
u8 initialized;
57+
u8 version[80];
58+
u8 unknown[48];
59+
} WDInfo;
60+
61+
typedef struct ScanParameters
62+
{
63+
u16 ChannelBitmap;
64+
u16 MaxChannelTime;
65+
u8 BSSID[BSSID_LENGTH];
66+
u16 ScanType;
67+
68+
u16 SSIDLength;
69+
u8 SSID[SSID_LENGTH];
70+
u8 SSIDMatchMask[6];
71+
72+
} ScanParameters;
73+
74+
typedef struct BSSDescriptor
75+
{
76+
u16 length;
77+
u16 RSSI;
78+
u8 BSSID[6];
79+
u16 SSIDLength;
80+
u8 SSID[32];
81+
u16 Capabilities;
82+
struct
83+
{
84+
u16 basic;
85+
u16 support;
86+
} rateSet;
87+
u16 beacon_period;
88+
u16 DTIM_period;
89+
u16 channel;
90+
u16 CF_period;
91+
u16 CF_max_duration;
92+
u16 element_info_length;
93+
u16 element_info[1];
94+
} BSSDescriptor;
95+
96+
s32 NCD_LockWirelessDriver();
97+
u32 NCD_UnlockWirelessDriver(s32 lockid);
98+
int WD_Init(u8 mode);
99+
void WD_Deinit();
100+
int WD_GetInfo(WDInfo* inf);
101+
u8 WD_GetRadioLevel(BSSDescriptor* Bss);
102+
int WD_Scan(ScanParameters *settings, u8* buff, u16 buffsize);
103+
int WD_ScanOnce(ScanParameters *settings, u8* buff, u16 buffsize);
104+
void WD_SetDefaultScanParameters(ScanParameters* set);
105+
106+
#endif

libogc/wd.c

Lines changed: 211 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,211 @@
1+
/*-----------------------------------------------------------------------
2+
3+
wd.h -- Wireless Driver Implementation.
4+
5+
Copyright (C) 2025-2026 B. Abdelali (Abdelali221)
6+
7+
This software is provided 'as-is', without any express or implied
8+
warranty. In no event will the authors be held liable for any
9+
damages arising from the use of this software.
10+
11+
Permission is granted to anyone to use this software for any
12+
purpose, including commercial applications, and to alter it and
13+
redistribute it freely, subject to the following restrictions:
14+
15+
1. The origin of this software must not be misrepresented; you
16+
must not claim that you wrote the original software. If you use
17+
this software in a product, an acknowledgment in the product
18+
documentation would be appreciated but is not required.
19+
20+
2. Altered source versions must be plainly marked as such, and
21+
must not be misrepresented as being the original software.
22+
23+
3. This notice may not be removed or altered from any source
24+
distribution.
25+
26+
-----------------------------------------------------------------------*/
27+
28+
#include "wd.h"
29+
#include "ipc.h"
30+
#include <stdio.h>
31+
#include <stdlib.h>
32+
#include <gccore.h>
33+
#include <string.h>
34+
#include <ogc/machine/processor.h>
35+
36+
#define WD_HEAP_SIZE 0x1000
37+
38+
extern void usleep(u32 t);
39+
40+
s32 wd_fd = -1;
41+
s32 wd_heap = -1;
42+
43+
s32* WD_GetWork() {
44+
return &wd_fd;
45+
}
46+
47+
u8 NCDcommonbuff[0x20] __attribute__((aligned(32)));
48+
s32 NCDheap;
49+
50+
s32 NCD_LockWirelessDriver() {
51+
s32 NCD = IOS_Open("/dev/net/ncd/manage", 0);
52+
53+
memset(NCDcommonbuff, 0, 0x20);
54+
55+
ioctlv vector = {0};
56+
vector.data = NCDcommonbuff;
57+
vector.len = 0x20;
58+
59+
IOS_Ioctlv(NCD, 1, 0, 1, &vector);
60+
61+
s32 lockid = 0;
62+
memcpy(&lockid, NCDcommonbuff, 4);
63+
64+
IOS_Close(NCD);
65+
NCD = -1;
66+
67+
return lockid;
68+
}
69+
70+
u32 NCD_UnlockWirelessDriver(s32 lockid) {
71+
s32 NCD = IOS_Open("/dev/net/ncd/manage", 0);
72+
if (NCD < 0) return -1;
73+
74+
u8 NCDresult[0x20] __attribute__((aligned(32)));
75+
memcpy(NCDresult, &lockid, 4);
76+
77+
ioctlv vectors[2] = {0};
78+
vectors[1].data = NCDresult;
79+
vectors[1].len = 4;
80+
vectors[0].data = NCDcommonbuff;
81+
vectors[0].len = 0x20;
82+
83+
IOS_Ioctlv(NCD, 2, 1, 1, vectors);
84+
85+
u32 ret;
86+
memcpy(&ret, NCDresult, 4);
87+
88+
IOS_Close(NCD);
89+
90+
NCD = -1;
91+
return ret;
92+
}
93+
94+
s32 WD_CreateHeap() {
95+
s32 heap;
96+
u32 level;
97+
98+
_CPU_ISR_Disable(level);
99+
heap = iosCreateHeap(WD_HEAP_SIZE);
100+
_CPU_ISR_Restore(level);
101+
102+
return heap;
103+
}
104+
105+
void WD_SetDefaultScanParameters(ScanParameters* set) {
106+
set->ChannelBitmap = 0xfffe;
107+
set->MaxChannelTime = 100; // 100 ms
108+
109+
memset(set->BSSID, 0xff, BSSID_LENGTH);
110+
111+
set->ScanType = 0;
112+
set->SSIDLength = 0;
113+
114+
memset(set->SSID, 0x00, SSID_LENGTH);
115+
memset(set->SSIDMatchMask, 0xff, 6);
116+
}
117+
118+
int WD_Init(u8 mode) {
119+
if(wd_fd < 0) {
120+
wd_fd = IOS_Open("/dev/net/wd/command", 0x10000 | mode);
121+
if (wd_fd < 0) return -1;
122+
}
123+
return 0;
124+
}
125+
126+
void WD_Deinit() {
127+
if(wd_fd < 0) return;
128+
129+
u32 level;
130+
131+
_CPU_ISR_Disable(level);
132+
IOS_Close(*WD_GetWork());
133+
wd_fd = -1;
134+
_CPU_ISR_Restore(level);
135+
}
136+
137+
u8 WD_GetRadioLevel(BSSDescriptor* Bss) {
138+
u8 ret;
139+
140+
if (Bss->RSSI < 0xc4) {
141+
if (Bss->RSSI < 0xb5) {
142+
if (Bss->RSSI < 0xab) {
143+
ret = 0;
144+
} else {
145+
ret = 1;
146+
}
147+
} else {
148+
ret = 2;
149+
}
150+
} else {
151+
ret = 3;
152+
}
153+
return ret;
154+
}
155+
156+
int WD_GetInfo(WDInfo* info) {
157+
s32 lockid = NCD_LockWirelessDriver();
158+
159+
if(WD_Init(AOSSAPScan) < 0) return -1;
160+
161+
u8 inf[sizeof(WDInfo)] __attribute__((aligned(32)));
162+
163+
ioctlv vector;
164+
vector.data = inf;
165+
vector.len = sizeof(WDInfo);
166+
167+
IOS_Ioctlv(wd_fd, 0x100e, 0, 1, &vector);
168+
memcpy(info, inf, sizeof(WDInfo));
169+
170+
WD_Deinit();
171+
NCD_UnlockWirelessDriver(lockid);
172+
173+
return 0;
174+
}
175+
176+
int WD_Scan(ScanParameters *settings, u8* buff, u16 buffsize) {
177+
if(wd_fd < 0) return -1;
178+
179+
u8 buf[buffsize + 2] __attribute__((aligned(32)));
180+
u8 settingsbuf[0x4e] __attribute__((aligned(32)));
181+
182+
memset(settingsbuf, 0, 0x4e);
183+
memset(buf, 0, buffsize + 2);
184+
memcpy(settingsbuf, settings, 0x1a);
185+
186+
ioctlv vectors[2];
187+
vectors[0].data = settingsbuf;
188+
vectors[0].len = 0x4e;
189+
vectors[1].data = buf;
190+
vectors[1].len = buffsize;
191+
192+
IOS_Ioctlv(wd_fd, 0x100a, 1, 1, vectors);
193+
usleep(100000);
194+
memcpy(buff, buf, buffsize);
195+
196+
return 0;
197+
}
198+
199+
int WD_ScanOnce(ScanParameters *settings, u8* buff, u16 buffsize) {
200+
s32 lockid = NCD_LockWirelessDriver();
201+
202+
if(WD_Init(AOSSAPScan) < 0) return -1;
203+
usleep(100000);
204+
205+
WD_Scan(settings, buff, buffsize);
206+
207+
WD_Deinit();
208+
NCD_UnlockWirelessDriver(lockid);
209+
210+
return 0;
211+
}

0 commit comments

Comments
 (0)