.
+ * #L%
+ */
+
+
+import com.pi4j.io.onewire.OneWire;
+import com.pi4j.io.onewire.OneWireConfig;
+import com.pi4j.io.onewire.OneWireProviderBase;
+import com.pi4j.plugin.linuxfs.internal.LinuxOneWire;
+
+/**
+ * The {@code LinuxFsOneWireProviderImpl} class implements the {@link LinuxFsOneWireProvider} interface
+ * and serves as the concrete implementation for the OneWire provider based on the Linux filesystem.
+ *
+ * This provider manages the creation and configuration of OneWire instances that interact with
+ * the Linux filesystem for hardware communication.
+ */
+public class LinuxFsOneWireProviderImpl extends OneWireProviderBase implements LinuxFsOneWireProvider {
+
+ /**
+ * The file system path where OneWire devices are located.
+ * This path is used to initialize and manage OneWire interactions.
+ */
+ final String oneWireFileSystemPath;
+
+ /**
+ * Constructs a new instance of {@code LinuxFsOneWireProviderImpl} with the specified filesystem path.
+ *
+ * @param oneWireFileSystemPath the path in the Linux filesystem where OneWire devices are located.
+ */
+ public LinuxFsOneWireProviderImpl(String oneWireFileSystemPath) {
+ this.id = ID; // Assign the unique provider ID
+ this.name = NAME; // Assign the provider name
+ this.oneWireFileSystemPath = oneWireFileSystemPath; // Store the specified file system path
+ }
+
+ /**
+ * Returns the priority of this provider. Providers with lower priority values
+ * are preferred during provider selection in the Pi4J framework.
+ *
+ * @return the priority value for this provider, default is 50.
+ */
+ @Override
+ public int getPriority() {
+ return 50;
+ }
+
+ /**
+ * Creates a new {@link OneWire} instance based on the provided configuration.
+ *
+ * This method initializes a filesystem-based OneWire instance using the provided
+ * configuration and registers it with the Pi4J context registry.
+ *
+ * @param config the {@link OneWireConfig} containing configuration details for the OneWire instance.
+ * @return a {@link OneWire} object representing the created OneWire instance.
+ */
+ @Override
+ public OneWire create(OneWireConfig config) {
+ LinuxOneWire oneWire = new LinuxOneWire(this.oneWireFileSystemPath);
+ LinuxFsOneWire fsOneWire = new LinuxFsOneWire(oneWire, this, config);
+ this.context.registry().add(fsOneWire);
+ return fsOneWire;
+ }
+}
+
From 5ad14b5cf09ca10ab7d7202742e28d944c4124e2 Mon Sep 17 00:00:00 2001
From: Dariusz Zbyrad
Date: Fri, 3 Jan 2025 08:05:22 +0100
Subject: [PATCH 3/7] Integrated `LinuxFsOneWireProvider` into the
LinuxFsPlugin I/O Providers array.
---
.../main/java/com/pi4j/plugin/linuxfs/LinuxFsPlugin.java | 8 +++++---
1 file changed, 5 insertions(+), 3 deletions(-)
diff --git a/plugins/pi4j-plugin-linuxfs/src/main/java/com/pi4j/plugin/linuxfs/LinuxFsPlugin.java b/plugins/pi4j-plugin-linuxfs/src/main/java/com/pi4j/plugin/linuxfs/LinuxFsPlugin.java
index 1a61f34f..c1b1a133 100644
--- a/plugins/pi4j-plugin-linuxfs/src/main/java/com/pi4j/plugin/linuxfs/LinuxFsPlugin.java
+++ b/plugins/pi4j-plugin-linuxfs/src/main/java/com/pi4j/plugin/linuxfs/LinuxFsPlugin.java
@@ -36,6 +36,7 @@
import com.pi4j.plugin.linuxfs.provider.i2c.LinuxFsI2CProvider;
import com.pi4j.plugin.linuxfs.provider.gpio.digital.LinuxFsDigitalInputProvider;
import com.pi4j.plugin.linuxfs.provider.gpio.digital.LinuxFsDigitalOutputProvider;
+import com.pi4j.plugin.linuxfs.provider.onewire.LinuxFsOneWireProvider;
import com.pi4j.plugin.linuxfs.provider.pwm.LinuxFsPwmProvider;
import com.pi4j.plugin.linuxfs.internal.LinuxPwm;
import com.pi4j.provider.Provider;
@@ -127,7 +128,8 @@ public void initialize(PluginService service) {
// get Linux file system path for GPIO & PWM
String gpioFileSystemPath = DEFAULT_GPIO_FILESYSTEM_PATH;
- String pwmFileSystemPath = DEFAULT_PWM_FILESYSTEM_PATH; //TODO add new one
+ String pwmFileSystemPath = DEFAULT_PWM_FILESYSTEM_PATH;
+ String oneWireFileSystemPath = DEFAULT_ONE_WIRE_FILESYSTEM_PATH;
int pwmChip;
if(BoardInfoHelper.usesRP1()) {
@@ -161,8 +163,8 @@ public void initialize(PluginService service) {
LinuxFsDigitalInputProvider.newInstance(gpioFileSystemPath),
LinuxFsDigitalOutputProvider.newInstance(gpioFileSystemPath),
LinuxFsPwmProvider.newInstance(pwmFileSystemPath, pwmChip),
- LinuxFsI2CProvider.newInstance()
- //TODO add 1-Wire provider
+ LinuxFsI2CProvider.newInstance(),
+ LinuxFsOneWireProvider.newInstance(oneWireFileSystemPath)
};
// register the LinuxFS I/O Providers with the plugin service
From c91f633f21c5ba75b482c08cc8b536ab3b681ee0 Mon Sep 17 00:00:00 2001
From: "dariusz.zbyrad"
Date: Fri, 3 Jan 2025 14:50:07 +0100
Subject: [PATCH 4/7] Add OneWire provider support in LinuxFs plugin
---
.../linuxfs/provider/onewire/LinuxFsOneWireProviderImpl.java | 2 +-
plugins/pi4j-plugin-linuxfs/src/main/java/module-info.java | 1 +
2 files changed, 2 insertions(+), 1 deletion(-)
diff --git a/plugins/pi4j-plugin-linuxfs/src/main/java/com/pi4j/plugin/linuxfs/provider/onewire/LinuxFsOneWireProviderImpl.java b/plugins/pi4j-plugin-linuxfs/src/main/java/com/pi4j/plugin/linuxfs/provider/onewire/LinuxFsOneWireProviderImpl.java
index c344ea54..3b2e54c4 100644
--- a/plugins/pi4j-plugin-linuxfs/src/main/java/com/pi4j/plugin/linuxfs/provider/onewire/LinuxFsOneWireProviderImpl.java
+++ b/plugins/pi4j-plugin-linuxfs/src/main/java/com/pi4j/plugin/linuxfs/provider/onewire/LinuxFsOneWireProviderImpl.java
@@ -81,7 +81,7 @@ public int getPriority() {
*/
@Override
public OneWire create(OneWireConfig config) {
- LinuxOneWire oneWire = new LinuxOneWire(this.oneWireFileSystemPath);
+ LinuxOneWire oneWire = new LinuxOneWire(this.oneWireFileSystemPath, config.device());
LinuxFsOneWire fsOneWire = new LinuxFsOneWire(oneWire, this, config);
this.context.registry().add(fsOneWire);
return fsOneWire;
diff --git a/plugins/pi4j-plugin-linuxfs/src/main/java/module-info.java b/plugins/pi4j-plugin-linuxfs/src/main/java/module-info.java
index 365fd0ee..3c8d2d69 100644
--- a/plugins/pi4j-plugin-linuxfs/src/main/java/module-info.java
+++ b/plugins/pi4j-plugin-linuxfs/src/main/java/module-info.java
@@ -39,6 +39,7 @@
exports com.pi4j.plugin.linuxfs.provider.gpio.digital;
exports com.pi4j.plugin.linuxfs.provider.pwm;
exports com.pi4j.plugin.linuxfs.provider.i2c;
+ exports com.pi4j.plugin.linuxfs.provider.onewire;
provides com.pi4j.extension.Plugin
with LinuxFsPlugin;
From d6a7b5d9ba06eb41f2185a3ce327edc75462d229 Mon Sep 17 00:00:00 2001
From: Dariusz Zbyrad
Date: Thu, 9 Jan 2025 14:40:20 +0100
Subject: [PATCH 5/7] Improved file write validation error messages
---
.../plugin/linuxfs/provider/onewire/LinuxFsOneWire.java | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)
diff --git a/plugins/pi4j-plugin-linuxfs/src/main/java/com/pi4j/plugin/linuxfs/provider/onewire/LinuxFsOneWire.java b/plugins/pi4j-plugin-linuxfs/src/main/java/com/pi4j/plugin/linuxfs/provider/onewire/LinuxFsOneWire.java
index aaa014a7..42e2bda4 100644
--- a/plugins/pi4j-plugin-linuxfs/src/main/java/com/pi4j/plugin/linuxfs/provider/onewire/LinuxFsOneWire.java
+++ b/plugins/pi4j-plugin-linuxfs/src/main/java/com/pi4j/plugin/linuxfs/provider/onewire/LinuxFsOneWire.java
@@ -183,14 +183,16 @@ private void validateFileForRead(Path filePath) throws IOException {
* Validates that a file exists and is writable.
*
* @param filePath the path of the file to validate.
- * @throws IOException if the file does not exist or is not writable.
+ * @throws IOException if the file does not exist or is not writable due to permissions or other restrictions.
+ * On Linux, consider using 'sudo' if this issue persists.
*/
private void validateFileForWrite(Path filePath) throws IOException {
if (!Files.exists(filePath)) {
throw new IOException("File " + filePath + " does not exist.");
}
if (!Files.isWritable(filePath)) {
- throw new IOException("File " + filePath + " is not writable.");
+ throw new IOException("File " + filePath + " is not writable. This may be due to permissions or access restrictions. "
+ + "On Linux, consider using 'sudo' to resolve this issue.");
}
}
}
From c0c79f3f4aa8fa5db5d3b40f1db05de733034df2 Mon Sep 17 00:00:00 2001
From: Dariusz Zbyrad
Date: Fri, 10 Jan 2025 20:08:50 +0100
Subject: [PATCH 6/7] Added mock for the 1-Wire
---
.../com/pi4j/internal/ProviderAliases.java | 12 ++
.../onewire/OneWireFileDataReaderWriter.java | 17 +-
.../main/java/com/pi4j/test/OneWireTest.java | 6 +-
.../test/io/onewire/OneWireRawDataTest.java | 103 +++++++++
.../provider/onewire/LinuxFsOneWire.java | 21 +-
.../main/java/com/pi4j/plugin/mock/Mock.java | 6 +
.../java/com/pi4j/plugin/mock/MockPlugin.java | 22 +-
.../plugin/mock/platform/MockPlatform.java | 5 +-
.../mock/provider/onewire/MockOneWire.java | 198 ++++++++++++++++++
.../provider/onewire/MockOneWireProvider.java | 46 ++++
.../onewire/MockOneWireProviderImpl.java | 60 ++++++
.../src/main/java/module-info.java | 1 +
12 files changed, 479 insertions(+), 18 deletions(-)
create mode 100644 pi4j-test/src/test/java/com/pi4j/test/io/onewire/OneWireRawDataTest.java
create mode 100644 plugins/pi4j-plugin-mock/src/main/java/com/pi4j/plugin/mock/provider/onewire/MockOneWire.java
create mode 100644 plugins/pi4j-plugin-mock/src/main/java/com/pi4j/plugin/mock/provider/onewire/MockOneWireProvider.java
create mode 100644 plugins/pi4j-plugin-mock/src/main/java/com/pi4j/plugin/mock/provider/onewire/MockOneWireProviderImpl.java
diff --git a/pi4j-core/src/main/java/com/pi4j/internal/ProviderAliases.java b/pi4j-core/src/main/java/com/pi4j/internal/ProviderAliases.java
index 95d216fd..58ddab48 100644
--- a/pi4j-core/src/main/java/com/pi4j/internal/ProviderAliases.java
+++ b/pi4j-core/src/main/java/com/pi4j/internal/ProviderAliases.java
@@ -31,6 +31,7 @@
import com.pi4j.io.gpio.digital.DigitalInputProvider;
import com.pi4j.io.gpio.digital.DigitalOutputProvider;
import com.pi4j.io.i2c.I2CProvider;
+import com.pi4j.io.onewire.OneWireProvider;
import com.pi4j.io.pwm.PwmProvider;
import com.pi4j.io.serial.SerialProvider;
import com.pi4j.io.spi.SpiProvider;
@@ -282,4 +283,15 @@ default T getI2CProvider() throws ProviderException{
default T getSerialProvider() throws ProviderException{
return this.serial();
}
+
+ /**
+ * 1-Wire
+ *
+ * @param a T object.
+ * @return a T object.
+ * @throws ProviderException if any.
+ */
+ default T oneWire() throws ProviderException {
+ return this.provider(IOType.ONE_WIRE);
+ }
}
diff --git a/pi4j-core/src/main/java/com/pi4j/io/onewire/OneWireFileDataReaderWriter.java b/pi4j-core/src/main/java/com/pi4j/io/onewire/OneWireFileDataReaderWriter.java
index 2fc4f918..bf523a80 100644
--- a/pi4j-core/src/main/java/com/pi4j/io/onewire/OneWireFileDataReaderWriter.java
+++ b/pi4j-core/src/main/java/com/pi4j/io/onewire/OneWireFileDataReaderWriter.java
@@ -27,17 +27,17 @@
import com.pi4j.io.exception.IOException;
-import java.util.Collection;
+import java.util.List;
public interface OneWireFileDataReaderWriter {
/**
* Read the entire content of a file on the device.
*
* @param fileName The name of the file to read.
- * @return The file content as array.
+ * @return The file content as List.
* @throws IOException if the file does not exist, is not readable, or another I/O error occurs.
*/
- Collection readFile(String fileName) throws IOException;
+ List readFile(String fileName) throws IOException;
/**
* Read a single line from a file on the device.
@@ -46,7 +46,16 @@ public interface OneWireFileDataReaderWriter {
* @return The first line of the file content as a string.
* @throws IOException if the file does not exist, is not readable, or another I/O error occurs.
*/
- String readFileLine(String fileName) throws IOException;
+ String readFirstLine(String fileName) throws IOException;
+
+ /**
+ * Read the entire content of a file on the device.
+ *
+ * @param fileName The name of the file to read.
+ * @return The file content as array of bytes.
+ * @throws IOException if the file does not exist, is not readable, or another I/O error occurs.
+ */
+ byte[] readFileAsBytes(String fileName) throws IOException;
/**
* Write a single byte to a file on the device.
diff --git a/pi4j-test/src/main/java/com/pi4j/test/OneWireTest.java b/pi4j-test/src/main/java/com/pi4j/test/OneWireTest.java
index 6cdce39d..da478bf1 100644
--- a/pi4j-test/src/main/java/com/pi4j/test/OneWireTest.java
+++ b/pi4j-test/src/main/java/com/pi4j/test/OneWireTest.java
@@ -54,7 +54,7 @@ private static void handleDS18B20(Context pi4j) {
logger.info("Set DS18B20 resolution to {} bits", DS18B20_RESOLUTION);
// Read temperature
- String temperature = ds18b20.readFileLine("temperature");
+ String temperature = ds18b20.readFirstLine("temperature");
// Parse and log temperature
try {
@@ -81,7 +81,7 @@ private static void handleDS2413(Context pi4j) {
var ds2413 = pi4j.create(ds2413Config);
// Read current state before setting new value
- String stateBefore = ds2413.readFileLine("state");
+ String stateBefore = ds2413.readFirstLine("state");
logger.info("Current state before update: {}", stateBefore);
// Set new state to 0xf2 (State 1 ON, State 2 OFF)
@@ -89,7 +89,7 @@ private static void handleDS2413(Context pi4j) {
logger.info("Set DS2413 state to 0xf2 (State 1 ON, State 2 OFF)");
// Read current state after setting new value
- String stateAfter = ds2413.readFileLine("state");
+ String stateAfter = ds2413.readFirstLine("state");
logger.info("Current state after update: {}", stateAfter);
} catch (Exception e) {
logger.error("Failed to interact with DS2413 sensor: {}", e.getMessage());
diff --git a/pi4j-test/src/test/java/com/pi4j/test/io/onewire/OneWireRawDataTest.java b/pi4j-test/src/test/java/com/pi4j/test/io/onewire/OneWireRawDataTest.java
new file mode 100644
index 00000000..d1e02ad5
--- /dev/null
+++ b/pi4j-test/src/test/java/com/pi4j/test/io/onewire/OneWireRawDataTest.java
@@ -0,0 +1,103 @@
+package com.pi4j.test.io.onewire;
+
+/*-
+ * #%L
+ * **********************************************************************
+ * ORGANIZATION : Pi4J
+ * PROJECT : Pi4J :: TESTING :: Unit/Integration Tests
+ * FILENAME : OneWireRawDataTest.java
+ *
+ * This file is part of the Pi4J project. More information about
+ * this project can be found here: https://pi4j.com/
+ * **********************************************************************
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * #L%
+ */
+
+import com.pi4j.Pi4J;
+import com.pi4j.context.Context;
+import com.pi4j.exception.Pi4JException;
+import com.pi4j.io.onewire.OneWire;
+import com.pi4j.plugin.mock.provider.onewire.MockOneWire;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.TestInstance;
+import org.junit.jupiter.api.TestInstance.Lifecycle;
+
+import java.util.List;
+
+import static org.junit.jupiter.api.Assertions.assertArrayEquals;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+
+@TestInstance(Lifecycle.PER_CLASS)
+public class OneWireRawDataTest {
+
+ private Context pi4j;
+
+ @BeforeEach
+ public void beforeTest() throws Pi4JException {
+ pi4j = Pi4J.newContextBuilder()
+ .autoDetectMockPlugins()
+ .autoDetectPlatforms()
+ .build();
+ }
+
+ @AfterEach
+ public void afterTest() {
+ try {
+ pi4j.shutdown();
+ } catch (Pi4JException ignored) { /* do nothing */ }
+ }
+
+ @Test
+ public void shouldWriteAndReadStringData() {
+ var content = "24000" + System.lineSeparator() + "48000";
+ var oneWire = createMockOneWire("28-000008d6bac6");
+
+ oneWire.writeFile("content", content);
+
+ var fileContent = oneWire.readFile("content");
+
+ assertNotNull(fileContent);
+ assertEquals(2, fileContent.size());
+ assertEquals("24000", fileContent.get(0));
+ assertEquals("48000", fileContent.get(1));
+ assertEquals(content, String.join(System.lineSeparator(), fileContent));
+ }
+
+ @Test
+ public void shouldWriteAndReadByteData() {
+ var content = new byte[]{0x01, 0x20, 0x30};
+ var oneWire = createMockOneWire("28-000008d6bac6");
+
+ oneWire.writeFile("content", content);
+
+ var fileContent = oneWire.readFileAsBytes("content");
+
+ assertNotNull(fileContent);
+ assertEquals(3, fileContent.length);
+ assertArrayEquals(content, fileContent);
+ }
+
+ private MockOneWire createMockOneWire(String deviceId) {
+ var config = OneWire.newConfigBuilder(pi4j)
+ .id("my-one-wire")
+ .name("My 1-Wire")
+ .device(deviceId)
+ .build();
+ return (MockOneWire) pi4j.oneWire().create(config);
+ }
+}
diff --git a/plugins/pi4j-plugin-linuxfs/src/main/java/com/pi4j/plugin/linuxfs/provider/onewire/LinuxFsOneWire.java b/plugins/pi4j-plugin-linuxfs/src/main/java/com/pi4j/plugin/linuxfs/provider/onewire/LinuxFsOneWire.java
index 42e2bda4..7864ca7b 100644
--- a/plugins/pi4j-plugin-linuxfs/src/main/java/com/pi4j/plugin/linuxfs/provider/onewire/LinuxFsOneWire.java
+++ b/plugins/pi4j-plugin-linuxfs/src/main/java/com/pi4j/plugin/linuxfs/provider/onewire/LinuxFsOneWire.java
@@ -99,7 +99,7 @@ public List readFile(String fileName) throws IOException {
* @return the first line of the file as a string, or {@code null} if the file is empty.
* @throws IOException if the file does not exist, is not readable, or another I/O error occurs.
*/
- public String readFileLine(String fileName) throws IOException {
+ public String readFirstLine(String fileName) throws IOException {
Path filePath = Paths.get(oneWire.getDevicePath(), fileName);
validateFileForRead(filePath);
@@ -110,6 +110,25 @@ public String readFileLine(String fileName) throws IOException {
}
}
+ /**
+ * Reads the entire content of a file as a byte array.
+ *
+ * @param fileName the name of the file to read.
+ * @return a byte array containing the file's content.
+ * @throws IOException if the file does not exist, is not readable, or another I/O error occurs.
+ */
+ @Override
+ public byte[] readFileAsBytes(String fileName) throws IOException {
+ Path filePath = Paths.get(oneWire.getDevicePath(), fileName);
+ validateFileForRead(filePath);
+
+ try {
+ return Files.readAllBytes(filePath);
+ } catch (java.io.IOException e) {
+ throw new IOException(e);
+ }
+ }
+
/**
* Writes a single byte to a file.
*
diff --git a/plugins/pi4j-plugin-mock/src/main/java/com/pi4j/plugin/mock/Mock.java b/plugins/pi4j-plugin-mock/src/main/java/com/pi4j/plugin/mock/Mock.java
index 4fada79c..d7b4dda0 100644
--- a/plugins/pi4j-plugin-mock/src/main/java/com/pi4j/plugin/mock/Mock.java
+++ b/plugins/pi4j-plugin-mock/src/main/java/com/pi4j/plugin/mock/Mock.java
@@ -94,4 +94,10 @@ public class Mock {
public static final String SERIAL_PROVIDER_NAME = NAME + " Serial Provider";
/** Constant SERIAL_PROVIDER_ID="ID + -serial"
*/
public static final String SERIAL_PROVIDER_ID = ID + "-serial";
+
+ // 1-Wire Provider name and unique ID
+ /** Constant ONE_WIRE_PROVIDER_NAME="NAME + Serial Provider"
*/
+ public static final String ONE_WIRE_PROVIDER_NAME = NAME + " 1-Wire Provider";
+ /** Constant ONE_WIRE_PROVIDER_ID="ID + -serial"
*/
+ public static final String ONE_WIRE_PROVIDER_ID = ID + "-one-wire";
}
diff --git a/plugins/pi4j-plugin-mock/src/main/java/com/pi4j/plugin/mock/MockPlugin.java b/plugins/pi4j-plugin-mock/src/main/java/com/pi4j/plugin/mock/MockPlugin.java
index f5ca2beb..5fd42bda 100644
--- a/plugins/pi4j-plugin-mock/src/main/java/com/pi4j/plugin/mock/MockPlugin.java
+++ b/plugins/pi4j-plugin-mock/src/main/java/com/pi4j/plugin/mock/MockPlugin.java
@@ -35,6 +35,7 @@
import com.pi4j.plugin.mock.provider.gpio.digital.MockDigitalInputProvider;
import com.pi4j.plugin.mock.provider.gpio.digital.MockDigitalOutputProvider;
import com.pi4j.plugin.mock.provider.i2c.MockI2CProvider;
+import com.pi4j.plugin.mock.provider.onewire.MockOneWireProvider;
import com.pi4j.plugin.mock.provider.pwm.MockPwmProvider;
import com.pi4j.plugin.mock.provider.serial.MockSerialProvider;
import com.pi4j.plugin.mock.provider.spi.MockSpiProvider;
@@ -49,14 +50,15 @@
public class MockPlugin implements Plugin {
private final Provider[] providers = {
- MockAnalogInputProvider.newInstance(),
- MockAnalogOutputProvider.newInstance(),
- MockDigitalInputProvider.newInstance(),
- MockDigitalOutputProvider.newInstance(),
- MockPwmProvider.newInstance(),
- MockI2CProvider.newInstance(),
- MockSpiProvider.newInstance(),
- MockSerialProvider.newInstance(),
+ MockAnalogInputProvider.newInstance(),
+ MockAnalogOutputProvider.newInstance(),
+ MockDigitalInputProvider.newInstance(),
+ MockDigitalOutputProvider.newInstance(),
+ MockPwmProvider.newInstance(),
+ MockI2CProvider.newInstance(),
+ MockSpiProvider.newInstance(),
+ MockSerialProvider.newInstance(),
+ MockOneWireProvider.newInstance()
};
@Override
@@ -64,7 +66,9 @@ public boolean isMock() {
return true;
}
- /** {@inheritDoc} */
+ /**
+ * {@inheritDoc}
+ */
@Override
public void initialize(PluginService service) {
diff --git a/plugins/pi4j-plugin-mock/src/main/java/com/pi4j/plugin/mock/platform/MockPlatform.java b/plugins/pi4j-plugin-mock/src/main/java/com/pi4j/plugin/mock/platform/MockPlatform.java
index 14e2d9ab..726fb459 100644
--- a/plugins/pi4j-plugin-mock/src/main/java/com/pi4j/plugin/mock/platform/MockPlatform.java
+++ b/plugins/pi4j-plugin-mock/src/main/java/com/pi4j/plugin/mock/platform/MockPlatform.java
@@ -36,6 +36,7 @@
import com.pi4j.plugin.mock.provider.gpio.digital.MockDigitalInputProvider;
import com.pi4j.plugin.mock.provider.gpio.digital.MockDigitalOutputProvider;
import com.pi4j.plugin.mock.provider.i2c.MockI2CProvider;
+import com.pi4j.plugin.mock.provider.onewire.MockOneWireProvider;
import com.pi4j.plugin.mock.provider.pwm.MockPwmProvider;
import com.pi4j.plugin.mock.provider.serial.MockSerialProvider;
import com.pi4j.plugin.mock.provider.spi.MockSpiProvider;
@@ -87,6 +88,8 @@ protected String[] getProviders() {
MockPwmProvider.ID,
MockSpiProvider.ID,
MockI2CProvider.ID,
- MockSerialProvider.ID };
+ MockSerialProvider.ID,
+ MockOneWireProvider.ID
+ };
}
}
diff --git a/plugins/pi4j-plugin-mock/src/main/java/com/pi4j/plugin/mock/provider/onewire/MockOneWire.java b/plugins/pi4j-plugin-mock/src/main/java/com/pi4j/plugin/mock/provider/onewire/MockOneWire.java
new file mode 100644
index 00000000..320b36b7
--- /dev/null
+++ b/plugins/pi4j-plugin-mock/src/main/java/com/pi4j/plugin/mock/provider/onewire/MockOneWire.java
@@ -0,0 +1,198 @@
+package com.pi4j.plugin.mock.provider.onewire;
+
+/*-
+ * #%L
+ * **********************************************************************
+ * ORGANIZATION : Pi4J
+ * PROJECT : Pi4J :: PLUGIN :: Mock Platform & Providers
+ * FILENAME : MockOneWire.java
+ *
+ * This file is part of the Pi4J project. More information about
+ * this project can be found here: https://pi4j.com/
+ * **********************************************************************
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Lesser Public License for more details.
+ *
+ * You should have received a copy of the GNU General Lesser Public
+ * License along with this program. If not, see
+ * .
+ * #L%
+ */
+
+import com.pi4j.io.exception.IOException;
+import com.pi4j.io.onewire.OneWire;
+import com.pi4j.io.onewire.OneWireBase;
+import com.pi4j.io.onewire.OneWireConfig;
+import com.pi4j.io.onewire.OneWireProvider;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.IntStream;
+
+import static java.nio.charset.StandardCharsets.UTF_8;
+
+/**
+ * Mock implementation of the OneWire interface for testing and simulation purposes.
+ * This class provides methods to read and write to mock 1-Wire files.
+ */
+public class MockOneWire extends OneWireBase implements OneWire {
+
+ private static final Logger logger = LoggerFactory.getLogger(MockOneWire.class);
+
+ // Stores the content of "files" as a map, where each file's content is a Byte array.
+ protected final Map content = new HashMap<>();
+
+ /**
+ * Constructs a new {@code MockOneWire} instance.
+ *
+ * @param provider the {@link OneWireProvider} used to manage 1-Wire communication.
+ * @param config the {@link OneWireConfig} specifying configuration settings for this instance.
+ */
+ public MockOneWire(OneWireProvider provider, OneWireConfig config) {
+ super(provider, config);
+ logger.debug("[{}::{}] :: CREATE(DEVICE={})",
+ provider.name(), this.id(), config.device());
+ }
+
+ /**
+ * Reads the entire content of a mock file and splits it into lines using the system's line separator.
+ *
+ * @param fileName the name of the file to read.
+ * @return a collection of strings, each representing a line from the file.
+ * @throws IOException if the file does not exist or cannot be read.
+ */
+ @Override
+ public List readFile(String fileName) throws IOException {
+ byte[] input = getContentFile(fileName);
+ String contentString = new String(input, UTF_8);
+ logger.debug("[{}::{}] :: READ FILE ({}): {}",
+ provider.name(), this.id(), fileName, contentString);
+ return Arrays.asList(contentString.split(System.lineSeparator()));
+ }
+
+ /**
+ * Reads the first line of a mock file.
+ *
+ * @param fileName the name of the file to read.
+ * @return the first line of the file, or an empty string if the file is empty or does not exist.
+ * @throws IOException if the file does not exist or cannot be read.
+ */
+ @Override
+ public String readFirstLine(String fileName) throws IOException {
+ byte[] input = getContentFile(fileName);
+ String contentString = new String(input, UTF_8);
+ String[] lines = contentString.split(System.lineSeparator());
+ String result = lines.length > 0 ? lines[0] : "";
+ logger.debug("[{}::{}] :: READ FILE LINE ({}): {}",
+ provider.name(), this.id(), fileName, result);
+ return result;
+ }
+
+ /**
+ * Reads the entire content of a mock file as a byte array.
+ *
+ * @param fileName the name of the file to read.
+ * @return a byte array containing the file's content, or an empty array if the file does not exist.
+ * @throws IOException if the file cannot be read.
+ */
+ @Override
+ public byte[] readFileAsBytes(String fileName) throws IOException {
+ byte[] input = getContentFile(fileName);
+ logger.debug("[{}::{}] :: READ FILE AS BYTES ({}): 0x{}",
+ provider.name(), this.id(), fileName, Arrays.toString(input));
+ return input;
+ }
+
+ /**
+ * Writes a single byte to a mock file, overwriting any existing content.
+ *
+ * @param fileName the name of the file to write.
+ * @param value the byte to write to the file.
+ * @throws IOException if the file cannot be written.
+ */
+ @Override
+ public void writeFile(String fileName, byte value) throws IOException {
+ content.put(fileName, new Byte[]{value});
+ logger.debug("[{}::{}] :: WRITE FILE ({}): 0x{}",
+ provider.name(), this.id(), fileName, String.format("%02X", value));
+ }
+
+ /**
+ * Writes a byte array to a mock file, overwriting any existing content.
+ *
+ * @param fileName the name of the file to write.
+ * @param values the byte array to write to the file.
+ * @throws IOException if the file cannot be written.
+ */
+ @Override
+ public void writeFile(String fileName, byte[] values) throws IOException {
+ content.put(fileName, toBoxed(values));
+ logger.debug("[{}::{}] :: WRITE FILE ({}): 0x{}",
+ provider.name(), this.id(), fileName, Arrays.toString(values));
+ }
+
+ /**
+ * Writes a string to a mock file as UTF-8 encoded bytes, overwriting any existing content.
+ *
+ * @param fileName the name of the file to write.
+ * @param content the string content to write to the file.
+ * @throws IOException if the file cannot be written.
+ */
+ @Override
+ public void writeFile(String fileName, String content) throws IOException {
+ byte[] byteArray = content.getBytes(UTF_8);
+ this.content.put(fileName, toBoxed(byteArray));
+ logger.debug("[{}::{}] :: WRITE FILE ({}): {}",
+ provider.name(), this.id(), fileName, content);
+ }
+
+ /**
+ * Retrieves the content of a mock file as a primitive byte array.
+ *
+ * @param fileName the name of the file to retrieve.
+ * @return the content of the file as a {@code byte[]} array, or an empty array if the file does not exist.
+ */
+ private byte[] getContentFile(String fileName) {
+ return toPrimitive(content.getOrDefault(fileName, new Byte[0]));
+ }
+
+ /**
+ * Converts a {@code Byte[]} array to a {@code byte[]} array.
+ *
+ * @param input the {@code Byte[]} array to convert.
+ * @return the converted {@code byte[]} array.
+ */
+ private static byte[] toPrimitive(Byte[] input) {
+ byte[] result = new byte[input.length];
+ IntStream
+ .range(0, input.length)
+ .forEach(i -> result[i] = input[i]);
+ return result;
+ }
+
+ /**
+ * Converts a {@code byte[]} array to a {@code Byte[]} array.
+ *
+ * @param input the {@code byte[]} array to convert.
+ * @return the converted {@code Byte[]} array.
+ */
+ private static Byte[] toBoxed(byte[] input) {
+ Byte[] result = new Byte[input.length];
+ IntStream
+ .range(0, input.length)
+ .forEach(i -> result[i] = input[i]);
+ return result;
+ }
+}
diff --git a/plugins/pi4j-plugin-mock/src/main/java/com/pi4j/plugin/mock/provider/onewire/MockOneWireProvider.java b/plugins/pi4j-plugin-mock/src/main/java/com/pi4j/plugin/mock/provider/onewire/MockOneWireProvider.java
new file mode 100644
index 00000000..2120a9a0
--- /dev/null
+++ b/plugins/pi4j-plugin-mock/src/main/java/com/pi4j/plugin/mock/provider/onewire/MockOneWireProvider.java
@@ -0,0 +1,46 @@
+package com.pi4j.plugin.mock.provider.onewire;
+
+/*-
+ * #%L
+ * **********************************************************************
+ * ORGANIZATION : Pi4J
+ * PROJECT : Pi4J :: PLUGIN :: Mock Platform & Providers
+ * FILENAME : MockSerialProvider.java
+ *
+ * This file is part of the Pi4J project. More information about
+ * this project can be found here: https://pi4j.com/
+ * **********************************************************************
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Lesser Public License for more details.
+ *
+ * You should have received a copy of the GNU General Lesser Public
+ * License along with this program. If not, see
+ * .
+ * #L%
+ */
+
+import com.pi4j.io.onewire.OneWireProvider;
+import com.pi4j.plugin.mock.Mock;
+
+public interface MockOneWireProvider extends OneWireProvider {
+ /** Constant NAME="Mock.ONE_WIRE_PROVIDER_NAME"
*/
+ String NAME = Mock.ONE_WIRE_PROVIDER_NAME;
+ /** Constant ID="Mock.ONE_WIRE_PROVIDER_ID"
*/
+ String ID = Mock.ONE_WIRE_PROVIDER_ID;
+ /**
+ * newInstance.
+ *
+ * @return a {@link MockOneWireProvider} object.
+ */
+ static MockOneWireProvider newInstance() {
+ return new MockOneWireProviderImpl();
+ }
+}
diff --git a/plugins/pi4j-plugin-mock/src/main/java/com/pi4j/plugin/mock/provider/onewire/MockOneWireProviderImpl.java b/plugins/pi4j-plugin-mock/src/main/java/com/pi4j/plugin/mock/provider/onewire/MockOneWireProviderImpl.java
new file mode 100644
index 00000000..44560e96
--- /dev/null
+++ b/plugins/pi4j-plugin-mock/src/main/java/com/pi4j/plugin/mock/provider/onewire/MockOneWireProviderImpl.java
@@ -0,0 +1,60 @@
+package com.pi4j.plugin.mock.provider.onewire;
+
+/*-
+ * #%L
+ * **********************************************************************
+ * ORGANIZATION : Pi4J
+ * PROJECT : Pi4J :: PLUGIN :: Mock Platform & Providers
+ * FILENAME : MockOneWireProviderImpl.java
+ *
+ * This file is part of the Pi4J project. More information about
+ * this project can be found here: https://pi4j.com/
+ * **********************************************************************
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Lesser Public License for more details.
+ *
+ * You should have received a copy of the GNU General Lesser Public
+ * License along with this program. If not, see
+ * .
+ * #L%
+ */
+
+import com.pi4j.io.onewire.OneWire;
+import com.pi4j.io.onewire.OneWireConfig;
+import com.pi4j.io.onewire.OneWireProviderBase;
+import com.pi4j.io.serial.Serial;
+import com.pi4j.io.serial.SerialConfig;
+import com.pi4j.io.serial.SerialProviderBase;
+import com.pi4j.plugin.mock.provider.i2c.MockI2C;
+
+public class MockOneWireProviderImpl extends OneWireProviderBase implements MockOneWireProvider {
+
+ /**
+ * Constructor for MockSerialProviderImpl.
+ */
+ public MockOneWireProviderImpl() {
+ this.id = ID;
+ this.name = NAME;
+ }
+
+ @Override
+ public OneWire create(OneWireConfig config) {
+ MockOneWire oneWire = new MockOneWire(this, config);
+ this.context.registry().add(oneWire);
+ return oneWire;
+ }
+
+ @Override
+ public int getPriority() {
+ // if the mock is loaded, then we most probably want to use it for testing
+ return 1000;
+ }
+}
diff --git a/plugins/pi4j-plugin-mock/src/main/java/module-info.java b/plugins/pi4j-plugin-mock/src/main/java/module-info.java
index 34d5fd4f..6c74a46c 100644
--- a/plugins/pi4j-plugin-mock/src/main/java/module-info.java
+++ b/plugins/pi4j-plugin-mock/src/main/java/module-info.java
@@ -38,6 +38,7 @@
exports com.pi4j.plugin.mock.provider.serial;
exports com.pi4j.plugin.mock.provider.spi;
exports com.pi4j.plugin.mock.provider.i2c;
+ exports com.pi4j.plugin.mock.provider.onewire;
provides com.pi4j.extension.Plugin
with com.pi4j.plugin.mock.MockPlugin;
From 62ae01cff421ed6356ce1d2859349b44edfe861b Mon Sep 17 00:00:00 2001
From: Dariusz Zbyrad
Date: Fri, 10 Jan 2025 21:08:19 +0100
Subject: [PATCH 7/7] Fixed build - removed unused imports
---
.../plugin/mock/provider/onewire/MockOneWireProviderImpl.java | 4 ----
1 file changed, 4 deletions(-)
diff --git a/plugins/pi4j-plugin-mock/src/main/java/com/pi4j/plugin/mock/provider/onewire/MockOneWireProviderImpl.java b/plugins/pi4j-plugin-mock/src/main/java/com/pi4j/plugin/mock/provider/onewire/MockOneWireProviderImpl.java
index 44560e96..70e97a6b 100644
--- a/plugins/pi4j-plugin-mock/src/main/java/com/pi4j/plugin/mock/provider/onewire/MockOneWireProviderImpl.java
+++ b/plugins/pi4j-plugin-mock/src/main/java/com/pi4j/plugin/mock/provider/onewire/MockOneWireProviderImpl.java
@@ -30,10 +30,6 @@
import com.pi4j.io.onewire.OneWire;
import com.pi4j.io.onewire.OneWireConfig;
import com.pi4j.io.onewire.OneWireProviderBase;
-import com.pi4j.io.serial.Serial;
-import com.pi4j.io.serial.SerialConfig;
-import com.pi4j.io.serial.SerialProviderBase;
-import com.pi4j.plugin.mock.provider.i2c.MockI2C;
public class MockOneWireProviderImpl extends OneWireProviderBase implements MockOneWireProvider {