| you.chen | 5ef374a | 2023-12-26 17:25:16 +0800 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <string.h> |
| 3 | #include <pthread.h> |
| 4 | #include <ctype.h> |
| 5 | #include <stdlib.h> |
| 6 | #include <errno.h> |
| 7 | #include <fcntl.h> |
| 8 | #include <gio/gio.h> |
| 9 | #include <log/log.h> |
| 10 | #include <glib.h> |
| 11 | #define GPIO_SERVICE "lynq.gpio" /*well-known bus name */ |
| 12 | #define GPIO_DATA_INTERFACE "lynq.gpio.Data" /*interface name*/ |
| 13 | #define GPIO_DATA_PATH "/lynq/gpio/data" /*object name*/ |
| 14 | GMainLoop *loop = NULL; |
| 15 | GDBusProxy *proxy = NULL; |
| 16 | #define LOG_TAG "GPIO_CONTROL" |
| 17 | int set_gpio (gchar * mode,guint *gpio_numb,guint param) |
| 18 | { |
| 19 | GDBusConnection *c1; |
| 20 | GVariant *result; |
| 21 | GError *error; |
| 22 | guint set_result = -1; |
| 23 | g_type_init(); |
| 24 | loop = g_main_loop_new(NULL, FALSE); /** create main loop, but do not start it.*/ |
| 25 | c1 = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL); |
| 26 | g_assert (c1 != NULL); |
| 27 | g_assert (!g_dbus_connection_is_closed (c1)); |
| 28 | result = g_dbus_connection_call_sync (c1, |
| 29 | GPIO_SERVICE, /* bus name */ |
| 30 | GPIO_DATA_PATH, /* object path */ |
| 31 | GPIO_DATA_INTERFACE, /* interface name */ |
| 32 | "setGpio", /* method name */ |
| 33 | g_variant_new ("(sii)", mode,gpio_numb,param), /* parameters */ |
| 34 | G_VARIANT_TYPE ("(i)"), /* return type */ |
| 35 | G_DBUS_CALL_FLAGS_NONE, |
| 36 | -1, |
| 37 | NULL, |
| 38 | &error); |
| 39 | g_assert_no_error (error); |
| 40 | g_assert (result != NULL); |
| 41 | g_variant_get(result, "((i))", &(set_result)); |
| 42 | g_variant_unref (result); |
| 43 | g_main_loop_run (loop); |
| 44 | g_main_loop_unref (loop); |
| 45 | return set_result; |
| 46 | } |
| 47 | int get_gpio (guint *gpio_numb) |
| 48 | { |
| 49 | GDBusConnection *c1; |
| 50 | GVariant *result; |
| 51 | GError *error; |
| 52 | guint parm=0; |
| 53 | const gchar *set_result; |
| 54 | const gchar *mode ="gpio"; |
| 55 | if(set_gpio(mode,gpio_numb,parm)== -1) |
| 56 | { |
| 57 | RLOGD("get_gpio number error"); |
| 58 | return -1; |
| 59 | } |
| 60 | c1 = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL); |
| 61 | g_assert (c1 != NULL); |
| 62 | g_assert (!g_dbus_connection_is_closed (c1)); |
| 63 | result = g_dbus_connection_call_sync (c1, |
| 64 | GPIO_SERVICE, /* bus name */ |
| 65 | GPIO_DATA_PATH, /* object path */ |
| 66 | GPIO_DATA_INTERFACE, /* interface name */ |
| 67 | "getGpio", /* method name */ |
| 68 | NULL, /* parameters */ |
| 69 | G_VARIANT_TYPE ("(s)"), /* return type */ |
| 70 | G_DBUS_CALL_FLAGS_NONE, |
| 71 | -1, |
| 72 | NULL, |
| 73 | &error); |
| 74 | g_assert_no_error (error); |
| 75 | g_assert (result != NULL); |
| 76 | g_variant_get(result, "((s))", &(set_result)); |
| 77 | RLOGD("get_gpio,%s",set_result); |
| 78 | g_variant_unref (result); |
| 79 | return 1; |
| 80 | } |
| 81 | void proxy_ready(GObject *source, GAsyncResult *result, gpointer user_data) { |
| 82 | GError *error; |
| 83 | |
| 84 | error = NULL; |
| 85 | proxy = g_dbus_proxy_new_for_bus_finish(result, &error); |
| 86 | if (proxy == NULL) { |
| 87 | RLOGE("create proxy fail"); |
| 88 | return ; |
| 89 | } |
| 90 | RLOGD("proxy is ready"); |
| 91 | gulong signal_handler_id; |
| 92 | |
| 93 | // signal_handler_id = g_signal_connect(proxy, "g-signal", |
| 94 | // G_CALLBACK (proxy_signals_on_signal), NULL); |
| 95 | // if (signal_handler_id == 0) { |
| 96 | // RLOGE("listen singal fail!"); |
| 97 | // } |
| 98 | } |
| 99 | |
| 100 | void* init_data_gdbus_cb(void *param) |
| 101 | { |
| 102 | /* all the tests rely on a shared main loop */ |
| 103 | loop = g_main_loop_new(NULL, FALSE); |
| 104 | |
| 105 | g_dbus_proxy_new_for_bus(G_BUS_TYPE_SYSTEM, |
| 106 | G_DBUS_PROXY_FLAGS_NONE, |
| 107 | NULL, /* GDBusInterfaceInfo */ |
| 108 | GPIO_SERVICE, /* name */ |
| 109 | GPIO_DATA_PATH, /* object path */ |
| 110 | GPIO_DATA_INTERFACE, /* interface */ |
| 111 | NULL, /* GCancellable */ |
| 112 | proxy_ready, |
| 113 | NULL); |
| 114 | |
| 115 | g_main_loop_run(loop); |
| 116 | |
| 117 | RLOGD("data gdbus main loop run()"); |
| 118 | if(proxy != NULL) { |
| 119 | g_object_unref (proxy); |
| 120 | } |
| 121 | if(loop != NULL) { |
| 122 | g_main_loop_unref(loop); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | |
| 127 | void startGdbusLoop(void) |
| 128 | { |
| 129 | pthread_t gpioThread; |
| 130 | |
| 131 | RLOGD("startGdbusLoop()"); |
| 132 | // pthread_mutex_lock(&s_startupMutex); |
| 133 | pthread_attr_t attr; |
| 134 | pthread_attr_init(&attr); |
| 135 | pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); |
| 136 | |
| 137 | int result = pthread_create(&gpioThread, &attr, init_data_gdbus_cb, NULL); |
| 138 | if (result != 0) { |
| 139 | RLOGW("Failed to create gdbus thread: %s", strerror(result)); |
| 140 | //goto done; |
| 141 | } |
| 142 | //done: |
| 143 | // pthread_mutex_unlock(&s_startupMutex); |
| 144 | } |