blob: df5c326c7c21e0a5601a425ed4e9a465453399f1 [file] [log] [blame]
Norman James10ff6a32015-08-27 14:24:17 -05001#include "interfaces/sensor2.h"
2#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 James10ff6a32015-08-27 14:24:17 -050035on_set_config (SensorInteger *sen,
Norman Jamese2765102015-08-19 22:00:55 -050036 GDBusMethodInvocation *invocation,
Norman James10ff6a32015-08-27 14:24:17 -050037 gchar** config,
Norman Jamese2765102015-08-19 22:00:55 -050038 gpointer user_data)
39{
Norman James10ff6a32015-08-27 14:24:17 -050040 sensor_integer_complete_set_config_data(sen,invocation);
Norman Jamese2765102015-08-19 22:00:55 -050041 return TRUE;
42}
43
Norman James10ff6a32015-08-27 14:24:17 -050044
Norman Jamese2765102015-08-19 22:00:55 -050045static void
46on_bus_acquired (GDBusConnection *connection,
47 const gchar *name,
48 gpointer user_data)
49{
Norman James10ff6a32015-08-27 14:24:17 -050050 g_print ("Acquired a message bus connection: %s\n",name);
Norman Jamese2765102015-08-19 22:00:55 -050051
Norman James10ff6a32015-08-27 14:24:17 -050052 cmdline *cmd = user_data;
53 if (cmd->argc < 2)
54 {
55 g_print("No objects created. Put object name(s) on command line\n");
56 return;
57 }
58 manager = g_dbus_object_manager_server_new (dbus_object_path);
59 int i=0;
60 for (i=1;i<cmd->argc;i++)
61 {
62 gchar *s;
63 s = g_strdup_printf ("%s/%s",dbus_object_path,cmd->argv[i]);
64 ObjectSkeleton *object = object_skeleton_new (s);
65 g_free (s);
Norman Jamese2765102015-08-19 22:00:55 -050066
Norman James10ff6a32015-08-27 14:24:17 -050067 SensorInteger *sensor = sensor_integer_skeleton_new ();
68 object_skeleton_set_sensor_integer (object, sensor);
69 g_object_unref (sensor);
70
71 // set units
72 sensor_integer_set_units(sensor,"C");
73 //define method callbacks here
74 g_signal_connect (sensor,
Norman Jamese2765102015-08-19 22:00:55 -050075 "handle-get-value",
76 G_CALLBACK (on_get),
77 NULL); /* user_data */
Norman James10ff6a32015-08-27 14:24:17 -050078 g_signal_connect (sensor,
Norman Jamese2765102015-08-19 22:00:55 -050079 "handle-get-units",
80 G_CALLBACK (on_get_units),
81 NULL); /* user_data */
82
Norman James10ff6a32015-08-27 14:24:17 -050083 g_signal_connect (sensor,
84 "handle-set-config-data",
85 G_CALLBACK (on_set_config),
86 NULL); /* user_data */
87
88
89 /* Export the object (@manager takes its own reference to @object) */
90 g_dbus_object_manager_server_export (manager, G_DBUS_OBJECT_SKELETON (object));
91 g_object_unref (object);
92 }
Norman Jamese2765102015-08-19 22:00:55 -050093
94 /* Export all objects */
95 g_dbus_object_manager_server_set_connection (manager, connection);
96}
97
98static void
99on_name_acquired (GDBusConnection *connection,
100 const gchar *name,
101 gpointer user_data)
102{
103 g_print ("Acquired the name %s\n", name);
104}
105
106static void
107on_name_lost (GDBusConnection *connection,
108 const gchar *name,
109 gpointer user_data)
110{
111 g_print ("Lost the name %s\n", name);
112}
113
Norman James10ff6a32015-08-27 14:24:17 -0500114
Norman Jamese2765102015-08-19 22:00:55 -0500115gint
116main (gint argc, gchar *argv[])
117{
118 GMainLoop *loop;
Norman James10ff6a32015-08-27 14:24:17 -0500119 cmdline cmd;
120 cmd.argc = argc;
121 cmd.argv = argv;
Norman Jamese2765102015-08-19 22:00:55 -0500122 guint id;
Norman Jamese2765102015-08-19 22:00:55 -0500123 loop = g_main_loop_new (NULL, FALSE);
124
125 id = g_bus_own_name (G_BUS_TYPE_SESSION,
Norman James26072c02015-08-25 07:14:29 -0500126 dbus_name,
Norman Jamese2765102015-08-19 22:00:55 -0500127 G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT |
128 G_BUS_NAME_OWNER_FLAGS_REPLACE,
129 on_bus_acquired,
130 on_name_acquired,
131 on_name_lost,
Norman James10ff6a32015-08-27 14:24:17 -0500132 &cmd,
Norman Jamese2765102015-08-19 22:00:55 -0500133 NULL);
Norman James10ff6a32015-08-27 14:24:17 -0500134
Norman Jamese2765102015-08-19 22:00:55 -0500135 g_main_loop_run (loop);
136
137 g_bus_unown_name (id);
138 g_main_loop_unref (loop);
139 return 0;
140}