-
Notifications
You must be signed in to change notification settings - Fork 45
/
hdhomerun_pkt.c
239 lines (198 loc) · 5.34 KB
/
hdhomerun_pkt.c
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/*
* hdhomerun_pkt.c
*
* Copyright © 2006-2014 Silicondust USA Inc. <www.silicondust.com>.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "hdhomerun.h"
struct hdhomerun_pkt_t *hdhomerun_pkt_create(void)
{
struct hdhomerun_pkt_t *pkt = (struct hdhomerun_pkt_t *)calloc(1, sizeof(struct hdhomerun_pkt_t));
if (!pkt) {
return NULL;
}
hdhomerun_pkt_reset(pkt);
return pkt;
}
void hdhomerun_pkt_destroy(struct hdhomerun_pkt_t *pkt)
{
free(pkt);
}
void hdhomerun_pkt_reset(struct hdhomerun_pkt_t *pkt)
{
pkt->limit = pkt->buffer + sizeof(pkt->buffer) - 4;
pkt->start = pkt->buffer + 1024;
pkt->end = pkt->start;
pkt->pos = pkt->start;
}
static uint32_t hdhomerun_pkt_calc_crc(uint8_t *start, uint8_t *end)
{
uint8_t *pos = start;
uint32_t crc = 0xFFFFFFFF;
while (pos < end) {
uint8_t x = (uint8_t)(crc) ^ *pos++;
crc >>= 8;
if (x & 0x01) crc ^= 0x77073096;
if (x & 0x02) crc ^= 0xEE0E612C;
if (x & 0x04) crc ^= 0x076DC419;
if (x & 0x08) crc ^= 0x0EDB8832;
if (x & 0x10) crc ^= 0x1DB71064;
if (x & 0x20) crc ^= 0x3B6E20C8;
if (x & 0x40) crc ^= 0x76DC4190;
if (x & 0x80) crc ^= 0xEDB88320;
}
return crc ^ 0xFFFFFFFF;
}
uint8_t hdhomerun_pkt_read_u8(struct hdhomerun_pkt_t *pkt)
{
uint8_t v = *pkt->pos++;
return v;
}
uint16_t hdhomerun_pkt_read_u16(struct hdhomerun_pkt_t *pkt)
{
uint16_t v;
v = (uint16_t)*pkt->pos++ << 8;
v |= (uint16_t)*pkt->pos++ << 0;
return v;
}
uint32_t hdhomerun_pkt_read_u32(struct hdhomerun_pkt_t *pkt)
{
uint32_t v;
v = (uint32_t)*pkt->pos++ << 24;
v |= (uint32_t)*pkt->pos++ << 16;
v |= (uint32_t)*pkt->pos++ << 8;
v |= (uint32_t)*pkt->pos++ << 0;
return v;
}
size_t hdhomerun_pkt_read_var_length(struct hdhomerun_pkt_t *pkt)
{
size_t length;
if (pkt->pos + 1 > pkt->end) {
return (size_t)-1;
}
length = (size_t)*pkt->pos++;
if (length & 0x0080) {
if (pkt->pos + 1 > pkt->end) {
return (size_t)-1;
}
length &= 0x007F;
length |= (size_t)*pkt->pos++ << 7;
}
return length;
}
uint8_t *hdhomerun_pkt_read_tlv(struct hdhomerun_pkt_t *pkt, uint8_t *ptag, size_t *plength)
{
if (pkt->pos + 2 > pkt->end) {
return NULL;
}
*ptag = hdhomerun_pkt_read_u8(pkt);
*plength = hdhomerun_pkt_read_var_length(pkt);
if (pkt->pos + *plength > pkt->end) {
return NULL;
}
return pkt->pos + *plength;
}
void hdhomerun_pkt_read_mem(struct hdhomerun_pkt_t *pkt, void *mem, size_t length)
{
memcpy(mem, pkt->pos, length);
pkt->pos += length;
}
void hdhomerun_pkt_write_u8(struct hdhomerun_pkt_t *pkt, uint8_t v)
{
*pkt->pos++ = v;
if (pkt->pos > pkt->end) {
pkt->end = pkt->pos;
}
}
void hdhomerun_pkt_write_u16(struct hdhomerun_pkt_t *pkt, uint16_t v)
{
*pkt->pos++ = (uint8_t)(v >> 8);
*pkt->pos++ = (uint8_t)(v >> 0);
if (pkt->pos > pkt->end) {
pkt->end = pkt->pos;
}
}
void hdhomerun_pkt_write_u32(struct hdhomerun_pkt_t *pkt, uint32_t v)
{
*pkt->pos++ = (uint8_t)(v >> 24);
*pkt->pos++ = (uint8_t)(v >> 16);
*pkt->pos++ = (uint8_t)(v >> 8);
*pkt->pos++ = (uint8_t)(v >> 0);
if (pkt->pos > pkt->end) {
pkt->end = pkt->pos;
}
}
void hdhomerun_pkt_write_var_length(struct hdhomerun_pkt_t *pkt, size_t v)
{
if (v <= 127) {
*pkt->pos++ = (uint8_t)v;
} else {
*pkt->pos++ = (uint8_t)(v | 0x80);
*pkt->pos++ = (uint8_t)(v >> 7);
}
if (pkt->pos > pkt->end) {
pkt->end = pkt->pos;
}
}
void hdhomerun_pkt_write_mem(struct hdhomerun_pkt_t *pkt, const void *mem, size_t length)
{
memcpy(pkt->pos, mem, length);
pkt->pos += length;
if (pkt->pos > pkt->end) {
pkt->end = pkt->pos;
}
}
int hdhomerun_pkt_open_frame(struct hdhomerun_pkt_t *pkt, uint16_t *ptype)
{
pkt->pos = pkt->start;
if (pkt->pos + 4 > pkt->end) {
return 0;
}
*ptype = hdhomerun_pkt_read_u16(pkt);
size_t length = hdhomerun_pkt_read_u16(pkt);
pkt->pos += length;
if (pkt->pos + 4 > pkt->end) {
pkt->pos = pkt->start;
return 0;
}
uint32_t calc_crc = hdhomerun_pkt_calc_crc(pkt->start, pkt->pos);
uint32_t packet_crc;
packet_crc = (uint32_t)*pkt->pos++ << 0;
packet_crc |= (uint32_t)*pkt->pos++ << 8;
packet_crc |= (uint32_t)*pkt->pos++ << 16;
packet_crc |= (uint32_t)*pkt->pos++ << 24;
if (calc_crc != packet_crc) {
return -1;
}
pkt->start += 4;
pkt->end = pkt->start + length;
pkt->pos = pkt->start;
return 1;
}
void hdhomerun_pkt_seal_frame(struct hdhomerun_pkt_t *pkt, uint16_t frame_type)
{
size_t length = pkt->end - pkt->start;
pkt->start -= 4;
pkt->pos = pkt->start;
hdhomerun_pkt_write_u16(pkt, frame_type);
hdhomerun_pkt_write_u16(pkt, (uint16_t)length);
uint32_t crc = hdhomerun_pkt_calc_crc(pkt->start, pkt->end);
*pkt->end++ = (uint8_t)(crc >> 0);
*pkt->end++ = (uint8_t)(crc >> 8);
*pkt->end++ = (uint8_t)(crc >> 16);
*pkt->end++ = (uint8_t)(crc >> 24);
pkt->pos = pkt->start;
}