Skip to content

Commit

Permalink
feat(protocol/c-bus): Continued implementing the driver core
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisdutz committed Mar 3, 2022
1 parent befcbd8 commit e6f462f
Show file tree
Hide file tree
Showing 6 changed files with 229 additions and 12 deletions.
8 changes: 4 additions & 4 deletions plc4j/drivers/c-bus/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -124,16 +124,16 @@
<version>0.10.0-SNAPSHOT</version>
</dependency>

<!--dependency>
<dependency>
<groupId>org.apache.plc4x</groupId>
<artifactId>plc4j-transport-tcp</artifactId>
<version>0.10.0-SNAPSHOT</version>
</dependency-->
</dependency>

<!--dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-buffer</artifactId>
</dependency-->
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,46 @@
*/
package org.apache.plc4x.java.cbus;

import io.netty.buffer.ByteBuf;
import org.apache.plc4x.java.api.value.PlcValueHandler;
import org.apache.plc4x.java.cbus.configuration.CBusConfiguration;
import org.apache.plc4x.java.cbus.context.CBusDriverContext;
import org.apache.plc4x.java.cbus.protocol.CBusProtocolLogic;
import org.apache.plc4x.java.cbus.readwrite.CBusCommand;
import org.apache.plc4x.java.spi.configuration.Configuration;
import org.apache.plc4x.java.spi.connection.GeneratedDriverBase;
import org.apache.plc4x.java.spi.connection.PlcFieldHandler;
import org.apache.plc4x.java.spi.connection.ProtocolStackConfigurer;
import org.apache.plc4x.java.spi.connection.SingleProtocolStackConfigurer;

import java.util.function.Consumer;
import java.util.function.ToIntFunction;

public class CBusDriver extends GeneratedDriverBase<CBusCommand> {

@Override
public String getProtocolCode() {
return null;
return "c-bus";
}

@Override
public String getProtocolName() {
return null;
return "Clipsal C-Bus";
}

@Override
protected String getDefaultTransport() {
return "tcp";
}

@Override
protected boolean canRead() {
return true;
}

@Override
protected Class<? extends Configuration> getConfigurationType() {
return null;
return CBusConfiguration.class;
}

@Override
Expand All @@ -52,12 +71,37 @@ protected PlcValueHandler getValueHandler() {
}

@Override
protected String getDefaultTransport() {
return null;
protected ProtocolStackConfigurer<CBusCommand> getStackConfigurer() {
return SingleProtocolStackConfigurer.builder(CBusCommand.class, CBusCommand::staticParse)
.withProtocol(CBusProtocolLogic.class)
.withDriverContext(CBusDriverContext.class)
.withPacketSizeEstimator(ByteLengthEstimator.class)
.withCorruptPacketRemover(CorruptPackageCleaner.class)
.build();
}

@Override
protected ProtocolStackConfigurer<CBusCommand> getStackConfigurer() {
return null;
public static class ByteLengthEstimator implements ToIntFunction<ByteBuf> {
@Override
public int applyAsInt(ByteBuf byteBuf) {
for(int i = 0; i < byteBuf.readableBytes() - 1; i++) {
if((byteBuf.getUnsignedByte(i) == (short) 0x0D) && (byteBuf.getUnsignedByte(i + 1) == (short) 0x0A)) {
return i + 1;
}
}
return -1;
}
}

/** Consumes all Bytes till a backslash is found */
public static class CorruptPackageCleaner implements Consumer<ByteBuf> {
@Override
public void accept(ByteBuf byteBuf) {
// Consume every byte until the next byte would be a backslash.
while (byteBuf.getUnsignedByte(0) != '\\') {
// Just consume the bytes till the next possible start position.
byteBuf.readByte();
}
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/
package org.apache.plc4x.java.cbus.configuration;

import org.apache.plc4x.java.spi.configuration.Configuration;
import org.apache.plc4x.java.spi.configuration.annotations.ConfigurationParameter;
import org.apache.plc4x.java.spi.configuration.annotations.defaults.BooleanDefaultValue;
import org.apache.plc4x.java.transport.tcp.TcpTransportConfiguration;

public class CBusConfiguration implements Configuration, TcpTransportConfiguration {

@ConfigurationParameter("srchk")
@BooleanDefaultValue(false)
public boolean srchk = false;

public boolean isSrchk() {
return srchk;
}

public void setSrchk(boolean srchk) {
this.srchk = srchk;
}

@Override
public int getDefaultPort() {
return 123;//CBusDriver.C_BUS_TCP_PORT;
}

@Override
public String toString() {
return "Configuration{" +
"srchk=" + srchk +
'}';
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/
package org.apache.plc4x.java.cbus.context;

import org.apache.plc4x.java.cbus.configuration.CBusConfiguration;
import org.apache.plc4x.java.spi.configuration.HasConfiguration;
import org.apache.plc4x.java.spi.context.DriverContext;

public class CBusDriverContext implements DriverContext, HasConfiguration<CBusConfiguration> {

@Override
public void setConfiguration(CBusConfiguration configuration) {
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/
package org.apache.plc4x.java.cbus.protocol;

import org.apache.plc4x.java.api.messages.*;
import org.apache.plc4x.java.cbus.readwrite.CBusCommand;
import org.apache.plc4x.java.spi.ConversationContext;
import org.apache.plc4x.java.spi.Plc4xProtocolBase;
import org.apache.plc4x.java.spi.context.DriverContext;
import org.apache.plc4x.java.spi.messages.*;
import org.apache.plc4x.java.spi.transaction.RequestTransactionManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.CompletableFuture;

public class CBusProtocolLogic extends Plc4xProtocolBase<CBusCommand> {

private final Logger logger = LoggerFactory.getLogger(CBusProtocolLogic.class);

private RequestTransactionManager tm;

@Override
public void setDriverContext(DriverContext driverContext) {
super.setDriverContext(driverContext);

// Initialize Transaction Manager.
// Until the number of concurrent requests is successfully negotiated we set it to a
// maximum of only one request being able to be sent at a time. During the login process
// No concurrent requests can be sent anyway. It will be updated when receiving the
// S7ParameterSetupCommunication response.
this.tm = new RequestTransactionManager(1);
}

@Override
public void onConnect(ConversationContext<CBusCommand> context) {

}

@Override
public CompletableFuture<PlcReadResponse> read(PlcReadRequest readRequest) {
CompletableFuture<PlcReadResponse> future = new CompletableFuture<>();
DefaultPlcReadRequest request = (DefaultPlcReadRequest) readRequest;
return future;
}

/**
* This method is only called when there is no Response Handler.
*/
@Override
protected void decode(ConversationContext<CBusCommand> context, CBusCommand msg) throws Exception {
}

@Override
public void close(ConversationContext<CBusCommand> context) {
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -198,4 +198,20 @@ void ExtendedFormatStatusReply3() throws Exception {
}
}

@Nested
class CBusQuickStartGuideTest {

// 4.2.9.1
@Test
void pointToPointCommandDirect() throws Exception {
byte[] bytes = Hex.decodeHex(BACKSLASH + "0538007902D4" + CR + LF);
ReadBufferByteBased readBufferByteBased = new ReadBufferByteBased(bytes);
CBusCommand msg = CBusCommand.staticParse(readBufferByteBased, true);
assertThat(msg)
.isNotNull();
System.out.println(msg);
}

}

}

0 comments on commit e6f462f

Please sign in to comment.