2021-01-10 23:37:09 +01:00
|
|
|
/* Copyright (C) 2019-2021 Andreas Shimokawa
|
2019-07-25 20:51:28 +02: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/>. */
|
|
|
|
package nodomain.freeyourgadget.gadgetbridge.service.devices.huami.operations;
|
|
|
|
|
|
|
|
import android.net.Uri;
|
|
|
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.R;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.devices.huami.HuamiService;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.service.btle.BLETypeConversions;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.service.btle.actions.SetDeviceBusyAction;
|
2022-08-18 23:03:28 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.service.devices.huami.AbstractHuamiFirmwareInfo;
|
2019-07-25 20:51:28 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.service.devices.huami.HuamiFirmwareInfo;
|
2020-11-20 22:03:23 +01:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.service.devices.huami.HuamiFirmwareType;
|
2019-07-25 20:51:28 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.service.devices.huami.HuamiSupport;
|
2020-11-20 22:03:23 +01:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.util.ArrayUtils;
|
|
|
|
|
2021-05-11 12:10:40 +02:00
|
|
|
import static nodomain.freeyourgadget.gadgetbridge.service.devices.huami.HuamiFirmwareInfo.UIHH_HEADER;
|
2019-07-25 20:51:28 +02:00
|
|
|
|
|
|
|
public class UpdateFirmwareOperationNew extends UpdateFirmwareOperation {
|
|
|
|
private static final Logger LOG = LoggerFactory.getLogger(UpdateFirmwareOperationNew.class);
|
|
|
|
|
|
|
|
|
|
|
|
public UpdateFirmwareOperationNew(Uri uri, HuamiSupport support) {
|
|
|
|
super(uri, support);
|
|
|
|
}
|
|
|
|
|
2020-11-20 22:03:23 +01:00
|
|
|
@Override
|
2019-07-25 20:51:28 +02:00
|
|
|
public boolean sendFwInfo() {
|
|
|
|
try {
|
|
|
|
TransactionBuilder builder = performInitialized("send firmware info");
|
|
|
|
builder.add(new SetDeviceBusyAction(getDevice(), getContext().getString(R.string.updating_firmware), getContext()));
|
|
|
|
int fwSize = getFirmwareInfo().getSize();
|
2020-11-20 22:03:23 +01:00
|
|
|
byte[] sizeBytes = BLETypeConversions.fromUint32(fwSize);
|
2019-07-25 20:51:28 +02:00
|
|
|
byte[] bytes = new byte[10];
|
|
|
|
int i = 0;
|
|
|
|
bytes[i++] = HuamiService.COMMAND_FIRMWARE_INIT;
|
|
|
|
bytes[i++] = getFirmwareInfo().getFirmwareType().getValue();
|
|
|
|
bytes[i++] = sizeBytes[0];
|
|
|
|
bytes[i++] = sizeBytes[1];
|
|
|
|
bytes[i++] = sizeBytes[2];
|
2020-11-20 22:03:23 +01:00
|
|
|
bytes[i++] = sizeBytes[3];
|
2019-07-25 20:51:28 +02:00
|
|
|
int crc32 = firmwareInfo.getCrc32();
|
|
|
|
byte[] crcBytes = BLETypeConversions.fromUint32(crc32);
|
|
|
|
bytes[i++] = crcBytes[0];
|
|
|
|
bytes[i++] = crcBytes[1];
|
|
|
|
bytes[i++] = crcBytes[2];
|
|
|
|
bytes[i] = crcBytes[3];
|
|
|
|
|
2020-11-20 22:03:23 +01:00
|
|
|
if (getFirmwareInfo().getFirmwareType() == HuamiFirmwareType.WATCHFACE) {
|
|
|
|
byte[] fwBytes = firmwareInfo.getBytes();
|
2021-05-11 12:10:40 +02:00
|
|
|
if (ArrayUtils.startsWith(fwBytes, UIHH_HEADER)) {
|
2020-11-20 22:03:23 +01:00
|
|
|
builder.write(getCharacteristic(HuamiService.UUID_CHARACTERISTIC_3_CONFIGURATION),
|
|
|
|
new byte[]{0x39, 0x00, 0x00, (byte) 0xff, (byte) 0xff, (byte) 0xff,
|
|
|
|
fwBytes[18],
|
|
|
|
fwBytes[19],
|
|
|
|
fwBytes[20],
|
|
|
|
fwBytes[21]
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2019-07-25 20:51:28 +02:00
|
|
|
builder.write(fwCControlChar, bytes);
|
|
|
|
builder.queue(getQueue());
|
|
|
|
return true;
|
|
|
|
} catch (IOException e) {
|
|
|
|
LOG.error("Error sending firmware info: " + e.getLocalizedMessage(), e);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2022-08-18 23:03:28 +02:00
|
|
|
protected void sendChecksum(AbstractHuamiFirmwareInfo firmwareInfo) throws IOException {
|
2019-07-25 20:51:28 +02:00
|
|
|
TransactionBuilder builder = performInitialized("send firmware upload finished");
|
|
|
|
builder.write(fwCControlChar, new byte[]{HuamiService.COMMAND_FIRMWARE_CHECKSUM});
|
|
|
|
builder.queue(getQueue());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected byte[] getFirmwareStartCommand() {
|
2019-07-25 22:18:48 +02:00
|
|
|
return new byte[]{HuamiService.COMMAND_FIRMWARE_START_DATA, 1};
|
2019-07-25 20:51:28 +02:00
|
|
|
}
|
|
|
|
}
|