最近开始对gdbus感兴趣,于是自己写了个demo,一直有个地方没有调通,希望各位大神能够指点迷津!
问题出现在server端,我在调用g_bus_own_name接口之后,注册的on_bus_acquired 回调函数没有被调到,不清楚卡在什么地方,是申请dbus服务失败了吗?这个回调函数能够被调用需要依赖什么?session-daemon吗?源码贴到下面,请各位大神指点!
server.c
#include <stdio.h>
#include <stdlib.h>
#include <glib.h>
#include <gio/gio.h>
#include "common.h"
#if DEBUG_SWITCH
#define debug(format,...)\
printf("\033[33m[FUNC %s]---[FILE %s]---[LINE %d]\033[0m: "format, __FUNCTION__, __FILE__, __LINE__, ##__VA_ARGS__)
#else
#define debug(format,...)
#endif
#define timeout 10
typedef struct
{
GMainLoop *mainloop;
GMainContext *context;
GSource *source;
DbusGlibDemo *demo_handle;
gint dbus_id;
}server_priv;
/*******************************************************************************
* Function Name : func_timeout
* Date : 2018/04/02
* Author : Spongebob
* Description : timeout for quit mainloop after 10 sec
* Return : result
*******************************************************************************/
gboolean func_timeout(gpointer data)
{
server_priv *priv = data;
static int count = 50;
debug ("timeout --- count = %d\n", count--);
if (!priv->demo_handle)
{
debug ("priv->demo_handle = %p\n", priv->demo_handle);
}
else
{
dbus_glib_demo_emit_test_status (priv->demo_handle, 0, 0);
}
if (!count)
{
g_main_loop_quit (priv->mainloop);
debug ("mainloop quit!\n");
}
}
/*******************************************************************************
* Function Name : on_bus_acquired
* Date : 2018/04/02
* Author : Spongebob
* Description : acquire bus for new skeleton
* Return : void
*******************************************************************************/
static void on_bus_acquired( GDBusConnection *connection, const gchar *name, gpointer user_data )
{
server_priv *priv = user_data;
DbusGlibDemo *demo_handle = NULL;
g_assert( demo_handle );
debug("on_bus_acquired %s!!\n", name );
demo_handle = dbus_glib_demo_skeleton_new();
debug("on_bus_acquired:file = %s, line = %d\n", __FILE__, __LINE__);
if( !g_dbus_interface_skeleton_export( G_DBUS_INTERFACE_SKELETON( demo_handle ), connection, BOB_DBUS_OBJECT, NULL ) ){
debug("g_dbus_interface_skeleton_export error\n" );
}
if (demo_handle)
{
priv->demo_handle= demo_handle;
}
return ;
}
/*******************************************************************************
* Function Name : on_bus_acquired
* Return : void
*******************************************************************************/
static void on_name_acquired( GDBusConnection *connection, const gchar *name, gpointer user_data )
{
debug ("on_name_acquired\n");
}
/*******************************************************************************
* Function Name : on_bus_acquired
* Return : void
*******************************************************************************/
static void on_name_lost( GDBusConnection *connection, const gchar *name, gpointer user_data )
{
debug ("on_name_lost\n");
}
/*******************************************************************************
* Function Name : main
* Date : 2018/04/02
* Author : Spongebob
* Description : server main
* Return : result
*******************************************************************************/
int main (int argv, char **argc)
{
debug ("Server Start!\n");
server_priv *priv = NULL;
/* initial */
priv = (server_priv*)malloc(sizeof(server_priv));
if (!priv){
debug ("malloc for server priv failed!\n");
return -1;
}
priv->context = g_main_context_new ();
priv->mainloop = g_main_loop_new (priv->context, TRUE);
priv->source = g_timeout_source_new (timeout);
/* set timeout callback */
// g_source_set_callback (priv->source, func_timeout, priv, NULL);
// g_source_attach (priv->source, priv->context);
priv->dbus_id = g_bus_own_name(G_BUS_TYPE_SESSION,
BOB_DBUS_NAME,
G_BUS_NAME_OWNER_FLAGS_NONE,
on_bus_acquired,
on_name_acquired,
on_name_lost,
priv, NULL);
debug ("priv->dbus_id = %d\n", priv->dbus_id);
/* start mainloop */
if (g_main_loop_is_running (priv->mainloop)){
debug ("Server mainloop start!\n");
g_main_loop_run(priv->mainloop);
}
g_main_loop_quit (priv->mainloop);
/* free */
g_main_loop_unref (priv->mainloop);
g_main_context_unref (priv->context);
g_bus_unown_name (priv->dbus_id);
free (priv);
priv = NULL;
return 0;
}
client.c
#include <stdio.h>
#include <stdlib.h>
#include <glib.h>
#include "common.h"
#if DEBUG_SWITCH
#define debug(format,...)\
printf("\033[33m[FUNC %s]---[FILE %s]---[LINE %d]\033[0m: "format, __FUNCTION__, __FILE__, __LINE__, ##__VA_ARGS__)
#else
#define debug(format,...)
#endif
typedef struct
{
GMainLoop *mainloop;
GMainContext *context;
DbusGlibDemo *proxy;
}client_priv;
/*******************************************************************************
* Function Name : test_callback
* Date : 2018/04/02
* Author : Spongebob
* Description : server callback
* Return : result
*******************************************************************************/
static gboolean test_callback ( const DbusGlibDemo *proxy, guint16 arg_notice_type,
guchar arg_notice_result, const gpointer user_data )
{
debug ("callback for client!\n");
client_priv *priv = user_data;
debug ("quit main loop!\n");
g_main_loop_quit (priv->mainloop);
return TRUE;
}
/*******************************************************************************
* Function Name : main
* Date : 2018/04/02
* Author : Spongebob
* Description : client main
* Return : result
*******************************************************************************/
int main (int argv, char **argc)
{
debug ("Client Start!\n");
client_priv *priv = NULL;
/* initial */
priv = (client_priv*)malloc(sizeof(client_priv));
if (!priv){
debug("malloc for client priv failed!\n");
return -1;
}
priv->context = g_main_context_new ();
priv->mainloop = g_main_loop_new (priv->context, TRUE);
/* acquire dbus proxy */
priv->proxy = dbus_glib_demo_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION,
G_DBUS_PROXY_FLAGS_NONE,
BOB_DBUS_NAME,
BOB_DBUS_OBJECT,
NULL, NULL );
debug ("acquire proxy:priv->proxy = %p\n", priv->proxy);
(void)g_signal_connect( priv->proxy, "test-status", G_CALLBACK( test_callback ), (gpointer)priv );
/* start mainloop */
if (g_main_loop_is_running (priv->mainloop)){
debug ("start client main loop!\n");
g_main_loop_run (priv->mainloop);
}
g_main_loop_quit (priv->mainloop);
/* free */
g_main_context_unref (priv->context);
g_main_loop_unref (priv->mainloop);
free (priv);
priv = NULL;
return 0;
}
xml
<node>
<interface name="dbus.glib.demo">
<method name="get_status">
<arg name="result" direction="out" type="y"/>
</method>
<signal name="test_status">
<arg name="type" type="q"/>
<arg name="result" type="y"/>
</signal>
</interface>
</node>
common.h
#ifndef _COMMON_H_
#define _COMMON_H_
#include "dbus-glib-demo.h"
#define DEBUG_SWITCH 1
/* dbus name */
#define BOB_DBUS_OBJECT "/dbus/glib/demo"
#define BOB_DBUS_NAME "dbus.glib.demo"
#endif