Skip to content

Commit

Permalink
#12 - Process device connection permission result asynchronously
Browse files Browse the repository at this point in the history
use an android broadcast receiver & events to handle permission result.
  • Loading branch information
LIBERADO committed Dec 29, 2020
1 parent 93f3f81 commit 0742fcd
Show file tree
Hide file tree
Showing 6 changed files with 209 additions and 96 deletions.
2 changes: 1 addition & 1 deletion MobileApplication/IHM.sln
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ VisualStudioVersion = 15.0.28307.1082
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IHM.Android", "IHM\IHM.Android\IHM.Android.csproj", "{BE5A8A0A-5A57-486F-82C2-E51124B94857}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IHM", "IHM\IHM\IHM.csproj", "{BD2E3759-DFF1-4368-B8ED-6F78F9692D16}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "IHM", "IHM\IHM\IHM.csproj", "{BD2E3759-DFF1-4368-B8ED-6F78F9692D16}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down
84 changes: 64 additions & 20 deletions MobileApplication/IHM/IHM.Android/Interfaces/DroidPandaVcom.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ public class USBBroadCastReceiver : BroadcastReceiver
/// </summary>
public static String ACTION_USB_PERMISSION = "com.cio.USB_PERMISSION";

/// <summary>
/// Use an event to notify that the permission completed
/// </summary>
public event EventHandler<bool> NotifyUsbPermissionCompleted;

/// <summary>
/// We complete the opening device process in the brodcast receive
/// FIXME - don't do that, just send event and let the task that triggered the permission request complete its process
/// </summary>
private DroidPandaVcom _droidPandaVcom;

public USBBroadCastReceiver(DroidPandaVcom droidPandaVcom)
Expand All @@ -44,20 +53,14 @@ public override void OnReceive(Context context, Intent intent)
if (ACTION_USB_PERMISSION.Equals(action))
{
UsbDevice device = (UsbDevice)intent.GetParcelableExtra(UsbManager.ExtraDevice);

if (intent.GetBooleanExtra(UsbManager.ExtraPermissionGranted, false))
{
if (device != null)
{
_droidPandaVcom.OpenDevice();
}
}
else
bool bPermissionGranted = false;
if (device != null)
{
Log.Debug(DroidPandaVcom.LOG_TAG, "permission denied for device " + device);
bPermissionGranted = intent.GetBooleanExtra(UsbManager.ExtraPermissionGranted, false);
}
EventHandler<bool> handler = NotifyUsbPermissionCompleted;
handler(this, bPermissionGranted);
}

}
}

Expand Down Expand Up @@ -92,14 +95,20 @@ public DroidDevHandle()
// everything else is set to null
}
}

/// <summary>
/// The Event declared in the interface
/// </summary>
public event EventHandler<bool> NotifyPermRequestCompleted;

////////////////////////////
// CONFIG SECTION
private const bool bConfUseUsbManagerOnly = true; // Select method to install interface (usbserial package, or android API only)
public const int BULK_XFER_TOUT_MS = 1000;
// END
public const int iVendorId = 0x0483; // Id for the STM32Nucleo
public const int iProductID = 0x5740; // Id for the STM32Nucleo
// END


// usb-to-serial stuff
private IList<IUsbSerialDriver> _availableDrivers; // Manage all available driver from package
Expand All @@ -118,11 +127,26 @@ public void Init(Object context)
{
_context = (ContextWrapper)context;
_devHandle.usbManager = (UsbManager)_context.GetSystemService(Context.UsbService);

_usbPermissionIntent = PendingIntent.GetBroadcast(_context, 0, new Intent(USBBroadCastReceiver.ACTION_USB_PERMISSION), 0);
IntentFilter filter = new IntentFilter(USBBroadCastReceiver.ACTION_USB_PERMISSION);
_context.RegisterReceiver(_USBBroadCastReceiver, filter);
_USBBroadCastReceiver = new USBBroadCastReceiver(this);
_USBBroadCastReceiver.NotifyUsbPermissionCompleted += _USBBroadCastReceiver_NotifyUsbPermissionCompleted;
_context.RegisterReceiver(_USBBroadCastReceiver, filter);
}

private void _USBBroadCastReceiver_NotifyUsbPermissionCompleted(object sender, bool bPermissionGranted)
{
if (bPermissionGranted)
{
OpenDevice();
// https://docs.microsoft.com/fr-fr/dotnet/api/system.eventhandler-1?view=net-5.0
EventHandler<bool> handler = NotifyPermRequestCompleted;
handler(this, true);
}
else
{
Log.Debug(LOG_TAG, "permission denied for device " + _devHandle.usbdev.DeviceName);
}
}

