<commit_before>/****************************************************************************
 * receive.c ***************************************************************
 ****************************************************************************
 *
 * functions that decode IR codes
 *
 * Copyright (C) 1999 <NAME> <<EMAIL>>
 *
 */

/**
 * @file receive.c
 * @author <NAME>
 * @brief Implements receive.h
 */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <limits.h>
#include <errno.h>
#include <poll.h>

#include "include/media/lirc.h"
#include "lirc/driver.h"
#include "lirc/lirc_log.h"
#include "lirc/receive.h"
#include "lirc/ir_remote.h"

#define RBUF_SIZE 512

#define REC_SYNC 8

static const logchannel_t logchannel = LOG_LIB;

/**
 * Structure for the receiving buffer.
 */
struct rbuf {
	lirc_t		data[RBUF_SIZE];
	ir_code		decoded;
	int		rptr;
	int		wptr;
	int		too_long;
	int		is_biphase;
	lirc_t		pendingp;
	lirc_t		pendings;
	lirc_t		sum;
	struct timeval	last_signal_time;
	int		at_eof;
	FILE*		input_log;
};


/**
 * Global receiver buffer.
 */
static struct rbuf rec_buffer;
static int update_mode = 0;


void rec_set_update_mode(int mode)
{
	update_mode = mode;
}


static lirc_t readdata(lirc_t timeout)
{
	lirc_t data;

	data = curr_driver->readdata(timeout);
	rec_buffer.at_eof = data & LIRC_EOF ? 1 : 0;
	if (rec_buffer.at_eof)
		log_debug("receive: Got EOF");
	return data;
}


static lirc_t lirc_t_max(lirc_t a, lirc_t b)
{
	return a > b ? a : b;
}

static void set_pending_pulse(lirc_t deltap)
{
	log_trace2("pending pulse: %lu", deltap);
	rec_buffer.pendingp = deltap;
}

static void set_pending_space(lirc_t deltas)
{
	log_trace2("pending space: %lu", deltas);
	rec_buffer.pendings = deltas;
}


static void log_input(lirc_t data)
{
	fprintf(rec_buffer.input_log, "%s %u\n",
		data & PULSE_BIT ? "pulse" : "space", data & PULSE_MASK);
	fflush(rec_buffer.input_log);
}


static lirc_t get_next_rec_buffer_internal(lirc_t maxusec)
{
	if (rec_buffer.rptr < rec_buffer.wptr) {
		log_trace2("<%c%lu", rec_buffer.data[rec_buffer.rptr] & PULSE_BIT ? 'p' : 's', (__u32)
			  rec_buffer.data[rec_buffer.rptr] & (PULSE_MASK));
		rec_buffer.sum += rec_buffer.data[rec_buffer.rptr] & (PULSE_MASK);
		return rec_buffer.data[rec_buffer.rptr++];
	}
	if (rec_buffer.wptr < RBUF_SIZE) {
		lirc_t data = 0;
		unsigned long elapsed = 0;

		if (timerisset(&rec_buffer.last_signal_time)) {
			struct timeval current;

			gettimeofday(&current, NULL);
			elapsed = time_elapsed(&rec_buffer.last_signal_time, &current);
		}
		if (elapsed < maxusec)
			data = readdata(maxusec - elapsed);
		if (!data) {
			log_trace2("timeout: %u", maxusec);
			return 0;
		}
		if (data & LIRC_EOF) {
			log_debug("Receive: returning EOF");
			return data;
		}
		if (LIRC_IS_TIMEOUT(data)) {
			log_trace("timeout received: %lu", (__u32)LIRC_VALUE(data));
			if (LIRC_VALUE(data) < maxusec)
				return get_next_rec_buffer_internal(maxusec - LIRC_VALUE(data));
			return 0;
		}

		rec_buffer.data[rec_buffer.wptr] = data;
		if (rec_buffer.input_log != NULL)
			log_input(data);
		if (rec_buffer.data[rec_buffer.wptr] == 0)
			return 0;
		rec_buffer.sum += rec_buffer.data[rec_buffer.rptr]
				  & (PULSE_MASK);
		rec_buffer.wptr++;
		rec_buffer.rptr++;
		log_trace2("+%c%lu", rec_buffer.data[rec_buffer.rptr - 1] & PULSE_BIT ? 'p' : 's', (__u32)
			  rec_buffer.data[rec_buffer.rptr - 1]
			  & (PULSE_MASK));
		return rec_buffer.data[rec_buffer.rptr - 1];
	}
	rec_buffer.too_long = 1;
	return 0;
}

