began to learn TI's ZigBee protocol stack, wireless Dragon's data to see a lot of confusion, in the network to find many analysis of the stack of articles, first paste up, and so they have time to have a good original.
Protocol stack version: zstack-1.4.3-1.2.1
TI z-stack Protocol stack Learning-adding new tasks
How to achieve your task in 1.Zstack http://zhenling.chen.blog.163.com/blog/static/1940851920097710392587/
In the Zstack (TI's ZigBee protocol stack), the newly created tasks for each user typically require two related processing functions, including:
(1). Functions for initialization, such as: Sampleapp_init (), this function is called in the Osalinittasks () osal (the small operating system that comes with the zstack), and its purpose is to put some variables in the task that some users write themselves, the network mode, Initialization of the type of network terminal;
(2). Event handlers to be executed after the event that caused the change in the status of the task, such as:
Sampleapp_processevent (), this function is first set (bound) in the const PTASKEVENTHANDLERFN tasksarr[], and then in the Osalinittasks () If an event occurs, call the bound event handler.
The following is a 3-part analysis.
1. User-designed task code in Zstack call procedure
(1). Main () execution (in zmain.c)
Main ()---> Osal_init_system ()
(2). Osal_init_system () call Osalinittasks (), (in OSAL.C)
Osal_init_system ()---> osalinittasks ()
(3). Osalinittasks () call Sampleapp_init (), (in osal_sampleapp.c)
Osalinittasks ()---> sampleapp_init ()
Multiple task initialization settings were implemented in Osalinittasks (), where several lines of Mactaskinit (taskid++) to Zdapp_init (taskid++) represent calls to run initialization tasks for several systems. The user's own implementation of the Sampleapp_init () in the end, here TaskID with the increase of the task increases. So the initialization of the user's own tasks should be incremented in osalinittasks ().
void Osalinittasks (void)
{
Uint8 TaskID = 0;
It is important here to call Osal_mem_alloc () to allocate storage space for each task in the current osal (actually an array of any//services), and point to the task Array (task queue) with tasksevents.
Tasksevents = (UInt16 *) osal_mem_alloc (sizeof (uint16) * taskscnt);
Osal_memset (tasksevents, 0, (sizeof (uint16) * taskscnt)); Zeroing the space that tasksevents points to
Mactaskinit (taskid++);
Nwk_init (taskid++);
Hal_init (taskid++);
#if defined (mt_task)
Mt_taskinit (taskid++);
#endif
Aps_init (taskid++);
Zdapp_init (taskid++);
Sampleapp_init (TaskID); Tasks that users need to add themselves
}
2. Important data structures for task processing calls
Here to explain, in the Zstack, for the same task may have a variety of events, you need to perform different event handling, for convenience, the event handler for each task is implemented uniformly in an event handler, and then based on the task ID number (TASK_ID) and the task's specific events (events) Invokes an event handler for a task, and after entering the task's event handler, it is then determined by events to determine which event of the task occurred, and then perform the corresponding event handling. PTASKEVENTHANDLERFN is a pointer to a function (event handler function) Pointer, each of the array elements implemented here corresponds to a task's event handler function, such as sampleapp_processevent for the user to implement the event handler function UInt16 sampleapp_processevent (uint8 task_ ID, uint16 events), so if we implement a task here, we also need to add the event handler function that implements the task here.
Const PTASKEVENTHANDLERFN tasksarr[] = {
Maceventloop,
Nwk_event_loop,
Hal_processevent,
#if defined (mt_task)//a MT task command
Mt_processevent,
#endif
Aps_event_loop,
Zdapp_event_loop,
Sampleapp_processevent
};
Note that the order in Tasksevents and tasksarr[] is one by one corresponding, and the I event handler function in tasksarr[] corresponds to the event of the I task in tasksevents.
Calculate the number of tasks
Const UINT8 taskscnt = sizeof (Tasksarr)/sizeof (tasksarr[0]);
UInt16 *tasksevents;
3. Calls to the task handler function after different events occur
Osal_start_system () is important, which determines that the corresponding event handler is called when a task's event occurs
void Osal_start_system (void)
{
#if!defined (zbit)
for (;;)//Forever Loop
#endif
{
Uint8 idx = 0;
Hal_processpoll (); This replaces Mt_serialpoll () and Osal_check_timer ().
Here is the rotation task queue and check if there is a task event occurring
do {
if (Tasksevents[idx])//Task is highest.
{
Break
}
} while (++idx < taskscnt);
if (IDX < taskscnt)
{
UInt16 events;
halintstate_t intstate;
Hal_enter_critical_section (intstate);
events = Tasksevents[idx]; A task event that handles the IDX, which is the event of the IDX task has occurred
TASKSEVENTS[IDX] = 0; Clear the Events for this task.
Hal_exit_critical_section (intstate);
Corresponds to the event handler function that invokes the IDX task, with events to indicate what the event
Events = (Tasksarr[idx]) (IDX, events);
When not done, return events to TASKSEVENTS[IDX]
Hal_enter_critical_section (intstate);
TASKSEVENTS[IDX] |= events; Add back unprocessed events to the current task.
Hal_exit_critical_section (intstate);
}
#if defined (power_saving)
else/complete pass through all tasks events with no activity?
{
Osal_pwrmgr_powerconserve (); Put the Processor/system into sleep
}
#endif
}
}
=========================================================================
2.z-stack Add a new task http://www.wyouxi.com/index.php/group_thread/view/id-8583
Add the appropriate items to Osalinittasks () and tasksarr[]. 1. Modify Osalinittasks () void osalinittasks (void) {... Ouhsapp_init (taskid++); Photoapp_init (TaskID); } 2. Modify Tasksarr[]const Ptaskeventha ...
Add the appropriate items to Osalinittasks () and tasksarr[].
1. Modify Osalinittasks ()
void Osalinittasks (void)
{
......
Ouhsapp_init (taskid++);
Photoapp_init (TaskID);
}
2. Modify tasksarr[]
Const PTASKEVENTHANDLERFN tasksarr[] = {
......
Ouhsapp_processevent
Photoapp_processevent
};
3. Add _init () and _processevent ()
void Photoapp_init (Uint8 task_id)
{
Photoapp_taskid = task_id;
Photoinit ();
Registerforphoto (Photoapp_taskid);
}
UInt16 photoapp_processevent (uint8 task_id uint16 events)
{
afincomingmsgpacket_t *msgpkt;
if (Events &sys_event_msg)
{
MSGPKT = (afincomingmsgpacket_t *) osal_msg_receive (photoapp_taskid);
while (MSGPKT)
{
Switch (msgpkt->hdr.event)
{
Case Photo_change:
Halledblink (hal_led_1 3 30 300);
P0ie = 1;
Break
}
Release the memory
Osal_msg_deallocate ((uint8 *) msgpkt);
Next-if one is available
MSGPKT = (afincomingmsgpacket_t *) osal_msg_receive (photoapp_taskid);
}
Return unprocessed events
Return (events ^ sys_event_msg);
}
Discard Unknown Events
return 0;
}
"Turn" TI z-stack protocol Stack Learning-Add new tasks