951623847 发表于 2020-3-30 23:23:14

csr867x source这段代码怎么理解?

本帖最后由 bluetooth123 于 2020-3-30 23:30 编辑

第一次开机进入idle模式,此时delay的初始值为0,且pskey ps_timers对应的值也为0,也就是delay==0,怎么进入的APP_ENTER_PAIRING_STATE_FROM_IDLE, APP_ENTER_CONNECTABLE_STATE_FROM_IDLE?


    if (delay != 0)
    {
      /* enter connectable or discoverable state if a delay has been configured */
      if (BdaddrIsZero(&theSource->ps_config->bdaddr_remote_device) || theSource->inquiry_mode.force_inquiry_mode)
      {
            /* no device to connect to, go discoverable */
            MessageSend(&theSource->app_data.appTask, APP_ENTER_PAIRING_STATE_FROM_IDLE, 0);
      }
      else
      {
            /* there is a device to connect to, go connectable */
            MessageSend(&theSource->app_data.appTask, APP_ENTER_CONNECTABLE_STATE_FROM_IDLE, 0);
      }
    }


完整代码地方,
static void states_enter_state_idle(SOURCE_STATE_T old_state)
{
    uint16 delay = 0; /* default is to have no delay before next connection attempt */

    switch (old_state)
    {
      case SOURCE_STATE_CONNECTING:
      {
            if (theSource->ps_config->features.connection_max_retries != INVALID_VALUE)
            {
                /* feature enabled to only try to a remote device a set number of times before giving up */
                if ((theSource->connection_data.connection_retries < theSource->ps_config->features.connection_max_retries) ||
                  theSource->timer_data.timers_stopped)
                {
                  /* if it was connnecting use the connection_idle_timer delay before next connection attempt */
                  delay = theSource->ps_config->ps_timers.connection_idle_timer;
                  STATES_DEBUG(("STATE: was connecting, connection attempts:[%d], can continue after delay:[%d secs]\n", theSource->connection_data.connection_retries, delay));
                  
                  if (!theSource->timer_data.timers_stopped)
                  {
                        /* increase number of connections attempted if timers haven't been bypassed */
                        theSource->connection_data.connection_retries++;
                  }
                }
                else
                {
                  /* configured number of connection attempts have been tried - give up connecting! */
                  delay = TIMER_NO_TIMEOUT;
                  theSource->connection_data.connection_retries = 0;
                  STATES_DEBUG(("STATE: was connecting, given up trying to connect\n"));
                }
            }
            else
            {
                /* if it was connnecting use the connection_idle_timer delay before next connection attempt */
                delay = theSource->ps_config->ps_timers.connection_idle_timer;
                STATES_DEBUG(("STATE: was connecting, next connect delay:[%d secs]\n", delay));
            }
      }
      break;
      
      case SOURCE_STATE_CONNECTED:
      {
            /* if it was connnected use the disconnect_idle_timer delay before next connection attempt */
            delay = theSource->ps_config->ps_timers.disconnect_idle_timer;
            STATES_DEBUG(("STATE: was connected, delay:[%d secs]\n", delay));
      }
      break;
      
      case SOURCE_STATE_INITIALISING:
      case SOURCE_STATE_POWERED_OFF:
      {
            if (BdaddrIsZero(&theSource->ps_config->bdaddr_remote_device))
            {
                /* if the device was powered off and no previous connection exists:
                  use the power_on_discover_idle_timer before first discovery attempt */
                delay = theSource->ps_config->ps_timers.power_on_discover_idle_timer;
            }
            else
            {
                /* if the device was powered off and a previous connection exists:
                  use the power_on_connect_idle_timer before first connection attempt */
                delay = theSource->ps_config->ps_timers.power_on_connect_idle_timer;
            }
      }
      break;
      
      default:
      {
            
      }
      break;            
    }
   
    if (old_state != SOURCE_STATE_CONNECTING)
    {
      /* reset connection attempts if not currently connecting */
      theSource->connection_data.connection_retries = 0;
    }
   
    if (delay != 0)
    {
      /* enter connectable or discoverable state if a delay has been configured */
      if (BdaddrIsZero(&theSource->ps_config->bdaddr_remote_device) || theSource->inquiry_mode.force_inquiry_mode)
      {
            /* no device to connect to, go discoverable */
            MessageSend(&theSource->app_data.appTask, APP_ENTER_PAIRING_STATE_FROM_IDLE, 0);
      }
      else
      {
            /* there is a device to connect to, go connectable */
            MessageSend(&theSource->app_data.appTask, APP_ENTER_CONNECTABLE_STATE_FROM_IDLE, 0);
      }
    }

    if (delay != TIMER_NO_TIMEOUT)
    {
      STATES_DEBUG(("STATE: IDLE delay before next connection:[%d secs]\n", delay));      
      /* initialise the connection with the connection manager */
      connection_mgr_start_connection_attempt(NULL, AGHFP_PROFILE_IS_ENABLED ? PROFILE_AGHFP : PROFILE_A2DP, delay);      
    }
    else
    {
      STATES_DEBUG(("STATE: No auto reconnection\n"));
    }
}

951623847 发表于 2020-3-31 11:58:33

看到了,这里面执行的。
MessageSendLater(app_get_instance(), APP_CONNECT_REQ, message, D_SEC(delay));


static void app_connect_req(const APP_CONNECT_REQ_T *message)
{
    APP_MSG_DEBUG(("APP_MSG: app_connect_req\n"));

    /* cancel any other connection requests as they may come from user or be automatically generated */
    MessageCancelAll(app_get_instance(), APP_CONNECT_REQ);
   
    switch (states_get_state())
    {
      case SOURCE_STATE_IDLE:
      case SOURCE_STATE_CONNECTABLE:
      case SOURCE_STATE_DISCOVERABLE:
      case SOURCE_STATE_INQUIRING: /* connect after device found through inquiry */
      case SOURCE_STATE_CONNECTING: /* this will occur when trying to connect further profiles - re-enter state to kick connection */
      case SOURCE_STATE_CONNECTED:
      {
            /* suspend any active audio */
            MessageSend(app_get_instance(), APP_AUDIO_SUSPEND, 0);
            /* connect to remote device */
            if (BdaddrIsZero(connection_mgr_get_remote_address()) || message->force_inquiry_mode)
            {               
                states_set_state(SOURCE_STATE_INQUIRING);
            }
            else
            {               
                states_set_state(SOURCE_STATE_CONNECTING);            
            }
      }
      break;
               
      default:
      {
            app_msg_unhandled_state();
      }
      break;
    }
}


忙忙碌碌 发表于 2020-5-20 18:36:45

完整代码states_enter_state_idle中的if (delay != 0)代码:

1,如果从关机状态 开机 进入idle模式,如果 delay不等于0,那么执行以下内容:
2,如果无已连接设备记录BdaddrIsZero或者处于强制进入配对模式,那么发送消息APP_ENTER_PAIRING_STATE_FROM_IDLE;否则发送消息APP_ENTER_CONNECTABLE_STATE_FROM_IDLE
页: [1]
查看完整版本: csr867x source这段代码怎么理解?