generate method
Generates the zip file with all the information to send to a device via OTA
Implementation
Future<Uint8List> generate() async{
if(applicationFirmware == null && bootloaderFirmware == null && softDeviceFirmware == null) throw Exception("No Files Found!");
if(applicationFirmware != null && bootloaderFirmware != null && softDeviceFirmware == null) throw Exception("Error Application must have, Softdevice and Bootloader Files!");
List<FwType> key = [];
List<String> fileNames = [];
List<Uint8List>? firmware = [];
Archive archive = Archive();
int? fwdblsize;
int? fwdsdsize;
if(applicationFirmware != null){
logger?.verbose("Application Firmware to Bin array!");
String app = applicationFirmware!;
firmware.add(IntelHex.decodeRecord(app).toBinArray(isApplication: true));
fileNames.add('application');
key.add(FwType.application);
}
if(bootloaderFirmware != null || softDeviceFirmware != null){
if(bootloaderFirmware != null && softDeviceFirmware != null){
logger?.verbose("Bootloader and Softdevice Firmware to Bin array!");
key.add(FwType.softdeviceBootloader);
fileNames.add('sd_bl');
String app1 = softDeviceFirmware!;
String app = bootloaderFirmware!;
Uint8List sdhex = IntelHex.decodeRecord(app1).toBinArray();
Uint8List blhex = IntelHex.decodeRecord(app).toBinArray();
fwdblsize = blhex.length;
fwdsdsize = sdhex.length;
firmware.add(Uint8List.fromList(sdhex+blhex));
}
else if(bootloaderFirmware != null){
logger?.verbose("Bootloader Firmware to Bin array!");
String app = bootloaderFirmware!;
firmware.add(IntelHex.decodeRecord(app).toBinArray());
key.add(FwType.bootloader);
fileNames.add('bootloader');
}
else if(softDeviceFirmware != null){
logger?.verbose("Softdevice Firmware to Bin array!");
String app = softDeviceFirmware!;
firmware.add(IntelHex.decodeRecord(app).toBinArray());
key.add(FwType.softdevice);
fileNames.add('softdevice');
}
}
String mani = json.encode(_manifest(key,fileNames,[fwdblsize,fwdsdsize]));
for(int i = 0; i < key.length; i++){
//Calculate the hash for the .bin file located in the work directory
List<int> firmwareHash = NRFPackage.calculateSHA256(firmware[i],key[i]);
int binLength = firmware[i].length;
int sdSize = 0;
int blSize = 0;
int appSize = 0;
if(key[i] == FwType.application || key[i] == FwType.externalApplication){
appSize = binLength;
}
else if(key[i] == FwType.softdevice){
sdSize = binLength;
}
else if(key[i] == FwType.bootloader){
blSize = binLength;
}
else if(key[i] == FwType.softdeviceBootloader){
blSize = fwdblsize!;
sdSize = fwdsdsize!;
}
List<List<int>> bootValidationBytesArray = [];
for(int x = 0; x < bootValidationTypeArray.length; x++){
if(bootValidationTypeArray[x] == ValidationType.validateP256){
if(key[i] == FwType.softdeviceBootloader){
bootValidationBytesArray.add(NRFPackage.signFirmware(signer,firmware[i]));
}
else{
bootValidationBytesArray.add(NRFPackage.signFirmware(signer,firmware[i]));
}
}
else{
bootValidationBytesArray.add([]);
}
}
final InitPacket initPacket = InitPacket(
fromBytes: null,
hashBytes: firmwareHash,
hashType: HashType.sha256,
bootValidationType: bootValidationTypeArray,
bootValidationBytes: bootValidationBytesArray,
dfuType: key[i],
isDebug: mode == NRFUtilMode.debug,
fwVersion: sdSize == 0?blSize == 0?applicationVersion:bootloaderVersion:0xffffffff,
hwVersion: hardwareVersion,
sdSize: sdSize,
appSize: appSize,
blSize: blSize,
sdReq: [sofDeviceReq],
);
final Uint8List sig = signer.sign(initPacket.getInitCommandBytes());
initPacket.setSignature(sig, SignatureType.ecdsaSHA256);
signer.verify(initPacket.getInitCommandBytes());
final Uint8List pack = initPacket.getPacketBytes();
archive.addFile(ArchiveFile('${fileNames[i]}.bin', firmware[i].length, firmware[i]));
archive.addFile(ArchiveFile('${fileNames[i]}.dat', pack.length, pack));
}
archive.addFile(ArchiveFile('manifest.json', mani.length, mani));
return NRFPackage.createZipFile(archive);
}