DA14580 RW消息事件处理----转

转自:https://www.cnblogs.com/yueqian-scut/p/5398209.html

软件版本:5.0.4

编辑器: Keil5

基于工程:\5.0.4\projects\target_apps\ble_examples\ble_app_ota\Keil_5 

1,

task列表:   rwip_config.h文件中

/// Tasks types definition
enum KE_TASK_TYPE
{
    TASK_NONE           = 0xFF,

    // Link Layer Tasks
    TASK_LLM            = 0   ,
    TASK_LLC            = 1   ,
    TASK_LLD            = 2   ,
    TASK_DBG            = 3   ,

    TASK_L2CM           = 4   ,
    TASK_L2CC           = 5   ,
    TASK_SMPM           = 6   ,
    TASK_SMPC           = 7   ,
    TASK_ATTM           = 8   ,   // Attribute Protocol Manager Task
    TASK_ATTC           = 9   ,   // Attribute Protocol Client Task
		
    TASK_ATTS           = 10  ,   // Attribute Protocol Server Task
    TASK_GATTM          = 11  ,   // Generic Attribute Profile Manager Task
    TASK_GATTC          = 12  ,   // Generic Attribute Profile Controller Task
    TASK_GAPM           = 13  ,   // Generic Access Profile Manager
    TASK_GAPC           = 14  ,   // Generic Access Profile Controller
    
    TASK_PROXM          = 15  ,   // Proximity Monitor Task
    TASK_PROXR          = 16  ,   // Proximity Reporter Task
    TASK_FINDL          = 17  ,   // Find Me Locator Task
    TASK_FINDT          = 18  ,   // Find Me Target Task
    TASK_HTPC           = 19  ,   // Health Thermometer Collector Task
    TASK_HTPT           = 20  ,   // Health Thermometer Sensor Task
    TASK_ACCEL          = 21  ,   // Accelerometer Sensor Task
    TASK_BLPS           = 22  ,   // Blood Pressure Sensor Task
    TASK_BLPC           = 23  ,   // Blood Pressure Collector Task
    TASK_HRPS           = 24  ,   // Heart Rate Sensor Task
    TASK_HRPC           = 25  ,   // Heart Rate Collector Task
    TASK_TIPS           = 26  ,   // Time Server Task
    TASK_TIPC           = 27  ,   // Time Client Task
    TASK_DISS           = 28  ,   // Device Information Service Server Task
    TASK_DISC           = 29  ,   // Device Information Service Client Task
    TASK_SCPPS          = 30  ,   // Scan Parameter Profile Server Task
    TASK_SCPPC          = 31  ,   // Scan Parameter Profile Client Task
    TASK_BASS           = 32  ,   // Battery Service Server Task
    TASK_BASC           = 33  ,   // Battery Service Client Task
    TASK_HOGPD          = 34  ,   // HID Device Task
    TASK_HOGPBH         = 35  ,   // HID Boot Host Task
    TASK_HOGPRH         = 36  ,   // HID Report Host Task
    TASK_GLPS           = 37  ,   // Glucose Profile Sensor Task
    TASK_GLPC           = 38  ,   // Glucose Profile Collector Task
    TASK_NBPS           = 39  ,   // Nebulizer Profile Server Task
    TASK_NBPC           = 40  ,   // Nebulizer Profile Client Task
    TASK_RSCPS          = 41  ,   // Running Speed and Cadence Profile Server Task
    TASK_RSCPC          = 42  ,   // Running Speed and Cadence Profile Collector Task
    TASK_CSCPS          = 43  ,   // Cycling Speed and Cadence Profile Server Task
    TASK_CSCPC          = 44  ,   // Cycling Speed and Cadence Profile Client Task
    TASK_ANPS           = 45  ,   // Alert Notification Profile Server Task
    TASK_ANPC           = 46  ,   // Alert Notification Profile Client Task
    TASK_PASPS          = 47  ,   // Phone Alert Status Profile Server Task
    TASK_PASPC          = 48  ,   // Phone Alert Status Profile Client Task

