blob: 198fe19e38b18d03fb0cb5319fe1db7271efafb6 [file] [log] [blame]
xf.libdd93d52023-05-12 07:10:14 -07001#ifdef USE_CAP_SUPPORT
2#include <stdio.h>
3#include <unistd.h>
4#include <string.h>
5#include <stdlib.h>
6#include <stdint.h>
7#include <sys/ioctl.h>
8#include <fcntl.h>
9#include "voice_ipc.h"
10#include "linux/rpmsg_zx29.h"
11
12extern int sc_audio_set_voice_device_mode(int dev_mode);
13extern int sc_audio_get_voice_device_mode(int *p_dev_mode);
14extern int sc_audio_set_rx_voice_vol(int vol);
15extern int sc_audio_get_rx_voice_vol(int * p_vol);
16extern int sc_audio_set_tx_voice_vol(int vol);
17extern int sc_audio_get_tx_voice_vol(int *p_vol);
18extern int sc_audio_set_tx_voice_mute_state(int mute );
19extern int sc_audio_get_tx_voice_mute_state(int *p_mute);
20extern int sc_audio_set_rx_voice_mute_state(int mute);
21extern int sc_audio_get_rx_voice_mute_state(int *p_mute);
22extern int sc_audio_set_loopback_enable_state(int enable);
23extern int sc_audio_get_loopback_enable_state(int *p_enable);
24
25int voice_ipc_fd = -1;
26voice_ipc_control_msg voice_ctrl_recvmsg[IPC_VOICE_FUNC_MAX] = {0};
27
28void ipc_set_voice_device_mode(voice_ipc_control_msg msg)
29{
30 int ret = VOICEIPC_OK;
31 int msg_len = 0;
32 int dev_mode = 0;
33
34 //memcpy(&dev_mode, msg.param, msg.param_len);
35 dev_mode = *((int *)msg.param);
36 ret = sc_audio_set_voice_device_mode(dev_mode);
37
38 //msg.func_id不变
39 msg.param_len = sizeof(int);
40 memcpy(msg.param, &ret, msg.param_len);
41 msg_len = VOICE_HEAD_LEN + msg.param_len;
42
43 ret = write(voice_ipc_fd, &msg, msg_len);
44
45 if (0 >= ret){
46 printf("%s: write error(%d)!\n", __func__, ret);
47 }
48}
49
50void ipc_get_voice_device_mode(voice_ipc_control_msg msg)
51{
52 int ret = VOICEIPC_OK;
53 int msg_len = 0;
54 int *p_dev_mode = NULL;
55
56 p_dev_mode = (int *)msg.param;
57 ret = sc_audio_get_voice_device_mode(p_dev_mode);
58
59 //msg.func_id不变
60 msg.param_len = sizeof(int);
61 memcpy(msg.param, &ret, msg.param_len);
62 msg_len = VOICE_HEAD_LEN + msg.param_len;
63
64 ret = write(voice_ipc_fd, &msg, msg_len);
65
66 if (0 >= ret){
67 printf("%s: write error(%d)!\n", __func__, ret);
68 }
69}
70
71void ipc_set_rx_voice_vol(voice_ipc_control_msg msg)
72{
73 int ret = VOICEIPC_OK;
74 int msg_len = 0;
75 int vol = 0;
76
77 //memcpy(&dev_mode, msg.param, msg.param_len);
78 vol = *((int *)msg.param);
79 ret = sc_audio_set_rx_voice_vol(vol);
80
81 //msg.func_id不变
82 msg.param_len = sizeof(int);
83 memcpy(msg.param, &ret, msg.param_len);
84 msg_len = VOICE_HEAD_LEN + msg.param_len;
85
86 ret = write(voice_ipc_fd, &msg, msg_len);
87
88 if (0 >= ret){
89 printf("%s: write error(%d)!\n", __func__, ret);
90 }
91}
92
93void ipc_get_rx_voice_vol(voice_ipc_control_msg msg)
94{
95 int ret = VOICEIPC_OK;
96 int msg_len = 0;
97 int *p_vol = NULL;
98
99 p_vol = (int *)msg.param;
100 ret = sc_audio_get_rx_voice_vol(p_vol);
101
102 //msg.func_id不变
103 msg.param_len = sizeof(int);
104 memcpy(msg.param, &ret, msg.param_len);
105 msg_len = VOICE_HEAD_LEN + msg.param_len;
106
107 ret = write(voice_ipc_fd, &msg, msg_len);
108
109 if (0 >= ret){
110 printf("%s: write error(%d)!\n", __func__, ret);
111 }
112}
113
114void ipc_set_tx_voice_vol(voice_ipc_control_msg msg)
115{
116 int ret = VOICEIPC_OK;
117 int msg_len = 0;
118 int vol = 0;
119
120 //memcpy(&dev_mode, msg.param, msg.param_len);
121 vol = *((int *)msg.param);
122 ret = sc_audio_set_tx_voice_vol(vol);
123
124 //msg.func_id不变
125 msg.param_len = sizeof(int);
126 memcpy(msg.param, &ret, msg.param_len);
127 msg_len = VOICE_HEAD_LEN + msg.param_len;
128
129 ret = write(voice_ipc_fd, &msg, msg_len);
130
131 if (0 >= ret){
132 printf("%s: write error(%d)!\n", __func__, ret);
133 }
134}
135
136void ipc_get_tx_voice_vol(voice_ipc_control_msg msg)
137{
138 int ret = VOICEIPC_OK;
139 int msg_len = 0;
140 int *p_vol = NULL;
141
142 p_vol = (int *)msg.param;
143 ret = sc_audio_get_tx_voice_vol(p_vol);
144
145 //msg.func_id不变
146 msg.param_len = sizeof(int);
147 memcpy(msg.param, &ret, msg.param_len);
148 msg_len = VOICE_HEAD_LEN + msg.param_len;
149
150 ret = write(voice_ipc_fd, &msg, msg_len);
151
152 if (0 >= ret){
153 printf("%s: write error(%d)!\n", __func__, ret);
154 }
155}
156
157void ipc_set_tx_voice_mute_state(voice_ipc_control_msg msg)
158{
159 int ret = VOICEIPC_OK;
160 int msg_len = 0;
161 int mute = 0;
162
163 //memcpy(&dev_mode, msg.param, msg.param_len);
164 mute = *((int *)msg.param);
165 ret = sc_audio_set_tx_voice_mute_state(mute);
166
167 //msg.func_id不变
168 msg.param_len = sizeof(int);
169 memcpy(msg.param, &ret, msg.param_len);
170 msg_len = VOICE_HEAD_LEN + msg.param_len;
171
172 ret = write(voice_ipc_fd, &msg, msg_len);
173
174 if (0 >= ret){
175 printf("%s: write error(%d)!\n", __func__, ret);
176 }
177}
178
179void ipc_get_tx_voice_mute_state(voice_ipc_control_msg msg)
180{
181 int ret = VOICEIPC_OK;
182 int msg_len = 0;
183 int *p_mute = NULL;
184
185 p_mute = (int *)msg.param;
186 ret = sc_audio_get_tx_voice_mute_state(p_mute);
187
188 //msg.func_id不变
189 msg.param_len = sizeof(int);
190 memcpy(msg.param, &ret, msg.param_len);
191 msg_len = VOICE_HEAD_LEN + msg.param_len;
192
193 ret = write(voice_ipc_fd, &msg, msg_len);
194
195 if (0 >= ret){
196 printf("%s: write error(%d)!\n", __func__, ret);
197 }
198}
199
200void ipc_set_rx_voice_mute_state(voice_ipc_control_msg msg)
201{
202 int ret = VOICEIPC_OK;
203 int msg_len = 0;
204 int mute = 0;
205
206 //memcpy(&dev_mode, msg.param, msg.param_len);
207 mute = *((int *)msg.param);
208 ret = sc_audio_set_rx_voice_mute_state(mute);
209
210 //msg.func_id不变
211 msg.param_len = sizeof(int);
212 memcpy(msg.param, &ret, msg.param_len);
213 msg_len = VOICE_HEAD_LEN + msg.param_len;
214
215 ret = write(voice_ipc_fd, &msg, msg_len);
216
217 if (0 >= ret){
218 printf("%s: write error(%d)!\n", __func__, ret);
219 }
220}
221
222void ipc_get_rx_voice_mute_state(voice_ipc_control_msg msg)
223{
224 int ret = VOICEIPC_OK;
225 int msg_len = 0;
226 int *p_mute = NULL;
227
228 p_mute = (int *)msg.param;
229 ret = sc_audio_get_rx_voice_mute_state(p_mute);
230
231 //msg.func_id不变
232 msg.param_len = sizeof(int);
233 memcpy(msg.param, &ret, msg.param_len);
234 msg_len = VOICE_HEAD_LEN + msg.param_len;
235
236 ret = write(voice_ipc_fd, &msg, msg_len);
237
238 if (0 >= ret){
239 printf("%s: write error(%d)!\n", __func__, ret);
240 }
241}
242
243void ipc_set_loopback_enable_state(voice_ipc_control_msg msg)
244{
245 int ret = VOICEIPC_OK;
246 int msg_len = 0;
247 int enable = 0;
248
249 //memcpy(&dev_mode, msg.param, msg.param_len);
250 enable = *((int *)msg.param);
251 ret = sc_audio_set_loopback_enable_state(enable);
252
253 //msg.func_id不变
254 msg.param_len = sizeof(int);
255 memcpy(msg.param, &ret, msg.param_len);
256 msg_len = VOICE_HEAD_LEN + msg.param_len;
257
258 ret = write(voice_ipc_fd, &msg, msg_len);
259
260 if (0 >= ret){
261 printf("%s: write error(%d)!\n", __func__, ret);
262 }
263}
264
265void ipc_get_loopback_enable_state(voice_ipc_control_msg msg)
266{
267 int ret = VOICEIPC_OK;
268 int msg_len = 0;
269 int *p_enable = NULL;
270
271 p_enable = (int *)msg.param;
272 ret = sc_audio_get_loopback_enable_state(p_enable);
273
274 //msg.func_id不变
275 msg.param_len = sizeof(int);
276 memcpy(msg.param, &ret, msg.param_len);
277 msg_len = VOICE_HEAD_LEN + msg.param_len;
278
279 ret = write(voice_ipc_fd, &msg, msg_len);
280
281 if (0 >= ret){
282 printf("%s: write error(%d)!\n", __func__, ret);
283 }
284}
285
286void voice_msg_proc(voice_ipc_control_msg msg)
287{
288 switch(msg.func_id){
289 case IPC_SET_VOICE_DEVICE_MODE:
290 ipc_set_voice_device_mode(voice_ctrl_recvmsg[msg.func_id]);
291 break;
292 case IPC_GET_VOICE_DEVICE_MODE:
293 ipc_get_voice_device_mode(voice_ctrl_recvmsg[msg.func_id]);
294 break;
295 case IPC_SET_RX_VOICE_VOL:
296 ipc_set_rx_voice_vol(voice_ctrl_recvmsg[msg.func_id]);
297 break;
298 case IPC_GET_RX_VOICE_VOL:
299 ipc_get_rx_voice_vol(voice_ctrl_recvmsg[msg.func_id]);
300 break;
301 case IPC_SET_TX_VOICE_VOL:
302 ipc_set_tx_voice_vol(voice_ctrl_recvmsg[msg.func_id]);
303 break;
304 case IPC_GET_TX_VOICE_VOL:
305 ipc_get_tx_voice_vol(voice_ctrl_recvmsg[msg.func_id]);
306 break;
307 case IPC_SET_TX_VOICE_MUTE_STATE:
308 ipc_set_tx_voice_mute_state(voice_ctrl_recvmsg[msg.func_id]);
309 break;
310 case IPC_GET_TX_VOICE_MUTE_STATE:
311 ipc_get_tx_voice_mute_state(voice_ctrl_recvmsg[msg.func_id]);
312 break;
313 case IPC_SET_RX_VOICE_MUTE_STATE:
314 ipc_set_rx_voice_mute_state(voice_ctrl_recvmsg[msg.func_id]);
315 break;
316 case IPC_GET_RX_VOICE_MUTE_STATE:
317 ipc_get_rx_voice_mute_state(voice_ctrl_recvmsg[msg.func_id]);
318 break;
319 case IPC_SET_LOOPBACK_ENABLE_STATE:
320 ipc_set_loopback_enable_state(voice_ctrl_recvmsg[msg.func_id]);
321 break;
322 case IPC_GET_LOOPBACK_ENABLE_STATE:
323 ipc_get_loopback_enable_state(voice_ctrl_recvmsg[msg.func_id]);
324 break;
325 default:
326 printf("%s: msg func_id(%d) error\n", __func__, msg.func_id);
327 break;
328 }
329}
330
331void Voice_Ctrl_Rpmsg_Recv(void)
332{
333 int read_len = 0;
334 voice_ipc_control_msg tmpbuf = {0};
335
336 for(;;){
337 read_len = 0;
338 read_len = read(voice_ipc_fd, &tmpbuf, (VOICE_HEAD_LEN + VOICE_CONTROL_MAX_LEN));
339 if (0 >= read_len){
340 continue;
341 }
342
343 //最后再改变func_id,确保其他地方判断func_id改变后有数据可读
344 memcpy(voice_ctrl_recvmsg[tmpbuf.func_id].param, tmpbuf.param, tmpbuf.param_len);
345 voice_ctrl_recvmsg[tmpbuf.func_id].param_len = tmpbuf.param_len;
346 voice_ctrl_recvmsg[tmpbuf.func_id].func_id = tmpbuf.func_id;
347
348 voice_msg_proc(voice_ctrl_recvmsg[tmpbuf.func_id]);
349 }
350}
351
352int voice_ipc_init(void) //通道初始化
353{
354 voice_ipc_fd = open(VOICE_IPC_CONTROL_CHANNEL, O_RDWR);
355
356 if(0 > voice_ipc_fd){
357 printf("%s: open the channel(%s) error!\n", __func__, VOICE_IPC_CONTROL_CHANNEL);
358 return VOICEIPC_ERROR;
359 }
360
361 if(0 > ioctl(voice_ipc_fd, RPMSG_CREATE_CHANNEL, VOICE_IPC_CONTROL_CHANNEL_SIZE)){
362 printf("%s: ioctl RPMSG_CREATE_CHANNEL fail!\n", __func__);
363 close(voice_ipc_fd);
364 voice_ipc_fd = -1;
365 return VOICEIPC_ERROR;
366 }
367
368 if(0 > ioctl(voice_ipc_fd, RPMSG_SET_INT_FLAG, NULL)){ //写中断
369 printf("%s: ioctl RPMSG_SET_INT_FLAG fail!\n", __func__);
370 close(voice_ipc_fd);
371 voice_ipc_fd = -1;
372 return VOICEIPC_ERROR;
373 }
374
375 if(0 > ioctl(voice_ipc_fd, RPMSG_CLEAR_POLL_FLAG, NULL)){ //阻塞方式读数据
376 printf("%s: ioctl RPMSG_CLEAR_POLL_FLAG fail!\n", __func__);
377 close(voice_ipc_fd);
378 voice_ipc_fd = -1;
379 return VOICEIPC_ERROR;
380 }
381 return VOICEIPC_OK;
382}
383
384int main(int argc, char **argv)
385{
386 int ret = 0;
387
388 ret = voice_ipc_init();
389
390 if(ret < 0){
391 printf("voice_ipc_init error!\n");
392 return -1;
393 }
394
395 printf("voice_ipc_init %s create success!\n", VOICE_IPC_CONTROL_CHANNEL);
396
397 Voice_Ctrl_Rpmsg_Recv();
398
399 return 0;
400}
401
402#endif