@nativescript-community/nordic-dfu
Version:
A NativeScript plugin for performing Nordic Bluetooth device firmware updates.
156 lines • 6.53 kB
JavaScript
import { DFUInitiatorCommon, DfuState } from './common';
import { DfuServiceController } from './serviceController';
// Keep strong references of the initiators while running
const executingInitiators = new Map();
var DFUServiceDelegateImpl = /** @class */ (function (_super) {
__extends(DFUServiceDelegateImpl, _super);
function DFUServiceDelegateImpl() {
return _super !== null && _super.apply(this, arguments) || this;
}
DFUServiceDelegateImpl.initWithOwner = function (owner) {
var delegate = DFUServiceDelegateImpl.new();
delegate.mOwner = new WeakRef(owner);
return delegate;
};
DFUServiceDelegateImpl.prototype.dfuErrorDidOccurWithMessage = function (error, message) {
var _a, _b;
var owner = (_b = (_a = this.mOwner) === null || _a === void 0 ? void 0 : _a.deref) === null || _b === void 0 ? void 0 : _b.call(_a);
if (owner) {
if (executingInitiators.has(owner.peripheralUUID)) {
executingInitiators.delete(owner.peripheralUUID);
}
owner._notifyDfuStateChanged(DfuState.DFU_FAILED, message);
}
};
DFUServiceDelegateImpl.prototype.dfuStateDidChangeTo = function (nativeState) {
var _a, _b;
var owner = (_b = (_a = this.mOwner) === null || _a === void 0 ? void 0 : _a.deref) === null || _b === void 0 ? void 0 : _b.call(_a);
if (owner) {
var state = void 0;
switch (nativeState) {
case DFUState.Connecting:
state = DfuState.CONNECTING;
break;
case DFUState.Starting:
state = DfuState.DFU_PROCESS_STARTING;
break;
case DFUState.EnablingDfuMode:
state = DfuState.ENABLING_DFU_MODE;
break;
case DFUState.Uploading:
state = DfuState.DFU_PROCESS_STARTING;
break;
case DFUState.Validating:
state = DfuState.FIRMWARE_VALIDATING;
break;
case DFUState.Disconnecting:
state = DfuState.DEVICE_DISCONNECTING;
break;
case DFUState.Completed:
if (executingInitiators.has(owner.peripheralUUID)) {
executingInitiators.delete(owner.peripheralUUID);
}
state = DfuState.DFU_COMPLETED;
break;
case DFUState.Aborted:
if (executingInitiators.has(owner.peripheralUUID)) {
executingInitiators.delete(owner.peripheralUUID);
}
state = DfuState.DFU_ABORTED;
break;
default:
state = null;
console.error("Invalid DFU state change to '".concat(nativeState, "'"));
break;
}
if (state != null) {
owner._notifyDfuStateChanged(state);
}
}
};
DFUServiceDelegateImpl.ObjCProtocols = [DFUServiceDelegate];
return DFUServiceDelegateImpl;
}(NSObject));
var DFUProgressDelegateImpl = /** @class */ (function (_super) {
__extends(DFUProgressDelegateImpl, _super);
function DFUProgressDelegateImpl() {
return _super !== null && _super.apply(this, arguments) || this;
}
DFUProgressDelegateImpl.initWithOwner = function (owner) {
var delegate = DFUProgressDelegateImpl.new();
delegate.mOwner = new WeakRef(owner);
return delegate;
};
DFUProgressDelegateImpl.prototype.dfuProgressDidChangeForOutOfToCurrentSpeedBytesPerSecondAvgSpeedBytesPerSecond = function (part, totalParts, progress, currentSpeedBytesPerSecond, avgSpeedBytesPerSecond) {
var _a, _b;
var owner = (_b = (_a = this.mOwner) === null || _a === void 0 ? void 0 : _a.deref) === null || _b === void 0 ? void 0 : _b.call(_a);
if (owner) {
owner.notify({
eventName: DFUInitiator.dfuProgressEvent,
object: owner,
percent: progress,
speed: currentSpeedBytesPerSecond,
avgSpeed: avgSpeedBytesPerSecond,
currentPart: part,
totalParts: totalParts,
});
}
};
DFUProgressDelegateImpl.ObjCProtocols = [DFUProgressDelegate];
return DFUProgressDelegateImpl;
}(NSObject));
export class DFUInitiator extends DFUInitiatorCommon {
constructor(peripheralUUID) {
super(peripheralUUID);
this.mNative = DFUHelper.initDFUServiceInitiator();
this.mDelegate = DFUServiceDelegateImpl.initWithOwner(this);
this.mProgressDelegate = DFUProgressDelegateImpl.initWithOwner(this);
this.mNative.delegate = this.mDelegate;
this.mNative.progressDelegate = this.mProgressDelegate;
}
setAndroidDeviceName(name) {
// Not implemented on iOS
return this;
}
setAndroidDisableNotification(val) {
// Not implemented on iOS
return this;
}
setAndroidForeground(val) {
// Not implemented on iOS
return this;
}
setAndroidKeepBond(val) {
// Not implemented on iOS
return this;
}
setForceDfu(val) {
this.mNative.forceDfu = val;
return this;
}
setForceScanningForNewAddressInLegacyDfu(val) {
this.mNative.forceScanningForNewAddressInLegacyDfu = val;
return this;
}
setUnsafeExperimentalButtonlessServiceInSecureDfuEnabled(val) {
this.mNative.enableUnsafeExperimentalButtonlessServiceInSecureDfu = val;
return this;
}
setPacketReceiptNotificationParameter(val) {
this.mNative.packetReceiptNotificationParameter = val;
return this;
}
disableResume() {
this.mNative.disableResume = true;
return this;
}
start(filePath) {
const selectedFirmware = DFUFirmware.alloc().initWithUrlToZipFileError(NSURL.fileURLWithPath(filePath));
const identifier = NSUUID.alloc().initWithUUIDString(this.mPeripheralUUID);
const nativeController = this.mNative.withFirmware(selectedFirmware).startWithTargetWithIdentifier(identifier);
executingInitiators.set(this.peripheralUUID, this);
return new DfuServiceController(nativeController);
}
}
export { DfuState };
//# sourceMappingURL=index.ios.js.map