* @brief Applies multiple driver connection properties to a source in one call. */
| 358 | * @brief Applies multiple driver connection properties to a source in one call. |
| 359 | */ |
| 360 | API::CommandResponse API::Handlers::SourceHandler::sourceConfigure(const QString& id, |
| 361 | const QJsonObject& params) |
| 362 | { |
| 363 | if (!params.contains(Keys::SourceId) || !params.contains(QStringLiteral("settings"))) |
| 364 | return CommandResponse::makeError( |
| 365 | id, QStringLiteral("MISSING_PARAM"), QStringLiteral("sourceId and settings are required")); |
| 366 | |
| 367 | const int sourceId = params[Keys::SourceId].toInt(-1); |
| 368 | const auto& model = DataModel::ProjectModel::instance(); |
| 369 | const int sourceCount = static_cast<int>(model.sources().size()); |
| 370 | |
| 371 | if (sourceId < 0 || sourceId >= sourceCount) |
| 372 | return CommandResponse::makeError( |
| 373 | id, QStringLiteral("INVALID_PARAM"), QStringLiteral("Invalid sourceId")); |
| 374 | |
| 375 | const QJsonObject settings = params[QStringLiteral("settings")].toObject(); |
| 376 | const bool usesUiDriver = sourceId == 0 && sourceCount == 1 |
| 377 | && AppState::instance().operationMode() == SerialStudio::ProjectFile; |
| 378 | |
| 379 | if (usesUiDriver) { |
| 380 | for (auto it = settings.constBegin(); it != settings.constEnd(); ++it) |
| 381 | IO::ConnectionManager::instance().setUiDriverProperty(it.key(), it.value().toVariant()); |
| 382 | |
| 383 | DataModel::ProjectModel::instance().captureSourceSettings(sourceId); |
| 384 | return CommandResponse::makeSuccess(id); |
| 385 | } |
| 386 | |
| 387 | IO::HAL_Driver* driver = IO::ConnectionManager::instance().driverForEditing(sourceId); |
| 388 | if (!driver) |
| 389 | return CommandResponse::makeError( |
| 390 | id, QStringLiteral("OPERATION_FAILED"), QStringLiteral("No driver for source")); |
| 391 | |
| 392 | for (auto it = settings.constBegin(); it != settings.constEnd(); ++it) |
| 393 | driver->setDriverProperty(it.key(), it.value().toVariant()); |
| 394 | |
| 395 | DataModel::ProjectModel::instance().captureSourceSettings(sourceId); |
| 396 | |
| 397 | IO::HAL_Driver* live = IO::ConnectionManager::instance().driver(sourceId); |
| 398 | if (live && live != driver) |
| 399 | for (auto it = settings.constBegin(); it != settings.constEnd(); ++it) |
| 400 | live->setDriverProperty(it.key(), it.value().toVariant()); |
| 401 | |
| 402 | return CommandResponse::makeSuccess(id); |
| 403 | } |
| 404 | |
| 405 | /** |
| 406 | * @brief Sets a driver connection property for a source. |
nothing calls this directly
no test coverage detected