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