Skip to content

Commit

Permalink
Merge pull request #364 from Pi4J/feature/#354
Browse files Browse the repository at this point in the history
Don't load native code when not running on a recognized board
  • Loading branch information
FDelporte committed Jun 13, 2024
2 parents 71d807e + 3b62080 commit 9fca5dc
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package com.pi4j.library.gpiod.internal;

import com.pi4j.boardinfo.definition.BoardModel;
import com.pi4j.boardinfo.util.BoardInfoHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.Closeable;
import java.util.*;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class GpioDContext implements Closeable {

Expand All @@ -31,6 +36,11 @@ public GpioDContext() {
}

public synchronized void initialize() {
if (BoardInfoHelper.current().getBoardModel() == BoardModel.UNKNOWN) {
logger.warn("Can't initialize GpioD context, board model is unknown");
return;
}

// already initialized
if (this.gpioChip != null)
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
* #L%
*/

import com.pi4j.boardinfo.definition.BoardModel;
import com.pi4j.boardinfo.util.BoardInfoHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -252,7 +254,12 @@ public static void loadLibraryFromClasspath(String path) throws IOException {
}
Files.copy(source, target, StandardCopyOption.REPLACE_EXISTING);
}

// Finally, load the library
System.load(target.toAbsolutePath().toString());
if (BoardInfoHelper.current().getBoardModel() == BoardModel.UNKNOWN) {
logger.warn("Can't load the library, board model is unknown");
} else {
System.load(target.toAbsolutePath().toString());
}
}
}
10 changes: 7 additions & 3 deletions pi4j-test/src/main/java/com/pi4j/test/About.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,16 @@ public void enumeratePlatforms(Context context) {
* @param context a {@link com.pi4j.context.Context} object.
* @throws Pi4JException if any.
*/
public void describeDeafultPlatform(Context context) throws Pi4JException {
public void describeDefaultPlatform(Context context) throws Pi4JException {
logger.info("=====================================================");
logger.info("DEFAULT (RUNTIME) PLATFORM ");
logger.info("=====================================================");
logger.info(" " + context.platform().name() + " [" + context.platform().id() + "]");
context.platform().describe().print(System.out);
if (context.platform() == null) {
logger.warn(" NOT DEFINED");
} else {
logger.info(" {}} [{}}]", context.platform().name(), context.platform().id());
context.platform().describe().print(System.out);
}
}

// public void enumeratePlatformProviders() throws Pi4JException {
Expand Down
2 changes: 1 addition & 1 deletion pi4j-test/src/main/java/com/pi4j/test/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public static void main(String[] args) {
About about = new About();
about.enumerateProviders(pi4j);
about.enumeratePlatforms(pi4j);
about.describeDeafultPlatform(pi4j);
about.describeDefaultPlatform(pi4j);
for(var ioType : IOType.values()){
about.enumerateProviders(pi4j, ioType);
}
Expand Down

0 comments on commit 9fca5dc

Please sign in to comment.