Example #1
0
File: yusb.c Project: emmt/yusb
void Y_usb_open_device(int argc)
{
  ydev_instance_t *obj = NULL;
  libusb_device* dev;
  int bus, port;
  int i, ret;

  if (argc != 2) {
    y_error("expecting exactly 2 arguments");
  }
  bus = ygets_i(1);
  port = ygets_i(0);

  load_device_list();
  for (i = 0; i < dev_count; ++i) {
    dev = dev_list[i];
    if (libusb_get_bus_number(dev) == bus &&
        libusb_get_port_number(dev) == port) {
      obj = (ydev_instance_t *)ypush_obj(&ydev_class, sizeof(ydev_instance_t));
      obj->device = libusb_ref_device(dev);
      ret = libusb_open(obj->device, &obj->handle);
      if (ret < 0) {
        obj->handle = NULL;
        failure("failed to open device", ret);
      }
      obj->bus = bus;
      obj->port = port;
      obj->address = libusb_get_device_address(dev);
      ret = libusb_get_device_descriptor(dev, &obj->descriptor);
      if (ret != 0) {
        free_dev_list();
        failure("unable to get device descriptor", ret);
      }
      break;
    }
  }
  free_dev_list();
  if (obj == NULL) {
    ypush_nil();
  }
}
Example #2
0
File: yusb.c Project: emmt/yusb
void Y_usb_debug(int argc)
{
  int level;

  if (argc != 1) {
    y_error("expecting exactly one argument");
  }
  level = ygets_i(0);
  INITIALIZE;
  libusb_set_debug(ctx, level);
  ypush_nil();
}
Example #3
0
File: yusb.c Project: emmt/yusb
void Y_usb_control_transfer(int argc)
{
  ydev_instance_t* obj;
  int ret;
  int type, request, value, index, length;
  long size;
  unsigned char* data;
  unsigned int timeout;

  /* Get arguments. */
  if (argc != 8) {
    y_error("expecting exactly 8 arguments");
  }
  obj = get_device(7);
  type = ygets_i(6) & 0xff; /* uint8_t */
  request = ygets_i(5) & 0xff; /* uint8_t */
  value = ygets_i(4) & 0xffff; /* uint16_t */
  index = ygets_i(3) & 0xffff; /* uint16_t */
  data = (unsigned char*)get_data(2, &size);
  length = ygets_i(1) & 0xffff; /* uint16_t */
  timeout = (unsigned int)(ygets_l(0) & 0xffffffffL);
  if (length < 0) {
    y_error("invalid length");
  }
  if (length > size) {
    y_error("length must be at most the size of the data");
  }

  /* Apply operation. */
  ret = libusb_control_transfer(obj->handle, type, request, value,
                                index, data, length, timeout);
  if (ret < 0 && yarg_subroutine()) {
    failure(NULL, ret);
  }
  ypush_int(ret);
}
Example #4
0
File: yusb.c Project: emmt/yusb
void Y_usb_claim_interface(int argc)
{
  ydev_instance_t* obj;
  int interface_number;
  int ret;

  if (argc != 2) {
    y_error("expecting exactly 2 arguments");
  }
  obj = get_device(1);
  interface_number = ygets_i(0);

  ret = libusb_claim_interface(obj->handle, interface_number);
  if (ret < 0 && yarg_subroutine()) {
    failure(NULL, ret);
  }
  ypush_int(ret);
}
Example #5
0
File: yusb.c Project: emmt/yusb
void Y_usb_get_string(int argc)
{
  char str[STRING_DESCRIPTOR_SIZE];
  ydev_instance_t* obj;
  int index;
  int ret;

  if (argc != 2) {
    y_error("expecting exactly 2 arguments");
  }
  obj = get_device(1);
  index = ygets_i(0);

  ret = get_string_descriptor(obj->handle, index, str, sizeof(str));
  if (ret < 0) {
    ypush_int(ret);
  } else {
    str[sizeof(str)/sizeof(str[0]) - 1] = 0;
    push_string(str);
  }
}
Example #6
0
void
gy_value_set_iarg(GValue* pval, GITypeInfo * info, int iarg)
{
  GY_DEBUG("in gy_value_set_iarg\n");
  GITypeTag type = g_type_info_get_tag(info);
  GIBaseInfo * itrf;
  switch (type) {
  case GI_TYPE_TAG_BOOLEAN:
    g_value_set_boolean(pval, ygets_c(iarg));
    break;
  case GI_TYPE_TAG_INT8:
    g_value_set_schar(pval, ygets_c(iarg));
    break;
  case GI_TYPE_TAG_UINT8:
    g_value_set_uchar(pval, ygets_c(iarg));
    break;
  case GI_TYPE_TAG_INT16:
  case GI_TYPE_TAG_INT32:
    g_value_set_int(pval, ygets_i(iarg));
    break;
  case GI_TYPE_TAG_UINT16:
  case GI_TYPE_TAG_UINT32:
    g_value_set_uint(pval, ygets_i(iarg));
    break;
  case GI_TYPE_TAG_INT64:
    g_value_set_int64(pval, ygets_l(iarg));
    break;
  case GI_TYPE_TAG_UINT64:
    g_value_set_uint64(pval, ygets_l(iarg));
    break;
  case GI_TYPE_TAG_FLOAT:
    g_value_set_float(pval, ygets_f(iarg));
    break;
  case GI_TYPE_TAG_DOUBLE:
    g_value_set_double(pval, ygets_d(iarg));
    break;
  case GI_TYPE_TAG_GTYPE:
    g_value_set_gtype(pval, ygets_l(iarg));
    break;
  case GI_TYPE_TAG_UTF8:
  case GI_TYPE_TAG_FILENAME:
    g_value_set_static_string (pval, ygets_q(iarg));
    GY_DEBUG("GValue is string: \"%s\"\n", ygets_q(iarg));
    break;
    /* interface types */
  case GI_TYPE_TAG_INTERFACE:
    itrf = g_type_info_get_interface(info);
    switch(g_base_info_get_type (itrf)) {
    case GI_INFO_TYPE_ENUM:
      g_value_set_enum (pval, ygets_l(iarg));
      break;
    case GI_INFO_TYPE_OBJECT:
      g_value_set_object(pval, yget_gy_Object(iarg)->object);
      break;
    default:
      y_errorn("Unimplemented GValue interface type %ld",
	       g_base_info_get_type (itrf));
    }
    g_base_info_unref(itrf);
    break;
  case GI_TYPE_TAG_VOID:
  default:
    y_error("Unimplement property GValue type");
  }
  GY_DEBUG("out gy_iarg2gvalue\n");
}
Example #7
0
File: yusb.c Project: emmt/yusb
 static void do_transfer(int argc,
                         int (*transfer)(struct libusb_device_handle* handle,
                                         unsigned char endpoint,
                                         unsigned char* data,
                                         int length,
                                         int* transferred,
                                         unsigned int timeout))
{
  ydev_instance_t* obj;
  int ret, iarg;
  int endpoint, length, transferred, offset;
  long size;
  unsigned char* data;
  unsigned int timeout;
  long transferred_index;

  /* Get arguments. */
  if (argc != 6 && argc != 7) {
    y_error("expecting 6 or 7 arguments");
  }
  iarg = argc;
  obj = get_device(--iarg);
  endpoint = ygets_i(--iarg) & 0xff; /* uint8_t */
  data = (unsigned char*)get_data(--iarg, &size);
  length = ygets_i(--iarg);
  if (length < 0) {
    y_error("invalid length");
  }
  if (length > size) {
    y_error("length must be at most the size of the data");
  }
  transferred_index = yget_ref(--iarg);
  if (transferred_index < 0L) {
    y_error("expecting a simple variable reference");
  }
  timeout = (unsigned int)(ygets_l(--iarg) & 0xffffffffL);
  if (iarg > 0) {
    offset = ygets_i(--iarg);
    if (offset < 0 || offset > length) {
      y_error("invalid offset");
    }
  } else {
    offset = 0;
  }

  /* Apply operation. */
  if (length > offset) {
    ret = transfer(obj->handle, endpoint, data + offset,
                   length - offset, &transferred, timeout);
    if (!(ret == 0 || (ret == LIBUSB_ERROR_TIMEOUT && transferred > 0))) {
      /* No data has been transferred. */
      transferred = 0;
      if (yarg_subroutine()) {
        /* Some error has occured. */
        failure(NULL, ret);
      }
    }
  } else {
    /* Nothing to do. */
    transferred = 0;
    ret = 0;
  }
  if (transferred_index >= 0L) {
    /* Store total number of transferred bytes so far. */
    ypush_int(offset + transferred);
    yput_global(transferred_index, 0);
  }
  ypush_int(ret);
}
Example #8
0
File: yusb.c Project: emmt/yusb
void Y_usb_error_description(int argc)
{
  if (argc != 1) y_error("expected exactly one argument");
  push_string(get_error_description(ygets_i(0)));
}