📄 appdemo.c
字号:
/**************************************************************Description: A simulated application that communicate with 3G$Log: **************************************************************/#include <glib.h>#include <dbus/dbus.h>#include <dbus/dbus-glib.h>static gboolean SendThenGet (DBusConnection *bus);static DBusHandlerResultsignal_filter (DBusConnection *connection, DBusMessage *message, void *user_data){ // User data is the event loop we are running in GMainLoop *loop = user_data; // A Ping signal on the com.burtonini.dbus.Signal interface if (dbus_message_is_signal (message, "com.compal.ThreeGtoApp.Signal", "Response")) { DBusError error; char *s; dbus_error_init (&error); if (dbus_message_get_args (message, &error, DBUS_TYPE_STRING, &s, DBUS_TYPE_INVALID)) { g_print("received: %s\n", s); } else { g_print("received, but error getting message: %s\n", error.message); dbus_error_free (&error); } return DBUS_HANDLER_RESULT_HANDLED; }}static gboolean SendThenGet (DBusConnection *bus){ DBusMessage *message; const char *command = "AT+CGMI\r"; /* Create a new signal "Command" on the "AppTo3G.dbus.Signal" interface, * from the object "/AppTo3G/dbus/command". */ message = dbus_message_new_signal ("/com/compal/3G/Send", "com.compal.AppTo3G.Signal", "Command"); // Append the arguments to the signal dbus_message_append_args (message, DBUS_TYPE_STRING, &command, DBUS_TYPE_INVALID); // Send the signal dbus_connection_send(bus, message, NULL); // Free the signal now we have finished with it dbus_message_unref(message); // Tell the user we send a signal g_print("%s command send!\n",command); // Return TRUE to tell the event loop we want to be called again g_timeout_add (2000, (GSourceFunc)SendThenGet, bus); return FALSE;}int main (int argc, char **argv){ GMainLoop *loop; DBusConnection *bus; DBusError error; /* Create a new event loop to run in */ loop = g_main_loop_new (NULL, FALSE); /* Get a connection to the session bus */ dbus_error_init(&error); bus = dbus_bus_get(DBUS_BUS_SESSION, &error); if (!bus) { g_warning("Failed to connect to the D-BUS daemon: %s", error.message); dbus_error_free(&error); return 1; } /* Set up this connection to work in a GLib event loop */ dbus_connection_setup_with_g_main (bus, NULL); /* listening to messages from all objects as no path is specified */ dbus_bus_add_match(bus, "type='signal',interface='com.compal.ThreeGtoApp.Signal'", NULL);// dbus_connection_add_filter(bus, signal_filter, loop, NULL); g_timeout_add(2000, (GSourceFunc)SendThenGet, bus); /* Start the event loop */ g_main_loop_run (loop); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -