1 /*---------------------------------------------------------------------------*
2   Project:  NintendoWare
3   File:     demo_HioPacketChannel.h
4 
5   Copyright (C)2009-2010 Nintendo Co., Ltd./HAL Laboratory, Inc.  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   $Revision: 14250 $
14  *---------------------------------------------------------------------------*/
15 
16 #ifndef NW_DEMO_HIOPACKETCHANNEL_H_
17 #define NW_DEMO_HIOPACKETCHANNEL_H_
18 
19 #include <nw/assert.h>
20 #include <nw/os/os_Memory.h>
21 #include <nw/dev/dev_Utility.h>
22 
23 #if defined(NN_SWITCH_ENABLE_HOST_IO)
24 
25 #include <nn/hio/CTR/hio_Api.h>
26 #include <nn/hio/CTR/hio_SerialChannel.h>
27 
28 namespace nw {
29 namespace demo {
30 
31 //---------------------------------------------------------------------------
32 //! @brief          HOST IO を経由して PC とパケットの送受信をするためのクラスです。
33 //---------------------------------------------------------------------------
34 class HioPacketChannel
35 {
36 private:
37     static const s32 PACKET_HEADER_ID;
38     static const size_t BUFFER_SIZE;
39 
40 public:
41     //! @brief 送受信クラスの設定内容です。
42     struct Description
43     {
44         s32 channelNumber;      //!< シリアル通信のチャンネル番号です。
45 
46         //! @brief コンストラクタです。
DescriptionDescription47         Description()
48         : channelNumber(1)
49         {}
50     };
51 
52     //! @brief 送受信クラスを構築するためのクラスです。
53     class Builder
54     {
55     public:
56         //! @brief 構築する送受信クラスを設定します。
HioPacketChannelDescription(const Description & description)57         Builder& HioPacketChannelDescription(const Description& description)
58         {
59             m_Description = description;
60             return *this;
61         }
62 
63         //! @brief シリアル通信のチャンネル番号を設定します。
ChannelNumber(s32 channelNumber)64         Builder& ChannelNumber(s32 channelNumber)
65         {
66             NW_MINMAX_ASSERT(channelNumber, 0, nn::hio::CTR::MAX_CHANNEL_NUM);
67 
68             m_Description.channelNumber = channelNumber;
69             return *this;
70         }
71 
72         //---------------------------------------------------------------------------
73         //! @brief          送受信クラスを生成し、シリアル通信のチャンネルを開きます。
74         //!
75         //!                 事前に HIO ライブラリを初期化しておく必要があります。
76         //!
77         //! @param[in]      allocator 送受信クラスで使用するアロケータです。
78         //! @param[in]      deviceAllocator 送受信クラスで使用するデバイスメモリアロケータです。
79         //---------------------------------------------------------------------------
80         HioPacketChannel* Create(
81             os::IAllocator* allocator,
82             os::IAllocator* deviceAllocator
83         );
84 
85     private:
86         Description m_Description;
87     };
88 
89     //----------------------------------------
90     //! @name 生成/破棄
91     //@{
92 
93     //! @brief 送受信クラスを削除します。
94     void Destroy();
95 
96     //}
97 
98     //----------------------------------------
99     //! @name Host IO 接続の初期化
100     //@{
101 
102     //! @brief Host IO 接続を開始します。
103     //!
104     //! @param[in]      deviceAllocator デバイスメモリアロケータです。
105     static bool Initialize(os::IAllocator* deviceAllocator);
106 
107     //! @brief Host IO 接続を終了します。
108     static void Finalize();
109 
110     //}
111 
112     //----------------------------------------
113     //! @name 接続の確立/切断
114     //@{
115 
116     //---------------------------------------------------------------------------
117     //! @brief      PC からの接続を待ちます。
118     //!
119     //!             PC 側で Connect により接続が行われるのをタイムアウト時間が経過するまで待ちます。
120     //!             タイムアウト時間が経過しても接続が無い場合、この関数は false を返します。
121     //!             timeOut に負の値を設定すると、接続が行われるまで待ち続けます。
122     //!
123     //!             現在 0 と負の値以外のタイムアウト時間はサポートされていません。
124     //!
125     //! @param[in]  timeOut ミリ秒単位のタイムアウト時間です。
126     //!
127     //! @return     接続が確立されたら true を返します。
128     //---------------------------------------------------------------------------
129     bool Listen(s32 timeOut);
130 
131     //---------------------------------------------------------------------------
132     //! @brief      PC へ接続します。
133     //!
134     //!             PC 側で Listen により接続待ちが行われるまで待機します。
135     //!
136     //! @return     接続が確立されたら true を返します。
137     //---------------------------------------------------------------------------
138     bool Connect();
139 
140     //---------------------------------------------------------------------------
141     //! @brief      PC との接続を切断します。
142     //!
143     //! @return     成功したら true を返します。
144     //---------------------------------------------------------------------------
145     bool Disconnect();
146 
147     //}
148 
149 
150 
151     //----------------------------------------
152     //! @name 送受信
153     //@{
154 
155     //---------------------------------------------------------------------------
156     //! @brief          パケットを PC へ送信します。
157     //!
158     //! @param[in]      headerBuffer ヘッダ情報へのポインタです。
159     //! @param[in]      headerLength ヘッダ情報のサイズです。
160     //! @param[in]      dataBuffer データへのポインタです。
161     //! @param[in]      dataLength データのサイズです。
162     //---------------------------------------------------------------------------
163     size_t SendPacket(
164         const void* headerBuffer,
165         size_t headerLength,
166         const void* dataBuffer,
167         size_t dataLength
168     );
169 
170     //---------------------------------------------------------------------------
171     //! @brief          パケットを PC から受信します。
172     //!
173     //!                 タイムアウト時間が経過してもパケットが受信できないときは NULL を返します。
174     //!                 timeOut に負の値を設定すると、パケットが受信できるまで待ち続けます。
175     //!
176     //!                 受信パケットのヘッダ、あるいはデータのサイズが 0 のときのみ、
177     //!                 headerBuffer と dataBuffer に NULL を指定できます。
178     //!
179     //!                 現在 0 と負の値以外のタイムアウト時間はサポートされていません。
180     //!
181     //! @param[out]     headerWrittenSize 書き込まれたヘッダ情報のサイズを代入するポインタです。
182     //! @param[out]     dataWrittenSize 書き込まれたヘッダ情報のサイズを代入するポインタです。
183     //! @param[in]      headerBuffer ヘッダ情報バッファへのポインタです。
184     //! @param[in]      headerLength ヘッダ情報バッファのサイズです。
185     //! @param[in]      dataBuffer データバッファへのポインタです。
186     //! @param[in]      dataLength データバッファのサイズです。
187     //! @param[in]      timeOut ミリ秒単位のタイムアウト時間です。
188     //---------------------------------------------------------------------------
189     bool ReceivePacket(
190         size_t* headerWrittenSize,
191         size_t* dataWrittenSize,
192         void* headerBuffer,
193         size_t headerLength,
194         void* dataBuffer,
195         size_t dataLength,
196         int timeOut
197     );
198 
199     //}
200 
201 private:
202     HioPacketChannel(
203         os::IAllocator* allocator,
204         os::IAllocator* deviceAllocator,
205         const Description& description
206     );
207 
208     ~HioPacketChannel();
209 
210     nw::os::IAllocator* m_Allocator;
211     nw::os::IAllocator* m_DeviceAllocator;
212 
213     static nw::os::IAllocator* s_DeviceAllocatorForHioWorkMemory;
214     static void* s_HioWorkMemory;
215     void* m_SerialWorkMemory;
216 
217     nn::hio::CTR::SerialChannel m_Serial;
218 };
219 
220 
221 } // namespace nw::demo
222 } // namespace nw
223 
224 #else
225 
226 
227 namespace nw
228 {
229 
230 namespace os
231 {
232 class IAllocator;
233 }
234 namespace demo
235 {
236 
237 //! @details :private
238 class HioPacketChannel
239 {
240 private:
241     static const s32 PACKET_HEADER_ID;
242     static const size_t BUFFER_SIZE;
243 public:
244     struct Description
245     {
DescriptionDescription246         Description()
247         {}
248     };
249     class Builder
250     {
251     public:
HioPacketChannelDescription(const Description &)252         Builder& HioPacketChannelDescription(const Description&){return *this;}
ChannelNumber(s32)253         Builder& ChannelNumber(s32 ){return *this;}
Create(os::IAllocator *,os::IAllocator *)254         HioPacketChannel* Create(os::IAllocator* ,os::IAllocator* ) { return NULL; }
255     };
Destroy()256     void Destroy() {}
Initialize(os::IAllocator *)257     static bool Initialize(os::IAllocator*) { return false; }
Finalize()258     static void Finalize() {}
Listen(s32)259     bool Listen(s32 ) { return false; }
Connect()260     bool Connect() { return false; }
Disconnect()261     bool Disconnect() { return false; }
SendPacket(const void *,size_t,const void *,size_t)262     size_t SendPacket(const void* ,size_t ,const void* ,size_t ) { return 0; }
ReceivePacket(size_t *,size_t *,void *,size_t,void *,size_t,int)263     bool ReceivePacket(size_t* ,size_t* ,void* ,size_t ,void* ,size_t ,int ){ return false; }
264 private:
HioPacketChannel(os::IAllocator *,os::IAllocator *,const Description &)265     HioPacketChannel(os::IAllocator* ,os::IAllocator* ,const Description& ){ }
~HioPacketChannel()266     ~HioPacketChannel() { }
267     static nw::os::IAllocator* s_DeviceAllocatorForHioWorkMemory;
268     static void* s_HioWorkMemory;
269 };
270 
271 
272 } // namespace nw::demo
273 } // namespace nw
274 
275 #endif
276 
277 #endif // NW_DEMO_HIOPACKETCHANNEL_H_
278 
279