Skip to content

Commit 7d3db77

Browse files
spacey-sootymcm001
authored andcommitted
Add version checking to C++ (PhotonVision#1774)
Signed-off-by: Jade Turner <spacey-sooty@proton.me> Co-authored-by: Matt <matthew.morley.ca@gmail.com>
1 parent c557a14 commit 7d3db77

1 file changed

Lines changed: 95 additions & 19 deletions

File tree

photon-lib/src/main/native/cpp/photon/PhotonCamera.cpp

Lines changed: 95 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,12 @@
2727
#include <hal/FRCUsageReporting.h>
2828
#include <net/TimeSyncServer.h>
2929

30+
#include <stdexcept>
3031
#include <string>
3132
#include <string_view>
3233
#include <vector>
3334

35+
#include <WPILibVersion.h>
3436
#include <frc/Errors.h>
3537
#include <frc/RobotController.h>
3638
#include <frc/Timer.h>
@@ -39,26 +41,81 @@
3941
#include <wpi/json.h>
4042

4143
#include "PhotonVersion.h"
44+
#include "opencv2/core/utility.hpp"
4245
#include "photon/dataflow/structures/Packet.h"
4346

44-
inline constexpr std::string_view bfw =
45-
"\n\n\n\n"
46-
">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n"
47-
">>> !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n"
48-
">>> \n"
49-
">>> You are running an incompatible version \n"
50-
">>> of PhotonVision on your coprocessor! \n"
51-
">>> \n"
52-
">>> This is neither tested nor supported. \n"
53-
">>> You MUST update PhotonVision, \n"
54-
">>> PhotonLib, or both. \n"
55-
">>> \n"
56-
">>> Your code will now crash. \n"
57-
">>> We hope your day gets better. \n"
58-
">>> \n"
59-
">>> !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n"
60-
">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n"
61-
"\n\n";
47+
inline void verifyDependencies() {
48+
if (!(std::string_view{GetWPILibVersion()} ==
49+
std::string_view{photon::PhotonVersion::wpilibTargetVersion})) {
50+
std::string bfw =
51+
"\n\n\n\n\n"
52+
">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n"
53+
">>> !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n"
54+
">>> \n"
55+
">>> You are running an incompatible version \n"
56+
">>> of PhotonVision ! \n"
57+
">>> \n"
58+
">>> PhotonLib ";
59+
bfw += photon::PhotonVersion::versionString;
60+
bfw += " is built for WPILib ";
61+
bfw += photon::PhotonVersion::wpilibTargetVersion;
62+
bfw +=
63+
"\n"
64+
">>> but you are using WPILib ";
65+
bfw += GetWPILibVersion();
66+
bfw +=
67+
"\n>>> \n"
68+
">>> This is neither tested nor supported. \n"
69+
">>> You MUST update PhotonVision, \n"
70+
">>> PhotonLib, or both. \n"
71+
">>> Verify the output of `./gradlew dependencies` \n"
72+
">>> \n"
73+
">>> Your code will now crash. \n"
74+
">>> We hope your day gets better. \n"
75+
">>> \n"
76+
">>> !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n"
77+
">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n";
78+
79+
FRC_ReportWarning(bfw);
80+
FRC_ReportError(frc::err::Error, bfw);
81+
throw new std::runtime_error(std::string{bfw});
82+
}
83+
if (!(std::string_view{cv::getVersionString()} ==
84+
std::string_view{photon::PhotonVersion::opencvTargetVersion})) {
85+
std::string bfw =
86+
"\n\n\n\n\n"
87+
">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n"
88+
">>> !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n"
89+
">>> \n"
90+
">>> You are running an incompatible version \n"
91+
">>> of PhotonVision ! \n"
92+
">>> \n"
93+
">>> PhotonLib ";
94+
bfw += photon::PhotonVersion::versionString;
95+
bfw += " is built for OpenCV ";
96+
bfw += photon::PhotonVersion::opencvTargetVersion;
97+
bfw +=
98+
"\n"
99+
">>> but you are using OpenCV ";
100+
bfw += cv::getVersionString();
101+
bfw +=
102+
"\n>>> \n"
103+
">>> This is neither tested nor supported. \n"
104+
">>> You MUST update PhotonVision, \n"
105+
">>> PhotonLib, or both. \n"
106+
">>> Verify the output of `./gradlew dependencies` \n"
107+
">>> \n"
108+
">>> Your code will now crash. \n"
109+
">>> We hope your day gets better. \n"
110+
">>> \n"
111+
">>> !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n"
112+
">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n";
113+
114+
FRC_ReportWarning(bfw);
115+
FRC_ReportError(frc::err::Error, bfw);
116+
throw new std::runtime_error(std::string{bfw});
117+
}
118+
}
62119

63120
// bit of a hack -- start a TimeSync server on port 5810 (hard-coded). We want
64121
// to avoid calling this from static initialization
@@ -125,6 +182,7 @@ PhotonCamera::PhotonCamera(nt::NetworkTableInstance instance,
125182
topicNameSubscriber(instance, PHOTON_PREFIX, {.topicsOnly = true}),
126183
path(rootTable->GetPath()),
127184
cameraName(cameraName) {
185+
verifyDependencies();
128186
HAL_Report(HALUsageReporting::kResourceType_PhotonCamera, InstanceCount);
129187
InstanceCount++;
130188

@@ -310,7 +368,25 @@ void PhotonCamera::VerifyVersion() {
310368
std::string remote_uuid{remote_uuid_json};
311369

312370
if (local_uuid != remote_uuid) {
313-
FRC_ReportError(frc::warn::Warning, bfw);
371+
constexpr std::string_view bfw =
372+
"\n\n\n\n"
373+
">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n"
374+
">>> !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n"
375+
">>> \n"
376+
">>> You are running an incompatible version \n"
377+
">>> of PhotonVision on your coprocessor! \n"
378+
">>> \n"
379+
">>> This is neither tested nor supported. \n"
380+
">>> You MUST update PhotonVision, \n"
381+
">>> PhotonLib, or both. \n"
382+
">>> \n"
383+
">>> Your code will now crash. \n"
384+
">>> We hope your day gets better. \n"
385+
">>> \n"
386+
">>> !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n"
387+
">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n"
388+
"\n\n";
389+
FRC_ReportWarning(bfw);
314390
std::string error_str = fmt::format(
315391
"Photonlib version {} (message definition version {}) does not match "
316392
"coprocessor version {} (message definition version {})!",

0 commit comments

Comments
 (0)