Brad Bishop | 7739049 | 2016-04-13 10:47:19 -0400 | [diff] [blame] | 1 | #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 | /* ------------------------------------------------------------------------- */ |
| 9 | static const gchar* dbus_object_path = "/org/openbmc/sensors"; |
| 10 | static const gchar* dbus_name = "org.openbmc.sensors.hwmon"; |
| 11 | |
| 12 | static GDBusObjectManagerServer *manager = NULL; |
| 13 | |
| 14 | typedef struct { |
| 15 | const gchar* filename; |
| 16 | const gchar* name; |
| 17 | int poll_interval; |
| 18 | const gchar* units; |
| 19 | int scale; |
| 20 | int fd; |
| 21 | } HWMON; |
| 22 | |
| 23 | #define NUM_HWMONS 1 |
| 24 | |
| 25 | // TODO: Don't hardcode |
| 26 | //Hardcoded for palmetto |
| 27 | HWMON hwmons[NUM_HWMONS] = { |
| 28 | (HWMON){"/sys/class/hwmon/hwmon0/temp1_input","temperature/ambient",3000,"C",1000}, |
| 29 | }; |
| 30 | |
| 31 | bool |
| 32 | is_hwmon_valid(HWMON* hwmon) |
| 33 | { |
| 34 | int fd = open(hwmon->filename, O_RDONLY); |
| 35 | if(fd == -1) |
| 36 | { |
| 37 | g_print("ERROR hwmon is not valid: %s\n",hwmon->filename); |
| 38 | return false; |
| 39 | } |
| 40 | close(fd); |
| 41 | return true; |
| 42 | } |
| 43 | |
| 44 | static gboolean |
| 45 | poll_hwmon(gpointer user_data) |
| 46 | { |
| 47 | Hwmon *hwmon = object_get_hwmon((Object*)user_data); |
| 48 | SensorValue *sensor = object_get_sensor_value((Object*)user_data); |
| 49 | const gchar* filename = hwmon_get_sysfs_path(hwmon); |
| 50 | |
| 51 | int fd = open(filename, O_RDONLY); |
| 52 | if(fd != -1) |
| 53 | { |
| 54 | char buf[255]; |
| 55 | if(read(fd,&buf,255) == -1) |
| 56 | { |
| 57 | g_print("ERROR: Unable to read value: %s\n",filename); |
| 58 | } else { |
| 59 | guint32 scale = hwmon_get_scale(hwmon); |
| 60 | if(scale == 0) |
| 61 | { |
| 62 | g_print("ERROR: Invalid scale value of 0\n"); |
| 63 | scale = 1; |
| 64 | |
| 65 | } |
| 66 | guint32 value = atoi(buf)/scale; |
| 67 | GVariant* v = NEW_VARIANT_U(value); |
| 68 | GVariant* old_value = sensor_value_get_value(sensor); |
| 69 | bool do_set = false; |
| 70 | if(old_value == NULL) |
| 71 | { |
| 72 | do_set = true; |
| 73 | } |
| 74 | else |
| 75 | { |
| 76 | if(GET_VARIANT_U(old_value) != value) { do_set = true; } |
| 77 | } |
| 78 | if(do_set) |
| 79 | { |
| 80 | g_print("Sensor changed: %s,%d\n",filename,value); |
| 81 | GVariant* v = NEW_VARIANT_U(value); |
| 82 | const gchar* units = sensor_value_get_units(sensor); |
| 83 | sensor_value_set_value(sensor,v); |
| 84 | sensor_value_emit_changed(sensor,v,units); |
| 85 | } |
| 86 | } |
| 87 | close(fd); |
| 88 | } else { |
| 89 | g_print("ERROR - hwmons: File %s doesn't exist\n",filename); |
| 90 | } |
| 91 | |
| 92 | return TRUE; |
| 93 | } |
| 94 | |
| 95 | static gboolean |
| 96 | on_set_value(SensorValue *sensor, |
| 97 | GDBusMethodInvocation *invocation, |
| 98 | GVariant* v_value, |
| 99 | gpointer user_data) |
| 100 | { |
| 101 | Hwmon *hwmon = object_get_hwmon((Object*)user_data); |
| 102 | const gchar* filename = hwmon_get_sysfs_path(hwmon); |
| 103 | |
| 104 | int fd = open(filename, O_WRONLY); |
| 105 | if(fd != -1) |
| 106 | { |
| 107 | char buf[255]; |
| 108 | guint32 value = GET_VARIANT_U(v_value); |
| 109 | sprintf(buf,"%d",value); |
| 110 | if(write(fd, buf, 255) == -1) |
| 111 | { |
| 112 | g_print("ERROR: Unable to read value: %s\n",filename); |
| 113 | } |
| 114 | close(fd); |
| 115 | } |
| 116 | sensor_value_complete_set_value(sensor,invocation); |
| 117 | return TRUE; |
| 118 | } |
| 119 | |
| 120 | static void |
| 121 | on_bus_acquired(GDBusConnection *connection, |
| 122 | const gchar *name, |
| 123 | gpointer user_data) |
| 124 | { |
| 125 | ObjectSkeleton *object; |
| 126 | |
| 127 | cmdline *cmd = user_data; |
| 128 | |
| 129 | manager = g_dbus_object_manager_server_new(dbus_object_path); |
| 130 | int i = 0; |
| 131 | for(i=0;i<NUM_HWMONS;i++) |
| 132 | { |
| 133 | if(!is_hwmon_valid(&hwmons[i])) { continue; } |
| 134 | gchar *s; |
| 135 | s = g_strdup_printf("%s/%s",dbus_object_path,hwmons[i].name); |
| 136 | object = object_skeleton_new(s); |
| 137 | g_free(s); |
| 138 | |
| 139 | Hwmon *hwmon = hwmon_skeleton_new(); |
| 140 | object_skeleton_set_hwmon(object, hwmon); |
| 141 | g_object_unref(hwmon); |
| 142 | |
| 143 | SensorValue *sensor = sensor_value_skeleton_new(); |
| 144 | object_skeleton_set_sensor_value(object, sensor); |
| 145 | g_object_unref(sensor); |
| 146 | |
| 147 | ObjectMapper* mapper = object_mapper_skeleton_new(); |
| 148 | object_skeleton_set_object_mapper(object, mapper); |
| 149 | g_object_unref(mapper); |
| 150 | |
| 151 | hwmon_set_sysfs_path(hwmon,hwmons[i].filename); |
| 152 | hwmon_set_scale(hwmon,hwmons[i].scale); |
| 153 | sensor_value_set_units(sensor,hwmons[i].units); |
| 154 | |
| 155 | //define method callbacks here |
| 156 | g_signal_connect(sensor, |
| 157 | "handle-set-value", |
| 158 | G_CALLBACK(on_set_value), |
| 159 | object); /* user_data */ |
| 160 | |
| 161 | |
| 162 | if(hwmons[i].poll_interval > 0) { |
| 163 | g_timeout_add(hwmons[i].poll_interval, poll_hwmon, object); |
| 164 | } |
| 165 | /* Export the object (@manager takes its own reference to @object) */ |
| 166 | g_dbus_object_manager_server_export(manager, G_DBUS_OBJECT_SKELETON(object)); |
| 167 | g_object_unref(object); |
| 168 | } |
| 169 | /* Export all objects */ |
| 170 | g_dbus_object_manager_server_set_connection(manager, connection); |
| 171 | emit_object_added((GDBusObjectManager*)manager); |
| 172 | } |
| 173 | |
| 174 | static void |
| 175 | on_name_acquired(GDBusConnection *connection, |
| 176 | const gchar *name, |
| 177 | gpointer user_data) |
| 178 | { |
| 179 | } |
| 180 | |
| 181 | static void |
| 182 | on_name_lost(GDBusConnection *connection, |
| 183 | const gchar *name, |
| 184 | gpointer user_data) |
| 185 | { |
| 186 | } |
| 187 | |
| 188 | gint |
| 189 | main(gint argc, gchar *argv[]) |
| 190 | { |
| 191 | GMainLoop *loop; |
| 192 | cmdline cmd; |
| 193 | cmd.argc = argc; |
| 194 | cmd.argv = argv; |
| 195 | |
| 196 | guint id; |
| 197 | loop = g_main_loop_new(NULL, FALSE); |
| 198 | |
| 199 | id = g_bus_own_name(DBUS_TYPE, |
| 200 | dbus_name, |
| 201 | G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT | |
| 202 | G_BUS_NAME_OWNER_FLAGS_REPLACE, |
| 203 | on_bus_acquired, |
| 204 | on_name_acquired, |
| 205 | on_name_lost, |
| 206 | &cmd, |
| 207 | NULL); |
| 208 | |
| 209 | g_main_loop_run(loop); |
| 210 | |
| 211 | g_bus_unown_name(id); |
| 212 | g_main_loop_unref(loop); |
| 213 | return 0; |
| 214 | } |