Skip to content

Commit

Permalink
Get errno via native function
Browse files Browse the repository at this point in the history
  • Loading branch information
magnusja committed May 19, 2017
1 parent 4a1b29b commit c6af16b
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 15 deletions.
37 changes: 24 additions & 13 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*.apk
*.ap_

# Files for the Dalvik VM
# Files for the ART/Dalvik VM
*.dex

# Java class files
Expand All @@ -14,6 +14,7 @@
# Generated files
bin/
gen/
out/

# Gradle files
.gradle/
Expand All @@ -31,23 +32,33 @@ proguard/
# Android Studio Navigation editor temp files
.navigation/

### Android Patch ###
gen-external-apklibs
# Android Studio captures folder
captures/

# Intellij
*.iml
.idea/workspace.xml
.idea/tasks.xml
.idea/gradle.xml
.idea/dictionaries
.idea/libraries

### Java ###
*.class
# Keystore files
*.jks

# Mobile Tools for Java (J2ME)
.mtj.tmp/
# External native build folder generated in Android Studio 2.2 and later
.externalNativeBuild

# Package Files #
*.jar
*.war
*.ear
# Google Services (e.g. APIs or Firebase)
google-services.json

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
# Freeline
freeline.py
freeline/
freeline_project_description.json

### Android Patch ###
gen-external-apklibs


### Intellij ###
Expand Down
9 changes: 9 additions & 0 deletions libaums/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
cmake_minimum_required(VERSION 3.4.1)

add_library( errno-lib

# Sets the library as a shared library.
SHARED

# Provides a relative path to your source file(s).
src/c/errno.c )
6 changes: 6 additions & 0 deletions libaums/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ android {
lintOptions {
abortOnError false
}

externalNativeBuild {
cmake {
path "CMakeLists.txt"
}
}
}

dependencies {
Expand Down
14 changes: 14 additions & 0 deletions libaums/src/c/errno.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include <errno.h>
#include <string.h>
#include <jni.h>

JNIEXPORT jint JNICALL
Java_com_github_mjdev_libaums_ErrNo_getErrno(JNIEnv *env, jobject thiz) {
return errno;
}

JNIEXPORT jstring JNICALL
Java_com_github_mjdev_libaums_ErrNo_getErrstr(JNIEnv *env, jobject thiz) {
char *error = strerror(errno);
return (*env)->NewStringUTF(env, error);
}
14 changes: 14 additions & 0 deletions libaums/src/main/java/com/github/mjdev/libaums/ErrNo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.github.mjdev.libaums;

/**
* Created by magnusja on 5/19/17.
*/

public class ErrNo {
static {
System.loadLibrary("errno-lib");
}

public static native int getErrno();
public static native String getErrstr();
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
import android.hardware.usb.UsbDeviceConnection;
import android.hardware.usb.UsbEndpoint;
import android.os.Build;
import android.system.ErrnoException;

import com.github.mjdev.libaums.ErrNo;

import java.io.IOException;
import java.nio.ByteBuffer;
Expand Down Expand Up @@ -39,7 +42,7 @@ public int bulkOutTransfer(ByteBuffer src) throws IOException {
src.array(), src.position(), src.remaining(), TRANSFER_TIMEOUT);

if (result == -1) {
throw new IOException("Could not write to device, result == -1");
throw new IOException("Could not write to device, result == -1 errno " + ErrNo.getErrno());
}

src.position(src.position() + result);
Expand All @@ -52,7 +55,7 @@ public int bulkInTransfer(ByteBuffer dest) throws IOException {
dest.array(), dest.position(), dest.remaining(), TRANSFER_TIMEOUT);

if (result == -1) {
throw new IOException("Could not read from device, result == -1");
throw new IOException("Could not read from device, result == -1 errno " + ErrNo.getErrno() + " " + ErrNo.getErrstr());
}

dest.position(dest.position() + result);
Expand Down

0 comments on commit c6af16b

Please sign in to comment.