标签:nal exp c++ 参数传递 port nbsp java release jni
java里面有如下本地定义:
private static native int setFilter(final int fd, final int[][] array_filter);
c++中对应的函数如下:
JNIEXPORT jint JNICALL Java_JniSocketCan_setFilter
(JNIEnv *env, jclass obj, jint fd, jobjectArray array_filter)
{
struct can_filter *pFilter = NULL;
jint filter_num = 0;
jint column_num = 0;
jint i;
jintArray filter_tmp;
jint mem_length = 0;
filter_num = env->GetArrayLength(array_filter);
DBG("filter_num = %d\n", filter_num);
mem_length = filter_num * sizeof(struct can_filter);
pFilter = (struct can_filter*)malloc(mem_length);
if (NULL == pFilter) {
LOG_ERR("memory is not enough, allocate failed\n");
return -2;
}
for (i = 0; i < filter_num; i++) {
filter_tmp = (jintArray)(env->GetObjectArrayElement(array_filter, i));
column_num = env->GetArrayLength(filter_tmp);
if (2 != column_num) {
LOG_ERR("array_filter column != 2, we only need can_id and can_mask\n");
return -1;
}
jint *pColumnData = env->GetIntArrayElements(filter_tmp, 0 );
pFilter[i].can_id = pColumnData[0];
pFilter[i].can_mask = pColumnData[1];
DBG("%d:id=0x%x,mask=0x%x\n", i, pFilter[i].can_id, pFilter[i].can_mask);
env->ReleaseIntArrayElements(filter_tmp, pColumnData, 0);
}
jint ret = setsockopt(fd, SOL_CAN_RAW, CAN_RAW_FILTER, pFilter, mem_length);
free(pFilter);
return ret;
}
java中调用的代码如下:
int[][] can_filter = {
{0x234,0x7FF},
{0x235,0x7FF}
};
canTest.setFilter(s, can_filter);
标签:nal exp c++ 参数传递 port nbsp java release jni
原文地址:http://www.cnblogs.com/eaggle/p/7777738.html