Skip to content

Commit

Permalink
Fixed exception at shutdown if the controllers are closed after the H…
Browse files Browse the repository at this point in the history
…IDDeviceManager is shutdown
  • Loading branch information
slouken committed Mar 13, 2020
1 parent 80d075a commit cb986af
Showing 1 changed file with 41 additions and 11 deletions.
52 changes: 41 additions & 11 deletions src/hidapi/android/hid.cpp
Expand Up @@ -436,6 +436,12 @@ class CHIDDevice
g_JVM->AttachCurrentThread( &env, NULL );
pthread_setspecific( g_ThreadKey, (void*)env );

if ( !g_HIDDeviceManagerCallbackHandler )
{
LOGV( "Device open without callback handler" );
return false;
}

m_bIsWaitingForOpen = false;
m_bOpenResult = env->CallBooleanMethod( g_HIDDeviceManagerCallbackHandler, g_midHIDDeviceManagerOpen, m_nId );
ExceptionCheck( env, "BOpen" );
Expand Down Expand Up @@ -545,11 +551,18 @@ class CHIDDevice
g_JVM->AttachCurrentThread( &env, NULL );
pthread_setspecific( g_ThreadKey, (void*)env );

jbyteArray pBuf = NewByteArray( env, pData, nDataLen );
int nRet = env->CallIntMethod( g_HIDDeviceManagerCallbackHandler, g_midHIDDeviceManagerSendOutputReport, m_nId, pBuf );
ExceptionCheck( env, "SendOutputReport" );

env->DeleteLocalRef( pBuf );
int nRet = -1;
if ( g_HIDDeviceManagerCallbackHandler )
{
jbyteArray pBuf = NewByteArray( env, pData, nDataLen );
nRet = env->CallIntMethod( g_HIDDeviceManagerCallbackHandler, g_midHIDDeviceManagerSendOutputReport, m_nId, pBuf );
ExceptionCheck( env, "SendOutputReport" );
env->DeleteLocalRef( pBuf );
}
else
{
LOGV( "SendOutputReport without callback handler" );
}
return nRet;
}

Expand All @@ -560,10 +573,18 @@ class CHIDDevice
g_JVM->AttachCurrentThread( &env, NULL );
pthread_setspecific( g_ThreadKey, (void*)env );

jbyteArray pBuf = NewByteArray( env, pData, nDataLen );
int nRet = env->CallIntMethod( g_HIDDeviceManagerCallbackHandler, g_midHIDDeviceManagerSendFeatureReport, m_nId, pBuf );
ExceptionCheck( env, "SendFeatureReport" );
env->DeleteLocalRef( pBuf );
int nRet = -1;
if ( g_HIDDeviceManagerCallbackHandler )
{
jbyteArray pBuf = NewByteArray( env, pData, nDataLen );
nRet = env->CallIntMethod( g_HIDDeviceManagerCallbackHandler, g_midHIDDeviceManagerSendFeatureReport, m_nId, pBuf );
ExceptionCheck( env, "SendFeatureReport" );
env->DeleteLocalRef( pBuf );
}
else
{
LOGV( "SendFeatureReport without callback handler" );
}
return nRet;
}

Expand All @@ -587,6 +608,12 @@ class CHIDDevice
g_JVM->AttachCurrentThread( &env, NULL );
pthread_setspecific( g_ThreadKey, (void*)env );

if ( !g_HIDDeviceManagerCallbackHandler )
{
LOGV( "GetFeatureReport without callback handler" );
return -1;
}

{
hid_mutex_guard cvl( &m_cvLock );
if ( m_bIsWaitingForFeatureReport )
Expand Down Expand Up @@ -657,8 +684,11 @@ class CHIDDevice
g_JVM->AttachCurrentThread( &env, NULL );
pthread_setspecific( g_ThreadKey, (void*)env );

env->CallVoidMethod( g_HIDDeviceManagerCallbackHandler, g_midHIDDeviceManagerClose, m_nId );
ExceptionCheck( env, "Close" );
if ( g_HIDDeviceManagerCallbackHandler )
{
env->CallVoidMethod( g_HIDDeviceManagerCallbackHandler, g_midHIDDeviceManagerClose, m_nId );
ExceptionCheck( env, "Close" );
}

hid_mutex_guard dataLock( &m_dataLock );
m_vecData.clear();
Expand Down

0 comments on commit cb986af

Please sign in to comment.