blob: 15e5f672e923dcde037c5036d89b47f8983395cf [file] [log] [blame]
Norman James10ff6a32015-08-27 14:24:17 -05001#include "interfaces/sensor2.h"
2#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 James26072c02015-08-25 07:14:29 -05008static const gchar* dbus_object_path = "/org/openbmc/sensors/Temperature/Ambient";
9static const gchar* dbus_name = "org.openbmc.sensors.Temperature.Ambient";
Norman James10ff6a32015-08-27 14:24:17 -050010static const guint poll_interval = 3000;
11static guint heartbeat = 0;
Norman James3f97c5d2015-08-26 17:44:14 -050012
Norman Jamese2765102015-08-19 22:00:55 -050013static GDBusObjectManagerServer *manager = NULL;
Norman Jamese2765102015-08-19 22:00:55 -050014
Norman Jamescc7ae122015-08-24 14:26:09 -050015static gchar* i2c_bus = "";
16static gchar* i2c_address = "";
Norman James3f97c5d2015-08-26 17:44:14 -050017
Norman James10ff6a32015-08-27 14:24:17 -050018static gboolean
19poll_sensor(gpointer user_data)
20{
21 SensorInteger *sensor = object_get_sensor_integer((Object*)user_data);
22 SensorIntegerThreshold *threshold = object_get_sensor_integer_threshold((Object*)user_data);
23
24 guint value = sensor_integer_get_value(sensor);
25 //TOOD: Change to actually read sensor
26 value = value+1;
27
Norman James90caa3c2015-08-27 21:28:48 -050028 if (heartbeat > 30000)
Norman James10ff6a32015-08-27 14:24:17 -050029 {
30 heartbeat = 0;
Norman James90caa3c2015-08-27 21:28:48 -050031 sensor_integer_emit_heartbeat(sensor,dbus_name);
Norman James10ff6a32015-08-27 14:24:17 -050032 }
33 else
34 {
35 heartbeat = heartbeat+poll_interval;
36 }
37
38 // End actually reading sensor
Norman James10ff6a32015-08-27 14:24:17 -050039
40 //if changed, set property and emit signal
41 if (value != sensor_integer_get_value(sensor))
42 {
Norman James90caa3c2015-08-27 21:28:48 -050043 //g_print("Sensor changed\n");
Norman James10ff6a32015-08-27 14:24:17 -050044 sensor_integer_set_value(sensor,value);
45 sensor_integer_emit_changed(sensor,value);
46 check_thresholds(threshold,value);
47 }
48 return TRUE;
49}
Norman James3f97c5d2015-08-26 17:44:14 -050050
Norman Jamese2765102015-08-19 22:00:55 -050051
52static gboolean
53on_get_units (SensorInteger *sen,
54 GDBusMethodInvocation *invocation,
55 gpointer user_data)
56{
57 const gchar* val = sensor_integer_get_units(sen);
58 sensor_integer_complete_get_units(sen,invocation,val);
59 return TRUE;
60}
61
62static gboolean
63on_get (SensorInteger *sen,
64 GDBusMethodInvocation *invocation,
65 gpointer user_data)
66{
67 guint reading = sensor_integer_get_value(sen);
68 sensor_integer_complete_get_value(sen,invocation,reading);
69 return TRUE;
70}
71
Norman Jamescc7ae122015-08-24 14:26:09 -050072static gboolean
73on_set_config (SensorInteger *sen,
74 GDBusMethodInvocation *invocation,
75 gchar** config,
76 gpointer user_data)
77{
78 g_print("I2C bus = %s\n",config[0]);
79 g_print("I2C addr = %s\n",config[1]);
80 sensor_integer_complete_set_config_data(sen,invocation);
Norman Jamescc7ae122015-08-24 14:26:09 -050081 return TRUE;
82}
83
84
Norman Jamese2765102015-08-19 22:00:55 -050085static void
86on_bus_acquired (GDBusConnection *connection,
87 const gchar *name,
88 gpointer user_data)
89{
Norman James10ff6a32015-08-27 14:24:17 -050090 g_print ("Acquired a message bus connection: %s\n",name);
Norman Jamese2765102015-08-19 22:00:55 -050091
Norman James10ff6a32015-08-27 14:24:17 -050092 cmdline *cmd = user_data;
93 if (cmd->argc < 2)
94 {
95 g_print("No objects created. Put object name(s) on command line\n");
96 return;
97 }
98 manager = g_dbus_object_manager_server_new (dbus_object_path);
99 int i=0;
100 for (i=1;i<cmd->argc;i++)
101 {
102 gchar *s;
103 s = g_strdup_printf ("%s/%s",dbus_object_path,cmd->argv[i]);
104 ObjectSkeleton *object = object_skeleton_new (s);
105 g_free (s);
Norman Jamese2765102015-08-19 22:00:55 -0500106
Norman James10ff6a32015-08-27 14:24:17 -0500107 SensorInteger *sensor = sensor_integer_skeleton_new ();
108 object_skeleton_set_sensor_integer (object, sensor);
109 g_object_unref (sensor);
110
111 SensorIntegerThreshold *threshold = sensor_integer_threshold_skeleton_new();
112 object_skeleton_set_sensor_integer_threshold (object,threshold);
113 g_object_unref (threshold);
Norman Jamese2765102015-08-19 22:00:55 -0500114
Norman James10ff6a32015-08-27 14:24:17 -0500115 // set units
116 sensor_integer_set_units(sensor,"C");
117 sensor_integer_threshold_set_state(threshold,NOT_SET);
118 //define method callbacks here
119 g_signal_connect (sensor,
Norman Jamese2765102015-08-19 22:00:55 -0500120 "handle-get-value",
121 G_CALLBACK (on_get),
122 NULL); /* user_data */
Norman James10ff6a32015-08-27 14:24:17 -0500123 g_signal_connect (sensor,
Norman Jamese2765102015-08-19 22:00:55 -0500124 "handle-get-units",
125 G_CALLBACK (on_get_units),
126 NULL); /* user_data */
127
Norman James10ff6a32015-08-27 14:24:17 -0500128 g_signal_connect (sensor,
Norman Jamescc7ae122015-08-24 14:26:09 -0500129 "handle-set-config-data",
130 G_CALLBACK (on_set_config),
131 NULL); /* user_data */
Norman James3f97c5d2015-08-26 17:44:14 -0500132
Norman James10ff6a32015-08-27 14:24:17 -0500133 g_signal_connect (threshold,
134 "handle-set",
135 G_CALLBACK (set_thresholds),
Norman James3f97c5d2015-08-26 17:44:14 -0500136 NULL); /* user_data */
Norman Jamescc7ae122015-08-24 14:26:09 -0500137
Norman James10ff6a32015-08-27 14:24:17 -0500138 g_signal_connect (threshold,
139 "handle-get-state",
140 G_CALLBACK (get_threshold_state),
Norman James3f97c5d2015-08-26 17:44:14 -0500141 NULL); /* user_data */
Norman Jamescc7ae122015-08-24 14:26:09 -0500142
Norman James10ff6a32015-08-27 14:24:17 -0500143 g_timeout_add(poll_interval, poll_sensor, object);
Norman Jamese2765102015-08-19 22:00:55 -0500144
Norman James10ff6a32015-08-27 14:24:17 -0500145 /* Export the object (@manager takes its own reference to @object) */
146 g_dbus_object_manager_server_export (manager, G_DBUS_OBJECT_SKELETON (object));
147 g_object_unref (object);
148 }
Norman Jamese2765102015-08-19 22:00:55 -0500149
150 /* Export all objects */
151 g_dbus_object_manager_server_set_connection (manager, connection);
152}
153
154static void
155on_name_acquired (GDBusConnection *connection,
156 const gchar *name,
157 gpointer user_data)
158{
159 g_print ("Acquired the name %s\n", name);
160}
161
162static void
163on_name_lost (GDBusConnection *connection,
164 const gchar *name,
165 gpointer user_data)
166{
167 g_print ("Lost the name %s\n", name);
168}
169
Norman Jamese2765102015-08-19 22:00:55 -0500170
171gint
172main (gint argc, gchar *argv[])
173{
174 GMainLoop *loop;
Norman James10ff6a32015-08-27 14:24:17 -0500175 cmdline cmd;
176 cmd.argc = argc;
177 cmd.argv = argv;
Norman Jamese2765102015-08-19 22:00:55 -0500178 guint id;
Norman Jamese2765102015-08-19 22:00:55 -0500179 loop = g_main_loop_new (NULL, FALSE);
180
181 id = g_bus_own_name (G_BUS_TYPE_SESSION,
Norman James26072c02015-08-25 07:14:29 -0500182 dbus_name,
Norman Jamese2765102015-08-19 22:00:55 -0500183 G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT |
184 G_BUS_NAME_OWNER_FLAGS_REPLACE,
185 on_bus_acquired,
186 on_name_acquired,
187 on_name_lost,
Norman James10ff6a32015-08-27 14:24:17 -0500188 &cmd,
Norman Jamese2765102015-08-19 22:00:55 -0500189 NULL);
190
Norman Jamese2765102015-08-19 22:00:55 -0500191 g_main_loop_run (loop);
192
193 g_bus_unown_name (id);
194 g_main_loop_unref (loop);
195 return 0;
196}