core: client API Majestic, modèles et CLI check
- libopenpu : validation VIN, modèles JSON (getAvailableUpdate, devices), classification ecuType -> type/guide, sélection des mises à jour - couche Transport abstraite + implémentation libcurl (proxy env, redirections, streaming, timeout d'inactivité) - tolérance au EOF TLS sans close_notify de la passerelle Majestic, limitée aux corps délimités par la fermeture (ni Content-Length ni chunked) - openpu-cli : check / raw-devices / raw-updates - tests doctest (VIN, parsing, classification, client API avec faux transport), fixtures dont une capture réelle anonymisée (NAC wave 2) - qualité : presets CMake (debug, asan, tidy), .clang-tidy, tools/check.sh (tests, ASan/UBSan, valgrind, clang-tidy, cppcheck) - validé contre l'API réelle sur 3 véhicules (NAC w4, RCC, NAC w2) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01WDASLCiN764v685boRVYar
This commit is contained in:
1 parent
660a420a3e
commit
95704f9882
28 files changed
+1877
-2
No files matched your search
@@ -0,0 +1,304 @@
|
||||
#include "openpu/models.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cctype>
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
namespace openpu {
|
||||
|
||||
using json = nlohmann::json;
|
||||
|
||||
namespace software {
|
||||
const std::vector<std::string>& all()
|
||||
{
|
||||
static const std::vector<std::string> types{
|
||||
std::string(FirmwareNac), std::string(FirmwareRcc), std::string(FirmwareAio),
|
||||
std::string(CronyNav), std::string(CronyNonav), std::string(MapEurope),
|
||||
std::string(MapCrony),
|
||||
};
|
||||
return types;
|
||||
}
|
||||
} // namespace software
|
||||
|
||||
std::string_view headUnitTypeName(HeadUnitType type)
|
||||
{
|
||||
switch (type) {
|
||||
case HeadUnitType::Nac: return "nac";
|
||||
case HeadUnitType::Rcc: return "rcc";
|
||||
case HeadUnitType::Aio: return "aio";
|
||||
case HeadUnitType::CronyNav: return "crony_nav";
|
||||
case HeadUnitType::CronyNonav: return "crony_nonav";
|
||||
case HeadUnitType::Unknown: break;
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
HeadUnitType headUnitTypeFromString(std::string_view s)
|
||||
{
|
||||
for (auto t : {HeadUnitType::Nac, HeadUnitType::Rcc, HeadUnitType::Aio, HeadUnitType::CronyNav,
|
||||
HeadUnitType::CronyNonav}) {
|
||||
if (headUnitTypeName(t) == s)
|
||||
return t;
|
||||
}
|
||||
return HeadUnitType::Unknown;
|
||||
}
|
||||
|
||||
SoftwarePair softwareTypesFor(HeadUnitType type)
|
||||
{
|
||||
switch (type) {
|
||||
case HeadUnitType::Nac: return {std::string(software::FirmwareNac), std::string(software::MapEurope)};
|
||||
case HeadUnitType::Rcc: return {std::string(software::FirmwareRcc), std::string(software::MapEurope)};
|
||||
case HeadUnitType::CronyNav: return {std::string(software::CronyNav), std::string(software::MapCrony)};
|
||||
case HeadUnitType::CronyNonav: return {std::string(software::CronyNonav), std::nullopt};
|
||||
case HeadUnitType::Aio:
|
||||
case HeadUnitType::Unknown: break;
|
||||
}
|
||||
// Same fallback as the original application (AIO is the default branch).
|
||||
return {std::string(software::FirmwareAio), std::string(software::MapEurope)};
|
||||
}
|
||||
|
||||
namespace {
|
||||
|
||||
std::string upper(std::string_view s)
|
||||
{
|
||||
std::string out(s);
|
||||
std::transform(out.begin(), out.end(), out.begin(),
|
||||
[](unsigned char c) { return static_cast<char>(std::toupper(c)); });
|
||||
return out;
|
||||
}
|
||||
|
||||
/// String field that may be absent, null, a string or a number.
|
||||
std::string str(const json& obj, const char* key)
|
||||
{
|
||||
auto it = obj.find(key);
|
||||
if (it == obj.end() || it->is_null())
|
||||
return {};
|
||||
if (it->is_string())
|
||||
return it->get<std::string>();
|
||||
if (it->is_number_unsigned())
|
||||
return std::to_string(it->get<std::uint64_t>());
|
||||
if (it->is_number_integer())
|
||||
return std::to_string(it->get<std::int64_t>());
|
||||
return it->dump();
|
||||
}
|
||||
|
||||
std::uint64_t u64(const json& obj, const char* key)
|
||||
{
|
||||
auto it = obj.find(key);
|
||||
if (it == obj.end() || it->is_null())
|
||||
return 0;
|
||||
if (it->is_number_unsigned() || it->is_number_integer())
|
||||
return it->get<std::uint64_t>();
|
||||
if (it->is_number_float())
|
||||
return static_cast<std::uint64_t>(it->get<double>());
|
||||
if (it->is_string()) {
|
||||
const auto s = it->get<std::string>();
|
||||
try {
|
||||
return std::stoull(s);
|
||||
} catch (const std::exception&) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
json parseObject(std::string_view text)
|
||||
{
|
||||
json doc = json::parse(text, nullptr, /*allow_exceptions=*/false);
|
||||
if (doc.is_discarded() || !doc.is_object())
|
||||
throw ParseError("invalid JSON document");
|
||||
return doc;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
UpdateListResponse parseUpdateList(std::string_view text)
|
||||
{
|
||||
const json doc = parseObject(text);
|
||||
UpdateListResponse r;
|
||||
r.requestResult = str(doc, "requestResult");
|
||||
r.installerUrl = str(doc, "installerURL");
|
||||
r.vin = str(doc, "vin");
|
||||
auto sw = doc.find("software");
|
||||
if (sw != doc.end() && sw->is_array()) {
|
||||
for (const auto& s : *sw) {
|
||||
if (!s.is_object())
|
||||
continue;
|
||||
SoftwareEntry e;
|
||||
e.softwareType = str(s, "softwareType");
|
||||
e.requestResult = str(s, "updateRequestResult");
|
||||
e.currentVersion = str(s, "currentSoftwareVersion");
|
||||
auto up = s.find("update");
|
||||
if (up != s.end() && up->is_array()) {
|
||||
for (const auto& u : *up) {
|
||||
if (!u.is_object())
|
||||
continue;
|
||||
UpdateInfo info;
|
||||
info.softwareType = e.softwareType;
|
||||
info.updateId = str(u, "updateId");
|
||||
info.version = str(u, "updateVersion");
|
||||
info.date = str(u, "updateDate");
|
||||
info.url = str(u, "updateURL");
|
||||
info.licenseUrl = str(u, "licenseURL");
|
||||
info.size = u64(u, "updateSize");
|
||||
e.updates.push_back(std::move(info));
|
||||
}
|
||||
}
|
||||
r.software.push_back(std::move(e));
|
||||
}
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
DevicesResponse parseDevices(std::string_view text)
|
||||
{
|
||||
const json doc = parseObject(text);
|
||||
DevicesResponse r;
|
||||
r.vin = str(doc, "vin");
|
||||
r.requestDate = str(doc, "requestDate");
|
||||
auto dev = doc.find("devices");
|
||||
if (dev != doc.end() && dev->is_array()) {
|
||||
for (const auto& d : *dev) {
|
||||
if (d.is_object())
|
||||
r.ecuTypes.push_back(str(d, "ecuType"));
|
||||
}
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
std::string buildUpdateRequest(std::string_view vin, const std::vector<std::string>& softwareTypes)
|
||||
{
|
||||
json body;
|
||||
body["vin"] = std::string(vin);
|
||||
body["softwareTypes"] = json::array();
|
||||
for (const auto& t : softwareTypes)
|
||||
body["softwareTypes"].push_back({{"softwareType", t}});
|
||||
return body.dump();
|
||||
}
|
||||
|
||||
HeadUnitType ecuFamily(std::string_view ecuType)
|
||||
{
|
||||
const std::string prefix = upper(ecuType.substr(0, ecuType.find('_')));
|
||||
if (prefix == "NAC")
|
||||
return HeadUnitType::Nac;
|
||||
if (prefix == "RCC")
|
||||
return HeadUnitType::Rcc;
|
||||
if (prefix == "AIO")
|
||||
return HeadUnitType::Aio;
|
||||
if (prefix.compare(0, 5, "CRONY") == 0)
|
||||
return HeadUnitType::CronyNav; // nav/nonav is decided from the available software
|
||||
return HeadUnitType::Unknown;
|
||||
}
|
||||
|
||||
namespace {
|
||||
|
||||
std::string guideKeyFor(std::string_view ecuType)
|
||||
{
|
||||
switch (ecuFamily(ecuType)) {
|
||||
case HeadUnitType::Rcc: return "path_guide_rcc";
|
||||
case HeadUnitType::Aio: return "path_guide_aio";
|
||||
case HeadUnitType::Nac: {
|
||||
const std::string last = upper(ecuType.substr(ecuType.rfind('_') + 1));
|
||||
if (last.size() == 5 && last.compare(0, 4, "WAVE") == 0 && last[4] >= '1' && last[4] <= '4')
|
||||
return std::string("path_guide_wave_") + last[4];
|
||||
return "path_guide_wave_default";
|
||||
}
|
||||
case HeadUnitType::CronyNav:
|
||||
case HeadUnitType::CronyNonav: {
|
||||
for (char v : {'1', '2', '3', '4'}) {
|
||||
if (ecuType == std::string("CRONY2_EU_VAR") + v + "_WAVE1")
|
||||
return std::string("path_guide_crony_") + v;
|
||||
}
|
||||
return {};
|
||||
}
|
||||
case HeadUnitType::Unknown: break;
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
VehicleCheck classifyVehicle(const UpdateListResponse& updates, const DevicesResponse& devices)
|
||||
{
|
||||
std::vector<std::string> valid;
|
||||
for (const auto& e : devices.ecuTypes) {
|
||||
if (!e.empty() && ecuFamily(e) != HeadUnitType::Unknown)
|
||||
valid.push_back(e);
|
||||
}
|
||||
if (updates.requestResult != "OK" || valid.empty())
|
||||
return {};
|
||||
|
||||
VehicleCheck result;
|
||||
result.exists = true;
|
||||
// A guide link (and therefore a type) is only determined when exactly one valid ECU is present.
|
||||
if (valid.size() != 1)
|
||||
return result;
|
||||
const std::string guide = guideKeyFor(valid.front());
|
||||
if (guide.empty())
|
||||
return result;
|
||||
|
||||
result.guideKey = guide;
|
||||
result.type = ecuFamily(valid.front());
|
||||
if (result.type == HeadUnitType::CronyNav) {
|
||||
const bool hasNonav = std::any_of(updates.software.begin(), updates.software.end(), [](const SoftwareEntry& s) {
|
||||
return s.softwareType == software::CronyNonav && !s.updates.empty() && !s.updates.front().updateId.empty();
|
||||
});
|
||||
if (hasNonav)
|
||||
result.type = HeadUnitType::CronyNonav;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
bool isHttpUrl(std::string_view url)
|
||||
{
|
||||
std::string_view rest;
|
||||
if (url.substr(0, 8) == "https://")
|
||||
rest = url.substr(8);
|
||||
else if (url.substr(0, 7) == "http://")
|
||||
rest = url.substr(7);
|
||||
else
|
||||
return false;
|
||||
const auto hostEnd = rest.find_first_of("/?#");
|
||||
const auto host = rest.substr(0, hostEnd);
|
||||
if (host.empty())
|
||||
return false;
|
||||
return std::none_of(host.begin(), host.end(), [](char c) { return std::isspace(static_cast<unsigned char>(c)) != 0; });
|
||||
}
|
||||
|
||||
std::optional<UpdateInfo> pickUpdate(const UpdateListResponse& response, std::string_view softwareType)
|
||||
{
|
||||
auto it = std::find_if(response.software.begin(), response.software.end(),
|
||||
[&](const SoftwareEntry& s) { return s.softwareType == softwareType; });
|
||||
if (it == response.software.end() || it->requestResult != "OK" || it->updates.empty())
|
||||
return std::nullopt;
|
||||
|
||||
// The original app's sort is a no-op (it subtracts strings), so it effectively uses update[0].
|
||||
// We keep that, except when every date looks like an ISO-8601 timestamp of the same shape:
|
||||
// then the most recent one is chosen.
|
||||
const auto& ups = it->updates;
|
||||
const UpdateInfo* chosen = &ups.front();
|
||||
const auto isoLike = [&](const UpdateInfo& u) {
|
||||
return u.date.size() >= 10 && u.date.size() == ups.front().date.size() && u.date[4] == '-' && u.date[7] == '-';
|
||||
};
|
||||
if (ups.size() > 1 && std::all_of(ups.begin(), ups.end(), isoLike)) {
|
||||
chosen = &*std::max_element(ups.begin(), ups.end(),
|
||||
[](const UpdateInfo& a, const UpdateInfo& b) { return a.date < b.date; });
|
||||
}
|
||||
if (!isHttpUrl(chosen->url))
|
||||
return std::nullopt;
|
||||
return *chosen;
|
||||
}
|
||||
|
||||
AvailableUpdates selectUpdates(const UpdateListResponse& response, HeadUnitType type)
|
||||
{
|
||||
AvailableUpdates out;
|
||||
if (response.requestResult != "OK" || response.software.empty())
|
||||
return out;
|
||||
const SoftwarePair types = softwareTypesFor(type);
|
||||
out.firmware = pickUpdate(response, types.firmware);
|
||||
if (types.map)
|
||||
out.map = pickUpdate(response, *types.map);
|
||||
return out;
|
||||
}
|
||||
|
||||
} // namespace openpu
|
||||
Reference in new issue
Block a user