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