1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
|
/*
* libhal.c : HAL compatibility library for Flash using UDisks2
*
* Copyright (C) 2014 Chris Horler, <cshorler@googlemail.com>
*
* Licensed under the Academic Free License version 2.1
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*
* Contributors to the Original HAL code are stated below for completeness
* (very little original code remains):
*
* Copyright (C) 2003 David Zeuthen, <david@fubar.dk>
* Copyright (C) 2006 Sjoerd Simons, <sjoerd@luon.net>
* Copyright (C) 2007 Codethink Ltd. Author Rob Taylor <rob.taylor@codethink.co.uk>
*
**************************************************************************/
#include <glib.h>
#include <gio/gio.h>
#include <glib/gprintf.h>
#include "libhal.h"
#define DBUS_SERVICE_UDISKS2 "org.freedesktop.UDisks2"
#define DBUS_PATH_UDISKS2 "/org/freedesktop/UDisks2"
#define DBUS_IFACE_UDISKS2_DRIVE "org.freedesktop.UDisks2.Drive"
#define DBUS_IFACE_UDISKS2_DRIVE_ATA "org.freedesktop.UDisks2.Drive.Ata"
#define DBUS_PATH_HAL_COMPUTER "/org/freedesktop/Hal/devices/computer"
struct LibHalContext_s {
DBusConnection *connection; /**< D-BUS connection */
dbus_bool_t is_initialized; /**< Are we initialised */
GDBusObjectManager* obj_manager;
void *user_data; /**< User data */
};
static char*
get_connection_bus(GDBusObjectManager *obj_manager, const char* hdd_object_path) {
GDBusProxy *hdd_interface;
GDBusProxy *ata_interface = NULL;
GVariant* property = NULL;
const char* vdata_p = NULL;
gchar* bus = NULL;
gboolean is_ata = FALSE;
hdd_interface = (GDBusProxy* ) g_dbus_object_manager_get_interface(obj_manager,
hdd_object_path, DBUS_IFACE_UDISKS2_DRIVE);
if (hdd_interface == NULL)
goto out;
ata_interface = (GDBusProxy* ) g_dbus_object_manager_get_interface(obj_manager,
hdd_object_path, DBUS_IFACE_UDISKS2_DRIVE_ATA);
if (ata_interface != NULL)
is_ata = TRUE;
property = g_dbus_proxy_get_cached_property(hdd_interface, "ConnectionBus");
if (property == NULL)
goto out;
bus = g_variant_dup_string(property, NULL);
if (g_strcmp0(bus, "") == 0 && is_ata) {
g_free(bus);
bus = g_strdup("ata");
}
out:
if (hdd_interface != NULL)
g_object_unref(hdd_interface);
if (ata_interface != NULL)
g_object_unref(ata_interface);
if (property != NULL)
g_variant_unref(property);
return g_strcmp0(bus,"") == 0 ? NULL : bus;
}
static char**
find_hard_drives(GDBusObjectManager *obj_manager, guint* count) {
GList* object_list = NULL;
GList* l;
GPtrArray* object_path_list;
GDBusObject* object;
GDBusInterface* drive_proxy = NULL;
char** hdd_list;
const gchar *object_path;
object_path_list = g_ptr_array_new();
object_list = g_dbus_object_manager_get_objects(obj_manager);
for (l = object_list; l != NULL; l = l->next) {
object = l->data;
object_path = g_dbus_object_get_object_path(object);
drive_proxy = g_dbus_object_get_interface(object, DBUS_IFACE_UDISKS2_DRIVE);
g_object_unref(object);
if (drive_proxy != NULL)
g_ptr_array_add(object_path_list, g_strdup(object_path));
}
*count = object_path_list->len;
g_ptr_array_add(object_path_list, NULL);
hdd_list = (char** ) g_ptr_array_free(object_path_list, FALSE);
if (object_list != NULL)
g_list_free(object_list);
return hdd_list;
}
static char *
get_drive_serial(GDBusObjectManager *mgr, const char *object_path)
{
GDBusProxy* drive_proxy = NULL;
GVariant* property = NULL;
char* drive_serial;
drive_proxy = (GDBusProxy* ) g_dbus_object_manager_get_interface(mgr, object_path, DBUS_IFACE_UDISKS2_DRIVE);
if (drive_proxy == NULL)
goto out;
property = g_dbus_proxy_get_cached_property(drive_proxy, "Serial");
drive_serial = g_variant_dup_string(property, NULL);
out:
if (property != NULL)
g_variant_unref(property);
if (drive_proxy != NULL)
g_object_unref(drive_proxy);
return drive_serial;
}
static guint64
get_drive_size(GDBusObjectManager *mgr, const char *object_path)
{
GDBusProxy* drive_proxy = NULL;
GVariant* property = NULL;
guint64 size = 0L;
drive_proxy = (GDBusProxy* ) g_dbus_object_manager_get_interface(mgr, object_path, DBUS_IFACE_UDISKS2_DRIVE);
if (drive_proxy == NULL)
goto out;
property = g_dbus_proxy_get_cached_property(drive_proxy, "Size");
size = g_variant_get_uint64(property);
out:
if (property != NULL)
g_variant_unref(property);
if (drive_proxy != NULL)
g_object_unref(drive_proxy);
return size;
}
/* Public Interface */
char **
libhal_manager_find_device_string_match(LibHalContext *ctx, const char *key, const char *value, int *num_devices,
DBusError *error)
{
gchar *default_r_val[] = {NULL,};
char **hal_device_names;
g_return_val_if_fail(ctx, NULL);
g_return_val_if_fail(key, NULL);
g_return_val_if_fail(value, NULL);
if (g_strcmp0(key, "storage.drive_type") == 0 && g_strcmp0(value, "disk") == 0) {
hal_device_names = find_hard_drives(ctx->obj_manager, num_devices);
} else {
hal_device_names = g_strdupv(default_r_val);
*num_devices = 0;
}
return hal_device_names;
}
void
libhal_free_string_array(char **str_array)
{
g_strfreev(str_array);
}
LibHalContext *
libhal_ctx_new(void)
{
LibHalContext *ctx;
ctx = g_try_malloc0(sizeof(LibHalContext));
if (ctx == NULL) {
g_printerr("%s %d : Failed to allocate %lu bytes\n", __FILE__, __LINE__, sizeof(LibHalContext));
return NULL;
}
ctx->is_initialized = FALSE;
ctx->connection = NULL;
ctx->obj_manager = NULL;
return ctx;
}
dbus_bool_t
libhal_ctx_set_dbus_connection(LibHalContext *ctx, DBusConnection *conn)
{
g_return_val_if_fail(ctx, FALSE);
g_return_val_if_fail(conn, FALSE);
GError *error = NULL;
ctx->connection = conn;
ctx->obj_manager = g_dbus_object_manager_client_new_for_bus_sync(G_BUS_TYPE_SYSTEM,
G_DBUS_OBJECT_MANAGER_CLIENT_FLAGS_NONE,
DBUS_SERVICE_UDISKS2,
DBUS_PATH_UDISKS2,
NULL, NULL, NULL, NULL,
&error);
if (ctx->obj_manager == NULL) {
g_printerr("Error creating DBus Object Manager: %s\n", error->message);
g_error_free(error);
return FALSE;
}
return TRUE;
}
dbus_bool_t
libhal_ctx_init(LibHalContext *ctx, DBusError *error)
{
g_return_val_if_fail(ctx, FALSE);
if (ctx->connection == NULL)
return FALSE;
if (ctx->obj_manager == NULL)
return FALSE;
ctx->is_initialized = TRUE;
return TRUE;
}
dbus_bool_t
libhal_ctx_shutdown(LibHalContext *ctx, DBusError *error)
{
g_return_val_if_fail(ctx, FALSE);
ctx->is_initialized = FALSE;
return TRUE;
}
dbus_bool_t
libhal_ctx_free(LibHalContext *ctx)
{
g_free(ctx);
return TRUE;
}
LibHalPropertyType
libhal_device_get_property_type (LibHalContext *ctx, const char *udi, const char *key, DBusError *error)
{
g_return_val_if_fail(ctx, LIBHAL_PROPERTY_TYPE_INVALID);
g_return_val_if_fail(key, LIBHAL_PROPERTY_TYPE_INVALID);
int property_type = LIBHAL_PROPERTY_TYPE_INVALID;
if (g_strcmp0(udi, DBUS_PATH_HAL_COMPUTER) == 0) {
if (g_strcmp0(key, "system.hardware.serial") == 0)
property_type = LIBHAL_PROPERTY_TYPE_STRING;
} else if (g_str_has_prefix(udi, DBUS_PATH_UDISKS2)) {
if (g_strcmp0(key, "storage.bus") == 0 || g_strcmp0(key, "storage.serial") == 0) {
property_type = LIBHAL_PROPERTY_TYPE_STRING;
} else if (g_strcmp0(key, "storage.size") == 0) {
property_type = LIBHAL_PROPERTY_TYPE_UINT64;
}
}
return property_type;
}
char *
libhal_device_get_property_string(LibHalContext *ctx, const char *udi, const char *key, DBusError *error)
{
g_return_val_if_fail(ctx, NULL);
g_return_val_if_fail(key, NULL);
if (g_strcmp0(key, "system.hardware.serial") == 0)
return dbus_get_local_machine_id();
else if (g_strcmp0(key, "storage.bus") == 0)
return get_connection_bus(ctx->obj_manager, udi);
else if (g_strcmp0(key, "storage.serial") == 0)
return get_drive_serial(ctx->obj_manager, udi);
else
return NULL;
}
void
libhal_free_string (char *str)
{
g_free(str);
str = NULL;
}
dbus_uint64_t
libhal_device_get_property_uint64 (LibHalContext *ctx, const char *udi, const char *key, DBusError *error)
{
g_return_val_if_fail(ctx, -1);
g_return_val_if_fail(key, -1);
if (g_strcmp0(key, "storage.size") == 0)
return get_drive_size(ctx->obj_manager, udi);
else
return -1;
}
dbus_int32_t
libhal_device_get_property_int (LibHalContext *ctx, const char *udi, const char *key, DBusError *error)
{
return -1;
}
double
libhal_device_get_property_double (LibHalContext *ctx, const char *udi, const char *key, DBusError *error)
{
return -1.0f;
}
|