Skip to content

Commit dc2dc06

Browse files
created ptp parsing function fixing issue when reading json from unit_8 buffer.
1 parent 9355e26 commit dc2dc06

2 files changed

Lines changed: 104 additions & 96 deletions

File tree

include/opentrackio-cpp/OpenTrackIOProperties.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,7 @@ namespace opentrackio::opentrackioproperties
476476

477477
private:
478478
static std::optional<Synchronization> parseSynchronization(nlohmann::json& json, std::vector<std::string>& errors);
479+
static std::optional<Synchronization::Ptp> parsePtp(nlohmann::json& json, std::vector<std::string>& errors);
479480
};
480481

481482
struct Tracker

src/OpenTrackIOProperties.cpp

Lines changed: 103 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -597,125 +597,132 @@ namespace opentrackio::opentrackioproperties
597597

598598
if (syncJson.contains("ptp"))
599599
{
600-
outSync.ptp = Synchronization::Ptp{};
601-
auto& ptpJson = syncJson["ptp"];
600+
outSync.ptp = parsePtp(syncJson, errors);
601+
OpenTrackIOHelpers::clearFieldIfEmpty(syncJson, "ptp");
602+
}
603+
604+
return outSync;
605+
}
602606

603-
std::optional<std::string> profileStr;
604-
OpenTrackIOHelpers::assignField(ptpJson, "profile", profileStr, "string", errors);
605-
bool successfullyAssignedProfileField = false;
606-
if (profileStr.has_value())
607+
std::optional<Timing::Synchronization::Ptp>
608+
Timing::parsePtp(nlohmann::json& json, std::vector<std::string>& errors)
609+
{
610+
Synchronization::Ptp outPtp{};
611+
auto& ptpJson = json["ptp"];
612+
613+
std::optional<std::string> profileStr;
614+
OpenTrackIOHelpers::assignField(ptpJson, "profile", profileStr, "string", errors);
615+
bool successfullyAssignedProfileField = false;
616+
if (profileStr.has_value())
617+
{
618+
successfullyAssignedProfileField = true;
619+
if (profileStr == "IEEE Std 1588-2019")
607620
{
608-
successfullyAssignedProfileField = true;
609-
if (profileStr == "IEEE Std 1588-2019")
610-
{
611-
outSync.ptp->profile = Synchronization::Ptp::ProfileType::IEEE_Std_1588_2019;
612-
}
613-
else if (profileStr == "IEEE Std 802.1AS-2020")
614-
{
615-
outSync.ptp->profile = Synchronization::Ptp::ProfileType::IEEE_Std_802_1AS_2020;
616-
}
617-
else if (profileStr == "SMPTE ST2059-2:2021")
618-
{
619-
outSync.ptp->profile = Synchronization::Ptp::ProfileType::SMPTE_ST2059_2_2021;
620-
}
621-
else
622-
{
623-
successfullyAssignedProfileField = false;
624-
}
621+
outPtp.profile = Synchronization::Ptp::ProfileType::IEEE_Std_1588_2019;
625622
}
626-
else
623+
else if (profileStr == "IEEE Std 802.1AS-2020")
627624
{
628-
errors.emplace_back("field: timing/synchronization/ptp/profile is required, however it is missing.");
629-
return std::nullopt;
625+
outPtp.profile = Synchronization::Ptp::ProfileType::IEEE_Std_802_1AS_2020;
630626
}
631-
632-
if (!successfullyAssignedProfileField)
627+
else if (profileStr == "SMPTE ST2059-2:2021")
633628
{
634-
errors.emplace_back("field: profile has an invalid string value.");
635-
return std::nullopt;
629+
outPtp.profile = Synchronization::Ptp::ProfileType::SMPTE_ST2059_2_2021;
636630
}
637-
ptpJson.erase("profile");
638-
639-
std::optional<uint16_t> domain;
640-
OpenTrackIOHelpers::assignField(ptpJson, "domain", domain, "uint16", errors);
641-
if (!domain.has_value())
631+
else
642632
{
643-
errors.emplace_back("field: timing/synchronization/ptp/domain is required, however it is missing.");
644-
return std::nullopt;
633+
successfullyAssignedProfileField = false;
645634
}
646-
outSync.ptp->domain = domain.value();
635+
}
636+
else
637+
{
638+
errors.emplace_back("field: timing/synchronization/ptp/profile is required, however it is missing.");
639+
return std::nullopt;
640+
}
647641

648-
std::optional<std::string> leaderIdentity;
649-
const std::regex pattern{R"((?:^[0-9a-f]{2}(?::[0-9a-f]{2}){5}$)|(?:^[0-9a-f]{2}(?:-[0-9a-f]{2}){5}$))"};
650-
OpenTrackIOHelpers::assignRegexField(ptpJson, "leaderIdentity", leaderIdentity, pattern, errors);
642+
if (!successfullyAssignedProfileField)
643+
{
644+
errors.emplace_back("field: profile has an invalid string value.");
645+
return std::nullopt;
646+
}
647+
ptpJson.erase("profile");
651648

652-
if (!leaderIdentity.has_value())
653-
{
654-
errors.emplace_back("field: timing/synchronization/ptp/leaderIdentity is required, however it is missing.");
655-
return std::nullopt;
656-
}
657-
outSync.ptp->leaderIdentity = leaderIdentity.value();
649+
std::optional<uint16_t> domain;
650+
OpenTrackIOHelpers::assignField(ptpJson, "domain", domain, "uint16", errors);
651+
if (!domain.has_value())
652+
{
653+
errors.emplace_back("field: timing/synchronization/ptp/domain is required, however it is missing.");
654+
return std::nullopt;
655+
}
656+
outPtp.domain = domain.value();
658657

659-
std::optional<uint8_t> priority1;
660-
std::optional<uint8_t> priority2;
661-
constexpr auto leaderPrioritiesStr = "leaderPriorities";
662-
OpenTrackIOHelpers::assignField(ptpJson[leaderPrioritiesStr], "priority1", priority1, "uint8", errors);
663-
OpenTrackIOHelpers::assignField(ptpJson[leaderPrioritiesStr], "priority2", priority2, "uint8", errors);
658+
std::optional<std::string> leaderIdentity;
659+
const std::regex pattern{R"((?:^[0-9a-f]{2}(?::[0-9a-f]{2}){5}$)|(?:^[0-9a-f]{2}(?:-[0-9a-f]{2}){5}$))"};
660+
OpenTrackIOHelpers::assignRegexField(ptpJson, "leaderIdentity", leaderIdentity, pattern, errors);
664661

665-
if (!priority1.has_value() || !priority2.has_value())
666-
{
667-
errors.emplace_back("field: timing/synchronization/ptp/leaderPriorities is required, however it is missing a subfield(s).");
668-
return std::nullopt;
669-
}
670-
OpenTrackIOHelpers::clearFieldIfEmpty(ptpJson, leaderPrioritiesStr);
662+
if (!leaderIdentity.has_value())
663+
{
664+
errors.emplace_back("field: timing/synchronization/ptp/leaderIdentity is required, however it is missing.");
665+
return std::nullopt;
666+
}
667+
outPtp.leaderIdentity = leaderIdentity.value();
668+
669+
std::optional<uint8_t> priority1;
670+
std::optional<uint8_t> priority2;
671+
constexpr auto leaderPrioritiesStr = "leaderPriorities";
672+
OpenTrackIOHelpers::assignField(ptpJson[leaderPrioritiesStr], "priority1", priority1, "uint8", errors);
673+
OpenTrackIOHelpers::assignField(ptpJson[leaderPrioritiesStr], "priority2", priority2, "uint8", errors);
674+
675+
if (!priority1.has_value() || !priority2.has_value())
676+
{
677+
errors.emplace_back("field: timing/synchronization/ptp/leaderPriorities is required, however it is missing a subfield(s).");
678+
return std::nullopt;
679+
}
680+
OpenTrackIOHelpers::clearFieldIfEmpty(ptpJson, leaderPrioritiesStr);
681+
682+
outPtp.leaderPriorities = Synchronization::Ptp::LeaderPriorities{
683+
priority1.value(),
684+
priority2.value()
685+
};
686+
687+
std::optional<double> leaderAccuracy;
688+
OpenTrackIOHelpers::assignField(ptpJson, "leaderAccuracy", leaderAccuracy, "double", errors);
689+
if (!leaderAccuracy.has_value())
690+
{
691+
errors.emplace_back("field: timing/synchronization/ptp/leaderAccuracy is required, however it is missing.");
692+
return std::nullopt;
693+
}
694+
outPtp.leaderAccuracy = leaderAccuracy.value();
695+
696+
std::optional<double> meanPathDelay;
697+
OpenTrackIOHelpers::assignField(ptpJson, "meanPathDelay", meanPathDelay, "double", errors);
698+
if (!meanPathDelay.has_value())
699+
{
700+
errors.emplace_back("field: timing/synchronization/ptp/meanPathDelay is required, however it is missing.");
701+
return std::nullopt;
702+
}
703+
outPtp.meanPathDelay = meanPathDelay.value();
671704

672-
outSync.ptp->leaderPriorities = Synchronization::Ptp::LeaderPriorities{
673-
priority1.value(),
674-
priority2.value()
675-
};
705+
OpenTrackIOHelpers::assignField(ptpJson, "vlan", outPtp.vlan, "uint32", errors);
676706

677-
std::optional<double> leaderAccuracy;
678-
OpenTrackIOHelpers::assignField(ptpJson, "leaderAccuracy", leaderAccuracy, "double", errors);
679-
if (!leaderAccuracy.has_value())
707+
std::optional<std::string> leaderTimeSourceStr;
708+
OpenTrackIOHelpers::assignField(ptpJson, "leaderTimeSource", leaderTimeSourceStr, "string", errors);
709+
if (leaderTimeSourceStr.has_value())
710+
{
711+
if (leaderTimeSourceStr == "GNSS")
680712
{
681-
errors.emplace_back("field: timing/synchronization/ptp/leaderAccuracy is required, however it is missing.");
682-
return std::nullopt;
713+
outPtp.leaderTimeSource = Synchronization::Ptp::LeaderTimeSourceType::GNSS;
683714
}
684-
outSync.ptp->leaderAccuracy = leaderAccuracy.value();
685-
686-
std::optional<double> meanPathDelay;
687-
OpenTrackIOHelpers::assignField(ptpJson, "meanPathDelay", meanPathDelay, "double", errors);
688-
if (!meanPathDelay.has_value())
715+
else if (leaderTimeSourceStr == "Atomic clock")
689716
{
690-
errors.emplace_back("field: timing/synchronization/ptp/meanPathDelay is required, however it is missing.");
691-
return std::nullopt;
717+
outPtp.leaderTimeSource = Synchronization::Ptp::LeaderTimeSourceType::Atomic_clock;
692718
}
693-
outSync.ptp->meanPathDelay = meanPathDelay.value();
694-
695-
OpenTrackIOHelpers::assignField(ptpJson, "vlan", outSync.ptp->vlan, "uint32", errors);
696-
697-
std::optional<std::string> leaderTimeSourceStr;
698-
OpenTrackIOHelpers::assignField(ptpJson, "leaderTimeSource", leaderTimeSourceStr, "string", errors);
699-
if (leaderTimeSourceStr.has_value())
719+
else if (leaderTimeSourceStr == "NTP")
700720
{
701-
if (leaderTimeSourceStr == "GNSS")
702-
{
703-
outSync.ptp->leaderTimeSource = Synchronization::Ptp::LeaderTimeSourceType::GNSS;
704-
}
705-
else if (leaderTimeSourceStr == "Atomic clock")
706-
{
707-
outSync.ptp->leaderTimeSource = Synchronization::Ptp::LeaderTimeSourceType::Atomic_clock;
708-
}
709-
else if (leaderTimeSourceStr == "NTP")
710-
{
711-
outSync.ptp->leaderTimeSource = Synchronization::Ptp::LeaderTimeSourceType::NTP;
712-
}
721+
outPtp.leaderTimeSource = Synchronization::Ptp::LeaderTimeSourceType::NTP;
713722
}
714-
715-
OpenTrackIOHelpers::clearFieldIfEmpty(syncJson, "ptp");
716723
}
717724

718-
return outSync;
725+
return outPtp;
719726
}
720727

721728
std::optional<Tracker> Tracker::parse(nlohmann::json &json, std::vector<std::string>& errors)

0 commit comments

Comments
 (0)