blob: cda89bcc400ff3cc67bb3ef1e3df28f6332b893c [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>
19#include "effect_util.h"
20
21#ifdef LOG_TAG
22#undef LOG_TAG
23#endif
24#define LOG_TAG "effect_util"
25
26/*#define LOG_NDEBUG 0*/
27
28enum {
29 EQUALIZER,
30 VIRTUALIZER,
31 BASSBOOST,
32};
33
34static const char *paramList[10] = {
35 "eq_enable",
36 "virt_enable",
37 "bb_enable",
38 "eq_param_level0",
39 "eq_param_level1",
40 "eq_param_level2",
41 "eq_param_level3",
42 "eq_param_level4",
43 "virt_param_strength",
44 "bassboost_param_strength"
45};
46
47#define EFFECT_FILE "/data/data/dts/effect"
48#define MAX_LENGTH_OF_INTEGER_IN_STRING 13
49
50#ifdef DTS_EAGLE
51void create_effect_state_node(int device_id)
52{
53 char prop[PROPERTY_VALUE_MAX];
54 int fd;
55 char buf[1024];
56 char path[PATH_MAX];
57 char value[MAX_LENGTH_OF_INTEGER_IN_STRING];
58
59 property_get("use.dts_eagle", prop, "0");
60 if (!strncmp("true", prop, sizeof("true")) || atoi(prop)) {
61 ALOGV("create_effect_node for - device_id: %d", device_id);
62 strlcpy(path, EFFECT_FILE, sizeof(path));
63 snprintf(value, sizeof(value), "%d", device_id);
64 strlcat(path, value, sizeof(path));
65 if ((fd=open(path, O_RDONLY)) < 0) {
66 ALOGV("No File exist");
67 } else {
68 ALOGV("A file with the same name exist. So, not creating again");
69 return;
70 }
71 if ((fd=creat(path, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)) < 0) {
72 ALOGE("opening effect state node failed returned");
73 return;
74 }
75 chmod(path, S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH);
76 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);
77 int n = write(fd, buf, strlen(buf));
78 ALOGV("number of bytes written: %d", n);
79 close(fd);
80 }
81}
82
83void update_effects_node(int device_id, int effect_type, int enable_or_set, int enable_disable, int strength, int eq_band, int eq_level)
84{
85 char prop[PROPERTY_VALUE_MAX];
86 char buf[1024];
87 int fd = 0;
88 int paramValue = 0;
89 char path[PATH_MAX];
90 char value[MAX_LENGTH_OF_INTEGER_IN_STRING];
91 char parameterValue[MAX_LENGTH_OF_INTEGER_IN_STRING];
92 int keyParamIndex = -1; //index in the paramlist array which has to be updated
93 char *s1, *s2;
94 char resultBuf[1024];
95 int index1 = -1;
96 //ALOGV("value of device_id and effect_type is %d and %d", device_id, effect_type);
97 property_get("use.dts_eagle", prop, "0");
98 if (!strncmp("true", prop, sizeof("true")) || atoi(prop)) {
99 strlcpy(path, EFFECT_FILE, sizeof(path));
100 snprintf(value, sizeof(value), "%d", device_id);
101 strlcat(path, value, sizeof(path));
102 switch (effect_type)
103 {
104 case EQUALIZER:
105 if (enable_or_set) {
106 keyParamIndex = 0;
107 paramValue = enable_disable;
108 } else {
109 switch (eq_band) {
110 case 0:
111 keyParamIndex = 3;
112 break;
113 case 1:
114 keyParamIndex = 4;
115 break;
116 case 2:
117 keyParamIndex = 5;
118 break;
119 case 3:
120 keyParamIndex = 6;
121 break;
122 case 4:
123 keyParamIndex = 7;
124 break;
125 default:
126 break;
127 }
128 paramValue = eq_level;
129 }
130 break;
131 case VIRTUALIZER:
132 if(enable_or_set) {
133 keyParamIndex = 1;
134 paramValue = enable_disable;
135 } else {
136 keyParamIndex = 8;
137 paramValue = strength;
138 }
139 break;
140 case BASSBOOST:
141 if (enable_or_set) {
142 keyParamIndex = 2;
143 paramValue = enable_disable;
144 } else {
145 keyParamIndex = 9;
146 paramValue = strength;
147 }
148 break;
149 default:
150 break;
151 }
152 if(keyParamIndex !=-1) {
153 FILE *fp;
154 fp = fopen(path,"r");
155 if (fp != NULL) {
156 memset(buf, 0, 1024);
157 memset(resultBuf, 0, 1024);
158 if (fgets(buf, 1024, fp) != NULL) {
159 s1 = strstr(buf, paramList[keyParamIndex]);
160 s2 = strstr(s1,";");
161 index1 = s1 - buf;
162 strncpy(resultBuf, buf, index1);
163 strncat(resultBuf, paramList[keyParamIndex], sizeof(resultBuf)-strlen(resultBuf)-1);
164 strncat(resultBuf, "=", sizeof(resultBuf)-strlen(resultBuf)-1);
165 snprintf(parameterValue, sizeof(parameterValue), "%d", paramValue);
166 strncat(resultBuf, parameterValue, sizeof(resultBuf)-strlen(resultBuf)-1);
167 if (s2)
168 strncat(resultBuf, s2, sizeof(resultBuf)-strlen(resultBuf)-1);
169 fclose(fp);
170 if ((fd=open(path, O_TRUNC|O_WRONLY)) < 0) {
171 ALOGV("opening file for writing failed");
172 return;
173 }
174 int n = write(fd, resultBuf, strlen(resultBuf));
175 close(fd);
176 ALOGV("number of bytes written: %d", n);
177 } else {
178 ALOGV("file could not be read");
179 fclose(fp);
180 }
181 } else
182 ALOGV("file could not be opened");
183 }
184 }
185}
186
187void remove_effect_state_node(int device_id)
188{
189 char prop[PROPERTY_VALUE_MAX];
190 int fd;
191 char path[PATH_MAX];
192 char value[MAX_LENGTH_OF_INTEGER_IN_STRING];
193
194 property_get("use.dts_eagle", prop, "0");
195 if (!strncmp("true", prop, sizeof("true")) || atoi(prop)) {
196 ALOGV("remove_state_notifier_node: device_id - %d", device_id);
197 strlcpy(path, EFFECT_FILE, sizeof(path));
198 snprintf(value, sizeof(value), "%d", device_id);
199 strlcat(path, value, sizeof(path));
200 if ((fd=open(path, O_RDONLY)) < 0) {
201 ALOGV("open effect state node failed");
202 } else {
203 ALOGV("open effect state node successful");
204 ALOGV("Remove the file");
205 close(fd);
206 remove(path);
207 }
208 }
209}
210#endif