bolt-device.c 8.36 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*
 * Copyright © 2017 Red Hat, Inc
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
 *
 * Authors:
 *       Christian J. Kellner <christian@kellner.me>
 */

#include "config.h"

23
#include "bolt-dbus.h"
24 25
#include "bolt-device.h"
#include "bolt-error.h"
26
#include "bolt-io.h"
27
#include "bolt-manager.h"
28

29
#include <dirent.h>
30 31 32 33 34 35
#include <libudev.h>

struct _BoltDevice
{
  BoltDBusDeviceSkeleton object;

36 37
  /* weak reference */
  BoltManager *mgr;
38

39 40 41 42 43
  char        *dbus_path;

  char        *uid;
  char        *name;
  char        *vendor;
44

45 46
  BoltStatus   status;

47 48
  /* when device is attached */
  char *syspath;
49 50 51 52 53 54 55
};


enum {
  PROP_0,

  PROP_UID,
56 57
  PROP_NAME,
  PROP_VENDOR,
58
  PROP_STATUS,
59
  PROP_SYSFS,
60 61 62 63 64 65 66 67 68 69 70 71 72 73

  PROP_LAST
};


G_DEFINE_TYPE (BoltDevice,
               bolt_device,
               BOLT_DBUS_TYPE_DEVICE_SKELETON)

static void
bolt_device_finalize (GObject *object)
{
  BoltDevice *dev = BOLT_DEVICE (object);

74 75
  g_free (dev->dbus_path);

76
  g_free (dev->uid);
77 78 79
  g_free (dev->name);
  g_free (dev->vendor);
  g_free (dev->syspath);
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103

  G_OBJECT_CLASS (bolt_device_parent_class)->finalize (object);
}

static void
bolt_device_init (BoltDevice *mgr)
{

}

static void
bolt_device_get_property (GObject    *object,
                          guint       prop_id,
                          GValue     *value,
                          GParamSpec *pspec)
{
  BoltDevice *dev = BOLT_DEVICE (object);

  switch (prop_id)
    {
    case PROP_UID:
      g_value_set_string (value, dev->uid);
      break;

104 105 106 107 108 109 110 111
    case PROP_NAME:
      g_value_set_string (value, dev->name);
      break;

    case PROP_VENDOR:
      g_value_set_string (value, dev->vendor);
      break;

112 113 114 115
    case PROP_STATUS:
      g_value_set_uint (value, dev->status);
      break;

116 117 118 119
    case PROP_SYSFS:
      g_value_set_string (value, dev->syspath);
      break;

120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
    }
}

static void
bolt_device_set_property (GObject      *object,
                          guint         prop_id,
                          const GValue *value,
                          GParamSpec   *pspec)
{
  BoltDevice *dev = BOLT_DEVICE (object);

  switch (prop_id)
    {
    case PROP_UID:
      dev->uid = g_value_dup_string (value);
      break;

139
    case PROP_NAME:
140
      dev->name = g_value_dup_string (value);
141 142 143
      break;

    case PROP_VENDOR:
144
      dev->vendor = g_value_dup_string (value);
145 146
      break;

147 148 149 150
    case PROP_STATUS:
      dev->status = g_value_get_uint (value);
      break;

151 152 153 154
    case PROP_SYSFS:
      dev->syspath = g_value_dup_string (value);
      break;

155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
    }
}


static void
bolt_device_class_init (BoltDeviceClass *klass)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);

  gobject_class->finalize = bolt_device_finalize;

  gobject_class->get_property = bolt_device_get_property;
  gobject_class->set_property = bolt_device_set_property;

  g_object_class_override_property (gobject_class,
                                    PROP_UID,
                                    "uid");
174 175 176 177 178 179 180 181 182

  g_object_class_override_property (gobject_class,
                                    PROP_NAME,
                                    "name");

  g_object_class_override_property (gobject_class,
                                    PROP_VENDOR,
                                    "vendor");

183 184 185 186
  g_object_class_override_property (gobject_class,
                                    PROP_STATUS,
                                    "status");

187 188 189 190 191
  g_object_class_override_property (gobject_class,
                                    PROP_SYSFS,
                                    "sysfs-path");

}
192 193 194

/* internal methods */

