]> rtime.felk.cvut.cz Git - orte/eurobot.git/blob - orte/libjorte/JORTETypeRegisterAdd.c
JORTE: add deserialize function to support endianness setting
[orte/eurobot.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 <jni.h>
29 #include <string.h>
30 // library header file's path
31 #include "orte.h"
32 // pregenerated header
33 #include "jorte/org_ocera_orte_DomainApp.h"
34 #include "jorte/jorte_protos_api.h"
35 #include "jorte/4all.h"
36
37 extern JavaVM *javavm;
38 static jobject byte_buf;
39
40 void deserialize(CDR_Codec *cdrCodec, void *instance) {
41   JNIEnv        *env = 0;
42   jclass         cls = 0;
43   jfieldID       fid = 0;
44   jmethodID      mid = 0;
45   jobject        obj_bo = 0;
46
47   //set byte order only once
48   if(byte_buf) {
49     // get environment
50     (*javavm)->AttachCurrentThread(javavm, (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     }
71     else {
72       fid = (*env)->GetStaticFieldID(env,
73                                          cls,
74                                          "LITTLE_ENDIAN",
75                                          "Ljava/nio/ByteOrder;");
76     }
77     if(fid == 0) {
78       #ifdef TEST_STAGE
79             printf(":!c: fid = NULL \n");
80           #endif
81     }
82     obj_bo = (*env)->GetStaticObjectField(env, cls, fid);
83     if(obj_bo == 0) {
84       #ifdef TEST_STAGE
85             printf(":!c: cls = NULL \n");
86           #endif
87     }
88
89     // set byte order to ByteBuffer
90     // get BB class
91     cls = (*env)->GetObjectClass(env, byte_buf);
92     if(cls == 0)
93     {
94       #ifdef TEST_STAGE
95         printf(":!c: cls = NULL \n");
96       #endif
97     }
98     // get methodID - order(ByteOrder)
99     mid = (*env)->GetMethodID(env,
100                              cls,
101                              "order",
102                              "(Ljava/nio/ByteOrder;)Ljava/nio/ByteBuffer;");
103     if(mid == 0)
104     {
105       #ifdef TEST_STAGE
106         printf(":!c: mid = NULL \n");
107       #endif
108     }
109
110     // set ByteOrder
111     if((*env)->CallObjectMethod(env,byte_buf,mid,obj_bo) == 0)
112     {
113       #ifdef TEST_STAGE
114         printf(":!c: set byte order failed.. \n");
115       #endif
116     }
117
118     // delete global reference
119     (*env)->DeleteGlobalRef(env, byte_buf);
120     byte_buf = 0;
121
122     (*javavm)->DetachCurrentThread(javavm);
123   }
124
125   //copy over the message instance
126   memcpy(instance,
127          cdrCodec->buffer,
128          cdrCodec->buf_len);
129 }
130
131 JNIEXPORT jint JNICALL
132 Java_org_ocera_orte_DomainApp_jORTETypeRegisterAdd
133 (JNIEnv *env, jclass cls, jlong handle, jstring jname, jlong jlength, jobject obj_bb)
134 {
135   const char     *name;
136   int            b;
137
138   // get type name from JAVA env
139   name = (*env)->GetStringUTFChars(env,jname,0);
140   // call ORTE function
141   b = ORTETypeRegisterAdd((ORTEDomain *) handle,
142                           name,
143                           NULL,
144                           (ORTETypeDeserialize)deserialize,
145                           NULL,
146                           (unsigned int) jlength);
147   // free memmory space
148   (*env)->ReleaseStringUTFChars(env,jname,name);
149
150   byte_buf = (*env)->NewGlobalRef(env, obj_bb);
151
152   #ifdef TEST_STAGE
153   printf(":c: jORTETypeRegisterAdd vraci %d [%d = ORTE_OK, %d = ORTE_BAD_HANDLE] \n",
154          b,ORTE_OK,ORTE_BAD_HANDLE);
155   #endif
156   return b;
157
158 }