• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
19 #include <log/log.h>
20 
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 
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         mStatus = Result::INVALID_ARGUMENTS;
67         return;
68     }
69     rc = radio_hw_device_open(mod, &mHwDevice);
70     if (rc != 0) {
71         ALOGE("couldn't open radio hw device in %s.%s (%s)",
72               RADIO_HARDWARE_MODULE_ID, "primary", strerror(-rc));
73         mHwDevice = NULL;
74         return;
75     }
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);
79         mHwDevice = NULL;
80     } else {
81         mStatus = Result::OK;
82     }
83 }
84 
closeHalTuner(const struct radio_tuner * halTuner)85 int BroadcastRadio::closeHalTuner(const struct radio_tuner *halTuner)
86 {
87     ALOGV("%s", __FUNCTION__);
88     if (mHwDevice == NULL) {
89         return -ENODEV;
90     }
91     if (halTuner == 0) {
92         return -EINVAL;
93     }
94     return mHwDevice->close_tuner(mHwDevice, halTuner);
95 }
96 
97 
98 // Methods from ::android::hardware::broadcastradio::V1_0::IBroadcastRadio follow.
getProperties(getProperties_cb _hidl_cb)99 Return<void> BroadcastRadio::getProperties(getProperties_cb _hidl_cb)
100 {
101     int rc;
102     radio_hal_properties_t halProperties;
103     Properties properties;
104 
105     if (mHwDevice == NULL) {
106         rc = -ENODEV;
107         goto exit;
108     }
109     rc = mHwDevice->get_properties(mHwDevice, &halProperties);
110     if (rc == 0) {
111         Utils::convertPropertiesFromHal(&properties, &halProperties);
112     }
113 
114 exit:
115     _hidl_cb(Utils::convertHalResult(rc), properties);
116     return Void();
117 }
118 
openTuner(const BandConfig & config,bool audio,const sp<ITunerCallback> & callback,openTuner_cb _hidl_cb)119 Return<void> BroadcastRadio::openTuner(const BandConfig& config, bool audio,
120                                        const sp<ITunerCallback>& callback, openTuner_cb _hidl_cb)
121 {
122     sp<Tuner> tunerImpl = new Tuner(callback, this);
123 
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(),
129                                    &halTuner);
130     if (rc == 0) {
131         tunerImpl->setHalTuner(halTuner);
132     }
133 
134     _hidl_cb(Utils::convertHalResult(rc), tunerImpl);
135     return Void();
136 }
137 
138 
139 } // namespace implementation
140 }  // namespace V1_0
141 }  // namespace broadcastradio
142 }  // namespace hardware
143 }  // namespace android
144