17ec681f3Smrg/*
27ec681f3Smrg * Copyright (C) 2015 The Android Open Source Project
37ec681f3Smrg *
47ec681f3Smrg * Licensed under the Apache License, Version 2.0 (the "License");
57ec681f3Smrg * you may not use this file except in compliance with the License.
67ec681f3Smrg * You may obtain a copy of the License at
77ec681f3Smrg *
87ec681f3Smrg *      http://www.apache.org/licenses/LICENSE-2.0
97ec681f3Smrg *
107ec681f3Smrg * Unless required by applicable law or agreed to in writing, software
117ec681f3Smrg * distributed under the License is distributed on an "AS IS" BASIS,
127ec681f3Smrg * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
137ec681f3Smrg * See the License for the specific language governing permissions and
147ec681f3Smrg * limitations under the License.
157ec681f3Smrg */
167ec681f3Smrg
177ec681f3Smrg#ifndef ANDROID_RADIO_H
187ec681f3Smrg#define ANDROID_RADIO_H
197ec681f3Smrg
207ec681f3Smrg#include <stdbool.h>
217ec681f3Smrg#include <stdint.h>
227ec681f3Smrg#include <stdio.h>
237ec681f3Smrg#include <sys/cdefs.h>
247ec681f3Smrg#include <sys/types.h>
257ec681f3Smrg
267ec681f3Smrg
277ec681f3Smrg#define RADIO_NUM_BANDS_MAX     16
287ec681f3Smrg#define RADIO_NUM_SPACINGS_MAX  16
297ec681f3Smrg#define RADIO_STRING_LEN_MAX    128
307ec681f3Smrg
317ec681f3Smrg/*
327ec681f3Smrg * Radio hardware module class. A given radio hardware module HAL is of one class
337ec681f3Smrg * only. The platform can not have more than one hardware module of each class.
347ec681f3Smrg * Current version of the framework only supports RADIO_CLASS_AM_FM.
357ec681f3Smrg */
367ec681f3Smrgtypedef enum {
377ec681f3Smrg    RADIO_CLASS_AM_FM = 0,  /* FM (including HD radio) and AM */
387ec681f3Smrg    RADIO_CLASS_SAT   = 1,  /* Satellite Radio */
397ec681f3Smrg    RADIO_CLASS_DT    = 2,  /* Digital Radio (DAB) */
407ec681f3Smrg} radio_class_t;
417ec681f3Smrg
427ec681f3Smrg/* value for field "type" of radio band described in struct radio_hal_band_config */
437ec681f3Smrgtypedef enum {
447ec681f3Smrg    RADIO_BAND_AM     = 0,  /* Amplitude Modulation band: LW, MW, SW */
457ec681f3Smrg    RADIO_BAND_FM     = 1,  /* Frequency Modulation band: FM */
467ec681f3Smrg    RADIO_BAND_FM_HD  = 2,  /* FM HD Radio / DRM (IBOC) */
477ec681f3Smrg    RADIO_BAND_AM_HD  = 3,  /* AM HD Radio / DRM (IBOC) */
487ec681f3Smrg} radio_band_t;
497ec681f3Smrg
507ec681f3Smrg/* RDS variant implemented. A struct radio_hal_fm_band_config can list none or several. */
517ec681f3Smrgenum {
527ec681f3Smrg    RADIO_RDS_NONE   = 0x0,
537ec681f3Smrg    RADIO_RDS_WORLD  = 0x01,
547ec681f3Smrg    RADIO_RDS_US     = 0x02,
557ec681f3Smrg};
567ec681f3Smrgtypedef unsigned int radio_rds_t;
577ec681f3Smrg
587ec681f3Smrg/* FM deemphasis variant implemented. A struct radio_hal_fm_band_config can list one or more. */
597ec681f3Smrgenum {
607ec681f3Smrg    RADIO_DEEMPHASIS_50   = 0x1,
617ec681f3Smrg    RADIO_DEEMPHASIS_75   = 0x2,
627ec681f3Smrg};
637ec681f3Smrgtypedef unsigned int radio_deemphasis_t;
647ec681f3Smrg
657ec681f3Smrg/* Region a particular radio band configuration corresponds to. Not used at the HAL.
667ec681f3Smrg * Derived by the framework when converting the band descriptors retrieved from the HAL to
677ec681f3Smrg * individual band descriptors for each supported region. */
687ec681f3Smrgtypedef enum {
697ec681f3Smrg    RADIO_REGION_NONE  = -1,
707ec681f3Smrg    RADIO_REGION_ITU_1 = 0,
717ec681f3Smrg    RADIO_REGION_ITU_2 = 1,
727ec681f3Smrg    RADIO_REGION_OIRT  = 2,
737ec681f3Smrg    RADIO_REGION_JAPAN = 3,
747ec681f3Smrg    RADIO_REGION_KOREA = 4,
757ec681f3Smrg} radio_region_t;
767ec681f3Smrg
777ec681f3Smrg/* scanning direction for scan() and step() tuner APIs */
787ec681f3Smrgtypedef enum {
797ec681f3Smrg    RADIO_DIRECTION_UP,
807ec681f3Smrg    RADIO_DIRECTION_DOWN
817ec681f3Smrg} radio_direction_t;
827ec681f3Smrg
837ec681f3Smrg/* unique handle allocated to a radio module */
847ec681f3Smrgtypedef uint32_t radio_handle_t;
857ec681f3Smrg
867ec681f3Smrg/* Opaque meta data structure used by radio meta data API (see system/radio_metadata.h) */
877ec681f3Smrgtypedef struct radio_metadata radio_metadata_t;
887ec681f3Smrg
897ec681f3Smrg
907ec681f3Smrg/* Additional attributes for an FM band configuration */
917ec681f3Smrgtypedef struct radio_hal_fm_band_config {
927ec681f3Smrg    radio_deemphasis_t  deemphasis; /* deemphasis variant */
937ec681f3Smrg    bool                stereo;     /* stereo supported */
947ec681f3Smrg    radio_rds_t         rds;        /* RDS variants supported */
957ec681f3Smrg    bool                ta;         /* Traffic Announcement supported */
967ec681f3Smrg    bool                af;         /* Alternate Frequency supported */
977ec681f3Smrg    bool                ea;         /* Emergency announcements supported */
987ec681f3Smrg} radio_hal_fm_band_config_t;
997ec681f3Smrg
1007ec681f3Smrg/* Additional attributes for an AM band configuration */
1017ec681f3Smrgtypedef struct radio_hal_am_band_config {
1027ec681f3Smrg    bool                stereo;     /* stereo supported */
1037ec681f3Smrg} radio_hal_am_band_config_t;
1047ec681f3Smrg
1057ec681f3Smrg/* Radio band configuration. Describes a given band supported by the radio module.
1067ec681f3Smrg * The HAL can expose only one band per type with the the maximum range supported and all options.
1077ec681f3Smrg * THe framework will derive the actual regions were this module can operate and expose separate
1087ec681f3Smrg * band configurations for applications to chose from. */
1097ec681f3Smrgtypedef struct radio_hal_band_config {
1107ec681f3Smrg    radio_band_t type;
1117ec681f3Smrg    bool         antenna_connected;
1127ec681f3Smrg    uint32_t     lower_limit;
1137ec681f3Smrg    uint32_t     upper_limit;
1147ec681f3Smrg    uint32_t     num_spacings;
1157ec681f3Smrg    uint32_t     spacings[RADIO_NUM_SPACINGS_MAX];
1167ec681f3Smrg    union {
1177ec681f3Smrg        radio_hal_fm_band_config_t fm;
1187ec681f3Smrg        radio_hal_am_band_config_t am;
1197ec681f3Smrg    };
1207ec681f3Smrg} radio_hal_band_config_t;
1217ec681f3Smrg
1227ec681f3Smrg/* Used internally by the framework to represent a band for s specific region */
1237ec681f3Smrgtypedef struct radio_band_config {
1247ec681f3Smrg    radio_region_t  region;
1257ec681f3Smrg    radio_hal_band_config_t band;
1267ec681f3Smrg} radio_band_config_t;
1277ec681f3Smrg
1287ec681f3Smrg
1297ec681f3Smrg/* Exposes properties of a given hardware radio module.
1307ec681f3Smrg * NOTE: current framework implementation supports only one audio source (num_audio_sources = 1).
1317ec681f3Smrg * The source corresponds to AUDIO_DEVICE_IN_FM_TUNER.
1327ec681f3Smrg * If more than one tuner is supported (num_tuners > 1), only one can be connected to the audio
1337ec681f3Smrg * source. */
1347ec681f3Smrgtypedef struct radio_hal_properties {
1357ec681f3Smrg    radio_class_t   class_id;   /* Class of this module. E.g RADIO_CLASS_AM_FM */
1367ec681f3Smrg    char            implementor[RADIO_STRING_LEN_MAX];  /* implementor name */
1377ec681f3Smrg    char            product[RADIO_STRING_LEN_MAX];  /* product name */
1387ec681f3Smrg    char            version[RADIO_STRING_LEN_MAX];  /* product version */
1397ec681f3Smrg    char            serial[RADIO_STRING_LEN_MAX];  /* serial number (for subscription services) */
1407ec681f3Smrg    uint32_t        num_tuners;     /* number of tuners controllable independently */
1417ec681f3Smrg    uint32_t        num_audio_sources; /* number of audio sources driven simultaneously */
1427ec681f3Smrg    bool            supports_capture; /* the hardware supports capture of audio source audio HAL */
1437ec681f3Smrg    uint32_t        num_bands;      /* number of band descriptors */
1447ec681f3Smrg    radio_hal_band_config_t bands[RADIO_NUM_BANDS_MAX]; /* band descriptors */
1457ec681f3Smrg} radio_hal_properties_t;
1467ec681f3Smrg
1477ec681f3Smrg/* Used internally by the framework. Same information as in struct radio_hal_properties plus a
1487ec681f3Smrg * unique handle and one band configuration per region. */
1497ec681f3Smrgtypedef struct radio_properties {
1507ec681f3Smrg    radio_handle_t      handle;
1517ec681f3Smrg    radio_class_t       class_id;
1527ec681f3Smrg    char                implementor[RADIO_STRING_LEN_MAX];
1537ec681f3Smrg    char                product[RADIO_STRING_LEN_MAX];
1547ec681f3Smrg    char                version[RADIO_STRING_LEN_MAX];
1557ec681f3Smrg    char                serial[RADIO_STRING_LEN_MAX];
1567ec681f3Smrg    uint32_t            num_tuners;
1577ec681f3Smrg    uint32_t            num_audio_sources;
1587ec681f3Smrg    bool                supports_capture;
1597ec681f3Smrg    uint32_t            num_bands;
1607ec681f3Smrg    radio_band_config_t bands[RADIO_NUM_BANDS_MAX];
1617ec681f3Smrg} radio_properties_t;
1627ec681f3Smrg
1637ec681f3Smrg/* Radio program information. Returned by the HAL with event RADIO_EVENT_TUNED.
1647ec681f3Smrg * Contains information on currently tuned channel.
1657ec681f3Smrg */
1667ec681f3Smrgtypedef struct radio_program_info {
1677ec681f3Smrg    uint32_t         channel;   /* current channel. (e.g kHz for band type RADIO_BAND_FM) */
1687ec681f3Smrg    uint32_t         sub_channel; /* current sub channel. (used for RADIO_BAND_FM_HD) */
1697ec681f3Smrg    bool             tuned;     /* tuned to a program or not */
1707ec681f3Smrg    bool             stereo;    /* program is stereo or not */
1717ec681f3Smrg    bool             digital;   /* digital program or not (e.g HD Radio program) */
1727ec681f3Smrg    uint32_t         signal_strength; /* signal strength from 0 to 100 */
1737ec681f3Smrg                                /* meta data (e.g PTY, song title ...), must not be NULL */
1747ec681f3Smrg    __attribute__((aligned(8))) radio_metadata_t *metadata;
1757ec681f3Smrg} radio_program_info_t;
1767ec681f3Smrg
1777ec681f3Smrg
1787ec681f3Smrg/* Events sent to the framework via the HAL callback. An event can notify the completion of an
1797ec681f3Smrg * asynchronous command (configuration, tune, scan ...) or a spontaneous change (antenna connection,
1807ec681f3Smrg * failure, AF switching, meta data reception... */
1817ec681f3Smrgenum {
1827ec681f3Smrg    RADIO_EVENT_HW_FAILURE  = 0,  /* hardware module failure. Requires reopening the tuner */
1837ec681f3Smrg    RADIO_EVENT_CONFIG      = 1,  /* configuration change completed */
1847ec681f3Smrg    RADIO_EVENT_ANTENNA     = 2,  /* Antenna connected, disconnected */
1857ec681f3Smrg    RADIO_EVENT_TUNED       = 3,  /* tune, step, scan completed */
1867ec681f3Smrg    RADIO_EVENT_METADATA    = 4,  /* New meta data received */
1877ec681f3Smrg    RADIO_EVENT_TA          = 5,  /* Traffic announcement start or stop */
1887ec681f3Smrg    RADIO_EVENT_AF_SWITCH   = 6,  /* Switch to Alternate Frequency */
1897ec681f3Smrg    RADIO_EVENT_EA          = 7,  /* Emergency announcement start or stop */
1907ec681f3Smrg    // begin framework only events
1917ec681f3Smrg    RADIO_EVENT_CONTROL     = 100, /* loss/gain of tuner control */
1927ec681f3Smrg    RADIO_EVENT_SERVER_DIED = 101, /* radio service died */
1937ec681f3Smrg};
1947ec681f3Smrgtypedef unsigned int radio_event_type_t;
1957ec681f3Smrg
1967ec681f3Smrg/* Event passed to the framework by the HAL callback */
1977ec681f3Smrgtypedef struct radio_hal_event {
1987ec681f3Smrg    radio_event_type_t  type;       /* event type */
1997ec681f3Smrg    int32_t             status;     /* used by RADIO_EVENT_CONFIG, RADIO_EVENT_TUNED */
2007ec681f3Smrg    union {
2017ec681f3Smrg        /* RADIO_EVENT_ANTENNA, RADIO_EVENT_TA, RADIO_EVENT_EA */
2027ec681f3Smrg        bool                    on;
2037ec681f3Smrg        radio_hal_band_config_t config; /* RADIO_EVENT_CONFIG */
2047ec681f3Smrg        radio_program_info_t    info;   /* RADIO_EVENT_TUNED, RADIO_EVENT_AF_SWITCH */
2057ec681f3Smrg        radio_metadata_t        *metadata; /* RADIO_EVENT_METADATA */
2067ec681f3Smrg    };
2077ec681f3Smrg} radio_hal_event_t;
2087ec681f3Smrg
2097ec681f3Smrg/* Used internally by the framework. Same information as in struct radio_hal_event */
2107ec681f3Smrgtypedef struct radio_event {
2117ec681f3Smrg    radio_event_type_t  type;
2127ec681f3Smrg    int32_t             status;
2137ec681f3Smrg    union {
2147ec681f3Smrg        bool                    on;
2157ec681f3Smrg        radio_band_config_t     config;
2167ec681f3Smrg        radio_program_info_t    info;
2177ec681f3Smrg                                /* meta data (e.g PTY, song title ...), must not be NULL */
2187ec681f3Smrg        __attribute__((aligned(8))) radio_metadata_t *metadata;
2197ec681f3Smrg    };
2207ec681f3Smrg} radio_event_t;
2217ec681f3Smrg
2227ec681f3Smrg
2237ec681f3Smrgstatic inline
2247ec681f3Smrgradio_rds_t radio_rds_for_region(bool rds, radio_region_t region) {
2257ec681f3Smrg    if (!rds)
2267ec681f3Smrg        return RADIO_RDS_NONE;
2277ec681f3Smrg    switch(region) {
2287ec681f3Smrg        case RADIO_REGION_ITU_1:
2297ec681f3Smrg        case RADIO_REGION_OIRT:
2307ec681f3Smrg        case RADIO_REGION_JAPAN:
2317ec681f3Smrg        case RADIO_REGION_KOREA:
2327ec681f3Smrg            return RADIO_RDS_WORLD;
2337ec681f3Smrg        case RADIO_REGION_ITU_2:
2347ec681f3Smrg            return RADIO_RDS_US;
2357ec681f3Smrg        default:
2367ec681f3Smrg            return RADIO_REGION_NONE;
2377ec681f3Smrg    }
2387ec681f3Smrg}
2397ec681f3Smrg
2407ec681f3Smrgstatic inline
2417ec681f3Smrgradio_deemphasis_t radio_demephasis_for_region(radio_region_t region) {
2427ec681f3Smrg    switch(region) {
2437ec681f3Smrg        case RADIO_REGION_KOREA:
2447ec681f3Smrg        case RADIO_REGION_ITU_2:
2457ec681f3Smrg            return RADIO_DEEMPHASIS_75;
2467ec681f3Smrg        case RADIO_REGION_ITU_1:
2477ec681f3Smrg        case RADIO_REGION_OIRT:
2487ec681f3Smrg        case RADIO_REGION_JAPAN:
2497ec681f3Smrg        default:
2507ec681f3Smrg            return RADIO_DEEMPHASIS_50;
2517ec681f3Smrg    }
2527ec681f3Smrg}
2537ec681f3Smrg
2547ec681f3Smrg#endif  // ANDROID_RADIO_H
255