    TASK_LANS           = 49  ,   // Location and Navigation Profile Server Task
    TASK_APP            = 50  ,   // Do not Alter. 

    TASK_LANC           = 51  ,   // Location and Navigation Profile Client Task

    TASK_CPPS           = 52  ,   // Cycling Power Profile Server Task
    TASK_CPPC           = 53  ,   // Cycling Power Profile Client Task
    
    // Start of conditionally assigned task types
    
    #if (BLE_BM_SERVER)
    TASK_BMSS           ,   // BMSS Task
    #endif

    #if (BLE_BM_CLIENT)
    TASK_BMSC         ,   // BMSC Task
    #endif

    #if BLE_SPOTA_RECEIVER
    TASK_SPOTAR         ,   // SPOTA Receiver task
    #endif

    #if BLE_STREAMDATA_DEVICE
    TASK_STREAMDATAD    ,   // Stream Data Device Server task
    #endif

    #if BLE_STREAMDATA_HOST
    TASK_STREAMDATAH    ,   // Stream Data Device Server task
    #endif

    #if BLE_ANC_CLIENT
    TASK_ANCC           ,   // ANCS Client Task
    #endif

    #if BLE_WPT_CLIENT
    TASK_WPTC           ,   // A4WP Wireless Power Transfer Client Profile Task
    #endif

    #if BLE_WPTS
    TASK_WPTS           ,   // A4WP Wireless Power Transfer Server Profile Task
    #endif

    #if BLE_APP_PTU
     TASK_APP_PTU       ,   // A4WP Wireless Power Transfer Client App Task
    #endif

    #if BLE_IEU
    TASK_IEU            ,   // Integrated Environmantal Unit Task
    #endif

    #if BLE_MPU
    TASK_MPU            ,   // Motion Processing Unit Task
    #endif

    #if BLE_WSS_SERVER
    TASK_WSSS           ,   // Weight Scale Server Task
    #endif

    #if BLE_WSS_COLLECTOR
    TASK_WSSC           ,   // Weight Scale Collector Task
    #endif

    #if BLE_UDS_SERVER
    TASK_UDSS           ,   // User Data Server Task
    #endif

    #if BLE_UDS_CLIENT
    TASK_UDSC           ,   // User Data Server Task
    #endif

    #if BLE_SPS_SERVER
    TASK_SPS_SERVER     ,   // Serial Proert Service Server Task
    #endif

    #if BLE_SPS_CLIENT
    TASK_SPS_CLIENT     ,   // Serial Proert Service Server Task
    #endif

    #if BLE_ADC_NOTIFY
    TASK_ADC_NOTIFY     ,   // Serial Proert Service Server Task
    #endif

    #if BLE_DEVICE_CONFIG
    TASK_DEVICE_CONFIG  ,   // Serial Proert Service Server Task
    #endif

    #if (BLE_BCS_SERVER)
    TASK_BCSS          ,   // Body Composition Server Task
    #endif

    #if (BLE_BCS_CLIENT)
    TASK_BCSC          ,   // Body Composition Client Task
    #endif

    #if (BLE_CTS_SERVER)
    TASK_CTSS          ,   // Current Time Server Task
    #endif

    #if (BLE_CTS_CLIENT)
    TASK_CTSC          ,   // Current Time Client Task
    #endif

    #if BLE_CUSTOM2_SERVER
	TASK_CUSTS2		    ,	// 2nd Custom profile server
    #endif

    #if BLE_CUSTOM1_SERVER
	TASK_CUSTS1		    ,	// 1st Custom profile server
    #endif

    // End of conditionally assigned task types
    
    TASK_HCI            = 60  ,
    TASK_HCIH           = 61  ,

    TASK_GTL            = 63  ,

    #if USE_AES
    TASK_AES            = 62  ,   // Task for asynchronous AES API
    #endif    

    TASK_MAX            = 64,  //MAX is 64. Do  not exceed. 
};

RW内核最大支持64个task,链路层task的优先级高,接着是profile,再是TASK_APP。  数值越小,优先级越高。

RW内核消息处理机制

Task数据结构:

ke_task_desc:     ke_task.h中

