<commit_before>package org.multibit.hd.hardware.core.usb;

import com.codeminders.hidapi.HIDDevice;
import com.google.common.base.Preconditions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.nio.ByteBuffer;

/**
 * <p>USB HID wrapper to provide the following to USB handlers:</p>
 * <ul>
 * <li>Low level read/write operations with an STM 32F205x USB to UART bridge</li>
 * <li>Simple stream-based communication via a {@link com.codeminders.hidapi.HIDDevice}</li>
 * </ul>
 */
public class STM32F205xBridge {

  /**
   * Provides logging for this class
   */
  private static final Logger log = LoggerFactory.getLogger(STM32F205xBridge.class);

  private final HIDDevice device;
  private final HIDInputStream hidInputStream;
  private final HIDOutputStream hidOutputStream;

  /**
   * @param device The HID device providing the low-level communications
   *
   * @throws java.io.IOException If something goes wrong
   */
  public STM32F205xBridge(HIDDevice device) throws IOException {
    this.device = device;
    this.hidInputStream = new HIDInputStream(device);
    this.hidOutputStream = new HIDOutputStream(device);
  }

  /**
   * <p>Provides an input stream consisting of incoming HID message payload data (no length byte)</p>
   * <p>Applications will use this to read data from the device, perhaps into a Protcol Buffer parser</p>
   *
   * @return The HID input stream
   *
   * @throws java.io.IOException If something goes wrong
   */
  public HIDInputStream getInputStream() throws IOException {
    return this.hidInputStream;
  }

  /**
   * <p>Provides an output stream consisting of outgoing HID message payload data (no length byte)</p>
   * <p>Applications will use this to write data to the device, perhaps from a Protcol Buffer serializer</p>
   *
   * @return The HID input stream
   *
   * @throws java.io.IOException If something goes wrong
   */
  public HIDOutputStream getOutputStream() throws IOException {
    return this.hidOutputStream;

  }

  /**
   * <p>Probe the UART looking for any response</p>
   *
   * @return The number of bytes sent in the feature report
   *
   * @throws java.io.IOException If something goes wrong
   */
  public int probe() throws IOException {

    Preconditions.checkNotNull(device, "Device is not connected");

    System.err.println("Probing...");

//    byte[] reportBuffer = new byte[]{0x0, (byte) 0x81};
//    System.err.printf("Feature report: %d bytes.%n", device.write(reportBuffer));

//    byte[] buf = new byte[32768];
//    int n = device.readTimeout(buf, 1000);

    // Send Initialize
    int msg_id = 0;
    int msg_size = 0;

    ByteBuffer data = ByteBuffer.allocate(9);
    data.put((byte) '#');
    data.put((byte) '#');
    data.put((byte) ((msg_id >> 8) & 0xFF));
    data.put((byte) (msg_id & 0xFF));
    data.put((byte) ((msg_size >> 24) & 0xFF));
    data.put((byte) ((msg_size >> 16) & 0xFF));
    data.put((byte) ((msg_size >> 8) & 0xFF));
    data.put((byte) (msg_size & 0xFF));
    data.put(new byte[0]);

    System.err.printf("Wrote %d bytes.%n", device.write(data.array()));

    // Provide a big buffer
    byte[] buf = new byte[32768];
    int n = device.readTimeout(buf, 1000);

    System.err.printf("Input buffer was %d bytes%n", n);

    // Send Ping
    msg_id = 1;
    msg_size = 0;

    data = ByteBuffer.allocate(9);
    data.put((byte) '#');
    data.put((byte) '#');
    data.put((byte) ((msg_id >> 8) & 0xFF));
    data.put((byte) (msg_id & 0xFF));
    data.put((byte) ((msg_size >> 24) & 0xFF));
    data.put((byte) ((msg_size >> 16) & 0xFF));
    data.put((byte) ((msg_size >> 8) & 0xFF));
    data.put((byte) (msg_size & 0xFF));
    data.put(new byte[0]);

    System.err.printf("Wrote %d bytes.%n", device.write(data.array()));

    // Provide a big buffer
    buf = new byte[32768];
    n = device.readTimeout(buf, 1000);

    System.err.printf("Input buffer was %d bytes%n", n);

    return n;

  }

  /**
   * <p>Reset the UART (equivalent to unplug then replug without warning)
   * </p>
   *
   * @return The number of bytes sent in the feature report
   *
   * @throws java.io.IOException If something goes wrong
   */
  public int reset() throws IOException {

    Preconditions.checkNotNull(device, "Device is not connected");

    byte[] featureReport;
    featureReport = new byte[]{0x040, 0x00};

    int bytesSent = device.sendFeatureReport(featureReport);
    log.debug("> UART Reset: {} '{}'", bytesSent, featureReport);

    return bytesSent;

  }
<commit_msg>Adjusting probe for win32 reportId<commit_after>package org.multibit.hd.hardware.core.usb;

import com.codeminders.hidapi.HIDDevice;
import com.google.common.base.Preconditions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.nio.ByteBuffer;

/**
 * <p>USB HID wrapper to provide the following to USB handlers:</p>
 * <ul>
 * <li>Low level read/write operations with an STM 32F205x USB to UART bridge</li>
 * <li>Simple stream-based communication via a {@link com.codeminders.hidapi.HIDDevice}</li>
 * </ul>
 */
public class STM32F205xBridge {

