1  /*---------------------------------------------------------------------------*
2   Project:  TwlSDK - MB - include
3   File:     mb_common.h
4 
5   Copyright 2007-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 #ifndef __MB_COMMON_H__
19 #define __MB_COMMON_H__
20 
21 /* Macro definition -------------------------------------------------------- */
22 
23 // For debugging
24 #ifdef	PRINT_DEBUG
25 #define MB_COMM_TYPE_OUTPUT             MBi_comm_type_output
26 #define MB_COMM_WMEVENT_OUTPUT          MBi_comm_wmevent_output
27 #else
28 #define MB_COMM_TYPE_OUTPUT( ... )      ((void)0)
29 #define MB_COMM_WMEVENT_OUTPUT( ... )   ((void)0)
30 #endif
31 
32 /* Send/receive size definitions */
33 #define MB_COMM_P_SENDLEN_DEFAULT       (256)
34 #define MB_COMM_P_RECVLEN_DEFAULT       (8)
35 
36 #define MB_COMM_P_SENDLEN_MAX           MB_COMM_PARENT_SEND_MAX
37 #define MB_COMM_P_RECVLEN_MAX           MB_COMM_PARENT_RECV_MAX
38 
39 #define MB_COMM_P_SENDLEN_MIN           (sizeof(MBDownloadFileInfo))    // Minimum size
40 #define MB_COMM_P_RECVLEN_MIN           MB_COMM_PARENT_RECV_MIN
41 
42 #define MB_COMM_REQ_DATA_SIZE           (29)
43 
44 /* Calculation macros relating to fragmented request data. */
45 #define MB_COMM_CALC_REQ_DATA_PIECE_SIZE( __P_RECVLEN__ )       (( __P_RECVLEN__ ) - 2)
46 #define MB_COMM_CALC_REQ_DATA_PIECE_NUM( __P_RECVLEN__ )        \
47                                 ( (MB_COMM_REQ_DATA_SIZE + 1) / (MB_COMM_CALC_REQ_DATA_PIECE_SIZE( __P_RECVLEN__ )) )
48 #define MB_COMM_CALC_REQ_DATA_BUF_SIZE( __P_RECVLEN__ )     (MB_COMM_REQ_DATA_SIZE + 1)
49 /*
50  * There are logical errors above, but they cannot be corrected because the IPL runs on these specifications.
51  *    Properly speaking,
52  * #define MB_COMM_CALC_REQ_DATA_PICE_NUM( __P_RECV_LEN__)                                                            \
53  *                                  ( (MB_COMM_REQ_DATA_SIZE + MB_COMM_CALC_REQ_DATA_PIECE_SIZE( __P_RECVLEN__ ) - 1) \
54  *                                    / MB_COMM_CALC_REQ_DATA_PIECE_SIZE( __P_RECVLEN__ ) )
55  * #define MB_COMM_CALC_REQ_DATA_BUF_SIZE( __P_RECV_LEN__ )                                 \
56  *                                  ( MB_COMM_CALC_REQ_DATA_PIECE_SIZE( __P_RECVLEN__ )     \
57  *                                    * MB_COMM_CALC_REQ_DATA_PIECE_NUM( __P_RECVLEN__ ) )
58  */
59 
60 
61 /* Block header size */
62 #define MB_COMM_PARENT_HEADER_SIZE      (6)     // MB_CommParentBlockHeader size (without padding)
63 #define MB_COMM_CHILD_HEADER_SIZE       (8)     // MB_CommChildBlockHeader size (without padding)
64 
65 #define MB_COMM_CALC_BLOCK_SIZE( __P_SENDLEN__ )                (( __P_SENDLEN__ ) - MB_COMM_PARENT_HEADER_SIZE)
66 
67 /* Definition of an error that is returned by data transmission functions.
68    Defined for values that do not overlap with WM ERRCODE. */
69 #define MB_SENDFUNC_STATE_ERR           (WM_ERRCODE_MAX + 1)
70 
71 /* Block transfer data types */
72 typedef enum MBCommType
73 {
74     MB_COMM_TYPE_DUMMY = 0,            //  0
75 
76     MB_COMM_TYPE_PARENT_SENDSTART,     //  1
77     MB_COMM_TYPE_PARENT_KICKREQ,       //  2
78     MB_COMM_TYPE_PARENT_DL_FILEINFO,   //  3
79     MB_COMM_TYPE_PARENT_DATA,          //  4
80     MB_COMM_TYPE_PARENT_BOOTREQ,       //  5
81     MB_COMM_TYPE_PARENT_MEMBER_FULL,   //  6
82 
83     MB_COMM_TYPE_CHILD_FILEREQ,        //  7
84     MB_COMM_TYPE_CHILD_ACCEPT_FILEINFO, //  8
85     MB_COMM_TYPE_CHILD_CONTINUE,       //  9
86     MB_COMM_TYPE_CHILD_STOPREQ,        //  10
87     MB_COMM_TYPE_CHILD_BOOTREQ_ACCEPTED //  11
88 }
89 MBCommType;
90 
91 /* Request type from the user */
92 typedef enum MBCommUserReq
93 {
94     MB_COMM_USER_REQ_NONE = 0,         //  0
95     MB_COMM_USER_REQ_DL_START,         //  1
96     MB_COMM_USER_REQ_SEND_START,       //  2
97     MB_COMM_USER_REQ_ACCEPT,           //  3
98     MB_COMM_USER_REQ_KICK,             //  4
99     MB_COMM_USER_REQ_BOOT              //  5
100 }
101 MBCommUserReq;
102 
103 /* Structure definition ---------------------------------------------------- */
104 
105 /*
106  * Error notification callback
107  */
108 typedef struct
109 {
110     u16     apiid;                     // API code
111     u16     errcode;                   // Error Codes
112 }
113 MBErrorCallback;
114 
115 
116 /* Functions --------------------------------------------------------------- */
117 
118 /*
119  * Initializes the block header, and configures only the type.
120  * After this, small fields will be packed in until they are sent with the MBi_BlockHeaderEnd function.
121  *
122  * If there are no arguments, it may be left unchanged.
123  */
124 void    MBi_BlockHeaderBegin(u8 type, u32 *sendbuf);
125 
126 /*
127  * Finishes configuring the block header and calculates the checksum.
128  * After that, it will actually be sent to the other party specified by pollbmp.
129  */
130 int     MBi_BlockHeaderEnd(int body_len, u16 pollbmp, u32 *sendbuf);
131 
132 
133 /*
134  * Checksum calculation
135  */
136 u16     MBi_calc_cksum(const u16 *buf, int length);
137 
138 // --- For debugging
139 void    MBi_DebugPrint(const char *file, int line, const char *func, const char *fmt, ...);
140 void    MBi_comm_type_output(u16 type);
141 void    MBi_comm_wmevent_output(u16 type, void *arg);
142 
143 #endif /* __MB_COMMON_H__ */
144