Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 1 | #include "interfaces/sensor.h"
|
| 2 |
|
Norman James | 3f97c5d | 2015-08-26 17:44:14 -0500 | [diff] [blame^] | 3 |
|
| 4 |
|
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 5 | /* ---------------------------------------------------------------------------------------------------- */
|
Norman James | 3f97c5d | 2015-08-26 17:44:14 -0500 | [diff] [blame^] | 6 | typedef enum { NORMAL,LOWER_CRITICAL,LOWER_WARNING,UPPER_WARNING,UPPER_CRITICAL } threshold_states;
|
| 7 |
|
| 8 |
|
Norman James | 26072c0 | 2015-08-25 07:14:29 -0500 | [diff] [blame] | 9 | static const gchar* dbus_object_path = "/org/openbmc/sensors/Temperature/Ambient";
|
| 10 | static const gchar* dbus_name = "org.openbmc.sensors.Temperature.Ambient";
|
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 11 |
|
Norman James | 3f97c5d | 2015-08-26 17:44:14 -0500 | [diff] [blame^] | 12 |
|
| 13 |
|
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 14 | static GDBusObjectManagerServer *manager = NULL;
|
| 15 | static SensorInteger *sensor = NULL;
|
| 16 |
|
Norman James | cc7ae12 | 2015-08-24 14:26:09 -0500 | [diff] [blame] | 17 | static gchar* i2c_bus = "";
|
| 18 | static gchar* i2c_address = "";
|
Norman James | 3f97c5d | 2015-08-26 17:44:14 -0500 | [diff] [blame^] | 19 | static gboolean thresholds_set = FALSE;
|
| 20 |
|
| 21 |
|
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 22 |
|
| 23 | static gboolean
|
| 24 | on_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 |
|
| 33 | static gboolean
|
| 34 | on_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 James | cc7ae12 | 2015-08-24 14:26:09 -0500 | [diff] [blame] | 43 | static gboolean
|
| 44 | on_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 James | cc7ae12 | 2015-08-24 14:26:09 -0500 | [diff] [blame] | 52 | return TRUE;
|
| 53 | }
|
| 54 |
|
Norman James | 3f97c5d | 2015-08-26 17:44:14 -0500 | [diff] [blame^] | 55 | static gboolean
|
| 56 | on_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 |
|
| 73 | static gboolean
|
| 74 | on_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 |
|
| 84 | static gboolean
|
| 85 | check_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 James | cc7ae12 | 2015-08-24 14:26:09 -0500 | [diff] [blame] | 120 |
|
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 121 | static void
|
| 122 | on_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 James | 26072c0 | 2015-08-25 07:14:29 -0500 | [diff] [blame] | 131 | manager = g_dbus_object_manager_server_new (dbus_object_path);
|
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 132 |
|
| 133 | gchar *s;
|
Norman James | 26072c0 | 2015-08-25 07:14:29 -0500 | [diff] [blame] | 134 | s = g_strdup_printf ("%s/0",dbus_object_path);
|
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 135 | 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 James | cc7ae12 | 2015-08-24 14:26:09 -0500 | [diff] [blame] | 153 | g_signal_connect (sensor,
|
| 154 | "handle-set-config-data",
|
| 155 | G_CALLBACK (on_set_config),
|
| 156 | NULL); /* user_data */
|
Norman James | 3f97c5d | 2015-08-26 17:44:14 -0500 | [diff] [blame^] | 157 |
|
| 158 | g_signal_connect (sensor,
|
| 159 | "handle-set-thresholds",
|
| 160 | G_CALLBACK (on_set_thresholds),
|
| 161 | NULL); /* user_data */
|
Norman James | cc7ae12 | 2015-08-24 14:26:09 -0500 | [diff] [blame] | 162 |
|
Norman James | 3f97c5d | 2015-08-26 17:44:14 -0500 | [diff] [blame^] | 163 | g_signal_connect (sensor,
|
| 164 | "handle-get-threshold-state",
|
| 165 | G_CALLBACK (on_get_threshold_state),
|
| 166 | NULL); /* user_data */
|
Norman James | cc7ae12 | 2015-08-24 14:26:09 -0500 | [diff] [blame] | 167 |
|
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 168 |
|
| 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 |
|
| 177 | static void
|
| 178 | on_name_acquired (GDBusConnection *connection,
|
| 179 | const gchar *name,
|
| 180 | gpointer user_data)
|
| 181 | {
|
| 182 | g_print ("Acquired the name %s\n", name);
|
| 183 | }
|
| 184 |
|
| 185 | static void
|
| 186 | on_name_lost (GDBusConnection *connection,
|
| 187 | const gchar *name,
|
| 188 | gpointer user_data)
|
| 189 | {
|
| 190 | g_print ("Lost the name %s\n", name);
|
| 191 | }
|
| 192 |
|
| 193 | static gboolean
|
| 194 | poll_sensor()
|
| 195 | {
|
Norman James | cc7ae12 | 2015-08-24 14:26:09 -0500 | [diff] [blame] | 196 | 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 James | 3f97c5d | 2015-08-26 17:44:14 -0500 | [diff] [blame^] | 204 | g_print("Sensor changed\n");
|
Norman James | cc7ae12 | 2015-08-24 14:26:09 -0500 | [diff] [blame] | 205 | sensor_integer_set_value(sensor,value);
|
| 206 | sensor_integer_emit_changed(sensor,value);
|
Norman James | 3f97c5d | 2015-08-26 17:44:14 -0500 | [diff] [blame^] | 207 | if (thresholds_set == TRUE)
|
| 208 | {
|
| 209 | check_thresholds();
|
| 210 | }
|
Norman James | cc7ae12 | 2015-08-24 14:26:09 -0500 | [diff] [blame] | 211 | }
|
Norman James | cc7ae12 | 2015-08-24 14:26:09 -0500 | [diff] [blame] | 212 | return TRUE;
|
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 213 | }
|
| 214 |
|
| 215 | gint
|
| 216 | main (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 James | 26072c0 | 2015-08-25 07:14:29 -0500 | [diff] [blame] | 225 | dbus_name,
|
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 226 | 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 | }
|