Norman James | 90baede | 2015-09-02 20:32:49 -0500 | [diff] [blame^] | 1 | #include "interfaces/sensor.h"
|
Norman James | 10ff6a3 | 2015-08-27 14:24:17 -0500 | [diff] [blame] | 2 | #include "openbmc.h"
|
| 3 |
|
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 4 |
|
| 5 | /* ---------------------------------------------------------------------------------------------------- */
|
Norman James | 10ff6a3 | 2015-08-27 14:24:17 -0500 | [diff] [blame] | 6 |
|
Norman James | 26072c0 | 2015-08-25 07:14:29 -0500 | [diff] [blame] | 7 | static const gchar* dbus_object_path = "/org/openbmc/sensors/HostStatus";
|
| 8 | static const gchar* dbus_name = "org.openbmc.sensors.HostStatus";
|
Norman James | 10ff6a3 | 2015-08-27 14:24:17 -0500 | [diff] [blame] | 9 | static const guint poll_interval = 3000;
|
| 10 | static guint heartbeat = 0;
|
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 11 |
|
| 12 | static GDBusObjectManagerServer *manager = NULL;
|
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 13 |
|
| 14 | static gboolean
|
Norman James | 10ff6a3 | 2015-08-27 14:24:17 -0500 | [diff] [blame] | 15 | on_get_units (SensorInteger *sen,
|
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 16 | GDBusMethodInvocation *invocation,
|
| 17 | gpointer user_data)
|
| 18 | {
|
Norman James | 10ff6a3 | 2015-08-27 14:24:17 -0500 | [diff] [blame] | 19 | const gchar* val = sensor_integer_get_units(sen);
|
| 20 | sensor_integer_complete_get_units(sen,invocation,val);
|
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 21 | return TRUE;
|
| 22 | }
|
| 23 |
|
| 24 | static gboolean
|
Norman James | 10ff6a3 | 2015-08-27 14:24:17 -0500 | [diff] [blame] | 25 | on_get (SensorInteger *sen,
|
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 26 | GDBusMethodInvocation *invocation,
|
| 27 | gpointer user_data)
|
| 28 | {
|
Norman James | 10ff6a3 | 2015-08-27 14:24:17 -0500 | [diff] [blame] | 29 | guint reading = sensor_integer_get_value(sen);
|
| 30 | sensor_integer_complete_get_value(sen,invocation,reading);
|
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 31 | return TRUE;
|
| 32 | }
|
Norman James | 10ff6a3 | 2015-08-27 14:24:17 -0500 | [diff] [blame] | 33 |
|
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 34 | static gboolean
|
Norman James | 90baede | 2015-09-02 20:32:49 -0500 | [diff] [blame^] | 35 | on_set (SensorInteger *sen,
|
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 36 | GDBusMethodInvocation *invocation,
|
Norman James | 90baede | 2015-09-02 20:32:49 -0500 | [diff] [blame^] | 37 | guint value,
|
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 38 | gpointer user_data)
|
| 39 | {
|
Norman James | 90baede | 2015-09-02 20:32:49 -0500 | [diff] [blame^] | 40 | sensor_integer_set_value(sen,value);
|
| 41 | sensor_integer_emit_changed(sen,value,sensor_integer_get_units(sen));
|
| 42 | sensor_integer_complete_set_value(sen,invocation);
|
| 43 | return TRUE;
|
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 44 | }
|
| 45 |
|
| 46 | static void
|
| 47 | on_bus_acquired (GDBusConnection *connection,
|
| 48 | const gchar *name,
|
| 49 | gpointer user_data)
|
| 50 | {
|
Norman James | 10ff6a3 | 2015-08-27 14:24:17 -0500 | [diff] [blame] | 51 | g_print ("Acquired a message bus connection: %s\n",name);
|
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 52 |
|
Norman James | 10ff6a3 | 2015-08-27 14:24:17 -0500 | [diff] [blame] | 53 | cmdline *cmd = user_data;
|
| 54 | if (cmd->argc < 2)
|
| 55 | {
|
| 56 | g_print("No objects created. Put object name(s) on command line\n");
|
| 57 | return;
|
| 58 | }
|
| 59 | manager = g_dbus_object_manager_server_new (dbus_object_path);
|
| 60 | int i=0;
|
| 61 | for (i=1;i<cmd->argc;i++)
|
| 62 | {
|
| 63 | gchar *s;
|
| 64 | s = g_strdup_printf ("%s/%s",dbus_object_path,cmd->argv[i]);
|
| 65 | ObjectSkeleton *object = object_skeleton_new (s);
|
| 66 | g_free (s);
|
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 67 |
|
Norman James | 10ff6a3 | 2015-08-27 14:24:17 -0500 | [diff] [blame] | 68 | SensorInteger *sensor = sensor_integer_skeleton_new ();
|
| 69 | object_skeleton_set_sensor_integer (object, sensor);
|
| 70 | g_object_unref (sensor);
|
| 71 |
|
| 72 | // set units
|
Norman James | 90baede | 2015-09-02 20:32:49 -0500 | [diff] [blame^] | 73 | sensor_integer_set_units(sensor,"");
|
Norman James | 10ff6a3 | 2015-08-27 14:24:17 -0500 | [diff] [blame] | 74 | //define method callbacks here
|
| 75 | g_signal_connect (sensor,
|
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 76 | "handle-get-value",
|
| 77 | G_CALLBACK (on_get),
|
| 78 | NULL); /* user_data */
|
Norman James | 10ff6a3 | 2015-08-27 14:24:17 -0500 | [diff] [blame] | 79 | g_signal_connect (sensor,
|
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 80 | "handle-get-units",
|
| 81 | G_CALLBACK (on_get_units),
|
| 82 | NULL); /* user_data */
|
Norman James | 10ff6a3 | 2015-08-27 14:24:17 -0500 | [diff] [blame] | 83 | g_signal_connect (sensor,
|
Norman James | 90baede | 2015-09-02 20:32:49 -0500 | [diff] [blame^] | 84 | "handle-set-value",
|
| 85 | G_CALLBACK (on_set),
|
Norman James | 10ff6a3 | 2015-08-27 14:24:17 -0500 | [diff] [blame] | 86 | NULL); /* user_data */
|
Norman James | 10ff6a3 | 2015-08-27 14:24:17 -0500 | [diff] [blame] | 87 |
|
| 88 | /* Export the object (@manager takes its own reference to @object) */
|
| 89 | g_dbus_object_manager_server_export (manager, G_DBUS_OBJECT_SKELETON (object));
|
| 90 | g_object_unref (object);
|
| 91 | }
|
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 92 |
|
| 93 | /* Export all objects */
|
| 94 | g_dbus_object_manager_server_set_connection (manager, connection);
|
| 95 | }
|
| 96 |
|
| 97 | static void
|
| 98 | on_name_acquired (GDBusConnection *connection,
|
| 99 | const gchar *name,
|
| 100 | gpointer user_data)
|
| 101 | {
|
| 102 | g_print ("Acquired the name %s\n", name);
|
| 103 | }
|
| 104 |
|
| 105 | static void
|
| 106 | on_name_lost (GDBusConnection *connection,
|
| 107 | const gchar *name,
|
| 108 | gpointer user_data)
|
| 109 | {
|
| 110 | g_print ("Lost the name %s\n", name);
|
| 111 | }
|
| 112 |
|
Norman James | 10ff6a3 | 2015-08-27 14:24:17 -0500 | [diff] [blame] | 113 |
|
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 114 | gint
|
| 115 | main (gint argc, gchar *argv[])
|
| 116 | {
|
| 117 | GMainLoop *loop;
|
Norman James | 10ff6a3 | 2015-08-27 14:24:17 -0500 | [diff] [blame] | 118 | cmdline cmd;
|
| 119 | cmd.argc = argc;
|
| 120 | cmd.argv = argv;
|
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 121 | guint id;
|
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 122 | loop = g_main_loop_new (NULL, FALSE);
|
| 123 |
|
| 124 | id = g_bus_own_name (G_BUS_TYPE_SESSION,
|
Norman James | 26072c0 | 2015-08-25 07:14:29 -0500 | [diff] [blame] | 125 | dbus_name,
|
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 126 | G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT |
|
| 127 | G_BUS_NAME_OWNER_FLAGS_REPLACE,
|
| 128 | on_bus_acquired,
|
| 129 | on_name_acquired,
|
| 130 | on_name_lost,
|
Norman James | 10ff6a3 | 2015-08-27 14:24:17 -0500 | [diff] [blame] | 131 | &cmd,
|
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 132 | NULL);
|
Norman James | 10ff6a3 | 2015-08-27 14:24:17 -0500 | [diff] [blame] | 133 |
|
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 134 | g_main_loop_run (loop);
|
| 135 |
|
| 136 | g_bus_unown_name (id);
|
| 137 | g_main_loop_unref (loop);
|
| 138 | return 0;
|
| 139 | }
|