* @brief Runs the gs_usb handshake: host format, bit timing, and start mode. */
| 707 | * @brief Runs the gs_usb handshake: host format, bit timing, and start mode. |
| 708 | */ |
| 709 | bool IO::Drivers::GsUsbCanBackend::configureDevice(quint32 bitrate) |
| 710 | { |
| 711 | Q_ASSERT(m_handle != nullptr); |
| 712 | |
| 713 | GsHostConfig config{kHostFormatLE}; |
| 714 | if (controlOut(kBreqHostFormat, 1, &config, sizeof(config)) < 0) { |
| 715 | setError(tr("CANable adapter rejected the host-format handshake."), |
| 716 | QCanBusDevice::ConfigurationError); |
| 717 | return false; |
| 718 | } |
| 719 | |
| 720 | GsDeviceBtConst bt{}; |
| 721 | if (controlIn(kBreqBtConst, 0, &bt, sizeof(bt)) < 0) { |
| 722 | setError(tr("Could not read CANable timing constants."), QCanBusDevice::ConfigurationError); |
| 723 | return false; |
| 724 | } |
| 725 | |
| 726 | GsDeviceBitTiming timing{}; |
| 727 | if (!solveBitTiming(bt, bitrate, timing)) { |
| 728 | setError(tr("The bitrate %1 bps is not supported by this CANable adapter.").arg(bitrate), |
| 729 | QCanBusDevice::ConfigurationError); |
| 730 | return false; |
| 731 | } |
| 732 | |
| 733 | if (controlOut(kBreqBitTiming, 0, &timing, sizeof(timing)) < 0) { |
| 734 | setError(tr("CANable adapter rejected the requested bitrate."), |
| 735 | QCanBusDevice::ConfigurationError); |
| 736 | return false; |
| 737 | } |
| 738 | |
| 739 | std::uint32_t modeFlags = kModeBerrReporting; |
| 740 | if (configurationParameter(QCanBusDevice::LoopbackKey).toBool()) |
| 741 | modeFlags |= kModeLoopBack; |
| 742 | |
| 743 | if (configurationParameter(IO::Drivers::kListenOnlyConfigKey).toBool()) |
| 744 | modeFlags |= kModeListenOnly; |
| 745 | |
| 746 | GsDeviceMode mode{kModeStart, modeFlags}; |
| 747 | if (controlOut(kBreqMode, 0, &mode, sizeof(mode)) < 0) { |
| 748 | setError(tr("Could not start the CANable channel."), QCanBusDevice::ConfigurationError); |
| 749 | return false; |
| 750 | } |
| 751 | |
| 752 | return true; |
| 753 | } |
| 754 | |
| 755 | /** |
| 756 | * @brief Finds the vendor interface and its bulk IN/OUT endpoints, then claims it. |
nothing calls this directly
no test coverage detected