Problem with Modem Connection API

I keep having a problem with the modem connection API, I have tried several tests and I can`t understand why this is still not working.
The application I have made is using an handler for reading the connection status of the modem, problem is that the handler is somehow returning a status which is different from what I read if in the handler itself I try to get the status.

"
static void StateChangeHandler(le_mdc_ProfileRef_t profileRef,le_mdc_ConState_t ConnectionStatus,void* contextPtr)
{
LE_DEBUG("\n================================================");
LE_ASSERT( le_mdc_GetSessionState(profileRef, &modemState) == LE_OK );
LE_INFO("=====Handler is returning %d while profile status is %d=====", ConnectionStatus, modemState);
static bool sessionAlreadyStopped = false;
} "

where I get logs with =====Handler is returning 0 while profile status is 2===== or vice versa and only occasionally being the same.

I am copying my application where I have tried to use a semaphore and also some global variables, but because the state handler should basically handle the modem connection, and I cant get how and why I get these not matching status, I cant move on to finalize the software…

mySystemModem.zip (355 Bytes) testModem.zip (5.2 KB)

some additional information here, I have tried to remove the tool “cm data” just to make sure the two apps weren`t conflicting on accessing the signals passed to the handler.
Also, the system works on bootup or if I download it again, it will not work if I stop the app and to run it again.
This also only works within the system, it will not work if I only install it as an app and try to run it.

What I am testing is if in case of modem disconnecting, the app is able to re-establish the connection or this only works one shot.

Hi @claudio.baldini ,
From your source code, I see that you are using modemStatus variable instead of ConnectionStatus to get the connection status. Following the legato definition, the module will send its state to ConnectionStatus. If you use another variable, the handler cannot get correct state. I modified the source code at lines 278-280.

if (ConnectionStatus == LE_MDC_DISCONNECTED) //replace modemState by ConnectionStatus
{
modemState = LE_MDC_DISCONNECTED; //keep modemState variable

And lines 309-313

    //switch(modemState)
  switch(ConnectionStatus) //replace modemState by ConnectionStatus
    {
        case LE_MDC_CONNECTED:
  		modemState = LE_MDC_CONNECTED; //keep modemState

mdcTest.c (14.6 KB)
You can find more sample at the link

Please share if you have any questions. Help us tick “Solution” if it is useful. It might be beneficial to someone getting same problem
Thanks

@Vianney the problem wasn`t because of the variable used, what I mentioned was that once the handler was invoked, then by calling the getStatus function on the same profile, the status was somehow conflicting with what the handler returned.

Anyhow, I was using the profile 1 and once I switched to profile 2 this problem was gone, so what I believe is happening is some other app consuming the signals and controlling the modem through profile 1.
It seems it is working now.