Skip to content

Commit

Permalink
Add ir-ctl as rust lib for testing
Browse files Browse the repository at this point in the history
Signed-off-by: Sean Young <[email protected]>
  • Loading branch information
seanyoung committed Apr 27, 2024
1 parent 8baf3ce commit 5e43ca6
Show file tree
Hide file tree
Showing 16 changed files with 3,227 additions and 89 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,14 @@ irp = { version = "0.3.2", path = "irp" }
assert_cmd = "2.0"
serde_json = "1.0"
liblircd = { path = "liblircd" }
libirctl = { path = "libirctl" }
rand = "0.8"

[workspace]
members = [
"irp",
"liblircd",
"libirctl",
"irp/tests/rust-irptransmogrifier",
]

Expand Down
13 changes: 13 additions & 0 deletions libirctl/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "libirctl"
description = "ir-ctl as a library for encoding testing"
version = "0.1.0"
edition = "2021"
publish = false
license = "GPL"

[build-dependencies]
cc = "1.0"

[dependencies]
libc = "0.2"
9 changes: 9 additions & 0 deletions libirctl/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
fn main() {
cc::Build::new()
.file("src/bpf_encoder.c")
.file("src/keymap.c")
.file("src/ir-encode.c")
.file("src/toml.c")
.warnings(false)
.compile("libirctl.a");
}
138 changes: 138 additions & 0 deletions libirctl/src/bpf_encoder.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
/* SPDX-License-Identifier: GPL-2.0 */

#include <stdbool.h>
#include <stdint.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <argp.h>

#include "keymap.h"

// Some keymaps use BPF decoders, so the kernel has no idea how to encode
// them. We need user-space encoders for these.
//
// This encoders should match what the BPF decoders in
// utils/keytable/bpf_protocols/*.c decode.

static void encode_pulse_distance(struct keymap *map, uint32_t scancode, int *buf, int *length)
{
int len = 0, bits, i;

buf[len++] = keymap_param(map, "header_pulse", 2125);
buf[len++] = keymap_param(map, "header_space", 1875);

bits = keymap_param(map, "bits", 4);

if (keymap_param(map, "reverse", 0)) {
for (i = 0; i < bits; i++) {
buf[len++] = keymap_param(map, "bit_pulse", 625);

if (scancode & (1 << i))
buf[len++] = keymap_param(map, "bit_1_space", 1625);
else
buf[len++] = keymap_param(map, "bit_0_space", 375);
}
} else {
for (i = bits - 1; i >= 0; i--) {
buf[len++] = keymap_param(map, "bit_pulse", 625);

if (scancode & (1 << i))
buf[len++] = keymap_param(map, "bit_1_space", 1625);
else
buf[len++] = keymap_param(map, "bit_0_space", 375);
}
}

buf[len++] = keymap_param(map, "trailer_pulse", 625);

*length = len;
}

static void encode_pulse_length(struct keymap *map, uint32_t scancode, int *buf, int *length)
{
int len = 0, bits, i;

buf[len++] = keymap_param(map, "header_pulse", 2125);
buf[len++] = keymap_param(map, "header_space", 1875);

bits = keymap_param(map, "bits", 4);

if (keymap_param(map, "reverse", 0)) {
for (i = 0; i < bits; i++) {
if (scancode & (1 << i))
buf[len++] = keymap_param(map, "bit_1_space", 1625);
else
buf[len++] = keymap_param(map, "bit_0_space", 375);

buf[len++] = keymap_param(map, "bit_pulse", 625);
}
} else {
for (i = bits - 1; i >= 0; i--) {
if (scancode & (1 << i))
buf[len++] = keymap_param(map, "bit_1_space", 1625);
else
buf[len++] = keymap_param(map, "bit_0_space", 375);

buf[len++] = keymap_param(map, "bit_pulse", 625);
}
}

*length = len;
}

static void manchester_advance_space(int *buf, int *len, unsigned length)
{
if (*len % 2)
buf[*len] += length;
else
buf[++(*len)] = length;
}

static void manchester_advance_pulse(int *buf, int *len, unsigned length)
{
if (*len % 2)
buf[++(*len)] = length;
else
buf[*len] += length;
}

static void encode_manchester(struct keymap *map, uint32_t scancode, int *buf, int *length)
{
int len = 0, bits, i;

bits = keymap_param(map, "bits", 14);

for (i = bits - 1; i >= 0; i--) {
if (scancode & (1 << i)) {
manchester_advance_pulse(buf, &len, keymap_param(map, "one_pulse", 888));
manchester_advance_space(buf, &len, keymap_param(map, "one_space", 888));
} else {
manchester_advance_space(buf, &len, keymap_param(map, "zero_space", 888));
manchester_advance_pulse(buf, &len, keymap_param(map, "zero_pulse", 888));
}
}

/* drop any trailing pulse */
*length = (len % 2) ? len : len + 1;
}

bool encode_bpf_protocol(struct keymap *map, uint32_t scancode, int *buf, int *length)
{
if (!strcmp(map->protocol, "pulse_distance")) {
encode_pulse_distance(map, scancode, buf, length);
return true;
}

if (!strcmp(map->protocol, "pulse_length")) {
encode_pulse_length(map, scancode, buf, length);
return true;
}

if (!strcmp(map->protocol, "manchester")) {
encode_manchester(map, scancode, buf, length);
return true;
}

return false;
}
7 changes: 7 additions & 0 deletions libirctl/src/bpf_encoder.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/* SPDX-License-Identifier: GPL-2.0 */
#ifndef __BPF_ENCODER_H
#define __BPF_ENCODER_H

bool encode_bpf_protocol(struct keymap *map, uint32_t scancode, int *buf, int *length);

#endif
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 5e43ca6

Please sign in to comment.