void populateServiceRecord(JNIEnv *env, jobject serviceRecord, sdp_record_t* sdpRecord, sdp_list_t* attributeList) { jclass serviceRecordImplClass = (*env)->GetObjectClass(env, serviceRecord); debug("populateServiceRecord"); jmethodID populateAttributeValueID = getGetMethodID(env, serviceRecordImplClass, "populateAttributeValue", "(ILjavax/bluetooth/DataElement;)V"); if (populateAttributeValueID == NULL) { return; } int attrCount = 0; for(; attributeList; attributeList = attributeList->next) { jint attributeID=*(uint16_t*)attributeList->data; sdp_data_t *data = sdp_data_get(sdpRecord, (uint16_t)attributeID); if (data) { jobject dataElement = createDataElement(env, data); if ((*env)->ExceptionCheck(env)) { break; } if (dataElement == NULL) { break; } (*env)->CallVoidMethod(env, serviceRecord, populateAttributeValueID, attributeID, dataElement); if ((*env)->ExceptionCheck(env)) { break; } attrCount ++; } } Edebug("attrCount %i", attrCount); }
jobject createJavaUUID(JNIEnv *env, uuid_t uuid) { jboolean shortUUID = true; const int strSize = 32; char uuidChars[strSize + 1]; switch (uuid.type) { case SDP_UUID16: snprintf(uuidChars, strSize, "%.4x", uuid.value.uuid16); break; case SDP_UUID32: snprintf(uuidChars, strSize, "%.8x", uuid.value.uuid32); break; case SDP_UUID128: { shortUUID = false; int j = 0; int i; for(i = 0; i < 16; i++) { uuidChars[j++] = b2hex((uuid.value.uuid128.data[i] >> 4) & 0xf); uuidChars[j++] = b2hex(uuid.value.uuid128.data[i] & 0xf); } uuidChars[j] = 0; break; } default: return NULL; } jstring uuidString = (*env)->NewStringUTF(env, uuidChars); jclass uuidClass = (*env)->FindClass(env, "javax/bluetooth/UUID"); jmethodID constructorID = getGetMethodID(env, uuidClass, "<init>", "(Ljava/lang/String;Z)V"); if (constructorID == NULL) { return NULL; } return (*env)->NewObject(env, uuidClass, constructorID, uuidString, shortUUID); }
bool isCurrentThreadInterrupted(JNIEnv *env, jobject peer) { jclass peerClass = (*env)->GetObjectClass(env, peer); jmethodID aMethod = getGetMethodID(env, peerClass, "isCurrentThreadInterruptedCallback", "()Z"); if (aMethod == NULL) { return true; } if ((*env)->CallBooleanMethod(env, peer, aMethod)) { throwInterruptedIOException(env, "thread interrupted"); return true; } return (*env)->ExceptionCheck(env); }
JNIEXPORT jint JNICALL Java_com_intel_bluetooth_BluetoothStackBlueZ_runSearchServicesImpl (JNIEnv *env, jobject peer, jobject searchServicesThread, jlong localDeviceBTAddress, jobjectArray uuidValues, jlong remoteDeviceAddressLong) { // Prepare serviceDiscoveredCallback jclass peerClass = (*env)->GetObjectClass(env, peer); if (peerClass == NULL) { throwRuntimeException(env, "Fail to get Object Class"); return SERVICE_SEARCH_ERROR; } jmethodID serviceDiscoveredCallback = getGetMethodID(env, peerClass, "serviceDiscoveredCallback", "(Lcom/intel/bluetooth/SearchServicesThread;JJ)Z"); if (serviceDiscoveredCallback == NULL) { return SERVICE_SEARCH_ERROR; } sdp_list_t *uuidList = NULL; sdp_list_t *rsp_list = NULL; sdp_session_t *session = NULL; jint rc = SERVICE_SEARCH_ERROR; const uint16_t max_rec_num = 256; int serviceCount = 0; int error; // convert uuid set from java array to bluez sdp_list_t jsize uuidSetSize = (*env)->GetArrayLength(env, uuidValues); jsize i; debug("runSearchServicesImpl uuidSetSize %i", uuidSetSize); for(i = 0; i < uuidSetSize; i++) { jbyteArray byteArray = (jbyteArray)(*env)->GetObjectArrayElement(env, uuidValues, i); uuid_t* uuid = (uuid_t*)malloc(sizeof(uuid_t)); convertUUIDByteArrayToUUID(env, byteArray, uuid); uuidList = sdp_list_append(uuidList, uuid); } // convert remote device address from jlong to bluez bdaddr_t bdaddr_t remoteAddress; longToDeviceAddr(remoteDeviceAddressLong, &remoteAddress); bdaddr_t localAddr; longToDeviceAddr(localDeviceBTAddress, &localAddr); // connect to the device to retrieve services session = sdp_connect(&localAddr, &remoteAddress, SDP_RETRY_IF_BUSY); // if connection is not established throw an exception if (session == NULL) { rc = SERVICE_SEARCH_DEVICE_NOT_REACHABLE; goto searchServicesImplEnd; } // then ask the device for service record handles error = sdp_service_search_req(session, uuidList, max_rec_num, &(rsp_list)); if (error) { debug("sdp_service_search_req error %i", error); rc = SERVICE_SEARCH_ERROR; goto searchServicesImplEnd; } Edebug("runSearchServicesImpl session %p %li", session, ptr2jlong(session)); // Notify java about found services sdp_list_t* handle; for(handle = rsp_list; handle; handle = handle->next) { uint32_t record = *(uint32_t*)handle->data; jlong recordHandle = record; Edebug("runSearchServicesImpl serviceRecordHandle %li", recordHandle); jboolean isTerminated = (*env)->CallBooleanMethod(env, peer, serviceDiscoveredCallback, searchServicesThread, ptr2jlong(session), recordHandle); if ((*env)->ExceptionCheck(env)) { rc = SERVICE_SEARCH_ERROR; goto searchServicesImplEnd; } else if (isTerminated) { rc = SERVICE_SEARCH_TERMINATED; goto searchServicesImplEnd; } serviceCount ++; } debug("runSearchServicesImpl found %i", serviceCount); rc = SERVICE_SEARCH_COMPLETED; searchServicesImplEnd: sdp_list_free(uuidList, free); sdp_list_free(rsp_list, free); if (session != NULL) { sdp_close(session); } return rc; }
jobject createDataElement(JNIEnv *env, sdp_data_t *data) { Edebug("createDataElement 0x%x", data->dtd); jclass dataElementClass = (*env)->FindClass(env, "javax/bluetooth/DataElement"); jmethodID constructorID; jobject dataElement = NULL; switch (data->dtd) { case SDP_DATA_NIL: { constructorID = getGetMethodID(env, dataElementClass, "<init>", "(I)V"); if (constructorID == NULL) { break; } dataElement = (*env)->NewObject(env, dataElementClass, constructorID, DATA_ELEMENT_TYPE_NULL); break; } case SDP_BOOL: { jboolean boolean = data->val.uint8; constructorID = getGetMethodID(env, dataElementClass, "<init>", "(Z)V"); if (constructorID == NULL) { break; } dataElement = (*env)->NewObject(env, dataElementClass, constructorID, boolean); break; } case SDP_UINT8: { jlong value = (jlong)data->val.uint8; constructorID = getGetMethodID(env, dataElementClass, "<init>", "(IJ)V"); if (constructorID == NULL) { break; } dataElement = (*env)->NewObject(env, dataElementClass, constructorID, DATA_ELEMENT_TYPE_U_INT_1, value); break; } case SDP_UINT16: { jlong value = (jlong)data->val.uint16; constructorID = getGetMethodID(env, dataElementClass, "<init>", "(IJ)V"); if (constructorID == NULL) { break; } dataElement = (*env)->NewObject(env, dataElementClass, constructorID, DATA_ELEMENT_TYPE_U_INT_2, value); break; } case SDP_UINT32: { jlong value = (jlong)data->val.uint32; constructorID = getGetMethodID(env, dataElementClass, "<init>", "(IJ)V"); if (constructorID == NULL) { break; } dataElement = (*env)->NewObject(env, dataElementClass, constructorID, DATA_ELEMENT_TYPE_U_INT_4, value); break; } case SDP_INT8: { jlong value = (jlong)data->val.int8; constructorID = getGetMethodID(env, dataElementClass,"<init>","(IJ)V"); dataElement = (*env)->NewObject(env, dataElementClass, constructorID, DATA_ELEMENT_TYPE_INT_1, value); break; } case SDP_INT16: { jlong value = (jlong)data->val.int16; constructorID = getGetMethodID(env, dataElementClass, "<init>", "(IJ)V"); if (constructorID == NULL) { break; } dataElement = (*env)->NewObject(env, dataElementClass, constructorID, DATA_ELEMENT_TYPE_INT_2, value); break; } case SDP_INT32: { jlong value = (jlong)data->val.int32; constructorID = getGetMethodID(env, dataElementClass, "<init>", "(IJ)V"); dataElement = (*env)->NewObject(env, dataElementClass, constructorID, DATA_ELEMENT_TYPE_INT_4, value); break; } case SDP_INT64: { jlong value = (jlong)data->val.int64; constructorID = getGetMethodID(env, dataElementClass, "<init>", "(IJ)V"); if (constructorID == NULL) { break; } dataElement = (*env)->NewObject(env, dataElementClass, constructorID, DATA_ELEMENT_TYPE_INT_8, value); break; } case SDP_UINT64: { Edebug("SDP_UINT64"); uint64_t value = data->val.uint64; jbyte* bytes = (jbyte*)&value; reverseArray(bytes, sizeof(value)); jbyteArray byteArray = (*env)->NewByteArray(env, sizeof(value)); (*env)->SetByteArrayRegion(env, byteArray, 0, sizeof(value), bytes); constructorID = getGetMethodID(env, dataElementClass, "<init>", "(ILjava/lang/Object;)V"); if (constructorID == NULL) { break; } dataElement = (*env)->NewObject(env, dataElementClass, constructorID, DATA_ELEMENT_TYPE_U_INT_8, byteArray); break; } case SDP_UINT128: { Edebug("SDP_UINT128"); uint128_t value = data->val.uint128; jbyte* bytes = (jbyte*)&value; reverseArray(bytes, sizeof(value)); jbyteArray byteArray = (*env)->NewByteArray(env, sizeof(value)); (*env)->SetByteArrayRegion(env, byteArray, 0, sizeof(value), bytes); constructorID = getGetMethodID(env, dataElementClass, "<init>", "(ILjava/lang/Object;)V"); if (constructorID == NULL) { break; } dataElement = (*env)->NewObject(env, dataElementClass, constructorID, DATA_ELEMENT_TYPE_U_INT_16, byteArray); break; } case SDP_INT128: { Edebug("SDP_INT128"); uint128_t value = data->val.int128; jbyte* bytes = (jbyte*)&value; reverseArray(bytes, sizeof(value)); jbyteArray byteArray = (*env)->NewByteArray(env, sizeof(value)); (*env)->SetByteArrayRegion(env, byteArray, 0, sizeof(value), bytes); constructorID = getGetMethodID(env, dataElementClass, "<init>", "(ILjava/lang/Object;)V"); if (constructorID == NULL) { break; } dataElement = (*env)->NewObject(env, dataElementClass, constructorID, DATA_ELEMENT_TYPE_INT_16, byteArray); break; } case SDP_URL_STR_UNSPEC: case SDP_URL_STR8: case SDP_URL_STR16: case SDP_URL_STR32: { Edebug("SDP_URL"); char* str = data->val.str; constructorID = getGetMethodID(env, dataElementClass, "<init>", "(ILjava/lang/Object;)V"); if (constructorID == NULL) { break; } jstring string = (*env)->NewStringUTF(env, str); dataElement = (*env)->NewObject(env, dataElementClass, constructorID, DATA_ELEMENT_TYPE_URL, string); break; } case SDP_TEXT_STR_UNSPEC: case SDP_TEXT_STR8: case SDP_TEXT_STR16: case SDP_TEXT_STR32: { Edebug("SDP_TEXT"); char* str = data->val.str; constructorID = getGetMethodID(env, dataElementClass, "<init>", "(ILjava/lang/Object;)V"); if (constructorID == NULL) { break; } jstring string = (*env)->NewStringUTF(env, str); dataElement = (*env)->NewObject(env, dataElementClass, constructorID, DATA_ELEMENT_TYPE_STRING, string); break; } case SDP_UUID_UNSPEC: case SDP_UUID16: case SDP_UUID32: case SDP_UUID128: { Edebug("SDP_UUID"); jobject javaUUID = createJavaUUID(env, data->val.uuid); if (javaUUID == NULL) { debug("fail to create UUID"); break; } constructorID = getGetMethodID(env, dataElementClass, "<init>", "(ILjava/lang/Object;)V"); if (constructorID == NULL) { break; } dataElement = (*env)->NewObject(env, dataElementClass, constructorID, DATA_ELEMENT_TYPE_UUID, javaUUID); break; } case SDP_SEQ_UNSPEC: case SDP_SEQ8: case SDP_SEQ16: case SDP_SEQ32: { Edebug("SDP_SEQ"); sdp_data_t *newData = data->val.dataseq; constructorID = getGetMethodID(env, dataElementClass, "<init>", "(I)V"); if (constructorID == NULL) { break; } dataElement = (*env)->NewObject(env, dataElementClass, constructorID, DATA_ELEMENT_TYPE_DATSEQ); jmethodID addElementID = getGetMethodID(env, dataElementClass, "addElement", "(Ljavax/bluetooth/DataElement;)V"); for(; newData; newData = newData->next) { jobject newDataElement = createDataElement(env, newData); if (newDataElement != NULL) { (*env)->CallVoidMethod(env, dataElement, addElementID, newDataElement); } if ((*env)->ExceptionCheck(env)) { break; } } break; } case SDP_ALT_UNSPEC: case SDP_ALT8: case SDP_ALT16: case SDP_ALT32: { Edebug("SDP_ALT"); sdp_data_t *newData = data->val.dataseq; constructorID = getGetMethodID(env, dataElementClass, "<init>", "(I)V"); if (constructorID == NULL) { break; } dataElement = (*env)->NewObject(env, dataElementClass, constructorID, DATA_ELEMENT_TYPE_DATALT); jmethodID addElementID = getGetMethodID(env, dataElementClass, "addElement", "(Ljavax/bluetooth/DataElement;)V"); for(; newData; newData = newData->next) { jobject newDataElement = createDataElement(env, newData); if (newDataElement == NULL) { break; } (*env)->CallVoidMethod(env, dataElement, addElementID, newDataElement); if ((*env)->ExceptionCheck(env)) { break; } } break; } default: { debug("strange data type 0x%x", data->dtd); constructorID = getGetMethodID(env, dataElementClass, "<init>", "(I)V"); if (constructorID == NULL) { break; } dataElement = (*env)->NewObject(env, dataElementClass, constructorID, DATA_ELEMENT_TYPE_NULL); break; } } if (dataElement != NULL) { Edebug("dataElement created 0x%x", data->dtd); } if ((*env)->ExceptionCheck(env)) { ndebug("Exception in data element creation 0x%x", data->dtd); } return dataElement; }