因此,在TinyOS中,接口由命令和事件组成。当一个模块使用一个接口时,它会调用它的命令并提供其事件的实现(提供一个事件处理程序)。
命令返回类型的含义是明确的,它与任何编程语言中的任何函数/方法相同,但是,事件的返回类型对我来说并不清楚。
,让我们举一个例子:
interface Counter{
command int64_t getCounter(int64_t destinationId);
event int64_t counterSent(int64_t destinationId);
}让我们定义一个提供计数器接口的模块。
module Provider{
provides interface Counter;
}
implementation {
int64_t counter;
counter = 75; //Random number
/**
Returns the value of the counter and signals an event that
someone with id equal to destinationId asked for the counter.
**/
command int64_t getCounter(int64_t destinationId){
int64_t signalReturnedValue;
signalReturnedValue = signal counterSent(destinationId);
return counter;
}
}现在,让我们定义两个使用此接口的模块。
module EvenIDChecker{
uses interface Counter;
}
implementation{
/**
Returns 1 if the destinationId is even, 0 otherwise
**/
event int64_t counterSent(int64_t destinationId){
if(destinationId % 2 == 0){
return 1;
} else {
return 0;
}
}
}现在,让我们定义另一个模块,它使用相同的接口,但与EvenIDChecker模块相反。
module OddIDChecker{
uses interface Counter;
}
implementation{
/**
Returns 1 if the destinationId is odd, 0 otherwise
**/
event int64_t counterSent(int64_t destinationId){
if(destinationId % 2 == 1){
return 1;
} else {
return 0;
}
}
}最后,var signalReturnedValue的最终值是多少?
发布于 2017-02-15 16:53:52
该值未指定,或者此代码甚至可能不编译。
在TinyOS中,有一个组合函数的概念,它是签名type f(type, type),它们的目的是合理地组合两个相同类型的值。对于error_t,定义了这样一个函数:
error_t ecombine(error_t e1, error_t e2) {
return (e1 == e2) ? e1 : FAIL;
}在代码片段这样的情况下,组合函数会自动调用。
如果您想在本例中定义一个组合函数,则需要对事件的返回类型进行类型标注。有关详细信息,请参阅4.4.3合并功能。
请注意,对于命令,情况是对称的。您可以将接口的两个实现连接到一个uses声明中,如下所示:
configuration ExampleC {
ExampleP.Counter -> Counter1;
ExampleP.Counter -> Counter2;
}假设Counter有一个返回某项的命令,每当ExampleP调用该命令时,就会执行两个实现,并组合两个返回值。
https://stackoverflow.com/questions/42231622
复制相似问题