1 /*---------------------------------------------------------------------------*
2   Project:  TwlSDK - WBT - libraries
3   File:     wbt_api.c
4 
5   Copyright 2006-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-11-05#$
14   $Rev: 9211 $
15   $Author: yosizaki $
16  *---------------------------------------------------------------------------*/
17 
18 #include	<nitro.h>
19 #include	<nitro/wbt.h>
20 #include	<nitro/wbt/context.h>
21 
22 /*---------------------------------------------------------------------------*
23 	Variable Definitions
24  *---------------------------------------------------------------------------*/
25 
26 static BOOL wbt_initialize_flag = FALSE;
27 
28 static WBTContext wbti_command_work[1];
29 
30 /* 2 level command queue */
31 static WBTCommandList cmd_q[2];
32 
33 
34 /*---------------------------------------------------------------------------*
35 	Function Definitions
36  *---------------------------------------------------------------------------*/
37 
38 /*---------------------------------------------------------------------------*
39   Name:         WBT_PrintBTList
40 
41   Description:  Displays the block information list with OS_Printf
42 
43   Arguments:    None.
44 
45   Returns:      None.
46  *---------------------------------------------------------------------------*/
WBT_PrintBTList(void)47 void WBT_PrintBTList(void)
48 {
49     WBTBlockInfoList *list = wbti_command_work->list;
50     for (; list != NULL; list = list->next)
51     {
52         OS_Printf("BTList id = %d\n", list->data_info.id);
53         OS_Printf("  data size %d\n", list->data_info.block_size);
54         OS_Printf("  uid %s\n", list->data_info.user_id);
55         OS_Printf("  info ptr = %p\n", &(list->data_info));
56     }
57 }
58 
59 /*---------------------------------------------------------------------------*
60   Name:         WBT_AidbitmapToAid
61 
62   Description:  Converts the AID bitmap to AID (looking only at the least significant bit)
63 
64   Arguments:    abmp - AID bitmap
65 
66   Returns:      int  - AID
67  *---------------------------------------------------------------------------*/
WBT_AidbitmapToAid(WBTAidBitmap abmp)68 int WBT_AidbitmapToAid(WBTAidBitmap abmp)
69 {
70     return abmp ? (int)MATH_CTZ(abmp) : -1;
71 }
72 
73 /*---------------------------------------------------------------------------*
74   Name:         WBT_InitParent
75 
76   Description:  Block transfer initialization function for the parent
77 
78   Arguments:    send_packet_size - Send packet size
79                 recv_packet_size - Receive packet size
80                 callback - Callback function that is used when the requests from the other party is generated
81 
82   Returns:      None.
83  *---------------------------------------------------------------------------*/
WBT_InitParent(int send_packet_size,int recv_packet_size,WBTCallback callback)84 void WBT_InitParent(int send_packet_size, int recv_packet_size, WBTCallback callback)
85 {
86     PLATFORM_ENTER_CRITICALSECTION();
87     if (!wbt_initialize_flag)
88     {
89         wbt_initialize_flag = TRUE;
90         WBT_InitContext(wbti_command_work, NULL, NULL);
91         wbti_command_work->system_cmd.callback = callback;
92         /* Initialize the command pool */
93         MI_CpuFill8(cmd_q, 0x00, sizeof(cmd_q));
94         WBT_AddCommandPool(wbti_command_work, cmd_q, sizeof(cmd_q) / sizeof(*cmd_q));
95         WBT_StartParent(wbti_command_work, send_packet_size, recv_packet_size);
96     }
97     PLATFORM_LEAVE_CRITICALSECTION();
98 }
99 
100 /*---------------------------------------------------------------------------*
101   Name:         WBT_InitChild
102 
103   Description:  Block transfer initialization function for a child
104 
105   Arguments:    callback - Callback function that is used when the requests from the other party is generated
106 
107   Returns:      None.
108  *---------------------------------------------------------------------------*/
WBT_InitChild(WBTCallback callback)109 void WBT_InitChild(WBTCallback callback)
110 {
111     PLATFORM_ENTER_CRITICALSECTION();
112     if (!wbt_initialize_flag)
113     {
114         wbt_initialize_flag = TRUE;
115         WBT_InitContext(wbti_command_work, NULL, NULL);
116         wbti_command_work->system_cmd.callback = callback;
117         /* Initialize the command pool */
118         MI_CpuFill8(cmd_q, 0x00, sizeof(cmd_q));
119         WBT_AddCommandPool(wbti_command_work, cmd_q, sizeof(cmd_q) / sizeof(*cmd_q));
120     }
121     PLATFORM_LEAVE_CRITICALSECTION();
122 }
123 
124 /*---------------------------------------------------------------------------*
125   Name:         WBT_End
126 
127   Description:  Parent and child common block transfer end function
128 
129   Arguments:    None.
130 
131   Returns:      None.
132  *---------------------------------------------------------------------------*/
WBT_End(void)133 void WBT_End(void)
134 {
135     PLATFORM_ENTER_CRITICALSECTION();
136     if (wbt_initialize_flag)
137     {
138         wbt_initialize_flag = FALSE;
139         wbti_command_work->system_cmd.callback = NULL;
140         WBT_ResetContext(wbti_command_work, NULL);
141     }
142     PLATFORM_LEAVE_CRITICALSECTION();
143 }
144 
145 /*---------------------------------------------------------------------------*
146   Name:         WBT_SetOwnAid
147 
148   Description:  Sets AID of self
149 
150   Arguments:    aid - AID of self
151 
152   Returns:      None.
153  *---------------------------------------------------------------------------*/
WBT_SetOwnAid(int aid)154 void WBT_SetOwnAid(int aid)
155 {
156     WBTContext *const work = wbti_command_work;
157     if (WBT_GetAid(work) == -1)
158     {
159         WBT_StartChild(work, aid);
160     }
161 }
162 
163 /*---------------------------------------------------------------------------*
164   Name:         WBT_GetOwnAid
165 
166   Description:  Gets AID of self
167 
168   Arguments:    None.
169 
170   Returns:      int - AID of self
171  *---------------------------------------------------------------------------*/
WBT_GetOwnAid(void)172 int WBT_GetOwnAid(void)
173 {
174     const WBTContext *const work = wbti_command_work;
175     return WBT_GetAid(work);
176 }
177 
178 /*---------------------------------------------------------------------------*
179   Name:         WBT_CalcPacketbitmapSize
180 
181   Description:  Calculates the size of the buffer used for recording packet reception number.
182                 (For a child, it must be called after synchronizing with the parent.)
183 
184   Arguments:    block_size - Receiving block size
185 
186   Returns:      int - Buffer size for recording packet reception number (bytes)
187  *---------------------------------------------------------------------------*/
WBT_CalcPacketbitmapSize(int block_size)188 int WBT_CalcPacketbitmapSize(int block_size)
189 {
190     return WBT_GetBitmapLength(wbti_command_work, block_size);
191 }
192 
193 /*---------------------------------------------------------------------------*
194   Name:         WBT_GetCurrentDownloadProgress
195 
196   Description:  Looks at the progress of the current block reception
197 
198   Arguments:    block_id: Block ID of block that is being received
199                 aid: AID of the send target(s)
200                 *current_count: Packet count when reception was completed
201                 *total_count: Total packet count
202 
203   Returns:      BOOL: Success/failure.
204  *---------------------------------------------------------------------------*/
WBT_GetCurrentDownloadProgress(u32 block_id,int aid,int * current_count,int * total_count)205 BOOL WBT_GetCurrentDownloadProgress(u32 block_id, int aid, int *current_count, int *total_count)
206 {
207     WBT_GetDownloadProgress(wbti_command_work, block_id, aid, current_count, total_count);
208     return (*total_count != 0);
209 }
210 
211 /*---------------------------------------------------------------------------*
212   Name:         WBT_SetPacketSize
213 
214   Description:  Function that changes the sending/receiving packet size of the parent (use it to change after calling WBT_InitParent)
215 
216   Arguments:    send_packet_size - Send packet size
217                 recv_packet_size - Receive packet size
218 
219   Returns:      BOOL   - FALSE Size change failed
220                          TRUE  Size change successful
221  *---------------------------------------------------------------------------*/
WBT_SetPacketSize(int send_packet_size,int recv_packet_size)222 BOOL WBT_SetPacketSize(int send_packet_size, int recv_packet_size)
223 {
224     return WBT_SetPacketLength(wbti_command_work, send_packet_size, recv_packet_size);
225 }
226 
227 /*---------------------------------------------------------------------------*
228   Name:         WBT_NumOfRegisteredBlock
229 
230   Description:  Returns the number of blocks that have been registered
231 
232   Arguments:    None.
233 
234   Returns:      WBTBlockNumEntry - Number of blocks
235  *---------------------------------------------------------------------------*/
WBT_NumOfRegisteredBlock(void)236 int WBT_NumOfRegisteredBlock(void)
237 {
238     return WBT_GetRegisteredCount(wbti_command_work);
239 }
240 
241 /*---------------------------------------------------------------------------*
242   Name:         WBT_MpParentSendHook
243 
244   Description:  Function that creates send data for the parent
245 
246   Arguments:    sendbuf   - Send buffer
247                 send_size - Send buffer size
248 
249   Returns:      int - Block transfer send size
250  *---------------------------------------------------------------------------*/
WBT_MpParentSendHook(void * sendbuf,int send_size)251 int WBT_MpParentSendHook(void *sendbuf, int send_size)
252 {
253     SDK_ASSERT(wbt_initialize_flag);
254     return WBT_CallPacketSendHook(wbti_command_work, sendbuf, send_size, TRUE);
255 }
256 
257 /*---------------------------------------------------------------------------*
258   Name:         WBT_MpChildSendHook
259 
260   Description:  Function that creates send data for a child
261 
262   Arguments:    sendbuf   - Send buffer
263                 send_size - Send buffer size
264 
265   Returns:      int - Block transfer send size
266  *---------------------------------------------------------------------------*/
WBT_MpChildSendHook(void * sendbuf,int send_size)267 int WBT_MpChildSendHook(void *sendbuf, int send_size)
268 {
269     SDK_ASSERT(wbt_initialize_flag);
270     return WBT_CallPacketSendHook(wbti_command_work, sendbuf, send_size, FALSE);
271 }
272 
273 /*---------------------------------------------------------------------------*
274   Name:         WBT_MpParentRecvHook
275 
276   Description:  Function that analyzes the receive data of the parent
277 
278   Arguments:    recv_buf  - Receive buffer
279                 recv_size - Receive buffer size
280                 aid       - AID of the recipient
281 
282   Returns:      None.
283  *---------------------------------------------------------------------------*/
WBT_MpParentRecvHook(const void * buf,int size,int aid)284 void WBT_MpParentRecvHook(const void *buf, int size, int aid)
285 {
286     SDK_ASSERT(wbt_initialize_flag);
287     WBT_CallPacketRecvHook(wbti_command_work, aid, buf, size);
288 }
289 
290 /*---------------------------------------------------------------------------*
291   Name:         WBT_MpChildRecvHook
292 
293   Description:  Function that analyzes the receive data for the child
294 
295   Arguments:    recv_buf  - Receive buffer
296                 recv_size - Receive buffer size
297 
298   Returns:      None.
299  *---------------------------------------------------------------------------*/
WBT_MpChildRecvHook(const void * buf,int size)300 void WBT_MpChildRecvHook(const void *buf, int size)
301 {
302     SDK_ASSERT(wbt_initialize_flag);
303     WBT_CallPacketRecvHook(wbti_command_work, 0, buf, size);
304 }
305 
306 /*---------------------------------------------------------------------------*
307   Name:         WBT_RegisterBlock
308 
309   Description:  Registers the sendable (deliverable) block
310 
311   Arguments:    block_info_list - Structure for registering block information
312                 block_id       - Block ID
313                 user_id         - User-defined information
314                 data_ptr        - Pointer to the data (when it is NULL, the callback is used every time there is a request from the other party. Users can set the data pointer in the callback functions.)
315 
316 
317                 data_size       - Data size
318                 permission_bmp  - Delivery permission bitmap ("plans" to permit with 0)
319 
320   Returns:      BOOL   - TRUE  Registration successful
321                          FALSE block_id is already registered
322  *---------------------------------------------------------------------------*/
323 BOOL
WBT_RegisterBlock(WBTBlockInfoList * block_info_list,WBTBlockId block_id,const void * user_id,const void * data_ptr,int data_size,WBTAidBitmap permission_bmp)324 WBT_RegisterBlock(WBTBlockInfoList *block_info_list, WBTBlockId block_id, const void *user_id,
325                   const void *data_ptr, int data_size, WBTAidBitmap permission_bmp)
326 {
327     (void)permission_bmp;
328     SDK_ASSERT(wbt_initialize_flag);
329     WBT_RegisterBlockInfo(wbti_command_work, block_info_list, block_id, user_id,
330                           data_ptr, data_size);
331     return TRUE;
332 }
333 
334 
335 /*---------------------------------------------------------------------------*
336   Name:         WBT_UnregisterBlock
337 
338   Description:  Removes from the block delivery registration
339 
340   Arguments:    block_id - Block ID to stop the delivery
341 
342   Returns:      WBTBlockInfoList -Structure for registering block information
343  *---------------------------------------------------------------------------*/
WBT_UnregisterBlock(WBTBlockId block_id)344 WBTBlockInfoList *WBT_UnregisterBlock(WBTBlockId block_id)
345 {
346     SDK_ASSERT(wbt_initialize_flag);
347     return WBT_UnregisterBlockInfo(wbti_command_work, block_id);
348 }
349 
350 /*---------------------------------------------------------------------------*
351   Name:         WBT_RequstSync
352 
353   Description:  Synchronizes with the other party (must be called when starting the block transfer)
354 
355   Arguments:    target   - The other party to synchronize (specify with AID bitmap)
356                 callback - Callback function that is called after the synchronization
357 
358   Returns:      BOOL   - FALSE: Previous command is running.
359                          TRUE Command issue successful
360  *---------------------------------------------------------------------------*/
WBT_RequestSync(WBTAidBitmap target,WBTCallback callback)361 BOOL WBT_RequestSync(WBTAidBitmap target, WBTCallback callback)
362 {
363     WBTCommandList *list = WBT_AllocCommandList(wbti_command_work);
364     if (list)
365     {
366         list->command.callback = callback;
367         WBT_CreateCommandSYNC(wbti_command_work, list);
368         WBT_PostCommand(wbti_command_work, list, target, NULL);
369     }
370     return (list != NULL);
371 }
372 
373 /*---------------------------------------------------------------------------*
374   Name:         WBT_GetBlockInfo
375 
376   Description:  Gets the block information
377 
378   Arguments:    target           - The other party to synchronize (specify with AID bitmap)
379                 block_info_no    - Block information number
380                 block_info_table - Pointer table to the buffer that stores the gotten block information
381                 callback         - Callback function that is called after the synchronization
382 
383   Returns:      BOOL   - FALSE: Previous command is running.
384                          TRUE Command issue successful
385  *---------------------------------------------------------------------------*/
386 BOOL
WBT_GetBlockInfo(WBTAidBitmap target,int block_info_no,WBTBlockInfoTable * block_info_table,WBTCallback callback)387 WBT_GetBlockInfo(WBTAidBitmap target, int block_info_no, WBTBlockInfoTable *block_info_table,
388                  WBTCallback callback)
389 {
390     WBTCommandList *list = WBT_AllocCommandList(wbti_command_work);
391     if (list)
392     {
393         list->command.callback = callback;
394         WBT_CreateCommandINFO(wbti_command_work, list, block_info_no, block_info_table);
395         WBT_PostCommand(wbti_command_work, list, target, NULL);
396     }
397     return (list != NULL);
398 }
399 
400 /*---------------------------------------------------------------------------*
401   Name:         WBT_GetBlock
402 
403   Description:  Gets the block
404 
405   Arguments:    target         - The other party to synchronize (specify with AID bitmap)
406                 block_id       - Block ID
407                 recv_buf_table - Pointer table to the buffer that stores the received block
408                 recv_size      - Received block size
409                 p_bmp_table    - Pointer table to the buffer for recording packet receipt number
410                 callback       - Callback function that is used after the block is obtained
411 
412   Returns:      BOOL   - FALSE: Previous command is running.
413                          TRUE Command issue successful
414  *---------------------------------------------------------------------------*/
415 BOOL
WBT_GetBlock(WBTAidBitmap target,WBTBlockId block_id,WBTRecvBufTable * recv_buf_table,u32 recv_size,WBTPacketBitmapTable * p_bmp_table,WBTCallback callback)416 WBT_GetBlock(WBTAidBitmap target, WBTBlockId block_id, WBTRecvBufTable *recv_buf_table,
417              u32 recv_size, WBTPacketBitmapTable *p_bmp_table, WBTCallback callback)
418 {
419     WBTCommandList *list = WBT_AllocCommandList(wbti_command_work);
420     if (list)
421     {
422         list->command.callback = callback;
423         WBT_CreateCommandGET(wbti_command_work, list, block_id, recv_size, recv_buf_table, p_bmp_table);
424         WBT_PostCommand(wbti_command_work, list, target, NULL);
425     }
426     return (list != NULL);
427 }
428 
429 /*---------------------------------------------------------------------------*
430   Name:         WBT_PutUserData
431 
432   Description:  Sends the data that is smaller than 9 bytes to the other party
433 
434   Arguments:    target    - The other party (specify with AID bitmap)
435                 user_data - Pointer to the data you want to send
436                 size:  The data size
437                 callback - callback function
438 
439   Returns:      BOOL   - FALSE: Previous command is running.
440                          TRUE Command issue successful
441  *---------------------------------------------------------------------------*/
WBT_PutUserData(WBTAidBitmap target,const void * user_data,int size,WBTCallback callback)442 BOOL WBT_PutUserData(WBTAidBitmap target, const void *user_data, int size, WBTCallback callback)
443 {
444     WBTCommandList *list = WBT_AllocCommandList(wbti_command_work);
445     if (list)
446     {
447         list->command.callback = callback;
448         WBT_CreateCommandMSG(wbti_command_work, list, user_data, (u32)size);
449         WBT_PostCommand(wbti_command_work, list, target, NULL);
450     }
451     return (list != NULL);
452 }
453 
454 /*---------------------------------------------------------------------------*
455   Name:         WBT_CancelCurrentCommand
456 
457   Description:  Cancels the currently issuing WBT command.
458 
459   Arguments:    target    - The other party (specify with AID bitmap)
460 
461   Returns:      BOOL: FALSE - No command to cancel
462                          TRUE - Cancellation succeeded
463  *---------------------------------------------------------------------------*/
WBT_CancelCurrentCommand(WBTAidBitmap cancel_target)464 BOOL WBT_CancelCurrentCommand(WBTAidBitmap cancel_target)
465 {
466     SDK_ASSERT(wbt_initialize_flag);
467     return (WBT_CancelCommand(wbti_command_work, cancel_target) != 0);
468 }
469 
470 
471 /*---------------------------------------------------------------------------*
472   $Log: wbt_api.c,v $
473   Revision 1.2  2007/04/10 23:50:11 PM  yosizaki
474   Enabled WBT_AidbitmapToAid().
475 
476   Revision 1.1  2007/04/10 08:19:45  yosizaki
477   Initial upload.
478 
479   $NoKeywords: $
480  *---------------------------------------------------------------------------*/
481