1 /*---------------------------------------------------------------------------*
2   Project:  TwlSDK - WXC - demos - wxc-pm
3   File:     user.c
4 
5   Copyright 2005-2009 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:: 2007-11-15#$
14   $Rev: 2414 $
15   $Author: hatamoto_minoru $
16  *---------------------------------------------------------------------------*/
17 #include <nitro.h>
18 
19 #include <nitro/wxc.h>
20 #include "user.h"
21 #include "common.h"
22 #include "dispfunc.h"
23 
24 
25 /*****************************************************************************/
26 /* Constants */
27 
28 /* GGID used for testing */
29 #define SDK_MAKEGGID_SYSTEM(num)              (0x003FFF00 | (num))
30 #define GGID_ORG_1                            SDK_MAKEGGID_SYSTEM(0x53)
31 #define GGID_ORG_2                            SDK_MAKEGGID_SYSTEM(0x54)
32 
33 /* Send/receive data size for testing */
34 #define DATA_SIZE (1024 * 20)
35 
36 
37 /*****************************************************************************/
38 /* Variables */
39 
40 /* Number of successful exchanges */
41 static int data_exchange_count = 0;
42 
43 /* Data buffer for testing */
44 static u8 send_data[DATA_SIZE];
45 static u8 recv_data[DATA_SIZE];
46 
47 static u8 send_data2[DATA_SIZE];
48 static u8 recv_data2[DATA_SIZE];
49 
50 extern int passby_endflg;
51 extern BOOL passby_ggid[MENU_MAX];     // GGID that does chance encounter communications
52 
53 /*---------------------------------------------------------------------------*
54   Name:         CalcHash
55 
56   Description:  Hash calculation for a simple check.
57 
58   Arguments:    src: Calculation target buffer
59 
60   Returns:      The calculated value.
61  *---------------------------------------------------------------------------*/
CalcHash(const u8 * src)62 static u8 CalcHash(const u8 *src)
63 {
64     int     i;
65     u8      sum = 0;
66     for (i = 0; i < DATA_SIZE - 1; i++)
67     {
68         sum ^= src[i];
69     }
70     return sum;
71 }
72 
73 /*---------------------------------------------------------------------------*
74   Name:         user_callback
75 
76   Description:  Data exchange complete callback.
77 
78   Arguments:    stat: Notification status (normally WXC_STATE_EXCHANGE_DONE)
79                 arg: Notification argument (receive data buffer)
80 
81   Returns:      The calculated value.
82  *---------------------------------------------------------------------------*/
user_callback(WXCStateCode stat,void * arg)83 static void user_callback(WXCStateCode stat, void *arg)
84 {
85 #pragma unused(stat)
86 
87     const WXCBlockDataFormat * block = (const WXCBlockDataFormat *)arg;
88     u8      sum = 0;
89     u8     *data = (u8 *)block->buffer;
90 
91     ++data_exchange_count;
92 
93     /* Displays debug receive data */
94     sum = CalcHash(data);
95 
96     if (sum == data[DATA_SIZE - 1])
97     {
98         BgPrintf((s16)1, (s16)5, WHITE, "sum OK 0x%02X %6d %6d sec", sum, data_exchange_count,
99                 OS_TicksToSeconds(OS_GetTick()));
100         BgPrintf((s16)1, (s16)6, WHITE, "%s", data);
101     }
102     else
103     {
104         BgPrintf((s16)1, (s16)5, WHITE, "sum NG 0x%02X %6d %6d sec", sum, data_exchange_count,
105                 OS_TicksToSeconds(OS_GetTick()));
106     }
107     MI_CpuClear32(data, DATA_SIZE);
108 
109     // End when communication is performed once
110 
111     if( WXC_IsParentMode() == TRUE)
112     {
113         passby_endflg = 1;
114     }
115     else
116     {
117         passby_endflg = 2;
118     }
119 }
120 
121 
122 /*---------------------------------------------------------------------------*
123   Name:         system_callback
124 
125   Description:  WXC library system callback.
126 
127   Arguments:    stat: Notification status (normally WXC_STATE_EXCHANGE_DONE)
128                 arg: Notification argument (receive data buffer)
129 
130   Returns:      The calculated value.
131  *---------------------------------------------------------------------------*/
system_callback(WXCStateCode state,void * arg)132 static void system_callback(WXCStateCode state, void *arg)
133 {
134     switch (state)
135     {
136     case WXC_STATE_READY:
137         /*
138          * Issued from a WXC_Init function call.
139          * arg is always NULL.
140          */
141         break;
142 
143     case WXC_STATE_ACTIVE:
144         /*
145          * Issued from a WXC_Start function call.
146          * arg is always NULL.
147          */
148         break;
149 
150     case WXC_STATE_ENDING:
151         /*
152          * Issued from a WXC_End function call.
153          * arg is always NULL.
154          */
155         break;
156 
157     case WXC_STATE_END:
158         /*
159          * Issued upon completion of the shutdown processing run by the WXC_End function.
160          * arg is internal work memory allocated by the WXC_Init function.
161          * At this point, work memory is deallocated by the user.
162          */
163         {
164             void *system_work = arg;
165             OS_Free(system_work);
166         }
167         break;
168 
169     case WXC_STATE_EXCHANGE_DONE:
170         /*
171          * Data exchange complete (not currently issued)
172          */
173         break;
174     }
175 }
176 
177 
User_Init(void)178 void User_Init(void)
179 {
180     /* Initializes the internal status of the library */
181     WXC_Init(OS_Alloc(WXC_WORK_SIZE), system_callback, 2);
182 
183     /*
184      * Register a data block.
185      * Currently, the send/receive data size is the same for this and other systems.
186      */
187     // Register if flag is set
188     if(passby_ggid[0] == TRUE)
189     {
190         (void)OS_SPrintf((char *)send_data, "---------------------");
191         send_data[DATA_SIZE - 2] = (u8)GGID_ORG_1;
192         send_data[DATA_SIZE - 1] = CalcHash(send_data);
193         WXC_RegisterCommonData(GGID_ORG_1, user_callback, send_data, DATA_SIZE, recv_data, DATA_SIZE);
194     }
195 
196     /*
197      * Register one more data block as test.
198      * Under current specifications, time-sharing is used to select each data block and data is exchanged with the child that has the same GGID.
199      *
200      */
201     // Register if flag is set
202 
203     if(passby_ggid[1] == TRUE)
204     {
205         (void)OS_SPrintf((char *)send_data2, "/////////////////////");
206         send_data2[DATA_SIZE - 2] = (u8)GGID_ORG_2;
207         send_data2[DATA_SIZE - 1] = CalcHash(send_data2);
208         WXC_RegisterCommonData(GGID_ORG_2, user_callback, send_data2, DATA_SIZE, recv_data2, DATA_SIZE);
209     }
210 
211     /* Library wireless startup */
212     WXC_Start();
213 }
214