JNIEXPORT jint JNICALL Java_de_entropia_can_CanSocket__1discoverInterfaceIndex
(JNIEnv *env, jclass clazz, jint socketFd, jstring ifName)
{
	struct ifreq ifreq;
	const jsize ifNameSize = env->GetStringUTFLength(ifName);
	if (ifNameSize > IFNAMSIZ-1) {
		throwIllegalArgumentException(env, "illegal interface name");
		return -1;
	}
	
	/* fetch interface name */
	memset(&ifreq, 0x0, sizeof(ifreq));
	env->GetStringUTFRegion(ifName, 0, ifNameSize,
				ifreq.ifr_name);	
	if (env->ExceptionCheck() == JNI_TRUE) {
		return -1;
	}
	/* discover interface id */
	const int err = ioctl(socketFd, SIOCGIFINDEX, &ifreq);
	if (err == -1) {
		throwIOExceptionErrno(env, errno);
		return -1;
	} else {
		return ifreq.ifr_ifindex;
	}
}
Example #2
0
/*
* Class:     eu_vandertil_jerasure_jni_Cauchy
* Method:    cauchy_original_coding_matrix
* Signature: (III)[I
*/
JNIEXPORT jintArray JNICALL Java_eu_vandertil_jerasure_jni_Cauchy_cauchy_1original_1coding_1matrix
	(JNIEnv *env, jclass clazz, jint k, jint m, jint w)
{
	bool outOfMemory = false;
	jintArray result = NULL;

	if (!(w < 31 && (k+m) > (1 << w))) { // from implementation.
		int* matrix = cauchy_original_coding_matrix(k, m, w);

		if(matrix != NULL) {
			result = env->NewIntArray(k*m);
			if(result != NULL) {
				env->SetIntArrayRegion(result, 0, k*m, (jint*)matrix);
			} else {
				outOfMemory = true;
			}
		} else {
			outOfMemory = true;
		}

		free(matrix);
	} else {
		throwIllegalArgumentException(env, "w < 31 && (k+m) > (1 << w)");
	}

	if(outOfMemory) {
		throwOutOfMemoryError(env, "Not enough free memory to complete");
	}

	return result;
}
JNIEXPORT jobject JNICALL Java_de_entropia_can_CanSocket__1recvFrame
(JNIEnv *env, jclass obj, jint fd)
{
	const int flags = 0;
	ssize_t nbytes;
	struct sockaddr_can addr;
	socklen_t len = sizeof(addr);
	struct can_frame frame;

	memset(&addr, 0, sizeof(addr));
	memset(&frame, 0, sizeof(frame));
	nbytes = recvfrom(fd, &frame, sizeof(frame), flags,
			  reinterpret_cast<struct sockaddr *>(&addr), &len);
	if (len != sizeof(addr)) {
		throwIllegalArgumentException(env, "illegal AF_CAN address");
		return nullptr;
	}
	if (nbytes == -1) {
		throwIOExceptionErrno(env, errno);
		return nullptr;
	} else if (nbytes != sizeof(frame)) {
		throwIOExceptionMsg(env, "invalid length of received frame");
		return nullptr;
	}
	const jsize fsize = static_cast<jsize>(std::min(static_cast<size_t>(frame.can_dlc),
							nbytes - offsetof(struct can_frame, data)));
	const jclass can_frame_clazz = env->FindClass("de/entropia/can/"
							"CanSocket$CanFrame");
	if (can_frame_clazz == nullptr) {
		return nullptr;
	}
	const jmethodID can_frame_cstr = env->GetMethodID(can_frame_clazz,
							"<init>", "(II[B)V");
	if (can_frame_cstr == nullptr) {
		return nullptr;
	}
	const jbyteArray data = env->NewByteArray(fsize);
	if (data == nullptr) {
		if (env->ExceptionCheck() != JNI_TRUE) {
			throwOutOfMemoryError(env, "could not allocate ByteArray");
		}
		return nullptr;
	}
	env->SetByteArrayRegion(data, 0, fsize, reinterpret_cast<jbyte *>(&frame.data));
	if (env->ExceptionCheck() == JNI_TRUE) {
		return nullptr;
	}
	const jobject ret = env->NewObject(can_frame_clazz, can_frame_cstr,
					   addr.can_ifindex, frame.can_id,
					   data);
	return ret;
}
JNIEXPORT jint JNICALL Java_de_entropia_can_CanSocket__1getsockopt
(JNIEnv *env, jclass obj, jint fd, jint op)
{
	int _stat = 0;
	socklen_t len = sizeof(_stat);
	if (getsockopt(fd, SOL_CAN_RAW, op, &_stat, &len) == -1) {
		throwIOExceptionErrno(env, errno);
	}
	if (len != sizeof(_stat)) {
		throwIllegalArgumentException(env, "setsockopt return size is different");
		return -1;
	}
	return _stat;
}
static void readBitmapPixels(JNIEnv* env, jclass /* clazz */, jobject jbitmap, jint fd) {
    // Read the info.
    AndroidBitmapInfo readInfo;
    bool read = readAllBytes(fd, (void*) &readInfo, sizeof(AndroidBitmapInfo));
    if (!read) {
        throwIllegalStateException(env, (char*) "Cannot read bitmap info");
        return;
    }

    // Get the info of the target bitmap.
    AndroidBitmapInfo targetInfo;
    int result = AndroidBitmap_getInfo(env, jbitmap, &targetInfo);
    if (result < 0) {
        throwIllegalStateException(env, (char*) "Cannot get bitmap info");
        return;
    }

    // Enforce we can reuse the bitmap.
    if (readInfo.width != targetInfo.width || readInfo.height != targetInfo.height
            || readInfo.stride != targetInfo.stride || readInfo.format != targetInfo.format
            || readInfo.flags != targetInfo.flags) {
        throwIllegalArgumentException(env, (char*) "Cannot reuse bitmap");
        return;
    }

    // Lock the pixels.
    void* pixels;
    result = AndroidBitmap_lockPixels(env, jbitmap, &pixels);
    if (result < 0) {
        throwIllegalStateException(env, (char*) "Cannot lock bitmap pixels");
        return;
    }

    // Read the pixels.
    size_t byteCount = readInfo.stride * readInfo.height;
    read = readAllBytes(fd, (void*) pixels, byteCount);
    if (!read) {
        throwIllegalStateException(env, (char*) "Cannot read bitmap pixels");
        return;
    }

    // Unlock the pixels.
    result = AndroidBitmap_unlockPixels(env, jbitmap);
    if (result < 0) {
        throwIllegalStateException(env, (char*) "Cannot unlock bitmap pixels");
    }
}
static int throwIfBadSlot(JNIEnv *env, jint slot)
{
    if (slot>=slots.nslots) {
	throwIllegalArgumentException(env, "bogus slot");
	return 1;
    }

    if ((void*)0 == slots.slots[slot]) {
	jclass newExcCls = (*env)->FindClass(env, "java/lang/IllegalArgumentException");
	if (newExcCls == 0) /* Unable to find the new exception class, give up. */
	    return 1;
	(*env)->ThrowNew(env, newExcCls, "slot is already empty");
	return 1;
    }

    return 0; // the slot is good
}
JNIEXPORT jint JNICALL Java_de_entropia_can_CanSocket__1fetchInterfaceMtu
(JNIEnv *env, jclass obj, jint fd, jstring ifName)
{
	struct ifreq ifreq;

	const jsize ifNameSize = env->GetStringUTFLength(ifName);
	if (ifNameSize > IFNAMSIZ-1) {
		throwIllegalArgumentException(env, "illegal interface name");
		return -1;
	}
	memset(&ifreq, 0x0, sizeof(ifreq));
	env->GetStringUTFRegion(ifName, 0, ifNameSize, ifreq.ifr_name);
	if (env->ExceptionCheck() == JNI_TRUE) {
		return -1;
	}
	if (ioctl(fd, SIOCGIFMTU, &ifreq) == -1) {
		throwIOExceptionErrno(env, errno);
		return -1;
	} else {
		return ifreq.ifr_mtu;
	}
}
/*
 * Class:     com_diozero_pigpioj_PigpioGpio
 * Method:    setISRFunc
 * Signature: (IIILorg/diozero/internal/provider/pigpio/impl/PigpioCallback;)I
 */
