Code Monkey home page Code Monkey logo

Comments (38)

szysas avatar szysas commented on May 22, 2024

There is nothing running on port 5694 you should use instead 5684. Note also, that it requires DTLS or TLS secure connection.

from java-coap.

Karandev avatar Karandev commented on May 22, 2024

Thanks for the reply.
Is the coap-core library making use of DTLS/TLS secure connection?

from java-coap.

szysas avatar szysas commented on May 22, 2024

At the moment only TLS.

from java-coap.

Karandev avatar Karandev commented on May 22, 2024

`CoapClient client = null;
try {
client = CoapClientBuilder.newBuilder(new InetSocketAddress("api.connector.mbed.com",5684)).build();
} catch (IOException e) {

		e.printStackTrace();
	}

	CoapPacket coapResp;
	try {
		coapResp = client.resource("/Test/0/S").sync().get();
		System.out.println(coapResp.toString());
	} catch (CoapException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}`

We tried using this code piece with the port number as 5684. We got CoapTimeOutException.

Could you please let us know why the problem happens?

from java-coap.

szysas avatar szysas commented on May 22, 2024

In your example, it is trying to connect with plain text.
For tls connection, you need to prepare keystore with proper certificates and setup transport similar as in this test class: https://github.com/ARMmbed/java-coap/blob/master/coap-core/src/test/java/com/mbed/coap/transport/javassl/SSLSocketClientTransportTest.java

from java-coap.

Karandev avatar Karandev commented on May 22, 2024

Thanks for the support!

