Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
119 changes: 119 additions & 0 deletions src/android/src/Usbserial.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbDeviceConnection;
import android.hardware.usb.UsbManager;
import android.system.ErrnoException;
import android.system.Os;
import android.system.OsConstants;
import android.system.StructPollfd;
import org.cagnulen.qdomyoszwift.QLog;
import android.app.Service;
import android.media.RingtoneManager;
Expand Down Expand Up @@ -39,15 +43,35 @@
public class Usbserial {

static UsbSerialPort port = null;
static java.io.FileDescriptor localSerialFd = null;
static boolean localSerialMode = false;
static byte[] receiveData = new byte[4096];
static int lastReadLen = 0;
static final String[] localSerialCandidates = {
"/dev/ttyS4",
"/dev/ttyS0",
"/dev/ttyS1",
"/dev/ttyS2",
"/dev/ttyS3"
};

public static void open(Context context) {
open(context, 2400); // Default baud rate for Computrainer
}

public static void open(Context context, int baudRate) {
open(context, baudRate, "");
}

public static void open(Context context, int baudRate, String devicePath) {
if (devicePath != null && (devicePath.startsWith("/dev/") || devicePath.equalsIgnoreCase("auto"))) {
openLocalSerial(devicePath, baudRate);
return;
}

QLog.d("QZ","UsbSerial open with baud rate: " + baudRate);
localSerialMode = false;
localSerialFd = null;
// Find all available drivers from attached devices.
UsbManager manager = (UsbManager) context.getSystemService(Context.USB_SERVICE);
List<UsbSerialDriver> availableDrivers = UsbSerialProber.getDefaultProber().findAllDrivers(manager);
Expand Down Expand Up @@ -110,7 +134,76 @@ public void onReceive(Context context, Intent intent) {
}
}

private static void openLocalSerial(String devicePath, int baudRate) {
QLog.d("QZ","UsbSerial local serial open " + devicePath + " with baud rate: " + baudRate);
port = null;
localSerialFd = null;
localSerialMode = false;
lastReadLen = 0;

if (devicePath.equalsIgnoreCase("auto")) {
for (String candidate : localSerialCandidates) {
if (openLocalSerialPath(candidate, baudRate)) {
return;
}
}
QLog.d("QZ","UsbSerial local serial auto open failed");
return;
}

openLocalSerialPath(devicePath, baudRate);
}

private static boolean openLocalSerialPath(String devicePath, int baudRate) {
try {
configureLocalSerial(devicePath, baudRate);
localSerialFd = Os.open(devicePath, OsConstants.O_RDWR | OsConstants.O_NOCTTY | OsConstants.O_NONBLOCK, 0);
localSerialMode = true;
QLog.d("QZ","UsbSerial local serial opened successfully: " + devicePath);
return true;
}
catch (ErrnoException e) {
QLog.d("QZ","UsbSerial local serial open failed: " + e.getMessage());
return false;
}
}

private static void configureLocalSerial(String devicePath, int baudRate) {
try {
String[] command = {
"stty",
"-F",
devicePath,
String.valueOf(baudRate),
"cs8",
"-cstopb",
"-parenb",
"raw",
"-echo"
};
Process process = Runtime.getRuntime().exec(command);
int rc = process.waitFor();
QLog.d("QZ","UsbSerial local serial stty exit code: " + rc);
}
catch (Exception e) {
QLog.d("QZ","UsbSerial local serial stty failed: " + e.getMessage());
}
}

public static void write (byte[] bytes) {
if(localSerialMode) {
if(localSerialFd == null)
return;

try {
Os.write(localSerialFd, bytes, 0, bytes.length);
}
catch (ErrnoException | IOException e) {
QLog.d("QZ","UsbSerial local serial write failed: " + e.getMessage());
}
return;
}

if(port == null)
return;

Expand All @@ -128,6 +221,32 @@ public static int readLen() {
}

public static byte[] read() {
if(localSerialMode) {
if(localSerialFd == null) {
lastReadLen = 0;
return receiveData;
}

try {
StructPollfd pollFd = new StructPollfd();
pollFd.fd = localSerialFd;
pollFd.events = (short)OsConstants.POLLIN;
int ready = Os.poll(new StructPollfd[]{pollFd}, 0);
if(ready <= 0) {
lastReadLen = 0;
return receiveData;
}

lastReadLen = Os.read(localSerialFd, receiveData, 0, receiveData.length);
QLog.d("QZ","UsbSerial local serial reading " + lastReadLen);
}
catch (ErrnoException | IOException e) {
lastReadLen = 0;
QLog.d("QZ","UsbSerial local serial read failed: " + e.getMessage());
}
return receiveData;
}

if(port == null) {
lastReadLen = 0;
return receiveData;
Expand Down
22 changes: 22 additions & 0 deletions src/devices/bluetooth.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,8 @@ void bluetooth::deviceDiscovered(const QBluetoothDeviceInfo &device) {
settings.value(QZSettings::computrainer_serialport, QZSettings::default_computrainer_serialport).toString();
QString kettlerUsbSerialPort =
settings.value(QZSettings::kettler_usb_serialport, QZSettings::default_kettler_usb_serialport).toString();
QString freebeatSerialPort =
settings.value(QZSettings::freebeat_serialport, QZSettings::default_freebeat_serialport).toString();
QString csaferowerSerialPort = settings.value(QZSettings::csafe_rower, QZSettings::default_csafe_rower).toString();
bool waterrowerUSBEnabled = settings.value(QZSettings::waterrower_usb, QZSettings::default_waterrower_usb).toBool();
QString csafeellipticalSerialPort =
Expand Down Expand Up @@ -954,6 +956,19 @@ void bluetooth::deviceDiscovered(const QBluetoothDeviceInfo &device) {
emit searchingStop();
}
this->signalBluetoothDeviceConnected(kettlerUsbBike);
} else if (!freebeatSerialPort.isEmpty() && !freebeatBike) {
this->stopDiscovery();
freebeatBike =
new freebeatbike(noWriteResistance, noHeartService, bikeResistanceOffset, bikeResistanceGain);
emit deviceConnected(b);
connect(freebeatBike, &bluetoothdevice::connectedAndDiscovered, this,
&bluetooth::connectedAndDiscovered);
connect(freebeatBike, &freebeatbike::debug, this, &bluetooth::debug);
freebeatBike->deviceDiscovered(b);
if (this->discoveryAgent && !this->discoveryAgent->isActive()) {
emit searchingStop();
}
this->signalBluetoothDeviceConnected(freebeatBike);
} else if (!csaferowerSerialPort.isEmpty() && !csafeRower) {
this->stopDiscovery();
csafeRower = new csaferower(noWriteResistance, noHeartService, false);
Expand Down Expand Up @@ -4220,6 +4235,11 @@ void bluetooth::restart() {
delete kettlerUsbBike;
kettlerUsbBike = nullptr;
}
if (freebeatBike) {

delete freebeatBike;
freebeatBike = nullptr;
}
if (csafeRower) {

delete csafeRower;
Expand Down Expand Up @@ -4589,6 +4609,8 @@ bluetoothdevice *bluetooth::device() {
return computrainerBike;
} else if (kettlerUsbBike) {
return kettlerUsbBike;
} else if (freebeatBike) {
return freebeatBike;
} else if (csafeRower) {
return csafeRower;
} else if (csafeElliptical) {
Expand Down
2 changes: 2 additions & 0 deletions src/devices/bluetooth.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#ifndef Q_OS_IOS
#include "devices/computrainerbike/computrainerbike.h"
#include "devices/kettlerusbbike/kettlerusbbike.h"
#include "devices/freebeatbike/freebeatbike.h"
#include "devices/csaferower/csaferower.h"
#include "devices/csafeelliptical/csafeelliptical.h"
#endif
Expand Down Expand Up @@ -203,6 +204,7 @@ class bluetooth : public QObject, public SignalHandler {
#ifndef Q_OS_IOS
computrainerbike *computrainerBike = nullptr;
kettlerusbbike *kettlerUsbBike = nullptr;
freebeatbike *freebeatBike = nullptr;
csaferower *csafeRower = nullptr;
csafeelliptical *csafeElliptical = nullptr;
#endif
Expand Down
Loading
Loading