blob: 57357d9ee20391eae5bf77deb3eb6829b816e31a [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_set_value (SensorValue *sen,
Norman Jamese2765102015-08-19 22:00:55 -050015 GDBusMethodInvocation *invocation,
Norman James9e6acf92015-09-08 07:00:04 -050016 GVariant* value,
Norman Jamese2765102015-08-19 22:00:55 -050017 gpointer user_data)
18{
Norman James9e6acf92015-09-08 07:00:04 -050019 SensorMatch *match = object_get_sensor_match((Object*)user_data);
Norman James362a80f2015-09-14 14:04:39 -050020 Control* control = object_get_control((Object*)user_data);
21
Norman James9e6acf92015-09-08 07:00:04 -050022 sensor_value_set_value(sen,value);
23 sensor_value_emit_changed(sen,value,"");
24 // Important host status values
25 guchar host_status = g_variant_get_byte(g_variant_get_variant(value));
26
27 if (host_status == BOOTED)
28 {
29 sensor_match_set_state(match,host_status);
30 sensor_match_emit_sensor_match(match,host_status);
Norman James362a80f2015-09-14 14:04:39 -050031 control_emit_goto_system_state(control,"BOOTED");
Norman James9e6acf92015-09-08 07:00:04 -050032 }
Norman James5d78b4d2015-09-05 13:34:34 -050033 sensor_value_complete_set_value(sen,invocation);
Norman James90baede2015-09-02 20:32:49 -050034 return TRUE;
Norman Jamese2765102015-08-19 22:00:55 -050035}
Norman James9e6acf92015-09-08 07:00:04 -050036
37static gboolean
38on_init (SensorValue *sen,
39 GDBusMethodInvocation *invocation,
40 gpointer user_data)
41{
42 sensor_value_complete_init(sen,invocation);
43 return TRUE;
44}
45
Norman James362a80f2015-09-14 14:04:39 -050046static gboolean
47on_init_control (Control *control,
48 GDBusMethodInvocation *invocation,
49 gpointer user_data)
50{
51 control_complete_init(control,invocation);
52 return TRUE;
53}
54
Norman Jamese2765102015-08-19 22:00:55 -050055static void
56on_bus_acquired (GDBusConnection *connection,
57 const gchar *name,
58 gpointer user_data)
59{
Norman James362a80f2015-09-14 14:04:39 -050060// g_print ("Acquired a message bus connection: %s\n",name);
Norman Jamese2765102015-08-19 22:00:55 -050061
Norman James10ff6a32015-08-27 14:24:17 -050062 cmdline *cmd = user_data;
63 if (cmd->argc < 2)
64 {
65 g_print("No objects created. Put object name(s) on command line\n");
66 return;
67 }
68 manager = g_dbus_object_manager_server_new (dbus_object_path);
69 int i=0;
70 for (i=1;i<cmd->argc;i++)
71 {
72 gchar *s;
73 s = g_strdup_printf ("%s/%s",dbus_object_path,cmd->argv[i]);
74 ObjectSkeleton *object = object_skeleton_new (s);
75 g_free (s);
Norman Jamese2765102015-08-19 22:00:55 -050076
Norman James5d78b4d2015-09-05 13:34:34 -050077 SensorValue *sensor = sensor_value_skeleton_new ();
78 object_skeleton_set_sensor_value (object, sensor);
Norman James10ff6a32015-08-27 14:24:17 -050079 g_object_unref (sensor);
Norman James9e6acf92015-09-08 07:00:04 -050080
81 SensorMatch *match = sensor_match_skeleton_new ();
82 object_skeleton_set_sensor_match (object, match);
83 g_object_unref (match);
Norman James362a80f2015-09-14 14:04:39 -050084
85 Control* control = control_skeleton_new ();
86 object_skeleton_set_control (object, control);
87 g_object_unref (control);
Norman James5d78b4d2015-09-05 13:34:34 -050088
89 //must init variant
Norman James9e6acf92015-09-08 07:00:04 -050090 GVariant* v = NEW_VARIANT_B(0);
Norman James5d78b4d2015-09-05 13:34:34 -050091 sensor_value_set_value(sensor,v);
92
Norman James9e6acf92015-09-08 07:00:04 -050093 // set units
Norman James5d78b4d2015-09-05 13:34:34 -050094 sensor_value_set_units(sensor,"");
Norman James9e6acf92015-09-08 07:00:04 -050095 sensor_value_set_settable(sensor,TRUE);
Norman James362a80f2015-09-14 14:04:39 -050096 //must emit change so sensor manager sees initial value
97 sensor_value_emit_changed(sensor,v,"");
Norman James9e6acf92015-09-08 07:00:04 -050098
Norman James10ff6a32015-08-27 14:24:17 -050099 //define method callbacks here
Norman James9e6acf92015-09-08 07:00:04 -0500100 g_signal_connect (sensor,
101 "handle-init",
102 G_CALLBACK (on_init),
103 NULL); /* user_data */
Norman James362a80f2015-09-14 14:04:39 -0500104
105 g_signal_connect (control,
106 "handle-init",
107 G_CALLBACK (on_init_control),
108 NULL); /* user_data */
109
110 g_signal_connect (sensor,
Norman James9e6acf92015-09-08 07:00:04 -0500111 "handle-set-value",
112 G_CALLBACK (on_set_value),
113 object); /* user_data */
Norman James10ff6a32015-08-27 14:24:17 -0500114
115 /* Export the object (@manager takes its own reference to @object) */
116 g_dbus_object_manager_server_export (manager, G_DBUS_OBJECT_SKELETON (object));
117 g_object_unref (object);
118 }
Norman Jamese2765102015-08-19 22:00:55 -0500119
120 /* Export all objects */
121 g_dbus_object_manager_server_set_connection (manager, connection);
122}
123
124static void
125on_name_acquired (GDBusConnection *connection,
126 const gchar *name,
127 gpointer user_data)
128{
Norman James362a80f2015-09-14 14:04:39 -0500129 //g_print ("Acquired the name %s\n", name);
Norman Jamese2765102015-08-19 22:00:55 -0500130}
131
132static void
133on_name_lost (GDBusConnection *connection,
134 const gchar *name,
135 gpointer user_data)
136{
Norman James362a80f2015-09-14 14:04:39 -0500137 //g_print ("Lost the name %s\n", name);
Norman Jamese2765102015-08-19 22:00:55 -0500138}
139
Norman James10ff6a32015-08-27 14:24:17 -0500140
Norman Jamese2765102015-08-19 22:00:55 -0500141gint
142main (gint argc, gchar *argv[])
143{
144 GMainLoop *loop;
Norman James10ff6a32015-08-27 14:24:17 -0500145 cmdline cmd;
146 cmd.argc = argc;
147 cmd.argv = argv;
Norman Jamese2765102015-08-19 22:00:55 -0500148 guint id;
Norman Jamese2765102015-08-19 22:00:55 -0500149 loop = g_main_loop_new (NULL, FALSE);
150
151 id = g_bus_own_name (G_BUS_TYPE_SESSION,
Norman James26072c02015-08-25 07:14:29 -0500152 dbus_name,
Norman Jamese2765102015-08-19 22:00:55 -0500153 G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT |
154 G_BUS_NAME_OWNER_FLAGS_REPLACE,
155 on_bus_acquired,
156 on_name_acquired,
157 on_name_lost,
Norman James10ff6a32015-08-27 14:24:17 -0500158 &cmd,
Norman Jamese2765102015-08-19 22:00:55 -0500159 NULL);
Norman James10ff6a32015-08-27 14:24:17 -0500160
Norman Jamese2765102015-08-19 22:00:55 -0500161 g_main_loop_run (loop);
162
163 g_bus_unown_name (id);
164 g_main_loop_unref (loop);
165 return 0;
166}