Fix noise when unmuting radio volume
[platform/adaptation/spreadtrum/audio-hal-sc7727.git] / tizen-audio-impl-ucm.c
1 /*
2  * audio-hal
3  *
4  * Copyright (c) 2015 - 2016 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #ifdef ALSA_UCM_DEBUG_TIME
28 #include <sys/time.h>
29 #include <time.h>
30 #endif
31
32 #include "tizen-audio-internal.h"
33
34 #ifdef ALSA_UCM_DEBUG_TIME
35 #define SND_USE_CASE_SET __set_use_case_with_time
36 #else
37 #define SND_USE_CASE_SET snd_use_case_set
38 #endif
39
40 #define UCM_PREFIX_CURRENT   ">>> UCM current"
41 #define UCM_PREFIX_REQUESTED "> UCM requested"
42 #define UCM_PREFIX_CHANGED   "<<< UCM changed"
43
44 #define DUMP_LEN 512
45
46 static void __dump_use_case(const char* prefix, const char *verb, const char *devices[], int dev_count, const char *modifiers[], int mod_count)
47 {
48     int i;
49     dump_data_t* dump = NULL;
50
51     if (!(dump = _audio_dump_new(DUMP_LEN))) {
52         AUDIO_LOG_ERROR("Failed to create dump string...");
53         return;
54     }
55
56     /* Verb */
57     _audio_dump_add_str(dump, "Verb [ %s ] Devices [ ", verb ? verb : AUDIO_USE_CASE_VERB_INACTIVE);
58
59     /* Devices */
60     if (devices) {
61         for (i = 0; i < dev_count; i++) {
62             _audio_dump_add_str(dump, (i != dev_count - 1) ? "%s, " : "%s", devices[i]);
63         }
64     }
65     _audio_dump_add_str(dump, " ] Modifier [ ");
66
67     /* Modifiers */
68     if (modifiers) {
69         for (i = 0; i < mod_count; i++) {
70             _audio_dump_add_str(dump, (i != mod_count - 1) ? "%s, " : "%s", modifiers[i]);
71         }
72     }
73     _audio_dump_add_str(dump, " ]");
74
75     AUDIO_LOG_INFO("TEST %s : %s", prefix, _audio_dump_get_str(dump));
76
77     _audio_dump_free(dump);
78 }
79
80 #ifdef ALSA_UCM_DEBUG_TIME
81 static inline int __set_use_case_with_time(snd_use_case_mgr_t *uc_mgr, const char *identifier, const char *value)
82 {
83     int ret = 0;
84     struct timeval t_start, t_stop;
85     unsigned long long t_diff = 0;
86
87     gettimeofday(&t_start, NULL);
88     ret = snd_use_case_set(uc_mgr, identifier, value);
89     gettimeofday(&t_stop, NULL);
90     if (t_start.tv_sec < t_stop.tv_sec)
91         t_diff = (t_stop.tv_sec - t_start.tv_sec) * 1000000;
92     t_diff += (t_stop.tv_usec - t_start.tv_usec);
93     AUDIO_LOG_DEBUG("identifier %s value %s takes %lluusec", identifier, value, t_diff);
94
95     return ret;
96 }
97 #endif
98
99 audio_return_t _ucm_init(audio_hal_t *ah)
100 {
101     AUDIO_RETURN_VAL_IF_FAIL(ah, AUDIO_ERR_PARAMETER);
102
103     snd_use_case_mgr_open(&ah->ucm.uc_mgr, ALSA_DEFAULT_CARD);
104
105     if (!ah->ucm.uc_mgr) {
106         AUDIO_LOG_ERROR("uc_mgr open failed");
107         return AUDIO_ERR_RESOURCE;
108     }
109     return AUDIO_RET_OK;
110 }
111
112 audio_return_t _ucm_deinit(audio_hal_t *ah)
113 {
114     AUDIO_RETURN_VAL_IF_FAIL(ah, AUDIO_ERR_PARAMETER);
115     AUDIO_RETURN_VAL_IF_FAIL(ah->ucm.uc_mgr, AUDIO_ERR_PARAMETER);
116
117     if (ah->ucm.uc_mgr) {
118         snd_use_case_mgr_close(ah->ucm.uc_mgr);
119         ah->ucm.uc_mgr = NULL;
120     }
121
122     return AUDIO_RET_OK;
123 }
124
125 audio_return_t _ucm_get_device_name(audio_hal_t *ah, const char *use_case, audio_direction_t direction, const char **value)
126 {
127     char identifier[70] = { 0, };
128
129     AUDIO_RETURN_VAL_IF_FAIL(ah, AUDIO_ERR_PARAMETER);
130     AUDIO_RETURN_VAL_IF_FAIL(use_case, AUDIO_ERR_PARAMETER);
131     AUDIO_RETURN_VAL_IF_FAIL(ah->ucm.uc_mgr, AUDIO_ERR_PARAMETER);
132
133     snprintf(identifier, sizeof(identifier), "%sPCM//%s",
134              (direction == AUDIO_DIRECTION_IN) ? "Capture" : "Playback", use_case);
135
136     snd_use_case_get(ah->ucm.uc_mgr, identifier, value);
137
138     AUDIO_LOG_INFO("get device name : [%s]", *value);
139
140     return AUDIO_RET_OK;
141 }
142
143 /* UCM sequence
144     1) If verb is null or verb is not changed
145     1-1) If device is changed
146          (If there is request for same device, it will be ignored)
147          -> Set "Inactive" verb, disable modifiers & devices, set current verb again, enable devices & modifiers
148             (playback/capture device will be enabled again if there is no request for playback/capture device)
149     1-2) If device is not changed
150      1-2-1) If modifier is changed
151             (If there is request for same modifier, it will be ignored)
152             -> Disable modifiers, enable modifiers
153    2) If verb is changed
154       -> Reset, set new verb, enable devices & modifiers
155  */
156 audio_return_t _ucm_set_use_case(audio_hal_t *ah, const char *verb, const char *devices[], const char *modifiers[])
157 {
158     audio_return_t audio_ret = AUDIO_RET_OK;
159     int is_verb_changed = 0, is_dev_changed = 0, is_mod_changed = 0;
160     const char *old_verb = NULL, **old_dev_list = NULL, **old_mod_list = NULL;
161     int old_dev_count = 0, dev_count = 0;
162     int old_mod_count = 0, mod_count = 0;
163     const char **dis_dev_list = NULL, **ena_dev_list = NULL;
164     const char **dis_mod_list = NULL, **ena_mod_list = NULL;
165     int dis_dev_count = 0, ena_dev_count = 0;
166     int dis_mod_count = 0, ena_mod_count = 0;
167     int i = 0, j = 0;
168
169     AUDIO_RETURN_VAL_IF_FAIL(ah, AUDIO_ERR_PARAMETER);
170     AUDIO_RETURN_VAL_IF_FAIL(ah->ucm.uc_mgr, AUDIO_ERR_PARAMETER);
171     AUDIO_RETURN_VAL_IF_FAIL(verb, AUDIO_ERR_PARAMETER);
172
173     snd_use_case_get(ah->ucm.uc_mgr, "_verb", &old_verb);
174     old_dev_count = snd_use_case_get_list(ah->ucm.uc_mgr, "_enadevs", &old_dev_list);
175     old_mod_count = snd_use_case_get_list(ah->ucm.uc_mgr, "_enamods", &old_mod_list);
176     __dump_use_case(UCM_PREFIX_CURRENT, old_verb, old_dev_list, old_dev_count, old_mod_list, old_mod_count);
177
178     if (devices) {
179         for (dev_count = 0; devices[dev_count]; dev_count++);
180     }
181     if (modifiers) {
182         for (mod_count = 0; modifiers[mod_count]; mod_count++);
183     }
184
185     __dump_use_case(UCM_PREFIX_REQUESTED, verb, devices, dev_count, modifiers, mod_count);
186
187     if (old_verb && streq(verb, old_verb)) {
188         AUDIO_LOG_DEBUG("current verb and new verb is same. No need to change verb, disable devices explicitely");
189
190         if (old_dev_count > 0) {
191             dis_dev_list = (const char **)malloc(sizeof(const char *) * old_dev_count);
192             for (i = 0; i < old_dev_count; i++) {
193                 dis_dev_list[i] = NULL;
194             }
195         }
196         if (dev_count > 0) {
197             ena_dev_list = (const char **)malloc(sizeof(const char *) * dev_count);
198             for (i = 0; i < dev_count; i++) {
199                 ena_dev_list[i] = NULL;
200             }
201         }
202         if (old_mod_count > 0) {
203             dis_mod_list = (const char **)malloc(sizeof(const char *) * old_mod_count);
204             for (i = 0; i < old_mod_count; i++) {
205                 dis_mod_list[i] = NULL;
206             }
207         }
208         if (mod_count > 0) {
209             ena_mod_list = (const char **)malloc(sizeof(const char *) * mod_count);
210             for (i = 0; i < mod_count; i++) {
211                 ena_mod_list[i] = NULL;
212             }
213         }
214
215         /* update disable modifiers list which are not present in new modifier list */
216         for (i = 0; i < old_mod_count; i++) {
217             int need_disable_mod = 1;
218
219             for (j = 0; j < mod_count; j++) {
220                 if (streq(old_mod_list[i], modifiers[j])) {
221                     need_disable_mod = 0;
222                     break;
223                 }
224             }
225             if (need_disable_mod) {
226                 if (is_mod_changed == 0)
227                     is_mod_changed = 1;
228                 dis_mod_list[dis_mod_count++] = old_mod_list[i];
229             }
230         }
231
232         /* update disable devices list which are not present in new device list */
233         for (i = 0; i < old_dev_count; i++) {
234             int need_disable_dev = 1;
235
236             for (j = 0; j < dev_count; j++) {
237                 if (streq(old_dev_list[i], devices[j])) {
238                     need_disable_dev = 0;
239                     break;
240                 }
241             }
242             if (need_disable_dev) {
243                 if (is_dev_changed == 0)
244                     is_dev_changed = 1;
245                 dis_dev_list[dis_dev_count++] = old_dev_list[i];
246             }
247         }
248
249         /* update enable devices list which are not present in old device list */
250         for (i = 0; i < dev_count; i++) {
251             int need_enable_dev = 1;
252
253             for (j = 0; j < old_dev_count; j++) {
254                 if (streq(devices[i], old_dev_list[j])) {
255                     need_enable_dev = 0;
256                     break;
257                 }
258             }
259             if (need_enable_dev) {
260                 if (is_dev_changed == 0)
261                     is_dev_changed = 1;
262                 ena_dev_list[ena_dev_count++] = devices[i];
263             }
264         }
265
266         /* update enable modifiers list which are not present in old modifier list */
267         for (i = 0; i < mod_count; i++) {
268             int need_enable_mod = 1;
269
270             for (j = 0; j < old_mod_count; j++) {
271                 if (streq(modifiers[i], old_mod_list[j])) {
272                     need_enable_mod = 0;
273                     break;
274                 }
275             }
276             if (need_enable_mod) {
277                 if (is_mod_changed == 0)
278                     is_mod_changed = 1;
279                 ena_mod_list[ena_mod_count++] = modifiers[i];
280             }
281         }
282
283         /* disable modifiers */
284         for (i = 0; i < dis_mod_count; i++) {
285             AUDIO_LOG_INFO("Disable modifier : %s", dis_mod_list[i]);
286             if (snd_use_case_set(ah->ucm.uc_mgr, "_dismod", dis_mod_list[i]) < 0)
287                 AUDIO_LOG_ERROR("disable %s modifier failed", dis_mod_list[i]);
288         }
289
290         /* disable devices */
291         for (i = 0; i < dis_dev_count; i++) {
292             AUDIO_LOG_INFO("Disable device : %s", dis_dev_list[i]);
293             if (snd_use_case_set(ah->ucm.uc_mgr, "_disdev", dis_dev_list[i]) < 0)
294                 AUDIO_LOG_ERROR("disable %s device failed", dis_dev_list[i]);
295         }
296
297         /* enable devices */
298         for (i = 0; i < ena_dev_count; i++) {
299             AUDIO_LOG_INFO("Enable device : %s", ena_dev_list[i]);
300             if (snd_use_case_set(ah->ucm.uc_mgr, "_enadev", ena_dev_list[i]) < 0)
301                 AUDIO_LOG_ERROR("enable %s device failed", ena_dev_list[i]);
302         }
303
304         /* enable modifiers */
305         for (i = 0; i < ena_mod_count; i++) {
306             AUDIO_LOG_INFO("Enable modifier : %s", ena_mod_list[i]);
307             if (snd_use_case_set(ah->ucm.uc_mgr, "_enamod", ena_mod_list[i]) < 0)
308                 AUDIO_LOG_ERROR("enable %s modifier failed", ena_mod_list[i]);
309         }
310     } else {
311         is_verb_changed = 1;
312
313         AUDIO_LOG_DEBUG("Setting new verb: %s", verb);
314         /* set new verb */
315         if (snd_use_case_set(ah->ucm.uc_mgr, "_verb", verb) < 0) {
316             AUDIO_LOG_ERROR("Setting verb %s failed", verb);
317             audio_ret = AUDIO_ERR_UNDEFINED;
318             goto exit;
319         }
320         /* enable devices */
321         for (i = 0; i < dev_count; i++) {
322             AUDIO_LOG_DEBUG("Enable device : %s", devices[i]);
323             if (snd_use_case_set(ah->ucm.uc_mgr, "_enadev", devices[i]) < 0)
324                 AUDIO_LOG_ERROR("Enable %s device failed", devices[i]);
325         }
326         /* enable modifiers */
327         for (i = 0; i < mod_count; i++) {
328             AUDIO_LOG_DEBUG("Enable modifier : %s", modifiers[i]);
329             if (snd_use_case_set(ah->ucm.uc_mgr, "_enamod", modifiers[i]) < 0)
330                 AUDIO_LOG_ERROR("Enable %s modifier failed", modifiers[i]);
331         }
332     }
333
334 exit:
335     if (old_verb)
336         free((void *)old_verb);
337     if (old_dev_list)
338         snd_use_case_free_list(old_dev_list, old_dev_count);
339     if (old_mod_list)
340         snd_use_case_free_list(old_mod_list, old_mod_count);
341     if (dis_dev_list)
342         free((void *)dis_dev_list);
343     if (ena_dev_list)
344         free((void *)ena_dev_list);
345     if (dis_mod_list)
346         free((void *)dis_mod_list);
347     if (ena_mod_list)
348         free((void *)ena_mod_list);
349
350     if (is_verb_changed == 1 || is_dev_changed == 1 || is_mod_changed == 1) {
351         const char *new_verb = NULL, **new_dev_list = NULL, **new_mod_list = NULL;
352         int new_dev_count = 0, new_mod_count = 0;
353
354         snd_use_case_get(ah->ucm.uc_mgr, "_verb", &new_verb);
355         new_dev_count = snd_use_case_get_list(ah->ucm.uc_mgr, "_enadevs", &new_dev_list);
356         new_mod_count = snd_use_case_get_list(ah->ucm.uc_mgr, "_enamods", &new_mod_list);
357         __dump_use_case(UCM_PREFIX_CHANGED, new_verb, new_dev_list, new_dev_count, new_mod_list, new_mod_count);
358
359         if (new_verb)
360             free((void *)new_verb);
361         if (new_dev_list)
362             snd_use_case_free_list(new_dev_list, new_dev_count);
363         if (new_mod_list)
364             snd_use_case_free_list(new_mod_list, new_mod_count);
365     }
366
367     return audio_ret;
368 }
369
370 audio_return_t _ucm_set_devices(audio_hal_t *ah, const char *verb, const char *devices[])
371 {
372     audio_return_t audio_ret = AUDIO_RET_OK;
373     int is_verb_changed = 0, is_dev_changed = 0;
374     const char *old_verb = NULL, **old_dev_list = NULL;
375     int old_dev_count = 0, dev_count = 0;
376     const char **dis_dev_list = NULL, **ena_dev_list = NULL;
377     int dis_dev_count = 0, ena_dev_count = 0;
378     int i = 0, j = 0;
379
380     AUDIO_RETURN_VAL_IF_FAIL(ah, AUDIO_ERR_PARAMETER);
381     AUDIO_RETURN_VAL_IF_FAIL(ah->ucm.uc_mgr, AUDIO_ERR_PARAMETER);
382     AUDIO_RETURN_VAL_IF_FAIL(verb, AUDIO_ERR_PARAMETER);
383     AUDIO_RETURN_VAL_IF_FAIL(devices, AUDIO_ERR_PARAMETER);
384
385     snd_use_case_get(ah->ucm.uc_mgr, "_verb", &old_verb);
386     old_dev_count = snd_use_case_get_list(ah->ucm.uc_mgr, "_enadevs", &old_dev_list);
387     __dump_use_case(UCM_PREFIX_CURRENT, old_verb, old_dev_list, old_dev_count, NULL, 0);
388
389     if (devices) {
390         if (streq(verb, "Voice")) {
391             /* In case of Voice verb with Bluetooth device, make this device alone */
392             for (dev_count = 0; devices[dev_count]; dev_count++) {
393                 if (streq(devices[dev_count], "Bluetooth")) {
394                     devices = &devices[dev_count];
395                     dev_count = 1;
396                     AUDIO_LOG_DEBUG("Voice verb with Bluetooth device only");
397                     break;
398                 }
399             }
400         } else {
401             for (dev_count = 0; devices[dev_count]; dev_count++);
402         }
403     }
404
405     __dump_use_case(UCM_PREFIX_REQUESTED, verb, devices, dev_count, NULL, 0);
406
407     if (old_verb && streq(verb, old_verb)) {
408         AUDIO_LOG_DEBUG("current verb and new verb is same. No need to change verb, disable devices explicitely");
409
410         if (old_dev_count > 0) {
411             dis_dev_list = (const char **)malloc(sizeof(const char *) * old_dev_count);
412             for (i = 0; i < old_dev_count; i++) {
413                 dis_dev_list[i] = NULL;
414             }
415         }
416         if (dev_count > 0) {
417             ena_dev_list = (const char **)malloc(sizeof(const char *) * dev_count);
418             for (i = 0; i < dev_count; i++) {
419                 ena_dev_list[i] = NULL;
420             }
421         }
422
423         /* update disable devices list which are not present in new device list */
424         for (i = 0; i < old_dev_count; i++) {
425             int need_disable_dev = 1;
426
427             for (j = 0; j < dev_count; j++) {
428                 if (streq(old_dev_list[i], devices[j])) {
429                     need_disable_dev = 0;
430                     break;
431                 }
432             }
433             if (need_disable_dev) {
434                 if (is_dev_changed == 0)
435                     is_dev_changed = 1;
436                 dis_dev_list[dis_dev_count++] = old_dev_list[i];
437             }
438         }
439
440         /* update enable devices list which are not present in old device list */
441         for (i = 0; i < dev_count; i++) {
442             int need_enable_dev = 1;
443
444             for (j = 0; j < old_dev_count; j++) {
445                 if (streq(devices[i], old_dev_list[j])) {
446                     need_enable_dev = 0;
447                     break;
448                 }
449             }
450             if (need_enable_dev) {
451                 if (is_dev_changed == 0)
452                     is_dev_changed = 1;
453                 ena_dev_list[ena_dev_count++] = devices[i];
454             }
455         }
456
457         /* disable devices */
458         for (i = 0; i < dis_dev_count; i++) {
459             AUDIO_LOG_INFO("Disable device : %s", dis_dev_list[i]);
460             if (snd_use_case_set(ah->ucm.uc_mgr, "_disdev", dis_dev_list[i]) < 0)
461                 AUDIO_LOG_ERROR("disable %s device failed", dis_dev_list[i]);
462         }
463
464         /* enable devices */
465         for (i = 0; i < ena_dev_count; i++) {
466             AUDIO_LOG_INFO("Enable device : %s", ena_dev_list[i]);
467             if (snd_use_case_set(ah->ucm.uc_mgr, "_enadev", ena_dev_list[i]) < 0)
468                 AUDIO_LOG_ERROR("enable %s device failed", ena_dev_list[i]);
469         }
470
471     } else {
472         is_verb_changed = 1;
473
474         AUDIO_LOG_INFO("Setting new verb: %s", verb);
475         /* set new verb */
476         if (snd_use_case_set(ah->ucm.uc_mgr, "_verb", verb) < 0) {
477             AUDIO_LOG_ERROR("Setting verb %s failed", verb);
478             audio_ret = AUDIO_ERR_UNDEFINED;
479             goto exit;
480         }
481         /* enable devices */
482         for (i = 0; i < dev_count; i++) {
483             AUDIO_LOG_INFO("Enable device : %s", devices[i]);
484             if (snd_use_case_set(ah->ucm.uc_mgr, "_enadev", devices[i]) < 0)
485                 AUDIO_LOG_ERROR("Enable %s device failed", devices[i]);
486         }
487     }
488
489 exit:
490     if (old_verb)
491         free((void *)old_verb);
492     if (old_dev_list)
493         snd_use_case_free_list(old_dev_list, old_dev_count);
494     if (dis_dev_list)
495         free((void *)dis_dev_list);
496     if (ena_dev_list)
497         free((void *)ena_dev_list);
498
499     if (is_verb_changed == 1 || is_dev_changed == 1) {
500         const char *new_verb = NULL, **new_dev_list = NULL;
501         int new_dev_count = 0;
502
503         snd_use_case_get(ah->ucm.uc_mgr, "_verb", &new_verb);
504         new_dev_count = snd_use_case_get_list(ah->ucm.uc_mgr, "_enadevs", &new_dev_list);
505         __dump_use_case(UCM_PREFIX_CHANGED, new_verb, new_dev_list, new_dev_count, NULL, 0);
506
507         if (new_verb)
508             free((void *)new_verb);
509         if (new_dev_list)
510             snd_use_case_free_list(new_dev_list, new_dev_count);
511     }
512
513     return audio_ret;
514
515 }
516
517 audio_return_t _ucm_set_modifiers(audio_hal_t *ah, const char *verb, const char *modifiers[])
518 {
519     audio_return_t audio_ret = AUDIO_RET_OK;
520     int is_verb_changed = 0, is_mod_changed = 0;
521     const char *old_verb = NULL, **old_mod_list = NULL;
522     int old_mod_count = 0, mod_count = 0;
523     const char **dis_mod_list = NULL, **ena_mod_list = NULL;
524     int dis_mod_count = 0, ena_mod_count = 0;
525     int i = 0, j = 0;
526
527     AUDIO_RETURN_VAL_IF_FAIL(ah, AUDIO_ERR_PARAMETER);
528     AUDIO_RETURN_VAL_IF_FAIL(ah->ucm.uc_mgr, AUDIO_ERR_PARAMETER);
529     AUDIO_RETURN_VAL_IF_FAIL(verb, AUDIO_ERR_PARAMETER);
530     AUDIO_RETURN_VAL_IF_FAIL(modifiers, AUDIO_ERR_PARAMETER);
531
532     snd_use_case_get(ah->ucm.uc_mgr, "_verb", &old_verb);
533     old_mod_count = snd_use_case_get_list(ah->ucm.uc_mgr, "_enamods", &old_mod_list);
534     __dump_use_case(UCM_PREFIX_CURRENT, old_verb, NULL, 0, old_mod_list, old_mod_count);
535
536     if (modifiers) {
537         for (mod_count = 0; modifiers[mod_count]; mod_count++);
538     }
539
540     __dump_use_case(UCM_PREFIX_REQUESTED, verb, NULL, 0, modifiers, mod_count);
541
542     if (old_verb && streq(verb, old_verb)) {
543         AUDIO_LOG_DEBUG("current verb and new verb is same. No need to change verb, disable devices explicitely");
544
545         if (old_mod_count > 0) {
546             dis_mod_list = (const char **)malloc(sizeof(const char *) * old_mod_count);
547             for (i = 0; i < old_mod_count; i++) {
548                 dis_mod_list[i] = NULL;
549             }
550         }
551         if (mod_count > 0) {
552             ena_mod_list = (const char **)malloc(sizeof(const char *) * mod_count);
553             for (i = 0; i < mod_count; i++) {
554                 ena_mod_list[i] = NULL;
555             }
556         }
557
558         /* update disable modifiers list which are not present in new modifier list */
559         for (i = 0; i < old_mod_count; i++) {
560             int need_disable_mod = 1;
561
562             for (j = 0; j < mod_count; j++) {
563                 if (streq(old_mod_list[i], modifiers[j])) {
564                     need_disable_mod = 0;
565                     break;
566                 }
567             }
568             if (need_disable_mod) {
569                 if (is_mod_changed == 0)
570                     is_mod_changed = 1;
571                 dis_mod_list[dis_mod_count++] = old_mod_list[i];
572             }
573         }
574
575         /* update enable modifiers list which are not present in old modifier list */
576         for (i = 0; i < mod_count; i++) {
577             int need_enable_mod = 1;
578
579             for (j = 0; j < old_mod_count; j++) {
580                 if (streq(modifiers[i], old_mod_list[j])) {
581                     need_enable_mod = 0;
582                     break;
583                 }
584             }
585             if (need_enable_mod) {
586                 if (is_mod_changed == 0)
587                     is_mod_changed = 1;
588                 ena_mod_list[ena_mod_count++] = modifiers[i];
589             }
590         }
591
592         /* disable modifiers */
593         for (i = 0; i < dis_mod_count; i++) {
594             AUDIO_LOG_INFO("Disable modifier : %s", dis_mod_list[i]);
595             if (snd_use_case_set(ah->ucm.uc_mgr, "_dismod", dis_mod_list[i]) < 0)
596                 AUDIO_LOG_ERROR("disable %s modifier failed", dis_mod_list[i]);
597         }
598
599         /* enable modifiers */
600         for (i = 0; i < ena_mod_count; i++) {
601             AUDIO_LOG_INFO("Enable modifier : %s", ena_mod_list[i]);
602             if (snd_use_case_set(ah->ucm.uc_mgr, "_enamod", ena_mod_list[i]) < 0)
603                 AUDIO_LOG_ERROR("enable %s modifier failed", ena_mod_list[i]);
604         }
605     } else {
606         is_verb_changed = 1;
607
608         AUDIO_LOG_DEBUG("Setting new verb: %s", verb);
609         /* set new verb */
610         if (snd_use_case_set(ah->ucm.uc_mgr, "_verb", verb) < 0) {
611             AUDIO_LOG_ERROR("Setting verb %s failed", verb);
612             audio_ret = AUDIO_ERR_UNDEFINED;
613             goto exit;
614         }
615         /* enable modifiers */
616         for (i = 0; i < mod_count; i++) {
617             AUDIO_LOG_DEBUG("Enable modifier : %s", modifiers[i]);
618             if (snd_use_case_set(ah->ucm.uc_mgr, "_enamod", modifiers[i]) < 0)
619                 AUDIO_LOG_ERROR("Enable %s modifier failed", modifiers[i]);
620         }
621     }
622
623 exit:
624     if (old_verb)
625         free((void *)old_verb);
626     if (old_mod_list)
627         snd_use_case_free_list(old_mod_list, old_mod_count);
628     if (dis_mod_list)
629         free((void *)dis_mod_list);
630     if (ena_mod_list)
631         free((void *)ena_mod_list);
632
633     if (is_verb_changed == 1 || is_mod_changed == 1) {
634         const char *new_verb = NULL, **new_mod_list = NULL;
635         int new_mod_count = 0;
636
637         snd_use_case_get(ah->ucm.uc_mgr, "_verb", &new_verb);
638         new_mod_count = snd_use_case_get_list(ah->ucm.uc_mgr, "_enamods", &new_mod_list);
639         __dump_use_case(UCM_PREFIX_CHANGED, new_verb, NULL, 0, new_mod_list, new_mod_count);
640
641         if (new_verb)
642             free((void *)new_verb);
643         if (new_mod_list)
644             snd_use_case_free_list(new_mod_list, new_mod_count);
645     }
646
647     return audio_ret;
648 }
649
650 audio_return_t _ucm_get_verb(audio_hal_t *ah, const char **value)
651 {
652     audio_return_t ret = AUDIO_RET_OK;
653
654     AUDIO_RETURN_VAL_IF_FAIL(ah, AUDIO_ERR_PARAMETER);
655     AUDIO_RETURN_VAL_IF_FAIL(ah->ucm.uc_mgr, AUDIO_ERR_PARAMETER);
656     AUDIO_RETURN_VAL_IF_FAIL(value, AUDIO_ERR_PARAMETER);
657
658     if ((ret = snd_use_case_get(ah->ucm.uc_mgr, "_verb", value)) < 0) {
659         AUDIO_LOG_ERROR("Getting current verb failed: Reason %d", ret);
660         ret = AUDIO_ERR_UNDEFINED;
661     }
662
663     return ret;
664 }
665
666 audio_return_t _ucm_reset_use_case(audio_hal_t *ah)
667 {
668     audio_return_t ret = AUDIO_RET_OK;
669
670     AUDIO_RETURN_VAL_IF_FAIL(ah, AUDIO_ERR_PARAMETER);
671     AUDIO_RETURN_VAL_IF_FAIL(ah->ucm.uc_mgr, AUDIO_ERR_PARAMETER);
672
673     AUDIO_LOG_INFO(">>> UCM reset Verb [ %s ]", AUDIO_USE_CASE_VERB_INACTIVE);
674
675     if ((ret = snd_use_case_set(ah->ucm.uc_mgr, "_verb", AUDIO_USE_CASE_VERB_INACTIVE)) < 0) {
676         AUDIO_LOG_ERROR("Reset use case failed: Reason %d", ret);
677         ret = AUDIO_ERR_UNDEFINED;
678     }
679
680     return ret;
681 }