Home | History | Annotate | Download | only in radio
      1 /*
      2  * Copyright (C) 2015 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 "radio_hw_stub"
     18 #define LOG_NDEBUG 0
     19 
     20 #include <string.h>
     21 #include <stdlib.h>
     22 #include <errno.h>
     23 #include <pthread.h>
     24 #include <sys/prctl.h>
     25 #include <sys/time.h>
     26 #include <sys/types.h>
     27 #include <sys/stat.h>
     28 #include <fcntl.h>
     29 #include <unistd.h>
     30 #include <cutils/log.h>
     31 #include <cutils/list.h>
     32 #include <system/radio.h>
     33 #include <system/radio_metadata.h>
     34 #include <hardware/hardware.h>
     35 #include <hardware/radio.h>
     36 
     37 static const radio_hal_properties_t hw_properties = {
     38     .class_id = RADIO_CLASS_AM_FM,
     39     .implementor = "The Android Open Source Project",
     40     .product = "Radio stub HAL",
     41     .version = "0.1",
     42     .serial = "0123456789",
     43     .num_tuners = 1,
     44     .num_audio_sources = 1,
     45     .supports_capture = false,
     46     .num_bands = 2,
     47     .bands = {
     48         {
     49             .type = RADIO_BAND_FM,
     50             .antenna_connected = false,
     51             .lower_limit = 87900,
     52             .upper_limit = 107900,
     53             .num_spacings = 1,
     54             .spacings = { 200 },
     55             .fm = {
     56                 .deemphasis = RADIO_DEEMPHASIS_75,
     57                 .stereo = true,
     58                 .rds = RADIO_RDS_US,
     59                 .ta = false,
     60                 .af = false,
     61                 .ea = true,
     62             }
     63         },
     64         {
     65             .type = RADIO_BAND_AM,
     66             .antenna_connected = true,
     67             .lower_limit = 540,
     68             .upper_limit = 1610,
     69             .num_spacings = 1,
     70             .spacings = { 10 },
     71             .am = {
     72                 .stereo = true,
     73             }
     74         }
     75     }
     76 };
     77 
     78 static const radio_metadata_clock_t hw_clock = {
     79     .utc_seconds_since_epoch = 1234567890,
     80     .timezone_offset_in_minutes = (-8 * 60),
     81 };
     82 
     83 struct stub_radio_tuner {
     84     struct radio_tuner interface;
     85     struct stub_radio_device *dev;
     86     radio_callback_t callback;
     87     void *cookie;
     88     radio_hal_band_config_t config;
     89     radio_program_info_t program;
     90     bool audio;
     91     pthread_t callback_thread;
     92     pthread_mutex_t lock;
     93     pthread_cond_t  cond;
     94     struct listnode command_list;
     95 };
     96 
     97 struct stub_radio_device {
     98     struct radio_hw_device device;
     99     struct stub_radio_tuner *tuner;
    100     pthread_mutex_t lock;
    101 };
    102 
    103 
    104 typedef enum {
    105     CMD_EXIT,
    106     CMD_CONFIG,
    107     CMD_STEP,
    108     CMD_SCAN,
    109     CMD_TUNE,
    110     CMD_CANCEL,
    111     CMD_METADATA,
    112     CMD_ANNOUNCEMENTS,
    113 } thread_cmd_type_t;
    114 
    115 struct thread_command {
    116     struct listnode node;
    117     thread_cmd_type_t type;
    118     struct timespec ts;
    119     union {
    120         unsigned int param;
    121         radio_hal_band_config_t config;
    122     };
    123 };
    124 
    125 /* must be called with out->lock locked */
    126 static int send_command_l(struct stub_radio_tuner *tuner,
    127                           thread_cmd_type_t type,
    128                           unsigned int delay_ms,
    129                           void *param)
    130 {
    131     struct thread_command *cmd = (struct thread_command *)calloc(1, sizeof(struct thread_command));
    132     struct timespec ts;
    133 
    134     if (cmd == NULL)
    135         return -ENOMEM;
    136 
    137     ALOGV("%s %d delay_ms %d", __func__, type, delay_ms);
    138 
    139     cmd->type = type;
    140     if (param != NULL) {
    141         if (cmd->type == CMD_CONFIG) {
    142             cmd->config = *(radio_hal_band_config_t *)param;
    143             ALOGV("%s CMD_CONFIG type %d", __func__, cmd->config.type);
    144         } else
    145             cmd->param = *(unsigned int *)param;
    146     }
    147 
    148     clock_gettime(CLOCK_REALTIME, &ts);
    149 
    150     ts.tv_sec  += delay_ms/1000;
    151     ts.tv_nsec += (delay_ms%1000) * 1000000;
    152     if (ts.tv_nsec >= 1000000000) {
    153         ts.tv_nsec -= 1000000000;
    154         ts.tv_sec  += 1;
    155     }
    156     cmd->ts = ts;
    157     list_add_tail(&tuner->command_list, &cmd->node);
    158     pthread_cond_signal(&tuner->cond);
    159     return 0;
    160 }
    161 
    162 #define BITMAP_FILE_PATH "/data/misc/audioserver/android.png"
    163 
    164 static int add_bitmap_metadata(radio_metadata_t **metadata, radio_metadata_key_t key,
    165                                const char *source)
    166 {
    167     int fd;
    168     ssize_t ret = 0;
    169     struct stat info;
    170     void *data = NULL;
    171     size_t size;
    172 
    173     fd = open(source, O_RDONLY);
    174     if (fd < 0)
    175         return -EPIPE;
    176 
    177     fstat(fd, &info);
    178     size = info.st_size;
    179     data = malloc(size);
    180     if (data == NULL) {
    181         ret = -ENOMEM;
    182         goto exit;
    183     }
    184     ret = read(fd, data, size);
    185     if (ret < 0)
    186         goto exit;
    187     ret = radio_metadata_add_raw(metadata, key, (const unsigned char *)data, size);
    188 
    189 exit:
    190     close(fd);
    191     free(data);
    192     ALOGE_IF(ret != 0, "%s error %d", __func__, ret);
    193     return (int)ret;
    194 }
    195 
    196 static int prepare_metadata(struct stub_radio_tuner *tuner,
    197                             radio_metadata_t **metadata, bool program)
    198 {
    199     int ret = 0;
    200     char text[RADIO_STRING_LEN_MAX];
    201     struct timespec ts;
    202 
    203     if (metadata == NULL)
    204         return -EINVAL;
    205 
    206     if (*metadata != NULL)
    207         radio_metadata_deallocate(*metadata);
    208 
    209     *metadata = NULL;
    210 
    211     ret = radio_metadata_allocate(metadata, tuner->program.channel, 0);
    212     if (ret != 0)
    213         return ret;
    214 
    215     if (program) {
    216         ret = radio_metadata_add_int(metadata, RADIO_METADATA_KEY_RBDS_PTY, 5);
    217         if (ret != 0)
    218             goto exit;
    219         ret = radio_metadata_add_text(metadata, RADIO_METADATA_KEY_RDS_PS, "RockBand");
    220         if (ret != 0)
    221             goto exit;
    222         ret = add_bitmap_metadata(metadata, RADIO_METADATA_KEY_ICON, BITMAP_FILE_PATH);
    223         if (ret != 0)
    224             goto exit;
    225         ret = radio_metadata_add_clock(metadata, RADIO_METADATA_KEY_CLOCK, &hw_clock);
    226         if (ret != 0)
    227             goto exit;
    228     } else {
    229         ret = add_bitmap_metadata(metadata, RADIO_METADATA_KEY_ART, BITMAP_FILE_PATH);
    230         if (ret != 0)
    231             goto exit;
    232     }
    233 
    234     clock_gettime(CLOCK_REALTIME, &ts);
    235     snprintf(text, RADIO_STRING_LEN_MAX, "Artist %ld", ts.tv_sec % 10);
    236     ret = radio_metadata_add_text(metadata, RADIO_METADATA_KEY_ARTIST, text);
    237     if (ret != 0)
    238         goto exit;
    239 
    240     snprintf(text, RADIO_STRING_LEN_MAX, "Song %ld", ts.tv_nsec % 10);
    241     ret = radio_metadata_add_text(metadata, RADIO_METADATA_KEY_TITLE, text);
    242     if (ret != 0)
    243         goto exit;
    244 
    245     return 0;
    246 
    247 exit:
    248     radio_metadata_deallocate(*metadata);
    249     *metadata = NULL;
    250     return ret;
    251 }
    252 
    253 static void *callback_thread_loop(void *context)
    254 {
    255     struct stub_radio_tuner *tuner = (struct stub_radio_tuner *)context;
    256     struct timespec ts = {0, 0};
    257 
    258     ALOGI("%s", __func__);
    259 
    260     prctl(PR_SET_NAME, (unsigned long)"sound trigger callback", 0, 0, 0);
    261 
    262     pthread_mutex_lock(&tuner->lock);
    263 
    264     // Fields which are used to toggle the state of traffic announcements and
    265     // ea announcements at random. They are access protected by tuner->lock.
    266     bool ea_state = false;
    267 
    268     while (true) {
    269         struct thread_command *cmd = NULL;
    270         struct listnode *item;
    271         struct listnode *tmp;
    272         struct timespec cur_ts;
    273         bool got_cancel = false;
    274         bool send_meta_data = false;
    275 
    276         if (list_empty(&tuner->command_list) || ts.tv_sec != 0) {
    277             ALOGV("%s SLEEPING", __func__);
    278             if (ts.tv_sec != 0) {
    279                 ALOGV("%s SLEEPING with timeout", __func__);
    280                 pthread_cond_timedwait(&tuner->cond, &tuner->lock, &ts);
    281             } else {
    282                 ALOGV("%s SLEEPING forever", __func__);
    283                 pthread_cond_wait(&tuner->cond, &tuner->lock);
    284             }
    285             ts.tv_sec = 0;
    286             ALOGV("%s RUNNING", __func__);
    287         }
    288 
    289         clock_gettime(CLOCK_REALTIME, &cur_ts);
    290 
    291         list_for_each_safe(item, tmp, &tuner->command_list) {
    292             cmd = node_to_item(item, struct thread_command, node);
    293 
    294             if (got_cancel && (cmd->type == CMD_STEP || cmd->type == CMD_SCAN ||
    295                     cmd->type == CMD_TUNE || cmd->type == CMD_METADATA ||
    296                     cmd->type == CMD_ANNOUNCEMENTS)) {
    297                  list_remove(item);
    298                  free(cmd);
    299                  continue;
    300             }
    301 
    302             if ((cmd->ts.tv_sec < cur_ts.tv_sec) ||
    303                     ((cmd->ts.tv_sec == cur_ts.tv_sec) && (cmd->ts.tv_nsec < cur_ts.tv_nsec))) {
    304                 radio_hal_event_t event;
    305                 radio_metadata_t *metadata = NULL;
    306 
    307                 event.type = RADIO_EVENT_HW_FAILURE;
    308                 list_remove(item);
    309 
    310                 ALOGV("%s processing command %d time %ld.%ld", __func__, cmd->type, cmd->ts.tv_sec,
    311                       cmd->ts.tv_nsec);
    312 
    313                 switch (cmd->type) {
    314                 default:
    315                 case CMD_EXIT:
    316                     free(cmd);
    317                     goto exit;
    318 
    319                 case CMD_CONFIG: {
    320                     tuner->config = cmd->config;
    321                     event.type = RADIO_EVENT_CONFIG;
    322                     event.config = tuner->config;
    323                     ALOGV("%s CMD_CONFIG type %d low %d up %d",
    324                           __func__, tuner->config.type,
    325                           tuner->config.lower_limit, tuner->config.upper_limit);
    326                     if (tuner->config.type == RADIO_BAND_FM) {
    327                         ALOGV("  - stereo %d\n  - rds %d\n  - ta %d\n  - af %d\n"
    328                               "  - ea %d\n",
    329                               tuner->config.fm.stereo, tuner->config.fm.rds,
    330                               tuner->config.fm.ta, tuner->config.fm.af,
    331                               tuner->config.fm.ea);
    332                     } else {
    333                         ALOGV("  - stereo %d", tuner->config.am.stereo);
    334                     }
    335                 } break;
    336 
    337                 case CMD_STEP: {
    338                     int frequency;
    339                     frequency = tuner->program.channel;
    340                     if (cmd->param == RADIO_DIRECTION_UP) {
    341                         frequency += tuner->config.spacings[0];
    342                     } else {
    343                         frequency -= tuner->config.spacings[0];
    344                     }
    345                     if (frequency > (int)tuner->config.upper_limit) {
    346                         frequency = tuner->config.lower_limit;
    347                     }
    348                     if (frequency < (int)tuner->config.lower_limit) {
    349                         frequency = tuner->config.upper_limit;
    350                     }
    351                     tuner->program.channel = frequency;
    352                     tuner->program.tuned  = (frequency / (tuner->config.spacings[0] * 5)) % 2;
    353                     tuner->program.signal_strength = 20;
    354                     if (tuner->config.type == RADIO_BAND_FM)
    355                         tuner->program.stereo = false;
    356                     else
    357                         tuner->program.stereo = false;
    358 
    359                     event.type = RADIO_EVENT_TUNED;
    360                     event.info = tuner->program;
    361                 } break;
    362 
    363                 case CMD_SCAN: {
    364                     int frequency;
    365                     frequency = tuner->program.channel;
    366                     if (cmd->param == RADIO_DIRECTION_UP) {
    367                         frequency += tuner->config.spacings[0] * 25;
    368                     } else {
    369                         frequency -= tuner->config.spacings[0] * 25;
    370                     }
    371                     if (frequency > (int)tuner->config.upper_limit) {
    372                         frequency = tuner->config.lower_limit;
    373                     }
    374                     if (frequency < (int)tuner->config.lower_limit) {
    375                         frequency = tuner->config.upper_limit;
    376                     }
    377                     tuner->program.channel = (unsigned int)frequency;
    378                     tuner->program.tuned  = true;
    379                     if (tuner->config.type == RADIO_BAND_FM)
    380                         tuner->program.stereo = tuner->config.fm.stereo;
    381                     else
    382                         tuner->program.stereo = tuner->config.am.stereo;
    383                     tuner->program.signal_strength = 50;
    384 
    385                     event.type = RADIO_EVENT_TUNED;
    386                     event.info = tuner->program;
    387                     send_meta_data = true;
    388                 } break;
    389 
    390                 case CMD_TUNE: {
    391                     tuner->program.channel = cmd->param;
    392                     tuner->program.tuned  = (tuner->program.channel /
    393                                                 (tuner->config.spacings[0] * 5)) % 2;
    394 
    395                     if (tuner->program.tuned) {
    396                         prepare_metadata(tuner, &tuner->program.metadata, true);
    397                         send_command_l(tuner, CMD_ANNOUNCEMENTS, 1000, NULL);
    398                     } else {
    399                         if (tuner->program.metadata != NULL)
    400                             radio_metadata_deallocate(tuner->program.metadata);
    401                         tuner->program.metadata = NULL;
    402                     }
    403                     tuner->program.signal_strength = 100;
    404                     if (tuner->config.type == RADIO_BAND_FM)
    405                         tuner->program.stereo =
    406                                 tuner->program.tuned ? tuner->config.fm.stereo : false;
    407                     else
    408                         tuner->program.stereo =
    409                             tuner->program.tuned ? tuner->config.am.stereo : false;
    410                     event.type = RADIO_EVENT_TUNED;
    411                     event.info = tuner->program;
    412                     send_meta_data = true;
    413                 } break;
    414 
    415                 case CMD_METADATA: {
    416                     int ret = prepare_metadata(tuner, &metadata, false);
    417                     if (ret == 0) {
    418                         event.type = RADIO_EVENT_METADATA;
    419                         event.metadata = metadata;
    420                     }
    421                     send_meta_data = true;
    422                 } break;
    423 
    424                 case CMD_CANCEL: {
    425                     got_cancel = true;
    426                 } break;
    427 
    428                 // Fire emergency announcements if they are enabled in the config. Stub
    429                 // implementation simply fires an announcement for 5 second
    430                 // duration with a gap of 5 seconds.
    431                 case CMD_ANNOUNCEMENTS: {
    432                     ALOGV("In annoucements. %d %d %d\n",
    433                           ea_state, tuner->config.type, tuner->config.fm.ea);
    434                     if (tuner->config.type == RADIO_BAND_FM ||
    435                         tuner->config.type == RADIO_BAND_FM_HD) {
    436                         if (ea_state) {
    437                             ea_state = false;
    438                             event.type = RADIO_EVENT_EA;
    439                         } else if (tuner->config.fm.ea) {
    440                             ea_state = true;
    441                             event.type = RADIO_EVENT_EA;
    442                         }
    443                         event.on = ea_state;
    444 
    445                         if (tuner->config.fm.ea) {
    446                             send_command_l(tuner, CMD_ANNOUNCEMENTS, 5000, NULL);
    447                         }
    448                     }
    449                 } break;
    450                 }
    451                 if (event.type != RADIO_EVENT_HW_FAILURE && tuner->callback != NULL) {
    452                     pthread_mutex_unlock(&tuner->lock);
    453                     tuner->callback(&event, tuner->cookie);
    454                     pthread_mutex_lock(&tuner->lock);
    455                     if (event.type == RADIO_EVENT_METADATA && metadata != NULL) {
    456                         radio_metadata_deallocate(metadata);
    457                         metadata = NULL;
    458                     }
    459                 }
    460                 ALOGV("%s processed command %d", __func__, cmd->type);
    461                 free(cmd);
    462             } else {
    463                 if ((ts.tv_sec == 0) ||
    464                         (cmd->ts.tv_sec < ts.tv_sec) ||
    465                         ((cmd->ts.tv_sec == ts.tv_sec) && (cmd->ts.tv_nsec < ts.tv_nsec))) {
    466                     ts.tv_sec = cmd->ts.tv_sec;
    467                     ts.tv_nsec = cmd->ts.tv_nsec;
    468                 }
    469             }
    470         }
    471 
    472         if (send_meta_data) {
    473             list_for_each_safe(item, tmp, &tuner->command_list) {
    474                 cmd = node_to_item(item, struct thread_command, node);
    475                 if (cmd->type == CMD_METADATA) {
    476                     list_remove(item);
    477                     free(cmd);
    478                 }
    479             }
    480             send_command_l(tuner, CMD_METADATA, 1000, NULL);
    481         }
    482     }
    483 
    484 exit:
    485     pthread_mutex_unlock(&tuner->lock);
    486 
    487     ALOGV("%s Exiting", __func__);
    488 
    489     return NULL;
    490 }
    491 
    492 
    493 static int tuner_set_configuration(const struct radio_tuner *tuner,
    494                          const radio_hal_band_config_t *config)
    495 {
    496     struct stub_radio_tuner *stub_tuner = (struct stub_radio_tuner *)tuner;
    497     int status = 0;
    498 
    499     ALOGI("%s stub_tuner %p", __func__, stub_tuner);
    500     pthread_mutex_lock(&stub_tuner->lock);
    501     if (config == NULL) {
    502         status = -EINVAL;
    503         goto exit;
    504     }
    505     send_command_l(stub_tuner, CMD_CANCEL, 0, NULL);
    506     send_command_l(stub_tuner, CMD_CONFIG, 500, (void *)config);
    507 
    508 exit:
    509     pthread_mutex_unlock(&stub_tuner->lock);
    510     return status;
    511 }
    512 
    513 static int tuner_get_configuration(const struct radio_tuner *tuner,
    514                                    radio_hal_band_config_t *config)
    515 {
    516     struct stub_radio_tuner *stub_tuner = (struct stub_radio_tuner *)tuner;
    517     int status = 0;
    518     struct listnode *item;
    519     radio_hal_band_config_t *src_config;
    520 
    521     ALOGI("%s stub_tuner %p", __func__, stub_tuner);
    522     pthread_mutex_lock(&stub_tuner->lock);
    523     src_config = &stub_tuner->config;
    524 
    525     if (config == NULL) {
    526         status = -EINVAL;
    527         goto exit;
    528     }
    529     list_for_each(item, &stub_tuner->command_list) {
    530         struct thread_command *cmd = node_to_item(item, struct thread_command, node);
    531         if (cmd->type == CMD_CONFIG) {
    532             src_config = &cmd->config;
    533         }
    534     }
    535     *config = *src_config;
    536 
    537 exit:
    538     pthread_mutex_unlock(&stub_tuner->lock);
    539     return status;
    540 }
    541 
    542 static int tuner_step(const struct radio_tuner *tuner,
    543                      radio_direction_t direction, bool skip_sub_channel)
    544 {
    545     struct stub_radio_tuner *stub_tuner = (struct stub_radio_tuner *)tuner;
    546 
    547     ALOGI("%s stub_tuner %p direction %d, skip_sub_channel %d",
    548           __func__, stub_tuner, direction, skip_sub_channel);
    549 
    550     pthread_mutex_lock(&stub_tuner->lock);
    551     send_command_l(stub_tuner, CMD_STEP, 20, &direction);
    552     pthread_mutex_unlock(&stub_tuner->lock);
    553     return 0;
    554 }
    555 
    556 static int tuner_scan(const struct radio_tuner *tuner,
    557                      radio_direction_t direction, bool skip_sub_channel)
    558 {
    559     struct stub_radio_tuner *stub_tuner = (struct stub_radio_tuner *)tuner;
    560 
    561     ALOGI("%s stub_tuner %p direction %d, skip_sub_channel %d",
    562           __func__, stub_tuner, direction, skip_sub_channel);
    563 
    564     pthread_mutex_lock(&stub_tuner->lock);
    565     send_command_l(stub_tuner, CMD_SCAN, 200, &direction);
    566     pthread_mutex_unlock(&stub_tuner->lock);
    567     return 0;
    568 }
    569 
    570 static int tuner_tune(const struct radio_tuner *tuner,
    571                      unsigned int channel, unsigned int sub_channel)
    572 {
    573     struct stub_radio_tuner *stub_tuner = (struct stub_radio_tuner *)tuner;
    574 
    575     ALOGI("%s stub_tuner %p channel %d, sub_channel %d",
    576           __func__, stub_tuner, channel, sub_channel);
    577 
    578     pthread_mutex_lock(&stub_tuner->lock);
    579     if (channel < stub_tuner->config.lower_limit || channel > stub_tuner->config.upper_limit) {
    580         pthread_mutex_unlock(&stub_tuner->lock);
    581         ALOGI("%s channel out of range", __func__);
    582         return -EINVAL;
    583     }
    584     send_command_l(stub_tuner, CMD_TUNE, 100, &channel);
    585     pthread_mutex_unlock(&stub_tuner->lock);
    586     return 0;
    587 }
    588 
    589 static int tuner_cancel(const struct radio_tuner *tuner)
    590 {
    591     struct stub_radio_tuner *stub_tuner = (struct stub_radio_tuner *)tuner;
    592 
    593     ALOGI("%s stub_tuner %p", __func__, stub_tuner);
    594 
    595     pthread_mutex_lock(&stub_tuner->lock);
    596     send_command_l(stub_tuner, CMD_CANCEL, 0, NULL);
    597     pthread_mutex_unlock(&stub_tuner->lock);
    598     return 0;
    599 }
    600 
    601 static int tuner_get_program_information(const struct radio_tuner *tuner,
    602                                         radio_program_info_t *info)
    603 {
    604     struct stub_radio_tuner *stub_tuner = (struct stub_radio_tuner *)tuner;
    605     int status = 0;
    606     radio_metadata_t *metadata;
    607 
    608     ALOGI("%s stub_tuner %p", __func__, stub_tuner);
    609     pthread_mutex_lock(&stub_tuner->lock);
    610     if (info == NULL) {
    611         status = -EINVAL;
    612         goto exit;
    613     }
    614     metadata = info->metadata;
    615     *info = stub_tuner->program;
    616     info->metadata = metadata;
    617     if (metadata != NULL && stub_tuner->program.metadata != NULL)
    618         radio_metadata_add_metadata(&info->metadata, stub_tuner->program.metadata);
    619 
    620 exit:
    621     pthread_mutex_unlock(&stub_tuner->lock);
    622     return status;
    623 }
    624 
    625 static int rdev_get_properties(const struct radio_hw_device *dev,
    626                                 radio_hal_properties_t *properties)
    627 {
    628     struct stub_radio_device *rdev = (struct stub_radio_device *)dev;
    629 
    630     ALOGI("%s", __func__);
    631     if (properties == NULL)
    632         return -EINVAL;
    633     memcpy(properties, &hw_properties, sizeof(radio_hal_properties_t));
    634     return 0;
    635 }
    636 
    637 static int rdev_open_tuner(const struct radio_hw_device *dev,
    638                           const radio_hal_band_config_t *config,
    639                           bool audio,
    640                           radio_callback_t callback,
    641                           void *cookie,
    642                           const struct radio_tuner **tuner)
    643 {
    644     struct stub_radio_device *rdev = (struct stub_radio_device *)dev;
    645     int status = 0;
    646 
    647     ALOGI("%s rdev %p", __func__, rdev);
    648     pthread_mutex_lock(&rdev->lock);
    649 
    650     if (rdev->tuner != NULL) {
    651         status = -ENOSYS;
    652         goto exit;
    653     }
    654 
    655     if (config == NULL || callback == NULL || tuner == NULL) {
    656         status = -EINVAL;
    657         goto exit;
    658     }
    659 
    660     rdev->tuner = (struct stub_radio_tuner *)calloc(1, sizeof(struct stub_radio_tuner));
    661     if (rdev->tuner == NULL) {
    662         status = -ENOMEM;
    663         goto exit;
    664     }
    665 
    666     rdev->tuner->interface.set_configuration = tuner_set_configuration;
    667     rdev->tuner->interface.get_configuration = tuner_get_configuration;
    668     rdev->tuner->interface.scan = tuner_scan;
    669     rdev->tuner->interface.step = tuner_step;
    670     rdev->tuner->interface.tune = tuner_tune;
    671     rdev->tuner->interface.cancel = tuner_cancel;
    672     rdev->tuner->interface.get_program_information = tuner_get_program_information;
    673 
    674     rdev->tuner->audio = audio;
    675     rdev->tuner->callback = callback;
    676     rdev->tuner->cookie = cookie;
    677 
    678     rdev->tuner->dev = rdev;
    679 
    680     pthread_mutex_init(&rdev->tuner->lock, (const pthread_mutexattr_t *) NULL);
    681     pthread_cond_init(&rdev->tuner->cond, (const pthread_condattr_t *) NULL);
    682     pthread_create(&rdev->tuner->callback_thread, (const pthread_attr_t *) NULL,
    683                         callback_thread_loop, rdev->tuner);
    684     list_init(&rdev->tuner->command_list);
    685 
    686     pthread_mutex_lock(&rdev->tuner->lock);
    687     send_command_l(rdev->tuner, CMD_CONFIG, 500, (void *)config);
    688     pthread_mutex_unlock(&rdev->tuner->lock);
    689 
    690     *tuner = &rdev->tuner->interface;
    691 
    692 exit:
    693     pthread_mutex_unlock(&rdev->lock);
    694     ALOGI("%s DONE", __func__);
    695     return status;
    696 }
    697 
    698 static int rdev_close_tuner(const struct radio_hw_device *dev,
    699                             const struct radio_tuner *tuner)
    700 {
    701     struct stub_radio_device *rdev = (struct stub_radio_device *)dev;
    702     struct stub_radio_tuner *stub_tuner = (struct stub_radio_tuner *)tuner;
    703     int status = 0;
    704 
    705     ALOGI("%s tuner %p", __func__, tuner);
    706     pthread_mutex_lock(&rdev->lock);
    707 
    708     if (tuner == NULL) {
    709         status = -EINVAL;
    710         goto exit;
    711     }
    712 
    713     pthread_mutex_lock(&stub_tuner->lock);
    714     stub_tuner->callback = NULL;
    715     send_command_l(stub_tuner, CMD_EXIT, 0, NULL);
    716     pthread_mutex_unlock(&stub_tuner->lock);
    717     pthread_join(stub_tuner->callback_thread, (void **) NULL);
    718 
    719     if (stub_tuner->program.metadata != NULL)
    720         radio_metadata_deallocate(stub_tuner->program.metadata);
    721 
    722     free(stub_tuner);
    723     rdev->tuner = NULL;
    724 
    725 exit:
    726     pthread_mutex_unlock(&rdev->lock);
    727     return status;
    728 }
    729 
    730 static int rdev_close(hw_device_t *device)
    731 {
    732     struct stub_radio_device *rdev = (struct stub_radio_device *)device;
    733     if (rdev != NULL) {
    734         free(rdev->tuner);
    735     }
    736     free(rdev);
    737     return 0;
    738 }
    739 
    740 static int rdev_open(const hw_module_t* module, const char* name,
    741                      hw_device_t** device)
    742 {
    743     struct stub_radio_device *rdev;
    744     int ret;
    745 
    746     if (strcmp(name, RADIO_HARDWARE_DEVICE) != 0)
    747         return -EINVAL;
    748 
    749     rdev = calloc(1, sizeof(struct stub_radio_device));
    750     if (!rdev)
    751         return -ENOMEM;
    752 
    753     rdev->device.common.tag = HARDWARE_DEVICE_TAG;
    754     rdev->device.common.version = RADIO_DEVICE_API_VERSION_1_0;
    755     rdev->device.common.module = (struct hw_module_t *) module;
    756     rdev->device.common.close = rdev_close;
    757     rdev->device.get_properties = rdev_get_properties;
    758     rdev->device.open_tuner = rdev_open_tuner;
    759     rdev->device.close_tuner = rdev_close_tuner;
    760 
    761     pthread_mutex_init(&rdev->lock, (const pthread_mutexattr_t *) NULL);
    762 
    763     *device = &rdev->device.common;
    764 
    765     return 0;
    766 }
    767 
    768 
    769 static struct hw_module_methods_t hal_module_methods = {
    770     .open = rdev_open,
    771 };
    772 
    773 struct radio_module HAL_MODULE_INFO_SYM = {
    774     .common = {
    775         .tag = HARDWARE_MODULE_TAG,
    776         .module_api_version = RADIO_MODULE_API_VERSION_1_0,
    777         .hal_api_version = HARDWARE_HAL_API_VERSION,
    778         .id = RADIO_HARDWARE_MODULE_ID,
    779         .name = "Stub radio HAL",
    780         .author = "The Android Open Source Project",
    781         .methods = &hal_module_methods,
    782     },
    783 };
    784