forked from patrikf/dvbdescramble
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ca-device.c
227 lines (195 loc) · 5.47 KB
/
ca-device.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
/*
* (C) 2008-2009 Patrik Fimml.
*
* This file is part of dvbdescramble.
*
* dvbdescramble is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "ca-device.h"
#include "misc.h"
#include "marshal.h"
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <linux/dvb/ca.h>
enum
{
PROP_DEVICE = 1,
N_PROPS
};
enum
{
SIGNAL_RECEIVED_TPDU = 0,
N_SIGNALS
};
static guint signals[N_SIGNALS];
static const gchar *
decode_tpdu_tag(int x)
{
const char *t[] = {
"T_SB",
"T_RCV",
"T_create_t_c",
"T_c_t_c_reply",
"T_delete_t_c",
"T_d_t_c_reply",
"T_request_t_c",
"T_new_t_c",
"T_t_c_error",
};
if (x < 0x80 || x > 0x88)
return "(unknown)";
return t[x-0x80];
}
static gboolean
dispatch(GIOChannel *ioc,
GIOCondition cond,
gpointer _data)
{
CADevice *ca = _data;
guint8 buf[512];
int r = read(ca->fd, buf, 512);
/* FIXME: implement dynamic buffering */
g_assert(r < 512);
if (r <= 0)
g_error("read: %s", g_strerror(errno));
#if DEBUG
g_debug("read %d bytes", r);
#endif
guint8 *pos = buf+2;
guint8 *end = buf+r;
while (pos < end)
{
#if DEBUG
g_debug("%d bytes remain", end-pos);
#endif
/* Note that the actual TPDU and the status part of the R_TPDU are
* treated like two separate TPDUs.
*/
guint len;
guint8 *data;
decode_length_field(pos+1, &len, &data);
gboolean r;
g_signal_emit(
ca,
signals[SIGNAL_RECEIVED_TPDU],
0,
buf[0], /* slot */
buf[1], /* channel */
pos[0], /* tag */
data,
len,
&r
);
#if DEBUG
g_debug("TPDU received:");
g_debug(" slot: 0x%02X", buf[0]);
g_debug(" channel: 0x%02X", buf[1]);
g_debug(" tag: 0x%02X (%s)", pos[0], decode_tpdu_tag(pos[0]));
for (guint8 *cur = pos+1; cur < data; cur++)
g_debug(" length_field: 0x%02X", *cur);
for (guint i = 0; i < len; i++)
g_debug(" data[%2d] 0x%02X", i, data[i]);
#endif
pos = data + len;
}
g_assert(pos == end);
return TRUE;
}
static void
set_property(CADevice *ca,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
if (prop_id == PROP_DEVICE)
{
const gchar *device = g_value_get_string(value);
ca->fd = open(device, O_RDWR);
if (ca->fd == -1)
{
g_error("unable to open %s: %s", device, g_strerror(errno));
}
GIOChannel *ioc = g_io_channel_unix_new(ca->fd);
ca->fd_watch = g_io_add_watch(ioc, G_IO_IN, dispatch, ca);
g_io_channel_unref(ioc);
}
}
static void
class_init(CADeviceClass *klass)
{
GObjectClass *gobj_class = (GObjectClass *)klass;
gobj_class->set_property = (GObjectSetPropertyFunc)set_property;
g_object_class_install_property(
gobj_class,
PROP_DEVICE,
g_param_spec_string(
"device",
"device",
"The device file to open (usually /dev/dvb/adapterX/caY)",
"/dev/dvb/adapter0/ca0",
G_PARAM_STATIC_NAME | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY
)
);
signals[SIGNAL_RECEIVED_TPDU] = g_signal_new(
"received-tpdu",
CA_TYPE_DEVICE,
G_SIGNAL_RUN_LAST,
0,
g_signal_accumulator_true_handled,
NULL,
g_cclosure_user_marshal_BOOLEAN__UCHAR_UCHAR_UCHAR_POINTER_UINT,
G_TYPE_BOOLEAN,
5,
G_TYPE_UCHAR, G_TYPE_UCHAR, G_TYPE_UCHAR, G_TYPE_POINTER, G_TYPE_UINT
);
}
GType
ca_device_get_type()
{
static GType type = 0;
if (type == 0)
type = g_type_register_static_simple(
G_TYPE_OBJECT, /* parent_type */
"CADeviceType", /* type_name */
sizeof(CADeviceClass), /* class_size */
(GClassInitFunc)class_init, /* class_init */
sizeof(CADevice), /* instance_size */
NULL, /* instance_init */
0 /* flags */
);
return type;
}
CADevice *
ca_open_device(gchar *device)
{
return g_object_new(CA_TYPE_DEVICE, "device", device, NULL);
}
void
ca_device_reset(CADevice *ca)
{
ioctl(ca->fd, CA_RESET);
/* This is not necessary. */
#if 0
/* clear any messages that arrived so far */
int flags = fcntl(ca->fd, F_GETFL);
fcntl(ca->fd, F_SETFL, flags | O_NONBLOCK);
char buf[512];
while (read(ca->fd, buf, 512) > 0);
if (errno != EAGAIN)
g_error("error clearing buffer of CA device: %s", g_strerror(errno));
fcntl(ca->fd, F_SETFL, flags);
#endif
}