summaryrefslogtreecommitdiff
path: root/broadcastradio/1.0/default/BroadcastRadio.cpp
blob: 72cdc19c1c1cdceebe007423497cefd34caa4053 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#define LOG_TAG "BroadcastRadio"
//#define LOG_NDEBUG 0

#include <log/log.h>

#include <hardware/radio.h>

#include "BroadcastRadio.h"
#include "Tuner.h"
#include "Utils.h"

namespace android {
namespace hardware {
namespace broadcastradio {
namespace V1_0 {
namespace implementation {

BroadcastRadio::BroadcastRadio(Class classId)
    : mStatus(Result::NOT_INITIALIZED), mClassId(classId), mHwDevice(NULL)
{
}

BroadcastRadio::~BroadcastRadio()
{
    if (mHwDevice != NULL) {
        radio_hw_device_close(mHwDevice);
    }
}

void BroadcastRadio::onFirstRef()
{
    const hw_module_t *mod;
    int rc;
    ALOGI("%s mClassId %d", __FUNCTION__, mClassId);

    mHwDevice = NULL;
    const char *classString = Utils::getClassString(mClassId);
    if (classString == NULL) {
        ALOGE("invalid class ID %d", mClassId);
        mStatus = Result::INVALID_ARGUMENTS;
        return;
    }

    ALOGI("%s RADIO_HARDWARE_MODULE_ID %s %s",
          __FUNCTION__, RADIO_HARDWARE_MODULE_ID, classString);

    rc = hw_get_module_by_class(RADIO_HARDWARE_MODULE_ID, classString, &mod);
    if (rc != 0) {
        ALOGE("couldn't load radio module %s.%s (%s)",
              RADIO_HARDWARE_MODULE_ID, classString, strerror(-rc));
        mStatus = Result::INVALID_ARGUMENTS;
        return;
    }
    rc = radio_hw_device_open(mod, &mHwDevice);
    if (rc != 0) {
        ALOGE("couldn't open radio hw device in %s.%s (%s)",
              RADIO_HARDWARE_MODULE_ID, "primary", strerror(-rc));
        mHwDevice = NULL;
        return;
    }
    if (mHwDevice->common.version != RADIO_DEVICE_API_VERSION_CURRENT) {
        ALOGE("wrong radio hw device version %04x", mHwDevice->common.version);
        radio_hw_device_close(mHwDevice);
        mHwDevice = NULL;
    } else {
        mStatus = Result::OK;
    }
}

int BroadcastRadio::closeHalTuner(const struct radio_tuner *halTuner)
{
    ALOGV("%s", __FUNCTION__);
    if (mHwDevice == NULL) {
        return -ENODEV;
    }
    if (halTuner == 0) {
        return -EINVAL;
    }
    return mHwDevice->close_tuner(mHwDevice, halTuner);
}


// Methods from ::android::hardware::broadcastradio::V1_0::IBroadcastRadio follow.
Return<void> BroadcastRadio::getProperties(getProperties_cb _hidl_cb)
{
    int rc;
    radio_hal_properties_t halProperties;
    Properties properties;

    if (mHwDevice == NULL) {
        rc = -ENODEV;
        goto exit;
    }
    rc = mHwDevice->get_properties(mHwDevice, &halProperties);
    if (rc == 0) {
        Utils::convertPropertiesFromHal(&properties, &halProperties);
    }

exit:
    _hidl_cb(Utils::convertHalResult(rc), properties);
    return Void();
}

Return<void> BroadcastRadio::openTuner(const BandConfig& config, bool audio,
                                       const sp<ITunerCallback>& callback, openTuner_cb _hidl_cb)
{
    sp<Tuner> tunerImpl = new Tuner(callback, this);

    radio_hal_band_config_t halConfig;
    const struct radio_tuner *halTuner;
    Utils::convertBandConfigToHal(&halConfig, &config);
    int rc = mHwDevice->open_tuner(mHwDevice, &halConfig, audio,
                                   Tuner::callback, tunerImpl.get(),
                                   &halTuner);
    if (rc == 0) {
        tunerImpl->setHalTuner(halTuner);
    }

    _hidl_cb(Utils::convertHalResult(rc), tunerImpl);
    return Void();
}


} // namespace implementation
}  // namespace V1_0
}  // namespace broadcastradio
}  // namespace hardware
}  // namespace android