void EIO_List(uv_work_t* req) { // This code exists in javascript for unix platforms #ifdef __APPLE__ if(!lockInitialised) { uv_mutex_init(&list_mutex); lockInitialised = TRUE; } ListBaton* data = static_cast<ListBaton*>(req->data); stDeviceListItem* devices = GetSerialDevices(); if (*(devices->length) > 0) { stDeviceListItem* next = devices; for (int i = 0, len = *(devices->length); i < len; i++) { stSerialDevice device = (* next).value; ListResultItem* resultItem = new ListResultItem(); resultItem->comName = device.port; if (device.locationId != NULL) { resultItem->locationId = device.locationId; } if (device.vendorId != NULL) { resultItem->vendorId = device.vendorId; } if (device.productId != NULL) { resultItem->productId = device.productId; } if (device.manufacturer != NULL) { resultItem->manufacturer = device.manufacturer; } if (device.serialNumber != NULL) { resultItem->serialNumber = device.serialNumber; } data->results.push_back(resultItem); stDeviceListItem* current = next; if (next->next != NULL) { next = next->next; } free(current); } } #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; }