blob: 5dc53caec738544a92ec890552b27d243db0711d
1 | #define LOG_TAG "audio_hw_utils" |
2 | #define LOG_NDEBUG 0 |
3 | |
4 | #include <errno.h> |
5 | #include <pthread.h> |
6 | #include <stdint.h> |
7 | #include <sys/time.h> |
8 | #include <stdlib.h> |
9 | #include <sys/stat.h> |
10 | #include <fcntl.h> |
11 | #include <time.h> |
12 | #include <utils/Timers.h> |
13 | #include <cutils/log.h> |
14 | #include <cutils/str_parms.h> |
15 | #include <cutils/properties.h> |
16 | #include <linux/ioctl.h> |
17 | #include <hardware/hardware.h> |
18 | #include <system/audio.h> |
19 | #include <hardware/audio.h> |
20 | #include <sound/asound.h> |
21 | #include <tinyalsa/asoundlib.h> |
22 | |
23 | #include "audio_hw_utils.h" |
24 | |
25 | #include "audio_hwsync.h" |
26 | #include "audio_hw.h" |
27 | |
28 | #ifdef LOG_NDEBUG_FUNCTION |
29 | #define LOGFUNC(...) ((void)0) |
30 | #else |
31 | #define LOGFUNC(...) (ALOGD(__VA_ARGS__)) |
32 | #endif |
33 | int get_sysfs_int16(const char *path, int *value) |
34 | { |
35 | int fd; |
36 | char valstr[64]; |
37 | int val = 0; |
38 | fd = open(path, O_RDONLY); |
39 | if (fd >= 0) { |
40 | memset(valstr, 0, 64); |
41 | read(fd, valstr, 64 - 1); |
42 | valstr[strlen(valstr)] = '\0'; |
43 | close(fd); |
44 | } else { |
45 | ALOGE("unable to open file %s\n", path); |
46 | return -1; |
47 | } |
48 | if (sscanf(valstr, "0x%x", &val) < 1) { |
49 | ALOGE("unable to get pts from: %s", valstr); |
50 | return -1; |
51 | } |
52 | *value = val; |
53 | return 0; |
54 | } |
55 | |
56 | int sysfs_set_sysfs_str(const char *path, const char *val) |
57 | { |
58 | int fd; |
59 | int bytes; |
60 | fd = open(path, O_CREAT | O_RDWR | O_TRUNC, 0644); |
61 | if (fd >= 0) { |
62 | bytes = write(fd, val, strlen(val)); |
63 | close(fd); |
64 | return 0; |
65 | } else { |
66 | ALOGE("unable to open file %s,err: %s", path, strerror(errno)); |
67 | } |
68 | return -1; |
69 | } |
70 | |
71 | int get_sysfs_int (const char *path) |
72 | { |
73 | int val = 0; |
74 | int fd = open (path, O_RDONLY); |
75 | if (fd >= 0) |
76 | { |
77 | char bcmd[16]; |
78 | read (fd, bcmd, sizeof (bcmd)); |
79 | val = strtol (bcmd, NULL, 10); |
80 | close (fd); |
81 | } |
82 | else |
83 | { |
84 | LOGFUNC ("[%s]open %s node failed! return 0\n", path, __FUNCTION__); |
85 | } |
86 | return val; |
87 | } |
88 | int mystrstr(char *mystr,char *substr) { |
89 | int i=0; |
90 | int j=0; |
91 | int score = 0; |
92 | int substrlen = strlen(substr); |
93 | int ok = 0; |
94 | for (i =0;i < 1024 - substrlen;i++) { |
95 | for (j = 0;j < substrlen;j++) { |
96 | score += (substr[j] == mystr[i+j])?1:0; |
97 | } |
98 | if (score == substrlen) { |
99 | ok = 1; |
100 | break; |
101 | } |
102 | score = 0; |
103 | } |
104 | return ok; |
105 | } |
106 | void set_codec_type(int type) |
107 | { |
108 | char buf[16]; |
109 | int fd = open ("/sys/class/audiodsp/digital_codec", O_WRONLY); |
110 | |
111 | if (fd >= 0) { |
112 | memset(buf, 0, sizeof(buf)); |
113 | snprintf(buf, sizeof(buf), "%d", type); |
114 | |
115 | write(fd, buf, sizeof(buf)); |
116 | close(fd); |
117 | } |
118 | } |
119 | unsigned char codec_type_is_raw_data(int type) |
120 | { |
121 | switch (type) { |
122 | case TYPE_AC3: |
123 | case TYPE_EAC3: |
124 | case TYPE_TRUE_HD: |
125 | case TYPE_DTS: |
126 | case TYPE_DTS_HD: |
127 | case TYPE_DTS_HD_MA: |
128 | return 1; |
129 | default: |
130 | return 0; |
131 | } |
132 | } |
133 | |
134 | int get_codec_type(int format) |
135 | { |
136 | switch (format) { |
137 | case AUDIO_FORMAT_AC3: |
138 | return TYPE_AC3; |
139 | case AUDIO_FORMAT_E_AC3: |
140 | return TYPE_EAC3; |
141 | case AUDIO_FORMAT_DTS: |
142 | return TYPE_DTS; |
143 | case AUDIO_FORMAT_DTS_HD: |
144 | return TYPE_DTS_HD_MA; |
145 | case AUDIO_FORMAT_TRUEHD: |
146 | return TYPE_TRUE_HD; |
147 | case AUDIO_FORMAT_PCM: |
148 | return TYPE_PCM; |
149 | default: |
150 | return TYPE_PCM; |
151 | } |
152 | } |
153 | int getprop_bool (const char *path) |
154 | { |
155 | char buf[PROPERTY_VALUE_MAX]; |
156 | int ret = -1; |
157 | |
158 | ret = property_get (path, buf, NULL); |
159 | if (ret > 0) |
160 | { |
161 | if (strcasecmp (buf, "true") == 0 || strcmp (buf, "1") == 0) |
162 | return 1; |
163 | } |
164 | return 0; |
165 | } |
166 | |
167 |