/*---------------------------------------------------------------------------* Project: Wii Connect24 API demos File: MsgSearch.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: MsgSearch.c,v $ Revision 1.5 2007/09/03 06:23:40 seiki_masashi Minor fixes. Revision 1.4 2007/09/03 03:21:23 seiki_masashi Minor fixes. Revision 1.3 2007/09/03 02:52:26 seiki_masashi Added NWC24SetSearchCondPublic(). Revision 1.2 2007/02/06 02:01:37 torigoe_nobutaka Changed to call OSGetAppGamename() to get application ID. Revision 1.1 2006/09/27 08:56:17 torigoe_nobutaka Moved from nwc24demo/src. Revision 1.4 2006/09/21 02:35:13 hirose_kazuki Changes due to re-arrangement of message types. Revision 1.3 2006/09/13 08:14:22 yosizaki Changed to call VFInit(). Revision 1.2 2006/09/10 09:20:56 hirose_kazuki Changed to use NWC24OpenLib/NWC24CloseLib instead of obsolete APIs. Revision 1.1 2006/08/25 06:17:59 torigoe_nobutaka Initial check in. $NoKeywords: $ *---------------------------------------------------------------------------*/ #include #include #include #include #include #include "PostTestMsg.h" #define MSGOBJ_ARRAY_SIZE 2 /*---------------------------------------------------------------------------* Static variables *---------------------------------------------------------------------------*/ static MEMHeapHandle s_HeapHndl; static MEMAllocator s_Allocator; static BOOL s_bSCondFromAddrId = FALSE; static BOOL s_bSCondSendBox = FALSE; static BOOL s_bSCondForMenu = FALSE; static BOOL s_bSCondPublic = FALSE; static BOOL s_bSCondAppId = FALSE; static NWC24UserId s_uidMy; static u32 s_appId; /*---------------------------------------------------------------------------* Forward declaration of function *---------------------------------------------------------------------------*/ static void ViewSearchConds( void ); static void ViewMessage ( NWC24MsgObj* msgObj ); static void PrintFrom ( NWC24MsgObj* msgObj ); static void PrintTo ( NWC24MsgObj* msgObj ); static void PrintSubject ( NWC24MsgObj* msgObj ); static void PrintMsgType ( NWC24MsgObj* msgObj ); static void PrintAppId ( NWC24MsgObj* msgObj ); /*---------------------------------------------------------------------------* App entry point *---------------------------------------------------------------------------*/ int main( void ) { NWC24Err err; s32 result; void* arenaLo = NULL; void* arenaHi = NULL; char* libWorkMem = NULL; u8 iLoop; /* Memory allocator initialization */ arenaLo = OSGetMEM1ArenaLo(); arenaHi = OSGetMEM1ArenaHi(); s_HeapHndl = MEMCreateExpHeap( arenaLo, (u32)arenaHi - (u32)arenaLo ); OSSetMEM1ArenaLo( arenaHi ); MEMInitAllocatorForExpHeap( &s_Allocator, s_HeapHndl, 32 ); result = NANDInit(); if ( result != NAND_RESULT_OK ) { OSHalt( "NANDInit() failed.\n" ); } VFInit(); OSReport( "*******************************************************\n" ); OSReport( " MessageBox Search demo\n" ); OSReport( "*******************************************************\n" ); libWorkMem = MEMAllocFromAllocator( &s_Allocator, NWC24_WORK_MEM_SIZE ); err = NWC24OpenLib( libWorkMem ); if ( err != NWC24_OK ) { OSReport( "NWC24OpenLib(): Error %d\n", err ); OSHalt( "Failed.\n" ); } /* Gets the user ID of the local hardware (used in search conditions)*/ err = NWC24GetMyUserId( &s_uidMy ); if ( err != NWC24_OK ) { OSReport( "NWC24GetMyUserId(): Error %d\n", err ); OSHalt( "Failed.\n" ); } /* Gets the current application ID (used in search conditions) */ s_appId = *(u32*)OSGetAppGamename(); /* Create text message */ PostTestMsg(); /* Search for and display messages while changing search conditions */ for ( iLoop = 0; iLoop < 6; ++iLoop ) { u32 iObj; u32 numStored; u32 numRemain; NWC24MsgObj msgObjArray[MSGOBJ_ARRAY_SIZE]; switch ( iLoop ) { case 0: break; case 1: s_bSCondPublic = TRUE; break; case 2: s_bSCondSendBox = TRUE; break; case 3: s_bSCondAppId = TRUE; break; case 4: s_bSCondFromAddrId = TRUE; break; case 5: s_bSCondForMenu = TRUE; break; } /* Search condition settings*/ (void)NWC24InitSearchConds(); if ( s_bSCondSendBox ) (void)NWC24SetSearchCondMsgBox( NWC24_SEND_BOX ); if ( s_bSCondAppId ) (void)NWC24SetSearchCondAppId( s_appId ); if ( s_bSCondFromAddrId ) (void)NWC24SetSearchCondFromAddrId( s_uidMy ); if ( s_bSCondForMenu ) (void)NWC24SetSearchCondForMenu(); (void)NWC24SetSearchCondPublic(s_bSCondPublic); ViewSearchConds(); do { /* Search messages */ err = NWC24SearchMsgs( msgObjArray, MSGOBJ_ARRAY_SIZE, &numStored, &numRemain ); if ( err != NWC24_OK ) { OSReport( "NWC24SearchMsgs(): Error %d\n", err ); OSHalt( "Failed.\n" ); } OSReport("===================================================\n"); OSReport(" [NWC24SearchMsgs(): Stored: %d Remain: %d]\n", numStored, numRemain); OSReport("===================================================\n"); /* Display messages */ for ( iObj = 0 ; iObj < numStored ; ++iObj ) { ViewMessage( &msgObjArray[iObj] ); } } while ( numRemain > 0 ); /* Repeat until all messages satisfying the search conditions have been gotten */ } err = NWC24CloseLib(); if ( err != NWC24_OK ) { OSReport( "NWC24CloseLib(): Error %d\n", err ); OSHalt( "Failed.\n" ); } MEMFreeToAllocator( &s_Allocator, libWorkMem ); OSHalt( "\nCompleted.\n" ); return 0; } /*---------------------------------------------------------------------------* Views search conditions. *---------------------------------------------------------------------------*/ static void ViewSearchConds( void ) { OSReport( "\n#######################################################\n" ); OSReport( " [Search Conditions]\n" ); OSReport( "-------------------------------------------------------\n" ); OSReport( " From Address ID : " ); if ( s_bSCondFromAddrId ) { OSReport( "%llu\n", s_uidMy ); } else { OSReport( "Disable\n" ); } OSReport( " Message Box : %s\n", s_bSCondSendBox ? "Send Box" : "Receive Box" ); OSReport( " For Menu : %s\n", s_bSCondForMenu ? "Enable" : "Disable" ); OSReport( " Include Public : %s\n", s_bSCondPublic ? "Enable" : "Disable" ); OSReport( " Application ID : " ); if ( s_bSCondAppId ) { OSReport( "0x%08x\n", s_appId ); } else { OSReport( "Disable\n" ); } OSReport( "#######################################################\n" ); return; } /*---------------------------------------------------------------------------* Views message data. *---------------------------------------------------------------------------*/ static void ViewMessage( NWC24MsgObj* msgObj ) { OSReport( "\n[From] " ); PrintFrom( msgObj ); OSReport( "\n[To] " ); PrintTo( msgObj ); OSReport( "\n[Subject] " ); PrintSubject( msgObj ); OSReport( "\n[Msg type] " ); PrintMsgType( msgObj ); OSReport( "\n[App ID] " ); PrintAppId( msgObj ); OSReport( "\n-------------------------------------------\n" ); return; } /*---------------------------------------------------------------------------*/ static void PrintFrom( NWC24MsgObj* msgObj ) { NWC24Err err; NWC24MsgType msgType; err = NWC24GetMsgType( msgObj, &msgType ); if ( err != NWC24_OK ) { OSReport( "NWC24GetMsgType(): Error %d\n", err ); return; } if ( msgType != NWC24_MSGTYPE_PUBLIC ) { NWC24UserId uid; err = NWC24GetMsgFromId( msgObj, &uid ); if ( err != NWC24_OK ) { OSReport( "NWC24GetMsgFromId(): Error %d\n", err ); return; } OSReport( "%016llu", uid ); } else { char strAddr[NWC24MSG_MAX_ADDRSTR]; err = NWC24ReadMsgFromAddr( msgObj, strAddr, NWC24MSG_MAX_ADDRSTR ); if ( err != NWC24_OK ) { OSReport( "NWC24ReadMsgFromAddr(): Error %d\n", err ); return; } OSReport( "%s", strAddr ); } return; } /*---------------------------------------------------------------------------*/ static void PrintTo( NWC24MsgObj* msgObj ) { NWC24Err err; NWC24MsgType msgType; u32 numTo; u32 iTo; err = NWC24GetMsgType( msgObj, &msgType ); 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; } if ( msgType != NWC24_MSGTYPE_PUBLIC ) { NWC24UserId uid; for ( iTo = 0; iTo < numTo; ++iTo ) { err = NWC24ReadMsgToId( msgObj, iTo, &uid ); if ( err != NWC24_OK ) { OSReport( "NWC24ReadMsgToId(): Error %d\n", err ); return; } OSReport( "%016llu", uid ); if ( iTo < numTo - 1 ) { OSReport( ", " ); } } } else { char strAddr[NWC24MSG_MAX_ADDRSTR]; for ( iTo = 0; iTo < numTo; ++iTo ) { err = NWC24ReadMsgToAddr( msgObj, iTo, strAddr, NWC24MSG_MAX_ADDRSTR ); if ( err != NWC24_OK ) { OSReport( "NWC24ReadMsgToAddr(): Error %d\n", err ); return; } OSReport( "%s", strAddr ); if ( iTo < numTo - 1 ) { OSReport( ", " ); } } } return; } /*---------------------------------------------------------------------------*/ static void PrintSubject( NWC24MsgObj* msgObj ) { NWC24Err err; u32 iBuf; u32 bufSize; char* buffer = NULL; err = NWC24GetMsgSubjectSize( msgObj, &bufSize ); if ( err != NWC24_OK ) { OSReport( "NWC24GetMsgSubjectSize(): Error %d\n", err ); return; } buffer = (char*)MEMAllocFromAllocator( &s_Allocator, OSRoundUp32B(bufSize) ); err = NWC24ReadMsgSubject( msgObj, buffer, bufSize ); if ( err != NWC24_OK ) { OSReport( "NWC24ReadMsgSubject(): Error %d\n", err ); return; } for ( iBuf = 0; iBuf < bufSize; ++iBuf ) { if ( buffer[iBuf] != '\r' && buffer[iBuf] != '\n' ) { OSReport( "%c", buffer[iBuf] ); } } MEMFreeToAllocator( &s_Allocator, buffer ); return; } /*---------------------------------------------------------------------------*/ static void PrintMsgType( NWC24MsgObj* msgObj ) { NWC24Err err; NWC24MsgType msgType; char* strMsgType; err = NWC24GetMsgType( msgObj, &msgType ); if ( err != NWC24_OK ) { OSReport( "NWC24GetMsgType(): Error %d\n", err ); return; } switch ( msgType ) { case NWC24_MSGTYPE_WII_MENU_SHARED: strMsgType = "Wii menu shared"; break; case NWC24_MSGTYPE_WII_APP: strMsgType = "Wii app"; break; case NWC24_MSGTYPE_WII_MENU: strMsgType = "Wii menu"; break; case NWC24_MSGTYPE_WII_APP_HIDDEN: strMsgType = "Wii app hidden"; break; case NWC24_MSGTYPE_PUBLIC: strMsgType = "Public"; break; default: strMsgType = "Unknown"; } OSReport( "%s", strMsgType ); return; } /*---------------------------------------------------------------------------*/ static void PrintAppId( NWC24MsgObj* msgObj ) { NWC24Err err; u32 appId; err = NWC24GetMsgAppId( msgObj, &appId ); if ( err != NWC24_OK ) { OSReport( "NWC24GetMsgAppId(): Error %d\n", err ); return; } OSReport( "0x%08x", appId ); return; } /*======== End of MsgSearch.c ========*/