Process Analysis Toolkit  (PAT) 3.5 Help  
3.7.1.2 The NesC Language

The development of NesC module in PAT aims at automatically analyzing, simulating and verifying NesC programs running on TinyOS. Therefore, we tried to support the full set of NesC syntax, including NesC structures, C-like language syntax, etc. A NesC application can be implemented by a number of .nc files and .h files, where .nc files define interface, module, or configuration, and .h files define some constants and data structures. NesC code implemented in the form of .nc files and .h files can be compiled, analyzed, simulated and verified in our tool.

(A) NesC Concepts

Interface

      An nesC interface is defined as the following:

Syntax of an interface definition file

interface name { 
                     command datatype name(datatype arg1, ...); 
                     ... 
                     event datatype name(datatype arg1, ...); 
                     ... 
              }

For example, the content of file Leds.nc defines the interface Leds:

Sample Code: Leds.nc

interface Leds { 
                    async command void led0On(); 
                    async command void led0Off(); 
                    async command void led0Toggle(); 
                    ... 
                }


  Module
      A nesC module defines a lower-level component, which can be referred by a higher-level one defined by a configuration. The syntax of a module is given as the following:

Syntax of a module definition file

module name { 
           
uses interface name; 
            ... 
           
provides interface name; 
            ... 
      }
implementation {
            datatype var, ...; 
           
command datatype name.name(datatype arg,...){ 
                  //command implementation statements 
            } 
            ... 
           
event datatype name.name(datatype arg,...){ 
                  //event implementation statements 
            } 
            ... 
           
task
void name(){ 
                  //task statements 
            }
            ...
      }


      For instance, the content of the file LedsP.nc defines a component named LedsP:

Sample Code: LedsP.nc

module LedsP() { 
          provides
               interface Init; 
               interface Leds; 
         

          uses
               interface GeneralIO as Led0; 
               interface GeneralIO as Led1; 
               interface GeneralIO as Led2; 
         

     }
implementation
          command error_t Init.init() { 
               atomic
                   
dbg("Init","LEDS: initialized.\n"); 
                    call Led0.makeOutput(); 
                   
... 
                   
call Led0.set(); 
                   
... 
              

            
return SUCCESS; 
         

          async command void Leds.led0On() { 
               call Led0.clr(); 
              
DBGLED(0); 
         

          async command void Leds.led0Off() { 
               call Led0.set(); 
              
DBGLED(0); 
         

          async command void Leds.led0Toggle() { 
               call Led0.toggle(); 
              
DBGLED(0); 
         

         
... 
     }



Configuration
      A configuration is the other kind of component (one kind is module) in NesC. A configuration wires components to one another via bi-directional interfaces.  In a configuration, components are wired to one another via bi-directional interfaces. These wiring statements are most important, because they bring all components defined elsewhere together to be an application. Each NesC application should have a configuration which is the top-level component and specifies the starting point of its execution.
The syntax of a configuration is shown below:

Syntax of a configuration definition file

configuration name { 
     
     provides interface name; 
          ... 
         
uses interface name; 
     }
implementation
          
component name, ...; 
          name(.name)
-> name(.name); 
          name(.name)
<- name(.name); 
          name(.name)
=
name(.name); 
          ... 
     }


(B) Datatypes and Data Structures
NesC supports the whole set of data types and structures of C language. In PAT, all these types and structures are supported, except floating and double data.

Category

Types

Primary data type

int, char, void

Integer type

int, int8_t, int16_t, int 32_t, int64_t,
      uint8_t, uint16_t, uint32_t, uint64_t

User defined type

typedef type identifier

Enumerated type

enum identifier {value 1, value 2, ...}

Structure

struct

Pointer

pointers of the supported types in


(C) Operators
The NesC module in PAT supports all the operators of NesC, which are the same as those of C language. The following table shows all the operators with their precedences, description and associativity.

 Precedence   Operator      Description    Associativity 

1

++ -- 
      ( ) 
      [ ]

Suffix increment and decrement
      Function call
      Array subscripting
      Element selection by reference
      Element selection through pointer

Left to right 

2

++  --
      +    -
      !     ~
     (type)
        *
       &
     sizeof

Prefixe increment and decrement
      Unary plus and minus
      Logical NOT and bitwise NOT
      Type cast
      Indirection (dereference)
      Address-of
      Size-of

Right to left

3

 *  /  %

 Multiplication, division, and modulus (remainder)

Left to right

4

 +   -

Addition and subtraction 

 Left to right

5

<<  >>

Bitwise left shift and right shift 

 Left to right

6

 <   <=
       >   >=

For relational operators less than (LT) and less than or equal to (LE)
      For relational operators greater than (GT) and greater than or equal to (GE)

 Left to right

7

 ==   !=

For relational equal to and inequal to

Left to right

8

 &

Bitwise AND 

Left to right

9

 ^

Bitwise XOR

Left to right

10

 |

Bitwise OR

Left to right

11

 &&

Logical AND

Left to right

12

 | |

Logical OR

Left to right

13

 c ? t : f

Ternary conditional

 Right to left

14

 =
       +=  -=
       *=   /=  %=

Direct assignment
      Assignment by sum or difference
      Assignment by product, quotient and remainder

Right to left


(D) Statements
Some important statements are given in the following table:

Statement

Syntax

if/if-else

same as C

while/do-while

same as C

for

same as C

command call

call intf.cmd(...);

event signal

signal intf.evnt(...);

task post

post taskname();

 


 
Copyright © 2007-2012 Semantic Engineering Pte. Ltd.