blob: c81357eeaea1eab789cad900cc8e51fe57c82e14
1 | #include <stdio.h> |
2 | #include <unistd.h> |
3 | #include <stdlib.h> |
4 | #include <sys/types.h> |
5 | #include <sys/stat.h> |
6 | #include <fcntl.h> |
7 | #include <string.h> |
8 | #include <pthread.h> |
9 | #include <termios.h> |
10 | #include <errno.h> |
11 | #include <linux/hidraw.h> |
12 | #include <android/log.h> |
13 | #include <cutils/log.h> |
14 | |
15 | //#include "../TvService.h" |
16 | //#include "../include/tvcmd.h" |
17 | //#include "../tvconfig/tvconfig_api.h" |
18 | //#include "../tvconfig/tvconfig_logcfg.h" |
19 | |
20 | #include "serial_base.h" |
21 | #include "serial_operate.h" |
22 | |
23 | #ifdef LOG_TAG |
24 | #undef LOG_TAG |
25 | #define LOG_TAG "headset" |
26 | #endif |
27 | #include "CTvLog.h" |
28 | |
29 | |
30 | //****************************************************** |
31 | #ifndef HIDIOCSFEATURE |
32 | #define HIDIOCSFEATURE(len) _IOC(_IOC_WRITE|_IOC_READ, 'H', 0x06, len) |
33 | #define HIDIOCGFEATURE(len) _IOC(_IOC_WRITE|_IOC_READ, 'H', 0x07, len) |
34 | #endif |
35 | #define product_nid 0x00e0 |
36 | //#define product_nid 0x00e0 |
37 | |
38 | #define vendor_id 0x1A1D |
39 | #define ENDPOINT 3 |
40 | |
41 | #define EVENT_2_4G_HEADSET_ON 0 |
42 | #define EVENT_2_4G_HEADSET_OFF 1 |
43 | |
44 | CTv2d4GHeadSetDetect::CTv2d4GHeadSetDetect() |
45 | { |
46 | } |
47 | |
48 | CTv2d4GHeadSetDetect::~CTv2d4GHeadSetDetect() |
49 | { |
50 | } |
51 | |
52 | int CTv2d4GHeadSetDetect::startDetect() |
53 | { |
54 | this->run(); |
55 | |
56 | return 0; |
57 | } |
58 | |
59 | bool CTv2d4GHeadSetDetect::threadLoop() |
60 | { |
61 | int i = 0, rd_len = 0; |
62 | int thread_cmd_dly_tm = 1000 * 1000; |
63 | int tvThermal_cnt = 0, fd = 0; |
64 | char data[10] = "0"; |
65 | |
66 | LOGD("%s, entering...\n", __FUNCTION__); |
67 | |
68 | //SetSerialBThreadExecFlag(1); |
69 | |
70 | //********************************* |
71 | int hidraw_fd; |
72 | unsigned char buf[32]; |
73 | char phybuf[256]; |
74 | struct hidraw_devinfo info; |
75 | int read_size = 0; |
76 | bool debug = true; |
77 | char device[68]; |
78 | int HeadsetConnectState = false; |
79 | |
80 | int curdeviceID = -1; |
81 | for (int deviceID = 0; deviceID < 5; deviceID++) { |
82 | sprintf(device, "/dev/hidraw%d", deviceID); |
83 | LOGD(" thread device =%s ", device ); |
84 | if ((hidraw_fd = open(device, O_RDWR)) < 0 ) { |
85 | LOGD("cann't open path:%s!!!\n", device); |
86 | continue; |
87 | } |
88 | memset(phybuf, 0x0, 256); |
89 | LOGD("AAAAAAAAAAAAAA:%s!!!\n", device); |
90 | if (ioctl(hidraw_fd, HIDIOCGRAWINFO, &info) >= 0 && |
91 | ioctl(hidraw_fd, HIDIOCGRAWPHYS(256), phybuf) >= 0) { |
92 | LOGD("\t %d, product id = 0x%04x \n", __LINE__, info.product); |
93 | LOGD("\t %d, vendor id = 0x%04x \n", __LINE__, info.vendor); |
94 | int len = strlen(phybuf); |
95 | if (phybuf[len - 1] - '0' == ENDPOINT) { |
96 | if (info.vendor == vendor_id) { |
97 | curdeviceID = deviceID; |
98 | LOGD("\t product id = 0x%04x \n", info.product); |
99 | LOGD("\t vendor id = 0x%04x\n", info.vendor); |
100 | break; |
101 | } |
102 | } |
103 | } |
104 | close(hidraw_fd); |
105 | } |
106 | if (curdeviceID == -1) |
107 | return 0; |
108 | |
109 | sprintf(device, "/dev/hidraw%d", curdeviceID); |
110 | LOGD(" thread device =%s ", device ); |
111 | if ( (hidraw_fd = open(device, O_RDWR | O_NONBLOCK) ) < 0 ) { |
112 | printf("cann't open path:%s!!!\n", device); |
113 | return 0; |
114 | } |
115 | int checkvalue[300] ; |
116 | int countcheck = 0; |
117 | int count = 0; |
118 | int ritemcounts = 15; |
119 | //**************************************** |
120 | |
121 | while ( !exitPending() ) { //requietexit() or requietexitWait() not call |
122 | //loop codes |
123 | //LOGD("while 2.4G %s ", __FUNCTION__); |
124 | |
125 | memset(buf, 0x0, 32); |
126 | for (int ritem = 0; ritem < ritemcounts ; ritem++ ) { |
127 | read_size = read(hidraw_fd, buf, 32); |
128 | //for (int i = 0; i < 32; i++) |
129 | //ALOGD("read_size %d ", read_size); |
130 | if (debug) { |
131 | count ++; |
132 | if (count == 3000) { |
133 | LOGD("%02x %02x %02x %02x %02x %02x ", buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]); |
134 | count = 0; |
135 | } |
136 | } |
137 | if (read_size < 0 ) { |
138 | |
139 | } |
140 | |
141 | checkvalue[countcheck] = buf[4] & 0x1f; |
142 | if (countcheck == 299) { |
143 | int checkcountvalue = 0; |
144 | for (int icheck = 0; icheck < countcheck ; icheck++ ) |
145 | checkcountvalue += checkvalue[icheck]; |
146 | // LOGD("checkcountvalue = %d",checkcountvalue); |
147 | if (checkcountvalue <= 5 * 4) { |
148 | if (HeadsetConnectState == true) { |
149 | if (debug) { |
150 | LOGD("headset connect false"); |
151 | LOGD("headset connect false"); |
152 | } |
153 | |
154 | mpObserver->onHeadSetDetect(0, 0); |
155 | //usleep(1000 * 200); |
156 | } |
157 | HeadsetConnectState = false; |
158 | } else if (checkcountvalue >= 200 * 4) { |
159 | if (HeadsetConnectState == false) { |
160 | if (debug) { |
161 | LOGD("headset connect true"); |
162 | LOGD("headset connect true"); |
163 | } |
164 | mpObserver->onHeadSetDetect(1, 0); |
165 | //usleep(1000 * 200); |
166 | } |
167 | HeadsetConnectState = true; |
168 | } |
169 | countcheck = 0; |
170 | } |
171 | countcheck ++; |
172 | |
173 | // bit 0: headset mic in/off; bit 1:headset on/off; bit 2: headphone on/off; bit 3: soundbar on/off ;bit 4: subwoofer on/off |
174 | /* else if (buf[4] & 0x1f) |
175 | { |
176 | if (HeadsetConnectState == false) |
177 | { |
178 | if (debug) |
179 | { |
180 | ALOGD("headset connect true"); |
181 | ALOGD("headset connect true"); |
182 | } |
183 | android::TvService::getIntance()->SendDtvStats(1,0,0,0,0,0); |
184 | //usleep(1000 * 200); |
185 | } |
186 | HeadsetConnectState = true; |
187 | } |
188 | else |
189 | { |
190 | if (HeadsetConnectState == true) |
191 | { |
192 | if (debug) |
193 | { |
194 | ALOGD("headset connect false"); |
195 | ALOGD("headset connect false"); |
196 | } |
197 | android::TvService::getIntance()->SendDtvStats(2,0,0,0,0,0); |
198 | //usleep(1000 * 200); |
199 | } |
200 | HeadsetConnectState = false; |
201 | }*/ |
202 | } |
203 | { |
204 | //added for fbc thermal setting |
205 | tvThermal_cnt++; |
206 | if (tvThermal_cnt == 300) { //60 sec |
207 | tvThermal_cnt = 0; |
208 | fd = open("/sys/class/thermal/thermal_zone0/temp", O_RDONLY); |
209 | if (fd < 0) { |
210 | LOGE("ERROR: failed to open file error: %d\n", errno); |
211 | } else { |
212 | read(fd, data, sizeof(data)); |
213 | close(fd); |
214 | LOGD("thermal temp data = %s ~~~~~~\n", data); |
215 | int x = 0; |
216 | x = atoi(data); |
217 | mpObserver->onThermalDetect(x); |
218 | LOGD("int data :%d\n", x); |
219 | } |
220 | } |
221 | } |
222 | usleep(1000 * 200); |
223 | } |
224 | //exit |
225 | //return true, run again, return false,not run. |
226 | return false; |
227 | } |
228 | |
229 | |
230 |