blob: 0057af475d047d4e627ccf043af175ccc502d62a [file] [log] [blame]
Brad Bishop77390492016-04-13 10:47:19 -04001#include "interfaces/openbmc_intf.h"
2#include <stdio.h>
Brad Bishop0c82c602016-04-13 13:36:49 -04003#include <stdlib.h>
Brad Bishop77390492016-04-13 10:47:19 -04004#include <fcntl.h>
5#include "openbmc.h"
6#include "gpio.h"
7#include "object_mapper.h"
8
9/* ------------------------------------------------------------------------- */
10static const gchar* dbus_object_path = "/org/openbmc/sensors";
11static const gchar* dbus_name = "org.openbmc.sensors.hwmon";
12
13static GDBusObjectManagerServer *manager = NULL;
14
15typedef struct {
16 const gchar* filename;
17 const gchar* name;
18 int poll_interval;
19 const gchar* units;
20 int scale;
21 int fd;
22} HWMON;
23
24#define NUM_HWMONS 7
25
26// TODO: Don't hardcode
27//Hardcoded for barreleye
28HWMON hwmons[NUM_HWMONS] = {
29 (HWMON){"/sys/class/hwmon/hwmon0/temp1_input","temperature/ambient",3000,"C",1000},
30 (HWMON){"/sys/class/hwmon/hwmon1/pwm1","speed/fan0",30000,"",1},
31 (HWMON){"/sys/class/hwmon/hwmon1/pwm2","speed/fan1",30000,"",1},
32 (HWMON){"/sys/class/hwmon/hwmon1/pwm3","speed/fan2",30000,"",1},
33 (HWMON){"/sys/class/hwmon/hwmon2/pwm1","speed/fan3",30000,"",1},
34 (HWMON){"/sys/class/hwmon/hwmon2/pwm2","speed/fan4",30000,"",1},
35 (HWMON){"/sys/class/hwmon/hwmon2/pwm3","speed/fan5",30000,"",1},
36};
37
38bool
39is_hwmon_valid(HWMON* hwmon)
40{
41 int fd = open(hwmon->filename, O_RDONLY);
42 if(fd == -1)
43 {
44 g_print("ERROR hwmon is not valid: %s\n",hwmon->filename);
45 return false;
46 }
47 close(fd);
48 return true;
49}
50
51static gboolean
52poll_hwmon(gpointer user_data)
53{
54 Hwmon *hwmon = object_get_hwmon((Object*)user_data);
55 SensorValue *sensor = object_get_sensor_value((Object*)user_data);
56 const gchar* filename = hwmon_get_sysfs_path(hwmon);
57
58 int fd = open(filename, O_RDONLY);
59 if(fd != -1)
60 {
61 char buf[255];
62 if(read(fd,&buf,255) == -1)
63 {
64 g_print("ERROR: Unable to read value: %s\n",filename);
65 } else {
66 guint32 scale = hwmon_get_scale(hwmon);
67 if(scale == 0)
68 {
69 g_print("ERROR: Invalid scale value of 0\n");
70 scale = 1;
71
72 }
73 guint32 value = atoi(buf)/scale;
Brad Bishop0c82c602016-04-13 13:36:49 -040074 NEW_VARIANT_U(value);
Brad Bishop77390492016-04-13 10:47:19 -040075 GVariant* old_value = sensor_value_get_value(sensor);
76 bool do_set = false;
77 if(old_value == NULL)
78 {
79 do_set = true;
80 }
81 else
82 {
83 if(GET_VARIANT_U(old_value) != value) { do_set = true; }
84 }
85 if(do_set)
86 {
87 g_print("Sensor changed: %s,%d\n",filename,value);
88 GVariant* v = NEW_VARIANT_U(value);
89 const gchar* units = sensor_value_get_units(sensor);
90 sensor_value_set_value(sensor,v);
91 sensor_value_emit_changed(sensor,v,units);
92 }
93 }
94 close(fd);
95 } else {
96 g_print("ERROR - hwmons: File %s doesn't exist\n",filename);
97 }
98
99 return TRUE;
100}
101
102static gboolean
103on_set_value(SensorValue *sensor,
104 GDBusMethodInvocation *invocation,
105 GVariant* v_value,
106 gpointer user_data)
107{
108 Hwmon *hwmon = object_get_hwmon((Object*)user_data);
109 const gchar* filename = hwmon_get_sysfs_path(hwmon);
110
111 int fd = open(filename, O_WRONLY);
112 if(fd != -1)
113 {
114 char buf[255];
115 guint32 value = GET_VARIANT_U(v_value);
116 sprintf(buf,"%d",value);
117 if(write(fd, buf, 255) == -1)
118 {
119 g_print("ERROR: Unable to read value: %s\n",filename);
120 }
121 close(fd);
122 }
123 sensor_value_complete_set_value(sensor,invocation);
124 return TRUE;
125}
126
127static void
128on_bus_acquired(GDBusConnection *connection,
129 const gchar *name,
130 gpointer user_data)
131{
132 ObjectSkeleton *object;
133
Brad Bishop77390492016-04-13 10:47:19 -0400134 manager = g_dbus_object_manager_server_new(dbus_object_path);
135 int i = 0;
136 for(i=0;i<NUM_HWMONS;i++)
137 {
138 if(!is_hwmon_valid(&hwmons[i])) { continue; }
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 hwmon_set_scale(hwmon,hwmons[i].scale);
158 sensor_value_set_units(sensor,hwmons[i].units);
159
160 //define method callbacks here
161 g_signal_connect(sensor,
162 "handle-set-value",
163 G_CALLBACK(on_set_value),
164 object); /* user_data */
165
166
167 if(hwmons[i].poll_interval > 0) {
168 g_timeout_add(hwmons[i].poll_interval, poll_hwmon, object);
169 }
170 /* Export the object (@manager takes its own reference to @object) */
171 g_dbus_object_manager_server_export(manager, G_DBUS_OBJECT_SKELETON(object));
172 g_object_unref(object);
173 }
174 /* Export all objects */
175 g_dbus_object_manager_server_set_connection(manager, connection);
176 emit_object_added((GDBusObjectManager*)manager);
177}
178
179static void
180on_name_acquired(GDBusConnection *connection,
181 const gchar *name,
182 gpointer user_data)
183{
184}
185
186static void
187on_name_lost(GDBusConnection *connection,
188 const gchar *name,
189 gpointer user_data)
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}