Java has the following local definition:
private static native int setfilter (final int fd, final int[][] array_filter);
The corresponding functions in C + + are as follows:
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;
}
The code that is called in Java is as follows:
Int[][] Can_filter = {
{0X234,0X7FF},
{0X235,0X7FF}
};
Cantest.setfilter (S, can_filter);
The Java JNI Two-dimensional array is passed as a method parameter to the local