~ubuntu-branches/ubuntu/precise/me-tv/precise-proposed

« back to all changes in this revision

Viewing changes to src/libdvbapi/dvbca.c

  • Committer: Bazaar Package Importer
  • Author(s): Philipp Kern
  • Date: 2008-07-23 14:03:56 UTC
  • mfrom: (1.1.3 upstream) (3.1.1 lenny)
  • Revision ID: james.westby@ubuntu.com-20080723140356-m6ze7fbkydes42c7
Tags: 0.5.33-3
Fix xine-lib ffmpeg dependency.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * libdvbca - interface onto raw CA devices
3
 
 *
4
 
 * Copyright (C) 2006 Andrew de Quincey (adq_dvb@lidskialf.net)
5
 
 *
6
 
 * This library is free software; you can redistribute it and/or
7
 
 * modify it under the terms of the GNU Lesser General Public
8
 
 * License as published by the Free Software Foundation; either
9
 
 * version 2.1 of the License, or (at your option) any later version.
10
 
 *
11
 
 * This library is distributed in the hope that it will be useful,
12
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
 
 * Lesser General Public License for more details.
15
 
 *
16
 
 * You should have received a copy of the GNU Lesser General Public
17
 
 * License along with this library; if not, write to the Free Software
18
 
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
19
 
 */
20
 
 
21
 
#include <stdlib.h>
22
 
#include <string.h>
23
 
#include <stdio.h>
24
 
#include <sys/param.h>
25
 
#include <sys/ioctl.h>
26
 
#include <fcntl.h>
27
 
#include <unistd.h>
28
 
#include <ctype.h>
29
 
#include <errno.h>
30
 
#include <linux/dvb/ca.h>
31
 
#include "dvbca.h"
32
 
 
33
 
 
34
 
int dvbca_open(int adapter, int cadevice)
35
 
{
36
 
        char filename[PATH_MAX+1];
37
 
        int fd;
38
 
 
39
 
        sprintf(filename, "/dev/dvb/adapter%i/ca%i", adapter, cadevice);
40
 
        if ((fd = open(filename, O_RDWR)) < 0) {
41
 
                // if that failed, try a flat /dev structure
42
 
                sprintf(filename, "/dev/dvb%i.ca%i", adapter, cadevice);
43
 
                fd = open(filename, O_RDWR);
44
 
        }
45
 
 
46
 
        return fd;
47
 
}
48
 
 
49
 
int dvbca_reset(int fd, uint8_t slot)
50
 
{
51
 
        return ioctl(fd, CA_RESET, (1 << slot));
52
 
}
53
 
 
54
 
int dvbca_get_interface_type(int fd, uint8_t slot)
55
 
{
56
 
        ca_slot_info_t info;
57
 
 
58
 
        info.num = slot;
59
 
        if (ioctl(fd, CA_GET_SLOT_INFO, &info))
60
 
                return -1;
61
 
 
62
 
        if (info.type & CA_CI_LINK)
63
 
                return DVBCA_INTERFACE_LINK;
64
 
        if (info.type & CA_CI)
65
 
                return DVBCA_INTERFACE_HLCI;
66
 
 
67
 
        return -1;
68
 
}
69
 
 
70
 
int dvbca_get_cam_state(int fd, uint8_t slot)
71
 
{
72
 
        ca_slot_info_t info;
73
 
 
74
 
        info.num = slot;
75
 
        if (ioctl(fd, CA_GET_SLOT_INFO, &info))
76
 
                return -1;
77
 
 
78
 
        if (info.flags == 0)
79
 
                return DVBCA_CAMSTATE_MISSING;
80
 
        if (info.flags & CA_CI_MODULE_READY)
81
 
                return DVBCA_CAMSTATE_READY;
82
 
        if (info.flags & CA_CI_MODULE_PRESENT)
83
 
                return DVBCA_CAMSTATE_INITIALISING;
84
 
 
85
 
        return -1;
86
 
}
87
 
 
88
 
int dvbca_link_write(int fd, uint8_t slot, uint8_t connection_id,
89
 
                     uint8_t *data, uint16_t data_length)
90
 
{
91
 
        uint8_t *buf = malloc(data_length + 2);
92
 
        if (buf == NULL)
93
 
                return -1;
94
 
 
95
 
        buf[0] = slot;
96
 
        buf[1] = connection_id;
97
 
        memcpy(buf+2, data, data_length);
98
 
 
99
 
        int result = write(fd, buf, data_length+2);
100
 
        free(buf);
101
 
        return result;
102
 
}
103
 
 
104
 
int dvbca_link_read(int fd, uint8_t *slot, uint8_t *connection_id,
105
 
                     uint8_t *data, uint16_t data_length)
106
 
{
107
 
        int size;
108
 
 
109
 
        uint8_t *buf = malloc(data_length + 2);
110
 
        if (buf == NULL)
111
 
                return -1;
112
 
 
113
 
        if ((size = read(fd, buf, data_length+2)) < 2)
114
 
                return -1;
115
 
 
116
 
        *slot = buf[0];
117
 
        *connection_id = buf[1];
118
 
        memcpy(data, buf+2, size-2);
119
 
        free(buf);
120
 
 
121
 
        return size - 2;
122
 
}
123
 
 
124
 
int dvbca_hlci_write(int fd, uint8_t *data, uint16_t data_length)
125
 
{
126
 
        struct ca_msg msg;
127
 
 
128
 
        if (data_length > 256) {
129
 
                return -1;
130
 
        }
131
 
        memset(&msg, 0, sizeof(msg));
132
 
        msg.length = data_length;
133
 
 
134
 
        memcpy(msg.msg, data, data_length);
135
 
 
136
 
        return ioctl(fd, CA_SEND_MSG, &msg);
137
 
}
138
 
 
139
 
int dvbca_hlci_read(int fd, uint32_t app_tag, uint8_t *data,
140
 
                    uint16_t data_length)
141
 
{
142
 
        struct ca_msg msg;
143
 
 
144
 
        if (data_length > 256) {
145
 
                data_length = 256;
146
 
        }
147
 
        memset(&msg, 0, sizeof(msg));
148
 
        msg.length = data_length;
149
 
        msg.msg[0] = app_tag >> 16;
150
 
        msg.msg[1] = app_tag >> 8;
151
 
        msg.msg[2] = app_tag;
152
 
 
153
 
        int status = ioctl(fd, CA_GET_MSG, &msg);
154
 
        if (status < 0) return status;
155
 
 
156
 
        if (msg.length > data_length) msg.length = data_length;
157
 
        memcpy(data, msg.msg, msg.length);
158
 
        return msg.length;
159
 
}