What is the best way of checking if the network is exist?

    This site uses cookies. By continuing to browse this site, you are agreeing to our Cookie Policy.

    • What is the best way of checking if the network is exist?

      Hello,

      My emNet version is V3.40.

      I've an issue when the network is not really there. I will explain what I mean,
      • I have minimum 2 network switches.
        • First one is connected to the local network directly.
        • Second one is a sitting between first switch and my custom PCB.
      • I power the custom PCB which has a firmware communicates with the network for several tasks.
      • I then remove the first network switch from the local network while the 2nd network switch is still connected to the custom PCB.


      At that stage, 100Mbps LED stays ON and the activity LED goes OFF or ON and stays there depends on at what state it was.
      And, all the network operation ceased naturally.

      If I directly remove the network from the custom PCB ,it is fine, I get a proper state change inside _OnStateChange event callback.
      And I can then notify the user for the issue.

      For now, I just ping an IP and wait for an answer? But, I prefer not to use it.

      I am probably missing a point when I read the document. Is there a robust way emNet can offer when the network is really disconnected.

      Best regards,
      Sener

      The post was edited 1 time, last by Sener ().

    • Hello Sener,

      This really depends on what exactly you're doing, ie if the devices are actively and regularly sending and receiving packets to each other, or if it is more like a webserver where you only see if the server disconnected when trying to connect.
      Generally it is not possible to detect if some other device in the network disconnects (unless using keep-alive, or like what you're doing, manually pinging the other side).

      Best regards,
      Jannik
    • Hello Jannik,

      Thank you for your reply.

      In fact, I have already keep-alive. If it is o, can expect a callback or something to detect/register the disconnect?

      Source Code

      1. IP_SOCKET_SetDefaultOptions(0| SO_KEEPALIVE); // Keep connections alive


      The device is not constantly aware if the network is present. I have MQTT client running. And, once a while a message is being published by a button push.

      I have this routine to publish

      Source Code

      1. r = IP_MQTT_CLIENT_Publish(&MQTTClient, &Publish);
      2. if (r < 0) { IP_MQTT_CLIENT_LOG(("MQTT Pub failed.")); }

      Can I simply rely on or consider if r < 0 then the connection is lost?
      (Ignore mqtt releated setting like authentication errors for a moment)

      Best regards,
      Sener
    • Hi Sener,

      As I'm not sure how your implementation looks like, I will instead refer to the IP_MQTT_CLIENT_PublisherSubscriber_2Tasks.c Sample that is part of the emNet shipment. Ideally your application follows the implementation from that sample.

      If the connection gets lost (remove first switch while target is still connected to second switch), IP_MQTT_CLIENT_Publish() will not return -1. Depending on your QoS setting the target might even send more Publish Messages, because it does not wait for a PUBACK or will only process it later (non-blocking case using pfRecvMessageEx). What will happen is that without a connection, the TCP Retransmission Timer will expire at some point (message has not been acked multiple times) and drop the TCP connection. When this happens, the _ReadHeader() in IP_MQTT_CLIENT_Exec() will fail its receive and the Sample disconnects.

      If that is too slow for you, you could set up another task that regularly pings the other device and checks for a lost connection. Especially if a Publish is only sent on a button-press by a user, a connection could be lost and you only realize it until much later.
      In general it is not easy to detect if another device lost its connection without sending/receiving a message.

      Best regards,
      Jannik
    • Hi Jannik,

      Thanks a lot for some insight, it has already given me some some idea and I've already noticed that the disconnect is slow however it is not that critical in my situation.
      Besides that, I am confused on one thing. You said

      SEGGER - Jannik Urban wrote:

      If the connection gets lost (remove first switch while target is still connected to second switch), IP_MQTT_CLIENT_Publish() will not return -1.
      But, I get r < 0 regardless which switch is disconnected. To be more specific;

      - If the switch directly connected to my device is removed, I get r < 0 immediately by press of a button.
      - If the far switch is removed from the LAN, I get r < 0 after a certain time, say 5 seconds or so after press of a button.

      SEGGER - Jannik Urban wrote:

      In general it is not easy to detect if another device lost its connection without sending/receiving a message
      Yeah, I get that, I might give a try to sending regular ping from an independent task and listen it back.


      Best regards,
      Sener
    • New

      Hi Sener,

      I would assume that you do not use pfRecvMessageEx (have it as NULL in your IP_MQTT_CLIENT_APP_API that is a parameter of IP_MQTT_CLIENT_Init()). Thus, the IP_MQTT_CLIENT_Publish() would wait for the PUBACK immediately (_ReceiveAck()) and then we indeed return with -1 in that case (because _ReceiveAck() fails). Otherwise we return with 1. However this is blocking and not the desirable way to handle this, and you won't be able to receive any messages (ie anything that is not a PUBACK) during this.

      That's why I mentioned the 2Tasks Sample, which you should have access to (\Application in your shipment). Feel free to try it out.

      Best regards,
      Jannik