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