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 
17 #define LOG_TAG "RadioHalLegacy"
18 //#define LOG_NDEBUG 0
19 
20 #include <utils/Log.h>
21 #include <utils/misc.h>
22 #include "RadioHalLegacy.h"
23 
24 namespace android {
25 
26 const char *RadioHalLegacy::sClassModuleNames[] = {
27     RADIO_HARDWARE_MODULE_ID_FM, /* corresponds to RADIO_CLASS_AM_FM */
28     RADIO_HARDWARE_MODULE_ID_SAT,  /* corresponds to RADIO_CLASS_SAT */
29     RADIO_HARDWARE_MODULE_ID_DT,   /* corresponds to RADIO_CLASS_DT */
30 };
31 
32 /* static */
connectModule(radio_class_t classId)33 sp<RadioInterface> RadioInterface::connectModule(radio_class_t classId)
34 {
35     return new RadioHalLegacy(classId);
36 }
37 
RadioHalLegacy(radio_class_t classId)38 RadioHalLegacy::RadioHalLegacy(radio_class_t classId)
39     : RadioInterface(), mClassId(classId), mHwDevice(NULL)
40 {
41 }
42 
onFirstRef()43 void RadioHalLegacy::onFirstRef()
44 {
45     const hw_module_t *mod;
46     int rc;
47     ALOGI("%s mClassId %d", __FUNCTION__, mClassId);
48 
49     mHwDevice = NULL;
50 
51     if ((mClassId < 0) ||
52             (mClassId >= NELEM(sClassModuleNames))) {
53         ALOGE("invalid class ID %d", mClassId);
54         return;
55     }
56 
57     ALOGI("%s RADIO_HARDWARE_MODULE_ID %s %s",
58           __FUNCTION__, RADIO_HARDWARE_MODULE_ID, sClassModuleNames[mClassId]);
59 
60     rc = hw_get_module_by_class(RADIO_HARDWARE_MODULE_ID, sClassModuleNames[mClassId], &mod);
61     if (rc != 0) {
62         ALOGE("couldn't load radio module %s.%s (%s)",
63               RADIO_HARDWARE_MODULE_ID, sClassModuleNames[mClassId], strerror(-rc));
64         return;
65     }
66     rc = radio_hw_device_open(mod, &mHwDevice);
67     if (rc != 0) {
68         ALOGE("couldn't open radio hw device in %s.%s (%s)",
69               RADIO_HARDWARE_MODULE_ID, "primary", strerror(-rc));
70         mHwDevice = NULL;
71         return;
72     }
73     if (mHwDevice->common.version != RADIO_DEVICE_API_VERSION_CURRENT) {
74         ALOGE("wrong radio hw device version %04x", mHwDevice->common.version);
75         radio_hw_device_close(mHwDevice);
76         mHwDevice = NULL;
77     }
78 }
79 
~RadioHalLegacy()80 RadioHalLegacy::~RadioHalLegacy()
81 {
82     if (mHwDevice != NULL) {
83         radio_hw_device_close(mHwDevice);
84     }
85 }
86 
getProperties(radio_hal_properties_t * properties)87 int RadioHalLegacy::getProperties(radio_hal_properties_t *properties)
88 {
89     if (mHwDevice == NULL) {
90         return -ENODEV;
91     }
92 
93     int rc = mHwDevice->get_properties(mHwDevice, properties);
94     if (rc != 0) {
95         ALOGE("could not read implementation properties");
96     }
97 
98     return rc;
99 }
100 
openTuner(const radio_hal_band_config_t * config,bool audio,sp<TunerCallbackInterface> callback,sp<TunerInterface> & tuner)101 int RadioHalLegacy::openTuner(const radio_hal_band_config_t *config,
102                 bool audio,
103                 sp<TunerCallbackInterface> callback,
104                 sp<TunerInterface>& tuner)
105 {
106     if (mHwDevice == NULL) {
107         return -ENODEV;
108     }
109     sp<Tuner> tunerImpl = new Tuner(callback);
110 
111     const struct radio_tuner *halTuner;
112     int rc = mHwDevice->open_tuner(mHwDevice, config, audio,
113                                    RadioHalLegacy::Tuner::callback, tunerImpl.get(),
114                                    &halTuner);
115     if (rc == 0) {
116         tunerImpl->setHalTuner(halTuner);
117         tuner = tunerImpl;
118     }
119     return rc;
120 }
121 
closeTuner(sp<TunerInterface> & tuner)122 int RadioHalLegacy::closeTuner(sp<TunerInterface>& tuner)
123 {
124     if (mHwDevice == NULL) {
125         return -ENODEV;
126     }
127     if (tuner == 0) {
128         return -EINVAL;
129     }
130     sp<Tuner> tunerImpl = (Tuner *)tuner.get();
131     return mHwDevice->close_tuner(mHwDevice, tunerImpl->getHalTuner());
132 }
133 
setConfiguration(const radio_hal_band_config_t * config)134 int RadioHalLegacy::Tuner::setConfiguration(const radio_hal_band_config_t *config)
135 {
136     if (mHalTuner == NULL) {
137         return -ENODEV;
138     }
139     return mHalTuner->set_configuration(mHalTuner, config);
140 }
141 
getConfiguration(radio_hal_band_config_t * config)142 int RadioHalLegacy::Tuner::getConfiguration(radio_hal_band_config_t *config)
143 {
144     if (mHalTuner == NULL) {
145         return -ENODEV;
146     }
147     return mHalTuner->get_configuration(mHalTuner, config);
148 }
149 
scan(radio_direction_t direction,bool skip_sub_channel)150 int RadioHalLegacy::Tuner::scan(radio_direction_t direction, bool skip_sub_channel)
151 {
152     if (mHalTuner == NULL) {
153         return -ENODEV;
154     }
155     return mHalTuner->scan(mHalTuner, direction, skip_sub_channel);
156 }
157 
step(radio_direction_t direction,bool skip_sub_channel)158 int RadioHalLegacy::Tuner::step(radio_direction_t direction, bool skip_sub_channel)
159 {
160     if (mHalTuner == NULL) {
161         return -ENODEV;
162     }
163     return mHalTuner->step(mHalTuner, direction, skip_sub_channel);
164 }
165 
tune(unsigned int channel,unsigned int sub_channel)166 int RadioHalLegacy::Tuner::tune(unsigned int channel, unsigned int sub_channel)
167 {
168     if (mHalTuner == NULL) {
169         return -ENODEV;
170     }
171     return mHalTuner->tune(mHalTuner, channel, sub_channel);
172 }
173 
cancel()174 int RadioHalLegacy::Tuner::cancel()
175 {
176     if (mHalTuner == NULL) {
177         return -ENODEV;
178     }
179     return mHalTuner->cancel(mHalTuner);
180 }
181 
getProgramInformation(radio_program_info_t * info)182 int RadioHalLegacy::Tuner::getProgramInformation(radio_program_info_t *info)
183 {
184     if (mHalTuner == NULL) {
185         return -ENODEV;
186     }
187     return mHalTuner->get_program_information(mHalTuner, info);
188 }
189 
onCallback(radio_hal_event_t * halEvent)190 void RadioHalLegacy::Tuner::onCallback(radio_hal_event_t *halEvent)
191 {
192     if (mCallback != 0) {
193         mCallback->onEvent(halEvent);
194     }
195 }
196 
197 //static
callback(radio_hal_event_t * halEvent,void * cookie)198 void RadioHalLegacy::Tuner::callback(radio_hal_event_t *halEvent, void *cookie)
199 {
200     wp<RadioHalLegacy::Tuner> weak = wp<RadioHalLegacy::Tuner>((RadioHalLegacy::Tuner *)cookie);
201     sp<RadioHalLegacy::Tuner> tuner = weak.promote();
202     if (tuner != 0) {
203         tuner->onCallback(halEvent);
204     }
205 }
206 
Tuner(sp<TunerCallbackInterface> callback)207 RadioHalLegacy::Tuner::Tuner(sp<TunerCallbackInterface> callback)
208     : TunerInterface(), mHalTuner(NULL), mCallback(callback)
209 {
210 }
211 
212 
~Tuner()213 RadioHalLegacy::Tuner::~Tuner()
214 {
215 }
216 
217 
218 } // namespace android
219