Exemplo n.º 1
0
static int rsct_usbdev_scan_serial(char **devices, int i_devices,
				   rsct_usbdev_t **usbdev_list) {
  int i;

  for (i=0; i<i_devices; i++) {
    const char *udi=devices[i];

    if (libhal_device_exists(global_hal_context->ctx, udi, &(global_hal_context->dbus_error))) {
      char *busType;

      busType=libhal_device_get_property_string(global_hal_context->ctx, udi, "info.subsystem", NULL);
      if (busType==NULL)
	busType=libhal_device_get_property_string(global_hal_context->ctx, udi, "info.bus", NULL);
      if (busType) {
	if (strcasecmp(busType, "tty")==0) {
	  char *parent_udi;
  
	  /* ttyUSB device, get USB info from parent */
	  parent_udi=libhal_device_get_property_string(global_hal_context->ctx,
						       udi,
						       "info.parent",
						       NULL);
	  if (parent_udi) {
	    rsct_usbdev_t *d;
	    char *path;

            /* find device entry for parent */
	    d=rsct_usbdev_list_findByUDI(*usbdev_list, parent_udi);
	    if (d) {
              int port;

	      port=libhal_device_get_property_int(global_hal_context->ctx,
						  udi,
						  "serial.port",
						  NULL);
	      d->port=port;

	      path=libhal_device_get_property_string(global_hal_context->ctx,
						     udi,
						     "serial.device",
						     NULL);
	      if (path) {
		strncpy(d->deviceNodePath, path, sizeof(d->deviceNodePath)-1);
		d->deviceNodePath[sizeof(d->deviceNodePath)-1]=0;
		libhal_free_string(path);
	      }
	    }
	    libhal_free_string(parent_udi);
	  }
	  else {
	    fprintf(stderr, "RSCT: Parent for serial device not found\n");
	  }
	} /* if tty */
	libhal_free_string(busType);
      } /* if bus type */
    } /* if device exists */
  } /* for */

  return 0;
}
Exemplo n.º 2
0
static gboolean hal_get_ipod_usb_position (LibHalContext *ctx, const char *udi,
                                           int *usb_bus_number, int *usb_device_number)
{
	char *parent_udi;
	char *subsystem;
	gboolean found_ids;
	DBusError error;


	parent_udi = NULL;
	subsystem = NULL;
	found_ids = FALSE;
	dbus_error_init (&error);
	while (TRUE) {
		parent_udi = libhal_device_get_property_string (ctx, udi,
				"info.parent", &error);
		if (parent_udi == NULL || dbus_error_is_set (&error))
			goto end;
		udi = parent_udi;
		subsystem = libhal_device_get_property_string (ctx, udi,
							       "linux.subsystem",
							       &error);
		if (subsystem == NULL || dbus_error_is_set (&error)) {
			dbus_error_free (&error);
			dbus_error_init (&error);
			continue;
		}
		if (strcmp (subsystem, "usb") == 0) {
			*usb_bus_number = libhal_device_get_property_int (ctx, udi,
									  "usb.bus_number", &error);
			if (dbus_error_is_set (&error)) {
				goto end;
			}
			*usb_device_number = libhal_device_get_property_int (ctx, udi,
									     "usb.linux.device_number", &error);
			if (dbus_error_is_set (&error)) {
				goto end;
			}
			found_ids = TRUE;
			goto end;
		}
	}
end:
	libhal_free_string (parent_udi);
	libhal_free_string (subsystem);
	if (dbus_error_is_set (&error)) {
	    g_print ("Error: %s\n", error.message);
	    dbus_error_free (&error);
	}

	return found_ids;
}
static void
device_added_callback (LibHalContext *ctx, const char *udi)
{
	gchar *type_string;
	gchar *class_string;
	int type;
	const gchar *element;
	gchar *pipeline, *description;
	gboolean ignore;

	if (!libhal_device_query_capability (ctx, udi, "alsa", NULL)) {
		return;
	}

	/* filter out "digitizer", "modem", "none", "unknown" */
	class_string = libhal_device_get_property_string (ctx, udi, "alsa.pcm_class", NULL);
	ignore = class_string != NULL
		 && strcmp (class_string, "generic") != 0
		 && strcmp (class_string, "multi") != 0;
	libhal_free_string (class_string);
	if (ignore) {
		return;
	}

	type_string = libhal_device_get_property_string (ctx, udi, "alsa.type", NULL);
	if (strcmp (type_string, "playback") == 0) {
		type = AUDIO_PLAYBACK;
		element = "halaudiosink";
	} else if (strcmp (type_string, "capture") == 0) {
		type = AUDIO_CAPTURE;
		element = "halaudiosrc";
	} else {
		type = -1;
		element = NULL;
	}
	libhal_free_string (type_string);
	if (type == -1) {
		return;
	}

	pipeline = g_strdup_printf ("%s udi=%s", element, udi);
	description = libhal_device_get_property_string (ctx, udi, "alsa.device_id", NULL);

	add_device (type, pipeline, description, NULL);

	g_free (pipeline);
	libhal_free_string (description);
}
Exemplo n.º 4
0
void CHALManager::UpdateDevice(const char *udi)
{
  CSingleLock lock(m_lock);
  char *category;
  category = libhal_device_get_property_string(m_Context, udi, "info.category", NULL);
  if (category == NULL)
    return;

  if (strcmp(category, "volume") == 0)
  {
    CStorageDevice dev(udi);
    if (!DeviceFromVolumeUdi(udi, &dev))
      return;
    for (unsigned int i = 0; i < m_Volumes.size(); i++)
    {
      if (strcmp(m_Volumes[i].UDI.c_str(), udi) == 0)
      {
        CLog::Log(LOGDEBUG, "HAL: Update - %s | %s", CHALManager::StorageTypeToString(dev.Type),  dev.toString().c_str());
        if (g_advancedSettings.m_handleMounting)  // If the device was mounted by XBMC before it's still mounted by XBMC.
            dev.MountedByXBMC = m_Volumes[i].MountedByXBMC;
        if (!dev.Mounted && m_Volumes[i].Mounted)
          CGUIDialogKaiToast::QueueNotification(CGUIDialogKaiToast::Info, g_localizeStrings.Get(13023), dev.FriendlyName.c_str(), TOAST_DISPLAY_TIME, false);
        m_Volumes[i] = dev;

        break;
      }
    }
  }

  libhal_free_string(category);
}
Exemplo n.º 5
0
// Helper function. creates a CStorageDevice from a HAL udi
bool CHALManager::DeviceFromVolumeUdi(const char *udi, CStorageDevice *device)
{
  if (g_HalManager.m_Context == NULL)
    return false;

  LibHalVolume *tempVolume;
  LibHalDrive  *tempDrive;
  bool Created = false;

  tempVolume = libhal_volume_from_udi(g_HalManager.m_Context, udi);
  if (tempVolume)
  {
    const char *DriveUdi = libhal_volume_get_storage_device_udi(tempVolume);
    tempDrive = libhal_drive_from_udi(g_HalManager.m_Context, DriveUdi);

    if (tempDrive)
    {
      char * FriendlyName   = libhal_device_get_property_string(g_HalManager.m_Context, udi, "info.product", NULL);
      device->FriendlyName  = FriendlyName;
      libhal_free_string(FriendlyName);
      char *block = libhal_device_get_property_string(g_HalManager.m_Context, udi, "block.device", NULL);
      device->DevID         = block;
      libhal_free_string(block);

      device->HotPlugged  = (bool)libhal_drive_is_hotpluggable(tempDrive);
      device->Type        = libhal_drive_get_type(tempDrive);
      device->Mounted     = (bool)libhal_volume_is_mounted(tempVolume);
      device->MountPoint  = libhal_volume_get_mount_point(tempVolume);
      if (device->Mounted)
        URIUtils::AddSlashAtEnd(device->MountPoint);
      device->Label       = libhal_volume_get_label(tempVolume);
      device->UUID        = libhal_volume_get_uuid(tempVolume);
      device->FileSystem  = libhal_volume_get_fstype(tempVolume);
      device->HalIgnore   = libhal_device_get_property_bool(g_HalManager.m_Context, udi, "volume.ignore", NULL);
      ApproveDevice(device);

      libhal_drive_free(tempDrive);
      Created = true;
    }
    else
      CLog::Log(LOGERROR, "HAL: Couldn't create a Drive even if we had a volume - %s", udi);

    libhal_volume_free(tempVolume);
  }

  return Created;
}
Exemplo n.º 6
0
static char *
uuid_get_from_hal(void)
{
    LibHalContext *ctx;

    DBusError error;
    DBusConnection *con;

    dbus_error_init(&error);

    if (!(con = dbus_bus_get(DBUS_BUS_SYSTEM, &error)) ) {
        goto bailout_nobus;
    }

    ctx = libhal_ctx_new();
    libhal_ctx_set_dbus_connection(ctx, con);

    if (!libhal_ctx_init(ctx, &error)) {
        goto bailout;
    }

    if (!libhal_device_property_exists(ctx,
                                       UUID_PATH,
                                       UUID_PROPERTY,
                                       &error)) {
        goto bailout;
    }

    char *uuid  = libhal_device_get_property_string(ctx,
                                                    UUID_PATH,
                                                    UUID_PROPERTY,
                                                    &error);
    if (looks_like_a_uuid (uuid)) {
        return uuid;
    }

 bailout:
    {
        DBusError ctxerror;
        dbus_error_init(&ctxerror);
        if (!(libhal_ctx_shutdown(ctx, &ctxerror))) {
            dbus_error_free(&ctxerror);
        }
    }

    libhal_ctx_free(ctx);
    //dbus_connection_unref(con);

 bailout_nobus:
    if (dbus_error_is_set(&error)) {
        /*printf("Error %s\n", error.name);*/
        dbus_error_free(&error);
    }
    return NULL;
}
Exemplo n.º 7
0
static void
copykeyval (char *key, char *compat_key)
{
	char *value;

	value = libhal_device_get_property_string (hfp_ctx, hfp_udi, key, NULL);
	if (value != NULL) {
		hfp_info ("Copying %s -> %s", key, compat_key);
		libhal_device_set_property_string (hfp_ctx, hfp_udi, compat_key, value, NULL);
	}
}
Exemplo n.º 8
0
static char *
get_prop_string(LibHalContext *hal_ctx, const char *udi, const char *name)
{
    char *prop, *ret;

    prop = libhal_device_get_property_string(hal_ctx, udi, name, NULL);
    DebugF(" [config/hal] getting %s on %s returned %s\n", name, udi, prop);
    if (prop) {
        ret = xstrdup(prop);
        libhal_free_string(prop);
    }
    else {
        return NULL;
    }

    return ret;
}
Exemplo n.º 9
0
static char *
get_prop_string(LibHalContext *hal_ctx, const char *udi, const char *name)
{
    char *prop, *ret;

    prop = libhal_device_get_property_string(hal_ctx, udi, name, NULL);
    LogMessageVerb(X_INFO, 10, "config/hal: getting %s on %s returned %s\n", name, udi, prop ? prop : "(null)");
    if (prop) {
        ret = xstrdup(prop);
        libhal_free_string(prop);
    }
    else {
        return NULL;
    }

    return ret;
}
Exemplo n.º 10
0
static int get_str_prop(LibHalContext *ctxt, const char *udi,
                        const char *prop, char **val_p)
{
    char *val = libhal_device_get_property_string(ctxt, udi, prop, NULL);

    if (val) {
        if (*val) {
            *val_p = val;
            return 0;
        } else {
            /* Treat empty strings as NULL values */
            VIR_FREE(val);
        }
    }

    return -1;
}
Exemplo n.º 11
0
int
open_device(LibHalContext *ctx, char *udi)
{
	char path[HAL_PATH_MAX] = "/devices";
	char *devfs_path;
	DBusError error;

	dbus_error_init(&error);
	devfs_path = libhal_device_get_property_string(ctx, udi,
	    "solaris.devfs_path", &error);
	my_dbus_error_free(&error);
	if (devfs_path == NULL) {
		return (-1);
	}
	strlcat(path, devfs_path, HAL_PATH_MAX);
	libhal_free_string(devfs_path);
	return (open(path, O_RDONLY | O_NONBLOCK));
}
Exemplo n.º 12
0
Arquivo: eject.c Projeto: pzanoni/tray
static void hal_property_modified(LibHalContext *ctx, const char *udi,
	  const char *key, dbus_bool_t is_removed, dbus_bool_t is_added)
{

	if (!strcmp(key, "volume.is_mounted")) {
		if (libhal_device_get_property_bool(ctx, udi, "volume.is_mounted", NULL)) {
			char *mountpoint = libhal_device_get_property_string(
					ctx, udi, "volume.mount_point", NULL);
			
			if (!strncmp(MEDIA_DIR, mountpoint, strlen(MEDIA_DIR))) {
				add_mount(udi, mountpoint);
			}

			if (mountpoint)
				libhal_free_string(mountpoint);
		} else {
			remove_mount(udi);
		}

		update_status();
	}
}
Exemplo n.º 13
0
static boolean_t
volume_should_mount(const char *udi)
{
	char	*storage_device = NULL;
	int	ret = B_FALSE;

	if (libhal_device_get_property_bool(hal_ctx, udi,
	    "volume.ignore", NULL)) {
		goto out;
	}

	/* get the backing storage device */
	if (!(storage_device = libhal_device_get_property_string(hal_ctx, udi,
	    "block.storage_device", NULL))) {
		dprintf("cannot get block.storage_device\n");
		goto out;
	}

	/* we handle either removable or hotpluggable */
	if (!libhal_device_get_property_bool(hal_ctx, storage_device,
	    "storage.removable", NULL) &&
	    !libhal_device_get_property_bool(hal_ctx, storage_device,
	    "storage.hotpluggable", NULL)) {
		goto out;
	}

	/* ignore if claimed by another volume manager */
	if (libhal_device_get_property_bool(hal_ctx, storage_device,
	    "info.claimed", NULL)) {
		goto out;
	}

	ret = B_TRUE;

out:
	libhal_free_string(storage_device);
	return (ret);
}
Exemplo n.º 14
0
static void
device_added_callback( LibHalContext *ctx, const char *udi )
{
  if( libhal_device_property_exists( ctx, udi, "battery", NULL ) )
  {
    char *type = libhal_device_get_property_string( ctx, udi,
                                                    "battery.type",
                                                    NULL );

    if( type )
    {
      /* We only track 'primary' batteries (ie: to avoid monitoring
       * batteries in cordless mice or UPSes etc.)
       */
      if( !strcmp( type, "primary" ) )
        add_to_list( ctx, &batteries, udi, sizeof (struct battery_info) );

      libhal_free_string( type );
    }
  }

  if( libhal_device_property_exists( ctx, udi, "ac_adapter", NULL ) )
    add_to_list( ctx, &adaptors, udi, sizeof (struct adaptor_info) );
}
Exemplo n.º 15
0
/* Parse newly found device and add it to our remembered devices */
void CHALManager::AddDevice(const char *udi)
{
  CSingleLock lock(m_lock);
  char *category;
  category = libhal_device_get_property_string(m_Context, udi, "info.category", NULL);
  if (category == NULL)
    return;

  if (strcmp(category, "volume") == 0)
  {
    CStorageDevice dev(udi);
    if (DeviceFromVolumeUdi(udi, &dev))
    {
      CLog::Log(LOGDEBUG, "HAL: Added - %s | %s", CHALManager::StorageTypeToString(dev.Type),  dev.toString().c_str());
      HandleNewVolume(&dev);
      m_Volumes.push_back(dev);
    }
  }
#if defined(HAS_SDL_JOYSTICK)
  // Scan input devices
  else if (strcmp(category, "input") == 0)
  {
    DBusError dbusError;
    dbus_error_init(&dbusError);

    char **capability;
    capability =libhal_device_get_property_strlist (m_Context, udi, "info.capabilities", &dbusError);
    for(char **ptr = capability; *ptr != NULL;ptr++)
    {
      // Reload joysticks
      if(strcmp(*ptr, "input.joystick") == 0)
      {
        CLog::Log(LOGINFO, "HAL: Joystick plugged in");
        CHALDevice dev = CHALDevice(udi);
        dev.FriendlyName = libhal_device_get_property_string(m_Context, udi, "info.product", &m_Error);
        m_Joysticks.push_back(dev);

        if(m_Joysticks.size() < 2 || m_bMultipleJoysticksSupport)
        {
          // Restart SDL joystick subsystem
          if (!g_Joystick.Reinitialize())
            break;

          if (m_Notifications)
            CGUIDialogKaiToast::QueueNotification(CGUIDialogKaiToast::Info, g_localizeStrings.Get(13024), dev.FriendlyName.c_str(), TOAST_DISPLAY_TIME, false);
        }
      }
    }
    libhal_free_string_array(capability);
  }
#endif
/*
  else if (strcmp(category, "camera") == 0)
  { // PTP-Devices }
  else if (strcmp(category, "bluetooth_hci") == 0)
  { // Bluetooth-Devices }
  else if (strcmp(category, "portable audio player") == 0)
  { // MTP-Devices }
  else if (strcmp(category, "alsa") == 0)
  { //Alsa Devices }
*/

  libhal_free_string(category);
}
Exemplo n.º 16
0
gboolean
check_libhal (const char *server_addr)
{
	pid_t child_pid;

	child_pid = fork ();
	if (child_pid == -1) {
		printf ("Cannot fork\n");
		exit (1);
	} else if (child_pid == 0) {
		DBusError error;
		DBusConnection *conn;
		LibHalContext *ctx;
		dbus_bool_t passed;

		printf ("server address='%s'\n", server_addr);

		dbus_error_init (&error);
		if ((conn = dbus_connection_open (server_addr, &error)) == NULL) {
			printf ("Error connecting to server: %s\n", error.message);
			goto fail;
		}

		dbus_connection_setup_with_g_main (conn, NULL);

		if ((ctx = libhal_ctx_new ()) == NULL) {
			printf ("Error getting libhal context\n");
			goto fail;
		}

		
		libhal_ctx_set_dbus_connection (ctx, conn);

		libhal_ctx_init (ctx, &error);

		if (dbus_error_is_set (&error)) {
			printf ("FAILED98: %s\n", error.message);			
			goto fail;
		}
		printf ("SUCCESS98\n");

		libhal_device_print (ctx, "/org/freedesktop/Hal/devices/testobj1", &error);


		if (dbus_error_is_set (&error)) {
			printf ("FAILED99: %s\n", error.message);			
			goto fail;
		}
		printf ("SUCCESS99\n");

		passed = FALSE;

		{
			char *val;
			
			val = libhal_device_get_property_string (ctx, "/org/freedesktop/Hal/devices/testobj1", "test.string", &error);

			if (val == NULL || strcmp (val, "fooooobar22") != 0 || dbus_error_is_set (&error)) {
				libhal_free_string (val);
				printf ("FAILED100\n");			
				goto fail;
			}
			printf ("SUCCESS100\n");
			libhal_free_string (val);
		}

		{
			char *val;
			val = libhal_device_get_property_string (ctx, "/org/freedesktop/Hal/devices/testobj1", "test.string2", &error);
			if (val == NULL || strcmp (val, "fooøةמ") != 0 || dbus_error_is_set (&error)) {
				libhal_free_string (val);
				printf ("FAILED101: %s\n", error.message);			
				goto fail;
			}
			libhal_free_string (val);
			printf ("SUCCESS101\n");
		}

		{
			dbus_bool_t b;
			b = libhal_device_get_property_bool (
			    ctx, "/org/freedesktop/Hal/devices/testobj1", 
			    "test.bool", &error);
			    
			if (!b || dbus_error_is_set (&error)) {
				printf ("FAILED102: %s, %i\n", error.message, b);			
				goto fail;
			}
			printf ("SUCCESS102\n");
		}

		{
			double val;
			double expected_val = 0.53434343;

			val = libhal_device_get_property_double (ctx, "/org/freedesktop/Hal/devices/testobj1", 
								 "test.double", &error);
			if ( memcmp (&val, &expected_val, sizeof (double)) != 0 || dbus_error_is_set (&error)) {
				printf ("FAILED103\n");
				goto fail;
			}
			printf ("SUCCESS103\n");
		}

		if (libhal_device_get_property_uint64 (
			    ctx, "/org/freedesktop/Hal/devices/testobj1", "test.uint64", &error) != 
		    ((((dbus_uint64_t)1)<<35) + 5) ||
		    dbus_error_is_set (&error)) {
			printf ("FAILED104: %s\n", error.message);			
			goto fail;
		}
		printf ("SUCCESS104\n");
		
		{
			char **val;
			val = libhal_device_get_property_strlist (ctx, "/org/freedesktop/Hal/devices/testobj1", "test.strlist", &error);
			if (val == NULL ||  dbus_error_is_set (&error)) {
				libhal_free_string_array (val);
				printf ("FAILED105: %s\n", error.message);			
				goto fail;
			}
			printf ("SUCCESS105\n");
			
			if (libhal_string_array_length (val) != 2) {
				libhal_free_string_array (val);
				printf ("FAILED106\n");			
				goto fail;
			}
			printf ("SUCCESS106\n");

			if (strcmp (val[0], "foostrlist2") != 0 ||
			    strcmp (val[1], "foostrlist3") != 0) {
				libhal_free_string_array (val);
				printf ("FAILED107\n");			
				goto fail;
			}
			printf ("SUCCESS107\n");

			libhal_free_string_array (val);
		}

		if (libhal_device_get_property_int (
			    ctx, "/org/freedesktop/Hal/devices/testobj1", "test.int", &error) != 42 ||
		    dbus_error_is_set (&error)) {
			printf ("FAILED108\n");			
			goto fail;
		}
		printf ("SUCCESS108\n");
	
		/* tests for libhal_psi */
		{
			int type;
			char *key;
			LibHalPropertySet *pset;
			LibHalPropertySetIterator it;
			unsigned int psi_num_passed;
			unsigned int psi_num_elems;

			if ((pset = libhal_device_get_all_properties (ctx, "/org/freedesktop/Hal/devices/testobj1", 
								      &error)) == NULL)
				return FALSE;
			printf ("SUCCESS110\n");

			psi_num_passed = 0;
			psi_num_elems = libhal_property_set_get_num_elems (pset);

			for (libhal_psi_init (&it, pset); libhal_psi_has_more (&it); libhal_psi_next (&it)) {
				type = libhal_psi_get_type (&it);
				key = libhal_psi_get_key (&it);

				switch (type) {
				case LIBHAL_PROPERTY_TYPE_STRING:
					if (strcmp (key, "info.udi") == 0) {
						if (strcmp (libhal_psi_get_string (&it), 
							    "/org/freedesktop/Hal/devices/testobj1") == 0) {
							psi_num_passed++;
						} else {
							printf ("fail on %s\n", key);
						}
					} else if (strcmp (key, "test.string") == 0) {
						if (strcmp (libhal_psi_get_string (&it), 
							    "fooooobar22") == 0) {
							psi_num_passed++;
						} else {
							printf ("fail on %s\n", key);
						}
					} else if (strcmp (key, "test.string2") == 0) {
						if (strcmp (libhal_psi_get_string (&it), 
							    "fooøةמ") == 0) {
							psi_num_passed++;
						} else {
							printf ("fail on %s\n", key);
						}
					}
					break;

				case LIBHAL_PROPERTY_TYPE_INT32:
					if (strcmp (key, "test.int") == 0) {
						if (libhal_psi_get_int (&it) == 42)
							psi_num_passed++;
						else
							printf ("fail on %s\n", key);
					}
					break;

				case LIBHAL_PROPERTY_TYPE_UINT64:
					if (strcmp (key, "test.uint64") == 0) {
						if (libhal_psi_get_uint64 (&it) == ((((dbus_uint64_t)1)<<35) + 5))
							psi_num_passed++;
						else
							printf ("fail on %s\n", key);
					}
					break;

				case LIBHAL_PROPERTY_TYPE_BOOLEAN:
					if (strcmp (key, "test.bool") == 0) {
						if (libhal_psi_get_bool (&it) == TRUE)
							psi_num_passed++;
						else
							printf ("fail on %s\n", key);
					}
					break;

				case LIBHAL_PROPERTY_TYPE_DOUBLE:
					if (strcmp (key, "test.double") == 0) {
						double val = 0.53434343;
						double val2;

						val2 = libhal_psi_get_double (&it);
						if (memcmp (&val, &val2, sizeof (double)) == 0)
							psi_num_passed++;
						else
							printf ("fail on %s\n", key);
					}
					break;

				case LIBHAL_PROPERTY_TYPE_STRLIST:
					if (strcmp (key, "test.strlist") == 0) {
						char **val;
					
						val = libhal_psi_get_strlist (&it);
						if (libhal_string_array_length (val) == 2 &&
						    strcmp (val[0], "foostrlist2") == 0 &&
						    strcmp (val[1], "foostrlist3") == 0 &&
						    val[2] == NULL)
							psi_num_passed++;
						else
							printf ("fail on %s\n", key);
					}
					break;

				default:
					printf ("    *** unknown type for key %s\n", key);
					break;
				}
			}
			libhal_free_property_set (pset);

			if (psi_num_passed != psi_num_elems) {
				printf ("FAILED111\n");			
				goto fail;
			}
			printf ("SUCCESS111\n");			


		} /* end libhal_test_psi */

	
		printf ("Passed all libhal tests\n");
		passed = TRUE;
		
	fail:
		if (dbus_error_is_set (&error))
			dbus_error_free (&error);
			
		send_tests_done (conn, passed);
		exit (1);

	} else {
		printf ("child pid=%d\n", child_pid);
	}
	return TRUE;
}
int
main (int argc, char *argv[])
{
	DBusError err;
	int retval = 0;

	hal_set_proc_title_init (argc, argv);

	setup_logger ();	

	device_udi = getenv ("UDI");

	HAL_DEBUG (("device:[%s]", device_udi));
	if (device_udi == NULL) {
		HAL_ERROR (("No device specified"));
		return -2;
	}

	dbus_error_init (&err);
	if ((halctx = libhal_ctx_init_direct (&err)) == NULL) {
		HAL_ERROR (("Cannot connect to hald"));
		retval = -3;
		goto out;
	}


	/* update_properties */
	libhal_device_set_property_bool (halctx, device_udi, 
			"battery.present", TRUE, &err);

	LIBHAL_FREE_DBUS_ERROR (&err);
	if (!libhal_device_property_exists (halctx, device_udi, 
			"battery.is_rechargeable", &err)) {
		LIBHAL_FREE_DBUS_ERROR (&err);
		libhal_device_set_property_bool (halctx, device_udi, 
			"battery.is_rechargeable", FALSE, &err);
	}

	LIBHAL_FREE_DBUS_ERROR (&err);
	libhal_device_set_property_int (halctx, device_udi, 
			"battery.charge_level.design", 7, &err);
	LIBHAL_FREE_DBUS_ERROR (&err);
	libhal_device_set_property_int (halctx, device_udi, 
			"battery.charge_level.last_full", 7, &err);
	LIBHAL_FREE_DBUS_ERROR (&err);
	libhal_device_set_property_string (halctx, device_udi, 
			"info.category", "battery", &err);
	LIBHAL_FREE_DBUS_ERROR (&err);
	libhal_device_set_property_string (halctx, device_udi, 
			"battery.command_interface", "csr", &err);

	/* monitor change */
	libhal_ctx_set_device_property_modified (halctx, property_modified);

	/* Initial fillup */
	dev_props = property_cache_item_get (device_udi);
	HAL_ERROR (("** Initial fillup done"));

	/* init usb */
	usb_init ();
	
	/* do coldplug */
	check_all_batteries (NULL);

	/* only add capability when initial charge_level key has been set */
	LIBHAL_FREE_DBUS_ERROR (&err);
	libhal_device_add_capability (halctx, device_udi, "battery", &err);

	LIBHAL_FREE_DBUS_ERROR (&err);
	if (!libhal_device_addon_is_ready (halctx, device_udi, &err)) {
		retval = -4;
		goto out;
	}

	hal_set_proc_title ("hald-addon-usb-csr: listening on '%s'", 
			    libhal_device_get_property_string(halctx, device_udi,
							      "info.product", &err));

	main_loop = g_main_loop_new (NULL, FALSE);
#ifdef HAVE_GLIB_2_14
	g_timeout_add_seconds (TIMEOUT, check_all_batteries, NULL);
#else
	g_timeout_add (1000L * TIMEOUT, check_all_batteries, NULL);
#endif
	g_main_loop_run (main_loop);
	return 0;

out:
        HAL_DEBUG (("An error occured, exiting cleanly"));

        LIBHAL_FREE_DBUS_ERROR (&err);

        if (halctx != NULL) {
                libhal_ctx_shutdown (halctx, &err);
                LIBHAL_FREE_DBUS_ERROR (&err);
                libhal_ctx_free (halctx);
        }

        return retval;
}
Exemplo n.º 18
0
int
prt_dev_props(LibHalContext *hal_ctx, char *dev_name)
{
	LibHalPropertySet *props;
	LibHalPropertySetIterator it;
	DBusError error;
	int type;
	char *devfs_path;

	dbus_error_init(&error);

	if (!(props =
		libhal_device_get_all_properties(hal_ctx, dev_name, &error))) {
		fprintf(stderr, "%s: %s\n", error.name, error.message);
		dbus_error_free(&error);
		return (1);
	}

	devfs_path = libhal_device_get_property_string(hal_ctx,
			dev_name, "solaris.devfs_path", &error);
	if (devfs_path) {
		printf("devfs path:%s\n", devfs_path);
		libhal_free_string(devfs_path);
	}

	for (libhal_psi_init(&it, props); libhal_psi_has_more(&it);
			libhal_psi_next(&it)) {
		type = libhal_psi_get_type(&it);
		switch (type) {
			case LIBHAL_PROPERTY_TYPE_STRING:
				printf("%s:'%s'\n",
					libhal_psi_get_key(&it),
					libhal_psi_get_string(&it));
				break;
			case LIBHAL_PROPERTY_TYPE_INT32:
				printf("%s:%d\n",
					libhal_psi_get_key(&it),
					libhal_psi_get_int(&it));
				break;
			case LIBHAL_PROPERTY_TYPE_UINT64:
				printf("%s:%lld\n",
					libhal_psi_get_key(&it),
				(long long) libhal_psi_get_uint64(&it));
				break;
			case LIBHAL_PROPERTY_TYPE_DOUBLE:
				printf("%s:%g\n",
					libhal_psi_get_key(&it),
					libhal_psi_get_double(&it));
				break;
			case LIBHAL_PROPERTY_TYPE_BOOLEAN:
				printf("%s:%s\n",
				libhal_psi_get_key(&it),
				libhal_psi_get_bool(&it) ? "true" : "false");
				break;
			case LIBHAL_PROPERTY_TYPE_STRLIST:
				{
					char **strlist;

					printf("%s:{ ",
					libhal_psi_get_key(&it));
					strlist = libhal_psi_get_strlist(&it);
					while (*strlist) {
						printf("'%s'%s",
					*strlist, strlist[1] ? ", " : "");
						strlist++;
					}
					printf(" }\n");
				}
				break;
			default:
				printf("Unknown type:%d=0x%02x\n",
					type, type);
				break;
			}
	}

	libhal_free_property_set(props);
	printf("\n");
	dbus_error_free(&error);
	return (0);
}
Exemplo n.º 19
0
/*
 * gst_hal_get_oss_element:
 * @ctx: a #LibHalContext which should be used for querying HAL.
 * @udi: a #gchar corresponding to the UDI you want to get.
 * @device_type: a #GstHalDeviceType specifying the wanted device type.
 *
 * Get Hal UDI @udi's string value.
 *
 * Returns: a newly allocated #gchar string containing the appropriate pipeline
 * for UDI @udi, or NULL in the case of an error..
 */
