2015-08-18 01:26:15 +02:00
|
|
|
package nodomain.freeyourgadget.gadgetbridge.service.serial;
|
2015-04-14 01:24:03 +02:00
|
|
|
|
2015-05-22 20:20:33 +02:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
2015-05-18 20:56:19 +02:00
|
|
|
import java.util.UUID;
|
|
|
|
|
2015-06-23 11:54:33 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEvent;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventSendBytes;
|
2015-09-13 21:44:26 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.devices.EventHandler;
|
2016-04-04 20:08:34 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.model.CallSpec;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.model.MusicSpec;
|
2015-09-24 14:45:21 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.model.NotificationSpec;
|
2015-08-03 23:51:53 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.service.AbstractDeviceSupport;
|
2015-04-14 01:24:03 +02:00
|
|
|
|
2015-08-02 00:12:21 +02:00
|
|
|
/**
|
2015-08-17 16:30:12 +02:00
|
|
|
* An abstract base class for devices speaking a serial protocol, like via
|
|
|
|
* an rfcomm bluetooth socket or a TCP socket.
|
2015-09-13 21:44:26 +02:00
|
|
|
* <p/>
|
2015-08-02 00:12:21 +02:00
|
|
|
* This class uses two helper classes to deal with that:
|
|
|
|
* - GBDeviceIoThread, which creates and maintains the actual socket connection and implements the transport layer
|
|
|
|
* - GBDeviceProtocol, which implements the encoding and decoding of messages, i.e. the actual device specific protocol
|
2015-09-13 21:44:26 +02:00
|
|
|
* <p/>
|
2015-08-02 00:12:21 +02:00
|
|
|
* Note that these two classes need to be implemented in a device specific way.
|
2015-09-13 21:44:26 +02:00
|
|
|
* <p/>
|
2015-08-02 00:12:21 +02:00
|
|
|
* This implementation implements all methods of {@link EventHandler}, calls the {@link GBDeviceProtocol device protocol}
|
|
|
|
* to create the device specific message for the respective events and sends them to the device via {@link #sendToDevice(byte[])}.
|
|
|
|
*/
|
2015-08-17 16:30:12 +02:00
|
|
|
public abstract class AbstractSerialDeviceSupport extends AbstractDeviceSupport {
|
2015-04-14 01:24:03 +02:00
|
|
|
|
2015-05-22 20:20:33 +02:00
|
|
|
private static final Logger LOG = LoggerFactory.getLogger(AbstractDeviceSupport.class);
|
|
|
|
|
2016-04-03 18:30:20 +02:00
|
|
|
protected GBDeviceProtocol gbDeviceProtocol;
|
|
|
|
protected GBDeviceIoThread gbDeviceIOThread;
|
2015-04-14 01:24:03 +02:00
|
|
|
|
2015-08-02 00:12:21 +02:00
|
|
|
/**
|
|
|
|
* Factory method to create the device specific GBDeviceProtocol instance to be used.
|
|
|
|
*/
|
2015-04-14 01:24:03 +02:00
|
|
|
protected abstract GBDeviceProtocol createDeviceProtocol();
|
|
|
|
|
2015-08-02 00:12:21 +02:00
|
|
|
/**
|
|
|
|
* Factory method to create the device specific GBDeviceIoThread instance to be used.
|
|
|
|
*/
|
2015-04-14 01:24:03 +02:00
|
|
|
protected abstract GBDeviceIoThread createDeviceIOThread();
|
2015-04-14 10:29:09 +02:00
|
|
|
|
2015-04-14 01:39:00 +02:00
|
|
|
@Override
|
|
|
|
public void dispose() {
|
|
|
|
// currently only one thread allowed
|
|
|
|
if (gbDeviceIOThread != null) {
|
|
|
|
gbDeviceIOThread.quit();
|
|
|
|
try {
|
|
|
|
gbDeviceIOThread.join();
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
gbDeviceIOThread = null;
|
|
|
|
}
|
|
|
|
}
|
2015-04-14 10:29:09 +02:00
|
|
|
|
2015-05-05 00:48:02 +02:00
|
|
|
@Override
|
|
|
|
public void pair() {
|
|
|
|
// Default implementation does no manual pairing, use the Android
|
|
|
|
// pairing dialog instead.
|
|
|
|
}
|
|
|
|
|
2015-08-02 00:12:21 +02:00
|
|
|
/**
|
|
|
|
* Lazily creates and returns the GBDeviceProtocol instance to be used.
|
|
|
|
*/
|
2015-04-14 01:24:03 +02:00
|
|
|
public synchronized GBDeviceProtocol getDeviceProtocol() {
|
|
|
|
if (gbDeviceProtocol == null) {
|
|
|
|
gbDeviceProtocol = createDeviceProtocol();
|
|
|
|
}
|
|
|
|
return gbDeviceProtocol;
|
|
|
|
}
|
2015-04-14 10:29:09 +02:00
|
|
|
|
2015-08-02 00:12:21 +02:00
|
|
|
/**
|
|
|
|
* Lazily creates and returns the GBDeviceIoThread instance to be used.
|
|
|
|
*/
|
2015-04-14 01:24:03 +02:00
|
|
|
public synchronized GBDeviceIoThread getDeviceIOThread() {
|
|
|
|
if (gbDeviceIOThread == null) {
|
|
|
|
gbDeviceIOThread = createDeviceIOThread();
|
|
|
|
}
|
|
|
|
return gbDeviceIOThread;
|
|
|
|
}
|
2015-04-14 10:29:09 +02:00
|
|
|
|
2015-08-02 00:12:21 +02:00
|
|
|
/**
|
|
|
|
* Sends the given message to the device. This implementation delegates the
|
|
|
|
* writing to the {@link #getDeviceIOThread device io thread}
|
2015-09-13 21:44:26 +02:00
|
|
|
*
|
2015-08-02 00:12:21 +02:00
|
|
|
* @param bytes the message to send to the device
|
|
|
|
*/
|
2015-04-14 01:24:03 +02:00
|
|
|
protected void sendToDevice(byte[] bytes) {
|
2015-04-14 01:39:00 +02:00
|
|
|
if (bytes != null && gbDeviceIOThread != null) {
|
2015-04-14 01:24:03 +02:00
|
|
|
gbDeviceIOThread.write(bytes);
|
|
|
|
}
|
|
|
|
}
|
2015-04-14 10:29:09 +02:00
|
|
|
|
2015-06-23 11:54:33 +02:00
|
|
|
public void handleGBDeviceEvent(GBDeviceEventSendBytes sendBytes) {
|
2015-05-22 20:20:33 +02:00
|
|
|
sendToDevice(sendBytes.encodedBytes);
|
|
|
|
}
|
|
|
|
|
2015-05-23 00:54:14 +02:00
|
|
|
@Override
|
2015-06-23 11:54:33 +02:00
|
|
|
public void evaluateGBDeviceEvent(GBDeviceEvent deviceEvent) {
|
2015-08-31 17:25:58 +02:00
|
|
|
if (deviceEvent instanceof GBDeviceEventSendBytes) {
|
|
|
|
handleGBDeviceEvent((GBDeviceEventSendBytes) deviceEvent);
|
|
|
|
return;
|
2015-05-22 20:20:33 +02:00
|
|
|
}
|
2015-06-23 11:54:33 +02:00
|
|
|
super.evaluateGBDeviceEvent(deviceEvent);
|
2015-05-22 20:20:33 +02:00
|
|
|
}
|
|
|
|
|
2015-04-14 01:24:03 +02:00
|
|
|
@Override
|
2015-09-24 14:45:21 +02:00
|
|
|
public void onNotification(NotificationSpec notificationSpec) {
|
|
|
|
byte[] bytes = gbDeviceProtocol.encodeNotification(notificationSpec);
|
2015-05-13 21:55:22 +02:00
|
|
|
sendToDevice(bytes);
|
|
|
|
}
|
|
|
|
|
2015-04-14 01:24:03 +02:00
|
|
|
@Override
|
2015-08-21 00:58:18 +02:00
|
|
|
public void onSetTime() {
|
|
|
|
byte[] bytes = gbDeviceProtocol.encodeSetTime();
|
2015-04-14 01:24:03 +02:00
|
|
|
sendToDevice(bytes);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2016-04-04 20:08:34 +02:00
|
|
|
public void onSetCallState(CallSpec callSpec) {
|
|
|
|
byte[] bytes = gbDeviceProtocol.encodeSetCallState(callSpec.number, callSpec.name, callSpec.command);
|
2015-04-14 01:24:03 +02:00
|
|
|
sendToDevice(bytes);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2016-04-04 20:08:34 +02:00
|
|
|
public void onSetMusicInfo(MusicSpec musicSpec) {
|
|
|
|
byte[] bytes = gbDeviceProtocol.encodeSetMusicInfo(musicSpec.artist, musicSpec.album, musicSpec.track, musicSpec.duration, musicSpec.trackCount, musicSpec.trackNr);
|
2015-04-14 01:24:03 +02:00
|
|
|
sendToDevice(bytes);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onAppInfoReq() {
|
|
|
|
byte[] bytes = gbDeviceProtocol.encodeAppInfoReq();
|
|
|
|
sendToDevice(bytes);
|
|
|
|
}
|
|
|
|
|
2015-05-18 22:20:01 +02:00
|
|
|
@Override
|
2015-09-13 21:44:26 +02:00
|
|
|
public void onAppStart(UUID uuid, boolean start) {
|
|
|
|
byte[] bytes = gbDeviceProtocol.encodeAppStart(uuid, start);
|
2015-05-18 22:20:01 +02:00
|
|
|
sendToDevice(bytes);
|
|
|
|
}
|
|
|
|
|
2015-04-14 01:24:03 +02:00
|
|
|
@Override
|
2015-05-18 20:56:19 +02:00
|
|
|
public void onAppDelete(UUID uuid) {
|
|
|
|
byte[] bytes = gbDeviceProtocol.encodeAppDelete(uuid);
|
2015-04-14 01:24:03 +02:00
|
|
|
sendToDevice(bytes);
|
|
|
|
}
|
|
|
|
|
2015-06-06 00:40:16 +02:00
|
|
|
@Override
|
2015-06-06 19:39:04 +02:00
|
|
|
public void onFetchActivityData() {
|
2015-06-06 00:40:16 +02:00
|
|
|
byte[] bytes = gbDeviceProtocol.encodeSynchronizeActivityData();
|
|
|
|
sendToDevice(bytes);
|
|
|
|
}
|
|
|
|
|
2015-05-18 22:40:39 +02:00
|
|
|
@Override
|
2015-05-17 21:58:08 +02:00
|
|
|
public void onReboot() {
|
|
|
|
byte[] bytes = gbDeviceProtocol.encodeReboot();
|
|
|
|
sendToDevice(bytes);
|
|
|
|
}
|
2015-06-21 00:34:05 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onFindDevice(boolean start) {
|
2015-06-21 20:59:36 +02:00
|
|
|
byte[] bytes = gbDeviceProtocol.encodeFindDevice(start);
|
2015-06-21 00:34:05 +02:00
|
|
|
sendToDevice(bytes);
|
|
|
|
}
|
2015-06-24 00:23:38 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onScreenshotReq() {
|
|
|
|
byte[] bytes = gbDeviceProtocol.encodeScreenshotReq();
|
|
|
|
sendToDevice(bytes);
|
|
|
|
}
|
2015-09-02 08:00:26 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onEnableRealtimeSteps(boolean enable) {
|
|
|
|
byte[] bytes = gbDeviceProtocol.encodeEnableRealtimeSteps(enable);
|
|
|
|
sendToDevice(bytes);
|
|
|
|
}
|
2016-04-03 22:38:06 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onEnableHeartRateSleepSupport(boolean enable) {
|
|
|
|
byte[] bytes = gbDeviceProtocol.encodeEnableHeartRateSleepSupport(enable);
|
|
|
|
sendToDevice(bytes);
|
|
|
|
}
|
2015-04-14 01:24:03 +02:00
|
|
|
}
|