xref: /OK3568_Linux_fs/external/rk_pcba_test/echo_bt_test.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /*
2  *  bluetooth_test.c  --  bluetooth test application
3  *
4  *  Copyright (c) 2017 Rockchip Electronics Co. Ltd.
5  *  Author: Panzhenzhuan Wang <randy.wang@rock-chips.com>
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * 	 http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <malloc.h>
23 
24 //*open ��������ͷ�ļ�
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <fcntl.h>
28 #include <unistd.h>
29 
30 //error ���ͷ�ļ�
31 #include <errno.h>
32 #include "bt_test.h"
33 
34 //_IOW ����ͷ�ļ�
35 #include <sys/ioctl.h>
36 //WIFEXITED ����ͷ�ļ�
37 #include <sys/wait.h>
38 
39 #define LOG_TAG "bt_test"
40 #include "common.h"
41 
42 //*����ѭ����ȡ������ַʱ�����Լ���ʱʱ��
43 #define PERIOD_TIME 1
44 #define BT_TIMEOUT_FIRMWARE 7
45 
46 #define BT_PROC_ERR -30
47 #define BT_FIRMWARE_LOAD_ERR -31
48 
49 #define WIFI_CHIP_TYPE_PATH "/sys/class/rkwifi/chip"
50 #define BT_ON 1
51 #define BT_OFF 0
52 
53 #define BT_INIT_ADDR "00:00:00:00:00:00"
54 
55 #ifndef HCI_DEV_ID
56 #define HCI_DEV_ID 0
57 #endif
58 
59 enum WIFI_CHIP_TYPE_LIST{
60     BT_UNKNOWN = -1,
61 	BCM4329 = 0,
62 	RTL8188CU,
63 	RTL8188EU,
64 	BCM4330,
65 	RK901,
66 	RK903,
67 	MT6620,
68 	RT5370,
69     MT5931,
70     RDA587x,
71     RDA5990,
72     RTK8723AS,
73     RTK8723BS,
74     RTK8723AU,
75     RTK8723BU,
76     BK3515,
77     SOFIA_3GR,
78 };
79 
80 static int rfkill_bt_id = -1;
81 static char rfkill_state_path[128];
82 static int bluetooth_power_status = 0;
83 static int chip_type;
84 
85 //* 1����ʼ��rfkill����ȡBluetooth��Ӧid
init_rfkill()86 static int init_rfkill() {
87     char path[64];
88     char buf[16];
89     int fd;
90     int length_r;
91     int id;
92     printf("======================init rfkill start==================\n\n");
93     for (id = 0; ; id++) {
94         sprintf(path, "/sys/class/rfkill/rfkill%d/type", id);
95         fd = open(path, O_RDONLY);
96         if (fd < 0) {
97             printf("open(%s) failed: %s (%d)\n", path, strerror(errno), errno);
98             printf("======================init rfkill failed==================\n\n");
99             return -1;
100         }
101         length_r = read(fd, &buf, sizeof(buf));
102         close(fd);
103         if (length_r >= 9 && memcmp(buf, "bluetooth", 9) == 0) {
104             rfkill_bt_id = id;
105             break;
106         }
107     }
108     printf("\t Bluetooth initialize id is :%d\n",rfkill_bt_id);
109     sprintf(rfkill_state_path, "/sys/class/rfkill/rfkill%d/state", rfkill_bt_id);
110     printf("======================init rfkill success==================\n\n");
111     return 0;
112 }
113 
114 //* 2����ȡBluetoothоƬ����
bt_get_chip_name(char * name,int len)115 int bt_get_chip_name(char* name, int len)
116 {
117     int fd = -1;
118     int length_r = 0;
119     char rfkill_name_path[128];
120     int ret = -1;
121 
122     printf("================bt_get_chip_name start================\n\n");
123     sprintf(rfkill_name_path, "/sys/class/rfkill/rfkill%d/name", rfkill_bt_id);
124     fd = open(rfkill_name_path, O_RDONLY|O_NOCTTY|O_NDELAY,0);
125     if (fd < 0) {
126         printf("open(%s) failed: %s (%d)", rfkill_name_path, strerror(errno),errno);
127         goto out;
128     }
129 
130     length_r = read(fd, name, len);
131     if (length_r < 0) {
132         printf("read(%s) failed: %s (%d)", rfkill_name_path, strerror(errno),errno);
133         goto out;
134     }
135     name[length_r-1] = '\0';
136     close(fd);
137     printf("================bt_get_chip_name success================\n");
138     return 0;
139 out:
140     if (fd >= 0) close(fd);
141     printf("================bt_get_chip_name failed================\n");
142     return -1;
143 }
144 
145 //* 3����ȡоƬ����
get_chip_type(char * bt_chip_name)146 static int get_chip_type(char *bt_chip_name)
147 
148 {
149     int chip_type;
150     char chip_type_name[64];
151     memset(chip_type_name,0,sizeof(chip_type_name));
152     printf("================bluetooth get_chip_type start================\n\n");
153     if(!memcmp(bt_chip_name, "rk903",strlen("rk903"))) {
154 		chip_type = RK903;
155 		memcpy(chip_type_name,"RK903",strlen("RK903"));
156 	} else if(!memcmp(bt_chip_name, "mt6622",strlen("mt6622"))) {
157 		chip_type = MT5931;
158 		memcpy(chip_type_name,"MT5931",strlen("MT5931"));
159 	} else if(!memcmp(bt_chip_name, "rda587x",strlen("rda587x"))) {
160 		chip_type = RDA587x;
161 		memcpy(chip_type_name,"RDA587x",strlen("RDA587x"));
162 	} else if(!memcmp(bt_chip_name, "rda5990",strlen("rda5990"))) {
163 		chip_type = RDA5990;
164 		memcpy(chip_type_name,"RDA5990",strlen("RDA5990"));
165 	} else if(!memcmp(bt_chip_name, "rtk8723as",strlen("rtk8723as"))) {
166 		chip_type = RTK8723AS;
167 		memcpy(chip_type_name,"RTK8723AS",strlen("RTK8723AS"));
168 	} else if(!memcmp(bt_chip_name, "rtk8723bs",strlen("rtk8723bs"))) {
169 		chip_type = RTK8723BS;
170 		memcpy(chip_type_name,"RTK8723BS",strlen("RTK8723BS"));
171 	} else if(!memcmp(bt_chip_name, "rtk8723au",strlen("rtk8723au"))) {
172 		chip_type = RTK8723AU;
173 		memcpy(chip_type_name,"RTK8723AU",strlen("RTK8723AU"));
174 	} else if(!memcmp(bt_chip_name, "rtk8723bu",strlen("rtk8723bu"))) {
175 		chip_type = RTK8723BU;
176 		memcpy(chip_type_name,"RTK8723BU",strlen("RTK8723BU"));
177     } else if(!memcmp(bt_chip_name, "bk3515",strlen("bk3515"))) {
178         chip_type = BK3515;
179         memcpy(chip_type_name,"BK3515",strlen("BK3515"));
180 	} else if(!memcmp(bt_chip_name, "Sofia-3gr",strlen("Sofia-3gr"))){
181         chip_type = SOFIA_3GR;
182         memcpy(chip_type_name,"SOFIA_3GR",strlen("SOFIA_3GR"));
183 	}
184     else if (!memcmp(bt_chip_name, "rk903_26M",strlen("rk903_26M"))){
185         chip_type = RK903;
186         memcpy(chip_type_name,"RK903",strlen("RK903"));
187     }
188     else if (!memcmp(bt_chip_name, "rk903",strlen("rk903")))
189         {
190         chip_type = RK903;
191         memcpy(chip_type_name,"RK903",strlen("RK903"));
192     }
193     else if (!memcmp(bt_chip_name, "ap6210",strlen("ap6210"))){
194         chip_type = RK903;
195         memcpy(chip_type_name,"RK903",strlen("RK903"));
196     }
197     else if (!memcmp(bt_chip_name, "ap6335",strlen("ap6330"))){
198         chip_type = RK903;
199         memcpy(chip_type_name,"RK903",strlen("RK903"));
200     }
201     else if (!memcmp(bt_chip_name, "ap6476",strlen("ap6476"))){
202         chip_type = RK903;
203         memcpy(chip_type_name,"RK903",strlen("RK903"));
204     }
205     else if (!memcmp(bt_chip_name, "ap6493",strlen("ap6493"))){
206         chip_type = RK903;
207         memcpy(chip_type_name,"RK903",strlen("RK903"));
208     }
209     else {
210         printf("Not support BT chip, skip bt test.\n");
211         chip_type = BT_UNKNOWN;
212         memcpy(chip_type_name,"BT_UNKNOWN",strlen("BT_UNKNOWN"));
213     }
214     printf("chip type is: %s\n", chip_type_name);
215     printf("================bluetooth get_chip_type finish================\n");
216     return chip_type;
217 }
218 
219 //* 4->1���رպ�̨brcm_patchram_plus1����
close_brcm_pathcram_plus1(void)220 int close_brcm_pathcram_plus1(void)
221 {
222     int test_flag = -1;
223     char pid_buf[64];
224     char cmd[64];
225     FILE* pp;
226     printf("====================function : %s start =================\n",__func__);
227     pp = popen("ps |grep brcm_patchram_plus1|awk 'NR==1 {print $1}'","r");
228     //����ļ���ʧ�ܣ������������Ϣ
229     if (!pp)
230     {
231         printf("%s popen err%s\n",__func__,strerror(errno));
232         return -1;
233     }
234     fgets(pid_buf,sizeof(pid_buf),pp);
235     pclose(pp);
236     printf("Get pid_buf is: \t %s\n",pid_buf);
237     sprintf(cmd,"kill -9 %d",atoi(pid_buf));
238     printf("cmd is: %s\n",cmd);
239     system(cmd);
240     printf("====================function : %s finish =================\n",__func__);
241     return 0;
242 }
243 
244 //* 4->2->1����ȡhci0��������ַ,ȷ�Ϲ̼��Ƿ���سɹ����ɹ��õ�������ַ��ʧ������������ַ
get_bdaddr_test(void)245 int get_bdaddr_test(void)
246 {
247     int test_flag =-1;
248     char bdaddr[64];
249     char cmd[64];
250     int count =0;
251     printf("*******************function:%s start******************\n",__func__);
252     FILE* pp = popen("hciconfig -a| grep \"BD Address:\"|awk '{print $3}'","r");
253     //����ļ���ʧ�ܣ������������Ϣ
254     if (!pp)
255     {
256         printf("%s popen err%s\n",__func__,strerror(errno));
257         return -1;
258     }
259     memset(bdaddr,0,sizeof(bdaddr));
260     fscanf(pp,"%s",bdaddr);
261     pclose(pp);
262     if(bdaddr[0]!='\0')
263     {
264        printf("Get bluetooth device address is: \t  %s\n",bdaddr);
265        test_flag = 0;
266     }
267     else
268     {
269         //printf("Failed to load bluetooth firmware.\n");
270         printf("loadind bluetooth firmware.....\n");
271         test_flag = -1;
272     }
273     if(!memcmp(bdaddr,BT_INIT_ADDR,strlen(BT_INIT_ADDR)))
274     {
275         printf("hci0 interface create failed\n");
276         test_flag = -1;
277     }
278     printf("*******************function:%s finish******************\n",__func__);
279     return test_flag;
280 }
281 
282 //* 4->2->2��ѭ����ȡhci0��������ַ,ȷ�Ϲ̼��Ƿ���سɹ��������30s�ڻ�ȡ������ַ���̼����سɹ�������ʧ��
confirm_firmware_test(void)283 static int confirm_firmware_test(void)
284 {
285     int period = PERIOD_TIME;
286     int time_out = 0;
287     int test_flag = 0;
288 
289     printf("*******************function:%s start******************\n",__func__);
290     while(time_out < BT_TIMEOUT_FIRMWARE)
291     {
292 
293         //ÿ���̶�ʱ���ȡһ��������ַ
294         sleep(period);
295         if(!get_bdaddr_test()){
296                 break;
297         }
298         time_out +=PERIOD_TIME;
299     }
300     if(time_out > BT_TIMEOUT_FIRMWARE)
301     {
302         printf("Failed to load bluetooth firmware.\n");
303         test_flag = -1;
304     }
305     printf("*******************function:%s finish******************\n",__func__);
306     return test_flag;
307 }
308 
309 //* 4->3��ʹ��hciconfig hci0 up ����������Ȼ��ʹ��hcitool dev�鿴�Ƿ���
activate_bt_test(void)310 int activate_bt_test(void)
311 {
312     int test_flag = -1;
313     char bdaddr[64];
314     char cmd[64];
315     FILE* pp = NULL;
316 
317     printf("*******************function:%s start******************\n",__func__);
318     system("hciconfig hci0 up");
319     pp = popen("hcitool dev|grep hci0|awk '{print $2}'","r");
320     //����ļ���ʧ�ܣ������������Ϣ
321     if (!pp)
322     {
323         printf("%s popen err%s\n",__func__,strerror(errno));
324         return -1;
325     }
326     memset(bdaddr,0,sizeof(bdaddr));
327     fscanf(pp,"%s",bdaddr);
328     pclose(pp);
329     if(bdaddr[0]!='\0')
330     {
331         printf("Get bluetooth device address is :\t  %s\n",bdaddr);
332         test_flag = 0;
333     }
334     else{
335         printf("hci0 activates failed.\n");
336         test_flag = -1;
337     }
338     printf("*******************function:%s finish******************\n",__func__);
339     return test_flag;
340 }
341 
342 //* 4->4��ɨ����Χ�������豸����ȡԶ�������豸��ַ,����ɨ��õ���Զ���豸����
bt_scan_test(void)343 int bt_scan_test(void)
344 {
345     int test_flag = -1;
346     char bdaddr[64];
347     char cmd[64];
348     int count =0;
349     FILE* pp;
350     printf("--------------------function: %s start-------------------\n",__func__);
351     pp = popen("hcitool scan |awk 'NR>=2{print $1}'","r");
352     //����ļ���ʧ�ܣ������������Ϣ
353     if (!pp)
354     {
355         printf("%s popen err%s\n",__func__,strerror(errno));
356         return -1;
357     }
358     while(!feof(pp))
359     {
360         memset(bdaddr,0,sizeof(bdaddr));
361         fscanf(pp,"%s",bdaddr);
362         if((bdaddr[0]!='\0'))
363         {
364             printf("Get remote Bluetooth device address is : \t  %s\n",bdaddr);
365             count++;
366         }
367         else if(!count)
368         {
369             printf("Failed to scan remote bluetooth.\n");
370             return -1;
371         }
372     }
373     pclose(pp);
374     printf("--------------------function: %s success-------------------\n",__func__);
375     return count;
376 }
377 
378 
379 //* 4����������������
bt_test_bluez(void)380 int bt_test_bluez(void)
381 {
382     int test_flag = -1;
383     char cmd[128];
384     int dev_cnt = 0;
385     int status;
386     //* 1) �ر����������ȹرպ�̨brcm_patchram_plus1���̣�Ȼ��������µ�
387     test_flag = close_brcm_pathcram_plus1();
388     if(test_flag<0)
389         goto out;
390     sprintf(cmd,"echo 0 > %s",rfkill_state_path);
391     system(cmd);
392 
393 
394     //* 2) ȷ�������رպ����¸������ϵ磬���ع̼�
395     sprintf(cmd,"echo 1 > %s",rfkill_state_path);
396     system(cmd);
397     //status = system("brcm_patchram_plus1 --enable_hci --no2bytes \
398     //       --use_baudrate_for_download  --tosleep  200000 \
399     //       --baudrate 1500000 --patchram /data/bcm4339a0.hcd /dev/ttyS1 &");
400 #ifdef PCBA_3308
401     status = system("brcm_patchram_plus1 --enable_hci --no2bytes \
402            --use_baudrate_for_download  --tosleep  200000 \
403            --baudrate 1500000 --patchram /system/etc/firmware/BCM4345C0.hcd /dev/ttyS4 &");
404 #endif
405 
406 #ifdef PCBA_PX3SE
407     status = system("rtlbt  -n -s 115200 /dev/ttyS1 rtk_h5 > /data/cfg/hciattach.txt  2>&1 &");
408 #endif
409 #ifdef PCBA_3229GVA
410     status = system("brcm_patchram_plus1 --enable_hci --no2bytes \
411            --use_baudrate_for_download  --tosleep  200000 \
412            --baudrate 1500000 --patchram /data/bcm4339a0.hcd /dev/ttyS1 &");
413 #endif
414     test_flag = confirm_firmware_test();
415     if(test_flag < 0)
416         goto out;
417 
418     //* 3) ����������Ȼ��ʹ��hcitool dev�鿴�Ƿ���
419     system("hciconfig hci0 up");
420     test_flag = activate_bt_test();
421     if(test_flag < 0)
422         goto out;
423 
424     //* 4) ɨ����Χ�������豸,��ӡ���豸��ַ
425 //    dev_cnt = bt_scan_test();
426 //    if(dev_cnt < 0)
427 //        goto out;
428     printf("Get remote Bluetooth device number is: %d\n",dev_cnt);
429     printf("==============function: %s success ================\n",__func__);
430     return 0;
431 out:
432     printf("==============function: %s failed ================\n",__func__);
433     return -1;
434 }
435 
436 //* Bluetooth test function
bt_test(void * argv)437 void *bt_test(void *argv)
438 {
439     int bt_init_result,bt_setPower_result;
440     char cmd[64];
441     char bt_chip_name[64];
442     int bt_chip_type;
443     int ret = -1;
444     printf("======================Bluetooth test start================\n\n");
445     //sprintf(cmd,"aplay %s/bt_test_start.wav",AUDIO_PATH);
446     //system(cmd);
447     //system("aplay /data/cfg/test/bt_test_start.wav");
448     //1����ʼ��rfkill����ȡBluetooth��Ӧid
449     ret = init_rfkill();
450     if(ret < 0){
451 		printf("function: %s failed! %s\n",__func__,strerror(errno));
452 		goto fail;
453 	}
454     printf("Bluetooth is /sys/class/rfkill/rfkill:%d\n",rfkill_bt_id);
455 
456     //2����ȡBluetoothоƬ���֣�chip name
457     ret = bt_get_chip_name(bt_chip_name,sizeof(bt_chip_name));
458     if(ret < 0){
459 		printf("function: %s failed! %s\n",__func__,strerror(errno));
460 		goto fail;
461 	}
462 	printf("Bluetooth chip name is %s\n",bt_chip_name);
463 
464     //3����ȡоƬ���͡�chip type
465     bt_chip_type = get_chip_type(bt_chip_name);
466     if(bt_chip_type < 0){
467 		printf("function: %s failed! %s\n",__func__,strerror(errno));
468 		goto fail;
469 	}
470     printf("Bluetooth chip type is: bt_chip_type = %d\n", bt_chip_type);
471 
472     //4������Bluetooth������
473      ret = bt_test_bluez();
474      if(ret < 0){
475         printf("bluetooth_test main function: %s failed.\n",__func__);
476 		goto fail;
477      }
478     printf("======================Bluetooth test success================\n");
479     system("busybox killall brcm_patchram_plus1");      //�رչ̼����س���
480     return (void*)ret;
481 fail:
482     printf("======================Bluetooth test failed================\n");
483     system("busybox killall brcm_patchram_plus1");      //�رչ̼����س���
484     return (void*)ret;
485 }
486 
487 //*���������
main(int argc,char * argv[])488 int main(int argc, char *argv[])
489 {
490     int test_flag = 0,err_code = 0;
491     char buf[COMMAND_VALUESIZE] = "bt_test";
492     char result[COMMAND_VALUESIZE] = RESULT_PASS;
493     test_flag = (int)bt_test(argv[0]);
494     if(test_flag < 0)
495     {
496         strcpy(result,RESULT_FAIL);
497         err_code = BT_PROC_ERR;
498         printf("===err_code = %d===\n",err_code);
499     }
500     if (err_code != 0)
501         strcpy(result, RESULT_FAIL);
502     send_msg_to_server(buf, result, err_code);
503 }
504 
505 
506