# Write value in mangOH /sys files

**URL:** <https://mangoh.discourse.group/t/write-value-in-mangoh-sys-files/4717>\
**Category:** mangOH Yellow\
**Created:** [June 25, 2020, 10:13am UTC](https://mangoh.discourse.group/t/write-value-in-mangoh-sys-files/4717 "2020-06-25T10:13:05Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![paulmeis](https://avatars.discourse-cdn.com/v4/letter/p/c57346/32.png) [@paulmeis](https://mangoh.discourse.group/u/paulmeis)\
**Post date:** [June 25, 2020, 10:13am UTC](https://mangoh.discourse.group/t/write-value-in-mangoh-sys-files/4717/1 "2020-06-25T10:13:05Z")

</div>

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:

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

```

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

```bash
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:

```auto
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:

```auto
#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:

```auto
#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:

```auto
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

---

<div class="post-metadata">

**Author:** ![jyijyi](https://sea2.discourse-cdn.com/flex016/user_avatar/mangoh.discourse.group/jyijyi/32/997_2.png) [@jyijyi](https://mangoh.discourse.group/u/jyijyi)\
**Post date:** [June 25, 2020, 11:57am UTC](https://mangoh.discourse.group/t/write-value-in-mangoh-sys-files/4717/2 "2020-06-25T11:57:54Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Jay](https://sea2.discourse-cdn.com/flex016/user_avatar/mangoh.discourse.group/jay/32/46_2.png) [@Jay](https://mangoh.discourse.group/u/Jay)\
**Post date:** [June 25, 2020, 12:27pm UTC](https://mangoh.discourse.group/t/write-value-in-mangoh-sys-files/4717/3 "2020-06-25T12:27:00Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![paulmeis](https://avatars.discourse-cdn.com/v4/letter/p/c57346/32.png) [@paulmeis](https://mangoh.discourse.group/u/paulmeis)\
**Post date:** [June 25, 2020, 1:18pm UTC](https://mangoh.discourse.group/t/write-value-in-mangoh-sys-files/4717/4 "2020-06-25T13:18:37Z")

</div>

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

This results in the same error when trying to `fprintf`:

```auto
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`:

```auto
errno=13, err_msg="Permission denied

```

---

<div class="post-metadata">

**Author:** ![jyijyi](https://sea2.discourse-cdn.com/flex016/user_avatar/mangoh.discourse.group/jyijyi/32/997_2.png) [@jyijyi](https://mangoh.discourse.group/u/jyijyi)\
**Post date:** [June 25, 2020, 3:07pm UTC](https://mangoh.discourse.group/t/write-value-in-mangoh-sys-files/4717/5 "2020-06-25T15:07:08Z")

</div>

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”);

---

<div class="post-metadata">

**Author:** ![paulmeis](https://avatars.discourse-cdn.com/v4/letter/p/c57346/32.png) [@paulmeis](https://mangoh.discourse.group/u/paulmeis)\
**Post date:** [June 26, 2020, 10:32am UTC](https://mangoh.discourse.group/t/write-value-in-mangoh-sys-files/4717/6 "2020-06-26T10:32:07Z")

</div>

> 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:

```auto
#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 !
