nesC程序设计中添加模块的写法(示例)

要写TinyOS上的nesC程序,应该从添加模块功能做起。下面是个实例框架:

Employer模块在Employee模块的上层,使用Employee模块的接口Employment。
Employer要实现接口的event以往下层响应事件,Employee要实现接口的command以向上层提供命令。

===  EmployerAppC.nc(顶层配置文件)  ===

configuration EmployerAppC{
}
implementation{
    // ...
    components EmployeeC;  //添加EmployeeC组件

    //...
    EmployerC.Employment -> EmployeeC.Employment;  //添加EmployerC到EmployeeC关于Employment接口的接线
}

===  EmployerC.nc(Employer模块文件)  ===

module EmployerC{
    uses interface Employment; //使用Employment接口
}
implementation{
    //实现Employment接口的事件响应函数

    event void Employment.work1Done(error_t err){
        //...
        // call Employment.work2();
    }

    event void Employment.work2Done(error_t err){
        // ...
    }
}

===  Employment.nc(Employment接口文件)  ===

interface Employment{ //Employment接口定义
    command void work1();
    command void work2();

    event void work1Done(error_t result);
    event void work2Done(error_t result);
}

===  EmployeeC.nc(Employee模块文件)  ===

module EmployeeC{
    provides interface Employment; //提供Employment接口
    uses interface ???;
}

implementation{
    //实现Employment接口的命令函数

    command void Employment.work1(){ //这个command在上游过程中被调用
        // ...
        // signal Employment.work1Done(0);
    }

    command void Employment.work2(){
        // ...
        // signal Employment.work2Done(0);
    }
}

你可能感兴趣的:(框架,Module,command,interface,Signal)