蓝牙协议栈中的 函数指针

蓝牙4.0对事件处理的方式为:把所有的事件放在一个事件表中,每个事件都有优先级,协议执行的整个过程为轮询事件表,响应事件。而事件在时间表中的存储方式为函数指针。

下面结合代码部分:

定义函数指针:typedef unsigned short (*pTaskEventHandlerFn)( unsigned char task_id, unsigned short event );

我一直觉得这个地方用的非常巧妙,在以后的编程中应该会用到。

定义事件表:extern const pTaskEventHandlerFn tasksArr[];

初始化事件表:

 1 const pTaskEventHandlerFn tasksArr[] =
 2 {
 3   LL_ProcessEvent,                                                  // task 0
 4   Hal_ProcessEvent,                                                 // task 1
 5   HCI_ProcessEvent,                                                 // task 2
 6 #if defined ( OSAL_CBTIMER_NUM_TASKS )
 7   OSAL_CBTIMER_PROCESS_EVENT( osal_CbTimerProcessEvent ),           // task 3
 8 #endif
 9   L2CAP_ProcessEvent,                                               // task 4
10   GAP_ProcessEvent,                                                 // task 5
11   GATT_ProcessEvent,                                                // task 6
12   SM_ProcessEvent,                                                  // task 7
13   GAPRole_ProcessEvent,                                             // task 8
14   GAPBondMgr_ProcessEvent,                                          // task 9
15   GATTServApp_ProcessEvent,                                         // task 10
16   SimpleBLEPeripheral_ProcessEvent                                  // task 11
17 };

事件初始化:

 1 void osalInitTasks( void )
 2 {
 3   uint8 taskID = 0;
 4 
 5   tasksEvents = (uint16 *)osal_mem_alloc( sizeof( uint16 ) * tasksCnt);
 6   osal_memset( tasksEvents, 0, (sizeof( uint16 ) * tasksCnt));
 7 
 8   /* LL Task */
 9   LL_Init( taskID++ );
10 
11   /* Hal Task */
12   Hal_Init( taskID++ );
13 
14   /* HCI Task */
15   HCI_Init( taskID++ );
16 
17 #if defined ( OSAL_CBTIMER_NUM_TASKS )
18   /* Callback Timer Tasks */
19   osal_CbTimerInit( taskID );
20   taskID += OSAL_CBTIMER_NUM_TASKS;
21 #endif
22 
23   /* L2CAP Task */
24   L2CAP_Init( taskID++ );
25 
26   /* GAP Task */
27   GAP_Init( taskID++ );
28 
29   /* GATT Task */
30   GATT_Init( taskID++ );
31 
32   /* SM Task */
33   SM_Init( taskID++ );
34 
35   /* Profiles */
36   GAPRole_Init( taskID++ );
37   GAPBondMgr_Init( taskID++ );
38 
39   GATTServApp_Init( taskID++ );
40 
41   /* Application */
42   SimpleBLEPeripheral_Init( taskID );
43 }
当你坚持做一件完全正确的事情,有可能在很长一段时间内,你的价值都是零。
原文地址:https://www.cnblogs.com/lweleven/p/ble_fun_pointer.html