1 /*---------------------------------------------------------------------------*
2   Project:  Wii Connect 24 API demos
3   File:     Check.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: Check.c,v $
14   Revision 1.7  2008/04/28 09:50:52  hirose_kazuki
15   Updated messsages again.
16 
17   Revision 1.6  2008/03/06 05:29:20  hirose_kazuki
18   Updated messsages and sequences to match the latest guideline requirements.
19 
20   Revision 1.5  2007/01/31 09:10:20  hirose_kazuki
21   Rewrote error check sequence after NWC24OpenLib.
22 
23   Revision 1.4  2006/11/01 01:50:59  hirose_kazuki
24   Removed NWC24_ERR_PROTECTED result from NWC24Check() sequence.
25 
26   Revision 1.3  2006/10/26 08:06:40  hirose_kazuki
27   Fixed an easy mistake.
28 
29   Revision 1.2  2006/10/26 02:52:49  hirose_kazuki
30   Added case handling for NWC24_ERR_FATAL on NWC24Check().
31 
32   Revision 1.1  2006/10/23 04:13:03  hirose_kazuki
33   Initial check in.
34 
35 
36  *---------------------------------------------------------------------------*/
37 #include <revolution.h>
38 
39 #include <revolution/nand.h>
40 #include <revolution/vf.h>
41 #include <revolution/mem.h>
42 
43 #include <revolution/nwc24.h>
44 
45 #include <stdio.h>
46 
47 /*---------------------------------------------------------------------------*
48     WiiConnect24 status check sequence sample.
49  *---------------------------------------------------------------------------*/
50 
51 /*---------------------------------------------------------------------------*/
52 // Error messages
53 
54 /*
55 =====================================================================
56 NOTE: Following messages might not be up-to-date. Please refer
57       to "WiiConnect24 message list" for messages you should
58       use in applications.
59 =====================================================================
60 */
61 
62 
63 #define ERRORMSG_WC24_01 /* File errors */ \
64   "[ID:WC24_01] \n" \
65   "There is a problem with the Wii system memory,\n" \
66   "so WiiConnect24 cannot be used.\n"
67 #define ERRORMSG_WC24_02 /* System update required */ \
68   "[ID:WC24_02] \n" \
69   "To use WiiConnect24, a system update is required.\n" \
70   "Refer to the Wii Operations Manual for details.\n"
71 #define ERRORMSG_WC24_03 /* Other fatal errors */ \
72   "[ID:WC24_03] \n" \
73   "WiiConnect24 cannot be used because an error has occurred.\n" \
74   "Reset and try again.\n"
75 #define ERRORMSG_WC24_04 /* WiiConnect24 is busy */ \
76   "[ID:WC24_04] \n" \
77   "WiiConnect24 is temporarily unavailable. Please try again later.\n"
78 #define ERRORMSG_WC24_05 /* Setting is disabled */ \
79   "[ID:WC24_05] \n" \
80   "The WiiConnect24 setting is not turned on. Please check the\n" \
81   "settings under Wii Options. Refer to the Wii Operations Manual\n" \
82   "for details.\n"
83 #define ERRORMSG_WC24_06 /* Network setting problem */ \
84   "[ID:WC24_06] \n" \
85   "An Internet connection cannot be established, so WiiConnect24\n" \
86   "cannot be used. Check the Internet settings and connection.\n" \
87   "Refer to the Wii Operations Manual for details.\n"
88 #define ERRORMSG_WC24_07 /* Server problem */ \
89   "[ID:WC24_07] \n" \
90   "WiiConnect24 is temporarily unavailable. Please try again later.\n"
91 #define ERRORMSG_WC24_08 /* Send-box full */ \
92   "[ID:WC24_08] \n" \
93   "WiiConnect24 is temporarily unavailable. Please try again later.\n"
94 #define ERRORMSG_WC24_12 /* Prohibited by parental control */ \
95   "[ID:WC24_12] \n" \
96   "Use of network features has been restricted. Please check the\n" \
97   "settings for Parental Controls under Wii Options.\n"
98 
99 #define ERRORMSG_NAND_08 /* NAND memory corrupted */ \
100   "[ID:NAND_08] \n" \
101   "The Wii system memory has been damaged.\n" \
102   "Refer to the Wii Operations Manual for details.\n"
103 
104 
105 
106 /*---------------------------------------------------------------------------*/
107 void    PrintError( const char* errorMsg );
108 
109 /*---------------------------------------------------------------------------*/
110 MEMHeapHandle   HeapHndl;
111 MEMAllocator    Allocator;
112 
113 /*---------------------------------------------------------------------------*
114     Main
115  *---------------------------------------------------------------------------*/
main(void)116 int main(void)
117 {
118     NWC24Err    err;
119     s32         result;
120     void*       arenaLo;
121     void*       arenaHi;
122     char*       libWorkMem;
123     u32         retryCount;
124 
125     // Memory allocator initialization
126     arenaLo = OSGetMEM1ArenaLo();
127     arenaHi = OSGetMEM1ArenaHi();
128     HeapHndl = MEMCreateExpHeap(arenaLo, (u32)arenaHi - (u32)arenaLo);
129     OSSetMEM1ArenaLo(arenaHi);
130     MEMInitAllocatorForExpHeap(&Allocator, HeapHndl, 32);
131 
132     result = NANDInit();
133     if ( result != NAND_RESULT_OK )
134     {
135         OSHalt("NANDInit() failed.\n");
136     }
137     VFInit();
138 
139     OSReport("*******************************************************\n");
140     OSReport("    WiiConnect24 status check sequence sample\n");
141     OSReport("*******************************************************\n");
142 
143     libWorkMem = MEMAllocFromAllocator(&Allocator, NWC24_WORK_MEM_SIZE);
144 
145     retryCount = 0;
146 openRetry:
147     /**********************************************
148      *    Opens the library and check results     *
149      **********************************************/
150     err = NWC24OpenLib(libWorkMem);
151     if ( err == NWC24_OK )
152     {
153     }
154     else if ( err == NWC24_ERR_NAND_CORRUPT )
155     {
156         /* NAND flash corrupted [NWC24_ERR_NAND_CORRUPT] */
157         PrintError(ERRORMSG_NAND_08);
158         goto end;
159     }
160     else if ( NWC24_ERR_IS_FILE(err) )
161     {
162         /* File access errors [NWC24_ERR_BROKEN, NWC24_ERR_FILE_*, NWC24_ERR_INTERNAL_VF] */
163         PrintError(ERRORMSG_WC24_01);
164         goto end;
165     }
166     else if ( NWC24_ERR_IS_FATAL(err) )
167     {
168         /* Fatal errors [NWC24_ERR_FATAL, NWC24_ERR_INTERNAL_IPC] */
169         PrintError(ERRORMSG_WC24_03);
170         goto end;
171     }
172     else if ( err == NWC24_ERR_OLD_SYSTEM )
173     {
174         /* Systemmenu version errors [NWC24_ERR_OLD_SYSTEM] */
175         PrintError(ERRORMSG_WC24_02);
176         goto end;
177     }
178     else if ( NWC24_ERR_IS_RETRIABLE(err) )
179     {
180         /* Retriable errors [NWC24_ERR_MUTEX, NWC24_ERR_BUSY, NWC24_ERR_INPROGRESS] */
181         if ( retryCount == 10 )
182         {
183             PrintError(ERRORMSG_WC24_04);
184             goto end;
185         }
186         OSReport("NWC24OpenLib() -- Busy(%d). Retrying.\n", err);
187         OSSleepSeconds(2);
188         ++retryCount;
189         goto openRetry;
190     }
191     else
192     {
193         /* Other errors (because of programming error) */
194         OSReport("NWC24OpenLib() -- Programming error (%d).\n", err);
195         goto end;
196     }
197 
198     /**********************************************
199      *        Status check by NWC24Check()        *
200      **********************************************/
201     err = NWC24Check(NWC24_USE_MESSAGES);
202     switch ( err )
203     {
204     case NWC24_OK:
205         break;
206     case NWC24_ERR_FATAL:
207         PrintError(ERRORMSG_WC24_03);
208         goto closeLib;
209     case NWC24_ERR_DISABLED:
210         PrintError(ERRORMSG_WC24_05);
211         goto closeLib;
212     case NWC24_ERR_NETWORK:
213         PrintError(ERRORMSG_WC24_06);
214         goto closeLib;
215     case NWC24_ERR_SERVER:
216         PrintError(ERRORMSG_WC24_07);
217         goto closeLib;
218     case NWC24_ERR_FULL:
219         PrintError(ERRORMSG_WC24_08);
220         goto closeLib;
221     case NWC24_ERR_PROTECTED:
222         PrintError(ERRORMSG_WC24_12);
223         goto closeLib;
224     default:
225         // Should not reach here.
226         break;
227     }
228 
229     OSReport("All NWC24 status check passed.\n");
230 
231 closeLib:
232     err = NWC24CloseLib();
233     if ( err != NWC24_OK )
234     {
235         OSReport("NWC24CloseLib(): Error %d\n", err);
236         OSHalt("Failed.\n");
237     }
238 
239 end:
240     MEMFreeToAllocator(&Allocator, libWorkMem);
241 
242     OSHalt("\nProgram finished.\n");
243 
244     return 0;
245 }
246 
247 /*---------------------------------------------------------------------------*
248     Prints error message and 6-digits error code
249  *---------------------------------------------------------------------------*/
PrintError(const char * errorMsg)250 void PrintError( const char* errorMsg )
251 {
252     s32  errorCode;
253 
254     errorCode = NWC24GetErrorCode();
255     OSReport("NWC24 status check failed. [%06d]\n", -errorCode);
256     OSReport("Message: %s\n", errorMsg);
257 
258     return;
259 }
260 
261 
262