1 /*---------------------------------------------------------------------------*
2   Project:     HostIO
3   File:        hio_interface.h
4   Description: This file defines the public HostIO interface.
5 
6   Copyright (C) 2010 Nintendo.  All rights reserved.
7 
8   These coded instructions, statements, and computer programs contain
9   proprietary information of Nintendo of America Inc. and/or Nintendo
10   Company Ltd., and are protected by Federal copyright law.  They may
11   not be disclosed to third parties or copied or duplicated in any form,
12   in whole or in part, without the prior written consent of Nintendo.
13 
14   *---------------------------------------------------------------------------*/
15 
16 #ifndef __HIO_INTERFACE_H__
17 #define __HIO_INTERFACE_H__
18 
19 #include <types.h>
20 #include <cafe/os.h>
21 #include <cafe/mem.h>
22 
23 #ifdef __cplusplus
24 extern "C" {
25 #endif // __cplusplus
26 
27 /* HostIO Channel Option Values */
28 #define HIO_CHANNEL_OPTION_READ       1
29 #define HIO_CHANNEL_OPTION_WRITE      2
30 #define HIO_CHANNEL_OPTION_READ_WRITE (HIO_CHANNEL_OPTION_READ | \
31                                        HIO_CHANNEL_OPTION_WRITE)
32 
33 /* HostIO Handle Values */
34 #define HIO_HANDLE_INVALID -1
35 
36 typedef s32 HIOStatus;
37 typedef s32 HIOHandle;
38 typedef u32 HIOChannelOptions;
39 typedef u32 HIOChannelFlags;
40 typedef void (*HIOAsyncParam)(HIOStatus status, void* context);
41 typedef void (*HIOConnectionCallback)(HIOStatus status, void* context);
42 typedef void (*HIOCallback)(HIOStatus status, void* context);
43 
44 typedef struct
45 {
46     OSMessageQueue* queue;
47     void*           userData;
48     HIOCallback     callback;
49 } HIOAsyncStruct;
50 
51 typedef struct
52 {
53     void*          userData;
54     HIOCallback    callback;
55     HIOStatus      status;
56     OSFunctionType type;
57 } HIOMessage;
58 
59 HIOStatus HIOInit(void);
60 HIOStatus HIOShutdown(void);
61 HIOHandle HIOOpenEx(const char * hioName,
62                   HIOAsyncStruct* asyncStruct,
63                   HIOChannelOptions options,
64                   HIOChannelFlags flags);
65 HIOStatus HIOClose(HIOHandle handle);
66 HIOStatus HIORead(HIOHandle handle, u32 sizeInBytes, void *pData);
67 HIOStatus HIOReadAsyncEx(HIOHandle handle, u32 sizeInBytes, void *pData,
68                        HIOAsyncStruct* asyncStruct);
69 HIOStatus HIOWrite(HIOHandle handle, u32 sizeInBytes, const void *pData);
70 HIOStatus HIOWriteAsyncEx(HIOHandle handle, u32 sizeInBytes, const void *pData,
71                         HIOAsyncStruct* asyncStruct);
72 HIOStatus HIOQueryHeadlessMode(void);
73 
74 #define HIO_STATUS_NEW_CONNECTION                1
75 #define HIO_STATUS_OK                            0
76 #define HIO_STATUS_INVALID_CHANNEL_NAME          (-1)
77 #define HIO_STATUS_NO_CONNECTIONS                (-2)
78 #define HIO_STATUS_NO_CHANNELS                   (-3)
79 #define HIO_STATUS_INVALID_HANDLE                (-4)
80 #define HIO_STATUS_NO_CLIENT_TXN_BUF_AVAILABLE   (-5)
81 #define HIO_STATUS_OUT_OF_MEMORY                 (-6)
82 #define HIO_STATUS_INVALID_PARAM                 (-7)
83 #define HIO_STATUS_NOT_INITIALIZED               (-8)
84 #define HIO_STATUS_INVALID_OPERATION             (-9)
85 #define HIO_STATUS_HEADLESS_MODE                 (-10)
86 #define HIO_STATUS_ALREADY_OPEN                  (-100)
87 #define HIO_STATUS_ALREADY_CLOSED                (-101)
88 
89 // DefaultAppIOQueue is provided for compatibility with the original
90 // HIOOpen, HIOReadAsync, and HIOWriteAsync methods. Applications should
91 // prefer using the "Ex" versions of these APIs and provide their own
92 // OSMessageQueue.
93 
HIOOpen(const char * hioName,HIOConnectionCallback connection,void * context,HIOChannelOptions options,HIOChannelFlags flags)94 inline HIOHandle HIOOpen(const char * hioName,
95                   HIOConnectionCallback connection,
96                   void * context,
97                   HIOChannelOptions options,
98                   HIOChannelFlags flags)
99 {
100     HIOAsyncStruct asyncStruct; // asyncStruct will be copied in HIOOpenEx.
101     asyncStruct.queue = OSGetDefaultAppIOQueue();
102     asyncStruct.userData = context;
103     asyncStruct.callback = (HIOCallback) connection;
104     return HIOOpenEx(hioName, &asyncStruct, options, flags);
105 }
106 
HIOReadAsync(HIOHandle handle,u32 sizeInBytes,void * pData,HIOAsyncParam asyncParam,void * context)107 inline HIOStatus HIOReadAsync(HIOHandle handle, u32 sizeInBytes, void *pData,
108                        HIOAsyncParam asyncParam, void* context)
109 {
110     HIOAsyncStruct asyncStruct; // asyncStruct will be copied in HIOReadAsyncEx.
111     asyncStruct.queue = OSGetDefaultAppIOQueue();
112     asyncStruct.userData = context;
113     asyncStruct.callback = (HIOCallback) asyncParam;
114 
115     return HIOReadAsyncEx(handle, sizeInBytes, pData, &asyncStruct);
116 }
117 
HIOWriteAsync(HIOHandle handle,u32 sizeInBytes,const void * pData,HIOAsyncParam asyncParam,void * context)118 inline HIOStatus HIOWriteAsync(HIOHandle handle, u32 sizeInBytes, const void *pData,
119                         HIOAsyncParam asyncParam, void* context)
120 {
121     HIOAsyncStruct asyncStruct; // asyncStruct will be copied in HIOWriteAsyncEx.
122     asyncStruct.queue = OSGetDefaultAppIOQueue();
123     asyncStruct.userData = context;
124     asyncStruct.callback = (HIOCallback) asyncParam;
125 
126     return HIOWriteAsyncEx(handle, sizeInBytes, pData, &asyncStruct);
127 }
128 
129 #ifdef __cplusplus
130 }
131 #endif // __cplusplus
132 
133 #endif // __HIO_INTERFACE_H__
134