PDA

View Full Version : Using Linunx "system" calls in C++ interactively



anneranch
May 27th, 2019, 04:38 PM
I am seeking help in figuring out how to use “system” calls from within C++ code.
My goal is to utilize Linux “command” bluetoothctl.
It is my understanding bluetoothctl is based on “bluez” package and it is now part of the Linux kernel.


I have successfully implemented simple system call to test retrieve “bluetoothctl help “.



system("bluetoothctl help | tee /tmp/A_test_file.txt >&2");



Works as expected.




I am having issues replicating the following output from “terminal”:






j
im@jim-desktop:~$ bluetoothctl

Agent registered
#






Here is my first attempt to run similar code from C++







cout << "bluetoothctl " << [B]endl; // just to see the prompt


system("bluetoothctl | tee /tmp/A_test_file.txt >&2" ); // expecting "Agent registered " and [bluetooth]# prompt on next line





The “output” from “"bluetoothctl"command is only


Agent registered


both in A_test_file.txt and stdout as expected , but no [bluetooth]# prompt






This call is different from the bluetoothctl help” - it actually receives response from the “bluetoothctl” and then a prompt.




I would guess the system call “return” needs to be more than the actual first line output.
Perhaps “file descriptor” ?
I do not know how to modify the system call to be more useful / interactive.

Any help would be appreciated.

oldos2er
May 27th, 2019, 04:43 PM
Moved to Programming Talk.

TheFu
May 28th, 2019, 01:41 PM
Part of the confusion is that a system call isn't the same as using the system() function. With access to C-bindings, I'd use the bluetooth libraries on the system directly, not call an external program from C/C++. If I just needed to ensure a daemon was running, then I'd script that outside my code, in a shell script.

For how to use the specific bluetoothctl command, I'd read the manpage for it. I purge all bluetooth stuff from my systems due to security considerations, so cannot try anything out. Sorry.