We tried with this code:
SSLContext clientSslContext = SSLUtils.sslContext(CLI_KS, SECRET); InetSocketAddress serverAdr = new InetSocketAddress("api.connector.mbed.com",5683); CoapClient client = null; try { client = CoapClientBuilder.clientFor(serverAdr,CoapServer.builder().transport(new SSLSocketClientTransport(serverAdr, clientSslContext.getSocketFactory())).build().start()); CoapPacket coapResp; try { coapResp = client.resource("/Test/0/S").sync().get(); System.out.println(coapResp.toString()); } catch (CoapException e) { // TODO Auto-generated catch block e.printStackTrace(); } } catch (IllegalStateException | IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } client.close();

But got the following exception:

java.net.ConnectException: Connection timed out: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at sun.security.ssl.SSLSocketImpl.connect(SSLSocketImpl.java:668)
at sun.security.ssl.SSLSocketImpl.(SSLSocketImpl.java:446)
at sun.security.ssl.SSLSocketFactoryImpl.createSocket(SSLSocketFactoryImpl.java:137)
at com.mbed.coap.transport.javassl.SSLSocketClientTransport.start(SSLSocketClientTransport.java:52)
at com.mbed.coap.server.CoapServer.start(CoapServer.java:169)
at MyTest.main(MyTest.java:92)

While creating the client. Please help.

from java-coap.

szysas avatar szysas commented on May 22, 2024

Note the port, it should be 5684.

from java-coap.

Karandev avatar Karandev commented on May 22, 2024

Yes, we have tried it with 5684 too.

from java-coap.

szysas avatar szysas commented on May 22, 2024

When you use correct address and port:

InetSocketAddress serverAdr = new InetSocketAddress("api.connector.mbed.com", 5684);
CoapClient client = CoapClientBuilder.clientFor(serverAdr,
                CoapServer.builder().transport(new SSLSocketClientTransport(serverAdr, clientSslContext.getSocketFactory())).build().start()
);

client.ping().get();

You should get error:
javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: No trusted certificate found

Next step is to create keystore from https://connector.mbed.com/#credentials and use this keystore to connect with mbed connector.

from java-coap.

Karandev avatar Karandev commented on May 22, 2024

Could you please share a reference link on how to create keystore from https://connector.mbed.com/#credentials ?
We are trying to connect to server from an Android application. We created a certificate using Keystore Explorer and added a certificate entry.
But we got the error : javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.
Please help.

from java-coap.

szysas avatar szysas commented on May 22, 2024

I created example client application, that uses keystore to register with mbed cloud. There is also instruction on how to create keystore:
https://github.com/ARMmbed/java-coap/tree/dev/example-client/example-client

from java-coap.

Karandev avatar Karandev commented on May 22, 2024

Thanks for the help again!
I used OpenSSL in Ubuntu and tried the full command as in the above link. I git a message that the usage is not correct. I split the command and executed the following:
openssl pkcs12 -export -in CERT.pem -inkey KEY.pem -out CERT_KEY.p12 -name device -password pass:secret keytool-keystore client.jks
But the same issue happens. Please help.

from java-coap.

szysas avatar szysas commented on May 22, 2024

OK, there was formatting issue in readme file, try again:

openssl pkcs12 -export -in CERT.pem -inkey KEY.pem -out CERT_KEY.p12 -name device -password pass:secret
keytool -keystore client.jks -storepass secret  -importkeystore -srcstorepass secret -srckeystore CERT_KEY.p12 -srcstoretype PKCS12 -noprompt
keytool -keystore client.jks -storepass secret  -import -alias server-ca -file SERVER_CERT.pem -noprompt

from java-coap.

szysas avatar szysas commented on May 22, 2024

You need to leave -----BEGIN CERTIFICATE----- and -----END CERTIFICATE-----

from java-coap.

Karandev avatar Karandev commented on May 22, 2024

I could create the keystore successfully, but got this error
"java.lang.RuntimeException: java.io.IOException: Wrong version of key store."

from java-coap.

szysas avatar szysas commented on May 22, 2024

Did you get it when running example-client from your PC? (./run.sh 'coaps://api.connector.mbed.com:5684/rd?ep={MBED_ENDPOINT_NAME}' client.jks )

from java-coap.

Karandev avatar Karandev commented on May 22, 2024

I tried directly in the Android application created using Android Studio.

from java-coap.

szysas avatar szysas commented on May 22, 2024

Can you verify that it works from PC?

from java-coap.

Karandev avatar Karandev commented on May 22, 2024

Okay

from java-coap.

szysas avatar szysas commented on May 22, 2024

For Android, you might need to convert keystore type from JKS to BKS. You can do it with Keystore explorer: http://keystore-explorer.org.

from java-coap.

Karandev avatar Karandev commented on May 22, 2024

Trying to register we got the following error
"Didn't find class "java.time.Duration" on path: DexPathList[[zip file "/data/app/com.example.a20120672.coap-1/base.apk"],nativeLibraryDirectories=[/data/app/com.example.a20120672.coap-1/lib/arm64, /system/lib64, /vendor/lib64]]" in RegistrationManager constructor.
We are using JDK 1.8 and running on Android.

from java-coap.

Karandev avatar Karandev commented on May 22, 2024

We tried with:
String registrationUri = "coaps://api.connector.mbed.com:5684/rd?ep=<mbed_end_point_name_here>";
But got exception while registering. The previous issue i commented the Duration codes in the RegistrationManager class and hardcoded a delay of 5000.

Please help.

from java-coap.

szysas avatar szysas commented on May 22, 2024
  1. Have you tried to register with example-client from PC?
  2. What kind of exception you've got?
  3. About Android, I'm not sure how android support java 8, you could try to use coap-core-java7 from branch: java7-backport

from java-coap.

Karandev avatar Karandev commented on May 22, 2024

I got the exception No connection with api.connector.mbed.com/169.45.82.18:5684 while trying to register.

from java-coap.

szysas avatar szysas commented on May 22, 2024

Can you be more specific, what exact exception, how did you run it?

from java-coap.

Karandev avatar Karandev commented on May 22, 2024

While registering, when debugging the sendPacket method in SSLSocketClientTransport class the following method gets called:
if(adr != this.destination) { throw new IllegalStateException("No connection with: " + adr); }
even though adr and destination are both api.connector.mbed.com/169.45.82.18:5684.

Should we be using .equals ?

from java-coap.

szysas avatar szysas commented on May 22, 2024

Yes it should, and it is fixed. You need to pull for latest changes.

from java-coap.

Karandev avatar Karandev commented on May 22, 2024

How will we know that we have registered a device successfully to mbed connector? We checked for connected devices in https://connector.mbed.com/#endpoints but could not find our device end point name. Is register enough or should we do something else?

from java-coap.

szysas avatar szysas commented on May 22, 2024

Good question. In the logs you should see response like CoAP received [ACK 201 .... I will have to improve logging in RegistrationManager class.

from java-coap.

Karandev avatar Karandev commented on May 22, 2024

Actually even though we could write to output stream, no callbacks are made to the register method in RegisterManager class.

from java-coap.

szysas avatar szysas commented on May 22, 2024

Can you please, try first to use example client to register? Lets see if that works for you.

from java-coap.

Karandev avatar Karandev commented on May 22, 2024

Yes, we tried with the example client Java application. In that also we are not receiving any acknowledgement for register. Also, no exceptions are present. We used the same jks file and end point name as was used for the remote server.

from java-coap.

szysas avatar szysas commented on May 22, 2024

Can you show logs?

from java-coap.

Karandev avatar Karandev commented on May 22, 2024

In the mbed portal, the end point name is now shown in connected devices. How can we know whether it is the Android app or the Java application which was the reason for the change? How to remove the registered device from the portal?

from java-coap.

szysas avatar szysas commented on May 22, 2024

You need to check logs and see if your android app gets ACK 201. Device will get removed after registration lifetime expires or by sending remove registration message (for example by calling RegistrationManager.removeRegistration)

from java-coap.

Karandev avatar Karandev commented on May 22, 2024

Thanks szysas, we are able to see the endpoint name as active and the resources in the mbed portal console using Android application.
:)

from java-coap.

Karandev avatar Karandev commented on May 22, 2024

How do we update the LWM2M server about a change in some sensor value, for example temperature value obtained from device sensors, in the form of endpoints and resources?

from java-coap.

szysas avatar szysas commented on May 22, 2024

Could you make a new ticket for you last question, and close this one?

from java-coap.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.