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