~ubuntu-branches/ubuntu/karmic/me-tv/karmic

« back to all changes in this revision

Viewing changes to src/dvb_tuner.cc

  • Committer: Bazaar Package Importer
  • Author(s): Michael Lamothe
  • Date: 2007-12-19 23:30:16 UTC
  • Revision ID: james.westby@ubuntu.com-20071219233016-2ng2clfh00xtlevc
Tags: upstream-0.4.19
ImportĀ upstreamĀ versionĀ 0.4.19

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2007 Michael Lamothe
 
3
 *
 
4
 * This program is free software; you can redistribute it and/or modify
 
5
 * it under the terms of the GNU General Public License as published by
 
6
 * the Free Software Foundation; either version 2 of the License, or
 
7
 * (at your option) any later version.
 
8
 * 
 
9
 * This program is distributed in the hope that it will be useful,
 
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
 * GNU Library General Public License for more details.
 
13
 * 
 
14
 * You should have received a copy of the GNU General Public License
 
15
 * along with this program; if not, write to the Free Software
 
16
 * Foundation, Inc., 51 Franklin Street, Fifth Floor Boston, MA 02110-1301,  USA
 
17
 */
 
18
 
 
19
#include "dvb_tuner.h"
 
20
#include "log.h"
 
21
 
 
22
DvbTuner::DvbTuner(const Glib::ustring& tuner_device_path)
 
23
{
 
24
        fd = -1;
 
25
 
 
26
        if ( ( fd = open ( tuner_device_path.c_str(), O_RDWR | O_NONBLOCK ) ) < 0 )
 
27
        {
 
28
                throw SystemException("Failed to open tuner");
 
29
        }
 
30
        
 
31
        if ( ioctl ( fd, FE_GET_INFO, &frontend_info ) < 0 )
 
32
        {
 
33
                throw SystemException("Failed to get tuner info");
 
34
        }
 
35
}
 
36
 
 
37
DvbTuner::~DvbTuner()
 
38
{
 
39
        if (fd != -1)
 
40
        {
 
41
                close(fd);
 
42
        }
 
43
}
 
44
 
 
45
void DvbTuner::tune_to (Transponder& transponder)
 
46
{
 
47
        struct dvb_frontend_event ev;
 
48
        
 
49
        if (frontend_info.type == FE_QPSK)
 
50
        {
 
51
                diseqc(transponder);
 
52
        }
 
53
 
 
54
        // Discard stale events
 
55
        while (ioctl(fd, FE_GET_EVENT, &ev) != -1);
 
56
 
 
57
        if ( ioctl ( fd, FE_SET_FRONTEND, &(transponder.frontend_parameters) ) < 0 )
 
58
        {
 
59
                throw SystemException("Failed to tune device");
 
60
        }
 
61
        
 
62
        Log::write("Waiting for signal lock ...");
 
63
        wait_lock();
 
64
        Log::write("Got signal lock");
 
65
}
 
66
 
 
67
void DvbTuner::diseqc(const Transponder& transponder)
 
68
{
 
69
        int satellite_number    = transponder.satellite_number;
 
70
        int polarisation                = transponder.polarisation;
 
71
        int hi_band                             = transponder.hi_band;
 
72
        
 
73
        struct dvb_diseqc_master_cmd cmd = { {0xe0, 0x10, 0x38, 0xf0, 0x00, 0x00}, 4};
 
74
        cmd.msg[3] = 0xf0 | (((satellite_number * 4) & 0x0f) | (hi_band ? 1 : 0) | (polarisation ? 0 : 2));
 
75
        
 
76
        fe_sec_voltage_t        voltage = polarisation ? SEC_VOLTAGE_13 : SEC_VOLTAGE_18;
 
77
        fe_sec_tone_mode_t      tone    = hi_band ? SEC_TONE_ON : SEC_TONE_OFF;
 
78
        fe_sec_mini_cmd_t       burst   = (satellite_number / 4) % 2 ? SEC_MINI_B : SEC_MINI_A;
 
79
 
 
80
        if (ioctl(fd, FE_SET_TONE, SEC_TONE_OFF) == -1)
 
81
        {
 
82
                throw SystemException("Failed to set tone off");
 
83
        }
 
84
        
 
85
        if (ioctl(fd, FE_SET_VOLTAGE, voltage) == -1)
 
86
        {
 
87
                throw SystemException("Failed to set voltage");
 
88
        }
 
89
 
 
90
        usleep(15 * 1000);
 
91
        if (ioctl(fd, FE_DISEQC_SEND_MASTER_CMD, &cmd) == -1)
 
92
        {
 
93
                throw SystemException("Failed to send master command");
 
94
        }
 
95
 
 
96
        usleep(15 * 1000);
 
97
        if (ioctl(fd, FE_DISEQC_SEND_BURST, burst) == -1)
 
98
        {
 
99
                throw SystemException("Failed to send burst");
 
100
        }
 
101
        
 
102
        usleep(15 * 1000);
 
103
        if (ioctl(fd, FE_SET_TONE, tone) == -1)
 
104
        {
 
105
                throw SystemException("Failed to set tone");
 
106
        }
 
107
}
 
108
 
 
109
void DvbTuner::wait_lock()
 
110
{
 
111
        fe_status_t     status;
 
112
        time_t          start_time = time(NULL);
 
113
 
 
114
        while ((start_time + 10) > time(NULL))
 
115
        {
 
116
                if (!ioctl(fd, FE_READ_STATUS, &status))
 
117
                {
 
118
                        if (status & FE_HAS_LOCK)
 
119
                        {
 
120
                                break;
 
121
                        }
 
122
                }
 
123
 
 
124
                usleep(100000);
 
125
        }
 
126
 
 
127
        if (!(status & FE_HAS_LOCK))
 
128
        {
 
129
                throw Exception("Failed to lock to channel");
 
130
        }
 
131
}