Пример #1
0
static GioHandle *
connectBluetoothResource (
  const char *identifier,
  const GioDescriptor *descriptor
) {
  GioHandle *handle = malloc(sizeof(*handle));

  if (handle) {
    BluetoothConnectionRequest request;

    bthInitializeConnectionRequest(&request);
    request.identifier = identifier;
    request.channel = descriptor->bluetooth.channelNumber;
    request.discover = descriptor->bluetooth.discoverChannel;

    memset(handle, 0, sizeof(*handle));

    if ((handle->connection = bthOpenConnection(&request))) {
      return handle;
    }

    free(handle);
  } else {
    logMallocError();
  }

  return NULL;
}
Пример #2
0
GioEndpoint *
gioConnectResource (
  const char *identifier,
  const GioDescriptor *descriptor
) {
  GioEndpoint *endpoint;

  if ((endpoint = malloc(sizeof(*endpoint)))) {
    endpoint->bytesPerSecond = 0;

    endpoint->input.error = 0;
    endpoint->input.from = 0;
    endpoint->input.to = 0;

    endpoint->hidReportItems.address = NULL;
    endpoint->hidReportItems.size = 0;

    if (descriptor->serial.parameters) {
      if (isSerialDevice(&identifier)) {
        if ((endpoint->handle.serial.device = serialOpenDevice(identifier))) {
          if (serialSetParameters(endpoint->handle.serial.device, descriptor->serial.parameters)) {
            endpoint->methods = &serialMethods;
            endpoint->options = descriptor->serial.options;
            setBytesPerSecond(endpoint, descriptor->serial.parameters);
            goto connectSucceeded;
          }

          serialCloseDevice(endpoint->handle.serial.device);
        }

        goto connectFailed;
      }
    }

    if (descriptor->usb.channelDefinitions) {
      if (isUsbDevice(&identifier)) {
        if ((endpoint->handle.usb.channel = usbFindChannel(descriptor->usb.channelDefinitions, identifier))) {
          endpoint->methods = &usbMethods;
          endpoint->options = descriptor->usb.options;

          if (!endpoint->options.applicationData) {
            endpoint->options.applicationData = endpoint->handle.usb.channel->definition.data;
          }

          {
            UsbChannel *channel = endpoint->handle.usb.channel;
            const SerialParameters *parameters = channel->definition.serial;
            if (parameters) setBytesPerSecond(endpoint, parameters);
          }

          goto connectSucceeded;
        }

        goto connectFailed;
      }
    }

    if (descriptor->bluetooth.channelNumber) {
      if (isBluetoothDevice(&identifier)) {
        if ((endpoint->handle.bluetooth.connection = bthOpenConnection(identifier, descriptor->bluetooth.channelNumber, 0))) {
          endpoint->methods = &bluetoothMethods;
          endpoint->options = descriptor->bluetooth.options;
          goto connectSucceeded;
        }

        goto connectFailed;
      }
    }

    errno = ENOSYS;
    logMessage(LOG_WARNING, "unsupported input/output resource identifier: %s", identifier);

  connectFailed:
    free(endpoint);
  } else {
    logMallocError();
  }

  return NULL;

connectSucceeded:
  {
    int delay = endpoint->options.readyDelay;
    if (delay) approximateDelay(delay);
  }

  if (!gioDiscardInput(endpoint)) {
    int originalErrno = errno;
    gioDisconnectResource(endpoint);
    errno = originalErrno;
    return NULL;
  }

  return endpoint;
}