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