-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlistCom_java.cpp
66 lines (45 loc) · 1.66 KB
/
listCom_java.cpp
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
//
// listCom.cpp
// listCom
//
// Created by Angelo Scialabba on 11/13/13.
//
//
#include "listCom_java.h"
#ifdef WIN
#include "src_win/listCom.c"
#elif defined(MAC)
#include "src_mac/listCom.c"
#else
#warning No platform specified
#endif
JNIEXPORT jobjectArray JNICALL Java_cc_arduino_packages_discoverers_SerialLister_serialPortList(JNIEnv *env, jobject obj){
jclass mapClass = env->FindClass("java/util/HashMap");
jmethodID mapCtor = env->GetMethodID(mapClass,"<init>","(I)V");
jmethodID put = env->GetMethodID(mapClass,"put","(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");
int len,index;
stDeviceListItem* devices,*temp;
len = 0;
devices = GetSerialDevices();
if (devices == NULL) return NULL;
temp = devices;
len = *(devices->length);
devices = temp;
index = 0;
//create the hash map
jobjectArray devicesArray = env->NewObjectArray(len, env->FindClass("java/util/HashMap"),0);
//populate the hash map
while(devices!=NULL){
jobject mapObj = env->NewObject(mapClass,mapCtor,3);
env->CallObjectMethod(mapObj, put,env->NewStringUTF("port"), env->NewStringUTF(devices->value.port));
env->CallObjectMethod(mapObj, put,env->NewStringUTF("vendorId"), env->NewStringUTF(devices->value.vendorId));
env->CallObjectMethod(mapObj, put,env->NewStringUTF("productId"), env->NewStringUTF(devices->value.productId));
temp = devices;
devices = devices->next;
free(temp);
env->SetObjectArrayElement(devicesArray, index, mapObj);
env->DeleteLocalRef(mapObj);
index++;
}
return devicesArray;
}