JNIEXPORT void JNICALL Java_com_intel_bluetooth_BluetoothStackBlueZ_l2Send (JNIEnv* env, jobject peer, jlong handle, jbyteArray data, jint transmitMTU) { #ifdef BLUECOVE_L2CAP_MTU_TRUNCATE struct l2cap_options opt; if (!l2Get_options(env, handle, &opt)) { return; } #endif //BLUECOVE_L2CAP_MTU_TRUNCATE if (data == NULL) { throwRuntimeException(env, "Invalid argument"); return; } jbyte *bytes = (*env)->GetByteArrayElements(env, data, 0); if (bytes == NULL) { throwRuntimeException(env, "Invalid argument"); return; } int len = (int)(*env)->GetArrayLength(env, data); if (len > transmitMTU) { len = transmitMTU; } #ifdef BLUECOVE_L2CAP_MTU_TRUNCATE if (len > opt.omtu) { len = opt.omtu; } #endif //BLUECOVE_L2CAP_MTU_TRUNCATE int count = send(handle, (char *)bytes, len, 0); if (count < 0) { throwIOException(env, "Failed to write. [%d] %s", errno, strerror(errno)); } (*env)->ReleaseByteArrayElements(env, data, bytes, 0); }
JNIEXPORT void JNICALL Java_com_intel_bluetooth_BluetoothStackBlueZDBus_connectionRfWrite__J_3BII (JNIEnv* env, jobject peer, jlong handle, jbyteArray b, jint off, jint len) { if (b == NULL) { throwRuntimeException(env, "Invalid argument"); return; } jbyte *bytes = (*env)->GetByteArrayElements(env, b, 0); if (bytes == NULL) { throwRuntimeException(env, "Invalid argument"); return; } int done = 0; while(done < len) { int count = send(handle, (char *)(bytes + off + done), len - done, 0); if (count < 0) { throwIOException(env, "Failed to write. [%d] %s", errno, strerror(errno)); break; } if (isCurrentThreadInterrupted(env, peer)) { break; } done += count; } (*env)->ReleaseByteArrayElements(env, b, bytes, 0); }
bool DeviceInquiryCallback_builDeviceInquiryCallbacks(JNIEnv * env, struct DeviceInquiryCallback* callback, jobject inquiryRunnable, jobject startedNotify) { jclass inquiryRunnableClass = (*env)->GetObjectClass(env, inquiryRunnable); if (inquiryRunnableClass == NULL) { throwRuntimeException(env, "Fail to get Object Class"); return false; } jmethodID deviceDiscoveredCallbackMethod = (*env)->GetMethodID(env, inquiryRunnableClass, "deviceDiscoveredCallback", "(Ljavax/bluetooth/DiscoveryListener;JILjava/lang/String;Z)V"); if (deviceDiscoveredCallbackMethod == NULL) { throwRuntimeException(env, "Fail to get MethodID deviceDiscoveredCallback"); return false; } jclass notifyClass = (*env)->GetObjectClass(env, startedNotify); if (notifyClass == NULL) { throwRuntimeException(env, "Fail to get Object Class"); return false; } jmethodID notifyMethod = (*env)->GetMethodID(env, notifyClass, "deviceInquiryStartedCallback", "()V"); if (notifyMethod == NULL) { throwRuntimeException(env, "Fail to get MethodID deviceInquiryStartedCallback"); return false; } callback->inquiryRunnable = inquiryRunnable; callback->deviceDiscoveredCallbackMethod = deviceDiscoveredCallbackMethod; callback->startedNotify = startedNotify; callback->startedNotifyNotifyMethod = notifyMethod; return true; }
static void setText(JNIEnv* env, jclass clazz, RegExData* data, jstring text) { UErrorCode status = U_ZERO_ERROR; uregex_setText(data->regex, &EMPTY_STRING, 0, &status); if (!U_SUCCESS(status)) { throwRuntimeException(env, status); return; } if (data->text != &EMPTY_STRING) { delete[] data->text; data->text = NULL; } int textLen = env->GetStringLength(text); if (textLen == 0) { data->text = &EMPTY_STRING; } else { data->text = new jchar[textLen + 1]; env->GetStringRegion(text, 0, textLen, data->text); data->text[textLen] = 0; } uregex_setText(data->regex, data->text, textLen, &status); if (!U_SUCCESS(status)) { throwRuntimeException(env, status); } }
JNIEXPORT jstring JNICALL Java_com_intel_bluetooth_BluetoothStackBlueZ_getRemoteDeviceVersionInfoImpl (JNIEnv *env, jobject peer, jint deviceDescriptor, jlong remoteDeviceAddressLong) { struct hci_conn_info_req *conn_info; struct hci_version ver; char info[256]; conn_info = (struct hci_conn_info_req*)malloc(sizeof(*conn_info) + sizeof(struct hci_conn_info)); if (!conn_info) { throwRuntimeException(env, cOUT_OF_MEMORY); return NULL; } memset(conn_info, 0, sizeof(struct hci_conn_info)); longToDeviceAddr(remoteDeviceAddressLong, &(conn_info->bdaddr)); conn_info->type = ACL_LINK; if (ioctl((int)deviceDescriptor, HCIGETCONNINFO, (unsigned long) conn_info) < 0) { free(conn_info); throwRuntimeException(env, "Fail to get connection info"); return NULL; } int error = hci_read_remote_version((int)deviceDescriptor, conn_info->conn_info->handle, &ver, READ_REMOTE_NAME_TIMEOUT); if (error < 0) { throwRuntimeException(env, "Can not get remote device info"); free(conn_info); return NULL; } snprintf(info, 256, "manufacturer=%i,lmp_version=%i,lmp_sub_version=%i", ver.manufacturer, ver.lmp_ver, ver.lmp_subver); free(conn_info); return (*env)->NewStringUTF(env, info); }
inline void convertException(JNIEnv* env, Func func) noexcept { try { func(); } catch (JavaException& e) { return; } catch (std::exception& e) { throwRuntimeException(env, e.what()); } catch (...) { throwRuntimeException(env, "Unindentified exception thrown (not derived from std::exception)"); } }
jmethodID getGetMethodID(JNIEnv * env, jclass clazz, const char *name, const char *sig) { if (clazz == NULL) { throwRuntimeException(env, "Fail to get MethodID %s for NULL class", name); return NULL; } jmethodID methodID = (*env)->GetMethodID(env, clazz, name, sig); if (methodID == NULL) { throwRuntimeException(env, "Fail to get MethodID %s", name); return NULL; } return methodID; }
jint epollCtl(JNIEnv * env, jint efd, int op, jint fd, jint flags, jint id) { uint32_t events = EPOLLET; if (flags & EPOLL_ACCEPT) { events |= EPOLLIN; } if (flags & EPOLL_READ) { events |= EPOLLIN | EPOLLRDHUP; } if (flags & EPOLL_WRITE) { events |= EPOLLOUT; } struct epoll_event ev = { .events = events, // encode the id into the events .data.u64 = (((uint64_t) id) << 32L) }; return epoll_ctl(efd, op, fd, &ev); } jint getOption(JNIEnv *env, jint fd, int level, int optname, const void *optval, socklen_t optlen) { int code; code = getsockopt(fd, level, optname, optval, &optlen); if (code == 0) { return 0; } int err = errno; throwRuntimeException(env, exceptionMessage("Error during getsockopt(...): ", err)); return code; }
static inline struct io_control * getIOControl(JNIEnv* env, jobject pointer) { struct io_control * ioControl = (struct io_control *) (*env)->GetDirectBufferAddress(env, pointer); if (ioControl == NULL) { throwRuntimeException(env, "Controller not initialized"); } return ioControl; }
/** * private static native int EVP_PKEY_new_RSA(byte[] n, byte[] e, byte[] d, byte[] p, byte[] q); */ static EVP_PKEY* NativeCrypto_EVP_PKEY_new_RSA(JNIEnv* env, jclass clazz, jbyteArray n, jbyteArray e, jbyteArray d, jbyteArray p, jbyteArray q) { // LOGD("Entering EVP_PKEY_new_RSA()"); RSA* rsa = RSA_new(); rsa->n = arrayToBignum(env, n); rsa->e = arrayToBignum(env, e); if (d != NULL) { rsa->d = arrayToBignum(env, d); } if (p != NULL) { rsa->p = arrayToBignum(env, p); } if (q != NULL) { rsa->q = arrayToBignum(env, q); } // int check = RSA_check_key(rsa); // LOGI("RSA_check_key returns %d", check); if (rsa->n == NULL || rsa->e == NULL) { RSA_free(rsa); throwRuntimeException(env, "Unable to convert BigInteger to BIGNUM"); return NULL; } EVP_PKEY* pkey = EVP_PKEY_new(); EVP_PKEY_assign_RSA(pkey, rsa); return pkey; }
/** * private static native int EVP_PKEY_new_DSA(byte[] p, byte[] q, byte[] g, byte[] pub_key, byte[] priv_key); */ static EVP_PKEY* NativeCrypto_EVP_PKEY_new_DSA(JNIEnv* env, jclass clazz, jbyteArray p, jbyteArray q, jbyteArray g, jbyteArray pub_key, jbyteArray priv_key) { // LOGD("Entering EVP_PKEY_new_DSA()"); DSA* dsa = DSA_new(); dsa->p = arrayToBignum(env, p); dsa->q = arrayToBignum(env, q); dsa->g = arrayToBignum(env, g); dsa->pub_key = arrayToBignum(env, pub_key); if (priv_key != NULL) { dsa->priv_key = arrayToBignum(env, priv_key); } if (dsa->p == NULL || dsa->q == NULL || dsa->g == NULL || dsa->pub_key == NULL) { DSA_free(dsa); throwRuntimeException(env, "Unable to convert BigInteger to BIGNUM"); return NULL; } EVP_PKEY* pkey = EVP_PKEY_new(); EVP_PKEY_assign_DSA(pkey, dsa); return pkey; }
bool threadSleep(JNIEnv *env, jlong millis) { jclass clazz = (*env)->FindClass(env, "java/lang/Thread"); if (clazz == NULL) { throwRuntimeException(env, "Fail to get Thread class"); return false; } jmethodID methodID = (*env)->GetStaticMethodID(env, clazz, "sleep", "(J)V"); if (methodID == NULL) { throwRuntimeException(env, "Fail to get MethodID Thread.sleep"); return false; } (*env)->CallStaticVoidMethod(env, clazz, methodID, millis); if ((*env)->ExceptionCheck(env)) { return false; } return true; }
JNIEXPORT jint JNICALL Java_io_netty_channel_epoll_Native_epollCreate(JNIEnv * env, jclass clazz) { jint efd = epoll_create1(EPOLL_CLOEXEC); if (efd < 0) { int err = errno; throwRuntimeException(env, exceptionMessage("Error during epoll_create(...): ", err)); } return efd; }
static void reset(JNIEnv* env, jclass clazz, RegExData* data, jint position) { UErrorCode status = U_ZERO_ERROR; uregex_reset(data->regex, position, &status); if (!U_SUCCESS(status)) { throwRuntimeException(env, status); } }
int setOption(JNIEnv *env, jint fd, int level, int optname, const void *optval, socklen_t len) { int rc = setsockopt(fd, level, optname, optval, len); if (rc < 0) { int err = errno; throwRuntimeException(env, exceptionMessage("Error during setsockopt(...): ", err)); } return rc; }
JNIEXPORT jint JNICALL Java_io_netty_channel_epoll_Native_read(JNIEnv * env, jclass clazz, jint fd, jobject jbuffer, jint pos, jint limit) { void *buffer = (*env)->GetDirectBufferAddress(env, jbuffer); if (buffer == NULL) { throwRuntimeException(env, "Unable to access address of buffer"); return -1; } return read0(env, clazz, fd, buffer, pos, limit); }
bool isCurrentThreadInterrupted(JNIEnv *env, jobject peer) { jclass peerClass = (*env)->GetObjectClass(env, peer); if (peerClass == NULL) { throwRuntimeException(env, "Fail to get Object Class"); return true; } jmethodID aMethod = (*env)->GetMethodID(env, peerClass, "isCurrentThreadInterruptedCallback", "()Z"); if (aMethod == NULL) { throwRuntimeException(env, "Fail to get MethodID isCurrentThreadInterruptedCallback"); return true; } if ((*env)->CallBooleanMethod(env, peer, aMethod)) { throwInterruptedIOException(env, "thread interrupted"); return true; } return (*env)->ExceptionCheck(env); }
/* * Creates compatible GraphicsInfo structure for specified device context */ static inline GraphicsInfo *createCompatibleImageInfo(JNIEnv *env, HDC hdc, jint width, jint height) { GraphicsInfo *gi = (GraphicsInfo *) malloc(sizeof(GraphicsInfo)); // To avoid issue of joint operation Windows NetMeeting and GL, // we create HDC and Bitmap for Volatile Image which will compatible // HDC of the entire screen. It may leads to other issues on // multimonitor systems in the future. gi->hwnd = 0; HDC dc = GetDC(NULL); //gi->hdc = CreateCompatibleDC(hdc); gi->hdc = CreateCompatibleDC(dc); if (gi->hdc == NULL) { // We are out of GDI resources runGC(env); //gi->hdc = CreateCompatibleDC(hdc); gi->hdc = CreateCompatibleDC(dc); if (gi->hdc == NULL) throwRuntimeException(env); } // Creating bitmap and setting it to DC //gi->bmp = CreateCompatibleBitmap(hdc, width, height); gi->bmp = CreateCompatibleBitmap(dc, width, height); if (gi->bmp == NULL) { // We are out of GDI resources runGC(env); //gi->bmp = CreateCompatibleBitmap(hdc, width, height); gi->bmp = CreateCompatibleBitmap(dc, width, height); if (gi->bmp == NULL) throwRuntimeException(env); } SelectObject(gi->hdc, gi->bmp); ReleaseDC(NULL, dc); gi->graphics = new Graphics(gi->hdc); gi->pen = 0; gi->brush = 0; gi->matrix = 0; gi->clip = new Region(Rect(0, 0, width, height)); gi->graphics->SetClip(gi->clip); return gi; }
JNIEXPORT void JNICALL Java_io_netty_channel_epoll_Native_epollCtlDel(JNIEnv * env, jclass clazz, jint efd, jint fd) { // Create an empty event to workaround a bug in older kernels which can not handle NULL. struct epoll_event event = { 0 }; if (epoll_ctl(efd, EPOLL_CTL_DEL, fd, &event) < 0) { int err = errno; throwRuntimeException(env, exceptionMessage("Error during calling epoll_ctl(...): ", err)); } }
JNIEXPORT jint JNICALL Java_io_netty_channel_epoll_Native_sendTo(JNIEnv * env, jclass clazz, jint fd, jobject jbuffer, jint pos, jint limit, jbyteArray address, jint scopeId, jint port) { void *buffer = (*env)->GetDirectBufferAddress(env, jbuffer); if (buffer == NULL) { throwRuntimeException(env, "Unable to access address of buffer"); return -1; } return sendTo0(env, fd, buffer, pos, limit, address, scopeId, port); }
JNIEXPORT void JNICALL Java_io_netty_channel_epoll_Native_eventFdRead(JNIEnv * env, jclass clazz, jint fd) { uint64_t eventfd_t; if (eventfd_read(fd, &eventfd_t) != 0) { // something is serious wrong throwRuntimeException(env, "Error calling eventfd_read(...)"); } }
JNIEXPORT void JNICALL Java_io_netty_channel_epoll_Native_eventFdWrite(JNIEnv * env, jclass clazz, jint fd, jlong value) { jint eventFD = eventfd_write(fd, (eventfd_t)value); if (eventFD < 0) { int err = errno; throwRuntimeException(env, exceptionMessage("Error calling eventfd_write(...): ", err)); } }
static void setRegion(JNIEnv* env, jclass clazz, RegExData* data, jint start, jint end) { UErrorCode status = U_ZERO_ERROR; uregex_setRegion(data->regex, start, end, &status); if (!U_SUCCESS(status)) { throwRuntimeException(env, status); } }
static jint regionStart(JNIEnv* env, jclass clazz, RegExData* data) { UErrorCode status = U_ZERO_ERROR; int result = uregex_regionStart(data->regex, &status); if (!U_SUCCESS(status)) { throwRuntimeException(env, status); } return result; }
static void useAnchoringBounds(JNIEnv* env, jclass clazz, RegExData* data, jboolean value) { UErrorCode status = U_ZERO_ERROR; uregex_useAnchoringBounds(data->regex, value, &status); if (!U_SUCCESS(status)) { throwRuntimeException(env, status); } }
JNIEXPORT jint JNICALL Java_io_netty_channel_epoll_Native_eventFd(JNIEnv * env, jclass clazz) { jint eventFD = eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK); if (eventFD < 0) { int err = errno; throwRuntimeException(env, exceptionMessage("Error creating eventFD(...): ", err)); } return eventFD; }
static jboolean requireEnd(JNIEnv* env, jclass clazz, RegExData* data) { UErrorCode status = U_ZERO_ERROR; jboolean result = uregex_requireEnd(data->regex, &status); if (!U_SUCCESS(status)) { throwRuntimeException(env, status); } return result; }
JNIEXPORT void JNICALL Java_org_apache_activemq_artemis_jlibaio_LibaioContext_freeBuffer (JNIEnv * env, jclass clazz, jobject jbuffer) { if (jbuffer == NULL) { throwRuntimeException(env, "Null pointer"); return; } void * buffer = (*env)->GetDirectBufferAddress(env, jbuffer); free(buffer); }
JNIEXPORT jstring JNICALL Java_io_netty_channel_epoll_Native_kernelVersion(JNIEnv *env, jclass clazz) { struct utsname name; int res = uname(&name); if (res == 0) { return (*env)->NewStringUTF(env, name.release); } int err = errno; throwRuntimeException(env, exceptionMessage("Error during uname(...): ", err)); return NULL; }
static jboolean lookingAt(JNIEnv* env, jclass clazz, RegExData* data, jint startIndex) { UErrorCode status = U_ZERO_ERROR; jboolean result = uregex_lookingAt(data->regex, startIndex, &status); if (!U_SUCCESS(status)) { throwRuntimeException(env, status); } return result; }