HAPI for Butterfly Haptics: Connection issues

Hi! I'm working with a team that is making a HAPI class for the Butterfly Haptics device.

We are having problems creating the initialize method. Or rather, creating meaningful error messages for the initialize method.

The problem is that we are getting incorrect error messages from the connection function in the ML API that comes with the butterfly haptics device, and this error causes major crashes down the line.

Here is our test code:

int main()
{
    // try to connect to the device
    ml_device_handle_t device_hdl = NULL;
    int error = ml_Connect(&device_hdl, HOST_NAME);
    if(error == ML_STATUS_OK)
    {
        faultString = "ml_Connect: ML_STATUS_OK\n";
    }
    else if(error == ML_STATUS_INVALID_DEVICE_HANDLE)
    {
        faultString = "ml_Connect:ML_STATUS_INVALID_DEVICE_HANDLE\n";
    }
    printf("The connection status is %s", faultString.c_str());
   ...
}

The faultString is what I wish to return in the HAPI initialize method.

When the controller (which handles the connection to the device) is OFF, this returns:

ML_STATUS_OK
device_hdl = some random memory address (not NULL!)

Subsequent calls to the device (such as get button status) cause a broken pipe error and crashes the program. 

I.e. the values ML_STATUS_OK and device_hdl == NULL are not sufficient to check the status of the connection to the device.

This is an issue because the controller takes some time to start up (and it is not clear to the user when it is "ready"). If you start the program before it is ready it will also yield the problems above.

Also, if you don't have a connection at all (i.e. someone decided to pull out the ethernet cable for some reason) the connect function just hangs indefinitely.

I am considering some work-arounds, such as: (a) creating a socket to check the connection before calling the ML API connect function or (b) instituting a timer that will generate an error message since the connect function waits about 40 s before sending me all of these garbage messages (ML_STATUS_OK and random memory address in device_hdl).

Ideally, though, someone has already experienced these issues and can point me in the direction of a solution.

Please note that when the connection is ok and the controller is ready, I am able to use the ML API just fine.

Thanks!

jen

Found error in source ml_communication.c

This solution was found by Sebastian Bozlee:
 

In the ml_communication.c, when the connect times out (i.e. the connect function has attempted to connect 10 times and has failed), it does not return an error value. This is the part that needs to change:

for (i=0; i<10; i++)
    {
      if (0 <= (dstSocket2 = connect(dstSocket, (struct sockaddr *) dstAddr, sizeof(*dstAddr))))
        break;
#ifdef WIN32
      Sleep(1000);
#else
      sleep(1);
#endif
    }

//change starts here
      if (dstSocket2 == -1) // We've timed out.
          return -1; 

//end change


      printf("dstSocket=%d\n",dstSocket);
      return dstSocket; 
    }

in this function

int connect_to_server(in_addr dst_host_address, struct sockaddr_in* dstAddr, 
              int port_num, int use_tcp)