Write value in mangOH /sys files

Hello !

I am currently trying to write a value in a /sys file of my mangOH Yellow.
i would like to be able to configure the accelerometer sampling frequency from my Legato application, thus I have to write into this file:

/sys/bus/i2c/devices/6-0068/iio:device0/in_accel_sampling_frequency

To change the sampling frequency from the console, you could do

echo 100 >  /sys/bus/i2c/devices/6-0068/iio:device0/in_accel_sampling_frequency

But I want to do this from my Legato application.

This is why I added this file in my .adef:

requires:
{
    file:
    {
        /sys/bus/i2c/devices/6-0068/iio:device0/in_accel_sampling_frequency    /driver/
    }
}

And in my source code, I can read into the file with:

#include "interfaces.h"
#include "legato.h"
#include <stdio.h>

COMPONENT_INIT
{
    FILE * fp;
    int accel_sampling;
    fp = fopen ("/driver/in_accel_sampling_frequency","r");
    fscanf(fp, "%d", &accel_sampling);
    LE_INFO("Accel sampling frequency : %d", accel_sampling);
    fclose(fp);
}

But now when I try to write into the same file with:

#include "interfaces.h"
#include "legato.h"
#include <stdio.h>
#include <errno.h>

COMPONENT_INIT
{
    FILE * fp;
    int rc;

    fp = fopen ("/driver/in_accel_sampling_frequency","rw");
    rc = fprintf(fp, "%d", 100);

    if (rc < 0)
    {
        LE_INFO("errno=%d, err_msg=\"%s\"\n", errno,strerror(errno));
        return;
    }
    fclose(fp);
}

When I run my app, I get this error:

errno=9, err_msg="Bad file descriptor

it looks like the file is read-only, but I can’t specify [rw] in the .cdef and changing the permissions of in_accel_sampling_frequency does not change the output…

Is it possible to do such thing ? And if it is, what am I doing wrong ?

Thanks for your help,

BR

Have you tried unsandboxed the application and see if your code is ok?

Did you try to open the file using fopen with "r+" instead of "rw" ?

Blockquote
Have you tried unsandboxed the application and see if your code is ok?

This results in the same error when trying to fprintf:

errno=9, err_msg="Bad file descriptor

Blockquote
Did you try to open the file using fopen with "r+" instead of "rw" ?

This results in another error when running fopen:

errno=13, err_msg="Permission denied

When using unsandboxed application, you need to specify the full path when fopen() .

Another way is to use the following for unsandboxed application:

system( “echo 100 > /sys/bus/i2c/devices/6-0068/iio:device0/in_accel_sampling_frequency”);

1 Like

Blockquote
When using unsandboxed application, you need to specify the full path when fopen() .

I did that, but i had the same error (Bad file descriptor).

But I tried (in unsandboxed mode) to use open instead of fopen and write instead of fprintf and it worked ! If anyone is interested, here is my code:

#include "interfaces.h"
#include "legato.h"
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>

COMPONENT_INIT
{
    int rc;
    int fp = open("/sys/bus/i2c/devices/6-0068/iio:device0/in_accel_sampling_frequency", O_RDWR);

    if (fp < 0)
    {
        LE_ERROR("Error openning the file (errno=%d, err_msg=\"%s\")\n", errno, strerror(errno));
        return LE_NOT_FOUND;
    }

    rc = write(fp, "100", strlen("100"));
    if (rc < 0)
    {
        LE_ERROR("Error writing accelerometer configuration (errno=%d, err_msg=\"%s\")\n", errno,strerror(errno));
        return LE_NOT_POSSIBLE;
    }
}

Keep in mind your app needs to be in unsandboxed mode for this to work !

Thanks for your help, have a good day !