// Retreive the physically connected devices.
Expand Down Expand Up @@ -243,20 +267,36 @@ protected int OpenInterface(UsbDevice usbdev, UsbDeviceConnection connection, re
return ret;
}

public bool CheckPermStatusAsync(string szDevName)
{
var usbDevice = _devHandle.usbManager.DeviceList[szDevName];
// Ask for permission to access the created device
return _devHandle.usbManager.HasPermission(usbDevice);
}

public void RequestPermAsync(string szDevName)
{
var usbDevice = _devHandle.usbManager.DeviceList[szDevName];
_devHandle.usbManager.RequestPermission(usbDevice, _usbPermissionIntent);
}

public void selectDevice(string name)
{
if (bConfUseUsbManagerOnly)
{

_devHandle.usbdev = _devHandle.usbManager.DeviceList[name];
// Ask for permission to access the created device
if (!_devHandle.usbManager.HasPermission(_devHandle.usbdev))
{
_devHandle.usbManager.RequestPermission(_devHandle.usbdev, _usbPermissionIntent);
// Check and Ask for permission to access the created device
if (_devHandle.usbManager.HasPermission(_devHandle.usbdev))
{
OpenDevice();
// https://docs.microsoft.com/fr-fr/dotnet/api/system.eventhandler-1?view=net-5.0
EventHandler<bool> handler = NotifyPermRequestCompleted;
handler(this, true);
}
else
{
_devHandle.usbManager.RequestPermission(_devHandle.usbdev, _usbPermissionIntent);
// The wapp don't wait for user input and crash when asking permission 1st time
// We must Wait for user input for opening connection.
}
Expand Down Expand Up @@ -295,7 +335,7 @@ public void selectDevice(string name)
public void OpenDevice()
{
// At this stage Persmission must Have been granted
if (!_devHandle.usbManager.HasPermission(_devHandle.usbdev))
if (_devHandle.usbManager.HasPermission(_devHandle.usbdev))
{
// Now open the port, with the USB Manager : we get the fd/enpoints and pass it to library, no more
_devHandle.connection = _devHandle.usbManager.OpenDevice(_devHandle.usbdev);
Expand All @@ -311,7 +351,11 @@ public void OpenDevice()
Log.Debug(DroidPandaVcom.LOG_TAG, "FAILED : open device endpoint" + _devHandle.usbdev.DeviceName);
}
}
}
}
else
{
throw new NotSupportedException("We shall have permission to device");
}
}

/// <summary>
Expand Down
22 changes: 17 additions & 5 deletions MobileApplication/IHM/IHM.Android/Interfaces/DroidUsbManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public class DroidUsbManager : IUsbManager
{
private UsbManager usbManager_;
private string selectedDevice;
public event EventHandler<bool> NotifyPermRequestCompleted;

public DroidUsbManager() {}

Expand Down Expand Up @@ -77,24 +78,35 @@ public int Close()
public int ReadRegisterFromDevice(byte uiRegister, ref byte value)
{
// not implemented
return -1;
throw new NotImplementedException();
}

public int WriteRegisterToDevice(byte uiRegister, byte value)
{
// not implemented
return -1;
throw new NotImplementedException();
}

public int WriteToDevice(byte[] data)
{
// not implemented
return -1;
throw new NotImplementedException();
}
public int ReadFromDevice(byte[] data, int len)
{
// not implemented
return -1;
throw new NotImplementedException();
}
}

public bool CheckPermStatusAsync(string szDevName)
{
throw new NotImplementedException();
}

public void RequestPermAsync(string szDevName)
{
throw new NotImplementedException();
}

}
}
4 changes: 1 addition & 3 deletions MobileApplication/IHM/IHM.Android/MainActivity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@ public class MainActivity : Xamarin.Forms.Platform.Android.FormsAppCompatActivit
{
protected override void OnCreate(Bundle savedInstanceState)
{
//Pour l'utilisation du service de l'USBManager
// Setup the USB Android Interface we want to use.
Xamarin.Forms.DependencyService.Register<DroidPandaVcom>();

TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;

Expand All @@ -39,7 +38,6 @@ protected override void OnCreate(Bundle savedInstanceState)
Xamarin.Forms.Forms.Init(this, savedInstanceState);
Xamarin.Forms.DependencyService.Get<IUsbManager>().Init(this);
LoadApplication(new App());

}
}
}
Loading

0 comments on commit 0742fcd

Please sign in to comment.