1 /*
2  * Copyright (C) 2015 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include "AudioPolicyConfig.h"
20 #include <utils/StrongPointer.h>
21 #include <utils/Errors.h>
22 #include <utils/RefBase.h>
23 #include <string>
24 #include <sstream>
25 #include <fstream>
26 
27 struct _xmlNode;
28 struct _xmlDoc;
29 
30 namespace android {
31 
32 struct AudioGainTraits
33 {
34     static const char *const tag;
35     static const char *const collectionTag;
36 
37     struct Attributes
38     {
39         static const char mode[]; /**< gain modes supported, e.g. AUDIO_GAIN_MODE_CHANNELS. */
40         /** controlled channels, needed if mode AUDIO_GAIN_MODE_CHANNELS. */
41         static const char channelMask[];
42         static const char minValueMB[]; /**< min value in millibel. */
43         static const char maxValueMB[]; /**< max value in millibel. */
44         static const char defaultValueMB[]; /**< default value in millibel. */
45         static const char stepValueMB[]; /**< step value in millibel. */
46         static const char minRampMs[]; /**< needed if mode AUDIO_GAIN_MODE_RAMP. */
47         static const char maxRampMs[]; /**< .needed if mode AUDIO_GAIN_MODE_RAMP */
48     };
49 
50     typedef AudioGain Element;
51     typedef sp<Element> PtrElement;
52     typedef AudioGainCollection Collection;
53     typedef void *PtrSerializingCtx;
54 
55     static status_t deserialize(_xmlDoc *doc, const _xmlNode *root, PtrElement &element,
56                                 PtrSerializingCtx serializingContext);
57 
58     // Gain has no child
59 };
60 
61 // A profile section contains a name,  one audio format and the list of supported sampling rates
62 // and channel masks for this format
63 struct AudioProfileTraits
64 {
65     static const char *const tag;
66     static const char *const collectionTag;
67 
68     struct Attributes
69     {
70         static const char name[];
71         static const char samplingRates[];
72         static const char format[];
73         static const char channelMasks[];
74     };
75 
76     typedef AudioProfile Element;
77     typedef sp<AudioProfile> PtrElement;
78     typedef AudioProfileVector Collection;
79     typedef void *PtrSerializingCtx;
80 
81     static status_t deserialize(_xmlDoc *doc, const _xmlNode *root, PtrElement &element,
82                                 PtrSerializingCtx serializingContext);
83 };
84 
85 struct MixPortTraits
86 {
87     static const char *const tag;
88     static const char *const collectionTag;
89 
90     struct Attributes
91     {
92         static const char name[];
93         static const char role[];
94         static const char flags[];
95         static const char maxOpenCount[];
96         static const char maxActiveCount[];
97     };
98 
99     typedef IOProfile Element;
100     typedef sp<Element> PtrElement;
101     typedef IOProfileCollection Collection;
102     typedef void *PtrSerializingCtx;
103 
104     static status_t deserialize(_xmlDoc *doc, const _xmlNode *root, PtrElement &element,
105                                 PtrSerializingCtx serializingContext);
106 
107     // Children are: GainTraits
108 };
109 
110 struct DevicePortTraits
111 {
112     static const char *const tag;
113     static const char *const collectionTag;
114 
115     struct Attributes
116     {
117         static const char tagName[]; /**<  <device tag name>: any string without space. */
118         static const char type[]; /**< <device type>. */
119         static const char role[]; /**< <device role: sink or source>. */
120         static const char roleSource[]; /**< <attribute role source value>. */
121         static const char address[]; /**< optional: device address, char string less than 64. */
122     };
123     typedef DeviceDescriptor Element;
124     typedef sp<DeviceDescriptor> PtrElement;
125     typedef DeviceVector Collection;
126     typedef void *PtrSerializingCtx;
127 
128     static status_t deserialize(_xmlDoc *doc, const _xmlNode *root, PtrElement &element,
129                                 PtrSerializingCtx serializingContext);
130     // Children are: GainTraits (optionnal)
131 };
132 
133 struct RouteTraits
134 {
135     static const char *const tag;
136     static const char *const collectionTag;
137 
138     struct Attributes
139     {
140         static const char type[]; /**< <route type>: mix or mux. */
141         static const char typeMix[]; /**< type attribute mix value. */
142         static const char sink[]; /**< <sink: involved in this route>. */
143         static const char sources[]; /**< sources: all source that can be involved in this route. */
144     };
145     typedef AudioRoute Element;
146     typedef sp<AudioRoute> PtrElement;
147     typedef AudioRouteVector Collection;
148     typedef HwModule *PtrSerializingCtx;
149 
150     static status_t deserialize(_xmlDoc *doc, const _xmlNode *root, PtrElement &element,
151                                 PtrSerializingCtx ctx);
152 };
153 
154 struct ModuleTraits
155 {
156     static const char *const tag;
157     static const char *const collectionTag;
158 
159     static const char *const childAttachedDevicesTag;
160     static const char *const childAttachedDeviceTag;
161     static const char *const childDefaultOutputDeviceTag;
162 
163     struct Attributes
164     {
165         static const char name[];
166         static const char version[];
167     };
168 
169     typedef HwModule Element;
170     typedef sp<Element> PtrElement;
171     typedef HwModuleCollection Collection;
172     typedef AudioPolicyConfig *PtrSerializingCtx;
173 
174     static status_t deserialize(_xmlDoc *doc, const _xmlNode *root, PtrElement &element,
175                                 PtrSerializingCtx serializingContext);
176 
177     // Children are: mixPortTraits, devicePortTraits and routeTraits
178     // Need to call deserialize on each child
179 };
180 
181 struct GlobalConfigTraits
182 {
183     static const char *const tag;
184 
185     struct Attributes
186     {
187         static const char speakerDrcEnabled[];
188     };
189 
190     static status_t deserialize(const _xmlNode *root, AudioPolicyConfig &config);
191 };
192 
193 struct VolumeTraits
194 {
195     static const char *const tag;
196     static const char *const collectionTag;
197     static const char *const volumePointTag;
198 
199     struct Attributes
200     {
201         static const char stream[];
202         static const char deviceCategory[];
203         static const char reference[];
204     };
205 
206     typedef VolumeCurve Element;
207     typedef sp<VolumeCurve> PtrElement;
208     typedef VolumeCurvesCollection Collection;
209     typedef void *PtrSerializingCtx;
210 
211     static status_t deserialize(_xmlDoc *doc, const _xmlNode *root, PtrElement &element,
212                                 PtrSerializingCtx serializingContext);
213 
214     // No Child
215 };
216 
217 class PolicySerializer
218 {
219 private:
220     static const char *const rootName;
221 
222     static const char *const versionAttribute;
223     static const uint32_t gMajor; /**< the major number of the policy xml format version. */
224     static const uint32_t gMinor; /**< the minor number of the policy xml format version. */
225 
226 public:
227     PolicySerializer();
228     status_t deserialize(const char *str, AudioPolicyConfig &config);
229 
230 private:
231     typedef AudioPolicyConfig Element;
232 
233     std::string mRootElementName;
234     std::string mVersion;
235 
236     // Children are: ModulesTraits, VolumeTraits
237 };
238 
239 } // namespace android
240