supremainc / BioStar2_device_SDK

35 stars 25 forks source link

Linux sdk #13

Open alexgg94 opened 3 years ago

alexgg94 commented 3 years ago

Hi, Following this model, I have created a net core 3.1 api to connect to a remote device to obtain the list of users on it.

Every time the api receives a petition, I connect the device using Connect function:

public IntPtr Connect(String ip, String port, ref UInt32 deviceID)
{
    IP = ip;
    PORT = port;

    deviceID = 0;
    IntPtr versionPtr = API.BS2_Version();
    //bool bSsl = false;

    //Console.WriteLine("SDK version : " + Marshal.PtrToStringAnsi(versionPtr));

    sdkContext = API.BS2_AllocateContext();
    if (sdkContext == IntPtr.Zero)
    {
        //Console.WriteLine("Can't allocate sdk context.");
        return IntPtr.Zero;
    }

    BS2ErrorCode result = (BS2ErrorCode)API.BS2_Initialize(sdkContext);
    if (result != BS2ErrorCode.BS_SDK_SUCCESS)
    {
        //Console.WriteLine("SDK initialization failed with : {0}", result);
        API.BS2_ReleaseContext(sdkContext);
        sdkContext = IntPtr.Zero;
        return IntPtr.Zero;
    }

    cbOnDeviceFound = new API.OnDeviceFound(DeviceFound);
    cbOnDeviceAccepted = new API.OnDeviceAccepted(DeviceAccepted);
    cbOnDeviceConnected = new API.OnDeviceConnected(DeviceConnected);
    cbOnDeviceDisconnected = new API.OnDeviceDisconnected(DeviceDisconnected);

    result = (BS2ErrorCode)API.BS2_SetDeviceEventListener(sdkContext,
                                                        cbOnDeviceFound,
                                                        cbOnDeviceAccepted,
                                                        cbOnDeviceConnected,
                                                        cbOnDeviceDisconnected);
    if (result != BS2ErrorCode.BS_SDK_SUCCESS)
    {
        //Console.WriteLine("Can't register a callback function/method to a sdk.({0})", result);
        API.BS2_ReleaseContext(sdkContext);
        sdkContext = IntPtr.Zero;
        return IntPtr.Zero;
    }

    #if SDK_AUTO_CONNECTION
    result = (BS2ErrorCode)API.BS2_SetAutoConnection(sdkContext, 1);
    #endif

    if (!ConnectToDevice(ref deviceID))
    {
        deviceID = 0;
    }

    if (deviceID > 0)
    {
        //Console.Title = String.Format("{0} connected deviceID[{1}]", title, deviceID);

    #if !SDK_AUTO_CONNECTION
        reconnectionTask = new ReconnectionTask(sdkContext);
        reconnectionTask.start();
    #endif
    }

    return sdkContext;
}

public void Disconnect(UInt32 deviceID, IntPtr sdkContext)
{
    if (deviceID > 0)
    {
#if !SDK_AUTO_CONNECTION
        reconnectionTask.stop();
        reconnectionTask = null;
#endif
    }

    BS2ErrorCode result = result = (BS2ErrorCode)API.BS2_DisconnectDevice(sdkContext, deviceID);
    if (result != BS2ErrorCode.BS_SDK_SUCCESS)
    {
        //Console.WriteLine("Got error({0}).", result);
    }

    eventWaitHandle.Close();
    API.BS2_ReleaseContext(sdkContext);
    sdkContext = IntPtr.Zero;

    cbOnDeviceFound = null;
    cbOnDeviceAccepted = null;
    cbOnDeviceConnected = null;
    cbOnDeviceDisconnected = null;
    cbOnSendRootCA = null;
}

bool ConnectToDevice(ref UInt32 deviceID)
{
    IntPtr ptrIPAddr = Marshal.StringToHGlobalAnsi(IP);
    BS2ErrorCode result = (BS2ErrorCode)API.BS2_ConnectDeviceViaIP(sdkContext, ptrIPAddr, Convert.ToUInt16(PORT), out deviceID);

    if (result != BS2ErrorCode.BS_SDK_SUCCESS)
    {
        return false;
    }
    Marshal.FreeHGlobal(ptrIPAddr);

    return true;
}

Once connected, I'm using BS2ErrorCode result = (BS2ErrorCode)API.BS2_GetUserList(sdkContext, deviceID, out outUidObjs, out numUserIds, cbIsAcceptableUserID); To get the users.

When I'm done with the conection, I use Disconnect to close it.

In windows this is working perfect. But when I try the same code for ubuntu 16.04 (with the linux sdk), it only works the first time, then, BS2_GetUserList only returns BS_SDK_ERROR_NULL_POINTER until I restart my API.

Am I doing something wrong?

alexgg94 commented 3 years ago

More specifically, the error starts here:

sdkContext = API.BS2_AllocateContext();
if (sdkContext == IntPtr.Zero)
  {
      Console.WriteLine("Can't allocate sdk context.");
      return IntPtr.Zero;
  }

The first petition, it works fine. After that, I'm just receiving BS_SDK_ERROR_CANNOT_LISTEN_SOCKET every time I'm trying to allocate context.

As I mentioned on the first post, this only happens on ubuntu 16.04 with linux sdk.