From 8a0c57433e746628468cfcb8e70c38dfc44c1e28 Mon Sep 17 00:00:00 2001 From: Willem Melching Date: Tue, 26 Oct 2021 18:59:17 +0200 Subject: [PATCH] navigation: use mapbox proxy server (#22660) * use mapbox proxy * remove MapboxToken param * do not log jwt * remove import * move to staging url * put has_prime in ui state * logic was flipped * Update selfdrive/ui/qt/maps/map.h old-commit-hash: cca07c0c730eb6e0d8dab0bde099a2ed706d14de --- selfdrive/athena/athenad.py | 22 +--------------------- selfdrive/common/params.cc | 1 - selfdrive/ui/qt/maps/map.cc | 1 + selfdrive/ui/qt/maps/map.h | 2 ++ selfdrive/ui/qt/onroad.cc | 9 +++++++-- selfdrive/ui/qt/widgets/prime.cc | 1 + selfdrive/ui/ui.h | 3 ++- 7 files changed, 14 insertions(+), 25 deletions(-) diff --git a/selfdrive/athena/athenad.py b/selfdrive/athena/athenad.py index fadbad0bf8..779045bf97 100755 --- a/selfdrive/athena/athenad.py +++ b/selfdrive/athena/athenad.py @@ -26,7 +26,7 @@ from common.file_helpers import CallbackReader from common.basedir import PERSIST from common.params import Params from common.realtime import sec_since_boot -from selfdrive.hardware import HARDWARE, PC, TICI +from selfdrive.hardware import HARDWARE, PC from selfdrive.loggerd.config import ROOT from selfdrive.loggerd.xattr_cache import getxattr, setxattr from selfdrive.swaglog import cloudlog, SWAGLOG_DIR @@ -268,9 +268,6 @@ def cancelUpload(upload_id): @dispatcher.add_method def primeActivated(activated): - dongle_id = Params().get("DongleId", encoding='utf-8') - api = Api(dongle_id) - manage_tokens(api) return {"success": 1} @@ -523,21 +520,6 @@ def backoff(retries): return random.randrange(0, min(128, int(2 ** retries))) -def manage_tokens(api): - if not TICI: - return - - try: - params = Params() - mapbox = api.get(f"/v1/tokens/mapbox/{api.dongle_id}/", timeout=5.0, access_token=api.get_token()) - if mapbox.status_code == 200: - params.put("MapboxToken", mapbox.json()["token"]) - else: - params.delete("MapboxToken") - except Exception: - cloudlog.exception("Failed to update tokens") - - def main(): params = Params() dongle_id = params.get("DongleId", encoding='utf-8') @@ -556,8 +538,6 @@ def main(): cloudlog.event("athenad.main.connected_ws", ws_uri=ws_uri) params.delete("PrimeRedirected") - manage_tokens(api) - conn_retries = 0 cur_upload_items.clear() diff --git a/selfdrive/common/params.cc b/selfdrive/common/params.cc index 22926c5934..e6e84d4437 100644 --- a/selfdrive/common/params.cc +++ b/selfdrive/common/params.cc @@ -169,7 +169,6 @@ std::unordered_map keys = { {"LastUpdateException", PERSISTENT}, {"LastUpdateTime", PERSISTENT}, {"LiveParameters", PERSISTENT}, - {"MapboxToken", PERSISTENT | DONT_LOG}, {"NavDestination", CLEAR_ON_MANAGER_START | CLEAR_ON_IGNITION_OFF}, {"NavSettingTime24h", PERSISTENT}, {"OpenpilotEnabledToggle", PERSISTENT}, diff --git a/selfdrive/ui/qt/maps/map.cc b/selfdrive/ui/qt/maps/map.cc index e07bf58be6..5993a47500 100644 --- a/selfdrive/ui/qt/maps/map.cc +++ b/selfdrive/ui/qt/maps/map.cc @@ -53,6 +53,7 @@ MapWindow::MapWindow(const QMapboxGLSettings &settings) : // Routing QVariantMap parameters; parameters["mapbox.access_token"] = m_settings.accessToken(); + parameters["mapbox.directions_api_url"] = MAPS_HOST + "/directions/v5/mapbox/"; geoservice_provider = new QGeoServiceProvider("mapbox", parameters); routing_manager = geoservice_provider->routingManager(); diff --git a/selfdrive/ui/qt/maps/map.h b/selfdrive/ui/qt/maps/map.h index a796872b32..917b4d8c10 100644 --- a/selfdrive/ui/qt/maps/map.h +++ b/selfdrive/ui/qt/maps/map.h @@ -27,6 +27,8 @@ #include "selfdrive/common/util.h" #include "cereal/messaging/messaging.h" +const QString MAPS_HOST = util::getenv("MAPS_HOST", "https://maps.comma.ai").c_str(); + class MapInstructions : public QWidget { Q_OBJECT diff --git a/selfdrive/ui/qt/onroad.cc b/selfdrive/ui/qt/onroad.cc index 30bc7fe4f3..01ace08b37 100644 --- a/selfdrive/ui/qt/onroad.cc +++ b/selfdrive/ui/qt/onroad.cc @@ -5,6 +5,7 @@ #include "selfdrive/common/timing.h" #include "selfdrive/ui/paint.h" #include "selfdrive/ui/qt/util.h" +#include "selfdrive/ui/qt/api.h" #ifdef ENABLE_MAPS #include "selfdrive/ui/qt/maps/map.h" #endif @@ -78,12 +79,16 @@ void OnroadWindow::mousePressEvent(QMouseEvent* e) { void OnroadWindow::offroadTransition(bool offroad) { #ifdef ENABLE_MAPS if (!offroad) { - QString token = QString::fromStdString(Params().get("MapboxToken")); - if (map == nullptr && !token.isEmpty()) { + if (map == nullptr && QUIState::ui_state.has_prime) { QMapboxGLSettings settings; + + // Valid for 4 weeks since we can't swap tokens on the fly + QString token = CommaApi::create_jwt({}, 4 * 7 * 24 * 3600); + if (!Hardware::PC()) { settings.setCacheDatabasePath("/data/mbgl-cache.db"); } + settings.setApiBaseUrl(MAPS_HOST); settings.setCacheDatabaseMaximumSize(20 * 1024 * 1024); settings.setAccessToken(token.trimmed()); diff --git a/selfdrive/ui/qt/widgets/prime.cc b/selfdrive/ui/qt/widgets/prime.cc index 2be91b5d16..b1a12fa1f3 100644 --- a/selfdrive/ui/qt/widgets/prime.cc +++ b/selfdrive/ui/qt/widgets/prime.cc @@ -314,6 +314,7 @@ void SetupWidget::replyFinished(const QString &response) { if (!json["prime"].toBool()) { mainLayout->setCurrentWidget(primeAd); } else { + QUIState::ui_state.has_prime = true; mainLayout->setCurrentWidget(primeUser); } } diff --git a/selfdrive/ui/ui.h b/selfdrive/ui/ui.h index 9930cb84a6..6bea6ad610 100644 --- a/selfdrive/ui/ui.h +++ b/selfdrive/ui/ui.h @@ -55,7 +55,7 @@ typedef struct Alert { } } Alert; -const Alert CONTROLS_WAITING_ALERT = {"openpilot Unavailable", "Waiting for controls to start", +const Alert CONTROLS_WAITING_ALERT = {"openpilot Unavailable", "Waiting for controls to start", "controlsWaiting", cereal::ControlsState::AlertSize::MID, AudibleAlert::NONE}; @@ -130,6 +130,7 @@ typedef struct UIState { UIScene scene = {}; bool awake; + bool has_prime = false; float car_space_transform[6]; bool wide_camera;