blob: a2977836eedd9cf44315b2f7d623740d5b470ecf [file] [log] [blame]
Norman James5236a8f2015-11-05 20:39:31 -06001#include "interfaces/openbmc_intf.h"
2#include <stdio.h>
3#include <fcntl.h>
4#include "openbmc.h"
5#include "gpio.h"
6#include "object_mapper.h"
7
8/* ---------------------------------------------------------------------------------------------------- */
9static const gchar* dbus_object_path = "/org/openbmc/sensors";
10static const gchar* dbus_name = "org.openbmc.sensors.hwmon";
11
12static GDBusObjectManagerServer *manager = NULL;
13
14typedef struct {
15 gchar* filename;
16 gchar* name;
17 int poll_interval;
18 int fd;
19} HWMON;
20
21#define NUM_HWMONS 2
22
23//HWMON hwmons[NUM_HWMONS] = {
24// (HWMON){"/sys/class/hwmon/lm75/temp1_input","temperature/ambient"},
25// (HWMON){"/sys/class/hwmon/nct9704/pwm1_input","speed/fan1"},
26//};
27HWMON hwmons[NUM_HWMONS] = {
28 (HWMON){"/home/njames/hwmon/lm75/temp1_input"},
29 (HWMON){"/home/njames/hwmon/fans/pwm1_input" },
30};
31
32// Gets the gpio device path from gpio manager object
33int hwmon_init(GDBusConnection *connection, HWMON* hwmon)
34{
35 int rc = GPIO_OK;
36 GDBusProxy *proxy;
37 GError *error;
38 GVariant *result;
39
40 error = NULL;
41 g_assert_no_error (error);
42 error = NULL;
43
44 proxy = g_dbus_proxy_new_sync (connection,
45 G_DBUS_PROXY_FLAGS_NONE,
46 NULL, /* GDBusInterfaceInfo */
47 "org.openbmc.managers.System", /* name */
48 "/org/openbmc/managers/System", /* object path */
49 "org.openbmc.managers.System", /* interface */
50 NULL, /* GCancellable */
51 &error);
52 if (error != NULL) {
53 return 1;
54 }
55
56 result = g_dbus_proxy_call_sync (proxy,
57 "hwmonInit",
58 g_variant_new ("(s)", hwmon->filename),
59 G_DBUS_CALL_FLAGS_NONE,
60 -1,
61 NULL,
62 &error);
63
64 if (error != NULL) {
65 return 1;
66 }
67 g_assert (result != NULL);
68 g_variant_get (result, "(&si)", &hwmon->name,&hwmon->poll_interval);
69 g_print("HWMON Lookup: %s = %s,%d\n",hwmon->filename,hwmon->name,hwmon->poll_interval);
70}
71
72static gboolean poll_hwmon(gpointer user_data)
73{
74 Hwmon *hwmon = object_get_hwmon((Object*)user_data);
75 SensorValue *sensor = object_get_sensor_value((Object*)user_data);
76 const gchar* filename = hwmon_get_sysfs_path(hwmon);
77
78 int fd = open(filename, O_RDONLY);
79 if (fd != -1)
80 {
81 char buf[255];
82 if (read(fd,&buf,255) == -1)
83 {
84 g_print("ERROR: Unable to read value: %s\n",filename);
85 } else {
86 guint32 value = atoi(buf);
87 g_print("%s = %d\n",filename,value);
88 GVariant* v = NEW_VARIANT_U(value);
89 sensor_value_set_value(sensor,v);
90 }
91 close(fd);
92 }
93
94 return TRUE;
95}
96static gboolean
97on_set_value (SensorValue *sensor,
98 GDBusMethodInvocation *invocation,
99 GVariant* v_value,
100 gpointer user_data)
101{
102 Hwmon *hwmon = object_get_hwmon((Object*)user_data);
103 const gchar* filename = hwmon_get_sysfs_path(hwmon);
104
105 int fd = open(filename, O_WRONLY);
106 if (fd != -1)
107 {
108 char buf[255];
109 guint32 value = GET_VARIANT_U(v_value);
110 sprintf(buf,"%d",value);
111 if (write(fd, buf, 255) == -1)
112 {
113 g_print("ERROR: Unable to read value: %s\n",filename);
114 }
115 close(fd);
116 }
117 sensor_value_complete_set_value(sensor,invocation);
118 return TRUE;
119}
120
121
122
123static void
124on_bus_acquired (GDBusConnection *connection,
125 const gchar *name,
126 gpointer user_data)
127{
128 ObjectSkeleton *object;
129
130 cmdline *cmd = user_data;
131
132
133 manager = g_dbus_object_manager_server_new (dbus_object_path);
134 int i = 0;
135 for (i=0;i<NUM_HWMONS;i++)
136 {
137 hwmon_init(connection,&hwmons[i]);
138
139 gchar *s;
140 s = g_strdup_printf ("%s/%s",dbus_object_path,hwmons[i].name);
141 object = object_skeleton_new (s);
142 g_free (s);
143
144 Hwmon *hwmon = hwmon_skeleton_new ();
145 object_skeleton_set_hwmon (object, hwmon);
146 g_object_unref (hwmon);
147
148 SensorValue *sensor = sensor_value_skeleton_new ();
149 object_skeleton_set_sensor_value (object, sensor);
150 g_object_unref (sensor);
151
152 ObjectMapper* mapper = object_mapper_skeleton_new ();
153 object_skeleton_set_object_mapper (object, mapper);
154 g_object_unref (mapper);
155
156 hwmon_set_sysfs_path(hwmon,hwmons[i].filename);
157
158 //define method callbacks here
159 g_signal_connect (sensor,
160 "handle-set-value",
161 G_CALLBACK (on_set_value),
162 object); /* user_data */
163
164
165 if (hwmons[i].poll_interval > 0) {
166 g_timeout_add(hwmons[i].poll_interval, poll_hwmon, object);
167 }
168 /* Export the object (@manager takes its own reference to @object) */
169 g_dbus_object_manager_server_export (manager, G_DBUS_OBJECT_SKELETON (object));
170 g_object_unref (object);
171 }
172 /* Export all objects */
173 g_dbus_object_manager_server_set_connection (manager, connection);
174 emit_object_added((GDBusObjectManager*)manager);
175}
176
177
178static void
179on_name_acquired (GDBusConnection *connection,
180 const gchar *name,
181 gpointer user_data)
182{
183}
184
185static void
186on_name_lost (GDBusConnection *connection,
187 const gchar *name,
188 gpointer user_data)
189{
190}
191
192
193gint
194main (gint argc, gchar *argv[])
195{
196 GMainLoop *loop;
197 cmdline cmd;
198 cmd.argc = argc;
199 cmd.argv = argv;
200
201 guint id;
202 loop = g_main_loop_new (NULL, FALSE);
203
204 id = g_bus_own_name (DBUS_TYPE,
205 dbus_name,
206 G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT |
207 G_BUS_NAME_OWNER_FLAGS_REPLACE,
208 on_bus_acquired,
209 on_name_acquired,
210 on_name_lost,
211 &cmd,
212 NULL);
213
214 g_main_loop_run (loop);
215
216 g_bus_unown_name (id);
217 g_main_loop_unref (loop);
218 return 0;
219}