Project

General

Profile

Statistics
| Revision:

root / branches / library_refactor / projects / libwireless / jni / lib / org_roboticsclub_colony_Wireless.c @ 1390

History | View | Annotate | Download (9.62 KB)

1
#include "org_roboticsclub_colony_Wireless.h"
2
#include "wireless.h"
3
#include <string.h>
4

    
5
/* Memory used to hold XBee port name */
6
char xbeeport[128];
7

    
8
/* Flag to track whether wl has been initialized */
9
static int _init = 0;
10

    
11
JavaVM *jvm;
12

    
13
#include "handlers.c"
14
jobject handlers[WL_MAX_PACKET_GROUPS] = {0};
15

    
16
void callTimeout(int num)
17
{
18
    jobject pgh = handlers[num];
19
    JNIEnv *env;
20
    int bAttached = 0;
21

    
22
    if (jvm==NULL) /* JVM wasn't set - there was an error */
23
        return;
24

    
25
    if ((*jvm)->GetEnv(jvm, (void **)&env, JNI_VERSION_1_1))
26
    {
27
        /* Aren't attached to a JVM, so try to attach */
28
        if ((*jvm)->AttachCurrentThread(jvm, (void **)&env, NULL))
29
            return; /* Error getting JNI Environment, can't continue */
30
        else
31
            bAttached = 1;
32
    }
33

    
34
    jclass cls = (*env)->GetObjectClass(env, pgh);
35
    jmethodID mid = (*env)->GetMethodID(env, cls, "timeout_handler", "()V");
36
    if (mid == NULL)
37
        return; /* method not found */
38
    (*env)->CallVoidMethod(env, pgh, mid);
39

    
40
    if (bAttached)
41
        (*jvm)->DetachCurrentThread(jvm);
42
}
43
void callHandleResponse(int num, int frame, int received)
44
{
45
    jobject pgh = handlers[num];
46
    JNIEnv *env;
47
    int bAttached = 0;
48

    
49
    if (jvm==NULL) /* JVM wasn't set - there was an error */
50
        return;
51

    
52
    if ((*jvm)->GetEnv(jvm, (void **)&env, JNI_VERSION_1_1))
53
    {
54
        /* Aren't attached to a JVM, so try to attach */
55
        if ((*jvm)->AttachCurrentThread(jvm, (void **)&env, NULL))
56
            return; /* Error getting JNI Environment, can't continue */
57
        else
58
            bAttached = 1;
59
    }
60

    
61
    jclass cls = (*env)->GetObjectClass(env, pgh);
62
    jmethodID mid = (*env)->GetMethodID(env, cls, "handle_response", "(II)V");
63
    if (mid == NULL)
64
        return; /* method not found */
65
    (*env)->CallVoidMethod(env, pgh, mid, (jint)frame, (jint)received);
66

    
67
    if (bAttached)
68
        (*jvm)->DetachCurrentThread(jvm);
69
}
70
void callHandleReceived(int num, char type, int source, unsigned char *packet, int length)
71
{
72
    jobject pgh = handlers[num];
73
    JNIEnv *env;
74
    int bAttached = 0;
75

    
76
    if (jvm==NULL) /* JVM wasn't set - there was an error */
77
        return;
78

    
79
    if ((*jvm)->GetEnv(jvm, (void **)&env, JNI_VERSION_1_1))
80
    {
81
        /* Aren't attached to a JVM, so try to attach */
82
        if ((*jvm)->AttachCurrentThread(jvm, (void **)&env, NULL))
83
            return; /* Error getting JNI Environment, can't continue */
84
        else
85
            bAttached = 1;
86
    }
87

    
88
    jclass cls = (*env)->GetObjectClass(env, pgh);
89
    jmethodID mid = (*env)->GetMethodID(env, cls, "handle_received", "(BI[B)V");
90
    if (mid == NULL)
91
        return; /* method not found */
92

    
93
    jbyteArray arr = (*env)->NewByteArray(env, length);
94
    (*env)->SetByteArrayRegion(env, arr, 0, length, (jbyte *)packet);
95

    
96
    (*env)->CallVoidMethod(env, pgh, mid, (jbyte)type, (jint)source, packet);
97

    
98
    if (bAttached)
99
        (*jvm)->DetachCurrentThread(jvm);
100
}
101
void callUnregister(int num)
102
{
103
    jobject pgh = handlers[num];
104
    JNIEnv *env;
105
    int bAttached = 0;
106

    
107
    if (jvm==NULL) /* JVM wasn't set - there was an error */
108
        return;
109

    
110
    if ((*jvm)->GetEnv(jvm, (void **)&env, JNI_VERSION_1_1))
111
    {
112
        /* Aren't attached to a JVM, so try to attach */
113
        if ((*jvm)->AttachCurrentThread(jvm, (void **)&env, NULL))
114
            return; /* Error getting JNI Environment, can't continue */
115
        else
116
            bAttached = 1;
117
    }
118

    
119
    jclass cls = (*env)->GetObjectClass(env, pgh);
120
    jmethodID mid = (*env)->GetMethodID(env, cls, "unregister", "()V");
121
    if (mid == NULL)
122
        return; /* method not found */
123
    (*env)->CallVoidMethod(env, pgh, mid);
124

    
125
    (*env)->DeleteGlobalRef(env, pgh);
126

    
127
    if (bAttached)
128
        (*jvm)->DetachCurrentThread(jvm);
129
}
130

    
131
/*
132
 * Class:     org_roboticsclub_colony_Wireless
133
 * Method:    wl_init
134
 * Signature: ()I
135
 */
