-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathModemUpdateDialogView.tsx
More file actions
307 lines (284 loc) · 10.9 KB
/
ModemUpdateDialogView.tsx
File metadata and controls
307 lines (284 loc) · 10.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/*
* Copyright (c) 2015 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: LicenseRef-Nordic-4-Clause
*/
import React, { useCallback, useEffect, useRef, useState } from 'react';
import ProgressBar from 'react-bootstrap/ProgressBar';
import { useDispatch, useSelector } from 'react-redux';
import {
addConfirmBeforeClose,
Alert,
clearConfirmBeforeClose,
DialogButton,
GenericDialog,
logger,
selectedDevice,
selectedDeviceInfo,
useStopwatch,
} from '@nordicsemiconductor/pc-nrfconnect-shared';
import { Progress } from '@nordicsemiconductor/pc-nrfconnect-shared/nrfutil';
import { performUpdate } from '../actions/modemTargetActions';
import { getDeviceDefinition } from '../reducers/deviceDefinitionReducer';
import { getZipFilePath } from '../reducers/fileReducer';
import {
getShowModemProgrammingDialog,
setShowModemProgrammingDialog,
} from '../reducers/modemReducer';
import { WithRequired } from '../util/types';
export const isValidNrf9160FirmwareName = (filename: string | undefined) =>
!filename || /mfw_nrf9160_\d+\.\d+\.\d+.*.zip/.test(filename);
export const isValidNrf91x1FirmwareName = (filename: string | undefined) =>
!filename ||
/mfw_nrf91x1_\d+\.\d+\.\d+.*.zip/.test(filename) ||
/mfw.*nrf91.1(-\w+)?_\d+\.\d+\.\d+.*.zip/.test(filename);
const ModemUpdateDialogView = () => {
const abortController = useRef(new AbortController());
const [progress, setProgress] =
useState<WithRequired<Progress, 'message'>>();
const [writing, setWriting] = useState(false);
const [writingFail, setWritingFail] = useState(false);
const [writingSucceed, setWritingSucceed] = useState(false);
const [writingFailError, setWritingFailError] = useState<string>();
const device = useSelector(selectedDevice);
const deviceInfo = useSelector(selectedDeviceInfo);
const deviceDefinition = useSelector(getDeviceDefinition);
const modemFwName = useSelector(getZipFilePath);
const isVisible = useSelector(getShowModemProgrammingDialog);
const isMcuboot = !!device?.traits.mcuBoot && !device?.traits.jlink;
const is9160 =
deviceDefinition.type?.toLocaleUpperCase().includes('NRF9160') ||
deviceInfo?.jlink?.deviceVersion
?.toLocaleUpperCase()
.includes('NRF9160');
const is91x1 =
deviceDefinition.type?.toLocaleUpperCase().match(/NRF91\d1/) ||
deviceInfo?.jlink?.deviceVersion?.toLocaleUpperCase().match(/NRF91\d1/);
const deviceTypeKnown = is9160 || is91x1;
let expectedFwName = false;
let expectedFileName = '';
let url = '';
if (is9160) {
expectedFileName = 'mfw_nrf9160_X.X.X*.zip';
expectedFwName = isValidNrf9160FirmwareName(modemFwName);
url =
'https://www.nordicsemi.com/Products/Development-hardware/nrf9160-dk/download#infotabs';
} else if (is91x1) {
expectedFileName = 'mfw_nrf91?1*_X.X.X*.zip';
expectedFwName = isValidNrf91x1FirmwareName(modemFwName);
url = 'https://www.nordicsemi.com/Products/nRF9161/Download';
}
useEffect(() => {
if (isVisible) {
setProgress(undefined);
setWriting(false);
setWritingSucceed(false);
setWritingFail(false);
setWritingFailError(undefined);
} else {
abortController.current.abort();
}
}, [isVisible]);
const dispatch = useDispatch();
const onCancel = useCallback(() => {
if (!writing) {
dispatch(setShowModemProgrammingDialog(false));
}
}, [dispatch, writing]);
const { time, start, pause, reset } = useStopwatch({
autoStart: false,
});
const onWriteStart = () => {
if (!device) {
logger.error('No target device!');
return;
}
if (!modemFwName) {
logger.error('No file selected');
return;
}
reset();
start();
abortController.current = new AbortController();
setWriting(true);
dispatch(
addConfirmBeforeClose({
id: 'modemProgramming',
message: `The device is being programmed.
Closing application right now might result in some unknown behavior and might also brick the device.
Are you sure you want to continue?`,
onClose: () => abortController.current.abort(),
}),
);
setProgress(progress);
performUpdate(
device,
modemFwName,
programmingProgress => {
let updatedProgress: WithRequired<Progress, 'message'> = {
...programmingProgress,
message: programmingProgress.message ?? '',
};
if (programmingProgress.operation === 'erase_image') {
updatedProgress = {
...programmingProgress,
message: `${programmingProgress.message} This will take some time.`,
};
}
if (
!programmingProgress.result &&
programmingProgress.operation === 'upload_image'
) {
updatedProgress = {
...programmingProgress,
message: `Uploading image. This will take some time.`,
};
}
setProgress(updatedProgress);
},
abortController.current,
)
.then(() => setWritingSucceed(true))
.catch(error => {
if (!abortController.current.signal.aborted) {
setWritingFailError(error.message);
setWritingFail(true);
}
})
.finally(() => {
setWriting(false);
dispatch(clearConfirmBeforeClose('modemProgramming'));
});
};
useEffect(() => {
if (writingSucceed || writingFail) {
pause();
}
}, [writingFail, writingSucceed, pause]);
return (
<GenericDialog
title={`Modem DFU ${isMcuboot ? ' via MCUboot' : ''}`}
showSpinner={writing}
onHide={onCancel}
closeOnEsc
closeOnUnfocus
footer={
<>
<DialogButton
variant="primary"
onClick={onWriteStart}
disabled={
writing ||
writingSucceed ||
writingFail ||
!modemFwName
}
>
Write
</DialogButton>
<DialogButton
variant="secondary"
onClick={onCancel}
disabled={writing}
>
Close
</DialogButton>
</>
}
isVisible={isVisible}
>
<div className="tw-flex tw-flex-col tw-gap-4">
<div className="tw-flex tw-flex-col tw-gap-2">
<div>
<b>Modem firmware</b>
</div>
<div>{modemFwName}</div>
</div>
{!writing &&
!writingSucceed &&
!writingFail &&
!expectedFwName &&
deviceTypeKnown && (
<Alert
label="Unexpected file name detected"
variant="warning"
>
<br />
Nordic official modem firmware files are named{' '}
{expectedFileName}.
<br />
Modem firmware files can be downloaded from{' '}
{url && (
<a
target="_blank"
rel="noopener noreferrer"
href={url}
>
www.nordicsemi.com
</a>
)}
.
</Alert>
)}
{!writing &&
!writingSucceed &&
!writingFail &&
!deviceTypeKnown && (
<Alert label="Modem firmware" variant="warning">
<br />
Unable to detect the device family. Make sure that
the modem firmware file is intended for the
connected device family.
</Alert>
)}
{writing && (
<div className="tw-flex tw-flex-col tw-gap-2">
<div>
<strong>Status: </strong>
<span>{`${
progress ? progress.message : 'Starting...'
}`}</span>
</div>
{progress && (
<ProgressBar
hidden={!writing}
now={progress.stepProgressPercentage}
style={{ height: '4px' }}
/>
)}
</div>
)}
{isMcuboot && !writing && !writingSucceed && !writingFail && (
<Alert variant="warning">
<p className="tw-mb-0">
You are now performing modem DFU via MCUboot.
</p>
<p className="tw-mb-0">
The device will be overwritten if you proceed to
write.
</p>
<p className="tw-mb-0">
Make sure the device is in{' '}
<strong>MCUboot mode</strong>.
</p>
</Alert>
)}
{writingSucceed && !writingFail && (
<Alert variant="success">
Completed successfully in
{` ${Math.round(time / 1000)} `}
seconds.
</Alert>
)}
{writingFail && !writing && (
<Alert variant="danger">
{writingFailError?.trim() ||
'Failed. Check the log below for more details...'}
</Alert>
)}
</div>
</GenericDialog>
);
};
ModemUpdateDialogView.defaultProps = {};
export default ModemUpdateDialogView;