1 /*---------------------------------------------------------------------------*
2   Project:  Wii Connect 24 API demos
3   File:     MsgPrint.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: MsgPrint.c,v $
14   Revision 1.2  2006/11/24 07:28:21  adachi_hiroaki
15   Fixed a memory corruption bug.
16 
17   Revision 1.1  2006/09/27 08:56:17  torigoe_nobutaka
18   Moved from nwc24demo/src.
19 
20   Revision 1.3  2006/09/21 02:35:13  hirose_kazuki
21   Changes due to re-arrangement of message types.
22 
23   Revision 1.2  2006/09/13 08:14:22  yosizaki
24   changed to call VFInit().
25 
26   Revision 1.1  2006/09/10 09:27:08  hirose_kazuki
27   Renamed from MsgView.c
28 
29   Revision 1.5  2006/09/10 09:20:56  hirose_kazuki
30   Changed to use NWC24OpenLib/NWC24CloseLib instead of obsoleted APIs.
31 
32   Revision 1.4  2006/08/29 13:11:39  hirose_kazuki
33   Corrected message type handling.
34 
35   Revision 1.3  2006/08/08 04:53:27  hirose_kazuki
36   Added ViewDate().
37 
38   Revision 1.2  2006/07/13 12:50:54  hirose_kazuki
39   Changed to display more attributes.
40 
41   Revision 1.1  2006/06/29 12:46:21  hirose_kazuki
42   Moved from another repository.
43 
44   Revision 1.1  2006/06/12 07:37:18  hirose
45   Initial check in.
46 
47 
48  *---------------------------------------------------------------------------*/
49 #include <revolution.h>
50 
51 #include <revolution/nand.h>
52 #include <revolution/vf.h>
53 #include <revolution/mem.h>
54 
55 #include <revolution/nwc24.h>
56 
57 
58 #include <stdio.h>
59 
60 /*---------------------------------------------------------------------------*
61     Printing contents stored in both message boxes.
62  *---------------------------------------------------------------------------*/
63 static void    GetIdList( NWC24MsgBoxId mBoxId );
64 static void    ReleaseIdList( void );
65 
66 static void    ViewMessage( NWC24MsgBoxId mBoxId, u32 msgId );
67 
68 static void    ViewFrom( NWC24MsgObj* msgObj );
69 static void    ViewTo( NWC24MsgObj* msgObj );
70 static void    ViewSubject( NWC24MsgObj* msgObj );
71 static void    ViewDate( NWC24MsgObj* msgObj );
72 static void    ViewBodyText( NWC24MsgObj* msgObj );
73 static void    ViewAttachment( NWC24MsgObj* msgObj );
74 
75 static void    PrintUserId( NWC24UserId uid );
76 
77 /*---------------------------------------------------------------------------*/
78 static MEMHeapHandle    HeapHndl;
79 static MEMAllocator     Allocator;
80 
81 static u32*             IdListBuf = 0;
82 static u32              NumMsgs = 0;
83 
84 /*---------------------------------------------------------------------------*
85     Main
86  *---------------------------------------------------------------------------*/
main(void)87 int main(void)
88 {
89     NWC24Err    err;
90     s32         result;
91     void*       arenaLo;
92     void*       arenaHi;
93     char*       libWorkMem;
94     u32         i;
95 
96     // Memory allocator initialization
97     arenaLo = OSGetMEM1ArenaLo();
98     arenaHi = OSGetMEM1ArenaHi();
99     HeapHndl = MEMCreateExpHeap(arenaLo, (u32)arenaHi - (u32)arenaLo);
100     OSSetMEM1ArenaLo(arenaHi);
101     MEMInitAllocatorForExpHeap(&Allocator, HeapHndl, 32);
102 
103     result = NANDInit();
104     if ( result != NAND_RESULT_OK )
105     {
106         OSHalt("NANDInit() failed.\n");
107     }
108 
109     VFInit();
110 
111     OSReport("*******************************************************\n");
112     OSReport("    Message Print demo\n");
113     OSReport("*******************************************************\n");
114 
115     libWorkMem = MEMAllocFromAllocator(&Allocator, NWC24_WORK_MEM_SIZE);
116 
117     err = NWC24OpenLib(libWorkMem);
118     if ( err != NWC24_OK )
119     {
120         OSReport("NWC24OpenLib(): Error %d\n", err);
121         OSHalt("Failed.\n");
122     }
123 
124     // View messages in send box.
125     GetIdList(NWC24_SEND_BOX);
126     for ( i = 0 ; i < NumMsgs ; ++i )
127     {
128         ViewMessage(NWC24_SEND_BOX, IdListBuf[i]);
129     }
130     ReleaseIdList();
131 
132     // View messages in receive box.
133     GetIdList(NWC24_RECV_BOX);
134     for ( i = 0 ; i < NumMsgs ; ++i )
135     {
136         ViewMessage(NWC24_RECV_BOX, IdListBuf[i]);
137     }
138     ReleaseIdList();
139 
140     err = NWC24CloseLib();
141     if ( err != NWC24_OK )
142     {
143         OSReport("NWC24CloseLib(): Error %d\n", err);
144         OSHalt("Failed.\n");
145     }
146 
147     MEMFreeToAllocator(&Allocator, libWorkMem);
148 
149     OSHalt("\nCompleted.\n");
150     return 0;
151 }
152 
153 /*---------------------------------------------------------------------------*
154     Obtains message list
155  *---------------------------------------------------------------------------*/
GetIdList(NWC24MsgBoxId mBoxId)156 void GetIdList( NWC24MsgBoxId mBoxId )
157 {
158     u32             bufSize;
159     NWC24Err        err;
160 
161     err = NWC24GetNumMsgs(mBoxId, &NumMsgs);
162     if ( err != NWC24_OK )
163     {
164         OSReport("NWC24GetNumMsgs(): error %d\n", err);
165         return;
166     }
167 
168     if ( NumMsgs == 0 )
169     {
170         IdListBuf = 0;
171         if ( mBoxId == NWC24_RECV_BOX )
172         {
173             OSReport("(Receive box has no message.)\n");
174         }
175         else
176         {
177             OSReport("(Send box has no message.)\n");
178         }
179         return;
180     }
181 
182     bufSize = OSRoundUp32B(NumMsgs * sizeof(u32));
183     IdListBuf = (u32*)MEMAllocFromAllocator(&Allocator, bufSize);
184     err = NWC24GetMsgIdList(mBoxId, IdListBuf, NumMsgs);
185     if ( err != NWC24_OK )
186     {
187         OSReport("NWC24GetNumMsgList(): error %d\n", err);
188         return;
189     }
190 
191     return;
192 }
193 
194 /*---------------------------------------------------------------------------*
195     Releases message list
196  *---------------------------------------------------------------------------*/
ReleaseIdList(void)197 void ReleaseIdList( void )
198 {
199     if ( NumMsgs == 0 || IdListBuf == 0 )
200         return;
201 
202     MEMFreeToAllocator(&Allocator, IdListBuf);
203     return;
204 }
205 
206 /*---------------------------------------------------------------------------*
207     Views specified message.
208  *---------------------------------------------------------------------------*/
ViewMessage(NWC24MsgBoxId mBoxId,u32 msgId)209 void ViewMessage( NWC24MsgBoxId mBoxId, u32 msgId )
210 {
211     NWC24Err    err;
212     NWC24MsgObj msgObj;
213 
214     err = NWC24GetMsgObj(&msgObj, mBoxId, msgId);
215     if ( err != NWC24_OK )
216     {
217         OSReport("NWC24GetMsgObj(): error %d\n", err);
218         return;
219     }
220 
221     OSReport("====================================================\n");
222     if ( mBoxId == NWC24_RECV_BOX )
223     {
224         OSReport("Receive Box /");
225     }
226     else
227     {
228         OSReport("Send Box /");
229     }
230     OSReport("Message ID: %07d\n", msgId);
231 
232     OSReport("From: ");
233     ViewFrom(&msgObj);
234 
235     OSReport("To:   ");
236     ViewTo(&msgObj);
237 
238     OSReport("Subject: ");
239     ViewSubject(&msgObj);
240 
241     OSReport("Date: ");
242     ViewDate(&msgObj);
243 
244     OSReport("\n");
245     ViewBodyText(&msgObj);
246 
247     OSReport("\n");
248     OSReport("----------------------------------------------------\n");
249     ViewAttachment(&msgObj);
250 
251     OSReport("\n");
252 }
253 
254 /*---------------------------------------------------------------------------*
255     Views 'from' address.
256  *---------------------------------------------------------------------------*/
ViewFrom(NWC24MsgObj * msgObj)257 void ViewFrom( NWC24MsgObj* msgObj )
258 {
259     NWC24Err        err;
260     NWC24MsgType    type;
261 
262     err = NWC24GetMsgType(msgObj, &type);
263     if ( err != NWC24_OK )
264     {
265         OSReport("NWC24GetMsgType(): error %d\n", err);
266         return;
267     }
268 
269     switch (type)
270     {
271       case NWC24_MSGTYPE_WII_MENU_SHARED:
272       case NWC24_MSGTYPE_WII_APP:
273       case NWC24_MSGTYPE_WII_MENU:
274       case NWC24_MSGTYPE_WII_APP_HIDDEN:
275         {
276             NWC24UserId     uid;
277 
278             err = NWC24GetMsgFromId(msgObj, &uid);
279             if ( err != NWC24_OK )
280             {
281                 OSReport("NWC24GetMsgFromId(): error %d\n", err);
282                 return;
283             }
284             PrintUserId(uid);
285             OSReport("\n");
286         }
287         break;
288       case NWC24_MSGTYPE_PUBLIC:
289         {
290             char    addrStr[NWC24MSG_MAX_ADDRSTR];
291 
292             err = NWC24ReadMsgFromAddr(msgObj, addrStr, NWC24MSG_MAX_ADDRSTR);
293             if ( err != NWC24_OK )
294             {
295                 OSReport("NWC24ReadMsgFromAddr(): error %d\n", err);
296                 return;
297             }
298             OSReport("%s\n", addrStr);
299         }
300         break;
301       default:
302         break;
303     }
304 
305     return;
306 }
307 
308 /*---------------------------------------------------------------------------*
309     Views 'to' addresses.
310  *---------------------------------------------------------------------------*/
ViewTo(NWC24MsgObj * msgObj)311 void ViewTo( NWC24MsgObj* msgObj )
312 {
313     NWC24Err        err;
314     NWC24MsgType    type;
315     u32             numTo;
316     u32             i;
317 
318     err = NWC24GetMsgType(msgObj, &type);
319     if ( err != NWC24_OK )
320     {
321         OSReport("NWC24GetMsgType(): error %d\n", err);
322         return;
323     }
324 
325     err = NWC24GetMsgNumTo(msgObj, &numTo);
326     if ( err != NWC24_OK )
327     {
328         OSReport("NWC24GetMsgNumTo(): error %d\n", err);
329         return;
330     }
331 
332     switch (type)
333     {
334       case NWC24_MSGTYPE_WII_MENU_SHARED:
335       case NWC24_MSGTYPE_WII_APP:
336       case NWC24_MSGTYPE_WII_MENU:
337       case NWC24_MSGTYPE_WII_APP_HIDDEN:
338         {
339             NWC24UserId     uid;
340 
341             for ( i = 0 ; i < numTo ; ++i )
342             {
343                 err = NWC24ReadMsgToId(msgObj, i, &uid);
344                 if ( err != NWC24_OK )
345                 {
346                     OSReport("NWC24ReadMsgToId(): error %d\n", err);
347                     return;
348                 }
349                 PrintUserId(uid);
350                 OSReport("\n");
351                 if ( i < numTo - 1 )
352                 {
353                     OSReport("      ");
354                 }
355             }
356         }
357         break;
358       case NWC24_MSGTYPE_PUBLIC:
359         {
360             char    addrStr[NWC24MSG_MAX_ADDRSTR];
361 
362             for ( i = 0 ; i < numTo ; ++i )
363             {
364                 err = NWC24ReadMsgToAddr(msgObj, i, addrStr, NWC24MSG_MAX_ADDRSTR);
365                 if ( err != NWC24_OK )
366                 {
367                     OSReport("NWC24ReadMsgToAddr(): error %d\n", err);
368                     return;
369                 }
370                 OSReport("%s\n", addrStr);
371                 if ( i < numTo - 1 )
372                 {
373                     OSReport("      ");
374                 }
375             }
376         }
377         break;
378       default:
379         break;
380     }
381 
382     return;
383 }
384 
385 /*---------------------------------------------------------------------------*
386     Views subject.
387  *---------------------------------------------------------------------------*/
ViewSubject(NWC24MsgObj * msgObj)388 void ViewSubject( NWC24MsgObj* msgObj )
389 {
390     NWC24Err    err;
391     u32         bufSize;
392     char*       buffer;
393     u32         i;
394 
395     err = NWC24GetMsgSubjectSize(msgObj, &bufSize);
396     if ( err != NWC24_OK )
397     {
398         OSReport("NWC24GetMsgSubjectSize(): error %d\n", err);
399         return;
400     }
401 
402     buffer = (char*)MEMAllocFromAllocator(&Allocator, OSRoundUp32B(bufSize));
403     err = NWC24ReadMsgSubject(msgObj, buffer, bufSize);
404     if ( err != NWC24_OK )
405     {
406         OSReport("NWC24ReadMsgSubject(): error %d\n", err);
407         MEMFreeToAllocator(&Allocator, buffer);
408         return;
409     }
410 
411     for ( i = 0 ; i < bufSize ; ++i )
412     {
413         if ( buffer[i] != 0x0D && buffer[i] != 0x0A )
414         {
415             OSReport("%c", buffer[i]);
416         }
417     }
418     OSReport("\n");
419 
420     MEMFreeToAllocator(&Allocator, buffer);
421     return;
422 }
423 
424 /*---------------------------------------------------------------------------*
425     Views date.
426  *---------------------------------------------------------------------------*/
427 static const char* MonStr[12] =
428 {
429     "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
430 };
431 
432 static const char* WdayStr[7] =
433 {
434     "Sun", "Mon", "Tue", "Wed", "Thr", "Fri", "Sat"
435 };
436 
ViewDate(NWC24MsgObj * msgObj)437 void ViewDate( NWC24MsgObj* msgObj )
438 {
439     NWC24Err        err;
440     OSCalendarTime  cTime;
441 
442     err = NWC24GetMsgDate(msgObj, &cTime);
443     if ( err != NWC24_OK )
444     {
445         OSReport("NWC24GetMsgDate(): error %d\n", err);
446         return;
447     }
448 
449     OSReport("%s, %d %s %d ", WdayStr[cTime.wday], cTime.mday, MonStr[cTime.mon], cTime.year);
450     OSReport("%02d:%02d -0000\n", cTime.hour, cTime.min);
451 
452     return;
453 }
454 
455 /*---------------------------------------------------------------------------*
456     Views body text.
457  *---------------------------------------------------------------------------*/
ViewBodyText(NWC24MsgObj * msgObj)458 void ViewBodyText( NWC24MsgObj* msgObj )
459 {
460     NWC24Err        err;
461     u32             bufSize;
462     char*           buffer;
463     u32             i;
464     NWC24Charset    charset;
465     NWC24Encoding   encoding;
466 
467     err = NWC24GetMsgTextSize(msgObj, &bufSize);
468     if ( err != NWC24_OK )
469     {
470         OSReport("NWC24GetMsgTextSize(): error %d\n", err);
471         return;
472     }
473 
474     buffer = (char*)MEMAllocFromAllocator(&Allocator, OSRoundUp32B(bufSize));
475     err = NWC24ReadMsgText(msgObj, buffer, bufSize, &charset, &encoding);
476     if ( err != NWC24_OK )
477     {
478         OSReport("NWC24ReadMsgText(): error %d\n", err);
479         MEMFreeToAllocator(&Allocator, buffer);
480         return;
481     }
482 
483     OSReport("--------- [Charset=%08X  Encoding=%d] -----------\n", charset, encoding);
484 
485     for ( i = 0 ; i < bufSize ; ++i )
486     {
487         if ( buffer[i] != 0x0A )
488         {
489             OSReport("%c", buffer[i]);
490         }
491     }
492 
493     MEMFreeToAllocator(&Allocator, buffer);
494     return;
495 }
496 
497 /*---------------------------------------------------------------------------*
498     Views attachment binaries.
499  *---------------------------------------------------------------------------*/
ViewAttachment(NWC24MsgObj * msgObj)500 void ViewAttachment( NWC24MsgObj* msgObj )
501 {
502     NWC24Err        err;
503     u32             bufSize;
504     char*           buffer;
505     u32             i, j;
506     u32             numAttach;
507     NWC24MIMEType   fileType;
508 
509     err = NWC24GetMsgNumAttached(msgObj, &numAttach);
510     if ( err != NWC24_OK )
511     {
512         OSReport("NWC24GetMsgNumAttached(): error %d\n", err);
513         return;
514     }
515 
516     for ( j = 0 ; j < numAttach ; ++j )
517     {
518         err = NWC24GetMsgAttachedSize(msgObj, j, &bufSize);
519         if ( err != NWC24_OK )
520         {
521             OSReport("NWC24GetMsgAttachedSize(): error %d\n", err);
522             return;
523         }
524 
525         buffer = (char*)MEMAllocFromAllocator(&Allocator, OSRoundUp32B(bufSize));
526         err = NWC24ReadMsgAttached(msgObj, j, buffer, bufSize);
527         if ( err != NWC24_OK )
528         {
529             OSReport("bufsize = %d\n", bufSize);
530             OSReport("NWC24ReadMsgAttached(): error %d\n", err);
531             MEMFreeToAllocator(&Allocator, buffer);
532             return;
533         }
534 
535         err = NWC24GetMsgAttachedType(msgObj, j, &fileType);
536         if ( err != NWC24_OK )
537         {
538             OSReport("NWC24GetMsgAttachedType(): error %d\n", err);
539             return;
540         }
541 
542         OSReport("[Attached binary %d : Type=%08X]\n", j, fileType);
543 
544         for ( i = 0 ; i < bufSize ; ++i )
545         {
546             OSReport(" %02X", (u8)buffer[i]);
547             if ( (i % 16) == 15 )
548             {
549                 OSReport("\n");
550             }
551         }
552         OSReport("\n");
553 
554         MEMFreeToAllocator(&Allocator, buffer);
555     }
556 
557     return;
558 }
559 
560 /*---------------------------------------------------------------------------*
561     Prints user id (16-digits)
562  *---------------------------------------------------------------------------*/
PrintUserId(NWC24UserId uid)563 void PrintUserId( NWC24UserId uid )
564 {
565     char    numBuffer[16];
566     int     i;
567 
568     OSReport("w");
569 
570     for ( i = 0 ; i < 16 ; ++i )
571     {
572         numBuffer[i] = (char)(uid % 10);
573         uid /= 10;
574     }
575 
576     for ( i = 15 ; i >= 0 ; --i )
577     {
578         OSReport("%01d", numBuffer[i]);
579     }
580 }
581 
582 /*---------------------------------------------------------------------------*/
583 
584 
585