136
JNIEXPORT jint JNICALL Java_org_roboticsclub_colony_Wireless_wl_1init
137
  (JNIEnv *env, jclass class)
138
{
139
    int ret;
140

    
141
    //init_pga();
142

    
143
    (*env)->GetJavaVM(env, &jvm);
144

    
145
    ret = wl_init();
146
    
147
    if (!ret)
148
      _init = 1;
149
    
150
    return ret;
151
}
152

    
153
/*
154
 * Class:     org_roboticsclub_colony_Wireless
155
 * Method:    wl_terminate
156
 * Signature: ()V
157
 */
158
JNIEXPORT void JNICALL Java_org_roboticsclub_colony_Wireless_wl_1terminate
159
  (JNIEnv *env, jclass class)
160
{
161
    if (_init)
162
        wl_terminate();
163
}
164

    
165
/*
166
 * Class:     org_roboticsclub_colony_Wireless
167
 * Method:    wl_do
168
 * Signature: ()V
169
 */
170
JNIEXPORT void JNICALL Java_org_roboticsclub_colony_Wireless_wl_1do
171
  (JNIEnv *env, jclass class)
172
{
173
    wl_do();
174
}
175

    
176
/*
177
 * Class:     org_roboticsclub_colony_Wireless
178
 * Method:    wl_register_packet_group
179
 * Signature: (Lorg/roboticsclub/colony/Wireless/PacketGroupHandler;)V
180
 */
181
JNIEXPORT void JNICALL Java_org_roboticsclub_colony_Wireless_wl_1register_1packet_1group
182
  (JNIEnv *env, jclass class, jobject pgh)
183
{
184
    jint groupId;
185
    jobject gPgh;
186

    
187
    jclass cls = (*env)->GetObjectClass(env, pgh);
188
    jmethodID mid = (*env)->GetMethodID(env, cls, "getGroupCode", "()I");
189
    if (mid == NULL)
190
        return; /* method not found */
191
    groupId = (*env)->CallIntMethod(env, pgh, mid);
192

    
193
    if (groupId > 15 || groupId < 0)
194
        return;
195

    
196
    gPgh = (*env)->NewGlobalRef(env, pgh);
197
    handlers[groupId] = gPgh;
198

    
199
    wl_register_packet_group(&packet_groups[groupId]);
200
}
201

    
202
/*
203
 * Class:     org_roboticsclub_colony_Wireless
204
 * Method:    wl_unregister_packet_group
205
 * Signature: (Lorg/roboticsclub/colony/Wireless/PacketGroupHandler;)V
206
 */
207
JNIEXPORT void JNICALL Java_org_roboticsclub_colony_Wireless_wl_1unregister_1packet_1group
208
  (JNIEnv *env, jclass class, jobject pgh)
209
{
210
    jint groupId;
211

    
212
    jclass cls = (*env)->GetObjectClass(env, pgh);
213
    jmethodID mid = (*env)->GetMethodID(env, cls, "getGroupCode", "()I");
214
    if (mid == NULL)
215
        return; /* method not found */
216
    groupId = (*env)->CallIntMethod(env, pgh, mid);
217

    
218
    if (groupId < 0 || groupId > 15)
219
        return;
220

    
221
    wl_unregister_packet_group(&packet_groups[groupId]);
222
}
223

    
224
/*
225
 * Class:     org_roboticsclub_colony_Wireless
226
 * Method:    wl_send_robot_to_robot_global_packet
227
 * Signature: (BB[BIB)I
228
 */
229
JNIEXPORT jint JNICALL Java_org_roboticsclub_colony_Wireless_wl_1send_1robot_1to_1robot_1global_1packet
230
  (JNIEnv *env, jclass class, jbyte group, jbyte type, jbyteArray data, jint dest, jbyte frame)
231
{
232
    jbyte buf[256];
233
    jsize length = (*env)->GetArrayLength(env, data);
234
    (*env)->GetByteArrayRegion(env, data, 0, 256, buf);
235
    return wl_send_robot_to_robot_global_packet(group, type, (char *)buf, length, (int)dest, frame);
236
}
237

    
238
/*
239
 * Class:     org_roboticsclub_colony_Wireless
240
 * Method:    wl_send_robot_to_robot_packet
241
 * Signature: (BB[BIB)I
242
 */
