log: add logs using dlog on libhusb apis
authorTaeyoung Kim <ty317.kim@samsung.com>
Wed, 29 Jul 2015 07:32:54 +0000 (16:32 +0900)
committerStanislaw Wadas <s.wadas@samsung.com>
Wed, 2 Dec 2015 12:50:46 +0000 (13:50 +0100)
- When a developer uses the apis, they should check the error situation.
  When the developer uses IDE, dlog will be shown by default. Thus
  dlog is used to store logs of the apis

Change-Id: I279bd1b2104cff33420523141034dc20b1f0bd36
Signed-off-by: Taeyoung Kim <ty317.kim@samsung.com>
CMakeLists.txt
capi-system-usbhost.pc.in
include/log.h [new file with mode: 0755]
src/libhusb.c

index 3226c9ed39c63fa0ec8673d92a93d58b45cffc01..f4a621f93d5d21e473daa0ed47c37be902d22b1c 100644 (file)
@@ -39,6 +39,7 @@ IF(BUILD_SHARED_LIBS)
 
        SET(PKG_MODULES
                libusb-1.0
+               dlog
        )
 
        INCLUDE(FindPkgConfig)
index 6233a57f1a8b039b431c63caf1e14e042f2b8ece..27377b1cbac192d06ab34e0ca35a1f79297ede00 100644 (file)
@@ -6,6 +6,6 @@ includedir=${prefix}/include
 Name: @LIBNAME@
 Description: apis for usb host devices
 Version: @VERSION@
-Requires: @PKG_MODULES@
+Requires:
 Libs: -L${libdir} -l@LIBNAME@
 Cflags: -I${includedir}/
diff --git a/include/log.h b/include/log.h
new file mode 100755 (executable)
index 0000000..589cd35
--- /dev/null
@@ -0,0 +1,32 @@
+/*
+ * libhusb
+ *
+ * Copyright (c) 2015 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+*/
+
+#ifndef __LOG_H__
+#define __LOG_H__
+
+#include <dlog.h>
+
+#undef LOG_TAG
+#define LOG_TAG "LIBHUSB"
+#define _D(fmt, args...)   SLOGD(fmt, ##args)
+#define _E(fmt, args...)   SLOGE(fmt, ##args)
+#define _I(fmt, args...)   SLOGI(fmt, ##args)
+
+#endif /* __LOG_H__ */
+
index 4afd084faf02861038a5241f2af9db94b28240a8..3d8ae66de3ef5b9a1e1f505800eb619459e60ae7 100644 (file)
@@ -26,6 +26,7 @@
 #include "libhusb.h"
 #include "uref.h"
 #include "common.h"
+#include "log.h"
 
 #define MAX_NMB_OF_CONFIGS 255
 
@@ -113,8 +114,10 @@ static struct libhusb_device *alloc_device(libusb_device *lusb_dev)
        struct libhusb_device *dev;
 
        dev = malloc(sizeof(*dev));
-       if (!dev)
+       if (!dev) {
+               _E("malloc() failed");
                goto out;
+       }
 
        uref_init(&dev->ref, free_device);
 
@@ -133,16 +136,20 @@ int libhusb_init(libhusb_context **ctx)
        assert(ctx);
 
        _ctx = malloc(sizeof(*_ctx));
-       if (!_ctx)
+       if (!_ctx) {
+               _E("malloc() failed");
                goto out;
+       }
 
        do {
                ret = libusb_init(&(_ctx->lusb_ctx));
                count--;
        } while (ret == LIBUSB_ERROR_OTHER && count > 0);
 
-       if (ret < 0)
+       if (ret < 0) {
+               _E("Failed to init libusb (%d)", ret);
                goto free_ctx;
+       }
 
        *ctx = _ctx;
 
@@ -172,8 +179,10 @@ libhusb_device_handle *libhusb_open(libhusb_device *dev)
        assert(dev);
 
        handle = malloc(sizeof(*handle));
-       if (!handle)
+       if (!handle) {
+               _E("malloc() failed");
                goto out;
+       }
 
        memset(handle, 0, sizeof(*handle));
 
@@ -182,6 +191,7 @@ libhusb_device_handle *libhusb_open(libhusb_device *dev)
        ret = libusb_open(dev->lusb_dev, &(handle->lusb_dev_handle));
        if (ret < 0) {
                ret = translate_error(ret);
+               _E("Failed to open device using libusb_open (%d)", ret);
                goto unref_dev;
        }
 
