It’s been forever since I’ve used C so I apologize for the simple question.
I’m working with GPIO on a armeabi-v7a device. I need to export the GPIO interface into userspace from within an android app and I am trying to do so from a JNI library.
To do this from the terminal I would just run echo 199 > /sys/class/gpio/export
But I need to do it from JNI 🙁
My current attempt looks like this (with some error handling), but does not work:
JNIEXPORT jboolean JNICALL Java_com_rnd_touchpanel_LED_exportGreen
(JNIEnv * je, jclass jc)
{
freopen("/sys/class/gpio/export", "w+", stdout);
printf("198");
fclose(stdout);
return JNI_TRUE;
}
I then realized that export is actually binary not just a file, and I’ve forgotten how to spawn new processes and send them input and all that. Could someone refresh my memory? Thanks!
If you’re just writing a file, there’s no need to redirect
stdout— just open the file withfopen(3), write to it withfprintf(3), and close it withfclose(3).If you need to execute another process with given data on its standard, you have a few options:
system(3)to invoke the shell and give it your desired redirectionspopen(3)with type"w"andfprintfyour data to the returnedFILE*fork(2)andexec*(3)to spawn the new process. After forking but before exec’ing, usepipe(2),dup2(2)andclose(2)to setup the new process’s stdin appropriately. You can then write to that file descriptor from the parent process to setup the data. This is the most complicated option, but it gives you the most control over how the child process is setup.