switch to new MPL based Fraunhofer FOKUS Public License
[senf.git] / senf / Socket / Protocols / DVB / DVBSocketController.hh
1 // $Id$
2 //
3 // Copyright (C) 2007
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 //
6 // The contents of this file are subject to the Fraunhofer FOKUS Public License
7 // Version 1.0 (the "License"); you may not use this file except in compliance
8 // with the License. You may obtain a copy of the License at 
9 // http://senf.berlios.de/license.html
10 //
11 // The Fraunhofer FOKUS Public License Version 1.0 is based on, 
12 // but modifies the Mozilla Public License Version 1.1.
13 // See the full license text for the amendments.
14 //
15 // Software distributed under the License is distributed on an "AS IS" basis, 
16 // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
17 // for the specific language governing rights and limitations under the License.
18 //
19 // The Original Code is Fraunhofer FOKUS code.
20 //
21 // The Initial Developer of the Original Code is Fraunhofer-Gesellschaft e.V. 
22 // (registered association), Hansastraße 27 c, 80686 Munich, Germany.
23 // All Rights Reserved.
24 //
25 // Contributor(s):
26 //   Anton Gillert <atx@berlios.de>
27
28
29 #ifndef HH_SENF_Socket_Protocols_DVB_DVBSocketController_
30 #define HH_SENF_Socket_Protocols_DVB_DVBSocketController_ 1
31
32 // Custom includes
33 #include <string>
34 #include <linux/dvb/frontend.h>
35 #include "DVBFrontendHandle.hh"
36 #include "DVBDemuxHandles.hh"
37 #include "DVBConfigParser.hh"
38 #include <senf/Scheduler/FdEvent.hh>
39 #include <senf/Utils/Console/ScopedDirectory.hh>
40
41 ///////////////////////////////hh.p////////////////////////////////////////
42 namespace senf {
43
44 #define MPE_TABLEID 62
45
46 /** \brief Helperclass for configuration and controlling DVB devices.
47
48      The DVB API provides two methods for tuning. The first method is
49      synchronous, which means e.g. "tuneTo_sync" will return when the tuning operation on the card
50      succeed, no matter how long it takes. The second (preferred) method is to tune asynchronous.
51      The call "tuneTo" will return immediately and (if a callback was set) call the callback when
52      the tuning operation succeeds. <br>
53
54      Note that you may have to take draw backs in using this API. This dues to the implementation
55      of the DVB device driver which you or your kernel choose. It has shown that the completeness
56      of implementations vary from driver to driver. This wrapper provides the full Frontend API which
57      is defined by <a href="http://www.linuxtv.org/docs/dvbapi/DVB_Frontend_API.html">Convergence</a>.
58      You have to find out which parts of these functionality are implemented by your preferred
59      device driver by your own.
60 */
61 class DVBSocketController : boost::noncopyable
62 {
63 public:
64
65     console::ScopedDirectory<DVBSocketController> dir;
66
67     typedef boost::function<void (const struct dvb_frontend_event & )> Callback;
68                                         ///< Callback which is called when an asynchronous tuning succeeds.
69
70     DVBSocketController(DVBFrontendHandle frontendHandle_ = DVBFrontendHandle(0,0), const Callback & cb = NULL);
71     ~DVBSocketController();
72
73     DVBDemuxSectionHandle createDVBDemuxSectionHandle(  int adapternumber=0, int demuxnumber=0, bool addToConsole=false );
74     DVBDemuxPESHandle createDVBDemuxPESHandle(  int adapternumber=0, int demuxnumber=0, bool addToConsole=false );
75
76     void addToConsole(DVBDemuxSectionHandle sh);
77                                         ///< Adds an DVBDemuxSectionHandle to the console
78                                         /**< Allocates the functionality of DVBDemuxSectionProtocol
79                                              into the folder of the DVBSocketController. If the
80                                              protocol is closed, or all handles are discarded the
81                                              console support will automatically removed.
82                                              \param[in] sh handle of a protocol*/
83
84     void addToConsole(DVBDemuxPESHandle sh);
85                                         ///< Adds an DVBDemuxPESHandle to the console
86                                         /**< Allocates the functionality of DVBDemuxPESProtocol into
87                                              the folder of the DVBSocketController. If the protocol
88                                              is closed, or all handles are discarded the console
89                                              support will automatically removed.
90                                              \param[in] sh handle of a protocol*/
91
92     void tuneToCMD(std::string const & input, std::string const & mode = "async");
93                                         ///< Tunes a DVB device given by the type of the DVBFrontendHandle
94                                         /**< Tunes a DVB device by a channel name or complete
95                                              configuration line. This method was created for use
96                                              in the SENF-Console.
97                                              \param[in] input A channel name or a complete
98                                                configuration line. If a channel name is given
99                                                it would be searched in the config file.
100                                              \param[in] mode The mode in which it will tune
101                                                "sync" or "async"*/
102     void tuneTo(std::string const & channel);
103                                         ///< Tunes a DVB device to a channel
104                                         /**< Tunes a DVB device to a channel whose parameters
105                                              are stored in a config file. The method determines
106                                              the type of DVB device by it self.
107                                              \param[in] channel A channel name which will be
108                                                  looked up in config file.*/
109
110     void tuneDVB_S(unsigned int frequency, fe_spectral_inversion_t inversion, unsigned int symbole_rate, fe_code_rate_t code_rate);
111                                         ///< Tunes a DVB-S device
112                                         /**< Tunes a DVB-S device in asynchronous mode and calls
113                                              the callback if existing. Needs full configuration */
114
115     void tuneDVB_T(unsigned int frequency,
116                     fe_spectral_inversion_t inversion, fe_bandwidth_t bandwidth,
117                     fe_code_rate_t code_rate_HP, /* high priority stream code rate */
118                     fe_code_rate_t code_rate_LP, /* low priority stream code rate */
119                     fe_modulation_t constellation, /* modulation type (see above) */
120                     fe_transmit_mode_t transmission_mode,
121                     fe_guard_interval_t guard_interval,
122                     fe_hierarchy_t hierarchy_information);
123                                         ///< Tunes a DVB-T device
124                                         /**< Tunes a DVB-T device in asynchronous mode and calls
125                                              the callback if existing. Needs full configuration. */
126
127     void tuneDVB_C(unsigned int frequency,
128                     fe_spectral_inversion_t inversion, unsigned int symbol_rate,
129                     fe_code_rate_t fec_inner, fe_modulation_t modulation);
130                                         ///< Tunes a DVB-C device
131                                         /**< Tunes a DVB-C device in asynchronous mode and calls
132                                              the callback if existing. Needs full configuration. */
133
134     dvb_frontend_event tuneTo_sync(std::string const & channel );
135                                         ///< Tunes a DVB device given by the type of the DVBFrontendHandle
136                                         /**< Tunes a DVB device, given by the type of the
137                                              DVBFrontendHandle, by a channel name in synchronous mode
138                                              \param[in] channel A channel name which will be looked
139                                                  up in config file.
140                                              \returns dvb_frontend_event
141                                              \note The member "dvb_frontend_event.status" should be
142                                                correct by the most device driver implementations.
143                                                But "dvb_frontend_event.parameters" maybe not and is
144                                                definitely not set by: Cinergy T� (2.6.27),
145                                                Terratec Cinergy DT USB XS Diversity (2.6.27) */
146
147     dvb_frontend_event tuneDVB_S_sync(unsigned int frequency, fe_spectral_inversion_t inversion, unsigned int symbole_rate, fe_code_rate_t code_rate);
148                                         ///< Tunes a DVB-S device
149                                         /**< Tunes a DVB-S device in synchronous mode. Needs full configuration
150                                              \returns dvb_frontend_event
151                                              \note The member "dvb_frontend_event.status" should be
152                                                correct by the most device driver implementations.
153                                                But "dvb_frontend_event.parameters" maybe not and is
154                                                definitely not set by: Cinergy T� (2.6.27),
155                                                Terratec Cinergy DT USB XS Diversity (2.6.27) */
156
157     dvb_frontend_event tuneDVB_T_sync(unsigned int frequency,
158                 fe_spectral_inversion_t inversion, fe_bandwidth_t bandwidth,
159                 fe_code_rate_t code_rate_HP, /* high priority stream code rate */
160                 fe_code_rate_t code_rate_LP, /* low priority stream code rate */
161                 fe_modulation_t constellation, /* modulation type (see above) */
162                 fe_transmit_mode_t transmission_mode,
163                 fe_guard_interval_t guard_interval,
164                 fe_hierarchy_t hierarsourcechy_information);
165                                         ///< Tunes a DVB-T device
166                                         /**< Tunes a DVB-T device in synchronous mode. Needs full
167                                              configuration
168                                              \returns dvb_frontend_event
169                                              \note The member "dvb_frontend_event.status" should be
170                                                correct by the most device driver implementations.
171                                                But "dvb_frontend_event.parameters" maybe not and is
172                                                definitely not set by: Cinergy T� (2.6.27),
173                                                Terratec Cinergy DT USB XS Diversity (2.6.27) */
174
175     dvb_frontend_event tuneDVB_C_sync(unsigned int frequency,
176                 fe_spectral_inversion_t inversion, unsigned int symbol_rate,
177                 fe_code_rate_t fec_inner, fe_modulation_t modulation);
178                                         ///< Tunes a DVB-C device
179                                         /**< Tunes a DVB-C device in synchronous mode. Needs full configuration
180                                              \returns dvb_frontend_event
181                                              \note The member "dvb_frontend_event.status" should be
182                                                correct by the most device driver implementations.
183                                                But "dvb_frontend_event.parameters" maybe not and is
184                                                definitely not set by: Cinergy T� (2.6.27),
185                                                Terratec Cinergy DT USB XS Diversity (2.6.27) */
186
187     fe_type_t getType();                ///< Returns the type of the card. The type is defined in frontend.h
188
189     std::string getTypeString();        ///< Returns the type of the card.
190                                         /**< Returns the type of the card but human readable e.g.
191                                              "DVB-S", "DVB-T" or "DVB-C"*/
192
193     std::string status2String(fe_status_t status);
194                                         ///< Returns a human readable status information
195                                         /**< Returns a human readable version of fe_status_t
196                                              (defined by frontend.h)*/
197
198     unsigned int bitErrorRate();        ///< Returns the bit error rate
199                                         /**< \note This function may not be implemented by your
200                                              specific driver implementation. In this case the
201                                              output is random. This is true for:
202                                                Cinergy T� (2.6.27),
203                                                Terratec Cinergy DT USB XS Diversity (2.6.27) */
204
205     unsigned int signalToNoiseRatio();  ///< Returns the signal to noise ratio
206                                         /**< \note This function may not be implemented by your
207                                              specific driver implementation. In this case the
208                                              output is random. This is true for:
209                                                Cinergy T� (2.6.27),
210                                                Terratec Cinergy DT USB XS Diversity (2.6.27) */
211
212     unsigned int signalStrength();      ///< Returns the signal strength
213                                         /**< Returns the signal to noise ratio
214                                              \note This function may not be implemented by your
215                                              specific driver implementation. In this case the output is
216                                              random.*/
217
218     std::string getTuneInfo(const std::string & conf ="Ssbuf");
219                                         ///< Returns a string which shows actual tuning status
220                                         /**< <br>"S" prints signal strength (in hex)
221                                              <br>"s" prints signal to noise ration (in hex)
222                                              <br>"b" prints bit error rate (in hex)
223                                              <br>"u" prints uncorrected blocks (in hex)
224                                              <br>"f" prints readable overall status e.g. "Has Lock"<br>
225                                              These characters can be used to form the output. Be
226                                              aware, some features may not be supported be your
227                                              current driver implementation and could end in throwing
228                                              an exception! */
229 private:
230     DVBFrontendHandle frontendHandle;
231     fe_type_t type;
232     DVBConfigParser parser;
233     Callback cb;
234     static unsigned int controllerNr;
235     unsigned int sectionNr;
236     unsigned int pesNr;
237     scheduler::FdEvent event;
238     void readEvent(int i);
239     void initConsole();
240 };
241
242 }
243
244 ///////////////////////////////hh.e////////////////////////////////////////
245 //#include "DVBSocketController.cci"
246 //#include "DVBSocketController.ct"
247 //#include "DVBSocketController.cti"
248 #endif