2017-02-14 22 views
2

所以在TinyOS中,一個接口由命令組成事件。當模塊使用接口時,它調用它的命令並提供其事件的實現(提供事件處理程序)。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的終值?

+1

當你編譯nesC代碼時,你是否遇到了'調用Counter.counterSent in ... uncombined'的警告? – IvanR

回答

1

該值未指定,或者該代碼甚至可能無法編譯。

在TinyOS中,有一個組合函數的概念,它們的簽名是type f(type, type),它們的目的是合理組合兩個相同類型的值。對於error_t有這樣的功能定義:

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

合併功能,如您的代碼段自動的情況下調用。

如果你想在這個例子中定義一個組合函數,你需要typedef事件的返回類型。有關更多信息,請參閱4.4.3 Combine Functions

請注意,情況對於命令是對稱的。您可以連接接口的兩種實現方式,以單個uses聲明是這樣的:

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

假設Counter有一個返回的東西,每當該命令ExampleP電話,兩個實現執行和兩個返回值組合的命令。