blob: d631200feedd6151c4fb68a0995f56ebcb1fadf6 [file] [log] [blame]
Norman James362a80f2015-09-14 14:04:39 -05001#include "interfaces/openbmc_intf.h"
Norman James10ff6a32015-08-27 14:24:17 -05002#include "openbmc.h"
3
Norman James9e6acf92015-09-08 07:00:04 -05004#define BOOTED 100
Norman Jamese2765102015-08-19 22:00:55 -05005/* ---------------------------------------------------------------------------------------------------- */
Norman James10ff6a32015-08-27 14:24:17 -05006
Norman James362a80f2015-09-14 14:04:39 -05007static const gchar* dbus_object_path = "/org/openbmc/sensors";
Norman James26072c02015-08-25 07:14:29 -05008static const gchar* dbus_name = "org.openbmc.sensors.HostStatus";
Norman James10ff6a32015-08-27 14:24:17 -05009static guint heartbeat = 0;
Norman Jamese2765102015-08-19 22:00:55 -050010
11static GDBusObjectManagerServer *manager = NULL;
Norman Jamese2765102015-08-19 22:00:55 -050012
13static gboolean
Norman James9e6acf92015-09-08 07:00:04 -050014on_init (SensorValue *sen,
15 GDBusMethodInvocation *invocation,
16 gpointer user_data)
17{
18 sensor_value_complete_init(sen,invocation);
19 return TRUE;
20}
21
Norman James362a80f2015-09-14 14:04:39 -050022static gboolean
Norman Jamesdfdaca92015-09-27 22:11:15 -050023on_init_control (Control *control,
Norman James362a80f2015-09-14 14:04:39 -050024 GDBusMethodInvocation *invocation,
25 gpointer user_data)
26{
27 control_complete_init(control,invocation);
28 return TRUE;
29}
Norman James19e45912015-10-04 20:19:41 -050030static gboolean
31on_set_value (SensorValue *sen,
32 GDBusMethodInvocation *invocation,
33 guchar host_status,
34 gpointer user_data)
Norman Jamesdfdaca92015-09-27 22:11:15 -050035{
Norman James19e45912015-10-04 20:19:41 -050036 SensorValue *v = object_get_sensor_value((Object*)user_data);
Norman Jamesdfdaca92015-09-27 22:11:15 -050037 SensorMatch *match = object_get_sensor_match((Object*)user_data);
Norman Jamesdfdaca92015-09-27 22:11:15 -050038 Control* control = object_get_control((Object*)user_data);
39
Norman James19e45912015-10-04 20:19:41 -050040 GVariant *old_value = sensor_value_get_value(sen);
41 GVariant *value = NEW_VARIANT_B(host_status);
42 if (VARIANT_COMPARE(old_value,value) != 0)
Norman Jamesdfdaca92015-09-27 22:11:15 -050043 {
Norman James19e45912015-10-04 20:19:41 -050044 sensor_value_set_value(sen, value);
45 sensor_value_emit_changed(sen, value, "");
46 if (host_status == BOOTED)
Norman Jamesdfdaca92015-09-27 22:11:15 -050047 {
Norman James19e45912015-10-04 20:19:41 -050048 sensor_match_set_state(match,host_status);
49 sensor_match_emit_sensor_match(match,host_status);
50 control_emit_goto_system_state(control,"BOOTED");
Norman Jamesdfdaca92015-09-27 22:11:15 -050051 }
52 }
Norman James19e45912015-10-04 20:19:41 -050053 sensor_value_complete_set_value(sen,invocation);
54 return TRUE;
Norman Jamesdfdaca92015-09-27 22:11:15 -050055}
56
Norman Jamese2765102015-08-19 22:00:55 -050057static void
58on_bus_acquired (GDBusConnection *connection,
59 const gchar *name,
60 gpointer user_data)
61{
Norman James362a80f2015-09-14 14:04:39 -050062// g_print ("Acquired a message bus connection: %s\n",name);
Norman Jamese2765102015-08-19 22:00:55 -050063
Norman James10ff6a32015-08-27 14:24:17 -050064 cmdline *cmd = user_data;
65 if (cmd->argc < 2)
66 {
67 g_print("No objects created. Put object name(s) on command line\n");
68 return;
69 }
70 manager = g_dbus_object_manager_server_new (dbus_object_path);
71 int i=0;
72 for (i=1;i<cmd->argc;i++)
73 {
74 gchar *s;
75 s = g_strdup_printf ("%s/%s",dbus_object_path,cmd->argv[i]);
76 ObjectSkeleton *object = object_skeleton_new (s);
77 g_free (s);
Norman Jamese2765102015-08-19 22:00:55 -050078
Norman James5d78b4d2015-09-05 13:34:34 -050079 SensorValue *sensor = sensor_value_skeleton_new ();
80 object_skeleton_set_sensor_value (object, sensor);
Norman James10ff6a32015-08-27 14:24:17 -050081 g_object_unref (sensor);
Norman James9e6acf92015-09-08 07:00:04 -050082
83 SensorMatch *match = sensor_match_skeleton_new ();
84 object_skeleton_set_sensor_match (object, match);
85 g_object_unref (match);
Norman James362a80f2015-09-14 14:04:39 -050086
Norman Jamesdfdaca92015-09-27 22:11:15 -050087 Control *control = control_skeleton_new ();
88 object_skeleton_set_control (object, control);
89 g_object_unref (control);
90
Norman James5d78b4d2015-09-05 13:34:34 -050091 //must init variant
Norman James9e6acf92015-09-08 07:00:04 -050092 GVariant* v = NEW_VARIANT_B(0);
Norman James5d78b4d2015-09-05 13:34:34 -050093 sensor_value_set_value(sensor,v);
94
Norman James9e6acf92015-09-08 07:00:04 -050095 // set units
Norman James5d78b4d2015-09-05 13:34:34 -050096 sensor_value_set_units(sensor,"");
Norman James9e6acf92015-09-08 07:00:04 -050097 sensor_value_set_settable(sensor,TRUE);
Norman James362a80f2015-09-14 14:04:39 -050098 //must emit change so sensor manager sees initial value
99 sensor_value_emit_changed(sensor,v,"");
Norman James9e6acf92015-09-08 07:00:04 -0500100
Norman Jamesdfdaca92015-09-27 22:11:15 -0500101 //signal handlers
Norman James19e45912015-10-04 20:19:41 -0500102 /*g_dbus_connection_signal_subscribe(connection,
Norman Jamesdfdaca92015-09-27 22:11:15 -0500103 "org.openbmc.sensors.IpmiBt",
104 "org.openbmc.sensors.IpmiBt",
105 "SetSensor",
106 "/org/openbmc/sensors/IpmiBt",
107 NULL,
108 G_DBUS_SIGNAL_FLAGS_NONE,
109 (GDBusSignalCallback) on_set_ipmi,
110 object,
111 NULL );
Norman James19e45912015-10-04 20:19:41 -0500112 */
Norman James10ff6a32015-08-27 14:24:17 -0500113 //define method callbacks here
Norman James9e6acf92015-09-08 07:00:04 -0500114 g_signal_connect (sensor,
115 "handle-init",
116 G_CALLBACK (on_init),
117 NULL); /* user_data */
Norman James362a80f2015-09-14 14:04:39 -0500118
119 g_signal_connect (control,
120 "handle-init",
121 G_CALLBACK (on_init_control),
122 NULL); /* user_data */
123
Norman James19e45912015-10-04 20:19:41 -0500124 g_signal_connect (sensor,
125 "handle-set-value",
126 G_CALLBACK (on_set_value),
127 object); /* user_data */
Norman Jamesdfdaca92015-09-27 22:11:15 -0500128
Norman James10ff6a32015-08-27 14:24:17 -0500129
130 /* Export the object (@manager takes its own reference to @object) */
131 g_dbus_object_manager_server_export (manager, G_DBUS_OBJECT_SKELETON (object));
132 g_object_unref (object);
133 }
Norman Jamese2765102015-08-19 22:00:55 -0500134
135 /* Export all objects */
136 g_dbus_object_manager_server_set_connection (manager, connection);
137}
138
139static void
140on_name_acquired (GDBusConnection *connection,
141 const gchar *name,
142 gpointer user_data)
143{
Norman James362a80f2015-09-14 14:04:39 -0500144 //g_print ("Acquired the name %s\n", name);
Norman Jamese2765102015-08-19 22:00:55 -0500145}
146
147static void
148on_name_lost (GDBusConnection *connection,
149 const gchar *name,
150 gpointer user_data)
151{
Norman James362a80f2015-09-14 14:04:39 -0500152 //g_print ("Lost the name %s\n", name);
Norman Jamese2765102015-08-19 22:00:55 -0500153}
154
Norman James10ff6a32015-08-27 14:24:17 -0500155
Norman Jamese2765102015-08-19 22:00:55 -0500156gint
157main (gint argc, gchar *argv[])
158{
159 GMainLoop *loop;
Norman James10ff6a32015-08-27 14:24:17 -0500160 cmdline cmd;
161 cmd.argc = argc;
162 cmd.argv = argv;
Norman Jamese2765102015-08-19 22:00:55 -0500163 guint id;
Norman Jamese2765102015-08-19 22:00:55 -0500164 loop = g_main_loop_new (NULL, FALSE);
165
Norman James5e792e32015-10-07 17:36:17 -0500166 id = g_bus_own_name (DBUS_TYPE,
Norman James26072c02015-08-25 07:14:29 -0500167 dbus_name,
Norman Jamese2765102015-08-19 22:00:55 -0500168 G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT |
169 G_BUS_NAME_OWNER_FLAGS_REPLACE,
170 on_bus_acquired,
171 on_name_acquired,
172 on_name_lost,
Norman James10ff6a32015-08-27 14:24:17 -0500173 &cmd,
Norman Jamese2765102015-08-19 22:00:55 -0500174 NULL);
Norman James10ff6a32015-08-27 14:24:17 -0500175
Norman Jamese2765102015-08-19 22:00:55 -0500176 g_main_loop_run (loop);
177
178 g_bus_unown_name (id);
179 g_main_loop_unref (loop);
180 return 0;
181}