195 196
static const char *
read_sysattr_name (struct udev_device *udev, const char *attr, GError **error)
197 198
{
  g_autofree char *s = NULL;
199
  const char *v;
200 201

  s = g_strdup_printf ("%s_name", attr);
202 203 204 205 206 207
  v = udev_device_get_sysattr_value (udev, s);

  if (v != NULL)
    return v;

  v = udev_device_get_sysattr_value (udev, attr);
208

209 210 211 212 213
  if (v == NULL) {
    g_set_error (error,
                 BOLT_ERROR, BOLT_ERROR_UDEV,
                 "failed to get sysfs attr: %s", attr);
  }
214 215

  return v;
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
static gint
read_sysfs_attr_int (struct udev_device *device, const char *attr)
{
  const char *str;
  char *end;
  gint64 val;

  str = udev_device_get_sysattr_value (device, attr);

  if (str == NULL)
    return 0;

  val = g_ascii_strtoull (str, &end, 0);

  if (str == end)
    return 0;

  if (val > G_MAXINT || val < G_MININT)
    {
      g_warning ("value read from sysfs outside of guint's range.");
      val = 0;
    }

  return (gint) val;
}

static gboolean
string_nonzero (const char *str)
{
  return str != NULL && str[0] != '\0';
}

static BoltStatus
bolt_status_from_udev (struct udev_device *udev)
{
  gint authorized;
  const char *key;
  gboolean have_key;

  authorized = read_sysfs_attr_int (udev, "authorized");

  if (authorized == 2)
    return BOLT_STATUS_AUTHORIZED_SECURE;

  key = udev_device_get_sysattr_value (udev, "key");
  have_key = string_nonzero (key);

  if (authorized == 1)
    {
      if (have_key)
        return BOLT_STATUS_AUTHORIZED_NEWKEY;
      else
        return BOLT_STATUS_AUTHORIZED;
    }
  else if (authorized == 0 && have_key)
    {
      return BOLT_STATUS_AUTH_ERROR;
    }

  return BOLT_STATUS_CONNECTED;
}

280 281 282 283 284 285 286
/* public methods */

BoltDevice *
bolt_device_new_for_udev (BoltManager        *mgr,
                          struct udev_device *udev,
                          GError            **error)
{
287 288 289 290
  const char *uid;
  const char *name;
  const char *vendor;
  const char *syspath;
291
  BoltDevice *dev;
292 293


294 295 296 297 298 299 300
  uid = udev_device_get_sysattr_value (udev, "unique_id");
  if (udev == NULL)
    {
      g_set_error (error, BOLT_ERROR, BOLT_ERROR_UDEV,
                   "could not get unique_id for udev");
      return NULL;
    }
301

302 303
  syspath = udev_device_get_syspath (udev);
  g_return_val_if_fail (syspath != NULL, NULL);
304

305
  name = read_sysattr_name (udev, "device", error);
306 307 308
  if (name == NULL)
    return NULL;

309
  vendor = read_sysattr_name (udev, "vendor", error);
310 311
  if (vendor == NULL)
    return NULL;
312 313

  dev = g_object_new (BOLT_TYPE_DEVICE,
314 315 316 317
                      "uid", uid,
                      "name", name,
                      "vendor", vendor,
                      "sysfs-path", syspath,
318 319
                      NULL);

320
  dev->status = bolt_status_from_udev (udev);
321

322 323 324
  g_object_add_weak_pointer (G_OBJECT (mgr),
                             (gpointer *) &dev->mgr);

325 326 327
  return dev;
}

328
const char *
329 330 331 332
bolt_device_export (BoltDevice      *device,
                    GDBusConnection *connection,
                    GError         **error)
{
333
  const char *path;
334
  gboolean ok;
335 336

  g_return_val_if_fail (BOLT_IS_DEVICE (device), FALSE);
337

338
  path = bolt_device_get_object_path (device);
339 340 341

  g_debug ("Exporting device at: %s", path);

342 343 344 345 346
  ok = g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON (device),
                                         connection,
                                         path,
                                         error);
  return ok ? path : NULL;
347 348 349 350 351
}

void
bolt_device_unexport (BoltDevice *device)
{
352 353 354 355 356
  const char *path;

  path = bolt_device_get_object_path (device);

  g_debug ("Unexporting device at: %s", path);
357 358
  g_dbus_interface_skeleton_unexport (G_DBUS_INTERFACE_SKELETON (device));
}
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376

const char *
bolt_device_get_object_path (BoltDevice *device)
{
  g_return_val_if_fail (BOLT_IS_DEVICE (device), FALSE);

  if (device->dbus_path == NULL)
    {
      char *path = NULL;

      path = g_strdup_printf ("/org/freedesktop/Bolt/devices/%s", device->uid);
      g_strdelimit (path, "-", '_');

      device->dbus_path = path;
    }

  return device->dbus_path;
}
377 378 379 380 381 382 383 384 385 386 387 388

const char *
bolt_device_get_uid (BoltDevice *dev)
{
  return dev->uid;
}

const char *
bolt_device_get_syspath (BoltDevice *dev)
{
  return dev->syspath;
}