Files
MicrOBU/microbu-esp32c5/external/vanetza-idf/vanetza/security/v3/geometry.cpp
T
Ashin Walpola 0e9525162d Keep the colleague's microbu-esp32c5 tree in this repository
obu-firmware builds against vanetza-idf from microbu-esp32c5/external, but
that tree was gitignored, so a clone of this repository could not build the
firmware it ships. It is now committed here as ordinary files in its own
folder, microbu-esp32c5/: the colleague's commit cf4b99f plus the V2X2MAP
bridge's signature verification (--trust) used on the bench. Nothing is
fetched from or pushed to the colleague's repository; this repository and
its remotes carry everything. The folder's own .gitignore keeps build output,
downloaded components and private key material out, as it did there; the
committed file set is identical to that repository's tracked files.

The ESP32-C5 is still flashed from obu-firmware/, which only takes
vanetza-idf from microbu-esp32c5/, so the two stay separate folders.
FLASHING.md says how to take a newer version of the colleague's tree (copy
it over the folder, rebuild, test, commit).
2026-09-23 17:46:40 +02:00

104 lines
3.8 KiB
C++

#include <vanetza/asn1/security_profile.hpp>
#include VANETZA_ASN1_SECURITY_HEADER(CircularRegion.h)
#include VANETZA_ASN1_SECURITY_HEADER(PolygonalRegion.h)
#include VANETZA_ASN1_SECURITY_HEADER(RectangularRegion.h)
#include VANETZA_ASN1_SECURITY_HEADER(SequenceOfRectangularRegion.h)
#include VANETZA_ASN1_SECURITY_HEADER(TwoDLocation.h)
#include <vanetza/common/position_fix.hpp>
#include <vanetza/security/v3/boost_geometry.hpp>
#include <vanetza/security/v3/distance.hpp>
#include <vanetza/security/v3/geometry.hpp>
#include <boost/geometry/algorithms/within.hpp>
#include <boost/units/cmath.hpp>
namespace vanetza
{
namespace security
{
namespace v3
{
bool is_valid(const asn1::TwoDLocation& location)
{
return location.latitude >= Vanetza_Security_NinetyDegreeInt_min &&
location.latitude <= Vanetza_Security_NinetyDegreeInt_max &&
location.longitude >= Vanetza_Security_OneEightyDegreeInt_min &&
location.longitude <= Vanetza_Security_OneEightyDegreeInt_max;
}
bool is_inside(const PositionFix& location, const asn1::CircularRegion& region)
{
if (is_valid(region.center) && region.radius >= 0) {
return distance(location, region.center) <= region.radius * units::si::meter;
} else {
return false;
}
}
bool is_inside(const PositionFix& location, const asn1::SequenceOfRectangularRegion& regions)
{
for (int i = 0; i < regions.list.count; ++i) {
if (regions.list.array[i] != nullptr && is_inside(location, *regions.list.array[i])) {
return true;
}
}
return false;
}
bool is_inside(const PositionFix& location, const asn1::RectangularRegion& region)
{
const bool location_valid = isfinite(location.latitude) && isfinite(location.longitude);
const bool region_valid = is_valid(region.northWest) && is_valid(region.southEast);
if (location_valid && region_valid) {
if (region.northWest.latitude <= region.southEast.latitude) {
// north-west is equal or south of south-east: invalid region
return false;
} else if (region.northWest.longitude == region.southEast.longitude) {
// equal longitudes are invalid
return false;
} else {
static constexpr long scale_90deg = Vanetza_Security_NinetyDegreeInt_max / 90;
static constexpr long scale_180deg = Vanetza_Security_OneEightyDegreeInt_max / 180;
Vanetza_Security_NinetyDegreeInt_t loc_lat = location.latitude / units::degree * scale_90deg;
Vanetza_Security_OneEightyDegreeInt_t loc_lon = location.longitude / units::degree * scale_180deg;
if (loc_lat >= region.southEast.latitude && loc_lat <= region.northWest.latitude) {
if (region.northWest.longitude < region.southEast.longitude) {
return loc_lon >= region.northWest.longitude && loc_lon <= region.southEast.longitude;
} else {
return loc_lon >= region.northWest.longitude || loc_lon <= region.southEast.longitude;
}
}
}
}
return false;
}
bool is_inside(const asn1::TwoDLocation* location, const asn1::PolygonalRegion* region)
{
if (region && location) {
PolygonalRegionRingAdapter polygonal_region(*region);
auto point = make_model(*location);
return boost::geometry::within(point, polygonal_region);
}
return false;
}
bool is_inside(const PositionFix& location, const asn1::PolygonalRegion& region)
{
if (isfinite(location.latitude) && isfinite(location.longitude)) {
PolygonalRegionRingAdapter polygonal_region(region);
auto point = make_model(location);
return boost::geometry::within(point, polygonal_region);
}
return false;
}
} // namespace v3
} // namespace security
} // namespace vanetza