1094332d3Sopenharmony_ci/*
2094332d3Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
3094332d3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4094332d3Sopenharmony_ci * you may not use this file except in compliance with the License.
5094332d3Sopenharmony_ci * You may obtain a copy of the License at
6094332d3Sopenharmony_ci *
7094332d3Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8094332d3Sopenharmony_ci *
9094332d3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10094332d3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11094332d3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12094332d3Sopenharmony_ci * See the License for the specific language governing permissions and
13094332d3Sopenharmony_ci * limitations under the License.
14094332d3Sopenharmony_ci */
15094332d3Sopenharmony_ci
16094332d3Sopenharmony_ci#ifndef ALSA_SOUNDCARD_H
17094332d3Sopenharmony_ci#define ALSA_SOUNDCARD_H
18094332d3Sopenharmony_ci
19094332d3Sopenharmony_ci#include "audio_common.h"
20094332d3Sopenharmony_ci#include "audio_if_lib_common.h"
21094332d3Sopenharmony_ci#include "hdf_io_service_if.h"
22094332d3Sopenharmony_ci#include "asoundlib.h"
23094332d3Sopenharmony_ci#include "osal_mem.h"
24094332d3Sopenharmony_ci#include "osal_time.h"
25094332d3Sopenharmony_ci#include "hdf_sbuf.h"
26094332d3Sopenharmony_ci#include "audio_uhdf_log.h"
27094332d3Sopenharmony_ci#include "securec.h"
28094332d3Sopenharmony_ci#include "local.h"
29094332d3Sopenharmony_ci
30094332d3Sopenharmony_ci#ifdef __cplusplus
31094332d3Sopenharmony_ciextern "C" {
32094332d3Sopenharmony_ci#endif
33094332d3Sopenharmony_ci
34094332d3Sopenharmony_ci#define SERVIC_NAME_MAX_LEN         32
35094332d3Sopenharmony_ci#define MAX_VOLUME                  100
36094332d3Sopenharmony_ci#define MIN_VOLUME                  0
37094332d3Sopenharmony_ci#define AUDIO_MIN_CARD_NUM          1
38094332d3Sopenharmony_ci#define AUDIO_MAX_CARD_NUM          8
39094332d3Sopenharmony_ci#define CARD_ID_LEN_MAX             32
40094332d3Sopenharmony_ci#define MAX_CARD_NAME_LEN           64
41094332d3Sopenharmony_ci#define MAX_CARD_NUM                (4 * (AUDIO_MAX_CARD_NUM))
42094332d3Sopenharmony_ci#define MAX_CTL_NAME_LEN            64
43094332d3Sopenharmony_ci#define MAX_CTL_VALUE_LEN           32
44094332d3Sopenharmony_ci#define AUDIO_ALSALIB_IOCTRL_RESUME 0
45094332d3Sopenharmony_ci#define AUDIO_ALSALIB_IOCTRL_PAUSE  1
46094332d3Sopenharmony_ci#define AUDIO_ALSALIB_MMAP_MAX      10
47094332d3Sopenharmony_ci#define AUDIO_ALSALIB_RETYR         3
48094332d3Sopenharmony_ci#define ALSA_CTL_NAME_LEN           64
49094332d3Sopenharmony_ci#define MIXER_CTL_MAX_NUM           64
50094332d3Sopenharmony_ci
51094332d3Sopenharmony_cienum SndCardType {
52094332d3Sopenharmony_ci    SND_CARD_UNKNOWN = -1,
53094332d3Sopenharmony_ci    SND_CARD_PRIMARY = 0,
54094332d3Sopenharmony_ci    SND_CARD_HDMI,
55094332d3Sopenharmony_ci    SND_CARD_USB,
56094332d3Sopenharmony_ci    SND_CARD_BT,
57094332d3Sopenharmony_ci    SND_CARD_MAX
58094332d3Sopenharmony_ci};
59094332d3Sopenharmony_ci
60094332d3Sopenharmony_cienum SndIfaceType {
61094332d3Sopenharmony_ci    IFACE_CARD = 0,
62094332d3Sopenharmony_ci    IFACE_MIXER,
63094332d3Sopenharmony_ci    IFACE_PCM,
64094332d3Sopenharmony_ci    IFACE_RAWMIDI,
65094332d3Sopenharmony_ci    IFACE_TIMER,
66094332d3Sopenharmony_ci    IFACE_SEQUENCER
67094332d3Sopenharmony_ci};
68094332d3Sopenharmony_ci
69094332d3Sopenharmony_cistruct AlsaMixerCtlElement {
70094332d3Sopenharmony_ci    unsigned int numid;
71094332d3Sopenharmony_ci    enum SndIfaceType iface;
72094332d3Sopenharmony_ci    char *name;
73094332d3Sopenharmony_ci    char *value;
74094332d3Sopenharmony_ci    unsigned int index;
75094332d3Sopenharmony_ci    unsigned int device;
76094332d3Sopenharmony_ci    unsigned int subdevice;
77094332d3Sopenharmony_ci};
78094332d3Sopenharmony_ci
79094332d3Sopenharmony_cistruct AlsaSoundCard {
80094332d3Sopenharmony_ci    /*
81094332d3Sopenharmony_ci        save alsa soundcard base info and hardware params
82094332d3Sopenharmony_ci    */
83094332d3Sopenharmony_ci    enum SndCardType cardType;
84094332d3Sopenharmony_ci    char adapterName[MAX_CARD_NAME_LEN + 1];  //save adapterName
85094332d3Sopenharmony_ci    char devName[MAX_CARD_NAME_LEN + 1];   //device name hw:x
86094332d3Sopenharmony_ci    char alsaCardId[MAX_CARD_NAME_LEN + 1];
87094332d3Sopenharmony_ci    char ctrlName[MAX_CARD_NAME_LEN + 1];
88094332d3Sopenharmony_ci    struct AudioPcmHwParams hwParams;
89094332d3Sopenharmony_ci
90094332d3Sopenharmony_ci    /*
91094332d3Sopenharmony_ci        alsa soundcard driver handle
92094332d3Sopenharmony_ci    */
93094332d3Sopenharmony_ci    snd_pcm_t *pcmHandle;
94094332d3Sopenharmony_ci    snd_mixer_t *mixerHandle;
95094332d3Sopenharmony_ci
96094332d3Sopenharmony_ci    /*
97094332d3Sopenharmony_ci        alsa soundcard public variable
98094332d3Sopenharmony_ci    */
99094332d3Sopenharmony_ci    uint8_t cardStatus;
100094332d3Sopenharmony_ci    bool canPause;
101094332d3Sopenharmony_ci    bool pauseState;
102094332d3Sopenharmony_ci    int32_t muteValue;
103094332d3Sopenharmony_ci    bool mmapFlag;
104094332d3Sopenharmony_ci    uint64_t mmapFrames;
105094332d3Sopenharmony_ci};
106094332d3Sopenharmony_ci
107094332d3Sopenharmony_cistruct DevHandle *AudioBindService(const char *name);
108094332d3Sopenharmony_civoid AudioCloseService(const struct DevHandle *handle);
109094332d3Sopenharmony_cistruct HdfIoService *HdfIoServiceBindName(const char *serviceName);
110094332d3Sopenharmony_ci
111094332d3Sopenharmony_ciint32_t SndMatchSelAdapter(struct AlsaSoundCard *cardIns, const char *adapterName);
112094332d3Sopenharmony_ciint32_t SndConverAlsaPcmFormat(const struct AudioPcmHwParams *hwParams,
113094332d3Sopenharmony_ci    snd_pcm_format_t *alsaPcmFormat);
114094332d3Sopenharmony_ciint32_t SndSaveCardListInfo(snd_pcm_stream_t stream);
115094332d3Sopenharmony_cibool  SndisBusy(struct AlsaSoundCard *cardIns);
116094332d3Sopenharmony_ciint32_t SndOpenMixer(struct AlsaSoundCard *cardIns);
117094332d3Sopenharmony_ciint32_t SndPcmPrepare(struct AlsaSoundCard *cardIns);
118094332d3Sopenharmony_cisnd_pcm_state_t SndGetRunState(struct AlsaSoundCard *cardIns);
119094332d3Sopenharmony_civoid  SndCloseHandle(struct AlsaSoundCard *cardIns);
120094332d3Sopenharmony_ci
121094332d3Sopenharmony_civoid SndElementItemInit(struct AlsaMixerCtlElement *m);
122094332d3Sopenharmony_ciint32_t SndElementReadInt(struct AlsaSoundCard *cardIns,
123094332d3Sopenharmony_ci    const struct AlsaMixerCtlElement *ctlElem, long *value);
124094332d3Sopenharmony_ciint32_t SndElementReadEnum(struct AlsaSoundCard *cardIns,
125094332d3Sopenharmony_ci    const struct AlsaMixerCtlElement *ctlElem, unsigned int *item);
126094332d3Sopenharmony_ciint32_t SndElementReadRange(struct AlsaSoundCard *cardIns,
127094332d3Sopenharmony_ci    const struct AlsaMixerCtlElement *ctlElem, long *mix, long *max);
128094332d3Sopenharmony_ciint32_t SndElementReadSwitch(struct AlsaSoundCard *cardIns,
129094332d3Sopenharmony_ci    const struct AlsaMixerCtlElement *ctlElem, bool *on);
130094332d3Sopenharmony_ciint32_t SndElementWriteInt(struct AlsaSoundCard *cardIns,
131094332d3Sopenharmony_ci    const struct AlsaMixerCtlElement *ctlElem, long value);
132094332d3Sopenharmony_ciint32_t SndElementWriteEnum(struct AlsaSoundCard *cardIns,
133094332d3Sopenharmony_ci    const struct AlsaMixerCtlElement *ctlElem, unsigned int item);
134094332d3Sopenharmony_ciint32_t SndElementWriteSwitch(struct AlsaSoundCard *cardIns,
135094332d3Sopenharmony_ci    const struct AlsaMixerCtlElement *ctlElem, bool on);
136094332d3Sopenharmony_ciint32_t SndElementWrite(struct AlsaSoundCard *cardIns,
137094332d3Sopenharmony_ci    const struct AlsaMixerCtlElement *ctlElem);
138094332d3Sopenharmony_ciint32_t SndElementGroupWrite(struct AlsaSoundCard *cardIns,
139094332d3Sopenharmony_ci    const struct AlsaMixerCtlElement* elemGroup, int32_t groupSize);
140094332d3Sopenharmony_ciint32_t SndTraversalMixerElement(struct AlsaSoundCard *cardIns,
141094332d3Sopenharmony_ci    bool (*callback)(void *data, snd_ctl_elem_id_t *elem_id), void *data);
142094332d3Sopenharmony_ci
143094332d3Sopenharmony_ci#ifdef __cplusplus
144094332d3Sopenharmony_ci}
145094332d3Sopenharmony_ci#endif
146094332d3Sopenharmony_ci
147094332d3Sopenharmony_ci#endif /* ALSA_SOUNDCARD_H */