/*---------------------------------------------------------------------------* Project: Wii Connect 24 API demos File: MsgPrint.c Copyright 2006 Nintendo. All rights reserved. These coded instructions, statements, and computer programs contain proprietary information of Nintendo of America Inc. and/or Nintendo Company Ltd., and are protected by Federal copyright law. They may not be disclosed to third parties or copied or duplicated in any form, in whole or in part, without the prior written consent of Nintendo. $Log: MsgPrint.c,v $ Revision 1.2 2006/11/24 07:28:21 adachi_hiroaki Fixed a memory corruption bug. Revision 1.1 2006/09/27 08:56:17 torigoe_nobutaka Moved from nwc24demo/src. Revision 1.3 2006/09/21 02:35:13 hirose_kazuki Changes due to re-arrangement of message types. Revision 1.2 2006/09/13 08:14:22 yosizaki changed to call VFInit(). Revision 1.1 2006/09/10 09:27:08 hirose_kazuki Renamed from MsgView.c Revision 1.5 2006/09/10 09:20:56 hirose_kazuki Changed to use NWC24OpenLib/NWC24CloseLib instead of obsoleted APIs. Revision 1.4 2006/08/29 13:11:39 hirose_kazuki Corrected message type handling. Revision 1.3 2006/08/08 04:53:27 hirose_kazuki Added ViewDate(). Revision 1.2 2006/07/13 12:50:54 hirose_kazuki Changed to display more attributes. Revision 1.1 2006/06/29 12:46:21 hirose_kazuki Moved from another repository. Revision 1.1 2006/06/12 07:37:18 hirose Initial check in. *---------------------------------------------------------------------------*/ #include #include #include #include #include #include /*---------------------------------------------------------------------------* Printing contents stored in both message boxes. *---------------------------------------------------------------------------*/ static void GetIdList( NWC24MsgBoxId mBoxId ); static void ReleaseIdList( void ); static void ViewMessage( NWC24MsgBoxId mBoxId, u32 msgId ); static void ViewFrom( NWC24MsgObj* msgObj ); static void ViewTo( NWC24MsgObj* msgObj ); static void ViewSubject( NWC24MsgObj* msgObj ); static void ViewDate( NWC24MsgObj* msgObj ); static void ViewBodyText( NWC24MsgObj* msgObj ); static void ViewAttachment( NWC24MsgObj* msgObj ); static void PrintUserId( NWC24UserId uid ); /*---------------------------------------------------------------------------*/ static MEMHeapHandle HeapHndl; static MEMAllocator Allocator; static u32* IdListBuf = 0; static u32 NumMsgs = 0; /*---------------------------------------------------------------------------* Main *---------------------------------------------------------------------------*/ int main(void) { NWC24Err err; s32 result; void* arenaLo; void* arenaHi; char* libWorkMem; u32 i; // Memory allocator initialization arenaLo = OSGetMEM1ArenaLo(); arenaHi = OSGetMEM1ArenaHi(); HeapHndl = MEMCreateExpHeap(arenaLo, (u32)arenaHi - (u32)arenaLo); OSSetMEM1ArenaLo(arenaHi); MEMInitAllocatorForExpHeap(&Allocator, HeapHndl, 32); result = NANDInit(); if ( result != NAND_RESULT_OK ) { OSHalt("NANDInit() failed.\n"); } VFInit(); OSReport("*******************************************************\n"); OSReport(" Message Print demo\n"); OSReport("*******************************************************\n"); libWorkMem = MEMAllocFromAllocator(&Allocator, NWC24_WORK_MEM_SIZE); err = NWC24OpenLib(libWorkMem); if ( err != NWC24_OK ) { OSReport("NWC24OpenLib(): Error %d\n", err); OSHalt("Failed.\n"); } // View messages in send box. GetIdList(NWC24_SEND_BOX); for ( i = 0 ; i < NumMsgs ; ++i ) { ViewMessage(NWC24_SEND_BOX, IdListBuf[i]); } ReleaseIdList(); // View messages in receive box. GetIdList(NWC24_RECV_BOX); for ( i = 0 ; i < NumMsgs ; ++i ) { ViewMessage(NWC24_RECV_BOX, IdListBuf[i]); } ReleaseIdList(); err = NWC24CloseLib(); if ( err != NWC24_OK ) { OSReport("NWC24CloseLib(): Error %d\n", err); OSHalt("Failed.\n"); } MEMFreeToAllocator(&Allocator, libWorkMem); OSHalt("\nCompleted.\n"); return 0; } /*---------------------------------------------------------------------------* Obtains message list *---------------------------------------------------------------------------*/ void GetIdList( NWC24MsgBoxId mBoxId ) { u32 bufSize; NWC24Err err; err = NWC24GetNumMsgs(mBoxId, &NumMsgs); if ( err != NWC24_OK ) { OSReport("NWC24GetNumMsgs(): error %d\n", err); return; } if ( NumMsgs == 0 ) { IdListBuf = 0; if ( mBoxId == NWC24_RECV_BOX ) { OSReport("(Receive box has no message.)\n"); } else { OSReport("(Send box has no message.)\n"); } return; } bufSize = OSRoundUp32B(NumMsgs * sizeof(u32)); IdListBuf = (u32*)MEMAllocFromAllocator(&Allocator, bufSize); err = NWC24GetMsgIdList(mBoxId, IdListBuf, NumMsgs); if ( err != NWC24_OK ) { OSReport("NWC24GetNumMsgList(): error %d\n", err); return; } return; } /*---------------------------------------------------------------------------* Releases message list *---------------------------------------------------------------------------*/ void ReleaseIdList( void ) { if ( NumMsgs == 0 || IdListBuf == 0 ) return; MEMFreeToAllocator(&Allocator, IdListBuf); return; } /*---------------------------------------------------------------------------* Views specified message. *---------------------------------------------------------------------------*/ void ViewMessage( NWC24MsgBoxId mBoxId, u32 msgId ) { NWC24Err err; NWC24MsgObj msgObj; err = NWC24GetMsgObj(&msgObj, mBoxId, msgId); if ( err != NWC24_OK ) { OSReport("NWC24GetMsgObj(): error %d\n", err); return; } OSReport("====================================================\n"); if ( mBoxId == NWC24_RECV_BOX ) { OSReport("Receive Box /"); } else { OSReport("Send Box /"); } OSReport("Message ID: %07d\n", msgId); OSReport("From: "); ViewFrom(&msgObj); OSReport("To: "); ViewTo(&msgObj); OSReport("Subject: "); ViewSubject(&msgObj); OSReport("Date: "); ViewDate(&msgObj); OSReport("\n"); ViewBodyText(&msgObj); OSReport("\n"); OSReport("----------------------------------------------------\n"); ViewAttachment(&msgObj); OSReport("\n"); } /*---------------------------------------------------------------------------* Views 'from' address. *---------------------------------------------------------------------------*/ void ViewFrom( NWC24MsgObj* msgObj ) { NWC24Err err; NWC24MsgType type; err = NWC24GetMsgType(msgObj, &type); if ( err != NWC24_OK ) { OSReport("NWC24GetMsgType(): error %d\n", err); return; } switch (type) { case NWC24_MSGTYPE_WII_MENU_SHARED: case NWC24_MSGTYPE_WII_APP: case NWC24_MSGTYPE_WII_MENU: case NWC24_MSGTYPE_WII_APP_HIDDEN: { NWC24UserId uid; err = NWC24GetMsgFromId(msgObj, &uid); if ( err != NWC24_OK ) { OSReport("NWC24GetMsgFromId(): error %d\n", err); return; } PrintUserId(uid); OSReport("\n"); } break; case NWC24_MSGTYPE_PUBLIC: { char addrStr[NWC24MSG_MAX_ADDRSTR]; err = NWC24ReadMsgFromAddr(msgObj, addrStr, NWC24MSG_MAX_ADDRSTR); if ( err != NWC24_OK ) { OSReport("NWC24ReadMsgFromAddr(): error %d\n", err); return; } OSReport("%s\n", addrStr); } break; default: break; } return; } /*---------------------------------------------------------------------------* Views 'to' addresses. *---------------------------------------------------------------------------*/ void ViewTo( NWC24MsgObj* msgObj ) { NWC24Err err; NWC24MsgType type; u32 numTo; u32 i; err = NWC24GetMsgType(msgObj, &type); if ( err != NWC24_OK ) { OSReport("NWC24GetMsgType(): error %d\n", err); return; } err = NWC24GetMsgNumTo(msgObj, &numTo); if ( err != NWC24_OK ) { OSReport("NWC24GetMsgNumTo(): error %d\n", err); return; } switch (type) { case NWC24_MSGTYPE_WII_MENU_SHARED: case NWC24_MSGTYPE_WII_APP: case NWC24_MSGTYPE_WII_MENU: case NWC24_MSGTYPE_WII_APP_HIDDEN: { NWC24UserId uid; for ( i = 0 ; i < numTo ; ++i ) { err = NWC24ReadMsgToId(msgObj, i, &uid); if ( err != NWC24_OK ) { OSReport("NWC24ReadMsgToId(): error %d\n", err); return; } PrintUserId(uid); OSReport("\n"); if ( i < numTo - 1 ) { OSReport(" "); } } } break; case NWC24_MSGTYPE_PUBLIC: { char addrStr[NWC24MSG_MAX_ADDRSTR]; for ( i = 0 ; i < numTo ; ++i ) { err = NWC24ReadMsgToAddr(msgObj, i, addrStr, NWC24MSG_MAX_ADDRSTR); if ( err != NWC24_OK ) { OSReport("NWC24ReadMsgToAddr(): error %d\n", err); return; } OSReport("%s\n", addrStr); if ( i < numTo - 1 ) { OSReport(" "); } } } break; default: break; } return; } /*---------------------------------------------------------------------------* Views subject. *---------------------------------------------------------------------------*/ void ViewSubject( NWC24MsgObj* msgObj ) { NWC24Err err; u32 bufSize; char* buffer; u32 i; err = NWC24GetMsgSubjectSize(msgObj, &bufSize); if ( err != NWC24_OK ) { OSReport("NWC24GetMsgSubjectSize(): error %d\n", err); return; } buffer = (char*)MEMAllocFromAllocator(&Allocator, OSRoundUp32B(bufSize)); err = NWC24ReadMsgSubject(msgObj, buffer, bufSize); if ( err != NWC24_OK ) { OSReport("NWC24ReadMsgSubject(): error %d\n", err); MEMFreeToAllocator(&Allocator, buffer); return; } for ( i = 0 ; i < bufSize ; ++i ) { if ( buffer[i] != 0x0D && buffer[i] != 0x0A ) { OSReport("%c", buffer[i]); } } OSReport("\n"); MEMFreeToAllocator(&Allocator, buffer); return; } /*---------------------------------------------------------------------------* Views date. *---------------------------------------------------------------------------*/ static const char* MonStr[12] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; static const char* WdayStr[7] = { "Sun", "Mon", "Tue", "Wed", "Thr", "Fri", "Sat" }; void ViewDate( NWC24MsgObj* msgObj ) { NWC24Err err; OSCalendarTime cTime; err = NWC24GetMsgDate(msgObj, &cTime); if ( err != NWC24_OK ) { OSReport("NWC24GetMsgDate(): error %d\n", err); return; } OSReport("%s, %d %s %d ", WdayStr[cTime.wday], cTime.mday, MonStr[cTime.mon], cTime.year); OSReport("%02d:%02d -0000\n", cTime.hour, cTime.min); return; } /*---------------------------------------------------------------------------* Views body text. *---------------------------------------------------------------------------*/ void ViewBodyText( NWC24MsgObj* msgObj ) { NWC24Err err; u32 bufSize; char* buffer; u32 i; NWC24Charset charset; NWC24Encoding encoding; err = NWC24GetMsgTextSize(msgObj, &bufSize); if ( err != NWC24_OK ) { OSReport("NWC24GetMsgTextSize(): error %d\n", err); return; } buffer = (char*)MEMAllocFromAllocator(&Allocator, OSRoundUp32B(bufSize)); err = NWC24ReadMsgText(msgObj, buffer, bufSize, &charset, &encoding); if ( err != NWC24_OK ) { OSReport("NWC24ReadMsgText(): error %d\n", err); MEMFreeToAllocator(&Allocator, buffer); return; } OSReport("--------- [Charset=%08X Encoding=%d] -----------\n", charset, encoding); for ( i = 0 ; i < bufSize ; ++i ) { if ( buffer[i] != 0x0A ) { OSReport("%c", buffer[i]); } } MEMFreeToAllocator(&Allocator, buffer); return; } /*---------------------------------------------------------------------------* Views attachment binaries. *---------------------------------------------------------------------------*/ void ViewAttachment( NWC24MsgObj* msgObj ) { NWC24Err err; u32 bufSize; char* buffer; u32 i, j; u32 numAttach; NWC24MIMEType fileType; err = NWC24GetMsgNumAttached(msgObj, &numAttach); if ( err != NWC24_OK ) { OSReport("NWC24GetMsgNumAttached(): error %d\n", err); return; } for ( j = 0 ; j < numAttach ; ++j ) { err = NWC24GetMsgAttachedSize(msgObj, j, &bufSize); if ( err != NWC24_OK ) { OSReport("NWC24GetMsgAttachedSize(): error %d\n", err); return; } buffer = (char*)MEMAllocFromAllocator(&Allocator, OSRoundUp32B(bufSize)); err = NWC24ReadMsgAttached(msgObj, j, buffer, bufSize); if ( err != NWC24_OK ) { OSReport("bufsize = %d\n", bufSize); OSReport("NWC24ReadMsgAttached(): error %d\n", err); MEMFreeToAllocator(&Allocator, buffer); return; } err = NWC24GetMsgAttachedType(msgObj, j, &fileType); if ( err != NWC24_OK ) { OSReport("NWC24GetMsgAttachedType(): error %d\n", err); return; } OSReport("[Attached binary %d : Type=%08X]\n", j, fileType); for ( i = 0 ; i < bufSize ; ++i ) { OSReport(" %02X", (u8)buffer[i]); if ( (i % 16) == 15 ) { OSReport("\n"); } } OSReport("\n"); MEMFreeToAllocator(&Allocator, buffer); } return; } /*---------------------------------------------------------------------------* Prints user id (16-digits) *---------------------------------------------------------------------------*/ void PrintUserId( NWC24UserId uid ) { char numBuffer[16]; int i; OSReport("w"); for ( i = 0 ; i < 16 ; ++i ) { numBuffer[i] = (char)(uid % 10); uid /= 10; } for ( i = 15 ; i >= 0 ; --i ) { OSReport("%01d", numBuffer[i]); } } /*---------------------------------------------------------------------------*/