static gchar *
gst_hal_get_oss_element (LibHalContext * ctx, const gchar * udi,
    GstHalDeviceType device_type)
{
  char *type, *element = NULL, *string = NULL;
  DBusError error;

  dbus_error_init (&error);

  if (!libhal_device_query_capability (ctx, udi, "oss", &error)) {
    if (dbus_error_is_set (&error)) {
      GST_DEBUG ("Failed querying %s for oss capability: %s: %s", udi,
          error.name, error.message);
      LIBHAL_FREE_DBUS_ERROR (&error);
    } else {
      GST_DEBUG ("UDI %s has no oss capability", udi);
    }
    return NULL;
  }

  type = libhal_device_get_property_string (ctx, udi, "oss.type", &error);
  if (dbus_error_is_set (&error)) {
    GST_DEBUG ("UDI %s has oss capabilities but no oss.type property: %s, %s",
        udi, error.name, error.message);
    LIBHAL_FREE_DBUS_ERROR (&error);
    return NULL;
  } else if (!type) {
    GST_DEBUG ("UDI %s has empty oss.type property", udi);
    return NULL;
  }

  if (strcmp (type, "pcm") == 0) {
    if (device_type == GST_HAL_AUDIOSINK)
      element = "osssink";
    else if (device_type == GST_HAL_AUDIOSRC)
      element = "osssrc";
  }
  libhal_free_string (type);

  if (element) {
    char *device = NULL;

    device =
        libhal_device_get_property_string (ctx, udi, "oss.device_file", &error);
    if (dbus_error_is_set (&error)) {
      GST_DEBUG
          ("UDI %s has oss capabilities but no oss.device_file property: %s, %s",
          udi, error.name, error.message);
      LIBHAL_FREE_DBUS_ERROR (&error);
      return NULL;
    } else if (!device) {
      GST_DEBUG ("UDI %s has empty oss.device_file property", udi);
      return NULL;
    }

    string = g_strdup_printf ("%s device=%s", element, device);
    libhal_free_string (device);
  }

  return string;
}
Exemplo n.º 20
0
	std::map<int, std::pair<std::string, std::string> > SerialPortEnumerator::getPorts()
	{
		std::map<int, std::pair<std::string, std::string> > ports;
		


#ifdef MACOSX
		// use IOKit to enumerates devices
		
		// get a matching dictionary to specify which IOService class we're interested in
		CFMutableDictionaryRef classesToMatch = IOServiceMatching(kIOSerialBSDServiceValue);
		if (classesToMatch == NULL)
			throw DashelException(DashelException::EnumerationError, 0, "IOServiceMatching returned a NULL dictionary");
		
		// specify all types of serial devices
		CFDictionarySetValue(classesToMatch, CFSTR(kIOSerialBSDTypeKey), CFSTR(kIOSerialBSDAllTypes));
		
		// get an iterator to serial port services
		io_iterator_t matchingServices;
		kern_return_t kernResult = IOServiceGetMatchingServices(kIOMasterPortDefault, classesToMatch, &matchingServices);    
		if (KERN_SUCCESS != kernResult)
			throw DashelException(DashelException::EnumerationError, kernResult, "IOServiceGetMatchingServices failed");
			
		// iterate over services
		io_object_t modemService;
		int index = 0;
		while((modemService = IOIteratorNext(matchingServices)))
		{
			// get path for device
			CFTypeRef bsdPathAsCFString = IORegistryEntryCreateCFProperty(modemService, CFSTR(kIOCalloutDeviceKey), kCFAllocatorDefault, 0);
			if (bsdPathAsCFString)
			{
				std::string path;
				char cStr[255];
				std::string name;
				
				bool res = CFStringGetCString((CFStringRef) bsdPathAsCFString, cStr, 255, kCFStringEncodingUTF8);
				if(res)
					path = cStr;
				else
					throw DashelException(DashelException::EnumerationError, 0, "CFStringGetCString failed");
				
				CFRelease(bsdPathAsCFString);
				
				CFTypeRef fn = IORegistryEntrySearchCFProperty(modemService, kIOServicePlane, CFSTR("USB Product Name"), kCFAllocatorDefault,
										kIORegistryIterateRecursively | kIORegistryIterateParents);
				if(fn) {
					res = CFStringGetCString((CFStringRef) fn, cStr, 255, kCFStringEncodingUTF8);
					if(res) 
						name = cStr;
					else
						throw DashelException(DashelException::EnumerationError, 0, "CFStringGetString failed");

					CFRelease(fn);
				} else 
					name = "Serial Port";
				name = name + " (" + path + ")";
				ports[index++] = std::make_pair<std::string, std::string>(path, name);
			}
			else
				throw DashelException(DashelException::EnumerationError, 0, "IORegistryEntryCreateCFProperty returned a NULL path");
			
			// release service
			IOObjectRelease(modemService);
		}

		IOObjectRelease(matchingServices);

			
#elif defined(USE_LIBUDEV)

		struct udev *udev;
		struct udev_enumerate *enumerate;
		struct udev_list_entry *devices, *dev_list_entry;
		struct udev_device *dev;
		int index = 0;

		udev = udev_new();
		if(!udev)
			throw DashelException(DashelException::EnumerationError, 0, "Cannot create udev context");

		enumerate = udev_enumerate_new(udev);
		udev_enumerate_add_match_subsystem(enumerate, "tty");
		udev_enumerate_scan_devices(enumerate);
		devices = udev_enumerate_get_list_entry(enumerate);

		udev_list_entry_foreach(dev_list_entry, devices) {
			const char *sysfs_path;
			struct udev_device *usb_dev;
			const char * path;
			struct stat st;
			unsigned int maj,min;

			/* Get sysfs path and create the udev device */
			sysfs_path = udev_list_entry_get_name(dev_list_entry);
			dev = udev_device_new_from_syspath(udev, sysfs_path);

			// Some sanity check
			path = udev_device_get_devnode(dev);
			if(stat(path, &st)) 
				throw DashelException(DashelException::EnumerationError, 0, "Cannot stat serial port");
			
			if(!S_ISCHR(st.st_mode))
				throw DashelException(DashelException::EnumerationError, 0, "Serial port is not character device");

			// Get the major/minor number
			maj = major(st.st_rdev);
			min = minor(st.st_rdev);

			// Ignore all the non physical ports
			if(!(maj == 2 || (maj == 4 && min < 64) || maj == 3 || maj == 5)) {
				ostringstream oss;

				// Check if usb, if yes get the device name
				usb_dev = udev_device_get_parent_with_subsystem_devtype(dev,"usb","usb_device");
				if(usb_dev)
					oss << udev_device_get_sysattr_value(usb_dev,"product");
				else
					oss << "Serial Port";

				oss << " (" << path << ")";

				ports[index++] = std::make_pair<std::string, std::string>(path,oss.str());
			}
			udev_device_unref(dev);
		}
		
		udev_enumerate_unref(enumerate);

		udev_unref(udev);

#elif defined(USE_HAL)

		// use HAL to enumerates devices
		DBusConnection* dbusConnection = dbus_bus_get(DBUS_BUS_SYSTEM, 0);
		if (!dbusConnection)
			throw DashelException(DashelException::EnumerationError, 0, "cannot connect to D-BUS.");
		
		LibHalContext* halContext = libhal_ctx_new();
		if (!halContext)
			throw DashelException(DashelException::EnumerationError, 0, "cannot create HAL context: cannot create context");
		if (!libhal_ctx_set_dbus_connection(halContext, dbusConnection))
			throw DashelException(DashelException::EnumerationError, 0, "cannot create HAL context: cannot connect to D-BUS");
		if (!libhal_ctx_init(halContext, 0))
			throw DashelException(DashelException::EnumerationError, 0, "cannot create HAL context: cannot init context");
		
		int devicesCount;
		char** devices = libhal_find_device_by_capability(halContext, "serial", &devicesCount, 0);
		for (int i = 0; i < devicesCount; i++)
		{
			char* devFileName = libhal_device_get_property_string(halContext, devices[i], "serial.device", 0);
			char* info = libhal_device_get_property_string(halContext, devices[i], "info.product", 0);
			int port = libhal_device_get_property_int(halContext, devices[i], "serial.port", 0);
			
			ostringstream oss;
			oss << info << " " << port;
			ports[devicesCount - i] = std::make_pair<std::string, std::string>(devFileName, oss.str());
			
			libhal_free_string(info);
			libhal_free_string(devFileName);
		}
		
		libhal_free_string_array(devices);
		libhal_ctx_shutdown(halContext, 0);
		libhal_ctx_free(halContext);
#endif
		
		return ports;
	};
