Skip to content

Commit 013c2d4

Browse files
AsCressmarcnause
andauthored
feat: added firmware checks and warning (#2856)
Co-authored-by: Marc Nause <[email protected]>
1 parent 95dcd83 commit 013c2d4

File tree

7 files changed

+92
-8
lines changed

7 files changed

+92
-8
lines changed

lib/communication/commands_proto.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ class CommandsProto {
132132
int getFrequency = 3;
133133
int getInductance = 4;
134134
int getVersion = 5;
135+
int getFwVersion = 6;
135136
int retrieveBuffer = 8;
136137
int getHighFrequency = 9;
137138
int clearBuffer = 10;

lib/communication/packet_handler.dart

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class PacketHandler {
1515
late SocketClient _socketClient;
1616
static String version = '';
1717
late CommandsProto _mCommandsProto;
18-
int _timeout = 500, versionStringLength = 8;
18+
int _timeout = 500, versionStringLength = 8, fwVersionLength = 3;
1919

2020
PacketHandler(int timeout, CommunicationHandler communicationHandler) {
2121
_connected = false;
@@ -134,6 +134,22 @@ class PacketHandler {
134134
return -1;
135135
}
136136

137+
Future<int> getFirmwareVersion() async {
138+
try {
139+
sendByte(_mCommandsProto.common);
140+
sendByte(_mCommandsProto.getFwVersion);
141+
int numBytesRead = await _commonRead(fwVersionLength);
142+
if (numBytesRead == 1) {
143+
return 2;
144+
} else {
145+
return _buffer[0];
146+
}
147+
} catch (e) {
148+
logger.e(e);
149+
}
150+
return 0;
151+
}
152+
137153
Future<int> read(Uint8List dest, int bytesToRead) async {
138154
int numBytesRead = await _commonRead(bytesToRead);
139155
for (int i = 0; i < bytesToRead; i++) {

lib/l10n/app_en.arb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -480,5 +480,7 @@
480480
"altitudeUnitLabel" : "m",
481481
"time" : "Time",
482482
"notAvailable" : "N/A",
483-
"estimated" : "Estimated"
483+
"estimated" : "Estimated",
484+
"legacyFirmwareAlertTitle": "Legacy Firmware Detected",
485+
"legacyFirmwareAlertMessage": "We have detected that your PSLab device is running legacy firmware. Please note that support for this firmware has ended. For the best experience and continued support, please update your device to the latest firmware version."
484486
}

lib/l10n/app_localizations.dart

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2789,6 +2789,18 @@ abstract class AppLocalizations {
27892789
/// In en, this message translates to:
27902790
/// **'Estimated'**
27912791
String get estimated;
2792+
2793+
/// No description provided for @legacyFirmwareAlertTitle.
2794+
///
2795+
/// In en, this message translates to:
2796+
/// **'Legacy Firmware Detected'**
2797+
String get legacyFirmwareAlertTitle;
2798+
2799+
/// No description provided for @legacyFirmwareAlertMessage.
2800+
///
2801+
/// In en, this message translates to:
2802+
/// **'We have detected that your PSLab device is running legacy firmware. Please note that support for this firmware has ended. For the best experience and continued support, please update your device to the latest firmware version.'**
2803+
String get legacyFirmwareAlertMessage;
27922804
}
27932805

27942806
class _AppLocalizationsDelegate

lib/l10n/app_localizations_en.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1444,4 +1444,11 @@ class AppLocalizationsEn extends AppLocalizations {
14441444

14451445
@override
14461446
String get estimated => 'Estimated';
1447+
1448+
@override
1449+
String get legacyFirmwareAlertTitle => 'Legacy Firmware Detected';
1450+
1451+
@override
1452+
String get legacyFirmwareAlertMessage =>
1453+
'We have detected that your PSLab device is running legacy firmware. Please note that support for this firmware has ended. For the best experience and continued support, please update your device to the latest firmware version.';
14471454
}

lib/providers/board_state_provider.dart

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,13 @@ class BoardStateProvider extends ChangeNotifier {
2020
String pslabVersionIDV6 = 'PSLab V6';
2121
String pslabVersionIDV5 = 'PSLab V5';
2222
int pslabVersion = 0;
23+
int pslabFirmwareVersion = 0;
2324
late String exportFormat;
2425
bool autoStart = true;
2526
bool _isProcessing = false;
2627

28+
final ValueNotifier<String?> legacyFirmwareNotifier = ValueNotifier(null);
29+
2730
BoardStateProvider() {
2831
scienceLabCommon = getIt.get<ScienceLabCommon>();
2932
exportFormat = appLocalizations.txtFormat;
@@ -32,9 +35,12 @@ class BoardStateProvider extends ChangeNotifier {
3235
Future<void> initialize() async {
3336
if (_isProcessing) return;
3437
_isProcessing = true;
35-
await scienceLabCommon.initialize();
36-
pslabIsConnected = await scienceLabCommon.openDevice();
37-
setPSLabVersionIDs();
38+
if (!scienceLabCommon.isConnected()) {
39+
await scienceLabCommon.initialize();
40+
pslabIsConnected = await scienceLabCommon.openDevice();
41+
await setPSLabVersionIDs();
42+
await fetchFirmwareVersion();
43+
}
3844
_isProcessing = false;
3945
if (autoStart) {
4046
if (Platform.isAndroid) {
@@ -43,9 +49,11 @@ class BoardStateProvider extends ChangeNotifier {
4349
if (usbEvent.event == UsbEvent.ACTION_USB_ATTACHED) {
4450
if (_isProcessing) return;
4551
_isProcessing = true;
46-
if (await attemptToConnectPSLab()) {
52+
if (!scienceLabCommon.isConnected() &&
53+
await attemptToConnectPSLab()) {
4754
pslabIsConnected = await scienceLabCommon.openDevice();
48-
setPSLabVersionIDs();
55+
await setPSLabVersionIDs();
56+
await fetchFirmwareVersion();
4957
_isProcessing = false;
5058
}
5159
} else if (usbEvent.event == UsbEvent.ACTION_USB_DETACHED &&
@@ -74,7 +82,8 @@ class BoardStateProvider extends ChangeNotifier {
7482
Future<void> initializeWiFi() async {
7583
if (!pslabIsConnected) {
7684
pslabIsConnected = await scienceLabCommon.openWiFiDevice();
77-
setPSLabVersionIDs();
85+
await setPSLabVersionIDs();
86+
await fetchFirmwareVersion();
7887
}
7988
}
8089

@@ -88,6 +97,17 @@ class BoardStateProvider extends ChangeNotifier {
8897
notifyListeners();
8998
}
9099

100+
Future<void> fetchFirmwareVersion() async {
101+
if (getIt.get<ScienceLab>().isConnected()) {
102+
pslabFirmwareVersion =
103+
await getIt.get<ScienceLab>().mPacketHandler.getFirmwareVersion();
104+
}
105+
if (pslabFirmwareVersion < 3 && pslabFirmwareVersion != 0) {
106+
legacyFirmwareNotifier.value = "LegacyFirmwareDetected";
107+
}
108+
notifyListeners();
109+
}
110+
91111
Future<bool> attemptToConnectPSLab() async {
92112
if (scienceLabCommon.isConnected()) {
93113
logger.d("Device Connected Successfully");

lib/view/instruments_screen.dart

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import 'package:flutter/material.dart';
22
import 'package:flutter/services.dart';
33
import 'package:pslab/constants.dart';
44
import 'package:pslab/l10n/app_localizations.dart';
5+
import 'package:pslab/providers/board_state_provider.dart';
56
import 'package:pslab/providers/locator.dart';
67
import 'package:pslab/view/widgets/applications_list_item.dart';
78
import 'package:pslab/view/widgets/main_scaffold_widget.dart';
@@ -210,6 +211,31 @@ class _InstrumentsScreenState extends State<InstrumentsScreen> {
210211
@override
211212
void initState() {
212213
super.initState();
214+
getIt.get<BoardStateProvider>().legacyFirmwareNotifier.addListener(() {
215+
if (getIt.get<BoardStateProvider>().legacyFirmwareNotifier.value ==
216+
"LegacyFirmwareDetected") {
217+
WidgetsBinding.instance.addPostFrameCallback((_) {
218+
showDialog<void>(
219+
context: context,
220+
builder: (BuildContext context) {
221+
return AlertDialog(
222+
icon: const Icon(Icons.warning),
223+
title: Text(appLocalizations.legacyFirmwareAlertTitle),
224+
content: Text(appLocalizations.legacyFirmwareAlertMessage),
225+
actions: <Widget>[
226+
TextButton(
227+
onPressed: () {
228+
Navigator.of(context).pop();
229+
},
230+
child: Text(appLocalizations.ok),
231+
),
232+
],
233+
);
234+
},
235+
);
236+
});
237+
}
238+
});
213239
instrumentHeadings = [
214240
appLocalizations.oscilloscope.toUpperCase(),
215241
appLocalizations.multimeter.toUpperCase(),

0 commit comments

Comments
 (0)