事件驱动机制广泛应用于嵌入式系统,类似于中断机制,当有事件到来时(比如按键、数据到达),系统响应并处理该事件。相对于轮询机制,事件机制优势很明星,低功耗(系统处于休眠状态,当有事件到达时才被唤醒)和MCU利用率高。
Contiki将事件机制融入Protothreads机制,每个事件绑定一个进程(广播事件例外),进程间的消息也是通过事件来传递的。用无符号字符型来标示事件。事件结构体event_data定义如下:
struct event_data {
process_event_t ev;
process_data_t data;
struct process *p;
};
typedef unsigned char process_event_t;
typedef void * process_data_t;
用无符号字符型标识一个事件,Contiki定义了10个事件(0x80~0x8A),其它的供用户使用。
#define PROCESS_EVENT_NONE 0x80
#define PROCESS_EVENT_INIT 0x81
#define PROCESS_EVENT_POLL 0x82
#define PROCESS_EVENT_EXIT 0x83
#define PROCESS_EVENT_SERVICE_REMOVED 0x84
#define PROCESS_EVENT_CONTINUE 0x85
#define PROCESS_EVENT_MSG 0x86
#define PROCESS_EVENT_EXITED 0x87
#define PROCESS_EVENT_TIMER 0x88
#define PROCESS_EVENT_COM 0x89
#define PROCESS_EVENT_MAX 0x8a
每个事件绑定一个进程,如果p为NULL,表示该事件绑定所有进程(即广播事件PROCESS_BROADCAST)。除此之外,事件可以携带数据data,可以用着电进行进程间的通信(向另一进程传递带数据的事件)。
static process_num_events_t nevents, fevent;
static struct event_data events[PROCESS_CONF_NUMEVENTS]; #define PROCESS_CONF_NUMEVENTS 32
Contiki用一个全局的静态数组存放事件,这意味着事件数目在系统运行之前就要指定(用户可以通过PROCESS_CONF_NUMEVENTS自选配置大小),通过数组下标可以快速访问事件。系统还定义另两个全局静态变量nevents和fevent,分别用于记录未处理事件总数及下一个待处理的位置。事件逻辑组成环形队列,存储在数组里。如下图:
可见,对于Contiki系统而言,事件并没有优先级之分,而是先到先服务的策略,全局变量fevent记录了下一次待处理事件的下标。
事件产生
Contiki有两种方式产生事件,即同步和异步。同步事件通过process_post_synch函数产生,事件触发后直接处理(调用call_process函数)。而异步事件产生是由process_post产生,并没有及时处理,而是放入事件队列等待处理,process_post流程图如下:
int process_post(struct process *p, process_event_t ev, process_data_t data)
{
static process_num_events_t snum; if(PROCESS_CURRENT() == NULL) {
PRINTF("process_post: NULL process posts event %d to process '%s', nevents %d\n",
ev,PROCESS_NAME_STRING(p), nevents);
}
else {
PRINTF("process_post: Process '%s' posts event %d to process '%s', nevents %d\n",
PROCESS_NAME_STRING(PROCESS_CURRENT()), ev,
p == PROCESS_BROADCAST? "<broadcast>": PROCESS_NAME_STRING(p), nevents);
}
/*nevents未处理事件总数,事件队列满,返回PROCESS_ERR_FULL*/
if(nevents == PROCESS_CONF_NUMEVENTS) {
#if DEBUG
if(p == PROCESS_BROADCAST) {
printf("soft panic: event queue is full when broadcast event %d was posted from %s\n", ev, PROCESS_NAME_STRING(process_current));
} else {
printf("soft panic: event queue is full when event %d was posted to %s frpm %s\n", ev, PROCESS_NAME_STRING(p), PROCESS_NAME_STRING(process_current));
}
#endif /* DEBUG */
return PROCESS_ERR_FULL;
}
//事件队列未满,继续
snum = (process_num_events_t)(fevent + nevents) % PROCESS_CONF_NUMEVENTS;//取得循环队列中下一个空闲位置
events[snum].ev = ev;//将事件加入队列
events[snum].data = data;
events[snum].p = p;
++nevents; #if PROCESS_CONF_STATS
if(nevents > process_maxevents) {
process_maxevents = nevents;
}
#endif /* PROCESS_CONF_STATS */ return PROCESS_ERR_OK;
}
process_post首先判断事件队列是否已满,若满返回错误,否则取得下一个空闲位置(因为环形队列,需要做余操作),而后,设置该事件并将未处理事件总数加1。
事件调度
事件没有优先级,采用先到先服务策略,每一次系统轮询(process_run函数)只处理一个事件,do_event函数用于处理事件,其流程图如下:
/*
* Process the next event in the event queue and deliver it to
* listening processes.
*/
/*---------------------------------------------------------------------------*/
static void
do_event(void)
{
static process_event_t ev;
static process_data_t data;
static struct process *receiver;
static struct process *p; /*
* If there are any events in the queue, take the first one and walk
* through the list of processes to see if the event should be
* delivered to any of them. If so, we call the event handler
* function for the process. We only process one event at a time and
* call the poll handlers inbetween.
*/ if(nevents > ) { /* There are events that we should deliver. */
/*取出待处理事件*/
ev = events[fevent].ev; data = events[fevent].data;
receiver = events[fevent].p; /* Since we have seen the new event, we move pointer upwards
and decrese the number of events. */
/*更新fevent和nevents*/
fevent = (fevent + ) % PROCESS_CONF_NUMEVENTS;
--nevents; /* If this is a broadcast event, we deliver it to all events, in
order of their priority. */
/*是否广播事件*/
if(receiver == PROCESS_BROADCAST) {
for(p = process_list; p != NULL; p = p->next) { /* If we have been requested to poll a process, we do this in
between processing the broadcast event. */
/*有高优先级进程,运行所有高优先级进程*/
if(poll_requested) {
do_poll();
}
call_process(p, ev, data);//处理事件
}
}
else {
/* This is not a broadcast event, so we deliver it to the
specified process.不是广播事件,提供给特定进程 */
/* If the event was an INIT event, we should also update the
state of the process. 如果是初始化事件,设置进程状态为RUNNING*/
if(ev == PROCESS_EVENT_INIT) {
receiver->state = PROCESS_STATE_RUNNING;
} /* Make sure that the process actually is running. */
call_process(receiver, ev, data);//处理事件
}
}
}
do_event首先取出该事件(即,将事件的值复制到一个新变量),更新总的未处理事件总数及下一个待处理事件的数组下标(环形队列,需要取余操作)。接着判断事件是否为广播事件PROCESS_BROADCAST,若是,考虑到处理广播事件可能需要更多的时间,为保证系统实时性,先运行高优先级的进程,而后再去处理事件(调用call_process函数)。如果事件是初始化事件PROCESS_EVENT_INIT(创建进程的时候会触发此事件),需要将进程状态设为PROCESS_STATE_RUNNING。
事件处理
实际的事件处理是在进程的函数体thread,正如上文所述,call_process会调用thread函数,执行该进程。关键代码如下:
ret = p->thread(&p->pt, ev, data);
参考Jelline大神博客: http://jelline.blog.chinaunix.net