243
JNIEXPORT jint JNICALL Java_org_roboticsclub_colony_Wireless_wl_1send_1robot_1to_1robot_1packet
244
  (JNIEnv *env, jclass class, jbyte group, jbyte type, jbyteArray data, jint dest, jbyte frame)
245
{
246
    jbyte buf[256];
247
    jsize length = (*env)->GetArrayLength(env, data);
248
    (*env)->GetByteArrayRegion(env, data, 0, 256, buf);
249
    return wl_send_robot_to_robot_packet(group, type, (char *)buf, length, (int)dest, frame);
250
}
251

    
252
/*
253
 * Class:     org_roboticsclub_colony_Wireless
254
 * Method:    wl_send_global_packet
255
 * Signature: (BB[BB)I
256
 */
257
JNIEXPORT jint JNICALL Java_org_roboticsclub_colony_Wireless_wl_1send_1global_1packet
258
  (JNIEnv *env, jclass class, jbyte group, jbyte type, jbyteArray data, jbyte frame)
259
{
260
    jbyte buf[256];
261
    jsize length = (*env)->GetArrayLength(env, data);
262
    (*env)->GetByteArrayRegion(env, data, 0, 256, buf);
263
    return wl_send_global_packet(group, type, (char *)buf, length, frame);
264
}
265

    
266
/*
267
 * Class:     org_roboticsclub_colony_Wireless
268
 * Method:    wl_send_pan_packet
269
 * Signature: (BB[BB)V
270
 */
271
JNIEXPORT void JNICALL Java_org_roboticsclub_colony_Wireless_wl_1send_1pan_1packet
272
  (JNIEnv *env, jclass class, jbyte group, jbyte type, jbyteArray data, jbyte frame)
273
{
274
    jbyte buf[256];
275
    jsize length = (*env)->GetArrayLength(env, data);
276
    (*env)->GetByteArrayRegion(env, data, 0, 256, buf);
277
    wl_send_pan_packet(group, type, (char *)buf, length, frame);
278
}
279

    
280
/*
281
 * Class:     org_roboticsclub_colony_Wireless
282
 * Method:    wl_set_pan
283
 * Signature: (I)I
284
 */
285
JNIEXPORT jint JNICALL Java_org_roboticsclub_colony_Wireless_wl_1set_1pan
286
  (JNIEnv *env, jclass class, jint pan)
287
{
288
    return wl_set_pan((int)pan);
289
}
290

    
291
/*
292
 * Class:     org_roboticsclub_colony_Wireless
293
 * Method:    wl_get_pan
294
 * Signature: ()I
295
 */
296
JNIEXPORT jint JNICALL Java_org_roboticsclub_colony_Wireless_wl_1get_1pan
297
  (JNIEnv *env, jclass class)
298
{
299
    return wl_get_pan();
300
}
301

    
302
/*
303
 * Class:     org_roboticsclub_colony_Wireless
304
 * Method:    wl_set_channel
305
 * Signature: (I)I
306
 */
307
JNIEXPORT jint JNICALL Java_org_roboticsclub_colony_Wireless_wl_1set_1channel
308
  (JNIEnv *env, jclass class, jint channel)
309
{
310
    return wl_set_channel((int)channel);
311
}
312

    
313
/*
314
 * Class:     org_roboticsclub_colony_Wireless
315
 * Method:    wl_get_channel
316
 * Signature: ()I
317
 */
318
JNIEXPORT jint JNICALL Java_org_roboticsclub_colony_Wireless_wl_1get_1channel
319
  (JNIEnv *env, jclass class)
320
{
321
    return wl_get_channel();
322
}
323

    
324
/*
325
 * Class:     org_roboticsclub_colony_Wireless
326
 * Method:    wl_get_xbee_id
327
 * Signature: ()I
328
 */
329
JNIEXPORT jint JNICALL Java_org_roboticsclub_colony_Wireless_wl_1get_1xbee_1id
330
  (JNIEnv *env, jclass class)
331
{
332
    return wl_get_xbee_id();
333
}
334

    
335
/*
336
 * Class:     org_roboticsclub_colony_Wireless
337
 * Method:    wl_set_com_port
338
 * Signature: (Ljava/lang/String;)V
339
 */
340
JNIEXPORT void JNICALL Java_org_roboticsclub_colony_Wireless_wl_1set_1com_1port
341
  (JNIEnv *env, jclass class, jstring port)
342
{
343
    const jbyte *str = (*env)->GetStringUTFChars(env, port, NULL);
344
    if (str == NULL)
345
        return; /* OutOfMemoryError already thrown */
346

    
347
    strcpy(xbeeport, (const char *)str);
348

    
349
    (*env)->ReleaseStringUTFChars(env, port, str);
350

    
351
    wl_set_com_port(xbeeport);
352
}
353