eclipse / paho.mqtt-sn.embedded-c

Paho C MQTT-SN gateway and libraries for embedded systems. Paho is an Eclipse IoT project.
https://eclipse.org/paho
Other
314 stars 179 forks source link

Failed to connect on arm64 soc #194

Closed jhx2002 closed 4 years ago

jhx2002 commented 4 years ago

Setup on same machine: mosquto broker + paho.mqtt-sn.gateway I have tested it successfully on x84 machine. On arm64 soc, build and run is OK, ping ipv6 and ipv4 is OK, but pub failed.

Here is log when gateway starting:

20200416 035308.819 MQTT-SN/gateway1 has been started.

 ConfigFile: ./gateway.conf
 ClientList: ./clients.conf
 SensorN/W:  GatewayUDP6Bind: fe80::f58c:2fb7:adad:1 Gateway Port: 10000
 Broker:     192.168.1.9 : 1883, 8883
 RootCApath: (null)
 RootCAfile: (null)
 CertKey:    (null)
 PrivateKey: (null)

20200416 035310.000   CONNECT           ===>  MQTT-SN/gateway1QoS-1               10 21 00 04 4D 51 54 54 04 02 03 84 00 15 4D 51 54 54 2D 53 4E 2F 67 61 74 65 77 61 79 31 51 6F 53 2D 31
20200416 035310.322   CONNACK           <===  MQTT-SN/gateway1QoS-1               20 02 00 00
20200416 035310.322   CONNACK           --->  MQTT-SN/gateway1QoS-1               03 05 00

Following is error log when connecting it with mqtt-sn-tools:

./mqtt-sn-tools-master/mqtt-sn-pub -h 192.168.1.9 -p 10000 -t 10 -m hello
aaa@aaa-T460s:~/proj/sys/sensornet/mqtt-sn-tools$ sh pub.sh 
2020-04-16 11:53:43 ERROR Failed to connect to MQTT-SN gateway.
jhx2002 commented 4 years ago

Use mqtt-sn-tools to test

 ./mqtt-sn-tools-master/mqtt-sn-pub -h 192.168.1.9 -p 10000 -t 10 -m hello
2020-04-16 15:24:16 DEBUG Debug level is: 1.
2020-04-16 15:24:16 DEBUG Network timeout is: 30 seconds.
2020-04-16 15:24:16 DEBUG Trying 192.168.1.9...
2020-04-16 15:24:16 DEBUG Connecting...
2020-04-16 15:24:16 DEBUG Sending CONNECT packet...
2020-04-16 15:24:16 DEBUG waiting for packet...
2020-04-16 15:24:46 DEBUG Timed out waiting for packet.
2020-04-16 15:24:46 ERROR Failed to connect to MQTT-SN gateway.
ty4tw commented 4 years ago

Gateway waits a packet via UDP6. Client have to send a packet via UDP6.

jhx2002 commented 4 years ago

tshark can get udp packet. Add some debug info in the source code as following. Foud that: return value of select always <= 0

//TODO: test if this is working properly (GW works, but this function is not completely tested)                                                                                                             
int UDPPort6::recv(uint8_t* buf, uint16_t len, SensorNetAddress* addr)
{
  struct timeval timeout;
  fd_set recvfds;

  timeout.tv_sec = 0;
  timeout.tv_usec = 1000000;    // 1 sec                                                                                                                                                                    
  FD_ZERO(&recvfds);
  FD_SET(_sockfdUnicast, &recvfds);

  std::cout << "xxxUDPPort6::recv" <<std::endl;

  int rc = 0;
  if ( select(_sockfdUnicast + 1, &recvfds, 0, 0, &timeout) > 0 )
  {
    // debug found : can not come here                                                                                                                                                                      
    if (FD_ISSET(_sockfdUnicast, &recvfds))
    {
      std::cout << "xxxUDPPort6::recv 2" <<std::endl;
      rc = recvfrom(_sockfdUnicast, buf, len, 0, addr);
      std::cout << "xxxUDPPort6::recv 3" <<std::endl;
    }
  }

  std::cout << "yyyUDPPort6::recv rc=" << rc << std::endl;
  return rc;
}
jhx2002 commented 4 years ago

Gateway waits a packet via UDP6. Client have to send a packet via UDP6.

fe80::f58c:2fb7:adad:1 and 192.168.1.9 are on the same eth0, so the client can use 192.168.1.9 to publish . right ?

jhx2002 commented 4 years ago

I test with sending a UDP6 packet. MQTT-SN Gateway can not recive the packet. But tshark can catch it.

jhx2002 commented 4 years ago

Two cases log : Gateway.conf bind ip6 to 240e:390:3f:7560:f01e:4888:7e85:a

  1. Do not open mqtt-sn gateway
    
    $ ./mqtt-sn-tools-master/mqtt-sn-pub -h 240e:390:3f:7560:f01e:4888:7e85:a -p 10000 -t 1 -m hello
    recv failed: Connection refused

2. Running mqtt-sn gateway.

$ ./mqtt-sn-tools-master/mqtt-sn-pub -h 240e:390:3f:7560:f01e:4888:7e85:a -p 10000 -t 1 -m hello 2020-04-19 11:17:02 ERROR Failed to connect to MQTT-SN gateway.

jhx2002 commented 4 years ago

I use udp 6 receiver from here, it's OK, it can recive the package from mqtt-sn-pub https://github.com/jhx2002/test_riot/tree/master/udp_recv