How to Use Golang to Call D-Bus in openKylin System
1.1 Golang
Golang (Go) is a programming language developed by Google and first released in 2009. It is known for being efficient, fast, and easy to use, with strong concurrency handling capabilities. Go is well-suited for building distributed systems and large-scale network applications. The language uses static typing, making it type-safe.
1.2 D-Bus
D-Bus (Desktop Bus) is an Inter-Process Communication (IPC) system for desktop environment optimization. It allows communication between processes or between a process and the kernel. There are two main types of D-Bus: SystemBus and SessionBus.
SystemBus is used for communication between system-level processes (often requiring administrative privileges) and other processes. There is only one SystemBus for the entire system.
SessionBus is used for communication between regular user processes. There is a separate SessionBus for each user session.
In the openKylin system terminal, execute the command d-feet to see the D-Bus interface shown below. If it is not available, you can run the command ‘sudo apt update’, and then finish the installation by running ‘sudo apt install d-feet’. The interface displayed after executing the d-feet command is shown in Figure 1.
You can see that D-Bus is divided into SystemBus and SessionBus.
SystemBus is the path for transmitting data and control information within the computer. It connects the CPU, memory, I/O devices, and other important hardware components. The SystemBus typically consists of address lines, data lines, and control lines, which work together to coordinate data transmission and control operations inside the computer.
SessionBus is used for communication between desktop user applications. Below, we use the Python script shown in Figure 2 as an example. By executing ‘python3 register.py’ in the terminal, a SessionBus for calculating the sum of two numbers will be created.
Once the script is executed, search for register in the SessionBus input. As shown in Figure 3, you should see the SessionBus named come.test.register.
Once the preparations are complete, you can proceed with coding in Golang. As shown in Figure 4, run the command ‘go run -o invokedbus invokedbus.go’ in the Golang environment to generate a binary file named invokedbus.
By comparing the code with the created D-Bus information, you can see that the program's “com.test.register” is the name of the created D-Bus, which is also the path that the Golang code needs to call at runtime. By appending the count method to this path, the D-Bus call can be executed.
After generating the binary file invokedbus, execute it using the command ‘bash ./invokedbus’. You will see the interface shown in Figure 5. Follow the prompt to input the first and second numbers, and the program will output the final result of the calculation.
Through the above steps, we managed to call D-Bus from Golang and get the execution result. The combination of Golang and D-Bus is useful in many scenarios. For example, in system monitoring, D-Bus can be used to collect and monitor system information; for devices like printers and cameras, D-Bus provides a convenient way to control and manage them; in application integration, D-Bus allows applications to share functionality through signals and method calls.
If you have any further questions, please feel free to email us. Your support matters to us—check out our openKylin page on Distrowatch and learn more about what makes our system unique. Thank you for being part of the openKylin community!