UNPKG

@nativescript-community/nordic-dfu

Version:

A NativeScript plugin for performing Nordic Bluetooth device firmware updates.

170 lines 6.92 kB
import { _addExecutingInitiator, _removeExecutingInitiator, DFUControllerInternal, DFUInitiatorCommon, DfuState } from './common'; import { DFUController } from '../DFUController'; var DFUServiceDelegateImpl = (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) { _removeExecutingInitiator(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: _removeExecutingInitiator(owner.peripheralUUID); state = DfuState.DFU_COMPLETED; break; case DFUState.Aborted: _removeExecutingInitiator(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 = (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; } setAndroidScanTimeout(_val) { // Not implemented on iOS return this; } setIOSConnectionTimeout(val) { this.mNative.connectionTimeout = val; 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(zipFile) { let selectedFirmware; if (typeof zipFile === 'string') { selectedFirmware = DFUFirmware.alloc().initWithUrlToZipFileError(NSURL.fileURLWithPath(zipFile)); } else if (zipFile instanceof ArrayBuffer) { const nativeBuffer = NSData.dataWithData(zipFile); selectedFirmware = DFUFirmware.alloc().initWithZipFileError(nativeBuffer); } else { throw new Error(`Incorrect zip file given: ${zipFile}`); } const identifier = NSUUID.alloc().initWithUUIDString(this.mPeripheralUUID); const nativeController = this.mNative.withFirmware(selectedFirmware).startWithTargetWithIdentifier(identifier); const serviceController = Reflect.construct(DFUControllerInternal, [nativeController], DFUController); _addExecutingInitiator(this.peripheralUUID, { object: this, serviceController, }); return serviceController; } } export { DfuState }; //# sourceMappingURL=index.ios.js.map