File: PS1200Device.cpp

package info (click to toggle)
openni2 2.2.0.33%2Bdfsg-18
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 22,240 kB
  • sloc: cpp: 111,183; ansic: 35,511; sh: 10,556; python: 1,313; java: 952; makefile: 575; xml: 12
file content (222 lines) | stat: -rw-r--r-- 5,367 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#include "PS1200Device.h"
#include "XnClientUSBConnectionFactory.h"
#include "XnSocketConnectionFactory.h"
#include <PSLink.h>
#include <XnLog.h>

#define XN_MASK_PS1200_DEVICE "PS1200Device"

namespace xn
{

const XnUInt32 PS1200Device::WAIT_FOR_FREE_BUFFER_TIMEOUT_MS = XN_WAIT_INFINITE;
const XnUInt32 PS1200Device::NUM_OUTPUT_CONNECTIONS = 0;
const XnUInt32 PS1200Device::NUM_INPUT_CONNECTIONS = 3;
const XnUInt32 PS1200Device::PRE_CONTROL_RECEIVE_SLEEP = 0;

PS1200Device::PS1200Device()
{
	m_hInputInterruptCallback = NULL;
	m_bInitialized = FALSE;
}

PS1200Device::~PS1200Device()
{
	Shutdown();
}

XnStatus PS1200Device::Init(const XnChar* strConnString, XnTransportType transportType)
{
	XnStatus nRetVal = XN_STATUS_OK;

	if (transportType != XN_TRANSPORT_TYPE_USB)
	{
		xnLogError(XN_MASK_LINK, "Transport type not supported: %d", transportType);
		XN_ASSERT(FALSE);
		return XN_STATUS_BAD_PARAM;
	}

	nRetVal = PrimeClient::Init(strConnString, XN_TRANSPORT_TYPE_USB);
	XN_IS_STATUS_OK_LOG_ERROR("Init EE Device", nRetVal);

#if (XN_PLATFORM == XN_PLATFORM_WIN32)
	// On all platforms other than Windows, prefer BULK
	nRetVal = SetUsbAltInterface(0);
	XN_IS_STATUS_OK_LOG_ERROR("Switch to ISO", nRetVal);
#elif (XN_PLATFORM == XN_PLATFORM_LINUX_X86 || XN_PLATFORM == XN_PLATFORM_LINUX_ARM || XN_PLATFORM == XN_PLATFORM_MACOSX || XN_PLATFORM == XN_PLATFORM_ANDROID_ARM || XN_PLATFORM_LINUX_GENERIC)
	// On all platforms other than Windows, prefer BULK
	nRetVal = SetUsbAltInterface(1);
	XN_IS_STATUS_OK_LOG_ERROR("Switch to BULK", nRetVal);
#else
	#error "Unsupported platform"
#endif

	m_bInitialized = TRUE;
	return XN_STATUS_OK;	
}

void PS1200Device::Shutdown()
{
	PrimeClient::Shutdown();
	m_bInitialized = FALSE;
}

XnBool PS1200Device::IsInitialized() const
{
	return m_bInitialized;
}

IConnectionFactory* PS1200Device::CreateConnectionFactory(XnTransportType transportType)
{
	if (transportType != XN_TRANSPORT_TYPE_USB)
	{
		XN_ASSERT(FALSE);
		return NULL;
	}

	return XN_NEW(ClientUSBConnectionFactory, 
			        NUM_INPUT_CONNECTIONS, 
					NUM_OUTPUT_CONNECTIONS, 
					PRE_CONTROL_RECEIVE_SLEEP);
}

ClientUSBConnectionFactory* PS1200Device::GetConnectionFactory()
{
	return (ClientUSBConnectionFactory*)m_pConnectionFactory;
}

const ClientUSBConnectionFactory* PS1200Device::GetConnectionFactory() const
{
	return (const ClientUSBConnectionFactory*)m_pConnectionFactory;
}

XnStatus PS1200Device::SetUsbAltInterface(XnUInt8 altInterface)
{
	return GetConnectionFactory()->SetUsbAltInterface(altInterface);
}

XnStatus PS1200Device::GetUsbAltInterface(XnUInt8& altInterface) const
{
	return GetConnectionFactory()->GetUsbAltInterface(&altInterface);
}

class UsbEndpointTester : public IDataDestination
{
public:
	void Reset()
	{
		m_nCounter = 0;
		m_nTotalBytes = 0;
		m_nLostPackets = 0;
	}

