@Billy_Milligan

Как передать значение из java в натив (c++)?

Использую для SPI библиотеку BlackLib.

Все действия нужно делать из java. Со сборкой и т.д и т.п проблем нет

Код на си:

JNIEXPORT jint JNICALL Java_BlackLib_BlackSPI__1transfer(JNIEnv *env, jclass obj, jobject writeBuffer, jobject readBuffer, jint bufferSize, jint wait_us)
{
    return spi->transfer(
        reinterpret_cast<uint8_t*>(env->GetDirectBufferAddress(writeBuffer)),
        reinterpret_cast<uint8_t*>(env->GetDirectBufferAddress(readBuffer)),
        bufferSize,
        wait_us
    );
}


Код на java:

BlackSPI spi = new BlackSPI(BlackSPI.SpiName.SPI1_1.getValue(), 32, BlackSPI.SpiMode.MODE_0.getValue(), 5_000_000);

    spi.open(BlackSPI.OpenMode.ReadWrite.getValue() | BlackSPI.OpenMode.NonBlock.getValue());

    int val = 0, stub = 0;

    for(int i=30 ; i >= 0 ; i--) {
        val = val & (~(3 << i));
        val |= (0 << i);
    }

    val = val & (~(3 << 0));
    val |= (1 << 0);

    try {
        System.out.println(spi.transfer(ByteBuffer.allocate(4).putInt(val), ByteBuffer.allocate(4).putInt(stub), 4, 0));
    } catch (IIOException e) {
        e.printStackTrace();
    }


Проблема именно в transfer и writeBuffer. Если часть кода из java перенести в C, то все работает как надо.

int val, stub;

    for(int i=30 ; i >= 0 ; i--) {
        val = val & (~(3 << i));
        val |= (0 << i);
    }

    val = val & (~(3 << 0));
    val |= (1 << 0);

    return spi->transfer((uint8_t*) & val, (uint8_t*) & stub, sizeof (stub), 0);


Идею с ByteBuffer подсмотрел тут. Так и не смог понять почему у меня это не работает.

Вопрос в том, как корректно передать в Си uint8_t*?

Вот такую ошибку получаю:

#
# A fatal error has been detected by the Java Runtime Environment:
#
#  SIGSEGV (0xb) at pc=0xb6ed0f34, pid=4509, tid=0xb63fe470
#
# JRE version: Java(TM) SE Runtime Environment (8.0_161-b12) (build 1.8.0_161-b12)
# Java VM: Java HotSpot(TM) Client VM (25.161-b12 mixed mode linux-arm )
# Problematic frame:
# C  [libc.so.6+0x59f34]  memcpy+0xb4
#
# Failed to write core dump. Core dumps have been disabled. To enable core dumping, try "ulimit -c unlimited" before starting Java again
#
# An error report file with more information is saved as:
# /root/hs_err_pid4509.log
#
# If you would like to submit a bug report, please visit:
#   http://bugreport.java.com/bugreport/crash.jsp
# The crash happened outside the Java Virtual Machine in native code.
# See problematic frame for where to report the bug.
#
Aborted
  • Вопрос задан
  • 209 просмотров
Решения вопроса 1
@Billy_Milligan Автор вопроса
Просто все переделал на int

JNIEXPORT jint JNICALL Java_BlackLib_BlackSPI__1transfer(JNIEnv *env, jclass obj, jint writeBuffer, jint readBuffer, jint bufferSize, jint wait_us)
{
    return spi->transfer((uint8_t*) & writeBuffer, (uint8_t*) & readBuffer, sizeof (readBuffer), 0);
}
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы
Bell Integrator Ульяновск
До 400 000 ₽
Bell Integrator Ижевск
До 400 000 ₽
Bell Integrator Хабаровск
До 400 000 ₽