src/mk_tls_https_request.c
int mk_recv_https_response(mk_tls_https_info *pmkctx)
{
   int ret = 0;
   int rc = 0;
   int len = RX_HTTPS_BUF_SZ-1;
   int rxBytes = 0;
   struct timeval tstart = {0,0};
   mk_mbedtls_conn_params *pmtls = &pmkctx->mtlsc;
   unsigned int ori_task_priority = 0;
   unsigned int cur_task_priority = 0;

   memset(pmkctx->rxbuf,0,RX_HTTPS_BUF_SZ);
   // pmkctx->rxbuf[0] = 0 ;
   pmkctx->rxlen = 0;

   ori_task_priority = uxTaskPriorityGet(NULL);
   MK_LOG(LOG_NOTICE,"%s() Try changing task priority from(%u) -> to(%u) \n",
                                   __func__,ori_task_priority,tskIDLE_PRIORITY);
   vTaskPrioritySet(NULL, tskIDLE_PRIORITY);
   MK_LOG(LOG_NOTICE,"%s() Task Prioirty after the change = %u \n",__func__, uxTaskPriorityGet(NULL));

   MK_LOG(LOG_INFO,"%s() Entry \n",__func__);

   gettimeofday(&tstart, NULL);
   while (len > 0) {

      ret = mbedtls_ssl_read(&pmtls->ssl_ctx, &pmkctx->rxbuf[rxBytes], len);
      if (ret > 0) {
	      rxBytes += ret;
	      len -= ret;
	      pmkctx->rxlen = rxBytes;
      } else {
	 if (ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE) {
	    MK_LOG(LOG_DEBUG,"%s() MBEDTLS_ERR_SSL_WANT_READ/WRITE, ret=-0x%x rxlen=%d \n",__func__,-ret,rxBytes);
	    continue;
	 }
	 else if (ret == 0) {
	    MK_LOG(LOG_INFO,"%s() ret=0 and rxlen=%d read end of the underlying transport was closed \n",__func__,rxBytes);
	    pmkctx->rxlen = rxBytes;
	    break;
	 }
	 else if(ret == MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY) {
	    pmkctx->rxlen = rxBytes;
	    MK_LOG(LOG_INFO,"%s() MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY, ret=-0x%x rxlen=%d \n",__func__,ret,rxBytes);
	    break;
	 }
	 else if (ret == MBEDTLS_ERR_SSL_TIMEOUT) {
            long int elapsedms = mk_get_elaspedms(&tstart);

	    //MK_LOG(LOG_DEBUG,"%s() mbedtls_ssl_read MBEDTLS_ERR_SSL_TIMEOUT ret=-0x%x, elapsedms=%ld, timeoutms=%d rxlen=%d\n",
            //                    __func__,-ret,elapsedms,pmkctx->timeoutms,rxBytes);

            if ((elapsedms > pmkctx->timeoutms) && (pmkctx->timeoutms > 0)) {
	       MK_LOG(LOG_ERR,"%s() mbedtls_ssl_read : returning MBEDTLS_ERR_SSL_TIMEOUT ret=-0x%x, elapsedms=%ld > timeoutms=%d\n",
                                __func__,-ret,elapsedms,pmkctx->timeoutms);
	       pmkctx->rxlen = 0;
	       rc = -1;
	       break;
            }

	    // SSL_READ_SUBTIMEOUT_MS timeout during mbedtls_ssl_read and got
	    // MBEDTLS_ERR_SSL_TIMEOUT err.  Now, will use this opputunity to
	    // yeild to cpu and also reset the freeRTOS system watchdog.
            //vTaskDelay(1);
	    continue;
	 }
	 pmkctx->rxlen = 0;
	 rc = -1;
	 break;
    }

  } //end of while

  //try to revert the task priority to original value reagrdless of the result of above call.
  cur_task_priority = uxTaskPriorityGet(NULL);
  if (cur_task_priority !=  ori_task_priority) {
     vTaskPrioritySet(NULL, ori_task_priority);
     MK_LOG(LOG_NOTICE,"%s() Reverted back task priority from(%u) -> to(%u)\n",
                            __func__, cur_task_priority, uxTaskPriorityGet(NULL));
  }

  MK_LOG(LOG_INFO,"%s() Leave ret=%d rxBytes=%d rxlen=%d rc=%d \n",__func__,ret,rxBytes,pmkctx->rxlen,rc);
  pmkctx->rxbuf[pmkctx->rxlen] = 0; //terminate with in anycase for easier string operation later
  return rc == 0?pmkctx->rxlen:rc;

}
// MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY : The peer notified us that the connection is going to be closed.
// MBEDTLS_ERR_SSL_WANT_READ         : No data of requested type currently available on underlying transport
// returns <=0 on error or failure to receive any data
// returns > 0 on successful data recpetion
MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY : The peer notified us that the connection is going to be closed.
MBEDTLS_ERR_SSL_WANT_READ         : No data of requested type currently available on underlying transport
returns <=0 on error or failure to receive any data
returns > 0 on successful data recpetion
MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY : The peer notified us that the connection is going to be closed . MBEDTLS_ERR_SSL_WANT_READ : No data of requested type currently available on underlying transport returns < = 0 on error or failure to receive any data returns > 0 on successful data recpetion
int mk_recv_https_response(mk_tls_https_info *pmkctx)
{
   int ret = 0;
   int rc = 0;
   int len = RX_HTTPS_BUF_SZ-1;
   int rxBytes = 0;
   struct timeval tstart = {0,0};
   mk_mbedtls_conn_params *pmtls = &pmkctx->mtlsc;
   unsigned int ori_task_priority = 0;
   unsigned int cur_task_priority = 0;
   memset(pmkctx->rxbuf,0,RX_HTTPS_BUF_SZ);
   pmkctx->rxlen = 0;
   ori_task_priority = uxTaskPriorityGet(NULL);
   MK_LOG(LOG_NOTICE,"%s() Try changing task priority from(%u) -> to(%u) \n",
                                   __func__,ori_task_priority,tskIDLE_PRIORITY);
   vTaskPrioritySet(NULL, tskIDLE_PRIORITY);
   MK_LOG(LOG_NOTICE,"%s() Task Prioirty after the change = %u \n",__func__, uxTaskPriorityGet(NULL));
   MK_LOG(LOG_INFO,"%s() Entry \n",__func__);
   gettimeofday(&tstart, NULL);
   while (len > 0) {
      ret = mbedtls_ssl_read(&pmtls->ssl_ctx, &pmkctx->rxbuf[rxBytes], len);
      if (ret > 0) {
	      rxBytes += ret;
	      len -= ret;
	      pmkctx->rxlen = rxBytes;
      } else {
	 if (ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE) {
	    MK_LOG(LOG_DEBUG,"%s() MBEDTLS_ERR_SSL_WANT_READ/WRITE, ret=-0x%x rxlen=%d \n",__func__,-ret,rxBytes);
	    continue;
	 }
	 else if (ret == 0) {
	    MK_LOG(LOG_INFO,"%s() ret=0 and rxlen=%d read end of the underlying transport was closed \n",__func__,rxBytes);
	    pmkctx->rxlen = rxBytes;
	    break;
	 }
	 else if(ret == MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY) {
	    pmkctx->rxlen = rxBytes;
	    MK_LOG(LOG_INFO,"%s() MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY, ret=-0x%x rxlen=%d \n",__func__,ret,rxBytes);
	    break;
	 }
	 else if (ret == MBEDTLS_ERR_SSL_TIMEOUT) {
            long int elapsedms = mk_get_elaspedms(&tstart);
            if ((elapsedms > pmkctx->timeoutms) && (pmkctx->timeoutms > 0)) {
	       MK_LOG(LOG_ERR,"%s() mbedtls_ssl_read : returning MBEDTLS_ERR_SSL_TIMEOUT ret=-0x%x, elapsedms=%ld > timeoutms=%d\n",
                                __func__,-ret,elapsedms,pmkctx->timeoutms);
	       pmkctx->rxlen = 0;
	       rc = -1;
	       break;
            }
	    continue;
	 }
	 pmkctx->rxlen = 0;
	 rc = -1;
	 break;
    }
  } 
  cur_task_priority = uxTaskPriorityGet(NULL);
  if (cur_task_priority !=  ori_task_priority) {
     vTaskPrioritySet(NULL, ori_task_priority);
     MK_LOG(LOG_NOTICE,"%s() Reverted back task priority from(%u) -> to(%u)\n",
                            __func__, cur_task_priority, uxTaskPriorityGet(NULL));
  }
  MK_LOG(LOG_INFO,"%s() Leave ret=%d rxBytes=%d rxlen=%d rc=%d \n",__func__,ret,rxBytes,pmkctx->rxlen,rc);
  pmkctx->rxbuf[pmkctx->rxlen] = 0; 
  return rc == 0?pmkctx->rxlen:rc;
}
int mk_recv_https_response ( mk_tls_https_info * pmkctx ) { int ret = 0 ; int rc = 0 ; int len = RX_HTTPS_BUF_SZ - 1 ; int rxBytes = 0 ; struct timeval tstart = { 0 , 0 } ; mk_mbedtls_conn_params * pmtls = & pmkctx -> mtlsc ; unsigned int ori_task_priority = 0 ; unsigned int cur_task_priority = 0 ; memset ( pmkctx -> rxbuf , 0 , RX_HTTPS_BUF_SZ ) ; pmkctx -> rxlen = 0 ; ori_task_priority = uxTaskPriorityGet ( NULL ) ; MK_LOG ( LOG_NOTICE , " \n " , __func__ , ori_task_priority , tskIDLE_PRIORITY ) ; vTaskPrioritySet ( NULL , tskIDLE_PRIORITY ) ; MK_LOG ( LOG_NOTICE , " \n " , __func__ , uxTaskPriorityGet ( NULL ) ) ; MK_LOG ( LOG_INFO , " \n " , __func__ ) ; gettimeofday ( & tstart , NULL ) ; while ( len > 0 ) { ret = mbedtls_ssl_read ( & pmtls -> ssl_ctx , & pmkctx -> rxbuf [ rxBytes ] , len ) ; if ( ret > 0 ) { rxBytes += ret ; len -= ret ; pmkctx -> rxlen = rxBytes ; } else { if ( ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE ) { MK_LOG ( LOG_DEBUG , " \n " , __func__ , - ret , rxBytes ) ; continue ; } else if ( ret == 0 ) { MK_LOG ( LOG_INFO , " \n " , __func__ , rxBytes ) ; pmkctx -> rxlen = rxBytes ; break ; } else if ( ret == MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY ) { pmkctx -> rxlen = rxBytes ; MK_LOG ( LOG_INFO , " \n " , __func__ , ret , rxBytes ) ; break ; } else if ( ret == MBEDTLS_ERR_SSL_TIMEOUT ) { long int elapsedms = mk_get_elaspedms ( & tstart ) ; if ( ( elapsedms > pmkctx -> timeoutms ) && ( pmkctx -> timeoutms > 0 ) ) { MK_LOG ( LOG_ERR , " \n " , __func__ , - ret , elapsedms , pmkctx -> timeoutms ) ; pmkctx -> rxlen = 0 ; rc = -1 ; break ; } continue ; } pmkctx -> rxlen = 0 ; rc = -1 ; break ; } } cur_task_priority = uxTaskPriorityGet ( NULL ) ; if ( cur_task_priority != ori_task_priority ) { vTaskPrioritySet ( NULL , ori_task_priority ) ; MK_LOG ( LOG_NOTICE , " \n " , __func__ , cur_task_priority , uxTaskPriorityGet ( NULL ) ) ; } MK_LOG ( LOG_INFO , " \n " , __func__ , ret , rxBytes , pmkctx -> rxlen , rc ) ; pmkctx -> rxbuf [ pmkctx -> rxlen ] = 0 ; return rc == 0 ? pmkctx -> rxlen : rc ; }
MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY : The peer notified us that the connection is going to be closed.
MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY : The peer notified us that the connection is going to be closed .
// pmkctx->rxbuf[0] = 0 ; //MK_LOG(LOG_DEBUG,"%s() mbedtls_ssl_read MBEDTLS_ERR_SSL_TIMEOUT ret=-0x%x, elapsedms=%ld, timeoutms=%d rxlen=%d\n", //                    __func__,-ret,elapsedms,pmkctx->timeoutms,rxBytes); // SSL_READ_SUBTIMEOUT_MS timeout during mbedtls_ssl_read and got // MBEDTLS_ERR_SSL_TIMEOUT err.  Now, will use this opputunity to // yeild to cpu and also reset the freeRTOS system watchdog. //vTaskDelay(1); //end of while //try to revert the task priority to original value reagrdless of the result of above call. //terminate with in anycase for easier string operation later