  /**
   * Provides logging for this class
   */
  private static final Logger log = LoggerFactory.getLogger(STM32F205xBridge.class);

  private final HIDDevice device;
  private final HIDInputStream hidInputStream;
  private final HIDOutputStream hidOutputStream;

  /**
   * @param device The HID device providing the low-level communications
   *
   * @throws java.io.IOException If something goes wrong
   */
  public STM32F205xBridge(HIDDevice device) throws IOException {
    this.device = device;
    this.hidInputStream = new HIDInputStream(device);
    this.hidOutputStream = new HIDOutputStream(device);
  }

  /**
   * <p>Provides an input stream consisting of incoming HID message payload data (no length byte)</p>
   * <p>Applications will use this to read data from the device, perhaps into a Protcol Buffer parser</p>
   *
   * @return The HID input stream
   *
   * @throws java.io.IOException If something goes wrong
   */
  public HIDInputStream getInputStream() throws IOException {
    return this.hidInputStream;
  }

  /**
   * <p>Provides an output stream consisting of outgoing HID message payload data (no length byte)</p>
   * <p>Applications will use this to write data to the device, perhaps from a Protcol Buffer serializer</p>
   *
   * @return The HID input stream
   *
   * @throws java.io.IOException If something goes wrong
   */
  public HIDOutputStream getOutputStream() throws IOException {
    return this.hidOutputStream;

  }

  /**
   * <p>Probe the UART looking for any response</p>
   *
   * @return The number of bytes sent in the feature report
   *
   * @throws java.io.IOException If something goes wrong
   */
  public int probe() throws IOException {

    Preconditions.checkNotNull(device, "Device is not connected");

    System.err.println("Probing...");

    byte[] reportBuffer = new byte[64];
    reportBuffer[0] = 0x0;
    //reportBuffer[1]= (byte) 0x81;
    
    log.info("Feature report > {} bytes.", device.write(reportBuffer));

    byte[] buf = new byte[64];
    log.info("Feature report < {} bytes.", device.readTimeout(buf, 1000));

    // Send Initialize
    int msg_id = 0;
    int msg_size = 0;

    ByteBuffer data = ByteBuffer.allocate(64);
    data.put((byte) '#');
    data.put((byte) '#');
    data.put((byte) ((msg_id >> 8) & 0xFF));
    data.put((byte) (msg_id & 0xFF));
    data.put((byte) ((msg_size >> 24) & 0xFF));
    data.put((byte) ((msg_size >> 16) & 0xFF));
    data.put((byte) ((msg_size >> 8) & 0xFF));
    data.put((byte) (msg_size & 0xFF));
    data.put(new byte[0]);

    log.info("Wrote {} bytes.", device.write(data.array()));

    // Provide a big buffer
    buf = new byte[32768];
    int n = device.readTimeout(buf, 1000);

    log.info("Input buffer was {} bytes", n);

    // Send Ping
    msg_id = 1;
    msg_size = 0;

    data = ByteBuffer.allocate(9);
    data.put((byte) '#');
    data.put((byte) '#');
    data.put((byte) ((msg_id >> 8) & 0xFF));
    data.put((byte) (msg_id & 0xFF));
    data.put((byte) ((msg_size >> 24) & 0xFF));
    data.put((byte) ((msg_size >> 16) & 0xFF));
    data.put((byte) ((msg_size >> 8) & 0xFF));
    data.put((byte) (msg_size & 0xFF));
    data.put(new byte[0]);

    log.info("Wrote {} bytes.", device.write(data.array()));

    // Provide a big buffer
    buf = new byte[32768];
    n = device.readTimeout(buf, 1000);

    log.info("Input buffer was {} bytes", n);

    return n;

  }

  /**
   * <p>Reset the UART (equivalent to unplug then replug without warning)
   * </p>
   *
   * @return The number of bytes sent in the feature report
   *
   * @throws java.io.IOException If something goes wrong
   */
  public int reset() throws IOException {

    Preconditions.checkNotNull(device, "Device is not connected");

    byte[] featureReport;
    featureReport = new byte[]{0x040, 0x00};

    int bytesSent = device.sendFeatureReport(featureReport);
    log.debug("> UART Reset: {} '{}'", bytesSent, featureReport);

    return bytesSent;

  }

{'fraction_non_alphanumeric': 0.137201140601009, 'fraction_numerical': 0.020837903048914235, 'mean_word_length': 3.319753671245855, 'pattern_counts': {'":': 0, '<': 44, '<?xml version=': 0, '>': 78, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7489662', 'n_tokens_mistral': 3066, 'n_tokens_neox': 2858, 'n_words': 834}