]> rtime.felk.cvut.cz Git - orte.git/blob - orte/libjorte/JORTETypeRegisterAdd.c
JORTE: switch from deserialize function to endianness callback
[orte.git] / orte / libjorte / JORTETypeRegisterAdd.c
1 /* JORTETypeRegisterAdd.c */
2
3 /**
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')
8   *
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
13   * @version 0.1
14   *
15   *
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.
20   *
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.
25   *
26   */
27
28 #include <stdlib.h>
29 #include <jni.h>
30 // library header file's path
31 #include "orte_all.h"
32 // pregenerated header
33 #include "jorte/org_ocera_orte_DomainApp.h"
34
35 #include "jorte/jorte_typedefs_defines.h"
36 #include "jorte/4all.h"
37
38 void set_order(CDR_Codec *cdrCodec, void *param) {
39   JNIEnv        *env = 0;
40   jclass         cls = 0;
41   jfieldID       fid = 0;
42   jmethodID      mid = 0;
43   jobject        obj_bo = 0;
44   
45   JORTESetEndiannessContext_t *ctx = (JORTESetEndiannessContext_t*) param;
46
47   //set byte order only once
48   if(cdrCodec->data_endian != ctx->cur_endian) {
49     // get environment
50     (*ctx->jvm)->AttachCurrentThread(ctx->jvm, (void **)&env, NULL);
51     if(env == 0)
52     {
53       #ifdef TEST_STAGE
54        printf(":!c: env = NULL \n");
55       #endif
56     }
57
58     //prepare ByteOrder
59     cls = (*env)->FindClass(env, "java/nio/ByteOrder");
60     if (cls == 0) {
61       #ifdef TEST_STAGE
62         printf(":!c: cls = NULL \n");
63       #endif
64     }
65     if(cdrCodec->data_endian == BigEndian) {
66       fid = (*env)->GetStaticFieldID(env,
67                                          cls,
68                                          "BIG_ENDIAN",
69                                          "Ljava/nio/ByteOrder;");
70       ctx->cur_endian = BigEndian;
71     }
72     else {
73       fid = (*env)->GetStaticFieldID(env,
74                                          cls,
75                                          "LITTLE_ENDIAN",
76                                          "Ljava/nio/ByteOrder;");
77       ctx->cur_endian = LittleEndian;
78     }
79     if(fid == 0) {
80       #ifdef TEST_STAGE
81             printf(":!c: fid = NULL \n");
82           #endif
83     }
84     obj_bo = (*env)->GetStaticObjectField(env, cls, fid);
85     if(obj_bo == 0) {
86       #ifdef TEST_STAGE
87             printf(":!c: cls = NULL \n");
88           #endif
89     }
90
91     // set byte order to ByteBuffer
92     // get BB class
93     cls = (*env)->GetObjectClass(env, ctx->obj_buf);
94     if(cls == 0)
95     {
96       #ifdef TEST_STAGE
97         printf(":!c: cls = NULL \n");
98       #endif
99     }
100     // get methodID - order(ByteOrder)
101     mid = (*env)->GetMethodID(env,
102                              cls,
103                              "order",
104                              "(Ljava/nio/ByteOrder;)Ljava/nio/ByteBuffer;");
105     if(mid == 0)
106     {
107       #ifdef TEST_STAGE
108         printf(":!c: mid = NULL \n");
109       #endif
110     }
111
112     // set ByteOrder
113     if((*env)->CallObjectMethod(env,ctx->obj_buf,mid,obj_bo) == 0)
114     {
115       #ifdef TEST_STAGE
116         printf(":!c: set byte order failed.. \n");
117       #endif
118     }
119
120     (*ctx->jvm)->DetachCurrentThread(ctx->jvm);
121   }
122 }
123
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)
127 {
128   const char     *name;
129   int            b;
130
131   JORTESetEndiannessContext_t *ctx = 0;
132   
133   jobject        byte_buf = 0;
134   JavaVM         *jvm = 0;
135
136   // TODO free() !
137   ctx = (JORTESetEndiannessContext_t*) malloc(sizeof(JORTESetEndiannessContext_t));
138   if(ctx == 0) {
139     #ifdef TEST_STAGE
140       printf(":c!: ctx = NULL\n");
141     #endif
142   }
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;
147   // get jvm
148   b = (*env)->GetJavaVM(env,&jvm);
149   if (b < 0)
150   {
151     printf(":!c: getJavaVM() failed! \n");
152     return 0;
153   }
154   ctx->jvm = jvm;
155   ctx->cur_endian = FLAG_ENDIANNESS;
156
157   // get type name from JAVA env
158   name = (*env)->GetStringUTFChars(env,jname,0);
159   // call ORTE function
160   b = ORTETypeRegisterAdd((ORTEDomain *) handle,
161                           name,
162                           NULL,
163                           NULL,
164                           NULL,
165                           (unsigned int) jlength,
166                           (ORTETypeProcessEndianness) set_order,
167                           (void*) ctx);
168   // free memmory space
169   (*env)->ReleaseStringUTFChars(env,jname,name);
170
171   #ifdef TEST_STAGE
172   printf(":c: jORTETypeRegisterAdd vraci %d [%d = ORTE_OK, %d = ORTE_BAD_HANDLE] \n",
173          b,ORTE_OK,ORTE_BAD_HANDLE);
174   #endif
175   return b;
176
177 }