#include <stdio.h>
#include <string.h>
#include <jni.h>
#include <fcntl.h> /*包括文件操作,如open() read() close()write()等*/
#include <android/log.h>
#define LOG_TAG "a7105" #define LOGI(...) __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__) #define DEVICE_NAME "/dev/a7105_spi_moni" //device point int fd; jstring Java_com_example_a7105demo_a7105Class_stringFromJNI( JNIEnv* env, jobject thiz )
{ return (*env)->NewStringUTF(env, "This is a7105 demo project!");
} jint Java_com_example_a7105demo_a7105Class_Init( JNIEnv* env)
{ LOGE("a7105Class()\n"); fd = open(DEVICE_NAME, 0); LOGE("a7105Class()-> fd = %d \n", fd); if (fd < 0){ LOGE("open device %s error \n", DEVICE_NAME); return 0; } return 1;
} #if 0
jint Java_com_example_a7105demo_a7105Class_IOCTL( JNIEnv* env, jobject obj , jint ID, jint state)
{ LOGE("IOCTL()-> %d dvr gpio State \n",state); LOGE("IOCTL()-> %d dvr gpio State \n",0); ioctl(fd,state, NULL); return 1;
}
#endifjint Java_com_example_a7105demo_a7105Class_Read( JNIEnv* env, jobject obj, jintArray bufArr, jint bufLen)
{ char *bufByte; int *bufInt;int fd; int ret;int i;bufByte = malloc(bufLen); bufInt = malloc(bufLen); LOGE("++++++++Read+++++++++ /n"); (*env)->GetIntArrayRegion(env, bufArr, 0, bufLen, bufInt);fd = open(DEVICE_NAME, O_RDONLY); if (fd < 0){LOGE("open device %s error \n", DEVICE_NAME); return fd;}ret = read(fd, bufByte, bufLen);if (ret < 0) {LOGE("open device %s error \n", DEVICE_NAME); return 0; }for (i = 0; i < bufLen; i++) { bufInt[i] = bufByte[i]; LOGE("a7105Class_Read:bufByte = %x, bufInt = %d\n", bufByte[i], bufInt[i]);} /*将读取的值,返回到VM中*/ (*env)->SetIntArrayRegion(env, bufArr, 0, bufLen, bufInt); close(fd);return 1;
} jint Java_com_example_a7105demo_a7105Class_Write( JNIEnv* env, jobject obj, jintArray bufArr, jint bufLen)
{ char *bufByte; int *bufInt;int fd; int i;LOGE("++++++++Write+++++++++ /n"); bufByte = malloc(bufLen); bufInt = malloc(bufLen); (*env)->GetIntArrayRegion(env, bufArr, 0, bufLen, bufInt); /*驱动是byte,所以将int转换成byte*/for (i = 0; i < bufLen; i++) { bufByte[i] = bufInt[i]; LOGE("a7105Class_Write:bufByte = %x, bufInt = %d\n", bufByte[i], bufInt[i]);}fd = open(DEVICE_NAME, O_WRONLY); if (fd < 0){LOGE("open device %s error \n", DEVICE_NAME); return fd;}write(fd, bufByte, bufLen);close(fd); return 1;
}
jint Java_com_example_a7105demo_a7105Class_CLOSE( JNIEnv* env, jobject obj )
{ close(fd); return 1;
}