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