1 /* JORTESubscriptionCreate.c */
4 * This code provides conversion between JAVA a C environments.
5 * The C functions are calling here and results are send to JAVA
6 * native functions. It uses the header pregenerated by JAVA
7 * (by command 'javah -jni class_with_native_function')
9 * @author Lukas Pokorny (lukas_pokorny@centrum.cz)
10 * @author CTU FEE Prague - Department of Control Engineering (dce.felk.cvut.cz)
11 * @author Project ORTE - OCERA Real Time Ethernet (www.ocera.org)
12 * @author dedication to Kj
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
32 // library header file's path
34 // pregenerated header
35 #include "jorte/org_ocera_orte_Subscription.h"
36 // enable TEST_STAGE run level
37 #include "jorte/4all.h"
39 #include "jorte/jorte_typedefs_defines.h"
40 #include "jorte/jorte_protos_api.h"
42 /* ****************************************************************** *
44 * ****************************************************************** */
47 recvCallBack(const ORTERecvInfo *info,void *vinstance, void *recvCallBackParam)
52 jclass cls = 0; // local reference!
59 jmethodID mid_callback = 0;
61 JORTECallbackContext_t *callback_cont = (JORTECallbackContext_t*)recvCallBackParam;
64 printf("\n\n:c: --------------- recvCallBack called.. --------------- \n");
69 // set local variables from struct
70 if(callback_cont->jvm == 0)
73 printf(":!c: jvm = NULL \n");
77 jvm = callback_cont->jvm;
79 (*jvm)->AttachCurrentThread(jvm,
89 printf(":!c: env = NULL \n");
94 // set byte order only if it differs from that currently set
95 if(info->data_endian != callback_cont->cur_endian) {
97 cls = (*env)->FindClass(env, "java/nio/ByteOrder");
100 printf(":!c: cls = NULL \n");
103 if(info->data_endian == BigEndian) {
104 fid = (*env)->GetStaticFieldID(env,
107 "Ljava/nio/ByteOrder;");
108 callback_cont->cur_endian = BigEndian;
111 fid = (*env)->GetStaticFieldID(env,
114 "Ljava/nio/ByteOrder;");
115 callback_cont->cur_endian = LittleEndian;
119 printf(":!c: fid = NULL \n");
122 obj_bo = (*env)->GetStaticObjectField(env, cls, fid);
125 printf(":!c: cls = NULL \n");
129 // set byte order to ByteBuffer
131 cls = (*env)->GetObjectClass(env, callback_cont->obj_buf);
135 printf(":!c: cls = NULL \n");
138 // get methodID - order(ByteOrder)
139 mid = (*env)->GetMethodID(env,
142 "(Ljava/nio/ByteOrder;)Ljava/nio/ByteBuffer;");
146 printf(":!c: mid = NULL \n");
151 if((*env)->CallObjectMethod(env,callback_cont->obj_buf,mid,obj_bo) == 0)
154 printf(":!c: set byte order failed.. \n");
159 if(callback_cont->obj == 0)
162 printf(":!c: obj = NULL \n");
167 rinfo = callback_cont->rinfo;
168 obj_msg = callback_cont->msg;
173 printf(":c: env = %#"PRIxPTR", obj_msg = %#"PRIxPTR" \n", (intptr_t)env, (intptr_t)obj_msg);
181 cls = findClass(env, "org.ocera.orte.types.RecvInfo");
185 printf(":!c: cls = NULL \n");
189 // call object constructor
190 mid = (*env)->GetMethodID(env, cls, "<init>", "()V");
194 printf(":!c: constructor failed! \n");
199 rinfo = (*env)->NewObject(env, cls, mid);
203 printf(":!c: rinfo = NULL \n");
207 // create global reference
208 callback_cont->rinfo = (*env)->NewGlobalRef(env, rinfo);
209 if (callback_cont->rinfo == 0)
212 printf(":!c: callback_cont->rinfo = NULL \n");
217 ////////////////////////////////////////////////////
218 // set RecvInfo instance
219 if(setRecvInfo(env,info,callback_cont->rinfo) == 0)
222 printf(":!c: setRecvInfo() failed! \n");
226 ////////////////////////////////////////////////////
227 // control print - only in TEST_STAGE
229 printf(":c: rinfo created :] \n");
230 printf(":c:----- ORTERecvInfo members ------ \n");
231 printf(":c: recvStatus: %#x \n", info->status);
232 printf(":c: senderGuid: hid = %#"PRIx32", aid = %#"PRIx32", oid = %#"PRIx32" \n",
233 info->senderGUID.hid,info->senderGUID.aid,info->senderGUID.oid);
234 printf(":c: topic: %s \n",info->topic);
235 printf(":c: type: %s \n",info->type);
236 printf(":c: localTimeRecv: sec = %"PRId32", fract = %"PRIu32" \n",
237 info->localTimeReceived.seconds,info->localTimeReceived.fraction);
238 printf(":c: remoteTimePub: sec = %"PRId32", fract = %"PRIu32" \n",
239 info->remoteTimePublished.seconds,info->remoteTimePublished.fraction);
240 printf(":c: seqNr: high = %"PRId32", low = %"PRIu32" \n",info->sn.high,info->sn.low);
241 printf(":c:---------------------------------- \n");
243 ////////////////////////////////////////////////////
244 // update MessageData instance
246 cls_msg = (*env)->GetObjectClass(env, obj_msg);
250 printf(":!c: cls_msg = NULL \n");
254 /////////////////////////////////////////////////////
256 mid = (*env)->GetMethodID(env,
263 printf(":!c: mid = NULL \n");
268 (*env)->CallVoidMethod(env,
272 /* *************************** *
273 * call JAVA CallBack method *
274 * *************************** */
276 printf(":c: call JAVA CallBack method \n");
281 cls = (*env)->GetObjectClass(env,callback_cont->obj);
285 printf(":!c: cls = NULL \n");
290 mid = (*env)->GetMethodID(env,
293 "(Lorg/ocera/orte/types/RecvInfo;Lorg/ocera/orte/types/MessageData;)V");
297 printf(":!c: cls = NULL \n");
304 printf(":c: volam callback metodu.. halo jsi tam?? \n");
306 // call object's method
307 (*env)->CallVoidMethod(env,
308 callback_cont->obj, /*obj*/
310 callback_cont->rinfo,
314 // detach current thread
315 if((*jvm)->DetachCurrentThread(jvm) != 0)
316 printf(":c!: DetachCurrentThread failed! \n");
319 printf(":c: ------------ thats all from recvCallBack ------------ \n\n");
324 /* ****************************************************************** *
326 * ****************************************************************** */
327 JNIEXPORT jlong JNICALL
328 Java_org_ocera_orte_Subscription_jORTESubscriptionCreate
331 jlong dhandle, // appDomain handle
332 jint jsmode, // subs mode
333 jint jstype, // subs type
334 jstring jtopic, // subs topic
335 jstring jtname, // subs typeName
336 jobject jinstance, // direct ByteBuffer
337 jint jbyteOrder,// byte order of ByteBuffer
338 jobject obj_msg, // messageData instance
340 jobject jminSeparation,
341 jobject obj_callback,
349 ORTESubscription *s = 0;
351 SubscriptionMode smode;
352 SubscriptionType stype;
354 NtpTime minSeparation;
356 JORTECallbackContext_t *callback_cont;
357 // standart variables
358 const char *topic = 0;
359 const char *typename = 0;
363 // don't forget use free() funct.!!
364 callback_cont = (JORTECallbackContext_t*)malloc(sizeof(JORTECallbackContext_t));
369 // get direct ByteBuffer pointer from Java
370 buffer = (*env)->GetDirectBufferAddress(env, jinstance);
371 // check obj_callback
372 if (obj_callback == 0)
375 printf(":!c: obj_callback = NULL \n");
380 jint b = (*env)->GetJavaVM(env,&jvm);
384 printf(":!c: getJavaVM() failed! \n");
391 printf(":c: getJavaVM succesfull.. \n");
394 callback_cont->jvm = jvm;
395 callback_cont->cur_endian = (CDR_Endianness) jbyteOrder;
396 // create global references
397 callback_cont->obj = (*env)->NewGlobalRef(env, obj_callback);
399 if (callback_cont->obj == 0)
402 printf(":c: global reference not created! \n");
406 // create global references
407 callback_cont->obj_buf = (*env)->NewGlobalRef(env, jinstance);
409 if (callback_cont->obj_buf == 0)
412 printf(":c: global reference not created! \n");
416 // create global references
417 callback_cont->msg = (*env)->NewGlobalRef(env, obj_msg);
419 if (callback_cont->msg == 0)
422 printf(":c: global reference not created! \n");
426 // init RecvInfo pointer
427 callback_cont->rinfo = 0;
429 cls = (*env)->GetObjectClass(env, obj);
433 printf(":!c: cls = NULL \n");
437 // fieldID - callbackContextHandle
438 fid = (*env)->GetFieldID(env,
440 "callbackContextHandle",
445 printf(":!c: fid = NULL \n");
449 (*env)->SetLongField(env,
452 (jlong) callback_cont);
454 printf(":c: ORTESubscriptionCreate() calling..\n");
457 d = (ORTEDomain *) dhandle;
461 printf(":!c: d = NULL [bad domain handle] \n");
466 smode = (SubscriptionMode) jsmode;
467 stype = (SubscriptionType) jstype;
468 topic = (*env)->GetStringUTFChars(env, jtopic, 0);
469 typename = (*env)->GetStringUTFChars(env, jtname, 0);
470 deadline = getNtpTime(env, jdeadline);//
471 minSeparation = getNtpTime(env, jminSeparation);//
472 // call ORTE function
473 s = ORTESubscriptionCreate(d,
482 (void*)callback_cont,
483 (uint32_t) j_multicastIP);
487 printf(":!c: s = NULL [subscription not created] \n");
497 (*env)->ReleaseStringUTFChars(env, jtopic, topic);
498 (*env)->ReleaseStringUTFChars(env, jtname, typename);
499 // returns handle of new created Subscription
500 if(flag_ok == 0) return 0;