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, (void **)&env, NULL);
83 printf(":!c: env = NULL \n");
88 // set byte order only if it differs from that currently set
89 if(info->data_endian != callback_cont->cur_endian) {
91 cls = (*env)->FindClass(env, "java/nio/ByteOrder");
94 printf(":!c: cls = NULL \n");
97 if(info->data_endian == BigEndian) {
98 fid = (*env)->GetStaticFieldID(env,
101 "Ljava/nio/ByteOrder;");
102 callback_cont->cur_endian = BigEndian;
105 fid = (*env)->GetStaticFieldID(env,
108 "Ljava/nio/ByteOrder;");
109 callback_cont->cur_endian = LittleEndian;
113 printf(":!c: fid = NULL \n");
116 obj_bo = (*env)->GetStaticObjectField(env, cls, fid);
119 printf(":!c: cls = NULL \n");
123 // set byte order to ByteBuffer
125 cls = (*env)->GetObjectClass(env, callback_cont->obj_buf);
129 printf(":!c: cls = NULL \n");
132 // get methodID - order(ByteOrder)
133 mid = (*env)->GetMethodID(env,
136 "(Ljava/nio/ByteOrder;)Ljava/nio/ByteBuffer;");
140 printf(":!c: mid = NULL \n");
145 if((*env)->CallObjectMethod(env,callback_cont->obj_buf,mid,obj_bo) == 0)
148 printf(":!c: set byte order failed.. \n");
153 if(callback_cont->obj == 0)
156 printf(":!c: obj = NULL \n");
161 rinfo = callback_cont->rinfo;
162 obj_msg = callback_cont->msg;
167 printf(":c: env = %#"PRIxPTR", obj_msg = %#"PRIxPTR" \n", (intptr_t)env, (intptr_t)obj_msg);
175 cls = findClass(env, "org.ocera.orte.types.RecvInfo");
179 printf(":!c: cls = NULL \n");
183 // call object constructor
184 mid = (*env)->GetMethodID(env, cls, "<init>", "()V");
188 printf(":!c: constructor failed! \n");
193 rinfo = (*env)->NewObject(env, cls, mid);
197 printf(":!c: rinfo = NULL \n");
201 // create global reference
202 callback_cont->rinfo = (*env)->NewGlobalRef(env, rinfo);
203 if (callback_cont->rinfo == 0)
206 printf(":!c: callback_cont->rinfo = NULL \n");
211 ////////////////////////////////////////////////////
212 // set RecvInfo instance
213 if(setRecvInfo(env,info,callback_cont->rinfo) == 0)
216 printf(":!c: setRecvInfo() failed! \n");
220 ////////////////////////////////////////////////////
221 // control print - only in TEST_STAGE
223 printf(":c: rinfo created :] \n");
224 printf(":c:----- ORTERecvInfo members ------ \n");
225 printf(":c: recvStatus: %#x \n", info->status);
226 printf(":c: senderGuid: hid = %#"PRIx32", aid = %#"PRIx32", oid = %#"PRIx32" \n",
227 info->senderGUID.hid,info->senderGUID.aid,info->senderGUID.oid);
228 printf(":c: topic: %s \n",info->topic);
229 printf(":c: type: %s \n",info->type);
230 printf(":c: localTimeRecv: sec = %"PRId32", fract = %"PRIu32" \n",
231 info->localTimeReceived.seconds,info->localTimeReceived.fraction);
232 printf(":c: remoteTimePub: sec = %"PRId32", fract = %"PRIu32" \n",
233 info->remoteTimePublished.seconds,info->remoteTimePublished.fraction);
234 printf(":c: seqNr: high = %"PRId32", low = %"PRIu32" \n",info->sn.high,info->sn.low);
235 printf(":c:---------------------------------- \n");
237 ////////////////////////////////////////////////////
238 // update MessageData instance
240 cls_msg = (*env)->GetObjectClass(env, obj_msg);
244 printf(":!c: cls_msg = NULL \n");
248 /////////////////////////////////////////////////////
250 mid = (*env)->GetMethodID(env,
257 printf(":!c: mid = NULL \n");
262 (*env)->CallVoidMethod(env,
266 /* *************************** *
267 * call JAVA CallBack method *
268 * *************************** */
270 printf(":c: call JAVA CallBack method \n");
275 cls = (*env)->GetObjectClass(env,callback_cont->obj);
279 printf(":!c: cls = NULL \n");
284 mid = (*env)->GetMethodID(env,
287 "(Lorg/ocera/orte/types/RecvInfo;Lorg/ocera/orte/types/MessageData;)V");
291 printf(":!c: cls = NULL \n");
298 printf(":c: volam callback metodu.. halo jsi tam?? \n");
300 // call object's method
301 (*env)->CallVoidMethod(env,
302 callback_cont->obj, /*obj*/
304 callback_cont->rinfo,
308 // detach current thread
309 if((*jvm)->DetachCurrentThread(jvm) != 0)
310 printf(":c!: DetachCurrentThread failed! \n");
313 printf(":c: ------------ thats all from recvCallBack ------------ \n\n");
318 /* ****************************************************************** *
320 * ****************************************************************** */
321 JNIEXPORT jlong JNICALL
322 Java_org_ocera_orte_Subscription_jORTESubscriptionCreate
325 jlong dhandle, // appDomain handle
326 jint jsmode, // subs mode
327 jint jstype, // subs type
328 jstring jtopic, // subs topic
329 jstring jtname, // subs typeName
330 jobject jinstance, // direct ByteBuffer
331 jobject obj_msg, // messageData instance
333 jobject jminSeparation,
334 jobject obj_callback,
342 ORTESubscription *s = 0;
344 SubscriptionMode smode;
345 SubscriptionType stype;
347 NtpTime minSeparation;
349 JORTECallbackContext_t *callback_cont;
350 // standart variables
351 const char *topic = 0;
352 const char *typename = 0;
356 // don't forget use free() funct.!!
357 callback_cont = (JORTECallbackContext_t*)malloc(sizeof(JORTECallbackContext_t));
362 // get direct ByteBuffer pointer from Java
363 buffer = (*env)->GetDirectBufferAddress(env, jinstance);
364 // check obj_callback
365 if (obj_callback == 0)
368 printf(":!c: obj_callback = NULL \n");
373 jint b = (*env)->GetJavaVM(env,&jvm);
377 printf(":!c: getJavaVM() failed! \n");
384 printf(":c: getJavaVM succesfull.. \n");
387 callback_cont->jvm = jvm;
388 callback_cont->cur_endian = FLAG_ENDIANNESS;
389 // create global references
390 callback_cont->obj = (*env)->NewGlobalRef(env, obj_callback);
392 if (callback_cont->obj == 0)
395 printf(":c: global reference not created! \n");
399 // create global references
400 callback_cont->obj_buf = (*env)->NewGlobalRef(env, jinstance);
402 if (callback_cont->obj_buf == 0)
405 printf(":c: global reference not created! \n");
409 // create global references
410 callback_cont->msg = (*env)->NewGlobalRef(env, obj_msg);
412 if (callback_cont->msg == 0)
415 printf(":c: global reference not created! \n");
419 // init RecvInfo pointer
420 callback_cont->rinfo = 0;
422 cls = (*env)->GetObjectClass(env, obj);
426 printf(":!c: cls = NULL \n");
430 // fieldID - callbackContextHandle
431 fid = (*env)->GetFieldID(env,
433 "callbackContextHandle",
438 printf(":!c: fid = NULL \n");
442 (*env)->SetLongField(env,
445 (jlong) callback_cont);
447 printf(":c: ORTESubscriptionCreate() calling..\n");
450 d = (ORTEDomain *) dhandle;
454 printf(":!c: d = NULL [bad domain handle] \n");
459 smode = (SubscriptionMode) jsmode;
460 stype = (SubscriptionType) jstype;
461 topic = (*env)->GetStringUTFChars(env, jtopic, 0);
462 typename = (*env)->GetStringUTFChars(env, jtname, 0);
463 deadline = getNtpTime(env, jdeadline);//
464 minSeparation = getNtpTime(env, jminSeparation);//
465 // call ORTE function
466 s = ORTESubscriptionCreate(d,
475 (void*)callback_cont,
476 (uint32_t) j_multicastIP);
480 printf(":!c: s = NULL [subscription not created] \n");
490 (*env)->ReleaseStringUTFChars(env, jtopic, topic);
491 (*env)->ReleaseStringUTFChars(env, jtname, typename);
492 // returns handle of new created Subscription
493 if(flag_ok == 0) return 0;