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
27struct _xmlNode;
28struct _xmlDoc;
29
30namespace android {
31
32struct 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
63struct 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
85struct 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    };
96
97    typedef IOProfile Element;
98    typedef sp<Element> PtrElement;
99    typedef IOProfileCollection Collection;
100    typedef void *PtrSerializingCtx;
101
102    static status_t deserialize(_xmlDoc *doc, const _xmlNode *root, PtrElement &element,
103                                PtrSerializingCtx serializingContext);
104
105    // Children are: GainTraits
106};
107
108struct DevicePortTraits
109{
110    static const char *const tag;
111    static const char *const collectionTag;
112
113    struct Attributes
114    {
115        static const char tagName[]; /**<  <device tag name>: any string without space. */
116        static const char type[]; /**< <device type>. */
117        static const char role[]; /**< <device role: sink or source>. */
118        static const char roleSource[]; /**< <attribute role source value>. */
119        static const char address[]; /**< optional: device address, char string less than 64. */
120    };
121    typedef DeviceDescriptor Element;
122    typedef sp<DeviceDescriptor> PtrElement;
123    typedef DeviceVector Collection;
124    typedef void *PtrSerializingCtx;
125
126    static status_t deserialize(_xmlDoc *doc, const _xmlNode *root, PtrElement &element,
127                                PtrSerializingCtx serializingContext);
128    // Children are: GainTraits (optionnal)
129};
130
131struct RouteTraits
132{
133    static const char *const tag;
134    static const char *const collectionTag;
135
136    struct Attributes
137    {
138        static const char type[]; /**< <route type>: mix or mux. */
139        static const char typeMix[]; /**< type attribute mix value. */
140        static const char sink[]; /**< <sink: involved in this route>. */
141        static const char sources[]; /**< sources: all source that can be involved in this route. */
142    };
143    typedef AudioRoute Element;
144    typedef sp<AudioRoute> PtrElement;
145    typedef AudioRouteVector Collection;
146    typedef HwModule *PtrSerializingCtx;
147
148    static status_t deserialize(_xmlDoc *doc, const _xmlNode *root, PtrElement &element,
149                                PtrSerializingCtx ctx);
150};
151
152struct ModuleTraits
153{
154    static const char *const tag;
155    static const char *const collectionTag;
156
157    static const char *const childAttachedDevicesTag;
158    static const char *const childAttachedDeviceTag;
159    static const char *const childDefaultOutputDeviceTag;
160
161    struct Attributes
162    {
163        static const char name[];
164        static const char version[];
165    };
166
167    typedef HwModule Element;
168    typedef sp<Element> PtrElement;
169    typedef HwModuleCollection Collection;
170    typedef AudioPolicyConfig *PtrSerializingCtx;
171
172    static status_t deserialize(_xmlDoc *doc, const _xmlNode *root, PtrElement &element,
173                                PtrSerializingCtx serializingContext);
174
175    // Children are: mixPortTraits, devicePortTraits and routeTraits
176    // Need to call deserialize on each child
177};
178
179struct GlobalConfigTraits
180{
181    static const char *const tag;
182
183    struct Attributes
184    {
185        static const char speakerDrcEnabled[];
186    };
187
188    static status_t deserialize(const _xmlNode *root, AudioPolicyConfig &config);
189};
190
191struct VolumeTraits
192{
193    static const char *const tag;
194    static const char *const collectionTag;
195    static const char *const volumePointTag;
196
197    struct Attributes
198    {
199        static const char stream[];
200        static const char deviceCategory[];
201        static const char reference[];
202    };
203
204    typedef VolumeCurve Element;
205    typedef sp<VolumeCurve> PtrElement;
206    typedef VolumeCurvesCollection Collection;
207    typedef void *PtrSerializingCtx;
208
209    static status_t deserialize(_xmlDoc *doc, const _xmlNode *root, PtrElement &element,
210                                PtrSerializingCtx serializingContext);
211
212    // No Child
213};
214
215class PolicySerializer
216{
217private:
218    static const char *const rootName;
219
220    static const char *const versionAttribute;
221    static const uint32_t gMajor; /**< the major number of the policy xml format version. */
222    static const uint32_t gMinor; /**< the minor number of the policy xml format version. */
223
224public:
225    PolicySerializer();
226    status_t deserialize(const char *str, AudioPolicyConfig &config);
227
228private:
229    typedef AudioPolicyConfig Element;
230
231    std::string mRootElementName;
232    std::string mVersion;
233
234    // Children are: ModulesTraits, VolumeTraits
235};
236
237}; // namespace android
238