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#include "VolumeProfile.h"
18#include "PolicyMappingKeys.h"
19#include "PolicySubsystem.h"
20#include "ParameterBlockType.h"
21#include <Volume.h>
22#include <math.h>
23
24using std::string;
25
26VolumeProfile::VolumeProfile(const string &mappingValue,
27                             CInstanceConfigurableElement *instanceConfigurableElement,
28                             const CMappingContext &context)
29    : CFormattedSubsystemObject(instanceConfigurableElement,
30                                mappingValue,
31                                MappingKeyAmend1,
32                                (MappingKeyAmendEnd - MappingKeyAmend1 + 1),
33                                context),
34      mPolicySubsystem(static_cast<const PolicySubsystem *>(
35                           instanceConfigurableElement->getBelongingSubsystem())),
36      mPolicyPluginInterface(mPolicySubsystem->getPolicyPluginInterface())
37{
38    uint32_t categoryKey = context.getItemAsInteger(MappingKeyCategory);
39    if (categoryKey >= Volume::DEVICE_CATEGORY_CNT) {
40        mCategory = Volume::DEVICE_CATEGORY_SPEAKER;
41    } else {
42        mCategory = static_cast<Volume::device_category>(categoryKey);
43    }
44    mId = static_cast<audio_stream_type_t>(context.getItemAsInteger(MappingKeyIdentifier));
45
46    // (no exception support, defer the error)
47    if (instanceConfigurableElement->getType() != CInstanceConfigurableElement::EParameterBlock) {
48        return;
49    }
50    // Get actual element type
51    const CParameterBlockType *parameterType = static_cast<const CParameterBlockType *>(
52                instanceConfigurableElement->getTypeElement());
53    mPoints = parameterType->getArrayLength();
54}
55
56bool VolumeProfile::receiveFromHW(string & /*error*/)
57{
58    return true;
59}
60
61bool VolumeProfile::sendToHW(string & /*error*/)
62{
63    Point points[mPoints];
64    blackboardRead(&points, sizeof(Point) * mPoints);
65
66    VolumeCurvePoints pointsVector;
67    for (size_t i = 0; i < mPoints; i++) {
68        VolumeCurvePoint curvePoint;
69        curvePoint.mIndex = points[i].index;
70        curvePoint.mDBAttenuation = static_cast<float>(points[i].dbAttenuation) /
71                (1UL << gFractional);
72        pointsVector.push_back(curvePoint);
73    }
74    return mPolicyPluginInterface->setVolumeProfileForStream(mId, mCategory, pointsVector);
75}
76