1/*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#ifndef ALSA_SOUNDCARD_H
17#define ALSA_SOUNDCARD_H
18
19#include "audio_common.h"
20#include "audio_if_lib_common.h"
21#include "hdf_io_service_if.h"
22#include "asoundlib.h"
23#include "osal_mem.h"
24#include "osal_time.h"
25#include "hdf_sbuf.h"
26#include "audio_uhdf_log.h"
27#include "securec.h"
28#include "local.h"
29
30#ifdef __cplusplus
31extern "C" {
32#endif
33
34#define SERVIC_NAME_MAX_LEN         32
35#define MAX_VOLUME                  100
36#define MIN_VOLUME                  0
37#define AUDIO_MIN_CARD_NUM          1
38#define AUDIO_MAX_CARD_NUM          8
39#define CARD_ID_LEN_MAX             32
40#define MAX_CARD_NAME_LEN           64
41#define MAX_CARD_NUM                (4 * (AUDIO_MAX_CARD_NUM))
42#define MAX_CTL_NAME_LEN            64
43#define MAX_CTL_VALUE_LEN           32
44#define AUDIO_ALSALIB_IOCTRL_RESUME 0
45#define AUDIO_ALSALIB_IOCTRL_PAUSE  1
46#define AUDIO_ALSALIB_MMAP_MAX      10
47#define AUDIO_ALSALIB_RETYR         3
48#define ALSA_CTL_NAME_LEN           64
49#define MIXER_CTL_MAX_NUM           64
50
51enum SndCardType {
52    SND_CARD_UNKNOWN = -1,
53    SND_CARD_PRIMARY = 0,
54    SND_CARD_HDMI,
55    SND_CARD_USB,
56    SND_CARD_BT,
57    SND_CARD_MAX
58};
59
60enum SndIfaceType {
61    IFACE_CARD = 0,
62    IFACE_MIXER,
63    IFACE_PCM,
64    IFACE_RAWMIDI,
65    IFACE_TIMER,
66    IFACE_SEQUENCER
67};
68
69struct AlsaMixerCtlElement {
70    unsigned int numid;
71    enum SndIfaceType iface;
72    char *name;
73    char *value;
74    unsigned int index;
75    unsigned int device;
76    unsigned int subdevice;
77};
78
79struct AlsaSoundCard {
80    /*
81        save alsa soundcard base info and hardware params
82    */
83    enum SndCardType cardType;
84    char adapterName[MAX_CARD_NAME_LEN + 1];  //save adapterName
85    char devName[MAX_CARD_NAME_LEN + 1];   //device name hw:x
86    char alsaCardId[MAX_CARD_NAME_LEN + 1];
87    char ctrlName[MAX_CARD_NAME_LEN + 1];
88    struct AudioPcmHwParams hwParams;
89
90    /*
91        alsa soundcard driver handle
92    */
93    snd_pcm_t *pcmHandle;
94    snd_mixer_t *mixerHandle;
95
96    /*
97        alsa soundcard public variable
98    */
99    uint8_t cardStatus;
100    bool canPause;
101    bool pauseState;
102    int32_t muteValue;
103    bool mmapFlag;
104    uint64_t mmapFrames;
105};
106
107struct DevHandle *AudioBindService(const char *name);
108void AudioCloseService(const struct DevHandle *handle);
109struct HdfIoService *HdfIoServiceBindName(const char *serviceName);
110
111int32_t SndMatchSelAdapter(struct AlsaSoundCard *cardIns, const char *adapterName);
112int32_t SndConverAlsaPcmFormat(const struct AudioPcmHwParams *hwParams,
113    snd_pcm_format_t *alsaPcmFormat);
114int32_t SndSaveCardListInfo(snd_pcm_stream_t stream);
115bool  SndisBusy(struct AlsaSoundCard *cardIns);
116int32_t SndOpenMixer(struct AlsaSoundCard *cardIns);
117int32_t SndPcmPrepare(struct AlsaSoundCard *cardIns);
118snd_pcm_state_t SndGetRunState(struct AlsaSoundCard *cardIns);
119void  SndCloseHandle(struct AlsaSoundCard *cardIns);
120
121void SndElementItemInit(struct AlsaMixerCtlElement *m);
122int32_t SndElementReadInt(struct AlsaSoundCard *cardIns,
123    const struct AlsaMixerCtlElement *ctlElem, long *value);
124int32_t SndElementReadEnum(struct AlsaSoundCard *cardIns,
125    const struct AlsaMixerCtlElement *ctlElem, unsigned int *item);
126int32_t SndElementReadRange(struct AlsaSoundCard *cardIns,
127    const struct AlsaMixerCtlElement *ctlElem, long *mix, long *max);
128int32_t SndElementReadSwitch(struct AlsaSoundCard *cardIns,
129    const struct AlsaMixerCtlElement *ctlElem, bool *on);
130int32_t SndElementWriteInt(struct AlsaSoundCard *cardIns,
131    const struct AlsaMixerCtlElement *ctlElem, long value);
132int32_t SndElementWriteEnum(struct AlsaSoundCard *cardIns,
133    const struct AlsaMixerCtlElement *ctlElem, unsigned int item);
134int32_t SndElementWriteSwitch(struct AlsaSoundCard *cardIns,
135    const struct AlsaMixerCtlElement *ctlElem, bool on);
136int32_t SndElementWrite(struct AlsaSoundCard *cardIns,
137    const struct AlsaMixerCtlElement *ctlElem);
138int32_t SndElementGroupWrite(struct AlsaSoundCard *cardIns,
139    const struct AlsaMixerCtlElement* elemGroup, int32_t groupSize);
140int32_t SndTraversalMixerElement(struct AlsaSoundCard *cardIns,
141    bool (*callback)(void *data, snd_ctl_elem_id_t *elem_id), void *data);
142
143#ifdef __cplusplus
144}
145#endif
146
147#endif /* ALSA_SOUNDCARD_H */