"Fossies" - the Fresh Open Source Software Archive

Member "usbutils-014/usbhid-dump/src/dev.c" (22 Feb 2021, 1126 Bytes) of package /linux/misc/usbutils-014.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "dev.c" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 013_vs_014.

    1 // SPDX-License-Identifier: GPL-2.0-or-later
    2 /*
    3  * usbhid-dump - device
    4  *
    5  * Copyright (C) 2010 Nikolai Kondrashov <spbnick@gmail.com>
    6  */
    7 
    8 #include "config.h"
    9 
   10 #include "dev.h"
   11 #include <assert.h>
   12 #include <stdlib.h>
   13 
   14 bool
   15 uhd_dev_valid(const uhd_dev *dev)
   16 {
   17     return dev != NULL &&
   18            dev->handle != NULL;
   19 }
   20 
   21 
   22 enum libusb_error
   23 uhd_dev_open(libusb_device     *lusb_dev,
   24              uhd_dev          **pdev)
   25 {
   26     enum libusb_error   err;
   27     uhd_dev            *dev;
   28 
   29     assert(lusb_dev != NULL);
   30 
   31     dev = malloc(sizeof(*dev));
   32     if (dev == NULL)
   33         return LIBUSB_ERROR_NO_MEM;
   34 
   35     dev->next       = NULL;
   36 
   37     err = libusb_open(lusb_dev, &dev->handle);
   38     if (err != LIBUSB_SUCCESS)
   39     {
   40         free(dev);
   41         return err;
   42     }
   43 
   44     assert(uhd_dev_valid(dev));
   45 
   46     if (pdev != NULL)
   47         *pdev = dev;
   48     else
   49     {
   50         libusb_close(dev->handle);
   51         free(dev);
   52     }
   53 
   54     return LIBUSB_SUCCESS;
   55 }
   56 
   57 
   58 void
   59 uhd_dev_close(uhd_dev *dev)
   60 {
   61     if (dev == NULL)
   62         return;
   63 
   64     assert(uhd_dev_valid(dev));
   65 
   66     libusb_close(dev->handle);
   67     dev->handle = NULL;
   68 
   69     free(dev);
   70 }
   71 
   72