4

I need to write Java binding for GPIO library. Decided to go with JNI for the purpose. All of the references have examples with using standard C libraries with functions such as printf or, from scientific library with method such as multiply. The library for which I need to write Java binding has macros, structs, with types such as __u32 which I am unable to see mapped to Java. Until now have watched some youtube videos, looking at JNI programmers's guide which was recommended(but it is very old) and looking at an ibm documentation on JNI https://docs.oracle.com/javase/8/docs/technotes/guides/jni/spec/types.html has not been very helpful. https://github.com/java-native-access/jna/blob/master/www/Mappings.md has mappings but in the library there are types such as __u32 for which there is no corresponding Java type https://docs.oracle.com/javase/8/docs/technotes/guides/jni/spec/types.html, even the official documentation has no mappingfor __u32 or unsigned 32/64 bit

Is there any tools that can help me?

Where can I find a good reference for this?

Or am I approaching this completely wrong?

Should I go for JNA or some other option?

Meenohara
  • 314
  • 4
  • 9
  • 2
    Please don't cross post questions about unwritten code on Code Review. – pacmaninbw Jul 20 '22 at 15:29
  • 2
    Regarding unsigned integers: Do you actually need to do anything with them in Java? If you're just passing them around, any 4-byte/8-byte data type will do and you can just as well use the Java int/long (but even byte arrays would work) because the sign only matters when some code needs to interpret the sign bit. If you actually need to compute something you could use long for unsigned 32-bit and BigIntegers for unsigned 64-bit, just be careful when converting them back – user2543253 Jul 21 '22 at 10:12

1 Answers1

0

Java is not a language I would use for programming hardware, I recently took a class in Embedded Programming where the instructor specifically said don't use Java.

This article does talk about how to convert unsigned integers to integers for Java.

I'm not sure why the example you have is __u32, it is more appropriate in C to use stdint.h which provides uint32_t.

Unsigned integer values are used in GPIO to toggle boolean values that are packed into addresses in the hardware. An unsigned char (8 bits), unsigned short (16 bits) or unsigned int (generally 32 bits for this use) contain control values for digital hardware, to reduce the address space necessary (memory on a device) these bits grouped together by function and packed into a single unsigned data type.

You can find examples of this in device data sheets.

pacmaninbw
  • 439
  • 1
  • 10
  • 22
  • Thank you @pacmaninbw I did not know that Java is not recommended for this purpose because I messaged a developer on linux-gpio mailing list and they thought it is good to have a Java binding, did not mention about unsuitability of Java. Rust binding is ongoing I do not know much about either embedded programming or JNI, this is why I had to post this question seeking help – Meenohara Jul 25 '22 at 14:04
  • It’s not an answer. Moreover, talking about direct hardware programming is for microcontroller and not for OS level of software communications. – 0andriy Jul 30 '22 at 08:27