Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added version fallback behaviour #58

Merged
merged 2 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 68 additions & 25 deletions RTProtocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,38 @@ namespace
std::make_pair(CRTProtocol::EDegreeOfFreedom::TranslationY, "TranslationY"),
std::make_pair(CRTProtocol::EDegreeOfFreedom::TranslationZ, "TranslationZ")
};

struct RTVersion
{
int major = MAJOR_VERSION;
int minor = MINOR_VERSION;

// Returns a list of rt protocol version which are supported by the current SDK,
// including an initial version by the function argument. (The initial version is not checked for compatibility)
// The list is sorted high to low and contains only unique elements.
static std::vector<RTVersion> VersionList(const RTVersion& initial)
{
// Valid versions added from high to low
std::vector<RTVersion> versions {
{MAJOR_VERSION, MINOR_VERSION},
{1, 25},
{1, 24},
{1, 23},
};

// Remove higher versions
versions.erase(
std::remove_if(versions.begin(), versions.end(), [&](const RTVersion& v) {
return v.major > initial.major || (v.major == initial.major && v.minor >= initial.minor);
}),
versions.end()
);

versions.insert(versions.begin(), initial);

return versions;
}
};
}

unsigned int CRTProtocol::GetSystemFrequency() const
Expand Down Expand Up @@ -92,17 +124,15 @@ CRTProtocol::~CRTProtocol()
}
} // ~CRTProtocol


bool CRTProtocol::Connect(const char* pServerAddr, unsigned short nPort, unsigned short* pnUDPServerPort,
int nMajorVersion, int nMinorVersion, bool bBigEndian)
int nMajorVersion, int nMinorVersion, bool bBigEndian, bool bNegotiateVersion)
{
CRTPacket::EPacketType eType;
std::string tempStr;
std::string responseStr;

mbBigEndian = bBigEndian;
mbIsMaster = false;

mnMajorVersion = 1;
if ((nMajorVersion == 1) && (nMinorVersion == 0))
{
Expand All @@ -125,6 +155,7 @@ bool CRTProtocol::Connect(const char* pServerAddr, unsigned short nPort, unsigne
{
delete mpoRTPacket;
}

mpoRTPacket = new CRTPacket(nMajorVersion, nMinorVersion, bBigEndian);

if (mpoRTPacket == nullptr)
Expand Down Expand Up @@ -159,37 +190,49 @@ bool CRTProtocol::Connect(const char* pServerAddr, unsigned short nPort, unsigne
const std::string welcomeMessage("QTM RT Interface connected");
if (strncmp(welcomeMessage.c_str(), mpoRTPacket->GetCommandString(), welcomeMessage.size()) == 0)
{
// Set protocol version
if (SetVersion(nMajorVersion, nMinorVersion))
std::vector<RTVersion> versionList;
if (bNegotiateVersion)
{
// Set byte order.
// Unless we use protocol version 1.0, we have set the byte order by selecting the correct port.

if ((mnMajorVersion == 1) && (mnMinorVersion == 0))
versionList = RTVersion::VersionList({nMajorVersion, nMinorVersion});
}
else
{
versionList = std::vector<RTVersion>(1, {nMajorVersion, nMinorVersion});
}

for(RTVersion& version : versionList)
{
// Set protocol version
if (SetVersion(version.major, version.minor))
{
if (mbBigEndian)
{
tempStr = "ByteOrder BigEndian";
}
else
// Set byte order.
// Unless we use protocol version 1.0, we have set the byte order by selecting the correct port.
if ((mnMajorVersion == 1) && (mnMinorVersion == 0))
{
tempStr = "ByteOrder LittleEndian";
}
if (mbBigEndian)
{
tempStr = "ByteOrder BigEndian";
}
else
{
tempStr = "ByteOrder LittleEndian";
}

if (SendCommand(tempStr, responseStr))
{
return true;
if (SendCommand(tempStr, responseStr))
{
return true;
}
else
{
strcpy(maErrorStr, "Set byte order failed.");
}
}
else
{
strcpy(maErrorStr, "Set byte order failed.");
GetState(meState, true);
return true;
}
}
else
{
GetState(meState, true);
return true;
}
}
Disconnect();
return false;
Expand Down
2 changes: 1 addition & 1 deletion RTProtocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -674,7 +674,7 @@ class DLL_EXPORT CRTProtocol
~CRTProtocol();

bool Connect(const char* pServerAddr, unsigned short nPort = cDefaultBasePort, unsigned short* pnUDPServerPort = nullptr,
int nMajorVersion = MAJOR_VERSION, int nMinorVersion = MINOR_VERSION, bool bBigEndian = false);
int nMajorVersion = MAJOR_VERSION, int nMinorVersion = MINOR_VERSION, bool bBigEndian = false, bool bNegotiateVersion = true);
unsigned short GetUdpServerPort();
void Disconnect();
bool Connected() const;
Expand Down
20 changes: 14 additions & 6 deletions RigidBodyStreaming/RigidBodyStreaming.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,31 @@ int main(int argc, char **argv)

const char serverAddr[] = "127.0.0.1";
const unsigned short basePort = 22222;
const int majorVersion = 1;
const int minorVersion = 19;
const bool bigEndian = false;

bool dataAvailable = false;
bool streamFrames = false;
unsigned short udpPort = 6734;
while (true)
{
if (!rtProtocol.Connected())
{
if (!rtProtocol.Connect(serverAddr, basePort, &udpPort, majorVersion, minorVersion, bigEndian))
if (!rtProtocol.Connect(serverAddr, basePort, &udpPort))
{
printf("rtProtocol.Connect: %s\n\n", rtProtocol.GetErrorString());
sleep(1);
sleep(1000);
continue;
}

unsigned int major, minor;
if(rtProtocol.GetVersion(major, minor))
{
printf("rtProtocol.Connect: RT Protocol Version %d.%d\n\n", major, minor);
}

std::string qtmVersion;
if(rtProtocol.GetQTMVersion(qtmVersion))
{
printf("rtProtocol.Connect: Connected to %s\n\n", qtmVersion.data());
}
}

if (!dataAvailable)
Expand Down