1 /*---------------------------------------------------------------------------*
2   Project:  Wii Connect24 API demos
3   File:     MsgSearch.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: MsgSearch.c,v $
14   Revision 1.5  2007/09/03 06:23:40  seiki_masashi
15   Minor fixes.
16 
17   Revision 1.4  2007/09/03 03:21:23  seiki_masashi
18   Minor fixes.
19 
20   Revision 1.3  2007/09/03 02:52:26  seiki_masashi
21   Added NWC24SetSearchCondPublic().
22 
23   Revision 1.2  2007/02/06 02:01:37  torigoe_nobutaka
24   Changed to call OSGetAppGamename() to get application ID.
25 
26   Revision 1.1  2006/09/27 08:56:17  torigoe_nobutaka
27   Moved from nwc24demo/src.
28 
29   Revision 1.4  2006/09/21 02:35:13  hirose_kazuki
30   Changes due to re-arrangement of message types.
31 
32   Revision 1.3  2006/09/13 08:14:22  yosizaki
33   Changed to call VFInit().
34 
35   Revision 1.2  2006/09/10 09:20:56  hirose_kazuki
36   Changed to use NWC24OpenLib/NWC24CloseLib instead of obsolete APIs.
37 
38   Revision 1.1  2006/08/25 06:17:59  torigoe_nobutaka
39   Initial check in.
40 
41 
42   $NoKeywords: $
43  *---------------------------------------------------------------------------*/
44 #include <revolution.h>
45 #include <revolution/mem.h>
46 #include <revolution/nand.h>
47 #include <revolution/vf.h>
48 #include <revolution/nwc24.h>
49 
50 #include "PostTestMsg.h"
51 
52 #define MSGOBJ_ARRAY_SIZE   2
53 
54 /*---------------------------------------------------------------------------*
55    Static variables
56  *---------------------------------------------------------------------------*/
57 static MEMHeapHandle    s_HeapHndl;
58 static MEMAllocator     s_Allocator;
59 static BOOL             s_bSCondFromAddrId  = FALSE;
60 static BOOL             s_bSCondSendBox     = FALSE;
61 static BOOL             s_bSCondForMenu     = FALSE;
62 static BOOL             s_bSCondPublic      = FALSE;
63 static BOOL             s_bSCondAppId       = FALSE;
64 static NWC24UserId      s_uidMy;
65 static u32              s_appId;
66 
67 /*---------------------------------------------------------------------------*
68    Forward declaration of function
69  *---------------------------------------------------------------------------*/
70 static void ViewSearchConds( void );
71 static void ViewMessage    ( NWC24MsgObj* msgObj );
72 static void PrintFrom      ( NWC24MsgObj* msgObj );
73 static void PrintTo        ( NWC24MsgObj* msgObj );
74 static void PrintSubject   ( NWC24MsgObj* msgObj );
75 static void PrintMsgType   ( NWC24MsgObj* msgObj );
76 static void PrintAppId     ( NWC24MsgObj* msgObj );
77 
78 /*---------------------------------------------------------------------------*
79    App entry point
80  *---------------------------------------------------------------------------*/
main(void)81 int main( void )
82 {
83     NWC24Err    err;
84     s32         result;
85     void*       arenaLo                         = NULL;
86     void*       arenaHi                         = NULL;
87     char*       libWorkMem                      = NULL;
88     u8          iLoop;
89 
90     /* Memory allocator initialization */
91     arenaLo = OSGetMEM1ArenaLo();
92     arenaHi = OSGetMEM1ArenaHi();
93     s_HeapHndl = MEMCreateExpHeap( arenaLo, (u32)arenaHi - (u32)arenaLo );
94     OSSetMEM1ArenaLo( arenaHi );
95     MEMInitAllocatorForExpHeap( &s_Allocator, s_HeapHndl, 32 );
96 
97     result = NANDInit();
98     if ( result != NAND_RESULT_OK )
99     {
100         OSHalt( "NANDInit() failed.\n" );
101     }
102 
103     VFInit();
104 
105     OSReport( "*******************************************************\n" );
106     OSReport( "    MessageBox Search demo\n" );
107     OSReport( "*******************************************************\n" );
108 
109     libWorkMem = MEMAllocFromAllocator( &s_Allocator, NWC24_WORK_MEM_SIZE );
110 
111     err = NWC24OpenLib( libWorkMem );
112     if ( err != NWC24_OK )
113     {
114         OSReport( "NWC24OpenLib(): Error %d\n", err );
115         OSHalt( "Failed.\n" );
116     }
117 
118     /* Gets the user ID of the local hardware (used in search conditions)*/
119     err = NWC24GetMyUserId( &s_uidMy );
120     if ( err != NWC24_OK )
121     {
122         OSReport( "NWC24GetMyUserId(): Error %d\n", err );
123         OSHalt( "Failed.\n" );
124     }
125 
126     /* Gets the current application ID (used in search conditions) */
127     s_appId = *(u32*)OSGetAppGamename();
128 
129     /* Create text message */
130     PostTestMsg();
131 
132     /* Search for and display messages while changing search conditions */
133     for ( iLoop = 0; iLoop < 6; ++iLoop )
134     {
135         u32         iObj;
136         u32         numStored;
137         u32         numRemain;
138         NWC24MsgObj msgObjArray[MSGOBJ_ARRAY_SIZE];
139 
140         switch ( iLoop )
141         {
142             case 0: break;
143             case 1: s_bSCondPublic     = TRUE; break;
144             case 2: s_bSCondSendBox    = TRUE; break;
145             case 3: s_bSCondAppId      = TRUE; break;
146             case 4: s_bSCondFromAddrId = TRUE; break;
147             case 5: s_bSCondForMenu    = TRUE; break;
148         }
149 
150         /* Search condition settings*/
151         (void)NWC24InitSearchConds();
152         if ( s_bSCondSendBox )    (void)NWC24SetSearchCondMsgBox( NWC24_SEND_BOX );
153         if ( s_bSCondAppId )      (void)NWC24SetSearchCondAppId( s_appId );
154         if ( s_bSCondFromAddrId ) (void)NWC24SetSearchCondFromAddrId( s_uidMy );
155         if ( s_bSCondForMenu )    (void)NWC24SetSearchCondForMenu();
156         (void)NWC24SetSearchCondPublic(s_bSCondPublic);
157 
158         ViewSearchConds();
159 
160         do
161         {
162             /* Search messages */
163             err = NWC24SearchMsgs( msgObjArray, MSGOBJ_ARRAY_SIZE, &numStored, &numRemain );
164             if ( err != NWC24_OK )
165             {
166                 OSReport( "NWC24SearchMsgs(): Error %d\n", err );
167                 OSHalt( "Failed.\n" );
168             }
169 
170             OSReport("===================================================\n");
171             OSReport(" [NWC24SearchMsgs():  Stored: %d  Remain: %d]\n", numStored, numRemain);
172             OSReport("===================================================\n");
173 
174             /* Display messages */
175             for ( iObj = 0 ; iObj < numStored ; ++iObj )
176             {
177                 ViewMessage( &msgObjArray[iObj] );
178             }
179         }
180         while ( numRemain > 0 );
181         /* Repeat until all messages satisfying the search conditions have been gotten */
182     }
183 
184     err = NWC24CloseLib();
185     if ( err != NWC24_OK )
186     {
187         OSReport( "NWC24CloseLib(): Error %d\n", err );
188         OSHalt( "Failed.\n" );
189     }
190 
191     MEMFreeToAllocator( &s_Allocator, libWorkMem );
192 
193     OSHalt( "\nCompleted.\n" );
194     return 0;
195 }
196 
197 /*---------------------------------------------------------------------------*
198    Views search conditions.
199  *---------------------------------------------------------------------------*/
ViewSearchConds(void)200 static void ViewSearchConds( void )
201 {
202     OSReport( "\n#######################################################\n" );
203     OSReport( " [Search Conditions]\n" );
204     OSReport( "-------------------------------------------------------\n" );
205     OSReport( "    From Address ID : " );
206 
207     if ( s_bSCondFromAddrId )
208     {
209         OSReport( "%llu\n", s_uidMy );
210     }
211     else
212     {
213         OSReport( "Disable\n" );
214     }
215 
216     OSReport( "    Message Box     : %s\n", s_bSCondSendBox ? "Send Box" : "Receive Box" );
217     OSReport( "    For Menu        : %s\n", s_bSCondForMenu ? "Enable" : "Disable" );
218     OSReport( "    Include Public  : %s\n", s_bSCondPublic  ? "Enable" : "Disable" );
219     OSReport( "    Application ID  : " );
220 
221     if ( s_bSCondAppId )
222     {
223         OSReport( "0x%08x\n", s_appId );
224     }
225     else
226     {
227         OSReport( "Disable\n" );
228     }
229 
230     OSReport( "#######################################################\n" );
231     return;
232 }
233 
234 /*---------------------------------------------------------------------------*
235    Views message data.
236  *---------------------------------------------------------------------------*/
ViewMessage(NWC24MsgObj * msgObj)237 static void ViewMessage( NWC24MsgObj* msgObj )
238 {
239     OSReport( "\n[From]     " );
240     PrintFrom( msgObj );
241 
242     OSReport( "\n[To]       " );
243     PrintTo( msgObj );
244 
245     OSReport( "\n[Subject]  " );
246     PrintSubject( msgObj );
247 
248     OSReport( "\n[Msg type] " );
249     PrintMsgType( msgObj );
250 
251     OSReport( "\n[App ID]   " );
252     PrintAppId( msgObj );
253 
254     OSReport( "\n-------------------------------------------\n" );
255     return;
256 }
257 
258 /*---------------------------------------------------------------------------*/
PrintFrom(NWC24MsgObj * msgObj)259 static void PrintFrom( NWC24MsgObj* msgObj )
260 {
261     NWC24Err        err;
262     NWC24MsgType    msgType;
263 
264     err = NWC24GetMsgType( msgObj, &msgType );
265     if ( err != NWC24_OK )
266     {
267         OSReport( "NWC24GetMsgType(): Error %d\n", err );
268         return;
269     }
270 
271     if ( msgType != NWC24_MSGTYPE_PUBLIC )
272     {
273         NWC24UserId     uid;
274 
275         err = NWC24GetMsgFromId( msgObj, &uid );
276         if ( err != NWC24_OK )
277         {
278             OSReport( "NWC24GetMsgFromId(): Error %d\n", err );
279             return;
280         }
281         OSReport( "%016llu", uid );
282     }
283     else
284     {
285         char    strAddr[NWC24MSG_MAX_ADDRSTR];
286 
287         err = NWC24ReadMsgFromAddr( msgObj, strAddr, NWC24MSG_MAX_ADDRSTR );
288         if ( err != NWC24_OK )
289         {
290             OSReport( "NWC24ReadMsgFromAddr(): Error %d\n", err );
291             return;
292         }
293         OSReport( "%s", strAddr );
294     }
295     return;
296 }
297 
298 /*---------------------------------------------------------------------------*/
PrintTo(NWC24MsgObj * msgObj)299 static void PrintTo( NWC24MsgObj* msgObj )
300 {
301     NWC24Err        err;
302     NWC24MsgType    msgType;
303     u32             numTo;
304     u32             iTo;
305 
306     err = NWC24GetMsgType( msgObj, &msgType );
307     if ( err != NWC24_OK )
308     {
309         OSReport( "NWC24GetMsgType(): Error %d\n", err );
310         return;
311     }
312 
313     err = NWC24GetMsgNumTo( msgObj, &numTo );
314     if ( err != NWC24_OK )
315     {
316         OSReport( "NWC24GetMsgNumTo(): Error %d\n", err );
317         return;
318     }
319 
320     if ( msgType != NWC24_MSGTYPE_PUBLIC )
321     {
322         NWC24UserId     uid;
323 
324         for ( iTo = 0; iTo < numTo; ++iTo )
325         {
326             err = NWC24ReadMsgToId( msgObj, iTo, &uid );
327             if ( err != NWC24_OK )
328             {
329                 OSReport( "NWC24ReadMsgToId(): Error %d\n", err );
330                 return;
331             }
332 
333             OSReport( "%016llu", uid );
334             if ( iTo < numTo - 1 )
335             {
336                 OSReport( ", " );
337             }
338         }
339     }
340     else
341     {
342         char    strAddr[NWC24MSG_MAX_ADDRSTR];
343 
344         for ( iTo = 0; iTo < numTo; ++iTo )
345         {
346             err = NWC24ReadMsgToAddr( msgObj, iTo, strAddr, NWC24MSG_MAX_ADDRSTR );
347             if ( err != NWC24_OK )
348             {
349                 OSReport( "NWC24ReadMsgToAddr(): Error %d\n", err );
350                 return;
351             }
352 
353             OSReport( "%s", strAddr );
354             if ( iTo < numTo - 1 )
355             {
356                 OSReport( ", " );
357             }
358         }
359     }
360     return;
361 }
362 
363 /*---------------------------------------------------------------------------*/
PrintSubject(NWC24MsgObj * msgObj)364 static void PrintSubject( NWC24MsgObj* msgObj )
365 {
366     NWC24Err    err;
367     u32         iBuf;
368     u32         bufSize;
369     char*       buffer  = NULL;
370 
371     err = NWC24GetMsgSubjectSize( msgObj, &bufSize );
372     if ( err != NWC24_OK )
373     {
374         OSReport( "NWC24GetMsgSubjectSize(): Error %d\n", err );
375         return;
376     }
377 
378     buffer = (char*)MEMAllocFromAllocator( &s_Allocator, OSRoundUp32B(bufSize) );
379     err = NWC24ReadMsgSubject( msgObj, buffer, bufSize );
380     if ( err != NWC24_OK )
381     {
382         OSReport( "NWC24ReadMsgSubject(): Error %d\n", err );
383         return;
384     }
385 
386     for ( iBuf = 0; iBuf < bufSize; ++iBuf )
387     {
388         if ( buffer[iBuf] != '\r' && buffer[iBuf] != '\n' )
389         {
390             OSReport( "%c", buffer[iBuf] );
391         }
392     }
393 
394     MEMFreeToAllocator( &s_Allocator, buffer );
395     return;
396 }
397 
398 /*---------------------------------------------------------------------------*/
PrintMsgType(NWC24MsgObj * msgObj)399 static void PrintMsgType( NWC24MsgObj* msgObj )
400 {
401     NWC24Err        err;
402     NWC24MsgType    msgType;
403     char*           strMsgType;
404 
405     err = NWC24GetMsgType( msgObj, &msgType );
406     if ( err != NWC24_OK )
407     {
408         OSReport( "NWC24GetMsgType(): Error %d\n", err );
409         return;
410     }
411 
412     switch ( msgType )
413     {
414         case NWC24_MSGTYPE_WII_MENU_SHARED:     strMsgType = "Wii menu shared"; break;
415         case NWC24_MSGTYPE_WII_APP:             strMsgType = "Wii app";         break;
416         case NWC24_MSGTYPE_WII_MENU:            strMsgType = "Wii menu";        break;
417         case NWC24_MSGTYPE_WII_APP_HIDDEN:      strMsgType = "Wii app hidden";  break;
418         case NWC24_MSGTYPE_PUBLIC:              strMsgType = "Public";          break;
419         default:                                strMsgType = "Unknown";
420     }
421 
422     OSReport( "%s", strMsgType );
423     return;
424 }
425 
426 /*---------------------------------------------------------------------------*/
PrintAppId(NWC24MsgObj * msgObj)427 static void PrintAppId( NWC24MsgObj* msgObj )
428 {
429     NWC24Err        err;
430     u32             appId;
431 
432     err = NWC24GetMsgAppId( msgObj, &appId );
433     if ( err != NWC24_OK )
434     {
435         OSReport( "NWC24GetMsgAppId(): Error %d\n", err );
436         return;
437     }
438 
439     OSReport( "0x%08x", appId );
440     return;
441 }
442 
443 /*======== End of MsgSearch.c ========*/
444