【问题标题】:TinyOS event return type meaningTinyOS 事件返回类型含义
【发布时间】:2017-02-14 16:44:42
【问题描述】:

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

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

举个例子:

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

【问题讨论】:

  • 当你编译 nesC 代码时,你会收到类似calls to Counter.counterSent in ... are uncombined 的警告吗?

标签: tinyos nesc


【解决方案1】:

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

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

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

在您的代码 sn-p 等情况下会自动调用组合函数。

如果你想在这个例子中定义一个组合函数,你需要 typedef 事件的返回类型。请参阅4.4.3 Combine Functions 了解更多信息。

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多