PPPoS interface disconnection event is not always calling the registered disconnect callback function

mr1000
Posts: 23
Joined: Fri Jan 12, 2018 9:05 am

Re: PPPoS interface disconnection event is not always calling the registered disconnect callback function

Postby mr1000 » Tue Oct 16, 2018 3:57 pm

Ritesh wrote:Yeah. something like that you need implement state machine for that
I've seen sometimes the GSM returns "NO CARRIER" message (which indicates connection has been lost), but sometimes it doesn't return the message and program gets stuck :(
However I added to go to a Reset state when receiveing a "NO CARRIER" message

That's not all. My program have 2 tasks, one for downloading the file (https) and other for GSM communication.
The problem is when I lose connection when downloading a file (removing the SIM card), the https task gets stuck in mbedtls_ssl_read(), I use the typical do while() for reading from server:

Code: Select all

do
        {
            len = BUFFSIZE;
            ret = mbedtls_ssl_read(&ssl, (unsigned char *)bufferRead, len);

            if(ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE)
            {
         ...
            }

            if(ret == MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY)
            {
         ...
            }

            if( ret < 0 ) /* receive error */
            {
         ...
            }
            else if( (ret > 0) && (!resp_body_start) )   /* reading header first and a piece of body */
            {
         ...
            }
            else if( (ret > 0) && (resp_body_start) )   /* already read the header, reading body */
            {
         ...
            }
            else if( ret == 0 )   /* packet over */
            {
         ...
            }
        }
        while(1);
I have printf's in all of the if's condition and none of them is called. So that's why I'm suposing the task is getting stuck at mbedtls_ssl_read()

Tried to call pppapi_close() and pppapi_free() in the reset state of GSM, but https still gets stuck.
It's like somehow I have to tell to ssl layer that connection have been closed...
Do you have any clue about this?

Thanks again Ritesh, you've been very helpful.

Ritesh
Posts: 1365
Joined: Tue Sep 06, 2016 9:37 am
Location: India
Contact:

Re: PPPoS interface disconnection event is not always calling the registered disconnect callback function

Postby Ritesh » Wed Oct 17, 2018 7:28 am

mr1000 wrote:
Ritesh wrote:Yeah. something like that you need implement state machine for that
I've seen sometimes the GSM returns "NO CARRIER" message (which indicates connection has been lost), but sometimes it doesn't return the message and program gets stuck :(
However I added to go to a Reset state when receiveing a "NO CARRIER" message

That's not all. My program have 2 tasks, one for downloading the file (https) and other for GSM communication.
The problem is when I lose connection when downloading a file (removing the SIM card), the https task gets stuck in mbedtls_ssl_read(), I use the typical do while() for reading from server:

Code: Select all

do
        {
            len = BUFFSIZE;
            ret = mbedtls_ssl_read(&ssl, (unsigned char *)bufferRead, len);

            if(ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE)
            {
         ...
            }

            if(ret == MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY)
            {
         ...
            }

            if( ret < 0 ) /* receive error */
            {
         ...
            }
            else if( (ret > 0) && (!resp_body_start) )   /* reading header first and a piece of body */
            {
         ...
            }
            else if( (ret > 0) && (resp_body_start) )   /* already read the header, reading body */
            {
         ...
            }
            else if( ret == 0 )   /* packet over */
            {
         ...
            }
        }
        while(1);
I have printf's in all of the if's condition and none of them is called. So that's why I'm suposing the task is getting stuck at mbedtls_ssl_read()

Tried to call pppapi_close() and pppapi_free() in the reset state of GSM, but https still gets stuck.
It's like somehow I have to tell to ssl layer that connection have been closed...
Do you have any clue about this?

Thanks again Ritesh, you've been very helpful.
Hi,

You might need to call below API to set timeout as default there is no any timeout for that

mbedtls_ssl_conf_read_timeout (mbedtls_ssl_config *conf, uint32_t timeout)

see below link for more information regarding same.

https://os.mbed.com/teams/sandbox/code/ ... sl_8h.html
Regards,
Ritesh Prajapati

Ritesh
Posts: 1365
Joined: Tue Sep 06, 2016 9:37 am
Location: India
Contact:

Re: PPPoS interface disconnection event is not always calling the registered disconnect callback function

Postby Ritesh » Thu Oct 18, 2018 3:28 am

Hi,

Is there any issue or your problem has been resolved with last post solution?

Let me know if you need any help regarding that
Regards,
Ritesh Prajapati

mr1000
Posts: 23
Joined: Fri Jan 12, 2018 9:05 am

Re: PPPoS interface disconnection event is not always calling the registered disconnect callback function

Postby mr1000 » Tue Oct 23, 2018 8:20 am

Hi Retesh, first of all thanks for your help again, it's very aprecited. And sorry to reply too late, I've been quite busy these last days :D

For now, I've encountered two problems, one it's mbedtls related and the other with the UART, so I'll make another topic for that.

I've tried the modification you commented:
mbedtls_ssl_conf_read_timeout(&conf, SSL_READ_TIMEOUT_MS);
also had to add
mbedtls_ssl_set_bio(&ssl, &server_fd, mbedtls_net_send, NULL, mbedtls_net_recv_timeout);

I set SSL_READ_TIMEOUT_MS to 10000ms
I've programed the program to do a download (GET request) to the server each 20 minutes aprox.
Also I programed it to connect to Internet via GSM, download the file and disconnect from the Internet (disconnect GSM).
But after a few iterations (5), the mbedtls_ssl_handshake returns -0x6800 (timeout error)
And after a retry, it doesn't return the error but it does in mbedtls_net_connect with -0x52 errror, which is "Failed to get an IP address for the given hostname"

Code: Select all

retry 0
mbedtls_ssl_handshake returned -0x6800
GET Failed
retry 1
mbedtls_net_connect returned -0x52
GET Failed
retry 2
mbedtls_net_connect returned -0x52
GET Failed
I'm wondering if I'm calling some mbedtls in wrong order or not correctly..
Here I put my https functions, simplified.

(executed once at begging of the program)

Code: Select all

HTTPS_INIT()
{
  mbedtls_ssl_init(&ssl);
  mbedtls_x509_crt_init(&cacert);
  mbedtls_ctr_drbg_init(&ctr_drbg);
  mbedtls_ssl_config_init(&conf);
  mbedtls_entropy_init(&entropy);
  mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, NULL, 0)
  mbedtls_x509_crt_parse(&cacert, server_root_cert_pem_start, server_root_cert_pem_end-server_root_cert_pem_start)
  mbedtls_ssl_set_hostname(&ssl, https_webServer)
  mbedtls_ssl_config_defaults(&conf, MBEDTLS_SSL_IS_CLIENT, MBEDTLS_SSL_TRANSPORT_STREAM, MBEDTLS_SSL_PRESET_DEFAULT)
  mbedtls_ssl_conf_authmode(&conf, MBEDTLS_SSL_VERIFY_OPTIONAL);
  mbedtls_ssl_conf_ca_chain(&conf, &cacert, NULL);
  mbedtls_ssl_conf_rng(&conf, mbedtls_ctr_drbg_random, &ctr_drbg);
  mbedtls_ssl_conf_read_timeout(&conf, SSL_READ_TIMEOUT_MS)
  mbedtls_ssl_setup(&ssl, &conf)
}
(executed each time I send a POST or a GET request)

Code: Select all

HTTPS_WRITE()
{
  mbedtls_net_init(&server_fd)
  mbedtls_net_connect(&server_fd, https_webServer, https_webPort, MBEDTLS_NET_PROTO_TCP)
  mbedtls_ssl_set_bio(&ssl, &server_fd, mbedtls_net_send, NULL, mbedtls_net_recv_timeout)
  mbedtls_ssl_handshake(&ssl)
  mbedtls_ssl_get_verify_result(&ssl)
  mbedtls_ssl_write(&ssl, ...)
}
If some function fails, I call this. For example, when handshake fails.

Code: Select all

HTTPS_RESET()
{
  mbedtls_ssl_session_reset(&ssl);
  mbedtls_net_free(&server_fd);
}
Or could just be I lost connection to Internet so I have to reset my GSM? (if it's that, then I have this problem with UART...)

Thanks again.
Cheers.

EDIT: Solved the UART problem, I've just had and old idf version... (viewtopic.php?f=13&t=7755)
Now when I'm receiving the mbedtls_net_connect returned -0x52, I do some retries, after that I reset the GSM, and mbedtls functions work again.
I really don't know if I'm masking the problem, but this workaround will suit for now.
Thanks again Ritesh, you've been very helpful.

Who is online

Users browsing this forum: HighVoltage and 148 guests