$NetBSD$

--- src/modules/module-hal-detect.c.orig	2012-07-19 11:29:39.000000000 +0000
+++ src/modules/module-hal-detect.c
@@ -334,6 +334,11 @@ static int hal_device_load_oss(struct us
 
     /* We store only one entry per card, hence we look for the originating device */
     originating_udi = libhal_device_get_property_string(u->context, udi, "oss.originating_device", &error);
+    if (dbus_error_is_set(&error) || !originating_udi) {
+	if (dbus_error_is_set(&error))
+            dbus_error_free(&error);
+	originating_udi = libhal_device_get_property_string(u->context, udi, "sound.originating_device", &error);
+    }
     if (dbus_error_is_set(&error) || !originating_udi)
         goto fail;
 
Exemplo n.º 22
0
static void
force_unmount (LibHalContext *ctx, const char *udi)
{
	DBusError error;
	DBusMessage *msg = NULL;
	DBusMessage *reply = NULL;
	char **options = NULL;
	unsigned int num_options = 0;
	DBusConnection *dbus_connection;
	char *device_file;

	dbus_error_init (&error);

	dbus_connection = libhal_ctx_get_dbus_connection (ctx);

	msg = dbus_message_new_method_call ("org.freedesktop.Hal", udi,
					    "org.freedesktop.Hal.Device.Volume",
					    "Unmount");
	if (msg == NULL) {
		HAL_DEBUG (("Could not create dbus message for %s", udi));
		goto out;
	}


	options = calloc (1, sizeof (char *));
	if (options == NULL) {
		HAL_DEBUG (("Could not allocate options array"));
		goto out;
	}

	device_file = libhal_device_get_property_string (ctx, udi, "block.device", &error);
	if (device_file != NULL) {
		libhal_free_string (device_file);
	}
	dbus_error_free (&error);

	if (!dbus_message_append_args (msg, 
				       DBUS_TYPE_ARRAY, DBUS_TYPE_STRING, &options, num_options,
				       DBUS_TYPE_INVALID)) {
		HAL_DEBUG (("Could not append args to dbus message for %s", udi));
		goto out;
	}
	
	if (!(reply = dbus_connection_send_with_reply_and_block (dbus_connection, msg, -1, &error))) {
		HAL_DEBUG (("Unmount failed for %s: %s : %s\n", udi, error.name, error.message));
		goto out;
	}

	if (dbus_error_is_set (&error)) {
		HAL_DEBUG (("Unmount failed for %s\n%s : %s\n", udi, error.name, error.message));
		goto out;
	}

	HAL_DEBUG (("Succesfully unmounted udi '%s'", udi));

out:
	dbus_error_free (&error);
	if (options != NULL)
		free (options);
	if (msg != NULL)
		dbus_message_unref (msg);
	if (reply != NULL)
		dbus_message_unref (reply);
}
Exemplo n.º 23
0
/** Entry point
 *
 *  @param  argc                Number of arguments given to program
 *  @param  argv                Arguments given to program
 *  @return                     Return code
 */
int
main (int argc, char *argv[])
{
	char *udi = NULL;
	char *key = NULL;
	int type;
	dbus_bool_t is_hex = FALSE;
	dbus_bool_t is_verbose = FALSE;
	dbus_bool_t is_version = FALSE;
	char *str;
	DBusError error;

	if (argc <= 1) {
		usage (argc, argv);
		return 1;
	}

	while (1) {
		int c;
		int option_index = 0;
		const char *opt;
		static struct option long_options[] = {
			{"udi", 1, NULL, 0},
			{"key", 1, NULL, 0},
			{"hex", 0, NULL, 0},
			{"verbose", 0, NULL, 0},
			{"version", 0, NULL, 0},
			{"help", 0, NULL, 0},
			{NULL, 0, NULL, 0}
		};

		c = getopt_long (argc, argv, "",
				 long_options, &option_index);
		if (c == -1)
			break;

		switch (c) {
		case 0:
			opt = long_options[option_index].name;

			if (strcmp (opt, "help") == 0) {
				usage (argc, argv);
				return 0;
			} else if (strcmp (opt, "hex") == 0) {
				is_hex = TRUE;
			} else if (strcmp (opt, "verbose") == 0) {
				is_verbose = TRUE;
			} else if (strcmp (opt, "version") == 0) {
				is_version = TRUE;
			} else if (strcmp (opt, "key") == 0) {
				key = strdup (optarg);
			} else if (strcmp (opt, "udi") == 0) {
				udi = strdup (optarg);
			}
			break;

		default:
			usage (argc, argv);
			return 1;
			break;
		}
	}

	if (is_version) {
		printf ("hal-get-property " PACKAGE_VERSION "\n");
		return 0;
	}

	if (udi == NULL || key == NULL) {
		usage (argc, argv);
		return 1;
	}

	dbus_error_init (&error);	
	if ((hal_ctx = libhal_ctx_new ()) == NULL) {
		fprintf (stderr, "error: libhal_ctx_new\n");
		return 1;
	}
	if (!libhal_ctx_set_dbus_connection (hal_ctx, dbus_bus_get (DBUS_BUS_SYSTEM, &error))) {
		fprintf (stderr, "error: libhal_ctx_set_dbus_connection: %s: %s\n", error.name, error.message);
		LIBHAL_FREE_DBUS_ERROR (&error);
		return 1;
	}
	if (!libhal_ctx_init (hal_ctx, &error)) {
		if (dbus_error_is_set(&error)) {
			fprintf (stderr, "error: libhal_ctx_init: %s: %s\n", error.name, error.message);
			LIBHAL_FREE_DBUS_ERROR (&error);
		}
		fprintf (stderr, "Could not initialise connection to hald.\n"
				 "Normally this means the HAL daemon (hald) is not running or not ready.\n");
		return 1;
	}

	type = libhal_device_get_property_type (hal_ctx, udi, key, &error);
	if (type == LIBHAL_PROPERTY_TYPE_INVALID) {
		fprintf (stderr, "error: libhal_device_get_property_type: %s: %s\n", error.name, error.message);
		LIBHAL_FREE_DBUS_ERROR (&error);
		return 1;
	}
	/* emit the value to stdout */
	switch (type) {
	case LIBHAL_PROPERTY_TYPE_STRING:
		str = libhal_device_get_property_string (hal_ctx, udi, key, &error);
		if (is_verbose)
			printf ("Type is string\n");
		printf ("%s\n", str);
		libhal_free_string (str);
		break;
	case LIBHAL_PROPERTY_TYPE_INT32:
		if (is_verbose)
			printf ("Type is integer (shown in %s)\n",
				(is_hex ? "hexadecimal" : "decimal"));
		printf ((is_hex ? "%x\n" : "%d\n"),
			libhal_device_get_property_int (hal_ctx, udi, key, &error));
		break;
	case LIBHAL_PROPERTY_TYPE_UINT64:
		if (is_verbose)
			printf ("Type is uint64 (shown in %s)\n",
				(is_hex ? "hexadecimal" : "decimal"));
		printf ((is_hex ? "%llx\n" : "%llu\n"),
			(long long unsigned int) libhal_device_get_property_uint64 (hal_ctx, udi, key, &error));
		break;
	case LIBHAL_PROPERTY_TYPE_DOUBLE:
		if (is_verbose)
			printf ("Type is double\n");
		printf ("%f\n",
			libhal_device_get_property_double (hal_ctx, udi, key, &error));
		break;
	case LIBHAL_PROPERTY_TYPE_BOOLEAN:
		if (is_verbose)
			printf ("Type is boolean\n");
		printf ("%s\n",
			libhal_device_get_property_bool (hal_ctx, udi, key, &error) ? "true" : "false");
		break;

	case LIBHAL_PROPERTY_TYPE_STRLIST:
	{
		unsigned int i;
		char **strlist;
		
		if ((strlist = libhal_device_get_property_strlist (hal_ctx, udi, key, &error)) != NULL) {
			
			for (i = 0; strlist[i] != 0; i++) {
				printf ("%s", strlist[i]);
				if (strlist[i+1] != NULL)
					printf (" ");
			}
		} 
		break;
	}
	default:
		printf ("Unknown type %d='%c'\n", type, type);
		return 1;
		break;
	}

	if (dbus_error_is_set (&error)) {
		fprintf (stderr, "error: %s: %s\n", error.name, error.message);
		dbus_error_free (&error);
		return 1;
	}


	return 0;
}
Exemplo n.º 24
0
static void
get_video_devices (ofGstCamData & cam_data)
{

    int            i, fd, ok;
    int            num_udis = 0;
    char         **udis;
    DBusError      error;
    LibHalContext *hal_ctx;

    cam_data.num_webcam_devices = 0;

    g_print ("Probing devices with HAL...\n");

    dbus_error_init (&error);
    hal_ctx = libhal_ctx_new ();
    if (hal_ctx == NULL)
    {
        g_warning ("Could not create libhal context");
        dbus_error_free (&error);
        goto fallback;
    }

    if (!libhal_ctx_set_dbus_connection (hal_ctx, dbus_bus_get (DBUS_BUS_SYSTEM, &error)))
    {
        g_warning ("libhal_ctx_set_dbus_connection: %s: %s", error.name, error.message);
        dbus_error_free (&error);
        goto fallback;
    }

    if (!libhal_ctx_init (hal_ctx, &error))
    {
        if (dbus_error_is_set (&error))
        {
            g_warning ("libhal_ctx_init: %s: %s", error.name, error.message);
            dbus_error_free (&error);
        }
        g_warning ("Could not initialise connection to hald.\n"
                   "Normally this means the HAL daemon (hald) is not running or not ready");
        goto fallback;
    }

    udis = libhal_find_device_by_capability (hal_ctx, "video4linux", &num_udis, &error);

    if (dbus_error_is_set (&error))
    {
        g_warning ("libhal_find_device_by_capability: %s: %s", error.name, error.message);
        dbus_error_free (&error);
        goto fallback;
    }

    /* Initialize webcam structures */
    cam_data.webcam_devices = new ofGstDevice[num_udis];

    for (i = 0; i < num_udis; i++)
    {
        char                   *device;
        char                   *parent_udi = NULL;
        char                   *subsystem = NULL;
        char                   *gstreamer_src, *product_name;
        struct v4l2_capability  v2cap;
        struct video_capability v1cap;
        gint vendor_id = 0;
        gint product_id = 0;
        gchar *property_name = NULL;

        parent_udi = libhal_device_get_property_string (hal_ctx, udis[i], "info.parent", &error);
        if (dbus_error_is_set (&error))
        {
            g_warning ("error getting parent for %s: %s: %s", udis[i], error.name, error.message);
            dbus_error_free (&error);
        }

        if (parent_udi != NULL) {
            subsystem = libhal_device_get_property_string (hal_ctx, parent_udi, "info.subsystem", NULL);
            if (subsystem == NULL) continue;
            property_name = g_strjoin (".", subsystem, "vendor_id", NULL);
            vendor_id = libhal_device_get_property_int (hal_ctx, parent_udi, property_name , &error);
            if (dbus_error_is_set (&error)) {
                g_warning ("error getting vendor id: %s: %s", error.name, error.message);
                dbus_error_free (&error);
            }
            g_free (property_name);

            property_name = g_strjoin (".", subsystem, "product_id", NULL);
            product_id = libhal_device_get_property_int (hal_ctx, parent_udi, property_name, &error);
            if (dbus_error_is_set (&error)) {
                g_warning ("error getting product id: %s: %s", error.name, error.message);
                dbus_error_free (&error);
            }
            g_free (property_name);
            libhal_free_string (subsystem);
            libhal_free_string (parent_udi);
        }

        g_print ("Found device %04x:%04x, getting capabilities...\n", vendor_id, product_id);

        device = libhal_device_get_property_string (hal_ctx, udis[i], "video4linux.device", &error);
        if (dbus_error_is_set (&error))
        {
            g_warning ("error getting V4L device for %s: %s: %s", udis[i], error.name, error.message);
            dbus_error_free (&error);
            continue;
        }

        /* vbi devices support capture capability too, but cannot be used,
         * so detect them by device name */
        if (strstr (device, "vbi"))
        {
            g_print ("Skipping vbi device: %s\n", device);
            libhal_free_string (device);
            continue;
        }

        if ((fd = open (device, O_RDONLY | O_NONBLOCK)) < 0)
        {
            g_warning ("Failed to open %s: %s", device, strerror (errno));
            libhal_free_string (device);
            continue;
        }
        ok = ioctl (fd, VIDIOC_QUERYCAP, &v2cap);
        if (ok < 0)
        {
            ok = ioctl (fd, VIDIOCGCAP, &v1cap);
            if (ok < 0)
            {
                g_warning ("Error while probing v4l capabilities for %s: %s",
                           device, strerror (errno));
                libhal_free_string (device);
                close (fd);
                continue;
            }
            g_print ("Detected v4l device: %s\n", v1cap.name);
            g_print ("Device type: %d\n", v1cap.type);
            gstreamer_src = "v4lsrc";
            product_name  = v1cap.name;
        }
        else
        {
            guint cap = v2cap.capabilities;
            g_print ("Detected v4l2 device: %s\n", v2cap.card);
            g_print ("Driver: %s, version: %d\n", v2cap.driver, v2cap.version);
            /* g_print ("Bus info: %s\n", v2cap.bus_info); */ /* Doesn't seem anything useful */
            g_print ("Capabilities: 0x%08X\n", v2cap.capabilities);
            if (!(cap & V4L2_CAP_VIDEO_CAPTURE))
            {
                g_print ("Device %s seems to not have the capture capability, (radio tuner?)\n"
                         "Removing it from device list.\n", device);
                libhal_free_string (device);
                close (fd);
                continue;
            }
            gstreamer_src = "v4l2src";
            product_name  = (char *) v2cap.card;
        }

        g_print ("\n");

        cam_data.webcam_devices[cam_data.num_webcam_devices].hal_udi           = g_strdup (udis[i]);
        cam_data.webcam_devices[cam_data.num_webcam_devices].video_device      = g_strdup (device);
        cam_data.webcam_devices[cam_data.num_webcam_devices].gstreamer_src     = g_strdup (gstreamer_src);
        cam_data.webcam_devices[cam_data.num_webcam_devices].product_name      = g_strdup (product_name);
        cam_data.webcam_devices[cam_data.num_webcam_devices].num_video_formats = 0;
        cam_data.webcam_devices[cam_data.num_webcam_devices].supported_resolutions =
            g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
        cam_data.num_webcam_devices++;

        libhal_free_string (device);
        close (fd);
    }
    libhal_free_string_array (udis);

    if (cam_data.num_webcam_devices == 0)
    {
        /* Create a fake device so that resolution changing stil works even if the
         * computer doesn't have a webcam. */
fallback:
        if (num_udis == 0)
        {
            cam_data.webcam_devices = new ofGstDevice;
        }
        cam_data.webcam_devices[0].num_video_formats = 0;
        cam_data.webcam_devices[0].hal_udi = g_strdup ("oF_fake_videodevice");
    }
    cam_data.bInited=true;

}
static void
thunar_vfs_volume_hal_update (ThunarVfsVolumeHal *volume_hal,
                              LibHalContext      *context,
                              LibHalVolume       *hv,
                              LibHalDrive        *hd)
{
  gchar *desired_mount_point;
  gchar *mount_root;
  gchar *basename;
  gchar *filename;

  _thunar_vfs_return_if_fail (THUNAR_VFS_IS_VOLUME_HAL (volume_hal));
  _thunar_vfs_return_if_fail (hd != NULL);

  /* reset the volume status */
  volume_hal->status = 0;

  /* determine the new device file */
  g_free (volume_hal->device_file);
  volume_hal->device_file = g_strdup ((hv != NULL) ? libhal_volume_get_device_file (hv) : libhal_drive_get_device_file (hd));

  /* compute a usable display name for the volume/drive */
  g_free (volume_hal->device_label);
  volume_hal->device_label = (hv == NULL)
    ? exo_hal_drive_compute_display_name (context, hd)
    : exo_hal_volume_compute_display_name (context, hv, hd);
  if (G_UNLIKELY (volume_hal->device_label == NULL))
    {
      /* use the basename of the device file as label */
      volume_hal->device_label = g_path_get_basename (volume_hal->device_file);
    }

  /* compute a usable list of icon names for the volume/drive */
  g_list_foreach (volume_hal->icon_list, (GFunc) g_free, NULL);
  g_list_free (volume_hal->icon_list);
  volume_hal->icon_list = (hv == NULL)
    ? exo_hal_drive_compute_icon_list (context, hd)
    : exo_hal_volume_compute_icon_list (context, hv, hd);

  /* release the previous mount point (if any) */
  if (G_LIKELY (volume_hal->mount_point != NULL))
    {
      thunar_vfs_path_unref (volume_hal->mount_point);
      volume_hal->mount_point = NULL;
    }

  /* determine the type of the volume */
  switch (libhal_drive_get_type (hd))
    {
    case LIBHAL_DRIVE_TYPE_CDROM:
      /* check if we have a pure audio CD without any data track */
      if (libhal_volume_disc_has_audio (hv) && !libhal_volume_disc_has_data (hv))
        {
          /* special treatment for pure audio CDs */
          volume_hal->kind = THUNAR_VFS_VOLUME_KIND_AUDIO_CD;
        }
      else
        {
          /* check which kind of CD-ROM/DVD we have */
          switch (libhal_volume_get_disc_type (hv))
            {
            case LIBHAL_VOLUME_DISC_TYPE_CDROM:
              volume_hal->kind = THUNAR_VFS_VOLUME_KIND_CDROM;
              break;

            case LIBHAL_VOLUME_DISC_TYPE_CDR:
              volume_hal->kind = THUNAR_VFS_VOLUME_KIND_CDR;
              break;

            case LIBHAL_VOLUME_DISC_TYPE_CDRW:
              volume_hal->kind = THUNAR_VFS_VOLUME_KIND_CDRW;
              break;

            case LIBHAL_VOLUME_DISC_TYPE_DVDROM:
              volume_hal->kind = THUNAR_VFS_VOLUME_KIND_DVDROM;
              break;

            case LIBHAL_VOLUME_DISC_TYPE_DVDRAM:
              volume_hal->kind = THUNAR_VFS_VOLUME_KIND_DVDRAM;
              break;

            case LIBHAL_VOLUME_DISC_TYPE_DVDR:
              volume_hal->kind = THUNAR_VFS_VOLUME_KIND_DVDR;
              break;

            case LIBHAL_VOLUME_DISC_TYPE_DVDRW:
              volume_hal->kind = THUNAR_VFS_VOLUME_KIND_DVDRW;
              break;

            case LIBHAL_VOLUME_DISC_TYPE_DVDPLUSR:
              volume_hal->kind = THUNAR_VFS_VOLUME_KIND_DVDPLUSR;
              break;

            case LIBHAL_VOLUME_DISC_TYPE_DVDPLUSRW:
              volume_hal->kind = THUNAR_VFS_VOLUME_KIND_DVDPLUSRW;
              break;

            default:
              /* unsupported disc type */
              volume_hal->kind = THUNAR_VFS_VOLUME_KIND_UNKNOWN;
              break;
            }
        }
      break;

    case LIBHAL_DRIVE_TYPE_FLOPPY:
      volume_hal->kind = THUNAR_VFS_VOLUME_KIND_FLOPPY;
      break;

    case LIBHAL_DRIVE_TYPE_PORTABLE_AUDIO_PLAYER:
      volume_hal->kind = THUNAR_VFS_VOLUME_KIND_AUDIO_PLAYER;
      break;

    case LIBHAL_DRIVE_TYPE_SMART_MEDIA:
    case LIBHAL_DRIVE_TYPE_SD_MMC:
      volume_hal->kind = THUNAR_VFS_VOLUME_KIND_MEMORY_CARD;
      break;

    default:
      /* check if the drive is connected to the USB bus */
      if (libhal_drive_get_bus (hd) == LIBHAL_DRIVE_BUS_USB)
        {
          /* we consider the drive to be an USB stick */
          volume_hal->kind = THUNAR_VFS_VOLUME_KIND_USBSTICK;
        }
      else if (libhal_drive_uses_removable_media (hd)
            || libhal_drive_is_hotpluggable (hd))
        {
          /* fallback to generic removable disk */
          volume_hal->kind = THUNAR_VFS_VOLUME_KIND_REMOVABLE_DISK;
        }
      else
        {
          /* fallback to harddisk drive */
          volume_hal->kind = THUNAR_VFS_VOLUME_KIND_HARDDISK;
        }
      break;
    }

  /* either we have a volume, which means we have media, or
   * a drive, which means non-pollable then, so it's present
   */
  volume_hal->status |= THUNAR_VFS_VOLUME_STATUS_PRESENT;
  
  /* figure out if the volume is mountable */
  if(hv != NULL && libhal_volume_get_fsusage (hv) == LIBHAL_VOLUME_USAGE_MOUNTABLE_FILESYSTEM)
    volume_hal->status |= THUNAR_VFS_VOLUME_STATUS_MOUNTABLE;

  /* check if the drive requires eject */
  volume_hal->requires_eject = libhal_drive_requires_eject (hd);

  /* check if the volume is currently mounted */
  if (hv != NULL && libhal_volume_is_mounted (hv))
    {
      /* try to determine the new mount point */
      volume_hal->mount_point = thunar_vfs_path_new (libhal_volume_get_mount_point (hv), NULL);

      /* we only mark the volume as mounted if we have a valid mount point */
      if (G_LIKELY (volume_hal->mount_point != NULL))
        volume_hal->status |= THUNAR_VFS_VOLUME_STATUS_MOUNTED | THUNAR_VFS_VOLUME_STATUS_PRESENT;
    }
  else
    {
      /* we don't trust HAL, so let's see what the kernel says about the volume */
      volume_hal->mount_point = thunar_vfs_volume_hal_find_active_mount_point (volume_hal);

      /* we must have been mounted successfully if we have a mount point */
      if (G_LIKELY (volume_hal->mount_point != NULL))
        volume_hal->status |= THUNAR_VFS_VOLUME_STATUS_MOUNTED | THUNAR_VFS_VOLUME_STATUS_PRESENT;
    }

  /* check if we have to figure out the mount point ourself */
  if (G_UNLIKELY (volume_hal->mount_point == NULL))
    {
      /* ask HAL for the default mount root (falling back to /media otherwise) */
      mount_root = libhal_device_get_property_string (context, "/org/freedesktop/Hal/devices/computer", "storage.policy.default.mount_root", NULL);
      if (G_UNLIKELY (mount_root == NULL || !g_path_is_absolute (mount_root)))
        {
          /* fallback to /media (seems to be sane) */
          g_free (mount_root);
          mount_root = g_strdup ("/media");
        }

      /* lets see, maybe /etc/fstab knows where to mount */
      volume_hal->mount_point = thunar_vfs_volume_hal_find_fstab_mount_point (volume_hal);

      /* if we still don't have a mount point, ask HAL */
      if (G_UNLIKELY (volume_hal->mount_point == NULL))
        {
          /* determine the desired mount point and prepend the mount root */
          desired_mount_point = libhal_device_get_property_string (context, volume_hal->udi, "volume.policy.desired_mount_point", NULL);
          if (G_LIKELY (desired_mount_point != NULL && *desired_mount_point != '\0'))
            {
              filename = g_build_filename (mount_root, desired_mount_point, NULL);
              volume_hal->mount_point = thunar_vfs_path_new (filename, NULL);
              g_free (filename);
            }
          libhal_free_string (desired_mount_point);
        }

      /* ok, last fallback, just use <mount-root>/<device> */
      if (G_UNLIKELY (volume_hal->mount_point == NULL))
        {
          /* <mount-root>/<device> looks like a good idea */
          basename = g_path_get_basename (volume_hal->device_file);
          filename = g_build_filename (mount_root, basename, NULL);
          volume_hal->mount_point = thunar_vfs_path_new (filename, NULL);
          g_free (filename);
          g_free (basename);
        }

      /* release the mount root */
      g_free (mount_root);
    }

  /* if we get here, we must have a valid mount point */
  g_assert (volume_hal->mount_point != NULL);

  /* emit the "changed" signal */
  thunar_vfs_volume_changed (THUNAR_VFS_VOLUME (volume_hal));
}
Exemplo n.º 26
0
/*
 * gst_hal_get_alsa_element:
 * @ctx: a #LibHalContext which should be used for querying HAL.
 * @udi: a #gchar corresponding to the UDI you want to get.
 * @device_type: a #GstHalDeviceType specifying the wanted device type.
 *
 * Get Hal UDI @udi's string value.
 *
 * Returns: a newly allocated #gchar string containing the appropriate pipeline
 * for UDI @udi, or NULL in the case of an error..
 */
static gchar *
gst_hal_get_alsa_element (LibHalContext * ctx, const gchar * udi,
    GstHalDeviceType device_type)
{
  char *type, *element = NULL, *string = NULL;
  DBusError error;

  dbus_error_init (&error);

  if (!libhal_device_query_capability (ctx, udi, "alsa", &error)) {
    if (dbus_error_is_set (&error)) {
      GST_DEBUG ("Failed querying %s for alsa capability: %s: %s",
          udi, error.name, error.message);
      LIBHAL_FREE_DBUS_ERROR (&error);
    } else {
      GST_DEBUG ("UDI %s has no alsa capability", udi);
    }
    return NULL;
  }

  type = libhal_device_get_property_string (ctx, udi, "alsa.type", &error);

  if (dbus_error_is_set (&error)) {
    GST_DEBUG ("UDI %s has alsa capabilities but no alsa.type property: %s, %s",
        udi, error.name, error.message);
    LIBHAL_FREE_DBUS_ERROR (&error);
    return NULL;
  } else if (!type) {
    GST_DEBUG ("UDI %s has empty alsa.type property", udi);
    return NULL;
  }

  if (strcmp (type, "playback") == 0 && device_type == GST_HAL_AUDIOSINK)
    element = "alsasink";
  else if (strcmp (type, "capture") == 0 && device_type == GST_HAL_AUDIOSRC)
    element = "alsasrc";

  libhal_free_string (type);

  if (element) {
    int card, device;

    card = libhal_device_get_property_int (ctx, udi, "alsa.card", &error);
    if (dbus_error_is_set (&error)) {
      GST_DEBUG ("UDI %s has no alsa.card property: %s: %s", udi, error.name,
          error.message);
      LIBHAL_FREE_DBUS_ERROR (&error);
      return NULL;
    } else if (card == -1) {
      GST_DEBUG ("UDI %s has no alsa.card property", udi);
      return NULL;
    }

    device = libhal_device_get_property_int (ctx, udi, "alsa.device", &error);
    if (dbus_error_is_set (&error)) {
      GST_DEBUG ("UDI %s has no alsa.device property: %s: %s", udi, error.name,
          error.message);
      LIBHAL_FREE_DBUS_ERROR (&error);
      return NULL;
    } else if (device == -1) {
      GST_DEBUG ("UDI %s has no alsa.device property", udi);
      return NULL;
    }

    /* This is a bit dodgy, since it makes lots of assumptions about the way
     * alsa is set up. In any case, only munge the device string for playback */
    if (strcmp (element, "alsasink") == 0 && device == 0) {
      /* handle default device specially to use
       * dmix, dsnoop, and softvol if appropriate */
      string = g_strdup_printf ("%s device=default:%d", element, card);
    } else {
      string =
          g_strdup_printf ("%s device=plughw:%d,%d", element, card, device);
    }
  }

  return string;
}
Exemplo n.º 27
0
static void
battery_dynamic_update(LibHalContext *ctx, const char *udi, int fd)
{
	int reporting_rate;
	int reporting_current;
	int reporting_lastfull;
	int design_voltage;
	int present_voltage;
	char *reporting_unit;
	int remaining_time;
	int remaining_percentage;
	gboolean charging;
	gboolean discharging;
	acpi_bst_t bst;
	LibHalChangeSet *cs;
	DBusError error;
	static int counter = 0;

	HAL_DEBUG(("battery_dynamic_update() enter"));
	bzero(&bst, sizeof (bst));
	if (ioctl(fd, BATT_IOC_STATUS, &bst) < 0) {
		return;
	}

	charging = bst.bst_state & BATT_BST_CHARGING ? TRUE : FALSE;
	discharging = bst.bst_state & BATT_BST_DISCHARGING ? TRUE : FALSE;
	/* No need to continue if battery is essentially idle. */
	if (counter && !charging && !discharging) {
		return;
	}
	dbus_error_init(&error);
	libhal_device_set_property_bool(ctx, udi, "battery.is_rechargeable",
	    TRUE, &error);
	my_dbus_error_free(&error);
	if (libhal_device_property_exists(ctx, udi,
	    "battery.charge_level.percentage", &error)) {
		remaining_percentage = libhal_device_get_property_int(ctx, udi,
		    "battery.charge_level.percentage", &error);
		if ((remaining_percentage == 100) && charging) {
			charging = FALSE;
		}
	}
	libhal_device_set_property_bool(ctx, udi,
	    "battery.rechargeable.is_charging", charging, &error);
	my_dbus_error_free(&error);
	libhal_device_set_property_bool(ctx, udi,
	    "battery.rechargeable.is_discharging", discharging, &error);
	my_dbus_error_free(&error);
	reporting_current = bst.bst_rem_cap;
	libhal_device_set_property_int(ctx, udi, "battery.reporting.current",
	    bst.bst_rem_cap, &error);
	my_dbus_error_free(&error);
	reporting_rate = bst.bst_rate;
	libhal_device_set_property_int(ctx, udi, "battery.reporting.rate",
	    bst.bst_rate, &error);
	my_dbus_error_free(&error);
	present_voltage = bst.bst_voltage;
	libhal_device_set_property_int(ctx, udi, "battery.voltage.present",
	    bst.bst_voltage, &error);
	/* get all the data we know */
	my_dbus_error_free(&error);
	reporting_unit = libhal_device_get_property_string(ctx, udi,
	    "battery.reporting.unit", &error);
	my_dbus_error_free(&error);
	reporting_lastfull = libhal_device_get_property_int(ctx, udi,
	    "battery.reporting.last_full", &error);

	/*
	 * Convert mAh to mWh since util_compute_time_remaining() works
	 * for mWh.
	 */
	if (reporting_unit && strcmp(reporting_unit, "mAh") == 0) {
		my_dbus_error_free(&error);
		design_voltage = libhal_device_get_property_int(ctx, udi,
		    "battery.voltage.design", &error);
		/*
		 * If the present_voltage is inaccurate, set it to the
		 * design_voltage.
		 */
		if (((present_voltage * 10) < design_voltage) ||
		    (present_voltage <= 0) ||
		    (present_voltage > design_voltage)) {
			present_voltage = design_voltage;
		}
		reporting_rate = (reporting_rate * present_voltage) / 1000;
		reporting_lastfull = (reporting_lastfull * present_voltage) /
		    1000;
		reporting_current = (reporting_current * present_voltage) /
		    1000;
	}

	/* Make sure the current charge does not exceed the full charge */
	if (reporting_current > reporting_lastfull) {
		reporting_current = reporting_lastfull;
	}
	if (!charging && !discharging) {
		counter++;
		reporting_rate = 0;
	}

	if ((cs = libhal_device_new_changeset(udi)) == NULL) {
		HAL_DEBUG(("Cannot allocate changeset"));
		libhal_free_string(reporting_unit);
		my_dbus_error_free(&error);
		return;
	}

	libhal_changeset_set_property_int(cs, "battery.charge_level.rate",
	    reporting_rate);
	libhal_changeset_set_property_int(cs,
	    "battery.charge_level.last_full", reporting_lastfull);
	libhal_changeset_set_property_int(cs,
	    "battery.charge_level.current", reporting_current);

	remaining_percentage = util_compute_percentage_charge(udi,
	    reporting_current, reporting_lastfull);
	remaining_time = util_compute_time_remaining(udi, reporting_rate,
	    reporting_current, reporting_lastfull, discharging, charging, 0);
	/*
	 * Some batteries give bad remaining_time estimates relative to
	 * the charge level.
	 */
	if (charging && ((remaining_time < 30) || ((remaining_time < 300) &&
	    (remaining_percentage < 95)) || (remaining_percentage > 97))) {
		remaining_time = util_compute_time_remaining(udi,
		    reporting_rate, reporting_current, reporting_lastfull,
		    discharging, charging, 1);
	}

	if (remaining_percentage > 0) {
		libhal_changeset_set_property_int(cs,
		    "battery.charge_level.percentage", remaining_percentage);
	} else {
		my_dbus_error_free(&error);
		libhal_device_remove_property(ctx, udi,
		    "battery.charge_level.percentage", &error);
	}
	if ((remaining_percentage == 100) && charging) {
		battery_last_full(cs, fd);
	}
	/*
	 * remaining_percentage is more accurate so we handle cases
	 * where the remaining_time cannot be correct.
	 */
	if ((!charging && !discharging) || ((remaining_percentage == 100) &&
	    !discharging)) {
		remaining_time = 0;
	}
	if (remaining_time < 0) {
		my_dbus_error_free(&error);
		libhal_device_remove_property(ctx, udi,
		    "battery.remaining_time", &error);
	} else if (remaining_time >= 0) {
		libhal_changeset_set_property_int(cs,
		    "battery.remaining_time", remaining_time);
	}

	my_dbus_error_free(&error);
	libhal_device_commit_changeset(ctx, cs, &error);
	libhal_device_free_changeset(cs);
	libhal_free_string(reporting_unit);
	my_dbus_error_free(&error);
	HAL_DEBUG(("battery_dynamic_update() exit"));
}
Exemplo n.º 28
0
int
main (int argc, char **argv)
{
  char *device_file;
  char *parent_udi;
  char *grandparent_udi;
  char *parent_drive_type;
  int fd = -1;
  struct volume_id *vid = NULL;
  int ret = 1;
  gboolean has_children;
  gboolean is_swap;
  gboolean is_cdrom;
  gboolean is_partition = FALSE;
  gboolean has_audio = FALSE;
  gboolean has_data = FALSE;
  gboolean is_blank = FALSE;
  const char *usage;
  char *label;
  unsigned int sector_size = 0;
  off_t media_size = 0;

  if (! hfp_init(argc, argv))
    goto end;

  device_file = getenv("HAL_PROP_BLOCK_DEVICE");
  if (! device_file)
    goto end;

  parent_udi = getenv("HAL_PROP_INFO_PARENT");
  if (! parent_udi)
    goto end;

  /* give a meaningful process title for ps(1) */
  setproctitle("%s", device_file);

  has_children = hfp_getenv_bool("HF_HAS_CHILDREN");
  is_swap = hfp_getenv_bool("HF_IS_SWAP");

  fd = open(device_file, O_RDONLY);
  if (fd < 0)
    goto end;

  parent_drive_type = libhal_device_get_property_string(hfp_ctx, parent_udi, "storage.drive_type", &hfp_error);
  dbus_error_free(&hfp_error);

  grandparent_udi = libhal_device_get_property_string(hfp_ctx, parent_udi, "info.parent", &hfp_error);
  dbus_error_free(&hfp_error);

  is_cdrom = parent_drive_type && ! strcmp(parent_drive_type, "cdrom");
  g_free(parent_drive_type);

  if (is_cdrom)
    {
      hf_probe_volume_get_disc_info(fd, &has_audio, &has_data);
      is_blank = (! has_audio && ! has_data);
    }

  ioctl(fd, DIOCGMEDIASIZE, &media_size);

  /*
   * We only check for filesystems if the volume has no children,
   * otherwise volume_id might find a filesystem in what is actually
   * the first child partition of the volume.
   *
   * If hald (which has looked at the partition type) reports that it
   * is a swap partition, we probe it nevertheless in case the
   * partition type is incorrect.
   */
  if (! has_children && ! (is_cdrom && ! has_data))
    {
      vid = volume_id_open_fd(fd);
      if (vid)
	{
	  if (volume_id_probe_all(vid, 0, media_size) == 0)
	    has_data = TRUE;
	  else
	    {
	      volume_id_close(vid);
	      vid = NULL;
	    }
	}
    }

  if (! has_children && ! is_swap && ! has_audio && ! has_data && ! is_blank)
    goto end;

  libhal_device_add_capability(hfp_ctx, hfp_udi, "volume", &hfp_error);
  if (is_cdrom)
    {
      HFPCDROM *cdrom;
      int type;
      guint64 capacity;

      libhal_device_set_property_string(hfp_ctx, hfp_udi, "info.category", "volume.disc", &hfp_error);
      libhal_device_add_capability(hfp_ctx, hfp_udi, "volume.disc", &hfp_error);

      libhal_device_set_property_bool(hfp_ctx, hfp_udi, "volume.disc.has_audio", has_audio, &hfp_error);
      libhal_device_set_property_bool(hfp_ctx, hfp_udi, "volume.disc.has_data", has_data, &hfp_error);
      libhal_device_set_property_bool(hfp_ctx, hfp_udi, "volume.disc.is_vcd", FALSE, &hfp_error);
      libhal_device_set_property_bool(hfp_ctx, hfp_udi, "volume.disc.is_svcd", FALSE, &hfp_error);
      libhal_device_set_property_bool(hfp_ctx, hfp_udi, "volume.disc.is_videodvd", FALSE, &hfp_error);
      libhal_device_set_property_bool(hfp_ctx, hfp_udi, "volume.disc.is_appendable", FALSE, &hfp_error);
      libhal_device_set_property_bool(hfp_ctx, hfp_udi, "volume.disc.is_blank", is_blank, &hfp_error);
      libhal_device_set_property_bool(hfp_ctx, hfp_udi, "volume.disc.is_rewritable", FALSE, &hfp_error);
      libhal_device_set_property_string(hfp_ctx, hfp_udi, "volume.disc.type", "unknown", &hfp_error);

      /* the following code was adapted from linux's probe-volume.c */

      cdrom = hfp_cdrom_new_from_fd(fd, device_file, grandparent_udi);
      if (cdrom)
	{
	  type = get_disc_type(cdrom);
	  if (type != -1)
	    switch (type)
	      {
	      case 0x08: /* CD-ROM */
		libhal_device_set_property_string(hfp_ctx, hfp_udi, "volume.disc.type", "cd_rom", &hfp_error);
		break;
	      case 0x09: /* CD-R */
		libhal_device_set_property_string(hfp_ctx, hfp_udi, "volume.disc.type", "cd_r", &hfp_error);
		break;
	      case 0x0a: /* CD-RW */
		libhal_device_set_property_string(hfp_ctx, hfp_udi, "volume.disc.type", "cd_rw", &hfp_error);
		libhal_device_set_property_bool(hfp_ctx, hfp_udi, "volume.disc.is_rewritable", TRUE, &hfp_error);
		break;
	      case 0x10: /* DVD-ROM */
		libhal_device_set_property_string(hfp_ctx, hfp_udi, "volume.disc.type", "dvd_rom", &hfp_error);
		break;
	      case 0x11: /* DVD-R Sequential */
		libhal_device_set_property_string(hfp_ctx, hfp_udi, "volume.disc.type", "dvd_r", &hfp_error);
		break;
	      case 0x12: /* DVD-RAM */
		libhal_device_set_property_string(hfp_ctx, hfp_udi, "volume.disc.type", "dvd_ram", &hfp_error);
		libhal_device_set_property_bool(hfp_ctx, hfp_udi, "volume.disc.is_rewritable", TRUE, &hfp_error);
		break;
	      case 0x13: /* DVD-RW Restricted Overwrite */
		libhal_device_set_property_string(hfp_ctx, hfp_udi, "volume.disc.type", "dvd_rw", &hfp_error);
		libhal_device_set_property_bool(hfp_ctx, hfp_udi, "volume.disc.is_rewritable", TRUE, &hfp_error);
		break;
	      case 0x14: /* DVD-RW Sequential */
		libhal_device_set_property_string(hfp_ctx, hfp_udi, "volume.disc.type", "dvd_rw", &hfp_error);
		libhal_device_set_property_bool(hfp_ctx, hfp_udi, "volume.disc.is_rewritable", TRUE, &hfp_error);
		break;
	      case 0x1A: /* DVD+RW */
		libhal_device_set_property_string(hfp_ctx, hfp_udi, "volume.disc.type", "dvd_plus_rw", &hfp_error);
		libhal_device_set_property_bool(hfp_ctx, hfp_udi, "volume.disc.is_rewritable", TRUE, &hfp_error);
		break;
	      case 0x1B: /* DVD+R */
		libhal_device_set_property_string(hfp_ctx, hfp_udi, "volume.disc.type", "dvd_plus_r", &hfp_error);
		break;
	      case 0x2B: /* DVD+R Double Layer */
		libhal_device_set_property_string(hfp_ctx, hfp_udi, "volume.disc.type", "dvd_plus_r_dl", &hfp_error);
		break;
	      case 0x40: /* BD-ROM  */
		libhal_device_set_property_string(hfp_ctx, hfp_udi, "volume.disc.type", "bd_rom", &hfp_error);
		break;
	      case 0x41: /* BD-R Sequential */
		libhal_device_set_property_string(hfp_ctx, hfp_udi, "volume.disc.type", "bd_r", &hfp_error);
		break;
	      case 0x42: /* BD-R Random */
		libhal_device_set_property_string(hfp_ctx, hfp_udi, "volume.disc.type", "bd_r", &hfp_error);
		break;
	      case 0x43: /* BD-RE */
		libhal_device_set_property_string(hfp_ctx, hfp_udi, "volume.disc.type", "bd_re", &hfp_error);
		libhal_device_set_property_bool(hfp_ctx, hfp_udi, "volume.disc.is_rewritable", TRUE, &hfp_error);
		break;
	      case 0x50: /* HD DVD-ROM */
		libhal_device_set_property_string(hfp_ctx, hfp_udi, "volume.disc.type", "hddvd_rom", &hfp_error);
		break;
	      case 0x51: /* HD DVD-R */
		libhal_device_set_property_string(hfp_ctx, hfp_udi, "volume.disc.type", "hddvd_r", &hfp_error);
		break;
	      case 0x52: /* HD DVD-Rewritable */
		libhal_device_set_property_string(hfp_ctx, hfp_udi, "volume.disc.type", "hddvd_rw", &hfp_error);
		libhal_device_set_property_bool(hfp_ctx, hfp_udi, "volume.disc.is_rewritable", TRUE, &hfp_error);
		break;
	      }

	  if (get_disc_capacity_for_type(cdrom, type, &capacity) == 0)
	    libhal_device_set_property_uint64(hfp_ctx, hfp_udi, "volume.disc.capacity", capacity, &hfp_error);

	  /*
	   * linux's probe-volume.c: "on some hardware the get_disc_type
	   * call fails, so we use this as a backup".
	   */
	  if (disc_is_rewritable(cdrom))
	    libhal_device_set_property_bool(hfp_ctx, hfp_udi, "volume.disc.is_rewritable", TRUE, &hfp_error);
	  if (disc_is_appendable(cdrom))
	    libhal_device_set_property_bool (hfp_ctx, hfp_udi, "volume.disc.is_appendable", TRUE, &hfp_error);

	  hfp_cdrom_free(cdrom);
	}

      if (has_data && vid && (! strcmp(vid->type, "iso9660") ||
          ! strcmp(vid->type, "udf")))
        hf_probe_volume_advanced_disc_detect(fd);
    }
  else
    {
      libhal_device_set_property_string(hfp_ctx, hfp_udi, "info.category", "volume", &hfp_error);

      if (libhal_device_query_capability(hfp_ctx, parent_udi, "storage", &hfp_error))
	{
	  char *geom_class;
	  char *type;
	  char *scheme;
	  int number;
	  guint64 mediasize;
	  guint64 offset;

	  geom_class = getenv("HF_VOLUME_GEOM_CLASS");

	  if (geom_class)
            {
              if (hf_probe_volume_get_partition_info(geom_class, device_file, &number, &type, &scheme, &mediasize, &offset))
                {
                  is_partition = TRUE;

		  libhal_device_set_property_int(hfp_ctx, hfp_udi, "volume.partition.number", number, &hfp_error);
		  libhal_device_set_property_string(hfp_ctx, hfp_udi, "volume.partition.scheme", scheme, &hfp_error);
		  libhal_device_set_property_string(hfp_ctx, hfp_udi, "volume.partition.type", type, &hfp_error);

		  /* FIXME We need to fill in the supported partition flags. */

		  libhal_device_set_property_uint64(hfp_ctx, hfp_udi, "volume.partition.media_size", mediasize, &hfp_error);
		  libhal_device_set_property_uint64(hfp_ctx, hfp_udi, "volume.partition.start", offset, &hfp_error);

		  if (! strcmp(scheme, "gpt"))
                    libhal_device_set_property_string(hfp_ctx, hfp_udi, "volume.partition.uuid", type, &hfp_error);

		  if (! strcmp(scheme, "gpt") || ! strcmp(scheme, "apm"))
                    libhal_device_set_property_string(hfp_ctx, hfp_udi, "volume.partition.label", "", &hfp_error);

		  g_free(type);
		  g_free(scheme);
		}
	    }
	}
      else
	dbus_error_free(&hfp_error);
    }

  libhal_device_set_property_bool(hfp_ctx, hfp_udi, "volume.is_disc", is_cdrom, &hfp_error);
  libhal_device_set_property_bool(hfp_ctx, hfp_udi, "volume.is_partition", is_partition, &hfp_error);

  libhal_device_set_property_bool(hfp_ctx, hfp_udi, "volume.ignore", has_children || is_swap, &hfp_error);

#ifdef HAVE_LIBUFS
  if (vid && ! strcmp (vid->type, "ufs"))
    {
      struct uufsd ufsdisk;

      if (ufs_disk_fillout(&ufsdisk, device_file) == 0)
        {
	  char ufsid[64];
	  char **ufs_devs = NULL;
	  int num_udis;
	  int i;

	  snprintf(ufsid, sizeof(ufsid), "%08x%08x", ufsdisk.d_fs.fs_id[0], ufsdisk.d_fs.fs_id[1]);
	  libhal_device_set_property_string(hfp_ctx, hfp_udi, "volume.freebsd.ufsid", ufsid, &hfp_error);
	  dbus_error_free(&hfp_error);
	  ufs_devs = libhal_manager_find_device_string_match(hfp_ctx,
			  				     "volume.freebsd.ufsid",
							     ufsid,
							     &num_udis,
							     &hfp_error);
	  dbus_error_free(&hfp_error);
	  for (i = 0; i < num_udis; i++)
            {
              if (ufs_devs[i] != NULL && strcmp(ufs_devs[i], hfp_udi))
                {
                  gboolean mounted;

		  mounted = libhal_device_get_property_bool(hfp_ctx, ufs_devs[i], "volume.is_mounted", &hfp_error);
	          dbus_error_free(&hfp_error);
		  if (mounted)
	            {
                      libhal_device_set_property_bool(hfp_ctx, hfp_udi, "volume.ignore", TRUE, &hfp_error);
		      dbus_error_free(&hfp_error);
		      break;
		    }
		}
	    }
	  if (ufs_devs)
	    libhal_free_string_array(ufs_devs);
	  ufs_disk_close(&ufsdisk);
	}
    }
#endif /* HAVE_LIBUFS */

  if (has_children)
    usage = "partitiontable";
  else if (is_swap)
    usage = "other";
  else
    switch (vid ? vid->usage_id : (enum volume_id_usage) -1)
      {
      case VOLUME_ID_FILESYSTEM:	usage = "filesystem"; break;
      case VOLUME_ID_DISKLABEL:		usage = "disklabel"; break;
      case VOLUME_ID_OTHER:		usage = "other"; break;
      case VOLUME_ID_RAID:		usage = "raid"; break;
      case VOLUME_ID_CRYPTO:		usage = "crypto"; break;
      case VOLUME_ID_UNUSED:		usage = "unused"; break;
      default:				usage = "unknown"; break;
      }

  libhal_device_set_property_string(hfp_ctx, hfp_udi, "volume.fsusage", usage, &hfp_error);
  libhal_device_set_property_string(hfp_ctx, hfp_udi, "volume.fstype", vid ? vid->type: "", &hfp_error);
  if (vid && *vid->type_version)
    libhal_device_set_property_string(hfp_ctx, hfp_udi, "volume.fsversion", vid->type_version, &hfp_error);

  label = hf_probe_volume_get_label(vid);
  libhal_device_set_property_string(hfp_ctx, hfp_udi, "volume.label", label ? label : "", &hfp_error);
  g_free(label);

  libhal_device_set_property_string(hfp_ctx, hfp_udi, "volume.uuid", vid ? vid->uuid : "", &hfp_error);

  ioctl(fd, DIOCGSECTORSIZE, &sector_size);

  if (sector_size != 0)
    libhal_device_set_property_uint64(hfp_ctx, hfp_udi, "volume.block_size", sector_size, &hfp_error);
  if (media_size != 0)
    libhal_device_set_property_uint64(hfp_ctx, hfp_udi, "volume.size", media_size, &hfp_error);
  if (sector_size != 0 && media_size != 0)
    libhal_device_set_property_uint64(hfp_ctx, hfp_udi, "volume.num_blocks", media_size / sector_size, &hfp_error);

  ret = 0;			/* is a volume */

 end:
  return ret;
}
Exemplo n.º 29
0
static int rsct_usbdev_scan_nonserial(char **devices, int i_devices,
				      rsct_usbdev_t **usbdev_list) {
  int i;

  for (i=0; i<i_devices; i++) {
    const char *udi=devices[i];

    if (libhal_device_exists(global_hal_context->ctx, udi, &(global_hal_context->dbus_error))) {
      char *busType;

      busType=libhal_device_get_property_string(global_hal_context->ctx, udi, "info.subsystem", NULL);
      if (busType && (strcasecmp(busType, "usb")!=0)) {
	libhal_free_string(busType);
	busType=NULL; /* non-USB devices are handled below */
      }

      if (busType==NULL)
	busType=libhal_device_get_property_string(global_hal_context->ctx, udi, "info.bus", NULL);
      if (busType) {
	if (strcasecmp(busType, "usb")==0) {
	  /* USB device, look for LibUSB info */
	  if (libhal_device_property_exists(global_hal_context->ctx, udi, "usb.bus_number", NULL) &&
	      libhal_device_property_exists(global_hal_context->ctx, udi, "usb.linux.device_number", NULL)){
	    int busId;
	    int busPos;
	    int vendorId;
	    int productId;
	    char pbuff[256];
	    struct stat st;
	    int havePath=0;

	    busId=libhal_device_get_property_int(global_hal_context->ctx,
						 udi,
						 "usb.bus_number",
						 NULL);
	    busPos=libhal_device_get_property_int(global_hal_context->ctx,
						  udi,
						  "usb.linux.device_number",
						  NULL);
	    vendorId=libhal_device_get_property_int(global_hal_context->ctx,
						    udi,
						    "usb.vendor_id",
						    NULL);
	    productId=libhal_device_get_property_int(global_hal_context->ctx,
						     udi,
						     "usb.product_id",
						     NULL);

	    if (vendorId==0xc4b && rsct_usbdev_list_findByBus(*usbdev_list, busId, busPos)==NULL) {
	      rsct_usbdev_t *d;
	      char *serial=NULL;
	      char *productName=NULL;

	      d=rsct_usbdev_new();

	      d->busId=busId;
	      d->busPos=busPos;
	      d->vendorId=vendorId;
	      d->productId=productId;

	      snprintf(d->halPath, sizeof(d->halPath)-1,
		       "usb:%04x/%04x:libhal:%s",
		       d->vendorId,
		       d->productId,
		       udi);
	      d->halPath[sizeof(d->halPath)-1]=0;

	      /* set HAL UDI */
	      strncpy(d->halUDI, udi, sizeof(d->halUDI)-1);
	      d->halUDI[sizeof(d->halUDI)-1]=0;

	      /* determine path for LibUSB */
	      snprintf(pbuff, sizeof(pbuff)-1,
		       "/dev/bus/usb/%03d/%03d",
		       busId, busPos);
	      pbuff[sizeof(pbuff)-1]=0;
	      if (stat(pbuff, &st)==0) {
		havePath=1;
	      }
	      else {
		snprintf(pbuff, sizeof(pbuff)-1,
			 "/proc/bus/usb/%03d/%03d",
			 busId, busPos);
		pbuff[sizeof(pbuff)-1]=0;
		if (stat(pbuff, &st)==0) {
		  havePath=1;
		}
	      }

	      if (havePath) {
		strncpy(d->usbPath, pbuff, sizeof(d->usbPath)-1);
		d->usbPath[sizeof(d->usbPath)-1]=0;

		strncpy(d->deviceNodePath, pbuff, sizeof(d->deviceNodePath)-1);
		d->deviceNodePath[sizeof(d->deviceNodePath)-1]=0;
	      }

	      /* generate path for CTAPI/IFD */
	      snprintf(d->path, sizeof(d->path)-1,
		       "usb:%04x/%04x:libusb:%03d:%03d",
		       d->vendorId,
		       d->productId,
		       d->busId,
		       d->busPos);

	      serial=libhal_device_get_property_string(global_hal_context->ctx,
						       udi,
						       "usb.serial",
						       NULL);
	      if (serial) {
		strncpy(d->serial, serial, sizeof(d->serial)-1);
		d->serial[sizeof(d->serial)-1]=0;
		libhal_free_string(serial);
	      }

	      /* get product name from parent.
	       * Please note: This udi refers to the *_ifX HAL device, and it's product name
	       * is the product name of the interface ("USB Vendor Specific Interface") rather than
	       * that of the device itself.
	       * Therefore we must access the parent in order to find the product name of the device.
	       */
	      if (1) {
		char *parent_udi;

		/* ttyUSB device, get USB info from parent */
		parent_udi=libhal_device_get_property_string(global_hal_context->ctx,
							     udi,
							     "info.parent",
							     NULL);
		if (parent_udi) {
		  productName=libhal_device_get_property_string(global_hal_context->ctx,
								parent_udi,
								"usb_device.product",
								NULL);
		  libhal_free_string(parent_udi);
		}
	      }

	      if (productName) {
		strncpy(d->productName, productName, sizeof(d->productName)-1);
		d->productName[sizeof(d->productName)-1]=0;
		libhal_free_string(productName);
	      }

	      /* all set, add device */
	      rsct_usbdev_list_add(usbdev_list, d);
	    }
	  }
	} /* if USB */
	libhal_free_string(busType);
      } /* if bus type */
    } /* if device exists */
  } /* for */

  return 0;
}
Exemplo n.º 30
0
char *
battstat_hal_initialise (void (*callback) (void))
{
  DBusConnection *connection;
  LibHalContext *ctx;
  DBusError error;
  char *error_str;
  char **devices;
  int i, num;

  status_updated_callback = callback;

  if( battstat_hal_ctx != NULL )
    return g_strdup( "Already initialised!" );

  dbus_error_init( &error );

  if( (connection = dbus_bus_get( DBUS_BUS_SYSTEM, &error )) == NULL )
    goto error_out;

  dbus_connection_setup_with_g_main( connection, g_main_context_default() );

  if( (ctx = libhal_ctx_new()) == NULL )
  {
    dbus_set_error( &error, _("HAL error"), _("Could not create libhal_ctx") );
    goto error_out;
  }

  libhal_ctx_set_device_property_modified( ctx, property_callback );
  libhal_ctx_set_device_added( ctx, device_added_callback );
  libhal_ctx_set_device_removed( ctx, device_removed_callback );
  libhal_ctx_set_dbus_connection( ctx, connection );

  if( libhal_ctx_init( ctx, &error ) == 0 )
    goto error_freectx;
  
  devices = libhal_find_device_by_capability( ctx, "battery", &num, &error );

  if( devices == NULL )
    goto error_shutdownctx;

  /* FIXME: for now, if 0 battery devices are present on first scan, then fail.
   * This allows fallover to the legacy (ACPI, APM, etc) backends if the
   * installed version of HAL doesn't know about batteries.  This check should
   * be removed at some point in the future (maybe circa MATE 2.13..).
   */
  if( num == 0 )
  {
    dbus_free_string_array( devices );
    dbus_set_error( &error, _("HAL error"), _("No batteries found") );
    goto error_shutdownctx;
  }

  for( i = 0; i < num; i++ )
  {
    char *type = libhal_device_get_property_string( ctx, devices[i],
                                                    "battery.type",
                                                    &error );

    if( type )
    {
      /* We only track 'primary' batteries (ie: to avoid monitoring
       * batteries in cordless mice or UPSes etc.)
       */
      if( !strcmp( type, "primary" ) )
        add_to_list( ctx, &batteries, devices[i],
                     sizeof (struct battery_info) );

      libhal_free_string( type );
    }
  }
  dbus_free_string_array( devices );

  devices = libhal_find_device_by_capability( ctx, "ac_adapter", &num, &error );

  if( devices == NULL )
  {
    batteries = free_entire_list( batteries );
    goto error_shutdownctx;
  }

  for( i = 0; i < num; i++ )
    add_to_list( ctx, &adaptors, devices[i], sizeof (struct adaptor_info) );
  dbus_free_string_array( devices );

  dbus_error_free( &error );

  battstat_hal_ctx = ctx;

  return NULL;

error_shutdownctx:
  libhal_ctx_shutdown( ctx, NULL );

error_freectx:
  libhal_ctx_free( ctx );

error_out:
  error_str = g_strdup_printf( _("Unable to initialise HAL: %s: %s"),
                               error.name, error.message );
  dbus_error_free( &error );
  return error_str;
}