Skip to content

Commit a1d7e04

Browse files
committed
Enhance BLE_Zwift_Service with new payload handling methods and update command codes for trainer configuration
1 parent bcec78c commit a1d7e04

7 files changed

Lines changed: 193 additions & 66 deletions

include/BLE_Zwift_Service.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,30 @@ class BLE_Zwift_Service {
7575

7676
// Encode uint64 as ULEB128 varint, returns number of bytes written
7777
static size_t encodeUleb128(uint64_t value, uint8_t *buffer);
78+
// Returns the number of bytes a ULEB128-encoded value would occupy
79+
static size_t encodeUleb128Len(uint64_t value);
7880

7981
// Decode ULEB128 varint from buffer, returns number of bytes consumed
8082
static size_t decodeUleb128(const uint8_t *buf, size_t bufLen, uint64_t *result);
8183

8284
// Send the "all buttons released" state
8385
void sendAllButtonsReleased();
8486

87+
// Send a fully-built payload on sync_tx and mirror it through DirCon.
88+
void sendSyncTxPayload(const uint8_t *payload, size_t length);
89+
90+
// Send a fully-built payload on async and mirror it through DirCon.
91+
void sendAsyncPayload(const uint8_t *payload, size_t length);
92+
93+
// Send current GearRatio device information on sync_tx.
94+
void sendGearRatioSyncTx();
95+
96+
// Send current GeneralInfo device information on sync_tx.
97+
void sendGeneralInfoSyncTx();
98+
99+
// Send TRAINER_CONFIG_STATUS with virtual_shifting_mode=1 on async.
100+
void sendTrainerConfigStatus();
101+
85102
// Handle Zwift trainer protocol command (non-RideOn messages)
86103
void handleZwiftCommand(const uint8_t *data, size_t length);
87104

include/Zwift_Protocol_Messages.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ enum class CommandCode : uint8_t {
2121
HubRequest = 0x00,
2222
HubRidingData = 0x03,
2323
HubCommand = 0x04,
24+
TrainerConfigStatus = 0x05, // Device → Zwift: report trainer config (virtual shifting)
2425
PlayKeyPadStatus = 0x07,
2526
PlayCommand = 0x12,
2627
Idle = 0x19,

platformio.ini

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,15 @@ build_flags =
6666
;-D CONFIG_BT_NIMBLE_BLOCK_COUNT=20
6767
-std=gnu++17
6868
lib_deps =
69-
;https://github.com/h2zero/esp-nimble-cpp/
69+
https://github.com/doudar/esp-nimble-cpp/
7070
https://github.com/teemuatlut/TMCStepper/archive/refs/tags/v0.7.3.zip
7171
https://github.com/bblanchon/ArduinoJson/archive/refs/tags/v7.3.1.zip
7272
;https://github.com/gin66/FastAccelStepper/archive/refs/tags/0.31.2.zip
7373
https://github.com/doudar/FastAccelStepper/
7474
;https://github.com/gilmaimon/ArduinoWebsockets/archive/refs/tags/0.5.4.zip
7575
https://github.com/doudar/ArduinoWebsockets/
7676
;https://github.com/Links2004/arduinoWebSockets
77-
https://github.com/h2zero/NimBLE-Arduino/
77+
;https://github.com/h2zero/NimBLE-Arduino/
7878

7979
[env:release]
8080
extends = esp32doit

src/BLE_Client.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ bool SpinBLEClient::connectToServer() {
305305
return false;
306306
};
307307
// Always create a brand-new client for each connection attempt.
308-
if (NimBLEDevice::getCreatedClientCount() >= 3) {
308+
if (NimBLEDevice::getCreatedClientCount() >= NIMBLE_MAX_CONNECTIONS) {
309309
Serial.println("Max clients reached - no more connections available");
310310
return false;
311311
}

src/BLE_Device_Information_Service.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ void BLE_Device_Information_Service::setupService(NimBLEServer* pServer) {
3434
pHardwareRevisionCharacteristic->setValue((String)userConfig->getDeviceName());
3535

3636
pFirmwareRevisionCharacteristic = pDeviceInformationService->createCharacteristic(FIRMWARE_REVISION_UUID, NIMBLE_PROPERTY::READ);
37-
pFirmwareRevisionCharacteristic->setValue((String)ESP.getChipRevision());
37+
pFirmwareRevisionCharacteristic->setValue(219);
3838

3939
pSoftwareRevisionCharacteristic = pDeviceInformationService->createCharacteristic(SOFTWARE_REVISION_UUID, NIMBLE_PROPERTY::READ);
40-
pSoftwareRevisionCharacteristic->setValue(FIRMWARE_VERSION);
40+
pSoftwareRevisionCharacteristic->setValue(219);
4141

4242
pSystemIDCharacteristic = pDeviceInformationService->createCharacteristic(SYSTEM_ID_UUID, NIMBLE_PROPERTY::READ);
4343
pSystemIDCharacteristic->setValue((String)userConfig->getDeviceName());

src/BLE_Server.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ void startBLEServer() {
5757

5858
// Zwift identifies controllers via manufacturer data with company ID 0x094A.
5959
// Set the last two bytes to the last two bytes of our BLE address in little endian.
60-
uint8_t zwiftMfrData[] = {0x4A, 0x09, 0x01, 0x58, 0x9A};
60+
uint8_t zwiftMfrData[] = {0x4A, 0x09, 0x0B, 0x58, 0x9A};
6161
const std::string bleAddress = BLEDevice::getAddress().toString(); // "aa:bb:cc:dd:ee:ff"
6262
unsigned int mac[6] = {0};
6363
if (sscanf(bleAddress.c_str(), "%02x:%02x:%02x:%02x:%02x:%02x", &mac[0], &mac[1], &mac[2], &mac[3], &mac[4], &mac[5]) == 6) {
@@ -67,7 +67,7 @@ void startBLEServer() {
6767
}
6868
pAdvertising->setManufacturerData(zwiftMfrData, sizeof(zwiftMfrData));
6969
pAdvertising->addServiceUUID(HEARTSERVICE_UUID);
70-
pAdvertising->addServiceUUID(ZWIFT_CUSTOM_SERVICE_UUID);
70+
pAdvertising->addServiceUUID(ZWIFT_RIDE_CUSTOM_SERVICE_UUID);
7171

7272
// Put the device name and SmartSpin2k service UUID in the scan response to avoid
7373
// overflowing the primary ad packet (which already carries manufacturer data + service UUIDs).

0 commit comments

Comments
 (0)