-1

I am having a kernel driver which is currently reading data from a sensor. Now I have to write a user space application which will call kernel's sensor_read() API and send data to cloud.

How can I expose kernel's sensor_read() call to user space and read this data from user space? Data is about 10 bytes.

Sam Protsenko
  • 14,045
  • 4
  • 59
  • 75
  • There are many ways to get there. But you didnt even say what sensor you like to read from. How about using `lm-sensor` ? Then you want send data to a cloud which is a complete different story. KeyWord: common-gateway-interface (cgi) – Mario Apr 04 '17 at 13:51
  • Thanks suleiman.. I am working on a Accl-Gyro sensor. I will check lm-sensor. Cloud part is already done, need to implement exposed cloud API's. – Sandeep Sharma Apr 06 '17 at 05:37

3 Answers3

3

How can I expose kernel's sensor_read() call to user space and read this data from user space?

Most likely you should use IIO kernel framework, as it's specifically designed for writing sensor drivers. IIO exposes necessary files for your driver (in /sys/bus/iio/ and /dev/iio*). You can read() those files, or poll() them (to handle interrupts).

Official documentation is available here. You can also use some existing drivers as a reference, look here: drivers/iio/ .

Before IIO framework was introduced, it was common to provide sysfs files for drivers manually. So if you use old enough kernel, that should be the way to write the driver: handle your bus (like I2C) and sysfs files manually. But still, the best way is to use new kernel and IIO.

I am working on a Gyro + accel sensor. Linux driver will be sending events of type EV_MSC for both of them

It's not unusual for chip to have more than one sensor on it. In that case you should create two different drivers: one for accelerometer, and one for gyro. This way you will have two different files, one file per sensor.

For example, look how it's done for LSM330DLC chip (accelerometer + gyro):

Both drivers are calling iio_device_register() function from driver's probe function, which creates corresponding files (which you can read/poll). Refer to documentation for further details.

As per my understanding I will open both the input devices from user space and add then to list of FD's which we want to poll. So when there is a new event how I can determine whether this event is from Gyro or aceel?

If you are curious about how to handle two /dev/input/event* files in user space, you basically have two choices:

  • using blocking I/O: you can read/poll them in different threads
  • using non-blocking I/O: you can open those files as O_NONBLOCK and just read() them in the same one thread; if new data is not available yet -- it will return -1 and errno will be set to EAGAIN; you can do those reads in infinite loop, for instance

This answer contains example of how to handle input file in C. And here you can read about blocking/non-blocking I/O.

Community
  • 1
  • 1
Sam Protsenko
  • 14,045
  • 4
  • 59
  • 75
  • Thanks Sam... poll() seems to be good approach. But I have one doubt. Basically I am working on a Gyro + accel sensor. Linux driver will be sending events of type EV_MSC for both of them. As per my understanding I will open both the input devices from user space and add then to list of FD's which we want to poll. So when there is a new event how I can determine whether this event is from Gyro or aceel? – Sandeep Sharma Apr 06 '17 at 05:35
  • @SandeepSharma I edited my answer and addressed your question there. – Sam Protsenko Apr 06 '17 at 11:39
  • Thanks a lot for such a wonderful Explanation... I will check it... will update . – Sandeep Sharma Apr 06 '17 at 13:52
  • I already did that .. but It says it will not affect publicly displayed score.May be I am new that's why.... – Sandeep Sharma Apr 07 '17 at 05:00
1

You can use IOCTL (ioctl/read) calls to access the kernel functions from User space.

Refer below link for sample: http://www.tldp.org/LDP/lkmpg/2.4/html/x856.html

Rajeshkumar
  • 739
  • 5
  • 16
1

There are many ways to access sensors data from kernel space to userspace

  1. Check for relevant driver of the sensor you used. Check whether it supports/provides sysfs support.
  2. you can read the data from /sys/class/ interfaces. You need to ensure that the relevant parameters exported to sysfs. For eg: Temperature sensors should have exported temperature values(equivalent factors) in sysfs entry.

    Examples(Below examples are fictions only)

    cat /sys/class/hwmon/tempsensor/value

    cat /sys/class/hwmon/tempsensor/min_value

    cat /sys/class/hwmon/tempsensor/max_value

  3. In some drivers, you can read them through ioctl / read / write api's to read/write sensor data.