예제 #1
0
파일: richcomm_usb.c 프로젝트: sbutler/nut
/*
 * Initialise the UPS
 */
int upsdrv_initups(void)
{
	char	reply[REPLY_PACKETSIZE];
	int	i;

	for (i = 0; usb_device_open(&udev, &usbdevice, &device_matcher, &driver_callback) < 0; i++) {

		if ((i < 32) && (sleep(5) == 0)) {
			usb_comm_fail("Can't open USB device, retrying ...");
			continue;
		}

		fatalx(EXIT_FAILURE,
			"Unable to find Richcomm dry-contact to USB solution\n\n"

			"Things to try:\n"
			" - Connect UPS device to USB bus\n"
			" - Run this driver as another user (upsdrvctl -u or 'user=...' in ups.conf).\n"
			"   See upsdrvctl(8) and ups.conf(5).\n\n"

			"Fatal error: unusable configuration");
	}

	/*
	 * Read rubbish data a few times; the UPS doesn't seem to respond properly
	 * the first few times after connecting
	 */
	for (i = 0; i < 5; i++) {
		query_ups(reply);
		sleep(1);
	}

	return 1;
}
예제 #2
0
파일: usb.c 프로젝트: h0nzZik/libiqrf
device_t *usb_device_open_by_addr(usb_addr *addr)
{
	int i;
	int cnt;
	struct libusb_device **list;
	device_t *device;
	usb_addr curr;

	cnt = libusb_get_device_list(usb_ctx, &list);
	if (cnt < 0)
		return NULL;

	for (i=0; i<cnt; i++) {
		usb_device_get_addr(list[i], &curr);
		if (!memcmp(addr, &curr, sizeof(curr)))
			break;
	}

	device = NULL;
	if (i == cnt)
		goto end;

	if (!device_supported(list[i]))
		goto end;

	device = usb_device_open(list[i]);
end:
	libusb_free_device_list(list, 1);
	return device;
}
예제 #3
0
파일: richcomm_usb.c 프로젝트: sbutler/nut
void upsdrv_updateinfo(void)
{
	char	reply[REPLY_PACKETSIZE];
	int	ret, online, battery_normal;

	if (!udev) {
		ret = usb_device_open(&udev, &usbdevice, &device_matcher, &driver_callback);

		if (ret < 0) {
			return;
		}
	}

	ret = query_ups(reply);

	if (ret < 4) {
		usb_comm_fail("Query to UPS failed");
		dstate_datastale();

		usb_device_close(udev);
		udev = NULL;

		return;
	}

	usb_comm_good();
	dstate_dataok();

	/*
	 * 3rd bit of 4th byte indicates whether the UPS is on line (1)
	 * or on battery (0)
	 */
	online = (reply[3]&4)>>2;

	/*
	 * 2nd bit of 4th byte indicates battery status; normal (1)
	 * or low (0)
	 */
	battery_normal = (reply[3]&2)>>1;

	status_init();

	if (online) {
	    status_set("OL");
	} else {
	    status_set("OB");
	}

	if (!battery_normal) {
	    status_set("LB");
	}

	status_commit();
}
예제 #4
0
static int lsusb_device_added(const char *dev_name, void *client_data)
{
    struct usb_device *dev = usb_device_open(dev_name);

    if (!dev) {
        fprintf(stderr, "can't open device %s: %s\n", dev_name, strerror(errno));
        return 0;
    }

    if (verbose) {
        struct usb_descriptor_iter iter;
        struct usb_descriptor_header *desc;

        printf("%s:\n", dev_name);

        usb_descriptor_iter_init(dev, &iter);

        while ((desc = usb_descriptor_iter_next(&iter)) != NULL)
            lsusb_parse_descriptor(dev, desc);

    } else {
        uint16_t vid, pid;
        char *mfg_name, *product_name, *serial;

        vid = usb_device_get_vendor_id(dev);
        pid = usb_device_get_product_id(dev);
        mfg_name = usb_device_get_manufacturer_name(dev);
        product_name = usb_device_get_product_name(dev);
        serial = usb_device_get_serial(dev);

        printf("%s: %04x:%04x %s %s %s\n", dev_name, vid, pid,
               mfg_name, product_name, serial);

        free(mfg_name);
        free(product_name);
        free(serial);
    }

    usb_device_close(dev);

    return 0;
}
예제 #5
0
/**
 * handle bus event
 *
 * @param [in, out] h
 *
 * @return E_ERR_SUCCESS
 */