JNIEXPORT jint JNICALL Java_com_diozero_pigpioj_PigpioGpio_setISRFunc
  (JNIEnv* env, jclass clz, jint gpio, jint edge, jint timeout, jobject listener) {
	if (listener == NULL) {
		gpioSetISRFunc(gpio, edge, timeout, NULL);
		if (listeners[gpio] != NULL) {
			(*env)->DeleteGlobalRef(env, listeners[gpio]);
			listeners[gpio] = NULL;
		}

		return 0;
	}

	// Validate the listener object has the appropriate method signature
	jclass listener_class = (*env)->GetObjectClass(env, listener);
	if (listener_class == NULL) {
		printf("PigpioGpio Native: Error: setISRFunc(%d) could not get listener class\n", gpio);
		throwRuntimeException(env, "Error in setISRFunc, could not get class for listener object");
		return -1;
	}

	// Verify that the object has a 'void callback(int, boolean, long, long)' method
	jmethodID callback_method = (*env)->GetMethodID(env, listener_class, "callback", "(IZJJ)V");
	if (callback_method == NULL) {
		printf("PigpioGpio Native: Error: setISRFunc(%d) could not get callback method id\n", gpio);
		throwIllegalArgumentException(env, "Error in setISRFunc, could not get listener method id callback(IZJJ)V");
		return -1;
	}

	// Remove the previous listener if there was one
	if (listeners[gpio] != NULL) {
		gpioSetISRFunc(gpio, edge, timeout, NULL);
		(*env)->DeleteGlobalRef(env, listeners[gpio]);
		listeners[gpio] = NULL;
	}

	// Register the new listener and pigpio callback
	listeners[gpio] = (*env)->NewGlobalRef(env, listener);
	return gpioSetISRFunc(gpio, edge, timeout, callbackFunction);
}