blob: ae5848cec71ed5e00bd2bece79d72fdb9f670fdf [file] [log] [blame]
Tomasz Wasilczyk213170b2017-02-07 17:38:21 -08001/*
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
17#define LOG_TAG "Tuner"
18//#define LOG_NDEBUG 0
19
20#include <log/log.h>
21
22#include "BroadcastRadio.h"
23#include "Tuner.h"
24#include "Utils.h"
25#include <system/RadioMetadataWrapper.h>
26
27namespace android {
28namespace hardware {
29namespace broadcastradio {
30namespace V1_1 {
31namespace implementation {
32
33void Tuner::onCallback(radio_hal_event_t *halEvent)
34{
35 BandConfig config;
36 ProgramInfo info;
37 hidl_vec<MetaData> metadata;
38
39 if (mCallback != 0) {
40 switch(halEvent->type) {
41 case RADIO_EVENT_CONFIG:
42 Utils::convertBandConfigFromHal(&config, &halEvent->config);
43 mCallback->configChange(Utils::convertHalResult(halEvent->status), config);
44 break;
45 case RADIO_EVENT_ANTENNA:
46 mCallback->antennaStateChange(halEvent->on);
47 break;
48 case RADIO_EVENT_TUNED:
49 Utils::convertProgramInfoFromHal(&info, &halEvent->info);
50 if (mCallback1_1 != nullptr) {
51 mCallback1_1->tuneComplete_1_1(Utils::convertHalResult(halEvent->status), info);
52 }
53 mCallback->tuneComplete(Utils::convertHalResult(halEvent->status), info.base);
54 break;
55 case RADIO_EVENT_METADATA: {
56 uint32_t channel;
57 uint32_t sub_channel;
58 if (radio_metadata_get_channel(halEvent->metadata, &channel, &sub_channel) == 0) {
59 Utils::convertMetaDataFromHal(metadata, halEvent->metadata);
60 mCallback->newMetadata(channel, sub_channel, metadata);
61 }
62 } break;
63 case RADIO_EVENT_TA:
64 mCallback->trafficAnnouncement(halEvent->on);
65 break;
66 case RADIO_EVENT_AF_SWITCH:
67 Utils::convertProgramInfoFromHal(&info, &halEvent->info);
68 if (mCallback1_1 != nullptr) {
69 mCallback1_1->afSwitch_1_1(info);
70 }
71 mCallback->afSwitch(info.base);
72 break;
73 case RADIO_EVENT_EA:
74 mCallback->emergencyAnnouncement(halEvent->on);
75 break;
76 case RADIO_EVENT_HW_FAILURE:
77 default:
78 mCallback->hardwareFailure();
79 break;
80 }
81 }
82}
83
84//static
85void Tuner::callback(radio_hal_event_t *halEvent, void *cookie)
86{
87 wp<Tuner> weak(reinterpret_cast<Tuner*>(cookie));
88 sp<Tuner> tuner = weak.promote();
89 if (tuner == 0) return;
90 tuner->onCallback(halEvent);
91}
92
93Tuner::Tuner(const sp<V1_0::ITunerCallback>& callback, const wp<BroadcastRadio>& parentDevice)
94 : mHalTuner(NULL), mCallback(callback), mCallback1_1(ITunerCallback::castFrom(callback)),
95 mParentDevice(parentDevice)
96{
97 ALOGV("%s", __FUNCTION__);
98}
99
100
101Tuner::~Tuner()
102{
103 ALOGV("%s", __FUNCTION__);
104 const sp<BroadcastRadio> parentDevice = mParentDevice.promote();
105 if (parentDevice != 0) {
106 parentDevice->closeHalTuner(mHalTuner);
107 }
108}
109
110// Methods from ::android::hardware::broadcastradio::V1_1::ITuner follow.
111Return<Result> Tuner::setConfiguration(const BandConfig& config) {
112 ALOGV("%s", __FUNCTION__);
113 if (mHalTuner == NULL) {
114 return Utils::convertHalResult(-ENODEV);
115 }
116 radio_hal_band_config_t halConfig;
117 Utils::convertBandConfigToHal(&halConfig, &config);
118 int rc = mHalTuner->set_configuration(mHalTuner, &halConfig);
119 return Utils::convertHalResult(rc);
120}
121
122Return<void> Tuner::getConfiguration(getConfiguration_cb _hidl_cb) {
123 int rc;
124 radio_hal_band_config_t halConfig;
125 BandConfig config;
126
127 ALOGV("%s", __FUNCTION__);
128 if (mHalTuner == NULL) {
129 rc = -ENODEV;
130 goto exit;
131 }
132 rc = mHalTuner->get_configuration(mHalTuner, &halConfig);
133 if (rc == 0) {
134 Utils::convertBandConfigFromHal(&config, &halConfig);
135 }
136
137exit:
138 _hidl_cb(Utils::convertHalResult(rc), config);
139 return Void();
140}
141
142Return<Result> Tuner::scan(Direction direction, bool skipSubChannel) {
143 if (mHalTuner == NULL) {
144 return Utils::convertHalResult(-ENODEV);
145 }
146 int rc = mHalTuner->scan(mHalTuner, static_cast<radio_direction_t>(direction), skipSubChannel);
147 return Utils::convertHalResult(rc);
148}
149
150Return<Result> Tuner::step(Direction direction, bool skipSubChannel) {
151 if (mHalTuner == NULL) {
152 return Utils::convertHalResult(-ENODEV);
153 }
154 int rc = mHalTuner->step(mHalTuner, static_cast<radio_direction_t>(direction), skipSubChannel);
155 return Utils::convertHalResult(rc);
156}
157
158Return<Result> Tuner::tune(uint32_t channel, uint32_t subChannel) {
159 if (mHalTuner == NULL) {
160 return Utils::convertHalResult(-ENODEV);
161 }
162 int rc = mHalTuner->tune(mHalTuner, channel, subChannel);
163 return Utils::convertHalResult(rc);
164}
165
166Return<Result> Tuner::cancel() {
167 if (mHalTuner == NULL) {
168 return Utils::convertHalResult(-ENODEV);
169 }
170 int rc = mHalTuner->cancel(mHalTuner);
171 return Utils::convertHalResult(rc);
172}
173
174Return<void> Tuner::getProgramInformation(getProgramInformation_cb _hidl_cb) {
175 ALOGV("%s", __FUNCTION__);
176 return getProgramInformation_1_1([&](Result result, const ProgramInfo& info) {
177 _hidl_cb(result, info.base);
178 });
179}
180
181Return<void> Tuner::getProgramInformation_1_1(getProgramInformation_1_1_cb _hidl_cb) {
182 int rc;
183 radio_program_info_t halInfo;
184 RadioMetadataWrapper metadataWrapper(&halInfo.metadata);
185 ProgramInfo info;
186
187 ALOGV("%s", __FUNCTION__);
188 if (mHalTuner == NULL) {
189 rc = -ENODEV;
190 goto exit;
191 }
192
193 rc = mHalTuner->get_program_information(mHalTuner, &halInfo);
194 if (rc == 0) {
195 Utils::convertProgramInfoFromHal(&info, &halInfo);
196 }
197
198exit:
199 _hidl_cb(Utils::convertHalResult(rc), info);
200 return Void();
201}
202
Tomasz Wasilczyk803301a2017-03-13 14:30:15 -0700203Return<ProgramListResult> Tuner::startBackgroundScan() {
Tomasz Wasilczyk6ca90ed2017-05-15 12:57:20 -0700204 return ProgramListResult::UNAVAILABLE;
Tomasz Wasilczyk803301a2017-03-13 14:30:15 -0700205}
206
Tomasz Wasilczyk2dd1d8d2017-03-01 14:21:07 -0800207Return<void> Tuner::getProgramList(const hidl_string& filter __unused, getProgramList_cb _hidl_cb) {
208 hidl_vec<ProgramInfo> pList;
209 // TODO(b/34054813): do the actual implementation.
Tomasz Wasilczyk6ca90ed2017-05-15 12:57:20 -0700210 _hidl_cb(ProgramListResult::NOT_STARTED, pList);
Tomasz Wasilczyk2dd1d8d2017-03-01 14:21:07 -0800211 return Void();
212}
213
Tomasz Wasilczykc7002822017-03-27 14:29:16 -0700214Return<void> Tuner::isAnalogForced(isAnalogForced_cb _hidl_cb) {
215 // TODO(b/34348946): do the actual implementation.
Tomasz Wasilczyk6ca90ed2017-05-15 12:57:20 -0700216 _hidl_cb(Result::INVALID_STATE, false);
Tomasz Wasilczykc7002822017-03-27 14:29:16 -0700217 return Void();
218}
219
220Return<Result> Tuner::setAnalogForced(bool isForced __unused) {
221 // TODO(b/34348946): do the actual implementation.
Tomasz Wasilczyk6ca90ed2017-05-15 12:57:20 -0700222 return Result::INVALID_STATE;
Tomasz Wasilczykc7002822017-03-27 14:29:16 -0700223}
224
Tomasz Wasilczyk213170b2017-02-07 17:38:21 -0800225} // namespace implementation
226} // namespace V1_1
227} // namespace broadcastradio
228} // namespace hardware
229} // namespace android