Ejemplo n.º 1
0
int
connectBrailleResource (
  BrailleDisplay *brl,
  const char *identifier,
  const GioDescriptor *descriptor,
  BrailleSessionInitializer *initializeSession
) {
  if ((brl->gioEndpoint = gioConnectResource(identifier, descriptor))) {
    if (!initializeSession || initializeSession(brl)) {
      if (gioDiscardInput(brl->gioEndpoint)) {
        return 1;
      }
    }

    gioDisconnectResource(brl->gioEndpoint);
    brl->gioEndpoint = NULL;
  }

  return 0;
}
Ejemplo n.º 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;
}