| #include <stdio.h> |
| #include <string.h> |
| #include <pthread.h> |
| #include <ctype.h> |
| #include <stdlib.h> |
| #include <errno.h> |
| #include <fcntl.h> |
| #include <gio/gio.h> |
| #include <log/log.h> |
| #include <glib.h> |
| #define GPIO_SERVICE "lynq.gpio" /*well-known bus name */ |
| #define GPIO_DATA_INTERFACE "lynq.gpio.Data" /*interface name*/ |
| #define GPIO_DATA_PATH "/lynq/gpio/data" /*object name*/ |
| GMainLoop *loop = NULL; |
| GDBusProxy *proxy = NULL; |
| #define LOG_TAG "GPIO_CONTROL" |
| int set_gpio (gchar * mode,guint *gpio_numb,guint param) |
| { |
| GDBusConnection *c1; |
| GVariant *result; |
| GError *error; |
| guint set_result = -1; |
| g_type_init(); |
| loop = g_main_loop_new(NULL, FALSE); /** create main loop, but do not start it.*/ |
| c1 = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL); |
| g_assert (c1 != NULL); |
| g_assert (!g_dbus_connection_is_closed (c1)); |
| result = g_dbus_connection_call_sync (c1, |
| GPIO_SERVICE, /* bus name */ |
| GPIO_DATA_PATH, /* object path */ |
| GPIO_DATA_INTERFACE, /* interface name */ |
| "setGpio", /* method name */ |
| g_variant_new ("(sii)", mode,gpio_numb,param), /* parameters */ |
| G_VARIANT_TYPE ("(i)"), /* return type */ |
| G_DBUS_CALL_FLAGS_NONE, |
| -1, |
| NULL, |
| &error); |
| g_assert_no_error (error); |
| g_assert (result != NULL); |
| g_variant_get(result, "((i))", &(set_result)); |
| g_variant_unref (result); |
| g_main_loop_run (loop); |
| g_main_loop_unref (loop); |
| return set_result; |
| } |
| int get_gpio (guint *gpio_numb) |
| { |
| GDBusConnection *c1; |
| GVariant *result; |
| GError *error; |
| guint parm=0; |
| const gchar *set_result; |
| const gchar *mode ="gpio"; |
| if(set_gpio(mode,gpio_numb,parm)== -1) |
| { |
| RLOGD("get_gpio number error"); |
| return -1; |
| } |
| c1 = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL); |
| g_assert (c1 != NULL); |
| g_assert (!g_dbus_connection_is_closed (c1)); |
| result = g_dbus_connection_call_sync (c1, |
| GPIO_SERVICE, /* bus name */ |
| GPIO_DATA_PATH, /* object path */ |
| GPIO_DATA_INTERFACE, /* interface name */ |
| "getGpio", /* method name */ |
| NULL, /* parameters */ |
| G_VARIANT_TYPE ("(s)"), /* return type */ |
| G_DBUS_CALL_FLAGS_NONE, |
| -1, |
| NULL, |
| &error); |
| g_assert_no_error (error); |
| g_assert (result != NULL); |
| g_variant_get(result, "((s))", &(set_result)); |
| RLOGD("get_gpio,%s",set_result); |
| g_variant_unref (result); |
| return 1; |
| } |
| void proxy_ready(GObject *source, GAsyncResult *result, gpointer user_data) { |
| GError *error; |
| |
| error = NULL; |
| proxy = g_dbus_proxy_new_for_bus_finish(result, &error); |
| if (proxy == NULL) { |
| RLOGE("create proxy fail"); |
| return ; |
| } |
| RLOGD("proxy is ready"); |
| gulong signal_handler_id; |
| |
| // signal_handler_id = g_signal_connect(proxy, "g-signal", |
| // G_CALLBACK (proxy_signals_on_signal), NULL); |
| // if (signal_handler_id == 0) { |
| // RLOGE("listen singal fail!"); |
| // } |
| } |
| |
| void* init_data_gdbus_cb(void *param) |
| { |
| /* all the tests rely on a shared main loop */ |
| loop = g_main_loop_new(NULL, FALSE); |
| |
| g_dbus_proxy_new_for_bus(G_BUS_TYPE_SYSTEM, |
| G_DBUS_PROXY_FLAGS_NONE, |
| NULL, /* GDBusInterfaceInfo */ |
| GPIO_SERVICE, /* name */ |
| GPIO_DATA_PATH, /* object path */ |
| GPIO_DATA_INTERFACE, /* interface */ |
| NULL, /* GCancellable */ |
| proxy_ready, |
| NULL); |
| |
| g_main_loop_run(loop); |
| |
| RLOGD("data gdbus main loop run()"); |
| if(proxy != NULL) { |
| g_object_unref (proxy); |
| } |
| if(loop != NULL) { |
| g_main_loop_unref(loop); |
| } |
| } |
| |
| |
| void startGdbusLoop(void) |
| { |
| pthread_t gpioThread; |
| |
| RLOGD("startGdbusLoop()"); |
| // pthread_mutex_lock(&s_startupMutex); |
| pthread_attr_t attr; |
| pthread_attr_init(&attr); |
| pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); |
| |
| int result = pthread_create(&gpioThread, &attr, init_data_gdbus_cb, NULL); |
| if (result != 0) { |
| RLOGW("Failed to create gdbus thread: %s", strerror(result)); |
| //goto done; |
| } |
| //done: |
| // pthread_mutex_unlock(&s_startupMutex); |
| } |