2021-01-10 23:37:09 +01:00
|
|
|
/* Copyright (C) 2016-2021 Andreas Shimokawa, Carsten Pfeiffer, Daniele
|
2019-02-13 20:43:30 +01:00
|
|
|
Gobbetti
|
2017-03-10 14:53:19 +01:00
|
|
|
|
|
|
|
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/>. */
|
2018-08-01 22:56:01 +02:00
|
|
|
package nodomain.freeyourgadget.gadgetbridge.service.devices.huami.operations;
|
2016-12-11 02:10:07 +01:00
|
|
|
|
|
|
|
import android.bluetooth.BluetoothGatt;
|
|
|
|
import android.bluetooth.BluetoothGattCharacteristic;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.net.Uri;
|
|
|
|
import android.widget.Toast;
|
|
|
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.UUID;
|
|
|
|
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.R;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventDisplayMessage;
|
2017-10-24 22:01:25 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.devices.huami.HuamiFWHelper;
|
2018-08-02 10:55:30 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.devices.huami.HuamiService;
|
2016-12-11 02:10:07 +01:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.service.btle.BLETypeConversions;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.service.btle.actions.SetDeviceBusyAction;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.service.btle.actions.SetProgressAction;
|
2022-08-18 23:03:28 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.service.devices.huami.AbstractHuamiFirmwareInfo;
|
2018-08-01 22:56:01 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.service.devices.huami.AbstractHuamiOperation;
|
2017-10-21 22:50:28 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.service.devices.huami.HuamiFirmwareType;
|
2018-08-01 22:56:01 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.service.devices.huami.HuamiSupport;
|
2016-12-11 02:10:07 +01:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.util.GB;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.util.Prefs;
|
|
|
|
|
2018-08-01 22:56:01 +02:00
|
|
|
public class UpdateFirmwareOperation extends AbstractHuamiOperation {
|
2016-12-11 02:10:07 +01:00
|
|
|
private static final Logger LOG = LoggerFactory.getLogger(UpdateFirmwareOperation.class);
|
|
|
|
|
2017-08-25 01:17:34 +02:00
|
|
|
protected final Uri uri;
|
2019-07-25 20:51:28 +02:00
|
|
|
final BluetoothGattCharacteristic fwCControlChar;
|
2020-09-28 12:04:00 +02:00
|
|
|
final BluetoothGattCharacteristic fwCDataChar;
|
2017-08-25 00:21:47 +02:00
|
|
|
protected final Prefs prefs = GBApplication.getPrefs();
|
2022-08-18 23:03:28 +02:00
|
|
|
protected AbstractHuamiFirmwareInfo firmwareInfo;
|
2016-12-11 02:10:07 +01:00
|
|
|
|
2018-08-01 22:56:01 +02:00
|
|
|
public UpdateFirmwareOperation(Uri uri, HuamiSupport support) {
|
2016-12-11 02:10:07 +01:00
|
|
|
super(support);
|
|
|
|
this.uri = uri;
|
2018-08-02 10:55:30 +02:00
|
|
|
fwCControlChar = getCharacteristic(HuamiService.UUID_CHARACTERISTIC_FIRMWARE);
|
|
|
|
fwCDataChar = getCharacteristic(HuamiService.UUID_CHARACTERISTIC_FIRMWARE_DATA);
|
2016-12-11 02:10:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void enableNeededNotifications(TransactionBuilder builder, boolean enable) {
|
|
|
|
builder.notify(fwCControlChar, enable);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void doPerform() throws IOException {
|
2017-09-02 21:45:56 +02:00
|
|
|
firmwareInfo = createFwInfo(uri, getContext());
|
2016-12-11 02:10:07 +01:00
|
|
|
if (!firmwareInfo.isGenerallyCompatibleWith(getDevice())) {
|
|
|
|
throw new IOException("Firmware is not compatible with the given device: " + getDevice().getAddress());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!sendFwInfo()) {
|
|
|
|
displayMessage(getContext(), "Error sending firmware info, aborting.", Toast.LENGTH_LONG, GB.ERROR);
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
//the firmware will be sent by the notification listener if the band confirms that the metadata are ok.
|
|
|
|
}
|
|
|
|
|
2022-08-18 23:03:28 +02:00
|
|
|
AbstractHuamiFirmwareInfo createFwInfo(Uri uri, Context context) throws IOException {
|
2017-10-24 22:01:25 +02:00
|
|
|
HuamiFWHelper fwHelper = getSupport().createFWHelper(uri, context);
|
2017-09-02 21:45:56 +02:00
|
|
|
return fwHelper.getFirmwareInfo();
|
|
|
|
}
|
|
|
|
|
2017-08-25 01:17:34 +02:00
|
|
|
protected void done() {
|
2016-12-11 02:10:07 +01:00
|
|
|
LOG.info("Operation done.");
|
|
|
|
operationFinished();
|
|
|
|
unsetBusy();
|
|
|
|
}
|
|
|
|
|
2017-09-25 23:23:20 +02:00
|
|
|
@Override
|
|
|
|
public boolean onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
|
|
|
|
if (status != BluetoothGatt.GATT_SUCCESS) {
|
|
|
|
operationFailed();
|
|
|
|
}
|
|
|
|
return super.onCharacteristicWrite(gatt, characteristic, status);
|
|
|
|
}
|
|
|
|
|
2020-09-28 12:04:00 +02:00
|
|
|
void operationFailed() {
|
2017-09-25 23:23:20 +02:00
|
|
|
GB.updateInstallNotification(getContext().getString(R.string.updatefirmwareoperation_write_failed), false, 0, getContext());
|
|
|
|
}
|
|
|
|
|
2016-12-11 02:10:07 +01:00
|
|
|
@Override
|
|
|
|
public boolean onCharacteristicChanged(BluetoothGatt gatt,
|
|
|
|
BluetoothGattCharacteristic characteristic) {
|
|
|
|
UUID characteristicUUID = characteristic.getUuid();
|
|
|
|
if (fwCControlChar.getUuid().equals(characteristicUUID)) {
|
|
|
|
handleNotificationNotif(characteristic.getValue());
|
2017-09-25 00:03:40 +02:00
|
|
|
return true; // don't let anyone else handle it
|
2016-12-11 02:10:07 +01:00
|
|
|
} else {
|
|
|
|
super.onCharacteristicChanged(gatt, characteristic);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* React to messages sent by the Mi Band to the MiBandService.UUID_CHARACTERISTIC_NOTIFICATION
|
|
|
|
* characteristic,
|
|
|
|
* These messages appear to be always 1 byte long, with values that are listed in MiBandService.
|
|
|
|
* It is not excluded that there are further values which are still unknown.
|
|
|
|
* <p/>
|
|
|
|
* Upon receiving known values that request further action by GB, the appropriate method is called.
|
|
|
|
*
|
|
|
|
* @param value
|
|
|
|
*/
|
2020-09-28 12:04:00 +02:00
|
|
|
protected void handleNotificationNotif(byte[] value) {
|
2019-07-25 20:51:28 +02:00
|
|
|
if (value.length != 3 && value.length != 11) {
|
|
|
|
LOG.error("Notifications should be 3 or 11 bytes long.");
|
2016-12-11 02:10:07 +01:00
|
|
|
getSupport().logMessageContent(value);
|
|
|
|
return;
|
|
|
|
}
|
2018-08-02 10:55:30 +02:00
|
|
|
boolean success = value[2] == HuamiService.SUCCESS;
|
2016-12-11 02:10:07 +01:00
|
|
|
|
2018-08-02 10:55:30 +02:00
|
|
|
if (value[0] == HuamiService.RESPONSE && success) {
|
2016-12-11 02:10:07 +01:00
|
|
|
try {
|
|
|
|
switch (value[1]) {
|
2018-08-02 10:55:30 +02:00
|
|
|
case HuamiService.COMMAND_FIRMWARE_INIT: {
|
2016-12-11 02:10:07 +01:00
|
|
|
sendFirmwareData(getFirmwareInfo());
|
|
|
|
break;
|
|
|
|
}
|
2018-08-02 10:55:30 +02:00
|
|
|
case HuamiService.COMMAND_FIRMWARE_START_DATA: {
|
2016-12-11 02:10:07 +01:00
|
|
|
sendChecksum(getFirmwareInfo());
|
|
|
|
break;
|
|
|
|
}
|
2018-08-02 10:55:30 +02:00
|
|
|
case HuamiService.COMMAND_FIRMWARE_CHECKSUM: {
|
2017-10-21 22:50:28 +02:00
|
|
|
if (getFirmwareInfo().getFirmwareType() == HuamiFirmwareType.FIRMWARE) {
|
2017-09-25 00:03:40 +02:00
|
|
|
TransactionBuilder builder = performInitialized("reboot");
|
|
|
|
getSupport().sendReboot(builder);
|
|
|
|
builder.queue(getQueue());
|
2017-03-07 23:20:59 +01:00
|
|
|
} else {
|
|
|
|
GB.updateInstallNotification(getContext().getString(R.string.updatefirmwareoperation_update_complete), false, 100, getContext());
|
|
|
|
done();
|
|
|
|
}
|
2016-12-11 02:10:07 +01:00
|
|
|
break;
|
|
|
|
}
|
2018-08-02 10:55:30 +02:00
|
|
|
case HuamiService.COMMAND_FIRMWARE_REBOOT: {
|
2017-09-25 00:03:40 +02:00
|
|
|
LOG.info("Reboot command successfully sent.");
|
2016-12-11 02:10:07 +01:00
|
|
|
GB.updateInstallNotification(getContext().getString(R.string.updatefirmwareoperation_update_complete), false, 100, getContext());
|
|
|
|
done();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
LOG.error("Unexpected response during firmware update: ");
|
|
|
|
getSupport().logMessageContent(value);
|
2017-09-25 23:23:20 +02:00
|
|
|
operationFailed();
|
2016-12-11 02:10:07 +01:00
|
|
|
displayMessage(getContext(), getContext().getString(R.string.updatefirmwareoperation_updateproblem_do_not_reboot), Toast.LENGTH_LONG, GB.ERROR);
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (Exception ex) {
|
|
|
|
displayMessage(getContext(), getContext().getString(R.string.updatefirmwareoperation_updateproblem_do_not_reboot), Toast.LENGTH_LONG, GB.ERROR);
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
LOG.error("Unexpected notification during firmware update: ");
|
2017-09-25 23:23:20 +02:00
|
|
|
operationFailed();
|
2016-12-11 02:10:07 +01:00
|
|
|
getSupport().logMessageContent(value);
|
|
|
|
displayMessage(getContext(), getContext().getString(R.string.updatefirmwareoperation_metadata_updateproblem), Toast.LENGTH_LONG, GB.ERROR);
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
}
|
2019-07-25 20:51:28 +02:00
|
|
|
|
2020-09-28 12:04:00 +02:00
|
|
|
void displayMessage(Context context, String message, int duration, int severity) {
|
2016-12-11 02:10:07 +01:00
|
|
|
getSupport().handleGBDeviceEvent(new GBDeviceEventDisplayMessage(message, duration, severity));
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean sendFwInfo() {
|
|
|
|
try {
|
|
|
|
TransactionBuilder builder = performInitialized("send firmware info");
|
|
|
|
// getSupport().setLowLatency(builder);
|
|
|
|
builder.add(new SetDeviceBusyAction(getDevice(), getContext().getString(R.string.updating_firmware), getContext()));
|
|
|
|
int fwSize = getFirmwareInfo().getSize();
|
|
|
|
byte[] sizeBytes = BLETypeConversions.fromUint24(fwSize);
|
2017-03-07 23:20:59 +01:00
|
|
|
int arraySize = 4;
|
2017-10-21 22:50:28 +02:00
|
|
|
boolean isFirmwareCode = getFirmwareInfo().getFirmwareType() == HuamiFirmwareType.FIRMWARE;
|
2017-03-07 23:20:59 +01:00
|
|
|
if (!isFirmwareCode) {
|
|
|
|
arraySize++;
|
|
|
|
}
|
|
|
|
byte[] bytes = new byte[arraySize];
|
|
|
|
int i = 0;
|
2018-08-02 10:55:30 +02:00
|
|
|
bytes[i++] = HuamiService.COMMAND_FIRMWARE_INIT;
|
2017-03-07 23:20:59 +01:00
|
|
|
bytes[i++] = sizeBytes[0];
|
|
|
|
bytes[i++] = sizeBytes[1];
|
|
|
|
bytes[i++] = sizeBytes[2];
|
|
|
|
if (!isFirmwareCode) {
|
2019-07-25 20:51:28 +02:00
|
|
|
bytes[i] = getFirmwareInfo().getFirmwareType().getValue();
|
2017-03-07 23:20:59 +01:00
|
|
|
}
|
2016-12-11 02:10:07 +01:00
|
|
|
|
|
|
|
builder.write(fwCControlChar, bytes);
|
|
|
|
builder.queue(getQueue());
|
|
|
|
return true;
|
|
|
|
} catch (IOException e) {
|
|
|
|
LOG.error("Error sending firmware info: " + e.getLocalizedMessage(), e);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Method that uploads a firmware (fwbytes) to the Mi Band.
|
|
|
|
* The firmware has to be split into chunks of 20 bytes each, and periodically a COMMAND_SYNC command has to be issued to the Mi Band.
|
|
|
|
* <p/>
|
|
|
|
* The Mi Band will send a notification after receiving this data to confirm if the firmware looks good to it.
|
|
|
|
*
|
|
|
|
* @param info
|
|
|
|
* @return whether the transfer succeeded or not. Only a BT layer exception will cause the transmission to fail.
|
2017-03-07 00:06:35 +01:00
|
|
|
* @see #handleNotificationNotif
|
2016-12-11 02:10:07 +01:00
|
|
|
*/
|
2022-08-18 23:03:28 +02:00
|
|
|
private boolean sendFirmwareData(AbstractHuamiFirmwareInfo info) {
|
2016-12-11 02:10:07 +01:00
|
|
|
byte[] fwbytes = info.getBytes();
|
|
|
|
int len = fwbytes.length;
|
2019-12-26 23:05:13 +01:00
|
|
|
final int packetLength = getSupport().getMTU() - 3;
|
2016-12-11 02:10:07 +01:00
|
|
|
int packets = len / packetLength;
|
|
|
|
|
|
|
|
try {
|
|
|
|
// going from 0 to len
|
|
|
|
int firmwareProgress = 0;
|
|
|
|
|
|
|
|
TransactionBuilder builder = performInitialized("send firmware packet");
|
2019-07-25 20:51:28 +02:00
|
|
|
builder.write(fwCControlChar, getFirmwareStartCommand());
|
2016-12-11 02:10:07 +01:00
|
|
|
|
|
|
|
for (int i = 0; i < packets; i++) {
|
|
|
|
byte[] fwChunk = Arrays.copyOfRange(fwbytes, i * packetLength, i * packetLength + packetLength);
|
|
|
|
|
|
|
|
builder.write(fwCDataChar, fwChunk);
|
|
|
|
firmwareProgress += packetLength;
|
|
|
|
|
|
|
|
int progressPercent = (int) ((((float) firmwareProgress) / len) * 100);
|
|
|
|
if ((i > 0) && (i % 100 == 0)) {
|
2018-08-02 10:55:30 +02:00
|
|
|
builder.write(fwCControlChar, new byte[]{HuamiService.COMMAND_FIRMWARE_UPDATE_SYNC});
|
2016-12-11 02:10:07 +01:00
|
|
|
builder.add(new SetProgressAction(getContext().getString(R.string.updatefirmwareoperation_update_in_progress), true, progressPercent, getContext()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (firmwareProgress < len) {
|
|
|
|
byte[] lastChunk = Arrays.copyOfRange(fwbytes, packets * packetLength, len);
|
|
|
|
builder.write(fwCDataChar, lastChunk);
|
|
|
|
}
|
|
|
|
|
2018-08-02 10:55:30 +02:00
|
|
|
builder.write(fwCControlChar, new byte[]{HuamiService.COMMAND_FIRMWARE_UPDATE_SYNC});
|
2016-12-11 02:10:07 +01:00
|
|
|
builder.queue(getQueue());
|
|
|
|
|
|
|
|
} catch (IOException ex) {
|
2019-07-25 20:51:28 +02:00
|
|
|
LOG.error("Unable to send fw to device", ex);
|
2016-12-11 02:10:07 +01:00
|
|
|
GB.updateInstallNotification(getContext().getString(R.string.updatefirmwareoperation_firmware_not_sent), false, 0, getContext());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-08-18 23:03:28 +02:00
|
|
|
protected void sendChecksum(AbstractHuamiFirmwareInfo firmwareInfo) throws IOException {
|
2016-12-11 02:10:07 +01:00
|
|
|
TransactionBuilder builder = performInitialized("send firmware checksum");
|
|
|
|
int crc16 = firmwareInfo.getCrc16();
|
|
|
|
byte[] bytes = BLETypeConversions.fromUint16(crc16);
|
2019-07-25 20:51:28 +02:00
|
|
|
builder.write(fwCControlChar, new byte[]{
|
2018-08-02 10:55:30 +02:00
|
|
|
HuamiService.COMMAND_FIRMWARE_CHECKSUM,
|
2016-12-11 02:10:07 +01:00
|
|
|
bytes[0],
|
|
|
|
bytes[1],
|
|
|
|
});
|
|
|
|
builder.queue(getQueue());
|
|
|
|
}
|
|
|
|
|
2022-08-18 23:03:28 +02:00
|
|
|
AbstractHuamiFirmwareInfo getFirmwareInfo() {
|
2016-12-11 02:10:07 +01:00
|
|
|
return firmwareInfo;
|
|
|
|
}
|
2019-07-25 20:51:28 +02:00
|
|
|
|
|
|
|
protected byte[] getFirmwareStartCommand() {
|
|
|
|
return new byte[]{HuamiService.COMMAND_FIRMWARE_START_DATA};
|
|
|
|
}
|
|
|
|
}
|