1 /* JORTETypeRegisterAdd.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 // library header file's path
32 // pregenerated header
33 #include "jorte/org_ocera_orte_DomainApp.h"
35 #include "jorte/jorte_typedefs_defines.h"
36 #include "jorte/4all.h"
38 void set_order(CDR_Codec *cdrCodec, void *param) {
45 JORTESetEndiannessContext_t *ctx = (JORTESetEndiannessContext_t*) param;
47 //set byte order only once
48 if(cdrCodec->data_endian != ctx->cur_endian) {
50 (*ctx->jvm)->AttachCurrentThread(ctx->jvm, (void **)&env, NULL);
54 printf(":!c: env = NULL \n");
59 cls = (*env)->FindClass(env, "java/nio/ByteOrder");
62 printf(":!c: cls = NULL \n");
65 if(cdrCodec->data_endian == BigEndian) {
66 fid = (*env)->GetStaticFieldID(env,
69 "Ljava/nio/ByteOrder;");
70 ctx->cur_endian = BigEndian;
73 fid = (*env)->GetStaticFieldID(env,
76 "Ljava/nio/ByteOrder;");
77 ctx->cur_endian = LittleEndian;
81 printf(":!c: fid = NULL \n");
84 obj_bo = (*env)->GetStaticObjectField(env, cls, fid);
87 printf(":!c: cls = NULL \n");
91 // set byte order to ByteBuffer
93 cls = (*env)->GetObjectClass(env, ctx->obj_buf);
97 printf(":!c: cls = NULL \n");
100 // get methodID - order(ByteOrder)
101 mid = (*env)->GetMethodID(env,
104 "(Ljava/nio/ByteOrder;)Ljava/nio/ByteBuffer;");
108 printf(":!c: mid = NULL \n");
113 if((*env)->CallObjectMethod(env,ctx->obj_buf,mid,obj_bo) == 0)
116 printf(":!c: set byte order failed.. \n");
120 (*ctx->jvm)->DetachCurrentThread(ctx->jvm);
124 JNIEXPORT jint JNICALL
125 Java_org_ocera_orte_DomainApp_jORTETypeRegisterAdd
126 (JNIEnv *env, jclass cls, jlong handle, jstring jname, jlong jlength, jobject obj_bb)
131 JORTESetEndiannessContext_t *ctx = 0;
133 jobject byte_buf = 0;
137 ctx = (JORTESetEndiannessContext_t*) malloc(sizeof(JORTESetEndiannessContext_t));
140 printf(":c!: ctx = NULL\n");
143 // create global reference for ByteBuffer
144 // TODO delete global reference
145 byte_buf = (*env)->NewGlobalRef(env, obj_bb);
146 ctx->obj_buf = byte_buf;
148 b = (*env)->GetJavaVM(env,&jvm);
151 printf(":!c: getJavaVM() failed! \n");
155 ctx->cur_endian = FLAG_ENDIANNESS;
157 // get type name from JAVA env
158 name = (*env)->GetStringUTFChars(env,jname,0);
159 // call ORTE function
160 b = ORTETypeRegisterAdd((ORTEDomain *) handle,
165 (unsigned int) jlength,
166 (ORTETypeProcessEndianness) set_order,
168 // free memmory space
169 (*env)->ReleaseStringUTFChars(env,jname,name);
172 printf(":c: jORTETypeRegisterAdd vraci %d [%d = ORTE_OK, %d = ORTE_BAD_HANDLE] \n",
173 b,ORTE_OK,ORTE_BAD_HANDLE);