Skip to content

Commit

Permalink
Android - Validate firmware and device SKUs match
Browse files Browse the repository at this point in the history
In Android firmware upadte flow, validate new firmware is matching the
connected device's SKU.
  • Loading branch information
Gilaadb committed Jun 7, 2021
1 parent 8a760ae commit e22e753
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
package com.intel.realsenseid.f450androidexample;

import android.bluetooth.BluetoothClass;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Environment;

import com.intel.realsenseid.api.DeviceController;
import com.intel.realsenseid.api.FwUpdater;
import com.intel.realsenseid.api.Status;
import com.intel.realsenseid.api.StringVector;
import com.intel.realsenseid.api.SwigWrapper;
import com.intel.realsenseid.api.StringVector;
import com.intel.realsenseid.impl.UsbCdcConnection;

import org.jetbrains.annotations.NotNull;

import java.util.regex.Pattern;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

public class FilesListActivity extends AppCompatActivity implements FilesListFragment.OnViewItemClickListener {

private FirmwareUpdateLogic firmwareUpdateLogic;
private String deviceSerialNumber;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -32,6 +38,43 @@ protected void onCreate(Bundle savedInstanceState) {
firmwareUpdateLogic = new FirmwareUpdateLogic(this);
}

@Override
protected void onStart() {
super.onStart();
retrieveDeviceSerialNumber();
}

private void retrieveDeviceSerialNumber() {
new Thread(new Runnable() {
public void run() {
UsbCdcConnection connection = new UsbCdcConnection();
if (connection == null) {
throw new RuntimeException("Error opening a USB connection");
}
if (!connection.FindSupportedDevice(getApplicationContext())) {
throw new RuntimeException("Supported USB device not found");
}
if (!connection.OpenConnection()) {
throw new RuntimeException("Couldn't open connection to USB device");
}
com.intel.realsenseid.api.AndroidSerialConfig config = new com.intel.realsenseid.api.AndroidSerialConfig();
config.setFileDescriptor(connection.GetFileDescriptor());
config.setReadEndpoint(connection.GetReadEndpointAddress());
config.setWriteEndpoint(connection.GetWriteEndpointAddress());
DeviceController dc = new DeviceController();
dc.Connect(config);
String sn[] = new String[] {""};
Status s = dc.QuerySerialNumber(sn);
if (s == Status.Ok) {
deviceSerialNumber = sn[0];
} else {
deviceSerialNumber = null;
}
dc.Disconnect();
}
}).start();
}

@Override
public void onClick(@NotNull FileModel fileModel) {
if (fileModel.getFileType() == FileType.FOLDER) {
Expand All @@ -53,6 +96,10 @@ private void handleFileSelection(@NotNull FileModel fileModel) {
showIncompatibleFileDialog(fileModel.getName());
return;
}
if (!fwu.IsEncryptionSupported(fileModel.getPath(), deviceSerialNumber)) {
showIncompatibleSkuDialog(fileModel.getName(), deviceSerialNumber);
return;
}
// Something seems to be wrong wit the version compatibility check. Disabled for now.
if (!SwigWrapper.IsFwCompatibleWithHost(firmwareVersion)) {
showIncompatibleVersionDialog(fileModel.getName(), firmwareVersion);
Expand All @@ -61,6 +108,28 @@ private void handleFileSelection(@NotNull FileModel fileModel) {
showUpdateConfirmationDialog(fileModel, firmwareVersion, recognitionVersion[0]);
}

private void showIncompatibleSkuDialog(String fileName, String deviceSerialNumber) {
String sku = DeviceSerialNumberToSku(deviceSerialNumber);
new AlertDialog.Builder(this)
.setTitle("Incompatible firmware SKU")
.setMessage("Selected firmware version is incompatible with your F450 model.\n" +
"Please make sure the firmware file you're using is for " + sku +
" devices and try again.\n")
.setPositiveButton(android.R.string.ok, null).show();
}

private String DeviceSerialNumberToSku(String deviceSerialNumber)
{
Pattern sku2Option1 = Pattern.compile("12[02]\\d6228\\d{4}.*");
Pattern sku2Option2 = Pattern.compile("\\d{4}6229\\d{4}.*");

if (sku2Option1.matcher(deviceSerialNumber).matches() || sku2Option2.matcher(deviceSerialNumber).matches())
{
return "sku2";
}
return "sku1";
}

private void showIncompatibleVersionDialog(String fileName, String firmwareVersion) {
new AlertDialog.Builder(this)
.setTitle("Incompatible firmware version")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import com.intel.realsenseid.api.AuthenticateStatus;
import com.intel.realsenseid.api.AuthenticationCallback;
import com.intel.realsenseid.api.DeviceConfig;
import com.intel.realsenseid.api.DeviceController;
import com.intel.realsenseid.api.EnrollStatus;
import com.intel.realsenseid.api.EnrollmentCallback;
import com.intel.realsenseid.api.FaceAuthenticator;
Expand Down
1 change: 1 addition & 0 deletions wrappers/android/src/swig.i.in
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ namespace std {
%apply std::string& OUTPUT { std::string& version }
%apply std::string& OUTPUT { std::string& outFwVersion }
%apply std::string& OUTPUT { std::string& outRecognitionVersion }
%apply std::string& OUTPUT { std::string& serial }



Expand Down

0 comments on commit e22e753

Please sign in to comment.