Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Windows 11 crash connecting #2764

Open
wants to merge 55 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
55 commits
Select commit Hold shift + click to select a range
05d598f
first raw version
cagnulein Oct 21, 2024
49c7a96
Merge branch 'master' into Custom-gearing-ranges/ratios-(Discussion-#…
cagnulein Oct 22, 2024
cc7757b
Update project.pbxproj
cagnulein Oct 22, 2024
2d13644
Update virtualbike_zwift.swift
cagnulein Oct 22, 2024
1c06260
Merge branch 'master' into Custom-gearing-ranges/ratios-(Discussion-#…
cagnulein Oct 23, 2024
d712621
fixing formula
cagnulein Oct 23, 2024
2da194f
Merge branch 'master' into Custom-gearing-ranges/ratios-(Discussion-#…
cagnulein Oct 24, 2024
89808ae
fixing casting to double
cagnulein Oct 24, 2024
ed1599c
need to center the values in the table
cagnulein Oct 24, 2024
0f14944
Update gears.qml
cagnulein Oct 25, 2024
527396e
Revert "Update gears.qml"
cagnulein Oct 28, 2024
74c37f5
Update gears.qml
cagnulein Oct 28, 2024
f85f743
i need to save the first 3 static objects and use it in the wahoo module
cagnulein Oct 28, 2024
8d07d7c
Merge branch 'master' into Custom-gearing-ranges/ratios-(Discussion-#…
cagnulein Oct 28, 2024
6fac977
qml finally saves the settings correctly
cagnulein Oct 28, 2024
2f2989f
completed?
cagnulein Oct 28, 2024
379cf8d
Update project.pbxproj
cagnulein Oct 29, 2024
64b9ec9
fixing gear conversion
cagnulein Oct 29, 2024
d7c3a84
adding max and minGears
cagnulein Oct 29, 2024
a7b8e63
fixing UI and settings
cagnulein Oct 29, 2024
82a6afe
kickr core to wahookickr class
cagnulein Oct 29, 2024
9cc02de
ftms wheel circumference for gears
cagnulein Oct 30, 2024
ad802f7
Merge branch 'master' into tacx_wheel_diameter
cagnulein Nov 4, 2024
dbb9ba3
implementing
cagnulein Nov 4, 2024
0625399
Update wahookickrsnapbike.cpp
cagnulein Nov 4, 2024
f3d8b39
Merge branch 'master' into tacx_wheel_diameter
cagnulein Nov 9, 2024
8b60b56
Update ftmsbike.cpp
cagnulein Nov 9, 2024
0337c08
first custom gear test
cagnulein Nov 9, 2024
bd0e703
Merge branch 'master' into zwift-hub-gear-custom
cagnulein Nov 9, 2024
07606fa
adding inclination custom message too
cagnulein Nov 9, 2024
095d8bc
Update ftmsbike.cpp
cagnulein Nov 9, 2024
9ca3fca
implemented protobuf
cagnulein Nov 11, 2024
e349ca9
protobuf also for the gears
cagnulein Nov 11, 2024
839213e
Update ftmsbike.cpp
cagnulein Nov 11, 2024
159f96d
Update project.pbxproj
cagnulein Nov 11, 2024
190011a
reverting tacxneo wheel diameter and ftms standard wheel diamater in …
cagnulein Nov 12, 2024
797ae5f
fixing mingears and maxgears
cagnulein Nov 12, 2024
9bb016f
adding android part
cagnulein Nov 12, 2024
7c52e92
Update main.cpp
cagnulein Nov 12, 2024
eafe75e
Update main.cpp
cagnulein Nov 12, 2024
f737e1b
Merge branch 'master' into zwift-hub-gear-custom
cagnulein Nov 12, 2024
a2e7218
Update main.cpp
cagnulein Nov 12, 2024
c5cfb1c
Update project.pbxproj
cagnulein Nov 12, 2024
12f77be
Merge branch 'zwift-hub-gear-custom' of https://github.com/cagnulein/…
cagnulein Nov 12, 2024
f602563
fixing android build
cagnulein Nov 12, 2024
79ba8ea
Update horizontreadmill.cpp
cagnulein Nov 12, 2024
c76195a
Update ftmsbike.cpp
cagnulein Nov 12, 2024
9939471
Update homeform.cpp
cagnulein Nov 12, 2024
cc4a5be
Merge branch 'master' into windows-11-crash-connecting
cagnulein Nov 13, 2024
e8faa3e
Update horizontreadmill.cpp
cagnulein Nov 13, 2024
df3e64e
Revert "Update horizontreadmill.cpp"
cagnulein Nov 14, 2024
e6efe0f
trying to use win11 api for discovery
cagnulein Nov 21, 2024
721f6d4
Update horizontreadmill.cpp
cagnulein Nov 21, 2024
b573442
Update qdomyos-zwift.pri
cagnulein Nov 21, 2024
f8aa63d
Update horizontreadmill.cpp
cagnulein Dec 10, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions src/android/src/ZwiftHub.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package org.cagnulen.qdomyoszwift;

import android.app.ActivityManager;
import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.os.Looper;
import android.os.Handler;
import android.util.Log;
import com.garmin.android.connectiq.ConnectIQ;
import com.garmin.android.connectiq.ConnectIQAdbStrategy;
import com.garmin.android.connectiq.IQApp;
import com.garmin.android.connectiq.IQDevice;
import com.garmin.android.connectiq.exception.InvalidStateException;
import com.garmin.android.connectiq.exception.ServiceUnavailableException;
import android.content.BroadcastReceiver;
import android.content.ContextWrapper;
import android.content.IntentFilter;
import android.widget.Toast;

import org.jetbrains.annotations.Nullable;

import com.google.protobuf.InvalidProtocolBufferException;

import java.util.HashMap;
import java.util.List;

public class ZwiftHub {

private static Context context;

private static final String TAG = "ZwiftHub: ";

public static byte[] inclinationCommand(double inclination) throws InvalidProtocolBufferException {
SimulationParam.Builder simulation = SimulationParam.newBuilder();
simulation.setInclineX100((int)(inclination * 100.0));

HubCommand.Builder command = HubCommand.newBuilder();
command.setSimulation(simulation.build());

byte[] data = command.build().toByteArray();
byte[] fullData = new byte[data.length + 1];
fullData[0] = 0x04;
System.arraycopy(data, 0, fullData, 1, data.length);

return fullData;
}

public static byte[] setGearCommand(int gears) throws InvalidProtocolBufferException {
PhysicalParam.Builder physical = PhysicalParam.newBuilder();
physical.setGearRatioX10000(gears);

HubCommand.Builder command = HubCommand.newBuilder();
command.setPhysical(physical.build());

byte[] data = command.build().toByteArray();
byte[] fullData = new byte[data.length + 1];
fullData[0] = 0x04;
System.arraycopy(data, 0, fullData, 1, data.length);

return fullData;
}
}
92 changes: 87 additions & 5 deletions src/devices/horizontreadmill/horizontreadmill.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2311,19 +2311,101 @@ void horizontreadmill::characteristicRead(const QLowEnergyCharacteristic &charac
}
}

#ifdef Q_OS_WIN
bool horizontreadmill::discoverServicesWin11(const QBluetoothAddress& address) {
BLUETOOTH_DEVICE_INFO deviceInfo = { sizeof(BLUETOOTH_DEVICE_INFO) };
deviceInfo.Address.ullLong = address.toUInt64();

DWORD error = BluetoothGetDeviceInfo(nullptr, &deviceInfo);
if (error != ERROR_SUCCESS) {
qDebug() << "Failed to get device info. Error code:" << error;
DWORD lastError = GetLastError();
qDebug() << "Last error:" << lastError;
return false;
}


DWORD serviceCount = 0;
GUID* serviceGuids = nullptr;

// Prima chiamata per ottenere il numero di servizi
DWORD result = BluetoothEnumerateInstalledServices(
nullptr,
&deviceInfo,
&serviceCount,
nullptr);

if (result != ERROR_SUCCESS) {
qDebug() << "Failed to enumerate services count";
return false;
}

if (serviceCount == 0) {
qDebug() << "No services found";
return false;
}

// Allocare memoria per i GUID dei servizi
serviceGuids = new GUID[serviceCount];

// Seconda chiamata per ottenere i GUID effettivi
result = BluetoothEnumerateInstalledServices(
nullptr,
&deviceInfo,
&serviceCount,
serviceGuids);

if (result != ERROR_SUCCESS) {
delete[] serviceGuids;
qDebug() << "Failed to enumerate services";
return false;
}

// Processare ogni servizio trovato
for (DWORD i = 0; i < serviceCount; i++) {
handleWin11Service(serviceGuids[i]);
}

delete[] serviceGuids;
return true;
}

void horizontreadmill::handleWin11Service(const GUID& serviceGuid) {
wchar_t guidString[39];
StringFromGUID2(serviceGuid, guidString, sizeof(guidString)/sizeof(wchar_t));

QString qtGuidString = QString::fromWCharArray(guidString);
QBluetoothUuid qtUuid(qtGuidString);

QLowEnergyService* service = m_control->createServiceObject(qtUuid);
if (service) {
gattCommunicationChannelService.append(service);
connect(service, &QLowEnergyService::stateChanged, this, &horizontreadmill::stateChanged);
service->discoverDetails();
qDebug() << "Discovered service:" << qtUuid;
}
}
#endif

void horizontreadmill::serviceScanDone(void) {
emit debug(QStringLiteral("serviceScanDone"));

initRequest = false;
firstStateChanged = 0;
auto services_list = m_control->services();

#ifdef Q_OS_WIN
if (discoverServicesWin11(m_control->remoteAddress())) {
return;
}
#endif

for (const QBluetoothUuid &s : qAsConst(services_list)) {
qDebug() << s << "discovering...";
gattCommunicationChannelService.append(m_control->createServiceObject(s));
connect(gattCommunicationChannelService.constLast(), &QLowEnergyService::stateChanged, this,
&horizontreadmill::stateChanged);
gattCommunicationChannelService.constLast()->discoverDetails();
qDebug() << s << "discovering...";
gattCommunicationChannelService.append(m_control->createServiceObject(s));
connect(gattCommunicationChannelService.constLast(), &QLowEnergyService::stateChanged, this,
&horizontreadmill::stateChanged);
gattCommunicationChannelService.constLast()->discoverDetails();
}
}

Expand Down
12 changes: 12 additions & 0 deletions src/devices/horizontreadmill/horizontreadmill.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@
#include "ios/lockscreen.h"
#endif

#ifdef Q_OS_WIN
#include <windows.h>
#include <bthsdpdef.h>
#include <bluetoothapis.h>
#include <VersionHelpers.h>
#endif

class horizontreadmill : public treadmill {
Q_OBJECT
public:
Expand All @@ -48,6 +55,11 @@ class horizontreadmill : public treadmill {
bool autoStartWhenSpeedIsGreaterThenZero() override;

private:
#ifdef Q_OS_WIN
bool discoverServicesWin11(const QBluetoothAddress& address);
void handleWin11Service(const GUID& serviceGuid);
#endif

void writeCharacteristic(QLowEnergyService *service, QLowEnergyCharacteristic characteristic, uint8_t *data,
uint8_t data_len, QString info, bool disable_log = false, bool wait_for_response = false);
void waitForAPacket();
Expand Down
15 changes: 8 additions & 7 deletions src/homeform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -990,14 +990,15 @@ void homeform::backup() {

static uint8_t index = 0;
qDebug() << QStringLiteral("saving fit file backup...");

QString path = getWritableAppDir();
bluetoothdevice *dev = bluetoothManager->device();
if (dev) {

QString filename = path + QString::number(index) + backupFitFileName;
QFile::remove(filename);
qfit::save(filename, Session, dev->deviceType(),
qDebug() << QStringLiteral("writable dir") << path;
bluetoothdevice *dev = bluetoothManager->device();
if (dev) {
QString filename = path + QString::number(index) + backupFitFileName;
qDebug() << QStringLiteral("filename") << filename;
QFile::remove(filename);
qDebug() << QStringLiteral("filename removed") << filename;
qfit::save(filename, Session, dev->deviceType(),
qobject_cast<m3ibike *>(dev) ? QFIT_PROCESS_DISTANCENOISE : QFIT_PROCESS_NONE,
stravaPelotonWorkoutType, dev->bluetoothDevice.name());

Expand Down
44 changes: 44 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,50 @@ int main(int argc, char *argv[]) {
app->setOrganizationDomain(QStringLiteral("robertoviola.cloud"));
app->setApplicationName(QStringLiteral("qDomyos-Zwift"));

/* TEST ZWIFT HUB */
#ifdef Q_OS_ANDROID
{
QAndroidJniObject rrr = QAndroidJniObject::callStaticObjectMethod(
"org/cagnulen/qdomyoszwift/ZwiftHub",
"inclinationCommand",
"(D)[B",
8.0);

if(!rrr.isValid()) {
qDebug() << "inclinationCommand returned invalid value";
}

jbyteArray array = rrr.object<jbyteArray>();
QAndroidJniEnvironment env;
jbyte* bytes = env->GetByteArrayElements(array, nullptr);
jsize length = env->GetArrayLength(array);

QByteArray message((char*)bytes, length);

env->ReleaseByteArrayElements(array, bytes, JNI_ABORT);
qDebug() << "inclination command" << message.toHex(' ');

QAndroidJniObject rr = QAndroidJniObject::callStaticObjectMethod(
"org/cagnulen/qdomyoszwift/ZwiftHub",
"setGearCommand",
"(I)[B",
32608);

if (!rr.isValid()) {
qDebug() << "setGearCommand returned invalid value";
}

array = rr.object<jbyteArray>();
bytes = env->GetByteArrayElements(array, nullptr);
length = env->GetArrayLength(array);

QByteArray proto((char*)bytes, length);

env->ReleaseByteArrayElements(array, bytes, JNI_ABORT);
qDebug() << "gear command" << proto.toHex(' ');
}
#endif

QSettings settings;

#if defined(Q_OS_ANDROID) || defined(Q_OS_IOS)
Expand Down
9 changes: 8 additions & 1 deletion src/qdomyos-zwift.pri
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,14 @@ CONFIG += qmltypes
#unix:!android: CONFIG += webengine

win32:DEFINES += _ITERATOR_DEBUG_LEVEL=0
win32:!mingw:LIBS += -llibprotobuf -llibprotoc -labseil_dll -llibprotobuf-lite -L$$PWD
win32:!mingw:LIBS += -llibprotobuf -llibprotoc -labseil_dll -llibprotobuf-lite -lBthprops -lVersion -lRpcrt4 -lole32 -L$$PWD

win32 {
LIBS += -lBthprops
LIBS += -lVersion
LIBS += -lRpcrt4 # Per StringFromGUID2
LIBS += -lole32 # Altra possibile dipendenza per GUID
}

QML_IMPORT_NAME = org.cagnulein.qdomyoszwift
QML_IMPORT_MAJOR_VERSION = 1
Expand Down
Loading