xref: /OK3568_Linux_fs/external/rk_pcba_test/pcba_minui/bt_test.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <time.h>
4 #include <unistd.h>
5 #include <errno.h>
6 #include <fcntl.h>
7 #include <sys/ioctl.h>
8 #include <sys/socket.h>
9 #include <sys/types.h>
10 #include <sys/stat.h>
11 #include <sys/poll.h>
12 #include <sys/time.h>
13 
14 #include <cutils/log.h>
15 #include <cutils/properties.h>
16 #include "common.h"
17 #include "language.h"
18 #include "test_case.h"
19 
20 #define BLUETOOTH_TTY_TEST
21 //#define SOFIA3GR_PCBA
22 //#define BLUEDROID_TEST
23 
24 #ifdef BLUEDROID_TEST
25 #include "libbluetooth/bluetooth/bluetooth.h"
26 #include "libbluetooth/bluetooth/hci.h"
27 #include "libbluetooth/bluetooth/hci_lib.h"
28 
29 #ifndef HCI_DEV_ID
30 #define HCI_DEV_ID 0
31 #endif
32 
33 #define HCID_START_DELAY_SEC   3
34 #define HCID_STOP_DELAY_USEC 500000
35 
36 #define TAG	"[PCBA,BT]: "
37 #define LOG(x...)	printf(TAG x)
38 
39 #define MIN(x,y) (((x)<(y))?(x):(y))
40 
41 #if 0
42 #define BTHWCTL_DEV_NAME             "/dev/bthwctl"
43 #define BTHWCTL_IOC_MAGIC            0xf6
44 #define BTHWCTL_IOCTL_SET_POWER      _IOWR(BTHWCTL_IOC_MAGIC, 0, uint32_t)
45 
46 #define TCC_BT_DEVICE_PATH                      "/dev/tcc_bt_dev"
47 #define BT_DEV_MAJOR_NUM                        234
48 #define IOCTL_BT_DEV_POWER              _IO(BT_DEV_MAJOR_NUM, 100)
49 
50 enum WIFI_CHIP_TYPE_LIST{
51     BT_UNKNOWN = -1,
52 	BCM4329 = 0,
53 	RTL8188CU,
54 	RTL8188EU,
55 	BCM4330,
56 	RK901,
57 	RK903,
58 	MT6620,
59 	RT5370,
60     MT5931,
61     RDA587x,
62     RDA5990,
63     RTK8723AS,
64     RTK8723BS,
65     RTK8723AU,
66     RTK8723BU,
67     BK3515,
68     SOFIA_3GR,
69 };
70 
71 static int rfkill_id = -1;
72 static char *rfkill_state_path = NULL;
73 static int bluetooth_power_status = 0;
74 static int chip_type;
75 
76 #define WIFI_CHIP_TYPE_PATH "/sys/class/rkwifi/chip"
77 int getChipType(void) {
78 	  int wififd;
79 	  char buf[64];
80 	  int chip_type = RTL8188EU;
81 
82 	  wififd = open(WIFI_CHIP_TYPE_PATH, O_RDONLY);
83 	  if( wififd < 0 ){
84 	          printf("Can't open %s, errno = %d\n", WIFI_CHIP_TYPE_PATH, errno);
85 	          goto done;
86 	  }
87 
88 	  memset(buf, 0, 64);
89 	  if( 0 == read(wififd, buf, 10) ){
90 	          printf("read failed\n");
91 	          close(wififd);
92 	          goto done;
93 	  }
94 	  close(wififd);
95 
96 	  if(0 == strncmp(buf, "BCM4329", strlen("BCM4329")) ) {
97 	          chip_type = BCM4329;
98 	          printf("Read wifi chip type OK ! chip_type = BCM4329\n");
99 	  }
100 	  else if (0 == strncmp(buf, "RTL8188CU", strlen("RTL8188CU")) ) {
101 	          chip_type = RTL8188CU;
102 	          printf("Read wifi chip type OK ! chip_type = RTL8188CU\n");
103 	  }
104 	  else if (0 == strncmp(buf, "RTL8188EU", strlen("RTL8188EU")) ) {
105 	          chip_type = RTL8188EU;
106 	          printf("Read wifi chip type OK ! chip_type = RTL8188EU\n");
107 	  }
108 	  else if (0 == strncmp(buf, "BCM4330", strlen("BCM4330")) ) {
109 	          chip_type = BCM4330;
110 	          printf("Read wifi chip type OK ! chip_type = BCM4330\n");
111 	  }
112 	  else if (0 == strncmp(buf, "RK901", strlen("RK901")) ) {
113 	          chip_type = RK901;
114 	          printf("Read wifi chip type OK ! chip_type = RK901\n");
115 	  }
116 	  else if (0 == strncmp(buf, "RK903", strlen("RK903")) ) {
117 	          chip_type = RK903;
118 	          printf("Read wifi chip type OK ! chip_type = RK903\n");
119 	  }
120 	  else if (0 == strncmp(buf, "MT6620", strlen("MT6620")) ) {
121 	          chip_type = MT6620;
122 	          printf("Read wifi chip type OK ! chip_type = MT6620\n");
123 	  }
124 	  else if (0 == strncmp(buf, "RT5370", strlen("RT5370")) ) {
125 	          chip_type = RT5370;
126 	          printf("Read wifi chip type OK ! chip_type = RT5370\n");
127 	  }
128 	  else if (0 == strncmp(buf, "MT5931", strlen("MT5931")) ) {
129 	          chip_type = MT5931;
130 	          printf("Read wifi chip type OK ! chip_type = MT5931\n");
131 	  }
132 
133 done:
134 	  return chip_type;
135 }
136 
137 static int init_rfkill() {
138     char path[64];
139     char buf[16];
140     int fd;
141     int sz;
142     int id;
143     for (id = 0; ; id++) {
144         snprintf(path, sizeof(path), "/sys/class/rfkill/rfkill%d/type", id);
145         fd = open(path, O_RDONLY);
146         if (fd < 0) {
147             LOGW("open(%s) failed: %s (%d)\n", path, strerror(errno), errno);
148             return -1;
149         }
150         sz = read(fd, &buf, sizeof(buf));
151         close(fd);
152         if (sz >= 9 && memcmp(buf, "bluetooth", 9) == 0) {
153             rfkill_id = id;
154             break;
155         }
156     }
157 
158     asprintf(&rfkill_state_path, "/sys/class/rfkill/rfkill%d/state", rfkill_id);
159     return 0;
160 }
161 
162 static int broadcom_set_bluetooth_power(int on) {
163     int sz;
164     int fd = -1;
165     int ret = -1;
166     const char buffer = (on ? '1' : '0');
167 
168     if (rfkill_id == -1) {
169         if (init_rfkill()) goto out;
170     }
171 
172     fd = open(rfkill_state_path, O_WRONLY);
173     if (fd < 0) {
174         printf("open(%s) for write failed: %s (%d)\n", rfkill_state_path,
175              strerror(errno), errno);
176         goto out;
177     }
178     sz = write(fd, &buffer, 1);
179     if (sz < 0) {
180         printf("write(%s) failed: %s (%d)\n", rfkill_state_path, strerror(errno),
181              errno);
182         goto out;
183     }
184     ret = 0;
185 
186 out:
187     if (fd >= 0) close(fd);
188     return ret;
189 }
190 
191 static int mtk_set_bluetooth_power(int on) {
192     int sz;
193     int fd = -1;
194     int ret = -1;
195     const uint32_t buf = (on ? 1 : 0);
196 
197     fd = open(BTHWCTL_DEV_NAME, O_RDWR);
198     if (fd < 0) {
199         LOGE("Open %s to set BT power fails: %s(%d)", BTHWCTL_DEV_NAME,
200              strerror(errno), errno);
201         goto out1;
202     }
203 
204     ret = ioctl(fd, BTHWCTL_IOCTL_SET_POWER, &buf);
205     if(ret < 0) {
206         LOGE("Set BT power %d fails: %s(%d)\n", buf,
207              strerror(errno), errno);
208         goto out1;
209     }
210 
211     bluetooth_power_status = on ? 1 : 0;
212 
213 out1:
214     if (fd >= 0) close(fd);
215     return ret;
216 }
217 
218 static int rda587x_set_bluetooth_power(int on) {
219 	int fd = -1;
220 	int bt_on_off = -1;
221 
222 	fd = open(TCC_BT_DEVICE_PATH, O_RDWR);
223 	if( fd < 0 )
224 	{
225 	    printf("[###### TCC BT #######] [%s] open error[%d]\n", TCC_BT_DEVICE_PATH, fd);
226 	    return -1;
227 	}
228 	else
229 	{
230 	    bt_on_off = 0;
231 	    ioctl(fd, IOCTL_BT_DEV_POWER, &bt_on_off);//make sure bt is disabled
232 	    printf("[##### TCC BT #####] set_bluetooth_power [%d]\n", bt_on_off);
233 	    bt_on_off = 1;
234 	    ioctl(fd, IOCTL_BT_DEV_POWER, &bt_on_off);
235 	    printf("[##### TCC BT #####] set_bluetooth_power [%d]\n", bt_on_off);
236 	    close(fd);
237 	    return 0;
238 	}
239 }
240 
241 static int rda5990_set_bluetooth_power(int on) {
242 	return 0;
243 }
244 
245 static int set_bluetooth_power(int on) {
246 	if(chip_type == MT5931) {
247 		return mtk_set_bluetooth_power(on);
248 	} else if(chip_type == RDA587x) {
249 		return rda587x_set_bluetooth_power(on);
250 	} else if(chip_type == RDA5990) {
251 		return rda5990_set_bluetooth_power(on);
252 	} else {
253 		return broadcom_set_bluetooth_power(on);
254 	}
255 }
256 
257 /**
258 @ret:
259   >=0 , socket created ok;
260   <0, socket created fail;
261 */
262 static inline int bt_test_create_sock() {
263     int sock = 0;
264 	sock = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI);
265     if (sock < 0) {
266         printf("bluetooth_test Failed to create bluetooth hci socket: %s (%d)\n",
267              strerror(errno), errno);
268 		return -1;
269     }
270     return sock;
271 }
272 
273 #if 1
274 static int start_hciattach() {
275 	int ret;
276 	if(chip_type == MT5931) {
277 	  ret = __system("/system/bin/hciattach_mtk -n -t 10 -s 115200 /dev/ttyS0 mtk 1500000 noflow &");
278 	} else if(chip_type == RDA587x) {
279 	  ret = __system("/system/bin/hciattach_5876 -n -s 115200 /dev/ttyS0 rda 1500000 noflow &");
280 	} else if(chip_type == RDA5990) {
281 	  ret = __system("/system/bin/hciattach_5990 -n -s 115200 /dev/ttyS0 rda 921600 noflow &");
282 	} else if(chip_type == RTK8723AS) {
283 	  ret = __system("/system/bin/hciattach_8723 -n -s 115200  /dev/ttyS0 rtk_h5 &");
284 	} else if(chip_type == RTK8723AU) {
285 	  ret = __system("insmod /res/rtk_btusb.ko");
286 	} else {
287 		ret = __system("/system/bin/brcm_patchram_plus --patchram bychip --baudrate 1500000 --enable_lpm --enable_hci /dev/ttyS0 &");
288 	}
289 	return ret;
290 }
291 #else
292 static int start_hciattach() {
293 	int ret;
294 	char service_name[32];
295 
296 	if(chip_type == MT5931) {
297 		strcpy(service_name, "hciattach_mtk");
298 	} else if(chip_type == RDA587x) {
299 	  strcpy(service_name, "hciattach_587x");
300 	} else if(chip_type == RDA5990) {
301 	  strcpy(service_name, "hciattach_5990");
302 	} else {
303 		strcpy(service_name, "hciattach_brm");
304 	}
305 
306 	if (property_set("ctl.start", service_name) < 0) {
307 		printf("bluetooth_test Failed to start %s\n", service_name);
308 		return -1;
309 	}
310 
311 	return ret;
312 }
313 #endif
314 
315 static int bt_test_enable() {
316     int ret = -1;
317     int hci_sock = -1;
318     int attempt;
319 
320     if (set_bluetooth_power(1) < 0) goto out;
321 
322     printf("Starting hciattach daemon\n");
323     if (start_hciattach() != 0) {
324         printf("Failed to start hciattach\n");
325         set_bluetooth_power(0);
326         goto out;
327     }
328 
329     // Try for 10 seconds, this can only succeed once hciattach has sent the
330     // firmware and then turned on hci device via HCIUARTSETPROTO ioctl
331     printf("Waiting for HCI device present...\n");
332     for (attempt = 50; attempt > 0;  attempt--) {
333         printf("..%d..\n", attempt);
334         hci_sock = bt_test_create_sock();
335         if (hci_sock < 0) goto out;
336 
337         ret = ioctl(hci_sock, HCIDEVUP, HCI_DEV_ID);
338 
339         if (!ret) {
340             break;
341         } else if (errno == EALREADY) {
342             printf("Bluetoothd already started, unexpectedly!\n");
343             break;
344         }
345 
346         close(hci_sock);
347         usleep(200000);  // 200 ms retry delay
348     }
349     if (attempt == 0) {
350         printf("%s: Timeout waiting for HCI device to come up, ret=%d\n",
351             __FUNCTION__, ret);
352         set_bluetooth_power(0);
353         goto out;
354     }
355 
356 		printf("bt_enable success.\n");
357     ret = 0;
358 
359 out:
360     if (hci_sock >= 0) close(hci_sock);
361     return ret;
362 }
363 
364 static int my_ba2str(const bdaddr_t *ba, char *str) {
365     return sprintf(str, "%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X",
366                 ba->b[5], ba->b[4], ba->b[3], ba->b[2], ba->b[1], ba->b[0]);
367 }
368 
369 int bt_get_chipname(char* name, int len)
370 {
371     int ret = -1;
372     int fd = -1;
373     int sz = 0;
374     char* rfkill_name_path = NULL;
375 
376     if (rfkill_id == -1) {
377         if (init_rfkill()) goto out;
378     }
379 
380     asprintf(&rfkill_name_path, "/sys/class/rfkill/rfkill%d/name", rfkill_id);
381 
382     fd = open(rfkill_name_path, O_RDONLY);
383     if (fd < 0) {
384         printf("open(%s) failed: %s (%d)", rfkill_name_path, strerror(errno),
385              errno);
386         goto out;
387     }
388 
389     sz = read(fd, name, len);
390     if (sz < 0) {
391         printf("read(%s) failed: %s (%d)", rfkill_name_path, strerror(errno),
392              errno);
393         goto out;
394     }
395     name[sz] = '\0';
396     if (name[sz-1]=='\n')
397         name[sz-1] = '\0';
398 
399     ret = 0;
400 
401 out:
402     if (fd >= 0) close(fd);
403     return ret;
404 }
405 
406 int check_bluedroid_test()
407 {
408 	FILE *fp;
409 
410 	fp = fopen("/data/bt_success.txt", "r");
411 	if(fp != NULL) {
412 		printf("check_bluedroid_test: success.\n");
413 		fclose(fp);
414 		fp = NULL;
415 		return 1;
416 	}
417 
418 	//fp = fopen("/data/bt_fail.txt", "r");
419 	//if(fp != NULL) {
420 	//	printf("check_bluedroid_test: fail.\n");
421 	//	fclose(fp);
422 	//	fp = NULL;
423 	//	return -1;
424 	//}
425 
426 	return 0; // wait
427 }
428 
429 int bluedroid_test()
430 {
431 	int ret, counts = 10;
432 
433     if (chmod(BTHWCTL_DEV_NAME, 0660) < 0) {
434         printf("Error changing permissions of %s to 0660: %s",
435                 BTHWCTL_DEV_NAME, strerror(errno));
436         unlink(BTHWCTL_DEV_NAME);
437     }
438 
439     if (chmod("/sys/class/rfkill/rfkill0/state", 0775) < 0) {
440         printf("Error changing permissions of %s to 0660: %s",
441                 "/sys/class/rfkill/rfkill0/state", strerror(errno));
442         unlink("/sys/class/rfkill/rfkill0/state");
443     }
444 
445     if (chmod("/sys/class/rfkill/rfkill0/type", 0775) < 0) {
446         printf("Error changing permissions of %s to 0660: %s",
447                 "/sys/class/rfkill/rfkill0/type", strerror(errno));
448         unlink("/sys/class/rfkill/rfkill0/type");
449     }
450 
451     if (chmod("/data", 0775) < 0) {
452         printf("Error changing permissions of %s to 0660: %s",
453                 "/data", strerror(errno));
454         unlink("/data");
455     }
456 
457     if (chmod("/dev/ttyS0", 0775) < 0) {
458         printf("Error changing permissions of %s to 0775 %s",
459                 "/dev/ttyS0", strerror(errno));
460     }
461 
462     printf("bluedroid_test: start bdt test:\n");
463 
464     ret = __system("/system/bin/bdt &");
465     if(ret != 0) {
466     	printf("bluedroid_test: start bdt failed.\n");
467     	return -1;
468     }
469 
470     while(counts-- > 0) {
471     	ret = check_bluedroid_test();
472     	if(ret == 1) {
473     		break;
474     	}
475     	usleep(1000000);
476     }
477     if (counts == 0) {
478         printf("bluedroid_test: waitting for bt test ready timeout!\n");
479         ret = -1;
480     }
481 
482     return ret;
483 }
484 
485 static char bt_chip[64] = "";
486 
487 int bt_test_bluez()
488 {
489     int dev_id = 0;
490 	int sock = 0;
491     int i = 0;
492 	int ret = 0;
493 	char dt[32] = {0};
494 	chip_type = RK903;
495 	if(script_fetch("bluetooth", "chip_type", (int *)dt, 8) == 0) {
496 		printf("script_fetch chip_type = %s.\n", dt);
497 	}
498 	if(strcmp(dt, "rk903") == 0) {
499 		chip_type = RK903;
500 	} else if(strcmp(dt, "mt6622") == 0) {
501 		chip_type = MT5931;
502 	} else if(strcmp(dt, "rda587x") == 0) {
503 		chip_type = RDA587x;
504 	} else if(strcmp(dt, "rda5990") == 0) {
505 		chip_type = RDA5990;
506 	} else if(strcmp(dt, "rtk8723as") == 0) {
507 		chip_type = RTK8723AS;
508 	} else if(strcmp(dt, "rtk8723bs") == 0) {
509 		chip_type = RTK8723BS;
510 	} else if(strcmp(dt, "rtk8723au") == 0) {
511 		chip_type = RTK8723AU;
512 	} else if(strcmp(dt, "rtk8723bu") == 0) {
513 		chip_type = RTK8723BU;
514     } else if(strcmp(dt, "bk3515") == 0) {
515         chip_type = BK3515;
516 		sleep(5);
517 	} else {
518 		if (bt_get_chipname(bt_chip, 63) != 0) {
519 
520 		    printf("Can't read BT chip name\n");
521 			goto fail;
522 		}
523 
524 		if (!strcmp(bt_chip, "rk903_26M"))
525 		    chip_type = RK903;
526 		else if (!strcmp(bt_chip, "rk903"))
527 		    chip_type = RK903;
528 		else if (!strcmp(bt_chip, "ap6210"))
529 		    chip_type = RK903;
530 		else if (!strcmp(bt_chip, "ap6330"))
531 		    chip_type = RK903;
532 		else if (!strcmp(bt_chip, "ap6476"))
533 		    chip_type = RK903;
534 		else if (!strcmp(bt_chip, "ap6493"))
535 		    chip_type = RK903;
536 		else {
537 		    printf("Not support BT chip, skip bt test.\n");
538 			goto fail;
539 		}
540 	}
541 
542 	printf("bluetooth_test main function started: chip_type = %d\n", chip_type);
543 
544 	if(chip_type == RTK8723BS || chip_type == BK3515) {
545 		ret = bluedroid_test();
546 		if(ret == 1) {
547 			printf("bluetooth_test success.\n");
548 			goto success;
549 		} else {
550 			printf("bluetooth_test fail.\n");
551 			goto fail;
552 		}
553 	}
554 
555 	if(chip_type == RTK8723AU || chip_type == RTK8723BU) {
556 		int ret;
557 		ret = __system("busybox dmesg | busybox grep 'hci_register_dev success'");
558 		printf("a:ret = %d.\n", ret);
559         if (ret != 0) {
560 		    ret = __system("insmod /res/rtk_btusb.ko");
561 		    ret = __system("busybox dmesg | busybox grep 'hci_register_dev success'");
562         }
563 		printf("b:ret = %d.\n", ret);
564 		if(ret != 0) {
565 			printf("bluetooth_test fail.\n");
566 			goto fail;
567 		}
568 		printf("bluetooth_test success.\n");
569 		goto success;
570 	}
571 
572 	ret = bt_test_enable();
573 	if(ret < 0){
574 		printf("bluetooth_test main function fail to enable \n");
575 		goto fail;
576 	}
577 
578 	dev_id = hci_get_route(NULL);
579 
580 	if(dev_id < 0){
581 		printf("bluetooth_test main function fail to get dev id\n");
582 		goto fail;
583 	}
584 
585 	printf("bluetooth_test main function hci_get_route dev_id=%d\n",dev_id);
586 
587     sock = hci_open_dev( dev_id );
588 	if(sock < 0){
589 		printf("bluetooth_test main function fail to open bluetooth sock\n");
590 		goto fail;
591 	}
592 
593 	printf("bluetooth_test main function hci_open_dev ok\n");
594 
595 	if(sock >= 0){
596 		close( sock );
597 	}
598 
599 	/*ret = bt_test_disable();
600 	if(ret < 0){
601 		printf("bluetooth_test main function fail to disable\n");
602 		ui_print_xy_rgba(0,tc_info->y,255,0,0,255,"bluetooth test error\n");
603 		return 0;
604 	}*/
605 
606 success:
607     printf("bluetooth_test main function end\n");
608     return 0;
609 
610 fail:
611     printf("bluetooth_test main function end\n");
612     return -1;
613 }
614 
615 #define LOG(x...)   printf("[BT_TEST] "x)
616 
617 static int get_chip_type()
618 {
619 	char dt[32] = {0};
620 	chip_type = RK903;
621 	if(script_fetch("bluetooth", "chip_type", (int *)dt, 8) == 0) {
622 		LOG("script_fetch chip_type = %s.\n", dt);
623 	}
624 	if(strcmp(dt, "rk903") == 0) {
625 		chip_type = RK903;
626 	} else if(strcmp(dt, "mt6622") == 0) {
627 		chip_type = MT5931;
628 	} else if(strcmp(dt, "rda587x") == 0) {
629 		chip_type = RDA587x;
630 	} else if(strcmp(dt, "rda5990") == 0) {
631 		chip_type = RDA5990;
632 	} else if(strcmp(dt, "rtk8723as") == 0) {
633 		chip_type = RTK8723AS;
634 	} else if(strcmp(dt, "rtk8723bs") == 0) {
635 		chip_type = RTK8723BS;
636 	} else if(strcmp(dt, "rtk8723au") == 0) {
637 		chip_type = RTK8723AU;
638 	} else if(strcmp(dt, "rtk8723bu") == 0) {
639 		chip_type = RTK8723BU;
640     } else if(strcmp(dt, "bk3515") == 0) {
641         chip_type = BK3515;
642 	} else if(strcmp(dt, "Sofia-3gr") == 0) {
643         chip_type = SOFIA_3GR;
644 	} else {
645 		if (bt_get_chipname(bt_chip, 63) != 0) {
646 		    LOG("Can't read BT chip name\n");
647 			chip_type = BT_UNKNOWN;
648 		}
649 
650 		if (!strcmp(bt_chip, "rk903_26M"))
651 		    chip_type = RK903;
652 		else if (!strcmp(bt_chip, "rk903"))
653 		    chip_type = RK903;
654 		else if (!strcmp(bt_chip, "ap6210"))
655 		    chip_type = RK903;
656 		else if (!strcmp(bt_chip, "ap6330"))
657 		    chip_type = RK903;
658 		else if (!strcmp(bt_chip, "ap6476"))
659 		    chip_type = RK903;
660 		else if (!strcmp(bt_chip, "ap6493"))
661 		    chip_type = RK903;
662 		else {
663 		    LOG("Not support BT chip, skip bt test.\n");
664 			chip_type = BT_UNKNOWN;
665 		}
666 	}
667 
668 	LOG("chip type is: %d\n", chip_type);
669 
670     return chip_type;
671 }
672 
673 static void change_mode()
674 {
675     if (chmod(BTHWCTL_DEV_NAME, 0660) < 0) {
676         LOG("Error changing permissions of %s to 0660: %s\n",
677                 BTHWCTL_DEV_NAME, strerror(errno));
678         unlink(BTHWCTL_DEV_NAME);
679     }
680 
681     if (chmod("/sys/class/rfkill/rfkill0/state", 0775) < 0) {
682         LOG("Error changing permissions of %s to 0660: %s\n",
683                 "/sys/class/rfkill/rfkill0/state", strerror(errno));
684         unlink("/sys/class/rfkill/rfkill0/state");
685     }
686 
687     if (chmod("/sys/class/rfkill/rfkill0/type", 0775) < 0) {
688         LOG("Error changing permissions of %s to 0660: %s\n",
689                 "/sys/class/rfkill/rfkill0/type", strerror(errno));
690         unlink("/sys/class/rfkill/rfkill0/type");
691     }
692 
693     if (chmod("/dev/rtk_btusb", 0775) < 0) {
694         LOG("Error changing permissions of %s to 0660: %s\n",
695                 "/dev/rtk_btusb", strerror(errno));
696     }
697 
698     if (chmod("/dev/ttyS0", 0775) < 0) {
699         LOG("Error changing permissions of %s to 0775 %s\n",
700                 "/dev/ttyS0", strerror(errno));
701     }
702 
703     LOG("Change mode finish\n");
704 
705 }
706 
707 static int bt_test_bluedroid()
708 {
709     int ret = -1;
710     FILE *fp=NULL;
711     int try = 20, bytes_for_second = 10*1024;
712     char *result_buf, *buf;
713     int result_size = try * bytes_for_second;
714 
715     result_buf = malloc(result_size);
716     if (result_buf == NULL) {
717         LOG("malloc result_buf fail\n");
718         return ret;
719     }
720     buf = result_buf;
721 
722     change_mode();
723 
724     fp = popen("echo \"enable\" | bdt", "r");
725     if (fp != NULL) {
726         int fd = fileno(fp);
727         int flags = fcntl(fd, F_GETFL, 0);
728         int len = 0;
729         fcntl(fd, F_SETFL, flags|O_NONBLOCK);
730 
731         LOG("running bdt for bluetooth test...\n");
732         while (try-->0) {
733             buf += len;
734             len = fread(buf, sizeof(char), bytes_for_second, fp);
735             if (len) {
736                 //LOG("read: %s\n", buf);
737                 if (strstr(result_buf, "ADAPTER STATE UPDATED : ON")) {
738                     LOG("bt test success!\n");
739                     ret = 0;
740                     break;
741                 }
742             }
743             LOG("wait %d\n", try);
744             sleep(1);
745         }
746         pclose(fp);
747         if (try<=0)
748             LOG("bt test timeout!\n");
749     } else
750         LOG("run bdt fail!\n");
751 
752     free(result_buf);
753     return ret;
754 }
755 
756 #elif defined(SOFIA3GR_PCBA)
check_Sofia3gr_bluedroid_test()757 int check_Sofia3gr_bluedroid_test()//add by wjh
758 {
759 	LOG("Sofia3gr_bluedroid_test: start check bdt test result.\n");
760 
761 	int ret = -1;
762     FILE *fp=NULL;
763     int try = 10, bytes_for_second = 10*1024;
764     char *result_buf, *buf;
765     int result_size = try * bytes_for_second;
766 
767     result_buf = malloc(result_size);
768     if (result_buf == NULL) {
769         LOG("malloc result_buf fail\n");
770         return ret;
771     }
772     buf = result_buf;
773 
774 	fp = popen("echo \"enable\" | bdt", "r");
775     if (fp != NULL) {
776         int fd = fileno(fp);
777         int flags = fcntl(fd, F_GETFL, 0);
778         int len = 0;
779         fcntl(fd, F_SETFL, flags|O_NONBLOCK);
780 
781         LOG("running bdt for bluetooth test...\n");
782         while (try-->0) {
783             buf += len;
784             len = fread(buf, sizeof(char), bytes_for_second, fp);
785             if (len) {
786                 //LOG("read: %s\n", buf);
787                 if (strstr(result_buf, "ADAPTER STATE UPDATED : ON")) {
788                     LOG("bt test success!\n");
789                     ret = 0;
790                     break;
791                 }
792             }
793             LOG("wait %d\n", try);
794             sleep(1);
795         }
796         pclose(fp);
797         if (try<=0)
798             LOG("bt test timeout!\n");
799     } else
800         LOG("run bdt fail!\n");
801 
802     free(result_buf);
803 	popen("echo \"quite\" | bdt", "r");
804     return ret;
805 }
806 
807 
Sofia3gr_bluedroid_test()808 int Sofia3gr_bluedroid_test()//add by wjh
809 {
810 	int ret, counts = 3;
811 
812 	LOG("Sofia3gr_bluedroid_test: start bdt.\n");
813 	while(counts-- > 0)
814 	{
815 		ret = check_Sofia3gr_bluedroid_test();
816 		if(ret == 0)
817 		{
818 			break;
819 		}
820 	}
821 	return ret;
822 }
823 
824 #else
825 int uart_fd = -1;
826 struct termios termios;
827 unsigned char  buffer[1024];
828 int ttytestResult= -1;
829 unsigned char hci_reset[] = { 0x01, 0x03, 0x0c, 0x00 };
830 unsigned char hci_rtksyc[] = { 0xc0, 0x00, 0x2f, 0x00,0xd0, 0x01,0x7e,0xc0};
831 
832 
833 void
init_uart_brcm()834 init_uart_brcm()
835 {
836 	tcflush(uart_fd, TCIOFLUSH);
837 	int n = tcgetattr(uart_fd, &termios);
838 	printf("tcgetattr %d\n",n);
839 
840 #ifndef __CYGWIN__
841 	cfmakeraw(&termios);
842 #else
843 	termios.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP
844                 | INLCR | IGNCR | ICRNL | IXON);
845 	termios.c_oflag &= ~OPOST;
846 	termios.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
847 	termios.c_cflag &= ~(CSIZE | PARENB);
848 	termios.c_cflag |= CS8;
849 #endif
850 
851 	//termios.c_cflag |= CRTSCTS;
852 	tcsetattr(uart_fd, TCSANOW, &termios);
853 	tcflush(uart_fd, TCIOFLUSH);
854 	tcsetattr(uart_fd, TCSANOW, &termios);
855 	tcflush(uart_fd, TCIOFLUSH);
856 	tcflush(uart_fd, TCIOFLUSH);
857 	cfsetospeed(&termios, B115200);
858 	cfsetispeed(&termios, B115200);
859 	tcsetattr(uart_fd, TCSANOW, &termios);
860 }
861 
862 
863 void
init_uart_rtk()864 init_uart_rtk()
865 {
866 	tcflush(uart_fd, TCIOFLUSH);
867 	int n = tcgetattr(uart_fd, &termios);
868 	printf("tcgetattr %d\n",n);
869 
870 #ifndef __CYGWIN__
871 	cfmakeraw(&termios);
872 #else
873 	termios.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP
874                 | INLCR | IGNCR | ICRNL | IXON);
875 	termios.c_oflag &= ~OPOST;
876 	termios.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
877 	termios.c_cflag &= ~(CSIZE | PARENB);
878 	termios.c_cflag |= CS8;
879 #endif
880 		termios.c_cflag &= ~CRTSCTS;
881 
882 	termios.c_cflag |= PARENB;
883 
884 	//termios.c_cflag |= CRTSCTS;
885 	tcsetattr(uart_fd, TCSANOW, &termios);
886 	tcflush(uart_fd, TCIOFLUSH);
887 	tcsetattr(uart_fd, TCSANOW, &termios);
888 	tcflush(uart_fd, TCIOFLUSH);
889 	tcflush(uart_fd, TCIOFLUSH);
890 	cfsetospeed(&termios, B115200);
891 	cfsetispeed(&termios, B115200);
892 	tcsetattr(uart_fd, TCSANOW, &termios);
893 }
894 
895 void
dump(unsigned char * out,int len)896 dump(unsigned char *out, int len)
897 {
898 	int i;
899 
900 	for (i = 0; i < len; i++) {
901 		if (i && !(i % 16)) {
902 			printf("\n");
903 		}
904 
905 		printf("%02x ", out[i]);
906 	}
907 
908 	printf("\n");
909 }
910 
911 
912 #define READTTY_TIMEOUT  30//3s
readttyLen(int fd,unsigned char * buffer,int len)913 int readttyLen(int fd,unsigned char *buffer,int len)
914 {
915 	int count;
916 	int i= 0;
917 	int timeout=0;
918 	while(len){
919 		count = read(fd,&buffer[i],1);
920 		if(count == 1){
921 			i += count;
922 			len -= count;
923 		}
924 		else{
925 			usleep(100000);//100ms
926 			timeout ++;
927 			//printf("timeout %d\n", timeout);
928 			if(timeout >= READTTY_TIMEOUT)
929 				return -1;
930 		}
931 	}
932 	return i;
933 }
readBrcmTty(int fd,unsigned char * buffer)934 void readBrcmTty(int fd, unsigned char  *buffer)
935 {
936 	int i=0;
937 	int count;
938 	int len;
939 	count = readttyLen(fd,buffer,3);
940 	printf("readBrcmTty count11 %d\n", count);
941 	if(count < 3)
942 		return;
943 	i += count;
944 	len = buffer[2];
945 
946 	count = readttyLen(fd,&buffer[i],len);
947 	if(count<len)
948 		return;
949 	i += count;
950 
951 	//if (debug)
952 	{
953 
954 		printf("readBrcmTty received %d\n", i);
955 		dump(buffer, i);
956 	}
957 
958 	ttytestResult = 0;
959 	printf("bt ttytest read_event succ\n");
960 }
961 
readRtkTty(int fd,unsigned char * buffer)962 void readRtkTty(int fd, unsigned char  *buffer)
963 {
964 	int i=0;
965 	int count;
966 	int len;
967 
968 	count = readttyLen(fd,buffer,16);
969 	if(count < 16)
970 		return;
971 	i += count;
972 
973 	//if (debug)
974 	{
975 
976 		printf("received %d\n", i);
977 		dump(buffer, i);
978 	}
979 
980 	ttytestResult = 0;
981 	printf("bt ttytest read_event succ\n");
982 }
983 
984 
985 void
hci_send_cmd(unsigned char * buf,int len)986 hci_send_cmd(unsigned char *buf, int len)
987 {
988 	//if (debug)
989 	{
990 		printf("writing\n");
991 		dump(buf, len);
992 	}
993 
994 	int writelen=write(uart_fd, buf, len);
995 	printf("writelen %d\n",writelen);
996 }
997 
998 
999 void
expired(int sig)1000 expired(int sig)
1001 {
1002     ttytestResult = -1;
1003 	printf("bt ttytest expired\n");
1004 }
1005 
1006 void
proc_reset()1007 proc_reset()
1008 {
1009 	signal(SIGALRM, expired);
1010 
1011 	printf( "proc_reset");
1012 	alarm(8);
1013 
1014 	hci_send_buf(hci_reset, sizeof(hci_reset));
1015 
1016 	read_event(uart_fd, buffer);
1017 
1018 	alarm(0);
1019 }
1020 
1021 #define CONF_COMMENT '#'
1022 #define CONF_DELIMITERS " =\n\r\t"
1023 #define CONF_VALUES_DELIMITERS "=\n\r\t"
1024 #define CONF_MAX_LINE_LEN 255
get_tty_conf(const char * p_path,char * ttyPort)1025 void get_tty_conf(const char *p_path,char *ttyPort)
1026 {
1027     FILE    *p_file;
1028     char    *p_name;
1029     char    *p_value;
1030     char    line[CONF_MAX_LINE_LEN+1]; /* add 1 for \0 char */
1031 
1032     printf( "Attempt to load conf from %s", p_path);
1033 
1034     if ((p_file = fopen(p_path, "r")) != NULL)
1035     {
1036         /* read line by line */
1037         while (fgets(line, CONF_MAX_LINE_LEN+1, p_file) != NULL)
1038         {
1039             if (line[0] == CONF_COMMENT)
1040                 continue;
1041 
1042             p_name = strtok(line, CONF_DELIMITERS);
1043 
1044             if (NULL == p_name)
1045             {
1046                 continue;
1047             }
1048 
1049             p_value = strtok(NULL, CONF_DELIMITERS);
1050 
1051             if (NULL == p_value)
1052             {
1053                 printf( "vnd_load_conf: missing value for name: %s", p_name);
1054                 continue;
1055             }
1056 
1057             if (strcmp("UartPort", (const char *)p_name) == 0){
1058 				printf("get ttyPort %s", p_value);
1059 				strcpy(ttyPort,p_value);
1060 				fclose(p_file);
1061 				return;
1062             }
1063 
1064         }
1065 
1066         fclose(p_file);
1067     }
1068     else
1069     {
1070         printf( "vnd_load_conf file >%s< not found", p_path);
1071     }
1072 	strcpy(ttyPort,"/dev/ttyS0");
1073 }
1074 
test_rtktty()1075 int test_rtktty()
1076 {
1077 	init_uart_rtk();
1078 	hci_send_cmd(hci_rtksyc, sizeof(hci_rtksyc));
1079 	readRtkTty(uart_fd, buffer);
1080 	return ttytestResult;
1081 }
test_brcmtty()1082 int test_brcmtty()
1083 {
1084 	init_uart_brcm();
1085 	hci_send_cmd(hci_reset, sizeof(hci_reset));
1086 	readBrcmTty(uart_fd, buffer);
1087 	return ttytestResult;
1088 }
1089 
ttytestThread(void * param)1090 static void ttytestThread(void *param)
1091 {
1092 	char ttyPort[30]={0};
1093 	__system("echo 1 > /sys/class/rfkill/rfkill0/state");
1094 	sleep(1);
1095     get_tty_conf("/vendor/etc/bluetooth/bt_vendor.conf",ttyPort);
1096 	if ((uart_fd = open(ttyPort, O_RDWR | O_NOCTTY | O_NONBLOCK)) == -1) {
1097 		printf( "port could not be opened, error %d\n", errno);
1098 	}
1099 
1100 	int i;
1101 	for(i=0;i<3;i++){
1102 	 if(test_brcmtty()>=0)
1103 	 	return;
1104 	 if(test_rtktty()>=0)
1105 	 	return;
1106 	}
1107 
1108 }
1109 
bluetoothtty_test()1110 int bluetoothtty_test()
1111 {
1112     int i;
1113 
1114 	pthread_t thread_id;
1115 	pthread_create(&thread_id, NULL,(void*)ttytestThread, NULL);
1116 	for(i=10;i>0;i--){
1117 		sleep(1);
1118 		if(ttytestResult == 0)
1119 			return 0;
1120 	}
1121 	return -1;
1122 
1123 }
1124 #endif
1125 
1126 #endif
1127 
bt_test(void * argv)1128 void *bt_test(void *argv)
1129 {
1130 	struct testcase_info *tc_info = (struct testcase_info *)argv;
1131     int ret = 0;
1132 
1133 	/*remind ddr test*/
1134 	if(tc_info->y <= 0)
1135 		tc_info->y  = get_cur_print_y();
1136 
1137 	ui_print_xy_rgba(0,tc_info->y,255,255,0,255,"%s:[%s..] \n",
1138 	                PCBA_BLUETOOTH,PCBA_TESTING);
1139 
1140 #if 0    //Android pcba test
1141 #if defined(SOFIA3GR_PCBA)
1142        ret = Sofia3gr_bluedroid_test();
1143 #elif defined (BLUETOOTH_TTY_TEST)
1144 	ret = bluetoothtty_test();
1145 #else
1146 	switch (get_chip_type()) {
1147     case RK903:
1148     case RTK8723BS:
1149     case BK3515:
1150         ret = bt_test_bluedroid();
1151         break;
1152 	/*case SOFIA_3GR:
1153 		ret = Sofia3gr_bluedroid_test();
1154         break;*/
1155     default:
1156         ret = bt_test_bluedroid();//bt_test_bluez();
1157         break;
1158     }
1159 #endif
1160 #else  //Linux pcba test
1161     int rst;
1162     char result_filename[100] = {0};
1163 
1164     printf("======================start bt test=============================\n");
1165     rst = run_test_item_cmd("echo_auto_test echo_bt_test");
1166 
1167     if(rst == 0) {
1168         snprintf(result_filename, sizeof(result_filename),
1169                 "%s/echo_bt_test_result", "/tmp");
1170         ret = parse_test_result(result_filename, "bt_test", NULL);
1171 
1172     }else  {
1173         return NULL;
1174     }
1175 
1176 #endif
1177 
1178     if (ret==0) {
1179         ui_print_xy_rgba(0,tc_info->y,0,255,0,255,"%s:[%s]\n",
1180                         PCBA_BLUETOOTH,PCBA_SECCESS);
1181 		tc_info->result = 0;
1182     }
1183     else {
1184         ui_print_xy_rgba(0,tc_info->y,255,0,0,255,"%s:[%s]\n",
1185                         PCBA_BLUETOOTH,PCBA_FAILED);
1186 		tc_info->result = -1;
1187     }
1188 	return 0;
1189 }
1190 
1191