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;
62 // if the subscriber has been destroyed, return
63 if ((*(JORTECallbackContext_t **)recvCallBackParam) == 0)
66 JORTECallbackContext_t *callback_cont = *((JORTECallbackContext_t **)recvCallBackParam);
69 printf("\n\n:c: --------------- recvCallBack called.. --------------- \n");
73 // set local variables from struct
74 if (callback_cont->jvm == 0) {
76 printf(":!c: jvm = NULL \n");
80 jvm = callback_cont->jvm;
82 (*jvm)->AttachCurrentThread(jvm,
91 printf(":!c: env = NULL \n");
96 // set byte order only if it differs from that currently set
97 if (info->data_endian != callback_cont->cur_endian) {
99 cls = (*env)->FindClass(env, "java/nio/ByteOrder");
102 printf(":!c: cls = NULL \n");
105 if (info->data_endian == BigEndian) {
106 fid = (*env)->GetStaticFieldID(env,
109 "Ljava/nio/ByteOrder;");
110 callback_cont->cur_endian = BigEndian;
112 fid = (*env)->GetStaticFieldID(env,
115 "Ljava/nio/ByteOrder;");
116 callback_cont->cur_endian = LittleEndian;
120 printf(":!c: fid = NULL \n");
123 obj_bo = (*env)->GetStaticObjectField(env, cls, fid);
126 printf(":!c: cls = NULL \n");
130 // set byte order to ByteBuffer
132 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;");
145 printf(":!c: mid = NULL \n");
150 if ((*env)->CallObjectMethod(env, callback_cont->obj_buf, mid, obj_bo) == 0) {
152 printf(":!c: set byte order failed.. \n");
157 if (callback_cont->obj == 0) {
159 printf(":!c: obj = NULL \n");
164 rinfo = callback_cont->rinfo;
165 obj_msg = callback_cont->msg;
170 printf(":c: env = %#" PRIxPTR ", obj_msg = %#" PRIxPTR " \n", (intptr_t)env, (intptr_t)obj_msg);
177 cls = findClass(env, "org.ocera.orte.types.RecvInfo");
180 printf(":!c: cls = NULL \n");
184 // call object constructor
185 mid = (*env)->GetMethodID(env, cls, "<init>", "()V");
188 printf(":!c: constructor failed! \n");
193 rinfo = (*env)->NewObject(env, cls, mid);
196 printf(":!c: rinfo = NULL \n");
200 // create global reference
201 callback_cont->rinfo = (*env)->NewGlobalRef(env, rinfo);
202 if (callback_cont->rinfo == 0) {
204 printf(":!c: callback_cont->rinfo = NULL \n");
209 ////////////////////////////////////////////////////
210 // set RecvInfo instance
211 if (setRecvInfo(env, info, callback_cont->rinfo) == 0) {
213 printf(":!c: setRecvInfo() failed! \n");
217 ////////////////////////////////////////////////////
218 // control print - only in TEST_STAGE
220 printf(":c: rinfo created :] \n");
221 printf(":c:----- ORTERecvInfo members ------ \n");
222 printf(":c: recvStatus: %#x \n", info->status);
223 printf(":c: senderGuid: hid = %#" PRIx32 ", aid = %#" PRIx32 ", oid = %#" PRIx32 " \n",
224 info->senderGUID.hid, info->senderGUID.aid, info->senderGUID.oid);
225 printf(":c: topic: %s \n", info->topic);
226 printf(":c: type: %s \n", info->type);
227 printf(":c: localTimeRecv: sec = %" PRId32 ", fract = %" PRIu32 " \n",
228 info->localTimeReceived.seconds, info->localTimeReceived.fraction);
229 printf(":c: remoteTimePub: sec = %" PRId32 ", fract = %" PRIu32 " \n",
230 info->remoteTimePublished.seconds, info->remoteTimePublished.fraction);
231 printf(":c: seqNr: high = %" PRId32 ", low = %" PRIu32 " \n", info->sn.high, info->sn.low);
232 printf(":c:---------------------------------- \n");
234 ////////////////////////////////////////////////////
235 // update MessageData instance
237 cls_msg = (*env)->GetObjectClass(env, obj_msg);
240 printf(":!c: cls_msg = NULL \n");
244 /////////////////////////////////////////////////////
246 mid = (*env)->GetMethodID(env,
252 printf(":!c: mid = NULL \n");
257 (*env)->CallVoidMethod(env,
261 /* *************************** *
262 * call JAVA CallBack method *
263 * *************************** */
265 printf(":c: call JAVA CallBack method \n");
270 cls = (*env)->GetObjectClass(env, callback_cont->obj);
273 printf(":!c: cls = NULL \n");
278 mid = (*env)->GetMethodID(env,
281 "(Lorg/ocera/orte/types/RecvInfo;Lorg/ocera/orte/types/MessageData;)V");
284 printf(":!c: cls = NULL \n");
291 printf(":c: volam callback metodu.. halo jsi tam?? \n");
293 // call object's method
294 (*env)->CallVoidMethod(env,
295 callback_cont->obj, /*obj*/
297 callback_cont->rinfo,
301 // detach current thread
302 if ((*jvm)->DetachCurrentThread(jvm) != 0)
303 printf(":c!: DetachCurrentThread failed! \n");
306 printf(":c: ------------ thats all from recvCallBack ------------ \n\n");
311 /* ****************************************************************** *
313 * ****************************************************************** */
314 JNIEXPORT jlong JNICALL
315 Java_org_ocera_orte_Subscription_jORTESubscriptionCreate
318 jlong dhandle, // appDomain handle
319 jint jsmode, // subs mode
320 jint jstype, // subs type
321 jstring jtopic, // subs topic
322 jstring jtname, // subs typeName
323 jobject jinstance, // direct ByteBuffer
324 jint jbyteOrder, // byte order of ByteBuffer
325 jobject obj_msg, // messageData instance
327 jobject jminSeparation,
328 jobject obj_callback,
336 ORTESubscription *s = 0;
338 SubscriptionMode smode;
339 SubscriptionType stype;
341 NtpTime minSeparation;
343 JORTECallbackContext_t *callback_cont;
344 JORTECallbackContext_t **callback_cont_ptr;
346 // standart variables
347 const char *topic = 0;
348 const char *typename = 0;
353 // don't forget use free() funct.!!
354 callback_cont = (JORTECallbackContext_t *)malloc(sizeof(JORTECallbackContext_t));
355 callback_cont_ptr = (JORTECallbackContext_t **)malloc(sizeof(JORTECallbackContext_t *));
356 *callback_cont_ptr = callback_cont;
360 // get direct ByteBuffer pointer from Java
361 buffer = (*env)->GetDirectBufferAddress(env, jinstance);
362 // check obj_callback
363 if (obj_callback == 0) {
365 printf(":!c: obj_callback = NULL \n");
370 jint b = (*env)->GetJavaVM(env, &jvm);
373 printf(":!c: getJavaVM() failed! \n");
379 printf(":c: getJavaVM succesfull.. \n");
382 callback_cont->jvm = jvm;
383 callback_cont->cur_endian = (CDR_Endianness)jbyteOrder;
384 // create global references
385 callback_cont->obj = (*env)->NewGlobalRef(env, obj_callback);
387 if (callback_cont->obj == 0) {
389 printf(":c: global reference not created! \n");
393 // create global references
394 callback_cont->obj_buf = (*env)->NewGlobalRef(env, jinstance);
396 if (callback_cont->obj_buf == 0) {
398 printf(":c: global reference not created! \n");
402 // create global references
403 callback_cont->msg = (*env)->NewGlobalRef(env, obj_msg);
405 if (callback_cont->msg == 0) {
407 printf(":c: global reference not created! \n");
411 // init RecvInfo pointer
412 callback_cont->rinfo = 0;
414 cls = (*env)->GetObjectClass(env, obj);
417 printf(":!c: cls = NULL \n");
421 // fieldID - callbackContextHandle
422 fid = (*env)->GetFieldID(env,
424 "callbackContextHandle",
428 printf(":!c: fid = NULL \n");
432 (*env)->SetLongField(env,
435 (jlong)callback_cont_ptr);
437 printf(":c: ORTESubscriptionCreate() calling..\n");
440 d = (ORTEDomain *)dhandle;
443 printf(":!c: d = NULL [bad domain handle] \n");
448 smode = (SubscriptionMode)jsmode;
449 stype = (SubscriptionType)jstype;
450 topic = (*env)->GetStringUTFChars(env, jtopic, 0);
451 typename = (*env)->GetStringUTFChars(env, jtname, 0);
452 deadline = getNtpTime(env, jdeadline); //
453 minSeparation = getNtpTime(env, jminSeparation); //
454 // call ORTE function
455 s = ORTESubscriptionCreate(d,
464 (void *)callback_cont_ptr,
465 (uint32_t)j_multicastIP);
468 printf(":!c: s = NULL [subscription not created] \n");
478 (*env)->ReleaseStringUTFChars(env, jtopic, topic);
479 (*env)->ReleaseStringUTFChars(env, jtname, typename);
480 // returns handle of new created Subscription