2022-10-29 00:30:28 +02:00
|
|
|
/* Copyright (C) 2022 José Rebelo
|
|
|
|
|
|
|
|
This file is part of Gadgetbridge.
|
|
|
|
|
|
|
|
Gadgetbridge is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Affero General Public License as published
|
|
|
|
by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Gadgetbridge is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU Affero General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Affero General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
|
|
|
package nodomain.freeyourgadget.gadgetbridge.service.devices.huami.zeppos;
|
|
|
|
|
2023-05-06 22:02:54 +02:00
|
|
|
import android.content.Context;
|
|
|
|
|
2023-06-16 21:43:05 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
|
2023-05-06 22:02:54 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEvent;
|
2023-06-16 21:43:05 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.devices.DeviceCoordinator;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.devices.huami.Huami2021Coordinator;
|
2022-10-29 00:30:28 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.service.devices.huami.Huami2021Support;
|
2023-06-16 21:43:05 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.util.DeviceHelper;
|
2023-04-30 12:42:34 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.util.Prefs;
|
2022-10-29 00:30:28 +02:00
|
|
|
|
|
|
|
public abstract class AbstractZeppOsService {
|
|
|
|
private final Huami2021Support mSupport;
|
|
|
|
|
|
|
|
public AbstractZeppOsService(final Huami2021Support support) {
|
|
|
|
this.mSupport = support;
|
|
|
|
}
|
|
|
|
|
|
|
|
public abstract short getEndpoint();
|
2023-01-05 01:28:50 +01:00
|
|
|
|
2022-10-29 00:30:28 +02:00
|
|
|
public abstract boolean isEncrypted();
|
2023-01-05 01:28:50 +01:00
|
|
|
|
2022-10-29 00:30:28 +02:00
|
|
|
public abstract void handlePayload(final byte[] payload);
|
|
|
|
|
2023-04-30 12:42:34 +02:00
|
|
|
/**
|
|
|
|
* Handle a preference change.
|
|
|
|
* @param config the preference key
|
|
|
|
* @param prefs the device preferences
|
|
|
|
* @return true if the preference was handled, false otherwise
|
|
|
|
*/
|
|
|
|
public boolean onSendConfiguration(final String config, final Prefs prefs) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void initialize(final TransactionBuilder builder) {
|
|
|
|
// Do nothing by default
|
|
|
|
}
|
|
|
|
|
2022-10-29 13:03:25 +02:00
|
|
|
protected Huami2021Support getSupport() {
|
|
|
|
return mSupport;
|
|
|
|
}
|
|
|
|
|
2023-06-16 21:43:05 +02:00
|
|
|
protected Huami2021Coordinator getCoordinator() {
|
|
|
|
final DeviceCoordinator coordinator = DeviceHelper.getInstance().getCoordinator(getSupport().getDevice());
|
|
|
|
return (Huami2021Coordinator) coordinator;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected Prefs getDevicePrefs() {
|
|
|
|
return new Prefs(GBApplication.getDeviceSpecificSharedPrefs(getSupport().getDevice().getAddress()));
|
|
|
|
}
|
|
|
|
|
2023-01-05 01:28:50 +01:00
|
|
|
protected void write(final String taskName, final byte b) {
|
|
|
|
this.write(taskName, new byte[]{b});
|
|
|
|
}
|
|
|
|
|
2022-10-29 00:30:28 +02:00
|
|
|
protected void write(final String taskName, final byte[] data) {
|
|
|
|
this.mSupport.writeToChunked2021(taskName, getEndpoint(), data, isEncrypted());
|
|
|
|
}
|
|
|
|
|
2023-04-01 14:51:23 +02:00
|
|
|
protected void write(final TransactionBuilder builder, final byte b) {
|
|
|
|
this.write(builder, new byte[]{b});
|
|
|
|
}
|
|
|
|
|
2022-10-29 00:30:28 +02:00
|
|
|
protected void write(final TransactionBuilder builder, final byte[] data) {
|
|
|
|
this.mSupport.writeToChunked2021(builder, getEndpoint(), data, isEncrypted());
|
|
|
|
}
|
2023-04-01 21:58:21 +02:00
|
|
|
|
2023-05-06 22:02:54 +02:00
|
|
|
protected void evaluateGBDeviceEvent(final GBDeviceEvent event) {
|
|
|
|
getSupport().evaluateGBDeviceEvent(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected Context getContext() {
|
|
|
|
return getSupport().getContext();
|
|
|
|
}
|
|
|
|
|
2023-04-01 21:58:21 +02:00
|
|
|
protected static Boolean booleanFromByte(final byte b) {
|
|
|
|
switch (b) {
|
|
|
|
case 0x00:
|
|
|
|
return false;
|
|
|
|
case 0x01:
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected byte bool(final boolean bool) {
|
|
|
|
return (byte) (bool ? 0x01 : 0x00);
|
|
|
|
}
|
2022-10-29 00:30:28 +02:00
|
|
|
}
|