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.
30 #include "jorte/getNtpTime.h"
31 // library header file's path
33 // pregenerated header
34 #include "jorte/org_ocera_orte_Subscription.h"
35 // enable TEST_STAGE run level
36 #include "jorte/4all.h"
44 int value; // !! pro testovani
45 } JORTECallbackContext_t;
50 jobject obj_recvStatus;
59 #include "jorte/typedefs_defines_jorte.h"
61 // ### recvCallBack() #########################################################
64 recvCallBack(const ORTERecvInfo *info,void *vinstance, void *recvCallBackParam)
66 // char *instance=(char*)vinstance;
69 JNIEnv *env = NULL; // local reference - Ok
71 jclass cls = 0; //! local reference
73 jobject obj = 0; //! local reference
74 //jobject obj_recvInfo = 0; //! local reference
75 jobject rinfo = 0; //! local reference
76 jobject obj_instance = 0;
78 jmethodID mid = 0; //! local reference
79 jmethodID mid_callback = 0; //! local reference
81 jfieldID fid = 0; //! local reference
83 //RecvInfoVar_t *recvInfoVar = (RecvInfoVar_t*)malloc(sizeof(RecvInfoVar_t));
85 JORTECallbackContext_t *callback_cont = (JORTECallbackContext_t*)recvCallBackParam;
87 int i = callback_cont->value++; // !! JEN PRO TESTOVACI UCELY
95 printf(":c: --------------- spustena recvCallBack.. z Ccka ------------------\n");
98 if(callback_cont->obj == 0) return;
99 obj = callback_cont->obj;
101 if(callback_cont->jvm == 0) return;
102 jvm = callback_cont->jvm;
104 rinfo = callback_cont->rinfo;
110 printf(":c: recvCallBack: jvm = %p \n", jvm);
111 printf(":c: recvCallBack: obj = %p \n", obj);
115 (*jvm)->AttachCurrentThread(jvm, (void **)&env, NULL);
118 printf(":!c: recvCallBack: can't get env..\n");
123 printf(":c: recvCallBack: env = %p \n", env);
127 cls = (*env)->GetObjectClass(env,obj);
130 printf(":!c: cannot find callbackObj class..\n");
133 // create global reference
136 printf(":c: recvCallBack: volana fce GetMethodID().. \n");
140 mid = (*env)->GetMethodID(env,
143 "(Lorg/ocera/orte/types/RecvInfo;Ljava/lang/Object;)V");
144 // "(Lorg/ocera/orte/types/RecvInfo;Ljava/lang/Object;)V");
145 // create global reference to callback method
148 printf(":!c: can not find callback() method...\n");
154 printf(":c: recvCallBack: obj = %p \n", obj);
155 printf(":c: recvCallBack: cls = %p \n", cls);
156 printf(":c: recvCallBack: mid = %p \n", mid);
161 // -- vytvoreni parametru callback funkce ---------------------------------------------
162 // vytvorit objekt RecvInfo - global ref
165 // get object RecvInfo class
166 cls = (*env)->FindClass(env, "org/ocera/orte/types/RecvInfo");
169 printf(":!c: cls RecvInfo class not found..\n");
170 return; // ma se pri selhani RecvInfa davat tvrdy return?
173 // create RecvInfo object
176 // call object constructor
177 mid = (*env)->GetMethodID(env, cls, "<init>", "()V");
180 printf(":!c: constructor RecvInfo() not found..\n");
181 return; // ma se pri selhani RecvInfa davat tvrdy return?
184 rinfo = (*env)->NewObject(env, cls, mid);
187 printf(":!c: obj_recvInfo not created..\n");
191 printf(":c: object RecvInfo created..\n");
193 // create global reference
194 callback_cont->rinfo = (*env)->NewGlobalRef(env, rinfo);
195 if (callback_cont->rinfo == 0)
197 printf(":!c: cannot create global reference callback_cont->rinfo ..\n");
201 printf(":c: global reference to object RecvInfo created..\n");
207 //--------------------------------------------------------------------
208 // set RecvInfo fields
211 //*** int i - testovaci
213 fid = (*env)->GetFieldID(env,cls,"i","I");
216 printf(":!c: cannot get ReccvInfo.i fieldID..\n");
217 return; // je toto nutne??
219 printf(":c: fid of ReccvInfo.i found..\n");
220 // set new value '1234'
221 (*env)->SetIntField(env,callback_cont->rinfo,fid,i++);
222 printf(":c: kontrola prirazeni %d \n",
223 (*env)->GetIntField(env,callback_cont->rinfo,fid));
224 printf(":c: new value of RecvInfo.i set..\n");
232 printf(":c: ---- RecvInfo [odesilane]:\n");
236 mid = (*env)->GetMethodID(env,
242 printf(":!c: can not find setRecvStatus() method...\n");
246 (*env)->CallVoidMethod(env,
247 callback_cont->rinfo,
252 // Look for the instance field s in cls
253 fid = (*env)->GetFieldID(env,
256 "Ljava/lang/String;");
259 printf(":!c: can not find 'topic' field ID...\n");
260 return; // failed to find the field
262 // Read the instance field s
263 jstr = (*env)->GetObjectField(env, obj, fid);
264 // Create a new string and overwrite the instance field
265 jstr = (*env)->NewStringUTF(env, info->topic);
271 (*env)->SetObjectField(env, callback_cont->rinfo, fid, jstr);
276 // Look for the instance field s in cls
277 fid = (*env)->GetFieldID(env,
280 "Ljava/lang/String;");
283 printf(":!c: can not find 'type' field ID...\n");
284 return; // failed to find the field
286 // Read the instance field s
287 jstr = (*env)->GetObjectField(env, obj, fid);
288 // Create a new string and overwrite the instance field
289 jstr = (*env)->NewStringUTF(env, info->type);
292 printf(":!c: can not set new value 'type'..\n");
296 (*env)->SetObjectField(env, callback_cont->rinfo, fid, jstr);
301 mid = (*env)->GetMethodID(env,
307 printf(":!c: can not find setSenderGuid() method...\n");
311 (*env)->CallVoidMethod(env,
312 callback_cont->rinfo,
314 (jlong)info->senderGUID.hid,
315 (jlong)info->senderGUID.aid,
316 (jlong)info->senderGUID.oid);
321 mid = (*env)->GetMethodID(env,
327 printf(":!c: can not find setLocalTimeRecv() method...\n");
331 (*env)->CallVoidMethod(env,
332 callback_cont->rinfo,
334 (jint) info->localTimeReceived.seconds,
335 (jlong)info->localTimeReceived.fraction);
339 //*** remoteTimePublished
341 mid = (*env)->GetMethodID(env,
347 printf(":!c: can not find setRemoteTimePub() method...\n");
351 (*env)->CallVoidMethod(env,
352 callback_cont->rinfo,
354 (jint) info->remoteTimePublished.seconds,
355 (jlong)info->remoteTimePublished.fraction);
358 //*** Sequence number
360 mid = (*env)->GetMethodID(env,
366 printf(":!c: can not find setSeqNumber() method...\n");
370 (*env)->CallVoidMethod(env,
371 callback_cont->rinfo,
377 printf(":c: recvStatus: %d \n", info->status);
378 printf(":c: senderGuid: hid = %d, aid = %d, oid = %d \n",
379 info->senderGUID.hid,info->senderGUID.aid,info->senderGUID.oid);
380 printf(":c: topic: %s \n",info->topic);
381 printf(":c: type: %s \n",info->type);
382 printf(":c: localTimeRecv: sec = %d, fract = %d \n",
383 info->localTimeReceived.seconds,info->localTimeReceived.fraction);
384 printf(":c: remoteTimePub: sec = %d, fract = %d \n",
385 info->remoteTimePublished.seconds,info->remoteTimePublished.fraction);
386 printf(":c: seqNr: high = %d, low = %d \n",info->sn.high,info->sn.low);
391 // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
393 obj_instance = (jobject)vinstance; //?? pujde to??
395 if(!obj_instance) printf(":!c: pretypovani: (jobject)vinstance = NULL..\n");
396 else printf(":c: pretypovani: (jobject)vinstance = %p OK ..\n", obj_instance);
399 // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
401 // -- vytvoreni parametru callback funkce ----------------------------------------end
403 // call object's method
405 printf(":c: volam callback metodu.. halo jsi tam?? \n\n");
407 (*env)->CallVoidMethod(env,
408 callback_cont->obj, /*obj*/
410 callback_cont->rinfo,
413 if((*jvm)->DetachCurrentThread(jvm)!=0) printf("DetachCurrentThread fault\n");
417 printf("-------------------- opoustim recvCallBack() z Ccka-------------------");
420 // --funkcni 05-01-13 ------------------------------------------------------------
425 // ### END recvCallBack() #####################################################
430 // ### nativni metoda #########################################################
432 JNIEXPORT jint JNICALL
433 Java_org_ocera_orte_Subscription_jORTESubscriptionCreate
434 (JNIEnv *env, jobject obj, jint dhandle, jint jsmode,
435 jint jstype, jstring jtopic, jstring jtname, jint jbufflength,
436 jobject jdeadline, jobject jminSeparation,
437 jobject callbackObj /* recvCallBackparam */, jlong j_multicastIP)
441 SubscriptionMode smode;
442 SubscriptionType stype;
444 const char *typename;
445 char *instance; // dynamicky naalokovat pole instance2send !!!!!
447 NtpTime minSeparation;
453 JORTECallbackContext_t *callback_cont = (JORTECallbackContext_t*)malloc(sizeof(JORTECallbackContext_t));
454 //RecvInfoVar_t *recvInfoVar = (RecvInfoVar_t*)malloc(sizeof(RecvInfoVar_t));
456 // bad/zero callbackObj
457 if (callbackObj == 0)
459 printf(":c!: callbackObj can not be null.. \n");
464 jint b = (*env)->GetJavaVM(env,&jvm);
467 printf(":!c: getJavaVM() fault..\n");
473 printf(":c: nativ fce: getJavaVM succesfull.. jvm = %p \n",jvm);
474 printf(":c: nativ fce: env = %p \n",env);
477 callback_cont->jvm = jvm;
479 // create global reference
480 callback_cont->obj = (*env)->NewGlobalRef(env, callbackObj);
481 // global reference not created
482 if (callback_cont->obj == 0)
484 printf(":!c: cannot create global reference obj_subsCallback ..\n");
488 // init RecvInfo pointer
489 callback_cont->rinfo = 0;
490 callback_cont->value = 0; // !!! JEN PRO TESTOVACI UCELY
493 printf(":c: global reference obj_subsCallback created [%p] ..\n", callback_cont->obj);
494 printf(":c: local reference callbackObj [%p] ..\n", callbackObj);
495 printf(":c: global reference RecvInfo [%p] ..\n", callback_cont->rinfo);
499 // nastavi handle na stukturu callback_cont
500 cls = (*env)->GetObjectClass(env, obj);
501 fid = (*env)->GetFieldID(env, cls, "callbackContextHandle", "I");
502 (*env)->SetIntField(env, obj, fid, (int)callback_cont);
504 // ---------------------------------------------------------------------------------------
505 //uz funkcni cast - bez recvCallbacku
507 printf(":c: chystam se vytvorit subscribera..\n");
508 d = (ORTEDomain *) dhandle;
511 printf(":!c: jORTESubscriptionCreate bad domain handle.. \n ");
515 smode = (SubscriptionMode) jsmode;
516 stype = (SubscriptionType) jstype;
517 topic = (*env)->GetStringUTFChars(env, jtopic, 0);
518 typename = (*env)->GetStringUTFChars(env, jtname, 0);
519 instance = (char *) malloc((int) jbufflength);
520 deadline = getNtpTime(env, jdeadline);//
521 minSeparation = getNtpTime(env, jminSeparation);//
525 printf(":c: jORTESubscriptionreate() - nacteny parametry z javy..\n");
528 ORTESubscriptionCreate(ORTEDomain *d,
529 SubscriptionMode mode,
530 SubscriptionType sType,
532 const char *typeName,
535 NtpTime *minimumSeparation,
536 ORTERecvCallBack recvCallBack,
537 void *recvCallBackParam,
538 IPAddress multicastIPAddress);
541 // call original liborte function
542 s = ORTESubscriptionCreate(d,
551 (void*)callback_cont,
552 (uint32_t) j_multicastIP);
554 printf(":c: provedeno volani ORTESubscriptionCreate()..\n");
557 printf(":!c: ORTESubscriptionCreate: bad publication handle.. \n ");
562 (*env)->ReleaseStringUTFChars(env, jtopic, topic);
563 (*env)->ReleaseStringUTFChars(env, jtname, typename);