1 /*---------------------------------------------------------------------------*
2   Project:  TwlSDK - WXC - demos - wxc-dataShare
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 
18 #include <nitro.h>
19 
20 #include <nitro/wxc.h>
21 #include "user.h"
22 
23 #include "common.h"
24 #include "disp.h"
25 #include "gmain.h"
26 #include "wh.h"
27 
28 static int data_exchange_count = 0;
29 
30 /**** Debug Data Buffer ****/
31 #define DATA_SIZE 1024*20
32 static u8 send_data[DATA_SIZE];
33 static u8 recv_data[DATA_SIZE];
34 
35 /* GGID used for testing */
36 #define SDK_MAKEGGID_SYSTEM(num)              (0x003FFF00 | (num))
37 #define GGID_ORG_1                            SDK_MAKEGGID_SYSTEM(0x52)
38 /*---------------------------------------------------------------------------*
39     External Variable Definitions
40  *---------------------------------------------------------------------------*/
41 extern int passby_endflg;
42 extern WMBssDesc bssdesc;
43 extern WMParentParam parentparam;
44 extern u8 macAddress[6];
45 
46 /*---------------------------------------------------------------------------*
47   Name:         CalcHash
48 
49   Description:  Hash calculation for a simple check.
50 
51   Arguments:    src: Calculation target buffer
52 
53   Returns:      The calculated value.
54  *---------------------------------------------------------------------------*/
CalcHash(const u8 * src)55 static u8 CalcHash(const u8 *src)
56 {
57     int     i;
58     u8      sum = 0;
59     for (i = 0; i < DATA_SIZE - 1; i++)
60     {
61         sum ^= src[i];
62     }
63     return sum;
64 }
65 
66 /*---------------------------------------------------------------------------*
67   Name:         user_callback
68 
69   Description:  Data exchange complete callback.
70 
71   Arguments:    stat: Notification status (normally WXC_STATE_EXCHANGE_DONE)
72                 arg: Notification argument (receive data buffer)
73 
74   Returns:      The calculated value.
75  *---------------------------------------------------------------------------*/
user_callback(WXCStateCode stat,void * arg)76 static void user_callback(WXCStateCode stat, void *arg)
77 {
78 #pragma unused(stat)
79 
80     const WXCBlockDataFormat * block = (const WXCBlockDataFormat *)arg;
81     u8      sum = 0;
82     u8     *data = (u8 *)block->buffer;
83 
84     ++data_exchange_count;
85 
86     /* Displays debug receive data */
87     sum = CalcHash(data);
88 
89     if (sum == data[DATA_SIZE - 1])
90     {
91         OS_TPrintf("sum OK 0x%02X %6d %6d sec\n", sum, data_exchange_count,
92                 OS_TicksToSeconds(OS_GetTick()));
93         OS_TPrintf("%s\n", data);
94     }
95     else
96     {
97         OS_TPrintf("sum NG 0x%02X sum 0x0%02X %6d sec", sum, data[DATA_SIZE - 1],
98                OS_TicksToSeconds(OS_GetTick()));
99     }
100     MI_CpuClear32(data, DATA_SIZE);
101 
102     // End when communication is performed once
103 
104     if( WXC_IsParentMode() == TRUE)
105     {
106         const WMParentParam *param;
107 
108         passby_endflg = 1;
109 
110         // Get parent information in advance
111         param = WXC_GetParentParam();
112         MI_CpuCopyFast( param, &parentparam, sizeof(WMParentParam) );
113     }
114     else
115     {
116         const WMBssDesc *bss;
117 
118         passby_endflg = 2;
119         // Get parent's MAC address
120         bss = WXC_GetParentBssDesc();
121         MI_CpuCopyFast( bss, &bssdesc, sizeof(WMBssDesc) );
122     }
123 }
124 
125 /*---------------------------------------------------------------------------*
126   Name:         system_callback
127 
128   Description:  WXC library system callback.
129 
130   Arguments:    stat: Notification status
131                 arg: Notification argument (the meaning changes based on the notification status)
132 
133   Returns:      The calculated value.
134  *---------------------------------------------------------------------------*/
system_callback(WXCStateCode state,void * arg)135 static void system_callback(WXCStateCode state, void *arg)
136 {
137     switch (state)
138     {
139     case WXC_STATE_READY:
140         /*
141          * Issued from a WXC_Init function call.
142          * arg is always NULL.
143          */
144         break;
145 
146     case WXC_STATE_ACTIVE:
147         /*
148          * Issued from a WXC_Start function call.
149          * arg is always NULL.
150          */
151         break;
152 
153     case WXC_STATE_ENDING:
154         /*
155          * Issued from a WXC_End function call.
156          * arg is always NULL.
157          */
158         break;
159 
160     case WXC_STATE_END:
161         /*
162          * Issued upon completion of the shutdown processing run by the WXC_End function.
163          * arg is internal work memory allocated by the WXC_Init function.
164          * At this point, work memory is deallocated by the user.
165          */
166         {
167             void *system_work = arg;
168             OS_Free(system_work);
169         }
170         break;
171 
172 	case WXC_STATE_CONNECTED:
173 		/*
174 		 * Issued when a new connection is made.
175 		 * arg is a pointer to the WXCUserInfo structure that shows the connection target.
176 		 */
177 		{
178 			const WXCUserInfo * user = (const WXCUserInfo *)arg;
179 			OS_TPrintf("connected(%2d):%02X:%02X:%02X:%02X:%02X:%02X\n",
180 				user->aid,
181 				user->macAddress[0], user->macAddress[1], user->macAddress[2],
182 				user->macAddress[3], user->macAddress[4], user->macAddress[5]);
183             MI_CpuCopy16( user->macAddress, macAddress, sizeof(u8)*6 );
184 		}
185 		break;
186 
187 	case WXC_STATE_EXCHANGE_DONE:
188 		/*
189 		 * Data exchange complete (not currently issued)
190 		 */
191 		break;
192 
193     }
194 }
195 
196 
User_Init(void)197 void User_Init(void)
198 {
199     u8      MacAddress[6];
200 
201 
202     /* Initializes the internal status of the library */
203     WXC_Init(OS_Alloc(WXC_WORK_SIZE), system_callback, 2);
204 
205     /* Set current situation data */
206     OS_GetMacAddress(MacAddress);
207     (void)OS_SPrintf((char *)send_data, "data %02X:%02X:%02X:%02X:%02X:%02X",
208                   MacAddress[0], MacAddress[1], MacAddress[2],
209                   MacAddress[3], MacAddress[4], MacAddress[5]);
210 
211     send_data[DATA_SIZE - 1] = CalcHash(send_data);
212 
213     /* Register own station data */
214     /* Target station data receive buffer. Data size is same as own station data size */
215     WXC_RegisterCommonData(GGID_ORG_1, user_callback, send_data, DATA_SIZE, recv_data, DATA_SIZE);
216 
217     /* Library wireless startup */
218     WXC_Start();
219 }
220