ZigBee ZStack Protocol Stack Learning--Architecture Analysis

https://blog.csdn.net/zl374216459/article/details/59110230

This article focuses on analyzing the program architecture of ZStack (V3.0), only the most important parts are retained, and other parts are temporarily skipped. . .
1. The ZStack program starts from the main() function: open the IAR project->ZMain folder->ZMain.c file
The main function contains two important functions as follows: the system initialization function osal_init_system() and the start system function osal_start_system()
int main( void )
{
    ……
    osal_init_system ();
    osal_start_system();
    ...
}


2. The system initialization function osal_init_system() contains an important function Task initialization function osalInitTasks();
uint8 osal_init_system( void )
{
    ……
    participantTasks ();
    ...
}

3. Three related parameters in the task initialization function osalInitTasks():

tasksEvents: pointer to the first address of the event table
tasksCnt: total number of tasks
tasksArr[]: pointer array of task processing functions

void osalInitTasks( void )
{
    a. Define a tasksEvents pointer to tasksArr[]
    b. Clear the array buffer to 0
    c. Call the task initialization function in turn from the bottom layer to the application layer, and assign taskID, +1 for each layer

}


4. Start an infinite loop in the system function osal_start_system(), and repeatedly call the osal_run_system() function
void osal_start_system( void )
{
    for(;;)
    {
        osal_run_system();
    }
}

5. The osal_run_system() function first queries the event table from the bottom up. If there is a task event, it will call the task processing function. If an event occurs in the APP layer, call the zclSampleApp_event_loop() function to process

void osal_run_system( void )
{
    do{
        if (tasksEvents[idx]) // Query from bottom to top whether there is an event in each layer
        {
            break;
        }
    } while (++idx < tasksCnt);
    if (idx < tasksCnt)
    {
        events = tasksEvents[idx];
        tasksEvents[idx] = 0; // Temporarily clear the event flag
        events = (tasksArr[idx])( idx, events );// Call the task handler for processing
        tasksEvents[idx] |= events; // Reset unprocessed events
    }
}

6. In the task processing function, the basic framework is as follows:

uint16 zclSampleApp_event_loop( uint8 task_id, uint16 events )
{
    if ( events & SYS_EVENT_MSG )// Extract system events: data received, button state change, network state change, etc.
    {
        while ( (MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( zclSampleApp_TaskID )) )// Receive the data in the message queue corresponding to the task ID
        {
            switch ( MSGpkt->hdr.event ){
              case ZCL_INCOMING_MSG:
               zclSampleApp_ProcessIncomingMsg( (zclIncomingMsg_t *)MSGpkt );
              break;
            ……
            default:
              break;
            }
        }
        return (events ^ SYS_EVENT_MSG);// The processing is completed, clear the event flag to 0
    }
    if ( events & SAMPLEAPP_END_DEVICE_REJOIN_EVT )// Extract user-defined events
    {
        bdb_ZedAttemptRecoverNwk();// Event handler
        return ( events ^ SAMPLEAPP_END_DEVICE_REJOIN_EVT );//The processing is completed, clear the event flag
    }
    ……
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325877271&siteId=291194637
Recommended