<commit_before>#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <prussdrv.h>
#include <pruss_intc_mapping.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <linux/if.h>
#include <linux/if_tun.h>
#include <arpa/inet.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
#include <unistd.h>

#include "crc32.h"

#define PRU_NUM	0

#define TX0 0x0000
#define TX1 0x0800
#define RX0 0x1000
#define RX1 0x1800

volatile uint16_t *rx_len[2];
volatile uint8_t *rx_data[2];

int tap0_fd;

void *receive_thread (void *arg) {
  uint8_t buffer[1600];
  uint16_t len;
  int i;
  crc_t crc;

  while (1) {
    prussdrv_pru_wait_event(PRU_EVTOUT_1);

    for (i = 0; i < 2; i++) {
      if ((len = ntohs(*rx_len[i]))) {
	memcpy(buffer, (void *) rx_data[i], len - 2); // 2 for length
	*rx_len[i] = 0;
	crc = crc_init();
	crc = crc_update(crc, (void *) buffer, len - 2 - 4); // 2 for length, 4 for crc32
	crc = crc_finalize(crc);
	if (crc == ntohl(*(uint32_t *) (buffer + len - 2 - 4))) {
	  write(tap0_fd, buffer, len - 2 - 4);
	}
      }
    }

    // for some reason we have to use PRU1_ARM_INTERRUPT
    // instead of PRU0_ARM_INTERRUPT
    // see https://groups.google.com/forum/#!topic/beagleboard/e-Nqdngv9mo
    prussdrv_pru_clear_event(PRU_EVTOUT_1, PRU1_ARM_INTERRUPT);
  }
}

int main (void) {
  volatile void *pru0_dataram = NULL;
  volatile uint16_t *tx_len[2];
  volatile uint8_t *tx_data[2];

  struct ifreq ifr;
  char buffer[1600];
  int nread;
  crc_t crc;
  int i;

  pthread_t rx_thread;
  
  if(getuid()!=0){
    printf("You must run this program as root. Exiting.\n");
    exit(EXIT_FAILURE);
  }
  // Initialize structure used by prussdrv_pruintc_intc
  // PRUSS_INTC_INITDATA is found in pruss_intc_mapping.h
  tpruss_intc_initdata pruss_intc_initdata = PRUSS_INTC_INITDATA;
  
  // Allocate and initialize memory
  prussdrv_init();
  prussdrv_open(PRU_EVTOUT_0);<commit_msg>Clear interrupt before reading packets.<commit_after>#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <prussdrv.h>
#include <pruss_intc_mapping.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <linux/if.h>
#include <linux/if_tun.h>
#include <arpa/inet.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
#include <unistd.h>

#include "crc32.h"

#define PRU_NUM	0

#define TX0 0x0000
#define TX1 0x0800
#define RX0 0x1000
#define RX1 0x1800

volatile uint16_t *rx_len[2];
volatile uint8_t *rx_data[2];

int tap0_fd;

void *receive_thread (void *arg) {
  uint8_t buffer[1600];
  uint16_t len;
  int i;
  crc_t crc;

  while (1) {
    prussdrv_pru_wait_event(PRU_EVTOUT_1);

    // for some reason we have to use PRU1_ARM_INTERRUPT
    // instead of PRU0_ARM_INTERRUPT
    // see https://groups.google.com/forum/#!topic/beagleboard/e-Nqdngv9mo
    prussdrv_pru_clear_event(PRU_EVTOUT_1, PRU1_ARM_INTERRUPT);

    for (i = 0; i < 2; i++) {
      if ((len = ntohs(*rx_len[i]))) {
	memcpy(buffer, (void *) rx_data[i], len - 2); // 2 for length
	*rx_len[i] = 0;
	crc = crc_init();
	crc = crc_update(crc, (void *) buffer, len - 2 - 4); // 2 for length, 4 for crc32
	crc = crc_finalize(crc);
	if (crc == ntohl(*(uint32_t *) (buffer + len - 2 - 4))) {
	  write(tap0_fd, buffer, len - 2 - 4);
	}
      }
    }
  }
}

int main (void) {
  volatile void *pru0_dataram = NULL;
  volatile uint16_t *tx_len[2];
  volatile uint8_t *tx_data[2];

  struct ifreq ifr;
  char buffer[1600];
  int nread;
  crc_t crc;
  int i;

  pthread_t rx_thread;
  
  if(getuid()!=0){
    printf("You must run this program as root. Exiting.\n");
    exit(EXIT_FAILURE);
  }
  // Initialize structure used by prussdrv_pruintc_intc
  // PRUSS_INTC_INITDATA is found in pruss_intc_mapping.h
  tpruss_intc_initdata pruss_intc_initdata = PRUSS_INTC_INITDATA;
  
  // Allocate and initialize memory
  prussdrv_init();
  prussdrv_open(PRU_EVTOUT_0);
{'fraction_non_alphanumeric': 0.13928210313447928, 'fraction_numerical': 0.038928210313447925, 'mean_word_length': 3.7674698795180723, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 35, 'https://': 2, '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': '6547843', 'n_tokens_mistral': 1825, 'n_tokens_neox': 1704, 'n_words': 383}