blob: 8a05fd2a4c652ba40df081322c14417ba84ed654 [file] [log] [blame]
Norman Jamese2765102015-08-19 22:00:55 -05001#include "interfaces/sensor.h"
2
Norman James3f97c5d2015-08-26 17:44:14 -05003
4
Norman Jamese2765102015-08-19 22:00:55 -05005/* ---------------------------------------------------------------------------------------------------- */
Norman James3f97c5d2015-08-26 17:44:14 -05006typedef enum { NORMAL,LOWER_CRITICAL,LOWER_WARNING,UPPER_WARNING,UPPER_CRITICAL } threshold_states;
7
8
Norman James26072c02015-08-25 07:14:29 -05009static const gchar* dbus_object_path = "/org/openbmc/sensors/Temperature/Ambient";
10static const gchar* dbus_name = "org.openbmc.sensors.Temperature.Ambient";
Norman Jamese2765102015-08-19 22:00:55 -050011
Norman James3f97c5d2015-08-26 17:44:14 -050012
13
Norman Jamese2765102015-08-19 22:00:55 -050014static GDBusObjectManagerServer *manager = NULL;
15static SensorInteger *sensor = NULL;
16
Norman Jamescc7ae122015-08-24 14:26:09 -050017static gchar* i2c_bus = "";
18static gchar* i2c_address = "";
Norman James3f97c5d2015-08-26 17:44:14 -050019static gboolean thresholds_set = FALSE;
20
21
Norman Jamese2765102015-08-19 22:00:55 -050022
23static gboolean
24on_get_units (SensorInteger *sen,
25 GDBusMethodInvocation *invocation,
26 gpointer user_data)
27{
28 const gchar* val = sensor_integer_get_units(sen);
29 sensor_integer_complete_get_units(sen,invocation,val);
30 return TRUE;
31}
32
33static gboolean
34on_get (SensorInteger *sen,
35 GDBusMethodInvocation *invocation,
36 gpointer user_data)
37{
38 guint reading = sensor_integer_get_value(sen);
39 sensor_integer_complete_get_value(sen,invocation,reading);
40 return TRUE;
41}
42
Norman Jamescc7ae122015-08-24 14:26:09 -050043static gboolean
44on_set_config (SensorInteger *sen,
45 GDBusMethodInvocation *invocation,
46 gchar** config,
47 gpointer user_data)
48{
49 g_print("I2C bus = %s\n",config[0]);
50 g_print("I2C addr = %s\n",config[1]);
51 sensor_integer_complete_set_config_data(sen,invocation);
Norman Jamescc7ae122015-08-24 14:26:09 -050052 return TRUE;
53}
54
Norman James3f97c5d2015-08-26 17:44:14 -050055static gboolean
56on_set_thresholds (SensorInteger *sen,
57 GDBusMethodInvocation *invocation,
58 guint lc,
59 guint lw,
60 guint uw,
61 guint uc,
62 gpointer user_data)
63{
64 sensor_integer_set_threshold_lower_critical(sen,lc);
65 sensor_integer_set_threshold_lower_warning(sen,lw);
66 sensor_integer_set_threshold_upper_warning(sen,uw);
67 sensor_integer_set_threshold_upper_critical(sen,uc);
68 sensor_integer_complete_set_thresholds(sen,invocation);
69 thresholds_set = TRUE;
70 return TRUE;
71}
72
73static gboolean
74on_get_threshold_state (SensorInteger *sen,
75 GDBusMethodInvocation *invocation,
76 gpointer user_data)
77{
78 guint state = sensor_integer_get_threshold_state(sen);
79 sensor_integer_complete_get_threshold_state(sen,invocation,state);
80 return TRUE;
81}
82
83
84static gboolean
85check_thresholds()
86{
87 if (thresholds_set == TRUE) {
88 threshold_states state = NORMAL;
89 guint value = sensor_integer_get_value(sensor);
90
91 if (value < sensor_integer_get_threshold_lower_critical(sensor)) {
92 state = LOWER_CRITICAL;
93 }
94 else if(value < sensor_integer_get_threshold_lower_warning(sensor)) {
95 state = LOWER_WARNING;
96 }
97 else if(value > sensor_integer_get_threshold_upper_critical(sensor)) {
98 state = UPPER_CRITICAL;
99 }
100 else if(value > sensor_integer_get_threshold_upper_warning(sensor)) {
101 state = UPPER_WARNING;
102 }
103 // only emit signal if threshold state changes
104 if (state != sensor_integer_get_threshold_state(sensor))
105 {
106 sensor_integer_set_threshold_state(sensor,state);
107 if (state == LOWER_CRITICAL || state == UPPER_CRITICAL)
108 {
109 sensor_integer_emit_critical(sensor);
110 g_print("Critical\n");
111 }
112 else if (state == LOWER_WARNING || state == UPPER_WARNING)
113 {
114 sensor_integer_emit_warning(sensor);
115 g_print("Warning\n");
116 }
117 }
118 }
119}
Norman Jamescc7ae122015-08-24 14:26:09 -0500120
Norman Jamese2765102015-08-19 22:00:55 -0500121static void
122on_bus_acquired (GDBusConnection *connection,
123 const gchar *name,
124 gpointer user_data)
125{
126 ObjectSkeleton *object;
127 guint n;
128
129 g_print ("Acquired a message bus connection: %s\n",name);
130
Norman James26072c02015-08-25 07:14:29 -0500131 manager = g_dbus_object_manager_server_new (dbus_object_path);
Norman Jamese2765102015-08-19 22:00:55 -0500132
133 gchar *s;
Norman James26072c02015-08-25 07:14:29 -0500134 s = g_strdup_printf ("%s/0",dbus_object_path);
Norman Jamese2765102015-08-19 22:00:55 -0500135 object = object_skeleton_new (s);
136 g_free (s);
137
138 sensor = sensor_integer_skeleton_new ();
139 object_skeleton_set_sensor_integer (object, sensor);
140 g_object_unref (sensor);
141
142 sensor_integer_set_units(sensor,"C");
143 //define method callbacks here
144 g_signal_connect (sensor,
145 "handle-get-value",
146 G_CALLBACK (on_get),
147 NULL); /* user_data */
148 g_signal_connect (sensor,
149 "handle-get-units",
150 G_CALLBACK (on_get_units),
151 NULL); /* user_data */
152
Norman Jamescc7ae122015-08-24 14:26:09 -0500153 g_signal_connect (sensor,
154 "handle-set-config-data",
155 G_CALLBACK (on_set_config),
156 NULL); /* user_data */
Norman James3f97c5d2015-08-26 17:44:14 -0500157
158 g_signal_connect (sensor,
159 "handle-set-thresholds",
160 G_CALLBACK (on_set_thresholds),
161 NULL); /* user_data */
Norman Jamescc7ae122015-08-24 14:26:09 -0500162
Norman James3f97c5d2015-08-26 17:44:14 -0500163 g_signal_connect (sensor,
164 "handle-get-threshold-state",
165 G_CALLBACK (on_get_threshold_state),
166 NULL); /* user_data */
Norman Jamescc7ae122015-08-24 14:26:09 -0500167
Norman Jamese2765102015-08-19 22:00:55 -0500168
169 /* Export the object (@manager takes its own reference to @object) */
170 g_dbus_object_manager_server_export (manager, G_DBUS_OBJECT_SKELETON (object));
171 g_object_unref (object);
172
173 /* Export all objects */
174 g_dbus_object_manager_server_set_connection (manager, connection);
175}
176
177static void
178on_name_acquired (GDBusConnection *connection,
179 const gchar *name,
180 gpointer user_data)
181{
182 g_print ("Acquired the name %s\n", name);
183}
184
185static void
186on_name_lost (GDBusConnection *connection,
187 const gchar *name,
188 gpointer user_data)
189{
190 g_print ("Lost the name %s\n", name);
191}
192
193static gboolean
194poll_sensor()
195{
Norman Jamescc7ae122015-08-24 14:26:09 -0500196 guint value = sensor_integer_get_value(sensor);
197 //TOOD: Change to actually read sensor
198 value = value+1;
199 g_print("Polling sensor: %d\n",value);
200
201 //if changed, set property and emit signal
202 if (value != sensor_integer_get_value(sensor))
203 {
Norman James3f97c5d2015-08-26 17:44:14 -0500204 g_print("Sensor changed\n");
Norman Jamescc7ae122015-08-24 14:26:09 -0500205 sensor_integer_set_value(sensor,value);
206 sensor_integer_emit_changed(sensor,value);
Norman James3f97c5d2015-08-26 17:44:14 -0500207 if (thresholds_set == TRUE)
208 {
209 check_thresholds();
210 }
Norman Jamescc7ae122015-08-24 14:26:09 -0500211 }
Norman Jamescc7ae122015-08-24 14:26:09 -0500212 return TRUE;
Norman Jamese2765102015-08-19 22:00:55 -0500213}
214
215gint
216main (gint argc, gchar *argv[])
217{
218 GMainLoop *loop;
219
220 guint id;
221 //g_type_init ();
222 loop = g_main_loop_new (NULL, FALSE);
223
224 id = g_bus_own_name (G_BUS_TYPE_SESSION,
Norman James26072c02015-08-25 07:14:29 -0500225 dbus_name,
Norman Jamese2765102015-08-19 22:00:55 -0500226 G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT |
227 G_BUS_NAME_OWNER_FLAGS_REPLACE,
228 on_bus_acquired,
229 on_name_acquired,
230 on_name_lost,
231 loop,
232 NULL);
233
234 g_timeout_add(5000, poll_sensor, NULL);
235 g_main_loop_run (loop);
236
237 g_bus_unown_name (id);
238 g_main_loop_unref (loop);
239 return 0;
240}