root/branches/libffado-2.0/src/libcontrol/ClockSelect.cpp

Revision 1295, 6.5 kB (checked in by ppalmers, 13 years ago)

add range checks for libcontrol indexes. add support for signals w/o arguments

Line 
1 /*
2  * Copyright (C) 2005-2008 by Pieter Palmers
3  *
4  * This file is part of FFADO
5  * FFADO = Free Firewire (pro-)audio drivers for linux
6  *
7  * FFADO is based upon FreeBoB
8  *
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 2 of the License, or
12  * (at your option) version 3 of the License.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  *
22  */
23
24 #include "ClockSelect.h"
25 #include "ffadodevice.h"
26
27 namespace Control {
28
29 //// --- ClockSelect --- ////
30 ClockSelect::ClockSelect(FFADODevice &d)
31 : AttributeEnum(&d)
32 , m_Device( d )
33 {
34     setName("ClockSelect");
35     setLabel("Clock Source");
36     setDescription("Select the device clock source");
37 }
38
39 bool
40 ClockSelect::select(int idx)
41 {
42     debugOutput(DEBUG_LEVEL_VERBOSE, "Selecting clock idx: %d\n", idx);
43     FFADODevice::ClockSourceVector v = m_Device.getSupportedClockSources();
44     if(idx >= (int)v.size()) {
45         debugError("index out of range\n");
46         return false;
47     }
48     if(idx < 0) {
49         debugError("index < 0\n");
50         return false;
51     }
52     if(!m_Device.setActiveClockSource(v.at(idx))) {
53         debugWarning("could not set active clocksource\n");
54         return false;
55     }
56     debugOutput(DEBUG_LEVEL_VERBOSE, " clock id: %d\n", v.at(idx).id);
57     return true;
58 }
59
60 int
61 ClockSelect::selected()
62 {
63     debugOutput(DEBUG_LEVEL_VERBOSE, "Finding active clock\n");
64     FFADODevice::ClockSourceVector v = m_Device.getSupportedClockSources();
65     FFADODevice::ClockSource active = m_Device.getActiveClockSource();
66     int i=0;
67     for (i=0; i < (int)v.size(); i++) {
68         FFADODevice::ClockSource c = v.at(i);
69         if(c.id == active.id) {
70             debugOutput(DEBUG_LEVEL_VERBOSE, " Active clock at %d, id %d\n", i, c.id);
71             return i;
72         }
73     }
74     debugError("No active clock source found!\n");
75     return -1;
76 }
77
78 int
79 ClockSelect::count()
80 {
81     return m_Device.getSupportedClockSources().size();
82 }
83
84 std::string
85 ClockSelect::getEnumLabel(int idx)
86 {
87     FFADODevice::ClockSourceVector v = m_Device.getSupportedClockSources();
88     if(idx >= (int)v.size()) {
89         debugError("index out of range\n");
90         return "Error";
91     }
92     if(idx < 0) {
93         debugError("index < 0\n");
94         return "Error";
95     }
96     return v.at(idx).description;
97 }
98
99 int
100 ClockSelect::attributeCount()
101 {
102 /*
103         /// indicates the type of the clock source (e.g. eCT_ADAT)
104         enum eClockSourceType type;
105         /// indicated the id of the clock source (e.g. id=1 => clocksource is ADAT_1)
106         unsigned int id;
107         /// is the clock source valid (i.e. can be selected) at this moment?
108         bool valid;
109         /// is the clock source active at this moment?
110         bool active;
111         /// is the clock source locked?
112         bool locked;
113         /// is the clock source slipping?
114         bool slipping;
115         /// description of the clock struct (optional)
116         std::string description;
117 */
118     return 7;
119 }
120
121 std::string
122 ClockSelect::getAttributeValue(int attridx)
123 {
124     char tmp[16];
125     std::string retval = "bad attr index";
126     FFADODevice::ClockSource active = m_Device.getActiveClockSource();
127    
128     switch(attridx) {
129         case 0:
130             retval = FFADODevice::ClockSourceTypeToString(active.type);
131             break;
132         case 1:
133             snprintf(tmp, 16, "%u", active.id);
134             retval = tmp;
135             break;
136         case 2:
137             snprintf(tmp, 16, "%u", active.valid);
138             retval = tmp;
139             break;
140         case 3:
141             snprintf(tmp, 16, "%u", active.active);
142             retval = tmp;
143             break;
144         case 4:
145             snprintf(tmp, 16, "%u", active.locked);
146             retval = tmp;
147             break;
148         case 5:
149             snprintf(tmp, 16, "%u", active.slipping);
150             retval = tmp;
151             break;
152         case 6:
153             retval = active.description;
154     }
155     return retval;
156 }
157
158 std::string
159 ClockSelect::getAttributeName(int attridx)
160 {
161     switch(attridx) {
162         case 0: return "type";
163         case 1: return "id";
164         case 2: return "valid";
165         case 3: return "active";
166         case 4: return "locked";
167         case 5: return "slipping";
168         case 6: return "description";
169         default: return "bad attr index";
170     }
171 }
172
173 void
174 ClockSelect::show()
175 {
176     debugOutput( DEBUG_LEVEL_NORMAL, "ClockSelect Element %s, active: %s\n",
177         getName().c_str(), m_Device.getActiveClockSource().description.c_str());
178 }
179
180 // --- samplerate selection ---
181
182 SamplerateSelect::SamplerateSelect(FFADODevice &d)
183 : Enum(&d)
184 , m_Device( d )
185 {
186     setName("SamplerateSelect");
187     setLabel("Samplerate Select");
188     setDescription("Select the device sample rate");
189 }
190
191 bool
192 SamplerateSelect::select(int idx)
193 {
194     std::vector<int> freqs = m_Device.getSupportedSamplingFrequencies();
195     if (idx >= 0 && idx < (int)freqs.size()) {
196         if(!m_Device.setSamplingFrequency(freqs.at(idx))) {
197             debugWarning("Could not select samplerate\n");
198             return false;
199         }
200         return true;
201     } else {
202         debugWarning("bad index specified\n");
203         return false;
204     }
205 }
206
207 int
208 SamplerateSelect::selected()
209 {
210     std::vector<int> freqs = m_Device.getSupportedSamplingFrequencies();
211     int samplerate = m_Device.getSamplingFrequency();
212     for (int i = 0; i < (int)freqs.size(); i++) {
213         if (samplerate == freqs.at(i)) {
214             return i;
215         }
216     }
217     debugError("could not find the selected samplerate\n");
218     return -1;
219 }
220
221 int
222 SamplerateSelect::count()
223 {
224     return  m_Device.getSupportedSamplingFrequencies().size();
225 }
226
227 std::string
228 SamplerateSelect::getEnumLabel(int idx)
229 {
230     char tmp[16];
231     std::string retval = "Error";
232     std::vector<int> freqs = m_Device.getSupportedSamplingFrequencies();
233     if (idx >= 0 && idx < (int)freqs.size()) {
234         snprintf(tmp, 16, "%u", freqs.at(idx));
235         retval = tmp;
236     } else {
237         debugWarning("bad index specified\n");
238     }
239     return retval;
240 }
241
242 void
243 SamplerateSelect::show()
244 {
245     debugOutput( DEBUG_LEVEL_NORMAL, "SamplerateSelect Element %s, current: %d\n",
246         getName().c_str(), m_Device.getSamplingFrequency());
247
248 }
249
250
251 } // namespace Control
Note: See TracBrowser for help on using the browser.