aboutsummaryrefslogtreecommitdiffstats
path: root/utils/keytable/bpf_protocols/manchester.c
blob: 94b53fd055b9f1eb050c1ed00a85c5fb1eaa0aaf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// SPDX-License-Identifier: GPL-2.0+
//
// Copyright (C) 2018 Sean Young <sean@mess.org>

#include <linux/lirc.h>
#include <linux/bpf.h>

#include "bpf_helpers.h"

struct decoder_state {
	unsigned int state;
	unsigned int count;
	unsigned long bits;
};

struct bpf_map_def SEC("maps") decoder_state_map = {
	.type = BPF_MAP_TYPE_ARRAY,
	.key_size = sizeof(unsigned int),
	.value_size = sizeof(struct decoder_state),
	.max_entries = 1,
};

// These values can be overridden in the rc_keymap toml
//
// We abuse elf relocations. We cast the address of these variables to
// an int, so that the compiler emits a mov immediate for the address
// but uses it as an int. The bpf loader replaces the relocation with the
// actual value (either overridden or taken from the data segment).
//
// See:
// http://clearwater.com.au/code/rc5
int margin = 200;
int header_pulse = 0;
int header_space = 0;
int zero_pulse = 888;
int zero_space = 888;
int one_pulse = 888;
int one_space = 888;
int toggle_bit = 100;
int bits = 14;
int scancode_mask = 0;
int rc_protocol = 66;

#define BPF_PARAM(x) (int)(&(x))

static inline int eq_margin(unsigned d1, unsigned d2)
{
	return ((d1 > (d2 - BPF_PARAM(margin))) && (d1 < (d2 + BPF_PARAM(margin))));
}

#define STATE_INACTIVE 0
#define STATE_HEADER   1
#define STATE_START1   2
#define STATE_MID1     3
#define STATE_MID0     4
#define STATE_START0   5

static int emitBit(unsigned int *sample, struct decoder_state *s, int bit, int state)
{
	s->bits <<= 1;
	s->bits |= bit;
	s->count++;

	if (s->count == BPF_PARAM(bits)) {
		unsigned int toggle = 0;
		unsigned long mask = BPF_PARAM(scancode_mask);
		if (BPF_PARAM(toggle_bit) < BPF_PARAM(bits)) {
			unsigned int tmask = 1 << BPF_PARAM(toggle_bit);
			if (s->bits & tmask)
				toggle = 1;
			mask |= tmask;
		}

		bpf_rc_keydown(sample, BPF_PARAM(rc_protocol), s->bits & ~mask,
			       toggle);
		state = STATE_INACTIVE;
	}

	return state;
}

SEC("manchester")
int bpf_decoder(unsigned int *sample)
{
	unsigned int key = 0;
	struct decoder_state *s = bpf_map_lookup_elem(&decoder_state_map, &key);
	if (!s)
		return 0;

	switch (*sample & LIRC_MODE2_MASK) {
	case LIRC_MODE2_SPACE:
	case LIRC_MODE2_PULSE:
	case LIRC_MODE2_TIMEOUT:
		break;
	default:
		// not a timing events
		return 0;
	}

	int duration = LIRC_VALUE(*sample);
	int pulse = LIRC_IS_PULSE(*sample);

	unsigned int newState = s->state;

	switch (s->state) {
	case STATE_INACTIVE:
		if (BPF_PARAM(header_pulse)) {
			if (pulse &&
			    eq_margin(BPF_PARAM(header_pulse), duration)) {
				s->state = STATE_HEADER;
			}
			break;
		}
		/* pass through */
	case STATE_HEADER:
		if (BPF_PARAM(header_space)) {
			if (!pulse &&
			    eq_margin(BPF_PARAM(header_space), duration)) {
				s->state = STATE_MID1;
			}
			break;
		}
		s->bits = 0;
		s->count = 0;
		/* pass through */
	case STATE_MID1:
		if (!pulse)
			break;

		if (eq_margin(BPF_PARAM(one_pulse), duration))
			newState = emitBit(sample, s, 1, STATE_START1);
		else if (eq_margin(BPF_PARAM(one_pulse) +
				   BPF_PARAM(zero_pulse), duration))
			newState = emitBit(sample, s, 1, STATE_MID0);
		break;
	case STATE_MID0:
		if (pulse)
			break;

		if (eq_margin(BPF_PARAM(zero_space), duration))
			newState = emitBit(sample, s, 0, STATE_START0);
		else if (eq_margin(BPF_PARAM(zero_space) +
				   BPF_PARAM(one_space), duration))
			newState = emitBit(sample, s, 0, STATE_MID1);
		else
			newState = emitBit(sample, s, 0, STATE_INACTIVE);
		break;
	case STATE_START1:
		if (!pulse && eq_margin(BPF_PARAM(zero_space), duration))
			newState = STATE_MID1;
		break;
	case STATE_START0:
		if (pulse && eq_margin(BPF_PARAM(one_pulse), duration))
			newState = STATE_MID0;
		break;
	}

	//char fmt1[] = "bits:%d state:%d newState:%d\n";
	//bpf_trace_printk(fmt1, sizeof(fmt1), pulse, duration, s->bits);
	//char fmt2[] = "count:%d state:%d newState:%d\n";
	//bpf_trace_printk(fmt2, sizeof(fmt2), s->count, s->state, newState);

	if (newState == s->state)
		s->state = STATE_INACTIVE;
	else
		s->state = newState;

	return 0;
}

char _license[] SEC("license") = "GPL";

Privacy Policy