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