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 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
|
/****************************************************************************
* *
* PrimeSense Sensor 5.x Alpha *
* Copyright (C) 2011 PrimeSense Ltd. *
* *
* This file is part of PrimeSense Sensor. *
* *
* PrimeSense Sensor is free software: you can redistribute it and/or modify*
* it under the terms of the GNU Lesser General Public License as published *
* by the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* PrimeSense Sensor is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with PrimeSense Sensor. If not, see <http://www.gnu.org/licenses/>.*
* *
****************************************************************************/
//---------------------------------------------------------------------------
// Includes
//---------------------------------------------------------------------------
#include "XnDeviceSensorProtocol.h"
#include "XnDeviceSensorIO.h"
#include "Uncomp.h"
#include "XnHostProtocol.h"
#include <XnLog.h>
#include <XnProfiling.h>
#include "XnStreamProcessor.h"
#include "XnSensor.h"
#include <XnOS.h>
FILE* g_fUSBDump;
//---------------------------------------------------------------------------
// Code
//---------------------------------------------------------------------------
XnBool XN_CALLBACK_TYPE XnDeviceSensorProtocolUsbEpCb(XnUChar* pBuffer, XnUInt32 nBufferSize, void* pCallbackData)
{
XN_PROFILING_START_MT_SECTION("XnDeviceSensorProtocolUsbEpCb");
XnUInt32 nReadBytes;
XnUInt16 nMagic;
XnSpecificUsbDevice* pDevice = (XnSpecificUsbDevice*)pCallbackData;
XnDevicePrivateData* pDevicePrivateData = pDevice->pDevicePrivateData;
XnUChar* pBufEnd = pBuffer + nBufferSize;
XnSpecificUsbDeviceState* pCurrState = &pDevice->CurrState;
while (pBuffer < pBufEnd)
{
switch (pCurrState->State)
{
case XN_WAITING_FOR_CONFIGURATION:
if (pDevicePrivateData->bIgnoreDataPackets)
{
// ignore this packet
xnLogVerbose(XN_MASK_SENSOR_PROTOCOL, "ignoring %d bytes - device requested to ignore!", nBufferSize);
pBuffer = pBufEnd;
}
else
{
pCurrState->State = XN_IGNORING_GARBAGE;
pCurrState->nMissingBytesInState = pDevice->nIgnoreBytes;
}
break;
case XN_IGNORING_GARBAGE:
// ignore first bytes on this endpoint. NOTE: due to a bug in the firmware, the first data received
// on each endpoint is corrupt, causing wrong timestamp calculation, causing future (true) timestamps
// to be calculated wrongly. By ignoring the first data received on each endpoint we hope to get
// only valid data.
nReadBytes = XN_MIN((XnUInt32)(pBufEnd - pBuffer), pCurrState->nMissingBytesInState);
if (nReadBytes > 0)
{
xnLogVerbose(XN_MASK_SENSOR_PROTOCOL, "ignoring %d bytes - ignore garbage phase!", nReadBytes);
pCurrState->nMissingBytesInState -= nReadBytes;
pBuffer += nReadBytes;
}
if (pCurrState->nMissingBytesInState == 0)
{
pCurrState->State = XN_LOOKING_FOR_MAGIC;
pCurrState->nMissingBytesInState = sizeof(XnUInt16);
}
break;
case XN_LOOKING_FOR_MAGIC:
nMagic = XN_PREPARE_VAR16_IN_BUFFER(pDevicePrivateData->FWInfo.nFWMagic);
if (pCurrState->nMissingBytesInState == sizeof(XnUInt8) && // first byte already found
pBuffer[0] == ((XnUInt8*)&nMagic)[1]) // we have here second byte
{
// move to next byte
pBuffer++;
// move to next state
pCurrState->CurrHeader.nMagic = nMagic;
pCurrState->State = XN_PACKET_HEADER;
pCurrState->nMissingBytesInState = sizeof(XnSensorProtocolResponseHeader);
break;
}
while (pBuffer < pBufEnd)
{
if (nMagic == *(XnUInt16*)(pBuffer))
{
pCurrState->CurrHeader.nMagic = nMagic;
pCurrState->State = XN_PACKET_HEADER;
pCurrState->nMissingBytesInState = sizeof(XnSensorProtocolResponseHeader);
break;
}
else
pBuffer++;
}
if (pBuffer == pBufEnd && // magic wasn't found
pBuffer[-1] == ((XnUInt8*)&nMagic)[0]) // last byte in buffer is first in magic
{
// mark that we found first one
pCurrState->nMissingBytesInState--;
}
break;
case XN_PACKET_HEADER:
nReadBytes = XN_MIN((XnUInt32)(pBufEnd - pBuffer), pCurrState->nMissingBytesInState);
xnOSMemCopy((XnUChar*)&pCurrState->CurrHeader + sizeof(XnSensorProtocolResponseHeader) - pCurrState->nMissingBytesInState,
pBuffer, nReadBytes);
pCurrState->nMissingBytesInState -= nReadBytes;
pBuffer += nReadBytes;
if (pCurrState->nMissingBytesInState == 0)
{
// we have entire header. Fix it
pCurrState->CurrHeader.nBufSize = XN_PREPARE_VAR16_IN_BUFFER(pCurrState->CurrHeader.nBufSize);
pCurrState->CurrHeader.nMagic = XN_PREPARE_VAR16_IN_BUFFER(pCurrState->CurrHeader.nMagic);
pCurrState->CurrHeader.nPacketID = XN_PREPARE_VAR16_IN_BUFFER(pCurrState->CurrHeader.nPacketID);
pCurrState->CurrHeader.nTimeStamp = XN_PREPARE_VAR32_IN_BUFFER(pCurrState->CurrHeader.nTimeStamp);
pCurrState->CurrHeader.nType = XN_PREPARE_VAR16_IN_BUFFER(pCurrState->CurrHeader.nType);
pCurrState->CurrHeader.nBufSize = xnOSEndianSwapUINT16(pCurrState->CurrHeader.nBufSize);
pCurrState->CurrHeader.nBufSize -= sizeof(XnSensorProtocolResponseHeader);
pCurrState->State = XN_PACKET_DATA;
pCurrState->nMissingBytesInState = pCurrState->CurrHeader.nBufSize;
}
break;
case XN_PACKET_DATA:
nReadBytes = XN_MIN((XnUInt32)(pBufEnd - pBuffer), pCurrState->nMissingBytesInState);
pDevicePrivateData->pSensor->GetFirmware()->GetStreams()->ProcessPacketChunk(&pCurrState->CurrHeader, pBuffer, pCurrState->CurrHeader.nBufSize - pCurrState->nMissingBytesInState, nReadBytes);
pBuffer += nReadBytes;
pCurrState->nMissingBytesInState -= nReadBytes;
if (pCurrState->nMissingBytesInState == 0)
{
pCurrState->State = XN_LOOKING_FOR_MAGIC;
pCurrState->nMissingBytesInState = sizeof(XnUInt16);
}
break;
}
}
XN_PROFILING_END_SECTION;
return TRUE;
}
XnStatus XnDeviceSensorProtocolFindStreamOfType(XnDevicePrivateData* pDevicePrivateData, const XnChar* strType, const XnChar** ppStreamName)
{
XnStatus nRetVal = XN_STATUS_OK;
const XnChar* strNames[100];
XnUInt32 nCount = 100;
nRetVal = pDevicePrivateData->pSensor->GetStreamNames(strNames, &nCount);
XN_IS_STATUS_OK(nRetVal);
for (XnUInt32 i = 0; i < nCount; ++i)
{
XnChar strCurType[XN_DEVICE_MAX_STRING_LENGTH];
nRetVal = pDevicePrivateData->pSensor->GetProperty(strNames[i], XN_STREAM_PROPERTY_TYPE, strCurType);
XN_IS_STATUS_OK(nRetVal);
if (strcmp(strType, strCurType) == 0)
{
*ppStreamName = strNames[i];
return (XN_STATUS_OK);
}
}
*ppStreamName = NULL;
return (XN_STATUS_NO_MATCH);
}
XnStatus XnDeviceSensorProtocolDumpLastRawFrameImpl(XnDevicePrivateData* pDevicePrivateData, const XnChar* strType, const XnChar* strFileName)
{
XnStatus nRetVal = XN_STATUS_OK;
const XnChar* strName;
nRetVal = XnDeviceSensorProtocolFindStreamOfType(pDevicePrivateData, strType, &strName);
XN_IS_STATUS_OK(nRetVal);
XnUInt64 nMaxDataSize;
nRetVal = pDevicePrivateData->pSensor->GetProperty(strName, XN_STREAM_PROPERTY_REQUIRED_DATA_SIZE, &nMaxDataSize);
XN_IS_STATUS_OK(nRetVal);
XnDynamicSizeBuffer dsb;
dsb.nMaxSize = (XnUInt32)nMaxDataSize;
dsb.pData = xnOSMallocAligned((XnUInt32)nMaxDataSize, XN_DEFAULT_MEM_ALIGN);
XN_VALIDATE_ALLOC_PTR(dsb.pData);
nRetVal = pDevicePrivateData->pSensor->GetProperty(strName, XN_STREAM_PROPERTY_LAST_RAW_FRAME, XN_PACK_GENERAL_BUFFER(dsb));
if (nRetVal != XN_STATUS_OK)
{
xnOSFreeAligned(dsb.pData);
return (nRetVal);
}
xnOSSaveFile(strFileName, dsb.pData, dsb.nDataSize);
xnOSFreeAligned(dsb.pData);
return (XN_STATUS_OK);
}
XnStatus XnDeviceSensorProtocolDumpLastRawFrame(XnDevicePrivateData* pDevicePrivateData, const XnChar* strType, const XnChar* strFileName)
{
XnStatus nRetVal = XN_STATUS_OK;
printf("* Dumping %s...\n", strType);
nRetVal = XnDeviceSensorProtocolDumpLastRawFrameImpl(pDevicePrivateData, strType, strFileName);
if (nRetVal != XN_STATUS_OK)
{
printf("** Failed! %s\n", xnGetStatusString(nRetVal));
}
else
{
printf ("** Saved %s to %s\n", strType, strFileName);
}
return (XN_STATUS_OK);
}
|