Eric Laurent | a174588 | 2016-11-21 10:41:22 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | #define LOG_TAG "BroadcastRadio" |
| 17 | //#define LOG_NDEBUG 0 |
| 18 | |
Mark Salyzyn | 3ff5260 | 2017-01-10 10:16:48 -0800 | [diff] [blame] | 19 | #include <log/log.h> |
| 20 | |
Eric Laurent | a174588 | 2016-11-21 10:41:22 -0800 | [diff] [blame] | 21 | #include <hardware/radio.h> |
| 22 | |
| 23 | #include "BroadcastRadio.h" |
| 24 | #include "Tuner.h" |
| 25 | #include "Utils.h" |
| 26 | |
| 27 | namespace android { |
| 28 | namespace hardware { |
| 29 | namespace broadcastradio { |
| 30 | namespace V1_0 { |
| 31 | namespace implementation { |
| 32 | |
| 33 | BroadcastRadio::BroadcastRadio(Class classId) |
| 34 | : mStatus(Result::NOT_INITIALIZED), mClassId(classId), mHwDevice(NULL) |
| 35 | { |
| 36 | } |
| 37 | |
| 38 | BroadcastRadio::~BroadcastRadio() |
| 39 | { |
| 40 | if (mHwDevice != NULL) { |
| 41 | radio_hw_device_close(mHwDevice); |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | void BroadcastRadio::onFirstRef() |
| 46 | { |
| 47 | const hw_module_t *mod; |
| 48 | int rc; |
| 49 | ALOGI("%s mClassId %d", __FUNCTION__, mClassId); |
| 50 | |
| 51 | mHwDevice = NULL; |
| 52 | const char *classString = Utils::getClassString(mClassId); |
| 53 | if (classString == NULL) { |
| 54 | ALOGE("invalid class ID %d", mClassId); |
| 55 | mStatus = Result::INVALID_ARGUMENTS; |
| 56 | return; |
| 57 | } |
| 58 | |
| 59 | ALOGI("%s RADIO_HARDWARE_MODULE_ID %s %s", |
| 60 | __FUNCTION__, RADIO_HARDWARE_MODULE_ID, classString); |
| 61 | |
| 62 | rc = hw_get_module_by_class(RADIO_HARDWARE_MODULE_ID, classString, &mod); |
| 63 | if (rc != 0) { |
| 64 | ALOGE("couldn't load radio module %s.%s (%s)", |
| 65 | RADIO_HARDWARE_MODULE_ID, classString, strerror(-rc)); |
| 66 | return; |
| 67 | } |
| 68 | rc = radio_hw_device_open(mod, &mHwDevice); |
| 69 | if (rc != 0) { |
| 70 | ALOGE("couldn't open radio hw device in %s.%s (%s)", |
| 71 | RADIO_HARDWARE_MODULE_ID, "primary", strerror(-rc)); |
| 72 | mHwDevice = NULL; |
| 73 | return; |
| 74 | } |
| 75 | if (mHwDevice->common.version != RADIO_DEVICE_API_VERSION_CURRENT) { |
| 76 | ALOGE("wrong radio hw device version %04x", mHwDevice->common.version); |
| 77 | radio_hw_device_close(mHwDevice); |
| 78 | mHwDevice = NULL; |
| 79 | } else { |
| 80 | mStatus = Result::OK; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | int BroadcastRadio::closeHalTuner(const struct radio_tuner *halTuner) |
| 85 | { |
| 86 | ALOGV("%s", __FUNCTION__); |
| 87 | if (mHwDevice == NULL) { |
| 88 | return -ENODEV; |
| 89 | } |
| 90 | if (halTuner == 0) { |
| 91 | return -EINVAL; |
| 92 | } |
| 93 | return mHwDevice->close_tuner(mHwDevice, halTuner); |
| 94 | } |
| 95 | |
| 96 | |
| 97 | // Methods from ::android::hardware::broadcastradio::V1_0::IBroadcastRadio follow. |
| 98 | Return<void> BroadcastRadio::getProperties(getProperties_cb _hidl_cb) |
| 99 | { |
| 100 | int rc; |
| 101 | radio_hal_properties_t halProperties; |
| 102 | Properties properties; |
| 103 | |
| 104 | if (mHwDevice == NULL) { |
| 105 | rc = -ENODEV; |
| 106 | goto exit; |
| 107 | } |
| 108 | rc = mHwDevice->get_properties(mHwDevice, &halProperties); |
| 109 | if (rc == 0) { |
| 110 | Utils::convertPropertiesFromHal(&properties, &halProperties); |
| 111 | } |
| 112 | |
| 113 | exit: |
| 114 | _hidl_cb(Utils::convertHalResult(rc), properties); |
| 115 | return Void(); |
| 116 | } |
| 117 | |
| 118 | Return<void> BroadcastRadio::openTuner(const BandConfig& config, bool audio, |
| 119 | const sp<ITunerCallback>& callback, openTuner_cb _hidl_cb) |
| 120 | { |
| 121 | sp<Tuner> tunerImpl = new Tuner(callback, this); |
| 122 | |
| 123 | radio_hal_band_config_t halConfig; |
| 124 | const struct radio_tuner *halTuner; |
| 125 | Utils::convertBandConfigToHal(&halConfig, &config); |
| 126 | int rc = mHwDevice->open_tuner(mHwDevice, &halConfig, audio, |
| 127 | Tuner::callback, tunerImpl.get(), |
| 128 | &halTuner); |
| 129 | if (rc == 0) { |
| 130 | tunerImpl->setHalTuner(halTuner); |
| 131 | } |
| 132 | |
| 133 | _hidl_cb(Utils::convertHalResult(rc), tunerImpl); |
| 134 | return Void(); |
| 135 | } |
| 136 | |
| 137 | |
| 138 | } // namespace implementation |
| 139 | } // namespace V1_0 |
| 140 | } // namespace broadcastradio |
| 141 | } // namespace hardware |
| 142 | } // namespace android |