blob: eb8fe8c3b6316d9bf54f72b0cfb66ee44d70a115 [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#include "sensor_threshold.h"
Norman James3f97c5d2015-08-26 17:44:14 -05004
5
Norman Jamese2765102015-08-19 22:00:55 -05006/* ---------------------------------------------------------------------------------------------------- */
Norman James3f97c5d2015-08-26 17:44:14 -05007
Norman James362a80f2015-09-14 14:04:39 -05008static const gchar* dbus_object_path = "/org/openbmc/sensors";
Norman James26072c02015-08-25 07:14:29 -05009static const gchar* dbus_name = "org.openbmc.sensors.Temperature.Ambient";
Norman James10ff6a32015-08-27 14:24:17 -050010static guint heartbeat = 0;
Norman James3f97c5d2015-08-26 17:44:14 -050011
Norman Jamese2765102015-08-19 22:00:55 -050012static GDBusObjectManagerServer *manager = NULL;
Norman Jamese2765102015-08-19 22:00:55 -050013
Norman James10ff6a32015-08-27 14:24:17 -050014static gboolean
15poll_sensor(gpointer user_data)
16{
Norman James5d78b4d2015-09-05 13:34:34 -050017 SensorValue *sensor = object_get_sensor_value((Object*)user_data);
18 SensorThreshold *threshold = object_get_sensor_threshold((Object*)user_data);
Norman James90baede2015-09-02 20:32:49 -050019 SensorI2c *i2c = object_get_sensor_i2c((Object*)user_data);
20
Norman James5d78b4d2015-09-05 13:34:34 -050021 GVariant* v_value = sensor_value_get_value(sensor);
Norman James9e6acf92015-09-08 07:00:04 -050022 guint poll_interval = sensor_value_get_poll_interval(sensor);
23
Norman James5d78b4d2015-09-05 13:34:34 -050024 //TODO: Change to actually read sensor
25 double value = GET_VARIANT_D(v_value);
Norman James362a80f2015-09-14 14:04:39 -050026 //g_print("Reading I2C = %s; Address = %s; %f\n",
27 // sensor_i2c_get_dev_path(i2c),sensor_i2c_get_address(i2c),value);
Norman James5d78b4d2015-09-05 13:34:34 -050028
Norman James10ff6a32015-08-27 14:24:17 -050029 value = value+1;
Norman James32e74e22015-09-15 21:28:06 -050030 // Do this in case of an error
31 //sensor_value_emit_error(sensor);
Norman James5d78b4d2015-09-05 13:34:34 -050032
Norman James362a80f2015-09-14 14:04:39 -050033 if (heartbeat > 4000)
Norman James10ff6a32015-08-27 14:24:17 -050034 {
35 heartbeat = 0;
Norman James5d78b4d2015-09-05 13:34:34 -050036 sensor_value_emit_heartbeat(sensor,dbus_name);
Norman James10ff6a32015-08-27 14:24:17 -050037 }
38 else
39 {
40 heartbeat = heartbeat+poll_interval;
41 }
42
43 // End actually reading sensor
Norman James10ff6a32015-08-27 14:24:17 -050044
45 //if changed, set property and emit signal
Norman James5d78b4d2015-09-05 13:34:34 -050046 if (value != GET_VARIANT_D(v_value))
Norman James10ff6a32015-08-27 14:24:17 -050047 {
Norman James5d78b4d2015-09-05 13:34:34 -050048 GVariant* v_new_value = NEW_VARIANT_D(value);
49 sensor_value_set_value(sensor,v_new_value);
Norman James9e6acf92015-09-08 07:00:04 -050050 const gchar* units = sensor_value_get_units(sensor);
51 sensor_value_emit_changed(sensor,v_new_value,units);
52 check_thresholds(threshold,v_new_value);
Norman James10ff6a32015-08-27 14:24:17 -050053 }
54 return TRUE;
55}
Norman James3f97c5d2015-08-26 17:44:14 -050056
Norman Jamesce46e3e2015-08-30 22:25:55 -050057static gboolean
Norman James5d78b4d2015-09-05 13:34:34 -050058on_init (SensorValue *sen,
Norman Jamesce46e3e2015-08-30 22:25:55 -050059 GDBusMethodInvocation *invocation,
60 gpointer user_data)
61{
Norman James9e6acf92015-09-08 07:00:04 -050062
63 guint poll_interval = sensor_value_get_poll_interval(sen);
64 g_timeout_add(poll_interval, poll_sensor, user_data);
65 sensor_value_complete_init(sen,invocation);
66 return TRUE;
Norman Jamesce46e3e2015-08-30 22:25:55 -050067}
68
Norman Jamese2765102015-08-19 22:00:55 -050069
Norman Jamese2765102015-08-19 22:00:55 -050070
71static void
72on_bus_acquired (GDBusConnection *connection,
73 const gchar *name,
74 gpointer user_data)
75{
Norman James362a80f2015-09-14 14:04:39 -050076 // g_print ("Acquired a message bus connection: %s\n",name);
Norman Jamese2765102015-08-19 22:00:55 -050077
Norman James10ff6a32015-08-27 14:24:17 -050078 cmdline *cmd = user_data;
79 if (cmd->argc < 2)
80 {
81 g_print("No objects created. Put object name(s) on command line\n");
82 return;
83 }
84 manager = g_dbus_object_manager_server_new (dbus_object_path);
85 int i=0;
86 for (i=1;i<cmd->argc;i++)
87 {
88 gchar *s;
Norman James362a80f2015-09-14 14:04:39 -050089 s = g_strdup_printf ("%s/Temperature/%s",dbus_object_path,cmd->argv[i]);
Norman James10ff6a32015-08-27 14:24:17 -050090 ObjectSkeleton *object = object_skeleton_new (s);
91 g_free (s);
Norman Jamese2765102015-08-19 22:00:55 -050092
Norman James5d78b4d2015-09-05 13:34:34 -050093 SensorValue *sensor = sensor_value_skeleton_new ();
94 object_skeleton_set_sensor_value (object, sensor);
Norman James10ff6a32015-08-27 14:24:17 -050095 g_object_unref (sensor);
96
Norman James5d78b4d2015-09-05 13:34:34 -050097 SensorThreshold *threshold = sensor_threshold_skeleton_new();
98 object_skeleton_set_sensor_threshold (object,threshold);
Norman James10ff6a32015-08-27 14:24:17 -050099 g_object_unref (threshold);
Norman Jamese2765102015-08-19 22:00:55 -0500100
Norman James90baede2015-09-02 20:32:49 -0500101 SensorI2c *i2c = sensor_i2c_skeleton_new();
102 object_skeleton_set_sensor_i2c (object,i2c);
103 g_object_unref (i2c);
104
105
Norman James5d78b4d2015-09-05 13:34:34 -0500106 // set properties
107 GVariant* value = g_variant_new_variant(g_variant_new_double(1.0));
108 sensor_value_set_value(sensor,value);
109 sensor_value_set_units(sensor,"C");
Norman James9e6acf92015-09-08 07:00:04 -0500110 sensor_value_set_settable(sensor,FALSE);
Norman James5d78b4d2015-09-05 13:34:34 -0500111 sensor_threshold_set_state(threshold,NOT_SET);
112
113 sensor_threshold_set_upper_critical(threshold,
114 g_variant_new_variant(g_variant_new_double(0.0)));
115 sensor_threshold_set_upper_warning(threshold,
116 g_variant_new_variant(g_variant_new_double(0.0)));
117 sensor_threshold_set_lower_warning(threshold,
118 g_variant_new_variant(g_variant_new_double(0.0)));
119 sensor_threshold_set_lower_critical(threshold,
120 g_variant_new_variant(g_variant_new_double(0.0)));
121
122 //define method callbacks here
Norman Jamese2765102015-08-19 22:00:55 -0500123
Norman Jamesce46e3e2015-08-30 22:25:55 -0500124 g_signal_connect (sensor,
125 "handle-init",
126 G_CALLBACK (on_init),
Norman James9e6acf92015-09-08 07:00:04 -0500127 object); /* user_data */
Norman James3f97c5d2015-08-26 17:44:14 -0500128
Norman James10ff6a32015-08-27 14:24:17 -0500129 g_signal_connect (threshold,
Norman James10ff6a32015-08-27 14:24:17 -0500130 "handle-get-state",
131 G_CALLBACK (get_threshold_state),
Norman James3f97c5d2015-08-26 17:44:14 -0500132 NULL); /* user_data */
Norman Jamescc7ae122015-08-24 14:26:09 -0500133
Norman James9e6acf92015-09-08 07:00:04 -0500134
Norman Jamese2765102015-08-19 22:00:55 -0500135
Norman James10ff6a32015-08-27 14:24:17 -0500136 /* Export the object (@manager takes its own reference to @object) */
137 g_dbus_object_manager_server_export (manager, G_DBUS_OBJECT_SKELETON (object));
138 g_object_unref (object);
139 }
Norman Jamese2765102015-08-19 22:00:55 -0500140
141 /* Export all objects */
142 g_dbus_object_manager_server_set_connection (manager, connection);
143}
144
145static void
146on_name_acquired (GDBusConnection *connection,
147 const gchar *name,
148 gpointer user_data)
149{
Norman James362a80f2015-09-14 14:04:39 -0500150// g_print ("Acquired the name %s\n", name);
Norman Jamese2765102015-08-19 22:00:55 -0500151}
152
153static void
154on_name_lost (GDBusConnection *connection,
155 const gchar *name,
156 gpointer user_data)
157{
Norman James362a80f2015-09-14 14:04:39 -0500158// g_print ("Lost the name %s\n", name);
Norman Jamese2765102015-08-19 22:00:55 -0500159}
160
Norman Jamese2765102015-08-19 22:00:55 -0500161
162gint
163main (gint argc, gchar *argv[])
164{
165 GMainLoop *loop;
Norman James10ff6a32015-08-27 14:24:17 -0500166 cmdline cmd;
167 cmd.argc = argc;
168 cmd.argv = argv;
Norman Jamese2765102015-08-19 22:00:55 -0500169 guint id;
Norman Jamese2765102015-08-19 22:00:55 -0500170 loop = g_main_loop_new (NULL, FALSE);
171
Norman James5e792e32015-10-07 17:36:17 -0500172 id = g_bus_own_name (DBUS_TYPE,
Norman James26072c02015-08-25 07:14:29 -0500173 dbus_name,
Norman Jamese2765102015-08-19 22:00:55 -0500174 G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT |
175 G_BUS_NAME_OWNER_FLAGS_REPLACE,
176 on_bus_acquired,
177 on_name_acquired,
178 on_name_lost,
Norman James10ff6a32015-08-27 14:24:17 -0500179 &cmd,
Norman Jamese2765102015-08-19 22:00:55 -0500180 NULL);
181
Norman Jamese2765102015-08-19 22:00:55 -0500182 g_main_loop_run (loop);
183
184 g_bus_unown_name (id);
185 g_main_loop_unref (loop);
186 return 0;
187}