Eric Laurent | d73697b | 2015-03-05 15:09:23 -0800 | [diff] [blame] | 1 | /* |
| 2 | ** |
| 3 | ** Copyright 2015, The Android Open Source Project |
| 4 | ** |
| 5 | ** Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | ** you may not use this file except in compliance with the License. |
| 7 | ** You may obtain a copy of the License at |
| 8 | ** |
| 9 | ** http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | ** |
| 11 | ** Unless required by applicable law or agreed to in writing, software |
| 12 | ** distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | ** See the License for the specific language governing permissions and |
| 15 | ** limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | #define LOG_TAG "IRadio" |
Tomasz Wasilczyk | 302ea57 | 2017-01-23 14:36:15 -0800 | [diff] [blame^] | 19 | //#define LOG_NDEBUG 0 |
Eric Laurent | d73697b | 2015-03-05 15:09:23 -0800 | [diff] [blame] | 20 | #include <utils/Log.h> |
| 21 | #include <utils/Errors.h> |
| 22 | #include <binder/IMemory.h> |
| 23 | #include <radio/IRadio.h> |
| 24 | #include <radio/IRadioService.h> |
| 25 | #include <radio/IRadioClient.h> |
| 26 | #include <system/radio.h> |
Tomasz Wasilczyk | 302ea57 | 2017-01-23 14:36:15 -0800 | [diff] [blame^] | 27 | #include <system/RadioMetadataWrapper.h> |
Eric Laurent | d73697b | 2015-03-05 15:09:23 -0800 | [diff] [blame] | 28 | |
| 29 | namespace android { |
| 30 | |
| 31 | enum { |
| 32 | DETACH = IBinder::FIRST_CALL_TRANSACTION, |
| 33 | SET_CONFIGURATION, |
| 34 | GET_CONFIGURATION, |
| 35 | SET_MUTE, |
| 36 | GET_MUTE, |
| 37 | SCAN, |
| 38 | STEP, |
| 39 | TUNE, |
| 40 | CANCEL, |
| 41 | GET_PROGRAM_INFORMATION, |
| 42 | HAS_CONTROL |
| 43 | }; |
| 44 | |
| 45 | class BpRadio: public BpInterface<IRadio> |
| 46 | { |
| 47 | public: |
Chih-Hung Hsieh | 090ef60 | 2016-04-27 10:39:54 -0700 | [diff] [blame] | 48 | explicit BpRadio(const sp<IBinder>& impl) |
Eric Laurent | d73697b | 2015-03-05 15:09:23 -0800 | [diff] [blame] | 49 | : BpInterface<IRadio>(impl) |
| 50 | { |
| 51 | } |
| 52 | |
| 53 | void detach() |
| 54 | { |
| 55 | ALOGV("detach"); |
| 56 | Parcel data, reply; |
| 57 | data.writeInterfaceToken(IRadio::getInterfaceDescriptor()); |
| 58 | remote()->transact(DETACH, data, &reply); |
| 59 | } |
| 60 | |
| 61 | virtual status_t setConfiguration(const struct radio_band_config *config) |
| 62 | { |
| 63 | Parcel data, reply; |
| 64 | if (config == NULL) { |
| 65 | return BAD_VALUE; |
| 66 | } |
| 67 | data.writeInterfaceToken(IRadio::getInterfaceDescriptor()); |
| 68 | data.write(config, sizeof(struct radio_band_config)); |
| 69 | status_t status = remote()->transact(SET_CONFIGURATION, data, &reply); |
| 70 | if (status == NO_ERROR) { |
| 71 | status = (status_t)reply.readInt32(); |
| 72 | } |
| 73 | return status; |
| 74 | } |
| 75 | |
| 76 | virtual status_t getConfiguration(struct radio_band_config *config) |
| 77 | { |
| 78 | Parcel data, reply; |
| 79 | if (config == NULL) { |
| 80 | return BAD_VALUE; |
| 81 | } |
| 82 | data.writeInterfaceToken(IRadio::getInterfaceDescriptor()); |
| 83 | status_t status = remote()->transact(GET_CONFIGURATION, data, &reply); |
| 84 | if (status == NO_ERROR) { |
| 85 | status = (status_t)reply.readInt32(); |
| 86 | if (status == NO_ERROR) { |
| 87 | reply.read(config, sizeof(struct radio_band_config)); |
| 88 | } |
| 89 | } |
| 90 | return status; |
| 91 | } |
| 92 | |
| 93 | virtual status_t setMute(bool mute) |
| 94 | { |
| 95 | Parcel data, reply; |
| 96 | data.writeInterfaceToken(IRadio::getInterfaceDescriptor()); |
| 97 | data.writeInt32(mute ? 1 : 0); |
| 98 | status_t status = remote()->transact(SET_MUTE, data, &reply); |
| 99 | if (status == NO_ERROR) { |
| 100 | status = (status_t)reply.readInt32(); |
| 101 | } |
| 102 | return status; |
| 103 | } |
| 104 | |
| 105 | virtual status_t getMute(bool *mute) |
| 106 | { |
| 107 | Parcel data, reply; |
| 108 | if (mute == NULL) { |
| 109 | return BAD_VALUE; |
| 110 | } |
| 111 | data.writeInterfaceToken(IRadio::getInterfaceDescriptor()); |
| 112 | status_t status = remote()->transact(GET_MUTE, data, &reply); |
| 113 | if (status == NO_ERROR) { |
| 114 | status = (status_t)reply.readInt32(); |
| 115 | if (status == NO_ERROR) { |
| 116 | int muteread = reply.readInt32(); |
| 117 | *mute = muteread != 0; |
| 118 | } |
| 119 | } |
| 120 | return status; |
| 121 | } |
| 122 | |
| 123 | virtual status_t scan(radio_direction_t direction, bool skipSubChannel) |
| 124 | { |
| 125 | Parcel data, reply; |
| 126 | data.writeInterfaceToken(IRadio::getInterfaceDescriptor()); |
| 127 | data.writeInt32(direction); |
| 128 | data.writeInt32(skipSubChannel ? 1 : 0); |
| 129 | status_t status = remote()->transact(SCAN, data, &reply); |
| 130 | if (status == NO_ERROR) { |
| 131 | status = (status_t)reply.readInt32(); |
| 132 | } |
| 133 | return status; |
| 134 | } |
| 135 | |
| 136 | virtual status_t step(radio_direction_t direction, bool skipSubChannel) |
| 137 | { |
| 138 | Parcel data, reply; |
| 139 | data.writeInterfaceToken(IRadio::getInterfaceDescriptor()); |
| 140 | data.writeInt32(direction); |
| 141 | data.writeInt32(skipSubChannel ? 1 : 0); |
| 142 | status_t status = remote()->transact(STEP, data, &reply); |
| 143 | if (status == NO_ERROR) { |
| 144 | status = (status_t)reply.readInt32(); |
| 145 | } |
| 146 | return status; |
| 147 | } |
| 148 | |
| 149 | virtual status_t tune(unsigned int channel, unsigned int subChannel) |
| 150 | { |
| 151 | Parcel data, reply; |
| 152 | data.writeInterfaceToken(IRadio::getInterfaceDescriptor()); |
| 153 | data.writeInt32(channel); |
| 154 | data.writeInt32(subChannel); |
| 155 | status_t status = remote()->transact(TUNE, data, &reply); |
| 156 | if (status == NO_ERROR) { |
| 157 | status = (status_t)reply.readInt32(); |
| 158 | } |
| 159 | return status; |
| 160 | } |
| 161 | |
| 162 | virtual status_t cancel() |
| 163 | { |
| 164 | Parcel data, reply; |
| 165 | data.writeInterfaceToken(IRadio::getInterfaceDescriptor()); |
| 166 | status_t status = remote()->transact(CANCEL, data, &reply); |
| 167 | if (status == NO_ERROR) { |
| 168 | status = (status_t)reply.readInt32(); |
| 169 | } |
| 170 | return status; |
| 171 | } |
| 172 | |
| 173 | virtual status_t getProgramInformation(struct radio_program_info *info) |
| 174 | { |
| 175 | Parcel data, reply; |
| 176 | if (info == NULL) { |
| 177 | return BAD_VALUE; |
| 178 | } |
| 179 | radio_metadata_t *metadata = info->metadata; |
| 180 | data.writeInterfaceToken(IRadio::getInterfaceDescriptor()); |
| 181 | status_t status = remote()->transact(GET_PROGRAM_INFORMATION, data, &reply); |
| 182 | if (status == NO_ERROR) { |
| 183 | status = (status_t)reply.readInt32(); |
| 184 | if (status == NO_ERROR) { |
| 185 | reply.read(info, sizeof(struct radio_program_info)); |
| 186 | info->metadata = metadata; |
| 187 | if (metadata == NULL) { |
| 188 | return status; |
| 189 | } |
| 190 | size_t size = (size_t)reply.readInt32(); |
| 191 | if (size == 0) { |
| 192 | return status; |
| 193 | } |
| 194 | metadata = |
| 195 | (radio_metadata_t *)calloc(size / sizeof(unsigned int), sizeof(unsigned int)); |
| 196 | if (metadata == NULL) { |
| 197 | return NO_MEMORY; |
| 198 | } |
| 199 | reply.read(metadata, size); |
| 200 | status = radio_metadata_add_metadata(&info->metadata, metadata); |
| 201 | free(metadata); |
| 202 | } |
| 203 | } |
| 204 | return status; |
| 205 | } |
| 206 | |
| 207 | virtual status_t hasControl(bool *hasControl) |
| 208 | { |
| 209 | Parcel data, reply; |
| 210 | if (hasControl == NULL) { |
| 211 | return BAD_VALUE; |
| 212 | } |
| 213 | data.writeInterfaceToken(IRadio::getInterfaceDescriptor()); |
| 214 | status_t status = remote()->transact(HAS_CONTROL, data, &reply); |
| 215 | if (status == NO_ERROR) { |
| 216 | status = (status_t)reply.readInt32(); |
| 217 | if (status == NO_ERROR) { |
| 218 | *hasControl = reply.readInt32() != 0; |
| 219 | } |
| 220 | } |
| 221 | return status; |
| 222 | } |
| 223 | }; |
| 224 | |
| 225 | IMPLEMENT_META_INTERFACE(Radio, "android.hardware.IRadio"); |
| 226 | |
| 227 | // ---------------------------------------------------------------------- |
| 228 | |
| 229 | status_t BnRadio::onTransact( |
| 230 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) |
| 231 | { |
| 232 | switch(code) { |
| 233 | case DETACH: { |
| 234 | ALOGV("DETACH"); |
| 235 | CHECK_INTERFACE(IRadio, data, reply); |
| 236 | detach(); |
| 237 | return NO_ERROR; |
| 238 | } break; |
| 239 | case SET_CONFIGURATION: { |
| 240 | CHECK_INTERFACE(IRadio, data, reply); |
| 241 | struct radio_band_config config; |
| 242 | data.read(&config, sizeof(struct radio_band_config)); |
| 243 | status_t status = setConfiguration(&config); |
| 244 | reply->writeInt32(status); |
| 245 | return NO_ERROR; |
| 246 | } |
| 247 | case GET_CONFIGURATION: { |
| 248 | CHECK_INTERFACE(IRadio, data, reply); |
| 249 | struct radio_band_config config; |
| 250 | status_t status = getConfiguration(&config); |
| 251 | reply->writeInt32(status); |
| 252 | if (status == NO_ERROR) { |
| 253 | reply->write(&config, sizeof(struct radio_band_config)); |
| 254 | } |
| 255 | return NO_ERROR; |
| 256 | } |
| 257 | case SET_MUTE: { |
| 258 | CHECK_INTERFACE(IRadio, data, reply); |
| 259 | bool mute = data.readInt32() != 0; |
| 260 | status_t status = setMute(mute); |
| 261 | reply->writeInt32(status); |
| 262 | return NO_ERROR; |
| 263 | } |
| 264 | case GET_MUTE: { |
| 265 | CHECK_INTERFACE(IRadio, data, reply); |
| 266 | bool mute; |
| 267 | status_t status = getMute(&mute); |
| 268 | reply->writeInt32(status); |
| 269 | if (status == NO_ERROR) { |
| 270 | reply->writeInt32(mute ? 1 : 0); |
| 271 | } |
| 272 | return NO_ERROR; |
| 273 | } |
| 274 | case SCAN: { |
| 275 | CHECK_INTERFACE(IRadio, data, reply); |
| 276 | radio_direction_t direction = (radio_direction_t)data.readInt32(); |
| 277 | bool skipSubChannel = data.readInt32() == 1; |
| 278 | status_t status = scan(direction, skipSubChannel); |
| 279 | reply->writeInt32(status); |
| 280 | return NO_ERROR; |
| 281 | } |
| 282 | case STEP: { |
| 283 | CHECK_INTERFACE(IRadio, data, reply); |
| 284 | radio_direction_t direction = (radio_direction_t)data.readInt32(); |
| 285 | bool skipSubChannel = data.readInt32() == 1; |
| 286 | status_t status = step(direction, skipSubChannel); |
| 287 | reply->writeInt32(status); |
| 288 | return NO_ERROR; |
| 289 | } |
| 290 | case TUNE: { |
| 291 | CHECK_INTERFACE(IRadio, data, reply); |
| 292 | unsigned int channel = (unsigned int)data.readInt32(); |
| 293 | unsigned int subChannel = (unsigned int)data.readInt32(); |
| 294 | status_t status = tune(channel, subChannel); |
| 295 | reply->writeInt32(status); |
| 296 | return NO_ERROR; |
| 297 | } |
| 298 | case CANCEL: { |
| 299 | CHECK_INTERFACE(IRadio, data, reply); |
| 300 | status_t status = cancel(); |
| 301 | reply->writeInt32(status); |
| 302 | return NO_ERROR; |
| 303 | } |
| 304 | case GET_PROGRAM_INFORMATION: { |
| 305 | CHECK_INTERFACE(IRadio, data, reply); |
| 306 | struct radio_program_info info; |
Tomasz Wasilczyk | 302ea57 | 2017-01-23 14:36:15 -0800 | [diff] [blame^] | 307 | RadioMetadataWrapper metadataWrapper(&info.metadata); |
Eric Laurent | d73697b | 2015-03-05 15:09:23 -0800 | [diff] [blame] | 308 | |
Tomasz Wasilczyk | 302ea57 | 2017-01-23 14:36:15 -0800 | [diff] [blame^] | 309 | status_t status = getProgramInformation(&info); |
Eric Laurent | d73697b | 2015-03-05 15:09:23 -0800 | [diff] [blame] | 310 | reply->writeInt32(status); |
| 311 | if (status == NO_ERROR) { |
| 312 | reply->write(&info, sizeof(struct radio_program_info)); |
| 313 | int count = radio_metadata_get_count(info.metadata); |
| 314 | if (count > 0) { |
| 315 | size_t size = radio_metadata_get_size(info.metadata); |
| 316 | reply->writeInt32(size); |
| 317 | reply->write(info.metadata, size); |
| 318 | } else { |
| 319 | reply->writeInt32(0); |
| 320 | } |
| 321 | } |
Eric Laurent | d73697b | 2015-03-05 15:09:23 -0800 | [diff] [blame] | 322 | return NO_ERROR; |
| 323 | } |
| 324 | case HAS_CONTROL: { |
| 325 | CHECK_INTERFACE(IRadio, data, reply); |
| 326 | bool control; |
| 327 | status_t status = hasControl(&control); |
| 328 | reply->writeInt32(status); |
| 329 | if (status == NO_ERROR) { |
| 330 | reply->writeInt32(control ? 1 : 0); |
| 331 | } |
| 332 | return NO_ERROR; |
| 333 | } |
| 334 | default: |
| 335 | return BBinder::onTransact(code, data, reply, flags); |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | // ---------------------------------------------------------------------------- |
| 340 | |
| 341 | }; // namespace android |