blob: a7dee75c15071542ab93367de8b1398e1586264a [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,
Eric Laurentde8e0132016-12-13 14:20:26 -080054 .antenna_connected = true,
Eric Laurent97d2ba62015-03-05 12:52:14 -080055 .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);
Eric Laurentc37e6fe2016-10-25 11:20:54 -0700196 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);
Eric Laurentc37e6fe2016-10-25 11:20:54 -0700216
Eric Laurent97d2ba62015-03-05 12:52:14 -0800217 if (ret != 0)
218 return ret;
219
220 if (program) {
221 ret = radio_metadata_add_int(metadata, RADIO_METADATA_KEY_RBDS_PTY, 5);
222 if (ret != 0)
223 goto exit;
224 ret = radio_metadata_add_text(metadata, RADIO_METADATA_KEY_RDS_PS, "RockBand");
225 if (ret != 0)
226 goto exit;
227 ret = add_bitmap_metadata(metadata, RADIO_METADATA_KEY_ICON, BITMAP_FILE_PATH);
Eric Laurentc37e6fe2016-10-25 11:20:54 -0700228 if (ret != 0 && ret != -EPIPE)
Eric Laurent97d2ba62015-03-05 12:52:14 -0800229 goto exit;
Sanket Agarwala83e0a82015-10-07 17:15:08 -0700230 ret = radio_metadata_add_clock(metadata, RADIO_METADATA_KEY_CLOCK, &hw_clock);
231 if (ret != 0)
232 goto exit;
Eric Laurent97d2ba62015-03-05 12:52:14 -0800233 } else {
234 ret = add_bitmap_metadata(metadata, RADIO_METADATA_KEY_ART, BITMAP_FILE_PATH);
Eric Laurentc37e6fe2016-10-25 11:20:54 -0700235 if (ret != 0 && ret != -EPIPE)
Eric Laurent97d2ba62015-03-05 12:52:14 -0800236 goto exit;
237 }
238
239 clock_gettime(CLOCK_REALTIME, &ts);
240 snprintf(text, RADIO_STRING_LEN_MAX, "Artist %ld", ts.tv_sec % 10);
241 ret = radio_metadata_add_text(metadata, RADIO_METADATA_KEY_ARTIST, text);
242 if (ret != 0)
243 goto exit;
244
245 snprintf(text, RADIO_STRING_LEN_MAX, "Song %ld", ts.tv_nsec % 10);
246 ret = radio_metadata_add_text(metadata, RADIO_METADATA_KEY_TITLE, text);
247 if (ret != 0)
248 goto exit;
249
250 return 0;
251
252exit:
253 radio_metadata_deallocate(*metadata);
254 *metadata = NULL;
255 return ret;
256}
257
258static void *callback_thread_loop(void *context)
259{
260 struct stub_radio_tuner *tuner = (struct stub_radio_tuner *)context;
261 struct timespec ts = {0, 0};
262
263 ALOGI("%s", __func__);
264
265 prctl(PR_SET_NAME, (unsigned long)"sound trigger callback", 0, 0, 0);
266
267 pthread_mutex_lock(&tuner->lock);
268
Sanket Agarwala83e0a82015-10-07 17:15:08 -0700269 // Fields which are used to toggle the state of traffic announcements and
270 // ea announcements at random. They are access protected by tuner->lock.
271 bool ea_state = false;
272
Eric Laurent97d2ba62015-03-05 12:52:14 -0800273 while (true) {
274 struct thread_command *cmd = NULL;
275 struct listnode *item;
276 struct listnode *tmp;
277 struct timespec cur_ts;
Eric Laurentacccf642015-04-23 19:11:36 -0700278 bool got_cancel = false;
279 bool send_meta_data = false;
Eric Laurent97d2ba62015-03-05 12:52:14 -0800280
281 if (list_empty(&tuner->command_list) || ts.tv_sec != 0) {
282 ALOGV("%s SLEEPING", __func__);
283 if (ts.tv_sec != 0) {
284 ALOGV("%s SLEEPING with timeout", __func__);
285 pthread_cond_timedwait(&tuner->cond, &tuner->lock, &ts);
286 } else {
287 ALOGV("%s SLEEPING forever", __func__);
288 pthread_cond_wait(&tuner->cond, &tuner->lock);
289 }
290 ts.tv_sec = 0;
291 ALOGV("%s RUNNING", __func__);
292 }
293
294 clock_gettime(CLOCK_REALTIME, &cur_ts);
295
296 list_for_each_safe(item, tmp, &tuner->command_list) {
297 cmd = node_to_item(item, struct thread_command, node);
298
Eric Laurentacccf642015-04-23 19:11:36 -0700299 if (got_cancel && (cmd->type == CMD_STEP || cmd->type == CMD_SCAN ||
Sanket Agarwala83e0a82015-10-07 17:15:08 -0700300 cmd->type == CMD_TUNE || cmd->type == CMD_METADATA ||
301 cmd->type == CMD_ANNOUNCEMENTS)) {
Eric Laurentacccf642015-04-23 19:11:36 -0700302 list_remove(item);
303 free(cmd);
304 continue;
305 }
306
Eric Laurent97d2ba62015-03-05 12:52:14 -0800307 if ((cmd->ts.tv_sec < cur_ts.tv_sec) ||
308 ((cmd->ts.tv_sec == cur_ts.tv_sec) && (cmd->ts.tv_nsec < cur_ts.tv_nsec))) {
309 radio_hal_event_t event;
Eric Laurentacccf642015-04-23 19:11:36 -0700310 radio_metadata_t *metadata = NULL;
Eric Laurent97d2ba62015-03-05 12:52:14 -0800311
312 event.type = RADIO_EVENT_HW_FAILURE;
313 list_remove(item);
314
315 ALOGV("%s processing command %d time %ld.%ld", __func__, cmd->type, cmd->ts.tv_sec,
316 cmd->ts.tv_nsec);
317
318 switch (cmd->type) {
319 default:
320 case CMD_EXIT:
321 free(cmd);
322 goto exit;
323
324 case CMD_CONFIG: {
325 tuner->config = cmd->config;
326 event.type = RADIO_EVENT_CONFIG;
327 event.config = tuner->config;
328 ALOGV("%s CMD_CONFIG type %d low %d up %d",
329 __func__, tuner->config.type,
330 tuner->config.lower_limit, tuner->config.upper_limit);
331 if (tuner->config.type == RADIO_BAND_FM) {
Sanket Agarwala83e0a82015-10-07 17:15:08 -0700332 ALOGV(" - stereo %d\n - rds %d\n - ta %d\n - af %d\n"
333 " - ea %d\n",
Eric Laurent97d2ba62015-03-05 12:52:14 -0800334 tuner->config.fm.stereo, tuner->config.fm.rds,
Sanket Agarwala83e0a82015-10-07 17:15:08 -0700335 tuner->config.fm.ta, tuner->config.fm.af,
336 tuner->config.fm.ea);
Eric Laurent97d2ba62015-03-05 12:52:14 -0800337 } else {
338 ALOGV(" - stereo %d", tuner->config.am.stereo);
339 }
340 } break;
341
342 case CMD_STEP: {
343 int frequency;
344 frequency = tuner->program.channel;
345 if (cmd->param == RADIO_DIRECTION_UP) {
346 frequency += tuner->config.spacings[0];
347 } else {
348 frequency -= tuner->config.spacings[0];
349 }
350 if (frequency > (int)tuner->config.upper_limit) {
351 frequency = tuner->config.lower_limit;
352 }
353 if (frequency < (int)tuner->config.lower_limit) {
354 frequency = tuner->config.upper_limit;
355 }
356 tuner->program.channel = frequency;
357 tuner->program.tuned = (frequency / (tuner->config.spacings[0] * 5)) % 2;
358 tuner->program.signal_strength = 20;
359 if (tuner->config.type == RADIO_BAND_FM)
360 tuner->program.stereo = false;
361 else
362 tuner->program.stereo = false;
Tomasz Wasilczyk3c7297a2017-01-06 14:19:52 -0800363 prepare_metadata(tuner, &tuner->program.metadata, tuner->program.tuned);
Eric Laurent97d2ba62015-03-05 12:52:14 -0800364
365 event.type = RADIO_EVENT_TUNED;
366 event.info = tuner->program;
367 } break;
368
369 case CMD_SCAN: {
370 int frequency;
371 frequency = tuner->program.channel;
372 if (cmd->param == RADIO_DIRECTION_UP) {
373 frequency += tuner->config.spacings[0] * 25;
374 } else {
375 frequency -= tuner->config.spacings[0] * 25;
376 }
377 if (frequency > (int)tuner->config.upper_limit) {
378 frequency = tuner->config.lower_limit;
379 }
380 if (frequency < (int)tuner->config.lower_limit) {
381 frequency = tuner->config.upper_limit;
382 }
383 tuner->program.channel = (unsigned int)frequency;
384 tuner->program.tuned = true;
385 if (tuner->config.type == RADIO_BAND_FM)
386 tuner->program.stereo = tuner->config.fm.stereo;
387 else
388 tuner->program.stereo = tuner->config.am.stereo;
389 tuner->program.signal_strength = 50;
Tomasz Wasilczyk3c7297a2017-01-06 14:19:52 -0800390 prepare_metadata(tuner, &tuner->program.metadata, tuner->program.tuned);
Eric Laurent97d2ba62015-03-05 12:52:14 -0800391
392 event.type = RADIO_EVENT_TUNED;
393 event.info = tuner->program;
Eric Laurentacccf642015-04-23 19:11:36 -0700394 send_meta_data = true;
Eric Laurent97d2ba62015-03-05 12:52:14 -0800395 } break;
396
397 case CMD_TUNE: {
398 tuner->program.channel = cmd->param;
399 tuner->program.tuned = (tuner->program.channel /
400 (tuner->config.spacings[0] * 5)) % 2;
401
402 if (tuner->program.tuned) {
Sanket Agarwala83e0a82015-10-07 17:15:08 -0700403 send_command_l(tuner, CMD_ANNOUNCEMENTS, 1000, NULL);
Eric Laurent97d2ba62015-03-05 12:52:14 -0800404 }
405 tuner->program.signal_strength = 100;
406 if (tuner->config.type == RADIO_BAND_FM)
407 tuner->program.stereo =
408 tuner->program.tuned ? tuner->config.fm.stereo : false;
409 else
410 tuner->program.stereo =
411 tuner->program.tuned ? tuner->config.am.stereo : false;
Tomasz Wasilczyk3c7297a2017-01-06 14:19:52 -0800412 prepare_metadata(tuner, &tuner->program.metadata, tuner->program.tuned);
413
Eric Laurent97d2ba62015-03-05 12:52:14 -0800414 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 }
Eric Laurent97d2ba62015-03-05 12:52:14 -0800425 } break;
426
427 case CMD_CANCEL: {
Eric Laurentacccf642015-04-23 19:11:36 -0700428 got_cancel = true;
Eric Laurent97d2ba62015-03-05 12:52:14 -0800429 } break;
430
Sanket Agarwala83e0a82015-10-07 17:15:08 -0700431 // Fire emergency announcements if they are enabled in the config. Stub
432 // implementation simply fires an announcement for 5 second
433 // duration with a gap of 5 seconds.
434 case CMD_ANNOUNCEMENTS: {
435 ALOGV("In annoucements. %d %d %d\n",
436 ea_state, tuner->config.type, tuner->config.fm.ea);
437 if (tuner->config.type == RADIO_BAND_FM ||
438 tuner->config.type == RADIO_BAND_FM_HD) {
439 if (ea_state) {
440 ea_state = false;
441 event.type = RADIO_EVENT_EA;
442 } else if (tuner->config.fm.ea) {
443 ea_state = true;
444 event.type = RADIO_EVENT_EA;
445 }
446 event.on = ea_state;
447
448 if (tuner->config.fm.ea) {
449 send_command_l(tuner, CMD_ANNOUNCEMENTS, 5000, NULL);
450 }
451 }
452 } break;
Eric Laurent97d2ba62015-03-05 12:52:14 -0800453 }
454 if (event.type != RADIO_EVENT_HW_FAILURE && tuner->callback != NULL) {
455 pthread_mutex_unlock(&tuner->lock);
456 tuner->callback(&event, tuner->cookie);
457 pthread_mutex_lock(&tuner->lock);
Eric Laurentacccf642015-04-23 19:11:36 -0700458 if (event.type == RADIO_EVENT_METADATA && metadata != NULL) {
459 radio_metadata_deallocate(metadata);
460 metadata = NULL;
461 }
Eric Laurent97d2ba62015-03-05 12:52:14 -0800462 }
463 ALOGV("%s processed command %d", __func__, cmd->type);
464 free(cmd);
465 } else {
466 if ((ts.tv_sec == 0) ||
467 (cmd->ts.tv_sec < ts.tv_sec) ||
468 ((cmd->ts.tv_sec == ts.tv_sec) && (cmd->ts.tv_nsec < ts.tv_nsec))) {
469 ts.tv_sec = cmd->ts.tv_sec;
470 ts.tv_nsec = cmd->ts.tv_nsec;
471 }
472 }
473 }
Eric Laurentacccf642015-04-23 19:11:36 -0700474
475 if (send_meta_data) {
476 list_for_each_safe(item, tmp, &tuner->command_list) {
477 cmd = node_to_item(item, struct thread_command, node);
478 if (cmd->type == CMD_METADATA) {
479 list_remove(item);
480 free(cmd);
481 }
482 }
Sanket Agarwala83e0a82015-10-07 17:15:08 -0700483 send_command_l(tuner, CMD_METADATA, 1000, NULL);
Eric Laurentacccf642015-04-23 19:11:36 -0700484 }
Eric Laurent97d2ba62015-03-05 12:52:14 -0800485 }
486
487exit:
488 pthread_mutex_unlock(&tuner->lock);
489
490 ALOGV("%s Exiting", __func__);
491
492 return NULL;
493}
494
495
496static int tuner_set_configuration(const struct radio_tuner *tuner,
497 const radio_hal_band_config_t *config)
498{
499 struct stub_radio_tuner *stub_tuner = (struct stub_radio_tuner *)tuner;
500 int status = 0;
501
502 ALOGI("%s stub_tuner %p", __func__, stub_tuner);
503 pthread_mutex_lock(&stub_tuner->lock);
504 if (config == NULL) {
505 status = -EINVAL;
506 goto exit;
507 }
Tomasz Wasilczykad78c8d2017-03-07 17:05:41 -0800508 if (config->lower_limit > config->upper_limit) {
509 status = -EINVAL;
510 goto exit;
511 }
Eric Laurent97d2ba62015-03-05 12:52:14 -0800512 send_command_l(stub_tuner, CMD_CANCEL, 0, NULL);
513 send_command_l(stub_tuner, CMD_CONFIG, 500, (void *)config);
514
515exit:
516 pthread_mutex_unlock(&stub_tuner->lock);
517 return status;
518}
519
520static int tuner_get_configuration(const struct radio_tuner *tuner,
Sanket Agarwala83e0a82015-10-07 17:15:08 -0700521 radio_hal_band_config_t *config)
Eric Laurent97d2ba62015-03-05 12:52:14 -0800522{
523 struct stub_radio_tuner *stub_tuner = (struct stub_radio_tuner *)tuner;
524 int status = 0;
525 struct listnode *item;
526 radio_hal_band_config_t *src_config;
527
528 ALOGI("%s stub_tuner %p", __func__, stub_tuner);
529 pthread_mutex_lock(&stub_tuner->lock);
530 src_config = &stub_tuner->config;
531
532 if (config == NULL) {
533 status = -EINVAL;
534 goto exit;
535 }
536 list_for_each(item, &stub_tuner->command_list) {
537 struct thread_command *cmd = node_to_item(item, struct thread_command, node);
538 if (cmd->type == CMD_CONFIG) {
539 src_config = &cmd->config;
540 }
541 }
542 *config = *src_config;
543
544exit:
545 pthread_mutex_unlock(&stub_tuner->lock);
546 return status;
547}
548
549static int tuner_step(const struct radio_tuner *tuner,
550 radio_direction_t direction, bool skip_sub_channel)
551{
552 struct stub_radio_tuner *stub_tuner = (struct stub_radio_tuner *)tuner;
553
554 ALOGI("%s stub_tuner %p direction %d, skip_sub_channel %d",
555 __func__, stub_tuner, direction, skip_sub_channel);
556
557 pthread_mutex_lock(&stub_tuner->lock);
558 send_command_l(stub_tuner, CMD_STEP, 20, &direction);
559 pthread_mutex_unlock(&stub_tuner->lock);
560 return 0;
561}
562
563static int tuner_scan(const struct radio_tuner *tuner,
564 radio_direction_t direction, bool skip_sub_channel)
565{
566 struct stub_radio_tuner *stub_tuner = (struct stub_radio_tuner *)tuner;
567
568 ALOGI("%s stub_tuner %p direction %d, skip_sub_channel %d",
569 __func__, stub_tuner, direction, skip_sub_channel);
570
571 pthread_mutex_lock(&stub_tuner->lock);
572 send_command_l(stub_tuner, CMD_SCAN, 200, &direction);
573 pthread_mutex_unlock(&stub_tuner->lock);
574 return 0;
575}
576
577static int tuner_tune(const struct radio_tuner *tuner,
578 unsigned int channel, unsigned int sub_channel)
579{
580 struct stub_radio_tuner *stub_tuner = (struct stub_radio_tuner *)tuner;
581
582 ALOGI("%s stub_tuner %p channel %d, sub_channel %d",
583 __func__, stub_tuner, channel, sub_channel);
584
585 pthread_mutex_lock(&stub_tuner->lock);
586 if (channel < stub_tuner->config.lower_limit || channel > stub_tuner->config.upper_limit) {
587 pthread_mutex_unlock(&stub_tuner->lock);
588 ALOGI("%s channel out of range", __func__);
589 return -EINVAL;
590 }
591 send_command_l(stub_tuner, CMD_TUNE, 100, &channel);
592 pthread_mutex_unlock(&stub_tuner->lock);
593 return 0;
594}
595
596static int tuner_cancel(const struct radio_tuner *tuner)
597{
598 struct stub_radio_tuner *stub_tuner = (struct stub_radio_tuner *)tuner;
599
600 ALOGI("%s stub_tuner %p", __func__, stub_tuner);
601
602 pthread_mutex_lock(&stub_tuner->lock);
603 send_command_l(stub_tuner, CMD_CANCEL, 0, NULL);
604 pthread_mutex_unlock(&stub_tuner->lock);
605 return 0;
606}
607
608static int tuner_get_program_information(const struct radio_tuner *tuner,
609 radio_program_info_t *info)
610{
611 struct stub_radio_tuner *stub_tuner = (struct stub_radio_tuner *)tuner;
612 int status = 0;
613 radio_metadata_t *metadata;
614
615 ALOGI("%s stub_tuner %p", __func__, stub_tuner);
616 pthread_mutex_lock(&stub_tuner->lock);
617 if (info == NULL) {
618 status = -EINVAL;
619 goto exit;
620 }
621 metadata = info->metadata;
622 *info = stub_tuner->program;
623 info->metadata = metadata;
Tomasz Wasilczyk33683bf2017-02-15 16:57:20 -0800624 if (metadata == NULL) {
625 ALOGE("%s metadata is a nullptr", __func__);
626 status = -EINVAL;
627 goto exit;
628 }
629 if (stub_tuner->program.metadata != NULL)
Eric Laurent97d2ba62015-03-05 12:52:14 -0800630 radio_metadata_add_metadata(&info->metadata, stub_tuner->program.metadata);
631
632exit:
633 pthread_mutex_unlock(&stub_tuner->lock);
634 return status;
635}
636
637static int rdev_get_properties(const struct radio_hw_device *dev,
638 radio_hal_properties_t *properties)
639{
640 struct stub_radio_device *rdev = (struct stub_radio_device *)dev;
641
642 ALOGI("%s", __func__);
643 if (properties == NULL)
644 return -EINVAL;
645 memcpy(properties, &hw_properties, sizeof(radio_hal_properties_t));
646 return 0;
647}
648
649static int rdev_open_tuner(const struct radio_hw_device *dev,
650 const radio_hal_band_config_t *config,
651 bool audio,
652 radio_callback_t callback,
653 void *cookie,
654 const struct radio_tuner **tuner)
655{
656 struct stub_radio_device *rdev = (struct stub_radio_device *)dev;
657 int status = 0;
658
659 ALOGI("%s rdev %p", __func__, rdev);
660 pthread_mutex_lock(&rdev->lock);
661
662 if (rdev->tuner != NULL) {
Tomasz Wasilczyk12ddfd02017-04-17 17:05:38 -0700663 ALOGE("Can't open tuner twice");
Eric Laurent97d2ba62015-03-05 12:52:14 -0800664 status = -ENOSYS;
665 goto exit;
666 }
667
668 if (config == NULL || callback == NULL || tuner == NULL) {
669 status = -EINVAL;
670 goto exit;
671 }
672
673 rdev->tuner = (struct stub_radio_tuner *)calloc(1, sizeof(struct stub_radio_tuner));
674 if (rdev->tuner == NULL) {
675 status = -ENOMEM;
676 goto exit;
677 }
678
679 rdev->tuner->interface.set_configuration = tuner_set_configuration;
680 rdev->tuner->interface.get_configuration = tuner_get_configuration;
681 rdev->tuner->interface.scan = tuner_scan;
682 rdev->tuner->interface.step = tuner_step;
683 rdev->tuner->interface.tune = tuner_tune;
684 rdev->tuner->interface.cancel = tuner_cancel;
685 rdev->tuner->interface.get_program_information = tuner_get_program_information;
686
687 rdev->tuner->audio = audio;
688 rdev->tuner->callback = callback;
689 rdev->tuner->cookie = cookie;
690
691 rdev->tuner->dev = rdev;
692
693 pthread_mutex_init(&rdev->tuner->lock, (const pthread_mutexattr_t *) NULL);
694 pthread_cond_init(&rdev->tuner->cond, (const pthread_condattr_t *) NULL);
695 pthread_create(&rdev->tuner->callback_thread, (const pthread_attr_t *) NULL,
696 callback_thread_loop, rdev->tuner);
697 list_init(&rdev->tuner->command_list);
698
699 pthread_mutex_lock(&rdev->tuner->lock);
700 send_command_l(rdev->tuner, CMD_CONFIG, 500, (void *)config);
701 pthread_mutex_unlock(&rdev->tuner->lock);
702
703 *tuner = &rdev->tuner->interface;
704
705exit:
706 pthread_mutex_unlock(&rdev->lock);
707 ALOGI("%s DONE", __func__);
708 return status;
709}
710
711static int rdev_close_tuner(const struct radio_hw_device *dev,
712 const struct radio_tuner *tuner)
713{
714 struct stub_radio_device *rdev = (struct stub_radio_device *)dev;
715 struct stub_radio_tuner *stub_tuner = (struct stub_radio_tuner *)tuner;
716 int status = 0;
717
718 ALOGI("%s tuner %p", __func__, tuner);
719 pthread_mutex_lock(&rdev->lock);
720
721 if (tuner == NULL) {
722 status = -EINVAL;
723 goto exit;
724 }
725
726 pthread_mutex_lock(&stub_tuner->lock);
727 stub_tuner->callback = NULL;
728 send_command_l(stub_tuner, CMD_EXIT, 0, NULL);
729 pthread_mutex_unlock(&stub_tuner->lock);
730 pthread_join(stub_tuner->callback_thread, (void **) NULL);
731
732 if (stub_tuner->program.metadata != NULL)
733 radio_metadata_deallocate(stub_tuner->program.metadata);
734
735 free(stub_tuner);
736 rdev->tuner = NULL;
737
738exit:
739 pthread_mutex_unlock(&rdev->lock);
740 return status;
741}
742
743static int rdev_close(hw_device_t *device)
744{
745 struct stub_radio_device *rdev = (struct stub_radio_device *)device;
746 if (rdev != NULL) {
747 free(rdev->tuner);
748 }
749 free(rdev);
750 return 0;
751}
752
753static int rdev_open(const hw_module_t* module, const char* name,
754 hw_device_t** device)
755{
756 struct stub_radio_device *rdev;
757 int ret;
758
759 if (strcmp(name, RADIO_HARDWARE_DEVICE) != 0)
760 return -EINVAL;
761
762 rdev = calloc(1, sizeof(struct stub_radio_device));
763 if (!rdev)
764 return -ENOMEM;
765
766 rdev->device.common.tag = HARDWARE_DEVICE_TAG;
767 rdev->device.common.version = RADIO_DEVICE_API_VERSION_1_0;
768 rdev->device.common.module = (struct hw_module_t *) module;
769 rdev->device.common.close = rdev_close;
770 rdev->device.get_properties = rdev_get_properties;
771 rdev->device.open_tuner = rdev_open_tuner;
772 rdev->device.close_tuner = rdev_close_tuner;
773
774 pthread_mutex_init(&rdev->lock, (const pthread_mutexattr_t *) NULL);
775
776 *device = &rdev->device.common;
777
778 return 0;
779}
780
781
782static struct hw_module_methods_t hal_module_methods = {
783 .open = rdev_open,
784};
785
786struct radio_module HAL_MODULE_INFO_SYM = {
787 .common = {
788 .tag = HARDWARE_MODULE_TAG,
789 .module_api_version = RADIO_MODULE_API_VERSION_1_0,
790 .hal_api_version = HARDWARE_HAL_API_VERSION,
791 .id = RADIO_HARDWARE_MODULE_ID,
792 .name = "Stub radio HAL",
793 .author = "The Android Open Source Project",
794 .methods = &hal_module_methods,
795 },
796};