-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathlibmqtt_jni.c
510 lines (418 loc) · 14.2 KB
/
libmqtt_jni.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
/*
* Copyright GoIIoT (https://github.com/goiiot)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "cc_goiiot_libmqtt_LibMQTT.h"
#include "libmqtt.h"
static JavaVM *jvm;
static jclass libmqtt_class;
static jmethodID on_conn_msg_mid;
static jmethodID on_sub_msg_mid;
static jmethodID on_pub_msg_mid;
static jmethodID on_unsub_msg_mid;
static jmethodID on_net_msg_mid;
static jmethodID on_persist_err_mid;
static jmethodID on_topic_msg_mid;
void conn_handler(int client, char *server, libmqtt_connack_t code, char *err) {
JNIEnv *g_env;
(*jvm)->AttachCurrentThread(jvm, (void **)&g_env, NULL);
if (g_env == NULL || libmqtt_class == NULL || on_conn_msg_mid == 0) {
return;
} else {
(*g_env)->CallStaticVoidMethod(g_env, libmqtt_class,
on_conn_msg_mid, client,
0,
(*g_env)->NewStringUTF(g_env, err));
}
}
void sub_handler(int client, char *topic, int qos, char *err) {
JNIEnv *g_env;
(*jvm)->AttachCurrentThread(jvm, (void **)&g_env, NULL);
if (g_env == NULL || libmqtt_class == NULL || on_sub_msg_mid == 0) {
return;
} else {
(*g_env)->CallStaticVoidMethod(g_env, libmqtt_class,
on_sub_msg_mid, client,
(*g_env)->NewStringUTF(g_env, topic),
qos,
(*g_env)->NewStringUTF(g_env, err));
}
}
void pub_handler(int client, char *topic, char *err) {
JNIEnv *g_env;
(*jvm)->AttachCurrentThread(jvm, (void **)&g_env, NULL);
if (g_env == NULL || libmqtt_class == NULL || on_pub_msg_mid == 0) {
return;
} else {
(*g_env)->CallStaticVoidMethod(g_env, libmqtt_class,
on_pub_msg_mid, client,
(*g_env)->NewStringUTF(g_env, topic),
(*g_env)->NewStringUTF(g_env, err));
}
}
void unsub_handler(int client, char *topic, char *err) {
JNIEnv *g_env;
(*jvm)->AttachCurrentThread(jvm, (void **)&g_env, NULL);
if (g_env == NULL || libmqtt_class == NULL || on_unsub_msg_mid == 0) {
return;
} else {
(*g_env)->CallStaticVoidMethod(g_env, libmqtt_class,
on_unsub_msg_mid, client,
(*g_env)->NewStringUTF(g_env, topic),
(*g_env)->NewStringUTF(g_env, err));
}
}
void net_handler(int client, char *server, char *err) {
JNIEnv *g_env;
(*jvm)->AttachCurrentThread(jvm, (void **)&g_env, NULL);
if (g_env == NULL || libmqtt_class == NULL || on_net_msg_mid == 0) {
return;
} else {
(*g_env)->CallStaticVoidMethod(g_env, libmqtt_class,
on_net_msg_mid, client,
(*g_env)->NewStringUTF(g_env, err));
}
}
void persist_handler(int client, char *err) {
JNIEnv *g_env;
(*jvm)->AttachCurrentThread(jvm, (void **)&g_env, NULL);
if (g_env == NULL || libmqtt_class == NULL || on_topic_msg_mid == 0) {
return;
} else {
(*g_env)->CallStaticVoidMethod(g_env, libmqtt_class,
on_persist_err_mid, client,
(*g_env)->NewStringUTF(g_env, err));
}
}
void topic_handler(int client, char *topic, int qos, char *payload, int size) {
JNIEnv *g_env;
(*jvm)->AttachCurrentThread(jvm, (void **)&g_env, NULL);
if (g_env == NULL || libmqtt_class == NULL || on_topic_msg_mid == 0) {
return;
} else {
jbyteArray result = (*g_env)->NewByteArray(g_env, size);
(*g_env)->SetByteArrayRegion(g_env, result, 0, size, (jbyte *)payload);
(*g_env)->CallStaticVoidMethod(g_env, libmqtt_class,
on_topic_msg_mid, client,
(*g_env)->NewStringUTF(g_env, topic),
qos, result);
}
}
/*
* Class: cc_goiiot_libmqtt_LibMQTT
* Method: _init
* Signature: ()V
*/
JNIEXPORT void JNICALL
Java_cc_goiiot_libmqtt_LibMQTT__1init
(JNIEnv *env, jclass c) {
(*env)->GetJavaVM(env, &jvm);
libmqtt_class = (jclass) (*env)->NewGlobalRef(env, (*env)->FindClass(env, "cc/goiiot/libmqtt/LibMQTT"));;
on_conn_msg_mid = (*env)->GetStaticMethodID(env, c,
"onConnMessage", "(IILjava/lang/String;)V");
on_net_msg_mid = (*env)->GetStaticMethodID(env, c,
"onNetMessage", "(ILjava/lang/String;)V");
on_pub_msg_mid = (*env)->GetStaticMethodID(env, c,
"onPubMessage", "(ILjava/lang/String;Ljava/lang/String;)V");
on_sub_msg_mid = (*env)->GetStaticMethodID(env, c,
"onSubMessage", "(ILjava/lang/String;ILjava/lang/String;)V");
on_unsub_msg_mid = (*env)->GetStaticMethodID(env, c,
"onUnsubMessage", "(ILjava/lang/String;Ljava/lang/String;)V");
on_persist_err_mid = (*env)->GetStaticMethodID(env, c,
"onPersistError", "(ILjava/lang/String;)V");
on_topic_msg_mid = (*env)->GetStaticMethodID(env, c,
"onTopicMessage", "(ILjava/lang/String;I[B)V");
}
/*
* Class: cc_goiiot_libmqtt_LibMQTT
* Method: _newClient
* Signature: ()I
*/
JNIEXPORT jint JNICALL
Java_cc_goiiot_libmqtt_LibMQTT__1newClient
(JNIEnv *env, jclass c) {
return Libmqtt_new_client();
}
/*
* Class: cc_goiiot_libmqtt_LibMQTT
* Method: _setServer
* Signature: (ILjava/lang/String;)V
*/
JNIEXPORT void JNICALL
Java_cc_goiiot_libmqtt_LibMQTT__1setServer
(JNIEnv *env, jclass c, jint id, jstring server) {
const char *srv = (*env)->GetStringUTFChars(env, server, 0);
Libmqtt_client_set_server(id, srv);
(*env)->ReleaseStringUTFChars(env, server, srv);
}
/*
* Class: cc_goiiot_libmqtt_LibMQTT
* Method: _setCleanSession
* Signature: (IZ)V
*/
JNIEXPORT void JNICALL
Java_cc_goiiot_libmqtt_LibMQTT__1setCleanSession
(JNIEnv *env, jclass c, jint id, jboolean flag) {
Libmqtt_client_set_clean_session(id, flag);
}
/*
* Class: cc_goiiot_libmqtt_LibMQTT
* Method: _setKeepalive
* Signature: (IID)V
*/
JNIEXPORT void JNICALL
Java_cc_goiiot_libmqtt_LibMQTT__1setKeepalive
(JNIEnv *env, jclass c, jint id, jint keepalive, jdouble factor) {
Libmqtt_client_set_keepalive(id, keepalive, factor);
}
/*
* Class: cc_goiiot_libmqtt_LibMQTT
* Method: _setClientID
* Signature: (ILjava/lang/String;)V
*/
JNIEXPORT void JNICALL
Java_cc_goiiot_libmqtt_LibMQTT__1setClientID
(JNIEnv *env, jclass c, jint id, jstring client_id) {
const char *cid = (*env)->GetStringUTFChars(env, client_id, 0);
Libmqtt_client_set_client_id(id, cid);
(*env)->ReleaseStringUTFChars(env, client_id, cid);
}
/*
* Class: cc_goiiot_libmqtt_LibMQTT
* Method: _setDialTimeout
* Signature: (II)V
*/
JNIEXPORT void JNICALL
Java_cc_goiiot_libmqtt_LibMQTT__1setDialTimeout
(JNIEnv *env, jclass c, jint id, jint timeout) {
Libmqtt_client_set_dial_timeout(id, timeout);
}
/*
* Class: cc_goiiot_libmqtt_LibMQTT
* Method: _setIdentity
* Signature: (ILjava/lang/String;Ljava/lang/String;)V
*/
JNIEXPORT void JNICALL
Java_cc_goiiot_libmqtt_LibMQTT__1setIdentity
(JNIEnv *env, jclass c, jint id, jstring username, jstring password) {
const char *user = (*env)->GetStringUTFChars(env, username, 0);
const char *pass = (*env)->GetStringUTFChars(env, password, 0);
Libmqtt_client_set_identity(id, user, pass);
(*env)->ReleaseStringUTFChars(env, username, user);
(*env)->ReleaseStringUTFChars(env, password, pass);
}
/*
* Class: cc_goiiot_libmqtt_LibMQTT
* Method: _setLog
* Signature: (II)V
*/
JNIEXPORT void JNICALL
Java_cc_goiiot_libmqtt_LibMQTT__1setLog
(JNIEnv *env, jclass c, jint id, jint log_level) {
Libmqtt_client_set_log(id, log_level);
}
/*
* Class: cc_goiiot_libmqtt_LibMQTT
* Method: _setSendBuf
* Signature: (II)V
*/
JNIEXPORT void JNICALL
Java_cc_goiiot_libmqtt_LibMQTT__1setSendBuf
(JNIEnv *env, jclass c, jint id, jint size) {
Libmqtt_client_set_send_buf(id, size);
}
/*
* Class: cc_goiiot_libmqtt_LibMQTT
* Method: _setRecvBuf
* Signature: (II)V
*/
JNIEXPORT void JNICALL
Java_cc_goiiot_libmqtt_LibMQTT__1setRecvBuf
(JNIEnv *env, jclass c, jint id, jint size) {
Libmqtt_client_set_send_buf(id, size);
}
/*
* Class: cc_goiiot_libmqtt_LibMQTT
* Method: _setTLS
* Signature: (ILjava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Z)V
*/
JNIEXPORT void JNICALL
Java_cc_goiiot_libmqtt_LibMQTT__1setTLS
(JNIEnv *env, jclass c, jint id, jstring cert,
jstring key, jstring ca, jstring srv_name, jboolean skip_verify) {
const char *c_cert = (*env)->GetStringUTFChars(env, cert, 0);
const char *c_key = (*env)->GetStringUTFChars(env, key, 0);
const char *c_ca = (*env)->GetStringUTFChars(env, ca, 0);
const char *c_srv = (*env)->GetStringUTFChars(env, srv_name, 0);
Libmqtt_client_set_tls(id, c_cert, c_key, c_ca, c_srv, skip_verify);
(*env)->ReleaseStringUTFChars(env, cert, c_cert);
(*env)->ReleaseStringUTFChars(env, key, c_key);
(*env)->ReleaseStringUTFChars(env, ca, c_ca);
(*env)->ReleaseStringUTFChars(env, srv_name, c_srv);
}
/*
* Class: cc_goiiot_libmqtt_LibMQTT
* Method: _setWill
* Signature: (ILjava/lang/String;IZ[B)V
*/
JNIEXPORT void JNICALL
Java_cc_goiiot_libmqtt_LibMQTT__1setWill
(JNIEnv *env, jclass c, jint id, jstring topic,
jint qos, jboolean retain, jbyteArray payload) {
const char *c_topic = (*env)->GetStringUTFChars(env, topic, 0);
jsize len = (*env)->GetArrayLength(env, payload);
jbyte *body = (*env)->GetByteArrayElements(env, payload, 0);
Libmqtt_client_set_will(id, c_topic, qos, retain, body, len);
(*env)->ReleaseStringUTFChars(env, topic, c_topic);
(*env)->ReleaseByteArrayElements(env, payload, body, 0);
}
/*
* Class: cc_goiiot_libmqtt_LibMQTT
* Method: _setNonePersist
* Signature: (I)V
*/
JNIEXPORT void JNICALL
Java_cc_goiiot_libmqtt_LibMQTT__1setNonePersist
(JNIEnv *env, jclass c, jint id) {
Libmqtt_client_set_none_persist(id);
}
/*
* Class: cc_goiiot_libmqtt_LibMQTT
* Method: _setMemPersist
* Signature: (IIZZ)V
*/
JNIEXPORT void JNICALL
Java_cc_goiiot_libmqtt_LibMQTT__1setMemPersist
(JNIEnv *env, jclass c, jint id, jint max_count,
jboolean ex_drop, jboolean dup_replace) {
Libmqtt_client_set_mem_persist(id, max_count, ex_drop, dup_replace);
}
/*
* Class: cc_goiiot_libmqtt_LibMQTT
* Method: _setFilePersist
* Signature: (ILjava/lang/String;IZZ)V
*/
JNIEXPORT void JNICALL
Java_cc_goiiot_libmqtt_LibMQTT__1setFilePersist
(JNIEnv *env, jclass c, jint id, jstring dir,
jint max_count, jboolean ex_drop, jboolean dup_replace) {
const char *c_dir = (*env)->GetStringUTFChars(env, dir, 0);
Libmqtt_client_set_file_persist(id, c_dir, max_count, ex_drop, dup_replace);
(*env)->ReleaseStringUTFChars(env, dir, c_dir);
}
/*
* Class: cc_goiiot_libmqtt_LibMQTT
* Method: _setup
* Signature: (I)Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL
Java_cc_goiiot_libmqtt_LibMQTT__1setup
(JNIEnv *env, jclass c, jint id) {
char *err = Libmqtt_setup(id);
if (err != NULL) {
return (*env)->NewStringUTF(env, err);
}
Libmqtt_set_pub_handler(id, &pub_handler);
Libmqtt_set_sub_handler(id, &sub_handler);
Libmqtt_set_net_handler(id, &net_handler);
Libmqtt_set_unsub_handler(id, &unsub_handler);
Libmqtt_set_persist_handler(id, &persist_handler);
return NULL;
}
/*
* Class: cc_goiiot_libmqtt_LibMQTT
* Method: _handle
* Signature: (ILjava/lang/String;Lcc/goiiot/libmqtt/LibMQTT/TopicMessageCallback;)V
*/
JNIEXPORT void JNICALL
Java_cc_goiiot_libmqtt_LibMQTT__1handle
(JNIEnv *env, jclass c, jint id, jstring topic, jobject callback) {
if (callback == NULL) {
return;
}
const char *c_topic = (*env)->GetStringUTFChars(env, topic, 0);
Libmqtt_handle(id, c_topic, &topic_handler);
(*env)->ReleaseStringUTFChars(env, topic, c_topic);
}
/*
* Class: cc_goiiot_libmqtt_LibMQTT
* Method: _conn
* Signature: (I)V
*/
JNIEXPORT void JNICALL
Java_cc_goiiot_libmqtt_LibMQTT__1connect
(JNIEnv *env, jclass c, jint id) {
Libmqtt_connect(id, &conn_handler);
}
/*
* Class: cc_goiiot_libmqtt_LibMQTT
* Method: _wait
* Signature: (I)V
*/
JNIEXPORT void JNICALL
Java_cc_goiiot_libmqtt_LibMQTT__1wait
(JNIEnv *env, jclass c, jint id) {
(*env)->MonitorEnter(env, c);
Libmqtt_wait(id);
(*env)->MonitorExit(env, c);
}
/*
* Class: cc_goiiot_libmqtt_LibMQTT
* Method: _pub
* Signature: (ILjava/lang/String;I[B)V
*/
JNIEXPORT void JNICALL
Java_cc_goiiot_libmqtt_LibMQTT__1pub
(JNIEnv *env, jclass c, jint id, jstring topic, jint qos, jbyteArray payload) {
const char *c_topic = (*env)->GetStringUTFChars(env, topic, 0);
jsize len = (*env)->GetArrayLength(env, payload);
jbyte *body = (*env)->GetByteArrayElements(env, payload, 0);
Libmqtt_publish(id, c_topic, qos, body, len);
(*env)->ReleaseStringUTFChars(env, topic, c_topic);
(*env)->ReleaseByteArrayElements(env, payload, body, 0);
}
/*
* Class: cc_goiiot_libmqtt_LibMQTT
* Method: _sub
* Signature: (ILjava/lang/String;I)V
*/
JNIEXPORT void JNICALL
Java_cc_goiiot_libmqtt_LibMQTT__1sub
(JNIEnv *env, jclass c, jint id, jstring topic, jint qos) {
const char *c_topic = (*env)->GetStringUTFChars(env, topic, 0);
Libmqtt_subscribe(id, c_topic, qos);
(*env)->ReleaseStringUTFChars(env, topic, c_topic);
}
/*
* Class: cc_goiiot_libmqtt_LibMQTT
* Method: _unsub
* Signature: (ILjava/lang/String;)V
*/
JNIEXPORT void JNICALL
Java_cc_goiiot_libmqtt_LibMQTT__1unsub
(JNIEnv *env, jclass c, jint id, jstring topic) {
const char *c_topic = (*env)->GetStringUTFChars(env, topic, 0);
Libmqtt_unsubscribe(id, c_topic);
(*env)->ReleaseStringUTFChars(env, topic, c_topic);
}
/*
* Class: cc_goiiot_libmqtt_LibMQTT
* Method: _destroy
* Signature: (IZ)V
*/
JNIEXPORT void JNICALL
Java_cc_goiiot_libmqtt_LibMQTT__1destroy
(JNIEnv *env, jclass c, jint id, jboolean force) {
Libmqtt_destroy(id, force);
}