void QNetworkInfoPrivate::connectNotify(const QMetaMethod &signal)
{
#if !defined(QT_NO_OFONO)
    if (QOfonoWrapper::isOfonoAvailable()) {
        if (!ofonoWrapper)
            ofonoWrapper = new QOfonoWrapper(this);
        QMetaMethod sourceSignal = proxyToSourceSignal(signal, ofonoWrapper);
        connect(ofonoWrapper, sourceSignal, this, signal, Qt::UniqueConnection);
    }
#endif

    static const QMetaMethod currentNetworkModeChangedSignal = QMetaMethod::fromSignal(&QNetworkInfoPrivate::currentNetworkModeChanged);
    static const QMetaMethod networkNameChangedSignal = QMetaMethod::fromSignal(&QNetworkInfoPrivate::networkNameChanged);
    static const QMetaMethod networkSignalStrengthChangedSignal = QMetaMethod::fromSignal(&QNetworkInfoPrivate::networkSignalStrengthChanged);
    static const QMetaMethod networkStatusChangedSignal = QMetaMethod::fromSignal(&QNetworkInfoPrivate::networkStatusChanged);

    //    we always monitor "networkInterfaceCount" , as long as users connect any signals,
    //    with update to date network interface counts, we can compute network mode, strength,
    //    status, name properties in an efficient way
    if (!watchNetworkInterfaceCount) {
        QList<QNetworkInfo::NetworkMode> modes;
        modes << QNetworkInfo::WlanMode << QNetworkInfo::EthernetMode << QNetworkInfo::BluetoothMode;
        networkInterfaceCounts.clear();
        foreach (QNetworkInfo::NetworkMode mode, modes)
            networkInterfaceCounts[mode] = getNetworkInterfaceCount(mode);
#if !defined(QT_NO_UDEV)
        if (!udevHandle) {
            udevHandle = udev_new();
            udevMonitor = udev_monitor_new_from_netlink(udevHandle, "udev");
            udev_monitor_filter_add_match_subsystem_devtype(udevMonitor, "net", NULL);
            udev_monitor_enable_receiving(udevMonitor);
            udevNotifier = new QSocketNotifier(udev_monitor_get_fd(udevMonitor), QSocketNotifier::Read, this);
            connect(udevNotifier, SIGNAL(activated(int)), this, SLOT(onUdevChanged()));
        }
Example #2
0
static int sixaxis_init(void)
{
	GIOChannel *channel;

	DBG("");

	ctx = udev_new();
	if (!ctx)
		return -EIO;

	monitor = udev_monitor_new_from_netlink(ctx, "udev");
	if (!monitor) {
		udev_unref(ctx);
		ctx = NULL;

		return -EIO;
	}

	/* Listen for newly connected hidraw interfaces */
	udev_monitor_filter_add_match_subsystem_devtype(monitor, "hidraw",
									NULL);
	udev_monitor_enable_receiving(monitor);

	channel = g_io_channel_unix_new(udev_monitor_get_fd(monitor));
	watch_id = g_io_add_watch(channel, G_IO_IN, monitor_watch, NULL);
	g_io_channel_unref(channel);

	return 0;
}
Example #3
0
QUdevPrivate::QUdevPrivate(QUdev *parent)
  : m_pUdev(0),
    q_ptr(parent),
    m_bMonitoringActive(false)
{

    qRegisterMetaType<QUdevEvent>("QUdevEvent");

    //fill the action map
    m_mUdevActions[QString("add")] = eDeviceAdd;
    m_mUdevActions[QString("remove")] = eDeviceRemove;
    m_mUdevActions[QString("change")] = eDeviceChange;
    m_mUdevActions[QString("online")] = eDeviceOnline;
    m_mUdevActions[QString("offline")] = eDeviceOffline;

    //create the udev object
    m_pUdev = udev_new();
    //set up a udev monitor object
    m_pMon = udev_monitor_new_from_netlink(m_pUdev, "udev");

    Q_ASSERT(m_pUdev);
    Q_ASSERT(m_pMon);

    udev_monitor_enable_receiving(m_pMon);
}
Example #4
0
GamepadController::GamepadController()
    : m_gamepadDevices(Nix::Gamepads::itemsLengthCap)
{
    m_udev = udev_new();
    m_gamepadsMonitor = udev_monitor_new_from_netlink(m_udev, "udev");

    udev_monitor_enable_receiving(m_gamepadsMonitor);
    udev_monitor_filter_add_match_subsystem_devtype(m_gamepadsMonitor, "input", 0);

    GIOChannel *channel = g_io_channel_unix_new(udev_monitor_get_fd(m_gamepadsMonitor));
    g_io_add_watch(channel, GIOCondition(G_IO_IN), static_cast<GIOFunc>(&GamepadController::onGamepadChange), this);
    g_io_channel_unref(channel);

    struct udev_enumerate* enumerate = udev_enumerate_new(m_udev);
    udev_enumerate_add_match_subsystem(enumerate, "input");
    udev_enumerate_add_match_property(enumerate, "ID_INPUT_JOYSTICK", "1");
    udev_enumerate_scan_devices(enumerate);
    struct udev_list_entry* cur;
    struct udev_list_entry* devs = udev_enumerate_get_list_entry(enumerate);
    udev_list_entry_foreach(cur, devs)
    {
        const char* devname = udev_list_entry_get_name(cur);
        struct udev_device* device = udev_device_new_from_syspath(m_udev, devname);
        if (isGamepadDevice(device))
            registerDevice(udev_device_get_devnode(device));
        udev_device_unref(device);
    }
    udev_enumerate_unref(enumerate);
}
Example #5
0
QDeviceDiscovery::QDeviceDiscovery(QDeviceTypes types, struct udev *udev, QObject *parent) :
    QObject(parent),
    m_types(types), m_udev(udev), m_udevMonitor(0), m_udevMonitorFileDescriptor(-1), m_udevSocketNotifier(0)
{
#ifdef QT_QPA_DEVICE_DISCOVERY_DEBUG
    qWarning() << "New UDeviceHelper created for type" << types;
#endif

    if (!m_udev)
        return;

    m_udevMonitor = udev_monitor_new_from_netlink(m_udev, "udev");
    if (!m_udevMonitor) {
#ifdef QT_QPA_DEVICE_DISCOVERY_DEBUG
        qWarning("Unable to create an Udev monitor. No devices can be detected.");
#endif
        return;
    }

    udev_monitor_filter_add_match_subsystem_devtype(m_udevMonitor, "input", 0);
    udev_monitor_filter_add_match_subsystem_devtype(m_udevMonitor, "drm", 0);
    udev_monitor_enable_receiving(m_udevMonitor);
    m_udevMonitorFileDescriptor = udev_monitor_get_fd(m_udevMonitor);

    m_udevSocketNotifier = new QSocketNotifier(m_udevMonitorFileDescriptor, QSocketNotifier::Read, this);
    connect(m_udevSocketNotifier, SIGNAL(activated(int)), this, SLOT(handleUDevNotification()));
}
Example #6
0
UdevNotifier::UdevNotifier(QString const & subsystem, QObject * parent/* = nullptr*/)
    : QObject(parent)
    , d(new Impl)
{
    d->udev = udev_new();
    d->monitor = udev_monitor_new_from_netlink(d->udev, "udev");
    if (nullptr == d->monitor)
    {
        qWarning() << QStringLiteral("UdevNotifier: unable to initialize udev_monitor, monitoring will be disabled");
        return;
    }

    int ret = udev_monitor_filter_add_match_subsystem_devtype(d->monitor, subsystem.toUtf8().constData(), nullptr);
    if (0 != ret)
        qWarning() << QStringLiteral("UdevNotifier: unable to add match subsystem, monitor will receive all devices");

    ret = udev_monitor_enable_receiving(d->monitor);
    if (0 != ret)
    {
        qWarning() << QStringLiteral("UdevNotifier: unable to enable receiving(%1), monitoring will be disabled").arg(ret);
        return;
    }

    d->notifier.reset(new QSocketNotifier(udev_monitor_get_fd(d->monitor), QSocketNotifier::Read));
    connect(d->notifier.data(), &QSocketNotifier::activated, this, &UdevNotifier::eventReady);
    d->notifier->setEnabled(true);
}
Example #7
0
int linux_udev_start_event_monitor(void)
{
	int r;

	assert(udev_ctx == NULL);
	udev_ctx = udev_new();
	if (!udev_ctx) {
		usbi_err(NULL, "could not create udev context");
		return LIBUSB_ERROR_OTHER;
	}

	udev_monitor = udev_monitor_new_from_netlink(udev_ctx, "udev");
	if (!udev_monitor) {
		usbi_err(NULL, "could not initialize udev monitor");
		goto err_free_ctx;
	}

	r = udev_monitor_filter_add_match_subsystem_devtype(udev_monitor, "usb", 0);
	if (r) {
		usbi_err(NULL, "could not initialize udev monitor filter for \"usb\" subsystem");
		goto err_free_monitor;
	}

	if (udev_monitor_enable_receiving(udev_monitor)) {
		usbi_err(NULL, "failed to enable the udev monitor");
		goto err_free_monitor;
	}

	udev_monitor_fd = udev_monitor_get_fd(udev_monitor);

	/* Some older versions of udev are not non-blocking by default,
	 * so make sure this is set */
	r = fcntl(udev_monitor_fd, F_GETFL);
	if (r == -1) {
		usbi_err(NULL, "getting udev monitor fd flags (%d)", errno);
		goto err_free_monitor;
	}
	r = fcntl(udev_monitor_fd, F_SETFL, r | O_NONBLOCK);
	if (r) {
		usbi_err(NULL, "setting udev monitor fd flags (%d)", errno);
		goto err_free_monitor;
	}

	r = pthread_create(&linux_event_thread, NULL, linux_udev_event_thread_main, NULL);
	if (r) {
		usbi_err(NULL, "creating hotplug event thread (%d)", r);
		goto err_free_monitor;
	}

	return LIBUSB_SUCCESS;

err_free_monitor:
	udev_monitor_unref(udev_monitor);
	udev_monitor = NULL;
	udev_monitor_fd = -1;
err_free_ctx:
	udev_unref(udev_ctx);
	udev_ctx = NULL;
	return LIBUSB_ERROR_OTHER;
}
int linux_udev_start_event_monitor(void)
{
	int r;

	if (NULL == udev_ctx) {
		udev_ctx = udev_new();
		if (!udev_ctx) {
			return LIBUSB_ERROR_OTHER;
		}
	}

        udev_monitor = udev_monitor_new_from_netlink(udev_ctx, "udev");
        if (!udev_monitor) {
                usbi_err(NULL, "could not initialize udev monitor");
                return LIBUSB_ERROR_OTHER;
        }

        r = udev_monitor_filter_add_match_subsystem_devtype(udev_monitor, "usb", 0);
        if (r) {
                usbi_err(NULL, "could not initialize udev monitor filter for \"usb\" subsystem");
                return LIBUSB_ERROR_OTHER;
        }

        if (udev_monitor_enable_receiving(udev_monitor)) {
                usbi_err(NULL, "failed to enable the udev monitor");
                return LIBUSB_ERROR_OTHER;
        }

        udev_monitor_fd = udev_monitor_get_fd(udev_monitor);

        pthread_create(&linux_event_thread, NULL, linux_udev_event_thread_main, NULL);

        return LIBUSB_SUCCESS;
}
Example #9
0
rw_status_t
rw_udev_register_cb(rw_udev_handle_t *handle,
                    void *userdata, rw_udev_cb_t cb)
{
  struct udev_enumerate *enumerate = NULL;
  struct udev_list_entry *devices, *dev_list_entry;
  int ret = -1;
  rw_status_t status = RW_STATUS_SUCCESS;
  rw_pci_address_t pci_addr;
  rw_pci_device_t *dev;
  struct udev_device *udevice;
  
  handle->cb = cb;
  handle->userdata = userdata;
  
  //walk throight he list and call the callback..
  enumerate = udev_enumerate_new(handle->udev);
  if (!enumerate){
    goto free_and_ret;
  }
  udev_enumerate_add_match_subsystem(enumerate, "net");
  udev_enumerate_scan_devices(enumerate);
  devices = udev_enumerate_get_list_entry(enumerate);
  udev_list_entry_foreach(dev_list_entry, devices) {
    const char *path;
    path = udev_list_entry_get_name(dev_list_entry);
    ret = rw_sys_populate_pci_from_path(path, &pci_addr);
    if (!ret){
      
    }else{
      udevice = udev_device_new_from_syspath(handle->udev, path);
      dev = rw_udev_insert_device(handle, &pci_addr);
      rw_udev_update_device(udevice, dev);
      if (dev){
        handle->cb(handle->userdata, dev, RW_UDEV_DEVICE_ADD);
      }
      udev_device_unref(udevice);
    }
  }
  
  handle->mon = udev_monitor_new_from_netlink(handle->udev, "udev");
  udev_monitor_filter_add_match_subsystem_devtype(handle->mon, "net", NULL);
  udev_monitor_enable_receiving(handle->mon);
  /* Get the file descriptor (fd) for the monitor.
     This fd will get passed to select() */
  handle->fd = udev_monitor_get_fd(handle->mon);

  
ret:
  return status;
  
free_and_ret:
  if (enumerate){
    udev_enumerate_unref(enumerate);
    enumerate = NULL;
  }
  status = RW_STATUS_FAILURE;
  goto ret;
}
Example #10
0
void input_init(char* mapfile) {
  #ifdef HAVE_LIBCEC
  init_cec();
  #endif

  udev = udev_new();
  if (!udev) {
    fprintf(stderr, "Can't create udev\n");
    exit(1);
  }

  autoadd = (numDevices == 0);
  if (autoadd) {
    struct udev_enumerate *enumerate = udev_enumerate_new(udev);
    udev_enumerate_add_match_subsystem(enumerate, "input");
    udev_enumerate_scan_devices(enumerate);
    struct udev_list_entry *devices = udev_enumerate_get_list_entry(enumerate);

    struct udev_list_entry *dev_list_entry;
    udev_list_entry_foreach(dev_list_entry, devices) {
      const char *path = udev_list_entry_get_name(dev_list_entry);
      struct udev_device *dev = udev_device_new_from_syspath(udev, path);
      const char *devnode = udev_device_get_devnode(dev);
      int id;
      if (devnode != NULL && sscanf(devnode, "/dev/input/event%d", &id) == 1) {
        input_create(devnode, mapfile);
      }
      udev_device_unref(dev);
    }

    udev_enumerate_unref(enumerate);
  }

  udev_mon = udev_monitor_new_from_netlink(udev, "udev");
  udev_monitor_filter_add_match_subsystem_devtype(udev_mon, "input", NULL);
  udev_monitor_enable_receiving(udev_mon);

  udev_fdindex = numFds++;
  sig_fdindex = numFds++;

  if (fds == NULL)
    fds = malloc(sizeof(struct pollfd)*numFds);
  else
    fds = realloc(fds, sizeof(struct pollfd)*numFds);

  if (fds == NULL) {
    fprintf(stderr, "Not enough memory\n");
    exit(EXIT_FAILURE);
  }

  defaultMapfile = mapfile;
  fds[udev_fdindex].fd = udev_monitor_get_fd(udev_mon);
  fds[udev_fdindex].events = POLLIN;

  main_thread_id = pthread_self();
}
Example #11
0
int udev_handler::start_monitor() {
  monitor = udev_monitor_new_from_netlink(udev, "udev");
  udev_monitor_filter_add_match_subsystem_devtype(monitor, "hid", NULL);
  udev_monitor_filter_add_match_subsystem_devtype(monitor, "input", NULL);

  udev_monitor_enable_receiving(monitor);

  monitor_thread = new std::thread(&udev_handler::read_monitor, this);
  return 0;
}
Example #12
0
void GamepadInit(void) {
	struct udev_list_entry* devices;
	struct udev_list_entry* item;
	struct udev_enumerate* enu;
	int i;

	/* initialize connection state */
	for (i = 0; i != GAMEPAD_COUNT; ++i) {
		STATE[i].flags = 0;
		STATE[i].fd = STATE[i].effect = -1;
	}

	/* open the udev handle */
	UDEV = udev_new();
	if (UDEV == NULL) {
		/* FIXME: flag error? */
		return;
	}
	
	/* open monitoring device (safe to fail) */
	MON = udev_monitor_new_from_netlink(UDEV, "udev");
	/* FIXME: flag error if hot-plugging can't be supported? */
	if (MON != NULL) {
		udev_monitor_enable_receiving(MON);
		udev_monitor_filter_add_match_subsystem_devtype(MON, "input", NULL);
	}

	/* enumerate joypad devices */
	enu = udev_enumerate_new(UDEV);
	udev_enumerate_add_match_subsystem(enu, "input");
	udev_enumerate_scan_devices(enu);
	devices = udev_enumerate_get_list_entry(enu);

	udev_list_entry_foreach(item, devices) {
		const char* name;
		const char* sysPath;
		const char* devPath;
		struct udev_device* dev;

		name = udev_list_entry_get_name(item);
		dev = udev_device_new_from_syspath(UDEV, name);
		sysPath = udev_device_get_syspath(dev);
		devPath = udev_device_get_devnode(dev);

		if (sysPath != NULL && devPath != NULL && strstr(sysPath, "/js") != 0) {
			GamepadAddDevice(devPath);
		}

		udev_device_unref(dev);
	}

	/* cleanup */
	udev_enumerate_unref(enu);
}
void joystick_linux::monitor_joysticks(udev *p_udev) {

	udev_device *dev = NULL;
	udev_monitor *mon = udev_monitor_new_from_netlink(p_udev, "udev");
	udev_monitor_filter_add_match_subsystem_devtype(mon, "input", NULL);
	udev_monitor_enable_receiving(mon);
	int fd = udev_monitor_get_fd(mon);

	while (!exit_udev) {

		fd_set fds;
		struct timeval tv;
		int ret;

		FD_ZERO(&fds);
		FD_SET(fd, &fds);
		tv.tv_sec = 0;
		tv.tv_usec = 0;

		ret = select(fd+1, &fds, NULL, NULL, &tv);

		/* Check if our file descriptor has received data. */
		if (ret > 0 && FD_ISSET(fd, &fds)) {
			/* Make the call to receive the device.
			   select() ensured that this will not block. */
			dev = udev_monitor_receive_device(mon);

			if (dev && udev_device_get_devnode(dev) != 0) {

				joy_mutex->lock();
				String action = udev_device_get_action(dev);
				const char* devnode = udev_device_get_devnode(dev);
				if (devnode) {

					String devnode_str = devnode;
					if (devnode_str.find(ignore_str) == -1) {

						if (action == "add")
							open_joystick(devnode);
						else if (String(action) == "remove")
							close_joystick(get_joy_from_path(devnode));
					}
				}

				udev_device_unref(dev);
				joy_mutex->unlock();
			}
		}
		usleep(50000);
	}
	//printf("exit udev\n");
	udev_monitor_unref(mon);
}
Example #14
0
int
main(int argc, char *argv[]) {
    struct udev *udev;
    // struct udev_enumerate *enumerate;
//    struct udev_list_entry *devices, *device;
    udev_monitor = NULL;

    struct udev_device *udev_device;
    const char *action;


    udev = udev_new();
    if (!udev) {
        printf("udev_new failed\n");
        return 0;
    }
    udev_monitor = udev_monitor_new_from_netlink(udev, "udev");
    if (!udev_monitor) {
        printf("udev_monitor_new_from_netlink failed\n");
        return 0;
    }

    udev_monitor_filter_add_match_subsystem_devtype(udev_monitor, "input", NULL);

    if (udev_monitor_enable_receiving(udev_monitor)) {
        printf("config/udev: failed to bind the udev monitor\n");
        return 0;
    }


    int udev_fd = udev_monitor_get_fd(udev_monitor);

    select_for_read(udev_fd);

    udev_device = udev_monitor_receive_device(udev_monitor);
    if (!udev_device) {
        printf("udev_monitor_receive_device failed\n");
        return 0;
    }
    action = udev_device_get_action(udev_device);
    printf("udev action %s\n", action);
    if (action) {
        if (!strcmp(action, "add") || !strcmp(action, "change")) {
            printf("device_removed(udev_device); device_added(udev_device);\n");
        }
        else if (!strcmp(action, "remove")) {
            printf("device_removed(udev_device);\n");
	}
    }
    udev_device_unref(udev_device);

    return 0;
}
Example #15
0
static int
udev_open(struct sol_flow_node *node, void *data, const struct sol_flow_node_options *options)
{
    struct udev_data *mdata = data;
    struct udev_device *device;
    bool value;
    const struct sol_flow_node_type_udev_boolean_options *opts =
        (const struct sol_flow_node_type_udev_boolean_options *)options;

    SOL_FLOW_NODE_OPTIONS_SUB_API_CHECK(options,
        SOL_FLOW_NODE_TYPE_UDEV_BOOLEAN_OPTIONS_API_VERSION, -EINVAL);

    mdata->udev = udev_new();
    SOL_NULL_CHECK(mdata->udev, -EINVAL);

    mdata->monitor = udev_monitor_new_from_netlink(mdata->udev, "udev");
    if (!mdata->monitor) {
        SOL_WRN("Fail on create the udev monitor");
        goto monitor_error;
    }

    if (udev_monitor_enable_receiving(mdata->monitor) < 0) {
        SOL_WRN("error: unable to subscribe to udev events");
        goto receive_error;
    }

    mdata->addr = strdup(opts->address);

    mdata->node = node;
    mdata->watch = sol_fd_add(udev_monitor_get_fd(mdata->monitor),
        SOL_FD_FLAGS_IN | SOL_FD_FLAGS_ERR | SOL_FD_FLAGS_HUP,
        _on_event, mdata);

    device = udev_device_new_from_syspath(mdata->udev, mdata->addr);
    if (device) {
        value = true;
        udev_device_unref(device);
    } else {
        value = false;
    }

    return sol_flow_send_boolean_packet(node,
        SOL_FLOW_NODE_TYPE_UDEV_BOOLEAN__OUT__OUT, value);

receive_error:
    mdata->monitor = udev_monitor_unref(mdata->monitor);
monitor_error:
    mdata->udev = udev_unref(mdata->udev);
    return -EINVAL;
}
gboolean testdisplay_setup_hotplug(void)
{
	int ret;

	udev = udev_new();
	if (!udev) {
		igt_warn("failed to create udev object\n");
		goto out;
	}

	uevent_monitor = udev_monitor_new_from_netlink(udev, "udev");
	if (!uevent_monitor) {
		igt_warn("failed to create udev event monitor\n");
		goto out;
	}

	ret = udev_monitor_filter_add_match_subsystem_devtype(uevent_monitor,
							      "drm",
							      "drm_minor");
	if (ret < 0) {
		igt_warn("failed to filter for drm events\n");
		goto out;
	}

	ret = udev_monitor_enable_receiving(uevent_monitor);
	if (ret < 0) {
		igt_warn("failed to enable udev event reception\n");
		goto out;
	}

	udevchannel =
		g_io_channel_unix_new(udev_monitor_get_fd(uevent_monitor));
	if (!udevchannel) {
		igt_warn("failed to create udev GIO channel\n");
		goto out;
	}

	ret = g_io_add_watch(udevchannel, G_IO_IN | G_IO_ERR, hotplug_event,
			     udev);
	if (ret < 0) {
		igt_warn("failed to add watch on udev GIO channel\n");
		goto out;
	}

	return TRUE;

out:
	testdisplay_cleanup_hotplug();
	return FALSE;
}
Example #17
0
int usbmain(){
    // Load the uinput module (if it's not loaded already)
    if(system("modprobe uinput") != 0)
        ckb_warn("Failed to load uinput module\n");

    // Create the udev object
    if(!(udev = udev_new())){
        ckb_fatal("Failed to initialize udev\n");
        return -1;
    }

    // Enumerate all currently connected devices
    udev_enum();

    // Done scanning. Enter a loop to poll for device updates
    struct udev_monitor* monitor = udev_monitor_new_from_netlink(udev, "udev");
    udev_monitor_filter_add_match_subsystem_devtype(monitor, "usb", 0);
    udev_monitor_enable_receiving(monitor);
    // Get an fd for the monitor
    int fd = udev_monitor_get_fd(monitor);
    fd_set fds;
    while(udev){
        FD_ZERO(&fds);
        FD_SET(fd, &fds);
        // Block until an event is read
        if(select(fd + 1, &fds, 0, 0, 0) > 0 && FD_ISSET(fd, &fds)){
            struct udev_device* dev = udev_monitor_receive_device(monitor);
            if(!dev)
                continue;
            const char* action = udev_device_get_action(dev);
            if(!action){
                udev_device_unref(dev);
                continue;
            }
            // Add/remove device
            if(!strcmp(action, "add")){
                int res = usb_add_device(dev);
                if(res == 0)
                    continue;
                // If the device matched but the handle wasn't opened correctly, re-enumerate (this sometimes solves the problem)
                if(res == -1)
                    udev_enum();
            } else if(!strcmp(action, "remove"))
                usb_rm_device(dev);
            udev_device_unref(dev);
        }
    }
    udev_monitor_unref(monitor);
    return 0;
}
Example #18
0
File: udev.c Project: sylware/lwl
void lwl_udev_init(i ep_fd)
{
  lwl_udev=udev_new();
  if(!lwl_udev){
    PERRC("fatal:unable to init udev context\n");
    exit(LWL_ERR);
  }

  //get events *after* udev rule engine processing
  monitor=udev_monitor_new_from_netlink(lwl_udev,"udev");
  if(!monitor){
    PERRC("fatal:unable to create an udev monitor\n");
    exit(LWL_ERR);
  }

  l r=(l)udev_monitor_filter_add_match_subsystem_devtype(monitor,INPUT_STR,0);
  if(r<0){
    PERRC("fatal:unable to add " INPUT_STR 
                                          " subsystem match to udev monitor\n");
    exit(LWL_ERR);
  }

  r=(l)udev_monitor_filter_add_match_subsystem_devtype(monitor,DCE6_DISPLAY_STR,
                                                                             0);
  if(r<0){
    PERRC("fatal:unable to add " DCE6_DISPLAY_STR
                                          " subsystem match to udev monitor\n");
    exit(LWL_ERR);
  }

  r=(l)udev_monitor_enable_receiving(monitor);
  if(r<0){
    PERRC("fatal:unable to enable receiving of udev monitor events\n");
    exit(LWL_ERR);
  }

  i monitor_fd=udev_monitor_get_fd(monitor);

  struct epoll_event ep_evt;
  memset(&ep_evt,0,sizeof(ep_evt));
  ep_evt.events=EPOLLIN|EPOLLHUP;//*level* triggered
  ep_evt.data._32=EPOLL_UDEV;

  r=epoll_ctl(ep_fd,EPOLL_CTL_ADD,monitor_fd,&ep_evt);
  if(ISERR(r)){
    PERR("fatal(%ld):unable to register udev monitor socket(%d) to epoll\n",r,
                                                                    monitor_fd);
    exit(LWL_ERR);
  }
}
Example #19
0
Monitor::Monitor(Root const &udev, char const *subsystem, char const *devtype)
    : fd_(-1)
    , h_([&udev, subsystem, devtype, this]() {
            auto h = udev.mk_monitor("udev");
            if (!h)
                throw cor::Error("Can't create udev monitor");
            udev_monitor_filter_add_match_subsystem_devtype
                (h.get(), subsystem, devtype);
            udev_monitor_enable_receiving(h_.get());
            fd_ = udev_monitor_get_fd(h_.get());
            return h;
        }())
{
}
Example #20
0
int main ()
{
	udev = udev_new();
	if(!udev) {
		fprintf(stderr, "Can't create udev.\n");
		exit(EXIT_FAILURE);
	}

	mon = udev_monitor_new_from_netlink(udev, "udev");
	udev_monitor_filter_add_match_subsystem_devtype(mon, "block", NULL);
	udev_monitor_enable_receiving(mon);

	while(1)
	{
		if (mon != NULL)
			FD_SET(udev_monitor_get_fd(mon), &readfds);
		select(udev_monitor_get_fd(mon) + 1, &readfds, NULL, NULL, NULL);

		if ((mon != NULL) && FD_ISSET(udev_monitor_get_fd(mon), &readfds)) 
		{
			dev = udev_monitor_receive_device(mon);
			if(dev) {
				device = (char *) udev_device_get_sysname(dev);
			}
		}

		devnum = udev_device_get_devnum(dev);
		major = major(devnum);
		minor = minor(devnum);
		action = udev_device_get_action(dev);
		printf("Processing device %s %d:%d\n", action, major, minor);

		struct udev_list_entry * props = udev_device_get_properties_list_entry(dev);
		while(props != NULL)
		{
			printf("%s = %s\n", udev_list_entry_get_name(props), udev_list_entry_get_value(props));
			props = udev_list_entry_get_next (props);
		}

		props = udev_device_get_sysattr_list_entry(dev);
		while(props != NULL)
		{
			printf("%s = %s\n", udev_list_entry_get_name(props), udev_device_get_sysattr_value(dev, udev_list_entry_get_name(props)));
			props = udev_list_entry_get_next (props);
		}
	}

  	return 0;
}
Example #21
0
void CALSADeviceMonitor::Start()
{
  int err;

  if (!m_udev)
  {
    m_udev = udev_new();
    if (!m_udev)
    {
      CLog::Log(LOGWARNING, "CALSADeviceMonitor::Start - Unable to open udev handle");
      return;
    }

    m_udevMonitor = udev_monitor_new_from_netlink(m_udev, "udev");
    if (!m_udevMonitor)
    {
      CLog::Log(LOGERROR, "CALSADeviceMonitor::Start - udev_monitor_new_from_netlink() failed");
      goto err_unref_udev;
    }

    err = udev_monitor_filter_add_match_subsystem_devtype(m_udevMonitor, "sound", NULL);
    if (err)
    {
      CLog::Log(LOGERROR, "CALSADeviceMonitor::Start - udev_monitor_filter_add_match_subsystem_devtype() failed");
      goto err_unref_monitor;
    }

    err = udev_monitor_enable_receiving(m_udevMonitor);
    if (err)
    {
      CLog::Log(LOGERROR, "CALSADeviceMonitor::Start - udev_monitor_enable_receiving() failed");
      goto err_unref_monitor;
    }

    g_fdEventMonitor.AddFD(
        CFDEventMonitor::MonitoredFD(udev_monitor_get_fd(m_udevMonitor),
                                     POLLIN, FDEventCallback, m_udevMonitor),
        m_fdMonitorId);
  }

  return;

err_unref_monitor:
  udev_monitor_unref(m_udevMonitor);
  m_udevMonitor = NULL;
err_unref_udev:
  udev_unref(m_udev);
  m_udev = NULL;
}
PowerExcessHandler::PowerExcessHandler(QObject *parent):
  QObject(parent), d_ptr(new PowerExcessHandlerPrivate) {
    Q_D(PowerExcessHandler);

    d->udevHandle = udev_new();
    d->udevMonitor = udev_monitor_new_from_netlink(d->udevHandle, "udev");
    udev_monitor_filter_add_match_subsystem_devtype(d->udevMonitor, "misc", 0);
    udev_monitor_enable_receiving(d->udevMonitor);

    d->udevSocketNotifier =
            new QSocketNotifier(udev_monitor_get_fd(d->udevMonitor),
                    QSocketNotifier::Read, this);
    connect(d->udevSocketNotifier, SIGNAL(activated(int)),
            this, SLOT(handleUdevNotification()));
}
Example #23
0
void UdevMonitor::listen(void)
	{
	/* Bail out if the monitor is already listening: */
	if(listening)
		return;
	
	/* Start listening: */
	if(udev_monitor_enable_receiving(monitor)!=0)
		throw std::runtime_error("RawHID::UdevMonitor::listen: Internal libudev error");
	listening=true;
	
	/* Set the monitor's event socket to blocking: */
	int fdFlags=fcntl(fd,F_GETFL);
	fdFlags&=~O_NONBLOCK;
	fcntl(fd,F_SETFL,fdFlags);
	}
Example #24
0
Storage::Storage()
    : privatePartition(NULL)
    , publicPartition(NULL)
    , removablePartition(NULL)
    , dataPartition(NULL)
    , usb_active(false)
{
        qDebug() << Q_FUNC_INFO;
        _partitions = QMap<QString, StoragePartition*>();
        _devices = QMap<QString, StorageDevice*>();

        udev = udev_new();
        if (!udev) {
                qDebug() << Q_FUNC_INFO << "Cannot create udev";
                return;
        }

        enumerate_mmcblk_devices(udev);

        udev_monitor = udev_monitor_new_from_netlink(udev, "udev");
        if (!udev_monitor) {
                qDebug() << Q_FUNC_INFO << "Cannot create mon";
                return;
        }

        if (udev_monitor_filter_add_match_subsystem_devtype(udev_monitor, "block", NULL) != 0) {
                qDebug() << Q_FUNC_INFO << "Error with udev_monitor_filter_add_match_subsystem_devtype";
                return;
        }

        udev_monitor_set_receive_buffer_size(udev_monitor, UDEV_MONITOR_BUFFER_SIZE_STORAGE);

        if (udev_monitor_enable_receiving(udev_monitor) < 0) {
                qDebug() << Q_FUNC_INFO << "Error, cannot receive from kernel";
                return;
        }
        int fd = udev_monitor_get_fd(udev_monitor);
        if (fd <= 0) {
                qDebug() << Q_FUNC_INFO << "Error at udev_monitor_get_fd";
                return;
        }

        notifier = new QSocketNotifier(fd, QSocketNotifier::Read, this);
        notifier->setEnabled(true);
        connect(notifier, SIGNAL(activated(int)), this, SLOT(checkDevice(int)));
}
Example #25
0
/*
 * Initiate the device list (with udev monitoring)
 * args:
 *   none
 * 
 * asserts:
 *   none
 * 
 * returns: none
 */ 
void v4l2core_init_device_list()
{
	/* Create a udev object */
	my_device_list.udev = udev_new();
	/*start udev device monitoring*/
	/* Set up a monitor to monitor v4l2 devices */
	if(my_device_list.udev)
	{
		my_device_list.udev_mon = udev_monitor_new_from_netlink(my_device_list.udev, "udev");
		udev_monitor_filter_add_match_subsystem_devtype(my_device_list.udev_mon, "video4linux", NULL);
		udev_monitor_enable_receiving(my_device_list.udev_mon);
		/* Get the file descriptor (fd) for the monitor */
		my_device_list.udev_fd = udev_monitor_get_fd(my_device_list.udev_mon);

		enum_v4l2_devices();
	}
} 
Example #26
0
static int
udev_start(void)
{
	int fd;

	if (udev) {
		syslog(LOG_ERR, "udev: already started");
		return -1;
	}

	syslog(LOG_DEBUG, "udev: starting");
	udev = udev_new();
	if (udev == NULL) {
		syslog(LOG_ERR, "udev_new: %m");
		return -1;
	}
	monitor = udev_monitor_new_from_netlink(udev, "udev");
	if (monitor == NULL) {
		syslog(LOG_ERR, "udev_monitor_new_from_netlink: %m");
		goto bad;
	}
#ifndef LIBUDEV_NOFILTER
	if (udev_monitor_filter_add_match_subsystem_devtype(monitor,
	    "net", NULL) != 0)
	{
		syslog(LOG_ERR,
		    "udev_monitor_filter_add_match_subsystem_devtype: %m");
		goto bad;
	}
#endif
	if (udev_monitor_enable_receiving(monitor) != 0) {
		syslog(LOG_ERR, "udev_monitor_enable_receiving: %m");
		goto bad;
	}
	fd = udev_monitor_get_fd(monitor);
	if (fd == -1) {
		syslog(LOG_ERR, "udev_monitor_get_fd: %m");
		goto bad;
	}
	return fd;

bad:
	udev_stop();
	return -1;
}
Example #27
0
void CUDevProvider::Initialize()
{
  CLog::Log(LOGDEBUG, "Selected UDev as storage provider");

  m_udev = udev_new();
  if (!m_udev)
  {
    CLog::Log(LOGERROR, "%s - failed to allocate udev context", __FUNCTION__);
    return;
  }
  /* set up a devices monitor that listen for any device change */
  m_udevMon = udev_monitor_new_from_netlink(m_udev, "udev");
  udev_monitor_filter_add_match_subsystem_devtype(m_udevMon, "block", "disk");
  udev_monitor_filter_add_match_subsystem_devtype(m_udevMon, "block", "partition");
  udev_monitor_enable_receiving(m_udevMon);

  PumpDriveChangeEvents(NULL);
}
Example #28
0
int main (int argc, char* argv[])
{
	struct udev *udev;
	struct udev_device *dev;
	struct udev_monitor *mon;
	int fd;

	/* Create the udev object */
	udev = udev_new();
	if (!udev) {
		printf("Can't create udev\n");
		exit(1);
	}

	/* Set up a monitor to monitor power_supply devices */
	mon = udev_monitor_new_from_netlink(udev, "udev");
	udev_monitor_filter_add_match_subsystem_devtype(mon, "power_supply", NULL);
	udev_monitor_enable_receiving(mon);
	printf("[INFO] Monitoring...\n");

	while (1) {
		dev = udev_monitor_receive_device(mon);
			if (dev) {
				
				if(strcmp(udev_device_get_sysname(dev), "ADP1") == 0) {
					printf("\n  [DEBUG] Found ADP1\n");
					
					if(strcmp(udev_device_get_sysattr_value(dev, "online"), "1") == 0) {
						printf("\n  [INFO] AC Adapter State: Online\n");
						changeBrightness(937);
					}
					
					else if (strcmp(udev_device_get_sysattr_value(dev, "online"), "1") < 0) {
						printf("\n  [INFO] AC Adapter State: Offline\n");
						changeBrightness(92);
					}
				}
			}
		udev_device_unref(dev);
		sleep(2);
		}
	udev_unref(udev);
	return 0;
}
Example #29
0
UdevListener::UdevListener(QObject *parent) :
    QObject(parent)
{
    udev = udev_new();

    if (!udev) {
        return;
    }

    /* Set up a monitor to monitor hid devices */
    mon = udev_monitor_new_from_netlink(udev, "udev");
    udev_monitor_filter_add_match_subsystem_devtype(mon, "hid", NULL);
    udev_monitor_enable_receiving(mon);

    fd = udev_monitor_get_fd(mon);

    socketNotifier = new QSocketNotifier(fd, QSocketNotifier::Read, this);
    connect(socketNotifier, SIGNAL(activated(int)), this, SLOT(fdActivated(int)));
}
Example #30
0
void UdevDeviceNotifier::init()
{
    m_udev = udev_new();
    if (!m_udev) {
        return;
    }

    m_monitor = udev_monitor_new_from_netlink(m_udev, "udev");
    if (!m_monitor) {
        return;
    }

    udev_monitor_filter_add_match_subsystem_devtype(m_monitor, "input", 0);
    udev_monitor_enable_receiving(m_monitor);

    int monitor_fd = udev_monitor_get_fd(m_monitor);
    QSocketNotifier *notifier = new QSocketNotifier(monitor_fd, QSocketNotifier::Read, this);
    connect(notifier, &QSocketNotifier::activated, this, &UdevDeviceNotifier::socketActivated);
}