Access to a file desciptor in a distance_sensor driver

Hi all,

I’m extending a driver (tfmini) with new features. The tfmini driver is currently reading data from device and I want to be able to write data to configure it.

I have implemented a new command to be called via “nsh”:

PRINT_MODULE_USAGE_COMMAND_DESCR("command","Configure driver (require -c hexacommand as in -c 5A04015F)");

This command is similar to “tfmini start” 'tfmini stop" and “tfmini status”: it will re-use the TFMINI object allocated upon “tfmini start” and will write commands to the tfmini driver. This is simplified code example:

uint8_t mycommand[8];
size_t mycommandlen=4;
mycommand[0] = 0x5A;
mycommand[1] = 0x04;
mycommand[2] = 0x01;
mycommand[3] = 0x5F;
ret = ::write(_fd, command, framelen);

The above is failing when called via nsh like:

$> tfmini command -c 5A04015F
result: fd=4 error: EBADF - Bad File Descriptor

However, I have tried to call the same “::write”, but inside the “TFMINI::collect()” method (this is where the ::read is made). The same ::write is successful in that method.

It looks like I can access the global TFMINI object and many of its parameters, but I cannot re-use the _fd parameter from this scope.

Can someone explain this? This will help me for further px4 dev…

Is there any solution to call ::write in the call stack of my “tfmini command -c 5A04015F” command line?

Tks to all!

File descriptors belong to the process (task) that creates them. So just open/close the device from within the currently executing context.

I was expecting such answer… Thanks a lot @dakejahl

I finally found minimal information about fd and task from dev.px4.io:

Modules are executed as tasks: they have their own file descriptor lists, but they share a single 
address space. A task can still start one or more threads that share the file descriptor list.

I guess there is no official documentation with more details… beyond the code!

The official documentation would be the NuttX docs.
https://nuttx.org/doku.php?id=documentation

Take a quick read of this article, it explains it well.
https://nuttx.org/doku.php?id=wiki:nxinternal:detachedfds

1 Like