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,64 @@
|
||||
#pragma once
|
||||
|
||||
#include "openpu/http.hpp"
|
||||
#include "openpu/models.hpp"
|
||||
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
|
||||
namespace openpu {
|
||||
|
||||
/// Backend endpoints. Defaults are those of the Peugeot build of the official application;
|
||||
/// every field can be overridden (other brands, or a local mock server).
|
||||
struct ApiConfig {
|
||||
std::string devicesUrl = "https://api.groupe-psa.com/applications/majesticf/v1/devices/";
|
||||
std::string updatesUrl = "https://api.groupe-psa.com/applications/majesticf/v1/getAvailableUpdate";
|
||||
std::string clientId = "20a4cf7c-f5fb-41d5-9175-a6e23b9880e5";
|
||||
|
||||
/// Defaults overridden by OPENPU_DEVICES_URL, OPENPU_UPDATES_URL and OPENPU_CLIENT_ID when set.
|
||||
static ApiConfig fromEnvironment();
|
||||
};
|
||||
|
||||
class ApiError : public std::runtime_error {
|
||||
public:
|
||||
enum class Kind {
|
||||
Network, ///< no HTTP response (offline, DNS, TLS, timeout)
|
||||
Server, ///< unexpected HTTP status
|
||||
BadResponse, ///< unparsable / unexpected body
|
||||
InvalidVin, ///< rejected locally before any request
|
||||
};
|
||||
ApiError(Kind kind, long status, const std::string& what) : std::runtime_error(what), kind_(kind), status_(status) {}
|
||||
Kind kind() const noexcept { return kind_; }
|
||||
long status() const noexcept { return status_; }
|
||||
|
||||
private:
|
||||
Kind kind_;
|
||||
long status_;
|
||||
};
|
||||
|
||||
class ApiClient {
|
||||
public:
|
||||
ApiClient(Transport& transport, ApiConfig config);
|
||||
|
||||
/// Does the VIN exist, and which head unit does it have?
|
||||
/// A 404 on the devices endpoint yields `exists == false` (not an error).
|
||||
VehicleCheck checkVehicle(const std::string& vin);
|
||||
|
||||
/// Latest firmware / map update for a vehicle whose type is already known.
|
||||
AvailableUpdates getUpdates(const std::string& vin, HeadUnitType type);
|
||||
|
||||
/// Raw calls, exposed for diagnostics (openpu-cli --raw) and tests.
|
||||
HttpResponse rawDevices(const std::string& vin);
|
||||
HttpResponse rawUpdates(const std::string& vin, const std::vector<std::string>& softwareTypes);
|
||||
|
||||
const ApiConfig& config() const noexcept { return config_; }
|
||||
|
||||
private:
|
||||
HttpResponse send(HttpRequest request);
|
||||
static void requireValidVin(const std::string& vin);
|
||||
|
||||
Transport& transport_;
|
||||
ApiConfig config_;
|
||||
};
|
||||
|
||||
} // namespace openpu
|
||||
@@ -0,0 +1,73 @@
|
||||
#pragma once
|
||||
|
||||
#include <chrono>
|
||||
#include <functional>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
namespace openpu {
|
||||
|
||||
enum class HttpMethod { Get, Head, Post };
|
||||
|
||||
struct HttpRequest {
|
||||
HttpMethod method = HttpMethod::Get;
|
||||
std::string url;
|
||||
std::vector<std::pair<std::string, std::string>> headers;
|
||||
std::string body;
|
||||
bool followRedirects = true;
|
||||
std::chrono::milliseconds connectTimeout{5000};
|
||||
/// Abort when no data was received for this long (0 = no limit).
|
||||
std::chrono::milliseconds stallTimeout{10000};
|
||||
/// Optional body sink: when set, the body is streamed here instead of being stored in the response.
|
||||
/// Returning false aborts the transfer.
|
||||
std::function<bool(const char* data, std::size_t size)> onData;
|
||||
/// Called once the status line and headers of the final response are known.
|
||||
/// Returning false aborts the transfer before any body is delivered.
|
||||
std::function<bool(long status, const std::map<std::string, std::string>& headers)> onHeaders;
|
||||
};
|
||||
|
||||
struct HttpResponse {
|
||||
long status = 0;
|
||||
std::map<std::string, std::string> headers; ///< lower-cased names, final response only
|
||||
std::string body;
|
||||
std::string effectiveUrl; ///< URL after redirects
|
||||
};
|
||||
|
||||
/// Transport-level failure (DNS, TLS, timeout, connection reset...). Equivalent to HTTP status 0.
|
||||
class NetworkError : public std::runtime_error {
|
||||
public:
|
||||
using std::runtime_error::runtime_error;
|
||||
};
|
||||
|
||||
/// Transfer aborted on purpose (by onData/onHeaders returning false, or cancel()).
|
||||
class TransferAborted : public std::runtime_error {
|
||||
public:
|
||||
using std::runtime_error::runtime_error;
|
||||
};
|
||||
|
||||
class Transport {
|
||||
public:
|
||||
virtual ~Transport() = default;
|
||||
/// Performs the request. Throws NetworkError / TransferAborted; HTTP errors are returned as status.
|
||||
virtual HttpResponse perform(const HttpRequest& request) = 0;
|
||||
};
|
||||
|
||||
struct CurlOptions {
|
||||
std::string userAgent; ///< empty: "OpenPU/<version>"
|
||||
bool verbose = false;
|
||||
};
|
||||
|
||||
/// libcurl-based transport. Honours the standard http_proxy/https_proxy/no_proxy variables.
|
||||
std::unique_ptr<Transport> makeCurlTransport(CurlOptions options = {});
|
||||
|
||||
/// Value of the `filename=` parameter of a Content-Disposition header (empty if none).
|
||||
std::string filenameFromContentDisposition(const std::string& header);
|
||||
|
||||
/// Last path segment of an URL, without query string or fragment, percent-decoded.
|
||||
std::string filenameFromUrl(const std::string& url);
|
||||
|
||||
} // namespace openpu
|
||||
@@ -0,0 +1,105 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <optional>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <vector>
|
||||
|
||||
namespace openpu {
|
||||
|
||||
/// Software types understood by the Majestic `getAvailableUpdate` endpoint.
|
||||
namespace software {
|
||||
inline constexpr std::string_view FirmwareNac = "ovip-int-firmware-version";
|
||||
inline constexpr std::string_view FirmwareRcc = "rcc-firmware";
|
||||
inline constexpr std::string_view FirmwareAio = "aio-firmware";
|
||||
inline constexpr std::string_view CronyNav = "crony2-software-nav";
|
||||
inline constexpr std::string_view CronyNonav = "crony2-software-nonav";
|
||||
inline constexpr std::string_view MapEurope = "map-eur";
|
||||
inline constexpr std::string_view MapCrony = "crony2-map-western_europe";
|
||||
|
||||
/// All software types, in the order used when probing a new vehicle.
|
||||
const std::vector<std::string>& all();
|
||||
} // namespace software
|
||||
|
||||
/// Head-unit family. String form matches the `type` field of the original vehicles.json.
|
||||
enum class HeadUnitType { Unknown, Nac, Rcc, Aio, CronyNav, CronyNonav };
|
||||
|
||||
std::string_view headUnitTypeName(HeadUnitType type);
|
||||
HeadUnitType headUnitTypeFromString(std::string_view s);
|
||||
|
||||
/// Thrown when a backend JSON document cannot be interpreted.
|
||||
class ParseError : public std::runtime_error {
|
||||
public:
|
||||
using std::runtime_error::runtime_error;
|
||||
};
|
||||
|
||||
struct UpdateInfo {
|
||||
std::string softwareType;
|
||||
std::string updateId;
|
||||
std::string version;
|
||||
std::string date;
|
||||
std::string url; ///< updateURL (redirects to the CDN)
|
||||
std::string licenseUrl; ///< licenseURL (only meaningful for firmware)
|
||||
std::uint64_t size = 0; ///< bytes, as announced by the backend
|
||||
};
|
||||
|
||||
struct SoftwareEntry {
|
||||
std::string softwareType;
|
||||
std::string requestResult;
|
||||
std::string currentVersion;
|
||||
std::vector<UpdateInfo> updates;
|
||||
};
|
||||
|
||||
struct UpdateListResponse {
|
||||
std::string requestResult;
|
||||
std::string installerUrl;
|
||||
std::string vin;
|
||||
std::vector<SoftwareEntry> software;
|
||||
};
|
||||
|
||||
struct DevicesResponse {
|
||||
std::string vin;
|
||||
std::string requestDate;
|
||||
std::vector<std::string> ecuTypes;
|
||||
};
|
||||
|
||||
struct VehicleCheck {
|
||||
bool exists = false;
|
||||
HeadUnitType type = HeadUnitType::Unknown;
|
||||
std::string guideKey; ///< i18n key of the installation guide (e.g. "path_guide_rcc"), may be empty
|
||||
};
|
||||
|
||||
struct AvailableUpdates {
|
||||
std::optional<UpdateInfo> firmware;
|
||||
std::optional<UpdateInfo> map;
|
||||
};
|
||||
|
||||
/// Firmware and (optional) map software types to request for a given head unit.
|
||||
struct SoftwarePair {
|
||||
std::string firmware;
|
||||
std::optional<std::string> map;
|
||||
};
|
||||
SoftwarePair softwareTypesFor(HeadUnitType type);
|
||||
|
||||
UpdateListResponse parseUpdateList(std::string_view json);
|
||||
DevicesResponse parseDevices(std::string_view json);
|
||||
std::string buildUpdateRequest(std::string_view vin, const std::vector<std::string>& softwareTypes);
|
||||
|
||||
/// Head-unit family from an ECU type string such as "NAC_EU_WAVE4" (Unknown if unsupported).
|
||||
HeadUnitType ecuFamily(std::string_view ecuType);
|
||||
|
||||
/// Reproduces the original vehicle check logic (devices + all-types update probe).
|
||||
VehicleCheck classifyVehicle(const UpdateListResponse& updates, const DevicesResponse& devices);
|
||||
|
||||
/// Usable update of a given software type, if any.
|
||||
std::optional<UpdateInfo> pickUpdate(const UpdateListResponse& response, std::string_view softwareType);
|
||||
|
||||
/// Firmware/map updates for a vehicle of the given type.
|
||||
AvailableUpdates selectUpdates(const UpdateListResponse& response, HeadUnitType type);
|
||||
|
||||
/// Basic absolute http(s) URL check, equivalent to the `is-url` package for our needs.
|
||||
bool isHttpUrl(std::string_view url);
|
||||
|
||||
} // namespace openpu
|
||||
@@ -0,0 +1,15 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
|
||||
namespace openpu {
|
||||
|
||||
/// Upper-cases and strips whitespace from a user-typed VIN.
|
||||
std::string normalizeVin(std::string_view input);
|
||||
|
||||
/// True when the VIN matches the format accepted by the Majestic backend:
|
||||
/// `^V[FR]3[A-Z0-9]{14}$` (17 characters, PSA/Stellantis WMI).
|
||||
bool isValidVin(std::string_view vin);
|
||||
|
||||
} // namespace openpu
|
||||
Reference in new issue
Block a user