-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhdhomerun_channels.c
269 lines (224 loc) · 7.91 KB
/
hdhomerun_channels.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/*
* hdhomerun_channelscan.c
*
* Copyright © 2007 Silicondust Engineering Ltd. <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 3 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, see <http://www.gnu.org/licenses/>.
*/
#include "hdhomerun_os.h"
#include "hdhomerun_pkt.h"
#include "hdhomerun_debug.h"
#include "hdhomerun_control.h"
#include "hdhomerun_device.h"
#include "hdhomerun_channels.h"
#define FREQUENCY_RESOLUTION 62500
struct hdhomerun_channel_entry_t {
struct hdhomerun_channel_entry_t *next;
uint32_t channel_map;
uint32_t frequency;
uint8_t channel_number;
char name[16];
};
struct hdhomerun_channel_map_range_t {
uint32_t channel_map;
uint8_t channel_range_start;
uint8_t channel_range_end;
uint32_t frequency;
uint32_t spacing;
};
static const struct hdhomerun_channel_map_range_t hdhomerun_channels_map_ranges[] = {
{CHANNEL_MAP_US_BCAST, 2, 4, 57000000, 6000000},
{CHANNEL_MAP_US_BCAST, 5, 6, 79000000, 6000000},
{CHANNEL_MAP_US_BCAST, 7, 13, 177000000, 6000000},
{CHANNEL_MAP_US_BCAST, 14, 69, 473000000, 6000000},
{CHANNEL_MAP_US_CABLE, 1, 1, 75000000, 6000000},
{CHANNEL_MAP_US_CABLE, 2, 4, 57000000, 6000000},
{CHANNEL_MAP_US_CABLE, 5, 6, 79000000, 6000000},
{CHANNEL_MAP_US_CABLE, 7, 13, 177000000, 6000000},
{CHANNEL_MAP_US_CABLE, 14, 22, 123000000, 6000000},
{CHANNEL_MAP_US_CABLE, 23, 94, 219000000, 6000000},
{CHANNEL_MAP_US_CABLE, 95, 99, 93000000, 6000000},
{CHANNEL_MAP_US_CABLE, 100, 136, 651000000, 6000000},
{CHANNEL_MAP_US_HRC, 1, 1, 73753600, 6000300},
{CHANNEL_MAP_US_HRC, 2, 4, 55752700, 6000300},
{CHANNEL_MAP_US_HRC, 5, 6, 79753900, 6000300},
{CHANNEL_MAP_US_HRC, 7, 13, 175758700, 6000300},
{CHANNEL_MAP_US_HRC, 14, 22, 121756000, 6000300},
{CHANNEL_MAP_US_HRC, 23, 94, 217760800, 6000300},
{CHANNEL_MAP_US_HRC, 95, 99, 91754500, 6000300},
{CHANNEL_MAP_US_HRC, 100, 136, 649782400, 6000300},
{CHANNEL_MAP_US_IRC, 1, 1, 75012500, 6000000},
{CHANNEL_MAP_US_IRC, 2, 4, 57012500, 6000000},
{CHANNEL_MAP_US_IRC, 5, 6, 81012500, 6000000},
{CHANNEL_MAP_US_IRC, 7, 13, 177012500, 6000000},
{CHANNEL_MAP_US_IRC, 14, 22, 123012500, 6000000},
{CHANNEL_MAP_US_IRC, 23, 41, 219012500, 6000000},
{CHANNEL_MAP_US_IRC, 42, 42, 333025000, 6000000},
{CHANNEL_MAP_US_IRC, 43, 94, 339012500, 6000000},
{CHANNEL_MAP_US_IRC, 95, 97, 93012500, 6000000},
{CHANNEL_MAP_US_IRC, 98, 99, 111025000, 6000000},
{CHANNEL_MAP_US_IRC, 100, 136, 651012500, 6000000},
{CHANNEL_MAP_UK_BCAST, 21, 68, 474000000, 8000000},
{CHANNEL_MAP_NZ_BCAST, 25, 62, 506000000, 8000000},
{0, 0, 0, 0, 0}
};
static struct hdhomerun_channel_entry_t *hdhomerun_channel_list = NULL;
uint32_t hdhomerun_channel_entry_channel_map(struct hdhomerun_channel_entry_t *entry)
{
return entry->channel_map;
}
uint8_t hdhomerun_channel_entry_channel_number(struct hdhomerun_channel_entry_t *entry)
{
return entry->channel_number;
}
uint32_t hdhomerun_channel_entry_frequency(struct hdhomerun_channel_entry_t *entry)
{
return entry->frequency;
}
const char *hdhomerun_channel_entry_name(struct hdhomerun_channel_entry_t *entry)
{
return entry->name;
}
static const char *hdhomerun_channel_map_name(uint32_t channel_map)
{
switch (channel_map) {
case CHANNEL_MAP_US_BCAST:
return "us-bcast";
case CHANNEL_MAP_US_CABLE:
return "us-cable";
case CHANNEL_MAP_US_HRC:
return "us-hrc";
case CHANNEL_MAP_US_IRC:
return "us-irc";
case CHANNEL_MAP_UK_BCAST:
return "uk-bcast";
case CHANNEL_MAP_NZ_BCAST:
return "nz-bcast";
default:
return "unknown";
}
}
static void hdhomerun_channel_list_build_insert(struct hdhomerun_channel_entry_t *entry)
{
struct hdhomerun_channel_entry_t **pprev = &hdhomerun_channel_list;
struct hdhomerun_channel_entry_t *p = hdhomerun_channel_list;
while (p) {
if (p->frequency > entry->frequency) {
break;
}
pprev = &p->next;
p = p->next;
}
entry->next = p;
*pprev = entry;
}
static void hdhomerun_channel_list_build_range(const struct hdhomerun_channel_map_range_t *range)
{
uint8_t channel_number;
for (channel_number = range->channel_range_start; channel_number <= range->channel_range_end; channel_number++) {
struct hdhomerun_channel_entry_t *entry = (struct hdhomerun_channel_entry_t *)calloc(1, sizeof(struct hdhomerun_channel_entry_t));
if (!entry) {
return;
}
entry->channel_map = range->channel_map;
entry->channel_number = channel_number;
entry->frequency = range->frequency + ((uint32_t)(channel_number - range->channel_range_start) * range->spacing);
entry->frequency = (entry->frequency / FREQUENCY_RESOLUTION) * FREQUENCY_RESOLUTION;
sprintf(entry->name, "%s:%u", hdhomerun_channel_map_name(entry->channel_map), entry->channel_number);
hdhomerun_channel_list_build_insert(entry);
}
}
static void hdhomerun_channel_list_build(void)
{
const struct hdhomerun_channel_map_range_t *range = hdhomerun_channels_map_ranges;
while (range->channel_map) {
hdhomerun_channel_list_build_range(range);
range++;
}
}
struct hdhomerun_channel_entry_t *hdhomerun_channel_list_first(uint32_t channel_map)
{
if (!hdhomerun_channel_list) {
hdhomerun_channel_list_build();
}
struct hdhomerun_channel_entry_t *entry = hdhomerun_channel_list;
while (entry) {
if (entry->channel_map & channel_map) {
return entry;
}
entry = entry->next;
}
return NULL;
}
struct hdhomerun_channel_entry_t *hdhomerun_channel_list_next(uint32_t channel_map, struct hdhomerun_channel_entry_t *entry)
{
entry = entry->next;
while (entry) {
if (entry->channel_map & channel_map) {
return entry;
}
entry = entry->next;
}
return NULL;
}
uint32_t hdhomerun_channel_list_total_count(uint32_t channel_map)
{
uint32_t count = 0;
struct hdhomerun_channel_entry_t *entry = hdhomerun_channel_list_first(channel_map);
while (entry) {
count++;
entry = hdhomerun_channel_list_next(channel_map, entry);
}
return count;
}
uint32_t hdhomerun_channel_list_frequency_count(uint32_t channel_map)
{
uint32_t count = 0;
uint32_t last_frequency = 0;
struct hdhomerun_channel_entry_t *entry = hdhomerun_channel_list_first(channel_map);
while (entry) {
if (entry->frequency != last_frequency) {
last_frequency = entry->frequency;
count++;
}
entry = hdhomerun_channel_list_next(channel_map, entry);
}
return count;
}
uint32_t hdhomerun_channel_number_to_frequency(uint32_t channel_map, uint8_t channel_number)
{
struct hdhomerun_channel_entry_t *entry = hdhomerun_channel_list_first(channel_map);
while (entry) {
if (entry->channel_number == channel_number) {
return entry->frequency;
}
entry = hdhomerun_channel_list_next(channel_map, entry);
}
return 0;
}
uint8_t hdhomerun_channel_frequency_to_number(uint32_t channel_map, uint32_t frequency)
{
frequency = (frequency / FREQUENCY_RESOLUTION) * FREQUENCY_RESOLUTION;
struct hdhomerun_channel_entry_t *entry = hdhomerun_channel_list_first(channel_map);
while (entry) {
if (entry->frequency == frequency) {
return entry->channel_number;
}
if (entry->frequency > frequency) {
return 0;
}
entry = hdhomerun_channel_list_next(channel_map, entry);
}
return 0;
}