1 /*
2  * Copyright (C) 2017 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 
19 #include <log/log.h>
20 
21 #include "BroadcastRadio.h"
22 #include "Tuner.h"
23 #include "Utils.h"
24 
25 namespace android {
26 namespace hardware {
27 namespace broadcastradio {
28 namespace V1_1 {
29 namespace implementation {
30 
31 using ::android::sp;
32 
BroadcastRadio(Class classId)33 BroadcastRadio::BroadcastRadio(Class classId)
34         : mStatus(Result::NOT_INITIALIZED), mClassId(classId), mHwDevice(NULL)
35 {
36 }
37 
~BroadcastRadio()38 BroadcastRadio::~BroadcastRadio()
39 {
40     if (mHwDevice != NULL) {
41         radio_hw_device_close(mHwDevice);
42     }
43 }
44 
onFirstRef()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 
closeHalTuner(const struct radio_tuner * halTuner)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_1::IBroadcastRadio follow.
getProperties(getProperties_cb _hidl_cb)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 
getProperties_1_1(getProperties_1_1_cb _hidl_cb __unused)118 Return<void> BroadcastRadio::getProperties_1_1(getProperties_1_1_cb _hidl_cb __unused)
119 {
120     return Status::fromExceptionCode(Status::EX_UNSUPPORTED_OPERATION);
121 }
122 
openTuner(const BandConfig & config,bool audio,const sp<V1_0::ITunerCallback> & callback,openTuner_cb _hidl_cb)123 Return<void> BroadcastRadio::openTuner(const BandConfig& config, bool audio,
124     const sp<V1_0::ITunerCallback>& callback, openTuner_cb _hidl_cb)
125 {
126     sp<Tuner> tunerImpl = new Tuner(callback, this);
127 
128     radio_hal_band_config_t halConfig;
129     const struct radio_tuner *halTuner;
130     Utils::convertBandConfigToHal(&halConfig, &config);
131     int rc = mHwDevice->open_tuner(mHwDevice, &halConfig, audio, Tuner::callback,
132             tunerImpl.get(), &halTuner);
133     if (rc == 0) {
134         tunerImpl->setHalTuner(halTuner);
135     }
136 
137     _hidl_cb(Utils::convertHalResult(rc), tunerImpl);
138     return Void();
139 }
140 
141 } // namespace implementation
142 }  // namespace V1_1
143 }  // namespace broadcastradio
144 }  // namespace hardware
145 }  // namespace android
146