1 /*---------------------------------------------------------------------------*
2   Project:  TwlSDK - WFS - include
3   File:     server.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-17#$
14   $Rev: 8556 $
15   $Author: okubata_ryoma $
16  *---------------------------------------------------------------------------*/
17 #ifndef	NITRO_WFS_SERVER_H_
18 #define	NITRO_WFS_SERVER_H_
19 
20 
21 #include <nitro/wfs/format.h>
22 #include <nitro/mi/device.h>
23 #include <nitro/fs.h>
24 
25 
26 #ifdef __cplusplus
27 extern  "C"
28 {
29 #endif
30 
31 
32 /*---------------------------------------------------------------------------*/
33 /* Constants */
34 
35 /* The total number of transfer lock handles (sufficient if there are enough for the maximum number of clients that can be connected at the same time) */
36 #define	WFS_LOCK_HANDLE_MAX	    15
37 
38 
39 /*---------------------------------------------------------------------------*/
40 /* Declarations */
41 
42 /* The information structure notified at a WFS_EVENT_SERVER_SEGMENT_REQUEST event */
43 typedef struct WFSSegmentBuffer
44 {
45     u32     offset;
46     u32     length;
47     void   *buffer;
48 }
49 WFSSegmentBuffer;
50 
51 /* The transfer lock range information structure */
52 typedef struct WFSLockInfo
53 {
54     WBTBlockInfoList info;
55     int     ref;        /* Reference count */
56     u32     offset;     /* Starting ROM address */
57     u32     length;     /* ROM read size */
58     u32     ack_seq;    /* The sequence number that should be returned next */
59 }
60 WFSLockInfo;
61 
62 /* The WFS server information structure */
63 typedef struct WFSServerContext
64 {
65     void               *userdata;
66     WFSEventCallback    callback;
67 
68     /* Internal thread information */
69     void               *thread_work;
70     void (*thread_hook)(void *thread, void *argument);
71 
72     /* The list managing the locked regions */
73     int                 use_bitmap;
74     WFSLockInfo         list[WFS_LOCK_HANDLE_MAX];
75 
76     /* The ROM file table */
77     WFSTableFormat      table[1];
78     WBTBlockInfoList    table_info[1];
79     MIAllocator        *allocator;
80 
81     /* The client status management member */
82     int                 all_bitmap;     /* Connecting */
83     int                 busy_bitmap;    /* Accessing */
84     int                 sync_bitmap;    /* Synchronizing */
85     int                 ack_bitmap;     /* Waiting for a response from the server */
86     WFSMessageFormat    recv_msg[16];   /* The latest message */
87 
88     /* Member related to dynamically changing packet sizes */
89     BOOL                is_changing;    /* Flag indicating change is expected */
90     int                 new_packet;     /* Packet size after the change */
91     int                 deny_bitmap;	/* Denied response target during change */
92 
93     /* WBT-related member */
94     u8                  cache_hit_buf[512];
95     WBTContext          wbt[1];
96     WBTCommandList      wbt_list[2];
97     BOOL                msg_busy;       /* Sending response */
98 }
99 WFSServerContext;
100 
101 
102 /*---------------------------------------------------------------------------*/
103 /* Functions */
104 
105 /*---------------------------------------------------------------------------*
106   Name:         WFS_InitServer
107 
108   Description:  Initializes the WFS server context.
109 
110   Arguments:    context:          The WFSServerContext structure.
111                 userdata:         Any user-defined value associated with the context.
112                 callback:         The system event notification callback.
113                                   Specify NULL if not needed.
114                 allocator:        The allocator used internally.
115                 packet:           The parent's initial packet size.
116 
117   Returns:      None.
118  *---------------------------------------------------------------------------*/
119 void WFS_InitServer(WFSServerContext *context,
120                     void *userdata, WFSEventCallback callback,
121                     MIAllocator *allocator, int packet);
122 
123 /*---------------------------------------------------------------------------*
124   Name:         WFS_EndServer
125 
126   Description:  Deallocates the WFS server context.
127 
128   Arguments:    context:          The WFSServerContext structure.
129 
130   Returns:      None.
131  *---------------------------------------------------------------------------*/
132 void WFS_EndServer(WFSServerContext *context);
133 
134 /*---------------------------------------------------------------------------*
135   Name:         WFS_RegisterServerTable
136 
137   Description:  Loads the ROM file table from the device and registers it to the server.
138 
139   Arguments:    context:          The WFSServerContext structure.
140                 device:           The device storing the NTR binary.
141                 fatbase:          The offset within the device wherein the NTR binary is located.
142                 overlay:          The offset in the device to the NTR binary containing the overlay to be merged.
143                                   (This has the same value as "fatbase" if multiple ROM file tables are not merged)
144 
145 
146 
147   Returns:      TRUE when the ROM file table is correctly loaded and registered.
148  *---------------------------------------------------------------------------*/
149 BOOL WFS_RegisterServerTable(WFSServerContext *context,
150                              MIDevice *device, u32 fatbase, u32 overlay);
151 
152 /*---------------------------------------------------------------------------*
153   Name:         WFS_CallServerConnectHook
154 
155   Description:  Server-side connection notification.
156 
157   Arguments:    context:          The WFSServerContext structure.
158                 peer:             Information for the connected client.
159 
160   Returns:      None.
161  *---------------------------------------------------------------------------*/
162 void WFS_CallServerConnectHook(WFSServerContext *context, const WFSPeerInfo *peer);
163 
164 /*---------------------------------------------------------------------------*
165   Name:         WFS_CallServerDisconnectHook
166 
167   Description:  Disconnect notification on the server side.
168 
169   Arguments:    context:          The WFSServerContext structure.
170                 peer:             Information for the disconnected client.
171 
172   Returns:      None.
173  *---------------------------------------------------------------------------*/
174 void WFS_CallServerDisconnectHook(WFSServerContext *context, const WFSPeerInfo *peer);
175 
176 /*---------------------------------------------------------------------------*
177   Name:         WFS_CallServerPacketSendHook
178 
179   Description:  Notification of timing when it is possible to send packets on the server side.
180 
181   Arguments:    context:          The WFSServerContext structure.
182                 packet:           Send packet settings.
183 
184   Returns:      None.
185  *---------------------------------------------------------------------------*/
186 void WFS_CallServerPacketSendHook(WFSServerContext *context, WFSPacketBuffer *packet);
187 
188 /*---------------------------------------------------------------------------*
189   Name:         WFS_CallServerPacketRecvHook
190 
191   Description:  Server-side packet receipt notification.
192 
193   Arguments:    context:          The WFSServerContext structure.
194                 packet:           Sender packet information.
195 
196   Returns:      None.
197  *---------------------------------------------------------------------------*/
198 void WFS_CallServerPacketRecvHook(WFSServerContext *context, const WFSPacketBuffer *packet);
199 
200 /*---------------------------------------------------------------------------*
201   Name:         WFS_GetServerConnectedBitmap
202 
203   Description:  Gets bitmaps for clients currently connected to the server.
204 
205   Arguments:    context:          The WFSServerContext structure.
206 
207   Returns:      The clients currently connected to the server.
208  *---------------------------------------------------------------------------*/
209 PLATFORM_ATTRIBUTE_INLINE
WFS_GetServerConnectedBitmap(const WFSServerContext * context)210 int WFS_GetServerConnectedBitmap(const WFSServerContext *context)
211 {
212     return context->all_bitmap;
213 }
214 
215 /*---------------------------------------------------------------------------*
216   Name:         WFS_GetServerBusyBitmap
217 
218   Description:  Gets bitmaps for clients currently accessing the server.
219 
220   Arguments:    context:          The WFSServerContext structure.
221 
222   Returns:      The clients currently accessing the server.
223  *---------------------------------------------------------------------------*/
224 PLATFORM_ATTRIBUTE_INLINE
WFS_GetServerBusyBitmap(const WFSServerContext * context)225 int WFS_GetServerBusyBitmap(const WFSServerContext *context)
226 {
227     return context->busy_bitmap;
228 }
229 
230 /*---------------------------------------------------------------------------*
231   Name:         WFS_GetServerSyncBitmap
232 
233   Description:  Gets bitmaps for clients currently synchronously accessing the server.
234 
235   Arguments:    context:          The WFSServerContext structure.
236 
237   Returns:      The clients currently synchronously accessing the server.
238  *---------------------------------------------------------------------------*/
239 PLATFORM_ATTRIBUTE_INLINE
WFS_GetServerSyncBitmap(const WFSServerContext * context)240 int WFS_GetServerSyncBitmap(const WFSServerContext *context)
241 {
242     return context->sync_bitmap;
243 }
244 
245 /*---------------------------------------------------------------------------*
246   Name:         WFS_GetServerPacketLength
247 
248   Description:  Obtain the size of packets sent by the server.
249 
250   Arguments:    context:          The WFSServerContext structure.
251 
252   Returns:      The currently specified packet size.
253  *---------------------------------------------------------------------------*/
254 int WFS_GetServerPacketLength(const WFSServerContext *context);
255 
256 /*---------------------------------------------------------------------------*
257   Name:         WFS_SetServerPacketLength
258 
259   Description:  Sets the size of the server send packet.
260 
261   Arguments:    context:          The WFSServerContext structure.
262                 length:           The packet size to be set.
263 
264   Returns:      None.
265  *---------------------------------------------------------------------------*/
266 void WFS_SetServerPacketLength(WFSServerContext *context, int length);
267 
268 /*---------------------------------------------------------------------------*
269   Name:         WFS_SetServerSync
270 
271   Description:  Sets the clients that synchronously access the server.
272                 This function synchronizes responses to clients that are clearly guaranteed to access the same files in precisely the same order, thereby achieving efficient transmission rates that utilize the unique characteristics of the WBT library.
273                 Note, however, that responses will grow out-of-sync and cause deadlocks if synchronization starts at logically insecure times.
274 
275 
276 
277 
278   Arguments:    context:          The WFSServerContext structure.
279                 bitmap:           The AID bitmap for the client to be synchronized.
280                                  Synchronization will not occur (default behavior) when 0 is specified.
281                                  The lowest bit is always ignored.
282 
283   Returns:      None.
284  *---------------------------------------------------------------------------*/
285 void WFS_SetServerSync(WFSServerContext *context, int bitmap);
286 
287 /*---------------------------------------------------------------------------*
288   Name:         WFS_ExecuteRomServerThread
289 
290   Description:  Registers the specified ROM file with the WFS library for distribution, and automatically starts an internal ROM access thread.
291 
292                 These threads are automatically destroyed in the WFS_EndServer function.
293 
294   Arguments:    context:          The WFSServerContext structure.
295                 file:             The SRL file that contains the file system to be distributed.
296                                  Specify NULL for clone boot.
297                 sharedFS:         TRUE if the file system is to be shared by the parent and child.
298                                  In this case, only the overlay held by 'file' is extracted and appended to the file system of the parent device itself to make a combined file system to distribute.
299                                  This argument is ignored if "file" is set to NULL, indicating a clone boot (this will always be interpreted as TRUE).
300 
301 
302 
303 
304 
305   Returns:      TRUE if ROM file registration and thread creation succeed.
306  *---------------------------------------------------------------------------*/
307 BOOL WFS_ExecuteRomServerThread(WFSServerContext *context, FSFile *file, BOOL sharedFS);
308 
309 
310 /*---------------------------------------------------------------------------*/
311 
312 
313 #ifdef __cplusplus
314 } /* extern "C" */
315 #endif
316 
317 
318 #endif /* NITRO_WFS_SERVER_H_ */
319 /*---------------------------------------------------------------------------*
320   $Log: server.h,v $
321   Revision 1.5  2007/08/22 05:22:32  yosizaki
322   Fixed dependencies.
323 
324   Revision 1.4  2007/06/14 13:14:10  yosizaki
325   Added WFS_ExecuteRomServerThread.
326 
327   Revision 1.3  2007/06/11 09:03:37  yosizaki
328   Added WFS_GetServerPacketLength.
329 
330   Revision 1.2  2007/04/17 00:00:52  yosizaki
331   Renamed some structures.
332 
333   Revision 1.1  2007/04/13 04:14:18  yosizaki
334   Initial upload.
335 
336   $NoKeywords: $
337  *---------------------------------------------------------------------------*/
338