<commit_before>/*
 * Debug sensor reader.
 *
 * Copyright (C) 2012 by <NAME> <<EMAIL>>
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Affero General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or (at your
 * option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License
 * for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/time.h>
#include <sys/types.h>
#include <errno.h>
#include <time.h>

void show_help(const char *app)
{
  fprintf(stderr, "usage: %s [options]\n", app);
  fprintf(stderr,
    "       -h         this text\n"
    "       -i sensor  sensor identifier\n"
    "       -d device  serial device\n"
    "       -t timeout wanted timeout in ms (default = 100ms)\n"
    "       -s value   write value to sensor\n"
  );
}

int main(int argc, char **argv)
{
  // Parse program options
  char *sensor_id = NULL;
  char *device = NULL;
  char *aset = NULL;
  unsigned short timeout = 100;
  
  char c;
  while ((c = getopt(argc, argv, "hi:d:s:t:")) != EOF) {
    switch (c) {
      case 'h': {
        show_help(argv[0]);
        return 1;
      }
      case 'i': sensor_id = strdup(optarg); break;
      case 'd': device = strdup(optarg); break;
      case 's': aset = strdup(optarg); break;
      case 't': timeout = atoi(optarg); break;
      default: {
        fprintf(stderr, "ERROR: Invalid option %c!\n", c);
        show_help(argv[0]);
        return 1;
      }
    }
  }
  
  if (sensor_id == NULL || device == NULL) {
    fprintf(stderr, "ERROR: Sensor identifier and serial device path are required!\n");
    return 1;
  }
  
  // Open the serial device
  FILE *serial = fopen(device, "r+");
  if (!serial) {
    fprintf(stderr, "ERROR: Failed to open the serial device '%s'!\n", device);
    return 2;
  }

  // Setup the serial device for non-blocking mode
  int serial_fd = fileno(serial);
  if (fcntl(serial_fd, F_SETFL, O_NONBLOCK) < 0) {
    fprintf(stderr, "ERROR: Failed to setup the serial device!\n");
    return 2;
  }
  
  char buffer[1024] = {0,};
  
  if (aset != NULL) {
    // Send write command instead of read
    if (fprintf(serial, "ACOM /%s %s\n", sensor_id, aset) < 0) {
      fprintf(stderr, "ERROR: Failed to send ASET %s command!\n", aset);
      fclose(serial);
      return 3;
    }
  } else {
    // Send read command
    if (fprintf(serial, "ACOM /%s\n", sensor_id) < 0) {
      fprintf(stderr, "ERROR: Failed to send AGET command!\n");
      fclose(serial);<commit_msg>Configure serial device for RAW I/O.<commit_after>/*
 * Debug sensor reader.
 *
 * Copyright (C) 2012 by <NAME> <<EMAIL>>
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Affero General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or (at your
 * option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License
 * for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/time.h>
#include <sys/types.h>
#include <errno.h>
#include <time.h>
#include <termios.h>

void show_help(const char *app)
{
  fprintf(stderr, "usage: %s [options]\n", app);
  fprintf(stderr,
    "       -h         this text\n"
    "       -i sensor  sensor identifier\n"
    "       -d device  serial device\n"
    "       -b rate    device baudrate (default = 115200)\n"
    "       -t timeout wanted timeout in ms (default = 100ms)\n"
    "       -s value   write value to sensor\n"
  );
}

int main(int argc, char **argv)
{
  // Parse program options
  char *sensor_id = NULL;
  char *device = NULL;
  char *aset = NULL;
  unsigned short timeout = 100;
  unsigned int baudrate = 115200;
  
  char c;
  while ((c = getopt(argc, argv, "hi:d:s:t:b:")) != EOF) {
    switch (c) {
      case 'h': {
        show_help(argv[0]);
        return 1;
      }
      case 'i': sensor_id = strdup(optarg); break;
      case 'd': device = strdup(optarg); break;
      case 's': aset = strdup(optarg); break;
      case 't': timeout = atoi(optarg); break;
      case 'b': baudrate = atoi(optarg); break;
      default: {
        fprintf(stderr, "ERROR: Invalid option %c!\n", c);
        show_help(argv[0]);
        return 1;
      }
    }
  }
  
  if (sensor_id == NULL || device == NULL) {
    fprintf(stderr, "ERROR: Sensor identifier and serial device path are required!\n");
    return 1;
  }
  
  // Open the serial device
  FILE *serial = fopen(device, "r+");
  if (!serial) {
    fprintf(stderr, "ERROR: Failed to open the serial device '%s'!\n", device);
    return 2;
  }

  // Setup the serial device for non-blocking mode
  int serial_fd = fileno(serial);
  if (fcntl(serial_fd, F_SETFL, O_NONBLOCK) < 0) {
    fprintf(stderr, "ERROR: Failed to setup the serial device!\n");
    return 2;
  }
  
  // Configure the serial device
  struct termios tio;
  if (tcgetattr(serial_fd, &tio) < 0) {
    fprintf(stderr, "ERROR: Failed to configure the serial device!\n");
    return 2;
  }
  
  // Configure for RAW I/O and setup baudrate
  cfmakeraw(&tio);
  cfsetispeed(&tio, baudrate);
  cfsetospeed(&tio, baudrate);
  
  if (tcsetattr(serial_fd, TCSAFLUSH, &tio) < 0) {
    fprintf(stderr, "ERROR: Failed to configure the serial device!\n");
    return 2;
  }
  
  char buffer[1024] = {0,};
  
  if (aset != NULL) {
    // Send write command instead of read
    if (fprintf(serial, "ACOM /%s %s\n", sensor_id, aset) < 0) {
      fprintf(stderr, "ERROR: Failed to send ASET %s command!\n", aset);
      fclose(serial);
      return 3;
    }
  } else {
    // Send read command
    if (fprintf(serial, "ACOM /%s\n", sensor_id) < 0) {
      fprintf(stderr, "ERROR: Failed to send AGET command!\n");
      fclose(serial);
{'fraction_non_alphanumeric': 0.13151061554910645, 'fraction_numerical': 0.010691919963341989, 'mean_word_length': 2.7697179044329303, 'pattern_counts': {'":': 0, '<': 36, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '1471383', 'n_tokens_mistral': 2241, 'n_tokens_neox': 2185, 'n_words': 755}