/// Task descriptor grouping all information required by the kernel for the scheduling.
struct ke_task_desc
{
    /// Pointer to the state handler table (one element for each state).
    const struct ke_state_handler* state_handler;
    /// Pointer to the default state handler (element parsed after the current state).
    const struct ke_state_handler* default_handler;
    /// Pointer to the state table (one element for each instance).
    ke_state_t* state;
    /// Maximum number of states in the task.
    uint16_t state_max;
    /// Maximum index of supported instances of the task.
    uint16_t idx_max;
};

ke_state_handler:  ke_task.h中

/// Element of a state handler table.
struct ke_state_handler
{
    /// Pointer to the message handler table of this state.
    const struct ke_msg_handler *msg_table;
    /// Number of messages handled in this state.
    uint16_t msg_cnt;
};

ke_msg_handler:  ke_task.h中

/// Element of a message handler table.
struct ke_msg_handler
{
    /// Id of the handled message.
    ke_msg_id_t id;
    /// Pointer to the handler function for the msgid above.
    ke_msg_func_t func;
};

RW内核是基于状态机对消息进行处理的。从ke_task_desc看来,一个task包括显式的状态处理state_handler和默认的状态处理default_handler。

  state是task的状态机变量,task可能有多个状态,那么state_handler是状态处理集合,每个状态可能会处理多个消息回调,例如上层task会发送消息来执行调用,或者下层task发送消息来执行回调。

  default_handler处理的消息代表该task在任意状态时都可能受到的消息,例如底层task发出的断开连接消息。

  我们也可以得出,ke_state_handler代表一个状态下的多个消息处理。因此,state_handler是一个ke_state_handler数组,而default_handler则是ke_state_handler元素。

三、app_task

app_task是一个特殊的task,它和所有的profile_tasks进行交互,执行调用和回调。基于RW内核的状态机消息处理机制,调用和回调都是通过发送消息来进行完成回调的。

app_task是通过ke_task_create(TASK_APP, &TASK_DESC_APP)来注册消息回调的。

TASK_DESC_APP赋值为:   app.c中

/// Application Task Descriptor
static const struct ke_task_desc TASK_DESC_APP = {NULL,
                                                  &app_default_handler,
                                                  app_state,
                                                  APP_STATE_MAX,
                                                  APP_IDX_MAX};

app_task有默认的消息回调接口集app_default_handler:     app_task.c中

/* Default State handlers definition. */
const struct ke_msg_handler app_default_state[] =
{
    {KE_MSG_DEFAULT_HANDLER,                (ke_msg_func_t)app_entry_point_handler},

#if USE_LED
		{APP_TIMER_API_MES1,         (ke_msg_func_t)app_second_timer_handler},
#endif
};

(ke_msg_func_t)app_entry_point_handler 内容:

这里是注册 app_process_handle[]数组里面定义的是 函数指针

int app_entry_point_handler(ke_msg_id_t const msgid,
                            void const *param,
                            ke_task_id_t const dest_id,
                            ke_task_id_t const src_id)
{
    int i = 0;
    enum ke_msg_status_tag process_msg_handling_result;

    while (i < (sizeof(app_process_handlers) / sizeof(process_event_func_t)))
    {
        ASSERT_ERR(app_process_handlers[i]);
         if (app_process_handlers[i](msgid, param, dest_id, src_id, &process_msg_handling_result) == PR_EVENT_HANDLED)
         {
             return (process_msg_handling_result);
         }
         i++;
    }

    //user cannot do anything else than consume the message
    if (app_process_catch_rest_cb != NULL)
    {
        app_process_catch_rest_cb(msgid,param, dest_id, src_id);
    }

    return (KE_MSG_CONSUMED);
};

app_process_handle[]数组:代码是注册是注册 app_process_handle[]数组里面定义的函数。

/*
 * GLOBAL VARIABLES DEFINITION
 ****************************************************************************************
 */