int waitfordata(__u32 maxusec)
{
	int ret;
	struct pollfd pfd = {
		.fd = curr_driver->fd, .events = POLLIN, .revents = 0 };

	while (1) {
		do {
			do {
				ret = poll(&pfd, 1, (maxusec > 0) ? (maxusec / 1000) : -1);
			} while (ret == -1 && errno == EINTR);
			if (ret == -1) {
				log_perror_err("poll() failed");<commit_msg>lib: receive: Return 0 on poll() timeout.<commit_after>/****************************************************************************
 * receive.c ***************************************************************
 ****************************************************************************
 *
 * functions that decode IR codes
 *
 * Copyright (C) 1999 <NAME> <<EMAIL>>
 *
 */

/**
 * @file receive.c
 * @author <NAME>
 * @brief Implements receive.h
 */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <limits.h>
#include <errno.h>
#include <poll.h>

#include "include/media/lirc.h"
#include "lirc/driver.h"
#include "lirc/lirc_log.h"
#include "lirc/receive.h"
#include "lirc/ir_remote.h"

#define RBUF_SIZE 512

#define REC_SYNC 8

static const logchannel_t logchannel = LOG_LIB;

/**
 * Structure for the receiving buffer.
 */
struct rbuf {
	lirc_t		data[RBUF_SIZE];
	ir_code		decoded;
	int		rptr;
	int		wptr;
	int		too_long;
	int		is_biphase;
	lirc_t		pendingp;
	lirc_t		pendings;
	lirc_t		sum;
	struct timeval	last_signal_time;
	int		at_eof;
	FILE*		input_log;
};


/**
 * Global receiver buffer.
 */
static struct rbuf rec_buffer;
static int update_mode = 0;


void rec_set_update_mode(int mode)
{
	update_mode = mode;
}


static lirc_t readdata(lirc_t timeout)
{
	lirc_t data;

	data = curr_driver->readdata(timeout);
	rec_buffer.at_eof = data & LIRC_EOF ? 1 : 0;
	if (rec_buffer.at_eof)
		log_debug("receive: Got EOF");
	return data;
}


static lirc_t lirc_t_max(lirc_t a, lirc_t b)
{
	return a > b ? a : b;
}

static void set_pending_pulse(lirc_t deltap)
{
	log_trace2("pending pulse: %lu", deltap);
	rec_buffer.pendingp = deltap;
}

static void set_pending_space(lirc_t deltas)
{
	log_trace2("pending space: %lu", deltas);
	rec_buffer.pendings = deltas;
}


static void log_input(lirc_t data)
{
	fprintf(rec_buffer.input_log, "%s %u\n",
		data & PULSE_BIT ? "pulse" : "space", data & PULSE_MASK);
	fflush(rec_buffer.input_log);
}


static lirc_t get_next_rec_buffer_internal(lirc_t maxusec)
{
	if (rec_buffer.rptr < rec_buffer.wptr) {
		log_trace2("<%c%lu", rec_buffer.data[rec_buffer.rptr] & PULSE_BIT ? 'p' : 's', (__u32)
			  rec_buffer.data[rec_buffer.rptr] & (PULSE_MASK));
		rec_buffer.sum += rec_buffer.data[rec_buffer.rptr] & (PULSE_MASK);
		return rec_buffer.data[rec_buffer.rptr++];
	}
	if (rec_buffer.wptr < RBUF_SIZE) {
		lirc_t data = 0;
		unsigned long elapsed = 0;

		if (timerisset(&rec_buffer.last_signal_time)) {
			struct timeval current;

			gettimeofday(&current, NULL);
			elapsed = time_elapsed(&rec_buffer.last_signal_time, &current);
		}
		if (elapsed < maxusec)
			data = readdata(maxusec - elapsed);
		if (!data) {
			log_trace2("timeout: %u", maxusec);
			return 0;
		}
		if (data & LIRC_EOF) {
			log_debug("Receive: returning EOF");
			return data;
		}
		if (LIRC_IS_TIMEOUT(data)) {
			log_trace("timeout received: %lu", (__u32)LIRC_VALUE(data));
			if (LIRC_VALUE(data) < maxusec)
				return get_next_rec_buffer_internal(maxusec - LIRC_VALUE(data));
			return 0;
		}

		rec_buffer.data[rec_buffer.wptr] = data;
		if (rec_buffer.input_log != NULL)
			log_input(data);
		if (rec_buffer.data[rec_buffer.wptr] == 0)
			return 0;
		rec_buffer.sum += rec_buffer.data[rec_buffer.rptr]
				  & (PULSE_MASK);
		rec_buffer.wptr++;
		rec_buffer.rptr++;
		log_trace2("+%c%lu", rec_buffer.data[rec_buffer.rptr - 1] & PULSE_BIT ? 'p' : 's', (__u32)
			  rec_buffer.data[rec_buffer.rptr - 1]
			  & (PULSE_MASK));
		return rec_buffer.data[rec_buffer.rptr - 1];
	}
	rec_buffer.too_long = 1;
	return 0;
}

int waitfordata(__u32 maxusec)
{
	int ret;
	struct pollfd pfd = {
		.fd = curr_driver->fd, .events = POLLIN, .revents = 0 };

	while (1) {
		do {
			do {
				ret = poll(&pfd, 1, (maxusec > 0) ? (maxusec / 1000) : -1);
				if (maxusec > 0 && ret == 0)
					return 0;
			} while (ret == -1 && errno == EINTR);
			if (ret == -1) {
				log_perror_err("poll() failed");
{'fraction_non_alphanumeric': 0.19210866752910738, 'fraction_numerical': 0.01241914618369987, 'mean_word_length': 4.227180527383367, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 28, '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': '3043866', 'n_tokens_mistral': 3442, 'n_tokens_neox': 3096, 'n_words': 628}