Norman James | 10ff6a3 | 2015-08-27 14:24:17 -0500 | [diff] [blame] | 1 | #include "interfaces/sensor2.h"
|
| 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 | 26072c0 | 2015-08-25 07:14:29 -0500 | [diff] [blame] | 8 | static const gchar* dbus_object_path = "/org/openbmc/sensors/Temperature/Ambient";
|
| 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 const guint poll_interval = 3000;
|
| 11 | static guint heartbeat = 0;
|
Norman James | 3f97c5d | 2015-08-26 17:44:14 -0500 | [diff] [blame] | 12 |
|
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 13 | static GDBusObjectManagerServer *manager = NULL;
|
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 14 |
|
Norman James | cc7ae12 | 2015-08-24 14:26:09 -0500 | [diff] [blame] | 15 | static gchar* i2c_bus = "";
|
| 16 | static gchar* i2c_address = "";
|
Norman James | 3f97c5d | 2015-08-26 17:44:14 -0500 | [diff] [blame] | 17 |
|
Norman James | 10ff6a3 | 2015-08-27 14:24:17 -0500 | [diff] [blame] | 18 | static gboolean
|
| 19 | poll_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 James | 90caa3c | 2015-08-27 21:28:48 -0500 | [diff] [blame] | 28 | if (heartbeat > 30000)
|
Norman James | 10ff6a3 | 2015-08-27 14:24:17 -0500 | [diff] [blame] | 29 | {
|
| 30 | heartbeat = 0;
|
Norman James | 90caa3c | 2015-08-27 21:28:48 -0500 | [diff] [blame] | 31 | sensor_integer_emit_heartbeat(sensor,dbus_name);
|
Norman James | 10ff6a3 | 2015-08-27 14:24:17 -0500 | [diff] [blame] | 32 | }
|
| 33 | else
|
| 34 | {
|
| 35 | heartbeat = heartbeat+poll_interval;
|
| 36 | }
|
| 37 |
|
| 38 | // End actually reading sensor
|
Norman James | 10ff6a3 | 2015-08-27 14:24:17 -0500 | [diff] [blame] | 39 |
|
| 40 | //if changed, set property and emit signal
|
| 41 | if (value != sensor_integer_get_value(sensor))
|
| 42 | {
|
Norman James | 90caa3c | 2015-08-27 21:28:48 -0500 | [diff] [blame] | 43 | //g_print("Sensor changed\n");
|
Norman James | 10ff6a3 | 2015-08-27 14:24:17 -0500 | [diff] [blame] | 44 | sensor_integer_set_value(sensor,value);
|
| 45 | sensor_integer_emit_changed(sensor,value);
|
| 46 | check_thresholds(threshold,value);
|
| 47 | }
|
| 48 | return TRUE;
|
| 49 | }
|
Norman James | 3f97c5d | 2015-08-26 17:44:14 -0500 | [diff] [blame] | 50 |
|
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 51 |
|
| 52 | static gboolean
|
| 53 | on_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 |
|
| 62 | static gboolean
|
| 63 | on_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 James | cc7ae12 | 2015-08-24 14:26:09 -0500 | [diff] [blame] | 72 | static gboolean
|
| 73 | on_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 James | cc7ae12 | 2015-08-24 14:26:09 -0500 | [diff] [blame] | 81 | return TRUE;
|
| 82 | }
|
| 83 |
|
| 84 |
|
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 85 | static void
|
| 86 | on_bus_acquired (GDBusConnection *connection,
|
| 87 | const gchar *name,
|
| 88 | gpointer user_data)
|
| 89 | {
|
Norman James | 10ff6a3 | 2015-08-27 14:24:17 -0500 | [diff] [blame] | 90 | g_print ("Acquired a message bus connection: %s\n",name);
|
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 91 |
|
Norman James | 10ff6a3 | 2015-08-27 14:24:17 -0500 | [diff] [blame] | 92 | 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 James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 106 |
|
Norman James | 10ff6a3 | 2015-08-27 14:24:17 -0500 | [diff] [blame] | 107 | 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 James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 114 |
|
Norman James | 10ff6a3 | 2015-08-27 14:24:17 -0500 | [diff] [blame] | 115 | // 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 James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 120 | "handle-get-value",
|
| 121 | G_CALLBACK (on_get),
|
| 122 | NULL); /* user_data */
|
Norman James | 10ff6a3 | 2015-08-27 14:24:17 -0500 | [diff] [blame] | 123 | g_signal_connect (sensor,
|
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 124 | "handle-get-units",
|
| 125 | G_CALLBACK (on_get_units),
|
| 126 | NULL); /* user_data */
|
| 127 |
|
Norman James | 10ff6a3 | 2015-08-27 14:24:17 -0500 | [diff] [blame] | 128 | g_signal_connect (sensor,
|
Norman James | cc7ae12 | 2015-08-24 14:26:09 -0500 | [diff] [blame] | 129 | "handle-set-config-data",
|
| 130 | G_CALLBACK (on_set_config),
|
| 131 | NULL); /* user_data */
|
Norman James | 3f97c5d | 2015-08-26 17:44:14 -0500 | [diff] [blame] | 132 |
|
Norman James | 10ff6a3 | 2015-08-27 14:24:17 -0500 | [diff] [blame] | 133 | g_signal_connect (threshold,
|
| 134 | "handle-set",
|
| 135 | G_CALLBACK (set_thresholds),
|
Norman James | 3f97c5d | 2015-08-26 17:44:14 -0500 | [diff] [blame] | 136 | NULL); /* user_data */
|
Norman James | cc7ae12 | 2015-08-24 14:26:09 -0500 | [diff] [blame] | 137 |
|
Norman James | 10ff6a3 | 2015-08-27 14:24:17 -0500 | [diff] [blame] | 138 | g_signal_connect (threshold,
|
| 139 | "handle-get-state",
|
| 140 | G_CALLBACK (get_threshold_state),
|
Norman James | 3f97c5d | 2015-08-26 17:44:14 -0500 | [diff] [blame] | 141 | NULL); /* user_data */
|
Norman James | cc7ae12 | 2015-08-24 14:26:09 -0500 | [diff] [blame] | 142 |
|
Norman James | 10ff6a3 | 2015-08-27 14:24:17 -0500 | [diff] [blame] | 143 | g_timeout_add(poll_interval, poll_sensor, object);
|
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 144 |
|
Norman James | 10ff6a3 | 2015-08-27 14:24:17 -0500 | [diff] [blame] | 145 | /* 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 James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 149 |
|
| 150 | /* Export all objects */
|
| 151 | g_dbus_object_manager_server_set_connection (manager, connection);
|
| 152 | }
|
| 153 |
|
| 154 | static void
|
| 155 | on_name_acquired (GDBusConnection *connection,
|
| 156 | const gchar *name,
|
| 157 | gpointer user_data)
|
| 158 | {
|
| 159 | g_print ("Acquired the name %s\n", name);
|
| 160 | }
|
| 161 |
|
| 162 | static void
|
| 163 | on_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 James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 170 |
|
| 171 | gint
|
| 172 | main (gint argc, gchar *argv[])
|
| 173 | {
|
| 174 | GMainLoop *loop;
|
Norman James | 10ff6a3 | 2015-08-27 14:24:17 -0500 | [diff] [blame] | 175 | cmdline cmd;
|
| 176 | cmd.argc = argc;
|
| 177 | cmd.argv = argv;
|
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 178 | guint id;
|
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 179 | loop = g_main_loop_new (NULL, FALSE);
|
| 180 |
|
| 181 | id = g_bus_own_name (G_BUS_TYPE_SESSION,
|
Norman James | 26072c0 | 2015-08-25 07:14:29 -0500 | [diff] [blame] | 182 | dbus_name,
|
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 183 | 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 James | 10ff6a3 | 2015-08-27 14:24:17 -0500 | [diff] [blame] | 188 | &cmd,
|
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 189 | NULL);
|
| 190 |
|
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 191 | g_main_loop_run (loop);
|
| 192 |
|
| 193 | g_bus_unown_name (id);
|
| 194 | g_main_loop_unref (loop);
|
| 195 | return 0;
|
| 196 | }
|