Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 1 | #include "interfaces/openbmc_intf.h"
|
Norman James | 10ff6a3 | 2015-08-27 14:24:17 -0500 | [diff] [blame] | 2 | #include "openbmc.h"
|
| 3 | #include "sensor_threshold.h"
|
Norman James | 3f97c5d | 2015-08-26 17:44:14 -0500 | [diff] [blame] | 4 |
|
| 5 |
|
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 6 | /* ---------------------------------------------------------------------------------------------------- */
|
Norman James | 3f97c5d | 2015-08-26 17:44:14 -0500 | [diff] [blame] | 7 |
|
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 8 | static const gchar* dbus_object_path = "/org/openbmc/sensors";
|
Norman James | 26072c0 | 2015-08-25 07:14:29 -0500 | [diff] [blame] | 9 | static const gchar* dbus_name = "org.openbmc.sensors.Temperature.Ambient";
|
Norman James | 10ff6a3 | 2015-08-27 14:24:17 -0500 | [diff] [blame] | 10 | static guint heartbeat = 0;
|
Norman James | 3f97c5d | 2015-08-26 17:44:14 -0500 | [diff] [blame] | 11 |
|
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 12 | static GDBusObjectManagerServer *manager = NULL;
|
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 13 |
|
Norman James | 10ff6a3 | 2015-08-27 14:24:17 -0500 | [diff] [blame] | 14 | static gboolean
|
| 15 | poll_sensor(gpointer user_data)
|
| 16 | {
|
Norman James | 5d78b4d | 2015-09-05 13:34:34 -0500 | [diff] [blame] | 17 | SensorValue *sensor = object_get_sensor_value((Object*)user_data);
|
| 18 | SensorThreshold *threshold = object_get_sensor_threshold((Object*)user_data);
|
Norman James | 90baede | 2015-09-02 20:32:49 -0500 | [diff] [blame] | 19 | SensorI2c *i2c = object_get_sensor_i2c((Object*)user_data);
|
| 20 |
|
Norman James | 5d78b4d | 2015-09-05 13:34:34 -0500 | [diff] [blame] | 21 | GVariant* v_value = sensor_value_get_value(sensor);
|
Norman James | 9e6acf9 | 2015-09-08 07:00:04 -0500 | [diff] [blame] | 22 | guint poll_interval = sensor_value_get_poll_interval(sensor);
|
| 23 |
|
Norman James | 5d78b4d | 2015-09-05 13:34:34 -0500 | [diff] [blame] | 24 | //TODO: Change to actually read sensor
|
| 25 | double value = GET_VARIANT_D(v_value);
|
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 26 | //g_print("Reading I2C = %s; Address = %s; %f\n",
|
| 27 | // sensor_i2c_get_dev_path(i2c),sensor_i2c_get_address(i2c),value);
|
Norman James | 5d78b4d | 2015-09-05 13:34:34 -0500 | [diff] [blame] | 28 |
|
Norman James | 10ff6a3 | 2015-08-27 14:24:17 -0500 | [diff] [blame] | 29 | value = value+1;
|
Norman James | 32e74e2 | 2015-09-15 21:28:06 -0500 | [diff] [blame] | 30 | // Do this in case of an error
|
| 31 | //sensor_value_emit_error(sensor);
|
Norman James | 5d78b4d | 2015-09-05 13:34:34 -0500 | [diff] [blame] | 32 |
|
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 33 | if (heartbeat > 4000)
|
Norman James | 10ff6a3 | 2015-08-27 14:24:17 -0500 | [diff] [blame] | 34 | {
|
| 35 | heartbeat = 0;
|
Norman James | 5d78b4d | 2015-09-05 13:34:34 -0500 | [diff] [blame] | 36 | sensor_value_emit_heartbeat(sensor,dbus_name);
|
Norman James | 10ff6a3 | 2015-08-27 14:24:17 -0500 | [diff] [blame] | 37 | }
|
| 38 | else
|
| 39 | {
|
| 40 | heartbeat = heartbeat+poll_interval;
|
| 41 | }
|
| 42 |
|
| 43 | // End actually reading sensor
|
Norman James | 10ff6a3 | 2015-08-27 14:24:17 -0500 | [diff] [blame] | 44 |
|
| 45 | //if changed, set property and emit signal
|
Norman James | 5d78b4d | 2015-09-05 13:34:34 -0500 | [diff] [blame] | 46 | if (value != GET_VARIANT_D(v_value))
|
Norman James | 10ff6a3 | 2015-08-27 14:24:17 -0500 | [diff] [blame] | 47 | {
|
Norman James | 5d78b4d | 2015-09-05 13:34:34 -0500 | [diff] [blame] | 48 | GVariant* v_new_value = NEW_VARIANT_D(value);
|
| 49 | sensor_value_set_value(sensor,v_new_value);
|
Norman James | 9e6acf9 | 2015-09-08 07:00:04 -0500 | [diff] [blame] | 50 | 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 James | 10ff6a3 | 2015-08-27 14:24:17 -0500 | [diff] [blame] | 53 | }
|
| 54 | return TRUE;
|
| 55 | }
|
Norman James | 3f97c5d | 2015-08-26 17:44:14 -0500 | [diff] [blame] | 56 |
|
Norman James | ce46e3e | 2015-08-30 22:25:55 -0500 | [diff] [blame] | 57 | static gboolean
|
Norman James | 5d78b4d | 2015-09-05 13:34:34 -0500 | [diff] [blame] | 58 | on_init (SensorValue *sen,
|
Norman James | ce46e3e | 2015-08-30 22:25:55 -0500 | [diff] [blame] | 59 | GDBusMethodInvocation *invocation,
|
| 60 | gpointer user_data)
|
| 61 | {
|
Norman James | 9e6acf9 | 2015-09-08 07:00:04 -0500 | [diff] [blame] | 62 |
|
| 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 James | ce46e3e | 2015-08-30 22:25:55 -0500 | [diff] [blame] | 67 | }
|
| 68 |
|
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 69 |
|
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 70 |
|
| 71 | static void
|
| 72 | on_bus_acquired (GDBusConnection *connection,
|
| 73 | const gchar *name,
|
| 74 | gpointer user_data)
|
| 75 | {
|
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 76 | // g_print ("Acquired a message bus connection: %s\n",name);
|
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 77 |
|
Norman James | 10ff6a3 | 2015-08-27 14:24:17 -0500 | [diff] [blame] | 78 | 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 James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 89 | s = g_strdup_printf ("%s/Temperature/%s",dbus_object_path,cmd->argv[i]);
|
Norman James | 10ff6a3 | 2015-08-27 14:24:17 -0500 | [diff] [blame] | 90 | ObjectSkeleton *object = object_skeleton_new (s);
|
| 91 | g_free (s);
|
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 92 |
|
Norman James | 5d78b4d | 2015-09-05 13:34:34 -0500 | [diff] [blame] | 93 | SensorValue *sensor = sensor_value_skeleton_new ();
|
| 94 | object_skeleton_set_sensor_value (object, sensor);
|
Norman James | 10ff6a3 | 2015-08-27 14:24:17 -0500 | [diff] [blame] | 95 | g_object_unref (sensor);
|
| 96 |
|
Norman James | 5d78b4d | 2015-09-05 13:34:34 -0500 | [diff] [blame] | 97 | SensorThreshold *threshold = sensor_threshold_skeleton_new();
|
| 98 | object_skeleton_set_sensor_threshold (object,threshold);
|
Norman James | 10ff6a3 | 2015-08-27 14:24:17 -0500 | [diff] [blame] | 99 | g_object_unref (threshold);
|
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 100 |
|
Norman James | 90baede | 2015-09-02 20:32:49 -0500 | [diff] [blame] | 101 | SensorI2c *i2c = sensor_i2c_skeleton_new();
|
| 102 | object_skeleton_set_sensor_i2c (object,i2c);
|
| 103 | g_object_unref (i2c);
|
| 104 |
|
| 105 |
|
Norman James | 5d78b4d | 2015-09-05 13:34:34 -0500 | [diff] [blame] | 106 | // 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 James | 9e6acf9 | 2015-09-08 07:00:04 -0500 | [diff] [blame] | 110 | sensor_value_set_settable(sensor,FALSE);
|
Norman James | 5d78b4d | 2015-09-05 13:34:34 -0500 | [diff] [blame] | 111 | 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 James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 123 |
|
Norman James | ce46e3e | 2015-08-30 22:25:55 -0500 | [diff] [blame] | 124 | g_signal_connect (sensor,
|
| 125 | "handle-init",
|
| 126 | G_CALLBACK (on_init),
|
Norman James | 9e6acf9 | 2015-09-08 07:00:04 -0500 | [diff] [blame] | 127 | object); /* user_data */
|
Norman James | 3f97c5d | 2015-08-26 17:44:14 -0500 | [diff] [blame] | 128 |
|
Norman James | 10ff6a3 | 2015-08-27 14:24:17 -0500 | [diff] [blame] | 129 | g_signal_connect (threshold,
|
Norman James | 10ff6a3 | 2015-08-27 14:24:17 -0500 | [diff] [blame] | 130 | "handle-get-state",
|
| 131 | G_CALLBACK (get_threshold_state),
|
Norman James | 3f97c5d | 2015-08-26 17:44:14 -0500 | [diff] [blame] | 132 | NULL); /* user_data */
|
Norman James | cc7ae12 | 2015-08-24 14:26:09 -0500 | [diff] [blame] | 133 |
|
Norman James | 9e6acf9 | 2015-09-08 07:00:04 -0500 | [diff] [blame] | 134 |
|
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 135 |
|
Norman James | 10ff6a3 | 2015-08-27 14:24:17 -0500 | [diff] [blame] | 136 | /* 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 James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 140 |
|
| 141 | /* Export all objects */
|
| 142 | g_dbus_object_manager_server_set_connection (manager, connection);
|
| 143 | }
|
| 144 |
|
| 145 | static void
|
| 146 | on_name_acquired (GDBusConnection *connection,
|
| 147 | const gchar *name,
|
| 148 | gpointer user_data)
|
| 149 | {
|
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 150 | // g_print ("Acquired the name %s\n", name);
|
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 151 | }
|
| 152 |
|
| 153 | static void
|
| 154 | on_name_lost (GDBusConnection *connection,
|
| 155 | const gchar *name,
|
| 156 | gpointer user_data)
|
| 157 | {
|
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 158 | // g_print ("Lost the name %s\n", name);
|
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 159 | }
|
| 160 |
|
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 161 |
|
| 162 | gint
|
| 163 | main (gint argc, gchar *argv[])
|
| 164 | {
|
| 165 | GMainLoop *loop;
|
Norman James | 10ff6a3 | 2015-08-27 14:24:17 -0500 | [diff] [blame] | 166 | cmdline cmd;
|
| 167 | cmd.argc = argc;
|
| 168 | cmd.argv = argv;
|
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 169 | guint id;
|
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 170 | loop = g_main_loop_new (NULL, FALSE);
|
| 171 |
|
Norman James | 5e792e3 | 2015-10-07 17:36:17 -0500 | [diff] [blame] | 172 | id = g_bus_own_name (DBUS_TYPE,
|
Norman James | 26072c0 | 2015-08-25 07:14:29 -0500 | [diff] [blame] | 173 | dbus_name,
|
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 174 | 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 James | 10ff6a3 | 2015-08-27 14:24:17 -0500 | [diff] [blame] | 179 | &cmd,
|
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 180 | NULL);
|
| 181 |
|
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 182 | g_main_loop_run (loop);
|
| 183 |
|
| 184 | g_bus_unown_name (id);
|
| 185 | g_main_loop_unref (loop);
|
| 186 | return 0;
|
| 187 | }
|