sysfs qmi_wwan files writing in AOSP native code

Writing to sysfs qmi_wwan files in a native AOSP application could not be an intuitive process, since it involves touching different configuration files. Let’s describe the steps.

sysfs qmi_wwan

Getting the cdc-wdm device

The very first issue that can arise is the cdc-wdm device missing inside /dev: this usually depends on ueventd not considering drivers belonging to the usbmisc class, like qmi_wwan. The fix is to instruct init to take care of them, as described in article cdc-wdm support now added in AOSP Hikey. Obviously this should not be done if you are using Hikey, but for other boards, e.g the Dragonboard 845c, it’s mandatory.

sysfs qmi_wwan permissions change

The second step is to give adequate permissions to the desired sysfs files: also for this task the ueventd.rc file can be used, following the instructions available in the ueventd documentation.

Suppose we want to set mode 0660 and user/group system/system for the sysfs qmi_wwan raw_ip file, the following line can be used:

/sys/class/net/wwan0 qmi/raw_ip 0660 system system

replacing wwan0 with the actual network interface name in your system.

SELinux policy for writing the sysfs file

With SELinux enabled, still one piece is missing for being able to write our sysfs qmi_wwan file from a native application: granting the proper capabilities to the application. A simple line like the following can be used:

allow sysfs:file write;

e.g. for a native application having type raw_ip_enabler:

allow raw_ip_enabler sysfs:file write;

You should be now able to write the desired value to the sysfs qmi_wwan file. A similar process can be used for allowing the sysfs file to be read or for using different files (e.g. add_mux, del_mux for QMAP interfaces management).

For debugging possible SELinux issues, it is warmly suggested to look at AOSP SELinux documentation. I found especially useful the tool audit2allow, that analyzes the policies of the device against denials found in dmesg, in order to provide corresponding SELinux policy statements.