Skip to content

Commit

Permalink
GUI and Messenger related bugfixes
Browse files Browse the repository at this point in the history
  • Loading branch information
raybbian committed Jul 14, 2024
1 parent d3ec823 commit 4951cc5
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 69 deletions.
2 changes: 1 addition & 1 deletion Configurator/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ protected override void OnLaunched(Microsoft.UI.Xaml.LaunchActivatedEventArgs ar

private void AppWindow_Destroying(Microsoft.UI.Windowing.AppWindow sender, object args)
{
m_messenger.Dispose();
m_messenger.Close();
}

private Messenger m_messenger;
Expand Down
1 change: 0 additions & 1 deletion Configurator/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ public MainWindow(Messenger messenger)

Canvas.Loaded += Canvas_Loaded;

m_presenter.IsAlwaysOnTop = true;
m_presenter.IsResizable = false;
m_presenter.IsMaximizable = false;
m_presenter.IsMinimizable = false;
Expand Down
43 changes: 12 additions & 31 deletions Configurator/Messenger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace Configurator
{
public class Messenger : IDisposable
public class Messenger
{

public Messenger(DispatcherQueue dispatch)
Expand All @@ -26,26 +26,26 @@ public Messenger(DispatcherQueue dispatch)

public void SetAddDeviceCallback(DeviceCallbackDelegate addDeviceCallback)
{
MSGSetAddDeviceCallback(m_context, (int device) =>
m_addDeviceCallback = (int device) =>
{
m_dispatch.TryEnqueue(() =>
{
addDeviceCallback(device);
});
});
m_addDeviceCallback = addDeviceCallback;
};
MSGSetAddDeviceCallback(m_context, m_addDeviceCallback);
}

public void SetRemoveDeviceCallback(DeviceCallbackDelegate removeDeviceCallback)
{
MSGSetRemoveDeviceCallback(m_context, (int device) =>
m_removeDeviceCallback = (int device) =>
{
m_dispatch.TryEnqueue(() =>
{
removeDeviceCallback(device);
});
});
m_removeDeviceCallback = removeDeviceCallback;
};
MSGSetRemoveDeviceCallback(m_context, m_removeDeviceCallback);
}

public int GetDevices()
Expand Down Expand Up @@ -107,39 +107,20 @@ public void SetConfiguration(int device, int configuration)
}
}

private IntPtr m_context = IntPtr.Zero;
private DispatcherQueue m_dispatch;
private DeviceCallbackDelegate m_addDeviceCallback;
private DeviceCallbackDelegate m_removeDeviceCallback;

// ================== CLEANUP CODE =========================

protected virtual void Dispose(bool disposing)
public void Close()
{
if (m_disposed) return;
Debug.WriteLine("[IUGUI] Trying to dispose");
if (m_context != IntPtr.Zero)
{
MSGClose(m_context);
m_context = IntPtr.Zero;
Debug.WriteLine("[IUGUI] Closed messenger");
}
m_disposed = true;
}

public void Dispose()
{
Dispose(true);

GC.SuppressFinalize(this);
}

~Messenger()
{
Dispose(false);
}

private bool m_disposed;
private IntPtr m_context = IntPtr.Zero;
private DispatcherQueue m_dispatch;
private DeviceCallbackDelegate m_addDeviceCallback;
private DeviceCallbackDelegate m_removeDeviceCallback;

// =========================== DLL_CONSTANTS ===============
public static readonly int IU_MAX_NUMBER_OF_DEVICES = 8;
Expand Down
3 changes: 2 additions & 1 deletion Configurator/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"commandName": "MsixPackage"
},
"Configurator (Unpackaged)": {
"commandName": "Project"
"commandName": "Project",
"nativeDebugging": true
}
}
}
22 changes: 17 additions & 5 deletions Messenger/Api.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,18 @@

PMESSENGER_CONTEXT WINAPI MSGInit() {
CONFIGRET ret = CR_SUCCESS;
PMESSENGER_CONTEXT MSGContext = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(MESSENGER_CONTEXT));
PMESSENGER_CONTEXT MSGContext = NULL;
HANDLE heap = HeapCreate(0, 0, 0);
if (heap == NULL) {
MSG_DEBUG(L"[IUMSG] Failed to create heap for MSG");
goto Error;
}
MSGContext = HeapAlloc(heap, HEAP_ZERO_MEMORY, sizeof(MESSENGER_CONTEXT));
if (MSGContext == NULL) {
MSG_DEBUG(L"[IUMSG] Failed to allocate memory for MSG context\n");
goto Error;
}
MSGContext->Heap = heap;

