ZigBee protocol and development-Preliminary Analysis of zstack protocol stack structure

Source: Internet
Author: User

 

Int main (void)
{
// Turn off interrupts (Guanzhong disconnection)
Osal_int_disable (ints_all );

// Initialization for Board related stuff such as LEDs (initialization board components, such as led) hal_board_init ();

// Make sure supply voltage is high enough to run (voltage check)
Zmain_vdd_check ();

// Initialize board I/O (initialize the I/O interface)
Initboard (ob_cold );

// Initialze Hal drivers (initialize the Hal device and implement it in hal_drivers.c)
Haldriverinit ();

// Initialize NV System (initialize the Nv system, that is, non-loss-prone devices, such as flash)
Osal_nv_init (null );

// Initialize the MAC (initialize Mac)
Zmacinit ();

// Determine the extended address (determine the Long Address of the device)
Zmain_ext_addr ();

// Initialize basic NV items (initialize the global variable of zstack. If it does not exist in the Nv memory, write the default value)
Zginit ();

# Ifndef nonwk
// Since the AF isn't a task, call it's initialization routine
Afinit ();
# Endif

// Initialize the Operating System (initialize the operating system)
Osal_init_system ();

// Allow interrupts (on interrupt)
Osal_int_enable (ints_all );

// Final board initialization (last initialization Board)
Initboard (ob_ready );

// Display information about this device (display device information. For example, the IEEE address of the collectoreb board is displayed in this function .)
Zmain_dev_info ();

/* Display the device info on the LCD */(display LCD device information)
# Ifdef LCD _supported
Zmain_ LCD _init ();
# Endif

# Ifdef wdt_in_pm1
/* If wdt is used, this is a good place to enable it. */(Open watchdog)
Watchdogenable (wdtimx );
# Endif

Osal_start_system (); // No return from here (starting the operating system actually enters an endless loop)

Return 0; // shouldn't get here.
} // Main ()

Note: Some of the above Initialization is related to hardware, some are related to the Operating System (starting with osal), and finally the system calls osal_start_system to enter the loop, and then processes messages in the loop, the following is the main body of this function (see osal. c ):

Void osal_start_system (void)
{
# If! Defined (zbit )&&! Defined (ubit)
For (;) // forever loop (endless loop)
# Endif
{
Uint8 idx = 0;

Osaltimeupdate (); // Update Time
Hal_processpoll (); // This replaces mt_serialpoll () and osal_check_timer (). (round-robin of device messages such as uart and timer)

Do {
If (tasksevents [idx]) // task is highest priority that is ready.
{
Break;
}
} While (++ idx <taskscnt );

If (idx <taskscnt)
{
Uint16 events;
Halintstate_t intstate;

Hal_enter_critical_section (intstate); // lock to prevent burst access event queues
Events = tasksevents [idx];
Tasksevents [idx] = 0; // clear the events for this task.
Hal_exit_critical_section (intstate );


Events = (tasksarr [idx]) (idx, events); // process the event to be processed and return the event ID

Hal_enter_critical_section (intstate );
Tasksevents [idx] | = events; // Add back unprocessed events to the current task. (update event status)
Hal_exit_critical_section (intstate );
}
# If defined (power_saving)
Else // complete pass through all task events with no activity?
{
Osal_pwrmgr_powerconserve (); // put the processor/system into sleep (no event processing, enters sleep state)
}
# Endif
}
}

The lines marked in red need to be specially parsed. This area looks like a function pointer. View taskarr definition (see SAPI. c) as follows:

# If osal_sapi
// The order in this table must be identical to the task initialization callbelow inosalinittask.
Const ptaskeventhandlerfn tasksarr [] = {
Maceventloop,
Nwk_event_loop,
Hal_processevent,
# If defined (mt_task)
Mt_processevent,
# Endif
Aps_event_loop,
Zdapp_event_loop,

Sapi_processevent
};

The above is basically *** eventloop, which can be inferred to be the event processing function that processes information of different layers, such as MAC, nwk, Hal, APS, and zdapp. Taking maceventloop as an example, you can find the function definition in the mac_api.h file as follows:

Extern uint16 maceventloop (uint8 taskid, uint16 events );
However, the file in which the preceding function is implemented is not tracked. However, for the temperature monitoring application demo, the above sapi_processevent () function is implemented in SAPI. C as follows:

Uint16 sapi_processevent (byte task_id, uint16 events)
{
Osal_event_hdr_t * PMSG;
Afincomingmsgpacket_t * pmsgpkt;
Afdataconfirm_t * pdataconfirm;
 

If (events & sys_event_msg) // system event message, which appears to be processed by upper-layer applications only when sapi_cb_func is defined.
{

...

Case key_change:
# If (sapi_cb_func)
Zb_handlekeys (keychange_t *) PMSG)-> state, (keychange_t *) PMSG)-> keys );
# Endif
Break;

...
// Return unprocessed events
Return (events ^ sys_event_msg );
}

If (events & zb_allow_bind_timer) // zb_allow_bind_timer
{
Afsetmatch (sapi_epdesc.simpledesc-> endpoint, false );
Return (events ^ zb_allow_bind_timer );
}

