Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add exception handling to Android hidapi.
  • Loading branch information
slouken committed Oct 22, 2018
1 parent 4a50a04 commit c4918db
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 1 deletion.
Empty file modified Xcode-iOS/Demos/Demos.xcodeproj/project.pbxproj 100644 → 100755
Empty file.
Empty file modified Xcode-iOS/SDL/SDL.xcodeproj/project.pbxproj 100644 → 100755
Empty file.
Empty file modified Xcode-iOS/Test/TestiPhoneOS.xcodeproj/project.pbxproj 100644 → 100755
Empty file.
Empty file modified Xcode/SDL/SDL.xcodeproj/project.pbxproj 100644 → 100755
Empty file.
Empty file modified Xcode/SDLTest/SDLTest.xcodeproj/project.pbxproj 100644 → 100755
Empty file.
39 changes: 38 additions & 1 deletion src/hidapi/android/hid.cpp
Expand Up @@ -14,6 +14,10 @@
#include <string.h> // For memcpy()

#define TAG "hidapi"

// Have error log always available
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, TAG, __VA_ARGS__)

#ifdef DEBUG
#define LOGV(...) __android_log_print(ANDROID_LOG_VERBOSE, TAG, __VA_ARGS__)
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, TAG, __VA_ARGS__)
Expand Down Expand Up @@ -398,6 +402,33 @@ class CHIDDevice
return m_pDevice;
}

void ExceptionCheck( JNIEnv *env, const char *pszMethodName )
{
if ( env->ExceptionCheck() )
{
// Get our exception
jthrowable jExcept = env->ExceptionOccurred();

// Clear the exception so we can call JNI again
env->ExceptionClear();

// Get our exception message
jclass jExceptClass = env->GetObjectClass( jExcept );
jmethodID jMessageMethod = env->GetMethodID( jExceptClass, "getMessage", "()Ljava/lang/String;" );
jstring jMessage = (jstring)( env->CallObjectMethod( jExcept, jMessageMethod ) );
const char *pszMessage = env->GetStringUTFChars( jMessage, NULL );

// ...and log it.
LOGE( "CHIDDevice::%s threw an exception: %s", pszMethodName, pszMessage );

// Cleanup
env->ReleaseStringUTFChars( jMessage, pszMessage );
env->DeleteLocalRef( jMessage );
env->DeleteLocalRef( jExceptClass );
env->DeleteLocalRef( jExcept );
}
}

bool BOpen()
{
// Make sure thread is attached to JVM/env
Expand All @@ -407,6 +438,7 @@ class CHIDDevice

m_bIsWaitingForOpen = false;
m_bOpenResult = env->CallBooleanMethod( g_HIDDeviceManagerCallbackHandler, g_midHIDDeviceManagerOpen, m_nId );
ExceptionCheck( env, "BOpen" );

if ( m_bIsWaitingForOpen )
{
Expand Down Expand Up @@ -515,6 +547,8 @@ class CHIDDevice

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

env->DeleteLocalRef( pBuf );
return nRet;
}
Expand All @@ -528,6 +562,7 @@ class CHIDDevice

jbyteArray pBuf = NewByteArray( env, pData, nDataLen );
int nRet = env->CallIntMethod( g_HIDDeviceManagerCallbackHandler, g_midHIDDeviceManagerSendFeatureReport, m_nId, pBuf );
ExceptionCheck( env, "SendFeatureReport" );
env->DeleteLocalRef( pBuf );
return nRet;
}
Expand Down Expand Up @@ -564,6 +599,7 @@ class CHIDDevice

jbyteArray pBuf = NewByteArray( env, pData, nDataLen );
int nRet = env->CallBooleanMethod( g_HIDDeviceManagerCallbackHandler, g_midHIDDeviceManagerGetFeatureReport, m_nId, pBuf ) ? 0 : -1;
ExceptionCheck( env, "GetFeatureReport" );
env->DeleteLocalRef( pBuf );
if ( nRet < 0 )
{
Expand Down Expand Up @@ -622,7 +658,8 @@ class CHIDDevice
pthread_setspecific( g_ThreadKey, (void*)env );

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 c4918db

Please sign in to comment.