1 /*---------------------------------------------------------------------------*
2   Project:  TwlSDK - WXC - libraries -
3   File:     wxc_common.c
4 
5   Copyright 2005-2009 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   $Date:: 2008-12-16#$
14   $Rev: 9661 $
15   $Author: okubata_ryoma $
16  *---------------------------------------------------------------------------*/
17 
18 #include <nitro.h>
19 
20 #include <nitro/wxc/common.h>
21 
22 
23 /*****************************************************************************/
24 /* Functions */
25 
26 /*---------------------------------------------------------------------------*
27   Name:         WXC_DEBUG_LOG
28 
29   Description:  WXC library internal debug output function.
30                 This function is modified with SDK_WEAK_SYMBOL and normally is equivalent to OS_TPrintf processing.
31                 You can override this with a function of the same name in an application-specific debugging system, for example.
32 
33 
34 
35   Arguments:    format: Document format string representing the debug output content
36                 ...: Variable argument
37 
38   Returns:      None.
39  *---------------------------------------------------------------------------*/
40 #if !defined(WXC_DEBUG_LOG)
WXC_DEBUG_LOG(const char * format,...)41 SDK_WEAK_SYMBOL void WXC_DEBUG_LOG(const char *format, ...)
42 {
43     va_list va;
44     va_start(va, format);
45     OS_TVPrintf(format, va);
46     va_end(va);
47 }
48 #endif
49 
50 /*---------------------------------------------------------------------------*
51   Name:         WXC_GetWmApiName
52 
53   Description:  Gets WM function's name string.
54 
55   Arguments:    id: WMApiid enumeration value representing WM function type
56 
57   Returns:      WM function name string.
58  *---------------------------------------------------------------------------*/
WXC_GetWmApiName(WMApiid id)59 const char *WXC_GetWmApiName(WMApiid id)
60 {
61     const char *ret = "";
62     static const char *(api_name_table[]) =
63     {
64     "WM_Initialize",
65             "WM_Reset",
66             "WM_End",
67             "WM_Enable",
68             "WM_Disable",
69             "WM_PowerOn",
70             "WM_PowerOff",
71             "WM_SetParentParameter",
72             "WM_StartParent",
73             "WM_EndParent",
74             "WM_StartScan",
75             "WM_EndScan",
76             "WM_StartConnect",
77             "WM_Disconnect",
78             "WM_StartMP",
79             "WM_SetMPData",
80             "WM_EndMP",
81             "WM_StartDCF",
82             "WM_SetDCFData",
83             "WM_EndDCF",
84             "WM_SetWEPKey",
85             "WM_StartKeySharing",
86             "WM_EndKeySharing",
87             "WM_GetKeySet",
88             "WM_SetGameInfo",
89             "WM_SetBeaconIndication",
90             "(WM_StartTestMode)",
91             "(WM_StopTestMode)",
92             "(W-Alarm ind. in ARM7)",
93             "WM_SetLifeTime",
94             "WM_MeasureChannel",
95             "WM_InitWirelessCounter",
96             "WM_GetWirelessCounter",
97             "WM_SetEntry",
98             "(auto-deauthentication in ARM7)",
99             "WM_SetMPFrequency",
100             "WMi_SetBeaconPeriod",
101             "(auto-disconnection in ARM7)",
102             "WM_StartScanEx",
103             "WM_SetWEPKeyEx",
104             "WM_SetPowerSaveMode",
105             "WM_StartTestRxMode",
106             "WM_StopTestRxMode",
107             "(MP-timing controller in ARM7)",
108             "(MP-timing controller in ARM7)", "(MP-timing controller in ARM7)",}
109     ;
110     /* If a compile error occurs here, should update based on the definition in wm.h */
111     SDK_STATIC_ASSERT(WM_APIID_ASYNC_KIND_MAX <= sizeof(api_name_table) / sizeof(*api_name_table));
112     if (id < WM_APIID_ASYNC_KIND_MAX)
113     {
114         ret = api_name_table[id];
115     }
116     else if (id == WM_APIID_INDICATION)
117     {
118         ret = "WM_SetIndCallback";
119     }
120     else if (id == WM_APIID_PORT_SEND)
121     {
122         ret = "PortSendCallback";
123     }
124     else if (id == WM_APIID_PORT_RECV)
125     {
126         ret = "PortRecvCallback";
127     }
128     else if (id == WM_APIID_READ_STATUS)
129     {
130         ret = "WM_ReadStatus";
131     }
132     else
133     {
134         ret = "(unknown)";
135     }
136     return ret;
137 }
138 
139 /*---------------------------------------------------------------------------*
140   Name:         WXC_GetWmErrorName
141 
142   Description:  Gets WM error code's name string.
143 
144   Arguments:    err: WMErrCode enumeration value representing WM error code
145 
146   Returns:      WM error code name string.
147  *---------------------------------------------------------------------------*/
WXC_GetWmErrorName(WMErrCode err)148 const char *WXC_GetWmErrorName(WMErrCode err)
149 {
150     const char *ret = "";
151     static const char *(error_name_table[]) =
152     {
153     "WM_ERRCODE_SUCCESS",
154             "WM_ERRCODE_FAILED",
155             "WM_ERRCODE_OPERATING",
156             "WM_ERRCODE_ILLEGAL_STATE",
157             "WM_ERRCODE_WM_DISABLE",
158             "WM_ERRCODE_NO_DATASET",
159             "WM_ERRCODE_INVALID_PARAM",
160             "WM_ERRCODE_NO_CHILD",
161             "WM_ERRCODE_FIFO_ERROR",
162             "WM_ERRCODE_TIMEOUT",
163             "WM_ERRCODE_SEND_QUEUE_FULL",
164             "WM_ERRCODE_NO_ENTRY",
165             "WM_ERRCODE_OVER_MAX_ENTRY",
166             "WM_ERRCODE_INVALID_POLLBITMAP",
167             "WM_ERRCODE_NO_DATA",
168             "WM_ERRCODE_SEND_FAILED",
169             "WM_ERRCODE_DCF_TEST",
170             "WM_ERRCODE_WL_INVALID_PARAM", "WM_ERRCODE_WL_LENGTH_ERR", "WM_ERRCODE_FLASH_ERROR",}
171     ;
172     /* If compile error occurs here, it should be updated based on the definition in wm.h */
173     SDK_STATIC_ASSERT(WM_ERRCODE_MAX == sizeof(error_name_table) / sizeof(*error_name_table));
174     if (err < WM_ERRCODE_MAX)
175     {
176         ret = error_name_table[err];
177     }
178     else
179     {
180         ret = "(unknown WMErrCode)";
181     }
182     return ret;
183 }
184 
185 /*---------------------------------------------------------------------------*
186   Name:         WXC_CheckWmApiResult
187 
188   Description:  Determines the return value from a WM function call and sends details to debug output if it is an error.
189 
190 
191   Arguments:    id: WM function types
192                 err: Error code returned from the function
193 
194   Returns:      TRUE if the return value is legitimate. FALSE if not.
195  *---------------------------------------------------------------------------*/
WXC_CheckWmApiResult(WMApiid id,WMErrCode err)196 BOOL WXC_CheckWmApiResult(WMApiid id, WMErrCode err)
197 {
198 #pragma unused(id)
199     BOOL    ret = TRUE;
200     if (err == WM_ERRCODE_SUCCESS)
201     {
202         WXC_DEBUG_LOG(">< %s succeeded.\n", WXC_GetWmApiName(id));
203     }
204     else if (err == WM_ERRCODE_OPERATING)
205     {
206         WXC_DEBUG_LOG(">  %s started.\n", WXC_GetWmApiName(id));
207     }
208     else
209     {
210         WXC_DEBUG_LOG(">< %s failed. %s\n", WXC_GetWmApiName(id), WXC_GetWmErrorName(err));
211         ret = FALSE;
212     }
213     return ret;
214 }
215 
216 /*---------------------------------------------------------------------------*
217   Name:         WXC_CheckWmCallbackResult
218 
219   Description:  Determines the return value from a WM callback and sends details to debug output if it is an error.
220 
221 
222   Arguments:    arg: Argument returned to WM function callback
223 
224   Returns:      TRUE if the resulting value is legitimate. FALSE if not.
225  *---------------------------------------------------------------------------*/
WXC_CheckWmCallbackResult(void * arg)226 BOOL WXC_CheckWmCallbackResult(void *arg)
227 {
228     const WMCallback *cb = (const WMCallback *)arg;
229     const WMApiid id = (WMApiid)cb->apiid;
230     const WMErrCode err = (WMErrCode)cb->errcode;
231     BOOL    ret = (cb->errcode == WM_ERRCODE_SUCCESS);
232 
233     /* If successful, display only the "confirm" result */
234     if (ret)
235     {
236         if ((id != WM_APIID_INDICATION) &&
237             ((id != WM_APIID_START_MP)
238              || (((WMStartMPCallback *)arg)->state == WM_STATECODE_MP_START))
239             && ((id != WM_APIID_START_CONNECT)
240                 || (((WMStartConnectCallback *)arg)->state == WM_STATECODE_CONNECT_START)))
241         {
242             WXC_DRIVER_LOG(" < %s succeeded.\n", WXC_GetWmApiName(id));
243         }
244     }
245     /* Among the errors, ignore warning errors that may occur a lot */
246     else
247     {
248         switch (id)
249         {
250         case WM_APIID_START_MP:
251             switch (err)
252             {
253             case WM_ERRCODE_SEND_FAILED:
254             case WM_ERRCODE_TIMEOUT:
255             case WM_ERRCODE_INVALID_POLLBITMAP:
256                 ret = TRUE;
257                 break;
258             }
259             break;
260         }
261         /* Display result only for the essential errors */
262         if (!ret)
263         {
264             WXC_DRIVER_LOG(" < %s failed. %s(cmd=%02X,res=%02X)\n",
265                            WXC_GetWmApiName(id), WXC_GetWmErrorName(err),
266                            cb->wlCmdID, cb->wlResult);
267         }
268     }
269     return ret;
270 }
271 
272 /*---------------------------------------------------------------------------*
273   Name:         WXC_GetNextAllowedChannel
274 
275   Description:  Gets the next usable channel after the selected channel.
276                 Loops to the bottom channel after the top channel.
277                 Gets the bottom channel if 0 is selected.
278 
279   Arguments:    ch: Channel position this time (1-14 or 0)
280 
281   Returns:      Next usable channel (1-14)
282  *---------------------------------------------------------------------------*/
WXC_GetNextAllowedChannel(int ch)283 u16 WXC_GetNextAllowedChannel(int ch)
284 {
285     int     allowed = WM_GetAllowedChannel();
286     if (!allowed)
287     {
288         OS_TPanic("no allowed channels to scan!");
289     }
290     else if (allowed == 0x8000)
291     {
292         OS_TPanic("WM_GetAllowedChannel() failed! (WM might have not been initialized)");
293     }
294     else
295     {
296         ++ch;
297         while (((1 << (ch - 1)) & allowed) == 0)
298         {
299             if (++ch > 16)
300             {
301                 ch = 1;
302             }
303         }
304     }
305     return (u16)ch;
306 }
307 
308 /*---------------------------------------------------------------------------*
309   End of file
310  *---------------------------------------------------------------------------*/
311