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