GeekIMU/4.Software/GeekIMU Manager GUI 1.2/GeekIMUDriver 1.2/Src/PipeHMD.h

151 lines
3.4 KiB
C++

#ifndef _PIPE_H_
#define _PIPE_H_
#pragma once
#include <string>
#include <iostream>
#include "OVR_CAPI.h"
using namespace std;
#define COMMAND_DEVICE_IS_OPEN 1
#define COMMAND_GET_DATA 2
#define COMMAND_DEVICE_CALIBRATION 3
#define COMMAND_DEVICE_GET_PURE_DATA 4
#define COMMAND_DEVICE_ENTER_DFU_MODE 11
#define ENTER_DFU_MODE_SUCCESS 1
#define ENTER_DFU_MODE_FAILED 0
#define CALIBRATION_SUCCESS 1
#define CALIBRATION_FAILED 0
namespace DMPipe{
class PipeHMD
{
public:
PipeHMD() :m_hPipe(INVALID_HANDLE_VALUE){}
~PipeHMD();
PipeHMD(std::string sname);
BOOL IsOpen();
ovrSensorState GetSensorData();
SensorPureData GetPureSensorData();
BOOL EnterDFUMode();
BOOL Calibration();
inline LPCTSTR GetName()
{
return name.c_str();
}
void SetName(std::string sname);
private:
std::string name;
unsigned char reciveBuf[512];
unsigned char writeBuf[512];
DWORD readLenth;
DWORD writeLenth;
HANDLE m_hPipe;
BOOL isConnection;
inline void Init()
{
for (;;)
{
m_hPipe = CreateFile(
GetName(), // pipe name
GENERIC_READ | // read and write access
GENERIC_WRITE,
0, // no sharing
NULL, // default security attributes
OPEN_EXISTING, // opens existing pipe
0, // default attributes
NULL); // no template file
// Break if the pipe handle is valid.
if (m_hPipe != INVALID_HANDLE_VALUE)
{
isConnection = TRUE;
return;
}
// Exit if an error other than ERROR_PIPE_BUSY occurs.
if (GetLastError() != ERROR_PIPE_BUSY)
{
//char buf[255] = { 0 };
//printf(TEXT("Could not open pipe. GLE=%d\n"), GetLastError());
//TimeLog(buf);
return;
}
// All pipe instances are busy, so wait for 1 milliseconds.
if (!WaitNamedPipe(GetName(), 1))
{
//TimeLog("Could not open pipe: 1 millisecond wait timed out.");
return;
}
}
}
inline void Free()
{
if (m_hPipe == INVALID_HANDLE_VALUE)
{
isConnection = FALSE;
return;
}
else
{
if (CloseHandle(m_hPipe))
{
m_hPipe = INVALID_HANDLE_VALUE;
isConnection = FALSE;
}
}
}
inline void PipeProcess(DWORD sizeofrecive)
{
this->Init();
if (isConnection)
{
BOOL fSuccess = FALSE;
fSuccess = WriteFile(
m_hPipe, // pipe handle
writeBuf, // message
sizeof(unsigned int) * 3, // message length
&writeLenth, // bytes written
NULL); // not overlapped
if (!fSuccess)
{
char buf[255] = { 0 };
sprintf(buf, TEXT("WriteFile to pipe failed. GLE=%d\n"), GetLastError());
//TimeLog(buf);
goto freepipe;
}
//printf("\nMessage sent to server, receiving reply as follows:\n");
do
{
// Read from the pipe.
fSuccess = ReadFile(
m_hPipe, // pipe handle
reciveBuf, // buffer to receive reply
sizeofrecive, // size of buffer
&readLenth, // number of bytes read
NULL); // not overlapped
if (!fSuccess && GetLastError() != ERROR_MORE_DATA)
break;
} while (!fSuccess); // repeat loop if ERROR_MORE_DATA
if (!fSuccess)
{
char buf[255] = { 0 };
sprintf(buf, TEXT("ReadFile from pipe failed. GLE=%d\n"), GetLastError());
//TimeLog(buf);
goto freepipe;
}
freepipe:
this->Free();
}
}
};
}
#endif