Skip to content

Commit

Permalink
Allow to change to 230400 bdps only in MTB-USB FW >=v1.3.
Browse files Browse the repository at this point in the history
  • Loading branch information
horacekj committed Nov 16, 2023
1 parent 34dde11 commit 65984d6
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 7 deletions.
7 changes: 4 additions & 3 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,20 +164,21 @@ void DaemonCoreApplication::mtbUsbOnConnect() {
}

void DaemonCoreApplication::mtbUsbGotInfo() {
const Mtb::MtbUsbInfo& mtbusbinfo = mtbusb.mtbUsbInfo().value();
const QJsonObject& mtbusbObj = this->config["mtb-usb"].toObject();
if (!mtbusbObj.contains("speed"))
return this->mtbUsbProperSpeedSet();

const int fileSpeed = mtbusbObj["speed"].toInt();
if (!Mtb::mtbBusSpeedValid(fileSpeed)) {
if (!Mtb::mtbBusSpeedValid(fileSpeed, mtbusbinfo.fw_raw())) {
log("Invalid MTBbus speed in config file: "+QString::number(mtbusbObj["speed"].toInt()),
Mtb::LogLevel::Warning);
return this->mtbUsbProperSpeedSet();
}

Mtb::MtbBusSpeed newSpeed = Mtb::intToMtbBusSpeed(fileSpeed);

if (newSpeed == mtbusb.mtbUsbInfo().value().speed) {
if (newSpeed == mtbusbinfo.speed) {
log("Saved MTBbus speed matches current MTB-USB speed, ok.", Mtb::LogLevel::Info);
return this->mtbUsbProperSpeedSet();
}
Expand Down Expand Up @@ -353,7 +354,7 @@ void DaemonCoreApplication::serverReceived(QTcpSocket *socket, const QJsonObject
if (!mtbusb.connected() || !mtbusb.mtbUsbInfo().has_value())
return sendError(socket, request, MTB_DEVICE_DISCONNECTED, "Disconnected from MTB-USB!");
size_t speed = jsonMtbUsb["speed"].toInt();
if (!Mtb::mtbBusSpeedValid(speed))
if (!Mtb::mtbBusSpeedValid(speed, mtbusb.mtbUsbInfo().value().fw_raw()))
return sendError(socket, request, MTB_INVALID_SPEED, "Invalid MTBbus speed!");
Mtb::MtbBusSpeed mtbUsbSpeed = mtbusb.mtbUsbInfo().value().speed;
Mtb::MtbBusSpeed newSpeed = Mtb::intToMtbBusSpeed(speed);
Expand Down
5 changes: 3 additions & 2 deletions src/mtbusb/mtbusb-common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ MtbBusSpeed intToMtbBusSpeed(int speed) {
return MtbBusSpeed::br38400;
}

bool mtbBusSpeedValid(int speed) {
return (speed == 38400) || (speed == 57600) || (speed == 115200) || (speed == 230400);
bool mtbBusSpeedValid(int speed, uint16_t mtbusbFWver) {
return ((speed == 38400) || (speed == 57600) || (speed == 115200) ||
((speed == 230400) && (mtbusbFWver >= 0x0103)));
}

bool isValidModuleAddress(size_t addr) {
Expand Down
4 changes: 2 additions & 2 deletions src/mtbusb/mtbusb-common.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ enum class MtbBusSpeed {
br38400 = 1,
br57600 = 2,
br115200 = 3,
br230400 = 4,
br230400 = 4, // only from MTB-USB FW v1.3
};

int mtbBusSpeedToInt(MtbBusSpeed speed);
MtbBusSpeed intToMtbBusSpeed(int speed);
bool mtbBusSpeedValid(int speed);
bool mtbBusSpeedValid(int speed, uint16_t mtbusbFWver);

struct EInvalidAddress : public MtbUsbError {
EInvalidAddress() : MtbUsbError(std::string("Invalid MTBbus module address!")) {}
Expand Down

0 comments on commit 65984d6

Please sign in to comment.