blob: 91935f2fb4c13fc1d6bba3300690a9b47efaafd3 [file] [log] [blame]
Norman James362a80f2015-09-14 14:04:39 -05001#include "interfaces/openbmc_intf.h"
2#include "openbmc.h"
3
4
5/* ---------------------------------------------------------------------------------------------------- */
6
7static const gchar* dbus_object_path = "/org/openbmc/sensors";
8static const gchar* dbus_name = "org.openbmc.sensors.Occ";
9static guint heartbeat = 0;
10
11static GDBusObjectManagerServer *manager = NULL;
12static gchar *instance_name = NULL;
13
14static gboolean poll_occ(gpointer user_data)
15{
Norman James2d1ee892015-09-16 23:13:45 -050016 //g_dbus_object_get_object_path(G_DBUS_OBJECT(user_data)),
17 Occ* occ = object_get_occ((Object*)user_data);
18
Norman James362a80f2015-09-14 14:04:39 -050019 gchar *s;
Norman James2d1ee892015-09-16 23:13:45 -050020 s = g_strdup_printf ("%s/Temperature/P8_%s_Core_%d",
21 dbus_object_path,occ_get_instance_name(occ),1);
Norman James362a80f2015-09-14 14:04:39 -050022 g_print("%s\n",s);
23
24 GDBusInterface* interface = g_dbus_object_manager_get_interface((GDBusObjectManager*)manager,s,
25 "org.openbmc.SensorValue");
26
27 if (interface != NULL)
28 {
29 SensorValue* sensor = (SensorValue*) interface;
30 GVariant *value = NEW_VARIANT_U(10);
31 sensor_value_set_value(sensor,value);
32 const gchar* units = sensor_value_get_units(sensor);
33 sensor_value_emit_changed(sensor,sensor_value_get_value(sensor),units);
34 }
35 g_free (s);
36 //g_free(interface);
Norman James362a80f2015-09-14 14:04:39 -050037 return TRUE;
38}
39
Norman James362a80f2015-09-14 14:04:39 -050040static gboolean
41on_init (Occ *occ,
42 GDBusMethodInvocation *invocation,
43 gpointer user_data)
44{
45 guint poll_interval = occ_get_poll_interval(occ);
46 g_timeout_add(poll_interval, poll_occ, user_data);
47 occ_complete_init(occ,invocation);
48 return TRUE;
49}
50static gboolean
51on_init_sensor (SensorValue *sensor,
52 GDBusMethodInvocation *invocation,
53 gpointer user_data)
54{
55 //must implement init method
56 sensor_value_complete_init(sensor,invocation);
57 return TRUE;
58}
59
60
61
62static void
63on_bus_acquired (GDBusConnection *connection,
64 const gchar *name,
65 gpointer user_data)
66{
67 //g_print ("Acquired a message bus connection: %s\n",name);
68
69 cmdline *cmd = user_data;
70 if (cmd->argc < 2)
71 {
72 g_print("No objects created. Put object name(s) on command line\n");
73 return;
74 }
75 manager = g_dbus_object_manager_server_new (dbus_object_path);
76 int i=0;
77 for (i=1;i<cmd->argc;i++)
78 {
79 gchar *s;
80 s = g_strdup_printf ("%s/%s",dbus_object_path,cmd->argv[i]);
81 ObjectSkeleton *object = object_skeleton_new (s);
82 g_free (s);
83
84 Occ *occ = occ_skeleton_new ();
85 object_skeleton_set_occ (object, occ);
86 g_object_unref (occ);
Norman James2d1ee892015-09-16 23:13:45 -050087 occ_set_instance_name(occ,cmd->argv[i]);
Norman James362a80f2015-09-14 14:04:39 -050088
89 SensorI2c *i2c = sensor_i2c_skeleton_new ();
90 object_skeleton_set_sensor_i2c (object, i2c);
91 g_object_unref (i2c);
92
93 g_signal_connect (occ,
94 "handle-init",
95 G_CALLBACK (on_init),
96 object); /* user_data */
97
98 //g_timeout_add(3000, poll_occ, object);
99
100 /* Export the object (@manager takes its own reference to @object) */
101 g_dbus_object_manager_server_export (manager, G_DBUS_OBJECT_SKELETON (object));
102 g_object_unref (object);
103
104 int c;
105 for (c=0;c<12;c++)
106 {
107 s = g_strdup_printf ("%s/Temperature/P8_%s_Core_%d",dbus_object_path,cmd->argv[i],c);
108 ObjectSkeleton *object = object_skeleton_new (s);
109 g_free (s);
110
111 SensorValue *sensor = sensor_value_skeleton_new ();
112 object_skeleton_set_sensor_value (object, sensor);
113 g_object_unref (sensor);
114 GVariant* v_new_value = NEW_VARIANT_U(c);
115 sensor_value_set_value(sensor,v_new_value);
116 sensor_value_set_units(sensor,"C");
117
118 g_signal_connect (sensor,
119 "handle-init",
120 G_CALLBACK (on_init_sensor),
121 NULL); /* user_data */
122
123 //emit changed signal so sensor manager sees initial value
124 sensor_value_emit_changed(sensor,sensor_value_get_value(sensor),sensor_value_get_units(sensor));
125 /* Export the object (@manager takes its own reference to @object) */
126 g_dbus_object_manager_server_export (manager, G_DBUS_OBJECT_SKELETON (object));
127 g_object_unref (object);
128
129 }
130 }
131
132 /* Export all objects */
133 g_dbus_object_manager_server_set_connection (manager, connection);
134}
135
136static void
137on_name_acquired (GDBusConnection *connection,
138 const gchar *name,
139 gpointer user_data)
140{
141// g_print ("Acquired the name %s\n", name);
142}
143
144static void
145on_name_lost (GDBusConnection *connection,
146 const gchar *name,
147 gpointer user_data)
148{
149 //g_print ("Lost the name %s\n", name);
150 g_print("shutting down: %s\n",name);
151 cmdline *cmd = user_data;
152 g_main_loop_quit(cmd->loop);
153}
154
155
156gint
157main (gint argc, gchar *argv[])
158{
159 GMainLoop *loop;
160 cmdline cmd;
161 cmd.argc = argc;
162 cmd.argv = argv;
163
164 guint id;
165 loop = g_main_loop_new (NULL, FALSE);
166 cmd.loop = loop;
167
168 id = g_bus_own_name (G_BUS_TYPE_SESSION,
169 dbus_name,
170 G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT |
171 G_BUS_NAME_OWNER_FLAGS_REPLACE,
172 on_bus_acquired,
173 on_name_acquired,
174 on_name_lost,
175 &cmd,
176 NULL);
177
178 g_main_loop_run (loop);
179
180 g_bus_unown_name (id);
181 g_main_loop_unref (loop);
182 return 0;
183}