/**
   * Reads profile from XML file if present
   *
   * @return boolean success
   */
  private boolean readDeviceXMLFile() {
    try {
      // Open and parse XML
      File fXmlFile = new File(pathToDeviceSettingsFile);
      DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
      DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
      Document doc = dBuilder.parse(fXmlFile);

      doc.getDocumentElement().normalize();

      // Read XML
      NodeList manufacturerList = doc.getElementsByTagName(this.DEVICE_MANUFACTURER);
      NodeList deviceModelList = doc.getElementsByTagName(this.DEVICE_MODEL);
      NodeList deviceGenericList = doc.getElementsByTagName(this.DEVICE_GENERIC);
      NodeList deviceClassNameList = doc.getElementsByTagName(this.DEVICE_CLASS_NAME);
      NodeList devicePortNameList = doc.getElementsByTagName(this.DEVICE_PORT_NAME);

      String manufacturerValue = manufacturerList.item(0).getTextContent();
      String modelValue = deviceModelList.item(0).getTextContent();
      String deviceGenericValue = deviceGenericList.item(0).getTextContent();
      String deviceClassNameValue = deviceClassNameList.item(0).getTextContent();
      String devicePortNameValue = devicePortNameList.item(0).getTextContent();

      boolean deviceIsGeneric = Boolean.parseBoolean(deviceGenericValue);

      // Populate class wide device settings profile
      deviceXMLProfile.setDeviceManufacturer(manufacturerValue);
      deviceXMLProfile.setDeviceModel(modelValue);
      deviceXMLProfile.setDeviceGeneric(deviceIsGeneric);
      deviceXMLProfile.setDevicePortName(devicePortNameValue);
      deviceXMLProfile.setDeviceClassName(deviceClassNameValue);
      deviceXMLProfile.setPopulated(true);
    } catch (Exception ex) {
      Logger.getLogger(AnonymousDataCollection.class.getName()).log(Level.SEVERE, null, ex);

      return false;
    }

    return true;
  }
  /**
   * 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;
  }