	virtual XnStatus IncomingData(const void* pData, XnUInt32 nSize)
	{
		m_nTotalBytes += nSize;

		const XnUInt8* pCurData = (const XnUInt8*)pData;
		const XnUInt8* pEndData = pCurData + nSize;

		while (pCurData < pEndData)
		{
			// first word is a counter
			const XnUInt32* pDWords = (const XnUInt32*)pCurData;
			XnUInt32 nPacketSize = pDWords[0];
			XnUInt32 nCounter = pDWords[1];

			XnUInt32 nLostPackets = (nCounter - m_nCounter - 1);

			m_nLostPackets += nLostPackets;
			m_nCounter = nCounter;
			pCurData += nPacketSize;
		}

		return XN_STATUS_OK;
	}

	virtual void HandleDisconnection()
	{
		xnLogWarning(XN_MASK_PS1200_DEVICE, "Endpoint disconnected during USB test!");
	}

	XnUInt32 m_nEP;
	XnUInt32 m_nTotalBytes;
	XnUInt32 m_nLostPackets;

private:
	int m_nCounter;
};

XnStatus PS1200Device::UsbTest(XnUInt32 nSeconds, XnUInt32& endpointsCount, XnUsbTestEndpointResult* endpoints)
{
	XnStatus nRetVal = XN_STATUS_OK;
	
	xn::ClientUSBConnectionFactory* pConnFactory = GetConnectionFactory();

	if (m_linkInputStreamsMgr.HasStreams())
	{
		xnLogWarning(XN_MASK_PS1200_DEVICE, "Can't start USB test when other streams exists!");
		return XN_STATUS_ERROR;
	}

	XnUInt16 nNumEndpoints = pConnFactory->GetNumInputDataConnections();
	if (nNumEndpoints > endpointsCount)
	{
		xnLogWarning(XN_MASK_PS1200_DEVICE, "Endpoints array is too small");
		return XN_STATUS_BAD_PARAM;
	}

	xn::IAsyncInputConnection* aEndpoints[20];
	UsbEndpointTester aTesters[20];

	for (int i = 0; i < nNumEndpoints; ++i)
	{
		nRetVal = pConnFactory->CreateInputDataConnection((XnUInt16)i, aEndpoints[i]);
		if (nRetVal != XN_STATUS_OK)
		{
			for (int j = 0; j < i; ++j)
			{
				XN_DELETE(aEndpoints[j]);
			}
			return nRetVal;
		}

		aTesters[i].Reset();
		aTesters[i].m_nEP = i;
		aEndpoints[i]->SetDataDestination(&aTesters[i]);
		aEndpoints[i]->Connect();
	}

	nRetVal = m_linkControlEndpoint.StartUsbTest();
	if (nRetVal != XN_STATUS_OK)
	{
		for (int i = 0; i < nNumEndpoints; ++i)
		{
			XN_DELETE(aEndpoints[i]);
		}
		return nRetVal;
	}

	// let the test run
	xnOSSleep(nSeconds*1000);

	nRetVal = m_linkControlEndpoint.StopUsbTest();
	if (nRetVal != XN_STATUS_OK)
	{
		xnLogWarning(XN_MASK_PS1200_DEVICE, "Failed to stop USB test!");
		XN_ASSERT(FALSE);
	}

	for (int i = 0; i < nNumEndpoints; ++i)
	{
		XN_DELETE(aEndpoints[i]);
		
		endpoints[i].averageBytesPerSecond = aTesters[i].m_nTotalBytes / (XnDouble)nSeconds;
		endpoints[i].lostPackets = aTesters[i].m_nLostPackets;
	}
	endpointsCount = nNumEndpoints;

	return (XN_STATUS_OK);
}

}