Small cleanups and fixlets.

here
cpfeiffer 2016-03-28 21:30:05 +02:00
parent 6ce63276a3
commit 8815f0d134
3 changed files with 22 additions and 23 deletions

View File

@ -3,9 +3,13 @@ package nodomain.freeyourgadget.gadgetbridge.service.btle.actions;
import android.bluetooth.BluetoothGatt; import android.bluetooth.BluetoothGatt;
import android.content.Context; import android.content.Context;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import nodomain.freeyourgadget.gadgetbridge.util.GB; import nodomain.freeyourgadget.gadgetbridge.util.GB;
public class SetProgressAction extends PlainAction { public class SetProgressAction extends PlainAction {
private static final Logger LOG = LoggerFactory.getLogger(SetProgressAction.class);
private final String text; private final String text;
private final boolean ongoing; private final boolean ongoing;
@ -30,6 +34,7 @@ public class SetProgressAction extends PlainAction {
@Override @Override
public boolean run(BluetoothGatt gatt) { public boolean run(BluetoothGatt gatt) {
LOG.info(toString());
GB.updateInstallNotification(this.text, this.ongoing, this.percentage, this.context); GB.updateInstallNotification(this.text, this.ongoing, this.percentage, this.context);
return true; return true;
} }

View File

@ -52,7 +52,6 @@ public class UpdateFirmwareOperation extends AbstractMiBandOperation {
} }
updateCoordinator.initNextOperation(); updateCoordinator.initNextOperation();
// updateCoordinator.initNextOperation(); // FIXME: remove, just testing mi band 1s fw update
if (!updateCoordinator.sendFwInfo()) { if (!updateCoordinator.sendFwInfo()) {
GB.toast(getContext(), "Error sending firmware info, aborting.", Toast.LENGTH_LONG, GB.ERROR); GB.toast(getContext(), "Error sending firmware info, aborting.", Toast.LENGTH_LONG, GB.ERROR);
done(); done();
@ -103,7 +102,6 @@ public class UpdateFirmwareOperation extends AbstractMiBandOperation {
if (firmwareInfoSent) { if (firmwareInfoSent) {
GB.toast(getContext(), "Firmware metadata successfully sent.", Toast.LENGTH_LONG, GB.INFO); GB.toast(getContext(), "Firmware metadata successfully sent.", Toast.LENGTH_LONG, GB.INFO);
if (!updateCoordinator.sendFwData()) { if (!updateCoordinator.sendFwData()) {
//TODO: the firmware transfer failed, but the miband should be still functional with the old firmware. What should we do?
GB.toast(getContext().getString(R.string.updatefirmwareoperation_updateproblem_do_not_reboot), Toast.LENGTH_LONG, GB.ERROR); GB.toast(getContext().getString(R.string.updatefirmwareoperation_updateproblem_do_not_reboot), Toast.LENGTH_LONG, GB.ERROR);
done(); done();
} }
@ -119,7 +117,7 @@ public class UpdateFirmwareOperation extends AbstractMiBandOperation {
if (updateCoordinator.initNextOperation()) { if (updateCoordinator.initNextOperation()) {
GB.toast(getContext(), "Heart Rate Firmware successfully updated, now updating Mi Band Firmware", Toast.LENGTH_LONG, GB.INFO); GB.toast(getContext(), "Heart Rate Firmware successfully updated, now updating Mi Band Firmware", Toast.LENGTH_LONG, GB.INFO);
if (!updateCoordinator.sendFwInfo()) { if (!updateCoordinator.sendFwInfo()) {
GB.toast(getContext(), "Sending Mi Band Firmware failed, aborting. Do NOT reboot your Mi Band!", Toast.LENGTH_LONG, GB.INFO); GB.toast(getContext(), "Error sending firmware info, aborting.", Toast.LENGTH_LONG, GB.ERROR);
done(); done();
} }
break; break;
@ -256,10 +254,10 @@ public class UpdateFirmwareOperation extends AbstractMiBandOperation {
/** /**
* Method that uploads a firmware (fwbytes) to the MiBand. * Method that uploads a firmware (fwbytes) to the Mi Band.
* The firmware has to be splitted into chunks of 20 bytes each, and periodically a COMMAND_SYNC comand has to be issued to the MiBand. * 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/> * <p/>
* The Mi Band will send a notification after receiving these data to confirm if the firmware looks good to it. * The Mi Band will send a notification after receiving this data to confirm if the firmware looks good to it.
* *
* @param fwbytes * @param fwbytes
* @return whether the transfer succeeded or not. Only a BT layer exception will cause the transmission to fail. * @return whether the transfer succeeded or not. Only a BT layer exception will cause the transmission to fail.
@ -270,6 +268,7 @@ public class UpdateFirmwareOperation extends AbstractMiBandOperation {
final int packetLength = 20; final int packetLength = 20;
int packets = len / packetLength; int packets = len / packetLength;
// going from 0 to len
int firmwareProgress = 0; int firmwareProgress = 0;
try { try {
@ -280,32 +279,25 @@ public class UpdateFirmwareOperation extends AbstractMiBandOperation {
builder.write(getCharacteristic(MiBandService.UUID_CHARACTERISTIC_FIRMWARE_DATA), fwChunk); builder.write(getCharacteristic(MiBandService.UUID_CHARACTERISTIC_FIRMWARE_DATA), fwChunk);
firmwareProgress += packetLength; firmwareProgress += packetLength;
int progressPercent = (int) (((float) firmwareProgress) / len) * 100;
if ((i > 0) && (i % 50 == 0)) { if ((i > 0) && (i % 50 == 0)) {
builder.write(getCharacteristic(MiBandService.UUID_CHARACTERISTIC_CONTROL_POINT), new byte[]{MiBandService.COMMAND_SYNC}); builder.write(getCharacteristic(MiBandService.UUID_CHARACTERISTIC_CONTROL_POINT), new byte[]{MiBandService.COMMAND_SYNC});
builder.add(new SetProgressAction(getContext().getString(R.string.updatefirmwareoperation_update_in_progress), true, (int) (((float) firmwareProgress) / len * 100), getContext())); builder.add(new SetProgressAction(getContext().getString(R.string.updatefirmwareoperation_update_in_progress), true, progressPercent, getContext()));
} }
LOG.info("Firmware update progress:" + firmwareProgress + " total len:" + len + " progress:" + (int) (((float) firmwareProgress) / len * 100));
} }
if (!(len % packetLength == 0)) { if (firmwareProgress < len) {
byte[] lastChunk = Arrays.copyOfRange(fwbytes, packets * packetLength, len); byte[] lastChunk = Arrays.copyOfRange(fwbytes, packets * packetLength, len);
builder.write(getCharacteristic(MiBandService.UUID_CHARACTERISTIC_FIRMWARE_DATA), lastChunk); builder.write(getCharacteristic(MiBandService.UUID_CHARACTERISTIC_FIRMWARE_DATA), lastChunk);
firmwareProgress += len % packetLength; firmwareProgress = len;
}
LOG.info("Firmware update progress:" + firmwareProgress + " total len:" + len + " progress:" + (firmwareProgress / len));
if (firmwareProgress >= len) {
builder.write(getCharacteristic(MiBandService.UUID_CHARACTERISTIC_CONTROL_POINT), new byte[]{MiBandService.COMMAND_SYNC});
} else {
GB.updateInstallNotification(getContext().getString(R.string.updatefirmwareoperation_write_failed), false, 0, getContext());
} }
builder.write(getCharacteristic(MiBandService.UUID_CHARACTERISTIC_CONTROL_POINT), new byte[]{MiBandService.COMMAND_SYNC});
builder.queue(getQueue()); builder.queue(getQueue());
} catch (IOException ex) { } catch (IOException ex) {
LOG.error("Unable to send fw to MI", ex); LOG.error("Unable to send fw to MI", ex);
GB.updateInstallNotification(getContext().getString(R.string.updatefirmwareoperation_write_failed), false, 0, getContext()); GB.updateInstallNotification(getContext().getString(R.string.updatefirmwareoperation_firmware_not_sent), false, 0, getContext());
return false; return false;
} }
return true; return true;
@ -381,7 +373,8 @@ public class UpdateFirmwareOperation extends AbstractMiBandOperation {
INITIAL, INITIAL,
SEND_FW2, SEND_FW2,
SEND_FW1, SEND_FW1,
FINISHED, UNKNOWN FINISHED,
UNKNOWN
} }
private class DoubleUpdateCoordinator extends UpdateCoordinator { private class DoubleUpdateCoordinator extends UpdateCoordinator {
@ -389,7 +382,7 @@ public class UpdateFirmwareOperation extends AbstractMiBandOperation {
private final byte[] fw1Info; private final byte[] fw1Info;
private final byte[] fw1Data; private final byte[] fw1Data;
private final byte[] fw21nfo; private final byte[] fw2Info;
private final byte[] fw2Data; private final byte[] fw2Data;
private byte[] currentFwInfo; private byte[] currentFwInfo;
@ -401,7 +394,7 @@ public class UpdateFirmwareOperation extends AbstractMiBandOperation {
super(reboot); super(reboot);
this.fw1Info = fw1Info; this.fw1Info = fw1Info;
this.fw1Data = fw1Data; this.fw1Data = fw1Data;
this.fw21nfo = fw2Info; this.fw2Info = fw2Info;
this.fw2Data = fw2Data; this.fw2Data = fw2Data;
// start with fw2 (heart rate) // start with fw2 (heart rate)
@ -423,7 +416,7 @@ public class UpdateFirmwareOperation extends AbstractMiBandOperation {
public boolean initNextOperation() { public boolean initNextOperation() {
switch (state) { switch (state) {
case INITIAL: case INITIAL:
currentFwInfo = fw21nfo; currentFwInfo = fw2Info;
currentFwData = fw2Data; currentFwData = fw2Data;
state = State.SEND_FW2; state = State.SEND_FW2;
return true; return true;

View File

@ -236,5 +236,6 @@
<string name="error_creating_directory_for_logfiles">Error creating directory for log files: %1$s</string> <string name="error_creating_directory_for_logfiles">Error creating directory for log files: %1$s</string>
<string name="DEVINFO_HR_VER">"HR: "</string> <string name="DEVINFO_HR_VER">"HR: "</string>
<string name="updatefirmwareoperation_update_in_progress">Firmware update in progress</string> <string name="updatefirmwareoperation_update_in_progress">Firmware update in progress</string>
<string name="updatefirmwareoperation_firmware_not_sent">Firmware not sent</string>
</resources> </resources>