int main () { #if 1 DBusGConnection *bus; DBusGProxy *bus_proxy; GError *error = NULL; SomeObject *obj; GMainLoop *mainloop; guint request_name_result; g_type_init (); { GLogLevelFlags fatal_mask; fatal_mask = g_log_set_always_fatal (G_LOG_FATAL_MASK); fatal_mask |= G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL; g_log_set_always_fatal (fatal_mask); } dbus_g_object_type_install_info (SOME_TYPE_OBJECT, &dbus_glib_some_object_object_info); mainloop = g_main_loop_new (NULL, FALSE); bus = dbus_g_bus_get (DBUS_BUS_SESSION, &error); if (!bus) lose_gerror ("Couldn't connect to session bus", error); printf("Server started....\n"); bus_proxy = dbus_g_proxy_new_for_name (bus, "org.freedesktop.DBus", "/org/freedesktop/DBus", "org.freedesktop.DBus"); if (!dbus_g_proxy_call (bus_proxy, "RequestName", &error, G_TYPE_STRING, "org.designfu.SampleService", G_TYPE_UINT, 0, G_TYPE_INVALID, G_TYPE_UINT, &request_name_result, G_TYPE_INVALID)) lose_gerror ("Failed to acquire org.designfu.SampleService", error); obj = g_object_new (SOME_TYPE_OBJECT, NULL); dbus_g_connection_register_g_object (bus, "/SomeObject", G_OBJECT (obj)); printf ("service running\n"); g_main_loop_run (mainloop); exit (0); #else DBusConnection *connection; connection = dbus_bus_get (DBUS_BUS_SESSION, NULL); #endif return 0; }
static void start_avahi_service_browser(DBusGConnection *bus_system){ GError *error = NULL; char *service_browser_path; if (!dbus_g_proxy_call(proxy_avahi_service, "ServiceBrowserNew", &error, G_TYPE_INT, -1, G_TYPE_INT, -1, G_TYPE_STRING, "_http._tcp", G_TYPE_STRING, "local", G_TYPE_UINT, 0, G_TYPE_INVALID, DBUS_TYPE_G_OBJECT_PATH, &service_browser_path, G_TYPE_INVALID)) lose_gerror("Failed to call ServiceBrowserNew", error); printf("new service browser (%s) has started.\n", service_browser_path); proxy_avahi_service_browser = dbus_g_proxy_new_for_name(bus_system, "org.freedesktop.Avahi", service_browser_path, "org.freedesktop.Avahi.ServiceBrowser"); dbus_g_object_register_marshaller( _avahi_marshal_VOID__INT_INT_STRING_STRING_STRING_UINT, G_TYPE_NONE, G_TYPE_INT, G_TYPE_INT, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_UINT, G_TYPE_INVALID); // dbus_g_object_register_marshaller( // _avahi_marshal_VOID__INT_INT_STRING_STRING_STRING_INT_UINT, // G_TYPE_NONE, G_TYPE_INT, G_TYPE_INT, G_TYPE_STRING, G_TYPE_STRING, // G_TYPE_STRING, G_TYPE_INT, G_TYPE_UINT, G_TYPE_INVALID); dbus_g_proxy_add_signal(proxy_avahi_service_browser, "ItemNew", G_TYPE_INT, G_TYPE_INT, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_UINT, G_TYPE_INVALID); dbus_g_proxy_connect_signal(proxy_avahi_service_browser, "ItemNew", G_CALLBACK(service_browser_ItemNew), NULL, NULL); dbus_g_proxy_add_signal(proxy_avahi_service_browser, "ItemRemove", G_TYPE_INT, G_TYPE_INT, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_UINT, G_TYPE_INVALID); dbus_g_proxy_connect_signal(proxy_avahi_service_browser, "ItemRemove", G_CALLBACK(service_browser_ItemRemove), NULL, NULL); }
int main(int argc, char **argv) { DBusGConnection *bus_system; GError *error = NULL; GMainLoop *mainloop; g_type_init(); { GLogLevelFlags fatal_mask; fatal_mask = g_log_set_always_fatal(G_LOG_FATAL_MASK); fatal_mask |= G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL; g_log_set_always_fatal(fatal_mask); } mainloop = g_main_loop_new(NULL, FALSE); bus_system = dbus_g_bus_get(DBUS_BUS_SYSTEM, &error); //DBUS_BUS_SESSION if (!bus_system) lose_gerror("Couldn't connect to bus_system", error); proxy_avahi_service = dbus_g_proxy_new_for_name(bus_system, "org.freedesktop.Avahi", "/", "org.freedesktop.Avahi.Server"); start_avahi_service_browser(bus_system); start_avahi_entry_group(bus_system); g_main_loop_run(mainloop); g_object_unref(G_OBJECT(proxy_avahi_service)); g_object_unref(G_OBJECT(proxy_avahi_service_browser)); exit(0); }
static void start_avahi_entry_group(DBusGConnection *bus_system) { GError *error = NULL; char *entry_group_path; if (!dbus_g_proxy_call(proxy_avahi_service, "EntryGroupNew", &error, G_TYPE_INVALID, DBUS_TYPE_G_OBJECT_PATH, &entry_group_path, G_TYPE_INVALID)) lose_gerror("Failed to call EntryGroupNew", error); printf("new entry group (%s) has started.\n", entry_group_path); proxy_avahi_entry_group = dbus_g_proxy_new_for_name(bus_system, "org.freedesktop.Avahi", entry_group_path, "org.freedesktop.Avahi.EntryGroup"); entry_group_AddService(); /* dbus_g_proxy_add_signal(proxy_avahi_service_browser, "ItemNew", G_TYPE_INT, G_TYPE_INT, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_UINT, G_TYPE_INVALID); dbus_g_proxy_connect_signal(proxy_avahi_service_browser, "ItemNew", G_CALLBACK(service_browser_ItemNew), NULL, NULL); dbus_g_proxy_add_signal(proxy_avahi_service_browser, "ItemRemove", G_TYPE_INT, G_TYPE_INT, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_UINT, G_TYPE_INVALID); dbus_g_proxy_connect_signal(proxy_avahi_service_browser, "ItemRemove", G_CALLBACK(service_browser_ItemRemove), NULL, NULL); */ }
static void entry_group_AddService(){ GError *error = NULL; dbus_int32_t interface = -1; dbus_int32_t protocol = -1; dbus_uint32_t flags = 0; char *name = "xifei"; char *type = "_http._tcp"; char *domain = "local"; char *host = "192.168.160.3"; dbus_uint16_t port = 500; char *txt1 = "xifei"; char *txt2 = "password of xifei"; GPtrArray *txt = g_ptr_array_new(); GArray *garr; garr = g_array_new(FALSE, FALSE, sizeof(char)); garr->data = txt1; garr->len = strlen(txt1); g_ptr_array_add(txt, garr); garr = g_array_new(FALSE, FALSE, sizeof(char)); garr->data = txt2; garr->len = strlen(txt2); g_ptr_array_add(txt, garr); // initialiset the errors printf("Calling remote method: %s\n", "org.freedesktop.Avahi.EntryGroup"); if (!dbus_g_proxy_call(proxy_avahi_entry_group, "AddService", &error, G_TYPE_INT, interface, G_TYPE_INT, protocol, G_TYPE_UINT, flags, G_TYPE_STRING, name, G_TYPE_STRING, type, G_TYPE_STRING, domain, G_TYPE_STRING, host, G_VARIANT_CLASS_UINT16, port,//G_TYPE_UCAHRDBUS_TYPE_UINT16 DBUS_TYPE_G_UCHAR_ARRAY_ARRAY, txt, G_TYPE_INVALID, G_TYPE_INVALID)) lose_gerror("Failed to call ServiceBrowserNew", error); if (!dbus_g_proxy_call(proxy_avahi_entry_group, "Commit", &error, G_TYPE_INVALID, G_TYPE_INVALID)) lose_gerror("Failed to call ServiceBrowserNew", error); }
void gaim_init(void) { GError *error = NULL; g_type_init (); bus = dbus_g_bus_get (DBUS_BUS_SESSION, &error); if (!bus) lose_gerror ("Couldn't connect to session bus", error); gaim_proxy = dbus_g_proxy_new_for_name (bus, DBUS_SERVICE_GAIM, DBUS_PATH_GAIM, DBUS_INTERFACE_GAIM); if (!gaim_proxy) lose_gerror ("Couldn't connect to the Gaim Service", error); }
int main (int argc, char **argv) { DBusGConnection *bus; DBusGProxy *bus_proxy; GError *error = NULL; char *obj; GMainLoop *mainloop; guint request_name_result; GIOChannel *chan; #ifdef SMSS_DEBUG g_slice_set_config(G_SLICE_CONFIG_ALWAYS_MALLOC, TRUE); #endif g_type_init (); //dbus_g_object_type_install_info (GSM_SMS_TYPE, & dbus_glib_gsm_sms_object_info); mainloop = g_main_loop_new (NULL, FALSE); bus = dbus_g_bus_get (DBUS_BUS_SESSION, &error); if (!bus) lose_gerror ("Couldn't connect to system bus", error); bus_proxy = dbus_g_proxy_new_for_name (bus, "org.freedesktop.DBus", "/", "org.freedesktop.DBus"); if (!dbus_g_proxy_call (bus_proxy, "RequestName", &error, G_TYPE_STRING, "org.freesmartphone.ogsmd", G_TYPE_UINT, 0, G_TYPE_INVALID, G_TYPE_UINT, &request_name_result, G_TYPE_INVALID)) lose_gerror ("Failed to acquire org.freesmartphone.ogsmd", error); obj = g_object_new (G_TYPE_STRING, NULL); dbus_g_connection_register_g_object (bus, "/org/freesmartphone/GSM/Device", G_OBJECT (obj)); printf ("service is running/n"); chan = g_io_channel_unix_new(0); g_io_add_watch(chan, G_IO_IN, channel_cb, obj); g_main_loop_run (mainloop); exit (0); }
static void service_browser_ItemNew(DBusGProxy *proxy, int interface, int protocol, char *name, char *stype, char *domain, unsigned int flags, gpointer user_data) { GError *error = NULL; //printf ("discovered:%d, %d, %s, %s, %s, %d.\n", interface, protocol, name, stype, domain, flags); char *host, *address; dbus_int32_t aprotocol = -1; dbus_uint32_t m_flags = 0; dbus_uint16_t port; GPtrArray *byte_arraies; /* dbus_int32_t minterface = 2; dbus_int32_t mprotocol = 1; char *mname = "TestService"; char *mstype = "_http._tcp"; char *mdomain = "local"; dbus_int32_t maprotocol = -1; dbus_uint32_t mflags = 0; */ printf("\n"); printf("Parameter for ResolvesService:%d, %d, %s, %s, %s, %d, %d.\n", interface, protocol, name, stype, domain, aprotocol, m_flags); if (!dbus_g_proxy_call(proxy_avahi_service, "ResolveService", &error, G_TYPE_INT, interface, G_TYPE_INT, protocol, G_TYPE_STRING, name, G_TYPE_STRING, stype, G_TYPE_STRING, domain, G_TYPE_INT, aprotocol, G_TYPE_UINT, m_flags, G_TYPE_INVALID, G_TYPE_INT, &interface, G_TYPE_INT, &protocol, G_TYPE_STRING, &name, G_TYPE_STRING, &stype, G_TYPE_STRING, &domain, G_TYPE_STRING, &host, G_TYPE_INT, &aprotocol, G_TYPE_STRING, &address, G_TYPE_UINT, &port, DBUS_TYPE_G_UCHAR_ARRAY_ARRAY, &byte_arraies, G_TYPE_UINT, &flags, G_TYPE_INVALID)) { lose_gerror("Failed to call ServiceBrowserNew", error); } printf( "Results of ResolveService:%d, %d, %s, %s, %s, %s, %d, %s, %d, %d.\n", interface, protocol, name, stype, domain, host, aprotocol, address, port, flags); if (byte_arraies->len == 0) { printf("value of txt: %s\n", "is null."); } else { for (int i = 0; i < byte_arraies->len; i++) { GArray *arr = g_ptr_array_index(byte_arraies, i); char *txt = malloc(sizeof(char) * (arr->len + 1)); strncpy(txt, arr->data, arr->len);txt[arr->len] = '\0'; //arr = g_ptr_array_index(byte_arraies, 0); printf("value of txt: %d, %s\n", arr->len, txt); } } }
int main (int argc, char **argv) { DBusGConnection *bus; DBusGProxy *remote_object; DBusGProxy *remote_object_introspectable; GError *error = NULL; char *introspect_data; guint i; gint sum; //yaomoon GMainLoop *mainloop; mainloop = g_main_loop_new (NULL, FALSE); //****** g_type_init (); { GLogLevelFlags fatal_mask; fatal_mask = g_log_set_always_fatal (G_LOG_FATAL_MASK); fatal_mask |= G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL; g_log_set_always_fatal (fatal_mask); } bus = dbus_g_bus_get (DBUS_BUS_SESSION, &error); if (!bus) lose_gerror ("Couldn't connect to session bus", error); remote_object = dbus_g_proxy_new_for_name (bus, "org.fmddlmyy.Test", "/TestObj", "org.fmddlmyy.Test.Basic"); if (!dbus_g_proxy_call (remote_object, "Add", &error, G_TYPE_INT, 100, G_TYPE_INT, 999, G_TYPE_INVALID, G_TYPE_INT, &sum, G_TYPE_INVALID)) lose_gerror ("Failed to call Add", error); printf("sum is %d\n", sum); //yaomoon g_signal_connect (remote_object, "position-change", G_CALLBACK (position), NULL); //**************** remote_object_introspectable = dbus_g_proxy_new_for_name (bus, "org.fmddlmyy.Test", "/TestObj", "org.freedesktop.DBus.Introspectable"); if (!dbus_g_proxy_call (remote_object_introspectable, "Introspect", &error, G_TYPE_INVALID, G_TYPE_STRING, &introspect_data, G_TYPE_INVALID)) lose_gerror ("Failed to complete Introspect", error); printf ("%s", introspect_data); g_free (introspect_data); //yaomoon g_main_loop_run (mainloop); //********* g_object_unref (G_OBJECT (remote_object_introspectable)); g_object_unref (G_OBJECT (remote_object)); exit(0); }
int main (int argc, char **argv) { DBusGConnection *bus; DBusGProxy *remote_object; DBusGProxy *remote_object_introspectable; GError *error = NULL; char **reply_list; char **reply_ptr; GValueArray *hello_reply_struct; GHashTable *hello_reply_dict; char *introspect_data; guint i; g_type_init (); { GLogLevelFlags fatal_mask; fatal_mask = g_log_set_always_fatal (G_LOG_FATAL_MASK); fatal_mask |= G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL; g_log_set_always_fatal (fatal_mask); } bus = dbus_g_bus_get (DBUS_BUS_SESSION, &error); if (!bus) lose_gerror ("Couldn't connect to session bus", error); remote_object = dbus_g_proxy_new_for_name (bus, "org.designfu.SampleService", "/SomeObject", "org.designfu.SampleInterface"); if (!dbus_g_proxy_call (remote_object, "HelloWorld", &error, G_TYPE_STRING, "Hello from example-client.c!", G_TYPE_INVALID, G_TYPE_STRV, &reply_list, G_TYPE_INVALID)) lose_gerror ("Failed to complete HelloWorld", error); if (!dbus_g_proxy_call (remote_object, "GetTuple", &error, G_TYPE_INVALID, G_TYPE_VALUE_ARRAY, &hello_reply_struct, G_TYPE_INVALID)) lose_gerror ("Failed to complete GetTuple", error); if (!dbus_g_proxy_call (remote_object, "GetDict", &error, G_TYPE_INVALID, DBUS_TYPE_G_STRING_STRING_HASHTABLE, &hello_reply_dict, G_TYPE_INVALID)) lose_gerror ("Failed to complete GetDict", error); printf ("reply_list: "); for (reply_ptr = reply_list; *reply_ptr; reply_ptr++) printf ("\"%s\" ", *reply_ptr); printf ("\n"); g_strfreev (reply_list); for (i = 0; i < hello_reply_struct->n_values; i++) { GValue strval = { 0, }; g_value_init (&strval, G_TYPE_STRING); if (!g_value_transform (g_value_array_get_nth (hello_reply_struct, i), &strval)) g_value_set_static_string (&strval, "(couldn't transform to string)"); g_print ("%s: %s\n", g_type_name (G_VALUE_TYPE (g_value_array_get_nth (hello_reply_struct, i))), g_value_get_string (&strval)); } g_value_array_free (hello_reply_struct); printf ("\n"); g_hash_table_foreach (hello_reply_dict, print_hash_value, NULL); g_hash_table_destroy (hello_reply_dict); remote_object_introspectable = dbus_g_proxy_new_for_name (bus, "org.designfu.SampleService", "/SomeObject", "org.freedesktop.DBus.Introspectable"); if (!dbus_g_proxy_call (remote_object_introspectable, "Introspect", &error, G_TYPE_INVALID, G_TYPE_STRING, &introspect_data, G_TYPE_INVALID)) lose_gerror ("Failed to complete Introspect", error); printf ("%s", introspect_data); g_free (introspect_data); g_object_unref (G_OBJECT (remote_object_introspectable)); g_object_unref (G_OBJECT (remote_object)); exit(0); }
int main () { DBusGConnection *bus; DBusGProxy *remote_object; GError *error = NULL; GMainLoop *mainloop; FILE *fp; g_type_init (); fp = popen("z:\\sys\\bin\\example-signal-emitter.exe 1","r"); sleep(30); fp = popen("z:\\sys\\bin\\example-signal-emitter.exe 2","r"); sleep(10); mainloop = g_main_loop_new (NULL, FALSE); bus = dbus_g_bus_get (DBUS_BUS_SESSION, &error); if (!bus) lose_gerror ("Couldn't connect to session bus", error); /* We use _for_name_owner in order to track this particular service * instance, which lets us receive signals. */ remote_object = dbus_g_proxy_new_for_name (bus, "org.designfu.TestService", "/org/designfu/TestService/object", "org.designfu.TestService"); if (!remote_object) lose_gerror ("Failed to get name owner", error); /* IMPORTANT: * * Note because this signal's signature is VOID__STRING, we do not * need to register a marshaller, since there is a builtin one. * However for other signatures, you must generate a marshaller, * then call dbus_g_object_register_marshaller. It would look like * this: * * dbus_g_object_register_marshaller (g_cclosure_marshal_VOID__STRING, G_TYPE_NONE, G_TYPE_STRING, G_TYPE_INVALID); * */ /* Tell DBus what the type signature of the signal callback is; this * allows us to sanity-check incoming messages before invoking the * callback. You need to do this once for each proxy you create, * not every time you want to connect to the signal. */ dbus_g_proxy_add_signal (remote_object, "HelloSignal", G_TYPE_STRING, G_TYPE_INVALID); /* Actually connect to the signal. Note you can call * dbus_g_proxy_connect_signal multiple times for one invocation of * dbus_g_proxy_add_signal. */ dbus_g_proxy_connect_signal (remote_object, "HelloSignal", G_CALLBACK (hello_signal_handler), NULL, NULL); g_timeout_add (2000, emit_signal, remote_object); g_main_loop_run (mainloop); exit (0); }