Skip to content

Commit

Permalink
i'm getting the 0003 but i need to notify the 0002
Browse files Browse the repository at this point in the history
it doesn't enter into the sendCharacteristicNotification loop
  • Loading branch information
cagnulein committed Dec 30, 2024
1 parent 4f7c7fa commit c835518
Show file tree
Hide file tree
Showing 8 changed files with 257 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,10 @@
87D5DC4228230496008CCDE7 /* moc_truetreadmill.cpp in Compile Sources */ = {isa = PBXBuildFile; fileRef = 87D5DC4128230496008CCDE7 /* moc_truetreadmill.cpp */; };
87D91F9A2800B9970026D43C /* proformwifibike.cpp in Compile Sources */ = {isa = PBXBuildFile; fileRef = 87D91F992800B9970026D43C /* proformwifibike.cpp */; };
87D91F9C2800B9B90026D43C /* moc_proformwifibike.cpp in Compile Sources */ = {isa = PBXBuildFile; fileRef = 87D91F9B2800B9B90026D43C /* moc_proformwifibike.cpp */; };
87DA62A42D2305E4008ADA0F /* moc_characteristicnotifier0002.cpp in Compile Sources */ = {isa = PBXBuildFile; fileRef = 87DA62A22D2305E4008ADA0F /* moc_characteristicnotifier0002.cpp */; };
87DA62A52D2305E4008ADA0F /* moc_characteristicwriteprocessor0003.cpp in Compile Sources */ = {isa = PBXBuildFile; fileRef = 87DA62A32D2305E4008ADA0F /* moc_characteristicwriteprocessor0003.cpp */; };
87DA62AA2D2305F5008ADA0F /* characteristicwriteprocessor0003.cpp in Compile Sources */ = {isa = PBXBuildFile; fileRef = 87DA62A92D2305F5008ADA0F /* characteristicwriteprocessor0003.cpp */; };
87DA62AB2D2305F5008ADA0F /* characteristicnotifier0002.cpp in Compile Sources */ = {isa = PBXBuildFile; fileRef = 87DA62A72D2305F5008ADA0F /* characteristicnotifier0002.cpp */; };
87DA76502848F98200A71B64 /* libQt5TextToSpeech.a in Link Binary With Libraries */ = {isa = PBXBuildFile; fileRef = 87DA764F2848F8F200A71B64 /* libQt5TextToSpeech.a */; };
87DA8465284933D200B550E9 /* fakeelliptical.cpp in Compile Sources */ = {isa = PBXBuildFile; fileRef = 87DA8464284933D200B550E9 /* fakeelliptical.cpp */; };
87DA8467284933DE00B550E9 /* moc_fakeelliptical.cpp in Compile Sources */ = {isa = PBXBuildFile; fileRef = 87DA8466284933DE00B550E9 /* moc_fakeelliptical.cpp */; };
Expand Down Expand Up @@ -1528,6 +1532,12 @@
87D91F982800B9970026D43C /* proformwifibike.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = proformwifibike.h; path = ../src/devices/proformwifibike/proformwifibike.h; sourceTree = "<group>"; };
87D91F992800B9970026D43C /* proformwifibike.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = proformwifibike.cpp; path = ../src/devices/proformwifibike/proformwifibike.cpp; sourceTree = "<group>"; };
87D91F9B2800B9B90026D43C /* moc_proformwifibike.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = moc_proformwifibike.cpp; sourceTree = "<group>"; };
87DA62A22D2305E4008ADA0F /* moc_characteristicnotifier0002.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = moc_characteristicnotifier0002.cpp; sourceTree = "<group>"; };
87DA62A32D2305E4008ADA0F /* moc_characteristicwriteprocessor0003.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = moc_characteristicwriteprocessor0003.cpp; sourceTree = "<group>"; };
87DA62A62D2305F5008ADA0F /* characteristicnotifier0002.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = characteristicnotifier0002.h; path = ../src/characteristics/characteristicnotifier0002.h; sourceTree = SOURCE_ROOT; };
87DA62A72D2305F5008ADA0F /* characteristicnotifier0002.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; name = characteristicnotifier0002.cpp; path = ../src/characteristics/characteristicnotifier0002.cpp; sourceTree = SOURCE_ROOT; };
87DA62A82D2305F5008ADA0F /* characteristicwriteprocessor0003.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = characteristicwriteprocessor0003.h; path = ../src/characteristics/characteristicwriteprocessor0003.h; sourceTree = SOURCE_ROOT; };
87DA62A92D2305F5008ADA0F /* characteristicwriteprocessor0003.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; name = characteristicwriteprocessor0003.cpp; path = ../src/characteristics/characteristicwriteprocessor0003.cpp; sourceTree = SOURCE_ROOT; };
87DA764F2848F8F200A71B64 /* libQt5TextToSpeech.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libQt5TextToSpeech.a; path = ../../Qt/5.15.2/ios/lib/libQt5TextToSpeech.a; sourceTree = "<group>"; };
87DA8463284933D200B550E9 /* fakeelliptical.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = fakeelliptical.h; path = ../src/devices/fakeelliptical/fakeelliptical.h; sourceTree = "<group>"; };
87DA8464284933D200B550E9 /* fakeelliptical.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = fakeelliptical.cpp; path = ../src/devices/fakeelliptical/fakeelliptical.cpp; sourceTree = "<group>"; };
Expand Down Expand Up @@ -2158,6 +2168,12 @@
2EB56BE3C2D93CDAB0C52E67 /* Sources */ = {
isa = PBXGroup;
children = (
87DA62A62D2305F5008ADA0F /* characteristicnotifier0002.h */,
87DA62A72D2305F5008ADA0F /* characteristicnotifier0002.cpp */,
87DA62A82D2305F5008ADA0F /* characteristicwriteprocessor0003.h */,
87DA62A92D2305F5008ADA0F /* characteristicwriteprocessor0003.cpp */,
87DA62A22D2305E4008ADA0F /* moc_characteristicnotifier0002.cpp */,
87DA62A32D2305E4008ADA0F /* moc_characteristicwriteprocessor0003.cpp */,
87EAC3D42D1D8D34004FE975 /* pitpatbike.h */,
87EAC3D52D1D8D34004FE975 /* pitpatbike.cpp */,
87EAC3D22D1D8D1D004FE975 /* moc_pitpatbike.cpp */,
Expand Down Expand Up @@ -3426,6 +3442,8 @@
873824BC27E64707004F1B46 /* moc_resolver.cpp in Compile Sources */,
87FA11AD27C5ECE4008AC5D1 /* moc_ultrasportbike.cpp in Compile Sources */,
871235BF26B297670012D0F2 /* kingsmithr1protreadmill.cpp in Compile Sources */,
87DA62AA2D2305F5008ADA0F /* characteristicwriteprocessor0003.cpp in Compile Sources */,
87DA62AB2D2305F5008ADA0F /* characteristicnotifier0002.cpp in Compile Sources */,
8772B7F72CB55E98004AB8E9 /* deerruntreadmill.cpp in Compile Sources */,
20A50533946A39CBD2C89104 /* bluetoothdevice.cpp in Compile Sources */,
87C5F0D126285E7E0067A1B5 /* moc_stagesbike.cpp in Compile Sources */,
Expand Down Expand Up @@ -3767,6 +3785,8 @@
1FBBC7C86C436CAAAFD37E56 /* moc_domyostreadmill.cpp in Compile Sources */,
876BFCA027BE35D8001D7645 /* moc_proformelliptical.cpp in Compile Sources */,
873824BD27E64707004F1B46 /* moc_resolver_p.cpp in Compile Sources */,
87DA62A42D2305E4008ADA0F /* moc_characteristicnotifier0002.cpp in Compile Sources */,
87DA62A52D2305E4008ADA0F /* moc_characteristicwriteprocessor0003.cpp in Compile Sources */,
876ED21A25C3E9010065F3DC /* moc_material.cpp in Compile Sources */,
87A4B76125AF27CB0027EF3C /* metric.cpp in Compile Sources */,
87D10552290996EA00B3935B /* mepanelbike.cpp in Compile Sources */,
Expand Down
16 changes: 16 additions & 0 deletions src/characteristics/characteristicnotifier0002.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include "characteristicnotifier0002.h"
#include "bike.h"
#include <QDebug>

CharacteristicNotifier0002::CharacteristicNotifier0002(bluetoothdevice *bike, QObject *parent)
: CharacteristicNotifier(0x0002, parent) {
Bike = bike;
}

int CharacteristicNotifier0002::notify(QByteArray &value) {
if(answer.length()) {
value.append(answer);
return CN_OK;
}
return CN_INVALID;
}
17 changes: 17 additions & 0 deletions src/characteristics/characteristicnotifier0002.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#ifndef CHARACTERISTICNOTIFIER0002_H
#define CHARACTERISTICNOTIFIER0002_H

#include "bluetoothdevice.h"
#include "characteristicnotifier.h"

class CharacteristicNotifier0002 : public CharacteristicNotifier {
Q_OBJECT
bluetoothdevice* Bike = nullptr;

public:
explicit CharacteristicNotifier0002(bluetoothdevice *bike, QObject *parent = nullptr);
int notify(QByteArray &value) override;
QByteArray answer;
};

#endif // CHARACTERISTICNOTIFIER0002_H
162 changes: 162 additions & 0 deletions src/characteristics/characteristicwriteprocessor0003.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
#include "bike.h"
#include "characteristicwriteprocessor0003.h"
#include <QDebug>

CharacteristicWriteProcessor0003::CharacteristicWriteProcessor0003(double bikeResistanceGain,
int8_t bikeResistanceOffset,
bluetoothdevice *bike,
CharacteristicNotifier0002 *notifier,
QObject *parent)
: CharacteristicWriteProcessor(bikeResistanceGain, bikeResistanceOffset, bike, parent), notifier(notifier) {
}

CharacteristicWriteProcessor0003::VarintResult CharacteristicWriteProcessor0003::decodeVarint(const QByteArray& bytes, int startIndex) {
qint64 result = 0;
int shift = 0;
int bytesRead = 0;

for (int i = startIndex; i < bytes.size(); i++) {
quint8 byte = static_cast<quint8>(bytes.at(i));
result |= static_cast<qint64>(byte & 0x7F) << shift;
bytesRead++;

if ((byte & 0x80) == 0) {
break;
}
shift += 7;
}

return {result, bytesRead};
}

qint32 CharacteristicWriteProcessor0003::decodeSInt(const QByteArray& bytes) {
if (static_cast<quint8>(bytes.at(0)) != 0x22) {
qFatal("Invalid field header");
}

int length = static_cast<quint8>(bytes.at(1));

if (static_cast<quint8>(bytes.at(2)) != 0x10) {
qFatal("Invalid inner header");
}

VarintResult varint = decodeVarint(bytes, 3);

qint32 decoded = (varint.value >> 1) ^ -(varint.value & 1);

return decoded;
}

void CharacteristicWriteProcessor0003::handleZwiftGear(const QByteArray &array) {
uint8_t g = 0;
if (array.size() >= 2) {
if ((uint8_t)array[0] == (uint8_t)0xCC && (uint8_t)array[1] == (uint8_t)0x3A) g = 1;
else if ((uint8_t)array[0] == (uint8_t)0xFC && (uint8_t)array[1] == (uint8_t)0x43) g = 2;
else if ((uint8_t)array[0] == (uint8_t)0xAC && (uint8_t)array[1] == (uint8_t)0x4D) g = 3;
else if ((uint8_t)array[0] == (uint8_t)0xDC && (uint8_t)array[1] == (uint8_t)0x56) g = 4;
else if ((uint8_t)array[0] == (uint8_t)0x8C && (uint8_t)array[1] == (uint8_t)0x60) g = 5;
else if ((uint8_t)array[0] == (uint8_t)0xE8 && (uint8_t)array[1] == (uint8_t)0x6B) g = 6;
else if ((uint8_t)array[0] == (uint8_t)0xC4 && (uint8_t)array[1] == (uint8_t)0x77) g = 7;
else if (array.size() >= 3) {
if ((uint8_t)array[0] == (uint8_t)0xA0 && (uint8_t)array[1] == (uint8_t)0x83 && (uint8_t)array[2] == (uint8_t)0x01) g = 8;
else if ((uint8_t)array[0] == (uint8_t)0xA8 && (uint8_t)array[1] == (uint8_t)0x91 && (uint8_t)array[2] == (uint8_t)0x01) g = 9;
else if ((uint8_t)array[0] == (uint8_t)0xB0 && (uint8_t)array[1] == (uint8_t)0x9F && (uint8_t)array[2] == (uint8_t)0x01) g = 10;
else if ((uint8_t)array[0] == (uint8_t)0xB8 && (uint8_t)array[1] == (uint8_t)0xAD && (uint8_t)array[2] == (uint8_t)0x01) g = 11;
else if ((uint8_t)array[0] == (uint8_t)0xC0 && (uint8_t)array[1] == (uint8_t)0xBB && (uint8_t)array[2] == (uint8_t)0x01) g = 12;
else if ((uint8_t)array[0] == (uint8_t)0xF3 && (uint8_t)array[1] == (uint8_t)0xCB && (uint8_t)array[2] == (uint8_t)0x01) g = 13;
else if ((uint8_t)array[0] == (uint8_t)0xA8 && (uint8_t)array[1] == (uint8_t)0xDC && (uint8_t)array[2] == (uint8_t)0x01) g = 14;
else if ((uint8_t)array[0] == (uint8_t)0xDC && (uint8_t)array[1] == (uint8_t)0xEC && (uint8_t)array[2] == (uint8_t)0x01) g = 15;
else if ((uint8_t)array[0] == (uint8_t)0x90 && (uint8_t)array[1] == (uint8_t)0xFD && (uint8_t)array[2] == (uint8_t)0x01) g = 16;
else if ((uint8_t)array[0] == (uint8_t)0xD4 && (uint8_t)array[1] == (uint8_t)0x90 && (uint8_t)array[2] == (uint8_t)0x02) g = 17;
else if ((uint8_t)array[0] == (uint8_t)0x98 && (uint8_t)array[1] == (uint8_t)0xA4 && (uint8_t)array[2] == (uint8_t)0x02) g = 18;
else if ((uint8_t)array[0] == (uint8_t)0xDC && (uint8_t)array[1] == (uint8_t)0xB7 && (uint8_t)array[2] == (uint8_t)0x02) g = 19;
else if ((uint8_t)array[0] == (uint8_t)0x9F && (uint8_t)array[1] == (uint8_t)0xCB && (uint8_t)array[2] == (uint8_t)0x02) g = 20;
else if ((uint8_t)array[0] == (uint8_t)0xD8 && (uint8_t)array[1] == (uint8_t)0xE2 && (uint8_t)array[2] == (uint8_t)0x02) g = 21;
else if ((uint8_t)array[0] == (uint8_t)0x90 && (uint8_t)array[1] == (uint8_t)0xFA && (uint8_t)array[2] == (uint8_t)0x02) g = 22;
else if ((uint8_t)array[0] == (uint8_t)0xC8 && (uint8_t)array[1] == (uint8_t)0x91 && (uint8_t)array[2] == (uint8_t)0x03) g = 23;
else if ((uint8_t)array[0] == (uint8_t)0xF3 && (uint8_t)array[1] == (uint8_t)0xAC && (uint8_t)array[2] == (uint8_t)0x03) g = 24;
else { return; }
}
else { return; }
}

if (g < currentZwiftGear) {
for (int i = 0; i < currentZwiftGear - g; ++i) {
((bike*)Bike)->gearDown();
}
} else if (g > currentZwiftGear) {
for (int i = 0; i < g - currentZwiftGear; ++i) {
((bike*)Bike)->gearUp();
}
}
currentZwiftGear = g;
}

int CharacteristicWriteProcessor0003::writeProcess(quint16 uuid, const QByteArray &data, QByteArray &reply) {
static const QByteArray expectedHexArray = QByteArray::fromHex("52696465 4F6E02");
static const QByteArray expectedHexArray2 = QByteArray::fromHex("410805");
static const QByteArray expectedHexArray3 = QByteArray::fromHex("00088804");
static const QByteArray expectedHexArray4 = QByteArray::fromHex("042A0A10 C0BB0120");
static const QByteArray expectedHexArray5 = QByteArray::fromHex("0422");
static const QByteArray expectedHexArray6 = QByteArray::fromHex("042A0410");
static const QByteArray expectedHexArray7 = QByteArray::fromHex("042A0310");
static const QByteArray expectedHexArray8 = QByteArray::fromHex("0418");

QByteArray receivedData = data;

if (receivedData.startsWith(expectedHexArray)) {
qDebug() << "Zwift Play Processor: Initial connection request";
reply = QByteArray::fromHex("2a08031211220f4154582030342c2053545820303400");
}
else if (receivedData.startsWith(expectedHexArray2)) {
qDebug() << "Zwift Play Processor: Device info request";
reply = QByteArray::fromHex("3c080012320a3008800412040500050"
"11a0b4b49434b5220434f524500320f"
"3430323431383030393834000000003a01314204080110140");
}
else if (receivedData.startsWith(expectedHexArray3)) {
qDebug() << "Zwift Play Processor: Status request";
reply = QByteArray::fromHex("3c0888041206 0a0440c0bb01");
}
else if (receivedData.startsWith(expectedHexArray5)) {
qDebug() << "Zwift Play Processor: Slope change request";
double slopefloat = decodeSInt(receivedData.mid(1));
QByteArray slope(2, 0);
slope[0] = quint8(qint16(slopefloat) & 0xFF);
slope[1] = quint8((qint16(slopefloat) >> 8) & 0x00FF);

emit ftmsCharacteristicChanged(QLowEnergyCharacteristic(),
QByteArray::fromHex("116901") + slope + QByteArray::fromHex("3228"));

reply = QByteArray::fromHex("3c0888041206 0a0440c0bb01");
}
else if (receivedData.startsWith(expectedHexArray6) ||
receivedData.startsWith(expectedHexArray7)) {
qDebug() << "Zwift Play Processor: Gear change request";
handleZwiftGear(receivedData.mid(4));
reply = QByteArray::fromHex("03080010001827e7 2000 28 00 3093ed01");
}
else if (receivedData.startsWith(expectedHexArray8)) {
qDebug() << "Zwift Play Processor: Power request";
VarintResult Power = decodeVarint(receivedData, 2);
QByteArray power(2, 0);
power[0] = quint8(qint16(Power.value) & 0xFF);
power[1] = quint8((qint16(Power.value) >> 8) & 0x00FF);

emit ftmsCharacteristicChanged(QLowEnergyCharacteristic(),
QByteArray::fromHex("05") + power);

reply = QByteArray::fromHex("030882011022181020002898523086ed01");
reply[2] = ((bike*)Bike)->wattsMetric().value();
}
else {
qDebug() << "Zwift Play Processor: Unhandled request:" << receivedData.toHex();
return -1;
}

if (notifier) {
notifier->answer = reply;
}

return 0;
}
32 changes: 32 additions & 0 deletions src/characteristics/characteristicwriteprocessor0003.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#ifndef CHARACTERISTICWRITEPROCESSOR0003_H
#define CHARACTERISTICWRITEPROCESSOR0003_H

#include "characteristicnotifier0002.h"
#include "characteristicwriteprocessor.h"

class CharacteristicWriteProcessor0003 : public CharacteristicWriteProcessor {
Q_OBJECT
CharacteristicNotifier0002 *notifier = nullptr;

public:
explicit CharacteristicWriteProcessor0003(double bikeResistanceGain, int8_t bikeResistanceOffset,
bluetoothdevice *bike, CharacteristicNotifier0002 *notifier,
QObject *parent = nullptr);
int writeProcess(quint16 uuid, const QByteArray &data, QByteArray &out) override;

private:
struct VarintResult {
qint64 value;
int bytesRead;
};

VarintResult decodeVarint(const QByteArray& bytes, int startIndex);
qint32 decodeSInt(const QByteArray& bytes);
void handleZwiftGear(const QByteArray &array);
int currentZwiftGear = 0;

signals:
void ftmsCharacteristicChanged(const QLowEnergyCharacteristic &characteristic, const QByteArray &newValue);
};

#endif // CHARACTERISTICWRITEPROCESSOR0003_H
4 changes: 3 additions & 1 deletion src/devices/dircon/dirconmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ using namespace std::chrono_literals;
OP(WAHOO_RPM_SPEED, "Wahoo SPEED $uuid_hex$", DM_MACHINE_TYPE_BIKE, P1, P2, P3) \
OP(WAHOO_TREADMILL, "Wahoo TREAD $uuid_hex$", DM_MACHINE_TYPE_TREADMILL, P1, P2, P3)

#define DP_PROCESS_WRITE_0003() writeP0003
#define DP_PROCESS_WRITE_2AD9() writeP2AD9
#define DP_PROCESS_WRITE_2AD9T() writeP2AD9
#define DP_PROCESS_WRITE_E005() writePE005
Expand Down Expand Up @@ -75,7 +76,7 @@ using namespace std::chrono_literals;
OP(HEART_RATE, 0x2A37, DPKT_CHAR_PROP_FLAG_NOTIFY, DM_BT("\x00"), DP_PROCESS_WRITE_NULL, P1, P2, P3) \
OP(ZWIFT_PLAY_EMULATOR, 0x0003, \
DPKT_CHAR_PROP_FLAG_WRITE, \
DM_BT("\x00"), DP_PROCESS_WRITE_NULL, P1, P2, P3) \
DM_BT("\x00"), DP_PROCESS_WRITE_0003, P1, P2, P3) \
OP(ZWIFT_PLAY_EMULATOR, 0x0002, \
DPKT_CHAR_PROP_FLAG_NOTIFY, \
DM_BT("\x00"), DP_PROCESS_WRITE_NULL, P1, P2, P3) \
Expand Down Expand Up @@ -171,6 +172,7 @@ DirconManager::DirconManager(bluetoothdevice *Bike, int8_t bikeResistanceOffset,
DM_CHAR_NOTIF_OP(DM_CHAR_NOTIF_BUILD_OP, Bike, 0, 0)
writeP2AD9 = new CharacteristicWriteProcessor2AD9(bikeResistanceGain, bikeResistanceOffset, Bike, notif2AD9, this);
writePE005 = new CharacteristicWriteProcessorE005(bikeResistanceGain, bikeResistanceOffset, Bike, this);
writeP0003 = new CharacteristicWriteProcessor0003(bikeResistanceGain, bikeResistanceOffset, Bike, notif0002, this);
DM_CHAR_OP(DM_CHAR_INIT_OP, services, service, 0)
connect(writeP2AD9, SIGNAL(changeInclination(double, double)), this, SIGNAL(changeInclination(double, double)));
connect(writeP2AD9, SIGNAL(ftmsCharacteristicChanged(QLowEnergyCharacteristic, QByteArray)), this,
Expand Down
Loading

0 comments on commit c835518

Please sign in to comment.