summaryrefslogtreecommitdiffstats
path: root/modules/radio/radio_hw.c
blob: b1a4d26ca03e14400fc0de16b0acdb8b5eff3f90 (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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
/*
 * Copyright (C) 2015 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 "radio_hw_stub"
#define LOG_NDEBUG 0

#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <pthread.h>
#include <sys/prctl.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <cutils/log.h>
#include <cutils/list.h>
#include <system/radio.h>
#include <system/radio_metadata.h>
#include <hardware/hardware.h>
#include <hardware/radio.h>

static const radio_hal_properties_t hw_properties = {
    .class_id = RADIO_CLASS_AM_FM,
    .implementor = "The Android Open Source Project",
    .product = "Radio stub HAL",
    .version = "0.1",
    .serial = "0123456789",
    .num_tuners = 1,
    .num_audio_sources = 1,
    .supports_capture = false,
    .num_bands = 2,
    .bands = {
        {
            .type = RADIO_BAND_FM,
            .antenna_connected = false,
            .lower_limit = 87900,
            .upper_limit = 107900,
            .num_spacings = 1,
            .spacings = { 200 },
            .fm = {
                .deemphasis = RADIO_DEEMPHASIS_75,
                .stereo = true,
                .rds = RADIO_RDS_US,
                .ta = false,
                .af = false,
            }
        },
        {
            .type = RADIO_BAND_AM,
            .antenna_connected = true,
            .lower_limit = 540,
            .upper_limit = 1610,
            .num_spacings = 1,
            .spacings = { 10 },
            .am = {
                .stereo = true,
            }
        }
    }
};

struct stub_radio_tuner {
    struct radio_tuner interface;
    struct stub_radio_device *dev;
    radio_callback_t callback;
    void *cookie;
    radio_hal_band_config_t config;
    radio_program_info_t program;
    bool audio;
    pthread_t callback_thread;
    pthread_mutex_t lock;
    pthread_cond_t  cond;
    struct listnode command_list;
};

struct stub_radio_device {
    struct radio_hw_device device;
    struct stub_radio_tuner *tuner;
    pthread_mutex_t lock;
};


typedef enum {
    CMD_EXIT,
    CMD_CONFIG,
    CMD_STEP,
    CMD_SCAN,
    CMD_TUNE,
    CMD_CANCEL,
    CMD_METADATA,
} thread_cmd_type_t;

struct thread_command {
    struct listnode node;
    thread_cmd_type_t type;
    struct timespec ts;
    union {
        unsigned int param;
        radio_hal_band_config_t config;
    };
};

/* must be called with out->lock locked */
static int send_command_l(struct stub_radio_tuner *tuner,
                          thread_cmd_type_t type,
                          unsigned int delay_ms,
                          void *param)
{
    struct thread_command *cmd = (struct thread_command *)calloc(1, sizeof(struct thread_command));
    struct timespec ts;

    if (cmd == NULL)
        return -ENOMEM;

    ALOGV("%s %d delay_ms %d", __func__, type, delay_ms);

    cmd->type = type;
    if (param != NULL) {
        if (cmd->type == CMD_CONFIG) {
            cmd->config = *(radio_hal_band_config_t *)param;
            ALOGV("%s CMD_CONFIG type %d", __func__, cmd->config.type);
        } else
            cmd->param = *(unsigned int *)param;
    }

    clock_gettime(CLOCK_REALTIME, &ts);

    ts.tv_sec  += delay_ms/1000;
    ts.tv_nsec += (delay_ms%1000) * 1000000;
    if (ts.tv_nsec >= 1000000000) {
        ts.tv_nsec -= 1000000000;
        ts.tv_sec  += 1;
    }
    cmd->ts = ts;
    list_add_tail(&tuner->command_list, &cmd->node);
    pthread_cond_signal(&tuner->cond);
    return 0;
}

#define BITMAP_FILE_PATH "/data/misc/media/android.png"

static int add_bitmap_metadata(radio_metadata_t **metadata, radio_metadata_key_t key,
                               const char *source)
{
    int fd;
    ssize_t ret = 0;
    struct stat info;
    void *data = NULL;
    size_t size;

    fd = open(source, O_RDONLY);
    if (fd < 0)
        return -EPIPE;

    fstat(fd, &info);
    size = info.st_size;
    data = malloc(size);
    if (data == NULL) {
        ret = -ENOMEM;
        goto exit;
    }
    ret = read(fd, data, size);
    if (ret < 0)
        goto exit;
    ret = radio_metadata_add_raw(metadata, key, (const unsigned char *)data, size);

exit:
    close(fd);
    free(data);
    ALOGE_IF(ret != 0, "%s error %d", __func__, ret);
    return (int)ret;
}

static int prepare_metadata(struct stub_radio_tuner *tuner,
                            radio_metadata_t **metadata, bool program)
{
    int ret = 0;
    char text[RADIO_STRING_LEN_MAX];
    struct timespec ts;

    if (metadata == NULL)
        return -EINVAL;

    if (*metadata != NULL)
        radio_metadata_deallocate(*metadata);

    *metadata = NULL;

    ret = radio_metadata_allocate(metadata, tuner->program.channel, 0);
    if (ret != 0)
        return ret;

    if (program) {
        ret = radio_metadata_add_int(metadata, RADIO_METADATA_KEY_RBDS_PTY, 5);
        if (ret != 0)
            goto exit;
        ret = radio_metadata_add_text(metadata, RADIO_METADATA_KEY_RDS_PS, "RockBand");
        if (ret != 0)
            goto exit;
        ret = add_bitmap_metadata(metadata, RADIO_METADATA_KEY_ICON, BITMAP_FILE_PATH);
        if (ret != 0)
            goto exit;
    } else {
        ret = add_bitmap_metadata(metadata, RADIO_METADATA_KEY_ART, BITMAP_FILE_PATH);
        if (ret != 0)
            goto exit;
    }

    clock_gettime(CLOCK_REALTIME, &ts);
    snprintf(text, RADIO_STRING_LEN_MAX, "Artist %ld", ts.tv_sec % 10);
    ret = radio_metadata_add_text(metadata, RADIO_METADATA_KEY_ARTIST, text);
    if (ret != 0)
        goto exit;

    snprintf(text, RADIO_STRING_LEN_MAX, "Song %ld", ts.tv_nsec % 10);
    ret = radio_metadata_add_text(metadata, RADIO_METADATA_KEY_TITLE, text);
    if (ret != 0)
        goto exit;

    return 0;

exit:
    radio_metadata_deallocate(*metadata);
    *metadata = NULL;
    return ret;
}

static void *callback_thread_loop(void *context)
{
    struct stub_radio_tuner *tuner = (struct stub_radio_tuner *)context;
    struct timespec ts = {0, 0};

    ALOGI("%s", __func__);

    prctl(PR_SET_NAME, (unsigned long)"sound trigger callback", 0, 0, 0);

    pthread_mutex_lock(&tuner->lock);

    while (true) {
        struct thread_command *cmd = NULL;
        struct listnode *item;
        struct listnode *tmp;
        struct timespec cur_ts;

        if (list_empty(&tuner->command_list) || ts.tv_sec != 0) {
            ALOGV("%s SLEEPING", __func__);
            if (ts.tv_sec != 0) {
                ALOGV("%s SLEEPING with timeout", __func__);
                pthread_cond_timedwait(&tuner->cond, &tuner->lock, &ts);
            } else {
                ALOGV("%s SLEEPING forever", __func__);
                pthread_cond_wait(&tuner->cond, &tuner->lock);
            }
            ts.tv_sec = 0;
            ALOGV("%s RUNNING", __func__);
        }

        clock_gettime(CLOCK_REALTIME, &cur_ts);

        list_for_each_safe(item, tmp, &tuner->command_list) {
            cmd = node_to_item(item, struct thread_command, node);

            if ((cmd->ts.tv_sec < cur_ts.tv_sec) ||
                    ((cmd->ts.tv_sec == cur_ts.tv_sec) && (cmd->ts.tv_nsec < cur_ts.tv_nsec))) {
                radio_hal_event_t event;

                event.type = RADIO_EVENT_HW_FAILURE;
                list_remove(item);

                ALOGV("%s processing command %d time %ld.%ld", __func__, cmd->type, cmd->ts.tv_sec,
                      cmd->ts.tv_nsec);

                switch (cmd->type) {
                default:
                case CMD_EXIT:
                    free(cmd);
                    goto exit;

                case CMD_CONFIG: {
                    tuner->config = cmd->config;
                    event.type = RADIO_EVENT_CONFIG;
                    event.config = tuner->config;
                    ALOGV("%s CMD_CONFIG type %d low %d up %d",
                          __func__, tuner->config.type,
                          tuner->config.lower_limit, tuner->config.upper_limit);
                    if (tuner->config.type == RADIO_BAND_FM) {
                        ALOGV("  - stereo %d\n  - rds %d\n  - ta %d\n  - af %d",
                              tuner->config.fm.stereo, tuner->config.fm.rds,
                              tuner->config.fm.ta, tuner->config.fm.af);
                    } else {
                        ALOGV("  - stereo %d", tuner->config.am.stereo);
                    }
                } break;

                case CMD_STEP: {
                    int frequency;
                    frequency = tuner->program.channel;
                    if (cmd->param == RADIO_DIRECTION_UP) {
                        frequency += tuner->config.spacings[0];
                    } else {
                        frequency -= tuner->config.spacings[0];
                    }
                    if (frequency > (int)tuner->config.upper_limit) {
                        frequency = tuner->config.lower_limit;
                    }
                    if (frequency < (int)tuner->config.lower_limit) {
                        frequency = tuner->config.upper_limit;
                    }
                    tuner->program.channel = frequency;
                    tuner->program.tuned  = (frequency / (tuner->config.spacings[0] * 5)) % 2;
                    tuner->program.signal_strength = 20;
                    if (tuner->config.type == RADIO_BAND_FM)
                        tuner->program.stereo = false;
                    else
                        tuner->program.stereo = false;

                    event.type = RADIO_EVENT_TUNED;
                    event.info = tuner->program;
                } break;

                case CMD_SCAN: {
                    int frequency;
                    frequency = tuner->program.channel;
                    if (cmd->param == RADIO_DIRECTION_UP) {
                        frequency += tuner->config.spacings[0] * 25;
                    } else {
                        frequency -= tuner->config.spacings[0] * 25;
                    }
                    if (frequency > (int)tuner->config.upper_limit) {
                        frequency = tuner->config.lower_limit;
                    }
                    if (frequency < (int)tuner->config.lower_limit) {
                        frequency = tuner->config.upper_limit;
                    }
                    tuner->program.channel = (unsigned int)frequency;
                    tuner->program.tuned  = true;
                    if (tuner->config.type == RADIO_BAND_FM)
                        tuner->program.stereo = tuner->config.fm.stereo;
                    else
                        tuner->program.stereo = tuner->config.am.stereo;
                    tuner->program.signal_strength = 50;

                    event.type = RADIO_EVENT_TUNED;
                    event.info = tuner->program;
                    if (tuner->program.metadata != NULL)
                        radio_metadata_deallocate(tuner->program.metadata);
                    tuner->program.metadata = NULL;
                    send_command_l(tuner, CMD_METADATA, 2000, NULL);
                } break;

                case CMD_TUNE: {
                    tuner->program.channel = cmd->param;
                    tuner->program.tuned  = (tuner->program.channel /
                                                (tuner->config.spacings[0] * 5)) % 2;

                    if (tuner->program.tuned) {
                        prepare_metadata(tuner, &tuner->program.metadata, true);
                        send_command_l(tuner, CMD_METADATA, 5000, NULL);
                    } else {
                        if (tuner->program.metadata != NULL)
                            radio_metadata_deallocate(tuner->program.metadata);
                        tuner->program.metadata = NULL;
                    }
                    tuner->program.signal_strength = 100;
                    if (tuner->config.type == RADIO_BAND_FM)
                        tuner->program.stereo =
                                tuner->program.tuned ? tuner->config.fm.stereo : false;
                    else
                        tuner->program.stereo =
                            tuner->program.tuned ? tuner->config.am.stereo : false;
                    event.type = RADIO_EVENT_TUNED;
                    event.info = tuner->program;
                } break;

                case CMD_METADATA: {
                    prepare_metadata(tuner, &tuner->program.metadata, false);
                    event.type = RADIO_EVENT_METADATA;
                    event.metadata = tuner->program.metadata;
                } break;

                case CMD_CANCEL: {
                    struct listnode *tmp2;
                    list_for_each_safe(item, tmp2, &tuner->command_list) {
                        cmd = node_to_item(item, struct thread_command, node);
                        if (cmd->type == CMD_STEP || cmd->type == CMD_SCAN ||
                                cmd->type == CMD_TUNE || cmd->type == CMD_METADATA) {
                            list_remove(item);
                            free(cmd);
                        }
                    }
                } break;

                }
                if (event.type != RADIO_EVENT_HW_FAILURE && tuner->callback != NULL) {
                    pthread_mutex_unlock(&tuner->lock);
                    tuner->callback(&event, tuner->cookie);
                    pthread_mutex_lock(&tuner->lock);
                }
                ALOGV("%s processed command %d", __func__, cmd->type);
                free(cmd);
            } else {
                if ((ts.tv_sec == 0) ||
                        (cmd->ts.tv_sec < ts.tv_sec) ||
                        ((cmd->ts.tv_sec == ts.tv_sec) && (cmd->ts.tv_nsec < ts.tv_nsec))) {
                    ts.tv_sec = cmd->ts.tv_sec;
                    ts.tv_nsec = cmd->ts.tv_nsec;
                }
            }
        }
    }

exit:
    pthread_mutex_unlock(&tuner->lock);

    ALOGV("%s Exiting", __func__);

    return NULL;
}


static int tuner_set_configuration(const struct radio_tuner *tuner,
                         const radio_hal_band_config_t *config)
{
    struct stub_radio_tuner *stub_tuner = (struct stub_radio_tuner *)tuner;
    int status = 0;

    ALOGI("%s stub_tuner %p", __func__, stub_tuner);
    pthread_mutex_lock(&stub_tuner->lock);
    if (config == NULL) {
        status = -EINVAL;
        goto exit;
    }
    send_command_l(stub_tuner, CMD_CANCEL, 0, NULL);
    send_command_l(stub_tuner, CMD_CONFIG, 500, (void *)config);

exit:
    pthread_mutex_unlock(&stub_tuner->lock);
    return status;
}

static int tuner_get_configuration(const struct radio_tuner *tuner,
                         radio_hal_band_config_t *config)
{
    struct stub_radio_tuner *stub_tuner = (struct stub_radio_tuner *)tuner;
    int status = 0;
    struct listnode *item;
    radio_hal_band_config_t *src_config;

    ALOGI("%s stub_tuner %p", __func__, stub_tuner);
    pthread_mutex_lock(&stub_tuner->lock);
    src_config = &stub_tuner->config;

    if (config == NULL) {
        status = -EINVAL;
        goto exit;
    }
    list_for_each(item, &stub_tuner->command_list) {
        struct thread_command *cmd = node_to_item(item, struct thread_command, node);
        if (cmd->type == CMD_CONFIG) {
            src_config = &cmd->config;
        }
    }
    *config = *src_config;

exit:
    pthread_mutex_unlock(&stub_tuner->lock);
    return status;
}

static int tuner_step(const struct radio_tuner *tuner,
                     radio_direction_t direction, bool skip_sub_channel)
{
    struct stub_radio_tuner *stub_tuner = (struct stub_radio_tuner *)tuner;

    ALOGI("%s stub_tuner %p direction %d, skip_sub_channel %d",
          __func__, stub_tuner, direction, skip_sub_channel);

    pthread_mutex_lock(&stub_tuner->lock);
    send_command_l(stub_tuner, CMD_STEP, 20, &direction);
    pthread_mutex_unlock(&stub_tuner->lock);
    return 0;
}

static int tuner_scan(const struct radio_tuner *tuner,
                     radio_direction_t direction, bool skip_sub_channel)
{
    struct stub_radio_tuner *stub_tuner = (struct stub_radio_tuner *)tuner;

    ALOGI("%s stub_tuner %p direction %d, skip_sub_channel %d",
          __func__, stub_tuner, direction, skip_sub_channel);

    pthread_mutex_lock(&stub_tuner->lock);
    send_command_l(stub_tuner, CMD_SCAN, 200, &direction);
    pthread_mutex_unlock(&stub_tuner->lock);
    return 0;
}

static int tuner_tune(const struct radio_tuner *tuner,
                     unsigned int channel, unsigned int sub_channel)
{
    struct stub_radio_tuner *stub_tuner = (struct stub_radio_tuner *)tuner;

    ALOGI("%s stub_tuner %p channel %d, sub_channel %d",
          __func__, stub_tuner, channel, sub_channel);

    pthread_mutex_lock(&stub_tuner->lock);
    if (channel < stub_tuner->config.lower_limit || channel > stub_tuner->config.upper_limit) {
        pthread_mutex_unlock(&stub_tuner->lock);
        ALOGI("%s channel out of range", __func__);
        return -EINVAL;
    }
    send_command_l(stub_tuner, CMD_TUNE, 100, &channel);
    pthread_mutex_unlock(&stub_tuner->lock);
    return 0;
}

static int tuner_cancel(const struct radio_tuner *tuner)
{
    struct stub_radio_tuner *stub_tuner = (struct stub_radio_tuner *)tuner;

    ALOGI("%s stub_tuner %p", __func__, stub_tuner);

    pthread_mutex_lock(&stub_tuner->lock);
    send_command_l(stub_tuner, CMD_CANCEL, 0, NULL);
    pthread_mutex_unlock(&stub_tuner->lock);
    return 0;
}

static int tuner_get_program_information(const struct radio_tuner *tuner,
                                        radio_program_info_t *info)
{
    struct stub_radio_tuner *stub_tuner = (struct stub_radio_tuner *)tuner;
    int status = 0;
    radio_metadata_t *metadata;

    ALOGI("%s stub_tuner %p", __func__, stub_tuner);
    pthread_mutex_lock(&stub_tuner->lock);
    if (info == NULL) {
        status = -EINVAL;
        goto exit;
    }
    metadata = info->metadata;
    *info = stub_tuner->program;
    info->metadata = metadata;
    if (metadata != NULL)
        radio_metadata_add_metadata(&info->metadata, stub_tuner->program.metadata);

exit:
    pthread_mutex_unlock(&stub_tuner->lock);
    return status;
}

static int rdev_get_properties(const struct radio_hw_device *dev,
                                radio_hal_properties_t *properties)
{
    struct stub_radio_device *rdev = (struct stub_radio_device *)dev;

    ALOGI("%s", __func__);
    if (properties == NULL)
        return -EINVAL;
    memcpy(properties, &hw_properties, sizeof(radio_hal_properties_t));
    return 0;
}

static int rdev_open_tuner(const struct radio_hw_device *dev,
                          const radio_hal_band_config_t *config,
                          bool audio,
                          radio_callback_t callback,
                          void *cookie,
                          const struct radio_tuner **tuner)
{
    struct stub_radio_device *rdev = (struct stub_radio_device *)dev;
    int status = 0;

    ALOGI("%s rdev %p", __func__, rdev);
    pthread_mutex_lock(&rdev->lock);

    if (rdev->tuner != NULL) {
        status = -ENOSYS;
        goto exit;
    }

    if (config == NULL || callback == NULL || tuner == NULL) {
        status = -EINVAL;
        goto exit;
    }

    rdev->tuner = (struct stub_radio_tuner *)calloc(1, sizeof(struct stub_radio_tuner));
    if (rdev->tuner == NULL) {
        status = -ENOMEM;
        goto exit;
    }

    rdev->tuner->interface.set_configuration = tuner_set_configuration;
    rdev->tuner->interface.get_configuration = tuner_get_configuration;
    rdev->tuner->interface.scan = tuner_scan;
    rdev->tuner->interface.step = tuner_step;
    rdev->tuner->interface.tune = tuner_tune;
    rdev->tuner->interface.cancel = tuner_cancel;
    rdev->tuner->interface.get_program_information = tuner_get_program_information;

    rdev->tuner->audio = audio;
    rdev->tuner->callback = callback;
    rdev->tuner->cookie = cookie;

    rdev->tuner->dev = rdev;

    pthread_mutex_init(&rdev->tuner->lock, (const pthread_mutexattr_t *) NULL);
    pthread_cond_init(&rdev->tuner->cond, (const pthread_condattr_t *) NULL);
    pthread_create(&rdev->tuner->callback_thread, (const pthread_attr_t *) NULL,
                        callback_thread_loop, rdev->tuner);
    list_init(&rdev->tuner->command_list);

    pthread_mutex_lock(&rdev->tuner->lock);
    send_command_l(rdev->tuner, CMD_CONFIG, 500, (void *)config);
    pthread_mutex_unlock(&rdev->tuner->lock);

    *tuner = &rdev->tuner->interface;

exit:
    pthread_mutex_unlock(&rdev->lock);
    ALOGI("%s DONE", __func__);
    return status;
}

static int rdev_close_tuner(const struct radio_hw_device *dev,
                            const struct radio_tuner *tuner)
{
    struct stub_radio_device *rdev = (struct stub_radio_device *)dev;
    struct stub_radio_tuner *stub_tuner = (struct stub_radio_tuner *)tuner;
    int status = 0;

    ALOGI("%s tuner %p", __func__, tuner);
    pthread_mutex_lock(&rdev->lock);

    if (tuner == NULL) {
        status = -EINVAL;
        goto exit;
    }

    pthread_mutex_lock(&stub_tuner->lock);
    stub_tuner->callback = NULL;
    send_command_l(stub_tuner, CMD_EXIT, 0, NULL);
    pthread_mutex_unlock(&stub_tuner->lock);
    pthread_join(stub_tuner->callback_thread, (void **) NULL);

    if (stub_tuner->program.metadata != NULL)
        radio_metadata_deallocate(stub_tuner->program.metadata);

    free(stub_tuner);
    rdev->tuner = NULL;

exit:
    pthread_mutex_unlock(&rdev->lock);
    return status;
}

static int rdev_close(hw_device_t *device)
{
    struct stub_radio_device *rdev = (struct stub_radio_device *)device;
    if (rdev != NULL) {
        free(rdev->tuner);
    }
    free(rdev);
    return 0;
}

static int rdev_open(const hw_module_t* module, const char* name,
                     hw_device_t** device)
{
    struct stub_radio_device *rdev;
    int ret;

    if (strcmp(name, RADIO_HARDWARE_DEVICE) != 0)
        return -EINVAL;

    rdev = calloc(1, sizeof(struct stub_radio_device));
    if (!rdev)
        return -ENOMEM;

    rdev->device.common.tag = HARDWARE_DEVICE_TAG;
    rdev->device.common.version = RADIO_DEVICE_API_VERSION_1_0;
    rdev->device.common.module = (struct hw_module_t *) module;
    rdev->device.common.close = rdev_close;
    rdev->device.get_properties = rdev_get_properties;
    rdev->device.open_tuner = rdev_open_tuner;
    rdev->device.close_tuner = rdev_close_tuner;

    pthread_mutex_init(&rdev->lock, (const pthread_mutexattr_t *) NULL);

    *device = &rdev->device.common;

    return 0;
}


static struct hw_module_methods_t hal_module_methods = {
    .open = rdev_open,
};

struct radio_module HAL_MODULE_INFO_SYM = {
    .common = {
        .tag = HARDWARE_MODULE_TAG,
        .module_api_version = RADIO_MODULE_API_VERSION_1_0,
        .hal_api_version = HARDWARE_HAL_API_VERSION,
        .id = RADIO_HARDWARE_MODULE_ID,
        .name = "Stub radio HAL",
        .author = "The Android Open Source Project",
        .methods = &hal_module_methods,
    },
};