1 /*---------------------------------------------------------------------------*
2 Project: TwlSDK - WBT - libraries
3 File: wbt_api.c
4
5 Copyright 2006-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:: 2009-06-04#$
14 $Rev: 10698 $
15 $Author: okubata_ryoma $
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 or 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: 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: 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: 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: 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: Parses 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
330 // Check block_id
331 if ( block_id <= WBT_BLOCK_ID_MIN )
332 {
333 OS_Warning("block_id must be bigger than WBT_BLOCK_ID_MIN.");
334 return FALSE;
335 }
336
337 // Check the total number of registered data blocks
338 if ( WBT_NumOfRegisteredBlock() >= WBT_NUM_MAX_BLOCK_INFO_ID )
339 {
340 OS_Warning("Number of registerd data blocks is full.");
341 return FALSE;
342 }
343 return WBT_RegisterBlockInfo(wbti_command_work, block_info_list, block_id, user_id,
344 data_ptr, data_size);
345 }
346
347
348 /*---------------------------------------------------------------------------*
349 Name: WBT_UnregisterBlock
350
351 Description: Removes from the block delivery registration.
352
353 Arguments: block_id: Block ID to stop the delivery
354
355 Returns: WBTBlockInfoList: Structure for registering block information
356 *---------------------------------------------------------------------------*/
WBT_UnregisterBlock(WBTBlockId block_id)357 WBTBlockInfoList *WBT_UnregisterBlock(WBTBlockId block_id)
358 {
359 SDK_ASSERT(wbt_initialize_flag);
360 return WBT_UnregisterBlockInfo(wbti_command_work, block_id);
361 }
362
363 /*---------------------------------------------------------------------------*
364 Name: WBT_RequstSync
365
366 Description: Synchronizes with the other party (must be called when starting the block transfer).
367
368 Arguments: target: The other party to synchronize (specify with AID bitmap)
369 callback: Callback function that is called after the synchronization
370
371 Returns: BOOL: FALSE: Previous command is running.
372 TRUE: Command issue successful.
373 *---------------------------------------------------------------------------*/
WBT_RequestSync(WBTAidBitmap target,WBTCallback callback)374 BOOL WBT_RequestSync(WBTAidBitmap target, WBTCallback callback)
375 {
376 WBTCommandList *list = WBT_AllocCommandList(wbti_command_work);
377 if (list)
378 {
379 list->command.callback = callback;
380 WBT_CreateCommandSYNC(wbti_command_work, list);
381 WBT_PostCommand(wbti_command_work, list, target, NULL);
382 }
383 return (list != NULL);
384 }
385
386 /*---------------------------------------------------------------------------*
387 Name: WBT_GetBlockInfo
388
389 Description: Gets the block information.
390
391 Arguments: target: The other party to synchronize (specify with AID bitmap)
392 block_info_no: Block information number
393 block_info_table: Pointer table to the buffer that stores the obtained block information
394 callback: Callback function that is called after the synchronization
395
396 Returns: BOOL: FALSE: Previous command is running.
397 TRUE: Command issue successful.
398 *---------------------------------------------------------------------------*/
399 BOOL
WBT_GetBlockInfo(WBTAidBitmap target,int block_info_no,WBTBlockInfoTable * block_info_table,WBTCallback callback)400 WBT_GetBlockInfo(WBTAidBitmap target, int block_info_no, WBTBlockInfoTable *block_info_table,
401 WBTCallback callback)
402 {
403 WBTCommandList *list = WBT_AllocCommandList(wbti_command_work);
404 if (list)
405 {
406 list->command.callback = callback;
407 WBT_CreateCommandINFO(wbti_command_work, list, block_info_no, block_info_table);
408 WBT_PostCommand(wbti_command_work, list, target, NULL);
409 }
410 return (list != NULL);
411 }
412
413 /*---------------------------------------------------------------------------*
414 Name: WBT_GetBlock
415
416 Description: Gets the block.
417
418 Arguments: target: The other party to synchronize (specify with AID bitmap)
419 block_id: Block ID
420 recv_buf_table: Pointer table to the buffer that stores the received block
421 recv_size: Received block size
422 p_bmp_table: Pointer table to the buffer for recording packet receipt number
423 callback: Callback function that is used after the block is obtained
424
425 Returns: BOOL: FALSE: Previous command is running.
426 TRUE: Command issue successful.
427 *---------------------------------------------------------------------------*/
428 BOOL
WBT_GetBlock(WBTAidBitmap target,WBTBlockId block_id,WBTRecvBufTable * recv_buf_table,u32 recv_size,WBTPacketBitmapTable * p_bmp_table,WBTCallback callback)429 WBT_GetBlock(WBTAidBitmap target, WBTBlockId block_id, WBTRecvBufTable *recv_buf_table,
430 u32 recv_size, WBTPacketBitmapTable *p_bmp_table, WBTCallback callback)
431 {
432 WBTCommandList *list = WBT_AllocCommandList(wbti_command_work);
433 if (list)
434 {
435 list->command.callback = callback;
436 WBT_CreateCommandGET(wbti_command_work, list, block_id, recv_size, recv_buf_table, p_bmp_table);
437 WBT_PostCommand(wbti_command_work, list, target, NULL);
438 }
439 return (list != NULL);
440 }
441
442 /*---------------------------------------------------------------------------*
443 Name: WBT_PutUserData
444
445 Description: Sends data that is smaller than 9 bytes to the other party.
446
447 Arguments: target: The other party (specify with AID bitmap)
448 user_data: Pointer to the data you want to send
449 size: Data size
450 callback: Callback function
451
452 Returns: BOOL: FALSE: Previous command is running.
453 TRUE: Command issue successful.
454 *---------------------------------------------------------------------------*/
WBT_PutUserData(WBTAidBitmap target,const void * user_data,int size,WBTCallback callback)455 BOOL WBT_PutUserData(WBTAidBitmap target, const void *user_data, int size, WBTCallback callback)
456 {
457 WBTCommandList *list = WBT_AllocCommandList(wbti_command_work);
458 if (list)
459 {
460 list->command.callback = callback;
461 WBT_CreateCommandMSG(wbti_command_work, list, user_data, (u32)size);
462 WBT_PostCommand(wbti_command_work, list, target, NULL);
463 }
464 return (list != NULL);
465 }
466
467 /*---------------------------------------------------------------------------*
468 Name: WBT_CancelCurrentCommand
469
470 Description: Cancels the currently issuing WBT command.
471
472 Arguments: target: The other party (specify with AID bitmap)
473
474 Returns: BOOL: FALSE: No command to cancel
475 TRUE: Cancellation succeeded.
476 *---------------------------------------------------------------------------*/
WBT_CancelCurrentCommand(WBTAidBitmap cancel_target)477 BOOL WBT_CancelCurrentCommand(WBTAidBitmap cancel_target)
478 {
479 SDK_ASSERT(wbt_initialize_flag);
480 return (WBT_CancelCommand(wbti_command_work, cancel_target) != 0);
481 }
482
483
484 /*---------------------------------------------------------------------------*
485 $Log: wbt_api.c,v $
486 Revision 1.2 2007/04/10 23:50:11 yosizaki
487 Enabled WBT_AidbitmapToAid().
488
489 Revision 1.1 2007/04/10 08:19:45 yosizaki
490 Initial upload.
491
492 $NoKeywords: $
493 *---------------------------------------------------------------------------*/
494