blob: d31330813fc4b9c6cd6fdc3f064605d4fc68a2b6 [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001#include <stdio.h>
2#include <string.h>
3#include <unistd.h>
4#include <stdlib.h>
5#include <sys/types.h>
6#include <sys/stat.h>
7#include <fcntl.h>
8#include <assert.h>
9#include <stdint.h>
10#include <errno.h>
11#include <fcntl.h>
12#include <sys/stat.h>
13#include <semaphore.h>
14#include "audio_ctrl_service_inner_api.h"
15
16enum {
17 UPPER_CALL,
18 LOWER_CALL,
19};
20
21static int do_IPC_call(const char* cmd, int dir);
22static int do_IPC_call_with_size(const char* cmd, int size, int dir);
23static int do_IPC_call_with_return_data(const char* cmd, char* dest,
24 int dest_size, int dir);
25static int do_IPC_call_general(const char* cmd, int size, char* dest,
26 int dest_size, int dir);
27
28//#define IPC_SEQ_DEBUG
29
30//turn on or off speech
31//return value: 0 if success, or a negitive error number
32int audio_ctrl_service_speech_on(int speech_on)
33{
34 static char buf[16];
35 sprintf(buf, "%d,%d", FUNC_AUDIO_CTRL_SERVICE_SPEECH_ON, speech_on);
36 return do_IPC_call(buf, UPPER_CALL);
37}
38
39//turn on or off BT speech
40//return value: 0 if success, or a negitive error number
41int audio_ctrl_service_bt_speech_on(int speech_on)
42{
43 static char buf[16];
44 sprintf(buf, "%d,%d", FUNC_AUDIO_CTRL_SERVICE_BT_SPEECH_ON, speech_on);
45 return do_IPC_call(buf, UPPER_CALL);
46}
47
48//set dl gain (-64dB~17dB)
49//return value: default device setting
50int audio_ctrl_service_set_dl_volume(int Db)
51{
52 static char buf[16];
53 sprintf(buf, "%d,%d", FUNC_AUDIO_CTRL_SERVICE_SET_DL_VOLUME, Db);
54 return do_IPC_call(buf, UPPER_CALL);
55}
56
57//data cb from modem
58//return value: 0 if success, or a negitive error number
59int audio_ctrl_service_inCall_record_data_cb(int data_size, const char* data)
60{
61 static char buf[IPC_DATA_SIZE_MAX]; //need larger size for record data
62 int str_length;
63 int ret = 0;
64 str_length = sprintf(buf, "%d,%d,",
65 FUNC_AUDIO_CTRL_INCALL_SERVICE_RECORD_DATA_CB,
66 data_size);
67 memcpy(&buf[str_length], data, data_size);
68 return do_IPC_call_with_size(buf, data_size + str_length, LOWER_CALL);
69}
70
71//start record data from modem (buf_size in bytes)
72//return value: 0 if success, or a negitive error number
73int audio_ctrl_service_inCall_record_start(int buf_size)
74{
75 static char buf[16];
76 sprintf(buf, "%d,%d", FUNC_AUDIO_CTRL_INCALL_RECORD_START, buf_size);
77 return do_IPC_call(buf, UPPER_CALL);
78}
79
80//stop record data from modem
81//return value: 0 if success, or a negitive error number
82int audio_ctrl_service_inCall_record_stop()
83{
84 static char buf[16];
85 sprintf(buf, "%d", FUNC_AUDIO_CTRL_INCALL_RECORD_STOP);
86 return do_IPC_call(buf, UPPER_CALL);
87}
88
89//pointer of buffer in audio-ctrl-service
90//return value: if success, return pointer location (in bytes)
91// or a negitive error number
92int audio_ctrl_service_inCall_record_pointer()
93{
94 static char buf[16];
95 sprintf(buf, "%d", FUNC_AUDIO_CTRL_INCALL_RECORD_POINTER);
96 return do_IPC_call(buf, UPPER_CALL);
97}
98
99//record watermark
100//return value: if success, return watermark (in bytes)
101// or a negitive error number
102int audio_ctrl_service_inCall_record_get_watermark()
103{
104 static char buf[16];
105 sprintf(buf, "%d", FUNC_AUDIO_CTRL_INCALL_RECORD_GET_WATERMARK);
106 return do_IPC_call(buf, UPPER_CALL);
107}
108
109//read data from audio-ctrl-service(in bytes)
110//now only support 16bits record
111//return value: if success, return read size (in bytes)
112// or a negitive error number
113int audio_ctrl_service_inCall_record_get_data(int data_size, char* dest)
114{
115 static char buf[16];
116 char* ret_ptr;
117 assert(dest);
118 sprintf(buf, "%d,%d", FUNC_AUDIO_CTRL_INCALL_RECORD_GET_DATA,
119 data_size);
120 return do_IPC_call_with_return_data(buf, dest, data_size, UPPER_CALL);
121}
122
123//start send data to modem
124//return value: 0 if success, or a negitive error number
125int audio_ctrl_service_inCall_playback_start()
126{
127 static char buf[16];
128 sprintf(buf, "%d", FUNC_AUDIO_CTRL_INCALL_PLAYBACK_START);
129 return do_IPC_call(buf, UPPER_CALL);
130}
131
132//stop send data to modem
133//return value: 0 if success, or a negitive error number
134int audio_ctrl_service_inCall_playback_stop()
135{
136 static char buf[16];
137 sprintf(buf, "%d", FUNC_AUDIO_CTRL_INCALL_PLAYBACK_STOP);
138 return do_IPC_call(buf, UPPER_CALL);
139}
140
141//playback send data to modem
142//return value: 0 if success, or a negitive error number
143int audio_ctrl_service_inCall_playback_send_data(int data_size, const char* data)
144{
145 static char buf[IPC_DATA_SIZE_MAX]; //need larger size for playback data
146 int str_length;
147
148 str_length = sprintf(buf, "%d,%d,",
149 FUNC_AUDIO_CTRL_INCALL_PLAYBACK_SEND_DATA,
150 data_size);
151 memcpy(&buf[str_length], data, data_size);
152 return do_IPC_call_with_size(buf, data_size + str_length, UPPER_CALL);
153}
154
155//check if speech on or off
156//return value: 0 if off, positive num if on, or a negitive error number
157int audio_ctrl_service_is_speech_on()
158{
159 static char buf[16];
160 sprintf(buf, "%d", FUNC_AUDIO_CTRL_SERVICE_IS_SPEECH_ON);
161 return do_IPC_call(buf, UPPER_CALL);
162}
163
164//check if speech on or off
165//return value: 0 if off, positive num if on, or a negitive error number
166int audio_ctrl_service_is_bt_speech_on()
167{
168 static char buf[16];
169 sprintf(buf, "%d", FUNC_AUDIO_CTRL_SERVICE_IS_BT_SPEECH_ON);
170 return do_IPC_call(buf, UPPER_CALL);
171}
172
173//get speech dl gain
174//return value: dl gain value (in dB)
175int audio_ctrl_service_get_dl_gain()
176{
177 static char buf[16];
178 sprintf(buf, "%d", FUNC_AUDIO_CTRL_SERVICE_GET_DL_GAIN);
179 return do_IPC_call(buf, UPPER_CALL);
180}
181
182//get record period size
183//return value: positive number for period size(in bytes), or a negitive error number
184int audio_ctrl_service_get_record_period_size()
185{
186 static char buf[16];
187 sprintf(buf, "%d", FUNC_AUDIO_CTRL_SERVICE_GET_RECORD_PERIOD_SIZE);
188 return do_IPC_call(buf, UPPER_CALL);
189}
190
191//get max record buffer size
192//return value: positive number for buffer size(in bytes), or a negitive error number
193int audio_ctrl_service_get_record_max_buffer_size()
194{
195 static char buf[16];
196 sprintf(buf, "%d", FUNC_AUDIO_CTRL_SERVICE_GET_RECORD_MAX_BUFFER_SIZE);
197 return do_IPC_call(buf, UPPER_CALL);
198}
199
200//get playback period size
201//return value: positive number for period size(in bytes), or a negitive error number
202int audio_ctrl_service_get_playback_period_size()
203{
204 static char buf[16];
205 sprintf(buf, "%d", FUNC_AUDIO_CTRL_SERVICE_GET_PLAYBACK_PERIOD_SIZE);
206 return do_IPC_call(buf, UPPER_CALL);
207}
208
209//get max playback buffer size
210//return value: positive number for buffer size(in bytes), or a negitive error number
211int audio_ctrl_service_get_playback_max_buffer_size()
212{
213 static char buf[16];
214 sprintf(buf, "%d", FUNC_AUDIO_CTRL_SERVICE_GET_PLAYBACK_MAX_BUFFER_SIZE);
215 return do_IPC_call(buf, UPPER_CALL);
216}
217
218//turn on/off vmlog
219//return value: from libspeech_drv
220int audio_ctrl_service_vmlog_on(int vmlog_on)
221{
222 static char buf[16];
223 sprintf(buf, "%d,%d", FUNC_AUDIO_CTRL_SERVICE_VMLOG_ON, vmlog_on);
224 return do_IPC_call(buf, UPPER_CALL);
225}
226
227//get vmlog on/off status
228//return value: 1 for vmlog on and 0 for vmlog off
229int audio_ctrl_service_get_vmlog_on()
230{
231 static char buf[16];
232 sprintf(buf, "%d", FUNC_AUDIO_CTRL_SERVICE_GET_VMLOG_ON);
233 return do_IPC_call(buf, UPPER_CALL);
234}
235
236//get bt_wbs on/off status
237//return value: 1 for vmlog on and 0 for vmlog off
238int audio_ctrl_service_get_bt_wbs()
239{
240 static char buf[16];
241 sprintf(buf, "%d", FUNC_AUDIO_CTRL_SERVICE_GET_BT_WBS);
242 return do_IPC_call(buf, UPPER_CALL);
243}
244
245//set on/off bt_wbs
246//return value: from libspeech_drv
247int audio_ctrl_service_set_bt_wbs(int bt_wbs_on)
248{
249 static char buf[16];
250 sprintf(buf, "%d,%d", FUNC_AUDIO_CTRL_SERVICE_SET_BT_WBS, bt_wbs_on);
251 return do_IPC_call(buf, UPPER_CALL);
252}
253
254//set bt dl gain (0 ~ 15)
255//return value: 0 if success, or a negitive error number
256int audio_ctrl_service_set_bt_dl_gain(int vol)
257{
258 static char buf[16];
259 sprintf(buf, "%d,%d", FUNC_AUDIO_CTRL_SERVICE_SET_BT_DL_GAIN, vol);
260 return do_IPC_call(buf, UPPER_CALL);
261}
262
263//get bt dl gain
264//return value: bt dl value
265int audio_ctrl_service_get_bt_dl_gain()
266{
267 static char buf[16];
268 sprintf(buf, "%d", FUNC_AUDIO_CTRL_SERVICE_GET_BT_DL_GAIN);
269 return do_IPC_call(buf, UPPER_CALL);
270}
271
272//get BT_HAS_ECNR on/off status
273//return value: 1 for vmlog on and 0 for vmlog off
274int audio_ctrl_service_get_bt_client_has_ecnr()
275{
276 static char buf[16];
277 sprintf(buf, "%d", FUNC_AUDIO_CTRL_SERVICE_GET_BT_CLIENT_HAS_ECNR);
278 return do_IPC_call(buf, UPPER_CALL);
279}
280
281//set on/off BT_HAS_ECNR
282//return value: from libspeech_drv
283int audio_ctrl_service_set_bt_client_has_ecnr(int bt_client_ecnr_on)
284{
285 static char buf[16];
286 sprintf(buf, "%d,%d", FUNC_AUDIO_CTRL_SERVICE_SET_BT_CLIENT_HAS_ECNR, bt_client_ecnr_on);
287 return do_IPC_call(buf, UPPER_CALL);
288}
289
290//get SWITCH_BT_IN_CALL on/off status
291//return value: 1 for vmlog on and 0 for vmlog off
292int audio_ctrl_service_get_use_bt_in_call()
293{
294 static char buf[16];
295 sprintf(buf, "%d", FUNC_AUDIO_CTRL_SERVICE_GET_USE_BT_IN_CALL);
296 return do_IPC_call(buf, UPPER_CALL);
297}
298
299//switch BT on/off during call
300//return value: from libspeech_drv
301int audio_ctrl_service_set_use_bt_in_call(int turn_on_bt_in_call)
302{
303 static char buf[16];
304 sprintf(buf, "%d,%d", FUNC_AUDIO_CTRL_SERVICE_SET_USE_BT_IN_CALL, turn_on_bt_in_call);
305 return do_IPC_call(buf, UPPER_CALL);
306}
307
308//modem reset
309//return value: 0 (always success)
310int audio_ctrl_service_reset()
311{
312 static char buf[16];
313 sprintf(buf, "%d", FUNC_AUDIO_CTRL_SERVICE_RESET_INNER);
314 return do_IPC_call(buf, UPPER_CALL);
315}
316
317
318/*
319 * private method for IPC call interface
320 */
321static inline int do_IPC_call(const char* cmd, int dir)
322{
323 return do_IPC_call_general(cmd, 0, 0, 0, dir);
324}
325
326static inline int do_IPC_call_with_size(const char* cmd, int size, int dir)
327{
328 return do_IPC_call_general(cmd, size, 0, 0, dir);
329}
330
331static int do_IPC_call_with_return_data(const char* cmd, char* dest,
332 int dest_size, int dir)
333{
334 return do_IPC_call_general(cmd, 0, dest, dest_size, dir);
335}
336
337#define AUDIO_CTRL_SERVICE_IPC_UPPER_SEM "AUDIO_CTRL_SERVICE_IPC_UPPER_SEM"
338#define AUDIO_CTRL_SERVICE_IPC_LOWER_SEM "AUDIO_CTRL_SERVICE_IPC_LOWER_SEM"
339
340static int do_IPC_call_general(const char* cmd, int size, char* dest,
341 int dest_size, int dir)
342{
343 int send_cmd_handler;
344 int receive_cmd_handler;
345 int read_size, call_result, record_size;
346 static char buf[IPC_DATA_SIZE_MAX];
347 char *data_str;
348 int ret;
349 sem_t *sem_ipc;
350#ifdef IPC_SEQ_DEBUG
351 int cmd_int;
352
353 cmd_int = atoi(cmd);
354 printf("%s, cmd_int: %d, size: %d\n", __func__, cmd_int, size);
355 fflush(stdout);
356#endif
357
358 if (dir == UPPER_CALL){
359 sem_ipc = sem_open(AUDIO_CTRL_SERVICE_IPC_UPPER_SEM, O_CREAT, 0644, 1);
360 if (sem_ipc == SEM_FAILED) {
361 printf("%s sem_open failed, WTF = = \n", __func__);
362 return errno;
363 }
364 sem_wait(sem_ipc);
365
366 send_cmd_handler = open(ACS_IPC_FOR_UPPER_RCV, O_WRONLY);
367 receive_cmd_handler = open(ACS_IPC_FOR_UPPER_SEND, O_RDONLY);
368 } else { /*LOWER_CALL*/
369 sem_ipc = sem_open(AUDIO_CTRL_SERVICE_IPC_LOWER_SEM, O_CREAT, 0644, 1);
370 if (sem_ipc == SEM_FAILED) {
371 printf("%s sem_open failed, WTF = = \n", __func__);
372 return errno;
373 }
374 sem_wait(sem_ipc);
375
376 send_cmd_handler = open(ACS_IPC_FOR_LOWER_RCV, O_WRONLY);
377 receive_cmd_handler = open(ACS_IPC_FOR_LOWER_SEND, O_RDONLY);
378 }
379
380 if(!size)
381 write(send_cmd_handler, cmd, strlen(cmd));
382 else
383 write(send_cmd_handler, cmd, size);
384
385 read_size = read(receive_cmd_handler, buf, IPC_DATA_SIZE_MAX);
386 buf[read_size] = '\0';
387
388 close(receive_cmd_handler);
389 close(send_cmd_handler);
390
391 if(!dest) { //no extra data contained
392 call_result = atoi(buf);
393 } else {
394 strtok_r(buf, ",", &data_str);
395 call_result = atoi(buf);
396 record_size = read_size - (data_str - buf);
397 assert(dest_size >= record_size);
398 memcpy(dest, data_str, record_size);
399 }
400
401 sem_post(sem_ipc);
402
403 return call_result;
404}