If (events & zb_bind_timer) // zb_bind_timer
{
// Send bind confirm callback to Application
Sapi_bindconfirm (sapi_bindinprogress, zb_timeout );
Sapi_bindinprogress = 0 xFFFF;

Return (events ^ zb_bind_timer );
}

If (events & zb_entry_event) // zb_entry_event
{
Uint8 startoptions;

// Give indication to application of device startup
# If (sapi_cb_func)
Zb_handleosalevent (zb_entry_event );
# Endif

// Led off cancels hold_auto_start blink set in the stack
Halledset (hal_led_4, hal_led_mode_off );

Zb_readconfiguration (zcd_nv_startup_option, sizeof (uint8), & startoptions );
If (startoptions & zcd_startopt_auto_start)
{
Zb_startrequest ();
}
Else
{
// Blink LEDs and wait for external input to config and restart
Halledblink (hal_led_2, 0, 50,500 );
}

Return (events ^ zb_entry_event );
}

// This must be the last event to be processed
If (events & (zb_user_events ))
{
// User events are passed to the application
# If (sapi_cb_func)
Zb_handleosalevent (events );
# Endif

// Do not return here, return 0 later
}

// Discard unknown events
Return 0;
}

In the above Code, only those marked with black are uploaded to application-layer APL for processing, and these codes are exactly what you need to implement, which are implemented in demosensor. C and democollector. C respectively. The vro code is shown as follows:

Void zb_handlekeys (uint8 shift, uint8 keys)
{
Static uint8 allowbind = false;
Static uint8 allowjoin = true;
Uint8 logicaltype;
 
 
// Shift is used to make each button/switch dual purpose.
If (shift)
{
If (Keys & hal_key_sw_1)
{
}
If (Keys & hal_key_sw_2)
{
}
If (Keys & hal_key_sw_3)
{
}
If (Keys & hal_key_sw_4)
{
}
}
Else
{
If (Keys & hal_key_sw_1) // joystick up key (set as cordinator, set as Coordinator)
{
If (appstate = app_init)
{
// Key 1 starts device as a coordinator
Logicaltype = zg_devicetype_coordinator;
Zb_writeconfiguration (zcd_nv_logical_type, sizeof (uint8), & logicaltype );

// Reset the device with new configuration
Zb_systemreset ();
}
}
If (Keys & hal_key_sw_2) // joystick right key (allow bind, which can be bound. There is only one node in a network, that is, the Coordinator has this setting .)
{
Allowbind ^ = 1;
If (allowbind)
{
// Turn on allow bind mode Infinitly
Zb_allowbind (0xff );
Halledset (hal_led_2, hal_led_mode_on );
// This node is the gateway node
Isgateway = true;

// Update the display
# If defined (LCD _supported)
Hallcdwritestring ("Gateway mode", hal_ LCD _line_2 );
# Endif
}
Else
{
// Turn off allow bind mode Infinitly
Zb_allowbind (0x00 );
Halledset (hal_led_2, hal_led_mode_off );
Isgateway = false;

// Update the display
# If defined (LCD _supported)
Hallcdwritestring ("Collector", hal_ LCD _line_2 );
# Endif
}
}
If (Keys & hal_key_sw_3) // joystick down key (the vro is valid and is used to periodically report events to the Coordinator to generate a topology)
{
// Start reporting
Osal_set_event (sapi_taskid, my_report_evt );
}
If (Keys & hal_key_sw_4) // joystick left key (used to disable/allow node addition. This function is used to enable or disable node addition for the coordinator, but I do not know why it is invalid !!?)
{
// Key 4 is used to control which Routers
// That can accept join requests
Allowjoin ^ = 1;
If (allowjoin)
{
Nlme_permitjoiningrequest (0xff );
}
Else {
Nlme_permitjoiningrequest (0 );
}
}
}
}

Another processing function is as follows:

Void zb_handleosalevent (uint16 event)
{
Uint8 logicaltype;
 
If (event & sys_event_msg) // system message, most of which have been processed at the underlying layer, so this field is empty.
{

}
 
If (event & zb_entry_event) // set the system startup event.
{
// Initialise UART
Inituart (uartrxcb );

// Blind led 1 to indicate starting/joining a network
Halledblink (hal_led_1, 0, 50,500 );
Halledset (hal_led_2, hal_led_mode_off );

// Read logical device type from NV
Zb_readconfiguration (zcd_nv_logical_type, sizeof (uint8), & logicaltype );

// Start the device
Zb_startrequest ();
}
 
If (event & my_start_evt)
{
Zb_startrequest ();
}
 
If (event & my_report_evt)
{
If (isgateway)
{
Osal_start_timerex (sapi_taskid, my_report_evt, myreportperiod );
}
Else if (appstate = app_binded)
{
Senddummyreport ();
Osal_start_timerex (sapi_taskid, my_report_evt, myreportperiod );
}
}
If (event & my_find_collector_evt)
{
// Find and bind to a gateway device (if this node is not gateway)
If (! Isgateway)
{
Zb_binddevice (true, dummy_report_assist_id, (uint8 *) null );

}
}
 
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.