@@ -213,21 +223,31 @@ libhusb_device_handle *libhusb_device_open_with_vid_pid(libhusb_context *ctx,
        libusb_device *ldev;
 
        handle = malloc(sizeof(*handle));
-       if (!handle)
+       if (!handle) {
+               _E("malloc() failed");
                goto out;
+       }
 
        ldev_handle = libusb_open_device_with_vid_pid(ctx->lusb_ctx,
                                                      vendor_id, product_id);
-       if (ldev_handle == NULL)
+       if (ldev_handle == NULL) {
+               _E("Failed to open usb device with vid and pid");
                goto free_handle;
+       }
 
        handle->lusb_dev_handle = ldev_handle;
 
        ldev = libusb_get_device(ldev_handle);
+       if (!ldev) {
+               _E("Failed to get device");
+               goto close_handle;
+       }
 
        dev = alloc_device(ldev);
-       if (!dev)
+       if (!dev) {
+               _E("Faild to allocate memory for a device");
                goto close_handle;
+       }
 
        handle->device = dev;
 
@@ -289,21 +309,27 @@ ssize_t libhusb_get_devices(libhusb_context *context, libhusb_device ***devs)
        assert(context);
        assert(devs);
 
+
        len = libusb_get_device_list(context->lusb_ctx, &lusb_list);
        if (len < 0) {
                ret = translate_error(len);
+               _E("Failed to get device list(%d)", ret);
                goto out;
        }
 
        list = calloc(len + 1, sizeof(*list));
-       if (!list)
+       if (!list) {
+               _E("calloc() failed");
                goto free_lusb_list;
+       }
 
        list[len] = NULL;
        for (i = 0; i < len; i++) {
                rdevice = alloc_device(lusb_list[i]);
-               if (!rdevice)
+               if (!rdevice) {
+                       _E("Failed to allocate memory for a device");
                        goto free_dev_list;
+               }
 
                list[i] = rdevice;
        }
@@ -356,8 +382,10 @@ int libhusb_get_max_packet_size(libhusb_device *dev, uint8_t endpoint)
        assert(dev);
 
        ret = libusb_get_max_iso_packet_size(dev->lusb_dev, endpoint);
-       if (ret < 0)
+       if (ret < 0) {
                ret = translate_error(ret);
+               _E("Failed to get max iso packet size(%d)", ret);
+       }
 
        return ret;
 }
@@ -369,8 +397,10 @@ int libhusb_get_active_config(libhusb_device_handle *handle, int *config)
        assert(handle);
 
        ret = libusb_get_configuration(handle->lusb_dev_handle, config);
-       if (ret < 0)
+       if (ret < 0) {
                ret = translate_error(ret);
+               _E("Failed to get configuration(%d)", ret);
+       }
 
        return ret;
 }
@@ -382,8 +412,10 @@ int libhusb_set_config(libhusb_device_handle *handle, int configuration)
        assert(handle);
 
        ret = libusb_set_configuration(handle->lusb_dev_handle, configuration);
-       if (ret < 0)
+       if (ret < 0) {
                ret = translate_error(ret);
+               _E("Failed to set configuration(%d)", ret);
+       }
 
        return ret;
 }
