/**
   * Disconnect connection to device
   *
   * @return boolean success
   */
  public boolean disconnectDevice() {
    if (!deviceConnected) return false;

    if (connectionMode == deviceConnectionMode.driver) {
      return currentDriver.disconnectDevice();
    } else if (connectionMode == deviceConnectionMode.serial_port) {
      return OpenSIMKit.serialPorts.disconnectPort();
    }

    return false;
  }
  /**
   * Connect via Drivers
   *
   * @return boolean success
   */
  public boolean connectViaDrivers() {
    deviceConnected = false;

    ArrayList<DriverDetails> availableDrivers = OpenSIMKit.drivers.getDriverDetails();
    int numDrivers = availableDrivers.size();

    boolean connectionStatus = false;

    if (deviceXMLProfile.isPopulated()) {
      // Load from the port name
      if (deviceXMLProfile.isDeviceGeneric()) {
        // Generic driver functionality
        boolean driverStatus = false;

        String className = deviceXMLProfile.getDeviceClassName();

        for (int driverLoop = 0; driverLoop < numDrivers; driverLoop++) {
          if (availableDrivers.get(driverLoop).getDriverClass().equals(className)) {
            // We got the driver
            currentDriver = availableDrivers.get(driverLoop).getDriverInterface();
            driverStatus = true;
          }
        }

        if (driverStatus) {
          connectionStatus = currentDriver.connectToDevice();
        }
      } else {
        // Not a generic device. Get the port identification

        ArrayList<String> allPortNames = OpenSIMKit.serialPorts.getSerialPortList();
        int numPorts = allPortNames.size();
        int portIndex = -1;

        for (int currentLoop = 0; currentLoop < numPorts; currentLoop++) {
          if (allPortNames.get(currentLoop).equals(deviceXMLProfile.getDevicePortName())) {
            // We have gotten the driver
            portIndex = currentLoop;
          }
        }

        // Try and see if we can connect to identified device

        if (portIndex >= 0) {
          boolean driverStatus = false;

          String className = deviceXMLProfile.getDeviceClassName();

          for (int driverLoop = 0; driverLoop < numDrivers; driverLoop++) {
            String driverClassName = availableDrivers.get(driverLoop).getDriverClass();
            driverClassName = driverClassName.replace("class", "");
            driverClassName = driverClassName.trim();

            if (driverClassName.equals(className)) {
              // We got the driver
              currentDriver = availableDrivers.get(driverLoop).getDriverInterface();
              driverStatus = true;
            }
          }

          if (driverStatus) {
            connectionStatus = currentDriver.connectToSerialPort(portIndex);
          }
        }
      }

      if (connectionStatus) {
        connectionMode = deviceConnectionMode.driver;
        deviceConnected = true;
        OpenSIMKit.mainFrame.setConnectedInterface();

        return true;
      }
    } else {
      // Device profile not populated, search for appropriate device
      for (int driverLoop = 0; driverLoop < numDrivers; driverLoop++) {
        DriverInterface currentLoopDriver = availableDrivers.get(driverLoop).getDriverInterface();

        if (currentLoopDriver.isGenericConnection()) {
          // Logic for a generic driver
          if (currentLoopDriver.connectToDevice()) {
            currentDriver = currentLoopDriver;
            connectionMode = deviceConnectionMode.driver;

            // Save connection settings for reference the next time
            saveDeviceXMLSettings(
                currentLoopDriver.getManufacturer(),
                currentLoopDriver.getModel(),
                currentLoopDriver.isGenericConnection(),
                currentLoopDriver.getClass().getName(),
                currentLoopDriver.getPortName());

            deviceConnected = true;
            OpenSIMKit.mainFrame.setConnectedInterface();

            return true;
          }
        } else {
          // Logic for a serial port driven device
          String manufacturer = currentLoopDriver.getManufacturer();
          String model = currentLoopDriver.getModel();
          String revision = currentLoopDriver.getRevision();

          PortIdentification portIdentification = searchForDevices(manufacturer, model, revision);

          if (portIdentification != null) {
            int portIndex = portIdentification.getPortIndex();
            String portName = portIdentification.getPortName();

            if (currentLoopDriver.connectToSerialPort(portIndex)) {
              currentDriver = currentLoopDriver;
              connectionMode = deviceConnectionMode.driver;

              // Save connection settings for reference the next time
              saveDeviceXMLSettings(
                  currentLoopDriver.getManufacturer(),
                  currentLoopDriver.getModel(),
                  currentLoopDriver.isGenericConnection(),
                  currentLoopDriver.getClass().getName(),
                  currentLoopDriver.getPortName());

              deviceConnected = true;
              OpenSIMKit.mainFrame.setConnectedInterface();

              return true;
            }
          }
        }
      }
    }

    return false;
  }