1 /*---------------------------------------------------------------------------*
2   Project:  Wii Connect 24 API demos
3   File:     MsgList.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: MsgList.c,v $
14   Revision 1.1  2006/09/27 08:56:17  torigoe_nobutaka
15   Moved from nwc24demo/src.
16 
17   Revision 1.7  2006/09/21 02:35:13  hirose_kazuki
18   Changes due to re-arrangement of message types.
19 
20   Revision 1.6  2006/09/13 08:14:22  yosizaki
21   changed to call VFInit().
22 
23   Revision 1.5  2006/09/10 09:20:56  hirose_kazuki
24   Changed to use NWC24OpenLib/NWC24CloseLib instead of obsoleted APIs.
25 
26   Revision 1.4  2006/08/29 13:11:43  hirose_kazuki
27   Corrected message type handling.
28 
29   Revision 1.3  2006/08/14 14:39:50  yasu
30   Suppressed return value ignored warnings
31 
32   Revision 1.2  2006/07/13 12:51:58  hirose_kazuki
33   Updated due to the API spec change.
34 
35   Revision 1.1  2006/06/29 12:46:21  hirose_kazuki
36   Moved from another repository.
37 
38   Revision 1.1  2006/06/12 07:37:18  hirose
39   Initial check in.
40 
41 
42  *---------------------------------------------------------------------------*/
43 #include <revolution.h>
44 
45 #include <revolution/nand.h>
46 #include <revolution/vf.h>
47 #include <revolution/mem.h>
48 
49 #include <revolution/nwc24.h>
50 
51 #include <stdio.h>
52 
53 /*---------------------------------------------------------------------------*
54     Listing contents stored in both message boxes.
55  *---------------------------------------------------------------------------*/
56 void    ListMessageBox( NWC24MsgBoxId mBoxId );
57 void    Print16Digits( u64 num );
58 
59 /*---------------------------------------------------------------------------*/
60 MEMHeapHandle   HeapHndl;
61 MEMAllocator    Allocator;
62 
63 /*---------------------------------------------------------------------------*
64     Main
65  *---------------------------------------------------------------------------*/
main(void)66 int main(void)
67 {
68     NWC24Err    err;
69     s32         result;
70     void*       arenaLo;
71     void*       arenaHi;
72     char*       libWorkMem;
73 
74     // Memory allocator initialization
75     arenaLo = OSGetMEM1ArenaLo();
76     arenaHi = OSGetMEM1ArenaHi();
77     HeapHndl = MEMCreateExpHeap(arenaLo, (u32)arenaHi - (u32)arenaLo);
78     OSSetMEM1ArenaLo(arenaHi);
79     MEMInitAllocatorForExpHeap(&Allocator, HeapHndl, 32);
80 
81     result = NANDInit();
82     if ( result != NAND_RESULT_OK )
83     {
84         OSHalt("NANDInit() failed.\n");
85     }
86 
87     VFInit();
88 
89     OSReport("*******************************************************\n");
90     OSReport("    MessageBox List demo\n");
91     OSReport("*******************************************************\n");
92 
93     libWorkMem = MEMAllocFromAllocator(&Allocator, NWC24_WORK_MEM_SIZE);
94 
95     err = NWC24OpenLib(libWorkMem);
96     if ( err != NWC24_OK )
97     {
98         OSReport("NWC24OpenLib(): Error %d\n", err);
99         OSHalt("Failed.\n");
100     }
101 
102     // Lists contents inside the send box.
103     OSReport("\n [Send Box]\n");
104     OSReport("-------------------------------------------------------\n");
105     ListMessageBox(NWC24_SEND_BOX);
106 
107     // Lists contents inside the receive box.
108     OSReport("\n [Receive Box]\n");
109     OSReport("-------------------------------------------------------\n");
110     ListMessageBox(NWC24_RECV_BOX);
111 
112     err = NWC24CloseLib();
113     if ( err != NWC24_OK )
114     {
115         OSReport("NWC24CloseLib(): Error %d\n", err);
116         OSHalt("Failed.\n");
117     }
118 
119     MEMFreeToAllocator(&Allocator, libWorkMem);
120 
121     OSHalt("\nCompleted.\n");
122     return 0;
123 }
124 
125 /*---------------------------------------------------------------------------*
126     Listing
127  *---------------------------------------------------------------------------*/
ListMessageBox(NWC24MsgBoxId mBoxId)128 void ListMessageBox( NWC24MsgBoxId mBoxId )
129 {
130     u32             numMsgs;
131     u32             bufSize;
132     u32*            idListBuf;
133     NWC24Err        err;
134     int             i;
135 
136     err = NWC24GetNumMsgs(mBoxId, &numMsgs);
137     if ( err != NWC24_OK )
138     {
139         OSReport("NWC24GetNumMsgs(): error %d\n", err);
140         return;
141     }
142 
143     if ( numMsgs == 0 )
144     {
145         OSReport("(No message.)\n");
146         return;
147     }
148 
149     bufSize = OSRoundUp32B(numMsgs * sizeof(u32));
150     idListBuf = (u32*)MEMAllocFromAllocator(&Allocator, bufSize);
151     err = NWC24GetMsgIdList(mBoxId, idListBuf, bufSize);
152     if ( err != NWC24_OK )
153     {
154         OSReport("NWC24GetNumMsgList(): error %d\n", err);
155         return;
156     }
157 
158     for ( i = 0 ; i < numMsgs ; ++i )
159     {
160         NWC24MsgObj     msgObj;
161         NWC24MsgType    type;
162 
163         OSReport(" [%08d] ", idListBuf[i]);
164 
165         err = NWC24GetMsgObj(&msgObj, mBoxId, idListBuf[i]);
166         if ( err != NWC24_OK )
167         {
168             OSReport("NWC24GetMsgObj(): error %d\n", err);
169             return;
170         }
171 
172         (void)NWC24GetMsgType(&msgObj, &type);
173 
174         switch (type)
175         {
176           case NWC24_MSGTYPE_WII_MENU_SHARED:
177           case NWC24_MSGTYPE_WII_APP:
178           case NWC24_MSGTYPE_WII_MENU:
179           case NWC24_MSGTYPE_WII_APP_HIDDEN:
180             {
181                 // Message from/to other Wii console(s).
182                 NWC24UserId uid;
183 
184                 if ( mBoxId == NWC24_SEND_BOX )
185                 {
186                     u32         numTo;
187                     u32         j;
188 
189                     (void)NWC24GetMsgNumTo(&msgObj, &numTo);
190                     OSReport("To:");
191                     for ( j = 0 ; j < numTo ; ++j )
192                     {
193                         (void)NWC24ReadMsgToId(&msgObj, j, &uid);
194                         Print16Digits(uid);
195                     }
196                     OSReport("\n");
197                 }
198                 else
199                 {
200                     (void)NWC24GetMsgFromId(&msgObj, &uid);
201                     OSReport("From:");
202                     Print16Digits(uid);
203                     OSReport("\n");
204                 }
205             }
206             break;
207           case NWC24_MSGTYPE_PUBLIC:
208             {
209                 // Message from/to public e-mail address.
210                 if ( mBoxId == NWC24_SEND_BOX )
211                 {
212                     OSReport("To: Public e-mail address.\n");
213                 }
214                 else
215                 {
216                     OSReport("From: Public e-mail address.\n");
217                 }
218             }
219             break;
220           default:
221             break;
222         }
223     }
224 
225     MEMFreeToAllocator(&Allocator, idListBuf);
226     return;
227 }
228 
229 /*---------------------------------------------------------------------------*
230     Function for printing 16-digits.
231  *---------------------------------------------------------------------------*/
Print16Digits(u64 num)232 void Print16Digits( u64 num )
233 {
234     char buf[20];
235     u32  i;
236     u64  tmp = num;
237 
238     for ( i = 0 ; i < 16 ; ++i )
239     {
240         buf[15-i] = (char)((tmp % 10) + 0x30);
241         tmp /= 10;
242     }
243     buf[16] = '\0';
244 
245     OSReport(" %s", buf);
246 }
247 
248 
249