1 /*---------------------------------------------------------------------------*
2   Project:  TwlSDK - WM - libraries
3   File:     wm_ks.c
4 
5   Copyright 2003-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-18#$
14   $Rev: 8573 $
15   $Author: okubata_ryoma $
16  *---------------------------------------------------------------------------*/
17 
18 #include    <nitro/wm.h>
19 #include    "wm_arm9_private.h"
20 
21 
22 /*---------------------------------------------------------------------------*
23   Name:         WM_StartKeySharing
24 
25   Description:  Enables key-sharing features.
26                 By performing MP communication after enabling the feature, key sharing communications will be carried out by piggybacking on the MP communications.
27 
28 
29   Arguments:    buf:            Pointer to the buffer that stores the key information.
30                                 Its actual body is a pointer to a WMDataSharingInfo structure.
31                 port:        Number of port to use.
32 
33   Returns:      WMErrCode:      Returns the processing result.
34  *---------------------------------------------------------------------------*/
WM_StartKeySharing(WMKeySetBuf * buf,u16 port)35 WMErrCode WM_StartKeySharing(WMKeySetBuf *buf, u16 port)
36 {
37     return WM_StartDataSharing(buf, port, 0xffff, WM_KEYDATA_SIZE, TRUE);
38 }
39 
40 /*---------------------------------------------------------------------------*
41   Name:         WM_EndKeySharing
42 
43   Description:  Disables the key sharing feature.
44 
45   Arguments:    buf:            Pointer to the buffer that stores the key information.
46                                 Its actual body is a pointer to a WMDataSharingInfo structure.
47 
48   Returns:      WMErrCode:      Returns the processing result.
49  *---------------------------------------------------------------------------*/
WM_EndKeySharing(WMKeySetBuf * buf)50 WMErrCode WM_EndKeySharing(WMKeySetBuf *buf)
51 {
52     return WM_EndDataSharing(buf);
53 }
54 
55 /*---------------------------------------------------------------------------*
56   Name:         WM_GetKeySet
57 
58   Description:  Loads one key set data that has been key shared.
59 
60   Arguments:    buf:            Pointer to the buffer that stores the key information.
61                                 Its actual body is a pointer to a WMDataSharingInfo structure.
62                 keySet:         Pointer to the buffer that reads out the key set.
63                                 Specify a buffer other than the one given with WM_StartKeySharing.
64 
65 
66   Returns:      MWErrCode:      Returns process results.
67  *---------------------------------------------------------------------------*/
WM_GetKeySet(WMKeySetBuf * buf,WMKeySet * keySet)68 WMErrCode WM_GetKeySet(WMKeySetBuf *buf, WMKeySet *keySet)
69 {
70     WMErrCode result;
71     u16     sendData[WM_KEYDATA_SIZE / sizeof(u16)];
72     WMDataSet ds;
73     WMArm9Buf *p = WMi_GetSystemWork();
74 
75     {
76         sendData[0] = (u16)PAD_Read();
77         result = WM_StepDataSharing(buf, sendData, &ds);
78         if (result == WM_ERRCODE_SUCCESS)
79         {
80             keySet->seqNum = buf->currentSeqNum;
81 
82             {
83                 u16     iAid;
84                 for (iAid = 0; iAid < 16; iAid++)
85                 {
86                     u16    *keyData;
87                     keyData = WM_GetSharedDataAddress(buf, &ds, iAid);
88                     if (keyData != NULL)
89                     {
90                         keySet->key[iAid] = keyData[0];
91                     }
92                     else
93                     {
94                         // Other party that failed to receive is 0
95                         keySet->key[iAid] = 0;
96                     }
97                 }
98             }
99             return WM_ERRCODE_SUCCESS; // Successful completion
100         }
101         else
102         {
103             return result;
104         }
105     }
106 }
107 
108 /*---------------------------------------------------------------------------*
109     End of file
110  *---------------------------------------------------------------------------*/
111