blob: 29545d1883f622ccc8a6a294f159f74fa34c0dd4 [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 Jamese2765102015-08-19 22:00:55 -050013
14static gboolean
Norman James10ff6a32015-08-27 14:24:17 -050015on_get_units (SensorInteger *sen,
Norman Jamese2765102015-08-19 22:00:55 -050016 GDBusMethodInvocation *invocation,
17 gpointer user_data)
18{
Norman James10ff6a32015-08-27 14:24:17 -050019 const gchar* val = sensor_integer_get_units(sen);
20 sensor_integer_complete_get_units(sen,invocation,val);
Norman Jamese2765102015-08-19 22:00:55 -050021 return TRUE;
22}
23
24static gboolean
Norman James10ff6a32015-08-27 14:24:17 -050025on_get (SensorInteger *sen,
Norman Jamese2765102015-08-19 22:00:55 -050026 GDBusMethodInvocation *invocation,
27 gpointer user_data)
28{
Norman James10ff6a32015-08-27 14:24:17 -050029 guint reading = sensor_integer_get_value(sen);
30 sensor_integer_complete_get_value(sen,invocation,reading);
Norman Jamese2765102015-08-19 22:00:55 -050031 return TRUE;
32}
Norman James10ff6a32015-08-27 14:24:17 -050033
Norman Jamese2765102015-08-19 22:00:55 -050034static gboolean
Norman James90baede2015-09-02 20:32:49 -050035on_set (SensorInteger *sen,
Norman Jamese2765102015-08-19 22:00:55 -050036 GDBusMethodInvocation *invocation,
Norman James90baede2015-09-02 20:32:49 -050037 guint value,
Norman Jamese2765102015-08-19 22:00:55 -050038 gpointer user_data)
39{
Norman James90baede2015-09-02 20:32:49 -050040 sensor_integer_set_value(sen,value);
41 sensor_integer_emit_changed(sen,value,sensor_integer_get_units(sen));
42 sensor_integer_complete_set_value(sen,invocation);
43 return TRUE;
Norman Jamese2765102015-08-19 22:00:55 -050044}
45
46static 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 James10ff6a32015-08-27 14:24:17 -050068 SensorInteger *sensor = sensor_integer_skeleton_new ();
69 object_skeleton_set_sensor_integer (object, sensor);
70 g_object_unref (sensor);
71
72 // set units
Norman James90baede2015-09-02 20:32:49 -050073 sensor_integer_set_units(sensor,"");
Norman James10ff6a32015-08-27 14:24:17 -050074 //define method callbacks here
75 g_signal_connect (sensor,
Norman Jamese2765102015-08-19 22:00:55 -050076 "handle-get-value",
77 G_CALLBACK (on_get),
78 NULL); /* user_data */
Norman James10ff6a32015-08-27 14:24:17 -050079 g_signal_connect (sensor,
Norman Jamese2765102015-08-19 22:00:55 -050080 "handle-get-units",
81 G_CALLBACK (on_get_units),
82 NULL); /* user_data */
Norman James10ff6a32015-08-27 14:24:17 -050083 g_signal_connect (sensor,
Norman James90baede2015-09-02 20:32:49 -050084 "handle-set-value",
85 G_CALLBACK (on_set),
Norman James10ff6a32015-08-27 14:24:17 -050086 NULL); /* user_data */
Norman James10ff6a32015-08-27 14:24:17 -050087
88 /* Export the object (@manager takes its own reference to @object) */
89 g_dbus_object_manager_server_export (manager, G_DBUS_OBJECT_SKELETON (object));
90 g_object_unref (object);
91 }
Norman Jamese2765102015-08-19 22:00:55 -050092
93 /* Export all objects */
94 g_dbus_object_manager_server_set_connection (manager, connection);
95}
96
97static void
98on_name_acquired (GDBusConnection *connection,
99 const gchar *name,
100 gpointer user_data)
101{
102 g_print ("Acquired the name %s\n", name);
103}
104
105static void
106on_name_lost (GDBusConnection *connection,
107 const gchar *name,
108 gpointer user_data)
109{
110 g_print ("Lost the name %s\n", name);
111}
112
Norman James10ff6a32015-08-27 14:24:17 -0500113
Norman Jamese2765102015-08-19 22:00:55 -0500114gint
115main (gint argc, gchar *argv[])
116{
117 GMainLoop *loop;
Norman James10ff6a32015-08-27 14:24:17 -0500118 cmdline cmd;
119 cmd.argc = argc;
120 cmd.argv = argv;
Norman Jamese2765102015-08-19 22:00:55 -0500121 guint id;
Norman Jamese2765102015-08-19 22:00:55 -0500122 loop = g_main_loop_new (NULL, FALSE);
123
124 id = g_bus_own_name (G_BUS_TYPE_SESSION,
Norman James26072c02015-08-25 07:14:29 -0500125 dbus_name,
Norman Jamese2765102015-08-19 22:00:55 -0500126 G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT |
127 G_BUS_NAME_OWNER_FLAGS_REPLACE,
128 on_bus_acquired,
129 on_name_acquired,
130 on_name_lost,
Norman James10ff6a32015-08-27 14:24:17 -0500131 &cmd,
Norman Jamese2765102015-08-19 22:00:55 -0500132 NULL);
Norman James10ff6a32015-08-27 14:24:17 -0500133
Norman Jamese2765102015-08-19 22:00:55 -0500134 g_main_loop_run (loop);
135
136 g_bus_unown_name (id);
137 g_main_loop_unref (loop);
138 return 0;
139}