diff --git a/Makefile b/Makefile index f7af272..76c5097 100644 --- a/Makefile +++ b/Makefile @@ -85,7 +85,7 @@ aa-supervisor: $(SUPERVISOR_OBJ) # iDrive PluginApp (installed in /opt/hmi/data/ro/Plugins/EntryAad, loaded by HmiMain3D at boot). Its objects are # position independent and hidden: HmiMain3D only looks up pluginAppInfo, the one symbol the library exports. HMI_SRC = hmiapp/entry_aa_app.cpp hmiapp/bmw_framework.cpp hmiapp/app_log.cpp hmiapp/app_guards.cpp \ - hmiapp/service_link.cpp hmiapp/car_signals.cpp hmiapp/hold_machine.cpp hmiapp/controller_coding.cpp + hmiapp/service_link.cpp hmiapp/car_signals.cpp hmiapp/hold_machine.cpp hmiapp/idrive_coding.cpp HMI_OBJ = $(HMI_SRC:.cpp=.o) HMI_LIBDIRS = -L$(SYSROOT)/opt/hmi/lib -Wl,-rpath-link,$(SYSROOT)/opt/hmi/lib hmiapp: hmiapp/libEntryAad.so diff --git a/core/aa_channels.cpp b/core/aa_channels.cpp index 260d9b8..2b52298 100644 --- a/core/aa_channels.cpp +++ b/core/aa_channels.cpp @@ -173,8 +173,8 @@ void AASession::onTlsHandshake(const Message& message) { // The description of this car: Google's Desktop Head Unit template, adapted by core/sdr_builder.cpp. void AASession::onDiscovery() { printf("[AASession] >>> Received ServiceDiscoveryRequest from phone! Sending ServiceDiscoveryResponse...\n"); - const Message response = message::discoveryResponse( - buildCarServiceDiscovery(kDhuSdpProtobuf, kDhuSdpProtobufLen, vehicleFile_, softwareVersion_, display_)); + const Message response = message::discoveryResponse(buildCarServiceDiscovery( + kDhuSdpProtobuf, kDhuSdpProtobufLen, vehicleFile_, softwareVersion_, display_, carMake_)); if (sendSealed(channel::kControl, response)) { printf("[AASession] >>> Sent ServiceDiscoveryResponse (%u bytes)\n", static_cast(response.size())); } @@ -253,6 +253,7 @@ void AASession::onSetup(uint8_t channel) { if (channel == channel::kVideo) { sendVideoFocus(true, false); videoReady_ = true; + keyframe_.videoStarted(locationClockMs()); } } diff --git a/core/aa_session.cpp b/core/aa_session.cpp index c2c0f21..9cb48f0 100644 --- a/core/aa_session.cpp +++ b/core/aa_session.cpp @@ -37,6 +37,9 @@ const unsigned kFramesLoggedEvery = 300; const unsigned kKeysLogged = 200; // evo_knob logs every key anyway const unsigned kMovesLogged = 40; const unsigned kMicrophoneLogEvery = 250; +// Encrypted frames in a row that do not decrypt: the TLS stream is broken (a record lost or cut on the way), and no +// later frame will decrypt either. The session ends rather than wait for the silence limit with a frozen picture. +const unsigned kUnreadableLimit = 16; const int kTouchMove = 2; uint64_t monotonicUs() { @@ -61,9 +64,12 @@ AASession::AASession() thread_(0), vehicleFile_(nullptr), softwareVersion_(nullptr), + carMake_(nullptr), longestSilenceMs_(0), lastReceivedMs_(0), videoReady_(false), + projected_(false), + unreadableInARow_(0), mediaMessages_(0), microphoneChunks_(0) { pthread_mutex_init(&lifecycleLock_, nullptr); @@ -125,6 +131,10 @@ void AASession::postVideoFocus(bool projected) { inbox_.postFocus(projected); } +void AASession::postKeyframeRequest() { + inbox_.postKeyframeRequest(); +} + void AASession::postNightMode(bool night) { inbox_.postNight(night); } @@ -211,6 +221,9 @@ bool AASession::start(int client_fd) { assembler_.clear(); inbox_.reset(); videoReady_ = false; + projected_ = false; + keyframe_.reset(); + unreadableInARow_ = 0; microphoneChunks_ = 0; mediaMessages_ = 0; memset(mediaSessions_, 0, sizeof(mediaSessions_)); @@ -441,7 +454,14 @@ AASession::Received AASession::receive(uint8_t& channel, Message& message) { } else if (!tls_.open(&payload[0], payload.size(), plain)) { printf("[AASession] Failed to decrypt encrypted frame (%u bytes).\n", header.length); assembler_.drop(channel); + if (++unreadableInARow_ >= kUnreadableLimit) { + printf("[AASession] %u encrypted frames in a row did not decrypt: the stream is broken, session ends.\n", + unreadableInARow_); + return kLost; + } return kNothing; + } else { + unreadableInARow_ = 0; } switch (assembler_.add(header, plain, message)) { case MessageAssembler::kComplete: @@ -497,6 +517,9 @@ void AASession::sendCarInput() { printf("[AASession] video focus -> phone: %s (unrequested) %s\n", projected ? "FOCUSED/projected" : "UNFOCUSED/native", sent ? "sent" : "SEND FAILED"); } + if (batch.keyframe) { + askKeyframe(); + } for (size_t i = 0; i < batch.keys.size(); ++i) { const CarKey& key = batch.keys[i]; const Message event = key.rotary ? message::rotaryEvent(monotonicUs(), key.keycode, key.delta) @@ -566,10 +589,22 @@ bool AASession::sendSensorEvent(uint32_t type) { bool AASession::sendVideoFocus(bool projected, bool unrequested) { const bool sent = sendSealed(channel::kVideo, message::videoFocusIndication(projected, unrequested)); if (sent) { + projected_ = projected; printf("[AASession] >>> Sent VideoFocusIndication (%s%s) on Channel %u!\n", projected ? "FOCUSED" : "UNFOCUSED", unrequested ? ", unrequested" : "", channel::kVideo); } return sent; } +// A lost frame leaves the picture broken until the phone's next keyframe: the focus is taken away and given back at +// once, which restarts the phone's encoder with one (keyframe_request.hpp). Only while the picture is projected. +void AASession::askKeyframe() { + if (!videoReady_ || !projected_ || !keyframe_.allow(locationClockMs())) { + return; + } + const bool sent = sendVideoFocus(false, true) && sendVideoFocus(true, true); + printf("[AASession] video frame lost: keyframe asked of the phone (focus off and on) %s\n", + sent ? "sent" : "SEND FAILED"); +} + } // namespace aa diff --git a/core/aa_session.hpp b/core/aa_session.hpp index e814257..84f36e2 100644 --- a/core/aa_session.hpp +++ b/core/aa_session.hpp @@ -17,6 +17,7 @@ #include "car_inbox.hpp" #include "car_location.hpp" #include "display_plan.hpp" +#include "keyframe_request.hpp" namespace aa { @@ -65,6 +66,8 @@ class AASession { void setSoftwareVersion(const char* version) { softwareVersion_ = version; } // The panel and the canvas asked of the phone for it (display_plan.hpp). The reference car's by default. void setDisplay(const DisplayPlan& display) { display_ = display; } + // The maker announced to the phone, as iDrive is coded ("MINI"); kept by the caller. Null: "BMW". + void setCarMake(const char* make) { carMake_ = make; } // Car input for the phone, from any thread: queued, then sent by the session thread. void postTouchEvent(const TouchEventOut& ev); @@ -72,6 +75,8 @@ class AASession { void postRelativeEvent(uint32_t keycode, int delta); // rotary steps (keycode 65536, ROTARY_CONTROLLER) // The car takes (false) or gives back (true) the screen: an unrequested video focus indication. void postVideoFocus(bool projected); + // The decoder lost a video frame. Any thread; the session asks the phone for a keyframe when it may. + void postKeyframeRequest(); // The car's day or night, kept across sessions: a NIGHT_MODE event once the phone started that sensor. void postNightMode(bool night); // The car's latest fix (monotonic capture time); an invalid one when the car's feed expired. @@ -114,6 +119,7 @@ class AASession { bool sendLocation(); bool sendSensorEvent(uint32_t type); bool sendVideoFocus(bool projected, bool unrequested); + void askKeyframe(); // One handler per route (aa_channels.cpp); dispatch() is false when the session must end. bool dispatch(uint8_t channel, const Message& message); @@ -150,6 +156,7 @@ class AASession { Callback navigation_; const char* vehicleFile_; const char* softwareVersion_; + const char* carMake_; DisplayPlan display_; CarInbox inbox_; @@ -158,7 +165,10 @@ class AASession { uint64_t lastReceivedMs_; // when the phone last sent anything, 0 before its first byte TlsClient tls_; MessageAssembler assembler_; - bool videoReady_; // the phone set the video channel up: a focus can go + bool videoReady_; // the phone set the video channel up: a focus can go + bool projected_; // the last video focus sent was FOCUSED + KeyframeRequest keyframe_; + unsigned unreadableInARow_; // encrypted frames that did not decrypt since the last one that did uint32_t mediaSessions_[32]; // per channel, the session id of its start indication, echoed in our acks uint32_t mediaMessages_; // media messages received, every channel uint32_t audioMessages_[2]; // guidance (8), media (9) diff --git a/core/car_inbox.cpp b/core/car_inbox.cpp index 8fbe0ee..4276934 100644 --- a/core/car_inbox.cpp +++ b/core/car_inbox.cpp @@ -32,6 +32,7 @@ CarInbox::CarInbox() keyDrops_(0), rotaryDrops_(0), focus_(kFocusNone), + keyframe_(false), night_(-1), nightStarted_(false), nightChanged_(false), @@ -134,6 +135,18 @@ void CarInbox::postFocus(bool projected) { wake(); } +void CarInbox::postKeyframeRequest() { + bool first; + { + MutexLock lock(mutex_); + first = !keyframe_; + keyframe_ = true; + } + if (first) { // a stalled decoder loses frame after frame: one wake is enough + wake(); + } +} + void CarInbox::postNight(bool night) { bool due; { @@ -188,6 +201,8 @@ void CarInbox::take(CarBatch& batch) { batch.keys.swap(keys_); batch.focus = focus_; focus_ = kFocusNone; + batch.keyframe = keyframe_; + keyframe_ = false; batch.nightChanged = nightChanged_ && nightStarted_; nightChanged_ = false; batch.microphone.swap(microphone_); @@ -248,6 +263,7 @@ void CarInbox::reset() { keys_.clear(); keyDrops_ = rotaryDrops_ = 0; focus_ = kFocusNone; + keyframe_ = false; nightStarted_ = nightChanged_ = false; location_.reset(); microphoneOpen_ = false; diff --git a/core/car_inbox.hpp b/core/car_inbox.hpp index 9bb2000..78eeac6 100644 --- a/core/car_inbox.hpp +++ b/core/car_inbox.hpp @@ -30,6 +30,7 @@ struct CarBatch { std::vector touches; std::vector keys; FocusRequest focus; + bool keyframe; // the decoder lost a video frame since the last turn bool nightChanged; // the phone started night mode and the car changed since the last event std::vector> microphone; }; @@ -50,6 +51,7 @@ class CarInbox { void postKey(uint32_t keycode, bool down); void postRotary(uint32_t keycode, int delta); void postFocus(bool projected); + void postKeyframeRequest(); // a video frame was lost: the session decides whether to ask the phone void postNight(bool night); // kept across sessions void postLocation(const CarLocation& location); void postMicrophone(const uint8_t* pcm, size_t size); @@ -83,6 +85,7 @@ class CarInbox { std::vector keys_; unsigned keyDrops_, rotaryDrops_; FocusRequest focus_; + bool keyframe_; int night_; bool nightStarted_, nightChanged_; LocationFeed location_; diff --git a/core/display_plan.hpp b/core/display_plan.hpp index a278004..9130666 100644 --- a/core/display_plan.hpp +++ b/core/display_plan.hpp @@ -6,6 +6,8 @@ // asked for; the phone is told to keep its UI in the top-left part the panel shows (insets, core/sdr_builder.cpp), // and the video surface shows that part only (platform/evo_video.cpp). The reference car is one case of the rule: a // 1280x480 panel in the 1280x720 canvas, its top 480 rows shown. +// A panel partly hidden by its surround (the MINI's round one, platform/car_brand.hpp) keeps the whole picture; the +// phone is only told to keep its UI away from the hidden sides. #ifndef ENTRY_AAD_CORE_DISPLAY_PLAN_HPP #define ENTRY_AAD_CORE_DISPLAY_PLAN_HPP @@ -17,10 +19,12 @@ struct DisplayPlan { uint32_t width, height; // the panel, and the part of the canvas that is shown uint32_t canvasWidth, canvasHeight; // what the phone streams uint32_t resolution; // the canvas as the protocol numbers it: 1, 2 or 3 + uint32_t hiddenSide; // panel columns hidden on each side by the surround // The reference car. - DisplayPlan() : width(1280), height(480), canvasWidth(1280), canvasHeight(720), resolution(2) {} + DisplayPlan() : width(1280), height(480), canvasWidth(1280), canvasHeight(720), resolution(2), hiddenSide(0) {} uint32_t bottomInset() const { return canvasHeight - height; } - uint32_t rightInset() const { return canvasWidth - width; } + uint32_t leftInset() const { return hiddenSide; } + uint32_t rightInset() const { return canvasWidth - width + hiddenSide; } bool reference() const { return width == 1280 && height == 480; } }; @@ -37,6 +41,7 @@ inline bool planDisplay(uint32_t panelWidth, uint32_t panelHeight, DisplayPlan& out.canvasWidth = kCanvas[i][0]; out.canvasHeight = kCanvas[i][1]; out.resolution = i + 1; + out.hiddenSide = 0; return true; } } diff --git a/core/keyframe_request.hpp b/core/keyframe_request.hpp new file mode 100644 index 0000000..3e9859b --- /dev/null +++ b/core/keyframe_request.hpp @@ -0,0 +1,50 @@ +// Copyright (c) 2026 Jiraiya43 +// SPDX-License-Identifier: PolyForm-Noncommercial-1.0.0 +// When the head unit may ask the phone for a new keyframe after the decoder lost a video frame. +#ifndef ENTRY_AAD_CORE_KEYFRAME_REQUEST_HPP +#define ENTRY_AAD_CORE_KEYFRAME_REQUEST_HPP + +#include + +namespace aa { + +// H.264 frames between two keyframes refer to the ones before them: once one is lost, the picture stays broken +// until the phone's next keyframe, which can be seconds away. Android Auto has no message to ask for one, but a +// phone that gets the video focus back starts its encoder again with a keyframe; the session takes the focus away +// and gives it back at once (aa_session.cpp). +// Not during the first seconds of the video, when the phone's own keyframes are still coming, and seldom: a decoder +// short of buffers would otherwise turn every frame into a keyframe, each one larger than the frames it replaces. +// Session thread only; the clock is the caller's, in milliseconds. +class KeyframeRequest { +public: + static const uint64_t kSettleMs = 5000; // after the video was set up + static const uint64_t kSpacingMs = 10000; // between two requests + + KeyframeRequest() : videoSinceMs_(0), lastMs_(0), video_(false), asked_(false) {} + + // The phone set the video channel up: a new stream, nothing asked yet. + void videoStarted(uint64_t nowMs) { + video_ = true; + videoSinceMs_ = nowMs; + asked_ = false; + } + // A new session. + void reset() { video_ = asked_ = false; } + // A frame was lost: true when a request may go now, and it then counts as made. + bool allow(uint64_t nowMs) { + if (!video_ || nowMs - videoSinceMs_ < kSettleMs || (asked_ && nowMs - lastMs_ < kSpacingMs)) { + return false; + } + asked_ = true; + lastMs_ = nowMs; + return true; + } + +private: + uint64_t videoSinceMs_, lastMs_; + bool video_, asked_; +}; + +} // namespace aa + +#endif // ENTRY_AAD_CORE_KEYFRAME_REQUEST_HPP diff --git a/core/sdr_builder.cpp b/core/sdr_builder.cpp index 8d6afad..46755e7 100644 --- a/core/sdr_builder.cpp +++ b/core/sdr_builder.cpp @@ -128,9 +128,9 @@ bool tuneVideoConfig(const pb::Field& config, Bytes& out, const DisplayPlan& dis return false; } Bytes insets; - pb::appendUint(insets, 1, 0); + pb::appendUint(insets, 1, 0); // top pb::appendUint(insets, 2, display.bottomInset()); - pb::appendUint(insets, 3, 0); + pb::appendUint(insets, 3, display.leftInset()); pb::appendUint(insets, 4, display.rightInset()); Bytes rebuilt; pb::appendMessage(rebuilt, kInsetsField, insets); @@ -318,11 +318,15 @@ void setDiscoveryTrim(DiscoveryTrim trim) { } std::vector buildCarServiceDiscovery(const uint8_t* base, size_t baseLen, const char* vehicleFile, - const char* softwareVersion, const DisplayPlan& display) { + const char* softwareVersion, const DisplayPlan& display, + const char* make) { SdrIdentity identity; if (vehicleFile) { identity.loadVehicleFile(vehicleFile); } + if (make && *make) { + identity.carMake = identity.huMake = make; + } if (softwareVersion && *softwareVersion) { identity.swVersion = softwareVersion; } diff --git a/core/sdr_builder.hpp b/core/sdr_builder.hpp index 34d7f3f..ae53b03 100644 --- a/core/sdr_builder.hpp +++ b/core/sdr_builder.hpp @@ -53,9 +53,11 @@ void setDiscoveryTrim(DiscoveryTrim trim); // What the phone is told about this car: the template adapted by every step above. vehicleFile: the model // and year (SdrIdentity::loadVehicleFile), given by the platform; null keeps the default ones. softwareVersion: the // version of the program, announced as the head unit's software version; null keeps "0.0.0". display: the panel -// and the canvas chosen for it; the reference car's when not given. +// and the canvas chosen for it; the reference car's when not given. make: the car's and the head unit's maker as +// iDrive is coded ("MINI"); null keeps "BMW". std::vector buildCarServiceDiscovery(const uint8_t* base, size_t baseLen, const char* vehicleFile, - const char* softwareVersion, const DisplayPlan& display = DisplayPlan()); + const char* softwareVersion, const DisplayPlan& display = DisplayPlan(), + const char* make = nullptr); } // namespace aa diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index 2d92921..228c5f9 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -42,6 +42,7 @@ Le protocole Android Auto côté voiture, sans rien de propre à BMW. | `car_location.hpp` | Position de la voiture vers le capteur Android Auto | | `crash_handler` | Trace d'un plantage, puis le nettoyage enregistré par la plateforme | | `display_plan.hpp` | Image demandée au téléphone selon la taille de l'écran, et zone montrée | +| `keyframe_request.hpp` | Image vidéo perdue : quand redemander une image complète au téléphone (pas dans les 5 premières secondes, au plus une fois par 10 s) | | `include/` | Modèle de description de la voiture, certificat de l'unité | ## `platform/` : l'EntryNav2 @@ -62,6 +63,7 @@ Le protocole Android Auto côté voiture, sans rien de propre à BMW. | `vmost_client` | Accès au bus vMOST de la voiture (molette, touches, Wi-Fi), chargé à l'exécution | | `evo_ccs.hpp`, `evo_touch_frame.hpp` | Messages de la molette BMW (deux générations de contrôleur) et trames du tactile | | `controller_layout.hpp` | Quelle touche est chaque bouton, selon le codage de la voiture, comme l'iDrive (contrôleur type G ou F) | +| `car_brand.hpp` | Marque codée dans l'iDrive (BMW ou MINI) : marque annoncée au téléphone, bords de l'écran cachés par le rond des MINI | | `evo_gps` | Lecture passive du GPS de la voiture | | `evo_mic` | Micro de la voiture pour l'Assistant | | `evo_wlan`, `evo_ap` | Wi-Fi de la voiture, allumé comme le fait CarPlay | @@ -88,7 +90,7 @@ Le protocole Android Auto côté voiture, sans rien de propre à BMW. | `pending_restart.hpp` | Redémarrage d'Android Auto quand la case du câble USB change en marche (testé sur PC) | | `service_link` | Lancement du superviseur, commandes au service, arrêt | | `car_signals` | Marche arrière, appels, jour et nuit, mode de conduite, lus dans l'iDrive | -| `controller_coding` | Codage des touches de la voiture, lu dans l'iDrive au démarrage de l'appli, écrit pour le service | +| `idrive_coding` | Codage de la voiture lu dans l'iDrive au démarrage de l'appli et écrit pour le service : touches du contrôleur, marque (BMW ou MINI) | | `app_guards` | Interrupteur, garde-fou contre les plantages, démarrage automatique | | `app_log`, `log_profile.hpp` | Journal de l'appli, profil maintenance | | `popup_peek.hpp`, `cds_parse.hpp` | Fenêtres de l'iDrive par-dessus Android Auto, lecture des données de l'iDrive | diff --git a/docs/MAINTENANCE.md b/docs/MAINTENANCE.md index 7b73b10..cd973c4 100644 --- a/docs/MAINTENANCE.md +++ b/docs/MAINTENANCE.md @@ -86,15 +86,15 @@ Journaux activés, le service écrit une ligne `[WifiLink]` quand le téléphone les 10 secondes jusqu'à la fin de la session : ```text -[WifiLink] 5180 MHz; signal -52 dBm (average -51); to the phone 150.0 Mbit/s; from the phone 135.0 Mbit/s; +[WifiLink] 5180 MHz; signal -52 dBm (average -51); from the phone 135.0 Mbit/s; in 10 s: 812 frames sent, 35 retried, 0 lost; on the Wi-Fi for 135 s; TCP round trip 4.2 ms, 0 segment(s) sent again ``` | Partie | Sens | |---|---| | `5180 MHz` | Fréquence du Wi-Fi de la voiture : vers 2400, la bande 2,4 GHz, partagée avec le Bluetooth ; vers 5000, la bande 5 GHz | -| `signal` | Niveau reçu du téléphone, et sa moyenne. Plus le nombre est bas, plus la liaison est fragile (-50 : bon, -80 : faible) | -| `to the phone`, `from the phone` | Débit radio choisi dans chaque sens | +| `signal` | Niveau reçu du téléphone, et sa moyenne. Plus le nombre est bas, plus la liaison est fragile (-50 : bon, -80 : faible). Quand la puce n'a pas mesuré la dernière trame, seule la moyenne est écrite (`average signal`) | +| `from the phone` | Débit radio choisi par le téléphone. Le débit vers le téléphone n'est pas écrit : la puce Wi-Fi le choisit seule et le pilote ne le connaît pas | | `frames sent, retried, lost` | Trames envoyées au téléphone depuis la ligne précédente, réémises, puis abandonnées | | `on the Wi-Fi for` | Temps depuis que le téléphone a rejoint le Wi-Fi. S'il repart de zéro, le téléphone l'a quitté puis rejoint | | `TCP round trip`, `sent again` | Temps d'aller-retour de la connexion Android Auto, et morceaux qu'il a fallu renvoyer | diff --git a/hmiapp/controller_coding.cpp b/hmiapp/controller_coding.cpp deleted file mode 100644 index e350112..0000000 --- a/hmiapp/controller_coding.cpp +++ /dev/null @@ -1,76 +0,0 @@ -// Copyright (c) 2026 Jiraiya43 -// SPDX-License-Identifier: PolyForm-Noncommercial-1.0.0 -#include "controller_coding.hpp" - -#include -#include -#include -#include - -#include "../platform/controller_layout.hpp" -#include "../platform/service_contract.hpp" -#include "app_log.hpp" - -namespace evo { -namespace app { - -namespace { - -// HmiMain3D's exported template instances (nm -D: weak, defined). The usage object is one pointer-sized value, -// returned in r0 (read in pushbuttonToKeycode: the result of get() is stored, then its address is passed to get()). -const char kGetUsage[] = "_ZN9hmiplugin3getI17IHmiVariantServerEENS_25CPluginInstanceCacheUsageIT_EEv"; -const char kUsageGet[] = "_ZNK9hmiplugin25CPluginInstanceCacheUsageI17IHmiVariantServerE3getEv"; -const unsigned kIsVariantSetSlot = 3; // CHmiVariantServer vtable (libhmicoding.so): isVariantSet(unsigned) -const unsigned kGetRoleSlot = 21; // getRole() - -typedef uintptr_t (*GetUsageFn)(); -typedef void* (*UsageGetFn)(const uintptr_t* usage); -typedef bool (*IsVariantSetFn)(void* server, unsigned id); -typedef int (*GetRoleFn)(void* server); - -} // namespace - -bool writeControllerCoding() { - GetUsageFn getUsage = reinterpret_cast(dlsym(RTLD_DEFAULT, kGetUsage)); - UsageGetFn usageGet = reinterpret_cast(dlsym(RTLD_DEFAULT, kUsageGet)); - void* server = nullptr; - if (getUsage && usageGet) { - const uintptr_t usage = getUsage(); - server = usageGet(&usage); - } - void** vtable = server ? *reinterpret_cast(server) : nullptr; - if (!vtable || !vtable[kIsVariantSetSlot] || !vtable[kGetRoleSlot]) { - alog("controller coding: iDrive's variant server not found (%s %s): the service keeps G-style keys", - getUsage ? "get" : "no get", usageGet ? "usage" : "no usage"); - unlink(contract::kControllerFile); - return false; - } - IsVariantSetFn isSet = reinterpret_cast(vtable[kIsVariantSetSlot]); - GetRoleFn role = reinterpret_cast(vtable[kGetRoleSlot]); - aa::ControllerCoding coding = aa::defaultControllerCoding(); - coding.known = true; - coding.gStyle = isSet(server, aa::kVariantGStyle); - coding.v5141 = isSet(server, aa::kVariant5141); - coding.v4e59 = isSet(server, aa::kVariant4E59); - coding.v4f1e = isSet(server, aa::kVariant4F1E); - coding.v5486 = isSet(server, aa::kVariant5486); - coding.role = role(server); - char line[96]; - aa::formatControllerCoding(coding, line, sizeof(line)); - const char tmp[] = "/tmp/entry-aad-controller.tmp"; - FILE* f = fopen(tmp, "w"); - bool written = f && fprintf(f, "%s\n", line) > 0; - if (f && fclose(f) != 0) { - written = false; - } - if (!written || rename(tmp, contract::kControllerFile) != 0) { - alog("controller coding: %s, not written", line); - unlink(tmp); - return false; - } - alog("controller coding: %s (%s controller)", line, coding.gStyle ? "G-style" : "F-style"); - return true; -} - -} // namespace app -} // namespace evo diff --git a/hmiapp/controller_coding.hpp b/hmiapp/controller_coding.hpp deleted file mode 100644 index 9eed649..0000000 --- a/hmiapp/controller_coding.hpp +++ /dev/null @@ -1,23 +0,0 @@ -// Copyright (c) 2026 Jiraiya43 -// SPDX-License-Identifier: PolyForm-Noncommercial-1.0.0 -// The car's key coding, read inside iDrive and written for the service (platform/controller_layout.hpp). -// -// iDrive decides which key each controller button is from coding variants (0xB80 G-style controller, and a few -// more), asked from its variant server: hmiplugin::get(), then isVariantSet(id) (vtable slot 3) -// and getRole() (slot 21), as CHmiCCSController::pushbuttonToKeycode does in HmiMain3D. The app runs inside HmiMain3D, -// so it asks the same server; both functions are found with dlsym, nothing is linked. If one is missing or the server -// is not there yet, nothing is written and the service keeps its G-style table. -// Runs on the app thread (onInit). -#ifndef ENTRY_AAD_HMIAPP_CONTROLLER_CODING_HPP -#define ENTRY_AAD_HMIAPP_CONTROLLER_CODING_HPP - -namespace evo { -namespace app { - -// Reads the coding and writes contract::kControllerFile. False: not read (logged), the file is removed. -bool writeControllerCoding(); - -} // namespace app -} // namespace evo - -#endif diff --git a/hmiapp/entry_aa_app.cpp b/hmiapp/entry_aa_app.cpp index 8905fb8..72a388b 100644 --- a/hmiapp/entry_aa_app.cpp +++ b/hmiapp/entry_aa_app.cpp @@ -37,8 +37,8 @@ #include "app_log.hpp" #include "bmw_framework.hpp" #include "car_signals.hpp" -#include "controller_coding.hpp" #include "hold_machine.hpp" +#include "idrive_coding.hpp" #include "mono_clock.hpp" #include "pending_restart.hpp" #include "rhmi_ids.hpp" @@ -355,7 +355,7 @@ bool appOnInit(bmw::PluginApp* self) { addStateHandler(self, kAaState, kHandlerVisible, &onAaVisible); g_initTime = time(nullptr); bool ok = self->onInit(); - writeControllerCoding(); // which key each controller button is on this car, for the service + writeIdriveCoding(); // the car's keys and brand, for the service alog("onInit: handlers registered, base onInit -> %d, start automatically %s, USB cable %s", static_cast(ok), autostartOn() ? "on" : "off", usbCableOn() ? "on" : "off"); g_runApp = self; diff --git a/hmiapp/idrive_coding.cpp b/hmiapp/idrive_coding.cpp new file mode 100644 index 0000000..47f7390 --- /dev/null +++ b/hmiapp/idrive_coding.cpp @@ -0,0 +1,121 @@ +// Copyright (c) 2026 Jiraiya43 +// SPDX-License-Identifier: PolyForm-Noncommercial-1.0.0 +#include "idrive_coding.hpp" + +#include +#include +#include +#include + +#include + +#include "../platform/car_brand.hpp" +#include "../platform/controller_layout.hpp" +#include "../platform/service_contract.hpp" +#include "app_log.hpp" + +namespace evo { +namespace app { + +namespace { + +// HmiMain3D's exported template instances (nm -D: weak, defined). The usage object is one pointer-sized value, +// returned in r0 (read in pushbuttonToKeycode: the result of get() is stored, then its address is passed to get()). +const char kGetUsage[] = "_ZN9hmiplugin3getI17IHmiVariantServerEENS_25CPluginInstanceCacheUsageIT_EEv"; +const char kUsageGet[] = "_ZNK9hmiplugin25CPluginInstanceCacheUsageI17IHmiVariantServerE3getEv"; +const unsigned kIsVariantSetSlot = 3; // CHmiVariantServer vtable (libhmicoding.so): isVariantSet(unsigned) +const unsigned kGetRoleSlot = 21; // getRole() + +typedef uintptr_t (*GetUsageFn)(); +typedef void* (*UsageGetFn)(const uintptr_t* usage); +typedef bool (*IsVariantSetFn)(void* server, unsigned id); +typedef int (*GetRoleFn)(void* server); + +// iDrive's variant server and the two calls asked of it. +class VariantServer { +public: + VariantServer() : self_(nullptr), isSet_(nullptr), role_(nullptr) {} + // False when HmiMain3D does not give it (logged). + bool find() { + GetUsageFn getUsage = reinterpret_cast(dlsym(RTLD_DEFAULT, kGetUsage)); + UsageGetFn usageGet = reinterpret_cast(dlsym(RTLD_DEFAULT, kUsageGet)); + if (getUsage && usageGet) { + const uintptr_t usage = getUsage(); + self_ = usageGet(&usage); + } + void** vtable = self_ ? *reinterpret_cast(self_) : nullptr; + if (!vtable || !vtable[kIsVariantSetSlot] || !vtable[kGetRoleSlot]) { + alog("iDrive coding: variant server not found (%s %s): the service keeps G-style keys and a BMW", + getUsage ? "get" : "no get", usageGet ? "usage" : "no usage"); + return false; + } + isSet_ = reinterpret_cast(vtable[kIsVariantSetSlot]); + role_ = reinterpret_cast(vtable[kGetRoleSlot]); + return true; + } + bool isSet(unsigned id) const { return isSet_(self_, id); } + int role() const { return role_(self_); } + +private: + void* self_; + IsVariantSetFn isSet_; + GetRoleFn role_; +}; + +// One line to path, through a temporary file renamed over it: the service never reads half a line. On failure the +// file is removed, so the service falls back to its default rather than an old value. +bool writeLine(const char* path, const char* line) { + const std::string tmp = std::string(path) + ".tmp"; + FILE* f = fopen(tmp.c_str(), "w"); + bool written = f && fprintf(f, "%s\n", line) > 0; + if (f && fclose(f) != 0) { + written = false; + } + if (!written || rename(tmp.c_str(), path) != 0) { + unlink(tmp.c_str()); + unlink(path); + return false; + } + return true; +} + +bool writeControllerCoding(const VariantServer& server) { + aa::ControllerCoding coding = aa::defaultControllerCoding(); + coding.known = true; + coding.gStyle = server.isSet(aa::kVariantGStyle); + coding.v5141 = server.isSet(aa::kVariant5141); + coding.v4e59 = server.isSet(aa::kVariant4E59); + coding.v4f1e = server.isSet(aa::kVariant4F1E); + coding.v5486 = server.isSet(aa::kVariant5486); + coding.role = server.role(); + char line[96]; + aa::formatControllerCoding(coding, line, sizeof(line)); + const bool written = writeLine(contract::kControllerFile, line); + alog("controller coding: %s (%s controller)%s", line, coding.gStyle ? "G-style" : "F-style", + written ? "" : ", not written"); + return written; +} + +bool writeBrand(const VariantServer& server) { + const aa::CarBrand brand = server.isSet(aa::kVariantBrandMini) ? aa::CarBrand::kMini : aa::CarBrand::kBmw; + const bool written = writeLine(contract::kBrandFile, aa::brandName(brand)); + alog("brand: %s%s", aa::brandName(brand), written ? "" : ", not written"); + return written; +} + +} // namespace + +bool writeIdriveCoding() { + VariantServer server; + if (!server.find()) { + unlink(contract::kControllerFile); + unlink(contract::kBrandFile); + return false; + } + const bool keys = writeControllerCoding(server); + const bool brand = writeBrand(server); + return keys && brand; +} + +} // namespace app +} // namespace evo diff --git a/hmiapp/idrive_coding.hpp b/hmiapp/idrive_coding.hpp new file mode 100644 index 0000000..5a5b8b6 --- /dev/null +++ b/hmiapp/idrive_coding.hpp @@ -0,0 +1,24 @@ +// Copyright (c) 2026 Jiraiya43 +// SPDX-License-Identifier: PolyForm-Noncommercial-1.0.0 +// What the service needs of the car's coding, read inside iDrive and written for it: which key each controller +// button is (platform/controller_layout.hpp) and the brand (platform/car_brand.hpp). +// +// iDrive keeps its coding behind a variant server: hmiplugin::get(), then isVariantSet(id) +// (vtable slot 3) and getRole() (slot 21), as CHmiCCSController::pushbuttonToKeycode does in HmiMain3D. The app runs +// inside HmiMain3D, so it asks the same server; both functions are found with dlsym, nothing is linked. If one is +// missing or the server is not there yet, nothing is written: the service keeps its G-style keys and a BMW. +// Runs on the app thread (onInit). +#ifndef ENTRY_AAD_HMIAPP_IDRIVE_CODING_HPP +#define ENTRY_AAD_HMIAPP_IDRIVE_CODING_HPP + +namespace evo { +namespace app { + +// Reads the coding and writes contract::kControllerFile and contract::kBrandFile. False when either was not written +// (logged); a file not written is removed. +bool writeIdriveCoding(); + +} // namespace app +} // namespace evo + +#endif diff --git a/platform/car_brand.hpp b/platform/car_brand.hpp new file mode 100644 index 0000000..a141468 --- /dev/null +++ b/platform/car_brand.hpp @@ -0,0 +1,52 @@ +// Copyright (c) 2026 Jiraiya43 +// SPDX-License-Identifier: PolyForm-Noncommercial-1.0.0 +// The car's brand as iDrive is coded: the maker the phone is told about (Android Auto shows it on its exit icon), and +// the round surround of the MINI's central screen. +// +// iDrive's coding variant HMI_BRAND (libhmicoding.so, its table of coding features): va_HMI_BRAND_bmw 0x1F6, +// va_HMI_BRAND_bmw_i 0x1F7, va_HMI_BRAND_mini 0x1F8, va_HMI_BRAND_rr 0x1F9, va_HMI_BRAND_bmw_m 0xE8D. The iDrive app +// asks its variant server for 0x1F8 (hmiapp/idrive_coding.cpp) and writes contract::kBrandFile; the service reads it at +// start. Without the file (an older app), a BMW. Pure: no I/O. tests/test_evo_sdr.cpp. +#ifndef ENTRY_AAD_PLATFORM_CAR_BRAND_HPP +#define ENTRY_AAD_PLATFORM_CAR_BRAND_HPP + +#include +#include + +namespace aa { + +enum class CarBrand { kBmw, kMini }; + +const unsigned kVariantBrandMini = 0x1F8; // va_HMI_BRAND_mini + +// The maker as the phone is told it, and as the brand file holds it. +inline const char* brandName(CarBrand brand) { + return brand == CarBrand::kMini ? "MINI" : "BMW"; +} + +// The brand file's line: "MINI" or "BMW", an end of line allowed; false otherwise (out unchanged). +inline bool parseCarBrand(const char* text, CarBrand& out) { + if (!text) { + return false; + } + const size_t len = strcspn(text, "\r\n"); + if (len == 4 && !strncmp(text, "MINI", 4)) { + out = CarBrand::kMini; + return true; + } + if (len == 3 && !strncmp(text, "BMW", 3)) { + out = CarBrand::kBmw; + return true; + } + return false; +} + +// The MINI's central screen sits in a round surround that hides about 40 pixels on each side of a 1280-pixel-wide +// panel, and the same share of a narrower one: the width hidden on each side, in panel pixels. 0 on a BMW. +inline uint32_t surroundSide(CarBrand brand, uint32_t panelWidth) { + return brand == CarBrand::kMini ? (40 * panelWidth + 640) / 1280 : 0; +} + +} // namespace aa + +#endif // ENTRY_AAD_PLATFORM_CAR_BRAND_HPP diff --git a/platform/evo_video.cpp b/platform/evo_video.cpp index 5a65605..5b94a9b 100644 --- a/platform/evo_video.cpp +++ b/platform/evo_video.cpp @@ -232,10 +232,10 @@ bool EvoVideo::isIdr(const uint8_t* d, size_t n) const { return false; } -bool EvoVideo::postVideoFrame(const uint8_t* data, size_t len, uint64_t pts) { +FrameFate EvoVideo::postVideoFrame(const uint8_t* data, size_t len, uint64_t pts) { (void)pts; if (!running_ || !started_) { - return false; + return FrameFate::kNoStream; } pthread_mutex_lock(&mu_); @@ -269,7 +269,7 @@ bool EvoVideo::postVideoFrame(const uint8_t* data, size_t len, uint64_t pts) { if (bi < 0) { ++framesDropped_; pthread_mutex_unlock(&mu_); - return false; + return FrameFate::kLost; } DmaBufPool::Buf& b = pool_.at(bi); @@ -277,7 +277,7 @@ bool EvoVideo::postVideoFrame(const uint8_t* data, size_t len, uint64_t pts) { ++framesDropped_; pthread_mutex_unlock(&mu_); ELOG("Video", "frame %zu B > buffer %u B", need, b.size); - return false; + return FrameFate::kLost; } size_t off = 0; if (prependCfg) { @@ -300,9 +300,9 @@ bool EvoVideo::postVideoFrame(const uint8_t* data, size_t len, uint64_t pts) { } pthread_cond_signal(&cv_); pthread_mutex_unlock(&mu_); - return false; + return FrameFate::kLost; } - return true; + return FrameFate::kDecoding; } void* EvoVideo::eventStub(void* self) { diff --git a/platform/evo_video.hpp b/platform/evo_video.hpp index 4d67f40..9671a69 100644 --- a/platform/evo_video.hpp +++ b/platform/evo_video.hpp @@ -44,6 +44,14 @@ struct EvoVideoConfig { void setDisplay(uint32_t canvasWidth, uint32_t canvasHeight, uint32_t panelWidth, uint32_t panelHeight); }; +// What became of one frame handed to postVideoFrame. +enum class FrameFate { + kDecoding, // in the decoder + kLost, // dropped (no free buffer in time, larger than a buffer) or refused by the decoder: the frames after it + // refer to a picture the decoder never had + kNoStream, // the decoder is not running +}; + class EvoVideo { public: EvoVideo(); @@ -62,8 +70,7 @@ class EvoVideo { // SPS/PPS (the Android Auto codec config): kept and prepended to the first IDR frame after each (re)start. void postCodecConfig(const uint8_t* data, size_t len); // One H.264 access unit, copied into a free dma-buf and handed to the decoder. - // False when no buffer frees up in time (frame dropped) or the stream is not started. - bool postVideoFrame(const uint8_t* data, size_t len, uint64_t pts); + FrameFate postVideoFrame(const uint8_t* data, size_t len, uint64_t pts); private: static void* eventStub(void* self); diff --git a/platform/main.cpp b/platform/main.cpp index 235dfcf..a70ec91 100644 --- a/platform/main.cpp +++ b/platform/main.cpp @@ -8,6 +8,7 @@ // the USB half of a wired one in usb_loop. // clang-format off #include "alps_spp.hpp" +#include "car_brand.hpp" #include "evo_ap.hpp" #include "evo_video.hpp" #include "evo_touch.hpp" @@ -127,6 +128,21 @@ void usage(const char* argv0) { " --alps-pid P --alps-start S --uuid-pair HEX Alps state saved by aa-supervisor, checked before each step\n", argv0); } + +// The brand the iDrive app read in the car's coding (car_brand.hpp). Without the file (an older app), a BMW. +aa::CarBrand readCarBrand() { + aa::CarBrand brand = aa::CarBrand::kBmw; + FILE* f = fopen(evo::contract::kBrandFile, "r"); + char line[16] = {0}; + const bool read = f && fgets(line, sizeof(line), f) && aa::parseCarBrand(line, brand); + if (f) { + fclose(f); + } + if (!read) { + ELOG("Main", "brand not read from %s: BMW", evo::contract::kBrandFile); + } + return brand; +} } // namespace int main(int argc, char** argv) { @@ -242,9 +258,16 @@ int main(int argc, char** argv) { ELOG("Main", "screen %ux%u: the phone is asked for %ux%u%s", display.width, display.height, display.canvasWidth, display.canvasHeight, display.reference() ? "" : " (a size not tried on the reference car)"); } + const aa::CarBrand brand = readCarBrand(); + display.hiddenSide = aa::surroundSide(brand, display.width); + if (display.hiddenSide) { + ELOG("Main", "%s: the round surround hides %u columns on each side, the phone keeps its UI inside", + aa::brandName(brand), display.hiddenSide); + } evo::ServiceApp app(kAaPort, video, &quit); app.videoConfig.setDisplay(display.canvasWidth, display.canvasHeight, display.width, display.height); app.session.setDisplay(display); + app.session.setCarMake(aa::brandName(brand)); app.usbPort = usbPort ? usbPort : ""; app.session.setTouchForwarding(input == evo::INPUT_FORWARD); app.session.setVehicleFile(evo::contract::kVehicle); // the car's model and year, written at installation diff --git a/platform/service_app.cpp b/platform/service_app.cpp index 7830ff9..7a4fddd 100644 --- a/platform/service_app.cpp +++ b/platform/service_app.cpp @@ -153,7 +153,9 @@ void ServiceApp::frame(uint32_t, const uint8_t* data, size_t len, uint64_t pts, // Phone frames never wait for mu: the main loop holds it while LayerManagerControl runs (show/hide, // opacity), which would stall the picture. ++a->videoFrames; - a->video.postVideoFrame(data, len, pts); + if (a->video.postVideoFrame(data, len, pts) == FrameFate::kLost) { + a->session.postKeyframeRequest(); // the session decides whether and when to ask the phone + } } } diff --git a/platform/service_contract.hpp b/platform/service_contract.hpp index 93484c3..87a01a0 100644 --- a/platform/service_contract.hpp +++ b/platform/service_contract.hpp @@ -43,6 +43,7 @@ constexpr char kStateFile[] = "/tmp/entry-aad-state"; // "state pid constexpr char kAppKeyFile[] = "/tmp/entry-aad-appkey"; // NAV/MEDIA/COM/BACK kept Android Auto up constexpr char kDayNightFile[] = "/tmp/entry-aad-daynight"; // day or night, written by the app constexpr char kControllerFile[] = "/tmp/entry-aad-controller"; // the car's key coding, written by the app +constexpr char kBrandFile[] = "/tmp/entry-aad-brand"; // "BMW" or "MINI", as iDrive is coded constexpr char kWirelessLock[] = "/tmp/entry-aad-wireless.lock"; // one wireless start at a time // One-shot double-MEDIA recall request: process id and monotonic timestamp, checked by the iDrive app. constexpr char kMediaRecallFile[] = "/tmp/entry-aad-media-recall"; diff --git a/platform/wifi_link.cpp b/platform/wifi_link.cpp index 061232e..48a8dca 100644 --- a/platform/wifi_link.cpp +++ b/platform/wifi_link.cpp @@ -78,9 +78,14 @@ int64_t number(const Attr& a) { return kNoFigure; } -// A signal level: one signed byte, in dBm. +// A signal level: one signed byte, in dBm. mac80211 copies the level the chip gave with the last frame received, and +// the WL18xx leaves it at 0 for frames it did not measure: 0 dBm is never a level received from a phone. int64_t level(const Attr& a) { - return a.len == 1 ? static_cast(a.data[0]) : kNoFigure; + if (a.len != 1) { + return kNoFigure; + } + const int8_t dbm = static_cast(a.data[0]); + return dbm < 0 ? dbm : kNoFigure; } int64_t bitrate(const Attr& nested) { @@ -107,9 +112,6 @@ void stationFigures(const Attr& nested, WifiStation& out) { case NL80211_STA_INFO_SIGNAL_AVG: out.signalAvgDbm = level(a); break; - case NL80211_STA_INFO_TX_BITRATE: - out.txRate = bitrate(a); - break; case NL80211_STA_INFO_RX_BITRATE: out.rxRate = bitrate(a); break; @@ -330,7 +332,6 @@ bool continues(const WifiStation& now, const WifiStation& before) { WifiStation::WifiStation() : signalDbm(kNoFigure), signalAvgDbm(kNoFigure), - txRate(kNoFigure), rxRate(kNoFigure), txFrames(kNoFigure), txRetries(kNoFigure), @@ -396,9 +397,8 @@ std::string describeWifiLink(const WifiSample& now, const WifiSample* before) { add(line, "; ", "signal %lld dBm (average %lld)", phone.signalDbm, phone.signalAvgDbm); } else if (phone.signalDbm != kNoFigure) { add(line, "; ", "signal %lld dBm", phone.signalDbm); - } - if (phone.txRate != kNoFigure) { - add(line, "; ", "to the phone %lld.%lld Mbit/s", phone.txRate / 10, phone.txRate % 10); + } else if (phone.signalAvgDbm != kNoFigure) { + add(line, "; ", "average signal %lld dBm", phone.signalAvgDbm); } if (phone.rxRate != kNoFigure) { add(line, "; ", "from the phone %lld.%lld Mbit/s", phone.rxRate / 10, phone.rxRate % 10); diff --git a/platform/wifi_link.hpp b/platform/wifi_link.hpp index d7c143e..7bee56f 100644 --- a/platform/wifi_link.hpp +++ b/platform/wifi_link.hpp @@ -4,8 +4,10 @@ // Everything here only reads. Two sources, both the kernel's own figures: // - nl80211 (generic netlink), the interface `iw` uses: the phone's station entry on the car's access point (signal, -// bitrates, frames sent, retried and lost) and the access point's frequency; +// bitrate from the phone, frames sent, retried and lost) and the access point's frequency; // https://www.kernel.org/doc/html/latest/userspace-api/netlink/intro.html, include/uapi/linux/nl80211.h +// The rate towards the phone is left out: the WL18xx firmware chooses it, and the driver (wlcore) reported +// 1 Mbit/s on every car, the reference G20 included, whatever was sent. // - TCP_INFO on the Android Auto connection: round trip time and segments sent again. // Nothing identifies the phone in the log: no address is written. #ifndef ENTRY_AAD_PLATFORM_WIFI_LINK_HPP @@ -25,8 +27,8 @@ const int64_t kNoFigure = -0x7fffffffffffffffLL - 1; // a figure the driver did // One phone on the access point, as nl80211 reports it. struct WifiStation { uint8_t mac[6]; - int64_t signalDbm, signalAvgDbm; - int64_t txRate, rxRate; // in 100 kbit/s + int64_t signalDbm, signalAvgDbm; // signalDbm: the last frame received, when the chip measured it + int64_t rxRate; // in 100 kbit/s int64_t txFrames, txRetries, txFailed; // since the phone joined int64_t connectedS; // how long ago that was WifiStation(); diff --git a/tests/test_evo_sdr.cpp b/tests/test_evo_sdr.cpp index a733487..2c265ad 100644 --- a/tests/test_evo_sdr.cpp +++ b/tests/test_evo_sdr.cpp @@ -11,6 +11,7 @@ #include #include +#include "car_brand.hpp" #include "dhu_sdp.h" #include "evo_gps.hpp" #include "evo_sdr_sensors.hpp" @@ -260,6 +261,34 @@ int main() { aa::buildCarServiceDiscovery(kDhuSdpProtobuf, kDhuSdpProtobufLen, nullptr, nullptr, wide); CHECK(shown(forWide, s) && s.resolution == 3 && s.marginW == 0 && s.marginH == 0 && s.bottom == 540 && s.right == 480 && s.top == 0 && s.left == 0 && s.touchW == 1920 && s.touchH == 1080); + // The brand file the iDrive app writes: "MINI" or "BMW", nothing else. + aa::CarBrand brand = aa::CarBrand::kBmw; + CHECK(aa::parseCarBrand("MINI\n", brand) && brand == aa::CarBrand::kMini && !strcmp(aa::brandName(brand), "MINI")); + CHECK(aa::parseCarBrand("BMW", brand) && brand == aa::CarBrand::kBmw && !strcmp(aa::brandName(brand), "BMW")); + CHECK(!aa::parseCarBrand("MINIS", brand) && !aa::parseCarBrand("mini", brand) && !aa::parseCarBrand("", brand) && + !aa::parseCarBrand(nullptr, brand) && brand == aa::CarBrand::kBmw); + CHECK(aa::surroundSide(aa::CarBrand::kBmw, 1280) == 0 && aa::surroundSide(aa::CarBrand::kMini, 1280) == 40 && + aa::surroundSide(aa::CarBrand::kMini, 800) == 25); + // A MINI: its round surround hides 40 columns on each side of the 1280x480 panel (25 of an 800x480 one). The + // phone keeps its UI away from them; the canvas, the touchscreen and the bottom inset stay. + aa::DisplayPlan mini = same, smallMini = small; + mini.hiddenSide = aa::surroundSide(aa::CarBrand::kMini, mini.width); + smallMini.hiddenSide = aa::surroundSide(aa::CarBrand::kMini, smallMini.width); + const std::vector forMini = + aa::buildCarServiceDiscovery(kDhuSdpProtobuf, kDhuSdpProtobufLen, nullptr, nullptr, mini, "MINI"); + CHECK(shown(forMini, s) && s.resolution == 2 && s.bottom == 240 && s.left == 40 && s.right == 40 && + s.touchW == 1280 && s.touchH == 720); + CHECK(shown(aa::buildCarServiceDiscovery(kDhuSdpProtobuf, kDhuSdpProtobufLen, nullptr, nullptr, smallMini), s) && + s.left == 25 && s.right == 25 && s.bottom == 0 && s.touchW == 800); + // The maker: "MINI" for the car and the head unit, at the top level and in the head unit info; "BMW" otherwise. + const std::string miniText(forMini.begin(), forMini.end()); + CHECK(plain.find("MINI") == std::string::npos && miniText.find("BMW") == std::string::npos); + size_t makes = 0; + for (size_t at = miniText.find("MINI"); at != std::string::npos; at = miniText.find("MINI", at + 1)) { + ++makes; + } + CHECK(makes == 4); // car make and head unit make, twice each + CHECK(aa::buildCarServiceDiscovery(kDhuSdpProtobuf, kDhuSdpProtobufLen, nullptr, nullptr, same, "") == unversioned); // Nothing but the picture and the touchscreen differs: every other channel is the reference car's. std::vector sensorsA, sensorsB; std::map> reference = channels(unversioned, sensorsA), diff --git a/tests/test_session_phone.cpp b/tests/test_session_phone.cpp index d677426..d71b46b 100644 --- a/tests/test_session_phone.cpp +++ b/tests/test_session_phone.cpp @@ -1017,6 +1017,78 @@ std::string quietPhone() { return std::string(); } +// An encrypted frame the head unit cannot decrypt: a record header TLS would never write. +void unreadable(Phone& phone) { + phone.raw("unreadable", bytes({0x00, kSealed, 0x00, 0x04, 0x01, 0x02, 0x03, 0x04})); +} + +// The TLS stream breaks: 15 unreadable frames in a row are survived, the 16th ends the session. +std::string brokenStream() { + std::vector log; + Link link; + Phone phone(link.phoneFd, log); + phone.handshake(); + phone.discover(); + for (int i = 0; i < 15; ++i) { + unreadable(phone); + } + phone.settle(); // a plain ping, still answered + if (!link.session.isActive() || phone.lost()) { + return "15 unreadable frames ended the session"; + } + unreadable(phone); + if (!waitEnded(link.session)) { + return "the 16th unreadable frame in a row did not end the session"; + } + return std::string(); +} + +// The video focus indications the head unit sent, in order: "1" FOCUSED, "2" UNFOCUSED, each followed by 'u' when +// unrequested. +std::string focusTrail(const std::vector& log, size_t from) { + std::string trail; + for (size_t i = from; i < log.size(); ++i) { + const size_t at = log[i].find("hu ch=6 fl=0b 8008 08"); + if (at != std::string::npos && log[i].size() >= at + 27) { + trail += log[i][at + 22]; + trail += log[i].compare(at + 23, 4, "1001") == 0 ? "u" : ""; + } + } + return trail; +} + +// A frame the decoder lost: no request during the video's first 5 s, then the focus off and on, once in 10 s. +std::string keyframeAfterLoss() { + std::vector log; + Link link; + Phone phone(link.phoneFd, log); + phone.handshake(); + phone.discover(); + phone.send(6, kSealedCtl, bytes({0x00, 0x07, 0x08, 0x00, 0x10, 0x06})); + phone.send(6, kSealed, bytes({0x80, 0x00, 0x08, 0x03})); // video setup: FOCUSED at once + phone.settle(); + size_t from = log.size(); + link.session.postKeyframeRequest(); + phone.settle(); + if (!focusTrail(log, from).empty()) { + return "a keyframe was asked during the video's first seconds: " + focusTrail(log, from); + } + phone.headUnitPings(5200, true); + from = log.size(); + link.session.postKeyframeRequest(); + phone.settle(); + if (focusTrail(log, from) != "2u1u") { + return "after 5 s, focus trail '" + focusTrail(log, from) + "' instead of off then on"; + } + from = log.size(); + link.session.postKeyframeRequest(); + phone.settle(); + if (!focusTrail(log, from).empty() || !link.session.isActive()) { + return "a second request within 10 s was not held back"; + } + return std::string(); +} + struct Scenario { const char* name; void (*run)(std::vector&); @@ -1097,6 +1169,10 @@ int main(int argc, char** argv) { } const std::string quiet = writing ? std::string() : quietPhone(); takeEvents(); + const std::string broken = writing ? std::string() : brokenStream(); + takeEvents(); + const std::string keyframe = writing ? std::string() : keyframeAfterLoss(); + takeEvents(); fflush(stdout); dup2(console, 1); close(sink); @@ -1136,5 +1212,15 @@ int main(int argc, char** argv) { return 1; } puts("PASS session, quiet phone: answered pings keep it, unanswered ones end it within the silence limit"); + if (!broken.empty()) { + printf("FAIL session, broken stream: %s\n", broken.c_str()); + return 1; + } + puts("PASS session, broken stream: 16 encrypted frames in a row that do not decrypt end it, 15 do not"); + if (!keyframe.empty()) { + printf("FAIL session, keyframe after a lost frame: %s\n", keyframe.c_str()); + return 1; + } + puts("PASS session, keyframe after a lost frame: focus off and on, not in the first 5 s, once in 10 s"); return 0; } diff --git a/tests/test_video_presentation.cpp b/tests/test_video_presentation.cpp index fe49a9a..4dc6522 100644 --- a/tests/test_video_presentation.cpp +++ b/tests/test_video_presentation.cpp @@ -103,6 +103,7 @@ static bool configThenIdr() { } int main() { + typedef evo::FrameFate Fate; evo::EvoVideoConfig cfg; evo::EvoVideo video; assert(!video.isReady()); @@ -120,18 +121,18 @@ int main() { // SPS/PPS go in front of the first IDR; a frame the decoder refuses keeps them for the next IDR. video.postCodecConfig(kSpsPps, sizeof(kSpsPps)); - assert(video.postVideoFrame(kSlice, sizeof(kSlice), 0) && decoded == sizeof(kSlice)); + assert(video.postVideoFrame(kSlice, sizeof(kSlice), 0) == Fate::kDecoding && decoded == sizeof(kSlice)); decodeFailures = 1; - assert(!video.postVideoFrame(kIdr, sizeof(kIdr), 0) && configThenIdr()); - assert(video.postVideoFrame(kIdr, sizeof(kIdr), 0) && configThenIdr()); - assert(video.postVideoFrame(kIdr, sizeof(kIdr), 0) && decoded == sizeof(kIdr)); // sent once + assert(video.postVideoFrame(kIdr, sizeof(kIdr), 0) == Fate::kLost && configThenIdr()); + assert(video.postVideoFrame(kIdr, sizeof(kIdr), 0) == Fate::kDecoding && configThenIdr()); + assert(video.postVideoFrame(kIdr, sizeof(kIdr), 0) == Fate::kDecoding && decoded == sizeof(kIdr)); // sent once video.stop(); assert(!visible && !video.isVisible() && !video.isReady()); - assert(!video.postVideoFrame(kIdr, sizeof(kIdr), 0)); + assert(video.postVideoFrame(kIdr, sizeof(kIdr), 0) == Fate::kNoStream); // stopped: nothing lost assert(video.init(cfg) && video.start() && video.isReady()); - assert(opacity == 1.f && video.setOpacity(0.25f) && opacity == 0.25f); // fresh surface, fresh cache - assert(video.postVideoFrame(kIdr, sizeof(kIdr), 0) && configThenIdr()); // the new decoder gets SPS/PPS again + assert(opacity == 1.f && video.setOpacity(0.25f) && opacity == 0.25f); // fresh surface, fresh cache + assert(video.postVideoFrame(kIdr, sizeof(kIdr), 0) == Fate::kDecoding && configThenIdr()); // SPS/PPS again video.stop(); puts("PASS video presentation: visibility, opacity cache, camera hide, SPS/PPS after a refused IDR, reconnect"); } diff --git a/tests/test_wifi_link.cpp b/tests/test_wifi_link.cpp index 51f7651..ab9ea57 100644 --- a/tests/test_wifi_link.cpp +++ b/tests/test_wifi_link.cpp @@ -86,7 +86,7 @@ evo::WifiStation station(uint32_t frames, uint32_t retries, uint32_t failed, uin void stationMessages() { const evo::WifiStation s = station(5000, 300, 2, 125); CHECK(!memcmp(s.mac, kPhone, sizeof(kPhone))); - CHECK(s.signalDbm == -52 && s.signalAvgDbm == -51 && s.txRate == 1500 && s.rxRate == 1350); + CHECK(s.signalDbm == -52 && s.signalAvgDbm == -51 && s.rxRate == 1350); CHECK(s.txFrames == 5000 && s.txRetries == 300 && s.txFailed == 2 && s.connectedS == 125); // A driver that gives little: what is missing stays "no figure". @@ -98,9 +98,19 @@ void stationMessages() { bare.nested(NL80211_ATTR_STA_INFO, info); evo::WifiStation little; CHECK(evo::parseWifiStation(&bare.bytes[0], bare.bytes.size(), little)); - CHECK(little.signalDbm == -70 && little.signalAvgDbm == evo::kNoFigure && little.txRate == evo::kNoFigure && + CHECK(little.signalDbm == -70 && little.signalAvgDbm == evo::kNoFigure && little.rxRate == evo::kNoFigure && little.txFrames == evo::kNoFigure && little.connectedS == evo::kNoFigure); + // A level of 0 dBm is the WL18xx's "not measured" (seen in every car log): no figure. + Attrs zero; + Attrs zeroInfo; + zeroInfo.u8(NL80211_STA_INFO_SIGNAL, 0); + zeroInfo.u8(NL80211_STA_INFO_SIGNAL_AVG, static_cast(-68)); + zero.put(NL80211_ATTR_MAC, kPhone, sizeof(kPhone)); + zero.nested(NL80211_ATTR_STA_INFO, zeroInfo); + CHECK(evo::parseWifiStation(&zero.bytes[0], zero.bytes.size(), little)); + CHECK(little.signalDbm == evo::kNoFigure && little.signalAvgDbm == -68); + // No station address: not a station. Attrs anonymous; anonymous.nested(NL80211_ATTR_STA_INFO, info); @@ -150,13 +160,13 @@ evo::WifiSample sample(uint32_t atMs, uint32_t frames, uint32_t retries, uint32_ void logLines() { const evo::WifiSample first = sample(1000, 5000, 300, 2, 125, 4250, 3); CHECK(evo::describeWifiLink(first, nullptr) == - "5180 MHz; signal -52 dBm (average -51); to the phone 150.0 Mbit/s; from the phone 135.0 Mbit/s; " + "5180 MHz; signal -52 dBm (average -51); from the phone 135.0 Mbit/s; " "since it joined: 5000 frames sent, 300 retried, 2 lost; on the Wi-Fi for 125 s; " "TCP round trip 4.2 ms, 3 segment(s) sent again since it opened"); // Ten seconds later: what happened in between. const evo::WifiSample second = sample(11040, 5812, 335, 2, 135, 38900, 9); CHECK(evo::describeWifiLink(second, &first) == - "5180 MHz; signal -52 dBm (average -51); to the phone 150.0 Mbit/s; from the phone 135.0 Mbit/s; " + "5180 MHz; signal -52 dBm (average -51); from the phone 135.0 Mbit/s; " "in 10 s: 812 frames sent, 35 retried, 0 lost; on the Wi-Fi for 135 s; " "TCP round trip 38.9 ms, 6 segment(s) sent again"); // The phone left the Wi-Fi and came back: its counters started again, nothing is subtracted. @@ -189,7 +199,12 @@ void logLines() { bare.station.txFrames = bare.station.txRetries = bare.station.txFailed = bare.station.connectedS = evo::kNoFigure; bare.station.signalAvgDbm = bare.station.rxRate = evo::kNoFigure; bare.hasTcp = false; - CHECK(evo::describeWifiLink(bare, nullptr) == "5180 MHz; signal -52 dBm; to the phone 150.0 Mbit/s"); + CHECK(evo::describeWifiLink(bare, nullptr) == "5180 MHz; signal -52 dBm"); + // The chip did not measure the last frame: the average alone. + evo::WifiSample unmeasured = first; + unmeasured.station.signalDbm = evo::kNoFigure; + unmeasured.hasTcp = false; + CHECK(evo::describeWifiLink(unmeasured, nullptr).find("5180 MHz; average signal -51 dBm; from the phone") == 0); } // A TCP connection on loopback: the car's end in car, the phone's in phone.