@@ -396,11 +428,14 @@ int libhusb_claim_interface(libhusb_device_handle *handle, int interface_number,
 
        assert(handle);
 
-       if (interface_number < 0 || interface_number > MAX_NMB_OF_CONFIGS)
+       if (interface_number < 0 || interface_number > MAX_NMB_OF_CONFIGS) {
+               _E("Invalid parameters");
                goto out;
+       }
 
        if (!force)
                goto claim_interface;
+
        /*
         * If force param has been set let's check if kernel driver is active
         * and detach it if necessary
@@ -409,12 +444,14 @@ int libhusb_claim_interface(libhusb_device_handle *handle, int interface_number,
                                          interface_number);
        if (ret < 0) {
                ret = translate_error(ret);
+               _E("Faield to activate kernel driver (%d)", ret);
                goto out;
        } else if (ret == 1) {
                ret = libusb_detach_kernel_driver(handle->lusb_dev_handle,
                                                  interface_number);
                if (ret < 0) {
                        ret = translate_error(ret);
+                       _E("Failed to detach kernel driver(%d)", ret);
                        goto out;
                }
 
@@ -425,6 +462,7 @@ claim_interface:
        ret = libusb_claim_interface(handle->lusb_dev_handle, interface_number);
        if (ret < 0) {
                ret = translate_error(ret);
+               _E("Faild to claim interface (%d)", ret);
                goto claim_failed;
        }
 
@@ -446,13 +484,16 @@ int libhusb_release_interface(libhusb_device_handle *handle, int interface_numbe
 
        assert(handle);
 
-       if (interface_number < 0 || interface_number > MAX_NMB_OF_CONFIGS)
+       if (interface_number < 0 || interface_number > MAX_NMB_OF_CONFIGS) {
+               _E("Invalid parameters");
                goto out;
+       }
 
        ret = libusb_release_interface(handle->lusb_dev_handle,
                                       interface_number);
        if (ret != 0) {
                ret = translate_error(ret);
+               _E("Failed to release interface(%d)", ret);
                goto out;
        }
 
@@ -477,8 +518,10 @@ int libhusb_clear_halt(libhusb_device_handle *handle, uint8_t endpoint)
        assert(handle);
 
        ret = libusb_clear_halt(handle->lusb_dev_handle, endpoint);
-       if (ret < 0)
+       if (ret < 0) {
                ret = translate_error(ret);
+               _E("Failed to clear halt (%d)", ret);
+       }
 
        return ret;
 }
@@ -490,8 +533,10 @@ int libhusb_reset_device(libhusb_device_handle *handle)
        assert(handle);
 
        ret = libusb_reset_device(handle->lusb_dev_handle);
-       if (ret < 0)
+       if (ret < 0) {
                ret = translate_error(ret);
+               _E("Failed to reset device (%d)", ret);
+       }
 
        return ret;
 }
@@ -503,8 +548,10 @@ int libhusb_kernel_driver_active(libhusb_device_handle *handle, int interface_nu
        assert(handle);
 
        ret = libusb_kernel_driver_active(handle->lusb_dev_handle, interface_number);
-       if (ret < 0)
+       if (ret < 0) {
                ret = translate_error(ret);
+               _E("Failed to activate kernel driver");
+       }
 
        return ret;
 }
@@ -516,8 +563,10 @@ int libhusb_detach_kernel_driver(libhusb_device_handle *handle, int interface_nu
        assert(handle);
 
        ret = libusb_detach_kernel_driver(handle->lusb_dev_handle, interface_number);
-       if (ret < 0)
+       if (ret < 0) {
                ret = translate_error(ret);
+               _E("Failed to detach kernel driver (%d)", ret);
+       }
 
        return ret;
 }
@@ -529,8 +578,10 @@ int libhusb_attach_kernel_driver(libhusb_device_handle *handle, int interface_nu
        assert(handle);
 
        ret = libusb_attach_kernel_driver(handle->lusb_dev_handle, interface_number);
-       if (ret < 0)
+       if (ret < 0) {
                ret = translate_error(ret);
+               _E("Failed to attach kernel driver(%d)", ret);
+       }
 
        return ret;
 }
@@ -544,8 +595,10 @@ int libhusb_get_device_descriptor(libhusb_device *dev, struct libhusb_device_des
        descriptor = libusb_get_device_descriptor(dev->lusb_dev,
                                                 (struct libusb_device_descriptor *)desc);
 
-       if (descriptor < 0)
+       if (descriptor < 0) {
                descriptor = translate_error(descriptor);
+               _E("Failed to get device descriptor(%d)", descriptor);
+       }
 
        return descriptor;
 }
@@ -598,8 +651,10 @@ static int dump_extra(const unsigned char *src, int src_len,
        if (src_len > 0) {
                extra_length = src_len;
                extra = malloc(extra_length);
-               if (!extra)
+               if (!extra) {
+                       _E("malloc() failed");
                        goto out;
+               }
 
                memcpy(extra, src, extra_length);
        }
@@ -653,19 +708,25 @@ static int dump_interface_desc(struct libhusb_interface_descriptor *dest,
 #undef COPY_FIELD
 
        eps = calloc(dest->bNumEndpoints, sizeof(*eps));
-       if (!eps)
+       if (!eps) {
+               _E("calloc() failed");
                goto out;
+       }
 
        for (i = 0; i < dest->bNumEndpoints; ++i) {
                ret = dump_ep_desc(eps + i, src->endpoint + i);
-               if (ret != 0)
+               if (ret != 0) {
+                       _E("dump_ep_desc() failed (%d)", ret);
                        goto free_eps;
+               }
        }
 
        ret = dump_extra(src->extra, src->extra_length,
                         &(dest->extra), &(dest->extra_length));
-       if (ret)
+       if (ret) {
+               _E("dump_extra() failed (%d)", ret);
                goto free_eps;
+       }
 
        dest->endpoint = eps;
 
@@ -688,13 +749,17 @@ static int dump_interface(struct libhusb_interface *dest,
 
        dest->num_altsetting = src->num_altsetting;
        int_descs = calloc(dest->num_altsetting, sizeof(*int_descs));
-       if (!int_descs)
+       if (!int_descs) {
+               _E("calloc() failed");
                goto out;
+       }
 
        for (i = 0; i < dest->num_altsetting; ++i) {
                ret = dump_interface_desc(int_descs + i, src->altsetting + i);
-               if (ret != 0)
+               if (ret != 0) {
+                       _E("dump_interface_desc() failed (%d)", ret);
                        goto free_int_desc;
+               }
        }
 
        dest->altsetting = int_descs;
@@ -728,19 +793,25 @@ static int dump_config_desc(struct libhusb_cfg_descriptor *dest,
 #undef COPY_FIELD
 
        interfaces = calloc(dest->bNumInterfaces, sizeof(*interfaces));
-       if (!interfaces)
+       if (!interfaces) {
+               _E("calloc() failed");
                goto out;
+       }
 
        for (i = 0; i < dest->bNumInterfaces; ++i) {
                ret = dump_interface(interfaces + i, src->interface + i);
-               if (ret != 0)
+               if (ret != 0) {
+                       _E("dump_interface() failed");
                        goto free_interfaces;
+               }
        }
 
        ret = dump_extra(src->extra, src->extra_length,
                         &(dest->extra), &(dest->extra_length));
-       if (ret)
+       if (ret) {
+               _E("dump_extra() failed");
                goto free_interfaces;
+       }
 
        dest->interface = interfaces;
 
@@ -765,18 +836,22 @@ int libhusb_get_config_descriptor(libhusb_device *dev, uint8_t config_index,
                                           config_index, &lcfg_desc);
        if (ret < 0) {
                ret = translate_error(ret);
+               _E("Failed to get configuration descriptor (%d)", ret);
                goto out;
        }
 
        cfg_desc = malloc(sizeof(*cfg_desc));
        if (!cfg_desc) {
+               _E("malloc() failed");
                ret = LIBHUSB_ERROR_NO_MEM;
                goto free_lcfg_desc;
        }
 
        ret = dump_config_desc(cfg_desc, lcfg_desc);
-       if (ret != 0)
+       if (ret != 0) {
+               _E("dump_config_desc() failed");
                free(cfg_desc);
+       }
 
        *config = cfg_desc;
 
@@ -794,8 +869,10 @@ int libhusb_get_string_descriptor_ascii(libhusb_device_handle *handle,
        assert(handle);
 
        ret = libusb_get_string_descriptor_ascii(handle->lusb_dev_handle, desc_index, data, length);
-       if (ret < 0)
+       if (ret < 0) {
                ret = translate_error(ret);
+               _E("Failded to get descriptor(%d)", ret);
+       }
 
        return ret;
 }
@@ -810,8 +887,10 @@ int libhusb_control_transfer(libhusb_device_handle *handle, uint8_t request_type
 
        ret = libusb_control_transfer(handle->lusb_dev_handle, request_type, bRequest,
                                       wValue, wIndex, data, wLength, timeout);
-       if (ret < 0)
+       if (ret < 0) {
                ret = translate_error(ret);
+               _E("Control transfer failed(%d)", ret);
+       }
 
        return ret;
 }
@@ -826,8 +905,10 @@ int libhusb_bulk_transfer(libhusb_device_handle *handle, uint8_t endpoint,
 
        ret = libusb_bulk_transfer(handle->lusb_dev_handle, endpoint, data, length,
                                   actual_length, timeout);
-       if (ret < 0)
+       if (ret < 0) {
                ret = translate_error(ret);
+               _E("Bulk transfer failed (%d)", ret);
+       }
 
        return ret;
 }
@@ -842,8 +923,10 @@ int libhusb_interrupt_transfer(libhusb_device_handle *handle,
 
        ret = libusb_interrupt_transfer(handle->lusb_dev_handle, endpoint, data, length,
                                        actual_length, timeout);
-       if (ret < 0)
+       if (ret < 0) {
                ret = translate_error(ret);
+               _E("Interrupt transfer failed (%d)", ret);
+       }
 
        return ret;
 }