consumerir: Dynamically grow an appropriately sized buffer
The IR pulse sequences are written to a local buffer to be passed to the kernel. Previously, we doubled the fixed size buffer (from 1K to 2K) to work around a crash that was occurring for long sequences. Another crash has been found when using Smart IR Remote and the POWER function of the TX P42X10E remote by Panasonic (TV category). Rather than double the size again and wait for another counter- example, dynamically allocate the buffer and grow it as needed to handle any IR sequence. Change-Id: Id4fb799d8a5ad4242e04a68e6cc6436a14f4d2d0
This commit is contained in:
parent
68741c6556
commit
5df5bd5278
@ -15,6 +15,9 @@
|
||||
*/
|
||||
#define LOG_TAG "ConsumerIrHal"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <malloc.h>
|
||||
#include <stdbool.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
@ -34,18 +37,62 @@ static const consumerir_freq_range_t consumerir_freqs[] = {
|
||||
{.min = 56000, .max = 56000},
|
||||
};
|
||||
|
||||
static bool
|
||||
try_append_number(char *buffer, int *len, int size, int number)
|
||||
{
|
||||
int stored;
|
||||
|
||||
stored = snprintf(&buffer[*len], size - *len, "%d,", number);
|
||||
|
||||
if (stored < 0 || stored >= size - *len) {
|
||||
return false;
|
||||
}
|
||||
|
||||
*len += stored;
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool
|
||||
grow_buffer(char **buffer, int *size)
|
||||
{
|
||||
char *new_buffer;
|
||||
|
||||
*size *= 2;
|
||||
if ((new_buffer = realloc(*buffer, *size)) == NULL) {
|
||||
return false;
|
||||
}
|
||||
*buffer = new_buffer;
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool
|
||||
append_number(char **buffer, int *len, int *size, int number)
|
||||
{
|
||||
if (! try_append_number(*buffer, len, *size, number)) {
|
||||
if (! grow_buffer(buffer, size)) return false;
|
||||
return try_append_number(*buffer, len, *size, number);
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
int fd = 0;
|
||||
static int consumerir_transmit(struct consumerir_device *dev,
|
||||
int carrier_freq, int pattern[], int pattern_len)
|
||||
{
|
||||
int strlen;
|
||||
int buffer_len = 0;
|
||||
int buffer_size = 128;
|
||||
int i;
|
||||
char buffer[2048];
|
||||
char *buffer;
|
||||
|
||||
memset(buffer, 0, 2048);
|
||||
if ((buffer = malloc(buffer_size)) == NULL) {
|
||||
return -ENOMEM;
|
||||
}
|
||||
|
||||
/* write the header */
|
||||
strlen = sprintf(buffer, "%d,", carrier_freq);
|
||||
if (! append_number(&buffer, &buffer_len, &buffer_size, carrier_freq)) {
|
||||
goto error;
|
||||
}
|
||||
|
||||
/* calculate factor of conversion from microseconds to pulses */
|
||||
float factor = 1000000 / carrier_freq;
|
||||
@ -53,14 +100,21 @@ static int consumerir_transmit(struct consumerir_device *dev,
|
||||
/* write out the timing pattern */
|
||||
for (i = 0; i < pattern_len; i++)
|
||||
{
|
||||
strlen += sprintf(buffer + strlen, "%d,", (int) (pattern[i]/factor));
|
||||
if (! append_number(&buffer, &buffer_len, &buffer_size, (int) (pattern[i]/factor))) {
|
||||
goto error;
|
||||
}
|
||||
}
|
||||
|
||||
buffer[strlen - 1] = 0;
|
||||
buffer[buffer_len - 1] = 0;
|
||||
write(fd, buffer, buffer_len - 1);
|
||||
|
||||
write(fd, buffer, strlen - 1);
|
||||
free(buffer);
|
||||
|
||||
return 0;
|
||||
|
||||
error:
|
||||
free(buffer);
|
||||
return -ENOMEM;
|
||||
}
|
||||
|
||||
static int consumerir_get_num_carrier_freqs(struct consumerir_device *dev)
|
||||
|
Loading…
Reference in New Issue
Block a user