2 * Copyright (C) 2016 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #define LOG_TAG "BroadcastRadio"
17 //#define LOG_NDEBUG 0
21 #include <hardware/radio.h>
23 #include "BroadcastRadio.h"
29 namespace broadcastradio {
31 namespace implementation {
33 BroadcastRadio::BroadcastRadio(Class classId)
34 : mStatus(Result::NOT_INITIALIZED), mClassId(classId), mHwDevice(NULL)
38 BroadcastRadio::~BroadcastRadio()
40 if (mHwDevice != NULL) {
41 radio_hw_device_close(mHwDevice);
45 void BroadcastRadio::onFirstRef()
47 const hw_module_t *mod;
49 ALOGI("%s mClassId %d", __FUNCTION__, mClassId);
52 const char *classString = Utils::getClassString(mClassId);
53 if (classString == NULL) {
54 ALOGE("invalid class ID %d", mClassId);
55 mStatus = Result::INVALID_ARGUMENTS;
59 ALOGI("%s RADIO_HARDWARE_MODULE_ID %s %s",
60 __FUNCTION__, RADIO_HARDWARE_MODULE_ID, classString);
62 rc = hw_get_module_by_class(RADIO_HARDWARE_MODULE_ID, classString, &mod);
64 ALOGE("couldn't load radio module %s.%s (%s)",
65 RADIO_HARDWARE_MODULE_ID, classString, strerror(-rc));
66 mStatus = Result::INVALID_ARGUMENTS;
69 rc = radio_hw_device_open(mod, &mHwDevice);
71 ALOGE("couldn't open radio hw device in %s.%s (%s)",
72 RADIO_HARDWARE_MODULE_ID, "primary", strerror(-rc));
76 if (mHwDevice->common.version != RADIO_DEVICE_API_VERSION_CURRENT) {
77 ALOGE("wrong radio hw device version %04x", mHwDevice->common.version);
78 radio_hw_device_close(mHwDevice);
85 int BroadcastRadio::closeHalTuner(const struct radio_tuner *halTuner)
87 ALOGV("%s", __FUNCTION__);
88 if (mHwDevice == NULL) {
94 return mHwDevice->close_tuner(mHwDevice, halTuner);
98 // Methods from ::android::hardware::broadcastradio::V1_0::IBroadcastRadio follow.
99 Return<void> BroadcastRadio::getProperties(getProperties_cb _hidl_cb)
102 radio_hal_properties_t halProperties;
103 Properties properties;
105 if (mHwDevice == NULL) {
109 rc = mHwDevice->get_properties(mHwDevice, &halProperties);
111 Utils::convertPropertiesFromHal(&properties, &halProperties);
115 _hidl_cb(Utils::convertHalResult(rc), properties);
119 Return<void> BroadcastRadio::openTuner(const BandConfig& config, bool audio,
120 const sp<ITunerCallback>& callback, openTuner_cb _hidl_cb)
122 sp<Tuner> tunerImpl = new Tuner(callback, this);
124 radio_hal_band_config_t halConfig;
125 const struct radio_tuner *halTuner;
126 Utils::convertBandConfigToHal(&halConfig, &config);
127 int rc = mHwDevice->open_tuner(mHwDevice, &halConfig, audio,
128 Tuner::callback, tunerImpl.get(),
131 tunerImpl->setHalTuner(halTuner);
134 _hidl_cb(Utils::convertHalResult(rc), tunerImpl);
139 } // namespace implementation
141 } // namespace broadcastradio
142 } // namespace hardware
143 } // namespace android