Skip to content

Commit a947805

Browse files
AIRO-1381 Version check (#210)
1 parent 07bdf30 commit a947805

File tree

2 files changed

+58
-1
lines changed

2 files changed

+58
-1
lines changed

com.unity.robotics.ros-tcp-connector/Runtime/TcpConnector/ROSConnection.cs

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ namespace Unity.Robotics.ROSTCPConnector
1515
{
1616
public class ROSConnection : MonoBehaviour
1717
{
18+
public const string k_Version = "v0.7.0";
19+
public const string k_CompatibleVersionPrefix = "v0.7.";
20+
1821
// Variables required for ROS communication
1922
[SerializeField]
2023
[FormerlySerializedAs("hostName")]
@@ -633,6 +636,32 @@ void ReceiveSysCommand(string topic, string json)
633636
{
634637
switch (topic)
635638
{
639+
case SysCommand.k_SysCommand_Handshake:
640+
{
641+
var handshakeCommand = JsonUtility.FromJson<SysCommand_Handshake>(json);
642+
if (handshakeCommand.version == null)
643+
{
644+
Debug.LogError($"Corrupted or unreadable ROS-TCP-Endpoint version data! Expected: {k_Version}");
645+
}
646+
else if (!handshakeCommand.version.StartsWith(k_CompatibleVersionPrefix))
647+
{
648+
Debug.LogError($"Incompatible ROS-TCP-Endpoint version: {handshakeCommand.version}. Expected: {k_Version}");
649+
}
650+
651+
var handshakeMetadata = JsonUtility.FromJson<SysCommand_Handshake_Metadata>(handshakeCommand.metadata);
652+
#if ROS2
653+
if (handshakeMetadata.protocol != "ROS2")
654+
{
655+
Debug.LogError($"Incompatible protocol: ROS-TCP-Endpoint is using {handshakeMetadata.protocol}, but Unity is in ROS2 mode. Switch it from the Robotics/Ros Settings menu.");
656+
}
657+
#else
658+
if (handshakeMetadata.protocol != "ROS1")
659+
{
660+
Debug.LogError($"Incompatible protocol: ROS-TCP-Endpoint is using {handshakeMetadata.protocol}, but Unity is in ROS1 mode. Switch it from the Robotics/Ros Settings menu.");
661+
}
662+
#endif
663+
}
664+
break;
636665
case SysCommand.k_SysCommand_Log:
637666
{
638667
var logCommand = JsonUtility.FromJson<SysCommand_Log>(json);
@@ -843,12 +872,23 @@ static async Task ConnectionThread(
843872

844873
static async Task ReaderThread(int readerIdx, NetworkStream networkStream, ConcurrentQueue<Tuple<string, byte[]>> queue, int sleepMilliseconds, CancellationToken token)
845874
{
875+
// First message should be the handshake
876+
Tuple<string, byte[]> handshakeContent = await ReadMessageContents(networkStream, sleepMilliseconds, token);
877+
if (handshakeContent.Item1 == SysCommand.k_SysCommand_Handshake)
878+
{
879+
ROSConnection.m_HasConnectionError = false;
880+
queue.Enqueue(handshakeContent);
881+
}
882+
else
883+
{
884+
Debug.LogError($"Invalid ROS-TCP-Endpoint version detected: 0.6.0 or older. Expected: {k_Version}.");
885+
}
886+
846887
while (!token.IsCancellationRequested)
847888
{
848889
try
849890
{
850891
Tuple<string, byte[]> content = await ReadMessageContents(networkStream, sleepMilliseconds, token);
851-
// Debug.Log($"Message {content.Item1} received");
852892
ROSConnection.m_HasConnectionError = false;
853893

854894
if (content.Item1 != "") // ignore keepalive messages

com.unity.robotics.ros-tcp-connector/Runtime/TcpConnector/SysCommand.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ namespace Unity.Robotics.ROSTCPConnector
77
{
88
public abstract class SysCommand
99
{
10+
public const string k_SysCommand_Handshake = "__handshake";
1011
public const string k_SysCommand_Log = "__log";
1112
public const string k_SysCommand_Warning = "__warn";
1213
public const string k_SysCommand_Error = "__error";
@@ -67,6 +68,22 @@ public struct SysCommand_TopicAndType
6768
public string message_name;
6869
}
6970

71+
// For backwards compatibility, we encode the handshake in two stages:
72+
// Stage 1 - which must NEVER change - is just a version string and a metadata string.
73+
public struct SysCommand_Handshake
74+
{
75+
public string version;
76+
public string metadata;
77+
}
78+
79+
// Stage 2 is the json encoded contents of the metadata string.
80+
// Because this structure may change with future versions of ROS TCP Connector, we only decode it
81+
// after checking the version number is correct.
82+
public struct SysCommand_Handshake_Metadata
83+
{
84+
public string protocol; // "ROS1" or "ROS2"
85+
}
86+
7087
public struct SysCommand_Log
7188
{
7289
public string text;

0 commit comments

Comments
 (0)