blob: 4950e28f9f10dda6428d335da019608cef59a446 [file] [log] [blame]
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08001/*
2 * (C) 2014 DTS, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <utils/Log.h>
18#include <stdlib.h>
Mingming Yin497419f2015-07-01 16:57:32 -070019#include <string.h>
Jitendra Naruka1b6513f2014-11-22 19:34:13 -080020#include "effect_util.h"
Sharad Sangleb27354b2015-06-18 15:58:55 +053021#include <string.h>
Vinay Vermaaddfa4a2018-04-29 14:03:38 +053022#include <unistd.h>
23#include <stdio.h>
Jitendra Naruka1b6513f2014-11-22 19:34:13 -080024
25#ifdef LOG_TAG
26#undef LOG_TAG
27#endif
28#define LOG_TAG "effect_util"
29
30/*#define LOG_NDEBUG 0*/
31
32enum {
33 EQUALIZER,
34 VIRTUALIZER,
35 BASSBOOST,
36};
37
38static const char *paramList[10] = {
39 "eq_enable",
40 "virt_enable",
41 "bb_enable",
42 "eq_param_level0",
43 "eq_param_level1",
44 "eq_param_level2",
45 "eq_param_level3",
46 "eq_param_level4",
47 "virt_param_strength",
48 "bassboost_param_strength"
49};
50
Dhanalakshmi Siddani79b98592015-03-23 11:59:02 +053051#define EFFECT_FILE "/data/misc/dts/effect"
Jitendra Naruka1b6513f2014-11-22 19:34:13 -080052#define MAX_LENGTH_OF_INTEGER_IN_STRING 13
53
54#ifdef DTS_EAGLE
55void create_effect_state_node(int device_id)
56{
57 char prop[PROPERTY_VALUE_MAX];
58 int fd;
59 char buf[1024];
60 char path[PATH_MAX];
61 char value[MAX_LENGTH_OF_INTEGER_IN_STRING];
62
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -070063 property_get("vendor.audio.use.dts_eagle", prop, "0");
Jitendra Naruka1b6513f2014-11-22 19:34:13 -080064 if (!strncmp("true", prop, sizeof("true")) || atoi(prop)) {
65 ALOGV("create_effect_node for - device_id: %d", device_id);
66 strlcpy(path, EFFECT_FILE, sizeof(path));
67 snprintf(value, sizeof(value), "%d", device_id);
68 strlcat(path, value, sizeof(path));
69 if ((fd=open(path, O_RDONLY)) < 0) {
70 ALOGV("No File exist");
71 } else {
72 ALOGV("A file with the same name exist. So, not creating again");
73 return;
74 }
75 if ((fd=creat(path, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)) < 0) {
76 ALOGE("opening effect state node failed returned");
77 return;
78 }
79 chmod(path, S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH);
80 snprintf(buf, sizeof(buf), "eq_enable=%d;virt_enable=%d;bb_enable=%d;eq_param_level0=%d;eq_param_level1=%d;eq_param_level2=%d;eq_param_level3=%d;eq_param_level4=%d;virt_param_strength=%d;bassboost_param_strength=%d", 0,0,0,0,0,0,0,0,0,0);
81 int n = write(fd, buf, strlen(buf));
82 ALOGV("number of bytes written: %d", n);
83 close(fd);
84 }
85}
86
87void update_effects_node(int device_id, int effect_type, int enable_or_set, int enable_disable, int strength, int eq_band, int eq_level)
88{
89 char prop[PROPERTY_VALUE_MAX];
90 char buf[1024];
91 int fd = 0;
92 int paramValue = 0;
93 char path[PATH_MAX];
94 char value[MAX_LENGTH_OF_INTEGER_IN_STRING];
95 char parameterValue[MAX_LENGTH_OF_INTEGER_IN_STRING];
96 int keyParamIndex = -1; //index in the paramlist array which has to be updated
97 char *s1, *s2;
98 char resultBuf[1024];
99 int index1 = -1;
100 //ALOGV("value of device_id and effect_type is %d and %d", device_id, effect_type);
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -0700101 property_get("vendor.audio.use.dts_eagle", prop, "0");
Jitendra Naruka1b6513f2014-11-22 19:34:13 -0800102 if (!strncmp("true", prop, sizeof("true")) || atoi(prop)) {
103 strlcpy(path, EFFECT_FILE, sizeof(path));
104 snprintf(value, sizeof(value), "%d", device_id);
105 strlcat(path, value, sizeof(path));
106 switch (effect_type)
107 {
108 case EQUALIZER:
109 if (enable_or_set) {
110 keyParamIndex = 0;
111 paramValue = enable_disable;
112 } else {
113 switch (eq_band) {
114 case 0:
115 keyParamIndex = 3;
116 break;
117 case 1:
118 keyParamIndex = 4;
119 break;
120 case 2:
121 keyParamIndex = 5;
122 break;
123 case 3:
124 keyParamIndex = 6;
125 break;
126 case 4:
127 keyParamIndex = 7;
128 break;
129 default:
130 break;
131 }
132 paramValue = eq_level;
133 }
134 break;
135 case VIRTUALIZER:
136 if(enable_or_set) {
137 keyParamIndex = 1;
138 paramValue = enable_disable;
139 } else {
140 keyParamIndex = 8;
141 paramValue = strength;
142 }
143 break;
144 case BASSBOOST:
145 if (enable_or_set) {
146 keyParamIndex = 2;
147 paramValue = enable_disable;
148 } else {
149 keyParamIndex = 9;
150 paramValue = strength;
151 }
152 break;
153 default:
154 break;
155 }
156 if(keyParamIndex !=-1) {
157 FILE *fp;
158 fp = fopen(path,"r");
159 if (fp != NULL) {
160 memset(buf, 0, 1024);
161 memset(resultBuf, 0, 1024);
162 if (fgets(buf, 1024, fp) != NULL) {
163 s1 = strstr(buf, paramList[keyParamIndex]);
164 s2 = strstr(s1,";");
165 index1 = s1 - buf;
166 strncpy(resultBuf, buf, index1);
167 strncat(resultBuf, paramList[keyParamIndex], sizeof(resultBuf)-strlen(resultBuf)-1);
168 strncat(resultBuf, "=", sizeof(resultBuf)-strlen(resultBuf)-1);
169 snprintf(parameterValue, sizeof(parameterValue), "%d", paramValue);
170 strncat(resultBuf, parameterValue, sizeof(resultBuf)-strlen(resultBuf)-1);
171 if (s2)
172 strncat(resultBuf, s2, sizeof(resultBuf)-strlen(resultBuf)-1);
173 fclose(fp);
174 if ((fd=open(path, O_TRUNC|O_WRONLY)) < 0) {
175 ALOGV("opening file for writing failed");
176 return;
177 }
178 int n = write(fd, resultBuf, strlen(resultBuf));
179 close(fd);
180 ALOGV("number of bytes written: %d", n);
181 } else {
182 ALOGV("file could not be read");
183 fclose(fp);
184 }
185 } else
186 ALOGV("file could not be opened");
187 }
188 }
189}
190
191void remove_effect_state_node(int device_id)
192{
193 char prop[PROPERTY_VALUE_MAX];
194 int fd;
195 char path[PATH_MAX];
196 char value[MAX_LENGTH_OF_INTEGER_IN_STRING];
197
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -0700198 property_get("vendor.audio.use.dts_eagle", prop, "0");
Jitendra Naruka1b6513f2014-11-22 19:34:13 -0800199 if (!strncmp("true", prop, sizeof("true")) || atoi(prop)) {
200 ALOGV("remove_state_notifier_node: device_id - %d", device_id);
201 strlcpy(path, EFFECT_FILE, sizeof(path));
202 snprintf(value, sizeof(value), "%d", device_id);
203 strlcat(path, value, sizeof(path));
204 if ((fd=open(path, O_RDONLY)) < 0) {
205 ALOGV("open effect state node failed");
206 } else {
207 ALOGV("open effect state node successful");
208 ALOGV("Remove the file");
209 close(fd);
210 remove(path);
211 }
212 }
213}
214#endif