Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: allow apps to controle auto connect when with sdfu device setup #927

Merged
merged 5 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ This project does _not_ adhere to
[Semantic Versioning](https://semver.org/spec/v2.0.0.html) but contrary to it
every new version is a new major version.

## 177.0.0 - 2024-05-28

### Added

- Apps can no control better when to auto reconnect after programming with
`sdfuDeviceSetup`. This is optional. Defaults have nit changed

## 176.0.0 - 2024-05-06

### Added
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nordicsemiconductor/pc-nrfconnect-shared",
"version": "176.0.0",
"version": "177.0.0",
"description": "Shared commodities for developing pc-nrfconnect-* packages",
"repository": {
"type": "git",
Expand Down
15 changes: 0 additions & 15 deletions scripts/nordic-publish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,6 @@ import { AppInfo, SourceJson } from '../ipc/MetaFiles';
import { PackageJsonApp } from '../ipc/schema/packageJson';
import checkAppProperties from './check-app-properties';

interface LegacyAppInfo {
['dist-tags']?: {
latest?: string;
};
versions?: {
[version: string]: {
dist: {
publishTimestamp?: string;
tarball: string;
shasum: string;
};
};
};
}

interface App {
filename: string;
name: string;
Expand Down
14 changes: 8 additions & 6 deletions src/Device/deviceAutoSelectSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@ import { createSlice, PayloadAction } from '@reduxjs/toolkit';
import type { AppThunk, RootState } from '../store';
import type { Device } from './deviceSlice';

export type WaitForDeviceWhen =
| 'always'
| 'applicationMode'
| 'dfuBootLoaderMode'
| 'sameTraits'
| ((device: Device) => boolean);

export interface WaitForDevice {
timeout: number;
when:
| 'always'
| 'applicationMode'
| 'dfuBootLoaderMode'
| 'sameTraits'
| ((device: Device) => boolean);
when: WaitForDeviceWhen;
once: boolean;
skipRefetchDeviceInfo?: boolean;
onSuccess?: (device: Device) => void;
Expand Down
1 change: 1 addition & 0 deletions src/Device/deviceLister.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@ export const startWatchingDevices =
(typeof waitForDevice.when === 'function' &&
waitForDevice.when(device))
) {
dispatch(setArrivedButWrongWhen(undefined));
dispatch(
setLastArrivedDeviceId(
deviceWithPersistedData.id
Expand Down
56 changes: 41 additions & 15 deletions src/Device/sdfuOperations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ import { McuState } from '../../nrfutil/device/setMcuState';
import logger from '../logging';
import { AppThunk, RootState } from '../store';
import { getAppFile } from '../utils/appDirs';
import { clearWaitForDevice, setWaitForDevice } from './deviceAutoSelectSlice';
import {
clearWaitForDevice,
setWaitForDevice,
WaitForDeviceWhen,
} from './deviceAutoSelectSlice';
import { DeviceSetup, DfuEntry } from './deviceSetup';
import { openDeviceSetupDialog } from './deviceSetupSlice';
import { Device } from './deviceSlice';
Expand Down Expand Up @@ -147,16 +151,20 @@ const switchToDeviceMode =
device: Device,
mcuState: McuState,
onSuccess: (device: Device) => void,
onFail: (reason?: unknown) => void
onFail: (reason?: unknown) => void,
autoReconnectWhen?: WaitForDeviceWhen
): AppThunk =>
dispatch => {
if (autoReconnectWhen === undefined) {
autoReconnectWhen =
mcuState === 'Application'
? 'applicationMode'
: 'dfuBootLoaderMode';
}
dispatch(
setWaitForDevice({
timeout: 10000,
when:
mcuState === 'Application'
? 'applicationMode'
: 'dfuBootLoaderMode',
when: autoReconnectWhen,
once: true,
onSuccess,
onFail,
Expand Down Expand Up @@ -199,7 +207,8 @@ export const switchToApplicationMode =
(
device: Device,
onSuccess: (device: Device) => void,
onFail: (reason?: unknown) => void
onFail: (reason?: unknown) => void,
autoReconnectWhen?: WaitForDeviceWhen
): AppThunk =>
dispatch => {
if (isDeviceInDFUBootloader(device)) {
Expand All @@ -216,7 +225,8 @@ export const switchToApplicationMode =
);
else onSuccess(d);
},
onFail
onFail,
autoReconnectWhen
)
);
} else {
Expand Down Expand Up @@ -408,7 +418,8 @@ const programInDFUBootloader =
dfu: DfuEntry,
onProgress: (progress: number, message?: string) => void,
onSuccess: (device: Device) => void,
onFail: (reason?: unknown) => void
onFail: (reason?: unknown) => void,
autoReconnectAfterProgrammingWhen: WaitForDeviceWhen = 'applicationMode'
): AppThunk<RootState, Promise<void>> =>
async dispatch => {
logger.debug(`${device.serialNumber} on is now in DFU-Bootloader...`);
Expand Down Expand Up @@ -494,7 +505,7 @@ const programInDFUBootloader =
dispatch(
setWaitForDevice({
timeout: DEFAULT_DEVICE_WAIT_TIME,
when: 'applicationMode',
when: autoReconnectAfterProgrammingWhen,
once: true,
onSuccess,
onFail,
Expand All @@ -518,7 +529,8 @@ const programDeviceWithFw =
(
device: Device,
selectedFw: DfuEntry,
onProgress: (progress: number, message?: string) => void
onProgress: (progress: number, message?: string) => void,
autoReconnectAfterProgrammingWhen: WaitForDeviceWhen = 'applicationMode'
): AppThunk<RootState, Promise<Device>> =>
dispatch =>
new Promise<Device>((resolve, reject) => {
Expand All @@ -529,7 +541,8 @@ const programDeviceWithFw =
selectedFw,
onProgress,
resolve,
reject
reject,
autoReconnectAfterProgrammingWhen
)
);
logger.debug('DFU finished: ', d);
Expand All @@ -542,7 +555,8 @@ const programDeviceWithFw =

export const sdfuDeviceSetup = (
dfuFirmware: DfuEntry[],
needSerialport = false
needSerialport = false,
autoReconnectAfterProgrammingWhen: WaitForDeviceWhen = 'applicationMode'
): DeviceSetup => ({
supportsProgrammingMode: (device, deviceInfo) =>
((!!deviceInfo?.dfuTriggerVersion &&
Expand All @@ -555,7 +569,12 @@ export const sdfuDeviceSetup = (
description: firmwareOption.description,
programDevice: onProgress => dispatch =>
dispatch(
programDeviceWithFw(device, firmwareOption, onProgress)
programDeviceWithFw(
device,
firmwareOption,
onProgress,
autoReconnectAfterProgrammingWhen
)
),
})),
isExpectedFirmware: (device, deviceInfo) => () => {
Expand All @@ -580,7 +599,14 @@ export const sdfuDeviceSetup = (
},
tryToSwitchToApplicationMode: device => dispatch =>
new Promise<Device>((resolve, reject) => {
dispatch(switchToApplicationMode(device, resolve, reject));
dispatch(
switchToApplicationMode(
device,
resolve,
reject,
autoReconnectAfterProgrammingWhen
)
);
}),
});

Expand Down