blob: a08522077ea95b7f1713948800ef2d88eaf3e3c2 [file] [log] [blame]
Norman James90baede2015-09-02 20:32:49 -05001#include "interfaces/sensor.h"
Norman James10ff6a32015-08-27 14:24:17 -05002#include "openbmc.h"
3
Norman Jamese2765102015-08-19 22:00:55 -05004
5/* ---------------------------------------------------------------------------------------------------- */
Norman James10ff6a32015-08-27 14:24:17 -05006
Norman James26072c02015-08-25 07:14:29 -05007static const gchar* dbus_object_path = "/org/openbmc/sensors/HostStatus";
8static const gchar* dbus_name = "org.openbmc.sensors.HostStatus";
Norman James10ff6a32015-08-27 14:24:17 -05009static const guint poll_interval = 3000;
10static guint heartbeat = 0;
Norman Jamese2765102015-08-19 22:00:55 -050011
12static GDBusObjectManagerServer *manager = NULL;
Norman James5d78b4d2015-09-05 13:34:34 -050013/*
Norman Jamese2765102015-08-19 22:00:55 -050014static gboolean
Norman James5d78b4d2015-09-05 13:34:34 -050015on_get_units (SensorValue *sen,
Norman Jamese2765102015-08-19 22:00:55 -050016 GDBusMethodInvocation *invocation,
17 gpointer user_data)
18{
Norman James5d78b4d2015-09-05 13:34:34 -050019 const gchar* val = sensor_value_get_units(sen);
20 sensor_value_complete_get_units(sen,invocation,val);
Norman Jamese2765102015-08-19 22:00:55 -050021 return TRUE;
22}
23
24static gboolean
Norman James5d78b4d2015-09-05 13:34:34 -050025on_get (SensorValue *sen,
Norman Jamese2765102015-08-19 22:00:55 -050026 GDBusMethodInvocation *invocation,
27 gpointer user_data)
28{
Norman James5d78b4d2015-09-05 13:34:34 -050029 guint reading = sensor_value_get_value(sen);
30 sensor_value_complete_get_value(sen,invocation,reading);
Norman Jamese2765102015-08-19 22:00:55 -050031 return TRUE;
32}
33static gboolean
Norman James5d78b4d2015-09-05 13:34:34 -050034on_set (SensorValue *sen,
Norman Jamese2765102015-08-19 22:00:55 -050035 GDBusMethodInvocation *invocation,
Norman James90baede2015-09-02 20:32:49 -050036 guint value,
Norman Jamese2765102015-08-19 22:00:55 -050037 gpointer user_data)
38{
Norman James5d78b4d2015-09-05 13:34:34 -050039 GVariant* v = NEW_VARIANT_U(value);
40 sensor_value_set_value(sen,v);
41 sensor_value_emit_changed(sen,v,sensor_value_get_units(sen));
42 sensor_value_complete_set_value(sen,invocation);
Norman James90baede2015-09-02 20:32:49 -050043 return TRUE;
Norman Jamese2765102015-08-19 22:00:55 -050044}
Norman James5d78b4d2015-09-05 13:34:34 -050045*/
Norman Jamese2765102015-08-19 22:00:55 -050046static void
47on_bus_acquired (GDBusConnection *connection,
48 const gchar *name,
49 gpointer user_data)
50{
Norman James10ff6a32015-08-27 14:24:17 -050051 g_print ("Acquired a message bus connection: %s\n",name);
Norman Jamese2765102015-08-19 22:00:55 -050052
Norman James10ff6a32015-08-27 14:24:17 -050053 cmdline *cmd = user_data;
54 if (cmd->argc < 2)
55 {
56 g_print("No objects created. Put object name(s) on command line\n");
57 return;
58 }
59 manager = g_dbus_object_manager_server_new (dbus_object_path);
60 int i=0;
61 for (i=1;i<cmd->argc;i++)
62 {
63 gchar *s;
64 s = g_strdup_printf ("%s/%s",dbus_object_path,cmd->argv[i]);
65 ObjectSkeleton *object = object_skeleton_new (s);
66 g_free (s);
Norman Jamese2765102015-08-19 22:00:55 -050067
Norman James5d78b4d2015-09-05 13:34:34 -050068 SensorValue *sensor = sensor_value_skeleton_new ();
69 object_skeleton_set_sensor_value (object, sensor);
Norman James10ff6a32015-08-27 14:24:17 -050070 g_object_unref (sensor);
Norman James5d78b4d2015-09-05 13:34:34 -050071
72 //must init variant
73 GVariant* v = NEW_VARIANT_U(0);
74 sensor_value_set_value(sensor,v);
75
Norman James10ff6a32015-08-27 14:24:17 -050076 // set units
Norman James5d78b4d2015-09-05 13:34:34 -050077 sensor_value_set_units(sensor,"");
Norman James10ff6a32015-08-27 14:24:17 -050078 //define method callbacks here
Norman James5d78b4d2015-09-05 13:34:34 -050079 //g_signal_connect (sensor,
80 // "handle-get-value",
81 // G_CALLBACK (on_get),
82 // NULL); /* user_data */
83 //g_signal_connect (sensor,
84 // "handle-get-units",
85 // G_CALLBACK (on_get_units),
86 // NULL); /* user_data */
87 //g_signal_connect (sensor,
88 // "handle-set-value",
89 // G_CALLBACK (on_set),
90 // NULL); /* user_data */
Norman James10ff6a32015-08-27 14:24:17 -050091
92 /* Export the object (@manager takes its own reference to @object) */
93 g_dbus_object_manager_server_export (manager, G_DBUS_OBJECT_SKELETON (object));
94 g_object_unref (object);
95 }
Norman Jamese2765102015-08-19 22:00:55 -050096
97 /* Export all objects */
98 g_dbus_object_manager_server_set_connection (manager, connection);
99}
100
101static void
102on_name_acquired (GDBusConnection *connection,
103 const gchar *name,
104 gpointer user_data)
105{
106 g_print ("Acquired the name %s\n", name);
107}
108
109static void
110on_name_lost (GDBusConnection *connection,
111 const gchar *name,
112 gpointer user_data)
113{
114 g_print ("Lost the name %s\n", name);
115}
116
Norman James10ff6a32015-08-27 14:24:17 -0500117
Norman Jamese2765102015-08-19 22:00:55 -0500118gint
119main (gint argc, gchar *argv[])
120{
121 GMainLoop *loop;
Norman James10ff6a32015-08-27 14:24:17 -0500122 cmdline cmd;
123 cmd.argc = argc;
124 cmd.argv = argv;
Norman Jamese2765102015-08-19 22:00:55 -0500125 guint id;
Norman Jamese2765102015-08-19 22:00:55 -0500126 loop = g_main_loop_new (NULL, FALSE);
127
128 id = g_bus_own_name (G_BUS_TYPE_SESSION,
Norman James26072c02015-08-25 07:14:29 -0500129 dbus_name,
Norman Jamese2765102015-08-19 22:00:55 -0500130 G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT |
131 G_BUS_NAME_OWNER_FLAGS_REPLACE,
132 on_bus_acquired,
133 on_name_acquired,
134 on_name_lost,
Norman James10ff6a32015-08-27 14:24:17 -0500135 &cmd,
Norman Jamese2765102015-08-19 22:00:55 -0500136 NULL);
Norman James10ff6a32015-08-27 14:24:17 -0500137
Norman Jamese2765102015-08-19 22:00:55 -0500138 g_main_loop_run (loop);
139
140 g_bus_unown_name (id);
141 g_main_loop_unref (loop);
142 return 0;
143}