1 /*---------------------------------------------------------------------------*
2   Project:  WD demo
3   File:     scan.c
4 
5   Copyright 2006 Nintendo. All rights reserved.
6 
7   These coded instructions, statements, and computer programs contain
8   proprietary information of Nintendo of America Inc. and/or Nintendo
9   Company Ltd., and are protected by Federal copyright law.  They may
10   not be disclosed to third parties or copied or duplicated in any form,
11   in whole or in part, without the prior written consent of Nintendo.
12 
13   $Log: scan.c,v $
14   Revision 1.12  2007/08/01 09:36:29  kitase_hirotake
15   TAB -> SPACE
16 
17   Revision 1.11  2006/11/14 00:08:22  terui
18   Made revision so that the buffer storing the scan result will be in MEM2.
19 
20   Revision 1.10  2006/09/27 09:46:44  terui
21   Changed the scan type to PASSIVE.
22   Made adjustment to the look of debug output.
23   Modified so that the signal strength of the discovered AP will be displayed.
24   Modified so that MP communication parent can be identified.
25 
26   Revision 1.9  2006/09/11 08:23:37  okajima_manabu
27   Corrected the reversed display of WPA2 AES and WPA AES. Miscellaneous minor adjustment to display related items.
28 
29   Revision 1.8  09/06/2006 02:31:12  yoshioka_yasuhiro
30   Added support for WD library.
31   Added support for encryption format scan.
32 
33   Revision 1.7  2006/08/29 09:57:34  terui
34   Changed the scan method and scan time specification.
35 
36   Revision 1.6  08/15/2006 03:51:15  yasu
37   Support for PADDING warnings.
38 
39   Revision 1.5  2006/08/09 00:36:19  yasu
40   NCDSleep->OSSleepTicks
41 
42   Revision 1.4  2006/08/08 01:19:10  yasu
43   Moved the wd related header under private.
44 
45   Revision 1.3  2006/07/18 09:25:25  terui
46   Fix appearance
47 
48   Revision 1.2  2006/07/13 12:39:07  terui
49   Fix appearance
50 
51   Revision 1.1  2006/07/13 12:16:23  terui
52   Initial upload
53 
54   $NoKeywords: $
55  *---------------------------------------------------------------------------*/
56 
57 #include <revolution/os.h>
58 #include <revolution/kpad.h>
59 #include <revolution/wd/WDScan.h>
60 #include "sample.h"
61 #include <string.h>     /* For memset(), memcpy() */
62 
63 /*---------------------------------------------------------------------------*
64     Constant Definitions
65  *---------------------------------------------------------------------------*/
66 #define     SCAN_BUF_SIZE       ( 2048 * 2 )
67 
68 /*---------------------------------------------------------------------------*
69     Internal variable definitions
70  *---------------------------------------------------------------------------*/
71 static u16          scanEnableChannel;
72 static WDScanParam  scanParam;
73 //static u16          scanResult[ SCAN_BUF_SIZE / sizeof( u16 ) ] ATTRIBUTE_ALIGN( 32 );
74 static u16*            scanResult;
75 
76 static const char*    privacyMode[] = {
77     "NONE     ",
78     "WEP40    ",
79     "WEP104   ",
80     "         ",
81     "WPA TKIP ",
82     "WPA2 AES ",
83     "WPA AES  ",
84     "WPA TKIP ",
85     "WEP      "
86 };
87 
88 
89 /*---------------------------------------------------------------------------*
90     Internal function prototype
91  *---------------------------------------------------------------------------*/
92 static void     ReportUsage( void );
93 static void     SetDefaultScanParam( WDScanParam* param );
94 static void     AnalyzeScanResult( u16* buf, u16 bufSize );
95 
96 /*---------------------------------------------------------------------------*
97   Name: main
98   Description: Entry point at time of program start.
99   Arguments: None
100   Returns: int  -  Returns the result of the process.
101  *---------------------------------------------------------------------------*/
102 int
main(void)103 main( void )
104 {
105     WDError err;
106     KPADStatus  input;
107 
108     SampleInit();
109     {
110         void*    arenaLo        = OSGetMEM2ArenaLo();
111 
112         scanResult    = (u16*)OSRoundUp32B( (u32)arenaLo );
113         arenaLo    = (void*)OSRoundUp32B( OSRoundUp32B( (u32)arenaLo ) + SCAN_BUF_SIZE );
114         OSSetMEM2ArenaLo( arenaLo );
115     }
116     SetDefaultScanParam( &scanParam );
117 
118     /* Scan scannable channels */
119     scanEnableChannel   = 0x0000;
120     err = WDCheckEnableChannel( &scanParam.channelBitmap );
121     if( err != WD_SUCCESS )
122         SampleReport( "ERROR(%d): StartupScanParam\n", err );
123 
124     ReportUsage();
125 
126     while( TRUE )
127     {
128         input.trig  = 0;
129         (void)KPADRead( WPAD_CHAN0, &input, 1 );
130 
131         /* (A) button */
132         if( input.trig & KPAD_BUTTON_A )
133         {
134             /* Execute scan */
135             err = WDScanOnce( scanResult, SCAN_BUF_SIZE, &scanParam );
136             if( err != WD_SUCCESS )
137             {
138                 OSReport( "ERROR(%d): UpdateScanInfo\n", err );
139                 if( err != WD_ERR_INSUFFICIENT_BUF )
140                     return err;
141             }
142             AnalyzeScanResult( scanResult, SCAN_BUF_SIZE );
143         }
144 
145         /* Draw and wait for V-Blank */
146         SampleWaitRetrace();
147     }
148 
149     OSHalt( "End of demo" );
150     return 0;
151 }
152 
153 /*---------------------------------------------------------------------------*
154   Name: ReportUsage
155   Description: Outputs how to operate the demo program.
156   Arguments: None
157   Returns: None
158  *---------------------------------------------------------------------------*/
159 static void
ReportUsage(void)160 ReportUsage( void )
161 {
162     SampleReport( "*******************************************************\n" );
163     SampleReport( " WD scan demo\n" );
164     SampleReport( " ---- Usage ----\n" );
165     SampleReport( "  (A) : Do scan\n" );
166     SampleReport( "*******************************************************\n" );
167 }
168 
169 /*---------------------------------------------------------------------------*
170   Name: SetDefaultScanParam
171   Description: Edits parameters used for scanning.
172   Arguments: param - Pointer to structure to be edited.
173   Returns: None.
174  *---------------------------------------------------------------------------*/
175 static void
SetDefaultScanParam(WDScanParam * param)176 SetDefaultScanParam( WDScanParam* param )
177 {
178     /* Specify the channel to be scanned using bit field format */
179     param->channelBitmap    = scanEnableChannel;
180     /* Specify the time to scan in units of ms */
181     param->maxChannelTime   = 100;
182     /* Specify the BSSIDs for which scan results are to be filtered (all 0xff means no filter) */
183     (void)memset( param->bssid, 0xff, WD_SIZE_BSSID );
184     /* Specify scan type */
185     param->scanType = WD_SCANTYPE_PASSIVE;
186 //    param->scanType = WD_SCANTYPE_ACTIVE;
187     /* Specify SSID for which scan results are to be filtered */
188     param->ssidLength   = 0;
189     (void)memset( param->ssid, 0x00, WD_SIZE_SSID );
190     (void)memset( param->ssidMatchMask, 0xff, WD_SIZE_SSID );
191 }
192 
193 /*---------------------------------------------------------------------------*
194   Name: AnalyzeScanResult
195   Description: Analyzes the scan result.
196   Arguments: buf     -   Buffer storing the scan result.
197                 bufSize -   Maximum size of the buffer.
198   Returns: None.
199  *---------------------------------------------------------------------------*/
200 static void
AnalyzeScanResult(u16 * buf,u16 bufSize)201 AnalyzeScanResult( u16* buf, u16 bufSize )
202 {
203     s32         i;
204     WDBssDesc*  p;
205     s32         size;
206 
207     if( buf[ 0 ] > 0 )
208     {
209         SampleReport( "-------------------------------------------------------------------------------\n" );
210         SampleReport( "ch lv ss   security  capa rateset   bssid             ssid\n" );
211         SampleReport( "-------------------------------------------------------------------------------\n" );
212         size    = sizeof( u16 );
213         p       = (WDBssDesc*)( (u32)buf + size );
214         for( i = 0; i < buf[ 0 ]; i ++ )
215         {
216             size    +=  ( p->length * 2 );
217             if( size > bufSize )
218             {
219                 break;
220             }
221             else
222             {
223                 s8  ssid[ WD_SIZE_SSID + 1 ];
224                 (void)memcpy( ssid, p->ssid, WD_SIZE_SSID );
225                 ssid[ WD_SIZE_SSID ]    = 0x00;
226                 SampleReport( "%2d %2u %s %s %s %04x/%04x %02x:%02x:%02x:%02x:%02x:%02x %s\n",
227                     p->channel,
228                     WDGetRadioLevel( (const WDBssDesc*)p ),
229                     ( WDFindGameInfo( NULL, NULL, (const WDBssDesc*)p ) ? "MP  "
230                         : ( ( p->capaInfo & 0x0003 ) == 0x0001 ? "ESS "
231                         : ( ( p->capaInfo & 0x0003 ) == 0x0002 ? "IBSS"
232                         : "    " ) ) ),
233                     privacyMode[ WDGetPrivacyMode( (const WDBssDesc*)p ) ],
234                     ( ( p->capaInfo % 0x0020 ) == 0x0020 ? "S   " : "L   " ),
235                     p->rateSet.basic, p->rateSet.support,
236                     p->bssid[ 0 ], p->bssid[ 1 ], p->bssid[ 2 ], p->bssid[ 3 ], p->bssid[ 4 ], p->bssid[ 5 ],
237                     ssid );
238             }
239             p   = (WDBssDesc*)( (u32)buf + size );
240         }
241     }
242     else
243     {
244         SampleReport( "Could not discover AP\n" );
245     }
246 }
247 
248 /*---------------------------------------------------------------------------*
249   End of file
250  *---------------------------------------------------------------------------*/
251