ret = RegisterInterfaceNotifications(MSGContext);
if (ret != CR_SUCCESS) {
Expand All @@ -28,13 +35,17 @@ PMESSENGER_CONTEXT WINAPI MSGInit() {
goto Cleanup;
Error:
if (MSGContext) {
if (MSGContext->InterfaceNotification)
if (MSGContext->InterfaceNotification) {
CM_Unregister_Notification(MSGContext->InterfaceNotification);
HeapFree(GetProcessHeap(), 0, MSGContext);
MSGContext->InterfaceNotification = NULL;
}
MSGContext = NULL;
}
if (heap) {
HeapDestroy(heap);
}
MSG_DEBUG(L"[IUMSG] Error initializing msg context")
Cleanup:
Cleanup:
return MSGContext;
}

Expand Down Expand Up @@ -116,9 +127,10 @@ VOID WINAPI MSGClose(IN PMESSENGER_CONTEXT MSGContext) {
}
//cleanup other context attributes
CONFIGRET ret = CM_Unregister_Notification(MSGContext->InterfaceNotification);
MSGContext->InterfaceNotification = NULL;
if (ret != CR_SUCCESS) {
MSG_DEBUG(L"[IUMSG] Could not unregister interface notifications, closing anyway\n");
}
HeapFree(GetProcessHeap(), 0, MSGContext);
HeapDestroy(MSGContext->Heap);
MSG_DEBUG(L"[IUMSG] Closed messenger\n");
}
67 changes: 39 additions & 28 deletions Messenger/Messenger.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,7 @@ CONFIGRET AddDevice(
MSGDeviceContext->DeviceInd = (UCHAR)ind;
MSGDeviceContext->ParentContext = MSGContext;
// fill Symbolic Link
MSGDeviceContext->SymbolicLink = HeapAlloc(GetProcessHeap(), 0, (linkLen + 1) * sizeof(WCHAR));
if (!MSGDeviceContext->SymbolicLink) {
MSG_DEBUG(L"[IUMSG] Could not allocate memory for symbolic link string\n");
RemoveDevice(MSGDeviceContext, FALSE);
return CR_OUT_OF_MEMORY;
}
wcscpy_s(MSGDeviceContext->SymbolicLink, linkLen, SymbolicLink);
MSGDeviceContext->SymbolicLink = SymbolicLink;

//init lock
InitializeCriticalSection(&MSGDeviceContext->Lock);
Expand Down Expand Up @@ -86,7 +80,7 @@ CONFIGRET AddDevice(

MSG_DEBUG(L"[IUMSG] Successfully added device %ws\n", SymbolicLink);
if (CallCallback && MSGContext->DeviceAddCallback != NULL) {
MSG_DEBUG(L"[IUMSG] Calling add device callback");
MSG_DEBUG(L"[IUMSG] Calling add device callback\n");
MSGContext->DeviceAddCallback(MSGDeviceContext->DeviceInd);
}

Expand All @@ -108,21 +102,29 @@ VOID RemoveDevice( //CALLED OUTSIDE OF USB HANDLE CALLBACK
if (MSGDeviceContext->DeviceHandle) {
MSG_DEBUG(L"[IUMSG] Closing device handle");
CloseHandle(MSGDeviceContext->DeviceHandle);
MSGDeviceContext->DeviceHandle = NULL;
}

// unregister for notifs, if was marked by callback to unregister, let it do that
if (MSGDeviceContext->HandleNotifications) {
MSG_DEBUG(L"[IUMSG] Unregistering notifications\n");
BOOL callbackUnregister = FALSE;
MSG_DEBUG(L"[IUMSG] Unregistering notifications from remove device\n");

BOOL callbackUnregister = TRUE;

EnterCriticalSection(&MSGDeviceContext->Lock);
if (MSGDeviceContext->ShouldUnregister)
callbackUnregister = TRUE;
LeaveCriticalSection(&MSGDeviceContext->Lock);

if (!callbackUnregister)
if (!MSGDeviceContext->ShouldUnregister) {
CM_Unregister_Notification(MSGDeviceContext->HandleNotifications);
else //pWork init before handle notifs, can't be null
MSGDeviceContext->HandleNotifications = NULL;
MSGDeviceContext->ShouldUnregister = FALSE;
callbackUnregister = FALSE;
}

LeaveCriticalSection(&MSGDeviceContext->Lock);

if (callbackUnregister) {
WaitForThreadpoolWorkCallbacks(MSGDeviceContext->pWork, FALSE);
}
}

// close work thread pool
Expand All @@ -140,7 +142,7 @@ VOID RemoveDevice( //CALLED OUTSIDE OF USB HANDLE CALLBACK
// free symlink string
if (MSGDeviceContext->SymbolicLink) {
MSG_DEBUG(L"[IUMSG] Freeing device symlink\n");
HeapFree(GetProcessHeap(), 0, MSGDeviceContext->SymbolicLink);
MSGDeviceContext->SymbolicLink = NULL;
}

// reset ShouldRegister
Expand All @@ -161,22 +163,30 @@ DWORD WINAPI USBHandleCallback(
switch (Action) {
case CM_NOTIFY_ACTION_DEVICEQUERYREMOVE:
MSG_DEBUG(L"[IUMSG] Handling device query remove\n");
if (MSGDeviceContext->DeviceHandle)
if (MSGDeviceContext->DeviceHandle) {
MSG_DEBUG(L"[IUMSG] Unregistering notifications from queryremove\n");
CloseHandle(MSGDeviceContext->DeviceHandle);
MSGDeviceContext->DeviceHandle = NULL;
}
break;
case CM_NOTIFY_ACTION_DEVICEQUERYREMOVEFAILED:
//unregister and wait
MSG_DEBUG(L"[IUMSG] Handling device query remove failed\n");
if (MSGDeviceContext->HandleNotifications) {
MSG_DEBUG(L"[IUMSG] Unregistering notifications from queryremovefailed\n");
UnregisterDeviceFromHandleCallbackContext(MSGDeviceContext);
WaitForThreadpoolWorkCallbacks(MSGDeviceContext->pWork, FALSE);
}

// close old handle
if (MSGDeviceContext->DeviceHandle)
if (MSGDeviceContext->DeviceHandle) {
MSG_DEBUG(L"[IUMSG] Closing device handle from queryremovefailed\n");
CloseHandle(MSGDeviceContext->DeviceHandle);
MSGDeviceContext->DeviceHandle = NULL;
}

// Open device handle again
MSG_DEBUG(L"[IUMSG] Opening device handle from queryremovefailed\n");
MSGDeviceContext->DeviceHandle = CreateFile(
MSGDeviceContext->SymbolicLink,
GENERIC_READ,
Expand All @@ -192,6 +202,7 @@ DWORD WINAPI USBHandleCallback(
}

// re-register notifications
MSG_DEBUG(L"[IUMSG] Registering notifications from queryremovefailed\n");
CM_NOTIFY_FILTER filter = { 0 };
filter.cbSize = sizeof(filter);
filter.FilterType = CM_NOTIFY_FILTER_TYPE_DEVICEHANDLE;
Expand All @@ -205,15 +216,19 @@ DWORD WINAPI USBHandleCallback(
if (ret != CR_SUCCESS) {
MSG_DEBUG(L"[IUMSG] Failed to reregister for device handle notifications\n");
CloseHandle(MSGDeviceContext->DeviceHandle);
MSGDeviceContext->DeviceHandle = NULL;
goto Cleanup;
}
break;
case CM_NOTIFY_ACTION_DEVICEREMOVEPENDING:
case CM_NOTIFY_ACTION_DEVICEREMOVECOMPLETE:
MSG_DEBUG(L"[IUMSG] Handling device remove action\n");
UnregisterDeviceFromHandleCallbackContext(MSGDeviceContext);
if (MSGDeviceContext->DeviceHandle)
if (MSGDeviceContext->DeviceHandle) {
MSG_DEBUG(L"[IUMSG] Closing device handle from device remove notification\n");
CloseHandle(MSGDeviceContext->DeviceHandle);
MSGDeviceContext->DeviceHandle = NULL;
}
break;
}
Cleanup:
Expand Down Expand Up @@ -280,14 +295,13 @@ CONFIGRET RetrieveExistingDevices(
}
MSG_DEBUG(L"[IUMSG] Interface list size is %d\n", reqLen);

PWCHAR buf = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (reqLen + 1));
if (!buf) {
PWCHAR buf = HeapAlloc(MSGContext->Heap, HEAP_ZERO_MEMORY, reqLen);
if (buf == NULL) {
MSG_DEBUG(L"[IUMSG] Failed to allocate memory for interface list\n");
ret = CR_OUT_OF_MEMORY;
goto Cleanup;
return CR_OUT_OF_MEMORY;
}

ret = CM_Get_Device_Interface_List(
ret = CM_Get_Device_Interface_ListW(
&GUID_DEVINTERFACE_Keystone,
NULL,
buf,
Expand All @@ -296,7 +310,7 @@ CONFIGRET RetrieveExistingDevices(
);
if (ret != CR_SUCCESS) {
MSG_DEBUG(L"[IUMSG] Failed to get full interface list\n");
goto Cleanup;
return ret;
}

PWCHAR curChar = buf;
Expand All @@ -312,9 +326,6 @@ CONFIGRET RetrieveExistingDevices(
reqLen -= strLen + 1;
}

Cleanup:
if (buf)
HeapFree(GetProcessHeap(), 0, buf);
return ret;
}

Expand Down
2 changes: 0 additions & 2 deletions Messenger/Messenger.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@
<ConformanceMode>true</ConformanceMode>
<PrecompiledHeader>Use</PrecompiledHeader>
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
<RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
Expand All @@ -134,7 +133,6 @@
<ConformanceMode>true</ConformanceMode>
<PrecompiledHeader>Use</PrecompiledHeader>
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
<RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
Expand Down
1 change: 1 addition & 0 deletions Messenger/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ typedef struct _MESSENGER_DEVICE_CONTEXT {
} MESSENGER_DEVICE_CONTEXT, *PMESSENGER_DEVICE_CONTEXT;

typedef struct _MESSENGER_CONTEXT {
HANDLE Heap;
HCMNOTIFICATION InterfaceNotification;
VOID(*DeviceAddCallback)(LONG);
VOID(*DeviceRemoveCallback)(LONG);
Expand Down

0 comments on commit 4951cc5

Please sign in to comment.