1 /*---------------------------------------------------------------------------*
2   Project:  TwlSDK - WXC - demos - relayStation-2
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 "dispfunc.h"
22 
23 
24 /*****************************************************************************/
25 /* Variables */
26 
27 /* If the relay station is in relay mode: TRUE */
28 BOOL    station_is_relay;
29 
30 /* Data buffer for exchange */
31 u8      send_data[DATA_SIZE];
32 static u8 recv_data[DATA_SIZE];
33 
34 /* The original owner of the above send data */
35 u8      send_data_owner[6];
36 
37 /* Number of successful exchanges */
38 static int data_exchange_count = 0;
39 
40 
41 /*****************************************************************************/
42 /* Functions */
43 
44 /*---------------------------------------------------------------------------*
45   Name:         CalcHash
46 
47   Description:  Hash calculation for a simple check.
48 
49   Arguments:    src: Calculation target buffer
50 
51   Returns:      The calculated value.
52  *---------------------------------------------------------------------------*/
CalcHash(const u8 * src)53 static u8 CalcHash(const u8 *src)
54 {
55     int     i;
56     u8      sum = 0;
57     for (i = 0; i < DATA_SIZE - 1; i++)
58     {
59         sum ^= src[i];
60     }
61     return sum;
62 }
63 
64 /*---------------------------------------------------------------------------*
65   Name:         user_callback
66 
67   Description:  Data exchange complete callback.
68 
69   Arguments:    stat: Notification status (normally WXC_STATE_EXCHANGE_DONE)
70                 arg: Notification argument (receive data buffer)
71 
72   Returns:      The calculated value.
73  *---------------------------------------------------------------------------*/
user_callback(WXCStateCode stat,void * arg)74 static void user_callback(WXCStateCode stat, void *arg)
75 {
76 #pragma unused(stat)
77 
78     /* Receive data information is given to the argument */
79     const WXCBlockDataFormat *recv = (const WXCBlockDataFormat *)arg;
80 
81     ++data_exchange_count;
82 
83     /*
84      * If the relay station is in relay mode, data that should be sent is re-registered with data that was just received
85      *
86      */
87     if (station_is_relay)
88     {
89         /* Saves the MAC address of the receiving party */
90         const WXCUserInfo *info = WXC_GetUserInfo((u16)((WXC_GetOwnAid() == 0) ? 1 : 0));
91         MI_CpuCopy8(info->macAddress, send_data_owner, 6);
92         /* Re-registers exchange data */
93         MI_CpuCopy8(recv->buffer, send_data, sizeof(send_data));
94         MI_CpuClear32(recv->buffer, DATA_SIZE);
95         WXC_SetInitialData(GGID_ORG_1, send_data, DATA_SIZE, recv_data, DATA_SIZE);
96     }
97 }
98 
99 /*---------------------------------------------------------------------------*
100   Name:         system_callback
101 
102   Description:  WXC library system callback.
103 
104   Arguments:    stat: Notification status
105                 arg: Notification argument (receive data buffer)
106 
107   Returns:      The calculated value.
108  *---------------------------------------------------------------------------*/
system_callback(WXCStateCode state,void * arg)109 static void system_callback(WXCStateCode state, void *arg)
110 {
111     switch (state)
112     {
113     case WXC_STATE_READY:
114         /*
115          * Issued from a WXC_Init function call.
116          * arg is always NULL.
117          */
118         break;
119 
120     case WXC_STATE_ACTIVE:
121         /*
122          * Issued from a WXC_Start function call.
123          * arg is always NULL.
124          */
125         break;
126 
127     case WXC_STATE_ENDING:
128         /*
129          * Issued from a WXC_End function call.
130          * arg is always NULL.
131          */
132         break;
133 
134     case WXC_STATE_END:
135         /*
136          * Issued upon completion of the shutdown processing run by the WXC_End function.
137          * arg is internal work memory allocated by the WXC_Init function.
138          * At this point, work memory is deallocated by the user.
139          */
140         {
141             void   *system_work = arg;
142             OS_Free(system_work);
143         }
144         break;
145 
146     case WXC_STATE_EXCHANGE_DONE:
147         /*
148          * Data exchange complete (not currently issued)
149          */
150         break;
151     }
152 }
153 
154 /*---------------------------------------------------------------------------*
155   Name:         User_Init
156 
157   Description:  User-side initialization operation for WXC library.
158 
159   Arguments:    None.
160 
161   Returns:      None.
162  *---------------------------------------------------------------------------*/
User_Init(void)163 void User_Init(void)
164 {
165     /* Initializes the WXC library internal status */
166     WXC_Init(OS_Alloc(WXC_WORK_SIZE), system_callback, 2);
167 
168     /* In relay station mode, it can only become a child device */
169     WXC_SetChildMode(TRUE);
170 
171     /*
172      * Register the initial data block.
173      * The following is for cases other than when data is received in relay mode.
174      * Registered data is not updated.
175      */
176     OS_GetMacAddress(send_data_owner);
177     (void)OS_SPrintf((char *)send_data, "data %02X:%02X:%02X:%02X:%02X:%02X",
178                      send_data_owner[0], send_data_owner[1], send_data_owner[2],
179                      send_data_owner[3], send_data_owner[4], send_data_owner[5]);
180     send_data[DATA_SIZE - 1] = CalcHash(send_data);
181     WXC_RegisterCommonData(GGID_ORG_1, user_callback, send_data, DATA_SIZE, recv_data, DATA_SIZE);
182 
183     /* WXC library wireless startup */
184     WXC_Start();
185 }
186