e_mmgr_errors_t bus_ev_hdle_events(bus_ev_hdle_t *h)
{
    e_mmgr_errors_t ret = E_ERR_FAILED;
    bus_ev_t *bus_events = (bus_ev_t *)h;
    int i;

    for (i = 0; i < bus_events->cli_ctx.i; i++) {
        LOG_DEBUG("Event: %s %d", bus_events->cli_ctx.evs[i].path,
                  bus_events->cli_ctx.evs[i].event);
        if (bus_events->cli_ctx.evs[i].event == EV_ADDED) {
            struct usb_device *dev;

            LOG_DEBUG("EVENT ADDED");

            dev = usb_device_open(bus_events->cli_ctx.evs[i].path);
            if (dev == NULL) {
                LOG_ERROR("Failed to open path: %s",
                          bus_events->cli_ctx.evs[i].path);
            } else {
                if (is_pid_and_vid(dev,
                                   bus_events->modem_flash_pid,
                                   bus_events->modem_flash_vid)) {
                    bus_events->mdm_state |= MDM_FLASH_READY;
                    strncpy(bus_events->modem_flash_path,
                            bus_events->cli_ctx.evs[i].path, PATH_MAX);
                    ret = E_ERR_SUCCESS;
                    LOG_DEBUG("+Modem flash is READY");
                } else if (is_pid_and_vid(dev,
                                          bus_events->modem_bb_pid,
                                          bus_events->modem_bb_vid)) {
                    bus_events->mdm_state |= MDM_BB_READY;
                    strncpy(bus_events->modem_bb_path,
                            bus_events->cli_ctx.evs[i].path, PATH_MAX);
                    ret = E_ERR_SUCCESS;
                    LOG_DEBUG("+Modem base band READY");
                } else if (is_pid_and_vid(dev,
                                          bus_events->mcdr_bb_pid,
                                          bus_events->mcdr_bb_vid)) {
                    bus_events->mdm_state |= MDM_CD_READY;
                    strncpy(bus_events->modem_cd_path,
                            bus_events->cli_ctx.evs[i].path, PATH_MAX);
                    ret = E_ERR_SUCCESS;
                    LOG_DEBUG("+Modem core dump READY");
                }

                usb_device_close(dev);
            }
        } else if (bus_events->cli_ctx.evs[i].event == EV_DELETED) {
            LOG_DEBUG("EVENT DELETED");
            if (strncmp(bus_events->modem_flash_path,
                        bus_events->cli_ctx.evs[i].path, PATH_MAX) == 0) {
                bus_events->mdm_state &= ~MDM_FLASH_READY;
                bus_events->modem_flash_path[0] = '\0';
                ret = E_ERR_SUCCESS;
                LOG_DEBUG("-Modem flash not READY");
            } else if (strncmp(bus_events->modem_bb_path,
                               bus_events->cli_ctx.evs[i].path,
                               PATH_MAX) == 0) {
                bus_events->mdm_state &= ~MDM_BB_READY;
                bus_events->modem_bb_path[0] = '\0';
                ret = E_ERR_SUCCESS;
                LOG_DEBUG("-Modem base band not READY");
            } else if (strncmp(bus_events->modem_cd_path,
                               bus_events->cli_ctx.evs[i].path,
                               PATH_MAX) == 0) {
                bus_events->mdm_state &= ~MDM_CD_READY;
                bus_events->modem_cd_path[0] = '\0';
                ret = E_ERR_SUCCESS;
                LOG_DEBUG("-Modem core dump not READY");
            }
        }
    }

    bus_events->cli_ctx.i = 0;
    return ret;
}
예제 #6
0
    // Callback for notification when new USB devices are attached. Return true to exit from usb_host_run.
  int usb_device_added (const char * devname, void * client_data) {
    logd ("\n");
    //logd ("client_data: %p  devname: %s", client_data, devname);
    logd ("devname: %s", devname);

    uint16_t usb_vid, usb_pid;
    int ret;
    //int unused = (int) client_data;

    if (ena_su_perm) {    // Set Permission w/ SU:
      char cmd [256] = "su -c chmod 777 ";
      strlcat (cmd, devname, sizeof (cmd));
      //strlcat (cmd, " 2>&1 > /sdcard/res", sizeof (cmd));
      errno = 0;
      ret = system (cmd);                                             // !! Binaries like ssd that write to stdout cause C system() to crash !
      logd ("system() ret: %d  errno: %d (%s)", ret, errno, strerror (errno));
    }

    struct usb_device * device = usb_device_open (devname);
    if  (! device) {
      loge ("usb_device_open failed");
      return (0);                                                       // 0 = Leave usb_host_run() running
    }

    usb_vid = usb_device_get_vendor_id  (device);
    usb_pid = usb_device_get_product_id (device);
    char * usb_ser = usb_device_get_serial (device);

    logd ("usb_vid: 0x%x (%d - %s)  usb_pid: 0x%x  usb_ser: %s", usb_vid, usb_vid, usb_vid_get (usb_vid), usb_pid, usb_ser);

    if (current_device != NULL) {                                       // If we already have a device, just ignore
      logd ("Ignoring have current_device: %p", current_device);
      usb_device_close (device);
      return (0);                                                       // 0 = Leave usb_host_run() running
    }

    if  (usb_vid != USB_VID_GOO || usb_pid >= USB_PID_ACC_MIN ||  usb_pid <= USB_PID_ACC_MAX) { // If not in Google Accessory mode...
      logd ("Found new device - attempting to switch to accessory mode");

      uint16_t protocol = -1;
      errno = 0;
      ret = usb_device_control_transfer (device, USB_DIR_IN | USB_TYPE_VENDOR, ACC_REQ_GET_PROTOCOL, 0, 0, & protocol, sizeof (protocol), 1000);
      if  (ret < 0) {
        loge ("No Acc OAP protocol ret: %d  errno: %d", ret, errno);
        usb_device_close (device);
        return (0);                                                     // 0 = Leave usb_host_run() running
      }
      if  (protocol < 2) {
        loge ("Acc OAP protocol version %d", protocol);
        usb_device_close (device);
        return (0);                                                   // 0 = Leave usb_host_run() running
      }
      logd ("Acc OAP protocol version %d", protocol);

      send_string (device, ACC_IDX_MAN, AAP_VAL_MAN);                 // Send Acc strings
      send_string (device, ACC_IDX_MOD, AAP_VAL_MOD);
      //send_string (device, ACC_IDX_DES, AAP_VAL_DES);
      //send_string (device, ACC_IDX_VER, AAP_VAL_VER);
      //send_string (device, ACC_IDX_URI, AAP_VAL_URI);
      //send_string (device, ACC_IDX_SER, AAP_VAL_SER);

      errno = 0;
      ret = usb_device_control_transfer (device, USB_DIR_OUT | USB_TYPE_VENDOR, ACC_REQ_START, 0, 0, NULL, 0, 1000);
      if  (ret < 0)
        loge ("Acc Start ret: %d errno: %d", ret, errno);
      else
        logd ("Acc Start ret: %d", ret);


    }

    logd ("Found android device in accessory mode");

    //pthread_mutex_lock (& device_mutex);
    //pthread_cond_broadcast (& device_cond);
    //pthread_mutex_unlock (& device_mutex);

    struct usb_descriptor_iter        iter;
    struct usb_descriptor_header    * desc  = NULL;
    struct usb_interface_descriptor * intf  = NULL;
    struct usb_endpoint_descriptor  * ep   = NULL;

    usb_descriptor_iter_init (device, & iter);                        // Init for iterating USB descriptors
    
    read_ep  = 255;
    write_ep = 255;

    while  ((desc = usb_descriptor_iter_next (& iter)) != NULL) {
      if (0);
      else if  (desc->bDescriptorType == LIBUSB_DT_DEVICE) {
        logd ("Device desc: %p", desc);
      }
      else if  (desc->bDescriptorType == LIBUSB_DT_CONFIG) {
        logd ("Config desc: %p", desc);
      }
      else if  (desc->bDescriptorType == LIBUSB_DT_STRING) {
        logd ("String desc: %p", desc);
      }
      else if  (desc->bDescriptorType == LIBUSB_DT_HID) {
        logd ("HID desc: %p", desc);
      }
      else if  (desc->bDescriptorType == LIBUSB_DT_REPORT) {
        logd ("HID Report desc: %p", desc);
      }
      else if  (desc->bDescriptorType == LIBUSB_DT_PHYSICAL) {
        logd ("Physical desc: %p", desc);
      }
      else if  (desc->bDescriptorType == LIBUSB_DT_HUB) {
        logd ("Hub desc: %p", desc);
      }
      else if  (desc->bDescriptorType == LIBUSB_DT_INTERFACE) {
        intf =  (struct usb_interface_descriptor *) desc;
        logd ("Interface desc/intf: %p", desc);
      }
      else if  (desc->bDescriptorType == LIBUSB_DT_ENDPOINT) {
        logd ("Endpoint  desc/ep:   %p", desc);
        ep = (struct usb_endpoint_descriptor *) desc;
        uint8_t ep_addr = ep->bEndpointAddress;
        if  ((ep_addr & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN) {
          if (read_ep == 255) {
            read_ep  = ep_addr;
            logd ("Set as first read endpoint ep_addr: %d");
          }
          else
            loge ("Ignored already have read endpoint ep_addr: %d");
        }
        else {
          if (write_ep == 255) {
            write_ep = ep_addr;
            logd ("Set as first write endpoint ep_addr: %d");
          }
          else
            loge ("Ignored already have write endpoint ep_addr: %d");
        }
      }
      else
        loge ("Unknown desc->bDescriptorType: %d", desc->bDescriptorType);
    }

    if  (! intf) {
      loge ("interface not found");
      usb_device_close (device);
      return (0);                                                     // 0 = Leave usb_host_run() running
    }
    if  (read_ep == 255 || write_ep == 255) {
      loge ("Read and/or write endpoints not found");
      usb_device_close (device);
      return (0);                                                     // 0 = Leave usb_host_run() running
    }

    errno = 0;
    ret = usb_device_claim_interface (device, intf->bInterfaceNumber);
    if  (ret) {
      loge ("Error usb_device_claim_interface() errno: %d", errno);
      usb_device_close (device);
      return (0);                                                     // 0 = Leave usb_host_run() running
    }

    current_device = device;

    loge ("Success usb_device_claim_interface() : Can start AA protocol now");

    //ret = hu_aap_start ();

    byte vr_buf [] = {0, 3, 0, 6, 0, 1, 0, 1, 0, 1};                    // Version Request
    //ret = hu_aap_usb_send (vr_buf, sizeof (vr_buf), 1000);             // Send Version Request
    errno = 0;
    ret = hu_bulk_send (vr_buf, sizeof (vr_buf));
    if (ret) {
      loge ("Error hu_bulk_send() ret: %d  errno: %d (%s)", ret, errno, strerror (errno));
      usb_device_close (device);
      return (0);                                                     // 0 = Leave usb_host_run() running
    }
    logd ("Success hu_bulk_send()");

    return (0);                                                         // 0 = Leave usb_host_run() running
  }
예제 #7
0
static int usb_device_added(const char *devname, void* client_data) {
    struct usb_descriptor_header* desc;
    struct usb_descriptor_iter iter;
    uint16_t vendorId, productId;
    int ret;
    pthread_t th;

    struct usb_device *device = usb_device_open(devname);
    if (!device) {
        fprintf(stderr, "usb_device_open failed\n");
        return 0;
    }

    vendorId = usb_device_get_vendor_id(device);
    productId = usb_device_get_product_id(device);

    if (!sDevice && (vendorId == 0x18D1 && (productId == 0x2D00 || productId == 0x2D01))) {
        struct usb_descriptor_header* desc;
        struct usb_descriptor_iter iter;
        struct usb_interface_descriptor *intf = NULL;
        struct usb_endpoint_descriptor *ep1 = NULL;
        struct usb_endpoint_descriptor *ep2 = NULL;

        printf("Found Android device in accessory mode (%x:%x)...\n",
               vendorId, productId);
        sDevice = device;

        usb_descriptor_iter_init(device, &iter);
        while ((desc = usb_descriptor_iter_next(&iter)) != NULL && (!intf || !ep1 || !ep2)) {
            if (desc->bDescriptorType == USB_DT_INTERFACE) {
                intf = (struct usb_interface_descriptor *)desc;
            } else if (desc->bDescriptorType == USB_DT_ENDPOINT) {
                if (ep1)
                    ep2 = (struct usb_endpoint_descriptor *)desc;
                else
                    ep1 = (struct usb_endpoint_descriptor *)desc;
            }
        }

        if (!intf) {
            fprintf(stderr, "Interface not found\n");
            exit(1);
        }
        if (!ep1 || !ep2) {
            fprintf(stderr, "Endpoints not found\n");
            exit(1);
        }

        if (usb_device_claim_interface(device, intf->bInterfaceNumber)) {
            fprintf(stderr, "usb_device_claim_interface failed errno: %d\n", errno);
            exit(1);
        }

        int endpoints[2];
        if ((ep1->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN) {
            endpoints[0] = ep1->bEndpointAddress;
            endpoints[1] = ep2->bEndpointAddress;
        } else {
            endpoints[0] = ep2->bEndpointAddress;
            endpoints[1] = ep1->bEndpointAddress;
        }
        pthread_create(&th, NULL, message_thread, (void *)endpoints);
    } else {
        printf("Found possible Android device (%x:%x) "
                "- attempting to switch to accessory mode...\n", vendorId, productId);

        uint16_t protocol = 0;
        ret = usb_device_control_transfer(device, USB_DIR_IN | USB_TYPE_VENDOR,
                ACCESSORY_GET_PROTOCOL, 0, 0, &protocol, sizeof(protocol), 0);
        if (ret == 2)
            printf("Device supports protocol version %d\n", protocol);
        else
            fprintf(stderr, "Failed to read protocol version\n");

        send_string(device, ACCESSORY_STRING_MANUFACTURER, "Android CTS");
        send_string(device, ACCESSORY_STRING_MODEL, "CTS USB Accessory");
        send_string(device, ACCESSORY_STRING_DESCRIPTION, "CTS USB Accessory");
        send_string(device, ACCESSORY_STRING_VERSION, "1.0");
        send_string(device, ACCESSORY_STRING_URI,
                "http://source.android.com/compatibility/cts-intro.html");
        send_string(device, ACCESSORY_STRING_SERIAL, "1234567890");

        ret = usb_device_control_transfer(device, USB_DIR_OUT | USB_TYPE_VENDOR,
                ACCESSORY_START, 0, 0, 0, 0, 0);
        return 0;
    }

    if (device != sDevice)
        usb_device_close(device);

    return 0;
}