1 /*---------------------------------------------------------------------------*
2   Project:  TwlSDK - WXC - demos - relayStation-1
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 /* Send/receive data size for testing */
29 #define DATA_SIZE (1024 * 20)
30 
31 /* GGID list compatible with relay stations */
32 static u32 ggid_list[] =
33 {
34     GGID_ORG_1,
35     GGID_ORG_2,
36 	0,
37 } ;
38 
39 
40 /*****************************************************************************/
41 /* Variables */
42 
43 /* Number of successful exchanges */
44 static int data_exchange_count = 0;
45 
46 /* Send/receive data buffer */
47 static u8 send_data[2][DATA_SIZE];
48 static u8 recv_data[2][DATA_SIZE];
49 
50 
51 /*---------------------------------------------------------------------------*
52   Name:         CalcHash
53 
54   Description:  Hash calculation for a simple check.
55 
56   Arguments:    src: Calculation target buffer
57 
58   Returns:      The calculated value.
59  *---------------------------------------------------------------------------*/
CalcHash(const u8 * src)60 static u8 CalcHash(const u8 *src)
61 {
62     int     i;
63     u8      sum = 0;
64     for (i = 0; i < DATA_SIZE - 1; i++)
65     {
66         sum ^= src[i];
67     }
68     return sum;
69 }
70 
71 /*---------------------------------------------------------------------------*
72   Name:         CreateData
73 
74   Description:  Creates send data.
75 
76   Arguments:    buf: Data storage target
77 
78   Returns:      None.
79  *---------------------------------------------------------------------------*/
CreateData(void * buf)80 static void CreateData(void *buf)
81 {
82 	*(OSTick*)buf = OS_GetTick();
83 	OS_GetMacAddress((u8*)buf + sizeof(OSTick));
84 }
85 
86 
87 /*---------------------------------------------------------------------------*
88   Name:         user_callback
89 
90   Description:  Data exchange complete callback.
91 
92   Arguments:    stat: Notification status (normally WXC_STATE_EXCHANGE_DONE)
93                 arg: Notification argument (receive data buffer)
94 
95   Returns:      The calculated value.
96  *---------------------------------------------------------------------------*/
user_callback(WXCStateCode stat,void * arg)97 static void user_callback(WXCStateCode stat, void *arg)
98 {
99 #pragma unused(stat)
100 
101     const WXCBlockDataFormat * block = (const WXCBlockDataFormat *)arg;
102     u8     *recv = (u8 *)block->buffer;
103 
104     ++data_exchange_count;
105 
106     /* Display the received data */
107 	{
108 	    const u8 *dst_mac = recv + sizeof(OSTick);
109 		u8      sum = CalcHash(recv);
110 		if (sum == recv[DATA_SIZE - 1])
111 		{
112 			BgPrintf((s16)1, (s16)5, WHITE, "sum OK 0x%02X %6d %6d sec", sum, data_exchange_count,
113 					OS_TicksToSeconds(OS_GetTick()));
114 			BgPrintf((s16)1, (s16)6, WHITE, "  (from %02X:%02X:%02X:%02X:%02X:%02X)\n\n",
115 						dst_mac[0], dst_mac[1], dst_mac[2], dst_mac[3], dst_mac[4], dst_mac[5]);
116 		}
117 		else
118 		{
119 			BgPrintf((s16)1, (s16)5, WHITE, "sum NG 0x%02X %6d %6d sec", sum, data_exchange_count,
120 					OS_TicksToSeconds(OS_GetTick()));
121 		}
122 	}
123 
124     /* Configures the next data buffer for exchange */
125     {
126 		/* Manages the buffers independently for each corresponding GGID */
127 		const u32 ggid = WXC_GetCurrentGgid();
128 		int index = 0;
129 		for ( index = 0 ; (ggid_list[index]|WXC_GGID_COMMON_BIT) != ggid ; ++index )
130 		{
131 		}
132 		/*
133 		 * The data received this time can either be used during the next submission (relay mode) or read and discarded here (distribution mode)
134 		 *
135 		 *
136 		 *
137 		 */
138 		if (keep_flg)
139 		{
140 			CreateData(send_data[index]);
141 		}
142 		else
143 		{
144 		    MI_CpuCopy8(recv, send_data[index], sizeof(send_data[index]));
145 		}
146 		/*
147 		 * Re-configure data
148 		 * This is the same as specifying NULL for the send data in WXC_RegisterCommonData() and then using WXC_AddData() when there is a CONNECTED notification.
149 		 *
150 		 */
151 		send_data[index][DATA_SIZE - 2] = (u8)ggid;
152 		send_data[index][DATA_SIZE - 1] = CalcHash(send_data[index]);
153 		MI_CpuClear32(recv, DATA_SIZE);
154 		WXC_SetInitialData(ggid, send_data[index], DATA_SIZE, recv_data[index], DATA_SIZE);
155     }
156 
157 }
158 
159 
160 /*---------------------------------------------------------------------------*
161   Name:         system_callback
162 
163   Description:  WXC library system callback.
164 
165   Arguments:    stat: Notification status (normally WXC_STATE_EXCHANGE_DONE)
166                 arg: Notification argument (receive data buffer)
167 
168   Returns:      The calculated value.
169  *---------------------------------------------------------------------------*/
system_callback(WXCStateCode state,void * arg)170 static void system_callback(WXCStateCode state, void *arg)
171 {
172     switch (state)
173     {
174     case WXC_STATE_READY:
175         /*
176          * Issued from a WXC_Init function call.
177          * arg is always NULL.
178          */
179         break;
180 
181     case WXC_STATE_ACTIVE:
182         /*
183          * Issued from a WXC_Start function call.
184          * arg is always NULL.
185          */
186         break;
187 
188     case WXC_STATE_ENDING:
189         /*
190          * Issued from a WXC_End function call.
191          * arg is always NULL.
192          */
193         break;
194 
195     case WXC_STATE_END:
196         /*
197          * Issued upon completion of the shutdown processing run by the WXC_End function.
198          * arg is internal work memory allocated by the WXC_Init function.
199          * At this point, work memory is deallocated by the user.
200          */
201         {
202             void *system_work = arg;
203             OS_Free(system_work);
204         }
205         break;
206 
207     case WXC_STATE_EXCHANGE_DONE:
208         /*
209          * Data exchange complete (not currently issued)
210          */
211         break;
212     }
213 }
214 
215 
User_Init(void)216 void User_Init(void)
217 {
218 	int	index;
219 
220     /* Initializes the internal status of the library */
221     WXC_Init(OS_Alloc(WXC_WORK_SIZE), system_callback, 2);
222 
223     /* In relay station mode, it can only become a child device */
224     WXC_SetChildMode(TRUE);
225 
226     /* Registers the first data block of each GGID */
227     for ( index = 0 ; ggid_list[index] ; ++index )
228     {
229         if(passby_ggid[index])
230         {
231             CreateData(send_data[index]);
232             send_data[index][DATA_SIZE - 2] = (u8)ggid_list[index];
233             send_data[index][DATA_SIZE - 1] = CalcHash(send_data[index]);
234             WXC_RegisterCommonData(ggid_list[index], user_callback, send_data[index], DATA_SIZE, recv_data[index], DATA_SIZE);
235         }
236     }
237 
238     /* Library wireless startup */
239     WXC_Start();
240 }
241