2

所以在 TinyOS 中,界面是由命令事件组成的。当模块使用接口时,它会调用其命令并提供其事件的实现(提供事件处理程序)。

命令的返回类型的含义很清楚,它与任何编程语言中的任何函数/方法的含义相同,但是事件的返回类型对我来说并不清楚。

举个例子:

interface Counter{
    command int64_t getCounter(int64_t destinationId);
    event int64_t counterSent(int64_t destinationId);
}

让我们定义一个提供Counter 接口的模块。

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的最终值是多少?

4

1 回答 1

1

该值未指定,否则此代码甚至可能无法编译。

在 TinyOS 中,有一个combine functions的概念,它是有签名的type f(type, type),它们的目的是合理地组合两个相同类型的值。因为error_t定义了这样一个函数:

error_t ecombine(error_t e1, error_t e2) {
  return (e1 == e2) ? e1 : FAIL;
}

在代码片段等情况下会自动调用组合函数。

如果要在本例中定义组合函数,则需要 typedef 事件的返回类型。有关详细信息,请参阅4.4.3 组合功能

请注意,命令的情况是对称的。您可以将接口的两个实现连接到单个uses声明,如下所示:

configuration ExampleC {
  ExampleP.Counter -> Counter1;
  ExampleP.Counter -> Counter2;
}

假设Counter有一个返回某些东西的命令,每当ExampleP调用该命令时,都会执行两个实现并组合两个返回值。

于 2017-02-15T16:53:52.960 回答