diff --git a/kuksa-can-provider b/kuksa-can-provider new file mode 160000 index 000000000..34b71ced4 --- /dev/null +++ b/kuksa-can-provider @@ -0,0 +1 @@ +Subproject commit 34b71ced42175ac68b8eecfd513501cb4cab4cf2 diff --git a/qt_app/CMakeLists.txt b/qt_app/CMakeLists.txt index c71826cfe..dad761bd5 100644 --- a/qt_app/CMakeLists.txt +++ b/qt_app/CMakeLists.txt @@ -9,6 +9,33 @@ set(CMAKE_AUTOUIC ON) # Use Qt6 find_package(Qt6 REQUIRED COMPONENTS Core Gui Quick Qml QuickControls2 SerialBus) +# --- Find Kuksa Middleware Dependencies --- +find_package(Protobuf REQUIRED) +find_package(gRPC REQUIRED) +find_program(gRPC_PLUGIN_EXECUTABLE grpc_cpp_plugin) # Tool to generate network code +find_package(absl REQUIRED) # Google common libraries (Critical dependency) + +# --- Protocol Buffers Code Generation --- +# Define paths for input (.proto) and output (generated C++) +set(PROTO_DIR "${CMAKE_CURRENT_SOURCE_DIR}/proto") +set(PROTO_SRCS "${PROTO_DIR}/val.proto" "${PROTO_DIR}/types.proto") +set(GEN_DIR "${CMAKE_CURRENT_BINARY_DIR}/generated") +file(MAKE_DIRECTORY ${GEN_DIR}) + +# Command to auto-generate C++ sources from .proto files before compilation +add_custom_command( + OUTPUT "${GEN_DIR}/val.pb.cc" "${GEN_DIR}/val.pb.h" + "${GEN_DIR}/val.grpc.pb.cc" "${GEN_DIR}/val.grpc.pb.h" + "${GEN_DIR}/types.pb.cc" "${GEN_DIR}/types.pb.h" + # Step 1: Generate gRPC Network Stubs + COMMAND ${Protobuf_PROTOC_EXECUTABLE} + ARGS --grpc_out=${GEN_DIR} --plugin=protoc-gen-grpc=${gRPC_PLUGIN_EXECUTABLE} -I${PROTO_DIR} ${PROTO_SRCS} + # Step 2: Generate Data Serialization Classes + COMMAND ${Protobuf_PROTOC_EXECUTABLE} + ARGS --cpp_out=${GEN_DIR} -I${PROTO_DIR} ${PROTO_SRCS} + DEPENDS ${PROTO_SRCS} +) + file(GLOB_RECURSE APP_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp" ) @@ -17,6 +44,7 @@ add_executable(myqtapp ${APP_SOURCES}) target_include_directories(myqtapp PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include + ${GEN_DIR} ) target_sources(myqtapp PRIVATE @@ -24,9 +52,20 @@ target_sources(myqtapp PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/resources/resources.qrc" "${CMAKE_CURRENT_SOURCE_DIR}/include/vehicledata.hpp" "${CMAKE_CURRENT_SOURCE_DIR}/include/canreader.hpp" + "${CMAKE_CURRENT_SOURCE_DIR}/include/kuksareader.hpp" + "${GEN_DIR}/val.pb.cc" + "${GEN_DIR}/val.grpc.pb.cc" + "${GEN_DIR}/types.pb.cc" ) -target_link_libraries(myqtapp PRIVATE Qt6::Core Qt6::Gui Qt6::Quick Qt6::Qml Qt6::QuickControls2 Qt6::SerialBus) +target_link_libraries(myqtapp PRIVATE + Qt6::Core Qt6::Gui Qt6::Quick Qt6::Qml Qt6::QuickControls2 Qt6::SerialBus + gRPC::grpc++ # The gRPC Transport Layer + protobuf::libprotobuf # The Serialization Library + absl::base # Abseil Base (Required by gRPC) + absl::strings # Abseil Strings + absl::log_internal_check_op # <--- Critical fix for "Undefined Symbol" linker errors +) install(TARGETS myqtapp RUNTIME DESTINATION bin) diff --git a/qt_app/include/kuksareader.hpp b/qt_app/include/kuksareader.hpp new file mode 100644 index 000000000..f06dd3f72 --- /dev/null +++ b/qt_app/include/kuksareader.hpp @@ -0,0 +1,31 @@ +#ifndef KUKSAREADER_HPP +#define KUKSAREADER_HPP + +#include +#include +#include +#include "val.grpc.pb.h" + +// KUKSA Val gRPC namespace to avoid long names +using namespace kuksa::val::v1; + +class KUKSAReader : public QObject +{ + Q_OBJECT +public: + explicit KUKSAReader(QObject *parent = nullptr); + ~KUKSAReader() override; +public slots: + // Starts the data fetching process + void start(); + +signals: + // Emitted when new speed data is received + void speedReceived(float speed); + +private: + // gRPC client stub -> used to communicate with KUKSA Val server + std::unique_ptr m_stub_; +}; + +#endif // KUKSAREADER_HPP \ No newline at end of file diff --git a/qt_app/include/vehicledata.hpp b/qt_app/include/vehicledata.hpp index da278c96c..45beb5b56 100644 --- a/qt_app/include/vehicledata.hpp +++ b/qt_app/include/vehicledata.hpp @@ -50,6 +50,9 @@ class VehicleData : public QObject Q_INVOKABLE void changeGearUp(); Q_INVOKABLE void changeGearDown(); + // Thread-safe handlers for incoming data + void handleSpeedUpdate(float speed); + public slots: // Called by CANReader (queued connection) with raw payload + canId void handleCanMessage(const QByteArray &payload, uint32_t canId); diff --git a/qt_app/proto/types.proto b/qt_app/proto/types.proto new file mode 100644 index 000000000..8914e7aff --- /dev/null +++ b/qt_app/proto/types.proto @@ -0,0 +1,288 @@ +/******************************************************************************** + * Copyright (c) 2022 Contributors to the Eclipse Foundation + * + * See the NOTICE file(s) distributed with this work for additional + * information regarding copyright ownership. + * + * This program and the accompanying materials are made available under the + * terms of the Apache License 2.0 which is available at + * http://www.apache.org/licenses/LICENSE-2.0 + * + * SPDX-License-Identifier: Apache-2.0 + ********************************************************************************/ + +syntax = "proto3"; + +// I added V1 as in databroker. Is this good practice? +package kuksa.val.v1; +import "google/protobuf/timestamp.proto"; + +option go_package = "kuksa/val/v1"; + +// Describes a VSS entry +// When requesting an entry, the amount of information returned can +// be controlled by specifying either a `View` or a set of `Field`s. +message DataEntry { + // Defines the full VSS path of the entry. + string path = 1; // [field: FIELD_PATH] + + // The value (datapoint) + Datapoint value = 2; // [field: FIELD_VALUE] + + // Actuator target (only used if the entry is an actuator) + Datapoint actuator_target = 3; // [field: FIELD_ACTUATOR_TARGET] + + // Metadata for this entry + Metadata metadata = 10; // [field: FIELD_METADATA] +} + +message Datapoint { + google.protobuf.Timestamp timestamp = 1; + + oneof value { + string string = 11; + bool bool = 12; + sint32 int32 = 13; + sint64 int64 = 14; + uint32 uint32 = 15; + uint64 uint64 = 16; + float float = 17; + double double = 18; + StringArray string_array = 21; + BoolArray bool_array = 22; + Int32Array int32_array = 23; + Int64Array int64_array = 24; + Uint32Array uint32_array = 25; + Uint64Array uint64_array = 26; + FloatArray float_array = 27; + DoubleArray double_array = 28; + } +} + +message Metadata { + // Data type + // The VSS data type of the entry (i.e. the value, min, max etc). + // + // NOTE: protobuf doesn't have int8, int16, uint8 or uint16 which means + // that these values must be serialized as int32 and uint32 respectively. + DataType data_type = 11; // [field: FIELD_METADATA_DATA_TYPE] + + // Entry type + EntryType entry_type = 12; // [field: FIELD_METADATA_ENTRY_TYPE] + + // Description + // Describes the meaning and content of the entry. + optional string description = 13; // [field: FIELD_METADATA_DESCRIPTION] + + // Comment [optional] + // A comment can be used to provide additional informal information + // on a entry. + optional string comment = 14; // [field: FIELD_METADATA_COMMENT] + + // Deprecation [optional] + // Whether this entry is deprecated. Can contain recommendations of what + // to use instead. + optional string deprecation = 15; // [field: FIELD_METADATA_DEPRECATION] + + // Unit [optional] + // The unit of measurement + optional string unit = 16; // [field: FIELD_METADATA_UNIT] + + // Value restrictions [optional] + // Restrict which values are allowed. + // Only restrictions matching the DataType {datatype} above are valid. + ValueRestriction value_restriction = 17; // [field: FIELD_METADATA_VALUE_RESTRICTION] + + // Entry type specific metadata + oneof entry_specific { + Actuator actuator = 20; // [field: FIELD_METADATA_ACTUATOR] + Sensor sensor = 30; // [field: FIELD_METADATA_SENSOR] + Attribute attribute = 40; // [field: FIELD_METADATA_ATTRIBUTE] + } +} + +/////////////////////// +// Actuator specific fields +message Actuator { + // Nothing for now +} + +//////////////////////// +// Sensor specific +message Sensor { + // Nothing for now +} + +//////////////////////// +// Attribute specific +message Attribute { + // Nothing for now. +} + +// Value restriction +// +// One ValueRestriction{type} for each type, since +// they don't make sense unless the types match +// +message ValueRestriction { + oneof type { + ValueRestrictionString string = 21; + // For signed VSS integers + ValueRestrictionInt signed = 22; + // For unsigned VSS integers + ValueRestrictionUint unsigned = 23; + // For floating point VSS values (float and double) + ValueRestrictionFloat floating_point = 24; + } +} + +message ValueRestrictionInt { + optional sint64 min = 1; + optional sint64 max = 2; + repeated sint64 allowed_values = 3; +} + +message ValueRestrictionUint { + optional uint64 min = 1; + optional uint64 max = 2; + repeated uint64 allowed_values = 3; +} + +message ValueRestrictionFloat { + optional double min = 1; + optional double max = 2; + + // allowed for doubles/floats not recommended + repeated double allowed_values = 3; +} + +// min, max doesn't make much sense for a string +message ValueRestrictionString { + repeated string allowed_values = 3; +} + +// VSS Data type of a signal +// +// Protobuf doesn't support int8, int16, uint8 or uint16. +// These are mapped to int32 and uint32 respectively. +// +enum DataType { + DATA_TYPE_UNSPECIFIED = 0; + DATA_TYPE_STRING = 1; + DATA_TYPE_BOOLEAN = 2; + DATA_TYPE_INT8 = 3; + DATA_TYPE_INT16 = 4; + DATA_TYPE_INT32 = 5; + DATA_TYPE_INT64 = 6; + DATA_TYPE_UINT8 = 7; + DATA_TYPE_UINT16 = 8; + DATA_TYPE_UINT32 = 9; + DATA_TYPE_UINT64 = 10; + DATA_TYPE_FLOAT = 11; + DATA_TYPE_DOUBLE = 12; + DATA_TYPE_TIMESTAMP = 13; + DATA_TYPE_STRING_ARRAY = 20; + DATA_TYPE_BOOLEAN_ARRAY = 21; + DATA_TYPE_INT8_ARRAY = 22; + DATA_TYPE_INT16_ARRAY = 23; + DATA_TYPE_INT32_ARRAY = 24; + DATA_TYPE_INT64_ARRAY = 25; + DATA_TYPE_UINT8_ARRAY = 26; + DATA_TYPE_UINT16_ARRAY = 27; + DATA_TYPE_UINT32_ARRAY = 28; + DATA_TYPE_UINT64_ARRAY = 29; + DATA_TYPE_FLOAT_ARRAY = 30; + DATA_TYPE_DOUBLE_ARRAY = 31; + DATA_TYPE_TIMESTAMP_ARRAY = 32; +} + +// Entry type +enum EntryType { + ENTRY_TYPE_UNSPECIFIED = 0; + ENTRY_TYPE_ATTRIBUTE = 1; + ENTRY_TYPE_SENSOR = 2; + ENTRY_TYPE_ACTUATOR = 3; +} + +// A `View` specifies a set of fields which should +// be populated in a `DataEntry` (in a response message) +enum View { + VIEW_UNSPECIFIED = 0; // Unspecified. Equivalent to VIEW_CURRENT_VALUE unless `fields` are explicitly set. + VIEW_CURRENT_VALUE = 1; // Populate DataEntry with value. + VIEW_TARGET_VALUE = 2; // Populate DataEntry with actuator target. + VIEW_METADATA = 3; // Populate DataEntry with metadata. + VIEW_FIELDS = 10; // Populate DataEntry only with requested fields. + VIEW_ALL = 20; // Populate DataEntry with everything. +} + +// A `Field` corresponds to a specific field of a `DataEntry`. +// +// It can be used to: +// * populate only specific fields of a `DataEntry` response. +// * specify which fields of a `DataEntry` should be set as +// part of a `Set` request. +// * subscribe to only specific fields of a data entry. +// * convey which fields of an updated `DataEntry` have changed. +enum Field { + FIELD_UNSPECIFIED = 0; // "*" i.e. everything + FIELD_PATH = 1; // path + FIELD_VALUE = 2; // value + FIELD_ACTUATOR_TARGET = 3; // actuator_target + FIELD_METADATA = 10; // metadata.* + FIELD_METADATA_DATA_TYPE = 11; // metadata.data_type + FIELD_METADATA_DESCRIPTION = 12; // metadata.description + FIELD_METADATA_ENTRY_TYPE = 13; // metadata.entry_type + FIELD_METADATA_COMMENT = 14; // metadata.comment + FIELD_METADATA_DEPRECATION = 15; // metadata.deprecation + FIELD_METADATA_UNIT = 16; // metadata.unit + FIELD_METADATA_VALUE_RESTRICTION = 17; // metadata.value_restriction.* + FIELD_METADATA_ACTUATOR = 20; // metadata.actuator.* + FIELD_METADATA_SENSOR = 30; // metadata.sensor.* + FIELD_METADATA_ATTRIBUTE = 40; // metadata.attribute.* +} + +// Error response shall be an HTTP-like code. +// Should follow https://www.w3.org/TR/viss2-transport/#status-codes. +message Error { + uint32 code = 1; + string reason = 2; + string message = 3; +} + +// Used in get/set requests to report errors for specific entries +message DataEntryError { + string path = 1; // vss path + Error error = 2; +} + +message StringArray { + repeated string values = 1; +} + +message BoolArray { + repeated bool values = 1; +} + +message Int32Array { + repeated sint32 values = 1; +} + +message Int64Array { + repeated sint64 values = 1; +} + +message Uint32Array { + repeated uint32 values = 1; +} + +message Uint64Array { + repeated uint64 values = 1; +} + +message FloatArray { + repeated float values = 1; +} + +message DoubleArray { + repeated double values = 1; +} diff --git a/qt_app/proto/val.proto b/qt_app/proto/val.proto new file mode 100644 index 000000000..26f72d59a --- /dev/null +++ b/qt_app/proto/val.proto @@ -0,0 +1,126 @@ +/******************************************************************************** + * Copyright (c) 2022 Contributors to the Eclipse Foundation + * + * See the NOTICE file(s) distributed with this work for additional + * information regarding copyright ownership. + * + * This program and the accompanying materials are made available under the + * terms of the Apache License 2.0 which is available at + * http://www.apache.org/licenses/LICENSE-2.0 + * + * SPDX-License-Identifier: Apache-2.0 + ********************************************************************************/ + +syntax = "proto3"; + +package kuksa.val.v1; + +option go_package = "kuksa/val/v1"; + +import "types.proto"; + +// Note on authorization: +// Tokens (auth-token or auth-uuid) are sent as (GRPC / http2) metadata. +// +// The auth-token is a JWT compliant token as the examples found here: +// https://github.com/eclipse-kuksa/kuksa-databroker/tree/main/certificates/jwt +// +// See also https://github.com/eclipse-kuksa/kuksa-databroker/blob/main/doc/authorization.md#jwt-access-token +// +// Upon reception of auth-token, server shall generate an auth-uuid in metadata +// that the client can use instead of auth-token in subsequent calls. + +service VAL { + // Get entries + rpc Get(GetRequest) returns (GetResponse); + + // Set entries + rpc Set(SetRequest) returns (SetResponse); + + rpc StreamedUpdate(stream StreamedUpdateRequest) returns (stream StreamedUpdateResponse); + + // Subscribe to a set of entries + // + // Returns a stream of notifications. + // + // InvalidArgument is returned if the request is malformed. + rpc Subscribe(SubscribeRequest) returns (stream SubscribeResponse); + + // Shall return information that allows the client to determine + // what server/server implementation/version it is talking to + // eg. kuksa-databroker 0.5.1 + rpc GetServerInfo(GetServerInfoRequest) returns (GetServerInfoResponse); +} + +// Define which data we want +message EntryRequest { + string path = 1; + View view = 2; + repeated Field fields = 3; +} + +// Request a set of entries. +message GetRequest { + repeated EntryRequest entries = 1; +} + +// Global errors are specified in `error`. +// Errors for individual entries are specified in `errors`. +message GetResponse { + repeated DataEntry entries = 1; + repeated DataEntryError errors = 2; + Error error = 3; +} + +// Define the data we want to set +message EntryUpdate { + DataEntry entry = 1; + repeated Field fields = 2; +} + +// A list of entries to be updated +message SetRequest { + repeated EntryUpdate updates = 1; +} + +// Global errors are specified in `error`. +// Errors for individual entries are specified in `errors`. +message SetResponse { + Error error = 1; + repeated DataEntryError errors = 2; +} + +message StreamedUpdateRequest { + repeated EntryUpdate updates = 1; +} + +message StreamedUpdateResponse { + Error error = 1; + repeated DataEntryError errors = 2; +} + +// Define what to subscribe to +message SubscribeEntry { + string path = 1; + View view = 2; + repeated Field fields = 3; +} + +// Subscribe to changes in datapoints. +message SubscribeRequest { + repeated SubscribeEntry entries = 1; +} + +// A subscription response +message SubscribeResponse { + repeated EntryUpdate updates = 1; +} + +message GetServerInfoRequest { + // Nothing yet +} + +message GetServerInfoResponse { + string name = 1; + string version = 2; +} diff --git a/qt_app/src/canreader.cpp b/qt_app/src/canreader.cpp index 07e664fa2..83134a836 100644 --- a/qt_app/src/canreader.cpp +++ b/qt_app/src/canreader.cpp @@ -1,4 +1,6 @@ #include "canreader.hpp" +//QDateTime for latency testing - remove later +#include CANReader *reader0 = new CANReader(QStringLiteral("can1")); @@ -81,6 +83,12 @@ void CANReader::handleFramesReceived() QCanBusFrame frame = m_device->readFrame(); QByteArray payload = frame.payload(); uint32_t canId = static_cast(frame.frameId()); + + //LATENCY TESTING CODE - REMOVE LATER + qint64 t1 = QDateTime::currentMSecsSinceEpoch(); + qDebug() << "CANReader: Received CAN frame ID=0x" << QString::number(canId, 16) << " at " << t1; + //END LATENCY TESTING CODE + emit canMessageReceived(payload, canId); qDebug() << "Received CAN frame: ID=0x" << QString::number(canId, 16) << " Payload=" << payload.toHex(); } diff --git a/qt_app/src/kuksareader.cpp b/qt_app/src/kuksareader.cpp new file mode 100644 index 000000000..57fffaf89 --- /dev/null +++ b/qt_app/src/kuksareader.cpp @@ -0,0 +1,51 @@ +#include "kuksareader.hpp" +//QDateTime for latency testing - remove later +#include + +KUKSAReader::KUKSAReader(QObject *parent) + : QObject(parent) +{} + +KUKSAReader::~KUKSAReader() +{} + +void KUKSAReader::start() +{ + //1. Connect to Broker auto channel + auto channel = grpc::CreateChannel("localhost:55555", grpc::InsecureChannelCredentials()); + m_stub_ = VAL::NewStub(channel); + + //2. Subscribe to speed data + grpc::ClientContext context; + SubscribeRequest request; + + auto *entry = request.add_entries(); + entry->set_path("Vehicle.Speed"); + entry->add_fields(Field::FIELD_VALUE); + + std::unique_ptr> reader( + m_stub_->Subscribe(&context, request)); + + SubscribeResponse response; + qDebug() << "KuksaReader: Connected and Subscribed to Vehicle.Speed"; + + //3. Loop to read incoming speed data (blocking call so run in separate thread (QThread)) + while(reader->Read(&response)) + { + for (const auto &update : response.updates()) + { + if (update.entry().path() == "Vehicle.Speed") + { + float speed = update.entry().value().float_(); + + //LATENCY TESTING CODE - REMOVE LATER + qint64 t1 = QDateTime::currentMSecsSinceEpoch(); + qDebug() << "KuksaReader: Received speed:" << speed << " at " << t1; + //END LATENCY TESTING CODE + + emit speedReceived(speed); + } + } + } +} + diff --git a/qt_app/src/main.cpp b/qt_app/src/main.cpp index 4f1b37ea4..affd609b3 100644 --- a/qt_app/src/main.cpp +++ b/qt_app/src/main.cpp @@ -5,64 +5,95 @@ #include #include #include +#include +#include #include "vehicledata.hpp" #include "canreader.hpp" +#include "kuksareader.hpp" int main(int argc, char *argv[]) { QGuiApplication app(argc, argv); - - // Create QML engine + app.setApplicationName("DrivaPi Dashboard"); + + // --- 1. ARGUMENT PARSING --- + QCommandLineParser parser; + parser.setApplicationDescription("Hybrid Dashboard (CAN / Kuksa)"); + parser.addHelpOption(); + + // Define the "--kuksa" or "-k" option + QCommandLineOption kuksaOption(QStringList() << "k" << "kuksa", + "Enable Kuksa mode (gRPC). Defaults to CAN if omitted."); + parser.addOption(kuksaOption); + + parser.process(app); + + // Check if the user passed the argument + bool useKuksa = parser.isSet(kuksaOption); + + // UI and Engine setup QQmlApplicationEngine engine; - - // Create VehicleData using QScopedPointer for automatic cleanup QScopedPointer vehicleData(new VehicleData()); - // Expose VehicleData to QML (keep ownership in C++) engine.rootContext()->setContextProperty("vehicleData", vehicleData.data()); - // Create CANReader and move it to its own thread if needed - QThread *canThread = new QThread(&app); - - // Use a raw pointer for the worker object (we call deleteLater on it) - CANReader *canReader = new CANReader(QStringLiteral("can1")); - canReader->moveToThread(canThread); - - // Start CANReader when thread starts - QObject::connect(canThread, &QThread::started, canReader, &CANReader::start); - // Ensure worker object is deleted when thread finishes (safe because it's a QObject) - QObject::connect(canThread, &QThread::finished, canReader, &CANReader::deleteLater); + // Worker thread setup for data reading + QThread *workerThread = new QThread(&app); - // Forward CAN messages from thread worker to UI (thread-safe) - QObject::connect(canReader, &CANReader::canMessageReceived, - vehicleData.data(), &VehicleData::handleCanMessage, - Qt::QueuedConnection); + // Pointers to readers for cleanup + CANReader *canReader = nullptr; + KUKSAReader *kuksaReader = nullptr; - // Handle CANReader errors (lambda - no capture needed here) - QObject::connect(canReader, &CANReader::errorOccurred, [](const QString &msg) + if (useKuksa) { - qWarning() << "CANReader error:" << msg; - }); - - // Start the CAN thread - canThread->start(); - - // Clean up thread and worker on app exit. Capture raw pointers by value. - QObject::connect(&app, &QCoreApplication::aboutToQuit, [canThread, canReader]() { - // ask the thread worker to stop reading CAN messages (queued to worker thread) - QMetaObject::invokeMethod(canReader, "stop", Qt::QueuedConnection); + qInfo() << "Starting in KUKSA mode"; + // KUKSA Reader setup + kuksaReader = new KUKSAReader(); + kuksaReader->moveToThread(workerThread); + // Start KUKSAReader when thread starts + QObject::connect(workerThread, &QThread::started, kuksaReader, &KUKSAReader::start); + // Ensure worker object is deleted when thread finishes (safe because it's a QObject) + QObject::connect(workerThread, &QThread::finished, kuksaReader, &KUKSAReader::deleteLater); + // Forward speed data from KUKSAReader to VehicleData (thread-safe) + QObject::connect(kuksaReader, &KUKSAReader::speedReceived, + vehicleData.data(), &VehicleData::handleSpeedUpdate); + } else { + qInfo() << "Starting in CAN mode"; + // CAN Reader setup + canReader = new CANReader(QStringLiteral("vcan0")); + canReader->moveToThread(workerThread); + // Start CANReader when thread starts + QObject::connect(workerThread, &QThread::started, canReader, &CANReader::start); + // Ensure worker object is deleted when thread finishes (safe because it's a QObject) + QObject::connect(workerThread, &QThread::finished, canReader, &CANReader::deleteLater); + + // Forward CAN messages from thread worker to UI (thread-safe) + QObject::connect(canReader, &CANReader::canMessageReceived, + vehicleData.data(), &VehicleData::handleCanMessage, + Qt::QueuedConnection); + } - // request thread to quit its event loop - canThread->quit(); + workerThread->start(); - // wait for thread to finish - if (!canThread->wait(2000)) { - qWarning() << "CAN thread did not quit in 2 seconds, terminating"; - canThread->terminate(); - canThread->wait(); + // CLEANUP HANDLING + QObject::connect(&app, &QCoreApplication::aboutToQuit, [workerThread, canReader, kuksaReader]() { + if (canReader) + { + // ask CAN reader to stop + QMetaObject::invokeMethod(canReader, "stop", Qt::QueuedConnection); + } + if (workerThread) + { + workerThread->quit(); + // wait for thread to finish + if (!workerThread->wait(2000)) { + qWarning() << "Worker thread did not quit in 2 seconds, terminating"; + workerThread->terminate(); + workerThread->wait(); + } + // delete the thread (worker will be deleted via deleteLater) + delete workerThread; } - // delete the thread (worker will be deleted via deleteLater) - delete canThread; }); // Load main QML file diff --git a/qt_app/src/vehicledata.cpp b/qt_app/src/vehicledata.cpp index 1bcf0b932..2834149e1 100644 --- a/qt_app/src/vehicledata.cpp +++ b/qt_app/src/vehicledata.cpp @@ -245,3 +245,11 @@ void VehicleData::checkStaleProperties() } // Repeat for other keys if you want special handling } + +// KUKSA speed update handler +void VehicleData::handleSpeedUpdate(float speed) +{ + setSpeed(speed); // updates timestamp inside + // debug + // qDebug() << "Updated speed from KUKSA (m/s):" << speed; +}