Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 1 | #include "interfaces/sensor.h"
|
| 2 |
|
| 3 | /* ---------------------------------------------------------------------------------------------------- */
|
| 4 |
|
| 5 | static GDBusObjectManagerServer *manager = NULL;
|
| 6 | static SensorInteger *sensor = NULL;
|
| 7 |
|
| 8 | static guint i2c_bus = 1;
|
| 9 |
|
| 10 | static gboolean
|
| 11 | on_get_units (SensorInteger *sen,
|
| 12 | GDBusMethodInvocation *invocation,
|
| 13 | gpointer user_data)
|
| 14 | {
|
| 15 | const gchar* val = sensor_integer_get_units(sen);
|
| 16 | sensor_integer_complete_get_units(sen,invocation,val);
|
| 17 | return TRUE;
|
| 18 | }
|
| 19 |
|
| 20 | static gboolean
|
| 21 | on_get (SensorInteger *sen,
|
| 22 | GDBusMethodInvocation *invocation,
|
| 23 | gpointer user_data)
|
| 24 | {
|
| 25 | guint reading = sensor_integer_get_value(sen);
|
| 26 | sensor_integer_complete_get_value(sen,invocation,reading);
|
| 27 | return TRUE;
|
| 28 | }
|
| 29 |
|
| 30 | static void
|
| 31 | on_bus_acquired (GDBusConnection *connection,
|
| 32 | const gchar *name,
|
| 33 | gpointer user_data)
|
| 34 | {
|
| 35 | ObjectSkeleton *object;
|
| 36 | guint n;
|
| 37 |
|
| 38 | g_print ("Acquired a message bus connection: %s\n",name);
|
| 39 |
|
| 40 | manager = g_dbus_object_manager_server_new ("/org/openbmc/Sensors/Temperature/Ambient");
|
| 41 |
|
| 42 | gchar *s;
|
| 43 | s = g_strdup_printf ("/org/openbmc/Sensors/Temperature/Ambient/0");
|
| 44 | object = object_skeleton_new (s);
|
| 45 | g_free (s);
|
| 46 |
|
| 47 | sensor = sensor_integer_skeleton_new ();
|
| 48 | object_skeleton_set_sensor_integer (object, sensor);
|
| 49 | g_object_unref (sensor);
|
| 50 |
|
| 51 | sensor_integer_set_units(sensor,"C");
|
| 52 | //define method callbacks here
|
| 53 | g_signal_connect (sensor,
|
| 54 | "handle-get-value",
|
| 55 | G_CALLBACK (on_get),
|
| 56 | NULL); /* user_data */
|
| 57 | g_signal_connect (sensor,
|
| 58 | "handle-get-units",
|
| 59 | G_CALLBACK (on_get_units),
|
| 60 | NULL); /* user_data */
|
| 61 |
|
| 62 |
|
| 63 | /* Export the object (@manager takes its own reference to @object) */
|
| 64 | g_dbus_object_manager_server_export (manager, G_DBUS_OBJECT_SKELETON (object));
|
| 65 | g_object_unref (object);
|
| 66 |
|
| 67 | /* Export all objects */
|
| 68 | g_dbus_object_manager_server_set_connection (manager, connection);
|
| 69 | }
|
| 70 |
|
| 71 | static void
|
| 72 | on_name_acquired (GDBusConnection *connection,
|
| 73 | const gchar *name,
|
| 74 | gpointer user_data)
|
| 75 | {
|
| 76 | g_print ("Acquired the name %s\n", name);
|
| 77 | }
|
| 78 |
|
| 79 | static void
|
| 80 | on_name_lost (GDBusConnection *connection,
|
| 81 | const gchar *name,
|
| 82 | gpointer user_data)
|
| 83 | {
|
| 84 | g_print ("Lost the name %s\n", name);
|
| 85 | }
|
| 86 |
|
| 87 | static gboolean
|
| 88 | poll_sensor()
|
| 89 | {
|
| 90 | guint value = sensor_integer_get_value(sensor);
|
| 91 | //TOOD: Change to actually read sensor
|
| 92 |
|
| 93 | g_print("Polling sensor: %d\n",value);
|
| 94 |
|
| 95 | //if changed, set property and emit signal
|
| 96 | if (value != sensor_integer_get_value(sensor))
|
| 97 | {
|
| 98 | sensor_integer_set_value(sensor,value);
|
| 99 | sensor_integer_emit_changed(sensor,value);
|
| 100 | }
|
| 101 | return TRUE;
|
| 102 | }
|
| 103 |
|
| 104 | gint
|
| 105 | main (gint argc, gchar *argv[])
|
| 106 | {
|
| 107 | GMainLoop *loop;
|
| 108 |
|
| 109 | guint id;
|
| 110 | //g_type_init ();
|
| 111 | loop = g_main_loop_new (NULL, FALSE);
|
| 112 |
|
| 113 | id = g_bus_own_name (G_BUS_TYPE_SESSION,
|
| 114 | "org.openbmc.Sensors.Temperature.Ambient",
|
| 115 | G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT |
|
| 116 | G_BUS_NAME_OWNER_FLAGS_REPLACE,
|
| 117 | on_bus_acquired,
|
| 118 | on_name_acquired,
|
| 119 | on_name_lost,
|
| 120 | loop,
|
| 121 | NULL);
|
| 122 |
|
| 123 | g_timeout_add(5000, poll_sensor, NULL);
|
| 124 | g_main_loop_run (loop);
|
| 125 |
|
| 126 | g_bus_unown_name (id);
|
| 127 | g_main_loop_unref (loop);
|
| 128 | return 0;
|
| 129 | }
|