[Tinyos-help] Calling nesC Commands from C functions

David Gay dgay42 at gmail.com
Fri Jul 7 21:11:59 PDT 2006


On 7/7/06, Jay Taneja <taneja at cs.berkeley.edu> wrote:
> I have a program where some processing is performed in an included C
> function. Ideally, I would like the C function to call and return the
> result of a nesC command. The reason I am using C is because the
> function takes a variable number of arguments, a feature not yet
> supported in nesC. However, I am unsure if there is any way to call a
> nesC command in my C function. I realize it is possible to have the C
> function simply return the parameters to be passed to the nesC
> command, but that involves two separate operations to be called in my
> module. Any ideas how to get around this?
>
> Here is some simple code:
>
> # myfile.h
> bool my_func(int number, ...);
>
> # myfile.c
> bool my_func(int number, ...) {
>    return call MyProg.my_cmd(number);
> }
>
> Hopefully I've described the issue enough. Thanks.

There's no direct way to do this. call refers to an interface, and
there's no interfaces in C code. But you can do this kind of thing
indirectly, by using the "C" attribute inside a module.

Here's how:

in your C function, call some function foo:

void foo(...); // declare foo's prototype

bool my_func(...) {
  foo(...);
}

in your module which has access to the command you want to call
(either it defines it, or it uses an interface that contains it):

module MyModule {
   ...
}
implementation {

  void foo(...) __attribute__((C)) {
    call MyInterface.myCommand(...);
  }
}

the __attribute__((C)) thing puts foo into the C name space, so that
you can call it from C code.

If you use nesC 1.2 or later, you can replace __attribute__((C)) my
@C() (identical but shorter). And, if using nesC 1.2 or later, don't
use @C() inside a generic component... (if you create several
instances, you'll end up with several C functions with the same name,
which won't work...).

David


More information about the Tinyos-help mailing list