/*
 * Copyright (C) 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef ANDROID_HARDWARE_AUTOMOTIVE_AUDIOCONTROL_V2_0_AUDIOCONTROL_H
#define ANDROID_HARDWARE_AUTOMOTIVE_AUDIOCONTROL_V2_0_AUDIOCONTROL_H

#include <android/hardware/audio/common/6.0/types.h>
#include <android/hardware/automotive/audiocontrol/2.0/IAudioControl.h>
#include <android/hardware/automotive/audiocontrol/2.0/ICloseHandle.h>
#include <hidl/MQDescriptor.h>
#include <hidl/Status.h>

using android::hardware::audio::common::V6_0::AudioUsage;

namespace android::hardware::automotive::audiocontrol::V2_0::implementation {

class AudioControl : public IAudioControl {
  public:
    // Methods from ::android::hardware::automotive::audiocontrol::V2_0::IAudioControl follow.
    Return<sp<ICloseHandle>> registerFocusListener(const sp<IFocusListener>& listener);
    Return<void> onAudioFocusChange(hidl_bitfield<AudioUsage> usage, int zoneId,
                                    hidl_bitfield<AudioFocusChange> focusChange);
    Return<void> setBalanceTowardRight(float value) override;
    Return<void> setFadeTowardFront(float value) override;
    Return<void> debug(const hidl_handle& fd, const hidl_vec<hidl_string>& options) override;

    // Implementation details
    AudioControl();

  private:
    sp<IFocusListener> mFocusListener;

    static bool checkArgumentsSize(int fd, const hidl_vec<hidl_string>& options, size_t minSize);
    static bool checkCallerHasWritePermissions(int fd);
    static bool isValidValue(float value);
    static bool safelyParseInt(std::string s, int* out);

    void cmdDump(int fd, const hidl_vec<hidl_string>& options);
    void cmdHelp(int fd) const;
    void cmdRequestFocus(int fd, const hidl_vec<hidl_string>& options);
    void cmdAbandonFocus(int fd, const hidl_vec<hidl_string>& options);
    void dump(int fd);
};

}  // namespace android::hardware::automotive::audiocontrol::V2_0::implementation

#endif  // ANDROID_HARDWARE_AUTOMOTIVE_AUDIOCONTROL_V2_0_AUDIOCONTROL_H