Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for KNX IP Communication: Unsecure Tunnel but secured device #142

Open
bhanujarya opened this issue Dec 7, 2024 · 0 comments
Open

Comments

@bhanujarya
Copy link

I am able to establish knx unsecure tunnel with the device , and able to write to device , the ip light is flickering as well but the actual knx light is not turning on
Can you please review my code and check if i am going correctly

`package knxtest;

import java.net.InetSocketAddress;
import java.time.LocalTime;
import java.util.Base64;
import java.util.HashMap;
import java.util.HashSet;
import java.util.HexFormat;
import java.util.Map;
import java.util.Set;

import javax.sql.rowset.serial.SerialArray;

import io.calimero.DetachEvent;
import io.calimero.GroupAddress;
import io.calimero.IndividualAddress;
import io.calimero.KNXException;
import io.calimero.SerialNumber;
import io.calimero.device.ProcessCommunicationResponder;
import io.calimero.device.ProcessCommunicationResponder;
import io.calimero.link.KNXNetworkLink;
import io.calimero.link.KNXNetworkLinkIP;
import io.calimero.link.medium.TPSettings;
import io.calimero.process.ProcessCommunicator;
import io.calimero.process.ProcessCommunicatorImpl;
import io.calimero.process.ProcessEvent;
import io.calimero.process.ProcessListener;
import io.calimero.secure.Keyring;
import io.calimero.secure.SecureApplicationLayer;
import io.calimero.secure.SecureApplicationLayer.SequenceNumbers;
import io.calimero.secure.Security;

public class ProcessCommunication {

// Address of your KNXnet/IP server. Replace the IP host or address as
// necessary.
private static final String remoteHost = "192.168.100.100";
private static final String localHost = "0.0.0.0";
private static final String keyringUri = "/home/bhanujarya/Downloads/KNX Secure.knxkeys";
private static final char[] keyringPwd = "jt@paragon".toCharArray();

// We will read a boolean from the KNX datapoint with this group address,
// replace the address as necessary.
// Make sure this datapoint exists, otherwise you will get a read timeout!
private static final String group = "1/1/1";

private static final String readGroup = "1/1/2";
private final SecureApplicationLayer sal = null;


public static void main(final String[] args) {
	final InetSocketAddress remote = new InetSocketAddress(remoteHost, 3671);
	final InetSocketAddress local = new InetSocketAddress(localHost, 0);

	// Create our network link, and pass it to a process communicator
	

	
	try (KNXNetworkLink knxLink = KNXNetworkLinkIP.newTunnelingLink(local, remote, false, new TPSettings());
			ProcessCommunicator pc = new ProcessCommunicatorImpl(knxLink)) {

// ProcessCommunicationResponder pCommRespond = new ProcessCommunicationResponder(knxLink,null);
//
// ProcessCommunicationResponder pCommRespondb = new ProcessCommunicationResponder(knxLink,null);
//
//
// ProcessCommunicationResponder pCommRespondc = new ProcessCommunicationResponder(knxLink,null);

		//new SecureApplicationLayer(knxLink, null, SerialNumber.of(Long.parseLong("00C501071402", 16)), new SequenceNumbers(217088278911l, 2l, null, null));
	
		IndividualAddress dev = new IndividualAddress("1.1.1");

		Map<IndividualAddress, Long> mp = new HashMap<>();
		mp.put(dev, 2l);

// Security.defaultInstallation().useKeyring(Keyring.load(keyringUri), keyringPwd);
// Set sett = new HashSet<>();
// sett.add(new IndividualAddress("1.1.1"));
// sett.add(new IndividualAddress("1.1.2"));
// String toolKeyBase64 = "Qx9QJ3Pds8TKdWroRclgog==";
//
// byte[] toolKeyBytes = Base64.getDecoder().decode(toolKeyBase64);
//
// Security.defaultInstallation().groupSenders().put(new GroupAddress("2305"), sett);
// Security.defaultInstallation().groupSenders().put(new GroupAddress("2306"), sett);
// Security.defaultInstallation().groupSenders().put(new GroupAddress("2307"), sett);
// Security.defaultInstallation().groupSenders().put(new GroupAddress("2308"), sett);
// Security.defaultInstallation().groupSenders().put(new GroupAddress("2309"), sett);
// Security.defaultInstallation().groupSenders().put(new GroupAddress("2048"), sett);
// Security.defaultInstallation().deviceToolKeys().put(new IndividualAddress("1.1.1"), toolKeyBytes);

		ProcessCommunicationResponder pCommRespond = new ProcessCommunicationResponder
				(knxLink,new SecureApplicationLayer(knxLink, Security.defaultInstallation(), SerialNumber.of(Long.parseLong("00C501071402", 16)),
						new SequenceNumbers(217088278911l, 2l, mp, mp)));

	//		addProccessListners(pCommRespond);
		
		
		
		
	GroupAddress addr = 	getGroupAddressObj(group);
	
	System.out.println("writing though p resp");
	pCommRespond.write(addr, true);
	Thread.sleep(10000);
	

	
	System.out.println("Writing command true ");
		pc.write(addr, true);
		
		Thread.sleep(10000);
		System.out.println("Writing command false ");
		pc.write(addr, false);
		
		Thread.sleep(10000);
		System.out.println("Writing command true ");

		pc.write(addr, true);
		
		Thread.sleep(10000);
		System.out.println("Writing command false ");
		pc.write(addr, false);
		Thread.sleep(10000);

		

		
		
		System.out.println("on");
		System.out.println("tunnel done ");
		System.out.println("read boolean value from datapoint " + readGroup);
		final boolean value = pc.readBool(new GroupAddress(readGroup));
		System.out.println("datapoint " + readGroup + " value = " + value);
		
		

		
		// Uncomment the next line, if you want to write back the same value to the KNX
		// network
		// pc.write(group, value);
	} catch (KNXException | InterruptedException e) {
		System.out.println("Error accessing KNX datapoint: " + e.getMessage());
	}
}

public static  GroupAddress getGroupAddressObj(String addr)
   {
      try
      {
         return new GroupAddress(addr);
      }
      catch (Exception e)
      {
         return null;
      }
   }

public static void addProccessListners(ProcessCommunicator pc) {
	pc.addProcessListener(new ProcessListener() {
		@Override
		public void groupWrite(final ProcessEvent e) {
			print("write.ind", e);
		}

		@Override
		public void groupReadRequest(final ProcessEvent e) {
			print("read.req", e);
		}

		@Override
		public void groupReadResponse(final ProcessEvent e) {
			print("read.res", e);
		}

		@Override
		public void detached(final DetachEvent e) {
		}
		
		
	});
}

private static void print(final String svc, final ProcessEvent pe) {
	try {
		System.out.format("%s %s->%s %s: %s%n", LocalTime.now(), pe.getSourceAddr(), pe.getDestination(), svc,
				HexFormat.of().formatHex(pe.getASDU()));
	} catch (final RuntimeException rte) {
		System.err.println(rte);
	}
}

}
`

Output is

Dec 07, 2024 1:15:11 PM io.calimero.knxnetip.ClientConnection connect
INFO: connection established (data endpoint 192.168.100.100:3,671, channel 31, tunneling address 1.0.255)
Dec 07, 2024 1:15:11 PM io.calimero.knxnetip.ClientConnection connect
INFO: connection established (data endpoint 192.168.100.100:3,671, channel 32)
writing though p resp
Writing command true

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant