1 /*---------------------------------------------------------------------------*
2   Project:  TwlSDK - MB - demos - multiboot-wfs - common
3   File:     common.c
4 
5   Copyright 2005-2008 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   $Date:: 2008-09-29#$
14   $Rev: 8747 $
15   $Author: okajima_manabu $
16  *---------------------------------------------------------------------------*/
17 
18 #include    "common.h"
19 #include    "wfs.h"
20 #include    "wh.h"
21 
22 
23 /*****************************************************************************/
24 /* Constants */
25 
26 /*****************************************************************************/
27 /* Functions */
28 
29 /*---------------------------------------------------------------------------*
30   Name:         StateCallbackForWFS
31 
32   Description:  WFS callback function.
33                 This is called when the state became WFS_STATE_READY.
34                 WFS_GetStatus can be used at any position to make a determination without receiving notification with this callback.
35 
36 
37   Arguments:    arg: User-defined argument specified in the callback
38 
39   Returns:      None.
40  *---------------------------------------------------------------------------*/
StateCallbackForWFS(void * arg)41 void StateCallbackForWFS(void *arg)
42 {
43     (void)arg;
44     switch (WFS_GetStatus())
45     {
46     case WFS_STATE_READY:
47         OS_TPrintf("WFS ready.\n");
48         break;
49     }
50 }
51 
52 /*---------------------------------------------------------------------------*
53   Name:         AllocatorForWFS
54 
55   Description:  Dynamic allocation function for memory specified in WFS.
56 
57   Arguments:    arg: User-defined argument specified in the allocator
58                 size: Required size if requesting memory allocation
59                 ptr: If NULL, allocates memory; Otherwise, requests deallocation
60 
61   Returns:      If ptr is NULL, the amount of memory in size is allocated and its pointer returned.
62                 If not, the ptr memory is released.
63                 If released, the return value is simply ignored.
64  *---------------------------------------------------------------------------*/
AllocatorForWFS(void * arg,u32 size,void * ptr)65 void   *AllocatorForWFS(void *arg, u32 size, void *ptr)
66 {
67     (void)arg;
68     if (!ptr)
69         return OS_Alloc(size);
70     else
71     {
72         OS_Free(ptr);
73         return NULL;
74     }
75 }
76 
77 /*---------------------------------------------------------------------------*
78   Name:         WCCallbackForWFS
79 
80   Description:  Callback that receives status notification from the WC's wireless automatic drive.
81 
82   Arguments:    arg: Callback pointer for the notification source WM function
83 
84   Returns:      None.
85  *---------------------------------------------------------------------------*/
WHCallbackForWFS(void * arg)86 void WHCallbackForWFS(void *arg)
87 {
88 //    WCStatus wcStatus = WcGetStatus();
89     switch (((WMCallback *)arg)->apiid)
90     {
91     case WM_APIID_START_MP:
92         {                              /* Begin MP state */
93             WMStartMPCallback *cb = (WMStartMPCallback *)arg;
94             switch (cb->state)
95             {
96             case WM_STATECODE_MP_START:
97                 /*
98                  * Call WFS_Start to send a notification for a transition to the MP_PARENT or MP_CHILD state
99 
100                  * Using this notification as a trigger, the WFS library begins to call the WM_SetMPDataToPort function internally to transfer blocks
101 
102                  */
103                 WFS_Start();
104                 break;
105             }
106         }
107         break;
108     }
109 }
110 
111 /*---------------------------------------------------------------------------*
112   Name:         InitFrameLoop
113 
114   Description:  Initialization for game frame loop.
115 
116   Arguments:    p_key: Key information structure to initialize
117 
118   Returns:      None.
119  *---------------------------------------------------------------------------*/
InitFrameLoop(KeyInfo * p_key)120 void InitFrameLoop(KeyInfo * p_key)
121 {
122     ClearLine();
123     ClearString();
124     KeyRead(p_key);
125 }
126 
127 /*---------------------------------------------------------------------------*
128   Name:         WaitForNextFrame
129 
130   Description:  Wait until next drawing frame.
131 
132   Arguments:    p_key: Key information structure to update
133 
134   Returns:      None.
135  *---------------------------------------------------------------------------*/
WaitForNextFrame(KeyInfo * p_key)136 void WaitForNextFrame(KeyInfo * p_key)
137 {
138     FlushLine();
139     OS_WaitVBlankIntr();
140     ClearString();
141     KeyRead(p_key);
142 }
143 
144 /*---------------------------------------------------------------------------*
145   Name:         ModeInitialize
146 
147   Description:  Initializes the wireless module.
148 
149   Arguments:    None.
150 
151   Returns:      None.
152  *---------------------------------------------------------------------------*/
ModeInitialize(void)153 void ModeInitialize(void)
154 {
155     // Use the WH module
156     // The value of WH_GetSystemState changes from WH_SYSSTATE_STOP to WH_SYSSTATE_IDLE.
157     if (!WH_Initialize())
158     {
159         OS_Panic("WH_Initialize failed.");
160     }
161 
162     WH_SetGgid(GGID_WBT_FS);
163     WH_SetSessionUpdateCallback(WHCallbackForWFS);
164 }
165 
166 /*---------------------------------------------------------------------------*
167   Name:         ModeError
168 
169   Description:  Processing in error display screen.
170 
171   Arguments:    None.
172 
173   Returns:      None.
174  *---------------------------------------------------------------------------*/
ModeError(void)175 void ModeError(void)
176 {
177     KeyInfo key[1];
178 
179     InitFrameLoop(key);
180     while (WH_GetSystemState() == WH_SYSSTATE_FATAL)
181     {
182         WaitForNextFrame(key);
183         PrintString(5, 10, COLOR_RED, "======= ERROR! =======");
184         PrintString(5, 13, COLOR_WHITE, " Fatal error occured.");
185         PrintString(5, 14, COLOR_WHITE, "Please reboot program.");
186     }
187 }
188 
189 /*---------------------------------------------------------------------------*
190   Name:         ModeWorking
191 
192   Description:  Processing in busy screen.
193 
194   Arguments:    None.
195 
196   Returns:      None.
197  *---------------------------------------------------------------------------*/
ModeWorking(void)198 void ModeWorking(void)
199 {
200     KeyInfo key[1];
201 
202     InitFrameLoop(key);
203     while (WH_GetSystemState() == WH_SYSSTATE_SCANNING || WH_GetSystemState() == WH_SYSSTATE_BUSY)
204     {
205         WaitForNextFrame(key);
206         PrintString(9, 11, COLOR_WHITE, "Now working...");
207         if (key->trg & PAD_BUTTON_START)
208         {
209             WH_Finalize();
210         }
211     }
212 }
213 
214 
215 /*---------------------------------------------------------------------------*
216   End of file
217  *---------------------------------------------------------------------------*/
218