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