const process_event_func_t app_process_handlers[] = {

#if (!EXCLUDE_DLG_GAP)
    (process_event_func_t) app_gap_process_handler,
#endif

#if (!EXCLUDE_DLG_TIMER)
    (process_event_func_t) app_timer_api_process_handler,
#endif

#if (!EXCLUDE_DLG_MSG)
    (process_event_func_t) app_msg_utils_api_process_handler,
#endif

#if ((BLE_APP_SEC) && (!EXCLUDE_DLG_SEC))
    (process_event_func_t) app_sec_process_handler,
#endif

#if ((BLE_DIS_SERVER) && (!EXCLUDE_DLG_DISS))
    (process_event_func_t) app_diss_process_handler,
#endif

#if ((BLE_PROX_REPORTER) && (!EXCLUDE_DLG_PROXR))
    (process_event_func_t) app_proxr_process_handler,
#endif

#if ((BLE_BAS_SERVER) && (!EXCLUDE_DLG_BASS))
    (process_event_func_t) app_bass_process_handler,
#endif

#if (((BLE_FINDME_TARGET) && (!EXCLUDE_DLG_FINDT)) || ((BLE_FINDME_LOCATOR) && (!EXCLUDE_DLG_FINDL)))
    (process_event_func_t) app_findme_process_handler,
#endif

#if ((BLE_SPOTA_RECEIVER) && (!EXCLUDE_DLG_SPOTAR))
    (process_event_func_t) app_spotar_process_handler,
#endif

#if ((BLE_CUSTOM1_SERVER) && (!EXCLUDE_DLG_CUSTS1))
    (process_event_func_t) app_custs1_process_handler,
#endif

#if ((BLE_CUSTOM2_SERVER) && (!EXCLUDE_DLG_CUSTS2))
    (process_event_func_t) app_custs2_process_handler,
#endif

};

注册app_custs1_process_handler任务:

enum process_event_response app_custs1_process_handler(ke_msg_id_t const msgid,
                                                       void const *param,
                                                       ke_task_id_t const dest_id,
                                                       ke_task_id_t const src_id,
                                                       enum ke_msg_status_tag *msg_ret)
{
    return app_std_process_event(msgid, param, src_id, dest_id, msg_ret, app_custs1_process_handlers,
                                         sizeof(app_custs1_process_handlers) / sizeof(struct ke_msg_handler));
}

每个函数都有一个ID 和一个回调函数。其他的函数注册也一样。

这样注册了可以用

void ke_timer_set(ke_msg_id_t const timer_id, ke_task_id_t const task, uint32_t delay);//来启动对应的事件

void ke_timer_clear(ke_msg_id_t const timer_id, ke_task_id_t const task);//来停止事件
 

消息回调接口集包括了GAP相关的消息,如BLE初始化过程的消息事件,和profile应用相关的数据通信消息事件。

GAP相关:app_task.c中

static const struct ke_msg_handler app_gap_process_handlers[]=
{
    {GAPM_DEVICE_READY_IND,                 (ke_msg_func_t)gapm_device_ready_ind_handler},
    {GAPM_CMP_EVT,                          (ke_msg_func_t)gapm_cmp_evt_handler},
    {GAPC_CMP_EVT,                          (ke_msg_func_t)gapc_cmp_evt_handler},
    {GAPC_CONNECTION_REQ_IND,               (ke_msg_func_t)gapc_connection_req_ind_handler},
    {GAPC_DISCONNECT_IND,                   (ke_msg_func_t)gapc_disconnect_ind_handler},
    {APP_MODULE_INIT_CMP_EVT,               (ke_msg_func_t)app_module_init_cmp_evt_handler},
    {GAPM_ADV_REPORT_IND,                   (ke_msg_func_t)gapm_adv_report_ind_handler},
#if (BLE_APP_SEC)
    {GAPC_SECURITY_IND,                     (ke_msg_func_t)gapc_security_ind_handler},
#endif
};

参考:

https://www.cnblogs.com/yueqian-scut/p/5398209.html

https://blog.csdn.net/a369000753/article/details/51456071

https://blog.csdn.net/yueqian_scut/article/details/51111669

猜你喜欢

转载自blog.csdn.net/jiangchao3392/article/details/84929984
今日推荐