1 /*---------------------------------------------------------------------------*
2   Project:  Wii Connect 24 API demos
3   File:     MsgPost.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: MsgPost.c,v $
14   Revision 1.2  2007/08/31 10:25:08  hirose_kazuki
15   Added NWC24Check() call.
16 
17   Revision 1.1  2006/09/27 08:56:17  torigoe_nobutaka
18   Moved from nwc24demo/src.
19 
20   Revision 1.4  2006/09/13 08:14:22  yosizaki
21   changed to call VFInit().
22 
23   Revision 1.3  2006/09/10 09:20:56  hirose_kazuki
24   Changed to use NWC24OpenLib/NWC24CloseLib instead of obsoleted APIs.
25 
26   Revision 1.2  2006/07/13 12:51:42  hirose_kazuki
27   Updated due to the API spec change.
28 
29   Revision 1.1  2006/06/29 12:46:21  hirose_kazuki
30   Moved from another repository.
31 
32   Revision 1.1  2006/06/12 07:37:18  hirose
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 #include <string.h>
47 
48 /*---------------------------------------------------------------------------*
49     A simple demo to post a message.
50  *---------------------------------------------------------------------------*/
51 void    PostTestMsg( void );
52 
53 /*---------------------------------------------------------------------------*/
54 MEMHeapHandle   HeapHndl;
55 MEMAllocator    Allocator;
56 
57 /*---------------------------------------------------------------------------*
58     Main
59  *---------------------------------------------------------------------------*/
main(void)60 int main(void)
61 {
62     NWC24Err    err;
63     s32         result;
64     void*       arenaLo;
65     void*       arenaHi;
66     char*       libWorkMem;
67 
68     // Memory allocator initialization
69     arenaLo = OSGetMEM1ArenaLo();
70     arenaHi = OSGetMEM1ArenaHi();
71     HeapHndl = MEMCreateExpHeap(arenaLo, (u32)arenaHi - (u32)arenaLo);
72     OSSetMEM1ArenaLo(arenaHi);
73     MEMInitAllocatorForExpHeap(&Allocator, HeapHndl, 32);
74 
75     result = NANDInit();
76     if ( result != NAND_RESULT_OK )
77     {
78         OSHalt("NANDInit() failed.\n");
79     }
80 
81     VFInit();
82 
83     OSReport("*******************************************************\n");
84     OSReport("    A simple demo to post a message.\n");
85     OSReport("*******************************************************\n");
86 
87     libWorkMem = MEMAllocFromAllocator(&Allocator, NWC24_WORK_MEM_SIZE);
88 
89     err = NWC24OpenLib(libWorkMem);
90     if ( err != NWC24_OK )
91     {
92         OSReport("NWC24OpenLib(): Error %d\n", err);
93         OSHalt("Failed.\n");
94     }
95 
96     err = NWC24Check(NWC24_USE_MESSAGES);
97     if ( err != NWC24_OK )
98     {
99         OSReport("NWC24Check(): Error %d (Code: %06d)\n", err, - NWC24GetErrorCode());
100         OSHalt("Failed.\n");
101     }
102 
103     // Post a message.
104     PostTestMsg();
105 
106     err = NWC24CloseLib();
107     if ( err != NWC24_OK )
108     {
109         OSReport("NWC24CloseLib(): Error %d\n", err);
110         OSHalt("Failed.\n");
111     }
112 
113     MEMFreeToAllocator(&Allocator, libWorkMem);
114 
115     OSHalt("\nCompleted.\n");
116     return 0;
117 }
118 
119 /*---------------------------------------------------------------------------*
120     Data for this test.
121  *---------------------------------------------------------------------------*/
122 static char* TestSubject = "Test Message";
123 static char* TestMsgText =
124     "Hello WiiConnect24 World!!\x0d\x0a"
125     "This is a test mail.\x0d\x0a"
126     "Thank you.\x0d\x0a";
127 
128 static NWC24UserId TestIdTo = 9999999999999999ULL;  /* Invalid destination */
129 
130 /*---------------------------------------------------------------------------*
131     Post a test message into the send box.
132  *---------------------------------------------------------------------------*/
PostTestMsg(void)133 void PostTestMsg( void )
134 {
135     NWC24Err        err;
136     NWC24MsgObj     msgObj;
137 
138     // Initializes the object, specifies Wii-to-Wii message.
139     err = NWC24InitMsgObj(&msgObj, NWC24_MSGTYPE_WII);
140     if ( err != NWC24_OK )
141     {
142         OSReport("NWC24InitMsgObj(): error %d\n", err);
143         return;
144     }
145 
146     // To address (by user id.)
147     err = NWC24SetMsgToId(&msgObj, TestIdTo);
148     if ( err != NWC24_OK )
149     {
150         OSReport("NWC24SetMsgToId(): error %d\n", err);
151         return;
152     }
153 
154     // Subject
155     err = NWC24SetMsgSubject(&msgObj, TestSubject, (u32)strlen(TestSubject));
156     if ( err != NWC24_OK )
157     {
158         OSReport("NWC24SetMsgSubject(): error %d\n", err);
159         return;
160     }
161 
162     // Message body (text)
163     err = NWC24SetMsgText(&msgObj, TestMsgText, (u32)strlen(TestMsgText),
164                           NWC24_US_ASCII, NWC24_ENC_7BIT);
165     if ( err != NWC24_OK )
166     {
167         OSReport("NWC24SetMsgText(): error %d\n", err);
168         return;
169     }
170 
171     // Commit settings and post the message into the send box.
172     err = NWC24CommitMsg(&msgObj);
173     if ( err != NWC24_OK )
174     {
175         OSReport("NWC24CommitMsg: error %d\n", err);
176         return;
177     }
178 
179     OSReport("Posted a test message successfully.\n");
180 
181     return;
182 }
183 
184 
185